mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-08-27 14:17:10 -04:00
1 commit
| Author | SHA1 | Message | Date | |
|---|---|---|---|---|
|
|
3f14eac060
|
fix: correct Go AST compression bugs and CODE_AWARE token accounting (#1668)
## Description Fixes four real bugs that made CODE_AWARE (AST-based) compression silently non-functional for Go, plus the product-behavior change to make CODE_AWARE the default for code (previously in #1670, now consolidated here per review). Closes # ## Type of Change - [x] Bug fix (non-breaking change that fixes an issue) - [x] 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 - `code_compressor.py`: unwrap tree-sitter-go's single `statement_list` wrapper node when building `body_stmts` — its row range was swallowing the block's own closing-brace row, producing a duplicated `}` in compressed Go output. - `code_compressor.py`: match opening-brace lines by `endswith("{")` instead of `startswith("{")`, so multi-line Go signatures (e.g. `) error {`) aren't silently dropped from the compressed output. - `content_router.py`: normalize CODE_AWARE's `compressed_tokens` to `len(compressed.split())`, matching the word-split convention every other strategy (search/log/tabular/diff) already uses for `original_tokens`. Previously the mismatched scales made genuinely-good compressions look like "no savings" and get discarded for the Kompress fallback. - `content_router.py`: default `prefer_code_aware_for_code` to `True` (was `False`) — CODE_AWARE gives higher, syntax-safe compression than Kompress for code, so now that the bugs above are fixed it should be the default path. (Consolidated from #1670, now closed.) - `server.py`: add `HEADROOM_PREFER_CODE_AWARE_FOR_CODE` env override for `ContentRouterConfig.prefer_code_aware_for_code`, mirroring the existing `HEADROOM_CODE_AWARE_ENABLED` pattern, defaulting to `True`. - Formatting: ran `ruff format` on `server.py` and `content_router.py` (CI was failing on this). - `tests/test_code_aware_regressions.py` (new): 5 regression tests — - Go `statement_list` unwrap: no duplicated closing brace after truncation. - Multi-line Go signature: `) error {` line survives truncation. - ContentRouter CODE_AWARE token accounting: `compressed_tokens` matches `len(compressed.split())`, and a real compression doesn't trigger a needless Kompress fallback. - `prefer_code_aware_for_code` defaults to `True` on the `ContentRouterConfig` dataclass. - `prefer_code_aware_for_code` defaults to `True` via the `HEADROOM_PREFER_CODE_AWARE_FOR_CODE` env var (through a real `HeadroomProxy` construction). ## 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 ruff check headroom/proxy/server.py headroom/transforms/code_compressor.py headroom/transforms/content_router.py tests/test_code_aware_regressions.py All checks passed! $ python -m ruff format --check headroom/proxy/server.py headroom/transforms/code_compressor.py headroom/transforms/content_router.py tests/test_code_aware_regressions.py 4 files already formatted $ python -m mypy ... Not run — mypy not installed in this environment. $ python -m pytest tests/test_code_compressor_thread_safety.py tests/test_content_router_exclude_tools.py \ tests/test_content_router_tool_role_reversibility.py tests/test_compression_units.py \ tests/test_compression_determinism.py tests/test_compression_safety_rails.py tests/test_netcost_gate.py \ tests/test_code_aware_regressions.py -q 15 failed, 66 passed, 1 warning in 7.17s # The 15 failures are the same pre-existing/environment-specific ones from # before (reproduced identically on a clean upstream/main checkout with no # code changes — missing torch/trafilatura/playwright, stale Rust _core # build in this checkout), not caused by this change. All 5 new regression # tests in test_code_aware_regressions.py pass. ``` ## Real Behavior Proof - Environment: Windows, Python 3.11.9, headroom-ai pipx install (0.28.0) with the same fixes applied, plus this fork's checkout for lint/test verification. - Exact command / steps: ran `CodeAwareCompressor.compress()` directly against real `.go` files from an external ~100-file Go codebase, and separately routed the same files through the full `ContentRouter` with `HEADROOM_PREFER_CODE_AWARE_FOR_CODE=1`. - Observed result: 72/97 files routed to `code_aware` and compressed with syntactically valid Go output (parsed via tree-sitter re-check), 0 invalid-syntax fallbacks, 0 "routed but unchanged" cases, 14641 total tokens saved. Before the fix: 0 tokens saved via this path (all bugs combined made it a no-op). - Not tested: `mypy`, and the full repo test suite (blocked by unrelated pre-existing environment issues — see Test Output). ## 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 - [ ] 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 - [ ] I have updated the CHANGELOG.md if applicable ## Additional Notes Per @JerrettDavis's review: consolidated #1670 (the `prefer_code_aware_for_code` default flip) into this PR and closed #1670 as the duplicate; fixed the `ruff format` CI failure; added the 4 requested regression tests (Go statement_list dedup, multiline-signature brace preservation, content-router token-accounting parity, and the config-default pin). --------- Co-authored-by: shekharcharles <shekhar.aegis@gmail.com> |