Skip to content

fix(runtime): estimate context at four bytes per token; let the summarizer past the window (C1) - #56

Merged
lg2m merged 1 commit into
mainfrom
fix/context-usability-1-estimator
Sep 16, 2026
Merged

lg2m merged 1 commit into
mainfrom
fix/context-usability-1-estimator

Conversation

@lg2m

@lg2m lg2m commented Sep 16, 2026

Copy link
Copy Markdown
Collaborator

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 /compact hit the same wall.

After: the same transcript sends (~182k estimated tokens). Window-triggered compaction runs and the prompt proceeds. /compact works 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 for qq 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. New CompactionDisposition::Summarizing skips 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 use Summarizing instead of AlreadyAttempted.
  • lib.rs in-run delta seed and claim.rs::compatible_context_tokens charge 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 warnings
  • New: context::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_step
  • New regressions: compaction::a_window_overflow_compacts_even_though_the_summarizer_reads_the_same_transcript, compaction::manual_compaction_runs_when_the_estimate_already_exceeds_the_window
  • Rescaled by the ratio: a_prompt_still_over_budget_after_compacting_fails_with_the_policy_outcome, a_compaction_that_does_not_shrink_the_assembly_never_loops, two occupancy-delta assertions

Measurements

None: behavioral change on the cold planning path; no hot-path code touched.

Compatibility

No protocol, schema, or descriptor change. Sessions whose persisted context_tokens came 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.md
  • docs/design/architecture.md § run loop step 3

…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.
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