{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "Cortex Agent Task",
  "type": "object",
  "required": [
    "task_id",
    "title",
    "description",
    "status",
    "stage",
    "priority",
    "acceptance_criteria",
    "dependencies",
    "subtasks",
    "required_artifacts",
    "artifacts",
    "gates",
    "created_at",
    "updated_at"
  ],
  "properties": {
    "task_id": { "type": "string", "minLength": 1 },
    "title": { "type": "string", "minLength": 1 },
    "description": { "type": "string", "minLength": 1 },
    "status": { "type": "string", "enum": ["draft", "active", "blocked", "completed", "canceled"] },
    "stage": { "type": "string", "enum": ["draft", "spec", "plan", "implement", "validate", "review", "done"] },
    "priority": { "type": "string", "enum": ["P0", "P1", "P2", "P3"] },
    "owner": { "type": ["string", "null"] },
    "collaborators": { "type": "array", "items": { "type": "string" }, "uniqueItems": true },
    "acceptance_criteria": { "type": "array", "minItems": 1, "items": { "type": "string", "minLength": 1 } },
    "dependencies": { "type": "array", "items": { "type": "string" }, "uniqueItems": true },
    "subtasks": { "type": "array", "items": { "type": "string" }, "uniqueItems": true },
    "source_refs": { "type": "array", "items": { "type": "string" }, "uniqueItems": true },
    "validation_commands": { "type": "array", "items": { "type": "string", "minLength": 1 } },
    "architecture_required": { "type": "boolean", "default": false },
    "required_artifacts": {
      "type": "array",
      "items": {
        "type": "object",
        "required": ["kind", "required_for_stage"],
        "properties": {
          "kind": { "$ref": "#/$defs/artifactKind" },
          "required_for_stage": { "$ref": "#/$defs/stage" },
          "conditional": { "type": "boolean", "default": false },
          "condition": { "type": "string" }
        },
        "additionalProperties": false
      }
    },
    "artifacts": {
      "type": "array",
      "items": {
        "type": "object",
        "required": ["kind", "ref", "status"],
        "properties": {
          "kind": { "$ref": "#/$defs/artifactKind" },
          "ref": { "type": "string", "minLength": 1 },
          "status": { "type": "string", "enum": ["draft", "final", "superseded"] },
          "producer": { "type": ["string", "null"] },
          "produced_at": { "type": ["string", "null"], "format": "date-time" },
          "summary": { "type": "string" }
        },
        "additionalProperties": false
      }
    },
    "gates": {
      "type": "array",
      "items": {
        "type": "object",
        "required": ["from_stage", "to_stage", "status", "required_artifacts", "evidence_refs"],
        "properties": {
          "from_stage": { "$ref": "#/$defs/stage" },
          "to_stage": { "$ref": "#/$defs/stage" },
          "status": { "type": "string", "enum": ["pending", "passed", "blocked", "waived"] },
          "required_artifacts": { "type": "array", "items": { "$ref": "#/$defs/artifactKind" }, "uniqueItems": true },
          "evidence_refs": { "type": "array", "items": { "type": "string" }, "uniqueItems": true },
          "checked_by": { "type": ["string", "null"] },
          "checked_at": { "type": ["string", "null"], "format": "date-time" },
          "reason": { "type": "string" }
        },
        "additionalProperties": false
      }
    },
    "stage_history": {
      "type": "array",
      "items": {
        "type": "object",
        "required": ["stage", "entered_at"],
        "properties": {
          "stage": { "$ref": "#/$defs/stage" },
          "entered_at": { "type": "string", "format": "date-time" },
          "gate_evidence_refs": { "type": "array", "items": { "type": "string" } }
        },
        "additionalProperties": false
      }
    },
    "created_at": { "type": "string", "format": "date-time" },
    "updated_at": { "type": "string", "format": "date-time" }
  },
  "$defs": {
    "stage": { "type": "string", "enum": ["draft", "spec", "plan", "implement", "validate", "review", "done"] },
    "artifactKind": {
      "type": "string",
      "enum": ["spec", "architecture", "plan", "implementation", "validation", "review", "decision", "learning", "handoff", "release-note", "published-doc"]
    }
  },
  "additionalProperties": false
}
