mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-12-03 10:27:23 +08:00
## Description `grep -A`/`-B`/`-C` context lines (both the real GNU `path-NN-content` shape and the reported `path:NN-content` shape) were classified as `PLAIN_TEXT`, so code inside them was routed to the word-dropping Kompress prose compressor and silently corrupted (`=` dropped from assignments, whole lines removed) behind a plausible `[N words compressed to M]` marker. `content_detector._is_search_result_line` only accepted the `path:NN:content` match shape, and the mixed-content splitter had the same gap. This change recognizes both context-line shapes as `SEARCH_RESULTS` with path-shape guards (prefix must contain `/` or `.`, same `</>=` exclusions as the colon branch) so dates and dashed prose stay out. Closes #3580 ## 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 - Added `_is_grep_context_line` predicate plus a `_GREP_COLON_DASH_PATTERN` branch in `headroom/transforms/content_detector.py`, sharing a `_prefix_looks_like_path` guard so the three grep-line shapes cannot drift. - Taught the mixed-content splitter (`headroom/transforms/mixed_content.py`) to carve the same shapes into `SEARCH_RESULTS` sections via the shared predicate. - Added one detection-signal table row in `docs/content/docs/text-and-logs.mdx`. - Added 6 regression tests (both separator shapes, mixed match/context blocks with `--` separators, date/dashed-prose/markup guard cases). - Thresholds, confidences, and the Rust production chain are untouched; the SearchCompressor already parses `-` context lines and keeps them verbatim. ## 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 python -m pytest tests/test_transforms_content_detection.py tests/test_mixed_content_sections.py \ tests/test_mixed_content_scan_cache.py tests/test_transforms_content_router.py \ tests/test_transforms/test_content_router.py tests/test_content_router_detection_dedup.py \ tests/test_transforms/test_detect_fallback_1123.py tests/test_search_compressor.py \ tests/test_transforms_search_compressor.py tests/test_search_compressor_cjk.py \ tests/test_bash_search_lossless_fold.py -q 527 passed in ~19s ruff check: clean. ruff format --check: clean. git diff --check: clean. ``` `mypy` reports no errors in the two changed source files (10 pre-existing errors elsewhere, e.g. proxy/server.py), so the type-check box above stays unchecked. ## Real Behavior Proof - Environment: Linux, submitter worktree `.venv` (native `headroom._core` built via maturin). - Exact command / steps: fed real `grep -A` output through the content router and the SearchCompressor; the exact `if manifest.scope != ...` line from the issue was checked verbatim after compression. - Observed result: match lines and context lines both route to `search`; the SearchCompressor keeps the issue's line byte-identical (before the fix, context lines routed to Kompress and were corrupted). - Not tested: full `pytest tests/` suite, `cargo test --workspace`, parity suite, and the live native (Magika/ONNX) classification path — upstream CI still required. ## Runtime Rollout Safety - Rollout-managed feature(s): None. - Minimum rollout channel: N/A (not rollout-gated). - Stable/default behavior changed: Yes — grep `-A`/`-B`/`-C` context lines now classify as search results instead of prose (the intended fix for #3580); all other inputs unchanged. - Kill switch / disable path: Not applicable; no flag, no new dependency, no I/O. - Unsafe override required: No. - Qualification impact: None. - 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 - [x] 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 - Prior attempt #3582 (closed unmerged) diagnosed the same area; this fix additionally covers the reported `path:NN-content` shape, which a dash-dash-only predicate would miss. - Follow-up (not this PR): port the predicate to the Rust oracle `crates/headroom-core/src/transforms/content_detector.rs`; the locked production chain in `detection.rs` is intentionally untouched. No new parity fixture was added for the same reason. - Out of scope, unchanged pre-existing behavior: pathless single-file `grep` lines (`30:def...`) and drive-letter paths (`C:\...`) still classify as before.
101 lines
3.3 KiB
Python
101 lines
3.3 KiB
Python
from headroom.transforms.content_detector import ContentType
|
|
from headroom.transforms.mixed_content import (
|
|
_extract_json_block,
|
|
is_mixed_content,
|
|
split_into_sections,
|
|
)
|
|
|
|
|
|
def test_mixed_content_detection_requires_multiple_signals():
|
|
prose = "\n".join(
|
|
[
|
|
"First sentence has enough words to count.",
|
|
"Second sentence has enough words to count.",
|
|
"Third sentence has enough words to count.",
|
|
"Fourth sentence has enough words to count.",
|
|
"Fifth sentence has enough words to count.",
|
|
"Sixth sentence has enough words to count.",
|
|
]
|
|
)
|
|
|
|
assert is_mixed_content(prose) is False
|
|
assert is_mixed_content(f"{prose}\n```python\nprint('x')\n```") is True
|
|
|
|
|
|
def test_split_into_sections_preserves_typed_boundaries():
|
|
content = "\n".join(
|
|
[
|
|
"Intro text",
|
|
"```python",
|
|
"print('x')",
|
|
"```",
|
|
'[{"id": 1}]',
|
|
"src/app.py:10:print('x')",
|
|
]
|
|
)
|
|
|
|
sections = split_into_sections(content)
|
|
|
|
assert [section.content_type for section in sections] == [
|
|
ContentType.PLAIN_TEXT,
|
|
ContentType.SOURCE_CODE,
|
|
ContentType.JSON_ARRAY,
|
|
ContentType.SEARCH_RESULTS,
|
|
]
|
|
assert sections[1].language == "python"
|
|
assert sections[1].content == "print('x')"
|
|
assert sections[1].is_code_fence is True
|
|
assert sections[2].content == '[{"id": 1}]'
|
|
assert sections[3].start_line == 5
|
|
|
|
|
|
def test_split_carves_grep_context_lines_into_search_sections():
|
|
"""Regression (#3580): prose + ``grep -A`` output keeps code out of text.
|
|
|
|
Context lines (``path-NN-content``) must join the SEARCH_RESULTS section
|
|
instead of stranding source code in a PLAIN_TEXT section, where the
|
|
word-dropping prose compressor would corrupt it. ``--`` group separators
|
|
ride along as ordinary text lines.
|
|
"""
|
|
content = "\n".join(
|
|
[
|
|
"Some intro prose explaining the search.",
|
|
"src/main.py-40- context before",
|
|
"src/main.py:42:def process_data(items):",
|
|
"src/main.py-43- context after",
|
|
"--",
|
|
"src/other.py:12: return True",
|
|
"Trailing prose after the results.",
|
|
]
|
|
)
|
|
|
|
sections = split_into_sections(content)
|
|
|
|
# The ``--`` group separator breaks the run, so the two grep groups keep
|
|
# their own SEARCH_RESULTS sections instead of merging through it.
|
|
assert [section.content_type for section in sections] == [
|
|
ContentType.PLAIN_TEXT,
|
|
ContentType.SEARCH_RESULTS,
|
|
ContentType.PLAIN_TEXT,
|
|
ContentType.SEARCH_RESULTS,
|
|
ContentType.PLAIN_TEXT,
|
|
]
|
|
assert "src/main.py-40- context before" in sections[1].content
|
|
assert "src/main.py:42:def process_data(items):" in sections[1].content
|
|
assert "src/main.py-43- context after" in sections[1].content
|
|
assert "src/other.py:12: return True" in sections[3].content
|
|
|
|
|
|
def test_extract_json_block_ignores_delimiters_inside_strings():
|
|
lines = [
|
|
"[",
|
|
' {"path": "a]b", "message": "keep {literal} braces"},',
|
|
' {"path": "c"}',
|
|
"]",
|
|
]
|
|
|
|
block, end_line = _extract_json_block(lines, 0)
|
|
|
|
assert end_line == 3
|
|
assert block == "\n".join(lines)
|