Skip to content

API Reference parrot.semantic.audit

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot.semantic.audit

View source

Signed semantic audit ledger (spec §3 Module 8, §8 Q3/Q5).

Alias AbstractKMSSigner

Re-exported from parrot.security.AbstractKMSSigner.

Alias LocalHMACSigner

Re-exported from parrot.security.LocalHMACSigner.

Class SemanticAuditEntry

One signed audit record for a semantic tool call.

engine is provenance only and is always "native". SQL text lives here, never in exception messages.

Fields:

Field Type Default Description
schema_version int 1
entry_id str
created_at datetime
user_id str
tenant_id str
channel str
agent_id str | None
subject_digest str
roles list[str]
static_subject bool
model str
model_digest str
policy_digest str
engine Literal['native'] 'native'
tool str
structured_query dict | None
raw_sql bool
compiled_sql str | None
governed_sql str | None
dialect str
driver str
rls_applied list[str]
masked_columns list[str]
pbac Literal['allow', 'deny', 'unavailable', 'not_governed']
outcome Literal['ok', 'denied', 'validation_error', 'execution_error']
error_code str | None
row_count int | None
duration_ms int | None
signature str ''

Method SemanticAuditEntry.canonical_bytes

Return sorted-key compact JSON of the entry, excluding signature.

canonical_bytes() -> bytes

Returns: bytes — Canonical UTF-8 bytes used for signing and verification.

Class SemanticAuditLedger

Bounded in-memory signed ledger with an optional async durable sink.

Parameter Type Default Description
signer AbstractKMSSigner | None None Async KMS signer; defaults to a process-local HMAC signer.
storage Callable[[str], Awaitable[None]] | None None Async callable receiving the entry JSON; makes the ledger durable.
max_entries int 10000 Maximum number of entries kept in memory.

Attributes:

Attribute Type Description
signer AbstractKMSSigner
entries deque[SignableAuditEntry]
durable bool True when a storage callable is configured (drives audit_required=None, §8 Q5).

Method SemanticAuditLedger.append

Sign the entry, keep it in memory and forward it to storage.

append(entry: _E, *, required: bool = False) -> _E

Parameter Type Default Description
entry _E The audit entry to record.
required bool False When True, a missing or failing sink raises instead of logging.

Returns: _E — The signed entry.

Raises:

  • SemanticExecutionError — audit_unavailable when required and the sink fails.

Method SemanticAuditLedger.verify

Verify the entry's signature with the configured async signer.

verify(entry: SignableAuditEntry) -> bool

Parameter Type Default Description
entry SignableAuditEntry Entry to verify.

Returns: bool — True when the signature matches the canonical content.

Alias SemanticExecutionError

Re-exported from parrot.semantic.errors.SemanticExecutionError.

Class SignableAuditEntry

What the ledger needs from an entry: an id, a signature slot and a canonical byte form.

Attributes:

Attribute Type Description
entry_id str
signature str

Method SignableAuditEntry.canonical_bytes

Return the bytes that are signed.

canonical_bytes() -> bytes

Returns: bytes

Method SignableAuditEntry.model_dump_json

Return the JSON handed to the durable sink.

model_dump_json() -> str

Returns: str

Attribute logger

Function postgres_audit_storage

Build an async storage callable that INSERTs entries into Postgres.

An injected pool (anything with acquire()) is never closed. A DSN string opens an owned asyncpg connection per write, always closed afterwards.

postgres_audit_storage(dsn_or_pool: str | Any, *, table: str = 'semantic_audit_events') -> Callable[[str], Awaitable[None]]

Parameter Type Default Description
dsn_or_pool str | Any asyncpg DSN or an existing pool.
table str 'semantic_audit_events' Target table (DDL: parrot/semantic/sql/semantic_audit_events.sql).

Returns: Callable[[str], Awaitable[None]] — async storage(json_str).

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally