Skip to content

Commit 20d66f8

Browse files
author
Mateusz
committed
fix(compression): detect previously compressed outputs via [COMPRESSED] content marker
The _compacted metadata flag only survives within a single request pass since clients don't echo back ChatMessage.metadata. Outputs dynamically compressed in prior requests now carry a [COMPRESSED...] prefix in their content, so checking for it in _already_processed_skip_warning ensures cross-request detection without any memory overhead.
1 parent 6283966 commit 20d66f8

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

src/core/services/tool_output_compression_service.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import hashlib
77
import json
88
import logging
9+
import re
910
import time
1011
from collections.abc import Sequence
1112

@@ -65,6 +66,7 @@
6566
_TIME_BUDGET_EXCEEDED_REASON = "time_budget_exceeded"
6667
_DYNAMIC_CONFIG_RUNTIME_TUNABLE_ATTR = "__dynamic_config_runtime_tunable__"
6768
_COMPACTED_STUB_MARKER = "[COMPACTED]"
69+
_COMPRESSED_MARKER_RE = re.compile(r"^\[COMPRESSED[^\]]*\]", re.MULTILINE)
6870
_SYSTEM_REMINDER_MARKER = "<system-reminder>"
6971
_NOISY_NOOP_DECISION_REASONS = frozenset(
7072
{
@@ -813,6 +815,8 @@ def _already_processed_skip_warning(message: ChatMessage) -> str | None:
813815
return None
814816
if _COMPACTED_STUB_MARKER in message.content:
815817
return "skipped_already_processed_compaction"
818+
if _COMPRESSED_MARKER_RE.match(message.content):
819+
return "skipped_already_processed_compression"
816820
if (
817821
_SYSTEM_REMINDER_MARKER in message.content
818822
and "artifact" in message.content.lower()

tests/unit/core/services/test_tool_output_compression_service.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,42 @@ async def test_service_sets_compacted_metadata_on_first_compression() -> None:
438438
assert "skipped_already_processed_compaction" in second.records[0].warnings
439439

440440

441+
@pytest.mark.asyncio
442+
async def test_service_skips_outputs_with_compressed_marker_already_processed() -> None:
443+
registry = CompressionStrategyRegistry()
444+
registry.register("half_trim", _HalfTrimStrategy())
445+
service = ToolOutputCompressionService(
446+
strategy_registry=registry,
447+
identity_resolver=ToolIdentityResolver(),
448+
selector=RuleBasedStrategySelector(),
449+
)
450+
previously_compressed = (
451+
"[COMPRESSED level=balanced methods=ansi_normalize,diff_compact saved=2048B]\n"
452+
"diff --git a/foo.py b/foo.py\n--- a/foo.py\n+++ b/foo.py\n"
453+
)
454+
messages = _build_tool_messages("git diff", previously_compressed)
455+
cfg = DynamicCompressionConfig(
456+
enabled=True,
457+
min_bytes=0,
458+
marker=CompressionMarkerConfig(enabled=False),
459+
methods={"half_trim": True},
460+
rules=[
461+
CompressionRule(
462+
name="default",
463+
priority=1,
464+
when=CompressionRulePredicate(command_signature="git"),
465+
pipeline=["half_trim"],
466+
)
467+
],
468+
)
469+
470+
result = await service.compress_messages(messages=messages, config=cfg)
471+
472+
assert result.messages[1].content == previously_compressed
473+
assert result.records[0].applied is False
474+
assert "skipped_already_processed_compression" in result.records[0].warnings
475+
476+
441477
@pytest.mark.asyncio
442478
async def test_service_skips_artifact_preview_system_reminder_outputs() -> None:
443479
registry = CompressionStrategyRegistry()

0 commit comments

Comments
 (0)