{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://contextosai.com/schemas/compiled-context.v1.schema.json",
  "title": "CompiledContext v1",
  "type": "object",
  "required": ["compiled_prompt", "manifests", "runtime_controls", "budget_report", "context_ledger"],
  "$defs": {
    "ContextBucket": {
      "type": "string",
      "enum": ["business", "policy", "tool", "evidence", "memory", "session"]
    },
    "ContextBlock": {
      "type": "object",
      "required": ["block_id", "bucket", "content", "priority"],
      "properties": {
        "block_id": { "type": "string" },
        "bucket": { "$ref": "#/$defs/ContextBucket" },
        "content": { "type": "string" },
        "priority": { "type": "integer" },
        "provenance_refs": { "type": "array", "items": { "type": "string" } },
        "truncated": { "type": "boolean" }
      },
      "additionalProperties": false
    },
    "PolicyManifestEntry": {
      "type": "object",
      "required": ["bundle_id", "rule_ids"],
      "properties": {
        "bundle_id": { "type": "string" },
        "rule_ids": { "type": "array", "items": { "type": "string" } }
      },
      "additionalProperties": false
    },
    "ToolManifestEntry": {
      "type": "object",
      "required": ["adapter_id", "capabilities"],
      "properties": {
        "adapter_id": { "type": "string" },
        "capabilities": { "type": "array", "items": { "type": "string" } },
        "constraints": { "type": "object", "additionalProperties": true },
        "capability_metadata": { "type": "object", "additionalProperties": { "type": "object", "additionalProperties": true } }
      },
      "additionalProperties": false
    },
    "EvidenceManifestEntry": {
      "type": "object",
      "required": ["evidence_ref"],
      "properties": {
        "evidence_ref": { "type": "string" },
        "source_id": { "type": "string" },
        "hash": { "type": "string" }
      },
      "additionalProperties": false
    }
  },
  "properties": {
    "compiled_prompt": {
      "type": "object",
      "required": ["system", "developer", "task", "context_blocks"],
      "properties": {
        "system": { "type": "string" },
        "developer": { "type": "string" },
        "task": { "type": "string" },
        "context_blocks": { "type": "array", "items": { "$ref": "#/$defs/ContextBlock" } }
      },
      "additionalProperties": false
    },
    "manifests": {
      "type": "object",
      "required": ["policy_manifest", "tool_manifest", "evidence_manifest"],
      "properties": {
        "policy_manifest": { "type": "array", "items": { "$ref": "#/$defs/PolicyManifestEntry" } },
        "tool_manifest": { "type": "array", "items": { "$ref": "#/$defs/ToolManifestEntry" } },
        "evidence_manifest": { "type": "array", "items": { "$ref": "#/$defs/EvidenceManifestEntry" } }
      },
      "additionalProperties": false
    },
    "runtime_controls": {
      "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
    },
    "budget_report": {
      "type": "object",
      "required": ["tokens_allocated", "tokens_used_at_compile", "tokens_used_by_bucket", "bucket_truncations", "dropped_block_ids", "warnings"],
      "properties": {
        "tokens_allocated": { "type": "object", "additionalProperties": { "type": "integer", "minimum": 0 } },
        "tokens_used_at_compile": { "type": "integer", "minimum": 0 },
        "tokens_used_by_bucket": { "type": "object", "additionalProperties": { "type": "integer", "minimum": 0 } },
        "bucket_truncations": { "type": "object", "additionalProperties": { "type": "boolean" } },
        "dropped_block_ids": { "type": "object", "additionalProperties": { "type": "array", "items": { "type": "string" } } },
        "warnings": { "type": "array", "items": { "type": "string" } }
      },
      "additionalProperties": false
    },
    "context_ledger": {
      "type": "object",
      "required": ["pack_ref", "intent", "request_id", "policy_bundles", "policy_rule_ids", "tools", "evidence_refs", "memory_refs", "dropped_block_ids", "budget", "compiled_context_hash"],
      "properties": {
        "pack_ref": { "type": "string" },
        "intent": { "type": "string" },
        "request_id": { "type": "string" },
        "session_id": { "type": "string" },
        "policy_bundles": { "type": "array", "items": { "type": "string" } },
        "policy_rule_ids": { "type": "array", "items": { "type": "string" } },
        "tools": { "type": "array", "items": { "type": "string" } },
        "evidence_refs": { "type": "array", "items": { "type": "string" } },
        "memory_refs": { "type": "array", "items": { "type": "string" } },
        "dropped_block_ids": { "type": "array", "items": { "type": "string" } },
        "budget": {
          "type": "object",
          "required": ["tokens_used_at_compile", "tokens_used_by_bucket", "truncated_buckets"],
          "properties": {
            "tokens_used_at_compile": { "type": "integer", "minimum": 0 },
            "tokens_used_by_bucket": { "type": "object", "additionalProperties": { "type": "integer", "minimum": 0 } },
            "truncated_buckets": { "type": "array", "items": { "$ref": "#/$defs/ContextBucket" } }
          },
          "additionalProperties": false
        },
        "compiled_context_hash": { "type": "string" }
      },
      "additionalProperties": false
    }
  },
  "additionalProperties": false
}
