mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 12:05:34 +08:00
fix(router): turn off the code compressor's internal Kompress fallback
The router's outer fallbacks no longer send code to Kompress, but the code compressor it builds still had fallback_to_kompress set from enable_kompress. Unknown languages, Perl and AST failures could then build a KompressCompressor inside CodeAwareCompressor (#4214). The router now always sets fallback_to_kompress=False. The #4719 test that pinned the old True value is repinned, and the unknown-language, Perl and AST-failure paths are tested with Kompress spies, with enable_kompress both off and on.
This commit is contained in:
@@ -109,7 +109,7 @@ result = compressor.compress(code)
|
||||
| `max_body_lines` | `5` | Max lines to keep per function body |
|
||||
| `min_tokens_for_compression` | `100` | Skip files smaller than this |
|
||||
| `language_hint` | `None` | Override language detection |
|
||||
| `fallback_to_kompress` | `True` | Use Kompress for unsupported languages |
|
||||
| `fallback_to_kompress` | `True` | Use Kompress for unsupported languages. `ContentRouter` (the proxy and `compress()`) always sets it to `False`, so code it routes is never sent to Kompress |
|
||||
|
||||
## Before and After
|
||||
|
||||
|
||||
@@ -5479,10 +5479,13 @@ class ContentRouter(Transform):
|
||||
self._code_compressor = CodeAwareCompressor(
|
||||
CodeCompressorConfig(
|
||||
enable_ccr=self.config.ccr_inject_marker,
|
||||
# The code compressor's Kompress fallback builds its
|
||||
# own KompressCompressor, so it must honour the same
|
||||
# switch as _get_kompress() (#4719).
|
||||
fallback_to_kompress=self.config.enable_kompress,
|
||||
# Never let the code compressor fall back to Kompress
|
||||
# internally (unknown language, Perl, no tree-sitter,
|
||||
# AST failure). Kompress is a prose model and drops
|
||||
# keywords from code (#4214). A block the code
|
||||
# compressor cannot handle stays unchanged, whether or
|
||||
# not Kompress is enabled for prose (#4719).
|
||||
fallback_to_kompress=False,
|
||||
)
|
||||
)
|
||||
else:
|
||||
|
||||
@@ -58,22 +58,49 @@ def test_disabled_kompress_turns_off_code_aware_fallback() -> None:
|
||||
assert compressor.config.fallback_to_kompress is False
|
||||
|
||||
|
||||
def test_enabled_kompress_keeps_code_aware_fallback() -> None:
|
||||
def test_enabled_kompress_still_turns_off_code_aware_fallback() -> None:
|
||||
# Kompress may be on for prose, but it must never compress code (#4214):
|
||||
# the router's code compressor leaves blocks it cannot handle unchanged.
|
||||
router = ContentRouter(ContentRouterConfig(enable_code_aware=True))
|
||||
|
||||
compressor = router._get_code_compressor()
|
||||
|
||||
assert compressor is not None
|
||||
assert compressor.config.fallback_to_kompress is True
|
||||
assert compressor.config.fallback_to_kompress is False
|
||||
|
||||
|
||||
def test_unknown_language_code_passes_through_without_kompress(
|
||||
kompress_spies: tuple[MagicMock, MagicMock],
|
||||
) -> None:
|
||||
assert detect_language(UNKNOWN_CODE)[0] == CodeLanguage.UNKNOWN
|
||||
router = ContentRouter(ContentRouterConfig(enable_code_aware=True, enable_kompress=False))
|
||||
PERL_CODE = "\n".join(
|
||||
["#!/usr/bin/perl", "use strict;", "use warnings;"]
|
||||
+ [
|
||||
f'my $v{i} = $ARGV[{i % 3}] // {i}; print "$v{i}\\n" if $v{i} =~ /^\\d+$/;'
|
||||
for i in range(60)
|
||||
]
|
||||
)
|
||||
|
||||
PYTHON_CODE = "\n".join(
|
||||
["import json", ""]
|
||||
+ [
|
||||
f"def handler_{i}(event):\n if event.get('k{i}'):\n return json.dumps(event)\n return None\n"
|
||||
for i in range(40)
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
def _code_aware_entry(enable_kompress: bool):
|
||||
router = ContentRouter(
|
||||
ContentRouterConfig(enable_code_aware=True, enable_kompress=enable_kompress)
|
||||
)
|
||||
entry = router.compressor_registry.get("code_aware")
|
||||
assert entry is not None
|
||||
return router, entry
|
||||
|
||||
|
||||
@pytest.mark.parametrize("enable_kompress", [False, True])
|
||||
def test_unknown_language_code_passes_through_without_kompress(
|
||||
kompress_spies: tuple[MagicMock, MagicMock], enable_kompress: bool
|
||||
) -> None:
|
||||
assert detect_language(UNKNOWN_CODE)[0] == CodeLanguage.UNKNOWN
|
||||
_, entry = _code_aware_entry(enable_kompress)
|
||||
|
||||
out = entry.compress(CompressInput(content=UNKNOWN_CODE, content_type="text/x-code"))
|
||||
|
||||
@@ -81,3 +108,44 @@ def test_unknown_language_code_passes_through_without_kompress(
|
||||
available.assert_not_called()
|
||||
compressor.assert_not_called()
|
||||
assert out.content == UNKNOWN_CODE
|
||||
|
||||
|
||||
@pytest.mark.parametrize("enable_kompress", [False, True])
|
||||
def test_perl_code_passes_through_without_kompress(
|
||||
kompress_spies: tuple[MagicMock, MagicMock], enable_kompress: bool
|
||||
) -> None:
|
||||
# detect_language() reports Perl-dominant code as UNKNOWN on purpose, so the
|
||||
# Perl branch is reached through an explicit language hint.
|
||||
_, entry = _code_aware_entry(enable_kompress)
|
||||
|
||||
out = entry.compress(
|
||||
CompressInput(content=PERL_CODE, content_type="text/x-code", config={"language": "perl"})
|
||||
)
|
||||
|
||||
available, compressor = kompress_spies
|
||||
available.assert_not_called()
|
||||
compressor.assert_not_called()
|
||||
assert out.content == PERL_CODE
|
||||
|
||||
|
||||
@pytest.mark.parametrize("enable_kompress", [False, True])
|
||||
def test_ast_failure_passes_through_without_kompress(
|
||||
kompress_spies: tuple[MagicMock, MagicMock],
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
enable_kompress: bool,
|
||||
) -> None:
|
||||
router, entry = _code_aware_entry(enable_kompress)
|
||||
code_compressor = router._get_code_compressor()
|
||||
assert code_compressor is not None
|
||||
|
||||
def _boom(*_args, **_kwargs):
|
||||
raise RuntimeError("simulated AST failure")
|
||||
|
||||
monkeypatch.setattr(code_compressor, "_compress_with_ast", _boom)
|
||||
|
||||
out = entry.compress(CompressInput(content=PYTHON_CODE, content_type="text/x-code"))
|
||||
|
||||
available, compressor = kompress_spies
|
||||
available.assert_not_called()
|
||||
compressor.assert_not_called()
|
||||
assert out.content == PYTHON_CODE
|
||||
|
||||
Reference in New Issue
Block a user