Compare commits

..

1 Commits

Author SHA1 Message Date
diegosouzapw
3e9db7a64a fix(sse): cap HuggingChat NDJSON body size and bound the read loop (#12577)
open-sse/executors/huggingchat/jsonlStream.ts accumulated the entire upstream
NDJSON body into buffer/fullText with no byte ceiling, and the only exit
condition was a signal.aborted check polled once per loop iteration — so an
already in-flight reader.read() never noticed an abort until the next chunk
arrived. A stalled or hostile HuggingChat backend that never emits a terminal
finalAnswer/status:finished marker could buffer indefinitely and exhaust the
heap.

Fix:
- Track accumulated bytes in both streamJsonlToOpenAi() and
  readJsonlResponse() and cancel the reader once HUGGINGCHAT_MAX_BODY_BYTES
  (4 MiB) is exceeded, mirroring the readCappedBuffer/readBodyCapped pattern
  already used by veoaifree-web.ts and context7-fetch.ts.
- streamJsonlToOpenAi() yields a sanitized upstream_error SSE chunk + [DONE]
  instead of throwing mid-stream (the response is already committed as 200);
  readJsonlResponse() throws HuggingChatStreamError, which huggingchat.ts's
  existing catch already turns into a 502 buildErrorBody() response.
- Bind reader cancellation to the plain signal (not just the cancellation
  signal) in both functions, so an in-flight read unblocks the instant a
  caller-supplied signal aborts instead of only being noticed on the next
  loop iteration.
- huggingchat.ts now passes combinedSignal (signal + AbortSignal.timeout
  (FETCH_TIMEOUT_MS)) into both call sites instead of the bare signal, so the
  existing fetch timeout actually bounds the body-read phase too.
2026-09-10 15:56:46 -03:00
8 changed files with 184 additions and 49 deletions

View File

@@ -1 +0,0 @@
- fix(providers): route opencode-go/gpt-5.6-luna to /responses instead of /chat/completions (#12196)

View File

@@ -0,0 +1 @@
- fix(sse): cap HuggingChat NDJSON body size and bound the read loop with the fetch timeout so a stalled or hostile upstream cannot buffer unbounded memory (#12577)

View File

@@ -55,6 +55,14 @@ export const SSE_HEARTBEAT_INTERVAL_MS = upstreamTimeouts.sseHeartbeatIntervalMs
// Defaults to FETCH_TIMEOUT_MS. Override with FETCH_BODY_TIMEOUT_MS env var.
export const FETCH_BODY_TIMEOUT_MS = upstreamTimeouts.fetchBodyTimeoutMs;
// Hard byte cap on the HuggingChat NDJSON body accumulated by
// open-sse/executors/huggingchat/jsonlStream.ts. Prevents a stalled/hostile upstream that
// never emits a terminal `finalAnswer` / `status: finished` marker from buffering
// indefinitely (#12577). Sized generously for legitimate long completions while staying
// well below a heap-exhausting size — mirrors the readCappedBuffer/readBodyCapped pattern
// already used by veoaifree-web.ts and context7-fetch.ts.
export const HUGGINGCHAT_MAX_BODY_BYTES = 4 * 1024 * 1024;
// Provider configurations
// OAuth credentials read from env vars with hardcoded fallbacks for backward compatibility.
// Use provider-credentials.json or env vars to override in production.

View File

@@ -250,16 +250,6 @@ export const opencode_goProvider: RegistryEntry = {
supportedThinkingEfforts: ["none", "low", "high", "max"],
targetFormat: "openai-responses",
},
// #12196: the Go upstream serves this model only on /responses —
// /chat/completions 500s for it. github already declares the same model
// id with targetFormat:"openai-responses" (see github/index.ts).
{
id: "gpt-5.6-luna",
name: "GPT-5.6 Luna",
supportsReasoning: true,
targetFormat: "openai-responses",
maxOutputTokens: 128000,
},
// Console Go free GLM-tier model (live-verified 2026-08-23): the upstream
// rejects every reasoning_effort outside {low, high, max} whenever tools
// are present — "[1210] This model always engages in thinking and cannot

View File

@@ -538,7 +538,7 @@ export class HuggingChatExecutor extends BaseExecutor {
resolvedModel,
id,
created,
signal,
combinedSignal,
streamCancellationController.signal
);
@@ -626,7 +626,7 @@ export class HuggingChatExecutor extends BaseExecutor {
let fullText: string;
try {
fullText = await readJsonlResponse(upstreamResponse.body, signal);
fullText = await readJsonlResponse(upstreamResponse.body, combinedSignal);
} catch (err) {
if (!(err instanceof HuggingChatStreamError)) throw err;
const message = err instanceof Error ? err.message : String(err);

View File

@@ -1,5 +1,10 @@
// Pure JSONL stream translation (HuggingChat NDJSON -> OpenAI SSE). Verbatim from huggingchat.ts.
import { HUGGINGCHAT_MAX_BODY_BYTES } from "../../config/constants.ts";
const MAX_BODY_EXCEEDED_MESSAGE =
"HuggingChat response exceeded the maximum supported size before completing";
export class HuggingChatStreamError extends Error {
constructor(message: string) {
super(message);
@@ -74,15 +79,23 @@ export async function* streamJsonlToOpenAi(
id: string,
created: number,
signal?: AbortSignal | null,
cancellationSignal?: AbortSignal | null
cancellationSignal?: AbortSignal | null,
maxBytes: number = HUGGINGCHAT_MAX_BODY_BYTES
): AsyncGenerator<string> {
const reader = body.getReader();
const unbindReaderCancellation = bindReaderCancellation(reader, cancellationSignal);
// Also bind the plain `signal` so an already-in-flight `reader.read()` unblocks the
// instant it aborts, instead of only being noticed the next time the loop polls
// `signal?.aborted` (#12577 — a stalled upstream can otherwise leave the read
// suspended forever even once a caller-supplied timeout signal has fired).
const unbindSignalCancellation = bindReaderCancellation(reader, signal);
const decoder = new TextDecoder();
let buffer = "";
let emittedRole = false;
let fullText = "";
let finished = false;
let totalBytes = 0;
let exceededCap = false;
try {
while (true) {
@@ -91,6 +104,13 @@ export async function* streamJsonlToOpenAi(
const { value, done } = await reader.read();
if (done) break;
totalBytes += value.byteLength;
if (totalBytes > maxBytes) {
exceededCap = true;
cancelReader(reader);
break;
}
buffer += decoder.decode(value, { stream: true });
const lines = buffer.split("\n");
@@ -163,7 +183,7 @@ export async function* streamJsonlToOpenAi(
if (finished) break;
}
if (!finished && buffer.trim()) {
if (!finished && !exceededCap && buffer.trim()) {
const parsed = parseJsonlLine(buffer.trim());
if (parsed.error) {
throw new HuggingChatStreamError(parsed.error);
@@ -190,9 +210,26 @@ export async function* streamJsonlToOpenAi(
}
} finally {
unbindReaderCancellation();
unbindSignalCancellation();
reader.releaseLock();
}
if (exceededCap) {
yield sseChunk({
id,
object: "chat.completion.chunk",
created,
model,
error: {
message: MAX_BODY_EXCEEDED_MESSAGE,
type: "upstream_error",
code: "huggingchat_payload_too_large",
},
});
yield "data: [DONE]\n\n";
return;
}
if (!signal?.aborted && !cancellationSignal?.aborted) {
yield sseChunk({
id,
@@ -209,12 +246,19 @@ export async function* streamJsonlToOpenAi(
export async function readJsonlResponse(
body: ReadableStream<Uint8Array>,
signal?: AbortSignal | null
signal?: AbortSignal | null,
maxBytes: number = HUGGINGCHAT_MAX_BODY_BYTES
): Promise<string> {
const reader = body.getReader();
// Bind the signal so an already-in-flight `reader.read()` unblocks the instant it
// aborts, instead of only being noticed the next time the loop polls `signal?.aborted`
// (#12577 — a stalled upstream can otherwise leave the read suspended forever even
// once a caller-supplied timeout signal has fired).
const unbindSignalCancellation = bindReaderCancellation(reader, signal);
const decoder = new TextDecoder();
let buffer = "";
let fullText = "";
let totalBytes = 0;
try {
while (true) {
@@ -223,6 +267,12 @@ export async function readJsonlResponse(
const { value, done } = await reader.read();
if (done) break;
totalBytes += value.byteLength;
if (totalBytes > maxBytes) {
cancelReader(reader);
throw new HuggingChatStreamError(MAX_BODY_EXCEEDED_MESSAGE);
}
buffer += decoder.decode(value, { stream: true });
const lines = buffer.split("\n");
@@ -249,6 +299,7 @@ export async function readJsonlResponse(
if (parsed.error) throw new HuggingChatStreamError(parsed.error);
}
} finally {
unbindSignalCancellation();
reader.releaseLock();
}

View File

@@ -0,0 +1,119 @@
// Regression test for issue #12577: HuggingChat NDJSON executor buffered the
// upstream body with no byte ceiling and no timeout, so a stalled/hostile
// upstream that never emits a terminal marker (`finalAnswer` / `status:
// finished`) drove unbounded memory growth per in-flight request.
import { test } from "node:test";
import assert from "node:assert/strict";
import {
streamJsonlToOpenAi,
readJsonlResponse,
HuggingChatStreamError,
} from "../../open-sse/executors/huggingchat/jsonlStream.ts";
const REASONABLE_CAP_BYTES = 2 * 1024 * 1024; // 2 MB
const TEST_SAFETY_CEILING_BYTES = REASONABLE_CAP_BYTES * 4; // 8 MB
function makeUnboundedStream(): {
body: ReadableStream<Uint8Array>;
getTotalSent: () => number;
getClosedBySafetyCeiling: () => boolean;
} {
const encoder = new TextEncoder();
const tokenChunk = "a".repeat(32 * 1024); // 32 KB token payload per line
const line = JSON.stringify({ type: "stream", token: tokenChunk }) + "\n";
const lineBytes = encoder.encode(line).byteLength;
let totalSent = 0;
let closedBySafetyCeiling = false;
const body = new ReadableStream<Uint8Array>({
pull(controller) {
if (totalSent >= TEST_SAFETY_CEILING_BYTES) {
closedBySafetyCeiling = true;
controller.close();
return;
}
controller.enqueue(encoder.encode(line));
totalSent += lineBytes;
// Deliberately never emit a finalAnswer/status:finished terminal marker.
},
});
return {
body,
getTotalSent: () => totalSent,
getClosedBySafetyCeiling: () => closedBySafetyCeiling,
};
}
test("streamJsonlToOpenAi aborts once accumulated upstream body exceeds a size cap, instead of buffering forever", async () => {
const { body, getTotalSent, getClosedBySafetyCeiling } = makeUnboundedStream();
const encoder = new TextEncoder();
let sawUpstreamErrorChunk = false;
let bytesReceivedByConsumer = 0;
for await (const chunk of streamJsonlToOpenAi(
body,
"gpt-huggingchat",
"id-1",
0,
undefined,
undefined,
REASONABLE_CAP_BYTES
)) {
bytesReceivedByConsumer += encoder.encode(chunk).byteLength;
if (/upstream_error|too_large|payload.*exceed/i.test(chunk)) {
sawUpstreamErrorChunk = true;
break;
}
}
assert.ok(
sawUpstreamErrorChunk,
`expected streamJsonlToOpenAi to abort with an upstream-error chunk once the ` +
`accumulated body exceeded ~${REASONABLE_CAP_BYTES} bytes, but it kept consuming ` +
`upstream data with no ceiling (sent ${getTotalSent()} bytes before the TEST's own ` +
`safety ceiling stepped in: closedBySafetyCeiling=${getClosedBySafetyCeiling()}, ` +
`bytesReceivedByConsumer=${bytesReceivedByConsumer}). This confirms issue #12577: ` +
`no byte cap is enforced on the read loop.`
);
assert.ok(
getTotalSent() < TEST_SAFETY_CEILING_BYTES,
"expected the cap to trip well before the test's own 8MB safety ceiling"
);
});
test("readJsonlResponse throws a HuggingChatStreamError once accumulated upstream body exceeds a size cap", async () => {
const { body, getClosedBySafetyCeiling } = makeUnboundedStream();
await assert.rejects(
() => readJsonlResponse(body, undefined, REASONABLE_CAP_BYTES),
(err: unknown) => err instanceof HuggingChatStreamError
);
assert.equal(
getClosedBySafetyCeiling(),
false,
"expected the cap to trip well before the test's own 8MB safety ceiling"
);
});
test("streamJsonlToOpenAi terminates the read loop once an idle-timeout signal fires", async () => {
const body = new ReadableStream<Uint8Array>({
pull() {
// Never enqueue and never close: simulates a stalled upstream connection
// that sends nothing at all after headers, relying solely on the caller's
// timeout signal (mirroring huggingchat.ts's combinedSignal) to unblock.
},
});
const idleTimeout = AbortSignal.timeout(50);
const chunks: string[] = [];
for await (const chunk of streamJsonlToOpenAi(body, "gpt-huggingchat", "id-2", 0, idleTimeout)) {
chunks.push(chunk);
}
assert.ok(idleTimeout.aborted, "expected the idle-timeout signal to have fired");
});

View File

@@ -1,33 +0,0 @@
import assert from "node:assert/strict";
import { test } from "node:test";
import { resolveOpencodeTargetFormat } from "../../open-sse/executors/opencode.ts";
// Issue #12196: opencode-go/gpt-5.6-luna is served by the Go upstream ONLY on
// /responses — /chat/completions 500s for this model. The github provider
// already declares targetFormat:"openai-responses" for the same model id, and
// opencode-go already does the same for deepseek-v4-pro/deepseek-v4-flash on
// this exact provider — but gpt-5.6-luna itself is missing from the
// opencode-go registry, so getModelTargetFormat() falls through to null and
// resolveOpencodeTargetFormat() defaults to "openai", which makes
// OpencodeExecutor.buildUrl() post to /chat/completions instead of /responses.
test("opencode-go/gpt-5.6-luna must resolve to the openai-responses target format", () => {
const resolved = resolveOpencodeTargetFormat("opencode-go", "gpt-5.6-luna");
assert.equal(
resolved,
"openai-responses",
"opencode-go/gpt-5.6-luna resolved to '" +
resolved +
"' instead of 'openai-responses' — OpencodeExecutor.buildUrl() will post to " +
"/chat/completions, which the Go upstream 500s on for this model (issue #12196)"
);
});
// Control: the sibling deepseek-v4-flash entry on the SAME opencode-go
// provider already declares targetFormat:"openai-responses" and must keep
// working — proves the assertion above isn't failing for an unrelated reason
// (e.g. a broken import or alias resolution).
test("control: opencode-go/deepseek-v4-flash already resolves to openai-responses", () => {
const resolved = resolveOpencodeTargetFormat("opencode-go", "deepseek-v4-flash");
assert.equal(resolved, "openai-responses");
});