mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 18:41:07 +08:00
feat/https-compose-caddy
3
Commits
| Author | SHA1 | Message | Date | |
|---|---|---|---|---|
|
|
93279f3208 |
fix(smart-crusher): preserve unchanged JSON bytes (#4044)
## Description Closes #3697. An unchanged SmartCrusher result now keeps the original bytes and reports no structural compression. Formatting-only savings come from the existing Rust JsonMinifier, not from pretending the JSON value changed. The minifier preserves duplicate keys, order, escapes and number literals. A mixed result cannot use one section's CCR marker to hide unmarked loss in another. ## Type of Change - [x] Bug fix (non-breaking change that fixes an issue) - [x] Documentation update ## Changes Made - Keep SmartCrusher's exact-byte no-op behavior; route actual whitespace savings through JsonMinifier in Python and the eligible native live zone. - Keep all records and JSON literals; do not store originals or inject CCR markers for lossless minification. - Preserve mixed routing and reject uncovered lossy sections in both tool-result formats. Explicit no-CCR behavior is unchanged. - Replace incidental forwarding tests with native minification and complete-record regressions; document the distinction. ## Testing - [ ] Entire Python suite — not claimed. - [x] Four consumer/native modules: `python -m pytest -q tests/test_recursive_json.py tests/test_transforms_content_router.py tests/test_transforms/test_smart_crusher_rust_parity.py tests/test_transforms/test_smart_crusher_lossless_default.py` - [x] Changed-file Ruff checks; normal commit hooks, including mypy and commitlint. - [x] Manual public-API runs, with and without the HTML extra. ### Test Output Consumer/native modules: ```text collected 136 items ============================= 136 passed in 36.43s ============================= ``` Normal pre-push (fmt, clippy, 1,668 Rust tests with 3 ignored, native build, Python checks, commitlint): ```text ======================== 180 passed, 1 warning in 8.35s ======================== ✅ ci-precheck PASSED — safe to push. To https://github.com/oxura/headroom.git 64688217..0002af3e HEAD -> fix/3697-unchanged-json-passthrough ``` The first push stopped on an inherited commit footer because `origin/main` was stale. Fetching the actual upstream ref fixed the lint range; the normal push then passed. No source merge, lint-rule change or hook bypass. Hosted checks on `0002af3e` now pass: all four Python shards, Windows permissions, smoke tests, native wrappers, parity and Docker e2e. [Checks](https://github.com/headroomlabs-ai/headroom/pull/4044/checks). ## Real Behavior Proof - Environment: Linux x86-64, Python 3.13.14, Rust 1.95 and the actual extension built from this checkout. No provider billing calls. - Exact command / steps: the earlier localhost `/v1/compress` and TransformPipeline runs exercised the original fix. For this follow-up, call `ContentRouter.compress` on a data-only HTML page containing 60 complete records, with `smart_crusher_lossless_only=True`, `smart_crusher_with_compaction=False`, and `enable_kompress=False`. Run it once without trafilatura, then with the locked 2.0.0 extra. The new public-API test contains the full input/config. Separately call `route_embedded_json` on a compact array followed by a padded array, with Unicode and CRLF surroundings. - Observed result: both HTML modes keep all 60 records and the surrounding markup, with no unrecoverable section. The unchanged first JSON span does not stop the second span's compaction; surrounding text stays exact. Final Codecov is 96%; the two remaining lines are the deferred HTML fallback return. The real no-HTML-extra API run executed both, and local coverage also exercises the unchanged-span branch. - Not tested: live provider acceptance, invoices or production cache hit rates. No native eligibility or admission gates changed. ## Runtime Rollout Safety - Rollout-managed feature(s): none; existing JSON routing. - Minimum rollout channel: unchanged. - Stable/default behavior changed: eligible formatting savings return as json_minifier; standalone SmartCrusher remains byte-exact on no-ops. - Kill switch / disable path: existing routing configuration and HEADROOM_LIVE_ZONE_DISABLE_ARMS. - Unsafe override required: no. - Qualification impact: no-op, formatting, structural and CCR paths exercised separately. - Rollback path: revert the minifier integration; no persistence migration. ## 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 made corresponding changes to the documentation - [x] I did not edit CHANGELOG.md — release-please owns it ## Additional Notes Addresses the [minifier review](https://github.com/headroomlabs-ai/headroom/pull/4044#issuecomment-6157583285). The maintainer’s main merge is retained. The latest follow-up only changes consumer tests. AI-assisted; no subagents. --------- Co-authored-by: JerrettDavis <mxjerrett@gmail.com> |
||
|
|
6326965046 |
fix(transforms): judge the code_aware Kompress fallback in tokens (#3881)
## Description `ContentRouter._apply_strategy_to_content` compares two different units in the `code_aware` path: - `code_aware`'s result is stored as a **word** count: `compressed_tokens = len(output.content.split())` (introduced with the registry dispatch in #2400, after #1857 had moved every other count to `_estimate_tokens`). - The no-savings Kompress fallback is then kept only `if fallback_tokens < compressed_tokens:`, where `fallback_tokens` is a **token** count (cl100k payload tokens for a real Kompress result). Code runs about 2 tokens per whitespace word, so an unchanged code block looks like a ~55% compression and the fallback can essentially never win. The common case is a Claude Code `Read` of source: the `N\t` line-number column stops `code_aware` from parsing, it hands the text back unchanged with `compressed=True`, the fallback runs Kompress over the whole block (up to ~20 s of ONNX inference on a 1,800-line file, holding the Kompress slot), and the result is always discarded. The phantom ~0.45 ratio is then refused by the lossy-unrecoverable guard and logged as `lossy_unrecoverable_skipped`, 0 tokens saved. It is the same class as the CONFIG word-count bug fixed in #2759. Fix: 1. Measure `code_aware`'s output with `_estimate_tokens`, the unit of `original_tokens` and of every other branch. 2. Measure the Kompress fallback's result with the same estimator instead of the count Kompress returns (a Kompress passthrough reports **words**, which would otherwise let an unchanged block "win"). 3. The lossless-then-lossy inline Kompress attempt in the same branch is measured the same way and now recorded in `strategy_chain` even when it loses, so the no-savings fallback below does not run the identical inference a second time. **This is a compression behaviour change, stated plainly.** The fallback can now win, so a `code_aware` no-op block (typically a large line-numbered code `Read`) can now be Kompress-compressed with a CCR retrieval marker, where before it went out verbatim after the inference was thrown away. Savings go up; the Kompress inference that already ran now pays for itself. Real `code_aware` compressions are now scored in honest tokens rather than an optimistic word ratio, so a marginal one could in principle land above `min_ratio` where it passed before (none did on the corpus below). ## 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/content_router.py`: `code_aware` result counted with `_estimate_tokens`; the no-savings Kompress fallback and the lossless-then-lossy inline attempt measured with `_estimate_tokens`; the inline attempt recorded in the chain before it runs. - `tests/test_transforms_content_router.py` (new, no tree-sitter needed): - `test_code_aware_noop_keeps_a_smaller_kompress_fallback`: a no-op `code_aware` on 120 line-numbered Rust lines, with a Kompress result smaller in tokens but larger than the word count, keeps the Kompress result. - `test_code_aware_noop_reports_tokens_when_kompress_passes_through`: when Kompress passes through, the block is reported at its token count, not its word count. - `test_code_aware_noop_runs_kompress_once_when_it_passes_through`: with lossless-then-lossy on, a losing inline attempt is not retried by the fallback. - `test_code_aware_noop_keeps_a_smaller_inline_kompress`: with lossless-then-lossy on, a winning inline attempt is kept, with one Kompress call. - `tests/test_code_aware_regressions.py`, `tests/test_router_registry_dispatch.py`: two existing assertions pinned the word-count metric (`len(shrunk.split())`); they now assert `_estimate_tokens`, the unit `original_tokens` uses. Their intent (the compressor's own estimate must not leak in, a real compression must not trigger a fallback) is unchanged. - `tests/test_router_registry_smartcrusher.py`, `tests/test_openai_responses_context_compaction.py`: two SMART_CRUSHER no-op tests go through the same shared fallback and asserted the count a mocked `_try_ml_compressor` reported (one mock "shrank" by returning longer text than it was given). The mocks now return shorter text and the assertions use `_estimate_tokens` of it. This also means a Kompress *passthrough* after a SMART_CRUSHER or CONFIG no-op, which Kompress reports in words, can no longer be adopted as a phantom saving. The first two new tests fail on `main` |
||
|
|
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> |