{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://github.com/Kucell/cortex-agent/schemas/run.schema.json",
  "title": "Cortex Agent Run Journal",
  "description": "Mode-agnostic run journal. One run = one coherent execution: a plan, an implement, a validate, a merge, a handoff, etc. Mode-agnostic fields only; mode-specific telemetry lives in mode-specific extensions.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schema_version",
    "run_id",
    "kind",
    "status",
    "started_at"
  ],
  "properties": {
    "schema_version": { "const": 1 },
    "run_id": {
      "type": "string",
      "minLength": 1,
      "description": "Stable run id. Convention: R-{task_or_mission_slug}, e.g. R-M-001-MS-001, R-T-FAE-001."
    },
    "task_id": { "type": ["string", "null"] },
    "mission_id": { "type": ["string", "null"] },
    "agent_id": { "type": ["string", "null"] },
    "role": { "type": ["string", "null"] },
    "kind": {
      "type": "string",
      "enum": ["plan", "implement", "validate", "merge", "handoff", "dashboard", "publish", "memory", "conversation"],
      "description": "memory/conversation are general-mode-only kinds."
    },
    "status": {
      "type": "string",
      "enum": ["queued", "running", "completed", "failed", "canceled"]
    },
    "phase": {
      "type": ["string", "null"],
      "description": "Free-form phase label. Recognized values: initializing / briefing / planning / reading / editing / running_command / validating / handoff / merging / waiting / blocked / completed."
    },
    "activity": { "type": ["string", "null"] },
    "worktree_path": { "type": ["string", "null"] },
    "branch": { "type": ["string", "null"] },
    "base_commit": { "type": ["string", "null"] },
    "head_commit": { "type": ["string", "null"] },
    "started_at": { "type": "string", "format": "date-time" },
    "finished_at": { "type": ["string", "null"], "format": "date-time" },
    "updated_at": { "type": ["string", "null"], "format": "date-time" },
    "events": {
      "type": "array",
      "items": {
        "type": "object",
        "required": ["type", "at"],
        "properties": {
          "type": { "type": "string", "minLength": 1 },
          "phase": { "type": ["string", "null"] },
          "status": { "type": ["string", "null"] },
          "activity": { "type": ["string", "null"] },
          "message": { "type": ["string", "null"] },
          "at": { "type": "string", "format": "date-time" }
        },
        "additionalProperties": true
      }
    },
    "next_action": { "type": ["string", "null"] },
    "transcript_refs": {
      "type": "array",
      "description": "Cross-host transcript path references (audit-trail P-002). Append-only; dedupe by source+session_id. Path is absolute; sha256/byte_size/turn_count/first/last timestamps are host-pushed metadata. Framework never reads transcript content.",
      "items": {
        "type": "object",
        "required": ["source", "session_id", "path", "sha256", "byte_size", "turn_count", "linked_at"],
        "properties": {
          "source": { "type": "string", "enum": ["claude-code", "codex", "cursor", "pi", "other"] },
          "session_id": { "type": "string" },
          "path": { "type": "string" },
          "sha256": { "type": "string", "pattern": "^[a-f0-9]{64}$" },
          "byte_size": { "type": "integer", "minimum": 0 },
          "turn_count": { "type": "integer", "minimum": 0 },
          "first_turn_at": { "type": ["string", "null"] },
          "last_turn_at": { "type": ["string", "null"] },
          "linked_at": { "type": "string" }
        },
        "additionalProperties": false
      }
    }
  }
}