Skip to content
Closed
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
7 changes: 6 additions & 1 deletion src/external-summary-runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ export class ConfiguredSummaryPlan {
const headers: Record<string, string> = 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
Expand Down
55 changes: 55 additions & 0 deletions tests/external-summary-runtime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown>[];
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<string, unknown>[];
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<string, unknown>[];
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) => {
Expand Down
Loading