mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-26 22:56:08 +08:00
## Description KompressCompressor flattened `tool_result` content into a space-joined word list, discarding line boundaries and column padding — so compression could glue words from unrelated shell commands onto one fabricated line, and drop `ls -l` table fields inconsistently row to row. Now tracks each word's source line and never joins across a line boundary, and protects fixed-width tabular rows as atomic units. Closes #3099 ## 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 - `KompressCompressor.compress()`/`compress_batch()`: track each word's source line, never join kept words from different lines onto one line. - Add `_tabular_protected_line_ids()`: runs of ≥3 consistent fixed-width-column lines (`ls -l`, `ps`) are kept whole, not partially crushed. Reuses the existing `HEADROOM_KOMPRESS_MUST_KEEP` toggle. - Add `tests/test_kompress_line_integrity.py` reproducing both bugs. ## Testing - [x] Unit tests pass (`pytest`) - [x] Linting passes (`ruff check .`) - [x] Type checking passes (`mypy headroom`) - [x] New tests added for new functionality - [x] Manual testing performed ### Test Output ```text tests/test_kompress_line_integrity.py .... [4 passed] 66 passed, 1 warning in 18.15s ruff check .: All checks passed! ``` ## Real Behavior Proof - Environment: macOS/Apple Silicon (MPS), Python 3.12, real cached `chopratejas/kompress-v2-base` model. - Exact command / steps: ran `compress()`/`compress_batch()` on Bash-transcript content shaped like the issue, before and after the fix, including an aggressive `target_ratio=0.15` run to force lines to fully empty out. - Observed result: unpatched code collapsed a 24-line transcript onto one line and dropped `ls -la` fields — bug reproduced live. Patched code: no merges under any condition, tabular rows stayed intact even at ~22-29% overall keep ratio. - Not tested: full HTTP proxy path; ONNX backend with a real model. ## Runtime Rollout Safety - Rollout-managed feature(s): None new — reuses `HEADROOM_KOMPRESS_MUST_KEEP` - Minimum rollout channel: N/A - Stable/default behavior changed: Yes — compression output is now line/row-safe by default (correctness fix, not opt-in) - Kill switch / disable path: `HEADROOM_KOMPRESS_MUST_KEEP=0` disables tabular-row protection; the line-join fix itself has no toggle (never valid to merge lines) - Unsafe override required: No - Qualification impact: None known - Rollback path: revert this commit/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) --------- Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com> Co-authored-by: JerrettDavis <mxjerrett@gmail.com>
109 lines
4.3 KiB
Python
109 lines
4.3 KiB
Python
"""Regression tests for issue #3099: compression merging words across line
|
|
boundaries, and dropping fields from tabular rows inconsistently row to row.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
from headroom.transforms import kompress_compressor as kc
|
|
from headroom.transforms.kompress_compressor import KompressCompressor, KompressConfig
|
|
|
|
|
|
class _Enc(dict):
|
|
def word_ids(self, batch_index=0):
|
|
return self["_word_ids"][batch_index]
|
|
|
|
|
|
class _Tok:
|
|
def __call__(self, chunk_words, **kw):
|
|
if chunk_words and isinstance(chunk_words[0], list):
|
|
batch_words = chunk_words
|
|
else:
|
|
batch_words = [chunk_words]
|
|
return _Enc(
|
|
input_ids=[[0] * len(words) for words in batch_words],
|
|
attention_mask=[[1] * len(words) for words in batch_words],
|
|
_word_ids=[list(range(len(words))) for words in batch_words],
|
|
)
|
|
|
|
|
|
class _KeepIndicesModel:
|
|
"""Fake model that keeps exactly the given (global, single-chunk) word indices."""
|
|
|
|
def __init__(self, keep_indices: set[int]):
|
|
self._keep = keep_indices
|
|
|
|
def get_keep_mask(self, input_ids, attention_mask):
|
|
return [[idx in self._keep for idx in range(len(row))] for row in input_ids]
|
|
|
|
def get_scores(self, input_ids, attention_mask):
|
|
return [[1.0 if idx in self._keep else 0.0 for idx in range(len(row))] for row in input_ids]
|
|
|
|
|
|
def _install_fake_kompress(monkeypatch, keep_indices: set[int], *, must_keep: bool = False) -> None:
|
|
model = _KeepIndicesModel(keep_indices)
|
|
monkeypatch.setattr(kc, "_load_kompress", lambda *a, **k: (model, _Tok(), "onnx"))
|
|
monkeypatch.setattr(kc, "_model_device_type", lambda *a, **k: "cpu")
|
|
if must_keep:
|
|
monkeypatch.delenv(kc._KOMPRESS_MUST_KEEP_ENV, raising=False)
|
|
else:
|
|
monkeypatch.setenv(kc._KOMPRESS_MUST_KEEP_ENV, "0")
|
|
|
|
|
|
def _compress(monkeypatch, content: str, path: str):
|
|
compressor = KompressCompressor(KompressConfig(enable_ccr=False, min_input_words=10))
|
|
if path == "compress":
|
|
monkeypatch.setattr(compressor, "_should_batch_single_content", lambda *a, **k: False)
|
|
return compressor.compress(content)
|
|
monkeypatch.setattr(compressor, "_should_use_sequential_fallback", lambda: False)
|
|
[result] = compressor.compress_batch([content], batch_size=8)
|
|
return result
|
|
|
|
|
|
@pytest.mark.parametrize("path", ["compress", "compress_batch"])
|
|
def test_compress_does_not_merge_words_across_lines(monkeypatch, path):
|
|
"""Reproduces #3099 bug 1: two commands' output on separate lines.
|
|
|
|
The model keeps only the last word of line 1 ("rg") and the first word of
|
|
line 2 ("/opt/homebrew/bin/ast-grep"). Before the fix, these landed adjacent
|
|
in the flat kept-word list and were joined with a space onto one line,
|
|
fabricating "rg /opt/homebrew/bin/ast-grep" -- read literally, that implies
|
|
rg resolves to that path, which is false.
|
|
"""
|
|
line1 = "alpha beta gamma delta epsilon zeta eta rg"
|
|
line2 = "/opt/homebrew/bin/ast-grep theta iota kappa lambda mu nu xi"
|
|
content = f"{line1}\n{line2}"
|
|
|
|
_install_fake_kompress(monkeypatch, keep_indices={7, 8})
|
|
|
|
result = _compress(monkeypatch, content, path)
|
|
|
|
assert "rg /opt/homebrew/bin/ast-grep" not in result.compressed
|
|
assert result.compressed.split("\n") == ["rg", "/opt/homebrew/bin/ast-grep"]
|
|
|
|
|
|
@pytest.mark.parametrize("path", ["compress", "compress_batch"])
|
|
def test_compress_keeps_tabular_row_intact(monkeypatch, path):
|
|
"""Reproduces #3099 bug 2: an `ls -la`-shaped listing.
|
|
|
|
The model keeps nothing on its own, so absent row protection only the
|
|
must-keep regex would save permission bits/numbers/filenames -- exactly
|
|
the reported symptom of some fields (the group column, the month)
|
|
disappearing inconsistently. Once a row is part of a >=3-row run of
|
|
consistent fixed-width columns, the whole row must survive intact.
|
|
"""
|
|
rows = [
|
|
"-rw-r--r--@ 1 user staff 4921 Aug 12 20:14 daemon.log",
|
|
"-rw-r--r--@ 1 user staff 10485 Aug 12 20:14 other.log",
|
|
"-rw-r--r--@ 1 user staff 512 Aug 12 20:15 third.log",
|
|
]
|
|
content = "\n".join(rows)
|
|
|
|
_install_fake_kompress(monkeypatch, keep_indices=set(), must_keep=True)
|
|
|
|
result = _compress(monkeypatch, content, path)
|
|
|
|
expected = "\n".join(" ".join(row.split()) for row in rows)
|
|
assert result.compressed == expected
|