Audit¶
The audit subsystem records every harness execution as an ExecutionTrace — intent, generated code, policy results, tool calls, cost, and errors. Recorders are pluggable via the AuditRecorderProtocol; ship-in-memory for dev or SQLite for production.
AuditRecorderProtocol¶
AuditRecorderProtocol ¶
Bases: Protocol
Structural interface satisfied by every audit recorder.
The protocol intentionally captures only the core methods every
recorder must provide. Optional extensions (get_by_id,
iter_since, vacuum_older_than) are provided by both shipped
implementations but are not part of the protocol — callers that
need them should depend on the concrete class instead.
Source code in src/agenticapi/harness/audit/recorder.py
InMemoryAuditRecorder¶
Bounded in-memory storage — fast, but wiped on process restart. Use for development, tests, and single-process demos.
SqliteAuditRecorder¶
Persistent audit storage backed by the Python standard library sqlite3 module — zero new dependencies, survives process restarts, and exposes query helpers suitable for admin dashboards.
from agenticapi.harness import HarnessEngine
from agenticapi.harness.audit import SqliteAuditRecorder
recorder = SqliteAuditRecorder(path="./audit.sqlite", max_traces=100_000)
harness = HarnessEngine(audit_recorder=recorder, policies=[...])
Writes are serialized through an asyncio.Lock and dispatched via asyncio.to_thread, so the recorder is safe to share across concurrent requests without starving the event loop. Two indices are created on first use: (timestamp DESC) for recency queries and (endpoint_name) for per-endpoint dashboards.
SqliteAuditRecorder ¶
Persistent :class:AuditRecorder backed by a single SQLite database file.
Satisfies the :class:AuditRecorderProtocol (structurally — no
inheritance). Drop-in for the in-memory recorder:
.. code-block:: python
from agenticapi.harness import HarnessEngine
from agenticapi.harness.audit import SqliteAuditRecorder
recorder = SqliteAuditRecorder(path="./audit.sqlite")
harness = HarnessEngine(audit_recorder=recorder, policies=[...])
All methods are async even though SQLite is blocking — the
blocking calls are off-loaded to a worker thread via
:func:asyncio.to_thread so the event loop is never starved.
Concurrency.
SQLite supports many concurrent readers and one concurrent
writer. We open the database with check_same_thread=False
and isolation_level=None (autocommit), and serialise writes
through an :class:asyncio.Lock. This is correct for the
agent-audit workload: writes happen at request rate, queries
are dashboards, and we never need long transactions.
Source code in src/agenticapi/harness/audit/sqlite_store.py
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 | |
__init__ ¶
Initialize the recorder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Filesystem path to the SQLite database file. Created
if missing. Use |
required |
max_traces
|
int | None
|
Optional hard cap on the number of stored
traces. When set, every |
None
|
Source code in src/agenticapi/harness/audit/sqlite_store.py
close ¶
record
async
¶
Persist an execution trace to SQLite.
Source code in src/agenticapi/harness/audit/sqlite_store.py
get_records ¶
Return the limit most recent traces, optionally filtered.
This call is synchronous to keep the protocol identical to
the in-memory recorder. Internally it opens a short-lived
connection — the typical query pattern (a dashboard ticking
every few seconds) does not warrant async overhead. Callers
on a hot path should use :meth:iter_since instead, which is
async-stream-shaped.
Source code in src/agenticapi/harness/audit/sqlite_store.py
get_by_id ¶
Look up a single trace by its identifier.
Source code in src/agenticapi/harness/audit/sqlite_store.py
iter_since
async
¶
Yield every trace recorded at or after since.
Streams rows so very large audit stores don't materialise the whole result set in memory.
Source code in src/agenticapi/harness/audit/sqlite_store.py
vacuum_older_than
async
¶
Drop every trace recorded before cutoff.
Returns:
| Type | Description |
|---|---|
int
|
The number of rows removed. |
Source code in src/agenticapi/harness/audit/sqlite_store.py
count
async
¶
Return the total number of stored traces.
Source code in src/agenticapi/harness/audit/sqlite_store.py
clear
async
¶
Remove every trace. Test-only / dev-only operation.
Source code in src/agenticapi/harness/audit/sqlite_store.py
ExecutionTrace¶
ExecutionTrace
dataclass
¶
Complete trace of an agent execution for auditing.
Captures all stages of the execution pipeline: intent parsing, code generation, policy evaluation, sandbox execution, and the final result.
Attributes:
| Name | Type | Description |
|---|---|---|
trace_id |
str
|
Unique identifier for this execution trace. |
endpoint_name |
str
|
Name of the agent endpoint that handled the request. |
timestamp |
datetime
|
When the execution started. |
intent_raw |
str
|
The original natural language request. |
intent_action |
str
|
The classified action type (read, write, etc.). |
generated_code |
str
|
The Python code generated by the LLM. |
reasoning |
str | None
|
Optional chain-of-thought reasoning from the LLM. |
policy_evaluations |
list[dict[str, Any]]
|
Results from each policy evaluation. |
execution_result |
Any
|
The output of the sandbox execution. |
execution_duration_ms |
float
|
Total execution duration in milliseconds. |
error |
str | None
|
Error message if the execution failed. |
llm_usage |
dict[str, int] | None
|
Token usage statistics from LLM calls. |
approval_request_id |
str | None
|
ID of an associated approval request, if any. |
stream_events |
list[dict[str, Any]]
|
Phase F8 — list of typed agent lifecycle events
emitted on this request's :class: |
Source code in src/agenticapi/harness/audit/trace.py
Exporters¶
ConsoleExporter ¶
Exports execution traces to stdout as JSON.
Useful for development and debugging. Requires no external dependencies.
Example
exporter = ConsoleExporter() await exporter.export(trace)
Source code in src/agenticapi/harness/audit/exporters.py
__init__ ¶
Initialize the console exporter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pretty
|
bool
|
If True, format JSON with indentation. |
True
|
export
async
¶
Print the trace as JSON to stdout.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trace
|
ExecutionTrace
|
The execution trace to export. |
required |
Source code in src/agenticapi/harness/audit/exporters.py
CompositeExporter ¶
Fans out trace exports to multiple exporters.
Example
exporter = CompositeExporter([ConsoleExporter(), OpenTelemetryExporter()]) await exporter.export(trace)
Source code in src/agenticapi/harness/audit/exporters.py
__init__ ¶
Initialize with a list of exporters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exporters
|
list[AuditExporter]
|
List of exporters to fan out to. |
required |
export
async
¶
Export the trace to all registered exporters in parallel.
Uses asyncio.gather for concurrent exports. Individual exporter failures are logged but do not prevent other exporters from running.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trace
|
ExecutionTrace
|
The execution trace to export. |
required |