{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://schemas.bunsen.dev/agent.v1.json",
  "title": "Bunsen Agent (v1)",
  "description": "Authoritative schema for agent.yaml v1. See docs/AGENT_YAML.md.",
  "type": "object",
  "required": ["version", "name", "install", "entrypoint", "interaction"],
  "properties": {
    "$schema": { "type": "string" },
    "version": { "const": "v1" },
    "name": { "type": "string", "pattern": "^[a-z0-9][a-z0-9-]*$" },
    "description": { "type": "string" },
    "install": { "$ref": "#/$defs/install" },
    "entrypoint": { "$ref": "#/$defs/entrypoint" },
    "interaction": { "$ref": "#/$defs/interaction" },
    "model": { "$ref": "#/$defs/model" },
    "defaults": { "$ref": "#/$defs/agentDefaults" },
    "examples": {
      "type": "array",
      "items": { "$ref": "#/$defs/agentExample" }
    },
    "variants": {
      "type": "object",
      "additionalProperties": { "$ref": "#/$defs/variant" }
    }
  },
  "additionalProperties": false,
  "$defs": {
    "duration": { "type": "string", "pattern": "^\\d+(?:\\.\\d+)?(?:ms|s|m|h)$" },
    "executionUser": { "type": "string", "enum": ["user", "root"] },

    "step": {
      "oneOf": [
        { "$ref": "#/$defs/runStep" },
        { "$ref": "#/$defs/writeFileStep" }
      ]
    },

    "runStep": {
      "type": "object",
      "required": ["run"],
      "properties": {
        "run": { "type": "string", "minLength": 1 },
        "as": { "$ref": "#/$defs/executionUser" },
        "timeout": { "$ref": "#/$defs/duration" }
      },
      "additionalProperties": false
    },

    "writeFileStep": {
      "type": "object",
      "required": ["writeFile"],
      "properties": {
        "writeFile": { "type": "string", "minLength": 1 },
        "from": { "type": "string", "minLength": 1 },
        "content": { "type": "string" },
        "as": { "$ref": "#/$defs/executionUser" },
        "timeout": { "$ref": "#/$defs/duration" }
      },
      "additionalProperties": false,
      "oneOf": [
        { "required": ["from"] },
        { "required": ["content"] }
      ]
    },

    "installSourceLocal": {
      "type": "object",
      "required": ["type"],
      "properties": {
        "type": { "const": "local" }
      },
      "additionalProperties": false
    },

    "installSourceGit": {
      "type": "object",
      "required": ["type", "repo"],
      "properties": {
        "type": { "const": "git" },
        "repo": { "type": "string", "minLength": 1 },
        "ref": { "type": "string", "minLength": 1 }
      },
      "additionalProperties": false
    },

    "installSourceNpm": {
      "type": "object",
      "required": ["type", "package"],
      "properties": {
        "type": { "const": "npm" },
        "package": { "type": "string", "minLength": 1 },
        "version": { "type": "string", "minLength": 1 }
      },
      "additionalProperties": false
    },

    "installSourceBinary": {
      "type": "object",
      "required": ["type", "url"],
      "properties": {
        "type": { "const": "binary" },
        "url": { "type": "string", "minLength": 1 },
        "sha256": { "type": "string", "pattern": "^[A-Fa-f0-9]{64}$" }
      },
      "additionalProperties": false
    },

    "installSource": {
      "oneOf": [
        { "$ref": "#/$defs/installSourceLocal" },
        { "$ref": "#/$defs/installSourceGit" },
        { "$ref": "#/$defs/installSourceNpm" },
        { "$ref": "#/$defs/installSourceBinary" }
      ]
    },

    "buildConfig": {
      "type": "object",
      "required": ["image", "run"],
      "properties": {
        "image": { "type": "string", "minLength": 1 },
        "network": { "type": "string", "enum": ["default", "none"] },
        "timeout": { "$ref": "#/$defs/duration" },
        "run": {
          "type": "array",
          "minItems": 1,
          "items": { "type": "string", "minLength": 1 }
        },
        "cacheSalt": { "type": "string" }
      },
      "additionalProperties": false
    },

    "install": {
      "type": "object",
      "required": ["source"],
      "properties": {
        "source": { "$ref": "#/$defs/installSource" },
        "deps": {
          "type": "array",
          "items": { "$ref": "#/$defs/agentDep" }
        },
        "build": { "$ref": "#/$defs/buildConfig" },
        "configure": {
          "type": "array",
          "items": { "$ref": "#/$defs/step" }
        }
      },
      "additionalProperties": false
    },

    "agentDepFileRef": {
      "type": "object",
      "required": ["file"],
      "properties": {
        "file": { "type": "string", "minLength": 1 }
      },
      "additionalProperties": false
    },

    "agentDepInstall": {
      "type": "object",
      "required": ["target", "run"],
      "properties": {
        "target": { "type": "string", "minLength": 1 },
        "image": { "type": "string", "minLength": 1 },
        "network": { "type": "string", "enum": ["default", "none"] },
        "timeout": { "$ref": "#/$defs/duration" },
        "run": {
          "type": "array",
          "minItems": 1,
          "items": { "type": "string", "minLength": 1 }
        }
      },
      "additionalProperties": false
    },

    "agentDepProvides": {
      "type": "object",
      "properties": {
        "binaries": {
          "type": "array",
          "items": { "type": "string", "minLength": 1 }
        }
      },
      "additionalProperties": false
    },

    "agentDepLinkage": {
      "type": "string",
      "enum": ["static", "closure", "dynamic"]
    },

    "agentDepAbi": {
      "type": "object",
      "required": ["libc"],
      "properties": {
        "libc": { "type": "string", "enum": ["glibc", "musl"] },
        "libc_version": { "type": "string", "minLength": 1 }
      },
      "additionalProperties": false
    },

    "agentDepLibraryRequirement": {
      "type": "object",
      "required": ["name"],
      "properties": {
        "name": { "type": "string", "minLength": 1 },
        "version": { "type": "string", "minLength": 1 }
      },
      "additionalProperties": false
    },

    "agentDepRequires": {
      "type": "object",
      "properties": {
        "libraries": {
          "type": "array",
          "items": { "$ref": "#/$defs/agentDepLibraryRequirement" }
        }
      },
      "additionalProperties": false
    },

    "agentDepSpec": {
      "type": "object",
      "required": ["name", "install"],
      "properties": {
        "name": { "type": "string", "pattern": "^[a-z0-9][a-z0-9-]*$" },
        "version": { "type": "string", "minLength": 1 },
        "description": { "type": "string" },
        "image": { "type": "string", "minLength": 1 },
        "linkage": { "$ref": "#/$defs/agentDepLinkage" },
        "abi": { "$ref": "#/$defs/agentDepAbi" },
        "requires": { "$ref": "#/$defs/agentDepRequires" },
        "provides": { "$ref": "#/$defs/agentDepProvides" },
        "install": {
          "type": "array",
          "minItems": 1,
          "items": { "$ref": "#/$defs/agentDepInstall" }
        }
      },
      "additionalProperties": false
    },

    "agentDep": {
      "oneOf": [
        { "$ref": "#/$defs/agentDepFileRef" },
        { "$ref": "#/$defs/agentDepSpec" }
      ]
    },

    "entrypoint": {
      "type": "object",
      "required": ["command"],
      "properties": {
        "command": { "type": "string", "minLength": 1 },
        "args": { "type": "array", "items": { "type": "string" } },
        "help": { "type": "string", "minLength": 1 }
      },
      "additionalProperties": false
    },

    "interaction": {
      "type": "object",
      "required": ["mode"],
      "properties": {
        "mode": { "type": "string", "enum": ["direct", "supervised"] }
      },
      "additionalProperties": false
    },

    "model": {
      "type": "object",
      "required": ["env"],
      "properties": {
        "env": { "type": "string", "pattern": "^[A-Za-z_][A-Za-z0-9_]*$" },
        "default": { "type": "string", "minLength": 1 }
      },
      "additionalProperties": false
    },

    "agentDefaults": {
      "type": "object",
      "properties": {
        "env": {
          "type": "object",
          "additionalProperties": { "type": "string" }
        },
        "passEnv": {
          "type": "array",
          "items": { "type": "string", "minLength": 1 }
        }
      },
      "additionalProperties": false
    },

    "agentExample": {
      "type": "object",
      "required": ["prompt", "invocation"],
      "properties": {
        "prompt": { "type": "string", "minLength": 1 },
        "invocation": { "type": "string", "minLength": 1 }
      },
      "additionalProperties": false
    },

    "variant": {
      "type": "object",
      "properties": {
        "description": { "type": "string" },
        "install": {
          "type": "object",
          "properties": {
            "source": { "$ref": "#/$defs/variantInstallSource" },
            "deps": {
              "type": "array",
              "items": { "$ref": "#/$defs/agentDep" }
            },
            "build": { "$ref": "#/$defs/buildConfig" },
            "configure": { "$ref": "#/$defs/variantConfigureSteps" }
          },
          "additionalProperties": false
        },
        "entrypoint": {
          "type": "object",
          "properties": {
            "command": { "type": "string", "minLength": 1 },
            "args": { "type": "array", "items": { "type": "string" } },
            "help": { "type": "string", "minLength": 1 }
          },
          "additionalProperties": false
        },
        "interaction": {
          "type": "object",
          "properties": {
            "mode": { "type": "string", "enum": ["direct", "supervised"] }
          },
          "additionalProperties": false
        },
        "defaults": { "$ref": "#/$defs/agentDefaults" }
      },
      "additionalProperties": false
    },

    "variantInstallSource": {
      "oneOf": [
        { "$ref": "#/$defs/installSource" },
        {
          "type": "object",
          "properties": {
            "ref": { "type": "string", "minLength": 1 },
            "version": { "type": "string", "minLength": 1 }
          },
          "additionalProperties": false
        }
      ]
    },

    "variantConfigureSteps": {
      "oneOf": [
        {
          "type": "array",
          "items": { "$ref": "#/$defs/step" }
        },
        {
          "type": "object",
          "required": ["items"],
          "properties": {
            "mergeMode": { "type": "string", "enum": ["append", "replace"] },
            "items": {
              "type": "array",
              "items": { "$ref": "#/$defs/step" }
            }
          },
          "additionalProperties": false
        }
      ]
    }
  }
}
