Expand description
Deterministic JSON crusher — single source of truth for structural JSON compaction (#934, Headroom “Smart Crusher” port, GitLab #935).
Real JSON payloads (API responses, kubectl get -o json, DB dumps, RAG
chunks) are dominated by arrays of objects that repeat the same keys and
values on every row. This module factors that redundancy out:
crush_losslesshoists every key that is present in all objects of an array to its dominant value (a_defaultsblock); each item then keeps only the fields that deviate from the default. A field absent from an item means “equals the default”, so the transform is exactly reconstructible viareconstruct.crush_lossyadditionally drops near-unique high-entropy columns (timestamps, UUIDs — pure noise for an agent) recorded in_dropped. The exact original is then recovered out-of-band via CCR, never from the text.
Determinism (#498): the output is a pure function of the input Value — no
timestamps, counters, randomness, or hash-map order leakage (candidate keys
are walked through a BTreeSet, value frequencies through a BTreeMap).
The crusher never inflates: callers gate on shorter_only, and a no-op input
returns None.
Structs§
- Crush
Opts - Tuning for a crush pass.
- Crush
Result - Result of a crush pass.
Constants§
- KEEP_
DATA_ DIVISOR - Shared “the crush must at least halve the payload” threshold. The lossless
crusher keeps every datum, so reshaping only pays when the array is redundant
enough that the compact form is at most
1/KEEP_DATA_DIVISORof the input; heterogeneous/low-redundancy data falls through to each caller’s own outline. Single source for the shell (json_schema,curl) and read (structured_read) paths so the gate can never drift (#936).
Functions§
- crush_
lossless - Lossless crush: returns
Someonly when something was actually factored. - crush_
lossy - Lossy crush: lossless factoring plus high-entropy column dropping.
- crush_
text_ if_ beneficial - Parse
textas JSON and losslessly crush it, returning the compact form only when it at least halves the input (KEEP_DATA_DIVISOR). The shared gate for callers that start from raw text (structured_read,ctx_readaggressive).Nonefor non-JSON or low-redundancy input — the caller keeps its own path. - crush_
text_ lossy_ if_ beneficial - Lossy crush of
text: drops near-unique high-entropy columns (timestamps, UUIDs — noise for an agent) whose distinct-value ratio is>= drop_entropy. Returns theCrushResultonly when the pass actually dropped a column (!lossless, so a lossless pass would not already cover it) AND the compact form at least halves the input (KEEP_DATA_DIVISOR). Because data is lost, the caller MUST persist the verbatim original out-of-band (CCR) before emitting it — the dropped columns are never reconstructible from the text.Nonefor non-JSON, low-redundancy, or all-lossless input. - crush_
value_ if_ beneficial - Lossless crush of
value, returning the compact text only when it at least halvesraw_len(KEEP_DATA_DIVISOR). The shared gate for callers that already hold a parsedValueplus its source length (json_schema,curl). - reconstruct
- Rebuild a
Valuefrom crushed text. Exact for lossless forms; for lossy forms the_droppedcolumns are simply absent (recover them via CCR).