Skip to content

feat(compaction): pre-flight cascade spine + usage anchor (M4.0 / v4) - #25

Merged
wusijian007 merged 1 commit into
mainfrom
feat/m4.0-cascade-spine-usage-anchor
Jun 19, 2026
Merged

wusijian007 merged 1 commit into
mainfrom
feat/m4.0-cascade-spine-usage-anchor

Conversation

@wusijian007

Copy link
Copy Markdown
Owner

Lay the foundation for the v4 five-layer compaction pipeline: a pre-flight cascade whose budget trigger hangs on a usage anchor instead of a whole- transcript chars/4 guess. Structural refactor with zero behavior change — the spine carries the single existing stage (semantic when a summarizer is injected, else tiered); the real layers L1-L5 land in M4.1-M4.5.

Usage anchor (estimateAnchoredTokens): the last API usage gives the EXACT server-side prompt-token count of the strict-extension prefix that was already sent (system + tools + the first N messages); only the messages appended since are estimated (chars/4). Error stays small because the bulk is exact and only the recent delta is guessed (<5%). The loop re-anchors after every model turn from that turn's usage + the sent message count, and INVALIDATES the anchor on a compaction (the prefix was rewritten). Falls back to the full heuristic when there is no anchor or the transcript is shorter than the anchored prefix.

Cascade spine (runCompactionPipeline): runs CompactionStages cheapest-first and short-circuits as soon as the transcript is under target — same shape as the M3.5 done-gate chain. The proactive turn-boundary block now computes the anchored estimate, and on crossing the soft limit runs the pipeline toward the target ratio.

  • core: estimateAnchoredTokens + UsageAnchor, CompactionStage + runCompactionPipeline (context.ts); query loop re-anchors per turn, triggers off the anchor, invalidates on compaction.
  • eval: semantic-compaction's scripted turn-0 usage raised to match its seeded whale (the anchor now trusts server token counts, so fixture usage must be consistent with seeded content) — fingerprint in 14350 -> 15850, rest stable.
  • tests: 6 new (anchor exact-prefix + 2 fallbacks; pipeline short-circuit + no-op; anchor-driven trigger suppresses a compaction chars/4 would have fired).
  • docs: CLAUDE.md query-loop step 7 rewritten around the cascade + anchor; v4 roadmap M4.0 marked delivered.

226 tests pass (+6); all 10 eval tasks green.

Lay the foundation for the v4 five-layer compaction pipeline: a pre-flight
cascade whose budget trigger hangs on a usage anchor instead of a whole-
transcript chars/4 guess. Structural refactor with zero behavior change — the
spine carries the single existing stage (semantic when a summarizer is injected,
else tiered); the real layers L1-L5 land in M4.1-M4.5.

Usage anchor (estimateAnchoredTokens): the last API usage gives the EXACT
server-side prompt-token count of the strict-extension prefix that was already
sent (system + tools + the first N messages); only the messages appended since
are estimated (chars/4). Error stays small because the bulk is exact and only
the recent delta is guessed (<5%). The loop re-anchors after every model turn
from that turn's usage + the sent message count, and INVALIDATES the anchor on a
compaction (the prefix was rewritten). Falls back to the full heuristic when
there is no anchor or the transcript is shorter than the anchored prefix.

Cascade spine (runCompactionPipeline): runs CompactionStages cheapest-first and
short-circuits as soon as the transcript is under target — same shape as the
M3.5 done-gate chain. The proactive turn-boundary block now computes the
anchored estimate, and on crossing the soft limit runs the pipeline toward the
target ratio.

- core: estimateAnchoredTokens + UsageAnchor, CompactionStage +
  runCompactionPipeline (context.ts); query loop re-anchors per turn, triggers
  off the anchor, invalidates on compaction.
- eval: semantic-compaction's scripted turn-0 usage raised to match its seeded
  whale (the anchor now trusts server token counts, so fixture usage must be
  consistent with seeded content) — fingerprint in 14350 -> 15850, rest stable.
- tests: 6 new (anchor exact-prefix + 2 fallbacks; pipeline short-circuit +
  no-op; anchor-driven trigger suppresses a compaction chars/4 would have fired).
- docs: CLAUDE.md query-loop step 7 rewritten around the cascade + anchor;
  v4 roadmap M4.0 marked delivered.

226 tests pass (+6); all 10 eval tasks green.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@wusijian007
wusijian007 merged commit 0c49876 into main Jun 19, 2026
3 checks passed
@wusijian007
wusijian007 deleted the feat/m4.0-cascade-spine-usage-anchor branch June 19, 2026 12:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant