mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 13:29:19 +08:00
feat/https-compose-caddy
2
Commits
| Author | SHA1 | Message | Date | |
|---|---|---|---|---|
|
|
3aafdf2dcc |
fix(router): keep source code out of the prose (Kompress) fallbacks (#4787)
## Description When the code compressor declined a block of source code, the router passed the block to Kompress. Kompress is a prose model. It drops low-salience words, and in code those words are keywords such as `for`, `in`, `if` and `import`. The result no longer parses. #4214 shows a fenced Python script that comes back with `for` and `in` removed. This PR stops that in the Python router and in the Rust live-zone dispatcher. When the code compressor declines, the code is forwarded unchanged. Closes #4214 Credit: the Python change is oxura's work from https://github.com/oxura/headroom/tree/fix/fenced-code-routing-4214-20261018 (277ca1f). Their commit is the first commit here, rebased onto `main`, and oxura is its author. The test, docs and Rust commits were added on top. oxura asked us to open this PR from our fork ([comment](https://github.com/headroomlabs-ai/headroom/issues/4214#issuecomment-6797572655)), and said they have not run this branch, so the model and native results below are ours, not theirs. ## Behaviour change to review With `enable_code_aware=False` (the default), source code now stays unchanged. Before this PR, it was sent to Kompress and lost keywords. With `enable_code_aware=True`, code that the code compressor declines also stays unchanged. Before, Kompress ran on it. A code block the code compressor does compress is unaffected. Prose, logs, JSON and tables are unaffected. Side effect: a code block that Kompress used to shrink (and break) now keeps its full size. This is the intended trade, but it means fewer tokens saved on code when `enable_code_aware=False`. ## Type of Change - [x] Bug fix (non-breaking change that fixes an issue) - [x] Documentation update - [ ] New feature - [ ] Breaking change (the default-config behaviour change above is intended, and it only stops damage to code) ## Changes Made Five commits: - `596764c3` `fix(router): keep source code out of prose fallbacks`. Author: oxura. Removes the CODE_AWARE to Kompress fallbacks in `headroom/transforms/content_router.py`. This is 277ca1f rebased onto `main`, and it differs from 277ca1f in two ways only: the two test-file conflicts are resolved, and its `wiki/proxy.md` hunk is dropped, because `AGENTS.md` says not to add content under `wiki/`. - `7705a4e6` `test(router): repin the code-to-Kompress tests and add the #4214 fence regression`. Author: Tao Hansen, with a `Co-authored-by: oxura` trailer. Five tests asserted the old fallback. They are rewritten to assert the new behaviour (code unchanged, Kompress never sees it) instead of being deleted. Adds a regression test for the script from #4214, with `enable_code_aware` both off and on. Updates `docs/content/docs/code-compression.mdx` and `docs/content/docs/how-compression-works.mdx`, which said that detected code goes to Kompress. - `6631668a` `fix(live-zone): leave source code unchanged when the code compressor declines`. Author: Tao Hansen. Same fix in `crates/headroom-core/src/transforms/live_zone.rs`: a declined `SourceCode` block is now a NoOp, not a Kompress input. Tests: `tests/live_zone_source_code_decline.rs` (single test, needs no model), and new cases in `tests/live_zone_dispatch.rs`. - `d8776283` `fix(router): turn off the code compressor's internal Kompress fallback`. Author: Tao Hansen. Follows review: the router's code compressor was still built with `fallback_to_kompress=self.config.enable_kompress` (#4719), so unknown languages, Perl and AST failures could build a `KompressCompressor` inside `CodeAwareCompressor`. The router now always sets `fallback_to_kompress=False`. The #4719 test that pinned `True` (`test_enabled_kompress_keeps_code_aware_fallback`) is repinned as `test_enabled_kompress_still_turns_off_code_aware_fallback`, and the unknown-language, Perl (by language hint, since `detect_language()` reports Perl-dominant code as unknown) and AST-failure paths are tested with spies on `is_kompress_available` and `KompressCompressor`, with `enable_kompress` both off and on. `docs/content/docs/code-compression.mdx` notes that the router always turns the option off. - `c1f8464e` `fix(router): keep passthrough code from external compressors and stale caches`. Author: Tao Hansen. Follows review: `_try_external_compressor` skips `PASSTHROUGH` blocks (code with `prefer_code_aware_for_code=False`, whole or as a mixed section), which would otherwise be offered to a prose-only external compressor as `text/plain`. The disk cache namespace now also hashes `_ROUTING_POLICY_REVISION`, so a same-version restart does not replay code that Kompress damaged before this change. Tests: two external-dispatch cases with a `text/plain` external, and one namespace test. All three fail on the previous head. I did not touch `CHANGELOG.md`. ## Testing - [x] Unit tests pass (`pytest`): see below, the failures are environment failures that also fail on `main` - [x] Linting passes (`ruff check .`, `ruff format --check .`) - [x] Type checking passes (`mypy headroom/transforms/content_router.py`) - [x] New tests added for new behaviour - [x] Manual testing performed (real model replay, below) ### Test Output macOS, Python 3.14. Head is now `c1f8464e`. ```text After the second review fix ( |
||
|
|
a6f180ae14 |
fix(code-aware): skip the Kompress fallback when Kompress is disabled (#4720)
## Description With Kompress turned off (`HEADROOM_COMPRESSORS` without `kompress`, or `HEADROOM_DISABLE_KOMPRESS=1`), `code_aware` still runs Kompress on code it cannot parse. The first unknown-language code block imports `transformers` and `torch`, then loads the Kompress model, and the proxy's RSS rises by about 1 GB for the rest of the process. Root cause: `CodeAwareCompressor._fallback_compress()` builds its own `KompressCompressor()` and never goes through the router. #4667 made `ContentRouter._get_kompress()` honour `enable_kompress=False`, but `ContentRouter._get_code_compressor()`, the only place that builds the code compressor, passes only `enable_ccr`. So `CodeCompressorConfig.fallback_to_kompress` keeps its default `True` whatever the router config says. The fallback runs for unknown languages, Perl, missing tree-sitter, and AST compression errors. Fix: `_get_code_compressor()` now passes `fallback_to_kompress=self.config.enable_kompress`. When Kompress is disabled, those four cases return the code unchanged, which is the existing `fallback_to_kompress=False` behaviour. Nothing changes when Kompress is enabled. Scope: this does not change `_is_onnx_available()`, the second issue raised in #4719 (the probe imports `transformers`). With this fix the probe is no longer reached from `code_aware` when Kompress is disabled. Making the probe itself cheaper changes availability semantics and is better handled on its own. Per-request `skip_kompress` and `kompress_model="disabled"` are also out of scope, because the code compressor is cached per router. Closes #4719 ## Type of Change - [x] Bug fix (non-breaking change that fixes an issue) ## Changes Made - `headroom/transforms/content_router.py`: `_get_code_compressor()` sets `CodeCompressorConfig.fallback_to_kompress` from `ContentRouterConfig.enable_kompress`. - `tests/test_code_aware_kompress_fallback.py` (new): - The router-built code compressor has the fallback off when Kompress is disabled and on by default. - Unknown-language code sent through the `code_aware` registry entry with Kompress disabled comes back unchanged. - `is_kompress_available` and `KompressCompressor` are never called. - Two of the three tests fail on `main`. The default-on test guards against a regression. ## Testing - [x] Linting passes (`ruff check .`) - [x] New tests added for new functionality - [x] Manual testing performed ### Test Output ```text $ pytest tests/test_code_aware_kompress_fallback.py # on main (fix reverted) E assert True is False (...fallback_to_kompress=True...) E AssertionError: Expected 'mock' to not have been called. Called 1 times. 2 failed, 1 passed in 0.87s $ pytest tests/test_code_aware_kompress_fallback.py # with fix 3 passed in 0.74s $ pytest tests/test_code_aware_kompress_fallback.py tests/test_proxy_disable_kompress.py tests/test_builtin_compressor_adapters.py tests/test_proxy_per_provider_kompress.py 37 passed, 2 skipped, 1 warning in 13.17s $ pytest tests/test_transforms_content_router.py 6 failed, 99 passed, 1 skipped # identical 6 failures on unmodified main: test_embedded_json_mixed_tool_output_preserves_every_record[*] # all fail downloading o200k_base.tiktoken, which the offline sandbox blocks $ ruff check headroom/transforms/content_router.py tests/test_code_aware_kompress_fallback.py All checks passed! $ ruff format --check headroom/transforms/content_router.py tests/test_code_aware_kompress_fallback.py 2 files already formatted $ mypy headroom/transforms/content_router.py --ignore-missing-imports Success: no issues found in 1 source file ``` ## Real Behavior Proof - Environment: Ubuntu 26.04 x86_64, Python 3.12, offline (`HEADROOM_OFFLINE=1`), no provider. `transformers` and `torch` are installed and `onnxruntime` is not. The `code` extra (tree-sitter) is not installed, so the script sets `code_compressor._tree_sitter_available = True`, which is the router's gate for building the compressor. The unknown-language path never calls the parser. - Exact command / steps: `python proof.py` builds `ContentRouter(ContentRouterConfig(enable_code_aware=True, enable_kompress=False))`. It sends the issue's 80-line pseudo-assembly through the router's `code_aware` registry entry, then prints the fallback flag, whether `kompress_compressor`, `transformers` and `torch` are imported, and the max RSS. - Observed result: on `main` (the Kompress model fetch is refused offline, so the output stays unchanged, but `torch` is already loaded): ```text detected: (<CodeLanguage.UNKNOWN: 'unknown'>, 0.0) fallback_to_kompress: True before: kompress_module=False transformers=False torch=False maxRSS=64 MB output unchanged: True after: kompress_module=True transformers=True torch=True maxRSS=723 MB ``` With this fix: ```text detected: (<CodeLanguage.UNKNOWN: 'unknown'>, 0.0) fallback_to_kompress: False before: kompress_module=False transformers=False torch=False maxRSS=64 MB output unchanged: True after: kompress_module=False transformers=False torch=False maxRSS=64 MB ``` - Not tested: a live proxy with Claude Code traffic, any provider API, the real Kompress model (cannot be downloaded offline), a real tree-sitter install, and the `tests/test_transforms_content_router.py` cases that need the tiktoken download. ## Runtime Rollout Safety - Rollout-managed feature(s): none; this is a config pass-through in the default router path, not a rollout flag - Minimum rollout channel: stable; the change only affects deployments that already disable Kompress - Stable/default behavior changed: no for the default (Kompress enabled); with Kompress disabled, unparseable code now passes through unchanged instead of being run through Kompress - Kill switch / disable path: re-enable Kompress (drop `HEADROOM_DISABLE_KOMPRESS` / add `kompress` to `HEADROOM_COMPRESSORS`) to restore the fallback - Unsafe override required: no - Qualification impact: none; no new dependency, model, or network access - Rollback path: revert this commit ## Review Readiness - [x] I have performed a self-review - [x] This PR is ready for human review <!-- devin-review-badge-begin --> --- <a href="https://app.devin.ai/review/headroomlabs-ai/headroom/pull/4720" 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: JD Davis <mxjerrett@gmail.com> |