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
86 changes: 71 additions & 15 deletions src/preflight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,27 +303,63 @@ function summaryHeaders(deps: PreflightDeps): Record<string, string> {
return headers;
}

// Extract the summary text from a buffered SSE body (the streaming twin of
// extractSummaryText). For Responses, prefer the response.completed event's
// full response object (reuses the JSON extractor); otherwise accumulate
// output_text deltas. Non-conforming upstreams that return plain JSON despite
// stream:true are handled by the caller's JSON fallback.
function extractSummaryFromSse(protocol: PreflightProtocol, text: string): string {
// #780: extraction carries a validity contract — it must separate "the stream
// delivered a complete summary" from "the stream died mid-delivery". The naive
// accumulator conflated the two: a gateway truncation (#764: half-line data,
// no [DONE]) left a partial `out` that was persisted as a complete tier-1
// summary — silently worse than an empty one, because #727's diagnosis chain
// only fires on empty results. Rejection rules:
// - a data line that fails to parse is corruption (badFrame), not noise to skip
// - failure terminals (response.incomplete/.failed/.error, generic error /
// bare {error}) invalidate any text accumulated before them
// - responses requires the spec-mandatory response.completed terminal; its
// response object reuses the JSON extractor and is authoritative — once
// seen it is trusted as-is (no framing check on top, so gateways that close
// right after the final event without a trailing blank line are safe)
// - anthropic/openai do NOT require finish_reason/[DONE]/message_stop (#764:
// real gateways omit these occasionally); the body must at least end on a
// frame boundary (\n\n, CRLF-tolerant), else it may have been cut mid-frame
// A rejected stream returns "" so requestSummary routes it into
// diagnoseEmptySummary + the #726 halving/cooldown chain.
export function extractSummaryFromSse(protocol: PreflightProtocol, text: string): string {
const framed = /\r?\n\r?\n$/.test(text);
let out = "";
let terminalText = "";
let completed = false;
let invalid = false;
let badFrame = false;
let eventType = "";
for (const line of text.split("\n")) {
if (line.trim() === "") {
eventType = "";
continue;
}
if (line.startsWith("event:")) {
eventType = line.slice(6).trim();
continue;
}
if (!line.startsWith("data:")) continue;
const payload = line.slice(5).trim();
if (!payload || payload === "[DONE]") continue;
let obj: unknown;
try {
obj = JSON.parse(payload);
} catch {
badFrame = true;
continue;
}
if (!obj || typeof obj !== "object") {
badFrame = true;
continue;
}
if (!obj || typeof obj !== "object") continue;
const o = obj as Record<string, unknown>;
const type = typeof o.type === "string" ? o.type : eventType;
if (type === "error" || type === "response.incomplete" || type === "response.failed" || type === "response.error" || (!type && o.error && typeof o.error === "object")) {
invalid = true;
continue;
}
if (protocol === "anthropic") {
if (o.type === "content_block_delta") {
if (type === "content_block_delta") {
const d = o.delta as Record<string, unknown> | undefined;
if (d && d.type === "text_delta" && typeof d.text === "string") out += d.text;
}
Expand All @@ -334,15 +370,23 @@ function extractSummaryFromSse(protocol: PreflightProtocol, text: string): strin
if (delta && typeof delta.content === "string") out += delta.content;
}
} else {
if (o.type === "response.output_text.delta" && typeof o.delta === "string") {
if (type === "response.output_text.delta" && typeof o.delta === "string") {
out += o.delta;
} else if (o.type === "response.completed" && o.response && typeof o.response === "object") {
const full = extractSummaryText(protocol, o.response as Record<string, unknown>);
if (full) return full;
} else if (type === "response.output_text.done" && typeof o.text === "string") {
terminalText += o.text;
} else if (type === "response.output_item.done" && o.item && typeof o.item === "object") {
terminalText += extractSummaryText("responses", { output: [o.item] });
} else if (type === "response.completed" && o.response && typeof o.response === "object") {
completed = true;
const full = extractSummaryText("responses", o.response as Record<string, unknown>);
if (full) terminalText = full;
}
}
}
return out;
if (invalid) return "";
if (protocol === "responses") return completed ? terminalText || out : "";
if (badFrame || !framed) return "";
return out || terminalText;
}

function extractSummaryText(protocol: PreflightProtocol, json: Record<string, unknown>): string {
Expand Down Expand Up @@ -424,6 +468,7 @@ export function diagnoseEmptySummary(text: string, json?: unknown): string {
if (err) return err;
}
let sseEvents = 0;
let halfLines = 0;
let firstPayload = "";
for (const line of text.split("\n")) {
if (!line.startsWith("data:")) continue;
Expand All @@ -434,14 +479,25 @@ export function diagnoseEmptySummary(text: string, json?: unknown): string {
try {
obj = JSON.parse(payload);
} catch {
halfLines += 1;
continue;
}
if (!obj || typeof obj !== "object") {
halfLines += 1;
continue;
}
if (!obj || typeof obj !== "object") continue;
sseEvents += 1;
const err = extractStreamError(obj as Record<string, unknown>);
if (err) return err;
}
if (sseEvents > 0) return `the upstream stream carried ${sseEvents} SSE event(s) but no summary text (first event: ${firstPayload})`;
// #780: the extractor rejects mid-frame-truncated streams (#764 shape) — say so
// explicitly instead of the generic no-text message, which reads like an
// upstream that simply never answered with a summary.
if (halfLines > 0 && sseEvents === 0) return `the upstream stream had ${halfLines} incomplete data line(s) and no parseable events (stream appears truncated)`;
if (sseEvents > 0) {
const truncated = halfLines > 0 || !/\r?\n\r?\n$/.test(text) ? ` (stream appears truncated: ${halfLines > 0 ? `${halfLines} incomplete data line(s)` : "no final frame terminator"})` : "";
return `the upstream stream carried ${sseEvents} SSE event(s) but no summary text${truncated} (first event: ${firstPayload})`;
}
const trimmed = text.trim();
if (!trimmed) return "the upstream returned an empty body";
return `the upstream returned a non-SSE body with no summary text (first 200 bytes: ${trimmed.slice(0, 200)})`;
Expand Down
57 changes: 56 additions & 1 deletion tests/preflight-empty-summary.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ function okSummarySse(res: http.ServerResponse): void {
res.end();
}

// #780: two complete delta frames, then a half-line cut mid-JSON — no
// completed terminal, no final frame terminator (#764 gateway-corruption shape).
// The extractor must reject this as unusable rather than persist the partial text.
function truncatedSummarySse(res: http.ServerResponse): void {
res.write(sse("response.output_text.delta", { type: "response.output_text.delta", delta: SUMMARY_TEXT.slice(0, 40) }));
res.write(sse("response.output_text.delta", { type: "response.output_text.delta", delta: SUMMARY_TEXT.slice(40, 80) }));
res.write(`data: ${JSON.stringify({ type: "response.output_text.delta", delta: "tail" }).slice(0, 30)}\n`);
res.end();
}

function forwardSse(res: http.ServerResponse, inputTokens = 800): void {
res.write(sse("response.completed", {
type: "response.completed",
Expand Down Expand Up @@ -84,7 +94,7 @@ function inputContentChars(parsed: ParsedBody): number {
return typeof c === "string" ? c.length : 0;
}

function makeUpstream(calls: Call[], failAboveChars: number, forwardInputTokens = 800): http.Server {
function makeUpstream(calls: Call[], failAboveChars: number, forwardInputTokens = 800, truncateSummaries = false): http.Server {
return http.createServer((req, res) => {
const chunks: Buffer[] = [];
req.on("data", (c: Buffer) => chunks.push(c));
Expand All @@ -98,6 +108,11 @@ function makeUpstream(calls: Call[], failAboveChars: number, forwardInputTokens
res.end(JSON.stringify({ detail: "Stream must be set to true" }));
return;
}
if (truncateSummaries && isSummaryCall(parsed)) {
res.writeHead(200, { "content-type": "text/event-stream", "cache-control": "no-cache" });
truncatedSummarySse(res);
return;
}
if (failed) {
res.writeHead(200, { "content-type": "text/event-stream", "cache-control": "no-cache" });
failedSummarySse(res);
Expand Down Expand Up @@ -312,3 +327,43 @@ test("#726 diagnoseEmptySummary: extracts terminal error signals from 200 bodies
/non-SSE body with no summary text/,
);
});

test("#780 truncated summary stream is unusable: diagnosis names truncation, bounded calls, cooldown arms, nothing forwarded", async () => {
const calls: Call[] = [];
const upstream = makeUpstream(calls, 0, 800, true);
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, "s780-trunc", "gpt-7-sol", longResponsesInput(12));
assert.equal(r.status, 502, `truncated summaries must fail-fast, got ${r.status}`);
const j = (await r.json()) as { error?: { code?: string; message?: string } };
assert.equal(j.error?.code, "preflight_compress_failed");
assert.ok(
j.error?.message?.includes("stream appears truncated"),
`fail-fast message must carry the truncation diagnosis, got: ${j.error?.message}`,
);
const summaries = calls.filter((c) => c.summary);
assert.ok(summaries.length >= 2 && summaries.length <= 9, `summary calls must be bounded, 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("s780-trunc"));
assert.ok(sess?.metadata?.preflightDeadEnd, "zero-progress failure must arm the dead-end marker");

const callsBeforeRetry = calls.length;
const r2 = await driveResponses(proxyPort, upstreamPort, "s780-trunc", "gpt-7-sol", longResponsesInput(12));
assert.equal(r2.status, 502, `cooldown must fail fast, got ${r2.status}`);
assert.equal(calls.length, callsBeforeRetry, "cooldown must spend ZERO upstream calls");
} finally {
proxy.close();
upstream.close();
await new Promise<void>((resolve, reject) => {
void Promise.allSettled([once(proxy, "close"), once(upstream, "close")]).then(() => resolve(), reject);
});
}
});
Loading
Loading