{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://open-coder-ai.github.io/chock/schemas/v0/subagent.schema.json",
  "title": "ChockSubagentManifest",
  "description": "Agent-agnostic subagent definition. Subagents are single-purpose, bounded, and invoked by a skill or workflow skill.",
  "type": "object",
  "required": ["id", "name", "version", "description", "artifact", "provenance", "lifecycle", "security", "effects"],
  "additionalProperties": false,
  "properties": {
    "id": { "type": "string", "pattern": "^[a-z][a-z0-9_-]{2,63}$" },
    "name": { "type": "string", "maxLength": 128 },
    "version": { "type": "string", "pattern": "^\\d+\\.\\d+\\.\\d+(-[a-zA-Z0-9.]+)?$" },
    "description": { "type": "string", "maxLength": 1024 },
    "artifact": { "type": "string", "const": "subagent" },
    "enforcement": {
      "type": "string",
      "enum": ["advise", "verify", "block"],
      "description": "Position on the enforcement spectrum for this subagent when acting as a policy."
    },
    "purpose": { "type": "string", "maxLength": 500 },
    "effects": {
      "type": "array",
      "items": {
        "type": "string",
        "enum": ["none", "read_only", "writes_workspace", "writes_external", "irreversible", "reads_private", "network"]
      },
      "description": "Declared side effects of executing this subagent. Any non-none effect requires approval wiring."
    },
    "approval": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "required": { "type": "boolean", "default": false },
        "mode": { "type": "string", "enum": ["human", "auto"], "default": "human" },
        "justification": { "type": "string", "maxLength": 500 }
      }
    },
    "runtime": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "max_retries": { "type": "integer", "minimum": 0, "maximum": 5, "default": 2 },
        "max_identical_invocations": { "type": "integer", "minimum": 1, "maximum": 10, "default": 3 }
      }
    },
    "input_schema": {
      "type": "object",
      "required": ["type", "properties", "additionalProperties"],
      "properties": {
        "type": { "type": "string", "const": "object" },
        "properties": {
          "type": "object",
          "additionalProperties": {
            "type": "object",
            "required": ["type", "description"],
            "properties": {
              "type": { "type": "string" },
              "description": { "type": "string", "minLength": 1 },
              "format": { "type": "string", "enum": ["workspace-path"] }
            }
          }
        },
        "required": { "type": "array", "items": { "type": "string" } },
        "additionalProperties": { "type": "boolean", "const": false }
      }
    },
    "output_schema": {
      "type": "object",
      "required": ["type", "properties"],
      "properties": {
        "type": { "type": "string", "const": "object" },
        "properties": { "type": "object" },
        "required": { "type": "array", "items": { "type": "string" } }
      }
    },
    "bounded_to": {
      "type": "array",
      "items": { "type": "string" },
      "description": "Tool/file scopes this subagent is allowed to touch."
    },
    "delegates": {
      "type": "object",
      "additionalProperties": false,
      "required": ["skill"],
      "properties": {
        "skill": { "type": "string", "minLength": 1 }
      }
    },
    "execution_scope": {
      "type": "object",
      "additionalProperties": false,
      "required": ["input_key", "isolation"],
      "properties": {
        "input_key": { "type": "string", "minLength": 1 },
        "repository_key": { "type": "string", "minLength": 1 },
        "allowed_effects": { "type": "array", "items": { "type": "string" } },
        "isolation": {
          "type": "object",
          "additionalProperties": false,
          "required": ["required", "boundary"],
          "properties": {
            "required": { "type": "boolean", "const": true },
            "boundary": { "type": "string", "enum": ["assigned_work_item", "assigned_repository", "workspace"] }
          }
        }
      }
    },
    "max_iterations": { "type": "integer", "minimum": 1, "maximum": 50 },
    "provenance": {
      "type": "object",
      "required": ["author", "created_at", "license", "trust_tier"],
      "properties": {
        "author": { "type": "string" },
        "author_email": { "type": "string", "format": "email" },
        "created_at": { "type": "string", "format": "date-time" },
        "updated_at": { "type": "string", "format": "date-time" },
        "source_repo": { "type": "string", "format": "uri" },
        "license": { "type": "string", "enum": ["Apache-2.0", "MIT", "BSD-3-Clause", "proprietary"] },
        "trust_tier": { "type": "string", "enum": ["sandbox", "community", "verified", "certified"] },
        "signature": { "type": "string" }
      }
    },
    "lifecycle": {
      "type": "object",
      "required": ["status"],
      "properties": {
        "status": { "type": "string", "enum": ["draft", "review", "staging", "production", "deprecated", "retired"] },
        "reviewed_by": { "type": "array", "items": { "type": "string" } },
        "deprecated_at": { "type": "string", "format": "date-time" },
        "replacement_id": { "type": "string" }
      }
    },
    "optimization": {
      "type": "object",
      "properties": {
        "optimizable": { "type": "boolean", "default": true },
        "learning_rate_budget": { "type": "number", "minimum": 0.0, "maximum": 1.0, "default": 0.1 },
        "frozen_sections": { "type": "array", "items": { "type": "string" } },
        "validation_gate": { "type": "string", "enum": ["strict", "standard", "permissive"], "default": "standard" },
        "min_eval_score": { "type": "number", "minimum": 0.0, "maximum": 1.0, "default": 0.8 }
      }
    },
    "evaluation": {
      "type": "object",
      "properties": {
        "test_suite_ref": { "type": "string" },
        "primary_metric": { "type": "string", "default": "pass_rate" },
        "metrics": {
          "type": "object",
          "additionalProperties": {
            "type": "object",
            "required": ["threshold", "direction"],
            "properties": {
              "weight": { "type": "number" },
              "threshold": { "type": "number" },
              "direction": { "type": "string", "enum": ["higher_is_better", "lower_is_better"] }
            }
          }
        }
      }
    },
    "dependencies": {
      "type": "object",
      "properties": {
        "skills": {
          "type": "array",
          "items": {
            "type": "object",
            "required": ["id"],
            "properties": {
              "id": { "type": "string" },
              "version_constraint": { "type": "string" },
              "optional": { "type": "boolean", "default": false }
            }
          }
        },
        "services": {
          "type": "array",
          "items": {
            "type": "object",
            "required": ["name", "type"],
            "properties": {
              "name": { "type": "string" },
              "type": { "type": "string" },
              "required": { "type": "boolean", "default": true }
            }
          }
        },
        "models": {
          "type": "array",
          "items": {
            "type": "object",
            "required": ["id"],
            "properties": {
              "id": { "type": "string" },
              "min_context_tokens": { "type": "integer" },
              "required": { "type": "boolean", "default": false }
            }
          }
        },
        "tools": {
          "type": "object",
          "properties": {
            "allowed": { "type": "array", "items": { "type": "string" } },
            "forbidden": { "type": "array", "items": { "type": "string" } }
          }
        },
        "mcp_servers": { "type": "array", "items": { "type": "string" } }
      }
    },
    "security": {
      "type": "object",
      "properties": {
        "content_instructions": { "type": "string", "enum": ["never-obey"], "default": "never-obey" },
        "prompt_injection_defense": { "type": "string", "enum": ["strict", "standard", "disabled"], "default": "standard" },
        "input_sanitization": { "type": "boolean", "default": true },
        "output_validation": { "type": "boolean", "default": true },
        "pii_handling": { "type": "string", "enum": ["block", "redact", "allow"], "default": "redact" },
        "data_residency": { "type": "string", "default": "global" },
        "audit_logging": { "type": "boolean", "default": true }
      }
    },
    "tags": {
      "type": "array",
      "items": { "type": "string", "maxLength": 64 },
      "maxItems": 20
    },
    "changelog": {
      "type": "array",
      "items": {
        "type": "object",
        "required": ["version", "date", "changes"],
        "properties": {
          "version": { "type": "string" },
          "date": { "type": "string", "format": "date" },
          "changes": { "type": "array", "items": { "type": "string" } },
          "breaking": { "type": "boolean", "default": false }
        }
      }
    }
  }
}
