{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://cortex-agent.local/schemas/hook-lifecycle.schema.json",
  "title": "Cortex Agent Workspace Hook Lifecycle",
  "type": "object",
  "required": ["hook_run_id", "workspace_id", "phase", "status", "owner", "policy", "attempt", "created_at", "updated_at"],
  "properties": {
    "hook_run_id": { "type": "string", "pattern": "^WH-[A-Za-z0-9][A-Za-z0-9._-]*$" },
    "workspace_id": { "type": "string", "pattern": "^WS-" },
    "phase": { "type": "string", "enum": ["setup", "run", "teardown"] },
    "status": { "type": "string", "enum": ["pending", "authorized", "running", "succeeded", "failed", "timed_out", "canceled", "compensating", "compensated"] },
    "owner": {
      "type": "object",
      "required": ["workflow", "agent_id"],
      "properties": {
        "workflow": { "type": "string", "minLength": 1 },
        "agent_id": { "type": "string", "minLength": 1 },
        "session_id": { "type": ["string", "null"] },
        "run_id": { "type": ["string", "null"] }
      },
      "additionalProperties": false
    },
    "command_ref": { "type": ["string", "null"], "description": "Reference to a project-owned command definition. Raw credentials are forbidden." },
    "policy": {
      "type": "object",
      "required": ["authorization", "timeout_seconds", "max_attempts", "redact_output", "external_side_effect"],
      "properties": {
        "authorization": { "type": "string", "enum": ["project_allowlist", "workflow_gate", "user_decision"] },
        "decision_id": { "type": ["string", "null"] },
        "timeout_seconds": { "type": "integer", "minimum": 1, "maximum": 86400 },
        "max_attempts": { "type": "integer", "minimum": 1, "maximum": 10 },
        "redact_output": { "const": true },
        "external_side_effect": { "type": "boolean" }
      },
      "allOf": [
        {
          "if": { "properties": { "authorization": { "const": "user_decision" } } },
          "then": { "required": ["decision_id"], "properties": { "decision_id": { "type": "string", "minLength": 1 } } }
        },
        {
          "if": { "properties": { "external_side_effect": { "const": true } } },
          "then": { "properties": { "authorization": { "const": "user_decision" }, "decision_id": { "type": "string", "minLength": 1 } } }
        }
      ],
      "additionalProperties": false
    },
    "attempt": { "type": "integer", "minimum": 1 },
    "idempotency_key": { "type": ["string", "null"] },
    "started_at": { "type": ["string", "null"], "format": "date-time" },
    "finished_at": { "type": ["string", "null"], "format": "date-time" },
    "exit_code": { "type": ["integer", "null"] },
    "failure_reason": { "type": ["string", "null"] },
    "compensation_hook_run_id": { "type": ["string", "null"] },
    "evidence_refs": { "type": "array", "items": { "type": "string" }, "uniqueItems": true },
    "created_at": { "type": "string", "format": "date-time" },
    "updated_at": { "type": "string", "format": "date-time" }
  },
  "allOf": [
    {
      "if": { "properties": { "status": { "enum": ["succeeded", "failed", "timed_out", "canceled", "compensated"] } } },
      "then": { "required": ["finished_at"] }
    }
  ],
  "additionalProperties": false
}
