Skip to content

feat(caveman-compress): deterministic phrase-lookup pre-pass (216 entries) - #813

Open
drewg2009 wants to merge 2 commits into
JuliusBrussee:mainfrom
drewg2009:feat/phrase-lookup-compression
Open

feat(caveman-compress): deterministic phrase-lookup pre-pass (216 entries)#813
drewg2009 wants to merge 2 commits into
JuliusBrussee:mainfrom
drewg2009:feat/phrase-lookup-compression

Conversation

@drewg2009

@drewg2009 drewg2009 commented Aug 7, 2026

Copy link
Copy Markdown

Summary

  • Adds a fixed, deterministic phrase-to-word lookup table (scripts/phrase_map.py, 216 entries) that runs before the Claude compression call in caveman-compress, collapsing multi-word phrases like "due to the fact that" β†’ "because" and "please make sure that" β†’ "confirm" for free β€” no model call needed for the part a dictionary already knows.
  • Skips fenced code blocks and inline code spans so code samples/comments are never touched.
  • Word-boundary-anchored and runs to a fixed point (re-applies until a pass makes no further change), so a replacement that creates a new matchable phrase gets caught too.
  • Only targets phrases that collapse to fewer tokens, not single-word synonyms ("extensive" β†’ "big" costs the same either way, so it's not in the table).

Why

compress.py currently only compresses via an LLM call β€” every phrase gets rewritten by Claude even when the correct rewrite is a fixed, unambiguous fact of English ("due to the fact that" always means "because"). Pre-collapsing those phrases with a lookup table shrinks the prompt Claude receives, for zero extra tokens or latency.

Sourcing and filtering

Entries were pulled from plain-language style guides across four domains β€” general plain-language (plainlanguage.gov, Federal Plain Language Guidelines), legal/government, academic/scientific writing, and technical documentation (Microsoft's and Google's own developer-docs style guides) β€” then hand-filtered against one hard rule: a phrase only qualifies if swapping it in verbatim, at any position in any sentence, produces grammatically valid text.

A meaningful chunk of raw source-list entries failed this and were deliberately excluded (see the module docstring for the full list and reasoning):

  • "perform an analysis of X" β†’ "analyze of X" β€” dangling preposition, broken.
  • "have a discussion about X" β†’ "discuss about X" β€” discuss doesn't take about.
  • "table this" β†’ "postpone" β€” means the opposite in British English (real regional ambiguity).
  • "as prescribed by" β†’ "under" β€” breaks on the common "as prescribed by the doctor" (person) sense vs. the statute sense.
  • "will allow you to" β†’ "lets you" β€” collapses future tense to present, which could misdescribe an unshipped feature as already live.
  • "it was demonstrated that" β†’ "evidently" β€” weakens a scientific claim's epistemic strength, not just wordiness.

Two bugs found and fixed while vetting the expanded set

  1. No word-boundary anchoring. A short entry like "point in time" could match mid-word inside unrelated text β€” e.g. the literal substring "point in time" occurs inside "checkpoint in time-series data", which an unanchored regex would mangle. Added \b anchors on both ends.
  2. Double-preposition bug. "close proximity to" β†’ "near" broke on the common "in close proximity to X" phrasing, producing "in near X". Fixed by adding the longer "in close proximity to" variant, which wins via longest-first matching.

Honest measurement (no fabricated numbers β€” see tests/test_phrase_map.py::PhraseMapTokenReductionTests)

Measured with tiktoken (o200k_base, same tokenizer evals/measure.py uses):

Input Tokens before β†’ after Cut
This repo's own tests/caveman-compress/*.original.md fixtures (terse engineering notes) 6198 β†’ 6197 ~0.02%
README-style instructional prose ("please make sure that...", "this allows you to...") 78 β†’ 58 ~26%
Deliberately wordy/corporate-style prose 60 β†’ 41 ~32%

This repo's own fixtures barely move β€” that style of prose doesn't use the phrases in the table. It helps most on verbose writing (meeting notes, policy docs, over-explained setup instructions, corporate email pasted into a memory file) and does close to nothing on prose that's already terse. Documented this conditional impact directly in SKILL.md and README.md rather than overstating it.

Changes

  • skills/caveman-compress/scripts/phrase_map.py β€” new module, apply_phrase_map(), 216-entry PHRASE_MAP, word-boundary-anchored, fence-aware, case-preserving, runs to a fixed point.
  • skills/caveman-compress/scripts/compress.py β€” calls apply_phrase_map() on the body before building the Claude prompt.
  • skills/caveman-compress/SKILL.md / README.md β€” sections explaining the pre-pass, sourcing, the two bug fixes, and the measured (conditional) impact.
  • tests/test_phrase_map.py β€” 17 tests: fence/code-span safety, case preservation, longest-phrase-wins ordering, word-boundary mid-word false-match prevention, the double-preposition regression, recursive-pass convergence (proven with a synthetic map), and tiktoken-based reduction tests (skip cleanly if tiktoken isn't installed, matching the existing optional-dependency pattern in evals/).

Test plan

  • python3 -m unittest tests.test_phrase_map -v β€” 15/17 pass without tiktoken installed (2 skip)
  • uv run --with tiktoken python3 -m unittest tests.test_phrase_map -v β€” 17/17 pass including token-count assertions
  • python3 -m unittest discover -s tests -p "test_*.py" β€” full suite, 73 tests, no regressions
  • Manually verified fence/inline-code exclusion, case preservation, and the double-preposition fix
  • Scanned PHRASE_MAP for duplicate keys (none β€” 216 unique)

Multi-word wordy phrases ("due to the fact that", "in order to", "with
regard to", ...) collapse to a single word with a fixed lookup table,
skipping code blocks and inline code. This runs before the Claude
compression call, shrinking the prompt for free instead of spending a
model call on something a dictionary already knows.

Measured with tiktoken (o200k_base): 0% match on this repo's own terse
engineering fixtures, ~32% token cut on deliberately wordy/corporate-style
prose. Value is conditional on how verbose the source writing is β€” see
phrase_map.py's docstring and the new SKILL.md/README sections for the
full reasoning and the reproducible measurement in test_phrase_map.py.
…boundary bug

Expanded PHRASE_MAP from ~60 to 216 entries by researching plain-language,
legal/government, academic, and technical-documentation style guides
(plainlanguage.gov, Federal Plain Language Guidelines, Microsoft's and
Google's developer-docs style guides, university writing-center
conciseness handouts). Every candidate was filtered against the same rule
as the original set: the replacement must be a grammatically valid drop-in
regardless of what follows it. A meaningful chunk of raw source-list
entries failed this and were excluded β€” e.g. "perform an analysis of" ->
"analyze of" is broken (dangling preposition), "have a discussion about"
-> "discuss about" repeats a preposition "discuss" doesn't take, and
"table this" -> "postpone" is flipped in British English. See the module
docstring for the full reasoning.

Also fixes two real bugs found while vetting the expanded set:

- Phrase matching had no word-boundary anchoring, so a short entry like
  "point in time" could match mid-word inside unrelated text (e.g. the
  literal substring "point in time" occurs inside "checkpoint in time-
  series data"). Added \b anchors on both ends of the compiled regex.
- "close proximity to" -> "near" broke on the common "in close proximity
  to X" phrasing, producing the double-preposition "in near X". Added the
  longer "in close proximity to" variant so it wins via longest-first
  matching.

Substitution now runs to a fixed point (bounded by _MAX_PASSES) instead of
a single pass, so a replacement that creates a new matchable phrase gets
caught too β€” proven with a synthetic map in the tests since real entries
rarely chain by design.

Re-measured with tiktoken: this repo's own terse fixtures still see near-
zero benefit (~0.02%), corporate-style prose cuts ~32%, and README-style
instructional prose ("please make sure that...", "this allows you to...")
cuts ~26% β€” the expanded table adds real coverage for the software-docs
style caveman-compress is most likely to actually run on.
@drewg2009 drewg2009 changed the title feat(caveman-compress): deterministic phrase-lookup pre-pass feat(caveman-compress): deterministic phrase-lookup pre-pass (216 entries) Aug 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants