Skip to content

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

fix(compaction): scope tail clamp to over-threshold + dedupe recursive fallback

xf gong905c3decfa58
diff --git a/cubepi/middleware/compaction/__init__.py b/cubepi/middleware/compaction/__init__.py
index 6a76267..fa7b460 100644
--- a/cubepi/middleware/compaction/__init__.py
+++ b/cubepi/middleware/compaction/__init__.py
@@ -167,16 +167,15 @@ class CompactionMiddleware(Middleware):
_clear_state(ctx)
# Single tail computation — shared by pruner and safe_boundary.
- # Clamp the effective tail budget to at most half the trigger
- # threshold. Without this, a configuration where
- # ``keep_tail_tokens`` exceeds ``max_tokens_before_compact``
- # produces ``tail_start == 0`` for any history that triggers
- # compaction — the tail swallows everything and ``safe_boundary``
- # finds nothing to summarise.
- effective_tail_tokens = min(
- self._keep_tail_tokens,
- max(1, self._max_tokens_before // 2),
- )
+ # The clamp below ONLY kicks in when ``keep_tail_tokens`` would
+ # swallow the entire triggering history (tail >= threshold). At
+ # smaller tail budgets the configured value is honoured verbatim:
+ # the caller knows their threshold and how much recent context
+ # they want preserved.
+ if self._keep_tail_tokens >= self._max_tokens_before:
+ effective_tail_tokens = max(1, self._max_tokens_before // 2)
+ else:
+ 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
diff --git a/cubepi/middleware/compaction/summarizer.py b/cubepi/middleware/compaction/summarizer.py
index 06e9e70..b1e9855 100644
--- a/cubepi/middleware/compaction/summarizer.py
+++ b/cubepi/middleware/compaction/summarizer.py
@@ -240,6 +240,40 @@ async def summarize(
)
+_FALLBACK_HEADER = "[Compaction fallback — LLM summariser unavailable]"
+_FALLBACK_USER_PREFIX = "User requests: "
+_FALLBACK_TOOL_PREFIX = "Tool calls: "
+
+
+def _parse_prior_fallback(summary: str) -> tuple[list[str], list[str]]:
+ """Extract user_lines and tool_names from a previous fallback summary.
+
+ Format is the one this module emits:
+
+ [Compaction fallback — LLM summariser unavailable]
+ User requests: line1; line2
+ Tool calls: bash, read_file
+
+ Returns ``([], [])`` if either marker is absent.
+ """
+ user_lines: list[str] = []
+ tool_names: list[str] = []
+ for line in summary.splitlines():
+ if line.startswith(_FALLBACK_USER_PREFIX):
+ user_lines = [
+ s.strip()
+ for s in line[len(_FALLBACK_USER_PREFIX) :].split(";")
+ if s.strip()
+ ]
+ elif line.startswith(_FALLBACK_TOOL_PREFIX):
+ tool_names = [
+ s.strip()
+ for s in line[len(_FALLBACK_TOOL_PREFIX) :].split(",")
+ if s.strip()
+ ]
+ return user_lines, tool_names
+
+
def build_fallback_summary(
messages_to_summarize: list[Message],
*,
@@ -256,6 +290,12 @@ def build_fallback_summary(
``ref_messages`` overrides the source for ID/SHA256-ref extraction —
mirrors the contract of :func:`summarize`. ``is_fallback=True`` is set
on the returned state.
+
+ When ``existing`` is itself a fallback, we re-derive its user_lines /
+ tool_names and merge with the current turn's instead of embedding the
+ prior fallback text verbatim. Embedding would compound through every
+ outage turn (run N includes run N-1 which includes N-2…), growing the
+ summary unboundedly and defeating compaction.
"""
ref_source = ref_messages if ref_messages is not None else messages_to_summarize
@@ -279,13 +319,21 @@ def build_fallback_summary(
):
tool_names.append(asst_block.name)
- parts: list[str] = ["[Compaction fallback — LLM summariser unavailable]"]
+ parts: list[str] = [_FALLBACK_HEADER]
if existing and existing.summary:
- parts.append(f"Prior context: {existing.summary}")
+ if existing.is_fallback:
+ # Merge the prior fallback's structured fields into the current
+ # turn — never embed the prior summary text. Preserves order
+ # (oldest user request first) and dedupes by exact match.
+ prior_users, prior_tools = _parse_prior_fallback(existing.summary)
+ user_lines = list(dict.fromkeys(prior_users + user_lines))[:5]
+ tool_names = list(dict.fromkeys(prior_tools + tool_names))
+ else:
+ parts.append(f"Prior context: {existing.summary}")
if user_lines:
- parts.append("User requests: " + "; ".join(user_lines))
+ parts.append(_FALLBACK_USER_PREFIX + "; ".join(user_lines))
if tool_names:
- parts.append("Tool calls: " + ", ".join(sorted(tool_names)))
+ parts.append(_FALLBACK_TOOL_PREFIX + ", ".join(sorted(tool_names)))
summary = "\n".join(parts)
diff --git a/tests/middleware/compaction/test_summarizer.py b/tests/middleware/compaction/test_summarizer.py
index 6b6ba1c..9257e87 100644
--- a/tests/middleware/compaction/test_summarizer.py
+++ b/tests/middleware/compaction/test_summarizer.py
@@ -479,3 +479,79 @@ def test_format_arguments_non_json_serialisable_falls_back_to_str() -> None:
# raises TypeError → fallback to str().
result = _format_arguments({"x": _NotJson()})
assert "<not-json>" in result
+
+
+# --- codex round 3: fallback does not recurse ---
+
+
+def test_fallback_summary_does_not_embed_prior_fallback() -> None:
+ """A fallback that follows another fallback must NOT embed the prior
+ fallback text under 'Prior context:'. Otherwise each LLM-outage turn
+ doubles the summary size — the chain compounds verbatim across runs."""
+ from cubepi.middleware.compaction.summarizer import build_fallback_summary
+
+ # Run 1: clean fallback.
+ msgs_1: list[Message] = [
+ UserMessage(content=[TextContent(text="task A")]),
+ AssistantMessage(
+ content=[ToolCall(id="c1", name="bash", arguments={"cmd": "ls"})]
+ ),
+ ]
+ state_1 = build_fallback_summary(msgs_1, existing=None)
+ assert state_1.is_fallback is True
+ assert "task A" in state_1.summary
+
+ # Run 2: NEW fallback whose existing is run 1's fallback.
+ msgs_2: list[Message] = [
+ UserMessage(content=[TextContent(text="task B")]),
+ AssistantMessage(
+ content=[ToolCall(id="c2", name="grep", arguments={"q": "x"})]
+ ),
+ ]
+ state_2 = build_fallback_summary(msgs_2, existing=state_1)
+
+ # The prior fallback text MUST NOT appear verbatim as a "Prior context:"
+ # line — that's the unbounded-growth pattern.
+ assert "Prior context:" not in state_2.summary
+ # But the structured fields from the prior fallback ARE preserved by
+ # merging into the new fallback's user_lines / tool_names.
+ assert "task A" in state_2.summary
+ assert "task B" in state_2.summary
+ assert "bash" in state_2.summary
+ assert "grep" in state_2.summary
+ # Tool names are deduplicated, not appended.
+ assert state_2.summary.count("bash") == 1
+
+
+def test_fallback_summary_user_lines_capped_after_merging() -> None:
+ """Cap of 5 user lines holds across the merge with a prior fallback."""
+ from cubepi.middleware.compaction.summarizer import build_fallback_summary
+
+ msgs_1 = [UserMessage(content=[TextContent(text=f"older {i}")]) for i in range(4)]
+ state_1 = build_fallback_summary(msgs_1, existing=None)
+
+ msgs_2 = [UserMessage(content=[TextContent(text=f"newer {i}")]) for i in range(4)]
+ state_2 = build_fallback_summary(msgs_2, existing=state_1)
+
+ # 4 prior + 4 new = 8 candidates, capped at 5.
+ user_line_section = [
+ ln for ln in state_2.summary.splitlines() if ln.startswith("User requests:")
+ ][0]
+ items = [
+ s.strip() for s in user_line_section.removeprefix("User requests: ").split(";")
+ ]
+ assert len(items) == 5
+
+
+def test_fallback_summary_after_real_summary_still_embeds_prior() -> None:
+ """When the prior was a REAL summary (is_fallback=False), the new
+ fallback still embeds it under 'Prior context:' — only fallback-after-
+ fallback is the unbounded-growth path."""
+ from cubepi.middleware.compaction.state import CompactionState
+ from cubepi.middleware.compaction.summarizer import build_fallback_summary
+
+ real_prior = CompactionState(summary="## Goal\nbuild the thing", is_fallback=False)
+ msgs = [UserMessage(content=[TextContent(text="next task")])]
+ state = build_fallback_summary(msgs, existing=real_prior)
+ assert "Prior context:" in state.summary
+ assert "build the thing" in state.summary
diff --git a/tests/middleware/test_compaction.py b/tests/middleware/test_compaction.py
index 27e6897..da9a040 100644
--- a/tests/middleware/test_compaction.py
+++ b/tests/middleware/test_compaction.py
@@ -765,3 +765,65 @@ async def test_state_invalidation_clears_guard_counters() -> None:
# values (2 and 99) did not carry through.
assert ctx.extra.get("compaction_low_savings_count", 0) < 2
assert ctx.extra.get("compaction_fallback_runs", 0) < 99
+
+
+# --- codex round 3: tail clamp only fires when tail would swallow history ---
+
+
+async def test_keep_tail_tokens_below_threshold_honoured_verbatim() -> None:
+ """A configured tail smaller than the threshold must NOT be silently
+ reduced. Codex P2: the original ``// 2`` clamp was over-eager."""
+ provider = _FakeSummaryProvider(reply="summary")
+ middleware = CompactionMiddleware(
+ summary_model=BoundModel(
+ provider=provider,
+ spec=Model(id="m", provider_id="faux"),
+ ),
+ max_tokens_before_compact=100,
+ keep_tail_tokens=80, # below threshold — must be honoured as-is
+ min_compact_messages=2,
+ )
+ # Build messages that, combined with a tail of 80 tokens, leave enough
+ # prefix to summarise. With keep_tail clamped to 50 (the OLD bug), the
+ # tail would protect more recent history than the caller wanted.
+ messages: list[Message] = [_user("x" * 60) for _ in range(8)]
+ ctx = AgentContext(system_prompt="", messages=messages, extra={})
+ await middleware.transform_context(messages, ctx=ctx)
+ # State written → boundary captured.
+ boundary = ctx.extra.get("compaction_until_msg_index")
+ assert boundary is not None and boundary > 0
+
+ # Compute what tail_start should be with the un-clamped 80-token budget.
+ # _user("x"*60) → 30 tokens each (60 chars / 2). tail budget 80 →
+ # last 2 messages fit (60 tokens, third would push to 90 > 80).
+ # Boundary should land at ≤ 6 (last 2 in tail).
+ # With the old `// 2` clamp (50 tokens), only 1 message fits; boundary
+ # would land at 7 — much more aggressive.
+ assert boundary <= 6
+
+
+async def test_keep_tail_tokens_above_threshold_still_clamped() -> None:
+ """Safety net is still active for the truly-broken configuration."""
+ provider = _FakeSummaryProvider(reply="summary")
+ middleware = CompactionMiddleware(
+ summary_model=BoundModel(
+ provider=provider,
+ spec=Model(id="m", provider_id="faux"),
+ ),
+ max_tokens_before_compact=10,
+ keep_tail_tokens=200, # above threshold — clamp engages
+ min_compact_messages=2,
+ )
+ messages: list[Message] = [
+ _user("turn 1"),
+ _assistant("reply 1"),
+ _user("turn 2"),
+ _assistant("reply 2"),
+ _user("turn 3"),
+ _assistant("reply 3"),
+ ]
+ ctx = AgentContext(system_prompt="", messages=messages, extra={})
+ await middleware.transform_context(messages, ctx=ctx)
+ # Clamp let compaction fire.
+ assert "compaction" in ctx.extra
+ assert len(provider.calls) == 1