mirror of
https://github.com/headroomlabs-ai/headroom.git
synced 2026-11-22 13:29:19 +08:00
fix(router): keep passthrough code from external compressors and stale caches
Two review findings on #4787: - An explicit PASSTHROUGH block (source code with prefer_code_aware_for_code=False, whole or as a mixed-content section) maps to text/plain, so a selected prose-only external compressor could rewrite the code. _try_external_compressor now skips PASSTHROUGH. - The disk cache namespace is keyed by version and config only, so a same-version restart could replay code that Kompress damaged before this change. The namespace now includes a routing-policy revision.
This commit is contained in:
@@ -1797,6 +1797,13 @@ def content_cache_key(
|
||||
return h.hexdigest()
|
||||
|
||||
|
||||
# Bump when a routing change makes earlier cached output wrong for the same
|
||||
# version and config, so the disk tier starts a fresh namespace instead of
|
||||
# replaying it. "code-never-kompress": before #4214, code could reach Kompress
|
||||
# and lose keywords; a cache made then must not be served after the fix.
|
||||
_ROUTING_POLICY_REVISION = "code-never-kompress"
|
||||
|
||||
|
||||
def _config_namespace(config: Any) -> str:
|
||||
"""A prefix for this router's keys in the shared disk tier: a digest of its
|
||||
config and the headroom version. The proxy may run routers configured
|
||||
@@ -1822,7 +1829,9 @@ def _config_namespace(config: Any) -> str:
|
||||
# starts a fresh namespace rather than serving output made under another.
|
||||
settings = sorted((k, v) for k, v in os.environ.items() if k.startswith("HEADROOM_"))
|
||||
h = hashlib.blake2b(digest_size=8)
|
||||
h.update(f"{__version__}|{stable(asdict(config))!r}|{settings!r}".encode())
|
||||
h.update(
|
||||
f"{__version__}|{_ROUTING_POLICY_REVISION}|{stable(asdict(config))!r}|{settings!r}".encode()
|
||||
)
|
||||
return h.hexdigest() + ":"
|
||||
|
||||
|
||||
@@ -4169,6 +4178,12 @@ class ContentRouter(Transform):
|
||||
active = self._active_external_compressors
|
||||
if not active:
|
||||
return None
|
||||
# PASSTHROUGH is the router's explicit "leave this block unmangled"
|
||||
# decision (source code with prefer_code_aware_for_code=False, whole or
|
||||
# as a mixed-content section). Its strategy maps to text/plain below, so
|
||||
# a prose-only external compressor would otherwise rewrite the code.
|
||||
if strategy == CompressionStrategy.PASSTHROUGH:
|
||||
return None
|
||||
content_mime = _CONTENT_TYPE_TO_MIME.get(self._content_type_from_strategy(strategy))
|
||||
if content_mime is None:
|
||||
return None
|
||||
|
||||
@@ -149,3 +149,17 @@ def test_ast_failure_passes_through_without_kompress(
|
||||
available.assert_not_called()
|
||||
compressor.assert_not_called()
|
||||
assert out.content == PYTHON_CODE
|
||||
|
||||
|
||||
def test_disk_cache_namespace_includes_the_routing_policy_revision(
|
||||
monkeypatch: pytest.MonkeyPatch,
|
||||
) -> None:
|
||||
# Output cached before #4214 (code sent to Kompress) must not be replayed
|
||||
# after the fix by a restart with the same version and config.
|
||||
import headroom.transforms.content_router as cr
|
||||
|
||||
config = ContentRouterConfig()
|
||||
current = cr._config_namespace(config)
|
||||
monkeypatch.setattr(cr, "_ROUTING_POLICY_REVISION", "earlier-policy")
|
||||
|
||||
assert cr._config_namespace(config) != current
|
||||
|
||||
@@ -394,3 +394,49 @@ def test_wildcard_selection_activates_registered_external(_memory_ccr):
|
||||
|
||||
assert comp.calls
|
||||
assert "external:ext_json" in result.strategy_chain
|
||||
|
||||
|
||||
# ───────────── explicit passthrough code never reaches an external ─────────────
|
||||
|
||||
_PY_FENCE = "\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(30)
|
||||
]
|
||||
)
|
||||
_PROSE = " ".join(
|
||||
["The nightly job failed after the deploy and the operator wants a short summary."] * 40
|
||||
)
|
||||
|
||||
|
||||
def test_passthrough_code_section_is_not_offered_to_a_text_external(_memory_ccr):
|
||||
# With prefer_code_aware_for_code=False the router keeps source code
|
||||
# unmangled (PASSTHROUGH). A selected prose-only external compressor must
|
||||
# not receive that code: mixed content splits the fence into its own section.
|
||||
comp = _RecordingExternal(name="ext_text", content_types=("text/plain",))
|
||||
router = ContentRouter(
|
||||
_cfg(active_external_compressors=["ext_text"], prefer_code_aware_for_code=False)
|
||||
)
|
||||
router.compressor_registry.register(comp, replace=True)
|
||||
router._active_external_compressors = router._resolve_active_external_compressors()
|
||||
content = f"{_PROSE}\n\n```python\n{_PY_FENCE}\n```\n\n{_PROSE}"
|
||||
|
||||
result = router.compress(content)
|
||||
|
||||
assert all("def handler_0" not in call.content for call in comp.calls)
|
||||
assert f"```python\n{_PY_FENCE}\n```" in result.compressed
|
||||
|
||||
|
||||
def test_passthrough_whole_code_block_is_not_offered_to_a_text_external(_memory_ccr):
|
||||
comp = _RecordingExternal(name="ext_text", content_types=("text/plain",))
|
||||
router = ContentRouter(
|
||||
_cfg(active_external_compressors=["ext_text"], prefer_code_aware_for_code=False)
|
||||
)
|
||||
router.compressor_registry.register(comp, replace=True)
|
||||
router._active_external_compressors = router._resolve_active_external_compressors()
|
||||
|
||||
result = router.compress(_PY_FENCE)
|
||||
|
||||
assert not comp.calls
|
||||
assert result.compressed == _PY_FENCE
|
||||
|
||||
Reference in New Issue
Block a user