{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://contextosai.com/schemas/decision-record.v1.schema.json",
  "title": "DecisionRecord v1",
  "type": "object",
  "required": ["record_id", "decision_key", "decision_version", "timestamp", "status", "actor", "evidence_refs", "policy_decisions", "approvals", "controls_active", "budget_usage", "lineage", "trace_id"],
  "$defs": {
    "ApprovalMode": {
      "type": "string",
      "enum": ["read_only", "local_write", "network", "delegated", "destructive"]
    }
  },
  "properties": {
    "record_id": { "type": "string" },
    "decision_key": { "type": "string" },
    "decision_version": { "type": "string" },
    "timestamp": { "type": "string", "format": "date-time" },
    "status": { "type": "string", "enum": ["DECIDED", "DEFERRED", "REJECTED", "ESCALATED", "IN_FLIGHT", "CLOSED"] },
    "actor": {
      "type": "object",
      "required": ["type", "id"],
      "properties": {
        "type": { "type": "string", "enum": ["USER", "AGENT", "SYSTEM"] },
        "id": { "type": "string" }
      },
      "additionalProperties": true
    },
    "intent_ref": { "type": "string" },
    "context_snapshot_hash": { "type": "string" },
    "subject_ids": { "type": "array", "items": { "type": "string" } },
    "inputs_refs": { "type": "object", "additionalProperties": { "type": "string" } },
    "outputs": { "type": "object", "additionalProperties": true },
    "evidence_refs": { "type": "array", "items": { "type": "string" } },
    "policy_decisions": {
      "type": "array",
      "items": {
        "type": "object",
        "required": ["policy_decision_id", "rule_ids"],
        "properties": {
          "policy_decision_id": { "type": "string" },
          "bundle_id": { "type": "string" },
          "rule_ids": { "type": "array", "items": { "type": "string" } },
          "verdict": { "type": "string", "enum": ["allow", "deny", "require_approval", "escalate"] }
        },
        "additionalProperties": false
      }
    },
    "approvals": {
      "type": "array",
      "items": {
        "type": "object",
        "required": ["gate_id", "approver", "approval_mode_effective", "evidence_snapshot_hash", "decided_at"],
        "properties": {
          "gate_id": { "type": "string" },
          "approver": { "type": "string" },
          "approval_mode_effective": { "$ref": "#/$defs/ApprovalMode" },
          "evidence_snapshot_hash": { "type": "string" },
          "decided_at": { "type": "string", "format": "date-time" }
        },
        "additionalProperties": false
      }
    },
    "controls_active": {
      "type": "object",
      "required": ["must_refuse", "must_escalate", "approval_gates_active", "redaction_rules_active"],
      "properties": {
        "must_refuse": { "type": "array", "items": { "type": "string" } },
        "must_escalate": { "type": "array", "items": { "type": "string" } },
        "approval_gates_active": { "type": "array", "items": { "type": "string" } },
        "redaction_rules_active": { "type": "array", "items": { "type": "string" } }
      },
      "additionalProperties": false
    },
    "tool_lineage": { "type": "array", "items": { "type": "object", "additionalProperties": true } },
    "scorecard": {
      "type": "object",
      "required": ["policy", "utility", "safety"],
      "properties": {
        "policy": { "type": "number", "minimum": 0, "maximum": 1 },
        "utility": { "type": "number", "minimum": 0, "maximum": 1 },
        "latency": { "type": "number", "minimum": 0 },
        "safety": { "type": "number", "minimum": 0, "maximum": 1 },
        "cost": { "type": "number", "minimum": 0 }
      },
      "additionalProperties": false
    },
    "confidence": { "type": "number", "minimum": 0, "maximum": 1 },
    "rationale": { "type": "object", "additionalProperties": true },
    "budget_usage": {
      "type": "object",
      "required": ["tokens", "tool_calls", "cost_usd_cents", "wall_clock_ms"],
      "properties": {
        "tokens": { "type": "integer", "minimum": 0 },
        "tool_calls": { "type": "integer", "minimum": 0 },
        "cost_usd_cents": { "type": "number", "minimum": 0 },
        "wall_clock_ms": { "type": "integer", "minimum": 0 }
      },
      "additionalProperties": false
    },
    "lineage": {
      "type": "object",
      "required": ["pack_version"],
      "properties": {
        "pack_version": { "type": "string" },
        "snapshot_version": { "type": "string" },
        "policy_versions": { "type": "array", "items": { "type": "string" } },
        "decision_spec": { "type": "string" },
        "kg_snapshot": { "type": "string" },
        "model_profile": { "type": "string" },
        "evaluator_suite": { "type": "string" },
        "routing_decision_id": { "type": "string" }
      },
      "additionalProperties": false
    },
    "replay": { "type": "object", "additionalProperties": true },
    "audit": { "type": "object", "additionalProperties": true },
    "trace_id": { "type": "string" }
  },
  "additionalProperties": false
}
