From 038334f183ce352f7866d863a5678562dd24acda Mon Sep 17 00:00:00 2001 From: ework-agent Date: Wed, 14 Oct 2026 01:49:24 +0800 Subject: [PATCH 1/3] fix: floor the ceiling-less starved output budget at an absolute minimum (#3071) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #3071: on a model with NO declared output ceiling, the proxy's starved-budget restore used the client's own last non-starved high-water as its only lower bound. That estimate decays unbounded from the client's raw-history size (..., 3071, 2061, 252, <=200), so once starved the session pins at a death rattle and every turn truncates at max-tokens (finish=length, 0 visible chars); a fresh fork child inherits the rattle and forwards max_tokens=1 verbatim. On v0.1.195 this blanked 9 turns across 3 DSH sessions in one day. Fix (minimal — matches the issue's suggestion #1 literally): when no source knows a ceiling, the STARVED / born-starved / fork-inherited restore target is floored at OUTPUT_RESTORE_ABSOLUTE_FLOOR (8192). A non-starved value is still trusted verbatim (client's explicit choice; far smaller blast radius), and a known-ceiling model is untouched. The no-ceiling WARN dedup moves from once-per-model (global) to once-per-session+model so each affected session logs its own WARN instead of staying silent behind another session's first hit. Paths fixed: 1 (no-ceiling restore pinned at poisoned high-water), 2a (killed session got zero warns), 2b (fork child forwarded the starved budget verbatim). Path 3 (headroom reserving the shrunken budget) needs no change: the restore runs before the headroom reservation reads max_tokens, so the floored value flows in automatically. Deferred to owner sign-off (product decisions / pinned fail-closed behavior, flagged in-thread): #4 invalidating the #2490 dsh planning floor after a refused replay (contradicts its sticky-by-design overflow guarantee), and #5 retrying a starved finish=length turn (reverses #2756's deliberate exclusion; the root-cause floor fix should remove most of those empty turns first). Closes #3071 --- src/server.ts | 2 +- src/server/side-request.ts | 70 +++++++++++--- tests/side-request-isolation.test.ts | 139 ++++++++++++++++++++------- 3 files changed, 159 insertions(+), 52 deletions(-) diff --git a/src/server.ts b/src/server.ts index 2d1726751e..77f08ad72f 100644 --- a/src/server.ts +++ b/src/server.ts @@ -3389,5 +3389,5 @@ function logMsg(opts: ProxyOptions, level: string, msg: string): void { export { getUnrecognizedPathStats, logDumpFailure, logUnrecognizedPath } from "./server/observability.js"; export { BILI_HOP_HEADER, parseLauncherModelWindows, anthropicBetaContextWindow, capRegistryWindowByStandard, expandedContextSuffixWindow } from "./server/context-window.js"; -export { BILI_TOOL_NAMES, isSideRequest, outputBudgetField, restoreOutputBudget, sideRequestGuard, stripLeakedBiliTools, _resetNoOutputCeilingWarningsForTest, type OutputBudgetField } from "./server/side-request.js"; +export { BILI_TOOL_NAMES, isSideRequest, outputBudgetField, restoreOutputBudget, sideRequestGuard, stripLeakedBiliTools, _resetNoOutputCeilingWarningsForTest, OUTPUT_RESTORE_ABSOLUTE_FLOOR, type OutputBudgetField } from "./server/side-request.js"; export { countSystemAndToolsTokens, estimateInputTokens, estimateWireOverhead, clampOutputBudget, emergencyNudge, projectThinkingMass, type ThinkingMassInput } from "./server/budget.js"; diff --git a/src/server/side-request.ts b/src/server/side-request.ts index 68288b8a34..a73548c69c 100644 --- a/src/server/side-request.ts +++ b/src/server/side-request.ts @@ -308,21 +308,41 @@ export function writeOutputBudget(parsed: Record, field: Output parsed.generationConfig = { ...(gen && typeof gen === "object" ? (gen as Record) : {}), maxOutputTokens: tokens }; } -// #1840: one-shot "restore has no ceiling" warnings, keyed by MODEL (the fact -// is model-scoped — any session hitting the same ceiling-less model repeats -// the same silent degradation). Capped like warnedNoModelRequests: an unbounded -// set would grow one entry per dead model id on long-running daemons. +// #3071: absolute capability floor for the output budget when NO source knows a +// ceiling for the model. The client's raw-history-derived budget decays unbounded +// (…, 3071, 2061, 252, <=200); "last non-starved wins" then pins the session at a +// death rattle with NO lower bound, and a fresh fork child inherits that rattle and +// forwards it verbatim (max_tokens=1 out the door). Floor the forwarded budget at +// this minimum so a ceiling-less model can never truncate every reply below the +// point where the model can even emit a compress call or short visible text. It is +// a LOWER bound only — a healthy client value above it passes through untouched. +// Flat (not window-relative): restoreOutputBudget runs BEFORE the window is resolved +// at its call site, and both the downstream #453 clamp and the output-headroom +// reservation (which self-caps at 25% of window) bound the result by real headroom +// regardless of this floor. +export const OUTPUT_RESTORE_ABSOLUTE_FLOOR = 8192; + +// #1840/#3071: one-shot "restore has no ceiling" warnings, keyed by SESSION+MODEL. +// #1840 keyed them by MODEL alone, but the degradation is SESSION-scoped: the +// session actually being starved got no warn because another session hit the same +// ceiling-less model first (#3071 path 2a). Keying by session+model gives each +// affected session exactly one WARN naming itself while staying bounded (the cap +// limits the set size, not the turn count). const warnedNoOutputCeiling = new Set(); const WARNED_NO_OUTPUT_CEILING_CAP = 4096; -function noteNoOutputCeilingWarning(model: string): boolean { - if (warnedNoOutputCeiling.has(model)) return false; +function noteNoOutputCeilingWarning(sessionId: string, model: string): boolean { + const key = `${sessionId}|${model}`; + if (warnedNoOutputCeiling.has(key)) return false; if (warnedNoOutputCeiling.size >= WARNED_NO_OUTPUT_CEILING_CAP) warnedNoOutputCeiling.clear(); - warnedNoOutputCeiling.add(model); + warnedNoOutputCeiling.add(key); return true; } export function _resetNoOutputCeilingWarningsForTest(): void { warnedNoOutputCeiling.clear(); } +function noCeilingWarnText(sessionId: string, model: string, from: number, to: number): string { + return `[${sessionId}] output-budget restore has NO known output ceiling for model=${model} (raised ${from} -> ${to}) — the target was the client's own raw-history estimate, which decays unbounded; while it stays pathologically small every turn truncates at max-tokens. Declare the model's max output in the bili config (providers..models."${model}".output) or have the client report it to floor the restore higher (#1840/#3071)`; +} // #2904: the no-ceiling degraded state surfaced by the /acp panel, the // /__bili/plugin/status?conversationId= JSON and the web session page while it holds. #1840's @@ -357,9 +377,13 @@ function clearNoCeilingDegraded(session: { metadata: Record }, * still bounds the result by real window headroom. A ceiling at/below the * side-request threshold is not usable; a healthy water mark above the * ceiling keeps winning (it is a capability floor, not an instruction cap). - * When NO source knows a ceiling, warn once per model: the restore is backed - * only by the client's own last non-starved value, which is exactly the - * silent-degradation path this issue reports. */ + * #3071: when NO source knows a ceiling, a STARVED / born-starved / + * fork-inherited value must never leave the proxy below + * OUTPUT_RESTORE_ABSOLUTE_FLOOR — the restore target is floored there so a + * ceiling-less model can never pin the session at a death rattle (the client's + * own decaying high-water is itself the poisoned quantity). A non-starved value + * is always trusted and recorded verbatim as the high-water. The no-ceiling + * condition warns once per SESSION+model (the degradation is session-scoped). */ export function restoreOutputBudget( parsed: unknown, session: { id: string; metadata: Record }, @@ -372,6 +396,9 @@ export function restoreOutputBudget( const value = readOutputBudget(p, field); if (value === undefined) return; const modelName = typeof p.model === "string" && p.model.length > 0 ? p.model : "?"; + // A usable ceiling is known from some source (runtime-info / launcher / + // configured route / registry); a value at/below the side threshold is not usable. + const ceiling = typeof outputCeiling === "number" && outputCeiling > SIDE_REQUEST_MAX_TOKENS ? outputCeiling : undefined; if (value > SIDE_REQUEST_MAX_TOKENS) { session.metadata.outputBudgetHighWater = value; // #2904: a healthy (non-starved) budget means the client's own estimate @@ -380,24 +407,35 @@ export function restoreOutputBudget( clearNoCeilingDegraded(session, modelName); return; } + // Starved main request (tiny budget + tools present). A side request (no tools) + // never reaches here — restoring it would break its side status. if (!Array.isArray(p.tools) || p.tools.length === 0) return; const highWaterRaw = session.metadata.outputBudgetHighWater; const highWater = typeof highWaterRaw === "number" && highWaterRaw > SIDE_REQUEST_MAX_TOKENS ? highWaterRaw : undefined; let target = highWater; - const ceiling = typeof outputCeiling === "number" && outputCeiling > SIDE_REQUEST_MAX_TOKENS ? outputCeiling : undefined; - if (ceiling !== undefined && (target === undefined || ceiling > target)) target = ceiling; + if (ceiling !== undefined && (target === undefined || ceiling > target)) { + target = ceiling; + } else if (ceiling === undefined) { + // #3071 path 1 + path 2b: a starved budget must never leave the proxy + // verbatim. A fresh fork child inherits the parent's rattle with no + // high-water of its own, so this branch guarantees max_tokens is always + // raised to at least the capability floor when no ceiling is known. + if (target === undefined || OUTPUT_RESTORE_ABSOLUTE_FLOOR > target) target = OUTPUT_RESTORE_ABSOLUTE_FLOOR; + } if (typeof target === "number") { writeOutputBudget(p, field, target); - const note = target === ceiling && ceiling !== undefined + const note = ceiling !== undefined && target === ceiling ? (highWater === undefined ? "; no healthy high-water yet — using known output ceiling (#1665/#1840)" : `; high-water ${highWater} below known output ceiling — floored (#1665/#1840)`) - : ""; + : (ceiling === undefined ? "; no known output ceiling — floored to the absolute capability minimum (#3071)" : ""); log("info", `[${session.id}] output budget restored ${value} -> ${target} (#546: client shrank it from its raw-history estimate${note})`); } // #2904: persist the no-ceiling degraded state for the user-visible surfaces. + // With the #3071 absolute floor a ceiling-less starve is ALWAYS restored (never + // forwarded verbatim), so target is always a number in that branch. if (ceiling !== undefined) clearNoCeilingDegraded(session, modelName); else session.metadata.outputBudgetDegraded = { model: modelName, value: typeof target === "number" ? target : value, ts: Date.now() }; - if (ceiling === undefined && noteNoOutputCeilingWarning(modelName)) { - log("warn", `[${session.id}] output-budget restore has NO known output ceiling for model=${modelName}${typeof target === "number" ? ` (restored ${value} -> ${target})` : ` (starved budget ${value} forwarded verbatim)`} — the target is the client's own last non-starved budget; while it stays pathologically small every turn truncates at max-tokens. Declare the model's max output in the bili config (providers..models."${modelName}".output) or have the client report it to floor the restore (#1840)`); + if (ceiling === undefined && noteNoOutputCeilingWarning(session.id, modelName)) { + log("warn", noCeilingWarnText(session.id, modelName, value, typeof target === "number" ? target : value)); } } diff --git a/tests/side-request-isolation.test.ts b/tests/side-request-isolation.test.ts index 9f262d1402..459a6a0ea6 100644 --- a/tests/side-request-isolation.test.ts +++ b/tests/side-request-isolation.test.ts @@ -3,7 +3,7 @@ import assert from "node:assert/strict"; import http from "node:http"; import { once } from "node:events"; import { defaultConfig, createInitialState, defaultCountTokens } from "acp-kernel"; -import { startServer, isSideRequest, outputBudgetField, restoreOutputBudget, sideRequestGuard, resolveKnownOutputCeiling, _resetNoOutputCeilingWarningsForTest } from "../src/server.ts"; +import { startServer, isSideRequest, outputBudgetField, restoreOutputBudget, sideRequestGuard, resolveKnownOutputCeiling, _resetNoOutputCeilingWarningsForTest, OUTPUT_RESTORE_ABSOLUTE_FLOOR } from "../src/server.ts"; import type { ProxyOptions } from "../src/config.ts"; import { recordPluginRuntimeInfo, _resetPluginStateForTest } from "../src/plugin.ts"; import { estimateRawBodyTokens } from "../src/preflight.ts"; @@ -93,22 +93,26 @@ test("restoreOutputBudget: high-water learning + starved-budget restore (#546)", const starved = { max_tokens: 16, tools: [{ name: "compress" }] } as { max_tokens: number; tools: unknown[] }; restoreOutputBudget(starved, s, noopLog); assert.equal(starved.max_tokens, 32768, "starved budget restored to high-water"); - // No high-water yet + starved + tools → nothing to restore to, untouched. + // No high-water yet + starved + tools + NO ceiling → #3071 absolute floor: a + // born-starved / fork-inherited rattle is never forwarded verbatim. const fresh = metaSession("fresh"); const noWater = { max_tokens: 16, tools: [{ name: "compress" }] }; restoreOutputBudget(noWater, fresh, noopLog); - assert.equal(noWater.max_tokens, 16); + assert.equal(noWater.max_tokens, OUTPUT_RESTORE_ABSOLUTE_FLOOR, "born-starved floored to the absolute minimum (#3071)"); // Side request (tiny budget, NO tools) → never touched, stays a side req. const side = { max_tokens: 100 }; restoreOutputBudget(side, s, noopLog); assert.equal(side.max_tokens, 100, "side request budget must not be restored"); assert.equal(isSideRequest(side), true); - // High-water re-learns downward on a new healthy budget (config change). + // A non-starved value is always trusted and recorded verbatim as the + // high-water (even below the absolute floor) — only a STARVE consults it. restoreOutputBudget({ max_tokens: 8000 }, s, noopLog); - assert.equal(s.metadata.outputBudgetHighWater, 8000, "last non-starved value wins"); + assert.equal(s.metadata.outputBudgetHighWater, 8000, "non-starved value recorded verbatim as high-water"); const starved2 = { max_tokens: 200, tools: [{ name: "t" }] } as { max_tokens: number }; restoreOutputBudget(starved2, s, noopLog); - assert.equal(starved2.max_tokens, 8000, "boundary 200 restored too (isSideRequest would misfire without tools)"); + assert.equal(starved2.max_tokens, OUTPUT_RESTORE_ABSOLUTE_FLOOR, "boundary 200 starve floored to the absolute minimum (#3071)"); + restoreOutputBudget({ max_tokens: 32768 }, s, noopLog); + assert.equal(s.metadata.outputBudgetHighWater, 32768, "a later healthy value wins as high-water"); // Field-name fidelity: restore writes the SAME field the client used. const s2 = metaSession("hw2"); restoreOutputBudget({ max_output_tokens: 32689 }, s2, noopLog); @@ -124,7 +128,7 @@ test("restoreOutputBudget: configured-output-limit floor for poisoned or missing restoreOutputBudget({ max_tokens: 384000 }, s, noopLog); restoreOutputBudget({ max_tokens: 680 }, s, noopLog); restoreOutputBudget({ max_tokens: 234 }, s, noopLog); - assert.equal(s.metadata.outputBudgetHighWater, 234, "last non-starved value wins (decay tracked)"); + assert.equal(s.metadata.outputBudgetHighWater, 234, "last non-starved value wins (decay tracked verbatim)"); const starved = { max_tokens: 1, tools: [{ name: "t" }] } as { max_tokens: number }; restoreOutputBudget(starved, s, noopLog, 384000); assert.equal(starved.max_tokens, 384000, "death-rattle water mark floored by the configured output limit"); @@ -139,55 +143,61 @@ test("restoreOutputBudget: configured-output-limit floor for poisoned or missing const bornDead = { max_tokens: 1, tools: [{ name: "t" }] } as { max_tokens: number }; restoreOutputBudget(bornDead, s3, noopLog, 32768); assert.equal(bornDead.max_tokens, 32768, "missing water mark falls back to the configured limit"); - // No configured limit → old behavior: nothing to restore to. + // No configured limit → no ceiling → the #3071 absolute floor applies (the + // starved budget is never forwarded verbatim). const s4 = metaSession("nofloor"); const noFloor = { max_tokens: 1, tools: [{ name: "t" }] } as { max_tokens: number }; restoreOutputBudget(noFloor, s4, noopLog); - assert.equal(noFloor.max_tokens, 1, "undefined configured limit leaves the request untouched"); - // A limit at/below the side threshold is not a usable floor. + assert.equal(noFloor.max_tokens, OUTPUT_RESTORE_ABSOLUTE_FLOOR, "no ceiling → absolute floor, not verbatim (#3071)"); + // A limit at/below the side threshold is not a usable ceiling — the absolute + // floor still applies. const s5 = metaSession("tinyfloor"); restoreOutputBudget({ max_tokens: 234 }, s5, noopLog); const tinyFloor = { max_tokens: 1, tools: [{ name: "t" }] } as { max_tokens: number }; restoreOutputBudget(tinyFloor, s5, noopLog, 200); - assert.equal(tinyFloor.max_tokens, 234, "limit <= 200 cannot floor"); + assert.equal(tinyFloor.max_tokens, OUTPUT_RESTORE_ABSOLUTE_FLOOR, "limit <= 200 is not a usable ceiling — absolute floor applies"); // Side requests (no tools) never receive the floor. const side = { max_tokens: 100 } as { max_tokens: number }; restoreOutputBudget(side, s3, noopLog, 32768); assert.equal(side.max_tokens, 100, "side request untouched even with a floor available"); }); -test("restoreOutputBudget: warns once per model when no output ceiling backs the restore (#1840)", () => { +test("restoreOutputBudget: warns once per SESSION+model when no output ceiling backs the restore (#1840/#3071)", () => { _resetNoOutputCeilingWarningsForTest(); const lines: string[] = []; const log = (_lvl: string, msg: string): void => { lines.push(msg); }; const tools = [{ name: "t" }]; const A = "stepfun/step-5-preview"; - // Poisoned water mark seeded by a decaying client; NO ceiling from any source. + // Decaying client, NO ceiling from any source. The non-starved value is + // trusted verbatim (recorded as high-water, silently); the STARVE is what + // gets floored to the absolute minimum — and that is where the warn fires. const s = metaSession("nocap"); restoreOutputBudget({ max_tokens: 583, model: A }, s, log); - assert.equal(lines.length, 0, "healthy-value learning is silent"); + assert.equal(s.metadata.outputBudgetHighWater, 583, "non-starved learning is silent and tracked verbatim"); const starved = { max_tokens: 1, tools, model: A } as { max_tokens: number }; restoreOutputBudget(starved, s, log); - assert.equal(starved.max_tokens, 583, "without a ceiling the (poisoned) water mark still wins"); + assert.equal(starved.max_tokens, OUTPUT_RESTORE_ABSOLUTE_FLOOR, "ceiling-less starve floored to the absolute minimum (#3071)"); let warns = lines.filter((l) => l.includes("NO known output ceiling")); - assert.equal(warns.length, 1, "warns once for the ceiling-less restore"); + assert.equal(warns.length, 1, "one warn per session+model (on the starve, not the silent learning)"); assert.match(warns[0] ?? "", /model=stepfun\/step-5-preview/, "names the model"); - assert.match(warns[0] ?? "", /restored 1 -> 583/, "states what was actually restored"); + assert.match(warns[0] ?? "", /raised 1 -> 8192/, "states what was actually raised"); assert.match(warns[0] ?? "", /providers\.\.models/, "points at the config escape hatch"); - // Same model again → deduped (no second warn). + // Same session + model again → deduped (no second warn). restoreOutputBudget({ max_tokens: 1, tools, model: A }, s, log); - assert.equal(lines.filter((l) => l.includes("NO known output ceiling")).length, 1, "deduped per model"); - // A different ceiling-less model warns once on its own. - restoreOutputBudget({ max_tokens: 1, tools, model: "other/model" }, metaSession("other"), log); + assert.equal(lines.filter((l) => l.includes("NO known output ceiling")).length, 1, "deduped per session+model"); + // A DIFFERENT session on the SAME ceiling-less model gets its OWN warn + // (#3071 path 2a: the old once-per-model dedup left the second session silent). + const sOther = metaSession("other"); + restoreOutputBudget({ max_tokens: 1, tools, model: A }, sOther, log); warns = lines.filter((l) => l.includes("NO known output ceiling")); - assert.equal(warns.length, 2, "per-model, not global"); - assert.match(warns[1] ?? "", /forwarded verbatim/, "born-starved (no water yet) says so"); - assert.equal((metaSession("other").metadata as Record).outputBudgetHighWater, undefined); + assert.equal(warns.length, 2, "per-session, not per-model (#3071)"); + assert.match(warns[1] ?? "", /\[other\]/, "names the affected session"); + assert.equal(sOther.metadata.outputBudgetHighWater, undefined, "the starved branch does not seed a high-water"); // With a usable ceiling → no warn at all (the floor note rides on the info line). _resetNoOutputCeilingWarningsForTest(); lines.length = 0; const s2 = metaSession("capped"); - restoreOutputBudget({ max_tokens: 583, model: "capped/model" }, s2, log); + restoreOutputBudget({ max_tokens: 583, model: "capped/model" }, s2, log, 32768); const capped = { max_tokens: 1, tools, model: "capped/model" } as { max_tokens: number }; restoreOutputBudget(capped, s2, log, 32768); assert.equal(capped.max_tokens, 32768, "ceiling lifts the death rattle"); @@ -195,19 +205,21 @@ test("restoreOutputBudget: warns once per model when no output ceiling backs the assert.ok(lines.some((l) => l.includes("high-water 583 below known output ceiling — floored (#1665/#1840)")), "floored info note present"); }); -test("restoreOutputBudget: tracks the no-ceiling degraded state for the user-visible surfaces (#2904)", () => { +test("restoreOutputBudget: tracks the no-ceiling degraded state for the user-visible surfaces (#2904/#3071)", () => { const tools = [{ name: "t" }]; - // Ceiling-less starved restore SEEDS the marker; healthy learning does not. + // A non-starved value is trusted verbatim and sets NO marker; a STARVE is + // what records the no-ceiling degraded state (floored to the absolute minimum). const s = metaSession("deg"); restoreOutputBudget({ max_tokens: 249, model: "prov/deepseek-flash" }, s, noopLog); - assert.equal(s.metadata.outputBudgetDegraded, undefined, "healthy-value learning (249>200) sets no marker"); + assert.equal(s.metadata.outputBudgetHighWater, 249, "non-starved learning tracked verbatim"); + assert.equal(s.metadata.outputBudgetDegraded, undefined, "healthy-value learning sets no marker"); const starved = { max_tokens: 16, tools, model: "prov/deepseek-flash" } as { max_tokens: number }; restoreOutputBudget(starved, s, noopLog); - assert.equal(starved.max_tokens, 249, "starved 16 restored to the 249 water mark (#2904 repro)"); + assert.equal(starved.max_tokens, OUTPUT_RESTORE_ABSOLUTE_FLOOR, "starved 16 restored to the absolute floor (#3071)"); const deg = s.metadata.outputBudgetDegraded as { model: string; value: number; ts: number } | undefined; assert.ok(deg !== undefined, "ceiling-less starved restore records the degraded state"); assert.equal(deg!.model, "prov/deepseek-flash"); - assert.equal(deg!.value, 249, "records what was actually forwarded (the restored target)"); + assert.equal(deg!.value, OUTPUT_RESTORE_ABSOLUTE_FLOOR, "records what was actually forwarded (the restored target)"); assert.equal(typeof deg!.ts, "number"); // A usable ceiling appearing CLEARS the marker (the pin is resolved). const capped = { max_tokens: 1, tools, model: "prov/deepseek-flash" } as { max_tokens: number }; @@ -225,10 +237,65 @@ test("restoreOutputBudget: tracks the no-ceiling degraded state for the user-vis restoreOutputBudget({ max_tokens: 10, tools, model: "prov/model-a" }, sB, noopLog); restoreOutputBudget({ max_tokens: 10, tools, model: "prov/model-b" }, sB, noopLog); assert.equal((sB.metadata.outputBudgetDegraded as { model?: string })?.model, "prov/model-b", "last starved model wins the single slot"); - // Born-starved (no high-water yet): value is the raw verbatim-forwarded budget. + // Born-starved (no high-water yet, no ceiling): floored to the absolute minimum — + // never recorded as a verbatim-forwarded rattle (#3071 path 2b). const sC = metaSession("bornDead"); restoreOutputBudget({ max_tokens: 16, tools, model: "prov/cold" }, sC, noopLog); - assert.equal((sC.metadata.outputBudgetDegraded as { value?: number })?.value, 16, "no high-water → records the verbatim-forwarded value"); + assert.equal((sC.metadata.outputBudgetDegraded as { value?: number })?.value, OUTPUT_RESTORE_ABSOLUTE_FLOOR, "no high-water + no ceiling → records the floored value"); +}); + +test("restoreOutputBudget: #3071 absolute floor stops the ceiling-less death spiral end-to-end", () => { + const tools = [{ name: "t" }]; + const s = metaSession("spiral"); + // The observed decay (v0.1.195, model ark-code-latest, no declared output + // ceiling): each NON-starved turn the client's raw-history estimate shrinks + // further. Those are trusted verbatim and drive the high-water down to a + // death rattle — exactly the poisoned state the old restore target pinned on. + for (const v of [41753, 3071, 2061, 252]) { + const body = { max_tokens: v, tools, model: "ark-code-latest" }; + restoreOutputBudget(body, s, noopLog); + assert.equal(body.max_tokens, v, `non-starved ${v} is trusted verbatim`); + } + assert.equal(s.metadata.outputBudgetHighWater, 252, "high-water decayed to the poison (last non-starved wins)"); + // Once fully starved (<=200), every restore must hit the absolute minimum — + // NOT the poisoned high-water of 252 that pinned the old behavior. + for (let i = 0; i < 8; i += 1) { + const body = { max_tokens: 1, tools, model: "ark-code-latest" }; + restoreOutputBudget(body, s, noopLog); + assert.equal(body.max_tokens, OUTPUT_RESTORE_ABSOLUTE_FLOOR, `starved turn ${i} floored to the absolute minimum, not 252`); + } +}); + +test("restoreOutputBudget: #3071 fork-inherited rattle is floored; a healthy value still wins", () => { + const tools = [{ name: "t" }]; + // Path 2b: a fresh fork child inherits the parent's starved budget with no + // high-water of its own and no known ceiling — the old code forwarded it + // verbatim (max_tokens=1 out the door). It is now always floored. + const child = metaSession("fork-child"); + const inherited = { max_tokens: 1, tools, model: "ark-code-latest" }; + restoreOutputBudget(inherited, child, noopLog); + assert.equal(inherited.max_tokens, OUTPUT_RESTORE_ABSOLUTE_FLOOR, "fork-inherited starved budget is floored, not forwarded verbatim (#3071 path 2b)"); + // The floor is a LOWER bound only — a genuinely-healthy value above it is not + // over-floored and sets no degraded marker. + const healthy = metaSession("healthy"); + const big = { max_tokens: 32768, model: "ark-code-latest" }; + restoreOutputBudget(big, healthy, noopLog); + assert.equal(big.max_tokens, 32768, "a value above the floor passes through untouched"); + assert.equal(healthy.metadata.outputBudgetDegraded, undefined, "a healthy value sets no degraded marker"); +}); + +test("restoreOutputBudget: #3071 leaves the ceiling-known behavior unchanged (regression guard)", () => { + const tools = [{ name: "t" }]; + // With a usable ceiling the client's small NON-starved value is trusted (not + // raised by the absolute minimum) — only the starved branch consults it. + const s = metaSession("ceiling-guard"); + const small = { max_tokens: 252, model: "capped/model" }; + restoreOutputBudget(small, s, noopLog, 131072); + assert.equal(small.max_tokens, 252, "ceiling-known: a small non-starved value is trusted, not floored (#3071 scope)"); + assert.equal(s.metadata.outputBudgetHighWater, 252, "high-water tracks the client's own value when a ceiling exists"); + const starved = { max_tokens: 1, tools, model: "capped/model" }; + restoreOutputBudget(starved, s, noopLog, 131072); + assert.equal(starved.max_tokens, 131072, "ceiling-known starve still restores to the ceiling"); }); test("resolveKnownOutputCeiling: runtime-info > launcher > declared > registry rank order (#1840)", () => { @@ -591,12 +658,14 @@ test("e2e: starved tool-carrying main request re-enters pipeline at restored bud const tools = [{ name: "compress", description: "compress", input_schema: { type: "object", properties: {} } }]; // Healthy main turn WITH tools: teaches the session its output budget. - const r1 = await fetch(url, { method: "POST", headers, body: JSON.stringify({ model: MODEL, max_tokens: 1024, stream: true, tools, messages: mainConversation(8) }) }); + // Above OUTPUT_RESTORE_ABSOLUTE_FLOOR so this exercises the high-water + // path, not the #3071 ceiling-less floor. + const r1 = await fetch(url, { method: "POST", headers, body: JSON.stringify({ model: MODEL, max_tokens: 16384, stream: true, tools, messages: mainConversation(8) }) }); assert.equal(r1.status, 200); await r1.text(); const s1 = getSession(SESSION); const requestsAfterMain1 = s1.stats.requests; - assert.equal(s1.metadata.outputBudgetHighWater, 1024, "high-water learned from the healthy main turn"); + assert.equal(s1.metadata.outputBudgetHighWater, 16384, "high-water learned from the healthy main turn"); // Death-spiral turn: the client shrank the budget to 16 tokens off its // raw-history estimate. The request still carries tools → it is a MAIN @@ -607,7 +676,7 @@ test("e2e: starved tool-carrying main request re-enters pipeline at restored bud await r2.text(); const s2 = getSession(SESSION); assert.ok(s2.stats.requests > requestsAfterMain1, "starved main turn must go through the pipeline, not side passthrough"); - assert.equal(rig.lastBody && rig.lastBody.max_tokens, 1024, "upstream received the restored budget (#546)"); + assert.equal(rig.lastBody && rig.lastBody.max_tokens, 16384, "upstream received the restored budget (#546)"); // A real side request (no tools, tiny budget) on the same session stays // a pure passthrough: budget NOT restored, kernel untouched. From b57b0d43552046e03247048891cab6b8d41ca7ca Mon Sep 17 00:00:00 2001 From: awork Date: Thu, 15 Oct 2026 10:22:41 +0800 Subject: [PATCH 2/3] fix: raise the ceiling-less starved output floor from 8K to 32K A ceiling-less model generally sustains >=32K of output; 8K is eaten by one modest thinking block before any visible text survives, so 8K left the #3071 blank-reply problem unfixed (owner decision 2026-10-15). Keep the #546 fixtures above the new floor so they exercise the high-water path, and pin that a known ceiling below the floor is honored, not over-floored. --- src/server/side-request.ts | 5 ++++- tests/side-request-isolation.test.ts | 18 ++++++++++++------ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/server/side-request.ts b/src/server/side-request.ts index f437bdacb1..da1aebcfad 100644 --- a/src/server/side-request.ts +++ b/src/server/side-request.ts @@ -304,11 +304,14 @@ export function writeOutputBudget(parsed: Record, field: Output // this minimum so a ceiling-less model can never truncate every reply below the // point where the model can even emit a compress call or short visible text. It is // a LOWER bound only — a healthy client value above it passes through untouched. +// Magnitude is 32K, not 8K (#3071 follow-up, owner decision 2026-10-15): a ceiling-less +// model generally sustains >=32K of output, while 8K is consumed by a single modest +// thinking block before any visible text survives, so 8K left the problem unfixed. // Flat (not window-relative): restoreOutputBudget runs BEFORE the window is resolved // at its call site, and both the downstream #453 clamp and the output-headroom // reservation (which self-caps at 25% of window) bound the result by real headroom // regardless of this floor. -export const OUTPUT_RESTORE_ABSOLUTE_FLOOR = 8192; +export const OUTPUT_RESTORE_ABSOLUTE_FLOOR = 32768; // #1840/#3071: one-shot "restore has no ceiling" warnings, keyed by SESSION+MODEL. // #1840 keyed them by MODEL alone, but the degradation is SESSION-scoped: the diff --git a/tests/side-request-isolation.test.ts b/tests/side-request-isolation.test.ts index 6dafbc6abd..2ff3ea4da6 100644 --- a/tests/side-request-isolation.test.ts +++ b/tests/side-request-isolation.test.ts @@ -115,10 +115,10 @@ test("restoreOutputBudget: high-water learning + starved-budget restore (#546)", assert.equal(s.metadata.outputBudgetHighWater, 32768, "a later healthy value wins as high-water"); // Field-name fidelity: restore writes the SAME field the client used. const s2 = metaSession("hw2"); - restoreOutputBudget({ max_output_tokens: 32689 }, s2, noopLog); + restoreOutputBudget({ max_output_tokens: 49152 }, s2, noopLog); const responsesStarved = { max_output_tokens: 1, tools: [{ type: "function" }] } as { max_output_tokens: number }; restoreOutputBudget(responsesStarved, s2, noopLog); - assert.equal(responsesStarved.max_output_tokens, 32689, "responses field restored on the same field"); + assert.equal(responsesStarved.max_output_tokens, 49152, "responses field restored on the same field"); }); test("restoreOutputBudget: configured-output-limit floor for poisoned or missing high-water (#1665)", () => { @@ -180,7 +180,7 @@ test("restoreOutputBudget: warns once per SESSION+model when no output ceiling b let warns = lines.filter((l) => l.includes("NO known output ceiling")); assert.equal(warns.length, 1, "one warn per session+model (on the starve, not the silent learning)"); assert.match(warns[0] ?? "", /model=stepfun\/step-5-preview/, "names the model"); - assert.match(warns[0] ?? "", /raised 1 -> 8192/, "states what was actually raised"); + assert.match(warns[0] ?? "", /raised 1 -> 32768/, "states what was actually raised"); assert.match(warns[0] ?? "", /providers\.\.models/, "points at the config escape hatch"); // Same session + model again → deduped (no second warn). restoreOutputBudget({ max_tokens: 1, tools, model: A }, s, log); @@ -296,6 +296,12 @@ test("restoreOutputBudget: #3071 leaves the ceiling-known behavior unchanged (re const starved = { max_tokens: 1, tools, model: "capped/model" }; restoreOutputBudget(starved, s, noopLog, 131072); assert.equal(starved.max_tokens, 131072, "ceiling-known starve still restores to the ceiling"); + // A known ceiling BELOW the absolute minimum must win un-raised — the floor + // bounds only a CEILING-LESS restore, never overrides a smaller declared cap. + const sCap = metaSession("small-cap"); + const cappedSmall = { max_tokens: 1, tools, model: "small/cap" }; + restoreOutputBudget(cappedSmall, sCap, noopLog, 4096); + assert.equal(cappedSmall.max_tokens, 4096, "known ceiling below the absolute floor is honored, not over-floored (#3071 scope)"); }); test("resolveKnownOutputCeiling: runtime-info > launcher > declared > registry rank order (#1840)", () => { @@ -662,12 +668,12 @@ test("e2e: starved tool-carrying main request re-enters pipeline at restored bud // Healthy main turn WITH tools: teaches the session its output budget. // Above OUTPUT_RESTORE_ABSOLUTE_FLOOR so this exercises the high-water // path, not the #3071 ceiling-less floor. - const r1 = await fetch(url, { method: "POST", headers, body: JSON.stringify({ model: MODEL, max_tokens: 16384, stream: true, tools, messages: mainConversation(8) }) }); + const r1 = await fetch(url, { method: "POST", headers, body: JSON.stringify({ model: MODEL, max_tokens: 49152, stream: true, tools, messages: mainConversation(8) }) }); assert.equal(r1.status, 200); await r1.text(); const s1 = getSession(SESSION); const requestsAfterMain1 = s1.stats.requests; - assert.equal(s1.metadata.outputBudgetHighWater, 16384, "high-water learned from the healthy main turn"); + assert.equal(s1.metadata.outputBudgetHighWater, 49152, "high-water learned from the healthy main turn"); // Death-spiral turn: the client shrank the budget to 16 tokens off its // raw-history estimate. The request still carries tools → it is a MAIN @@ -678,7 +684,7 @@ test("e2e: starved tool-carrying main request re-enters pipeline at restored bud await r2.text(); const s2 = getSession(SESSION); assert.ok(s2.stats.requests > requestsAfterMain1, "starved main turn must go through the pipeline, not side passthrough"); - assert.equal(rig.lastBody && rig.lastBody.max_tokens, 16384, "upstream received the restored budget (#546)"); + assert.equal(rig.lastBody && rig.lastBody.max_tokens, 49152, "upstream received the restored budget (#546)"); // A real side request (no tools, tiny budget) on the same session stays // a pure passthrough: budget NOT restored, kernel untouched. From 519bd0c2ac8ad3c28294275f4eb8b5652f12dad3 Mon Sep 17 00:00:00 2001 From: awork Date: Thu, 15 Oct 2026 10:22:41 +0800 Subject: [PATCH 3/3] docs(web): align the degraded-budget banner with the absolute-floor semantics The banner called the restored value 'from the client's own estimate', which no longer holds once the absolute floor applies: the value is the larger of the client estimate and the floor. zh/en wording fix only. --- src/web/i18n.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/web/i18n.ts b/src/web/i18n.ts index 8e0ced233e..f963e4e366 100644 --- a/src/web/i18n.ts +++ b/src/web/i18n.ts @@ -224,7 +224,7 @@ export const MESSAGES: Record> = { "det.conflicts_more": "更早的 {n} 条…", "det.conflicts_clear": "清除本会话记录", "det.conflicts_cleared": "已清除", - "det.obudget_degraded": "⚠️ 模型 {model} 无已知输出上限:输出预算被恢复到 {value} tokens(取自客户端自身估算,无权威上限兜底),较长的回复可能在输出上限处被截断。请在 bili 配置为该 provider/model 声明 output,或在宿主模型项显式设置 maxTokens (#2904)", + "det.obudget_degraded": "⚠️ 模型 {model} 无已知输出上限:输出预算被抬到 {value} tokens(客户端自身估算与绝对下限取大者,仍无权威上限兜底),较长的回复可能在输出上限处被截断。请在 bili 配置为该 provider/model 声明 output,或在宿主模型项显式设置 maxTokens (#2904/#3071)", "det.usage": "用量统计", "det.last_input": "最近单次输入", "det.upper_bound": "保守上界 {v}(字符数上界,只用于安全保护,不代表实际用量)", @@ -691,7 +691,7 @@ export const MESSAGES: Record> = { "det.conflicts_more": "{n} earlier event(s)…", "det.conflicts_clear": "Clear this session's records", "det.conflicts_cleared": "Cleared", - "det.obudget_degraded": "⚠️ Model {model} has NO known output ceiling: its output budget is restored to {value} tokens from the client's own estimate (no authoritative ceiling to back it), so longer replies may be cut off at the output limit. Declare an output limit for this provider/model in bili config, or set maxTokens on the model in the host profile (#2904)", + "det.obudget_degraded": "⚠️ Model {model} has NO known output ceiling: its output budget is raised to {value} tokens (the larger of the client's own estimate and the absolute minimum; no authoritative ceiling to back it), so longer replies may be cut off at the output limit. Declare an output limit for this provider/model in bili config, or set maxTokens on the model in the host profile (#2904/#3071)", "det.usage": "Usage", "det.last_input": "Last single-turn input", "det.upper_bound": "bound {v} (char-count upper bound — safety protection only, not actual usage)",