aeat.core.observability._golden module¶
Canonicalise / mask / compare primitive for golden-output determinism.
One substrate, two consumers (ADR
2026-06-30-deterministic-output-replay-substrate): the observability
core.observability.replay_run() envelope-assertion tier and
the harness operator golden gate both call this primitive; neither
re-implements capture or compare.
The captured payload is the verbatim emitted
SchemaEnvelope document. On load it is
re-validated against SCHEMA_REGISTRY[command] by
validate_captured_envelope() so the captured payload is a typed
envelope around a registered OutputSchema, never a
dict[str, Any] bag. Comparison
(assert_golden_match()) is over the FULL envelope (shared spine
plus result), key-sorted and canonicalised, after a declared narrow
field mask that hides only the residual non-deterministic surrogate
keys.
Masking honesty¶
GOLDEN_MASK_FIELDS is a declared, narrow allowlist — the two
opaque surrogate keys that still flap once the clock seam
(core.time.frozen_clock()) is frozen and profile_id is
injected: snapshot_id (its profile_id prefix and timestamp are
deterministic, only the trailing uuid4().hex flaps) and the
observability run_id (a minted uuid4 tail). A mask broad enough
to hide a real regression voids the gate, so the mask is kept minimal
and is proven minimal by an anti-tautology test that asserts, for a
scenario captured twice under a frozen clock and injected identity, the
set of differing JSON paths reduces to exactly these masked fields —
differing_field_names() and differing_paths() exist for that
proof.
- MASK_SENTINEL¶
The sentinel a masked leaf is replaced with before comparison.
- GOLDEN_MASK_FIELDS: frozenset[str]¶
Declared, narrow allowlist of leaf field names whose values are opaque, non-assertable surrogate keys carrying an unseedable
uuid4tail. These are the ONLY residual non-deterministic leaves oncecore.time.frozen_clock()is frozen andprofile_idis injected. Widening this set is a standing honesty hazard; every addition must be proven minimal by the anti-tautology gate.
- canonicalise(document)[source]¶
Return the UTF-8, key-sorted, fixed-indent canonical form of
document.The canonical form is the byte string two captures are compared on. Key-sorting makes the comparison insensitive to emit-time key order; the fixed indent keeps a mismatch diff human-readable.
- mask_document(document, *, fields=frozenset({'run_id', 'snapshot_id'}))[source]¶
Return a deep copy of
documentwith masked leaves replaced by a sentinel.Any mapping value stored under a key in
fields— at any depth — is replaced byMASK_SENTINEL. The rest of the document is copied verbatim, so a real output difference outside the masked fields is preserved for comparison.
- flatten_paths(document)[source]¶
Return a flat
{dotted-path: leaf-value}view ofdocument.List elements are addressed with
[index]segments (e.g.notices[0].code). Leaves are scalars; empty containers are recorded as their own leaf so a container that gains its first element still surfaces as a differing path.
- differing_paths(left, right)[source]¶
Return the set of dotted JSON paths whose leaves differ between two documents.
A path is included when it is present in only one document, or when it is present in both with unequal leaf values. This is the raw, UNMASKED diff used by the anti-tautology proof to show the mask is exactly the residual non-deterministic field set.
- differing_field_names(left, right)[source]¶
Return the leaf field names of the paths that differ between two documents.
The leaf field name of
result.snapshot_idissnapshot_idand ofnotices[0].codeiscode. Used by the anti-tautology proof to assert the residual diff reduces to exactlyGOLDEN_MASK_FIELDS.
- assert_golden_match(expected, actual, *, fields=frozenset({'run_id', 'snapshot_id'}))[source]¶
Assert two envelope documents are byte-identical after masking, else raise.
Both documents are masked with
fieldsand canonicalised; if the canonical byte strings differ, aGoldenReplayMismatchErroris raised carrying the differing masked paths.- Parameters:
- Raises:
GoldenReplayMismatchError – When the masked canonical forms differ.
- Return type:
- validate_captured_envelope(document, *, registry=None)[source]¶
Re-validate a captured envelope document through its registered schema.
Looks up
document["command"]inregistry(defaulting to the process-globalSCHEMA_REGISTRY), specialisesSchemaEnvelopeover the registered result schema, and strictly validates the document. The return value is a typed envelope, never adict[str, Any]bag — this is the typed boundary the substrate keeps captured payloads behind.- Parameters:
- Return type:
SchemaEnvelope[OutputSchema]- Returns:
The strictly-validated
SchemaEnvelopewhose result is the registeredOutputSchemafor the captured command.- Raises:
GoldenCaptureError – When the document has no
commandstring, the command is unregistered, or the payload fails strict validation against the registered schema.