Caveman
Compression engineCompressors

Compressors

A compressor takes one content shape and tries to return fewer bytes. It does not count tokens, write recovery data, or call the network. It returns smaller bytes, or it says no and the engine keeps the original.

Fig. — the compression path
Payloadstdin · bodyDetectcontent routerCompressorS4 · lossyTokenizeBPE · offlineSmaller output+ handlejson · log · codediff · search · textCCR storebyte-exactstore originalrecovery_handlerecord · parse-fail · not-smaller → pass through unchanged

The engine routes a payload to a compressor by content type. Detect classifies the bytes, the engine hands them to the matching compressor, the offline counter measures the token reduction, and — because these compressors are lossy — the original is stored for byte-exact recovery before any compressed result is emitted. S4lossy · recoverable is the class for every detected compressor: lossy to the bytes, but recoverable.

Each compressor is structural and deterministic. The engine wraps it with token counting, CCR recovery, and pass-through rules.

Pass-through is the default outcome

A compressor returns ok=false on any malformed or unsupported input, and the engine forwards the original bytes unchanged. The engine also passes through when the result is not actually smaller in tokens, and when no recovery store is available (a lossy result is only emitted if its original was stored). The default record mode never transforms at all.

The six detected compressors

Detect routes to one of these six. Low confidence falls through to text. The percentages are honest token-reduction targets by content type, not measured guarantees — actual results depend on the payload.

TypeWhat it keeps / dropsTarget
jsonCollapses repetitive arrays; keeps keys, structure, and error/message subtrees70–90%
logKeeps errors, stack traces, and the first/last lines; drops INFO and progress noise85–95%
codeKeeps imports, signatures, and types; elides function bodies so the syntax stays valid40–70%
diffKeeps file and hunk headers and the changed lines; elides repeated context60–80%
search-resultKeeps the top and bottom hits plus important diagnostic and security hits80–95%
textKeeps headings, opening and closing context, and important sections; strips HTML noise50–80%

The principle is the same across all six: keep the part a downstream model would need to answer correctly, drop the part that only adds tokens. A log compressor that throws away a stack trace would be useless; one that keeps every INFO line defeats the point.

The code compressor and tree-sitter

The code compressor parses source so it can keep declarations and elide bodies without producing invalid syntax. How it parses depends on the build:

  • Built with cgo, it uses tree-sitter and understands Go, Python, and JavaScript/TypeScript.
  • Built cgo-free (or for WASM), it falls back to a pure-Go go/ast parser that compresses Go only.

The fallback is deliberate and honest: rather than mangle a Python file it can't parse, the cgo-free build leaves non-Go code as a pass-through. The embedded eval fixtures cover all three languages under cgo.

The two forced-only compressors

Two compressors exist but are never auto-detected. Detect must never route to them — they only run when a caller explicitly forces the type via Options.Type (the CLI's --type flag, or cave compress --toon).

  • toolschema — compresses MCP/OpenAI tool catalogs: drops annotation bloat (examples, titles, defaults, $schema) and truncates descriptions while keeping every selection token (tool and parameter names, types, enums, required) byte-for-byte, so the model still picks the same tool. This is the compressor caveman-shrink wraps.
  • toon — a TOON re-encode of tabular JSON. Unlike the detected compressors it is lossless to the model; you ask for it on purpose, never by detection.

Forced-only means forced-only

toolschema and toon are reached only by setting an explicit type. Routing Detect to either would break the content-detection contract — a JSON payload that happens to look like a tool catalog must still go to the json compressor unless the caller asked for toolschema.

Compressing JSON

The detected path needs no flags. Pipe a payload into caveman-engine compress; the clean compressed bytes land on stdout and the inferred report (ratio, basis, recovery handle) goes to stderr.

terminal
# stdout = compressed payload, stderr = the inferred JSON report
caveman-engine compress < tool-output.json > compressed.txt
# {"content_type":"json","tokens_before":16098,"tokens_after":1091,
# "ratio":0.93,"basis":"inferred","recovery_handle":"..."}

That recovery handle is how you get the original back, byte-for-byte:

terminal
caveman-engine retrieve <handle> > original.json

On the committed sample, this run reproduces the headline demo:

16,0981,091tok93%inferred

Honesty rule

That 93% is inferred and recoverable: an offline token estimate from the engine's own counter, not a verified saving. The public engine never claims verified.

See also