CubeLoop — a Pythonic, async-native agent harness core designed for high performance, readability, and production-grade persistence.
Run-scoped Compaction & Real-token Triggering — Implementation Plan
For agentic workers: REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (
- [ ]) syntax for tracking.
Goal: Let CompactionMiddleware compress history within a single long
agentic run (not just between user turns), and trigger compaction on the real
context token count instead of a cache-blind char estimate.
Spec: dev/specs/2026-06-26-run-scoped-compaction.md
Architecture: Two surgical changes inside cubepi/middleware/compaction/, no
loop.py / agent.py / public-API changes:
boundary.py— drop theUserMessage-only gate insafe_boundary; rely on the existing suffix self-containment check. Turn-boundaryAssistantMessagecuts become legal, so the boundary advances inside a run.tokens.py+__init__.py— add a real-token estimate that prefers the most recentAssistantMessage.usage(summinginput + cache_read + cache_write) plus the estimated delta since; switch the trigger onto it; delete thescale_factorcalibration (its only job was absolute-vs-threshold, now served by real numbers).
A third spec item (§2.3, oversized tool results) ships docs only — the
existing after_tool_call seam is the mechanism; no code.
Tech Stack: Python 3.11+, Pydantic v2, pytest (asyncio_mode=auto), mypy
strict, FauxProvider for deterministic runs.
File Structure
- Modify
cubepi/middleware/compaction/boundary.py:42-74— removeUserMessagegate insafe_boundary; reword docstring. - Modify
cubepi/middleware/compaction/tokens.py— removescale_factorblock (lines 15, 24, 37-46, 53-54); addreal_context_estimate(messages). - Modify
cubepi/middleware/compaction/__init__.py:209-212— switch trigger fromapprox_tokens(...)toreal_context_estimate(...). - Modify
tests/middleware/compaction/test_boundary.py(or equivalent) — add run-scoped boundary cases. - Modify/Create
tests/middleware/compaction/test_tokens.py— real-token estimate + calibration-removal cases. - Modify
tests/middleware/compaction/test_*.py— update any test asserting aUserMessageboundary or relying on thescale_factor. - Modify
website/docs/guides/middleware/compaction.md— document in-run compaction, real-token triggering, and theafter_tool_callseam for oversized tool results.
Confirm exact test-file paths first:
ls tests/middleware/compaction/.
Task 1: Run-scoped boundary (§2.1)
Files:
Modify:
cubepi/middleware/compaction/boundary.pyTest:
tests/middleware/compaction/test_boundary.pyStep 1: Write the failing tests
Build a message list that mimics a long run with no intermediate
UserMessage:User(open), then repeatedAssistant(tool_use)+ToolResultpairs, plus a multi-tool turn (Assistant(tool_use A, tool_use B),ToolResult A,ToolResult B). Assert:safe_boundary(msgs, tail_start=…)returns an index pointing at a turn-startAssistantMessage(not pinned to the opening user message)._suffix_is_self_contained(msgs[boundary:])holds for the returned index.- The boundary is never between
ToolResult AandToolResult B, and never between atool_useand itsToolResult(parametrize over every index; accepted indices must all be self-contained). ToolResultMessageindices are never returned.- Existing user-boundary behaviour still works (regression: a normal multi-run history still cuts cleanly).
Step 2: Implement
In
safe_boundary(boundary.py:63-72), delete the gate:if not isinstance(messages[candidate], UserMessage): candidate -= 1 continueKeep the
_suffix_is_self_containedcheck and themin_compactguard. Reword the docstring (boundary.py:42-53): "searches the prefix for the latest self-contained turn boundary" (drop the "latestUserMessage" wording). Remove the now-unusedUserMessageimport if nothing else uses it.Step 3: Verify —
uv run pytest tests/middleware/compaction/test_boundary.py -v,uv run mypy cubepi,uv run ruff check cubepi/ tests/.
Task 2: Real-token trigger + remove calibration (§2.2)
Files:
Modify:
cubepi/middleware/compaction/tokens.py,cubepi/middleware/compaction/__init__.pyTest:
tests/middleware/compaction/test_tokens.pyStep 1: Write the failing tests
real_context_estimate(messages):- With a trailing
AssistantMessagecarryingusage(input_tokens=1000, cache_read_tokens=50_000, cache_write_tokens=2_000) and two messages appended after it, returns53_000 + approx_tokens(tail_two)— i.e. sums all three usage fields, notinput_tokensalone. - With no usage anywhere (cold start), falls back to
approx_tokens(messages). - Picks the most recent usage-bearing assistant when several exist.
- With a trailing
approx_tokensno longer applies a scale factor: anAssistantMessagewithusageproduces the same value as one without (purechars/2).- End-to-end via
FauxProviderreporting cache usage (faux.py:262-265): compaction triggers when the combined real count crosses the threshold even thoughinput_tokensalone is well under it.
Step 2: Implement
tokens.py: adddef real_context_estimate(messages: list[Message]) -> int: # walk backward to the most recent AssistantMessage with usage; # base = usage.input_tokens + usage.cache_read_tokens + usage.cache_write_tokens # return base + approx_tokens(messages_after_that_assistant) # no usage anywhere -> approx_tokens(messages)tokens.py: delete the calibration —scale_factorvar (line 24), theusagelookup + clamp block (lines 37-46), the* scale_factorreturn path (lines 53-54), and_SCALE_MIN_TOKENS(line 15).approx_tokensbecomes a pure char estimate. Keep the per-message-type char accounting intact.__init__.py:209-212: replace the trigger
withtokens_now = approx_tokens(unpruned_compressed) if tokens_now < self._max_tokens_before:tokens_now = real_context_estimate(unpruned_compressed). Leaveraw_tokens(anti-thrash,__init__.py:256),tokens_after(__init__.py:346),tail_start_by_tokens, and the summary budget onapprox_tokens— those are relative/budget decisions where a consistent bias cancels.
Step 3: Verify —
uv run pytest tests/middleware/compaction/ -v,uv run mypy cubepi,uv run ruff check. Note any tail-size drift caused by uncalibratedapprox_tokensand confirm anti-thrash tests still pass (retune only if red — see Open Q2).
Task 3 (CONDITIONAL — gated on spec Open Q1): threshold auto-binding
Implement only if the user chooses to auto-default the threshold. Otherwise skip.
- Default
max_tokens_before_compactfrom the bound model when omitted:int(ratio * model.context_window)(providers/base.py:91), ratio per the user's answer. Add a test that an unset threshold derives fromcontext_windowand an explicit threshold still wins. Document the default in the docs page.
Task 4: Documentation (§2.3 seam + the two features)
Files:
Modify:
website/docs/guides/middleware/compaction.mdStep 1: Document in-run compaction — compaction now advances the summary boundary at turn boundaries inside a single run, not only between user turns.
Step 2: Document real-token triggering — the trigger uses the true context fill (
input + cache_read + cache_writefrom the last turn's usage), so thresholds behave correctly under prompt caching.Step 3: Add a short "Bounding oversized tool results" note: this is the application's responsibility via an
after_tool_callmiddleware (linkhooks.md), because cubepi is environment-agnostic and cannot assume where to persist spilled content. Show a minimal sketch that previews + replaces a large result usingAfterToolCallResult.content.
Final verification
-
uv run pytest tests/(full suite green). -
uv run ruff check cubepi/ tests/anduv run ruff format --check cubepi/ tests/. -
uv run mypy cubepi. - Re-read the spec's §5 test-plan checklist; confirm every bullet has a test.
- Docs updated (feature is not done without docs — CLAUDE.md §4).
Open decisions to confirm before/while implementing
- Threshold auto-binding (Task 3) — do it now and with what ratio, or keep
max_tokens_before_compactexplicit? - Anti-thrash tuning — keep
_ANTI_THRASH_NEW_MSGS = 8for the tighter in-run cadence (default: keep, verify via tests), or retune?