mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 13:29:19 +08:00
fix(lossless): fold only pure path listings, never markup (#4716)
## Description
The path-listing fold (`path_heading`) splits text that is not a path
listing. `_PATH_ROW_RE` matches `</body>` as directory `</` plus
basename `body>`, and `path_heading` folds any text that holds two such
lines, despite its "pure file-path listing" docstring. ContentRouter
tries every lossless fold on every section of mixed content and keeps
the smallest exact round-trip, so an HTML page tail ends up as
`</\nbody>\nhtml>`. Template closers (`{{/if}}`, Svelte `{/each}`),
escaped HTML and slashed values such as `N/A` split the same way. The
fold round-trips, but the model only ever sees the folded side. It reads
broken markup and writes it back into the user's files.
Reach on wheel 0.42.0, measured by replaying Claude Code requests
through the Anthropic pipeline in-process:
- HTML in Bash or MCP tool output that also carries a JSON block
(JSON-LD on its own lines) routes `router:tool_result:mixed` and gets
folded.
- Plain HTML without a JSON block passes through untouched, and a
complete document with a DOCTYPE/`<html>` root takes the HTML override.
- Claude Code's Read tool is safe: its line-number prefixes stop the
regex from matching.
- Kompress is not involved. The only off switch today is
`HEADROOM_LOSSLESS_COMPACTION=0`, which disables every fold.
We hit this in Extra Headroom: a user's Claude Code session broke the
closing tags on their web pages.
## Type of Change
- [x] Bug fix (non-breaking change that fixes an issue)
- [ ] New feature (non-breaking change that adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to change)
- [ ] Documentation update
- [ ] Performance improvement
- [ ] Code refactoring (no functional changes)
## Changes Made
- `headroom/transforms/lossless_compaction.py`: `path_heading` folds
only when every non-blank line is a path row, as its docstring already
said, and stops at the first line that is not. `_PATH_ROW_RE` excludes
`<`, `>` and `;` from both character classes and requires a letter,
digit or `_` in every directory segment, so a section made only of
closers (`</body>`, `{{/if}}`, `[/quote]`, `</body>`) is not a
listing either.
- `tests/test_bash_search_lossless_fold.py`: `compact_lossless(...,
"paths")` leaves an HTML tail, Handlebars and Svelte closers, escaped
HTML and an `N/A` column unchanged, both among other lines and as
sections made only of closers; the existing pure-list test also checks
the exact folded output, including a Next.js `app/[slug]/` listing; an
HTML page tail with a JSON-LD block through `ContentRouter.compress`
routes MIXED and keeps `</body>\n</html>`. All ten new cases fail on
main and pass here.
Cost, measured on 30 days of one developer's Claude Code transcripts
(21,059 tool results, 63M chars): the path fold saved 0.036% of
tool-result chars before and saves 0.014% after. What it no longer folds
is mostly text with a few path-shaped lines (Markdown notes, git
output), which is the misleading case.
## Testing
- [x] Unit tests pass (`pytest`)
- [x] Linting passes (`ruff check .`)
- [ ] Type checking passes (`mypy headroom`)
- [x] New tests added for new functionality
- [x] Manual testing performed
### Test Output
```text
$ uv run --frozen --extra dev pytest tests/test_*router*.py tests/test_*lossless*.py tests/test_mixed_content*.py tests/test_bash_search_lossless_fold.py tests/test_transforms_config_compressor.py tests/test_openai_responses_compression_units.py -q
930 passed, 2 skipped
# new tests against main's lossless_compaction.py:
10 failed, 30 passed
# on this branch:
40 passed
$ uvx ruff check headroom/transforms/lossless_compaction.py tests/test_bash_search_lossless_fold.py
All checks passed!
$ uvx ruff format --check headroom/transforms/lossless_compaction.py tests/test_bash_search_lossless_fold.py
2 files already formatted
```
## Real Behavior Proof
- Environment: macOS 27.2, Python 3.12 runtime with the released
headroom-ai 0.42.0 wheel (before), and this branch via `uv run --frozen`
on Python 3.13 (after).
- Exact command / steps: `python -c` running `compact_lossless(text,
'paths')` on an HTML tail, a Handlebars snippet and a path listing, once
against the 0.42.0 wheel and once on this branch.
- Observed result: before, the HTML tail becomes `</\nbody>\nhtml>` and
the template's closers split; after, both come back unchanged, and the
path listing folds identically on both.
- Not tested: a live proxy session; the router-level test covers the
mixed-content path in-process.
```text
before (wheel 0.42.0):
' </script>\n</\nbody>\nhtml>\n'
'{{#if active}}\n<p>{{name}}</\np>\n{{/\nif}}\neach}}\n'
'src/app/\nmain.rs\nlib.rs\nutil.rs\ntests/\na.rs\n'
after (this branch):
' </script>\n</body>\n</html>\n'
'{{#if active}}\n<p>{{name}}</p>\n{{/if}}\n{{/each}}\n'
'src/app/\nmain.rs\nlib.rs\nutil.rs\ntests/\na.rs\n'
```
## Runtime Rollout Safety
- Rollout-managed feature(s): None.
- Minimum rollout channel: Stable.
- Stable/default behavior changed: Yes. Text with non-path lines, lines
containing `<`, `>` or `;`, and rows with a directory segment of only
punctuation no longer take the path fold; pure path listings fold as
before.
- Kill switch / disable path: `HEADROOM_LOSSLESS_COMPACTION=0` disables
every lossless fold.
- Unsafe override required: No.
- Qualification impact: None.
- Rollback path: Revert this PR.
## Review Readiness
- [x] I have performed a self-review
- [x] This PR is ready for human review
## Checklist
- [x] My code follows the project's style guidelines
- [x] I have performed a self-review of my code
- [x] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit tests pass locally with my changes
- [x] I did **not** edit `CHANGELOG.md` — it is generated by
release-please from my Conventional Commit PR title (a CI guard enforces
this)
## Screenshots (if applicable)
Not applicable.
## Additional Notes
Documentation: no docs describe the path fold's matching rules, so none
changed. Type checking: mypy was not run; the change adds no new types.
Still folded: a section made only of slashed values (say three `N/A`
lines), which looks the same as a path listing. Separately, the grep
fold splits an HTML table of times (`<td>12:30:00</td>` reads as path
`<td>12`, line 30); that is a different fold and a separate fix.
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
aa2093b4a0
commit
0ccd559db0
@@ -386,8 +386,14 @@ def diff_strip_index(text: str) -> str:
|
||||
# A whole-line file path: optional ``./``/``../`` root, >=1 directory segment,
|
||||
# then a basename. No whitespace or ':' (so grep ``path:line:content`` rows —
|
||||
# handled by search_heading — are excluded). Directory-only lines (trailing '/')
|
||||
# don't match (empty basename), which keeps the fold unambiguous.
|
||||
_PATH_ROW_RE = re.compile(r"^(?P<dir>(?:\.{0,2}/)?(?:[^/\s:]+/)+)(?P<base>[^/\s:]+)$")
|
||||
# don't match (empty basename), which keeps the fold unambiguous. Markup is
|
||||
# not a path either: no '<', '>' or ';' (``</body>``, ``</body>``), and
|
||||
# every directory segment holds a letter, digit or '_', so a template closer
|
||||
# such as ``{{/if}}`` is not dir ``{{`` + base ``if}}``. Folded, those still
|
||||
# round-trip, but the model reads broken markup and writes it back.
|
||||
_PATH_ROW_RE = re.compile(
|
||||
r"^(?P<dir>(?:\.{0,2}/)?(?:(?=[^/]*\w)[^/\s:<>;]+/)+)(?P<base>[^/\s:<>;]+)$"
|
||||
)
|
||||
|
||||
|
||||
def path_heading(text: str) -> str:
|
||||
@@ -395,20 +401,27 @@ def path_heading(text: str) -> str:
|
||||
into ripgrep-heading form: each parent directory printed once on its own
|
||||
line (ending in ``/``), then the bare basenames beneath it.
|
||||
|
||||
Every non-blank line must be a path row, and there must be >=2 of them.
|
||||
Text that merely holds path-shaped lines (a template's ``{{/if}}``
|
||||
closers, a ``N/A`` table column) passes through: folding it round-trips,
|
||||
but the model only sees the folded side and reads fragments.
|
||||
Reversibility is not assumed here — ``compact_lossless`` verifies the exact
|
||||
round-trip via :func:`path_unheading` and discards the fold on any mismatch
|
||||
(e.g. a stray no-slash line mistaken for a basename), so mixed content is
|
||||
always safe. Requires >=2 path rows or there is nothing to group.
|
||||
round-trip via :func:`path_unheading` and discards the fold on any mismatch.
|
||||
Complements ``search_heading``, which only handles the ``path:line:content``
|
||||
grep shape, not plain path lists.
|
||||
"""
|
||||
lines, had_trailing = _split_keep_trailing(text)
|
||||
if sum(1 for ln in lines if _PATH_ROW_RE.match(ln)) < 2:
|
||||
matches = []
|
||||
for ln in lines:
|
||||
m = _PATH_ROW_RE.match(ln)
|
||||
if ln and not m: # not a pure listing; stop at the first such line
|
||||
return text
|
||||
matches.append(m)
|
||||
if sum(1 for m in matches if m) < 2:
|
||||
return text
|
||||
out: list[str] = []
|
||||
current: str | None = None
|
||||
for line in lines:
|
||||
m = _PATH_ROW_RE.match(line)
|
||||
for line, m in zip(lines, matches):
|
||||
if m:
|
||||
d = m.group("dir")
|
||||
if d != current:
|
||||
|
||||
@@ -169,6 +169,10 @@ def test_path_fold_roundtrip_and_shrinks_pure_list():
|
||||
assert _puh(_ph(c)) == c # exact inverse
|
||||
assert len(folded) < len(c) # shrinks
|
||||
assert folded != c
|
||||
c = "src/app/main.rs\nsrc/app/lib.rs\nsrc/app/util.rs\ntests/a.rs\n"
|
||||
assert _cl(c, "paths") == "src/app/\nmain.rs\nlib.rs\nutil.rs\ntests/\na.rs\n"
|
||||
c = "app/[slug]/page.tsx\napp/[slug]/layout.tsx\n"
|
||||
assert _cl(c, "paths") == "app/[slug]/\npage.tsx\nlayout.tsx\n"
|
||||
|
||||
|
||||
def test_path_fold_safe_passthrough_on_non_path_shapes():
|
||||
@@ -189,6 +193,62 @@ def test_path_fold_mixed_content_roundtrips_or_passes_through():
|
||||
assert _puh(_ph(c)) == c or _cl(c, "paths") == c
|
||||
|
||||
|
||||
def test_path_fold_leaves_html_closing_tags_alone():
|
||||
# `</body>` is markup, not dir `</` + base `body>`. The fold round-trips,
|
||||
# but the model only sees the folded side and reads broken HTML.
|
||||
tail = " </script>\n</body>\n</html>\n"
|
||||
assert _cl(tail, "paths") == tail
|
||||
assert _cl("</body>\n</html>", "paths") == "</body>\n</html>"
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"text",
|
||||
[
|
||||
"{{#each people}}\n{{#if active}}\n<p>{{name}}</p>\n{{/if}}\n{{/each}}\n",
|
||||
"{#each items as item}\n <li>{item}</li>\n{/each}\n{/if}\n",
|
||||
"<pre>\n</body>\n</html>\n</pre>\n",
|
||||
"Results for 3 hosts:\nN/A\nN/A\nN/A\n",
|
||||
# sections made only of closers
|
||||
"{{/if}}\n{{/each}}\n",
|
||||
"{/if}\n{/each}\n",
|
||||
"[/quote]\n[/b]\n",
|
||||
"</body>\n</html>\n",
|
||||
],
|
||||
)
|
||||
def test_path_fold_skips_text_that_only_contains_path_shaped_lines(text):
|
||||
# Closers and slashed values among other lines are not a path listing.
|
||||
assert _cl(text, "paths") == text
|
||||
|
||||
|
||||
def test_router_keeps_html_closing_tags_in_mixed_content(monkeypatch):
|
||||
# A page tail (no DOCTYPE/<html> root, so no HTML override) with a JSON-LD
|
||||
# block routes MIXED; every section gets the lossless folds tried on it.
|
||||
monkeypatch.delenv("HEADROOM_LOSSLESS_COMPACTION", raising=False)
|
||||
ld = json.dumps(
|
||||
{
|
||||
"@context": "https://schema.org",
|
||||
"@type": "Product",
|
||||
"name": "Example Widget",
|
||||
"offers": {"@type": "Offer", "price": "19.00", "priceCurrency": "USD"},
|
||||
},
|
||||
indent=2,
|
||||
)
|
||||
body = "".join(
|
||||
f" <p>Paragraph {i} of the example page with some ordinary text.</p>\n"
|
||||
for i in range(20)
|
||||
)
|
||||
tail = (
|
||||
" <main>\n" + body + " </main>\n"
|
||||
' <script type="application/ld+json">\n' + ld + "\n </script>\n</body>\n</html>\n"
|
||||
)
|
||||
result = ContentRouter(ContentRouterConfig()).compress(tail)
|
||||
# Fails loudly if routing changes, so the test cannot silently stop
|
||||
# covering the mixed path.
|
||||
assert result.strategy_used.value == "mixed"
|
||||
assert result.compressed.endswith(" </script>\n</body>\n</html>\n")
|
||||
assert "</\n" not in result.compressed
|
||||
|
||||
|
||||
# ---- EXPERIMENT: HEADROOM_EXPERIMENTAL_READ_KEEP_RATIO (light Kompress on reads) ----
|
||||
def test_experimental_read_keep_ratio_flag_and_gating(monkeypatch):
|
||||
from headroom.transforms.content_router import ContentRouter, ContentRouterConfig
|
||||
|
||||
Reference in New Issue
Block a user