Repository navigation
fix(runtime): estimate context at four bytes per token; let the summarizer past the window (C1) - #56
Merged
Merged
Conversation
…mmarizer past the window (C1) The context planner charged one token per request byte whenever no provider measurement covered the request, so a ~730 KB transcript on a 200k-token model was rejected as "729498 input tokens". Every reference harness (Codex, pi, fx, OpenCode) estimates at four bytes per token; QQ now does the same (`ESTIMATED_BYTES_PER_TOKEN`, rounded up) for the fallback estimate, the in-run delta seed, and the cross-run occupancy seed. The summarizer's own request was planned against the model window it was asked to shrink, so a window-triggered compaction was refused before it started and the prompt failed with "compaction was already attempted" for a compaction that never ran; /compact hit the same wall. A new `CompactionDisposition::Summarizing` plans the summarizer against the storage backstop only and lets the provider adjudicate its fit. Rejection text now says "estimated", shows the byte basis, and names the recovery step (/compact, a new session, a durable session for `qq ask`). Tests: context planner unit tests rescaled to the ratio plus three new (ratio arithmetic with the reported case, summarizer against storage, recovery text); two session regressions (window-triggered auto-compaction completes and the prompt proceeds; manual compaction past the window completes); two existing window fixtures scaled; two occupancy-delta assertions updated. Docs: docs/plans/context-usability.md (new plan, C1–C6), its ledger, plan and progress indexes, architecture.md run-loop step 3.
This was referenced Sep 17, 2026
Merged
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.
C1: estimator at 4 bytes/token; summarizer planned against storage only; actionable rejection text
Spec: docs/plans/context-usability.md (new plan; this is the first of a six-PR stack C1→C6)
User-visible behavior
Before: a ~730 KB transcript on a 200k-token model failed with
estimated provider-neutral context requires 729498 input tokens plus a 16384-token output reserve….Once the estimate exceeded the window the session was unrecoverable: auto-compaction was refused, the failure said "compaction was already attempted" for a compaction that never started, and
/compacthit the same wall.After: the same transcript sends (~182k estimated tokens). Window-triggered compaction runs and the prompt proceeds.
/compactworks on a session whose estimate is already past the window. Every rejection says "estimated", shows the byte basis, and names the recovery (/compact, a new session, or a durable session forqq ask).What changed
sessions/context.rs:ESTIMATED_BYTES_PER_TOKEN = 4,estimate_tokens(ceil). Fallback estimate, irreducible-prefix check, and the compaction token target use it. NewCompactionDisposition::Summarizingskips the model-window check (the summarizer carries the transcript that overflowed; the provider adjudicates) and keeps the 4 MiB storage backstop. Rejection messages rewritten.sessions/execution.rs: the three compaction plan sites useSummarizinginstead ofAlreadyAttempted.lib.rsin-run delta seed andclaim.rs::compatible_context_tokenscharge appended bytes at the ratio instead of 1:1.Design decision: no tokenizer dependency; four bytes per token is what Codex (
APPROX_BYTES_PER_TOKEN), pi and OpenCode (chars / 4), and fx (ceil(span/4)) all use, with the output reserve and ceiling division keeping it conservative. A provider-reported overflow remains the authoritative backstop (existing compact-then-retry path unchanged).How verified
cargo test --workspace— all green (qq-core 597 passed / 2 ignored)cargo fmt --all -- --check,cargo clippy --workspace --all-targets --all-features -- -D warningscontext::tests::tokens_are_estimated_at_four_bytes_each_rounded_up(includes the reported 729,498 case),the_summarizer_request_is_planned_against_storage_only,rejection_messages_name_a_recovery_stepcompaction::a_window_overflow_compacts_even_though_the_summarizer_reads_the_same_transcript,compaction::manual_compaction_runs_when_the_estimate_already_exceeds_the_windowa_prompt_still_over_budget_after_compacting_fails_with_the_policy_outcome,a_compaction_that_does_not_shrink_the_assembly_never_loops, two occupancy-delta assertionsMeasurements
None: behavioral change on the cold planning path; no hot-path code touched.
Compatibility
No protocol, schema, or descriptor change. Sessions whose persisted
context_tokenscame from a provider are unaffected; only the fallback estimate changes.Docs
docs/plans/context-usability.md(new),docs/plans/progress/context-usability.md(new),docs/plans/README.md,docs/plans/progress/README.mddocs/design/architecture.md§ run loop step 3