{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://getzero.dev/contracts/zero.replay_graph_context.v1.schema.json",
  "title": "ZERO Public Replay Graph Context",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schema_version",
    "kind",
    "status",
    "generated_at",
    "canonical_url",
    "public",
    "replay_id",
    "operator_handle",
    "agent_handle",
    "anchor",
    "hash_coverage",
    "adjacency",
    "links",
    "privacy_boundary",
    "score_gate",
    "blockers"
  ],
  "properties": {
    "schema_version": { "const": "zero.replay_graph_context.v1" },
    "kind": { "const": "public_replay_graph_context" },
    "status": { "enum": ["graph_context_ready", "graph_context_degraded"] },
    "generated_at": { "type": "string", "format": "date-time" },
    "canonical_url": {
      "type": "string",
      "format": "uri",
      "pattern": "^https://getzero\\.dev/api/replays/.+/graph-context$"
    },
    "public": { "const": true },
    "replay_id": { "type": "string", "minLength": 1 },
    "operator_handle": { "type": "string", "minLength": 1 },
    "agent_handle": { "type": "string", "minLength": 1 },
    "anchor": { "$ref": "#/$defs/node" },
    "hash_coverage": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "prompt",
        "tools",
        "model",
        "policy",
        "signing_payload_hash",
        "journal_entry_hash",
        "journal_root_hash",
        "coverage_complete"
      ],
      "properties": {
        "prompt": {
          "type": "object",
          "additionalProperties": false,
          "required": ["status", "prompt_sha", "hash_public", "body_public"],
          "properties": {
            "status": { "enum": ["complete", "partial"] },
            "prompt_sha": { "type": "string", "minLength": 1 },
            "hash_public": { "type": "boolean" },
            "body_public": { "const": false }
          }
        },
        "tools": {
          "type": "object",
          "additionalProperties": false,
          "required": ["total", "complete", "items"],
          "properties": {
            "total": { "type": "integer", "minimum": 0 },
            "complete": { "type": "integer", "minimum": 0 },
            "items": {
              "type": "array",
              "items": {
                "type": "object",
                "additionalProperties": false,
                "required": ["name", "status", "evidence_hash"],
                "properties": {
                  "name": { "type": "string", "minLength": 1 },
                  "status": { "enum": ["complete", "partial"] },
                  "evidence_hash": { "type": "string", "pattern": "^[a-f0-9]{64}$" }
                }
              }
            }
          }
        },
        "model": {
          "type": "object",
          "additionalProperties": false,
          "required": ["status", "model_id", "runtime_build", "policy_version"],
          "properties": {
            "status": { "enum": ["complete", "partial"] },
            "model_id": { "type": "string", "minLength": 1 },
            "runtime_build": { "type": "string", "minLength": 1 },
            "policy_version": { "type": "string", "minLength": 1 }
          }
        },
        "policy": {
          "type": "object",
          "additionalProperties": false,
          "required": ["policy_version", "hash"],
          "properties": {
            "policy_version": { "type": "string", "minLength": 1 },
            "hash": { "type": "string", "pattern": "^[a-f0-9]{64}$" }
          }
        },
        "signing_payload_hash": { "type": ["string", "null"] },
        "journal_entry_hash": { "type": ["string", "null"] },
        "journal_root_hash": { "type": ["string", "null"] },
        "coverage_complete": { "type": "boolean" }
      }
    },
    "adjacency": {
      "type": "object",
      "additionalProperties": false,
      "required": ["previous", "next", "related", "edges"],
      "properties": {
        "previous": { "anyOf": [{ "$ref": "#/$defs/node" }, { "type": "null" }] },
        "next": { "anyOf": [{ "$ref": "#/$defs/node" }, { "type": "null" }] },
        "related": {
          "type": "array",
          "maxItems": 6,
          "items": { "$ref": "#/$defs/node" }
        },
        "edges": {
          "type": "array",
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["from", "to", "relation", "href"],
            "properties": {
              "from": { "type": "string", "minLength": 1 },
              "to": { "type": "string", "minLength": 1 },
              "relation": { "$ref": "#/$defs/relation" },
              "href": { "type": "string", "format": "uri" }
            }
          }
        }
      }
    },
    "links": {
      "type": "object",
      "additionalProperties": false,
      "required": ["replay", "json", "receipt_card", "share_image", "operator", "replay_index", "entrypoints", "schema"],
      "properties": {
        "replay": { "type": "string", "format": "uri" },
        "json": { "type": "string", "format": "uri" },
        "receipt_card": { "type": "string", "format": "uri" },
        "share_image": { "type": "string", "format": "uri" },
        "operator": { "type": "string", "format": "uri" },
        "replay_index": { "type": "string", "format": "uri" },
        "entrypoints": { "const": "https://getzero.dev/api/replays/entrypoints" },
        "schema": { "const": "https://getzero.dev/contracts/zero.replay_graph_context.v1.schema.json" }
      }
    },
    "privacy_boundary": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "public_only",
        "private_trace_payloads_public",
        "private_keys_public",
        "operator_ids_public",
        "prompt_bodies_public",
        "tool_payloads_public",
        "performance_metrics_public"
      ],
      "properties": {
        "public_only": { "const": true },
        "private_trace_payloads_public": { "const": false },
        "private_keys_public": { "const": false },
        "operator_ids_public": { "const": false },
        "prompt_bodies_public": { "const": false },
        "tool_payloads_public": { "const": false },
        "performance_metrics_public": { "const": false }
      }
    },
    "score_gate": {
      "type": "object",
      "additionalProperties": false,
      "required": ["can_promote_public_score", "reason", "next_blocker"],
      "properties": {
        "can_promote_public_score": { "type": "boolean" },
        "reason": { "type": "string", "minLength": 1 },
        "next_blocker": { "const": "recurring_public_decision_artifacts_missing" }
      }
    },
    "blockers": {
      "type": "array",
      "items": { "type": "string", "minLength": 1 }
    }
  },
  "$defs": {
    "relation": {
      "enum": [
        "anchor",
        "previous",
        "next",
        "same_operator",
        "same_agent",
        "same_outcome",
        "same_symbol",
        "nearest_refusal",
        "same_journal_root"
      ]
    },
    "node": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "replay_id",
        "href",
        "occurred_at",
        "outcome_kind",
        "relation",
        "summary",
        "journal_status",
        "audit_status",
        "hash_refs"
      ],
      "properties": {
        "replay_id": { "type": "string", "minLength": 1 },
        "href": { "type": "string", "format": "uri" },
        "occurred_at": { "type": "string", "format": "date-time" },
        "outcome_kind": { "enum": ["closed_trade", "heartbeat", "position", "refusal", "public_evidence"] },
        "relation": { "$ref": "#/$defs/relation" },
        "summary": { "type": "string", "minLength": 1 },
        "journal_status": { "enum": ["complete", "partial", "unavailable"] },
        "audit_status": { "enum": ["complete", "partial", "weak"] },
        "hash_refs": {
          "type": "object",
          "additionalProperties": false,
          "required": ["payload_hash", "journal_root_hash"],
          "properties": {
            "payload_hash": { "type": ["string", "null"] },
            "journal_root_hash": { "type": ["string", "null"] }
          }
        }
      }
    }
  }
}
