{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://getzero.dev/contracts/zero.runtime_capability_state.v1.schema.json",
  "title": "ZERO Runtime Capability State v1",
  "type": "object",
  "required": [
    "schema_version",
    "kind",
    "status",
    "identity",
    "trust",
    "mode",
    "live_ready",
    "builder_authority_ready",
    "live_blockers",
    "score_components",
    "authority",
    "builder",
    "safety",
    "data_plane",
    "account",
    "proof_refs",
    "next_action"
  ],
  "properties": {
    "schema_version": { "const": "zero.runtime_capability_state.v1" },
    "schema": { "const": "https://getzero.dev/contracts/zero.runtime_capability_state.v1.schema.json" },
    "kind": { "const": "runtime_capability_state" },
    "public": { "const": false },
    "generated_at": { "type": "string" },
    "status": { "enum": ["ready", "gated", "blocked", "degraded", "unavailable"] },
    "identity": {
      "type": "object",
      "required": ["deployment_id", "network", "venue"],
      "properties": {
        "deployment_id": { "type": ["string", "null"] },
        "operator_handle": { "type": ["string", "null"] },
        "network": { "enum": ["mainnet", "testnet"] },
        "venue": { "type": "string" }
      }
    },
    "trust": {
      "type": "object",
      "required": [
        "chain_schema",
        "runtime_claimed",
        "runtime_live_observed",
        "paper_capable",
        "authority_ready",
        "execution_authorized",
        "revenue_authority",
        "revenue_reconciled",
        "missing",
        "revenue_authority_missing",
        "revenue_reconciled_missing"
      ],
      "properties": {
        "chain_schema": { "const": "https://getzero.dev/contracts/zero.vibe_deploy.proof_chain.v1.schema.json" },
        "runtime_claimed": { "type": "boolean" },
        "runtime_live_observed": { "type": "boolean" },
        "paper_capable": { "type": "boolean" },
        "authority_ready": { "type": "boolean" },
        "execution_authorized": { "type": "boolean" },
        "revenue_authority": { "type": "boolean" },
        "revenue_reconciled": { "type": "boolean" },
        "missing": {
          "type": "array",
          "items": {
            "enum": [
              "claim_runtime",
              "observe_first_heartbeat",
              "run_paper_acceptance",
              "record_paper_acceptance",
              "approve_agent",
              "approve_builder_fee",
              "grant_short_live_lease"
            ]
          }
        },
        "revenue_authority_missing": {
          "type": "array",
          "items": {
            "enum": [
              "claim_runtime",
              "observe_first_heartbeat",
              "run_paper_acceptance",
              "record_paper_acceptance",
              "approve_agent",
              "approve_builder_fee",
              "grant_short_live_lease"
            ]
          }
        },
        "revenue_reconciled_missing": {
          "type": "array",
          "items": {
            "enum": [
              "claim_runtime",
              "observe_first_heartbeat",
              "run_paper_acceptance",
              "record_paper_acceptance",
              "approve_agent",
              "approve_builder_fee",
              "grant_short_live_lease",
              "builder_fill_reconciliation"
            ]
          }
        }
      }
    },
    "mode": { "enum": ["paper", "supervised", "live"] },
    "live_ready": { "type": "boolean" },
    "builder_authority_ready": { "type": "boolean" },
    "live_blockers": {
      "type": "array",
      "items": { "type": "string" }
    },
    "score_components": {
      "type": "object",
      "required": [
        "paper_capable",
        "live_ready",
        "proof_complete",
        "operational_hygiene",
        "public_verifiable",
        "raw_score",
        "honest_score",
        "max_score",
        "cap_reason"
      ],
      "properties": {
        "paper_capable": { "$ref": "#/$defs/score_component" },
        "live_ready": { "$ref": "#/$defs/score_component" },
        "proof_complete": { "$ref": "#/$defs/score_component" },
        "operational_hygiene": { "$ref": "#/$defs/score_component" },
        "public_verifiable": { "$ref": "#/$defs/score_component" },
        "raw_score": { "type": "number", "minimum": 0, "maximum": 99 },
        "honest_score": { "type": "number", "minimum": 0, "maximum": 99 },
        "max_score": { "const": 99 },
        "cap_reason": { "type": ["string", "null"] }
      }
    },
    "authority": {
      "type": "object",
      "required": [
        "agent_approved",
        "signer_ready",
        "lease",
        "allowed_live_actions",
        "risk_reducing_actions_allowed",
        "schedule_cancel_certified",
        "blocked_live_action_reasons"
      ],
      "properties": {
        "agent_approved": { "type": "boolean" },
        "approved_at": { "type": ["string", "null"] },
        "signer_ready": { "type": "boolean" },
        "signing_mode": { "type": "string" },
        "lease": {
          "type": "object",
          "required": ["state", "expires_at", "seconds_remaining"],
          "properties": {
            "state": { "enum": ["active", "expired", "missing"] },
            "expires_at": { "type": ["string", "null"] },
            "seconds_remaining": { "type": ["number", "null"] }
          }
        },
        "allowed_live_actions": {
          "type": "array",
          "items": { "enum": ["order", "cancel", "scheduleCancel"] }
        },
        "risk_reducing_actions_allowed": {
          "type": "array",
          "items": { "enum": ["scheduleCancel"] }
        },
        "schedule_cancel_certified": { "type": "boolean" },
        "blocked_live_action_reasons": {
          "type": "array",
          "items": {
            "enum": [
              "approve_agent_missing",
              "privy_signer_not_ready",
              "live_lease_missing",
              "live_lease_expired"
            ]
          }
        }
      }
    },
    "builder": {
      "type": "object",
      "required": ["configured", "approved", "active", "max_fee_tenth_bps", "disabled_reason"],
      "properties": {
        "configured": { "type": "boolean" },
        "approved": { "type": "boolean" },
        "active": { "type": "boolean" },
        "builder_address": { "type": ["string", "null"] },
        "requested_fee_tenth_bps": { "type": ["number", "null"] },
        "max_fee_tenth_bps": { "type": ["number", "null"] },
        "disabled_reason": { "type": ["string", "null"] },
        "approval_event": {
          "oneOf": [
            { "type": "null" },
            {
              "type": "object",
              "required": ["event_kind", "event_nonce", "occurred_at", "source", "network", "approval_state", "attribution_feature_enabled"],
              "properties": {
                "event_kind": { "const": "approve_builder_fee" },
                "event_nonce": { "type": ["number", "null"] },
                "occurred_at": { "type": ["string", "null"] },
                "source": { "type": "string" },
                "network": { "type": ["string", "null"] },
                "approval_state": {
                  "type": ["string", "null"],
                  "enum": ["approved", "revoked", "insufficient", null]
                },
                "attribution_feature_enabled": { "type": "boolean" }
              }
            }
          ]
        },
        "fill_reconciliation": {
          "oneOf": [
            { "type": "null" },
            {
              "type": "object",
              "required": [
                "event_kind",
                "event_nonce",
                "occurred_at",
                "source",
                "receipt_id",
                "builder_address",
                "user_address",
                "fills_count",
                "matched_fills_count",
                "total_builder_fee_usd",
                "total_fee_usd",
                "export_ref",
                "no_secret_material"
              ],
              "properties": {
                "event_kind": { "const": "builder_fill_reconciliation" },
                "event_nonce": { "type": ["number", "null"] },
                "occurred_at": { "type": ["string", "null"] },
                "source": { "type": "string" },
                "receipt_id": { "type": ["string", "null"] },
                "builder_address": { "type": ["string", "null"] },
                "user_address": { "type": ["string", "null"] },
                "fills_count": { "type": ["number", "null"] },
                "matched_fills_count": { "type": ["number", "null"] },
                "total_builder_fee_usd": { "type": ["number", "null"] },
                "total_fee_usd": { "type": ["number", "null"] },
                "export_ref": {
                  "oneOf": [
                    { "type": "null" },
                    {
                      "type": "object",
                      "required": ["source", "date", "url"],
                      "properties": {
                        "source": { "type": ["string", "null"] },
                        "date": { "type": ["string", "null"] },
                        "url": { "type": ["string", "null"] }
                      }
                    }
                  ]
                },
                "no_secret_material": { "type": "boolean" }
              }
            }
          ]
        }
      }
    },
    "safety": {
      "type": "object",
      "required": [
        "global_halt",
        "halt_reason",
        "circuit_breaker_active",
        "daily_loss_limit_breached",
        "capital_floor_breached",
        "immune_healthy",
        "intervention_required"
      ],
      "properties": {
        "global_halt": { "type": "boolean" },
        "halt_reason": { "type": ["string", "null"] },
        "circuit_breaker_active": { "type": ["boolean", "null"] },
        "daily_loss_limit_breached": { "type": ["boolean", "null"] },
        "capital_floor_breached": { "type": ["boolean", "null"] },
        "immune_healthy": { "type": ["boolean", "null"] },
        "intervention_required": { "type": "boolean" }
      }
    },
    "data_plane": {
      "type": "object",
      "required": [
        "source",
        "source_id",
        "source_kind",
        "checked_at",
        "account_state_age_ms",
        "open_orders_age_ms",
        "fills_age_ms",
        "book_age_ms",
        "heartbeat_age_ms",
        "ws_connected",
        "divergence_status",
        "fallback_used",
        "fallback_reason",
        "freshness_budgets_ms",
        "ages_ms",
        "stale_fields",
        "runtime_safety_status",
        "runtime_safety_reasons",
        "live_signing_policy",
        "refusal_reason"
      ],
      "properties": {
        "source": { "enum": ["zero_primary", "hl_public", "fallback", "unchecked"] },
        "source_id": { "type": ["string", "null"] },
        "source_kind": {
          "type": ["string", "null"],
          "enum": [
            "public_hl_api",
            "foundation_nonvalidating_node",
            "self_hosted_node_api",
            "managed_private_endpoint",
            null
          ]
        },
        "checked_at": { "type": ["string", "null"] },
        "account_state_age_ms": { "type": ["number", "null"], "minimum": 0 },
        "open_orders_age_ms": { "type": ["number", "null"], "minimum": 0 },
        "fills_age_ms": { "type": ["number", "null"], "minimum": 0 },
        "book_age_ms": { "type": ["number", "null"], "minimum": 0 },
        "heartbeat_age_ms": { "type": ["number", "null"], "minimum": 0 },
        "ws_connected": { "type": ["boolean", "null"] },
        "divergence_status": { "enum": ["match", "minor_drift", "major_drift", "unchecked"] },
        "fallback_used": { "type": "boolean" },
        "fallback_reason": { "type": ["string", "null"] },
        "freshness_budgets_ms": { "$ref": "#/$defs/data_plane_age_map" },
        "ages_ms": { "$ref": "#/$defs/data_plane_age_map" },
        "stale_fields": {
          "type": "array",
          "items": { "type": "string" }
        },
        "runtime_safety_status": { "enum": ["ok", "degraded", "halted", "unchecked"] },
        "runtime_safety_reasons": {
          "type": "array",
          "items": { "type": "string" }
        },
        "live_signing_policy": { "enum": ["allow", "degrade", "refuse", "unchecked"] },
        "refusal_reason": { "type": ["string", "null"] }
      }
    },
    "account": {
      "type": "object",
      "required": [
        "equity_usd",
        "open_positions",
        "open_exposure_usd",
        "open_orders",
        "daily_pnl_usd",
        "drawdown_pct"
      ],
      "properties": {
        "equity_usd": { "type": ["number", "null"] },
        "open_positions": { "type": ["integer", "null"], "minimum": 0 },
        "open_exposure_usd": { "type": ["number", "null"], "minimum": 0 },
        "open_orders": { "type": ["integer", "null"], "minimum": 0 },
        "daily_pnl_usd": { "type": ["number", "null"] },
        "drawdown_pct": { "type": ["number", "null"] }
      }
    },
    "proof_refs": {
      "type": "object",
      "required": [
        "latest_replay",
        "latest_journal_root",
        "public_chain_confirmed_journal_root",
        "raw_evidence_url",
        "proof_chain_url"
      ],
      "properties": {
        "latest_replay": {
          "oneOf": [
            { "type": "null" },
            {
              "type": "object",
              "required": ["replay_id", "href", "occurred_at", "source_type"],
              "properties": {
                "replay_id": { "type": "string" },
                "href": { "type": ["string", "null"] },
                "occurred_at": { "type": ["string", "null"] },
                "source_type": { "type": ["string", "null"] }
              }
            }
          ]
        },
        "latest_journal_root": {
          "oneOf": [
            { "type": "null" },
            {
              "type": "object",
              "required": ["root_hash", "root_url", "finality", "updated_at"],
              "properties": {
                "root_hash": { "type": ["string", "null"] },
                "root_url": { "type": ["string", "null"] },
                "finality": { "type": ["string", "null"] },
                "updated_at": { "type": ["string", "null"] }
              }
            }
          ]
        },
        "public_chain_confirmed_journal_root": {
          "oneOf": [
            { "type": "null" },
            {
              "type": "object",
              "required": ["root_hash", "root_url", "finality", "updated_at"],
              "properties": {
                "root_hash": { "type": ["string", "null"] },
                "root_url": { "type": ["string", "null"] },
                "finality": { "const": "public_chain_confirmed" },
                "updated_at": { "type": ["string", "null"] }
              }
            }
          ]
        },
        "raw_evidence_url": { "type": "string" },
        "proof_chain_url": { "type": "string" },
        "paper_acceptance": {
          "oneOf": [
            { "type": "null" },
            {
              "type": "object",
              "required": ["source", "event_kind", "event_nonce", "occurred_at"],
              "properties": {
                "source": { "enum": ["deployment_events", "decision_replay_frames"] },
                "event_kind": { "const": "paper_run_accepted" },
                "event_nonce": { "type": ["number", "null"] },
                "occurred_at": { "type": ["string", "null"] },
                "mode": { "type": ["string", "null"] },
                "trace_id": { "type": ["string", "null"] },
                "journal_entry_id": { "type": ["string", "null"] },
                "journal_entry_hash": { "type": ["string", "null"] },
                "audit_export_sha256": { "type": ["string", "null"] },
                "exchange_orders_submitted": { "type": ["boolean", "null"] },
                "replay_id": { "type": "string" },
                "href": { "type": ["string", "null"] },
                "source_type": { "type": ["string", "null"] }
              }
            }
          ]
        }
      }
    },
    "next_action": { "type": "string" },
    "degraded_reasons": { "type": "array", "items": { "type": "string" } }
  },
  "$defs": {
    "score_component": {
      "type": "object",
      "required": ["ready", "score", "max"],
      "properties": {
        "ready": { "type": "boolean" },
        "score": { "type": "number", "minimum": 0 },
        "max": { "type": "number", "minimum": 0 }
      }
    },
    "data_plane_age_map": {
      "type": "object",
      "required": ["account_state", "open_orders", "fills", "book"],
      "properties": {
        "account_state": { "type": ["number", "null"], "minimum": 0 },
        "open_orders": { "type": ["number", "null"], "minimum": 0 },
        "fills": { "type": ["number", "null"], "minimum": 0 },
        "book": { "type": ["number", "null"], "minimum": 0 }
      }
    }
  }
}
