{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://rantropy.xyz/schemas/ai-agent-audit-event-v1.schema.json",
  "title": "Rantropy AI Agent Audit Event v1",
  "description": "A vendor-neutral event envelope for recording AI agent decisions, tool actions, approvals, outcomes, and integrity metadata.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "schema_version",
    "event_id",
    "event_type",
    "tenant_id",
    "execution_id",
    "actor",
    "source",
    "environment",
    "event_time",
    "observed_at",
    "sequence",
    "integrity",
    "payload"
  ],
  "properties": {
    "schema_version": {
      "const": "1.0.0"
    },
    "event_id": {
      "type": "string",
      "minLength": 16,
      "maxLength": 128
    },
    "event_type": {
      "type": "string",
      "pattern": "^[a-z][a-z0-9_]*(\\.[a-z][a-z0-9_]*)+$",
      "maxLength": 128
    },
    "tenant_id": {
      "type": "string",
      "minLength": 1,
      "maxLength": 128
    },
    "execution_id": {
      "type": "string",
      "minLength": 1,
      "maxLength": 128
    },
    "parent_event_id": {
      "type": "string",
      "minLength": 16,
      "maxLength": 128
    },
    "actor": {
      "$ref": "#/$defs/actor"
    },
    "source": {
      "$ref": "#/$defs/source"
    },
    "environment": {
      "type": "string",
      "enum": ["development", "test", "staging", "production"]
    },
    "region": {
      "type": "string",
      "minLength": 1,
      "maxLength": 64
    },
    "event_time": {
      "type": "string",
      "format": "date-time"
    },
    "observed_at": {
      "type": "string",
      "format": "date-time"
    },
    "sequence": {
      "type": "integer",
      "minimum": 0
    },
    "integrity": {
      "$ref": "#/$defs/integrity"
    },
    "payload": {
      "type": "object",
      "description": "Event-specific fields. Define a separate payload schema for each event_type and version.",
      "additionalProperties": true
    }
  },
  "$defs": {
    "actor": {
      "type": "object",
      "additionalProperties": false,
      "required": ["type", "id"],
      "properties": {
        "type": {
          "type": "string",
          "enum": ["human", "ai_agent", "service", "system"]
        },
        "id": {
          "type": "string",
          "minLength": 1,
          "maxLength": 256
        },
        "credential_id": {
          "type": "string",
          "minLength": 1,
          "maxLength": 256
        }
      }
    },
    "source": {
      "type": "object",
      "additionalProperties": false,
      "required": ["service", "version"],
      "properties": {
        "service": {
          "type": "string",
          "minLength": 1,
          "maxLength": 128
        },
        "version": {
          "type": "string",
          "minLength": 1,
          "maxLength": 64
        }
      }
    },
    "integrity": {
      "type": "object",
      "additionalProperties": false,
      "required": ["canonicalization", "hash_algorithm", "payload_hash"],
      "properties": {
        "canonicalization": {
          "type": "string",
          "enum": ["jcs-v1"]
        },
        "hash_algorithm": {
          "type": "string",
          "enum": ["sha-256"]
        },
        "payload_hash": {
          "$ref": "#/$defs/sha256Digest"
        },
        "previous_event_hash": {
          "$ref": "#/$defs/sha256Digest"
        }
      }
    },
    "sha256Digest": {
      "type": "string",
      "pattern": "^sha256:[a-f0-9]{64}$"
    }
  },
  "examples": [
    {
      "schema_version": "1.0.0",
      "event_id": "0198f2f2-7a3b-7d64-8e21-b7c9386c1801",
      "event_type": "tool.completed",
      "tenant_id": "tenant_acme",
      "execution_id": "exec_20260823_1042",
      "actor": {
        "type": "ai_agent",
        "id": "agent:invoice-reviewer"
      },
      "source": {
        "service": "agent-runtime",
        "version": "2026.08.1"
      },
      "environment": "production",
      "event_time": "2026-08-23T10:42:31.184Z",
      "observed_at": "2026-08-23T10:42:31.229Z",
      "sequence": 42,
      "integrity": {
        "canonicalization": "jcs-v1",
        "hash_algorithm": "sha-256",
        "payload_hash": "sha256:77418e964aac33bbf7ad94e7a43b833c08d53dd8156a6ea1ad18d25e6c3c26fe"
      },
      "payload": {
        "outcome": "succeeded"
      }
    }
  ]
}
