{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://knowz.io/schemas/efficiency-event.schema.json",
  "title": "KnowzCode Efficiency Event v1",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schema",
    "event",
    "observed_at",
    "task_corpus_id",
    "provider",
    "runtime",
    "model",
    "profile",
    "mode",
    "reason_codes",
    "logical",
    "billed",
    "outcome"
  ],
  "properties": {
    "schema": { "const": "knowzcode.efficiency-event/v1" },
    "event": {
      "enum": [
        "dispatch_decision",
        "worker_usage",
        "worker_completed",
        "verification_run",
        "vault_operation",
        "budget_transition",
        "gate_outcome",
        "lineage_invalidated"
      ]
    },
    "observed_at": { "type": "string", "format": "date-time" },
    "task_corpus_id": {
      "anyOf": [
        { "type": "string", "minLength": 1, "maxLength": 69, "pattern": "^(?:anon-[a-f0-9]{16,64}|(?:small-tier2|backend-refactor|ui-integration|security-compliance|recovery-invalidation)-[0-9]{2})$" },
        { "type": "null" }
      ]
    },
    "provider": { "enum": ["claude", "codex", "other"] },
    "runtime": {
      "anyOf": [
        { "enum": ["cli", "mcp", "api", "desktop", "plugin", "unknown"] },
        { "type": "null" }
      ]
    },
    "model": {
      "anyOf": [
        { "enum": ["unknown", "opus", "sonnet", "haiku", "fable", "gpt-5", "gpt-5.6-sol", "gpt-5.6-terra"] },
        { "type": "string", "pattern": "^anon-model-[a-f0-9]{16,64}$" },
        { "type": "null" }
      ]
    },
    "profile": {
      "anyOf": [
        { "enum": ["quality", "balanced", "economy", "latency"] },
        { "type": "null" }
      ]
    },
    "mode": {
      "anyOf": [
        {
          "enum": [
            "local",
            "resume",
            "inherit-full",
            "inherit-recent",
            "fresh-capsule",
            "coordinated-team"
          ]
        },
        { "type": "null" }
      ]
    },
    "reason_codes": {
      "type": "array",
      "items": {
        "enum": [
          "LOCAL_CHEAPER",
          "BLOCKING",
          "RESUME_COMPATIBLE",
          "HIGH_CONTEXT_AFFINITY",
          "BOUNDED_RECENT_CONTEXT",
          "INDEPENDENT_CAPSULE",
          "SENSITIVITY_ISOLATION",
          "REVIEW_INDEPENDENCE",
            "TEAM_COORDINATION_REQUIRED",
            "WRITER_OWNERSHIP_CONFLICT",
            "NESTING_LIMIT",
            "CAPABILITY_FALLBACK"
        ]
      },
      "uniqueItems": true
    },
    "logical": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "estimated_context_occupancy",
        "compaction_count",
        "repeated_file_reads",
        "prompt_bytes",
        "tool_output_bytes",
        "capsule_bytes"
      ],
      "properties": {
        "estimated_context_occupancy": { "type": ["number", "null"], "minimum": 0 },
        "compaction_count": { "type": ["integer", "null"], "minimum": 0 },
        "repeated_file_reads": { "type": ["integer", "null"], "minimum": 0 },
        "prompt_bytes": { "type": ["integer", "null"], "minimum": 0 },
        "tool_output_bytes": { "type": ["integer", "null"], "minimum": 0 },
        "capsule_bytes": { "type": ["integer", "null"], "minimum": 0 }
      }
    },
    "billed": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "uncached_input_tokens",
        "cache_creation_input_tokens",
        "cache_read_input_tokens",
        "output_tokens",
        "amount",
        "currency",
        "units",
        "accounting_source"
      ],
      "properties": {
        "uncached_input_tokens": { "type": ["integer", "null"], "minimum": 0 },
        "cache_creation_input_tokens": { "type": ["integer", "null"], "minimum": 0 },
        "cache_read_input_tokens": { "type": ["integer", "null"], "minimum": 0 },
        "output_tokens": { "type": ["integer", "null"], "minimum": 0 },
        "amount": { "type": ["number", "null"], "minimum": 0 },
        "currency": {
          "anyOf": [
            { "type": "string", "pattern": "^[A-Z]{3}$" },
            { "type": "null" }
          ]
        },
        "units": { "type": ["number", "null"], "minimum": 0 },
        "accounting_source": {
          "enum": ["authoritative", "provider-reported", "estimated", "unknown"]
        }
      }
    },
    "outcome": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "accepted_workgroup",
        "verify_passed",
        "verify_total",
        "audit_score",
        "rework_rounds",
        "escaped_high_critical",
        "elapsed_ms"
      ],
      "properties": {
        "accepted_workgroup": { "type": ["boolean", "null"] },
        "verify_passed": { "type": ["integer", "null"], "minimum": 0 },
        "verify_total": { "type": ["integer", "null"], "minimum": 0 },
        "audit_score": { "type": ["number", "null"], "minimum": 0 },
        "rework_rounds": { "type": ["integer", "null"], "minimum": 0 },
        "escaped_high_critical": { "type": ["integer", "null"], "minimum": 0 },
        "elapsed_ms": { "type": ["integer", "null"], "minimum": 0 }
      }
    }
  }
}
