{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://workspai.dev/schemas/workspace-intelligence/goal-agent-handoff.v1.json",
  "title": "Workspai Goal Agent Handoff",
  "type": "object",
  "required": [
    "schemaVersion",
    "goalId",
    "goalFingerprint",
    "generatedAt",
    "consumer",
    "state",
    "objective",
    "scope",
    "discovery",
    "retrieval",
    "evidence",
    "guardrails",
    "workflow",
    "renewal"
  ],
  "properties": {
    "schemaVersion": { "const": "workspai.goal-agent-handoff.v1" },
    "goalId": { "type": "string", "pattern": "^[A-Za-z0-9][A-Za-z0-9._-]{7,95}$" },
    "goalFingerprint": { "$ref": "#/$defs/hash" },
    "generatedAt": { "type": "string", "format": "date-time" },
    "consumer": { "enum": ["generic", "claude", "codex"] },
    "state": { "enum": ["ready-to-plan", "needs-confirmation", "needs-evidence", "blocked"] },
    "objective": { "type": "string", "minLength": 1, "maxLength": 2000 },
    "scope": {
      "type": "object",
      "required": ["kind", "projects", "selectionSource"],
      "properties": {
        "kind": { "enum": ["workspace", "project", "project-set"] },
        "projects": {
          "type": "array",
          "minItems": 1,
          "items": { "type": "string", "minLength": 1 },
          "uniqueItems": true
        },
        "selectionSource": {
          "enum": [
            "workspace",
            "single-project-workspace",
            "invocation-project",
            "explicit",
            "interactive"
          ]
        },
        "resolution": { "enum": ["selected", "selection-required"] }
      },
      "allOf": [
        {
          "if": { "properties": { "kind": { "const": "project" } } },
          "then": { "properties": { "projects": { "type": "array", "maxItems": 1 } } }
        },
        {
          "if": { "properties": { "kind": { "const": "project-set" } } },
          "then": { "properties": { "projects": { "type": "array", "minItems": 2 } } }
        },
        {
          "if": {
            "properties": { "resolution": { "const": "selection-required" } },
            "required": ["resolution"]
          },
          "then": {
            "properties": {
              "kind": { "const": "workspace" },
              "selectionSource": { "const": "workspace" }
            }
          }
        }
      ],
      "additionalProperties": false
    },
    "evidence": {
      "type": "array",
      "minItems": 3,
      "items": {
        "type": "object",
        "required": ["role", "artifact", "binding"],
        "properties": {
          "role": { "enum": ["model", "graph", "goal"] },
          "artifact": { "type": "string", "pattern": "^\\.workspai/" },
          "binding": {
            "type": "object",
            "required": ["algorithm", "semantics", "value"],
            "properties": {
              "algorithm": { "const": "sha256" },
              "semantics": { "enum": ["workspace-model-structural-v1", "canonical-json-v1"] },
              "value": { "$ref": "#/$defs/hash" }
            },
            "additionalProperties": false
          }
        },
        "additionalProperties": false
      }
    },
    "discovery": {
      "type": "object",
      "required": ["index", "statusCommand", "requiredReads"],
      "properties": {
        "index": { "const": ".workspai/goals/index.json" },
        "statusCommand": { "type": "string", "pattern": "^workspai goal --status " },
        "requiredReads": {
          "type": "array",
          "minItems": 3,
          "items": { "type": "string", "pattern": "^\\.workspai/" },
          "uniqueItems": true
        }
      },
      "additionalProperties": false
    },
    "retrieval": {
      "type": "object",
      "required": ["status", "strategy", "queries", "anchors"],
      "properties": {
        "status": { "enum": ["grounded", "partial", "empty"] },
        "strategy": { "const": "deterministic-category-v1" },
        "queries": {
          "type": "array",
          "minItems": 1,
          "maxItems": 3,
          "items": { "type": "string", "minLength": 1 },
          "uniqueItems": true
        },
        "anchors": {
          "type": "array",
          "maxItems": 20,
          "items": {
            "type": "object",
            "required": ["entityId", "kind", "label", "proofIds"],
            "properties": {
              "entityId": { "type": "string" },
              "kind": { "type": "string" },
              "label": { "type": "string" },
              "proofIds": { "type": "array", "items": { "type": "string" }, "uniqueItems": true }
            },
            "additionalProperties": false
          }
        }
      },
      "additionalProperties": false
    },
    "guardrails": {
      "type": "array",
      "minItems": 5,
      "items": { "type": "string", "minLength": 1 },
      "uniqueItems": true
    },
    "workflow": {
      "type": "array",
      "minItems": 5,
      "items": {
        "type": "object",
        "required": ["order", "owner", "instruction"],
        "properties": {
          "order": { "type": "integer", "minimum": 1 },
          "owner": { "enum": ["workspai-cli", "agent", "human"] },
          "instruction": { "type": "string", "minLength": 1 }
        },
        "additionalProperties": false
      }
    },
    "renewal": {
      "type": "object",
      "required": ["command", "reason"],
      "properties": {
        "command": { "type": "string", "pattern": "^workspai goal " },
        "reason": { "type": "string", "minLength": 1 }
      },
      "additionalProperties": false
    }
  },
  "$defs": { "hash": { "type": "string", "pattern": "^[a-f0-9]{64}$" } },
  "additionalProperties": false
}
