Skip to content

fix(runtime): decide in-run context recovery on the measured estimate - #195

Merged
lg2m merged 1 commit into
mainfrom
fix/eng-940-measured-context-recovery
Sep 26, 2026
Merged

lg2m merged 1 commit into
mainfrom
fix/eng-940-measured-context-recovery

Conversation

@lg2m

@lg2m lg2m commented Sep 26, 2026

Copy link
Copy Markdown
Collaborator

Fixes ENG-940.

User-visible behavior

Long code-heavy runs no longer die with context is estimated at N input tokens (B bytes) plus a 16384-token output reserve, over the selected model's window; no earlier history remains to compact when in-run compaction could have saved them. Eight production runs failed this way on 2026-09-25, all with context_compaction_attempted = 0 after 21–1,091 completed turns; the reported request was 968,606 bytes / 256,359 tokens against a 272,000-token window.

Cause

The run loop in qq-core/src/lib.rs triggered stale-result stubbing and in-run compaction from the raw estimate_tokens(input_bytes) (bytes/4), while the session admission guard judged the Prepared weight by compatible_input_tokens: the calibrated measured chain from RR7. On code-heavy transcripts (~3 B/t) the two diverge by a third. The loop said "fits", skipped recovery, and the guard failed the run closed.

Design

One estimate_input_tokens closure computes the measured-chain figure (or the byte ratio when no compatible measurement covers the request) and every decision on the turn uses it: the stub trigger, the compaction trigger, and the weight handed to admission. The loop and the guard can no longer disagree. The chain is passed by value so clearing it after an in-run summary is visible to the next estimate. No schema or protocol change.

Verification

  • New regression a_dense_tokenizer_compacts_in_run_instead_of_failing_at_the_guard: a Measured harness wrapper reports usage at 2 B/t over 24 mutating turns in a 16k window. Fails on main with the exact production message; passes here with ≥1 in-run compaction and every prompt-run request under the window by the provider's own ratio.
  • cargo test --workspace: all green (735 qq-core, 0 failures workspace-wide).
  • cargo clippy --workspace --all-targets --all-features -- -D warnings clean; cargo fmt --check clean.

Performance

Hot path unchanged: the same arithmetic runs once per turn instead of twice (the compatible_input_tokens computation moved earlier and is reused). cargo bench -p qq-core --bench plan_compile unchanged (compile 23,756 ns/op, digest 2,292 ns/op).

Follow-up

ENG-952: an empty completed turn with no usage settles completed instead of retrying as a provider fault. Stacked on this branch next.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 26, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-26T19:07:12.360209Z dff7485 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

… (ENG-940)

The run loop triggered stale-result stubbing and in-run compaction from the
raw bytes/4 estimate while the session admission guard judged the prepared
weight by the calibrated measured chain. On code-heavy transcripts (~3 B/t)
the two diverge by a third: the loop said the request fit, skipped recovery,
and the guard failed the run closed. Eight production runs on 2026-09-25
failed this way with `context_compaction_attempted = 0` after 21–1,091
completed turns; the reported request was 968,606 bytes / 256,359 tokens
against a 272,000 window.

One `estimate_input_tokens` closure now produces the figure every decision
on the turn shares: the stub trigger, the compaction trigger, and the
`compatible_input_tokens` handed to admission. The chain is passed by value
so clearing it after an in-run summary is visible to the next estimate.

Regression: `a_dense_tokenizer_compacts_in_run_instead_of_failing_at_the_guard`
uses a new `Measured` harness wrapper that reports usage at 2 B/t over 24
mutating turns in a 16k window. It fails on the previous code with the exact
production message and passes with at least one in-run compaction and every
prompt-run request under the window by the provider's own ratio.

No schema or protocol change. `plan_compile` benchmark unchanged.
@lg2m
lg2m force-pushed the fix/eng-940-measured-context-recovery branch from a7d22f5 to dff7485 Compare September 26, 2026 19:03
@lg2m
lg2m merged commit d49507c into main Sep 26, 2026
4 checks passed
@lg2m
lg2m deleted the fix/eng-940-measured-context-recovery branch September 26, 2026 19:04
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