Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions kernel/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 55 additions & 2 deletions src/preflight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,25 @@ const EMERGENCY_FOLD_COVERAGE = MAX_PREFLIGHT_ROUNDS * 2 * CHUNK_FRACTION;
// covers ~250x the window at observed per-fold savings (~0.5-0.6x window) —
// far beyond any realistic resend size.
const EMERGENCY_ROUND_CAP = 512;
// #3573: client-patience wall budget for the LLM fold path. A summarization
// round costs 25-44s against a real upstream regardless of range size (#2682
// measurement), while the common client timeout class (dsh ~60s) only fits
// ONE such round — pre-fix the marathon kept admitting rounds until the
// client disconnected mid-round, wasting the in-flight summary call and
// leaving the user with an opaque 60s hang. Admission now stops BEFORE a
// round that would blow the budget (estimate: next round costs 2x the last
// one's measured wall time); the attempt's completed folds are already
// persisted, so the client's retry resumes from the smaller view and the
// marathon converges across attempts instead of dying on the same wall. The
// first round is always admitted (nothing measured yet), the digest regime is
// exempt (CPU-only, converges in seconds by design), and fast-upstream
// sessions never notice: their whole preflight finishes far under the budget.
const PREFLIGHT_WALL_BUDGET_MS = 50_000;
let preflightWallBudgetOverrideMs: number | null = null;
/** Test seam (#3091 pattern): override the wall-clock admission budget; null = production behavior. */
export function _setPreflightWallBudgetForTest(ms: number | null): void {
preflightWallBudgetOverrideMs = ms;
}
// Per-entry head-fragment length in digest form A (form B strips heads when
// even that does not fit the bounds).
const DIGEST_HEAD_CHARS = 120;
Expand Down Expand Up @@ -1769,6 +1788,12 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa
probeDead = false;
};
let rangesTried = 0;
// #3573: wall-clock tracking for client-patience round admission.
const wallStart = Date.now();
let lastRoundMs = 0;
let lastRoundStart = 0;
let wallBudgetHit = false;
let wallStopAfterRounds = 0;
// #2657×#2678 fusion: set when a round's whole external-summary batch died
// in the shared pool's queue (deadline, zero dispatched attempts). Later
// rounds would queue behind the same holders with fresh budgets each,
Expand Down Expand Up @@ -1817,7 +1842,7 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa
const tiersEnabled = (deps.config as ResolvedKernelConfig).tiers?.enabled !== false;
let tierRecoveryAttempted = false;
const eligibleForTierRecovery = (): boolean => {
if (!tiersEnabled || tierRecoveryAttempted || deps.signal?.aborted || emergencyFold) return false;
if (!tiersEnabled || tierRecoveryAttempted || deps.signal?.aborted || emergencyFold || wallBudgetHit) return false;
if (summaryCalls >= summaryBudget) return false;
const active = deps.session.state.blocks.filter((b) => b.active);
return active.filter((b) => b.tier === 1).length >= 2 || active.filter((b) => b.tier === 2).length >= 2;
Expand Down Expand Up @@ -2039,6 +2064,25 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa
break;
}
if (poolQueueDrop) break;
// #3573: client-patience admission (LLM fold path only). Rounds that
// make upstream calls are admitted only while the wall clock leaves
// room for the next one — see PREFLIGHT_WALL_BUDGET_MS above. Stopping
// here is progress, not failure: every completed fold is persisted, so
// the caller's fit gate either forwards the now-fitting payload or
// fails fast and the client retry resumes from the smaller view.
const admissionNow = Date.now();
if (lastRoundStart > 0) lastRoundMs = admissionNow - lastRoundStart;
if (round > 0 && !emergencyFold) {
const elapsedMs = admissionNow - wallStart;
const estNextMs = lastRoundMs > 0 ? lastRoundMs * 2 : 0;
if (elapsedMs + estNextMs > (preflightWallBudgetOverrideMs ?? PREFLIGHT_WALL_BUDGET_MS)) {
wallBudgetHit = true;
wallStopAfterRounds = round;
deps.log("warn", `[preflight] stopping after round ${round}: ${elapsedMs}ms elapsed + ~${estNextMs}ms for the next round exceeds the wall budget (${preflightWallBudgetOverrideMs ?? PREFLIGHT_WALL_BUDGET_MS}ms) — ${result.compressedRanges} fold(s) from this attempt are persisted; the client retry resumes from the smaller view (#3573)`);
break;
}
}
lastRoundStart = admissionNow;
// Re-run the pipeline each round: a successful compress hides its
// range behind a new block, changing the visible view; refs stay
// stable per-session snapshots (#387), but which ranges are
Expand Down Expand Up @@ -3066,7 +3110,16 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa
// minCompressRange" for a range that died before any summary call).
const unusableNote = lastUnusableDetail ? ` Last unusable summary: ${safePrefix(lastUnusableDetail, 300)}.` : "";
const skipNote = skipReasons.length > 0 ? ` Skipped: ${skipReasons.slice(0, 3).join(" | ")}.` : "";
if (budgetHit) {
if (wallBudgetHit) {
// #3573: the marathon stopped on the client-patience wall clock, not on a
// round or summary budget — say so. The payload may still overflow the
// window, but it CAN fit after more folding: every landed fold is persisted,
// so the client retry resumes from a smaller view. Never fall through to the
// "budget exhausted" wording below — and never into the relaxed deterministic
// branch (a 413 terminal verdict would kill exactly this stop's
// retry-convergence story); this stop is retryable by design.
failure = { kind: "exhausted", retryable: true, detail: `stopped at the client-patience wall budget after ${wallStopAfterRounds} round(s): ${result.compressedRanges} fold(s) from this attempt are persisted — the next request resumes from the smaller view and keeps folding (#3573)` };
} else if (budgetHit) {
failure = { kind: "exhausted", detail: `the preflight summarization budget (${summaryBudget} calls per protection regime) was exhausted before the payload fit the window${unusableNote}${skipNote}` };
} else if (relaxed && result.compressedRanges > 0) {
// #3461: "folded everything compressible" is only a structural
Expand Down
Loading
Loading