Skip to content

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

fix(compaction): skip pre-pruning when no compaction is needed

xf gongfae78c780014
diff --git a/cubepi/middleware/compaction/__init__.py b/cubepi/middleware/compaction/__init__.py
index fa7b460..571c805 100644
--- a/cubepi/middleware/compaction/__init__.py
+++ b/cubepi/middleware/compaction/__init__.py
@@ -178,20 +178,26 @@ class CompactionMiddleware(Middleware):
effective_tail_tokens = self._keep_tail_tokens
tail_start = tail_start_by_tokens(messages, effective_tail_tokens)
- # Phase 1: pre-prune old tool results (cheap, no LLM call) — skip
- # entirely when prune_tool_outputs=False (audit-chain agents).
+ # Threshold check uses the UN-pruned view. Pre-pruning is a pre-pass
+ # for the summariser and the post-compaction tail; running it when no
+ # compaction is needed would silently hide tool outputs from the main
+ # model every turn, with no state recording the loss.
+ unpruned_compressed = _compressed_view(messages, state, boundary)
+ tokens_now = approx_tokens(unpruned_compressed)
+ 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)
- tokens_now = approx_tokens(compressed)
- if tokens_now < self._max_tokens_before:
- return compressed
-
# Find boundary before guards (needed for anti-thrash new-msgs check).
new_boundary = safe_boundary(
messages,
diff --git a/tests/middleware/test_compaction.py b/tests/middleware/test_compaction.py
index da9a040..04d19be 100644
--- a/tests/middleware/test_compaction.py
+++ b/tests/middleware/test_compaction.py
@@ -827,3 +827,41 @@ async def test_keep_tail_tokens_above_threshold_still_clamped() -> None:
# Clamp let compaction fire.
assert "compaction" in ctx.extra
assert len(provider.calls) == 1
+
+
+async def test_under_threshold_does_not_silently_prune_tool_outputs() -> None:
+ """Codex round 5 P2: when the un-pruned history is already under the
+ compaction threshold, the middleware must return the ORIGINAL messages,
+ not the pruned view. Otherwise old tool outputs are silently replaced
+ with one-liner placeholders on every turn, with no state recording the
+ loss."""
+ from cubepi.providers.base import ToolCall, ToolResultMessage
+
+ provider = _FakeSummaryProvider()
+ # Big threshold so the un-pruned history stays under it.
+ middleware = _make_middleware(provider, max_tokens_before=100_000)
+ big_text = "important detail " * 200 # >> _PRUNE_KEEP_CHARS
+ messages: list[Message] = [
+ _user("q"),
+ 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("more"),
+ _assistant("done"),
+ _user("another"),
+ ]
+ ctx = AgentContext(system_prompt="", messages=messages, extra={})
+
+ result = await middleware.transform_context(messages, ctx=ctx)
+
+ # No compaction happened.
+ assert provider.calls == []
+ assert "compaction" not in ctx.extra
+ # 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