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
17 changes: 14 additions & 3 deletions tests/wire-exit-gap-cells.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ function bigConversation(): Array<{ role: string; content: string }> {
type TimedResponse = {
status: number;
headers: http.IncomingHttpHeaders;
/** epoch ms when the request was created */
t0: number;
tHeaderMs: number;
body: string;
};
Expand All @@ -150,6 +152,7 @@ function timedPost(url: string, headers: Record<string, string>, body: string):
resolve({
status: res.statusCode ?? 0,
headers: res.headers,
t0,
tHeaderMs,
body: Buffer.concat(chunks).toString("utf8"),
});
Expand All @@ -164,8 +167,9 @@ function timedPost(url: string, headers: Record<string, string>, body: string):
* first — the 32768 cap no longer identifies them since #987 clamps it to
* the window headroom) is slow; the forward call gets `forwardStatus`
* immediately. */
function startUpstream(forwardStatus: number, forwardBody: string, summaryFails: boolean): Promise<{ server: http.Server; port: number; forwards: () => number }> {
function startUpstream(forwardStatus: number, forwardBody: string, summaryFails: boolean): Promise<{ server: http.Server; port: number; forwards: () => number; summaryDoneMs: () => number }> {
let forwards = 0;
let summaryDoneMs = 0;
const server = http.createServer((req, res) => {
const chunks: Buffer[] = [];
req.on("data", (c: Buffer) => chunks.push(c));
Expand All @@ -176,6 +180,7 @@ function startUpstream(forwardStatus: number, forwardBody: string, summaryFails:
const msgs = parsed.messages;
if (Array.isArray(msgs) && msgs.length === 2 && msgs[0]?.role === "system") {
setTimeout(() => {
summaryDoneMs = Date.now();
if (summaryFails) {
res.writeHead(429, { "content-type": "application/json" });
res.end(JSON.stringify({ error: { message: "rate limited", type: "rate_limit_error" } }));
Expand All @@ -193,7 +198,7 @@ function startUpstream(forwardStatus: number, forwardBody: string, summaryFails:
});
return new Promise((resolve) => {
server.listen(0, "127.0.0.1", () => {
resolve({ server, port: (server.address() as { port: number }).port, forwards: () => forwards });
resolve({ server, port: (server.address() as { port: number }).port, forwards: () => forwards, summaryDoneMs: () => summaryDoneMs });
});
});
}
Expand Down Expand Up @@ -239,7 +244,13 @@ test("gap 3: non-stream + preflight 429 after early commit → structured JSON e
);
assert.equal(r.status, 200, "status was committed early and cannot become 503 anymore");
assert.equal(r.headers["x-bili-preflight"], "compressing");
assert.ok(r.tHeaderMs < SLOW_MS, `headers arrived before summarization finished (${r.tHeaderMs}ms)`);
// #3228: RELATIVE invariant (headers before the mock's actual summarization
// completion), not an absolute wall-clock bound — a loaded runner stalls both
// timers equally and eats the fixed margin while behavior stays correct (same
// fix as #3152-C in preflight-hold). Early-commit regression lands headers at
// response end, i.e. AFTER summarization finished, so order still catches it.
assert.ok(up.summaryDoneMs() > 0, "the slow summarization call completed");
assert.ok(r.tHeaderMs < up.summaryDoneMs() - r.t0, `headers arrived before summarization finished (${r.tHeaderMs}ms vs ${up.summaryDoneMs() - r.t0}ms)`);
assert.ok(r.body.startsWith(" "), "whitespace keep-alive precedes the JSON body");
const json = JSON.parse(r.body) as { error?: { code?: string; message?: string; retryable?: boolean } };
assert.equal(json.error?.code, "preflight_compress_failed", "structured error body under the committed 200");
Expand Down
29 changes: 21 additions & 8 deletions tests/wire-exit-matrix.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,10 @@ function bigResponsesInput(): Array<{ type: string; role: string; content: strin

type UpstreamMode = "hang-openai" | "hang-anthropic" | "hang-responses" | "hang-json" | "slow-summary-fwd-429" | "slow-summary-429";

function startUpstream(mode: UpstreamMode): Promise<{ server: http.Server; port: number; socketsClosed: () => number; requests: () => number }> {
function startUpstream(mode: UpstreamMode): Promise<{ server: http.Server; port: number; socketsClosed: () => number; requests: () => number; summaryDoneMs: () => number }> {
let closed = 0;
let requests = 0;
let summaryDoneMs = 0;
const server = http.createServer((req, res) => {
const chunks: Buffer[] = [];
req.on("data", (c: Buffer) => chunks.push(c));
Expand All @@ -87,13 +88,15 @@ function startUpstream(mode: UpstreamMode): Promise<{ server: http.Server; port:
// Fail the summarization itself AFTER the hold grace so the
// early 200 is already committed (→ emitPreflightError path).
setTimeout(() => {
summaryDoneMs = Date.now();
res.writeHead(429, { "content-type": "application/json" });
res.end(JSON.stringify({ error: { message: "rate limited", type: "rate_limit_error" } }));
}, SLOW_MS);
return;
}
if (isSummary) {
setTimeout(() => {
summaryDoneMs = Date.now();
res.writeHead(200, { "content-type": "application/json" });
res.end(JSON.stringify({ id: "sum", object: "chat.completion", choices: [{ index: 0, message: { role: "assistant", content: SUMMARY_TEXT }, finish_reason: "stop" }], usage: { prompt_tokens: 500, completion_tokens: 50, total_tokens: 550 } }));
}, SLOW_MS);
Expand Down Expand Up @@ -123,7 +126,7 @@ function startUpstream(mode: UpstreamMode): Promise<{ server: http.Server; port:
});
return new Promise((resolve) => {
server.listen(0, "127.0.0.1", () => {
resolve({ server, port: (server.address() as { port: number }).port, socketsClosed: () => closed, requests: () => requests });
resolve({ server, port: (server.address() as { port: number }).port, socketsClosed: () => closed, requests: () => requests, summaryDoneMs: () => summaryDoneMs });
});
});
}
Expand Down Expand Up @@ -292,20 +295,26 @@ test("error-delivery x proxy-openai-sse: upstream 429 AFTER the early 200 commit
// slow (outlives the 300ms hold grace) and the FORWARD after it hits
// 429 — both after the early 200 commit, so the error must arrive
// in-band on the openai wire.
const r = await new Promise<{ status: number; headers: http.IncomingHttpHeaders; tHeaderMs: number; body: string }>((resolve, reject) => {
const r = await new Promise<{ status: number; headers: http.IncomingHttpHeaders; t0: number; tHeaderMs: number; body: string }>((resolve, reject) => {
const t0 = Date.now();
const req = http.request(`http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${up.port}/v1/chat/completions`, { method: "POST", headers: { "content-type": "application/json", "x-acp-session": "mx-inband-oai" } }, (res) => {
const tHeaderMs = Date.now() - t0;
const chunks: Buffer[] = [];
res.on("data", (c: Buffer) => chunks.push(c));
res.on("end", () => resolve({ status: res.statusCode ?? 0, headers: res.headers, tHeaderMs, body: Buffer.concat(chunks).toString("utf8") }));
res.on("end", () => resolve({ status: res.statusCode ?? 0, headers: res.headers, t0, tHeaderMs, body: Buffer.concat(chunks).toString("utf8") }));
});
req.on("error", reject);
req.end(JSON.stringify({ model: "m-test", max_tokens: 1024, stream: true, messages: bigConversation() }));
});
assert.equal(r.status, 200, "early commit owns the status line");
assert.equal(String(r.headers["content-type"]), "text/event-stream");
assert.ok(r.tHeaderMs < SLOW_MS, `headers committed before the slow preflight finished (${r.tHeaderMs}ms)`);
// #3228: assert the RELATIVE invariant (headers before the mock's actual
// summarization completion), not an absolute wall-clock bound — a loaded
// runner stalls both timers equally and eats the fixed margin while the
// behavior stays correct (same fix as #3152-C in preflight-hold). If early
// commit regresses, headers land at stream end, i.e. AFTER summarization.
assert.ok(up.summaryDoneMs() > 0, "the slow summarization call completed");
assert.ok(r.tHeaderMs < up.summaryDoneMs() - r.t0, `headers committed before summarization finished (${r.tHeaderMs}ms vs ${up.summaryDoneMs() - r.t0}ms)`);
assert.ok(r.body.includes(": bili-preflight"), "keep-alive comment present");
// Post-commit upstream failure goes through emitStreamError: openai
// shape = top-level error frame + [DONE]. #1455: the legacy
Expand All @@ -332,20 +341,24 @@ test("error-delivery x proxy-openai-sse: preflight failure after the early commi
await once(proxy, "listening");
const proxyPort = (proxy.address() as { port: number }).port;
try {
const r = await new Promise<{ status: number; headers: http.IncomingHttpHeaders; tHeaderMs: number; body: string }>((resolve, reject) => {
const r = await new Promise<{ status: number; headers: http.IncomingHttpHeaders; t0: number; tHeaderMs: number; body: string }>((resolve, reject) => {
const t0 = Date.now();
const req = http.request(`http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${up.port}/v1/chat/completions`, { method: "POST", headers: { "content-type": "application/json", "x-acp-session": "mx-inband-oai2" } }, (res) => {
const tHeaderMs = Date.now() - t0;
const chunks: Buffer[] = [];
res.on("data", (c: Buffer) => chunks.push(c));
res.on("end", () => resolve({ status: res.statusCode ?? 0, headers: res.headers, tHeaderMs, body: Buffer.concat(chunks).toString("utf8") }));
res.on("end", () => resolve({ status: res.statusCode ?? 0, headers: res.headers, t0, tHeaderMs, body: Buffer.concat(chunks).toString("utf8") }));
});
req.on("error", reject);
req.end(JSON.stringify({ model: "m-test", max_tokens: 1024, stream: true, messages: bigConversation() }));
});
assert.equal(r.status, 200, "early commit owns the status line");
assert.equal(String(r.headers["content-type"]), "text/event-stream");
assert.ok(r.tHeaderMs < SLOW_MS, `headers committed before the slow summarization failed (${r.tHeaderMs}ms)`);
// #3228: RELATIVE invariant — see the sibling test above (#3152-C pattern).
// Early-commit regression lands headers at stream end, i.e. AFTER the
// slow summarization failed, so the order still catches it.
assert.ok(up.summaryDoneMs() > 0, "the slow summarization call completed");
assert.ok(r.tHeaderMs < up.summaryDoneMs() - r.t0, `headers committed before the slow summarization failed (${r.tHeaderMs}ms vs ${up.summaryDoneMs() - r.t0}ms)`);
assert.ok(r.body.includes(": bili-preflight"), "keep-alive comment present");
// Post-commit PREFLIGHT failure goes through emitPreflightError: openai
// shape = top-level {error} object + [DONE] (no choices framing).
Expand Down
Loading