diff --git a/src/preflight.ts b/src/preflight.ts index 6c28700e7d..d546a98f38 100644 --- a/src/preflight.ts +++ b/src/preflight.ts @@ -254,8 +254,8 @@ export interface PreflightResult { * unknown-baseline ones (#553 — the optimistic figure can undershoot by * up to ~4x on dense replays, so only the upper bound proves a fit). */ fitsWindow: boolean; - /** Why the loop stopped while the payload still overflows the window. - * Undefined when the payload fits. */ + /** 循环因窗口仍溢出或客户端取消而停止的原因。 + * 即使压缩后的内容已能放入窗口,也必须保留取消状态。 */ failure?: PreflightFailure; } @@ -1914,7 +1914,8 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa } result.rangesRemaining = rangesRemaining; result.savedTokens = Math.max(0, startTokens - currentTokens); - if (currentTokens >= limit) result.failure = failure; + if (deps.signal?.aborted) failure = ABORTED_FAILURE; + if (currentTokens >= limit || failure?.kind === "aborted") result.failure = failure; result.fitsWindow = baselineKnown ? result.payloadEstimate < limit : finalUpper < limit; return result; } diff --git a/src/server.ts b/src/server.ts index fa0bfbba61..9938ad5f0e 100644 --- a/src/server.ts +++ b/src/server.ts @@ -308,6 +308,8 @@ export function resolveUpstream(_opts: ProxyOptions, reqUrl: string, req?: http. const requestAborts = new WeakMap(); export function registerRequestAbort(res: http.ServerResponse, ac: AbortController): void { + // 取消可能已在 preflight 与 relay 交接前发生,close 事件不会重放。 + if (requestAborts.get(res)?.signal.aborted || res.destroyed || res.writableEnded || res.socket?.destroyed) ac.abort(); requestAborts.set(res, ac); } @@ -2736,6 +2738,7 @@ export async function preflightCompressIfNeeded( // sessions keep the loop's own upper-bound judgment (result.fitsWindow, // #553) — the optimistic re-estimate is exactly what that regime distrusts. let outbound: Prepared = prepared; + let fits: boolean; if (result.compressedRanges > 0) { const rebuilt = await runPrepare(); // runPrepare re-incremented stats.requests; the rebuild is internal @@ -2766,7 +2769,7 @@ export async function preflightCompressIfNeeded( // The baseline anchor above stays raw deliberately: it is a floor for // future meters, and a deflated (k̂ < 1) value would only delay the // next trigger, never advance it. - const fits = unknownBaseline + fits = unknownBaseline ? result.fitsWindow : applyEstimateCalibration(rebuiltTextSize, kFactor, kOrigin, currentOrigin) + imageTokens < limit; // #1820: anchor the meter to the rebuilt payload's measured size — the @@ -2777,46 +2780,53 @@ export async function preflightCompressIfNeeded( // deflation) mirrors the fit-gate quantity; lifetime is bounded (see // session.ts) so never-reporting upstreams fall back to legacy sizing. setPostRebuildAnchor(session, rebuiltTextSize + imageTokens); - if (fits) return rebuilt; - // #1839: the two measurements disagree — preflight's own final view - // (post-fold content + images + wire overhead) fits, but the fresh - // normal-config rebuild measures over. That divergence produced the - // self-contradictory fail-fast ("context ~44231 … exceeds window - // 253725"). Forward once and let the upstream arbitrate (the #496 - // house pattern): a genuinely over-window payload is rejected once - // and armOverflowShrink recovers with real evidence; a fitting - // payload is no longer refused on a stale measurement. One forward - // only — the #330 relaxed-zone caveat still bounds the risk. - if (!unknownBaseline && result.failure?.kind !== "aborted" && result.payloadEstimate < limit) { - log("warn", `[${session.id}] preflight view fits (~${result.payloadEstimate}/${limit}) but the rebuilt payload measures over — forwarding once for upstream arbitration (#1839)`); - return rebuilt; - } - } else if (unknownBaseline - ? result.fitsWindow - : estimateCoreMessages(prepared.processedMessages) + overheadEstimate + imageTokens < limit) { - // [#autoFold] A growth-armed payload fits the real window by definition, - // so a zero-progress fold is not an error — but it does mean the chain - // could not deliver at all. Arm the cooldown right here (the arm branch - // below is unreachable from this path — it sits after this return) so - // classic nudges resume instead of re-attempting the dead chain every - // turn. The arm is deliberately NOT gated on the calibrated payload: - // a calibration straddle (estimate < limit <= calibrated) used to skip - // it, re-firing the trigger every turn with a guaranteed-400 forward - // and no recovery (PR #2581 review) — now the straddle arms too, and - // the forwarded request's 400 arms overflow-shrink with real evidence. - if (growthArmed) { - log("warn", `[${session.id}] auto-fold made no progress (0 range(s) folded) — forwarding as-is (estimate fits the model window ${limit}); backing off auto-fold for ${Math.round(AUTO_FOLD_BACKOFF_MS / 60_000)}m so classic nudges resume`); - armAutoFoldBackoff(session); - markDirty(session); - } else { - log("warn", `[${session.id}] preflight made no progress but the payload fits; forwarding as-is`); - } - return prepared; + } else { + fits = unknownBaseline + ? result.fitsWindow + : estimateCoreMessages(prepared.processedMessages) + overheadEstimate + imageTokens < limit; } const f = result.failure; - if (f?.kind === "aborted") { - log("warn", `[${session.id}] preflight aborted (${f.detail}); not forwarding`); - return { failFast: true, status: 0, message: f.detail, retryable: false, respond: false }; + // 重建过程也会让出执行权;所有转发许可都必须在此重新检查取消。 + if (clientAbort.signal.aborted || f?.kind === "aborted") { + const detail = f?.kind === "aborted" ? f.detail : "the client disconnected during preflight compression"; + log("warn", `[${session.id}] preflight aborted (${detail}); not forwarding`); + return { failFast: true, status: 0, message: detail, retryable: false, respond: false }; + } + if (fits) { + if (result.compressedRanges === 0) { + // [#autoFold] A growth-armed payload fits the real window by definition, + // so a zero-progress fold is not an error — but it does mean the chain + // could not deliver at all. Arm the cooldown right here (the fail-open + // arm branch below is unreachable from this path — it sits after this + // return) so classic nudges resume instead of re-attempting the dead + // chain every turn. The arm is deliberately NOT gated on the calibrated + // payload: a calibration straddle (estimate < limit <= calibrated) used + // to skip it, re-firing the trigger every turn with a guaranteed-400 + // forward and no recovery (PR #2581 review) — now the straddle arms + // too, and the forwarded request's 400 arms overflow-shrink with real + // evidence. + if (growthArmed) { + log("warn", `[${session.id}] auto-fold made no progress (0 range(s) folded) — forwarding as-is (estimate fits the model window ${limit}); backing off auto-fold for ${Math.round(AUTO_FOLD_BACKOFF_MS / 60_000)}m so classic nudges resume`); + armAutoFoldBackoff(session); + markDirty(session); + } else { + log("warn", `[${session.id}] preflight made no progress but the payload fits; forwarding as-is`); + } + } + return outbound; + } + // #1839: the two measurements disagree — preflight's own final view + // (post-fold content + images + wire overhead) fits, but the fresh + // normal-config rebuild measures over. That divergence produced the + // self-contradictory fail-fast ("context ~44231 … exceeds window + // 253725"). Forward once and let the upstream arbitrate (the #496 + // house pattern): a genuinely over-window payload is rejected once + // and armOverflowShrink recovers with real evidence; a fitting + // payload is no longer refused on a stale measurement. One forward + // only — the #330 relaxed-zone caveat still bounds the risk. + if (result.compressedRanges > 0 && !unknownBaseline && result.payloadEstimate < limit) { + log("warn", `[${session.id}] preflight view fits (~${result.payloadEstimate}/${limit}) but the rebuilt payload measures over — forwarding once for upstream arbitration (#1839)`); + return outbound; } // [#autoFold] fail-fast is an OVERFLOW concept: a growth-armed payload // fits the real window by definition, so a fold that could not reach the diff --git a/src/server/relay.ts b/src/server/relay.ts index bae0df80c4..213af166aa 100644 --- a/src/server/relay.ts +++ b/src/server/relay.ts @@ -416,6 +416,7 @@ export async function forward( // the rewriter loop below so fetch and loop stop together. const clientAbort = new AbortController(); registerRequestAbort(res, clientAbort); + if (clientAbort.signal.aborted) return; res.on("close", () => { if (!res.writableEnded) { // #1647: without this, a client killed by its own undici bodyTimeout diff --git a/tests/preflight-client-abort.test.ts b/tests/preflight-client-abort.test.ts new file mode 100644 index 0000000000..db66168141 --- /dev/null +++ b/tests/preflight-client-abort.test.ts @@ -0,0 +1,259 @@ +import assert from "node:assert/strict"; +import http from "node:http"; +import net from "node:net"; +import { once } from "node:events"; +import { randomUUID } from "node:crypto"; +import { afterEach, test } from "node:test"; +import { createCore, defaultConfig, defaultPrompts, type CoreMessage } from "acp-kernel"; +import { type ProxyOptions } from "../src/config.ts"; +import { preflightCompress, estimateCoreMessagesUpper } from "../src/preflight.ts"; +import { preflightCompressIfNeeded, registerRequestAbort, type Prepared } from "../src/server.ts"; +import { forward } from "../src/server/relay.ts"; +import { getSession } from "../src/session.ts"; +import { parseExternalSummarySettings } from "../src/external-summary-settings.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _liveUpstreamTimersForTest, _resetFetchUtilForTest } from "../src/fetch-util.ts"; + +process.env.NODE_ENV = "test"; +_setStoreForTest(new SessionStore({ enabled: false })); +const SUMMARY = "SUMMARY: preserve the task goal, exact acceptance criteria and next step; the repeated fixture output is disposable."; + +afterEach(() => { + assert.equal(_liveUpstreamTimersForTest(), 0); + _resetFetchUtilForTest(); +}); + +async function withUpstream(run: (url: string, mainCalls: () => number) => Promise) { + let mainCalls = 0; + const server = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (chunk: Buffer) => chunks.push(chunk)); + req.on("end", () => { + const body = JSON.parse(Buffer.concat(chunks).toString()) as { stream?: boolean }; + if (body.stream) mainCalls++; + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ output: [{ type: "message", role: "assistant", content: [{ type: "output_text", text: SUMMARY }] }] })); + }); + }); + server.listen(0, "127.0.0.1"); + await once(server, "listening"); + const address = server.address(); + assert.ok(address && typeof address === "object"); + try { + await run(`http://127.0.0.1:${address.port}/responses`, () => mainCalls); + } finally { + const closed = once(server, "close"); + server.close(); + server.closeAllConnections(); + await closed; + } +} + +function fixture(url: string, window = 6000) { + const session = getSession(`client-abort-${randomUUID()}`); + const core = createCore(); + const config = defaultConfig(window, { preserveRecentMessages: 0, preserveRecentTokens: 0 }); + const messages: CoreMessage[] = [ + { id: "first", role: "user", contentType: "text", text: "Keep the task goal and acceptance criteria." }, + { id: "large", role: "assistant", contentType: "text", text: "FILLER_".repeat(4000) }, + { id: "last", role: "user", contentType: "text", text: "Continue the task." }, + ]; + const prepared: Prepared = { + body: JSON.stringify({ model: "test-model", stream: true, input: messages.map(m => ({ role: m.role, content: m.text })) }), + session, processedMessages: messages, originalMessages: messages, + protocol: "responses", stream: true, compressInjected: false, + }; + const req = new http.IncomingMessage(new net.Socket()); + req.method = "POST"; + req.url = url; + req.headers = { "content-type": "application/json" }; + const res = new http.ServerResponse(req); + const opts: ProxyOptions = { + host: "127.0.0.1", port: 0, upstream: url, routes: {}, + proxy: "", proxyMode: "direct", proxySource: "direct", + modelContextLimit: window, kernelConfig: config, + compress: { injectTool: false, injectNudge: false }, + promptCache: { routing: "auto" }, compat: { roles: {} }, + streamErrorShape: "protocol", sessionHeader: "x-acp-session", + log: false, debug: false, passthrough: false, passthroughSource: null, + autoUpdate: false, autoRestartOnUpdate: false, updateTag: "latest", + advisoryCheck: false, releaseNotesCheck: false, mitm: { enabled: false, domains: [] }, + }; + const logs: string[] = []; + const log = (_level: string, message: string) => { logs.push(message); }; + return { session, core, config, messages, prepared, req, res, opts, log, logs }; +} + +type Fixture = ReturnType; +function rebuild(f: Fixture): Prepared { + const turn = f.core.processTurn({ messages: f.messages, state: f.session.state, config: f.config, + tokenCount: f.session.stats.lastInputTokens, renderTags: "text-only" }); + f.session.stats.requests++; + return { ...f.prepared, processedMessages: turn.messages, + body: JSON.stringify({ model: "test-model", stream: true, input: turn.messages.map(m => ({ role: m.role, content: m.text })) }), + }; +} + +function gate(f: Fixture, rebuild: () => Promise) { + return preflightCompressIfNeeded(f.prepared, rebuild, f.req, Buffer.from(f.prepared.body), f.res, + f.opts, f.core, f.config, f.config.modelContextLimit, "test-model", undefined, undefined, undefined, undefined, false, f.log, "abort-test"); +} + +test("preflight keeps cancellation after an applied fold makes the final payload fit", async () => { + await withUpstream(async url => { + const f = fixture(url); + const ac = new AbortController(); + const processTurn = f.core.processTurn.bind(f.core); + f.core.processTurn = params => { + const turn = processTurn(params); + // 在真实摘要应用后的最终窗口检查时取消。 + if (params.config.modelContextLimit === 600_000 && f.session.state.blocks.length > 0 && estimateCoreMessagesUpper(turn.messages) < 6000) ac.abort(); + return turn; + }; + const result = await preflightCompress({ core: f.core, session: f.session, config: f.config, + prompts: defaultPrompts, protocol: "responses", url, headers: {}, model: "test-model", + signal: ac.signal, log: f.log }, f.messages); + assert.ok(result.compressedRanges > 0); + assert.equal(ac.signal.aborted, true); + assert.equal(result.fitsWindow, true, JSON.stringify({ result, logs: f.logs })); + assert.equal(result.failure?.kind, "aborted"); + assert.ok(JSON.stringify(f.session.state.blocks).includes(SUMMARY), "completed folds survive cancellation"); + }); +}); + +for (const pluginMode of [false, true]) { + test(`cancel during a fitting rebuild prevents main forwarding (plugin=${pluginMode})`, { timeout: 10_000 }, async () => { + await withUpstream(async (url, mainCalls) => { + const f = fixture(url); + f.prepared.pluginMode = pluginMode; + let ready!: () => void; + const readyPromise = new Promise(resolve => { ready = resolve; }); + let complete!: (result: Awaited>) => void; + let fail!: (error: unknown) => void; + const completedPromise = new Promise>>((resolve, reject) => { complete = resolve; fail = reject; }); + const downstream = http.createServer((req, res) => { + req.resume(); + f.req = req; + f.req.url = url; + f.res = res; + gate(f, async () => { + const closed = once(res, "close"); + ready(); + await closed; + return rebuild(f); + }).then(complete, fail); + }); + downstream.listen(0, "127.0.0.1"); + await once(downstream, "listening"); + const address = downstream.address(); + assert.ok(address && typeof address === "object"); + const client = http.request({ host: "127.0.0.1", port: address.port, method: "POST" }); + client.on("error", () => {}); // 主动断开产生的 ECONNRESET 是本用例的预期行为。 + client.end(); + try { + await Promise.race([readyPromise, completedPromise.then(() => assert.fail("rebuild was not reached"))]); + client.destroy(); + const result = await completedPromise; + assert.ok("failFast" in result, "a fitting payload must not erase cancellation"); + assert.equal(result.respond, false); + assert.equal(result.retryable, false); + assert.ok(f.session.state.blocks.length > 0); + assert.equal(mainCalls(), 0); + } finally { + client.destroy(); + const closed = once(downstream, "close"); + downstream.close(); + downstream.closeAllConnections(); + await closed; + } + }); + }); +} + +test("cancelled growth auto-fold stops before fail-open forwarding and backoff", async () => { + await withUpstream(async (url, mainCalls) => { + const f = fixture(url, 50_000); + f.session.stats.lastInputTokens = 10_000; + f.session.stats.lastInputTokensSource = "usage"; + Object.assign(f.config, { externalSummary: parseExternalSummarySettings({ + enabled: true, autoFold: true, autoFoldTargetTokens: 8192, + targets: [{ name: "sum", protocol: "responses", url, model: "test-model", apiKey: "fixture-key" }], + }, { inlineKeys: true }) }); + f.prepared.nudge = f.core.processTurn({ messages: f.messages, state: f.session.state, + config: f.config, tokenCount: 10_000, renderTags: "text-only" }).nudge; + assert.ok(f.prepared.nudge?.compressibleRanges.length); + const processTurn = f.core.processTurn.bind(f.core); + f.core.processTurn = params => { + const turn = processTurn(params); + f.res.emit("close"); + return turn; + }; + const result = await gate(f, async () => assert.fail("cancelled zero-progress fold must not rebuild")); + assert.ok(f.logs.some(message => message.includes("auto-fold growth floor")), JSON.stringify(f.logs)); + assert.ok("failFast" in result, "cancellation must override growth fail-open"); + assert.equal(result.respond, false); + assert.equal(result.retryable, false); + assert.equal(mainCalls(), 0); + assert.equal(f.session.metadata.autoFoldBackoffUntil, undefined, "cancellation is not a summary-service failure"); + }); +}); + +test("live fitting rebuild forwards exactly once and retains its summary", async () => { + await withUpstream(async (url, mainCalls) => { + const f = fixture(url); + const result = await gate(f, async () => rebuild(f)); + assert.ok(!("failFast" in result)); + assert.ok(result.body.toString().includes(SUMMARY)); + await forward(f.req, f.res, f.opts, result.body, result, f.core, f.config, f.log, undefined, "abort-test"); + assert.equal(mainCalls(), 1); + assert.ok(JSON.stringify(f.session.state.blocks).includes(SUMMARY)); + }); +}); + +test("relay rejects a response already closed before it registers the abort listener", async () => { + await withUpstream(async (url, mainCalls) => { + const f = fixture(url); + f.res.destroy(); + f.res.emit("close"); + await forward(f.req, f.res, f.opts, f.prepared.body, null, f.core, f.config, f.log, undefined, "abort-test"); + assert.equal(mainCalls(), 0, "the main fetch must start with an aborted signal"); + }); +}); + +test("request abort handoff inherits a watchdog cancellation before response end", () => { + const req = new http.IncomingMessage(new net.Socket()); + const res = new http.ServerResponse(req); + const old = new AbortController(); + registerRequestAbort(res, old); + old.abort(); + assert.equal(res.writableEnded, false); + const next = new AbortController(); + registerRequestAbort(res, next); + assert.equal(next.signal.aborted, true); +}); + +test("preflight preserves an existing aborted failure even when its input already fits", async () => { + await withUpstream(async (url, mainCalls) => { + const f = fixture(url); + const ac = new AbortController(); + ac.abort(); + const result = await preflightCompress({ core: f.core, session: f.session, config: f.config, + prompts: defaultPrompts, protocol: "responses", url, headers: {}, model: "test-model", + signal: ac.signal, log: f.log }, [f.messages[0]]); + assert.equal(result.fitsWindow, true); + assert.equal(result.failure?.kind, "aborted"); + assert.equal(mainCalls(), 0); + }); +}); + +test("request abort registration keeps a live response usable and rejects an ended response", () => { + const req = new http.IncomingMessage(new net.Socket()); + const res = new http.ServerResponse(req); + const live = new AbortController(); + registerRequestAbort(res, live); + assert.equal(live.signal.aborted, false); + res.end(); + const ended = new AbortController(); + registerRequestAbort(res, ended); + assert.equal(ended.signal.aborted, true); +});