Skip to content

fix: auto-recover from a pre-flight context-window rejection instead of stalling - #477

Closed
Satya Arjunan (satya-arjunan) wants to merge 1 commit into
synthetic-sciences:mainfrom
satya-arjunan:fix/preflight-context-window-auto-retry
Closed

Satya Arjunan (satya-arjunan) wants to merge 1 commit into
synthetic-sciences:mainfrom
satya-arjunan:fix/preflight-context-window-auto-retry

Conversation

@satya-arjunan

@satya-arjunan Satya Arjunan (satya-arjunan) commented Sep 1, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #478

Problem

When the current turn's own unreducible content — contextPreflight()'s newest, the protected active-turn span plus tool schemas — alone exceeds the safe input budget (hard), the session hard-fails with:

This message cannot fit in <model>'s context window: the newest request plus
required instructions and tool schemas is estimated at N tokens, above the
safe input budget of M. Shorten or split the request, remove large
attachments, or choose a model with a larger context window. No provider
request was sent.

backend/cli/src/session/prompt.ts:

if (preflight.newest > preflight.hard) {
  await failTooLarge(...)
  break
}

That's it — the loop just stops. No retry, nothing automatic. In a real overnight run, a session stalled here for hours before an unrelated issue happened later — recovery required a human to notice and send literally anything (even just "resume") to unstick it.

That manual recovery works only because failTooLarge()'s error reply makes the failed turn terminal (SessionLoopState.terminalError treats any assistant reply with error as answered) — so it's no longer "protected" from compaction the next time around, and any new message triggers a fresh preflight where the previously-stuck bulk is now compactable. There's no reason that recovery needs a human in the loop; the session already has everything it needs to do this itself once the error is recorded.

Fix

Mirrors the existing overflowRecovery/outputRecovery pattern (backend/cli/src/session/loop-state.ts) already used for two structurally similar recoverable-failure cases in this same loop:

export function preflightRecovery(input: { attempts: number }): "none" | "compact" {
  return input.attempts > 0 ? "none" : "compact"
}

After recording the same error as before (unchanged, for transcript/observability — nothing about what the user sees on the first failure changes), the loop now automatically enqueues a synthetic "context" continuation (a new Continuation kind, same mechanism output/contract continuations already use) and retries once per epoch:

if (preflight.newest > preflight.hard) {
  await failTooLarge(...)
  if (SessionLoopState.preflightRecovery({ attempts: preflightCompactions }) === "compact") {
    preflightCompactions++
    await enqueue({ user: lastUser, kind: "context", epoch: turn, text: "..." })
    continue
  }
  break
}

A second rejection in the same epoch means the turn's own content doesn't fit even after everything else has been compacted away — that still hard-fails exactly as before, since no amount of compaction could ever help there.

Verification

  • New tests in test/session/loop-state.test.ts: preflightRecovery's cap-at-one-retry behavior, and restore()'s replay correctly recovering preflightCompactions from a synthetic context continuation after a session reload (mirrors the existing output-continuation restart test directly above it).
  • bun run typecheck — clean.
  • Full backend/cli suite: 2906 pass / 115 fail / 16 errors (3058 total) on this branch vs. 2900 pass / 116 fail / 16 errors (3053 total) on a clean main baseline — net +6 passing (this PR's 2 new tests plus normal variance), one fewer failure, identical error count. Same pre-existing, unrelated failures both times (confirmed the increase from an earlier ~90-fail baseline was entirely from two newly-merged, environment-dependent test suites unrelated to this change).
  • No existing full-loop integration harness exists for SessionPrompt's while(true) loop anywhere in this codebase — every other recovery path in it (overflowRecovery, outputRecovery, terminalError) is tested purely at the extracted-decision-function level, which this follows exactly.

…of stalling

When the current turn's own unreducible content (protected span + tool
schemas) alone exceeds the safe input budget, the session hard-failed with
"No provider request was sent" and just stopped - no retry, nothing
automatic. Recovering required a human to notice and send literally
anything (even "resume") to unstick it, which worked only because the
recorded error reply makes that turn terminal/answered and therefore no
longer protected from compaction on the next attempt.

Confirmed via a real overnight incident: a benchmark session stalled here
for hours before an unrelated desktop crash even happened, with nobody
around to send the nudge.

Adds SessionLoopState.preflightRecovery, mirroring the existing
overflowRecovery/outputRecovery pattern: after recording the same error as
before (unchanged, for transcript/observability), automatically enqueue a
synthetic "context" continuation once per epoch, which correctly sidesteps
terminalError's gate the same way an operator-sent message would. A second
rejection in the same epoch means the turn's own content doesn't fit even
after everything else has been compacted away, and still requires a real
user decision rather than looping forever.
@vercel

vercel Bot commented Sep 1, 2026

Copy link
Copy Markdown

Satya Arjunan (@satya-arjunan) is attempting to deploy a commit to the InkVell Team on Vercel.

A member of the Team first needs to authorize it.

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.

Pre-flight context-window rejection stalls the session permanently instead of retrying

2 participants