1from __future__ import annotations
2
3from typing import TYPE_CHECKING, Protocol, runtime_checkable
4
5if TYPE_CHECKING:
6 from lexigram.ai.governance.audit.models import AuditQuery, AuditSummary
7 from lexigram.contracts.ai.governance import AIAuditEvent
8
9
10@runtime_checkable
11class AIAuditStore(Protocol):
12 """Protocol for audit event persistence backends.
13
14 Implementations must be async and should treat ``record()`` as
15 fire-and-forget safe — governance/LLM hot-paths must never block
16 on audit persistence.
17 """
18
19 async def record(self, event: AIAuditEvent) -> None:
20 """Persist a single audit event.
21
22 Args:
23 event: The audit event to store.
24 """
25 ...
26
27 async def query(self, query: AuditQuery) -> list[AIAuditEvent]:
28 """Retrieve audit events matching the given filter.
29
30 Args:
31 query: Filter criteria.
32
33 Returns:
34 List of matching events ordered by timestamp descending.
35 """
36 ...
37
38 async def aggregate(self, query: AuditQuery) -> AuditSummary:
39 """Compute aggregated statistics for matching events.
40
41 Args:
42 query: Filter criteria that scope the aggregation.
43
44 Returns:
45 Summary statistics for the matching events.
46 """
47 ...
48
49
50# ---------------------------------------------------------------------------
51# In-memory implementation (testing / development)
52# ---------------------------------------------------------------------------