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

推荐订阅源

aimingoo的专栏
aimingoo的专栏
WordPress大学
WordPress大学
阮一峰的网络日志
阮一峰的网络日志
博客园 - 司徒正美
月光博客
月光博客
宝玉的分享
宝玉的分享
Recent Announcements
Recent Announcements
小众软件
小众软件
H
Hackread – Cybersecurity News, Data Breaches, AI and More
美团技术团队
博客园 - 三生石上(FineUI控件)
A
About on SuperTechFans
J
Java Code Geeks
云风的 BLOG
云风的 BLOG
罗磊的独立博客
大猫的无限游戏
大猫的无限游戏
IT之家
IT之家
Vercel News
Vercel News
量子位
Martin Fowler
Martin Fowler
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Visual Studio Blog
腾讯CDC
有赞技术团队
有赞技术团队

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 Google prompt cache response reads (#9...
Alix-007 · 2026-06-23 · via Recent Commits to openclaw:main

@@ -68,6 +68,30 @@ function createCacheFetchMock(params: { name: string; expireTime: string }) {

6868

);

6969

}

707071+

// Builds a 200-OK response whose body streams more than 1 MiB with no

72+

// Content-Length, mirroring a buggy/hostile Google cachedContents endpoint.

73+

// The shared byte-cap reader must cancel the body before fully buffering it.

74+

function createOversizedJsonResponse(): { response: Response; cancel: ReturnType<typeof vi.fn> } {

75+

const cancel = vi.fn(async () => undefined);

76+

let pullCount = 0;

77+

const response = new Response(

78+

new ReadableStream<Uint8Array>({

79+

pull(controller) {

80+

pullCount += 1;

81+

// First chunk already exceeds the 1 MiB cap so the reader truncates

82+

// and cancels instead of waiting for the (never-ending) rest.

83+

controller.enqueue(new Uint8Array(pullCount === 1 ? 1024 * 1024 + 1 : 1));

84+

},

85+

cancel,

86+

}),

87+

{

88+

status: 200,

89+

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

90+

},

91+

);

92+

return { response, cancel };

93+

}

94+7195

function createCapturingStreamFn(result = "stream") {

7296

// The wrapper mutates payloads through onPayload before calling the real

7397

// stream; capture that final payload instead of mocking Google responses.

@@ -552,4 +576,91 @@ describe("google prompt cache", () => {

552576

expect(wrapped).toBeUndefined();

553577

expect(fetchMock).not.toHaveBeenCalled();

554578

});

579+580+

it("bounds an oversized cache-creation response body instead of buffering it", async () => {

581+

const now = 4_000_000;

582+

const { response, cancel } = createOversizedJsonResponse();

583+

const fetchMock = vi.fn(async () => response);

584+

const sessionManager = makeSessionManager();

585+

const innerStreamFn = vi.fn(() => "stream" as never);

586+587+

const wrapped = await preparePromptCacheStream({

588+

fetchMock,

589+

now,

590+

sessionManager,

591+

streamFn: innerStreamFn,

592+

});

593+594+

await expect(

595+

Promise.resolve(

596+

wrapped?.(

597+

makeGoogleModel(),

598+

{ systemPrompt: "Follow policy.", messages: [] } as never,

599+

{} as never,

600+

),

601+

),

602+

).rejects.toThrow(/Google prompt cache response too large: \d+ bytes/);

603+604+

expect(fetchMock).toHaveBeenCalledTimes(1);

605+

expect(callArg(fetchMock, 0, 0)).toBe(

606+

"https://generativelanguage.googleapis.com/v1beta/cachedContents",

607+

);

608+

expect(cancel).toHaveBeenCalledOnce();

609+

});

610+611+

it("bounds an oversized cache-refresh response body instead of buffering it", async () => {

612+

const now = 4_500_000;

613+

const expireSoon = new Date(now + 60_000).toISOString();

614+

const systemPromptDigest = crypto.createHash("sha256").update("Follow policy.").digest("hex");

615+

const sessionManager = makeSessionManager([

616+

{

617+

id: "entry-1",

618+

parentId: null,

619+

timestamp: new Date(now - 5_000).toISOString(),

620+

type: "custom",

621+

customType: "openclaw.google-prompt-cache",

622+

data: {

623+

status: "ready",

624+

timestamp: now - 5_000,

625+

provider: "google",

626+

modelId: "gemini-3.1-pro-preview",

627+

modelApi: "google-generative-ai",

628+

baseUrl: "https://generativelanguage.googleapis.com/v1beta",

629+

systemPromptDigest,

630+

cacheRetention: "long",

631+

cachedContent: "cachedContents/system-cache-overflow",

632+

expireTime: expireSoon,

633+

},

634+

},

635+

]);

636+

const { response, cancel } = createOversizedJsonResponse();

637+

const fetchMock = vi.fn(async () => response);

638+

const { streamFn: innerStreamFn, getCapturedPayload } = createCapturingStreamFn();

639+640+

const wrapped = await preparePromptCacheStream({

641+

fetchMock,

642+

now,

643+

sessionManager,

644+

streamFn: innerStreamFn,

645+

});

646+647+

// The TTL-refresh read swallows errors (.catch(() => null)) and falls back

648+

// to the still-valid cached content, so the oversized body must be cancelled

649+

// by the byte cap rather than fully buffered.

650+

await Promise.resolve(

651+

wrapped?.(

652+

makeGoogleModel(),

653+

{ systemPrompt: "Follow policy.", messages: [] } as never,

654+

{} as never,

655+

),

656+

);

657+658+

expect(fetchMock).toHaveBeenCalledTimes(1);

659+

expect(fetchUrl(fetchMock)).toBe(

660+

"https://generativelanguage.googleapis.com/v1beta/cachedContents/system-cache-overflow?updateMask=ttl",

661+

);

662+

expect(fetchInit(fetchMock).method).toBe("PATCH");

663+

expect(cancel).toHaveBeenCalledOnce();

664+

expect(getCapturedPayload()?.cachedContent).toBe("cachedContents/system-cache-overflow");

665+

});

555666

});