{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://cortex-agent.local/schemas/composite-workspace.schema.json",
  "title": "Cortex Agent Composite Workspace",
  "type": "object",
  "required": ["composite_workspace_id", "task_id", "branch_family", "status", "owner", "members", "merge_order", "created_at", "updated_at"],
  "properties": {
    "composite_workspace_id": { "type": "string", "pattern": "^CWS-[A-Za-z0-9][A-Za-z0-9._-]*$" },
    "task_id": { "type": "string", "minLength": 1 },
    "mission_id": { "type": ["string", "null"] },
    "branch_family": { "type": "string", "minLength": 1 },
    "status": { "type": "string", "enum": ["planned", "preparing", "running", "validating_members", "merge_ready", "merging", "validating_composite", "completed", "blocked", "recovering", "closed"] },
    "owner": {
      "type": "object",
      "required": ["agent_id"],
      "properties": {
        "agent_id": { "type": "string", "minLength": 1 },
        "session_id": { "type": ["string", "null"] },
        "run_id": { "type": ["string", "null"] }
      },
      "additionalProperties": false
    },
    "members": {
      "type": "array",
      "minItems": 2,
      "items": {
        "type": "object",
        "required": ["repository_id", "workspace_id", "branch", "base_commit", "status"],
        "properties": {
          "repository_id": { "type": "string", "minLength": 1 },
          "workspace_id": { "type": "string", "pattern": "^WS-" },
          "branch": { "type": "string", "minLength": 1 },
          "base_commit": { "type": "string", "minLength": 7 },
          "head_commit": { "type": ["string", "null"] },
          "status": { "type": "string", "enum": ["planned", "ready", "running", "validated", "merge_ready", "merged", "failed", "blocked", "closed"] },
          "validation_refs": { "type": "array", "items": { "type": "string" }, "uniqueItems": true }
        },
        "additionalProperties": false
      }
    },
    "merge_order": { "type": "array", "items": { "type": "string" }, "uniqueItems": true },
    "atomic_merge": { "const": false, "default": false },
    "recovery": {
      "type": "object",
      "required": ["strategy", "checkpoint_refs"],
      "properties": {
        "strategy": { "const": "ordered_checkpoints_with_compensation" },
        "failed_repository_id": { "type": ["string", "null"] },
        "next_repository_id": { "type": ["string", "null"] },
        "checkpoint_refs": { "type": "array", "items": { "type": "string" }, "uniqueItems": true },
        "next_action": { "type": ["string", "null"] }
      },
      "additionalProperties": false
    },
    "session_ids": { "type": "array", "items": { "type": "string" }, "uniqueItems": true },
    "run_ids": { "type": "array", "items": { "type": "string" }, "uniqueItems": true },
    "validation_refs": { "type": "array", "items": { "type": "string" }, "uniqueItems": true },
    "created_at": { "type": "string", "format": "date-time" },
    "updated_at": { "type": "string", "format": "date-time" },
    "closed_at": { "type": ["string", "null"], "format": "date-time" }
  },
  "additionalProperties": false
}
