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
22 changes: 18 additions & 4 deletions src/preflight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1966,9 +1966,14 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa
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;
};
type TierRecoveryResult = { recovered: boolean; callsUsed: number; netSaved: number; honestDetail: string };
type TierRecoveryResult = { recovered: boolean; callsUsed: number; attemptsUsed: number; netSaved: number; honestDetail: string };
const attemptTierRecovery = async (): Promise<TierRecoveryResult> => {
let callsUsed = 0;
// #3663: distillation dispatches committed, INCLUDING the ones that threw /
// timed out / came back unusable — callsUsed alone counts only completed
// folds, so a fully-failed recovery read as "0 summary call(s)" and hid
// the attempts the operator's logs show did happen.
let attemptsUsed = 0;
let netSaved = 0;
// #4227: the most recent measurement's nudge offer — pinned into the
// distill fold's ctx so its receipt tail judges from the same
Expand Down Expand Up @@ -2106,6 +2111,7 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa
break;
}
let outcome;
attemptsUsed += 1; // #3663: the dispatch is committed here — every post-dispatch exit (throw/unusable/drift/refuse/success) counts as an attempt
try {
outcome = await summarizeRange(deps, content, first.blockId, last.blockId, undefined, system);
} catch (error) {
Expand Down Expand Up @@ -2174,11 +2180,18 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa
: stopReason === "cap" ? `the bounded distillation call budget was reached after ${callsUsed} call(s)`
: stopReason === "unavailable" ? "the distillation summary was unavailable"
: "a distillation pass produced no further shrink";
// #3663: state attempted-vs-completed when some dispatch failed — "0 summary
// call(s)" read as "nothing was tried" although the logs show calls that
// threw/timed out. Byte-identical to the historical phrasing when every
// dispatched call completed.
const callCountPhrase = attemptsUsed > callsUsed
? `${callsUsed} of ${attemptsUsed} attempted summary call(s) completed`
: `${callsUsed} summary call(s)`;
const honestDetail =
`futile round: folding compressible raw messages plus tier distillation (${callsUsed} summary call(s), net ~${netSaved} tokens saved) still leaves the payload ~${overBy} tokens over the window — ${why}. ` +
`futile round: folding compressible raw messages plus tier distillation (${callCountPhrase}, net ~${netSaved} tokens saved) still leaves the payload ~${overBy} tokens over the window — ${why}. ` +
`These figures are measured on the post-fold wire view; the separately displayed context total additionally floors on the upstream usage baseline, which also bills the system prompt and tool definitions. ` +
`Raise the model context window or restart the session.`;
return { recovered, callsUsed, netSaved, honestDetail };
return { recovered, callsUsed, attemptsUsed, netSaved, honestDetail };
};
for (let round = 0; round < effectiveRoundCap; round++) {
if (deps.signal?.aborted) {
Expand Down Expand Up @@ -3226,7 +3239,8 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa
if (!tierRecoveryAttempted && eligibleForTierRecovery()) {
tierRecoveryAttempted = true;
const rec = await attemptTierRecovery();
deps.log("info", `[preflight] tier-distillation recovery finished: ${rec.recovered ? "recovered" : "not recovered"}; ${rec.callsUsed} summary call(s), net ~${rec.netSaved} tok saved (#2952)`);
const recCallPhrase = rec.attemptsUsed > rec.callsUsed ? `${rec.callsUsed} of ${rec.attemptsUsed} attempted summary call(s) completed` : `${rec.callsUsed} summary call(s)`;
deps.log("info", `[preflight] tier-distillation recovery finished: ${rec.recovered ? "recovered" : "not recovered"}; ${recCallPhrase}, net ~${rec.netSaved} tok saved (#2952, #3663)`);
if (!rec.recovered) {
failure = { kind: "exhausted", detail: rec.honestDetail };
}
Expand Down
32 changes: 30 additions & 2 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3731,6 +3731,31 @@ export function buildWindowShrinkNote(args: {
return head + `it was aligned down to codex's own window perception; set compress.modelContextLimit explicitly if your upstream serves the larger window.`;
}

/** #3663: label the wall the preflight fail-fast headline cites. When bili's own
* output-headroom reservation (handle.ts rewrites modelContextLimit in place and
* records NO shrink reason) is what sits between the cited wall and the model's
* native window, name it as the effective INPUT budget and point at the setting
* that controls it — calling that budget "the model window" made readers chase
* model metadata instead of compress.outputHeadroomMaxPct / max_tokens. Every
* other shape keeps today's wording byte-identical: operator/codex shrink reasons
* carry their own notes (#736), and a wall strictly below config.modelContextLimit
* means the stated-wall overflow arm (#3450) bound, not headroom. */
export function buildEffectiveWallClause(args: {
limit: number;
configuredLimit: number;
nativeWindow?: number;
shrinkReason: "operator" | "codex" | undefined;
}): string {
const { limit, configuredLimit, nativeWindow, shrinkReason } = args;
const headroomShrunk = shrinkReason === undefined
&& nativeWindow !== undefined
&& limit === configuredLimit
&& configuredLimit < nativeWindow;
return headroomShrunk
? `the effective input budget ${limit} (model window ${nativeWindow}, ${nativeWindow - configuredLimit} tokens reserved for model output via compress.outputHeadroomMaxPct)`
: `the model window ${limit}`;
}

// #5000/#4543: the core-text byte floor (estimateCoreMessagesUpperBytes) prices
// only m.text — tools[], tool_calls[].function.arguments, the system prompt and
// the JSON envelope are invisible to it, so a body can ride multi-MB over the
Expand Down Expand Up @@ -4141,9 +4166,12 @@ export async function preflightCompressIfNeeded(
const imageNote = remainingImageTokens >= limit
? ` Images alone account for ~${remainingImageTokens} estimated tokens (≥ window ${limit}); reduce the remaining image payload or raise the window.`
: "";
// #3663: label the cited wall by what actually set it (native window vs
// headroom-reserved effective input budget) instead of always "model window".
const wallClause = buildEffectiveWallClause({ limit, configuredLimit: config.modelContextLimit, nativeWindow: resolvedNativeWindow, shrinkReason: windowShrinkReason });
const sizeClause = foldedTokens !== undefined && foldedTokens < tokenCount
? `context ~${foldedTokens} tokens (down from ~${tokenCount} before preflight) exceeds the model window ${limit}`
: `context ~${tokenCount} tokens exceeds the model window ${limit}`;
? `context ~${foldedTokens} tokens (down from ~${tokenCount} before preflight) exceeds ${wallClause}`
: `context ~${tokenCount} tokens exceeds ${wallClause}`;
const rangesClause = rangesLeft !== undefined ? `, with ${rangesLeft} compressible range(s) still visible` : "";
// #736: when the wall is bili's own shrunken window, say so — "raise the
// model context window" otherwise sends operators to the upstream when
Expand Down
Loading
Loading