Repository navigation
feat(compaction): pre-flight cascade spine + usage anchor (M4.0 / v4) - #25
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
226 tests pass (+6); all 10 eval tasks green.