Skip to content

CubeLoop — a Pythonic, async-native agent harness core designed for high performance, readability, and production-grade persistence.

fix(compaction): defer pruning until compaction is committed

xf gong65640f2c9dc0
diff --git a/cubepi/middleware/compaction/__init__.py b/cubepi/middleware/compaction/__init__.py
index 571c805..403da04 100644
--- a/cubepi/middleware/compaction/__init__.py
+++ b/cubepi/middleware/compaction/__init__.py
@@ -187,25 +187,17 @@ class CompactionMiddleware(Middleware):
if tokens_now < self._max_tokens_before:
return unpruned_compressed
- # We are going to compact — pre-prune old tool results now so the
- # summariser transcript and the post-compaction view shrink. Skip
- # the pruner entirely when ``prune_tool_outputs=False`` (audit-chain
- # agents that need full historical tool results preserved).
- pruned_messages = (
- prune_tool_results(messages, tail_start=tail_start)
- if self._prune_tool_outputs
- else list(messages)
- )
- compressed = _compressed_view(pruned_messages, state, boundary)
-
- # Find boundary before guards (needed for anti-thrash new-msgs check).
+ # Find boundary on the ORIGINAL messages. Pruning is deferred until
+ # we're committed to compacting — otherwise a bailout path (no safe
+ # boundary, or the anti-thrash guard firing) would silently return a
+ # pruned view with no state recording the loss.
new_boundary = safe_boundary(
messages,
tail_start=tail_start,
min_compact=max(self._min_compact, boundary + 1),
)
if new_boundary is None or new_boundary <= boundary:
- return compressed
+ return unpruned_compressed
# Circuit breaker — gates LLM only; fallback always runs.
failures = _load_int(ctx.extra.get("compaction_failures"), 0)
@@ -245,7 +237,16 @@ class CompactionMiddleware(Middleware):
enough_new = (new_boundary - boundary) >= _ANTI_THRASH_NEW_MSGS
if low_savings >= _MAX_LOW_SAVINGS and not force_emergency and not enough_new:
logger.debug("CompactionMiddleware: skipping — low savings guard active")
- return compressed
+ return unpruned_compressed
+
+ # Committed to compacting — apply pre-pruning now. Skip entirely when
+ # ``prune_tool_outputs=False`` (audit-chain agents that need full
+ # historical tool results preserved).
+ pruned_messages = (
+ prune_tool_results(messages, tail_start=tail_start)
+ if self._prune_tool_outputs
+ else list(messages)
+ )
if llm_allowed:
try:
diff --git a/tests/middleware/test_compaction.py b/tests/middleware/test_compaction.py
index 04d19be..a451438 100644
--- a/tests/middleware/test_compaction.py
+++ b/tests/middleware/test_compaction.py
@@ -865,3 +865,78 @@ async def test_under_threshold_does_not_silently_prune_tool_outputs() -> None:
# The original tool result content is intact in the returned view.
tool_result_msg = next(m for m in result if isinstance(m, ToolResultMessage))
assert tool_result_msg.content[0].text == big_text
+
+
+async def test_no_safe_boundary_does_not_silently_prune_tool_outputs() -> None:
+ """Codex round 6 P2: when over threshold but safe_boundary returns None
+ (no valid split point), the middleware must return original messages
+ rather than the pruned view."""
+ from cubepi.providers.base import ToolCall, ToolResultMessage
+
+ provider = _FakeSummaryProvider()
+ # Threshold low enough that the conversation exceeds it.
+ middleware = _make_middleware(provider, max_tokens_before=10, keep_tail_tokens=1000)
+ big_text = "x" * 5000 # > _PRUNE_KEEP_CHARS
+ # Only 2 messages — too short for safe_boundary to find a valid split.
+ messages: list[Message] = [
+ AssistantMessage(
+ content=[ToolCall(id="c1", name="read_file", arguments={"p": "x"})]
+ ),
+ ToolResultMessage(
+ tool_call_id="c1",
+ tool_name="read_file",
+ content=[TextContent(text=big_text)],
+ ),
+ ]
+ ctx = AgentContext(system_prompt="", messages=messages, extra={})
+
+ result = await middleware.transform_context(messages, ctx=ctx)
+
+ # No compaction (no valid boundary), no LLM call.
+ assert provider.calls == []
+ assert "compaction" not in ctx.extra
+ # The big tool result content must NOT have been silently pruned.
+ tool_result_msg = next(m for m in result if isinstance(m, ToolResultMessage))
+ assert tool_result_msg.content[0].text == big_text
+
+
+async def test_anti_thrash_guard_skip_does_not_silently_prune() -> None:
+ """Codex round 6 P2: when the anti-thrash guard fires, the returned view
+ must come from the un-pruned messages so the main model still sees the
+ full tool outputs."""
+ from cubepi.providers.base import ToolCall, ToolResultMessage
+
+ provider = _FakeSummaryProvider()
+ big_text = "x" * 5000 # 2500 tokens via approx_tokens
+ # Threshold chosen so raw_tokens (~2515) > threshold (compaction
+ # triggers) but raw < 1.5 × threshold (emergency override skipped) and
+ # the boundary advance stays under _ANTI_THRASH_NEW_MSGS=8.
+ middleware = _make_middleware(provider, max_tokens_before=2000)
+ messages: list[Message] = [
+ _user("turn 1"),
+ _assistant("reply 1"),
+ _user("turn 2"),
+ AssistantMessage(
+ content=[ToolCall(id="c1", name="read_file", arguments={"p": "x"})]
+ ),
+ ToolResultMessage(
+ tool_call_id="c1",
+ tool_name="read_file",
+ content=[TextContent(text=big_text)],
+ ),
+ _user("turn 3"),
+ ]
+ ctx = AgentContext(
+ system_prompt="",
+ messages=messages,
+ extra={"compaction_low_savings_count": 2}, # guard tripped
+ )
+
+ result = await middleware.transform_context(messages, ctx=ctx)
+
+ # Guard fired — no LLM call, no state written.
+ assert provider.calls == []
+ assert "compaction" not in ctx.extra
+ # The original big tool result is intact in the returned view.
+ tool_result_msg = next(m for m in result if isinstance(m, ToolResultMessage))
+ assert tool_result_msg.content[0].text == big_text