diff --git a/src/external-summary-runtime.ts b/src/external-summary-runtime.ts index 80717a43d8..d0b8ec00db 100644 --- a/src/external-summary-runtime.ts +++ b/src/external-summary-runtime.ts @@ -43,7 +43,12 @@ export class ConfiguredSummaryPlan { const headers: Record = target.protocol === "anthropic" ? { "x-api-key": key } : target.protocol === "google" ? { "x-goog-api-key": key } : { authorization: `Bearer ${key}` }; - return createSummaryHttpCandidate({ ...target, headers, proxyUrl, ...(this.sessionId !== undefined ? { sessionId: this.sessionId } : {}) }, this.settings.budget.maxSummaryBytes * 4 + 65536); + // SSE repeats framing/model metadata for every token; those bytes + // are not summary content. Keep a bounded transport allowance while + // the executor independently enforces maxSummaryBytes on the text. + const jsonResponseBytes = this.settings.budget.maxSummaryBytes * 4 + 65536; + const responseBytes = target.stream ? Math.max(8 * 1024 * 1024, jsonResponseBytes) : jsonResponseBytes; + return createSummaryHttpCandidate({ ...target, headers, proxyUrl, ...(this.sessionId !== undefined ? { sessionId: this.sessionId } : {}) }, responseBytes); } catch (error) { // Log WHY this target is unusable (once per signature) so a // misconfigured chain is diagnosable instead of silently degrading diff --git a/tests/external-summary-runtime.test.ts b/tests/external-summary-runtime.test.ts index 1a2f302dec..fee423ea6d 100644 --- a/tests/external-summary-runtime.test.ts +++ b/tests/external-summary-runtime.test.ts @@ -127,6 +127,61 @@ test("all candidates failing preserves state and refuses without borrowing main }, (_req, res) => { calls++; res.writeHead(503); res.end("private-provider-error"); }); }); +test("SSE framing overhead does not consume the summary content budget", async () => { + const text = "s".repeat(2000); + await fixture(async (_base, externalSummary) => { + const targets = externalSummary.targets as Record[]; + const plan = configuredSummaryPlan({ ...externalSummary, + targets: [{ ...targets[0], protocol: "openai", stream: true }], + budget: { totalTimeoutMs: 5000, targetTimeoutMs: 4000, maxSummaryBytes: 2048 }, + }); + assert.ok(plan); + const batch = await plan.summarize([{ content: "synthetic source", instructions: "retain the fixture" }]); + assert.equal(batch.results[0]?.status, "success", JSON.stringify(batch)); + const result = batch.results[0]; + if (result?.status === "success") assert.equal(result.summary, text); + }, (_req, res) => { + res.setHeader("content-type", "text/event-stream"); + for (const char of text) res.write(`data: ${JSON.stringify({ id: "synthetic", padding: "x".repeat(256), choices: [{ delta: { content: char }, finish_reason: null }] })}\n\n`); + res.end(`data: ${JSON.stringify({ choices: [{ delta: {}, finish_reason: "stop" }] })}\n\ndata: [DONE]\n\n`); + }); +}); + +test("SSE transport allowance remains bounded independently of summary content", async () => { + await fixture(async (_base, externalSummary) => { + const targets = externalSummary.targets as Record[]; + const plan = configuredSummaryPlan({ ...externalSummary, + targets: [{ ...targets[0], protocol: "openai", stream: true }], + budget: { totalTimeoutMs: 5000, targetTimeoutMs: 4000, maxSummaryBytes: 2048 }, + }); + assert.ok(plan); + const batch = await plan.summarize([{ content: "synthetic source", instructions: "retain the fixture" }]); + assert.equal(batch.results[0]?.status, "failed"); + }, (_req, res) => { + res.setHeader("content-type", "text/event-stream"); + // Valid SSE with a legal small summary: without the transport byte + // guard this succeeds, so parser rejection cannot satisfy the test. + res.end(`: ${"x".repeat(8 * 1024 * 1024)}\n\ndata: ${JSON.stringify({ choices: [{ delta: { content: SUMMARY }, finish_reason: "stop" }] })}\n\ndata: [DONE]\n\n`); + }); +}); + +test("SSE transport allowance does not relax the summary content byte limit", async () => { + await fixture(async (_base, externalSummary) => { + const targets = externalSummary.targets as Record[]; + const plan = configuredSummaryPlan({ ...externalSummary, + targets: [{ ...targets[0], protocol: "openai", stream: true }], + budget: { totalTimeoutMs: 5000, targetTimeoutMs: 4000, maxSummaryBytes: 2048 }, + }); + assert.ok(plan); + const batch = await plan.summarize([{ content: "synthetic source", instructions: "retain the fixture" }]); + assert.equal(batch.results[0]?.status, "failed"); + assert.equal(batch.results[0]?.attempts[0]?.outcome, "invalid_summary"); + }, (_req, res) => { + res.setHeader("content-type", "text/event-stream"); + res.end(`data: ${JSON.stringify({ choices: [{ delta: { content: "s".repeat(2049) }, finish_reason: "stop" }] })}\n\ndata: [DONE]\n\n`); + }); +}); + test("disabled setting preserves the legacy supplied-summary path without HTTP calls", async () => { let calls = 0; await fixture(async (_base, _externalSummary) => {