{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://www.deixei.com/aes/v0.1.0/audit_evidence_schema.json",
  "title": "Audit Evidence Schema (Predicate)",
  "description": "The normative JSON Schema for the nine-field record every agent action must answer. This schema describes the in-toto Statement *predicate*; the outer in-toto v1 Statement and Sigstore signature are not redefined here. The prose definition lives in Vol 1 Appendix B of 'Building Organizations for the Age of AI'.",

  "type": "object",
  "required": [
    "schema_version",
    "record_id",
    "session_id",
    "kind",
    "initiator",
    "executing_agent",
    "envelope_snapshot",
    "action",
    "data",
    "model",
    "approval",
    "time",
    "outcome"
  ],
  "additionalProperties": false,

  "properties": {
    "schema_version": {
      "type": "string",
      "pattern": "^\\d+\\.\\d+\\.\\d+$",
      "description": "Semantic version of the AES this record conforms to."
    },
    "record_id": {
      "type": "string",
      "minLength": 1,
      "description": "Unique identifier for this record (ULID or UUID recommended)."
    },
    "session_id": {
      "type": "string",
      "minLength": 1,
      "description": "Identifier grouping related records into one work unit."
    },
    "change_id": {
      "type": ["string", "null"],
      "description": "Optional identifier of a proposed change this record relates to."
    },
    "kind": {
      "type": "string",
      "pattern": "^[a-z][a-z0-9_]*\\.[a-z][a-z0-9_]*$",
      "description": "Dotted kind discriminator (e.g., 'agent.action', 'ops.run', 'opslog.decision', 'cab.evaluate', 'deployer.apply', 'chk.advise')."
    },
    "payload": {
      "type": "object",
      "description": "Optional kind-specific extension. Untyped at the AES level. May be constrained by parameters_schema_ref."
    },
    "parameters_schema_ref": {
      "type": "string",
      "format": "uri",
      "description": "Optional URL pointing to a kind-specific JSON Schema that constrains action.parameters, outcome.output, and payload."
    },

    "initiator":         { "$ref": "#/$defs/initiator" },
    "executing_agent":   { "$ref": "#/$defs/executingAgent" },
    "envelope_snapshot": { "$ref": "#/$defs/envelopeSnapshot" },
    "action":            { "$ref": "#/$defs/action" },
    "data":              { "type": "array", "items": { "$ref": "#/$defs/dataEntry" } },
    "model":             { "type": "array", "items": { "$ref": "#/$defs/modelEntry" } },
    "approval":          { "$ref": "#/$defs/approval" },
    "time":              { "$ref": "#/$defs/time" },
    "outcome":           { "$ref": "#/$defs/outcome" }
  },

  "$defs": {

    "initiator": {
      "description": "Q1 — Who initiated the action? Discriminated union on type.",
      "type": "object",
      "required": ["type"],
      "oneOf": [
        { "$ref": "#/$defs/initiatorHuman" },
        { "$ref": "#/$defs/initiatorAgent" },
        { "$ref": "#/$defs/initiatorScheduled" }
      ]
    },
    "initiatorHuman": {
      "type": "object",
      "required": ["type", "user_upn", "channel", "session_start"],
      "additionalProperties": false,
      "properties": {
        "type":         { "const": "human" },
        "user_upn":     { "type": "string", "minLength": 1 },
        "channel":      { "type": "string", "minLength": 1 },
        "session_ref":  { "type": "string" },
        "on_behalf_of": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "is_delegation":   { "type": "boolean" },
            "access_package":  { "type": "string" }
          }
        },
        "session_start": { "type": "string", "format": "date-time" }
      }
    },
    "initiatorAgent": {
      "type": "object",
      "required": ["type", "upstream_agent_id"],
      "additionalProperties": false,
      "properties": {
        "type":               { "const": "agent" },
        "upstream_agent_id":  { "type": "string" },
        "chain":              { "type": "array", "items": { "type": "string" } }
      }
    },
    "initiatorScheduled": {
      "type": "object",
      "required": ["type", "schedule_id", "trigger_event"],
      "additionalProperties": false,
      "properties": {
        "type":                  { "const": "scheduled" },
        "schedule_id":           { "type": "string" },
        "trigger_event":         { "type": "string" },
        "scheduler_record_id":   { "type": "string" }
      }
    },

    "executingAgent": {
      "description": "Q2 — Which agent executed it?",
      "type": "object",
      "required": ["agent_id", "agent_version", "deployment_environment", "zone"],
      "additionalProperties": false,
      "properties": {
        "agent_id":                       { "type": "string", "minLength": 1 },
        "agent_id_claim":                 { "type": "string" },
        "agent_version":                  { "type": "string", "minLength": 1 },
        "system_prompt_version":          { "type": "string" },
        "system_prompt_sha256":           { "type": "string", "pattern": "^[a-f0-9]{64}$" },
        "deployment_environment":         { "type": "string", "minLength": 1 },
        "control_plane_registry_record":  { "type": "string" },
        "zone": {
          "type": "string",
          "enum": ["development", "diagnostic", "remediation", "privileged-incident"]
        }
      }
    },

    "envelopeSnapshot": {
      "description": "Q3 — Under what envelope (Mandate/Means/Memory/Monitoring) at action time?",
      "type": "object",
      "required": ["registry_version", "mandate_digest", "means", "memory", "monitoring"],
      "additionalProperties": false,
      "properties": {
        "registry_version": { "type": "string", "minLength": 1 },
        "mandate_digest":   { "type": "string", "minLength": 1 },
        "means": {
          "type": "object",
          "required": ["tools"],
          "additionalProperties": false,
          "properties": {
            "tools": {
              "type": "array",
              "items": {
                "type": "object",
                "required": ["name", "version"],
                "additionalProperties": false,
                "properties": {
                  "name":    { "type": "string" },
                  "version": { "type": "string" }
                }
              }
            }
          }
        },
        "memory": {
          "type": "object",
          "required": ["read_scopes", "write_scopes"],
          "additionalProperties": false,
          "properties": {
            "read_scopes":  { "type": "array", "items": { "type": "string" } },
            "write_scopes": { "type": "array", "items": { "type": "string" } },
            "retention":    { "type": "string" }
          }
        },
        "monitoring": {
          "type": "object",
          "required": ["workspace", "rule_set", "evaluation_cadence"],
          "additionalProperties": false,
          "properties": {
            "workspace":          { "type": "string" },
            "rule_set":           { "type": "string" },
            "evaluation_cadence": { "type": "string" }
          }
        }
      }
    },

    "action": {
      "description": "Q4 — What action was performed? Verb + target + parameters.",
      "type": "object",
      "required": ["verb", "target", "parameters"],
      "additionalProperties": false,
      "properties": {
        "verb": { "type": "string", "minLength": 1 },
        "target": {
          "type": "object",
          "required": ["entity", "id"],
          "additionalProperties": false,
          "properties": {
            "entity":      { "type": "string" },
            "id":          { "type": "string" },
            "label":       { "type": "string" },
            "context_ref": { "type": "string" }
          }
        },
        "parameters": {
          "type": "object",
          "description": "Untyped at AES level; constrained by parameters_schema_ref if present."
        },
        "downstream_effects": { "type": "array", "items": { "type": "string" } }
      }
    },

    "dataEntry": {
      "description": "Q5 — On what data?",
      "type": "object",
      "required": ["operation", "dataset_id", "classification"],
      "additionalProperties": false,
      "properties": {
        "operation": {
          "type": "string",
          "enum": ["read", "write", "read_write"]
        },
        "dataset_id":     { "type": "string", "minLength": 1 },
        "classification": {
          "type": "string",
          "enum": ["public", "internal", "confidential", "restricted", "regulated"]
        },
        "lineage":        { "type": "string" },
        "retention":      { "type": "string" }
      }
    },

    "modelEntry": {
      "description": "Q6 — With which model?",
      "type": "object",
      "required": ["model_id", "role"],
      "additionalProperties": false,
      "properties": {
        "model_id":         { "type": "string", "minLength": 1 },
        "model_sha256":     { "type": "string" },
        "registry_record":  { "type": "string" },
        "endpoint":         { "type": "string" },
        "region":           { "type": "string" },
        "deployment_id":    { "type": "string" },
        "role":             { "type": "string", "minLength": 1 }
      }
    },

    "approval": {
      "description": "Q7 — Approved by whom? required=false must still cite the policy that allowed it.",
      "type": "object",
      "required": ["required", "policy_decision_id", "policy_version", "rationale"],
      "additionalProperties": false,
      "properties": {
        "required":            { "type": "boolean" },
        "policy_decision_id":  { "type": "string", "minLength": 1 },
        "policy_version":      { "type": "string", "minLength": 1 },
        "rationale":           { "type": "string", "minLength": 1 },
        "workflow_id":         { "type": "string" },
        "approvers": {
          "type": "array",
          "items": {
            "type": "object",
            "required": ["upn", "role", "timestamp"],
            "additionalProperties": false,
            "properties": {
              "upn":         { "type": "string" },
              "role":        { "type": "string" },
              "timestamp":   { "type": "string", "format": "date-time" },
              "ticket_ref":  { "type": "string" }
            }
          }
        }
      },
      "allOf": [
        {
          "if":   { "properties": { "required": { "const": true } } },
          "then": { "required": ["workflow_id", "approvers"] }
        }
      ]
    },

    "time": {
      "description": "Q8 — When? UTC, sub-second precision required for initiation and completion.",
      "type": "object",
      "required": ["initiation", "completion", "duration_ms"],
      "additionalProperties": false,
      "properties": {
        "initiation":            { "type": "string", "format": "date-time" },
        "completion":            { "type": "string", "format": "date-time" },
        "duration_ms":           { "type": "number", "minimum": 0 },
        "end_to_end_latency_ms": { "type": "number", "minimum": 0 },
        "clock_source":          { "type": "string" },
        "clock_last_sync":       { "type": "string", "format": "date-time" },
        "clock_drift_ms":        { "type": "number" }
      }
    },

    "outcome": {
      "description": "Q9 — With what outcome?",
      "type": "object",
      "required": ["result", "output", "evidence_pointers"],
      "additionalProperties": false,
      "properties": {
        "result": {
          "type": "string",
          "enum": ["SUCCESS", "FAILURE", "PARTIAL"]
        },
        "output": {
          "type": "object",
          "description": "Untyped at AES level; constrained by parameters_schema_ref if present."
        },
        "side_effects":      { "type": "array", "items": { "type": "string" } },
        "evidence_pointers": {
          "type": "array",
          "items": {
            "type": "object",
            "required": ["type", "ref"],
            "additionalProperties": false,
            "properties": {
              "type": { "type": "string", "minLength": 1 },
              "ref":  { "type": "string", "minLength": 1 }
            }
          }
        }
      }
    }
  }
}
