{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://h33.ai/agent-zero/authority-state/v1/schema.json",
  "title": "H33 Authority State Snapshot v1",
  "description": "Reconstructable view of who possessed what authority at time T. Owned by Q-Sign; produced from the tenant's auth_events log. Embedded in Composite Attestations when the decision requires authority-state context. Signed by Q-Sign's PQ key envelope. The canonical question this object answers: 'Who possessed what authority at time T?'",
  "type": "object",
  "additionalProperties": false,
  "required": ["format", "state_id", "timestamp_t_ms", "computed_at_ms", "tenant_id", "principals_with_authority", "active_grants", "active_revocations", "active_policies", "delegation_graph_snapshot", "signature_envelope"],
  "properties": {
    "format": {"type": "string", "const": "h33-authority-state-snapshot-v1"},
    "state_id": {"type": "string", "pattern": "^[0-9a-f]{64}$", "description": "SHA3-256 of canonical form EXCLUDING this field. Content-addressable."},
    "timestamp_t_ms": {"type": "integer", "minimum": 0, "description": "The moment in time this snapshot describes."},
    "computed_at_ms": {"type": "integer", "minimum": 0, "description": "When the snapshot was actually computed (may differ from timestamp_t_ms for retroactive reconstructions)."},
    "tenant_id": {"type": "string", "pattern": "^[0-9a-f]{32}$"},

    "principals_with_authority": {
      "type": "array",
      "description": "Every principal who held some authority at T. A single principal may appear multiple times (once per authority held).",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": ["principal_id", "authority_id", "effective_scope"],
        "properties": {
          "principal_id": {"type": "string"},
          "authority_id": {"type": "string"},
          "effective_scope": {"type": "array", "items": {"type": "string"}}
        }
      }
    },

    "active_grants": {
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": ["authority_id", "granted_by", "granted_to", "effective_expiry_at_ms"],
        "properties": {
          "authority_id": {"type": "string"},
          "granted_by": {"type": "string"},
          "granted_to": {"type": "string"},
          "effective_expiry_at_ms": {"type": "integer", "minimum": 0, "description": "Per Layer 0: min(parent_expiry, child_expiry, ..., root_expiry)."}
        }
      }
    },

    "active_revocations": {
      "type": "array",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": ["authority_id", "revoked_at_ms"],
        "properties": {
          "authority_id": {"type": "string"},
          "revoked_at_ms": {"type": "integer", "minimum": 0}
        }
      }
    },

    "active_policies": {
      "type": "array",
      "items": {"type": "string", "pattern": "^[0-9a-f]{64}$"}
    },

    "delegation_graph_snapshot": {
      "type": "array",
      "description": "One entry per agent active at T, with its delegation chain frozen.",
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": ["agent_id", "chain"],
        "properties": {
          "agent_id": {"type": "string"},
          "chain": {
            "type": "array",
            "items": {
              "type": "object",
              "additionalProperties": false,
              "required": ["depth", "agent_id", "supervision_mode", "effective_scope", "max_delegation_depth", "delegated_at_ms"],
              "properties": {
                "depth": {"type": "integer", "minimum": 0},
                "agent_id": {"type": "string"},
                "parent_agent_id": {"type": "string"},
                "supervision_mode": {"type": "string", "enum": ["autonomous", "human_approved", "human_reviewed", "agent_approved", "quorum_approved", "policy_only", "emergency_override"]},
                "effective_scope": {"type": "array", "items": {"type": "string"}},
                "max_delegation_depth": {"type": "integer", "minimum": 0},
                "delegated_at_ms": {"type": "integer", "minimum": 0}
              }
            }
          }
        }
      }
    },

    "signature_envelope": {
      "type": "object",
      "additionalProperties": false,
      "required": ["signing_message_algorithm", "signing_message", "pq_signatures", "signing_engine_id", "signing_engine_version", "signing_engine_public_key_fingerprint"],
      "properties": {
        "signing_message_algorithm": {"type": "string", "const": "sha3-256"},
        "signing_message": {"type": "string", "pattern": "^[0-9a-f]{64}$", "description": "SHA3-256 of canonical form EXCLUDING signature_envelope and state_id."},
        "pq_signatures": {
          "type": "object",
          "additionalProperties": false,
          "required": ["ml_dsa_65", "falcon_512", "slh_dsa_128f"],
          "properties": {
            "ml_dsa_65": {"type": "string"},
            "falcon_512": {"type": "string"},
            "slh_dsa_128f": {"type": "string"}
          }
        },
        "signing_engine_id": {"type": "string"},
        "signing_engine_version": {"type": "string"},
        "signing_engine_public_key_fingerprint": {"type": "string", "pattern": "^[0-9a-f]{64}$"}
      }
    }
  }
}
