Skip to content

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

fix(compaction): reset breaker / anti-thrash counters in _clear_state

xf gong17a9bf4b66ae
diff --git a/cubepi/middleware/compaction/__init__.py b/cubepi/middleware/compaction/__init__.py
index 60b7e63..6a76267 100644
--- a/cubepi/middleware/compaction/__init__.py
+++ b/cubepi/middleware/compaction/__init__.py
@@ -68,8 +68,20 @@ def _load_state(value: Any) -> CompactionState | None:
def _clear_state(ctx: AgentContext) -> None:
+ """Drop every piece of compaction bookkeeping in ``ctx.extra``.
+
+ Called when the persisted summary / boundary is no longer trustworthy
+ (corrupt payload, boundary beyond history, refs mismatch from a replaced
+ history). The breaker / anti-thrash counters are tied to a specific
+ conversation; carrying them over to a fresh history would, for example,
+ skip the LLM on the first turn of a brand-new conversation because the
+ *previous* one had hit ``compaction_failures = 3``.
+ """
ctx.extra.pop("compaction", None)
ctx.extra.pop("compaction_until_msg_index", None)
+ ctx.extra.pop("compaction_failures", None)
+ ctx.extra.pop("compaction_low_savings_count", None)
+ ctx.extra.pop("compaction_fallback_runs", None)
def _load_int(value: Any, default: int) -> int:
diff --git a/tests/middleware/test_compaction.py b/tests/middleware/test_compaction.py
index 1b029e8..27e6897 100644
--- a/tests/middleware/test_compaction.py
+++ b/tests/middleware/test_compaction.py
@@ -719,3 +719,49 @@ async def test_corrupt_failure_counter_treated_as_zero() -> None:
# Compaction proceeded as if all counters were 0.
assert "compaction" in ctx.extra
assert ctx.extra["compaction_failures"] == 0 # success path
+
+
+async def test_state_invalidation_clears_guard_counters() -> None:
+ """When the persisted summary is invalidated (history replaced), the
+ breaker / anti-thrash counters must also reset — otherwise a fresh
+ conversation would skip the LLM on its first compaction because the
+ previous conversation hit MAX_FAILURES."""
+ provider = _FakeSummaryProvider(reply="summary")
+ middleware = _make_middleware(provider, max_tokens_before=1)
+ # New messages — refs in ctx.extra won't match, so state is invalidated.
+ messages: list[Message] = [
+ _user("brand new 1"),
+ _assistant("reply 1"),
+ _user("brand new 2"),
+ _assistant("reply 2"),
+ _user("brand new 3"),
+ _assistant("reply 3"),
+ ]
+ ctx = AgentContext(
+ system_prompt="",
+ messages=messages,
+ extra={
+ "compaction": CompactionState(
+ summary="stale from previous conversation",
+ summarized_message_refs=["sha256:does-not-match"],
+ ).model_dump(),
+ "compaction_until_msg_index": 1,
+ "compaction_failures": 3, # stale breaker — would gate LLM
+ "compaction_low_savings_count": 2, # stale guard — would skip
+ "compaction_fallback_runs": 99,
+ },
+ )
+
+ await middleware.transform_context(messages, ctx=ctx)
+
+ # The LLM was called (breaker counter cleared); fresh state written.
+ assert len(provider.calls) == 1
+ assert ctx.extra["compaction_failures"] == 0
+ # The new summary is real, not a fallback.
+ state = CompactionState.model_validate(ctx.extra["compaction"])
+ assert state.is_fallback is False
+ # Stale guard counters were cleared BEFORE the turn; the new turn may
+ # legitimately set them to small values. The point is that the STALE
+ # 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