Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/error-dump.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
43 changes: 39 additions & 4 deletions src/preflight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -762,7 +763,12 @@ async function requestSummaryBody(deps: PreflightDeps, body: string): Promise<st
const retryable = failure instanceof UpstreamHttpError
? isTransientUpstreamError(failure.status, failure.body)
: failure.retryable;
if (!retryable || attempt >= 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?.();
Expand Down Expand Up @@ -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`);
Expand Down Expand Up @@ -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
Expand Down
66 changes: 65 additions & 1 deletion tests/error-dump.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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", "<html>req</html>", "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, "<html>req</html>");
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();
}
});
Loading
Loading