diff --git a/src/error-dump.ts b/src/error-dump.ts index 6483d89b14..a8b166a8f0 100644 --- a/src/error-dump.ts +++ b/src/error-dump.ts @@ -55,3 +55,34 @@ export function dumpRejectedBody(status: number, sessionId: string, body: string return null; } } + +// #1993: persist a rejected PREFLIGHT SUMMARY exchange — both sides of it, the +// request bytes bili sent and the response bytes the upstream answered — when +// BILI_DUMP_4XX=1. The summary call is built independently of the forwarded +// wire body, so dumpRejectedBody's two main-path call sites never see it; +// without this, "summarization failed: HTTP 400" could not be explained +// byte-for-byte after the fact. Separate `summary-err-` prefix so a main-path +// and a summary dump landing in the same millisecond cannot clobber each +// other. Sides are embedded as strings so the envelope stays valid JSON even +// when a side is not itself JSON. +export function dumpSummaryRejection(status: number, sessionId: string, request: string, response: string): string | null { + if (process.env.BILI_DUMP_4XX !== "1") return null; + if (!request && !response) return null; + try { + const cap = Math.max(1024, Number(process.env.BILI_DUMP_4XX_MAX_BYTES) || DEFAULT_MAX_BYTES); + const side = (raw: string): string => { + const bounded = raw.length > cap ? `${raw.slice(0, cap)}\n[truncated: ${raw.length - cap} more character(s)]` : raw; + try { return JSON.stringify(JSON.parse(bounded)); } catch { return bounded; } + }; + const dir = dumpsDir(); + fs.mkdirSync(dir, { recursive: true }); + const sid = sessionId.replace(/[^a-zA-Z0-9_-]/g, "_"); + const out = path.join(dir, `summary-err-${Date.now()}-${sid}-${status}.json`); + fs.writeFileSync(out, JSON.stringify({ status, request: side(request), response: side(response) }, null, 2)); + loggerLog("info", `[dump] upstream ${status} rejected summary exchange written to ${out}`); + return out; + } catch (err) { + warnDumpFailure(err); + return null; + } +} diff --git a/src/preflight.ts b/src/preflight.ts index 74c501e80c..78d952be41 100644 --- a/src/preflight.ts +++ b/src/preflight.ts @@ -13,6 +13,7 @@ import { applyAbsorbView } from "./absorb.js"; import { adoptContentStore, ccrLoopConfig, contentStoreOf } from "./store.js"; import { applyRanges, normalizeRangeOrder, type RewriteCtx } from "./stream.js"; import { fetchWithTimeout, isTransientUpstreamError, replayMaxAttempts, replayBackoffMs, sleep, UpstreamHttpError } from "./fetch-util.js"; +import { dumpSummaryRejection } from "./error-dump.js"; import { proxyDispatcher } from "./upstream-proxy.js"; import { lastCompressSuffix, type Session } from "./session.js"; import { peekRegistryOutputLimit } from "./registry.js"; @@ -762,7 +763,12 @@ async function requestSummaryBody(deps: PreflightDeps, body: string): Promise= maxAttempts) throw failure; + if (!retryable || attempt >= maxAttempts) { + if (failure instanceof UpstreamHttpError && failure.status >= 400 && failure.status < 500) { + dumpSummaryRejection(failure.status, deps.session.id, body, failure.body); + } + throw failure; + } retryDetail = failure instanceof UpstreamHttpError ? `HTTP ${failure.status}` : failure.message; } finally { clearTimer?.(); @@ -1182,15 +1188,39 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa } } catch (err) { if (err instanceof UpstreamHttpError) { + // #1993: the rejection body is the only evidence for WHY the + // upstream said no — log it the way the main paths already do + // and carry a bounded snippet into the client-visible detail. + const transient = isTransientUpstreamError(err.status, err.body); + const bodySnippet = safePrefix(err.body.trim(), 200); failure = { kind: "upstream", status: err.status, - retryable: isTransientUpstreamError(err.status, err.body), + retryable: transient, detail: err.status === 429 ? `the summarization call was rate-limited by the upstream (HTTP 429)` - : `the summarization call was rejected by the upstream (HTTP ${err.status})`, + : `the summarization call was rejected by the upstream (HTTP ${err.status})${bodySnippet ? `: ${bodySnippet}` : ""}`, }; - deps.log("warn", `[preflight] summarization failed: HTTP ${err.status} after ${err.attempts} attempt(s)`); + deps.log("warn", `[preflight] summarization failed: HTTP ${err.status} after ${err.attempts} attempt(s)${bodySnippet ? `: ${bodySnippet}` : ""}`); + // #1993: a hard (non-transient) size-plausible rejection gets the + // same halving recovery as an unusable HTTP-200 summary — the + // whole request was refused, so it is at least as likely to be + // size-driven as an empty 200. Transient 4xx (risk-control + // markers) keep the replay-retry semantics; other 4xx (auth / + // routing) fail fast instead of burning the call budget; + // unsplittable spans fall through to the give-up below. The + // cascade is bounded by the per-invocation summary budget + // (summaryBudget, #1933) via the while-top budgetHit check. + const floorUnits = baselineKnown ? 2 * MIN_CHUNK_TOKENS : 2 * minChars; + if ((err.status === 400 || err.status === 413) && !transient + && ce > cs && spanUnitsOf(messages, cs, ce, countText) >= floorUnits) { + lastUnusableDetail = `HTTP ${err.status}: ${bodySnippet}`; + deps.log("warn", `[preflight] chunk ${startRef}:${endRef} rejected with HTTP ${err.status}; retrying with smaller chunks`); + const mid = Math.floor((cs + ce) / 2); + spans.push([mid + 1, ce]); + spans.push([cs, mid]); + continue; + } } else if (deps.signal?.aborted) { failure = ABORTED_FAILURE; deps.log("warn", `[preflight] summarization aborted: client disconnected`); @@ -1234,6 +1264,11 @@ export async function preflightCompress(deps: PreflightDeps, messages: CoreMessa skipSet.add(skipKey); break; } + // #1993: a fold succeeded after an earlier rejection in this walk — the + // payload shrank, so the stale failure no longer describes the end + // state; clear it so a later exhaustion reports its own reason (and a + // fitting result reports none at all). + failure = undefined; // The summary itself re-enters the payload; net its cost against // both the folded size and the session's input baseline. Without a // baseline currentTokens is char-based, so net the folded span's diff --git a/tests/error-dump.test.ts b/tests/error-dump.test.ts index caa5bee774..1932c4b3f0 100644 --- a/tests/error-dump.test.ts +++ b/tests/error-dump.test.ts @@ -3,7 +3,7 @@ import assert from "node:assert/strict"; import fs from "node:fs"; import os from "node:os"; import path from "node:path"; -import { dumpRejectedBody } from "../src/error-dump.js"; +import { dumpRejectedBody, dumpSummaryRejection } from "../src/error-dump.js"; import { setLogCapture } from "../src/logger.js"; import { rmrf } from "./tmp-rm.ts"; @@ -113,3 +113,67 @@ test("on: Buffer bodies work and session ids are sanitized", () => { restoreDumpEnv(); } }); + +function summaryErrFiles(): string[] { + return fs.readdirSync(dir).filter((f) => f.startsWith("summary-err-")); +} + +// #1993: the preflight summary exchange is built independently of the forwarded +// wire body, so it needs its own dump call site carrying BOTH sides. + +test("#1993 summary dump off by default: no file written", () => { + delete process.env.BILI_DUMP_4XX; + try { + assert.equal(dumpSummaryRejection(400, "s1", '{"instructions":"i"}', '{"error":"e"}'), null); + assert.deepEqual(summaryErrFiles(), []); + } finally { + restoreDumpEnv(); + } +}); + +test("#1993 summary dump on: envelope carries status plus both sides as strings", () => { + process.env.BILI_DUMP_4XX = "1"; + try { + const out = dumpSummaryRejection(400, "s1", '{"instructions":"compress it","model":"gpt"}', '{"error":{"message":"too large"}}'); + assert.ok(out && out.startsWith(dir)); + const files = summaryErrFiles(); + assert.equal(files.length, 1); + assert.match(files[0]!, /^summary-err-\d+-s1-400\.json$/); + const parsed = JSON.parse(fs.readFileSync(out!, "utf8")) as { status: number; request: unknown; response: unknown }; + assert.equal(parsed.status, 400); + assert.equal(typeof parsed.request, "string"); + assert.equal(typeof parsed.response, "string"); + assert.deepEqual(JSON.parse(parsed.request as string), { instructions: "compress it", model: "gpt" }); + assert.deepEqual(JSON.parse(parsed.response as string), { error: { message: "too large" } }); + } finally { + restoreDumpEnv(); + } +}); + +test("#1993 summary dump on: non-JSON sides stay raw, oversized side is capped", () => { + process.env.BILI_DUMP_4XX = "1"; + process.env.BILI_DUMP_4XX_MAX_BYTES = "2048"; + try { + const out = dumpSummaryRejection(413, "s1", "req", "x".repeat(5000)); + assert.ok(out); + const text = fs.readFileSync(out!, "utf8"); + assert.match(text, /\[truncated: \d+ more character\(s\)\]/); + assert.ok(text.length < 2048 * 3 + 512, `capped file should be far smaller than raw (${text.length})`); + const parsed = JSON.parse(text) as { request: unknown; response: unknown }; + assert.equal(parsed.request, "req"); + assert.ok(typeof parsed.response === "string" && (parsed.response as string).includes("[truncated:")); + } finally { + restoreDumpEnv(); + } +}); + +test("#1993 summary dump on: empty exchange is skipped", () => { + process.env.BILI_DUMP_4XX = "1"; + const before = summaryErrFiles().length; + try { + assert.equal(dumpSummaryRejection(400, "s1", "", ""), null); + assert.equal(summaryErrFiles().length, before); + } finally { + restoreDumpEnv(); + } +}); diff --git a/tests/issue1993-preflight-400.test.ts b/tests/issue1993-preflight-400.test.ts new file mode 100644 index 0000000000..c4d5398b24 --- /dev/null +++ b/tests/issue1993-preflight-400.test.ts @@ -0,0 +1,324 @@ +import assert from "node:assert/strict"; +import fs from "node:fs"; +import http from "node:http"; +import os from "node:os"; +import path from "node:path"; +import { once } from "node:events"; +import test from "node:test"; + +process.env.NODE_ENV = "test"; +// Fail fast on 4xx retries so rejections surface immediately. +process.env.BILI_REPLAY_RETRY_MAX = "1"; +process.env.BILI_REPLAY_RETRY_BASE_MS = "0"; +// Short dead-end cooldown so the expiry leg stays fast. +process.env.BILI_PREFLIGHT_DEAD_END_COOLDOWN_MS = "400"; + +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"; +import { setLogCapture } from "../src/logger.ts"; +import { rmrf } from "./tmp-rm.ts"; + +// #1993: a preflight summarization call answered HTTP 400 left the session +// wedged and undiagnosable — the response body was never logged or dumped, +// there was no chunk-shrink fallback (unlike the unusable-HTTP-200 path), and +// the turn fail-fasted 502 retryable=false with a dead-end cooldown. These +// tests pin: (a) the rejection body is logged AND surfaced in the client +// message, (b) hard size-plausible 400/413 get the same halving recovery as an +// unusable 200 while transient/auth/routing 4xx keep their old semantics, +// (c) BILI_DUMP_4XX persists the full summary exchange (request + response), +// (d) the dead-end cooldown only arms after the shrink cascade is genuinely +// exhausted with zero progress. + +const SUMMARY_TEXT = + "PREFLIGHT-400 TEST SUMMARY: the segment held a deterministic load-growth payload across several turns; every raw marker is derivable from the seed, so the folded view loses nothing of value."; + +// No transient marker ("rate limit", "try again", ...), no stream/max_output_tokens +// adaptation trigger — a plain hard rejection. +const SYSTEMIC_400_BODY = '{"error":{"message":"request too large: 523200 tokens exceeds the 128000 input limit"}}'; +const FAIL_ABOVE_CHARS = 16_000; + +type Call = { stream: boolean; summary: boolean; contentChars: number }; + +function sse(event: string, data: unknown): string { + return `event: ${event}\ndata: ${JSON.stringify(data)}\n\n`; +} + +function okSummarySse(res: http.ServerResponse): void { + for (const part of [SUMMARY_TEXT.slice(0, 40), SUMMARY_TEXT.slice(40)]) { + res.write(sse("response.output_text.delta", { type: "response.output_text.delta", delta: part })); + } + res.write(sse("response.completed", { type: "response.completed", response: { id: "resp_sum", status: "completed", output: [], usage: { input_tokens: 100, output_tokens: 5 } } })); + res.end(); +} + +function forwardSse(res: http.ServerResponse, inputTokens = 800): void { + res.write(sse("response.completed", { + type: "response.completed", + response: { + id: "resp_fwd", + status: "completed", + output: [{ type: "message", role: "assistant", content: [{ type: "output_text", text: "ok" }] }], + usage: { input_tokens: inputTokens, output_tokens: 4 }, + }, + })); + res.end(); +} + +type ParsedBody = { stream?: boolean; instructions?: unknown; input?: unknown; model?: string }; + +function parseBody(raw: string): ParsedBody { + try { + return JSON.parse(raw) as ParsedBody; + } catch { + return {}; + } +} + +function isSummaryCall(parsed: ParsedBody): boolean { + return typeof parsed.instructions === "string" && Array.isArray(parsed.input) && parsed.input.length === 1; +} + +function inputContentChars(parsed: ParsedBody): number { + const first = Array.isArray(parsed.input) ? parsed.input[0] : undefined; + const c = first && typeof first === "object" ? (first as Record).content : undefined; + return typeof c === "string" ? c.length : 0; +} + +// Summary calls over failAboveChars get `rejectStatus` + rejectBody; everything +// else is summarized normally. failAboveChars=0 makes every summary call reject. +function makeUpstream(calls: Call[], failAboveChars: number, rejectStatus: number, rejectBody: string, forwardInputTokens = 800): http.Server { + return http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (c: Buffer) => chunks.push(c)); + req.on("end", () => { + const parsed = parseBody(Buffer.concat(chunks).toString("utf8")); + const contentChars = isSummaryCall(parsed) ? inputContentChars(parsed) : 0; + calls.push({ stream: parsed.stream === true, summary: isSummaryCall(parsed), contentChars }); + if (isSummaryCall(parsed)) { + if (contentChars > failAboveChars) { + res.writeHead(rejectStatus, { "content-type": "application/json" }); + res.end(rejectBody); + return; + } + res.writeHead(200, { "content-type": "text/event-stream", "cache-control": "no-cache" }); + okSummarySse(res); + return; + } + forwardSse(res, forwardInputTokens); + }); + }); +} + +function longResponsesInput(count: number) { + const input: { type: string; role: string; content: string }[] = []; + for (let i = 0; i < count; i++) { + input.push({ type: "message", role: i % 2 === 0 ? "user" : "assistant", content: `Message ${i} of the long conversation. ` + `MARKER_${i}_content_`.repeat(250) }); + } + return input; +} + +function startProxy(upstreamPort: number, models: Record, log = false): Promise { + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + return startServer({ + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: { [`http://127.0.0.1:${upstreamPort}`]: { models } }, + modelContextLimit: 10_000, + kernelConfig: defaultConfig(10_000), + compress: { injectTool: true, injectNudge: true }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log, + debug: false, + passthrough: false, + autoUpdate: false, + mitm: { enabled: false, domains: [] }, + } as ProxyOptions); +} + +async function driveResponses(proxyPort: number, upstreamPort: number, session: string, model: string, input: unknown): Promise { + return await fetch(`http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstreamPort}/responses`, { + method: "POST", + headers: { "content-type": "application/json", "x-acp-session": session }, + body: JSON.stringify({ model, stream: true, input }), + }); +} + +async function closeAll(...servers: http.Server[]): Promise { + servers.forEach((s) => s.close()); + await new Promise((resolve, reject) => { + void Promise.allSettled(servers.map((s) => once(s, "close"))).then(() => resolve(), reject); + }); +} + +test("#1993 systemic hard 400: body surfaced in log+message, bounded halving cascade, cooldown arms after exhaustion", async () => { + const calls: Call[] = []; + const upstream = makeUpstream(calls, 0, 400, SYSTEMIC_400_BODY); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamPort = (upstream.address() as { port: number }).port; + const proxy = await startProxy(upstreamPort, { "gpt-7-sol": { context: 10_000 } }); + await once(proxy, "listening"); + const proxyPort = (proxy.address() as { port: number }).port; + try { + const r1 = await driveResponses(proxyPort, upstreamPort, "s1993-systemic", "gpt-7-sol", longResponsesInput(12)); + assert.equal(r1.status, 502, `systemic hard 400 must fail-fast, got ${r1.status}`); + const j1 = (await r1.json()) as { error?: { code?: string; message?: string; retryable?: boolean } }; + assert.equal(j1.error?.code, "preflight_compress_failed"); + assert.equal(j1.error?.retryable, false); + assert.ok( + j1.error?.message?.includes("rejected by the upstream (HTTP 400)"), + `fail-fast message must name the rejection, got: ${j1.error?.message}`, + ); + // The core of #1993: WHY the upstream said no must reach the operator. + assert.ok( + j1.error?.message?.includes("request too large: 523200 tokens exceeds the 128000 input limit"), + `fail-fast message must carry the rejection body snippet, got: ${j1.error?.message}`, + ); + const summaries = calls.filter((c) => c.summary); + // More than one call proves the halving cascade ran instead of the old + // instant give-up; the ceiling is the per-preflight budget. + assert.ok(summaries.length >= 2 && summaries.length <= 16, `summary calls must be bounded by the cascade, got ${summaries.length}: ${JSON.stringify(summaries)}`); + assert.ok(!calls.some((c) => !c.summary), "nothing may be forwarded on failure"); + + const sess = listSessions().find((s) => s.id.includes("s1993-systemic")); + const marker = sess?.metadata.preflightDeadEnd as Record | undefined; + assert.ok(marker && typeof marker === "object", "exhausted zero-progress 400 cascade must arm the dead-end marker"); + assert.match(String(marker?.key), /^gpt-7-sol\u000010000\u0000[0-9a-f]{64}$/, "marker scoped to model, window and request body"); + + const callsBeforeRetry = calls.length; + const r2 = await driveResponses(proxyPort, upstreamPort, "s1993-systemic", "gpt-7-sol", longResponsesInput(12)); + assert.equal(r2.status, 502, `cooldown must fail fast, got ${r2.status}`); + const j2 = (await r2.json()) as { error?: { message?: string } }; + assert.equal(j2.error?.message, j1.error?.message, "cooldown replays the cached diagnosis"); + assert.equal(calls.length, callsBeforeRetry, "cooldown must spend ZERO upstream calls"); + } finally { + await closeAll(proxy, upstream); + } +}); + +test("#1993 size-driven 400: oversized chunk rejected, halved chunk recovers, session continues", async () => { + const calls: Call[] = []; + const upstream = makeUpstream(calls, FAIL_ABOVE_CHARS, 400, SYSTEMIC_400_BODY); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamPort = (upstream.address() as { port: number }).port; + const proxy = await startProxy(upstreamPort, { "gpt-7-sol": { context: 10_000 } }); + await once(proxy, "listening"); + const proxyPort = (proxy.address() as { port: number }).port; + try { + const r = await driveResponses(proxyPort, upstreamPort, "s1993-recover", "gpt-7-sol", longResponsesInput(12)); + assert.equal(r.status, 200, `request must recover via smaller chunks, got ${r.status}`); + await r.text(); + + const summaries = calls.filter((c) => c.summary); + const failed = summaries.filter((c) => c.contentChars > FAIL_ABOVE_CHARS); + const succeeded = summaries.filter((c) => c.contentChars <= FAIL_ABOVE_CHARS); + assert.ok(failed.length >= 1, `expected at least one oversized rejected summary, got ${JSON.stringify(summaries)}`); + assert.ok(succeeded.length >= 1, `expected recovered smaller summaries, got ${JSON.stringify(summaries)}`); + assert.ok( + summaries.indexOf(failed[0]!) < summaries.indexOf(succeeded[0]!), + `halved retry must follow the rejected chunk, got ${JSON.stringify(summaries)}`, + ); + assert.ok(calls.some((c) => !c.summary), "the folded payload was forwarded"); + + const sess = listSessions().find((s) => s.id.includes("s1993-recover")); + assert.ok(sess, "session recorded"); + assert.equal(sess?.metadata?.preflightDeadEnd, undefined, "recovered preflight must not leave a dead-end marker"); + } finally { + await closeAll(proxy, upstream); + } +}); + +test("#1993 non-size 4xx does not shrink: 401 fails fast after exactly one summary call", async () => { + const calls: Call[] = []; + const upstream = makeUpstream(calls, 0, 401, '{"error":"unauthorized"}'); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamPort = (upstream.address() as { port: number }).port; + const proxy = await startProxy(upstreamPort, { "gpt-7-sol": { context: 10_000 } }); + await once(proxy, "listening"); + const proxyPort = (proxy.address() as { port: number }).port; + try { + const r = await driveResponses(proxyPort, upstreamPort, "s1993-auth", "gpt-7-sol", longResponsesInput(12)); + assert.equal(r.status, 502, `auth rejection must fail-fast, got ${r.status}`); + const j = (await r.json()) as { error?: { message?: string; retryable?: boolean } }; + assert.equal(j.error?.retryable, false); + assert.ok(j.error?.message?.includes("rejected by the upstream (HTTP 401)"), `got: ${j.error?.message}`); + const summaries = calls.filter((c) => c.summary); + assert.equal(summaries.length, 1, `401 must not burn the budget on halving, got ${summaries.length} calls`); + const sess = listSessions().find((s) => s.id.includes("s1993-auth")); + assert.ok(sess?.metadata?.preflightDeadEnd, "zero-progress auth rejection must arm the dead-end marker"); + } finally { + await closeAll(proxy, upstream); + } +}); + +test("#1993 warn log carries the rejection body snippet and the halving decision", async () => { + const lines: string[] = []; + setLogCapture((_level, msg) => { lines.push(msg); }); + const calls: Call[] = []; + const upstream = makeUpstream(calls, 0, 400, SYSTEMIC_400_BODY); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamPort = (upstream.address() as { port: number }).port; + const proxy = await startProxy(upstreamPort, { "gpt-7-sol": { context: 10_000 } }, true); + await once(proxy, "listening"); + const proxyPort = (proxy.address() as { port: number }).port; + try { + const r = await driveResponses(proxyPort, upstreamPort, "s1993-log", "gpt-7-sol", longResponsesInput(12)); + assert.equal(r.status, 502); + await r.text(); + assert.ok( + lines.some((l) => l.includes("[preflight] summarization failed: HTTP 400") && l.includes("request too large: 523200")), + `warn log must carry the rejection body snippet, got: ${lines.filter((l) => l.includes("summarization failed")).join(" | ")}`, + ); + assert.ok( + lines.some((l) => /\[preflight\] chunk m\d+:m\d+ rejected with HTTP 400; retrying with smaller chunks/.test(l)), + `warn log must record the halving decision`, + ); + } finally { + setLogCapture(null); + await closeAll(proxy, upstream); + } +}); + +test("#1993 BILI_DUMP_4XX persists the rejected summary exchange (request + response)", async () => { + const savedGate = process.env.BILI_DUMP_4XX; + const savedDir = process.env.ACP_DUMP_DIR; + const dumpDir = fs.mkdtempSync(path.join(os.tmpdir(), "bili-summary-dump-")); + process.env.BILI_DUMP_4XX = "1"; + process.env.ACP_DUMP_DIR = dumpDir; + const calls: Call[] = []; + const upstream = makeUpstream(calls, 0, 400, SYSTEMIC_400_BODY); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const upstreamPort = (upstream.address() as { port: number }).port; + const proxy = await startProxy(upstreamPort, { "gpt-7-sol": { context: 10_000 } }); + await once(proxy, "listening"); + const proxyPort = (proxy.address() as { port: number }).port; + try { + const r = await driveResponses(proxyPort, upstreamPort, "s1993-dump", "gpt-7-sol", longResponsesInput(12)); + assert.equal(r.status, 502); + await r.text(); + + const files = fs.readdirSync(dumpDir).filter((f) => f.startsWith("summary-err-") && f.endsWith("-400.json")); + assert.ok(files.length >= 1, `expected at least one summary-err dump, dir has: ${JSON.stringify(fs.readdirSync(dumpDir))}`); + const parsed = JSON.parse(fs.readFileSync(path.join(dumpDir, files[0]!), "utf8")) as { status: number; request: unknown; response: unknown }; + assert.equal(parsed.status, 400); + assert.ok(typeof parsed.request === "string" && parsed.request.includes("instructions"), "dump must carry the request bytes"); + assert.ok(typeof parsed.response === "string" && parsed.response.includes("request too large: 523200"), "dump must carry the response bytes"); + } finally { + if (savedGate === undefined) delete process.env.BILI_DUMP_4XX; + else process.env.BILI_DUMP_4XX = savedGate; + if (savedDir === undefined) delete process.env.ACP_DUMP_DIR; + else process.env.ACP_DUMP_DIR = savedDir; + await closeAll(proxy, upstream); + rmrf(dumpDir); + } +});