mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 13:29:19 +08:00
fix(mixed-content): do not carve ISO-timestamped log rows as search results (#4463)
## Description
The mixed-content splitter's `_is_search_line` ORs the legacy
`^\S+:\d+:` pattern with the detector's predicate. That pattern matches
ISO-timestamped log rows (`2026-09-23T10:05:07Z ...`), which the
detector has rejected since #3736, so the splitter carved them into a
SEARCH_RESULTS section and `_compress_mixed` sent them to the lossy
SearchCompressor. `_is_search_line` now returns False for rows matched
by `_TIMESTAMP_ROW_RE` (the regex the detector already uses) before the
OR; real `path:NN:content` grep lines still carve.
Fixes #4462
## 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/mixed_content.py`: import `_TIMESTAMP_ROW_RE`
from `lossless_compaction` (the detector already imports it from there)
and return False for such rows in `_is_search_line`; docstring note.
- `tests/test_mixed_content_sections.py`: new test
`test_split_does_not_carve_timestamped_log_rows_as_search` (timestamped
rows produce no SEARCH_RESULTS section; a `src/app.py:10:` grep line
after the same prose and fence still does).
## Testing
Rebased onto upstream/main `3fef15d3` (2026-10-24): the new tests pass
(5 passed); with only the source change reverted, 1 fails. The
real-behavior run below was done on `e995eb9f`.
- [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
(`ruff check` and `ruff format --check` were run on the two changed
files only. `mypy headroom/transforms/mixed_content.py` reports one
error in `headroom/providers/model_metadata.py:79`, a file this PR does
not touch.)
### Test Output
```text
# new test with only the mixed_content.py change reverted
FAILED tests/test_mixed_content_sections.py::test_split_does_not_carve_timestamped_log_rows_as_search
AssertionError: assert <ContentType.SEARCH_RESULTS: 'search'> not in [<ContentType.PLAIN_TEXT: 'text'>, <ContentType.SOURCE_CODE: 'source_code'>, <ContentType.SEARCH_RESULTS: 'search'>]
1 failed, 4 passed in 1.12s
# with the fix: the new file, test_mixed_content_scan_cache.py and the content-detector tests
285 passed in 3.31s
ruff check: All checks passed!
ruff format --check: 2 files already formatted
```
## Real Behavior Proof
- Environment: Ubuntu on WSL2, Python 3.12.15, upstream/main e995eb9f
plus this commit, Rust core `headroom/_core.abi3.so` copied (untracked)
from the official headroom-ai 0.41.0 wheel. The real proxy `python -m
headroom.proxy.server` on 127.0.0.1:18991 with
`ANTHROPIC_TARGET_API_URL` pointing at a local stub on 127.0.0.1:18990
that records the tool_result text it receives.
- Exact command / steps: one `POST /v1/messages` whose last user message
holds a `tool_result` of 3,970 chars (one prose line, a python fence, 41
timestamped INFO log rows) plus a text block. Run once with the
`mixed_content.py` change reverted (`git apply -R`) and once with it
applied. Also the in-process router run (`ContentRouter().compress`) on
the same text. The stub log shows each request arriving, so the proxy
was pointed at the stub in both runs. Scripts: `router_proof.py`,
`stub.py`, `client.py`, `run.sh`.
- Observed result: before, the router routes the 41 rows as
SEARCH_RESULTS (974 -> 138 tokens); the stub receives 5 rows with the
date rewritten (`2026:9:23T10:05:00Z`) and `[41 matches compressed to 5.
Retrieve more: hash=...]`; proxy PERF `tok_before=1397 tok_after=252`.
After, the rows are a PLAIN_TEXT section; the stub receives all 41 rows
byte-for-byte (41 lines starting `2026-09-23`), PERF `tok_saved=0`
(`route_counts` shows `ratio_too_high`: the text compressor did not
shrink them, so nothing was changed). Excerpts below.
- Not tested: a real Anthropic or OpenAI API (stub upstream only);
streaming responses; OpenAI-shaped requests; Windows or macOS; a Rust
core built from this checkout (the 0.41.0 wheel's was used); other
timestamp shapes beyond the four `_TIMESTAMP_ROW_RE` already covers; the
effect on token savings across real workloads (this payload gets no
savings after the fix, by design).
Router, before (`before_router.out`):
```text
section PLAIN_TEXT 0 0
section SOURCE_CODE 1 3
section SEARCH_RESULTS 4 44
route PLAIN_TEXT TEXT 15 -> 15
route SOURCE_CODE CODE_AWARE 3 -> 3
route SEARCH_RESULTS SEARCH 974 -> 138
orig chars 3974 compressed chars 630
log rows kept verbatim: 0 of 41
```
Router, after (`after_router.out`):
```text
section PLAIN_TEXT 0 0
section SOURCE_CODE 1 3
section PLAIN_TEXT 4 44
route PLAIN_TEXT TEXT 15 -> 15
route SOURCE_CODE CODE_AWARE 3 -> 3
route PLAIN_TEXT TEXT 974 -> 974
orig chars 3974 compressed chars 3976
log rows kept verbatim: 41 of 41
```
Proxy, before (stub upstream received):
```text
client sent body_bytes=4415; tool_result chars=3970; got HTTP 200
2026:9:23T10:05:00Z worker-0 INFO request 300 handled status=200 path=/api/items latency_ms=35
2026:9:23T10:09:07Z worker-1 INFO upstream shard 3 drained
2026:9:23T10:05:06Z worker-2 INFO request 306 handled status=200 path=/api/items latency_ms=41
2026:9:23T10:05:12Z worker-0 INFO request 312 handled status=200 path=/api/items latency_ms=47
2026:9:23T10:05:18Z worker-2 INFO request 318 handled status=200 path=/api/items latency_ms=53
[... and 36 more matches in 2026]
[41 matches compressed to 5. Retrieve more: hash=06f542f16b8e09223e33e3e7]
proxy: route_counts={'content_blocks': 1, 'cache_miss': 1} compressed=1 frozen=2 msgs=3 ... tok_before=1397 tok_after=252
```
Proxy, after:
```text
client sent body_bytes=4415; tool_result chars=3970; got HTTP 200
2026-09-23T10:05:00Z worker-0 INFO request 300 handled status=200 path=/api/items latency_ms=35
2026-09-23T10:05:06Z worker-2 INFO request 306 handled status=200 path=/api/items latency_ms=41
... (41 rows, all unchanged)
2026-09-23T10:09:07Z worker-1 INFO upstream shard 3 drained
proxy: route_counts={'ratio_too_high': 1, 'content_blocks': 1, 'cache_miss': 1} compressed=0 frozen=2 msgs=3 ... tok_before=1397 tok_after=1397
```
## Runtime Rollout Safety
- Rollout-managed feature(s): none
- Minimum rollout channel: n/a
- Stable/default behavior changed: yes, narrowly: mixed payloads
containing ISO/syslog/slash-date/bare-clock timestamped rows no longer
send those rows to the SearchCompressor, so they are no longer reduced
to a few rows with a rewritten date
- Kill switch / disable path: none; no config involved
- Unsafe override required: no
- Qualification impact: none expected
- Rollback path: revert this commit
## 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)
N/A
## Additional Notes
Design choice to review: the guard sits in the splitter's
`_is_search_line` and reuses the lossless fold's `_TIMESTAMP_ROW_RE`, so
the splitter and detector cannot drift apart again. The alternative is
dropping the legacy `^\S+:\d+:` pattern entirely; I kept it because the
comment at `mixed_content.py:40-46` says it is there so previously
carved lines still carve. Documentation: no doc describes this routing.
Produced with AI assistance (Claude Code) operated by me (HetCreep); the
reproduction was run on my machine on a clone of the repository.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
<!-- devin-review-badge-begin -->
---
<a href="https://app.devin.ai/review/headroomlabs-ai/headroom/pull/4463"
target="_blank"><picture><source media="(prefers-color-scheme: dark)"
srcset="https://static.devin.ai/assets/gh-devin-review-dark.svg?v=4"><img
src="https://static.devin.ai/assets/gh-devin-review-light.svg?v=4"
alt="Devin Review"></picture></a>
<!-- devin-review-badge-end -->
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
4c3799fd81
commit
abbade9b4c
@@ -7,6 +7,7 @@ import re
|
||||
from dataclasses import dataclass
|
||||
|
||||
from .content_detector import ContentType, _is_search_result_line
|
||||
from .lossless_compaction import _TIMESTAMP_ROW_RE
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -45,7 +46,13 @@ def _is_search_line(line: str) -> bool:
|
||||
Sharing it keeps the splitter carving the same lines the detector claims
|
||||
(#3580); otherwise code in context lines would strand in PLAIN_TEXT
|
||||
sections and reach the prose compressor.
|
||||
|
||||
Timestamped log rows (``2026-09-23T10:05:07Z ...``) match the legacy
|
||||
pattern but are never grep output; the detector rejects them (#3736), so
|
||||
the splitter must too or it hands them to the lossy search compressor.
|
||||
"""
|
||||
if _TIMESTAMP_ROW_RE.match(line):
|
||||
return False
|
||||
return bool(_SEARCH_RESULT_PATTERN.match(line)) or _is_search_result_line(line)
|
||||
|
||||
|
||||
|
||||
@@ -98,3 +98,22 @@ def test_extract_json_block_ignores_delimiters_inside_strings():
|
||||
|
||||
assert end_line == 3
|
||||
assert block == "\n".join(lines)
|
||||
|
||||
|
||||
def test_split_does_not_carve_timestamped_log_rows_as_search():
|
||||
"""Regression (#3736 residual): the detector rejects ISO-timestamped log
|
||||
rows as search output; the splitter must not carve them either, while
|
||||
real ``path:NN:content`` grep lines still carve.
|
||||
"""
|
||||
logs = [
|
||||
"2026-09-23T10:05:07Z worker-1 INFO request handled",
|
||||
"2026-09-23T10:05:13Z worker-2 ERROR upstream timeout",
|
||||
]
|
||||
content = "\n".join(["Intro text", "```python", "print('x')", "```", *logs])
|
||||
|
||||
assert ContentType.SEARCH_RESULTS not in [
|
||||
section.content_type for section in split_into_sections(content)
|
||||
]
|
||||
|
||||
grep = "\n".join(["Intro text", "```python", "print('x')", "```", "src/app.py:10:print('x')"])
|
||||
assert split_into_sections(grep)[-1].content_type == ContentType.SEARCH_RESULTS
|
||||
|
||||
Reference in New Issue
Block a user