From ac6a53824508ad8626d5380a5395e1f8dab4bd39 Mon Sep 17 00:00:00 2001 From: ranxianglei Date: Fri, 25 Sep 2026 19:36:22 +0800 Subject: [PATCH] fix: rescue context-overflow turns in-request instead of arming a rescue that never comes (#1195) Wire clients (pi/omp on the plain proxy path) treat an upstream context-overflow 400 as fatal and end the session, so the #987 arm-and-fold-on-the-next-turn recovery never gets its next turn and the session locks. When forward() sees a context overflow with a stated window, re-run prepare+preflight under the session lock with the stated window as a per-call limit override, fold the payload below the REAL window, and re-send it once within the same request. Nothing is learned; the declared window keeps governing (#987). An unfoldable payload keeps today's verbatim passthrough; a rejected retry passes the RETRY's verdict through, not the stale first 400. Fixes #1195 --- src/server.ts | 243 ++++++++++++++++------- tests/e2e-overflow-selfheal.test.ts | 26 +-- tests/issue1195-overflow-refold.test.ts | 253 ++++++++++++++++++++++++ 3 files changed, 445 insertions(+), 77 deletions(-) create mode 100644 tests/issue1195-overflow-refold.test.ts diff --git a/src/server.ts b/src/server.ts index 8a3593e5ce..d0ebf690f9 100644 --- a/src/server.ts +++ b/src/server.ts @@ -102,7 +102,7 @@ import { flushPrefixAffinity, hydratePrefixAffinity, scheduleAffinityPersist } f import { consumePluginRegisterFor, flushConversations, handlePluginCompact, handlePluginManifest, handlePluginRegister, handlePluginRuntimeInfo, handlePluginStatus, handlePluginTool, loadConversations, pipePluginChatWithStrip, pipePluginJson, pipePluginResponsesWithStrip, pluginAgentHeader, pluginConversationHeader, pluginHeadersMatchModel, pluginReportedContextWindow, pluginReportedMaxOutput, pluginRuntimeInfoFor, recordChainVerdict, recordPluginSession, rememberPluginMessages, takePendingPluginRegister } from "./plugin.js"; import { setupMitm, readMitmUpstream, getBlindTunnelStats } from "./mitm.js"; import type { BiliMessage } from "acp-kernel/wire"; -import { BILI_PASSTHROUGH_HEADER, BILI_PLUGIN_BYPASS_HEADER, hardenOpenaiAssistantContent, isLoopbackAddress, inspectContextOverflow, reserveOutputHeadroom, resolveOutputHeadroomCap, shouldReserveOutputHeadroom, systemToUser, usageOutputTotal, usageTotals, type WireProtocol } from "./util.js"; +import { BILI_PASSTHROUGH_HEADER, BILI_PLUGIN_BYPASS_HEADER, hardenOpenaiAssistantContent, isLoopbackAddress, inspectContextOverflow, reserveOutputHeadroom, resolveOutputHeadroomCap, shouldReserveOutputHeadroom, systemToUser, usageOutputTotal, usageTotals, type ContextOverflowInfo, type WireProtocol } from "./util.js"; import { BILI_TUNNEL_HEADER, checkTunnelDestination, classifyIp, localMachineIps, normalizeIpLiteral, parseIpLiteral, tunnelAllowlistFromEnv } from "./tunnel-guard.js"; import { dumpRejectedBody } from "./error-dump.js"; @@ -2036,9 +2036,18 @@ async function handle( // session id (Claude Code subagents) behind one slow upstream stream. // forward() re-acquires the lock around its own discrete mutation // sections; do not re-wrap the whole forward in this lock. - const pendingForward = await withSessionLock( - session, - async (): Promise<{ body: string | Buffer; prepared: Prepared | null } | null> => { + // #1195: prepare + preflight run as a REUSABLE closure so an upstream + // overflow can re-run the stage mid-request (see the overflowRefold + // wiring below): the caller re-enters it under the session lock with + // the window the upstream STATED, forcing the fold the declared + // window could never trigger. respondFailFast=false (the overflow + // retry path) suppresses the fail-fast response — forward() answers + // with the original upstream 400 instead, preserving today's + // client-visible contract when the payload cannot be rescued. + const runPreparedPipeline = async ( + respondFailFast: boolean, + overflowWindow?: number, + ): Promise<{ body: string | Buffer; prepared: Prepared | null } | null> => { const runPrepare = async (): Promise => { const cs = resolveCompress(opts.routes, route?.rewrittenUrl, requestModel, opts.compress); const visibilityMarkers = cs.visibilityMarkers ?? true; @@ -2116,7 +2125,7 @@ async function handle( res, opts, core, - reqConfig, + overflowWindow !== undefined ? { ...reqConfig, modelContextLimit: overflowWindow } : reqConfig, nativeWindow, requestModel, resolvedNativeWindow, @@ -2131,7 +2140,7 @@ async function handle( // #301: the payload still overflows the window and // preflight could not fix it — answer with a // structured error instead of forwarding. - if (outcome.respond && !res.destroyed) { + if (respondFailFast && outcome.respond && !res.destroyed) { if (res.headersSent) { // #568: the hold already committed 200 early — the status // can no longer change, so deliver the same error in-band @@ -2176,10 +2185,30 @@ async function handle( } logRequestCost(log, session.id, inboundMsgs, inboundBytes, reqT0, prepared!.body); return { body: prepared!.body, prepared: prepared! }; - }); + }; + const pendingForward = await withSessionLock(session, () => runPreparedPipeline(true)); if (pendingForward) { forwarded = true; - await forward(req, res, opts, pendingForward.body, pendingForward.prepared, core, reqConfig, log, route, instanceId, affinity); + // #1195: wire clients (omp/pi on the plain proxy path) treat an + // upstream overflow 400 as fatal and end the session — the armed + // emergency shrink then never gets its "next turn". When forward() + // sees a context overflow it calls this hook: re-run prepare+ + // preflight under the lock with the window the upstream STATED + // (per-call limit override — nothing is learned, #987 keeps + // governing), folding the payload below the REAL window and + // re-sending it within this same request. An unchanged body + // (nothing foldable) returns null and forward() passes the + // original 400 through verbatim. + const overflowRefold = pendingForward.prepared + ? async (realWindow: number | undefined): Promise => { + const next = await withSessionLock(session, () => runPreparedPipeline(false, realWindow)); + if (!next || String(next.body) === String(pendingForward.body)) return null; + pendingForward.body = next.body; + pendingForward.prepared = next.prepared; + return next.body; + } + : undefined; + await forward(req, res, opts, pendingForward.body, pendingForward.prepared, core, reqConfig, log, route, instanceId, affinity, overflowRefold); // Remember for ALL modes (not just plugin): wire clients (dsh, // hermes, unplug'd pi) read the same panel via /__bili/plugin/status // and need the nudge/breakdown sections too; locked so a racing @@ -4196,6 +4225,7 @@ async function forward( route: ReturnType, instanceId: string, affinity?: string, + overflowRefold?: (realWindow: number | undefined) => Promise, ): Promise { // E2: a codex native-compaction request intercepted in prepare() carries a // forged success response — serve it without contacting upstream. @@ -4487,6 +4517,141 @@ async function forward( } } } + // #987/#1195: arm the one-shot emergency shrink (declared window unchanged, + // nothing persisted/learned) — extracted so the same-request overflow retry + // below and the !ok passthrough share it exactly once per request. + let overflowArmed = false; + const armOverflowShrink = (info: ContextOverflowInfo): void => { + if (overflowArmed || !info.isOverflow) return; + overflowArmed = true; + const s = prepared!.session; + let reqModel: string | undefined; + let rawBody: string | undefined; + try { + rawBody = typeof prepared!.body === "string" ? prepared!.body : prepared!.body.toString("utf8"); + const parsedBody = JSON.parse(rawBody) as Record; + reqModel = typeof parsedBody.model === "string" ? parsedBody.model : undefined; + } catch { + reqModel = undefined; + } + if (info.window) { + // Arm the emergency shrink at EXACTLY the stated window: the + // upstream just proved a turn cannot succeed above it, so the + // next turn's kernel emergency nudge + tool-result truncate + // must fire. #857: a number the upstream itself stated is + // usage-grade provenance (not a content estimate); a real + // usage report on the next successful turn overwrites it. + s.stats.lastInputTokens = info.window; + s.stats.lastInputTokensSource = "usage"; + // #1110: record the arm SEPARATELY so the side-request guard + // can read it without ever touching the nudge baseline. + s.stats.overflowArmTokens = info.window; + log("warn", `[${s.id}] upstream context overflow (model=${reqModel ?? "unknown"}) — window ${info.window} stated upstream; armed emergency shrink, declared window unchanged (#987)`); + } else { + // No window number stated — nothing to learn (and #987 + // removed the learner anyway), but the rejection itself is + // evidence at the size actually sent: arm at + // min(declared, payload estimate). A payload BELOW the + // declared window being rejected means the declaration is + // wrong (or the upstream is flaky) — arming at the payload's + // own size never over-triggers, while a payload OVER the + // declared window arms at the declaration — which is what + // the #496 image-relay forward-once gate needs to break the + // #488 400 loop after exactly one rejected forward. + const declared = typeof s.metadata.effectiveContextLimit === "number" ? s.metadata.effectiveContextLimit : 0; + let est = 0; + try { + est = estimateTokensFast(rawBody ?? ""); + } catch { est = 0; } + const arm = Math.max(0, Math.min(declared, Number.isFinite(est) ? est : declared)); + if (arm > 0) { + s.stats.lastInputTokens = arm; + s.stats.lastInputTokensSource = "usage"; + s.stats.overflowArmTokens = arm; // #1110: guard reads this, not the baseline + } + log("warn", `[${s.id}] upstream context overflow (window not parseable, model=${reqModel ?? "unknown"}) — armed emergency shrink at ~${arm} tokens (min of declared ${declared} and payload estimate), nothing learned (#987): ${info.message}`); + } + // The armed emergency (lastInputTokens) lives in memory only + // until scheduled — the error path returns before forward()'s + // trailing markDirty, so schedule the save HERE or the arm is + // lost on restart. + markDirty(s); + }; + // #1195: a context overflow used to arm the shrink and pass the 400 through + // verbatim, expecting the NEXT turn to fold — but wire clients treat the + // error as fatal and end the session, so the armed rescue never runs and the + // session locks. With a refold hook: arm, let the caller re-run prepare+ + // preflight against the window the upstream STATED (per-call override, no + // learning), and re-send the folded body ONCE within this same request. An + // unchanged body (nothing foldable), a null refold, or a transport failure + // falls back to today's verbatim passthrough below. + if ( + overflowRefold && + prepared?.session && + (upstreamResult.response.status === 400 || upstreamResult.response.status === 413) && + upstreamResult.response.body && + res.writable && + !res.writableEnded && + !res.destroyed + ) { + let overflowText: string | null = null; + try { + overflowText = (await readStreamToBuffer(upstreamResult.response.body)).toString("utf8"); + } catch { + overflowText = null; + } + if (overflowText !== null) { + // Rebuild the consumed body (same shape as the role ladder above) + // so the error path below reads the same bytes verbatim. + upstreamResult = { + response: new Response(overflowText, { + status: upstreamResult.response.status, + statusText: upstreamResult.response.statusText, + headers: new Headers(upstreamResult.response.headers), + }), + clearTimer: upstreamResult.clearTimer, + stopIdleTimer: upstreamResult.stopIdleTimer, + }; + const overflowInfo = inspectContextOverflow(upstreamResult.response.status, overflowText); + if (overflowInfo.isOverflow) { + armOverflowShrink(overflowInfo); + const refolded = await overflowRefold(overflowInfo.window).catch(() => null); + if (refolded) { + try { + const retried = await fetchWithTimeout(upstreamUrl, { ...init, body: refolded }, undefined, clientAbort.signal); + if (retried.response.ok) { + upstreamResult.clearTimer(); + upstreamResult = retried; + log("info", `[${prepared.session.id}] context overflow — refolded and re-sent within the same request, upstream accepted (#1195)`); + } else { + let retryErrText: string | null = null; + if (retried.response.body) { + try { + retryErrText = (await readStreamToBuffer(retried.response.body)).toString("utf8"); + } catch { + retryErrText = null; + } + } + // Answer with the retry's own verdict, not the stale first 400. + upstreamResult.clearTimer(); + upstreamResult = { + response: new Response(retryErrText ?? "", { + status: retried.response.status, + statusText: retried.response.statusText, + headers: new Headers(retried.response.headers), + }), + clearTimer: retried.clearTimer, + stopIdleTimer: retried.stopIdleTimer, + }; + log("warn", `[${prepared.session.id}] context overflow — refold retry still rejected (HTTP ${retried.response.status}); passing the retry response through (#1195)`); + } + } catch { + // transport failure — the buffered original 400 answers below + } + } + } + } + } const { response: upstream, clearTimer: clearUpstreamTimer } = upstreamResult; const respHeaders: Record = {}; const respConnNamed = connectionNamedHeaders(upstream.headers.get("connection") ?? undefined); @@ -4526,7 +4691,9 @@ async function forward( // when the configured window is wrong (e.g. the 200k fallback for an // unknown model on a relay) an upstream 400 is the only reliable signal // that the real window is smaller. Learn the window, arm an emergency - // shrink for the next turn, then pass the error through verbatim. + // shrink for the next turn, then pass the error through verbatim. When + // the #1195 same-request refold above already ran, this is the + // passthrough of last resort (retry rejected / nothing foldable). let errBody: Buffer | null = null; if (upstream.body) { try { @@ -4539,63 +4706,7 @@ async function forward( const s = prepared.session; const info = inspectContextOverflow(upstream.status, errBody.toString("utf8")); if (info.isOverflow) { - // #987: no window learning — the context window is a deployment - // property, owned by declarations (config / registry / - // runtime-info), never adjusted from session traffic. An overflow - // still arms the one-shot emergency shrink so the NEXT turn - // compresses below the failing size, but nothing is persisted - // and the declared window keeps governing. - let reqModel: string | undefined; - let rawBody: string | undefined; - try { - rawBody = typeof prepared.body === "string" ? prepared.body : prepared.body.toString("utf8"); - const parsedBody = JSON.parse(rawBody) as Record; - reqModel = typeof parsedBody.model === "string" ? parsedBody.model : undefined; - } catch { - reqModel = undefined; - } - if (info.window) { - // Arm the emergency shrink at EXACTLY the stated window: the - // upstream just proved a turn cannot succeed above it, so the - // next turn's kernel emergency nudge + tool-result truncate - // must fire. #857: a number the upstream itself stated is - // usage-grade provenance (not a content estimate); a real - // usage report on the next successful turn overwrites it. - s.stats.lastInputTokens = info.window; - s.stats.lastInputTokensSource = "usage"; - // #1110: record the arm SEPARATELY so the side-request guard - // can read it without ever touching the nudge baseline. - s.stats.overflowArmTokens = info.window; - log("warn", `[${s.id}] upstream context overflow (model=${reqModel ?? "unknown"}) — window ${info.window} stated upstream; armed emergency shrink, declared window unchanged (#987)`); - } else { - // No window number stated — nothing to learn (and #987 - // removed the learner anyway), but the rejection itself is - // evidence at the size actually sent: arm at - // min(declared, payload estimate). A payload BELOW the - // declared window being rejected means the declaration is - // wrong (or the upstream is flaky) — arming at the payload's - // own size never over-triggers, while a payload OVER the - // declared window arms at the declaration — which is what - // the #496 image-relay forward-once gate needs to break the - // #488 400 loop after exactly one rejected forward. - const declared = typeof s.metadata.effectiveContextLimit === "number" ? s.metadata.effectiveContextLimit : 0; - let est = 0; - try { - est = estimateTokensFast(rawBody ?? ""); - } catch { est = 0; } - const arm = Math.max(0, Math.min(declared, Number.isFinite(est) ? est : declared)); - if (arm > 0) { - s.stats.lastInputTokens = arm; - s.stats.lastInputTokensSource = "usage"; - s.stats.overflowArmTokens = arm; // #1110: guard reads this, not the baseline - } - log("warn", `[${s.id}] upstream context overflow (window not parseable, model=${reqModel ?? "unknown"}) — armed emergency shrink at ~${arm} tokens (min of declared ${declared} and payload estimate), nothing learned (#987): ${info.message}`); - } - // The armed emergency (lastInputTokens) lives in memory only - // until scheduled — the error path returns before forward()'s - // trailing markDirty, so schedule the save HERE or the arm is - // lost on restart. - markDirty(s); + armOverflowShrink(info); } // #762: learn strict-echo on the MAIN request path too. The loop-only // learner (src/loop/core.ts) never sees client-originated 400s, so a diff --git a/tests/e2e-overflow-selfheal.test.ts b/tests/e2e-overflow-selfheal.test.ts index 071351c9bf..487207eb54 100644 --- a/tests/e2e-overflow-selfheal.test.ts +++ b/tests/e2e-overflow-selfheal.test.ts @@ -99,7 +99,7 @@ function proxyBaseOptions(upstreamPort: number, window: number): ProxyOptions { } as ProxyOptions; } -test("e2e #987 T1: a stated-window overflow arms the emergency shrink but learns nothing — the declared window keeps governing", async () => { +test("e2e #987/#1195 T1: a stated-window overflow arms the one-shot shrink, learns nothing, and (#1195) the turn is rescued in-request", async () => { let streamingCall = 0; let summaryCalls = 0; const bodies: string[] = []; @@ -146,28 +146,32 @@ test("e2e #987 T1: a stated-window overflow arms the emergency shrink but learns const body = JSON.stringify({ model: "claude-test", max_tokens: 1024, stream: true, messages: bigConversation() }); const headers = { "content-type": "application/json", "x-acp-session": "t1-sess" }; - // r1: the 400 (stating 128000) passes through verbatim and arms the - // one-shot emergency shrink — but persists NO learned window. + // r1: the upstream 400 stating 128000 no longer reaches the client — + // #1195: the arm fires, prepare+preflight re-run against the STATED + // window (per-call override, nothing learned), the folded body is + // re-sent within the same request, and the retry's 200 answers. const r1 = await fetch(url, { method: "POST", headers, body }); - assert.equal(r1.status, 400); - const r1text = await r1.text(); - assert.ok(r1text.includes("prompt is too long"), "error body passes through verbatim"); + assert.equal(r1.status, 200, "#1195: refolded and re-sent within the same request"); + assert.equal(r1.headers.get("content-type"), "text/event-stream", "the retry's SSE is what the client sees"); + await r1.text(); const s = listSessions().find((x) => x.id === "t1-sess"); assert.ok(s, "session exists"); assert.equal(s!.metadata.confirmedContextLimits, undefined, "#987: nothing learned"); assert.equal(s!.metadata.confirmedContextLimit, undefined, "#987: no legacy scalar either"); - assert.equal(s!.stats.lastInputTokens, 128000, "emergency shrink armed at the stated window"); + assert.equal(s!.stats.lastInputTokens, 5000, "the retry's own usage report already replaced the armed baseline"); assert.equal(s!.stats.lastInputTokensSource, "usage", "a window the upstream stated is usage-grade (#857)"); + assert.equal(s!.stats.overflowArmTokens, undefined, "#1129: the retry's usage report retired the one-shot arm — the emergency is over"); // r2: same payload, declared window (400k) governs — the payload is - // IN-window, so no preflight fold: forwarded verbatim, upstream 200. + // IN-window, so no further fold: forwarded verbatim, upstream 200. const r2 = await fetch(url, { method: "POST", headers, body }); assert.equal(r2.status, 200); await r2.text(); - assert.equal(summaryCalls, 0, "in-window payload under the declared window — no fold"); + assert.equal(summaryCalls, 1, "exactly the one fold from r1's rescue — r2 itself does not fold"); const lastForward = bodies[bodies.length - 1]; - assert.ok(lastForward.includes("MARKER_1_") && lastForward.includes("MARKER_11_"), "history forwarded verbatim"); - assert.ok(!lastForward.includes(SUMMARY_TEXT), "no fold"); + assert.ok(lastForward.includes(SUMMARY_TEXT), "the fold is sticky — r2 forwards the compacted view, not the resent full history"); + assert.ok(lastForward.includes("MARKER_11_"), "the recent tail is preserved verbatim"); + assert.ok(!lastForward.includes("MARKER_1_"), "the already-folded early range stays folded"); const s2 = listSessions().find((x) => x.id === "t1-sess"); assert.equal(s2?.stats.lastInputTokens, 5000, "the successful turn's usage report overwrote the armed value"); assert.equal(s2?.metadata.confirmedContextLimits, undefined, "still nothing learned after recovery"); diff --git a/tests/issue1195-overflow-refold.test.ts b/tests/issue1195-overflow-refold.test.ts new file mode 100644 index 0000000000..0d90f45054 --- /dev/null +++ b/tests/issue1195-overflow-refold.test.ts @@ -0,0 +1,253 @@ +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, 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"; + +// #1195: wire clients (omp/pi on the plain proxy path) treat an upstream +// context-overflow 400 as FATAL — they end the session on the first error, so +// the #987 arm-and-wait-for-the-next-turn rescue never gets its next turn and +// the session locks. The fix: when forward() sees a context overflow with a +// stated window, it re-runs prepare+preflight with the STATED window as a +// per-call limit override, folds the payload below the REAL window, and +// re-sends it ONCE within the same request. The original 400 still passes +// through verbatim when the payload cannot be rescued (nothing foldable, or +// the retry is also rejected) — the client-visible contract only IMPROVES. + +const STATED_OVERFLOW_BODY_128K = JSON.stringify({ + type: "error", + error: { type: "invalid_request_error", message: "prompt is too long: 130000 tokens > 128000 maximum" }, +}); + +const RETRY_REJECT_BODY = JSON.stringify({ + type: "error", + error: { type: "invalid_request_error", message: "rejected after compaction: 129000 tokens > 128000 maximum (retry-after-refold)" }, +}); + +function okSse(): string { + return ( + `event: message_start\ndata: ${JSON.stringify({ type: "message_start", message: { id: "m1", role: "assistant", usage: { input_tokens: 5000 } } })}\n\n` + + `event: content_block_start\ndata: ${JSON.stringify({ type: "content_block_start", index: 0, content_block: { type: "text", text: "" } })}\n\n` + + `event: content_block_delta\ndata: ${JSON.stringify({ type: "content_block_delta", index: 0, delta: { type: "text_delta", text: "ok" } })}\n\n` + + `event: content_block_stop\ndata: ${JSON.stringify({ type: "content_block_stop", index: 0 })}\n\n` + + `event: message_delta\ndata: ${JSON.stringify({ type: "message_delta", delta: { stop_reason: "end_turn", stop_sequence: null }, usage: { output_tokens: 3 } })}\n\n` + + `event: message_stop\ndata: ${JSON.stringify({ type: "message_stop" })}\n\n` + ); +} + +const SUMMARY_TEXT = + "PREFLIGHT SUMMARY: the segment covered a multi-step debugging session. Key decisions: chose the preflight approach over lossy truncation because the payload must stay coherent. Files touched: src/a.ts:10, src/b.ts:20. Outcome: fixed and verified by tests."; + +function summaryJson(): string { + return JSON.stringify({ + id: "msg_summary", + type: "message", + role: "assistant", + model: "claude-test", + content: [{ type: "text", text: SUMMARY_TEXT }], + stop_reason: "end_turn", + usage: { input_tokens: 500, output_tokens: 50 }, + }); +} + +function bigConversation(): Array<{ role: string; content: string }> { + const msgs: Array<{ role: string; content: string }> = []; + for (let i = 0; i < 12; i++) { + const role = i % 2 === 0 ? "user" : "assistant"; + msgs.push({ role, content: `Message ${i} of the long conversation. ` + `MARKER_${i}_content_`.repeat(250) }); + } + return msgs; +} + +function proxyBaseOptions(upstreamPort: number, window: number): ProxyOptions { + return { + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: { [`http://127.0.0.1:${upstreamPort}`]: { models: { "claude-test": { context: window } } } }, + modelContextLimit: window, + kernelConfig: defaultConfig(window), + compress: { injectTool: true, injectNudge: true }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + autoUpdate: false, + mitm: { enabled: false, domains: [] }, + } as ProxyOptions; +} + +interface RecordedUpstream { + server: http.Server; + port: number; + bodies: string[]; +} + +// Records every request body in order. Streaming requests are answered by +// `onStreaming(callIndex)`; stream:false (summarization) requests by the +// in-band summary response. +async function recordedUpstream(onStreaming: (callIndex: number, res: http.ServerResponse) => void): Promise { + const bodies: string[] = []; + let streamingCall = 0; + const server = 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"); + bodies.push(raw); + let parsed: { stream?: boolean } = {}; + try { + parsed = JSON.parse(raw) as { stream?: boolean }; + } catch { + parsed = {}; + } + if (parsed.stream === false) { + res.writeHead(200, { "content-type": "application/json" }); + res.end(summaryJson()); + return; + } + const call = streamingCall; + streamingCall += 1; + onStreaming(call, res); + }); + }); + server.listen(0, "127.0.0.1"); + await once(server, "listening"); + return { server, port: (server.address() as { port: number }).port, bodies }; +} + +test("e2e #1195 A: stated-window overflow is rescued within the SAME request — folded body re-sent upstream, client sees the retry's 200", async (t) => { + const upstream = await recordedUpstream((call, res) => { + if (call === 0) { + res.writeHead(400, { "content-type": "application/json" }); + res.end(STATED_OVERFLOW_BODY_128K); + } else { + res.writeHead(200, { "content-type": "text/event-stream" }); + res.end(okSse()); + } + }); + + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + // The operator declared 400k — the upstream just proved the REAL window is 128k. + const proxy = await startServer(proxyBaseOptions(upstream.port, 400_000)); + await once(proxy, "listening"); + const proxyPort = (proxy.address() as { port: number }).port; + t.after(async () => { + proxy.close(); + await once(proxy, "close"); + upstream.server.close(); + await once(upstream.server, "close"); + }); + + const url = `http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstream.port}/v1/messages`; + const body = JSON.stringify({ model: "claude-test", max_tokens: 1024, stream: true, messages: bigConversation() }); + const headers = { "content-type": "application/json", "x-acp-session": "refold-a" }; + + const r1 = await fetch(url, { method: "POST", headers, body }); + assert.equal(r1.status, 200, "the client never sees the 400 — the turn is rescued in-request"); + assert.match(r1.headers.get("content-type") ?? "", /text\/event-stream/); + const r1text = await r1.text(); + assert.ok(r1text.includes("text_delta"), "the retry's SSE stream is delivered"); + + // bodies: [0] original 400'd forward, [1] summarization call, [2] retry. + assert.equal(upstream.bodies.length, 3, `one overflow + one summary + one retry: ${upstream.bodies.length}`); + assert.ok(upstream.bodies[0].includes("MARKER_1_") && upstream.bodies[0].includes("MARKER_11_"), "first forward carried the full history"); + assert.ok(upstream.bodies[2].includes(SUMMARY_TEXT), "the retry carries the fold summary"); + assert.ok(upstream.bodies[2].includes("MARKER_11_") && !upstream.bodies[2].includes("MARKER_1_"), "the retry keeps the recent tail, drops the folded early range"); + assert.ok(upstream.bodies[2].length < upstream.bodies[0].length, "the retried body is smaller than the rejected one"); + + const s = listSessions().find((x) => x.id === "refold-a"); + assert.ok(s, "session exists"); + assert.equal(s!.metadata.confirmedContextLimits, undefined, "#987: nothing learned — the declared window keeps governing"); + assert.equal(s!.stats.lastInputTokensSource, "usage"); +}); + +test("e2e #1195 B: an unfoldable payload passes the ORIGINAL overflow 400 through verbatim — the refold is not a client-visible change", async (t) => { + const upstream = await recordedUpstream((_call, res) => { + res.writeHead(400, { "content-type": "application/json" }); + res.end(STATED_OVERFLOW_BODY_128K); + }); + + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const proxy = await startServer(proxyBaseOptions(upstream.port, 400_000)); + await once(proxy, "listening"); + const proxyPort = (proxy.address() as { port: number }).port; + t.after(async () => { + proxy.close(); + await once(proxy, "close"); + upstream.server.close(); + await once(upstream.server, "close"); + }); + + const url = `http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstream.port}/v1/messages`; + // A single enormous message: nothing to fold — preflight cannot rescue it. + const body = JSON.stringify({ model: "claude-test", max_tokens: 1024, stream: true, messages: [{ role: "user", content: "MEGA_" + "payload ".repeat(60_000) }] }); + const headers = { "content-type": "application/json", "x-acp-session": "refold-b" }; + + const r1 = await fetch(url, { method: "POST", headers, body }); + assert.equal(r1.status, 400, "an unfoldable payload keeps the verbatim passthrough"); + const r1text = await r1.text(); + assert.equal(r1text, STATED_OVERFLOW_BODY_128K, "the ORIGINAL error body is byte-identical"); + + const s = listSessions().find((x) => x.id === "refold-b"); + assert.ok(s, "session exists"); + assert.equal(s!.stats.lastInputTokens, 128000, "the arm still fires — the next turn folds if it can"); + assert.equal(s!.stats.overflowArmTokens, 128000, "#1110: the arm is recorded"); + assert.equal(s!.metadata.confirmedContextLimits, undefined, "#987: nothing learned"); +}); + +test("e2e #1195 C: when the folded retry is ALSO rejected, the client sees the RETRY's verdict, not the stale first 400 — and the arm stays for the next turn", async (t) => { + const upstream = await recordedUpstream((call, res) => { + if (call === 0) { + res.writeHead(400, { "content-type": "application/json" }); + res.end(STATED_OVERFLOW_BODY_128K); + } else { + res.writeHead(400, { "content-type": "application/json" }); + res.end(RETRY_REJECT_BODY); + } + }); + + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const proxy = await startServer(proxyBaseOptions(upstream.port, 400_000)); + await once(proxy, "listening"); + const proxyPort = (proxy.address() as { port: number }).port; + t.after(async () => { + proxy.close(); + await once(proxy, "close"); + upstream.server.close(); + await once(upstream.server, "close"); + }); + + const url = `http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstream.port}/v1/messages`; + const body = JSON.stringify({ model: "claude-test", max_tokens: 1024, stream: true, messages: bigConversation() }); + const headers = { "content-type": "application/json", "x-acp-session": "refold-c" }; + + const r1 = await fetch(url, { method: "POST", headers, body }); + assert.equal(r1.status, 400, "the retry's rejection reaches the client"); + const r1text = await r1.text(); + assert.ok(r1text.includes("retry-after-refold"), "the RETRY's body is what the client sees"); + assert.ok(!r1text.includes("prompt is too long"), "the stale first 400 is not re-served"); + + assert.ok(upstream.bodies.length >= 3, "original + summary + retry all hit the upstream"); + assert.ok(upstream.bodies[2].includes(SUMMARY_TEXT), "the retry was the folded body"); + + const s = listSessions().find((x) => x.id === "refold-c"); + assert.ok(s, "session exists"); + assert.ok( + s!.stats.lastInputTokens > 0 && s!.stats.lastInputTokens < 128000, + `no usage landed — the armed baseline survives minus the fold's reclaim credit: ${s!.stats.lastInputTokens}`, + ); + assert.equal(s!.stats.overflowArmTokens, 128000, "#1110 arm record stays at the stated window"); + assert.equal(s!.metadata.confirmedContextLimits, undefined, "#987: nothing learned"); +});