aeat.core.observability._store module¶
Filesystem persistence for run traces and JSONL event logs.
One subdirectory per run_id under
core.config.Settings.aeat_runs_dir, containing
trace.json and events.jsonl. Both files round-trip through the
strict pydantic models in core.observability._models.
Run traces are DIAGNOSTIC class. The redaction rule set returned by
core.redaction.default_rules_for_class() for
DIAGNOSTIC walks
every string leaf — NIFs SHA-256-prefixed, URLs reduced to host-only,
bearer-shaped tokens fingerprinted, opaque bearers fingerprinted —
before serialisation. The core redaction helper is imported lazily so
commands that never persist traces avoid resolving the rule registry on
import.
- runs_dir(settings=None)[source]¶
Return the configured runs directory, creating it when absent.
- Parameters:
settings (
Settings|None) – Optionalcore.config.Settingsoverride (used by tests). WhenNone, the active settings are loaded viacore.config.load_settings().- Return type:
Path- Returns:
Absolute path to the per-process runs root.
- save_trace(trace, *, settings=None)[source]¶
Persist a
RunTraceto<runs_dir>/<run_id>/trace.json.Every string leaf passes through
core.redaction.redact_structured()at DIAGNOSTIC class before serialisation so the on-disk record never carries a plaintext NIF, bearer token, or sensitive URL path even if a caller fed one intoarguments.- Parameters:
settings (
Settings|None) – Optionalcore.config.Settingsoverride.
- Return type:
Path- Returns:
Absolute path of the written
trace.jsonfile.
- load_trace(run_id, *, settings=None)[source]¶
Load and strictly validate a persisted
RunTrace.Read-only lookups do not create the per-run directory — a missing
trace.jsonraisesRunTraceValidationErrorwithout polluting the runs directory with an empty entry.- Parameters:
run_id (
str) – 16-char lowercase hex run identifier.settings (
Settings|None) – Optionalcore.config.Settingsoverride.
- Return type:
- Returns:
The validated
RunTrace.- Raises:
RunTraceValidationError – When
run_idhas an invalid shape, when the file is missing, or when its contents fail strict validation.
- save_envelope(run_id, document, *, settings=None)[source]¶
Persist an emitted envelope document to
<runs_dir>/<run_id>/envelope.json.The document is the verbatim, already-CLI-redacted
SchemaEnvelopemapping captured bycore.observability.capture_envelopes()during the run. It is stored key-sorted so the on-disk artifact is byte-stable, and it is the golden expectation a laterreplay_run()asserts against. Re-validation into a typed envelope happens on load viacore.observability.validate_captured_envelope(); this writer stays free of any JSON-contract dependency.
- load_envelope_document(run_id, *, settings=None)[source]¶
Load the persisted emitted-envelope document for a run.
Read-only: does not create the per-run directory. Returns the raw mapping; type it with
core.observability.validate_captured_envelope().- Parameters:
run_id (
str) – 16-char lowercase hex run identifier.settings (
Settings|None) – Optionalcore.config.Settingsoverride.
- Return type:
- Returns:
The persisted envelope mapping.
- Raises:
RunTraceValidationError – When
run_idhas an invalid shape, the file is missing, or its contents are not a JSON object.
- save_events_append(run_id, event, *, settings=None)[source]¶
Append a single
RunEventline to the per-runevents.jsonl.newline=""pins the on-disk line terminator to\\non every platform — mirroringcore.observability._sink.JsonlRunSink— soevents.jsonlis byte-stable across Windows and POSIX writers. Every string leaf in the event is redacted at DIAGNOSTIC class before serialisation so the on-disk record stays free of plaintext NIFs / tokens / sensitive URLs.
- iter_events(run_id, *, settings=None)[source]¶
Return an iterator of
RunEventrecords from the per-runevents.jsonl.Streams records so callers processing a long-running run’s event log can avoid holding the entire file in memory. The
run_idis validated eagerly — before the iterator starts — so a bad id surfaces at the call site instead of on first iteration.Read-only: does not create a run directory when absent. A missing file yields no records.
- load_events(run_id, *, settings=None)[source]¶
Load and strictly validate every JSONL event for a run.
Thin wrapper over
iter_events()that drains the iterator into a tuple. Preferiter_events()for long-running traces where the whole log may exceed available memory.Read-only: does not create a run directory when absent.
- iter_runs(*, settings=None)[source]¶
Yield
(run_id, RunTrace)pairs sorted bystarted_atdescending.Directories without a valid
trace.json— or whose name does not match the canonicalrun_idshape — are skipped silently. This lets crashed runs (no on-exit finaliser call) coexist with healthy ones rather than poisoningaeat run list, and blocks any non-run artefacts that may have been dropped into the runs directory by hand.