惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

L
LangChain Blog
S
SegmentFault 最新的问题
V
Visual Studio Blog
J
Java Code Geeks
宝玉的分享
宝玉的分享
美团技术团队
博客园 - Franky
酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hackread – Cybersecurity News, Data Breaches, AI and More
有赞技术团队
有赞技术团队
量子位
Martin Fowler
Martin Fowler
MyScale Blog
MyScale Blog
Google DeepMind News
Google DeepMind News
Jina AI
Jina AI
博客园 - 叶小钗
月光博客
月光博客
P
Proofpoint News Feed
D
DataBreaches.Net
Blog — PlanetScale
Blog — PlanetScale
博客园_首页
腾讯CDC
Microsoft Azure Blog
Microsoft Azure Blog
Stack Overflow Blog
Stack Overflow Blog

Recent Commits to openclaw:main

test: merge chat side-result checks · openclaw/openclaw@ddd2c2a test: merge cron history checks · openclaw/openclaw@f7eb746 test: merge responsive navigation shell checks · openclaw/openclaw@c2e4b47 docs(changelog): add codex oauth fixes · openclaw/openclaw@628e6cd test: merge navigation routing cases · openclaw/openclaw@5d8cecb Tests: mock channel registry bundled fallback · openclaw/openclaw@2b08233 Secrets: avoid broad web search discovery for single plugin config · openclaw/openclaw@a464f59 test: merge config view browser checks · openclaw/openclaw@20cf511 fix(status): align oauth health with runtime · openclaw/openclaw@eed7116 feat: add macOS screen snapshots for monitor preview (#67954) thanks … · openclaw/openclaw@f377db1 fix: report shared auth scopes in hello-ok (#67810) thanks @BunsDev · openclaw/openclaw@0b6c39b Auto-reply: avoid eager bundled route fallback · openclaw/openclaw@3ea1bf4 Tests: narrow session binding contract setup · openclaw/openclaw@54e4e16 fix(macOS): enable undo/redo in webchat composer text input (#34962) · openclaw/openclaw@00951dc Tests: speed up channel setup promotion · openclaw/openclaw@82b529a Docs: refresh agent instructions · openclaw/openclaw@5775fe2 fix(auth): serialize OAuth refresh across agents to fix #26322 (#67876) · openclaw/openclaw@8e79080 test: allow ollama public surface boundary test · openclaw/openclaw@7d4f1a6 Docs: add test performance guardrails · openclaw/openclaw@89706d3 Tests: restore context-engine usage proof · openclaw/openclaw@e4c4f95 Tests: slim context engine runtime coverage · openclaw/openclaw@74c198f ci: retry failed custom checkouts · openclaw/openclaw@0ee5baf test: trim duplicate provider auth onboarding cases · openclaw/openclaw@1ffc02e matrix: fix sessions_spawn --thread subagent session spawning (#67643) · openclaw/openclaw@1ce2596 test: reduce auth choice fixture churn · openclaw/openclaw@857b9cd test: mock health status config boundaries · openclaw/openclaw@9d5ab4a test: mock onboard config io boundary · openclaw/openclaw@299694d test: mock legacy state plugin boundaries · openclaw/openclaw@2713089 test: mock channel install boundaries · openclaw/openclaw@b945248 test: mock doctor preview channel boundaries · openclaw/openclaw@b1a3ad4
fix(agents): bound provider JSON response reads (#95218) ...
Alix-007 · 2026-06-23 · via Recent Commits to openclaw:main
Original file line numberDiff line numberDiff line change

@@ -38,6 +38,32 @@ function createStreamingBinaryResponse(params: {

3838

};

3939

}

4040
41+

function createStreamingJsonResponse(params: { chunkCount: number; chunkSize: number }): {

42+

response: Response;

43+

getReadCount: () => number;

44+

} {

45+

// Streaming fixture proves oversized JSON reads stop before buffering everything.

46+

let reads = 0;

47+

const encoder = new TextEncoder();

48+

const stream = new ReadableStream<Uint8Array>({

49+

pull(controller) {

50+

if (reads >= params.chunkCount) {

51+

controller.close();

52+

return;

53+

}

54+

reads += 1;

55+

controller.enqueue(encoder.encode("a".repeat(params.chunkSize)));

56+

},

57+

});

58+

return {

59+

response: new Response(stream, {

60+

status: 200,

61+

headers: { "Content-Type": "application/json" },

62+

}),

63+

getReadCount: () => reads,

64+

};

65+

}

66+
4167

describe("provider error utils", () => {

4268

it("formats nested provider error details with request ids", async () => {

4369

const response = new Response(

@@ -211,6 +237,32 @@ describe("provider error utils", () => {

211237

);

212238

});

213239
240+

it("parses well-formed JSON responses under the byte cap", async () => {

241+

const response = new Response(JSON.stringify({ models: ["a", "b"] }), {

242+

status: 200,

243+

headers: { "content-type": "application/json" },

244+

});

245+
246+

await expect(

247+

readProviderJsonResponse<{ models: string[] }>(response, "Provider catalog failed"),

248+

).resolves.toEqual({ models: ["a", "b"] });

249+

});

250+
251+

it("caps successful JSON responses instead of buffering oversized bodies", async () => {

252+

const streamed = createStreamingJsonResponse({

253+

chunkCount: 20,

254+

chunkSize: 1024,

255+

});

256+
257+

await expect(

258+

readProviderJsonResponse(streamed.response, "Provider catalog failed", {

259+

maxBytes: 2048,

260+

}),

261+

).rejects.toThrow("Provider catalog failed: JSON response exceeds 2048 bytes");

262+
263+

expect(streamed.getReadCount()).toBeLessThan(20);

264+

});

265+
214266

it("caps successful binary responses instead of buffering oversized bodies", async () => {

215267

const streamed = createStreamingBinaryResponse({

216268

chunkCount: 20,

Original file line numberDiff line numberDiff line change

@@ -13,6 +13,7 @@ export { normalizeOptionalString as trimToUndefined } from "../../packages/norma

1313
1414

const ERROR_BODY_METADATA_LIMIT = 500;

1515

const PROVIDER_BINARY_RESPONSE_MAX_BYTES = 16 * 1024 * 1024;

16+

const PROVIDER_JSON_RESPONSE_MAX_BYTES = 16 * 1024 * 1024;

1617
1718

/** Returns a plain object view for provider JSON payloads when one exists. */

1819

export function asObject(value: unknown): Record<string, unknown> | undefined {

@@ -287,10 +288,24 @@ export async function assertOkOrThrowHttpError(response: Response, label: string

287288

throw await createProviderHttpError(response, label, { statusPrefix: "HTTP " });

288289

}

289290
290-

/** Parses a provider JSON response and wraps malformed JSON with the caller's label. */

291-

export async function readProviderJsonResponse<T>(response: Response, label: string): Promise<T> {

291+

/**

292+

* Parses a provider JSON response under a byte cap and wraps malformed JSON with the caller's label.

293+

*

294+

* The body is read through the same bounded reader as binary responses so a provider that streams an

295+

* unbounded JSON body cannot force the runtime to buffer the whole payload before parsing.

296+

*/

297+

export async function readProviderJsonResponse<T>(

298+

response: Response,

299+

label: string,

300+

opts?: { maxBytes?: number },

301+

): Promise<T> {

302+

const maxBytes = opts?.maxBytes ?? PROVIDER_JSON_RESPONSE_MAX_BYTES;

303+

const bytes = await readResponseWithLimit(response, maxBytes, {

304+

onOverflow: ({ maxBytes: maxBytesLocal }) =>

305+

new Error(`${label}: JSON response exceeds ${maxBytesLocal} bytes`),

306+

});

292307

try {

293-

return (await response.json()) as T;

308+

return JSON.parse(new TextDecoder().decode(bytes)) as T;

294309

} catch (cause) {

295310

throw new Error(`${label}: malformed JSON response`, { cause });

296311

}