From 0d98e165ae988c6001b6d4de0a4c037f632be9f3 Mon Sep 17 00:00:00 2001 From: Joshwa Pohlmann Date: Wed, 16 Sep 2026 18:30:29 +0200 Subject: [PATCH 1/5] fix(preflight): fold deep enough for a payload far over the window MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A preflight folds one range per round and can spend a bounded number of summarization calls, so a payload that is well over the window and split into many ranges needs several rounds. Eight of each was not enough for the cases that matter: a live Gemini session of 1.39M tokens against a 1.05M window had every fold it attempted succeed and still came out ~8k short, then dropped the turn with "the compress budget was exhausted after 8 rounds" — the session was recoverable and the budget refused to recover it. Both budgets move to 16, which the new test pins: a fixture whose fold needs more rounds than the old budget allows is refused at 8 and forwarded at 16 (needs a fold deeper than one round, and stays bounded). The preflight is the last resort before an outright failure, so a bounded doubling of its work is cheaper than the failure it prevents. --- src/preflight.ts | 4 +- tests/preflight-multi-range.test.ts | 3 +- tests/preflight-round-budget.test.ts | 135 +++++++++++++++++++++++++++ 3 files changed, 139 insertions(+), 3 deletions(-) create mode 100644 tests/preflight-round-budget.test.ts diff --git a/src/preflight.ts b/src/preflight.ts index 8c516148c6..72730db823 100644 --- a/src/preflight.ts +++ b/src/preflight.ts @@ -25,14 +25,14 @@ import { lastCompressSuffix, type Session } from "./session.js"; // summarization calls sized to fit the smaller window, before the payload is // forwarded. -const MAX_PREFLIGHT_ROUNDS = 8; +const MAX_PREFLIGHT_ROUNDS = 16; const CHUNK_FRACTION = 0.6; const MIN_CHUNK_TOKENS = 2000; const MIN_SUMMARY_CHARS = 50; const MAX_SUMMARY_OUTPUT_TOKENS = 8192; // #574: bound on upstream summarization calls per invocation — the multi-range // walk can otherwise spend a call per viable range in a block-dense history. -const MAX_SUMMARY_CALLS_PER_PREFLIGHT = 8; +export const MAX_SUMMARY_CALLS_PER_PREFLIGHT = 16; export type PreflightProtocol = "anthropic" | "openai" | "responses"; diff --git a/tests/preflight-multi-range.test.ts b/tests/preflight-multi-range.test.ts index 5b27eded4b..1795862ad8 100644 --- a/tests/preflight-multi-range.test.ts +++ b/tests/preflight-multi-range.test.ts @@ -10,6 +10,7 @@ import { startServer, type ProxyOptions } from "../src/server.ts"; import { SessionStore, _setStoreForTest } from "../src/persist.ts"; import { _setForTest as setRegistryForTest } from "../src/registry.ts"; import { listSessions } from "../src/session.ts"; +import { MAX_SUMMARY_CALLS_PER_PREFLIGHT } from "../src/preflight.ts"; // #574/#569: preflight used to try exactly ONE range per round (the oldest) // and declare GLOBAL exhaustion when that single range yielded no applied @@ -200,7 +201,7 @@ test("#574 budget cap: many unusable ranges → exactly MAX_SUMMARY_CALLS_PER_PR assert.match(json.error?.message ?? "", /summarization budget/i, `the budget variant is reported (got: ${json.error?.message})`); const summaryCalls = calls.filter((c) => !c.stream); - assert.equal(summaryCalls.length, 8, `exactly MAX_SUMMARY_CALLS_PER_PREFLIGHT summary calls (got ${summaryCalls.length})`); + assert.equal(summaryCalls.length, MAX_SUMMARY_CALLS_PER_PREFLIGHT, `the call cap bounds the walk (got ${summaryCalls.length})`); assert.equal(calls.filter((c) => c.stream).length, 0, "the over-window payload was NOT forwarded"); const s = listSessions()[0]; diff --git a/tests/preflight-round-budget.test.ts b/tests/preflight-round-budget.test.ts new file mode 100644 index 0000000000..131ad7fda4 --- /dev/null +++ b/tests/preflight-round-budget.test.ts @@ -0,0 +1,135 @@ +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import test from "node:test"; + +process.env.NODE_ENV = "test"; + +import { defaultConfig } from "acp-kernel"; +import { startServer } from "../src/server.ts"; +import type { ProxyOptions } from "../src/config.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; + +// The preflight folds ONE range per round and caps a range at CHUNK_FRACTION of +// the window (src/preflight.ts), so the round budget is a hard limit +// on how far a payload can be brought down. A session that is well over the +// window and split into many small ranges (a long agent history is exactly +// that) needs a deep fold, and the per-invocation budget of 8 left a live +// 1.39M-token Gemini session ~8k tokens short of fitting: it reported "the +// compress budget was exhausted after 8 rounds" and dropped the turn even +// though every fold it attempted had succeeded. + +const WINDOW = 30_000; +const MESSAGES = 60; +const FILLER_REPEATS = 300; +const SUMMARY_TEXT = + "PREFLIGHT SUMMARY: this segment is a load-growth fixture whose every marker is derivable from its turn index, so the folded view keeps everything the work still needs and drops only repetition."; + +const CALLS: { summary: boolean; raw: string }[] = []; + +function chatSse(text: string): string { + const chunk = (delta: Record, finish: string | null): string => + `data: ${JSON.stringify({ id: "chatcmpl-1", object: "chat.completion.chunk", created: 1, model: "gpt-test", choices: [{ index: 0, delta, finish_reason: finish }] })}\n\n`; + return chunk({ role: "assistant" }, null) + chunk({ content: text }, null) + chunk({}, "stop") + "data: [DONE]\n\n"; +} + +function longMessages(): { role: string; content: string }[] { + const messages: { role: string; content: string }[] = []; + for (let i = 0; i < MESSAGES; i++) { + messages.push({ + role: i % 2 === 0 ? "user" : "assistant", + content: `turn ${i}: ` + `FILLER_${i}_payload_`.repeat(FILLER_REPEATS), + }); + } + return messages; +} + +function materialiseTokens(): number { + return Math.round(longMessages().reduce((n, m) => n + m.content.length, 0) / 4); +} + +function mockUpstream(): http.Server { + return http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + const raw = Buffer.concat(chunks).toString("utf8"); + const isSummary = /TASK: The conversation segment below/.test(raw); + CALLS.push({ summary: isSummary, raw }); + const text = isSummary ? SUMMARY_TEXT : "forwarded answer"; + // The preflight summary call is not streaming; answer it in the + // shape it asked for (the wire branch handles the Gemini case where + // the endpoint streams regardless — that is a different defect). + if (raw.includes('"stream":true')) { + res.writeHead(200, { "content-type": "text/event-stream" }); + res.end(chatSse(text)); + } else { + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ id: "chatcmpl-sum", object: "chat.completion", model: "gpt-test", choices: [{ index: 0, message: { role: "assistant", content: text }, finish_reason: "stop" }] })); + } + }); + }); +} + +function proxyOptions(upstreamPort: number): ProxyOptions { + return { + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: { [`http://127.0.0.1:${upstreamPort}`]: { models: { "gpt-test": { context: WINDOW } } } } as ProxyOptions["routes"], + modelContextLimit: WINDOW, + kernelConfig: defaultConfig(WINDOW, { + preserveRecentMessages: 2, + preserveRecentTokens: 2000, + compress: { minCompressRange: 1000, maxSummaryLength: 20000, minSummaryLength: 50 }, + }), + compress: { injectTool: true, injectNudge: true }, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + autoUpdate: false, + mitm: { enabled: false, domains: [] }, + } as ProxyOptions; +} + +test("e2e preflight: a payload that needs more folds than one round allows is still brought under the window", async () => { + CALLS.length = 0; + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const upstream = mockUpstream(); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamPort = (upstream.address() as { port: number }).port; + const proxy = await startServer(proxyOptions(upstreamPort)); + await once(proxy, "listening"); + const proxyPort = (proxy.address() as { port: number }).port; + + try { + const payloadTokens = materialiseTokens(); + assert.ok(payloadTokens > WINDOW, `fixture must start over the window (${payloadTokens} vs ${WINDOW})`); + + const resp = await fetch(`http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstreamPort}/chat/completions`, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": "budget-deep-fold" }, + body: JSON.stringify({ model: "gpt-test", stream: true, messages: longMessages() }), + }); + const body = await resp.text(); + const summaries = CALLS.filter((c) => c.summary); + const forwarded = CALLS.filter((c) => !c.summary); + + assert.equal(resp.status, 200, `an over-window turn must be folded and forwarded, not refused: HTTP ${resp.status} ${body.slice(0, 240)}`); + assert.ok(summaries.length > 8, `this payload needs a deeper fold than one round allows, got ${summaries.length} summary call(s)`); + assert.ok(summaries.length <= 24, `the fold must stay bounded, got ${summaries.length} summary call(s)`); + assert.ok(body.includes("forwarded answer"), "the model reply must reach the client"); + assert.ok(forwarded.some((f) => f.raw.includes(SUMMARY_TEXT.slice(0, 30))), "the folded summary must replace a raw range on the wire"); + const markers = forwarded.reduce((n, f) => n + (f.raw.match(/FILLER_\d+_payload_/g) ?? []).length, 0); + assert.ok(markers < MESSAGES * FILLER_REPEATS, `the fold must drop raw range text, still carried ${markers}`); + } finally { + proxy.close(); + await once(proxy, "close"); + upstream.close(); + await once(upstream, "close"); + } +}); From 3f87af765391bcc5f4bb53de2bf05922ccea10d5 Mon Sep 17 00:00:00 2001 From: Joshwa Pohlmann Date: Wed, 16 Sep 2026 18:57:17 +0200 Subject: [PATCH 2/5] docs: changelog entry for the preflight fold budget --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d02f52e71b..08e968b65c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ Versions follow the merge of a `*_release-v*` branch; CI publishes to npm on tag ### Fixes +- **Preflight fold budget raised 8 → 16: a payload far over the window can now be brought under it**: one range folds per round and both per-invocation budgets were 8, so the loop gave up before finishing even when every fold succeeded. A live Gemini session (1.39M-token payload against a 1,048,576 window) folded 8 ranges, saved ~329k tokens, landed ~8k short of the window, and then fail-fast 502'd with `the compress budget was exhausted after 8 rounds` — a message that also reads misleadingly, because the failure quotes the ORIGINAL context rather than the folded size. `MAX_PREFLIGHT_ROUNDS` and `MAX_SUMMARY_CALLS_PER_PREFLIGHT` are now 16 (`src/preflight.ts`, the latter exported so budget-cap tests derive from it). `tests/preflight-round-budget.test.ts` pins the behaviour on the openai wire: an ~83k-token fixture against a 30k window is refused at 8 rounds and forwarded at 16, using more than 8 summary calls; the #574 budget-cap case in `tests/preflight-multi-range.test.ts` no longer re-pins the literal 8. - **Degenerate-turn auto-retry reachable on OpenAI/Anthropic wires (#821, PR #822)**: the #732 invisible single-retry for a completed turn containing ONLY a thinking block (zero visible text, zero tool calls) was unreachable on the OpenAI/Anthropic wires: its gate required `!forwardedAny`, but on those wires reasoning deltas are forwarded verbatim to the client, so at `done` time both `forwardedAny` and `sawThinking` are true — mutually exclusive for exactly the shape this retry exists for. Only the Responses wire (round>1 framing suppression) could ever fire it. Wild-caught via WorkBuddy + deepseek-v4.1-flash through an OpenAI-chat gateway: two round-1 thinking-only turns (~6–8 min of thinking, zero visible output) logged the #673 `[degenerate-turn]` warning but passed through as empty turns, and the host aborted with an "empty stream" error. The loop now tracks model-VISIBLE output separately (`forwardedVisible`, set only on text forwards; reasoning/meta keep setting `forwardedAny`), and the retry gate uses it — a forwarded thinking prefix is invisible to host turn semantics and its chunks carry no `finish_reason` (adapters strip it), so appending the retry's content to the same stream is safe. Unchanged: one-shot bound, mandatory `sawThinking` (a genuinely empty no-reasoning turn still passes through untouched), and the #413 truncation retry's strict `!forwardedAny`. Regression tests pin the openai-wire round-1 shape (retry fires, nudge present, thinking prefix precedes retried content in the same stream) and the no-reasoning control (no retry). - **Cross-process startup-window race: concurrent launches no longer double-spawn proxies (#707)**: `ensureProxyRunning` only attached when it found a *healthy* instance at probe time — between the launcher's spawn and the child's first successful listen there was zero on-disk evidence that a bring-up was in flight, so two launches inside that window (~1s+) each spawned their own child: two live proxies over one sessions dir (double writers), last-writer-wins instance file, and the extra proxy living until ITS OWN spawner exited. The starter now claims `/proxy-starting` with O_EXCL before spawning (carrying `{token, pid, host, port, startedAt}`); a concurrent caller that sees an active marker (owner pid alive AND younger than SPAWN_WAIT_MS + 30s) waits for the starter's instance with the same probe as attach (pid alive + `/__bili/health` + instanceId match), bounded by SPAWN_WAIT_MS, and attaches on success; marker disappearance (starter gave up / crashed) breaks the wait early so the caller spawns immediately. The starter clears the marker on every terminal path via a token-checked clear; a hard crash leaves a stale marker that the dead-owner/TTL checks render inert. The O_EXCL claim itself is the cross-process arbiter: the pre-spawn read is only a fast path, so a loser of the claim (read→claim race, or a stale/garbage marker file left by a crash mid-write) re-checks and either waits for the live holder or removes-and-reclaims the slot — it never spawns blindly, and an unreadable marker file self-heals on the next launch instead of permanently degrading coordination. A final probe after the wait deadline catches a starter that succeeded in its last moments — its success path clears the marker, which otherwise looks identical to a failure bail. Incompatible-config fall-through is preserved (a waited-for instance that isn't compatible with our launch options still spawns, matching today's incompatible-healthy-instance behavior); explicit `bili start` bypasses the marker entirely (it's a user action, not auto-bootstrap). In-process dedup (`singleFlight`, #706) is untouched — this is the cross-process half. - **DeepSeek thinking mode 400 fixed: turns are atomic, reasoning-drop self-disables on strict-echo upstreams (#684)**: two paths could ship a rebuilt request whose assistant `tool_calls` turn lost its `reasoning_content` — (1) the proxy's recent-zone carve kept the last tool-call of a multi-call turn visible while its reasoning run folded into the block (kernel boundary logic only pulled the FIRST companion message after reasoning), and (2) #651's reasoning-drop stripped reasoning from a closed compress turn even on upstreams that document thinking-mode `reasoning_content` as mandatory round-trip. DeepSeek rejects both with 400 (`The \`reasoning_content\` in the thinking mode must be passed back to the API`) and the turn dies. Fixes: the kernel now computes atomic turn groups (reasoning run + its full assistant burst + every result paired by `toolCallId` — all fold together or none do; a range that would split a turn is withdrawn with a warning, or rejected with an explanatory error when nothing else remains), the forward boundary pull covers the whole burst, and the reasoning-drop never fires when the session is known strict-echo — statically for `deepseek` upstreams, or learned from the first 400 whose body mentions `reasoning_content` (persisted on the session, self-healing like #552). Three wire-level exit sentinels (`[reasoning-pair-violated]` on chat/completions and Responses, `[thinking-pair-violated]` on Anthropic) log loudly if any future path reintroduces a split turn. From ea17a12a9d2925f2ee594d416286fc0025282976 Mon Sep 17 00:00:00 2001 From: Joshwa Pohlmann Date: Wed, 16 Sep 2026 19:06:47 +0200 Subject: [PATCH 3/5] docs: cite the filed numbers in the preflight fold budget changelog entry (#868, PR #869) --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 08e968b65c..2bb4dd9c45 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ Versions follow the merge of a `*_release-v*` branch; CI publishes to npm on tag ### Fixes -- **Preflight fold budget raised 8 → 16: a payload far over the window can now be brought under it**: one range folds per round and both per-invocation budgets were 8, so the loop gave up before finishing even when every fold succeeded. A live Gemini session (1.39M-token payload against a 1,048,576 window) folded 8 ranges, saved ~329k tokens, landed ~8k short of the window, and then fail-fast 502'd with `the compress budget was exhausted after 8 rounds` — a message that also reads misleadingly, because the failure quotes the ORIGINAL context rather than the folded size. `MAX_PREFLIGHT_ROUNDS` and `MAX_SUMMARY_CALLS_PER_PREFLIGHT` are now 16 (`src/preflight.ts`, the latter exported so budget-cap tests derive from it). `tests/preflight-round-budget.test.ts` pins the behaviour on the openai wire: an ~83k-token fixture against a 30k window is refused at 8 rounds and forwarded at 16, using more than 8 summary calls; the #574 budget-cap case in `tests/preflight-multi-range.test.ts` no longer re-pins the literal 8. +- **Preflight fold budget raised 8 → 16: a payload far over the window can now be brought under it (#868, PR #869)**: one range folds per round and both per-invocation budgets were 8, so the loop gave up before finishing even when every fold succeeded. A live Gemini session (1.39M-token payload against a 1,048,576 window) folded 8 ranges, saved ~329k tokens, landed ~8k short of the window, and then fail-fast 502'd with `the compress budget was exhausted after 8 rounds` — a message that also reads misleadingly, because the failure quotes the ORIGINAL context rather than the folded size. `MAX_PREFLIGHT_ROUNDS` and `MAX_SUMMARY_CALLS_PER_PREFLIGHT` are now 16 (`src/preflight.ts`, the latter exported so budget-cap tests derive from it). `tests/preflight-round-budget.test.ts` pins the behaviour on the openai wire: an ~83k-token fixture against a 30k window is refused at 8 rounds and forwarded at 16, using more than 8 summary calls; the #574 budget-cap case in `tests/preflight-multi-range.test.ts` no longer re-pins the literal 8. - **Degenerate-turn auto-retry reachable on OpenAI/Anthropic wires (#821, PR #822)**: the #732 invisible single-retry for a completed turn containing ONLY a thinking block (zero visible text, zero tool calls) was unreachable on the OpenAI/Anthropic wires: its gate required `!forwardedAny`, but on those wires reasoning deltas are forwarded verbatim to the client, so at `done` time both `forwardedAny` and `sawThinking` are true — mutually exclusive for exactly the shape this retry exists for. Only the Responses wire (round>1 framing suppression) could ever fire it. Wild-caught via WorkBuddy + deepseek-v4.1-flash through an OpenAI-chat gateway: two round-1 thinking-only turns (~6–8 min of thinking, zero visible output) logged the #673 `[degenerate-turn]` warning but passed through as empty turns, and the host aborted with an "empty stream" error. The loop now tracks model-VISIBLE output separately (`forwardedVisible`, set only on text forwards; reasoning/meta keep setting `forwardedAny`), and the retry gate uses it — a forwarded thinking prefix is invisible to host turn semantics and its chunks carry no `finish_reason` (adapters strip it), so appending the retry's content to the same stream is safe. Unchanged: one-shot bound, mandatory `sawThinking` (a genuinely empty no-reasoning turn still passes through untouched), and the #413 truncation retry's strict `!forwardedAny`. Regression tests pin the openai-wire round-1 shape (retry fires, nudge present, thinking prefix precedes retried content in the same stream) and the no-reasoning control (no retry). - **Cross-process startup-window race: concurrent launches no longer double-spawn proxies (#707)**: `ensureProxyRunning` only attached when it found a *healthy* instance at probe time — between the launcher's spawn and the child's first successful listen there was zero on-disk evidence that a bring-up was in flight, so two launches inside that window (~1s+) each spawned their own child: two live proxies over one sessions dir (double writers), last-writer-wins instance file, and the extra proxy living until ITS OWN spawner exited. The starter now claims `/proxy-starting` with O_EXCL before spawning (carrying `{token, pid, host, port, startedAt}`); a concurrent caller that sees an active marker (owner pid alive AND younger than SPAWN_WAIT_MS + 30s) waits for the starter's instance with the same probe as attach (pid alive + `/__bili/health` + instanceId match), bounded by SPAWN_WAIT_MS, and attaches on success; marker disappearance (starter gave up / crashed) breaks the wait early so the caller spawns immediately. The starter clears the marker on every terminal path via a token-checked clear; a hard crash leaves a stale marker that the dead-owner/TTL checks render inert. The O_EXCL claim itself is the cross-process arbiter: the pre-spawn read is only a fast path, so a loser of the claim (read→claim race, or a stale/garbage marker file left by a crash mid-write) re-checks and either waits for the live holder or removes-and-reclaims the slot — it never spawns blindly, and an unreadable marker file self-heals on the next launch instead of permanently degrading coordination. A final probe after the wait deadline catches a starter that succeeded in its last moments — its success path clears the marker, which otherwise looks identical to a failure bail. Incompatible-config fall-through is preserved (a waited-for instance that isn't compatible with our launch options still spawns, matching today's incompatible-healthy-instance behavior); explicit `bili start` bypasses the marker entirely (it's a user action, not auto-bootstrap). In-process dedup (`singleFlight`, #706) is untouched — this is the cross-process half. - **DeepSeek thinking mode 400 fixed: turns are atomic, reasoning-drop self-disables on strict-echo upstreams (#684)**: two paths could ship a rebuilt request whose assistant `tool_calls` turn lost its `reasoning_content` — (1) the proxy's recent-zone carve kept the last tool-call of a multi-call turn visible while its reasoning run folded into the block (kernel boundary logic only pulled the FIRST companion message after reasoning), and (2) #651's reasoning-drop stripped reasoning from a closed compress turn even on upstreams that document thinking-mode `reasoning_content` as mandatory round-trip. DeepSeek rejects both with 400 (`The \`reasoning_content\` in the thinking mode must be passed back to the API`) and the turn dies. Fixes: the kernel now computes atomic turn groups (reasoning run + its full assistant burst + every result paired by `toolCallId` — all fold together or none do; a range that would split a turn is withdrawn with a warning, or rejected with an explanatory error when nothing else remains), the forward boundary pull covers the whole burst, and the reasoning-drop never fires when the session is known strict-echo — statically for `deepseek` upstreams, or learned from the first 400 whose body mentions `reasoning_content` (persisted on the session, self-healing like #552). Three wire-level exit sentinels (`[reasoning-pair-violated]` on chat/completions and Responses, `[thinking-pair-violated]` on Anthropic) log loudly if any future path reintroduces a split turn. From e05894ebeaa7460842ca7f749bc49d81c816ba2c Mon Sep 17 00:00:00 2001 From: ework-agent Date: Thu, 17 Sep 2026 08:17:15 +0800 Subject: [PATCH 4/5] fix(preflight): report the post-fold size and remaining ranges in the fail-fast 502 The exhaustion path quoted the original tokenCount captured before folding, so a 502 after 16 successful folds read as if nothing happened (the #868 incident: 1.39M -> 1.06M, still over the 1M window). The fail-fast now reports the post-fold size ("~N tokens (down from ~M before preflight)") and the compressible ranges still visible, via PreflightResult.rangesRemaining tracked per round from the kernel's viable ranges; original phrasing is kept verbatim when no fold ran. MAX_PREFLIGHT_ROUNDS is exported next to MAX_SUMMARY_CALLS_PER_PREFLIGHT so tests derive budgets from constants. New e2e case scales the round-budget fixture past the bound and pins the honest 502 (round-budget wording, post-fold size, remaining-range count, exactly one summary call per round, nothing forwarded). --- CHANGELOG.md | 2 +- src/preflight.ts | 13 +++++- src/server.ts | 14 +++++-- tests/preflight-multi-range.test.ts | 1 + tests/preflight-round-budget.test.ts | 59 ++++++++++++++++++++++++---- 5 files changed, 76 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2bb4dd9c45..0f00e941a0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ Versions follow the merge of a `*_release-v*` branch; CI publishes to npm on tag ### Fixes -- **Preflight fold budget raised 8 → 16: a payload far over the window can now be brought under it (#868, PR #869)**: one range folds per round and both per-invocation budgets were 8, so the loop gave up before finishing even when every fold succeeded. A live Gemini session (1.39M-token payload against a 1,048,576 window) folded 8 ranges, saved ~329k tokens, landed ~8k short of the window, and then fail-fast 502'd with `the compress budget was exhausted after 8 rounds` — a message that also reads misleadingly, because the failure quotes the ORIGINAL context rather than the folded size. `MAX_PREFLIGHT_ROUNDS` and `MAX_SUMMARY_CALLS_PER_PREFLIGHT` are now 16 (`src/preflight.ts`, the latter exported so budget-cap tests derive from it). `tests/preflight-round-budget.test.ts` pins the behaviour on the openai wire: an ~83k-token fixture against a 30k window is refused at 8 rounds and forwarded at 16, using more than 8 summary calls; the #574 budget-cap case in `tests/preflight-multi-range.test.ts` no longer re-pins the literal 8. +- **Preflight fold budget raised 8 → 16: a payload far over the window can now be brought under it (#868, PR #869)**: one range folds per round and both per-invocation budgets were 8, so the loop gave up before finishing even when every fold succeeded. A live Gemini session (1.39M-token payload against a 1,048,576 window) folded 8 ranges, saved ~329k tokens, landed ~8k short of the window, and then fail-fast 502'd with `the compress budget was exhausted after 8 rounds` — a message that also read misleadingly, because it quoted the ORIGINAL context rather than the folded size. `MAX_PREFLIGHT_ROUNDS` and `MAX_SUMMARY_CALLS_PER_PREFLIGHT` are now 16 (`src/preflight.ts`, both exported so budget tests derive from them); the fail-fast 502 now reports the post-fold size ("~N tokens (down from ~M before preflight)") and the remaining compressible-range count (`result.rangesRemaining`) instead of only the original token count. `tests/preflight-round-budget.test.ts` pins the behaviour on the openai wire: an ~83k-token fixture against a 30k window is refused at 8 rounds and forwarded at 16, using more than 8 summary calls; a second case scales the same history past the 16-round bound and pins the honest 502 (post-fold size, remaining-range count, exactly 16 summary calls); the #574 budget-cap case in `tests/preflight-multi-range.test.ts` no longer re-pins the literal 8. - **Degenerate-turn auto-retry reachable on OpenAI/Anthropic wires (#821, PR #822)**: the #732 invisible single-retry for a completed turn containing ONLY a thinking block (zero visible text, zero tool calls) was unreachable on the OpenAI/Anthropic wires: its gate required `!forwardedAny`, but on those wires reasoning deltas are forwarded verbatim to the client, so at `done` time both `forwardedAny` and `sawThinking` are true — mutually exclusive for exactly the shape this retry exists for. Only the Responses wire (round>1 framing suppression) could ever fire it. Wild-caught via WorkBuddy + deepseek-v4.1-flash through an OpenAI-chat gateway: two round-1 thinking-only turns (~6–8 min of thinking, zero visible output) logged the #673 `[degenerate-turn]` warning but passed through as empty turns, and the host aborted with an "empty stream" error. The loop now tracks model-VISIBLE output separately (`forwardedVisible`, set only on text forwards; reasoning/meta keep setting `forwardedAny`), and the retry gate uses it — a forwarded thinking prefix is invisible to host turn semantics and its chunks carry no `finish_reason` (adapters strip it), so appending the retry's content to the same stream is safe. Unchanged: one-shot bound, mandatory `sawThinking` (a genuinely empty no-reasoning turn still passes through untouched), and the #413 truncation retry's strict `!forwardedAny`. Regression tests pin the openai-wire round-1 shape (retry fires, nudge present, thinking prefix precedes retried content in the same stream) and the no-reasoning control (no retry). - **Cross-process startup-window race: concurrent launches no longer double-spawn proxies (#707)**: `ensureProxyRunning` only attached when it found a *healthy* instance at probe time — between the launcher's spawn and the child's first successful listen there was zero on-disk evidence that a bring-up was in flight, so two launches inside that window (~1s+) each spawned their own child: two live proxies over one sessions dir (double writers), last-writer-wins instance file, and the extra proxy living until ITS OWN spawner exited. The starter now claims `/proxy-starting` with O_EXCL before spawning (carrying `{token, pid, host, port, startedAt}`); a concurrent caller that sees an active marker (owner pid alive AND younger than SPAWN_WAIT_MS + 30s) waits for the starter's instance with the same probe as attach (pid alive + `/__bili/health` + instanceId match), bounded by SPAWN_WAIT_MS, and attaches on success; marker disappearance (starter gave up / crashed) breaks the wait early so the caller spawns immediately. The starter clears the marker on every terminal path via a token-checked clear; a hard crash leaves a stale marker that the dead-owner/TTL checks render inert. The O_EXCL claim itself is the cross-process arbiter: the pre-spawn read is only a fast path, so a loser of the claim (read→claim race, or a stale/garbage marker file left by a crash mid-write) re-checks and either waits for the live holder or removes-and-reclaims the slot — it never spawns blindly, and an unreadable marker file self-heals on the next launch instead of permanently degrading coordination. A final probe after the wait deadline catches a starter that succeeded in its last moments — its success path clears the marker, which otherwise looks identical to a failure bail. Incompatible-config fall-through is preserved (a waited-for instance that isn't compatible with our launch options still spawns, matching today's incompatible-healthy-instance behavior); explicit `bili start` bypasses the marker entirely (it's a user action, not auto-bootstrap). In-process dedup (`singleFlight`, #706) is untouched — this is the cross-process half. - **DeepSeek thinking mode 400 fixed: turns are atomic, reasoning-drop self-disables on strict-echo upstreams (#684)**: two paths could ship a rebuilt request whose assistant `tool_calls` turn lost its `reasoning_content` — (1) the proxy's recent-zone carve kept the last tool-call of a multi-call turn visible while its reasoning run folded into the block (kernel boundary logic only pulled the FIRST companion message after reasoning), and (2) #651's reasoning-drop stripped reasoning from a closed compress turn even on upstreams that document thinking-mode `reasoning_content` as mandatory round-trip. DeepSeek rejects both with 400 (`The \`reasoning_content\` in the thinking mode must be passed back to the API`) and the turn dies. Fixes: the kernel now computes atomic turn groups (reasoning run + its full assistant burst + every result paired by `toolCallId` — all fold together or none do; a range that would split a turn is withdrawn with a warning, or rejected with an explanatory error when nothing else remains), the forward boundary pull covers the whole burst, and the reasoning-drop never fires when the session is known strict-echo — statically for `deepseek` upstreams, or learned from the first 400 whose body mentions `reasoning_content` (persisted on the session, self-healing like #552). Three wire-level exit sentinels (`[reasoning-pair-violated]` on chat/completions and Responses, `[thinking-pair-violated]` on Anthropic) log loudly if any future path reintroduces a split turn. diff --git a/src/preflight.ts b/src/preflight.ts index 72730db823..1e68dcb998 100644 --- a/src/preflight.ts +++ b/src/preflight.ts @@ -25,7 +25,7 @@ import { lastCompressSuffix, type Session } from "./session.js"; // summarization calls sized to fit the smaller window, before the payload is // forwarded. -const MAX_PREFLIGHT_ROUNDS = 16; +export const MAX_PREFLIGHT_ROUNDS = 16; const CHUNK_FRACTION = 0.6; const MIN_CHUNK_TOKENS = 2000; const MIN_SUMMARY_CHARS = 50; @@ -92,6 +92,12 @@ export interface PreflightResult { * stale (e.g. a double-counted usage report, #300). The caller uses it * to decide whether forwarding as-is is actually safe. */ payloadEstimate: number; + /** Compressible ranges still visible in the kernel's final view after the + * walk stopped — how much foldable headroom a deeper budget would find + * (0 when nothing foldable remains). Surfaced in the fail-fast message + * so an operator can see why the payload is still over the window + * (#869 review). */ + rangesRemaining: number; /** Whether the final payload fits the window, judged with the same * measure the loop used: the optimistic token estimate for * measured-baseline sessions (#300 — a stale HIGH baseline must not @@ -669,7 +675,7 @@ function noEmergencyTruncate(config: Config): Config { export async function preflightCompress(deps: PreflightDeps, messages: CoreMessage[]): Promise { const limit = deps.config.modelContextLimit; let target = Math.min(limit, deps.compressionTarget ?? limit); - const result: PreflightResult = { compressedRanges: 0, savedTokens: 0, payloadEstimate: estimateCoreMessages(messages) + (deps.imageFloor ?? 0) + (deps.wireOverhead ?? 0), fitsWindow: true }; + const result: PreflightResult = { compressedRanges: 0, savedTokens: 0, payloadEstimate: estimateCoreMessages(messages) + (deps.imageFloor ?? 0) + (deps.wireOverhead ?? 0), rangesRemaining: 0, fitsWindow: true }; if (limit <= 0) return result; const budget = Math.max(MIN_CHUNK_TOKENS, Math.floor(limit * CHUNK_FRACTION)); // applyCompression rejects ranges below config.compress.minCompressRange @@ -705,6 +711,7 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa let summaryCalls = 0; let budgetHit = false; let rangesTried = 0; + let rangesRemaining = 0; for (let round = 0; round < MAX_PREFLIGHT_ROUNDS; round++) { if (deps.signal?.aborted) { failure = ABORTED_FAILURE; @@ -742,6 +749,7 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa if (startTokens < 0) startTokens = currentTokens; if (currentTokens < target) break; const ranges = viableRanges(turn.nudge?.compressibleRanges ?? []); + rangesRemaining = ranges.length; if (ranges.length === 0) { // #330: nothing foldable outside the soft-protected recent zone. // Relax the soft zone (oldest-first within it) and retry — the hard @@ -904,6 +912,7 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa } } if (result.compressedRanges > 0) deps.session.stats.lastInputTokens = currentTokens; + result.rangesRemaining = rangesRemaining; result.savedTokens = Math.max(0, startTokens - currentTokens); if (currentTokens >= limit) result.failure = failure; result.fitsWindow = baselineKnown ? result.payloadEstimate < limit : finalUpper < limit; diff --git a/src/server.ts b/src/server.ts index 32d674e1a7..f524cab674 100644 --- a/src/server.ts +++ b/src/server.ts @@ -2854,12 +2854,20 @@ async function preflightCompressIfNeeded( // session.stats.lastInputTokens, which can be stale — e.g. a // double-counted usage report (#300) — and must not turn a fitting // payload into a fail-fast false positive. - const failFast = (status: number, detail: string, retryable: boolean): PreflightFailFast => { + // #869 review: quote the POST-FOLD size once folding happened — reporting + // only the original tokenCount reads as "nothing happened" even when 16 + // folds removed hundreds of thousands of tokens. foldedTokens/rangesLeft + // stay undefined when no fold ran, so the original phrasing holds there. + const failFast = (status: number, detail: string, retryable: boolean, foldedTokens?: number, rangesLeft?: number): PreflightFailFast => { const imageNote = imageTokens >= limit ? ` Images alone account for ~${imageTokens} tokens (≥ window ${limit}); compression cannot remove them — shrink or remove the images, or raise the window.` : ""; + 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}`; + const rangesClause = rangesLeft !== undefined ? `, with ${rangesLeft} compressible range(s) still visible` : ""; const message = - `context ~${tokenCount} tokens exceeds the model window ${limit} (model=${model}) ` + + `${sizeClause} (model=${model})${rangesClause} ` + `and preflight compression could not bring it under: ${detail}.` + imageNote + ` The over-window payload was NOT forwarded.`; @@ -2985,7 +2993,7 @@ async function preflightCompressIfNeeded( } const status = f?.kind === "upstream" && f.status === 429 ? 503 : 502; const retryable = f?.retryable === true || (f?.kind === "upstream" && f.status !== undefined && (f.status === 429 || f.status >= 500)); - const ff = failFast(status, f?.detail ?? "the payload still exceeds the window after preflight compression", retryable); + const ff = failFast(status, f?.detail ?? "the payload still exceeds the window after preflight compression", retryable, result.compressedRanges > 0 ? session.stats.lastInputTokens : undefined, result.rangesRemaining); const contentDeadEnd = f?.kind === "exhausted" || (f?.kind === "upstream" && f.status !== undefined && f.status >= 400 && f.status < 500 && !retryable); if (contentDeadEnd && result.compressedRanges === 0) { const cooldownMs = preflightDeadEndCooldownMs(); diff --git a/tests/preflight-multi-range.test.ts b/tests/preflight-multi-range.test.ts index 1795862ad8..d7410cb42d 100644 --- a/tests/preflight-multi-range.test.ts +++ b/tests/preflight-multi-range.test.ts @@ -199,6 +199,7 @@ test("#574 budget cap: many unusable ranges → exactly MAX_SUMMARY_CALLS_PER_PR assert.equal(json.error?.code, "preflight_compress_failed"); assert.equal(json.error?.retryable, false); assert.match(json.error?.message ?? "", /summarization budget/i, `the budget variant is reported (got: ${json.error?.message})`); + assert.match(json.error?.message ?? "", /compressible range\(s\) still visible/, `reports how many compressible ranges remain (got: ${json.error?.message})`); const summaryCalls = calls.filter((c) => !c.stream); assert.equal(summaryCalls.length, MAX_SUMMARY_CALLS_PER_PREFLIGHT, `the call cap bounds the walk (got ${summaryCalls.length})`); diff --git a/tests/preflight-round-budget.test.ts b/tests/preflight-round-budget.test.ts index 131ad7fda4..9aceb4387b 100644 --- a/tests/preflight-round-budget.test.ts +++ b/tests/preflight-round-budget.test.ts @@ -10,6 +10,7 @@ import { startServer } from "../src/server.ts"; import type { ProxyOptions } from "../src/config.ts"; import { SessionStore, _setStoreForTest } from "../src/persist.ts"; import { _setForTest as setRegistryForTest } from "../src/registry.ts"; +import { MAX_PREFLIGHT_ROUNDS, MAX_SUMMARY_CALLS_PER_PREFLIGHT } from "../src/preflight.ts"; // The preflight folds ONE range per round and caps a range at CHUNK_FRACTION of // the window (src/preflight.ts), so the round budget is a hard limit @@ -34,19 +35,19 @@ function chatSse(text: string): string { return chunk({ role: "assistant" }, null) + chunk({ content: text }, null) + chunk({}, "stop") + "data: [DONE]\n\n"; } -function longMessages(): { role: string; content: string }[] { +function longMessages(count: number, repeats: number): { role: string; content: string }[] { const messages: { role: string; content: string }[] = []; - for (let i = 0; i < MESSAGES; i++) { + for (let i = 0; i < count; i++) { messages.push({ role: i % 2 === 0 ? "user" : "assistant", - content: `turn ${i}: ` + `FILLER_${i}_payload_`.repeat(FILLER_REPEATS), + content: `turn ${i}: ` + `FILLER_${i}_payload_`.repeat(repeats), }); } return messages; } -function materialiseTokens(): number { - return Math.round(longMessages().reduce((n, m) => n + m.content.length, 0) / 4); +function materialiseTokens(count: number, repeats: number): number { + return Math.round(longMessages(count, repeats).reduce((n, m) => n + m.content.length, 0) / 4); } function mockUpstream(): http.Server { @@ -107,13 +108,13 @@ test("e2e preflight: a payload that needs more folds than one round allows is st const proxyPort = (proxy.address() as { port: number }).port; try { - const payloadTokens = materialiseTokens(); + const payloadTokens = materialiseTokens(MESSAGES, FILLER_REPEATS); assert.ok(payloadTokens > WINDOW, `fixture must start over the window (${payloadTokens} vs ${WINDOW})`); const resp = await fetch(`http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstreamPort}/chat/completions`, { method: "POST", headers: { "content-type": "application/json", "x-acp-session": "budget-deep-fold" }, - body: JSON.stringify({ model: "gpt-test", stream: true, messages: longMessages() }), + body: JSON.stringify({ model: "gpt-test", stream: true, messages: longMessages(MESSAGES, FILLER_REPEATS) }), }); const body = await resp.text(); const summaries = CALLS.filter((c) => c.summary); @@ -133,3 +134,47 @@ test("e2e preflight: a payload that needs more folds than one round allows is st await once(upstream, "close"); } }); + +test("e2e preflight: beyond the round budget the fail-fast reports the post-fold state", async () => { + CALLS.length = 0; + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const upstream = mockUpstream(); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamPort = (upstream.address() as { port: number }).port; + const proxy = await startServer(proxyOptions(upstreamPort)); + await once(proxy, "listening"); + const proxyPort = (proxy.address() as { port: number }).port; + + try { + // 400 small turns (~340k tokens vs a 30k window): each fold removes + // far less than the chunk budget, so even 16 successful rounds leave + // the payload well over the window — the walk stops at the round cap. + const COUNT = 400; + const REPEATS = 200; + assert.ok(materialiseTokens(COUNT, REPEATS) > WINDOW * 3, `fixture must start well over the window (${materialiseTokens(COUNT, REPEATS)} vs ${WINDOW})`); + + const resp = await fetch(`http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstreamPort}/chat/completions`, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": "budget-exhaust-state" }, + body: JSON.stringify({ model: "gpt-test", stream: true, messages: longMessages(COUNT, REPEATS) }), + }); + const body = await resp.text(); + assert.equal(resp.status, 502, `beyond the round budget the turn must be refused: HTTP ${resp.status} ${body.slice(0, 240)}`); + const json = JSON.parse(body) as { error?: { code?: string; message?: string } }; + assert.equal(json.error?.code, "preflight_compress_failed"); + assert.match(json.error?.message ?? "", new RegExp(`exhausted after ${MAX_PREFLIGHT_ROUNDS} rounds`, "i"), `names the round budget (got: ${json.error?.message})`); + assert.match(json.error?.message ?? "", /down from ~\d+ before preflight/, `quotes the post-fold size, not only the original (got: ${json.error?.message})`); + assert.match(json.error?.message ?? "", /compressible range\(s\) still visible/, `reports how many compressible ranges remain (got: ${json.error?.message})`); + + const summaries = CALLS.filter((c) => c.summary); + assert.equal(summaries.length, MAX_SUMMARY_CALLS_PER_PREFLIGHT, `one fold per round, one call each (got ${summaries.length})`); + assert.equal(CALLS.filter((c) => c.raw.includes('"stream":true')).length, 0, "the over-window payload was NOT forwarded"); + } finally { + proxy.close(); + await once(proxy, "close"); + upstream.close(); + await once(upstream, "close"); + } +}); From 0a16f29ed0ebaa9792f2e927295212a4f9948a5b Mon Sep 17 00:00:00 2001 From: ework-agent Date: Thu, 17 Sep 2026 08:17:39 +0800 Subject: [PATCH 5/5] docs(preflight): state the fold-depth coverage bound beside the budgets MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Document next to MAX_PREFLIGHT_ROUNDS / MAX_SUMMARY_CALLS_PER_PREFLIGHT how far over the window 16 folds can reach (one range per round x at most CHUNK_FRACTION of the window per fold ~= 9.6x-window overshoot, i.e. a payload up to ~10.6x the window, best case; lower in practice) and that beyond the bound the loop still exits cleanly with the post-fold size and remaining compressible-range count reported. Note that 16 is tuned to the #868 incident class — an overshoot-scaled budget is deliberately left as a separate design question. --- CHANGELOG.md | 2 +- src/preflight.ts | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f00e941a0..a2740623d1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ Versions follow the merge of a `*_release-v*` branch; CI publishes to npm on tag ### Fixes -- **Preflight fold budget raised 8 → 16: a payload far over the window can now be brought under it (#868, PR #869)**: one range folds per round and both per-invocation budgets were 8, so the loop gave up before finishing even when every fold succeeded. A live Gemini session (1.39M-token payload against a 1,048,576 window) folded 8 ranges, saved ~329k tokens, landed ~8k short of the window, and then fail-fast 502'd with `the compress budget was exhausted after 8 rounds` — a message that also read misleadingly, because it quoted the ORIGINAL context rather than the folded size. `MAX_PREFLIGHT_ROUNDS` and `MAX_SUMMARY_CALLS_PER_PREFLIGHT` are now 16 (`src/preflight.ts`, both exported so budget tests derive from them); the fail-fast 502 now reports the post-fold size ("~N tokens (down from ~M before preflight)") and the remaining compressible-range count (`result.rangesRemaining`) instead of only the original token count. `tests/preflight-round-budget.test.ts` pins the behaviour on the openai wire: an ~83k-token fixture against a 30k window is refused at 8 rounds and forwarded at 16, using more than 8 summary calls; a second case scales the same history past the 16-round bound and pins the honest 502 (post-fold size, remaining-range count, exactly 16 summary calls); the #574 budget-cap case in `tests/preflight-multi-range.test.ts` no longer re-pins the literal 8. +- **Preflight fold budget raised 8 → 16: a payload far over the window can now be brought under it (#868, PR #869)**: one range folds per round and both per-invocation budgets were 8, so the loop gave up before finishing even when every fold succeeded. A live Gemini session (1.39M-token payload against a 1,048,576 window) folded 8 ranges, saved ~329k tokens, landed ~8k short of the window, and then fail-fast 502'd with `the compress budget was exhausted after 8 rounds` — a message that also read misleadingly, because it quoted the ORIGINAL context rather than the folded size. `MAX_PREFLIGHT_ROUNDS` and `MAX_SUMMARY_CALLS_PER_PREFLIGHT` are now 16 (`src/preflight.ts`, both exported so budget tests derive from them); the fail-fast 502 now reports the post-fold size ("~N tokens (down from ~M before preflight)") and the remaining compressible-range count (`result.rangesRemaining`) instead of only the original token count; the fold-depth coverage bound (best case ~9.6x-window overshoot, lower in practice) is documented beside the constants. `tests/preflight-round-budget.test.ts` pins the behaviour on the openai wire: an ~83k-token fixture against a 30k window is refused at 8 rounds and forwarded at 16, using more than 8 summary calls; a second case scales the same history past the 16-round bound and pins the honest 502 (post-fold size, remaining-range count, exactly 16 summary calls); the #574 budget-cap case in `tests/preflight-multi-range.test.ts` no longer re-pins the literal 8. - **Degenerate-turn auto-retry reachable on OpenAI/Anthropic wires (#821, PR #822)**: the #732 invisible single-retry for a completed turn containing ONLY a thinking block (zero visible text, zero tool calls) was unreachable on the OpenAI/Anthropic wires: its gate required `!forwardedAny`, but on those wires reasoning deltas are forwarded verbatim to the client, so at `done` time both `forwardedAny` and `sawThinking` are true — mutually exclusive for exactly the shape this retry exists for. Only the Responses wire (round>1 framing suppression) could ever fire it. Wild-caught via WorkBuddy + deepseek-v4.1-flash through an OpenAI-chat gateway: two round-1 thinking-only turns (~6–8 min of thinking, zero visible output) logged the #673 `[degenerate-turn]` warning but passed through as empty turns, and the host aborted with an "empty stream" error. The loop now tracks model-VISIBLE output separately (`forwardedVisible`, set only on text forwards; reasoning/meta keep setting `forwardedAny`), and the retry gate uses it — a forwarded thinking prefix is invisible to host turn semantics and its chunks carry no `finish_reason` (adapters strip it), so appending the retry's content to the same stream is safe. Unchanged: one-shot bound, mandatory `sawThinking` (a genuinely empty no-reasoning turn still passes through untouched), and the #413 truncation retry's strict `!forwardedAny`. Regression tests pin the openai-wire round-1 shape (retry fires, nudge present, thinking prefix precedes retried content in the same stream) and the no-reasoning control (no retry). - **Cross-process startup-window race: concurrent launches no longer double-spawn proxies (#707)**: `ensureProxyRunning` only attached when it found a *healthy* instance at probe time — between the launcher's spawn and the child's first successful listen there was zero on-disk evidence that a bring-up was in flight, so two launches inside that window (~1s+) each spawned their own child: two live proxies over one sessions dir (double writers), last-writer-wins instance file, and the extra proxy living until ITS OWN spawner exited. The starter now claims `/proxy-starting` with O_EXCL before spawning (carrying `{token, pid, host, port, startedAt}`); a concurrent caller that sees an active marker (owner pid alive AND younger than SPAWN_WAIT_MS + 30s) waits for the starter's instance with the same probe as attach (pid alive + `/__bili/health` + instanceId match), bounded by SPAWN_WAIT_MS, and attaches on success; marker disappearance (starter gave up / crashed) breaks the wait early so the caller spawns immediately. The starter clears the marker on every terminal path via a token-checked clear; a hard crash leaves a stale marker that the dead-owner/TTL checks render inert. The O_EXCL claim itself is the cross-process arbiter: the pre-spawn read is only a fast path, so a loser of the claim (read→claim race, or a stale/garbage marker file left by a crash mid-write) re-checks and either waits for the live holder or removes-and-reclaims the slot — it never spawns blindly, and an unreadable marker file self-heals on the next launch instead of permanently degrading coordination. A final probe after the wait deadline catches a starter that succeeded in its last moments — its success path clears the marker, which otherwise looks identical to a failure bail. Incompatible-config fall-through is preserved (a waited-for instance that isn't compatible with our launch options still spawns, matching today's incompatible-healthy-instance behavior); explicit `bili start` bypasses the marker entirely (it's a user action, not auto-bootstrap). In-process dedup (`singleFlight`, #706) is untouched — this is the cross-process half. - **DeepSeek thinking mode 400 fixed: turns are atomic, reasoning-drop self-disables on strict-echo upstreams (#684)**: two paths could ship a rebuilt request whose assistant `tool_calls` turn lost its `reasoning_content` — (1) the proxy's recent-zone carve kept the last tool-call of a multi-call turn visible while its reasoning run folded into the block (kernel boundary logic only pulled the FIRST companion message after reasoning), and (2) #651's reasoning-drop stripped reasoning from a closed compress turn even on upstreams that document thinking-mode `reasoning_content` as mandatory round-trip. DeepSeek rejects both with 400 (`The \`reasoning_content\` in the thinking mode must be passed back to the API`) and the turn dies. Fixes: the kernel now computes atomic turn groups (reasoning run + its full assistant burst + every result paired by `toolCallId` — all fold together or none do; a range that would split a turn is withdrawn with a warning, or rejected with an explanatory error when nothing else remains), the forward boundary pull covers the whole burst, and the reasoning-drop never fires when the session is known strict-echo — statically for `deepseek` upstreams, or learned from the first 400 whose body mentions `reasoning_content` (persisted on the session, self-healing like #552). Three wire-level exit sentinels (`[reasoning-pair-violated]` on chat/completions and Responses, `[thinking-pair-violated]` on Anthropic) log loudly if any future path reintroduces a split turn. diff --git a/src/preflight.ts b/src/preflight.ts index 1e68dcb998..b3f5cb2e87 100644 --- a/src/preflight.ts +++ b/src/preflight.ts @@ -34,6 +34,19 @@ const MAX_SUMMARY_OUTPUT_TOKENS = 8192; // walk can otherwise spend a call per viable range in a block-dense history. export const MAX_SUMMARY_CALLS_PER_PREFLIGHT = 16; +// #869 review: coverage bound of the two depth budgets above. One round folds +// ONE range and each fold removes at most CHUNK_FRACTION x window tokens (the +// per-call chunk budget), so MAX_PREFLIGHT_ROUNDS rounds cover an overshoot of +// at most MAX_PREFLIGHT_ROUNDS x CHUNK_FRACTION x window ~= 9.6x the window — +// a payload of up to ~10.6x the window in the best case. Real coverage is +// lower: a range smaller than the chunk budget saves less, and the #726 +// halving worklist can spend several calls on one range without completing a +// fold. Beyond the bound the loop still exits cleanly — the fail-fast reports +// the post-fold size and the remaining compressible-range count, so an +// operator sees exactly how far the budget ran out. 16 is tuned to the +// incident class behind #868 (a 1.39x-window payload); it is a fixed depth, +// not scaled to the overshoot — scaling it is a separate design question. + export type PreflightProtocol = "anthropic" | "openai" | "responses"; export interface PreflightDeps {