Skip to content

Commit 8825ac6

Browse files
vincentkockrissding
andcommitted
fix(plugin-sdk): contextualize malformed provider catalogs
Co-authored-by: krissding <[email protected]>
1 parent 1f01733 commit 8825ac6

6 files changed

Lines changed: 326 additions & 164 deletions

‎src/agents/provider-http-errors.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,29 @@ describe("provider error utils", () => {
417417
expect(streamed.getReadCount()).toBeLessThan(20);
418418
});
419419

420+
it("honors custom JSON overflow errors and stops reading", async () => {
421+
const streamed = createStreamingJsonResponse({
422+
chunkCount: 20,
423+
chunkSize: 1024,
424+
});
425+
const sentinel = new Error("custom overflow");
426+
const onOverflow = vi.fn(() => sentinel);
427+
428+
const error = await readProviderJsonResponse(streamed.response, "Provider catalog failed", {
429+
maxBytes: 2048,
430+
onOverflow,
431+
}).catch((cause: unknown) => cause);
432+
433+
expect(error).toBe(sentinel);
434+
expect(onOverflow).toHaveBeenCalledOnce();
435+
expect(onOverflow).toHaveBeenCalledWith({
436+
size: 3072,
437+
maxBytes: 2048,
438+
res: streamed.response,
439+
});
440+
expect(streamed.getReadCount()).toBeLessThan(20);
441+
});
442+
420443
it("rejects provider JSON responses with invalid UTF-8 bytes instead of silently replacing them", async () => {
421444
const invalidUtf8Bytes = new Uint8Array([0x7b, 0x22, 0x6b, 0x65, 0x79, 0x22, 0x3a, 0xff, 0x7d]);
422445
const response = new Response(invalidUtf8Bytes.buffer, {

‎src/agents/provider-http-errors.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ function readProviderResponseBytes(
9898
label: string,
9999
kind: string,
100100
opts?: ProviderResponseReadOptions,
101-
onOverflow?: ProviderResponseReadOptions["onOverflow"],
102101
): Promise<Uint8Array> {
103102
return readResponseWithLimit(response, opts?.maxBytes ?? PROVIDER_RESPONSE_MAX_BYTES, {
104103
...opts,
@@ -108,7 +107,7 @@ function readProviderResponseBytes(
108107
(({ chunkTimeoutMs }) =>
109108
new Error(`${label}: response body stalled for ${chunkTimeoutMs}ms`)),
110109
onOverflow:
111-
onOverflow ??
110+
opts?.onOverflow ??
112111
(({ maxBytes: limit }) => new Error(`${label}: ${kind} response exceeds ${limit} bytes`)),
113112
});
114113
}
@@ -512,7 +511,7 @@ export async function readProviderBinaryResponse(
512511
void response.body?.cancel().catch(() => undefined);
513512
throw error;
514513
}
515-
const bytes = await readProviderResponseBytes(response, label, kind, opts, opts?.onOverflow);
514+
const bytes = await readProviderResponseBytes(response, label, kind, opts);
516515
if (bytes.byteLength === 0) {
517516
throw new Error(`${label}: malformed ${kind} response`);
518517
}
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
import { describe, expect, it, vi, type MockedFunction } from "vitest";
2+
import {
3+
fetchLiveProviderModelIds,
4+
type LiveModelCatalogFetchGuard,
5+
} from "./provider-catalog-live-runtime.js";
6+
7+
describe("provider catalog malformed pagination", () => {
8+
it("reports incomplete pagination on malformed absolute next URL with no usable fallback", async () => {
9+
const release = vi.fn(async () => undefined);
10+
const fetchGuardMock: MockedFunction<LiveModelCatalogFetchGuard> = vi.fn(async () => ({
11+
response: new Response(
12+
JSON.stringify({
13+
data: [{ id: "model-a", object: "model" }],
14+
// Space in hostname makes this a genuinely invalid absolute URL.
15+
next: "http://exa mple.com/models?page=2",
16+
has_more: false,
17+
}),
18+
),
19+
finalUrl: "https://provider.example.test/v1/models",
20+
release,
21+
}));
22+
23+
// The provider explicitly advertised a next page via the `next` field but
24+
// the URL is malformed and there is no cursor fallback. The controlled
25+
// incomplete-pagination error prevents silently returning a truncated
26+
// catalog.
27+
await expect(
28+
fetchLiveProviderModelIds({
29+
providerId: "provider",
30+
endpoint: "https://provider.example.test/v1/models",
31+
fetchGuard: fetchGuardMock,
32+
}),
33+
).rejects.toThrow(
34+
"provider model discovery did not include a supported next page before the catalog completed",
35+
);
36+
37+
expect(fetchGuardMock).toHaveBeenCalledTimes(1);
38+
});
39+
40+
it("reports incomplete pagination on malformed nested links.next URL", async () => {
41+
const release = vi.fn(async () => undefined);
42+
const fetchGuardMock: MockedFunction<LiveModelCatalogFetchGuard> = vi.fn(async () => ({
43+
response: new Response(
44+
JSON.stringify({
45+
data: [{ id: "model-a", object: "model" }],
46+
links: { next: "http://exa mple.com/models?page=2" },
47+
has_more: false,
48+
}),
49+
),
50+
finalUrl: "https://provider.example.test/v1/models",
51+
release,
52+
}));
53+
54+
await expect(
55+
fetchLiveProviderModelIds({
56+
providerId: "provider",
57+
endpoint: "https://provider.example.test/v1/models",
58+
fetchGuard: fetchGuardMock,
59+
}),
60+
).rejects.toThrow(
61+
"provider model discovery did not include a supported next page before the catalog completed",
62+
);
63+
64+
expect(fetchGuardMock).toHaveBeenCalledTimes(1);
65+
});
66+
67+
it("recovers malformed next URL via cursor fallback", async () => {
68+
const release = vi.fn(async () => undefined);
69+
const fetchGuardMock: MockedFunction<LiveModelCatalogFetchGuard> = vi
70+
.fn()
71+
.mockResolvedValueOnce({
72+
response: new Response(
73+
JSON.stringify({
74+
data: [{ id: "model-a", object: "model" }],
75+
next: "http://exa mple.com/models?page=2",
76+
next_cursor: "cursor-2",
77+
has_more: true,
78+
}),
79+
),
80+
finalUrl: "https://provider.example.test/v1/models",
81+
release,
82+
})
83+
.mockResolvedValueOnce({
84+
response: new Response(
85+
JSON.stringify({ data: [{ id: "model-b", object: "model" }], has_more: false }),
86+
),
87+
finalUrl: "https://provider.example.test/v1/models?after=cursor-2",
88+
release,
89+
});
90+
91+
// The malformed next URL is ignored; cursor-based pagination takes over.
92+
await expect(
93+
fetchLiveProviderModelIds({
94+
providerId: "provider",
95+
endpoint: "https://provider.example.test/v1/models",
96+
fetchGuard: fetchGuardMock,
97+
}),
98+
).resolves.toEqual(["model-a", "model-b"]);
99+
100+
expect(fetchGuardMock).toHaveBeenCalledTimes(2);
101+
expect(fetchGuardMock.mock.calls[1]?.[0].url).toBe(
102+
"https://provider.example.test/v1/models?after=cursor-2",
103+
);
104+
});
105+
106+
it("sets safe replay headers when final URL is unparseable", async () => {
107+
const release = vi.fn(async () => undefined);
108+
const fetchGuardMock: MockedFunction<LiveModelCatalogFetchGuard> = vi.fn(async () => ({
109+
response: new Response(
110+
JSON.stringify({
111+
data: [{ id: "model-a", object: "model" }],
112+
}),
113+
),
114+
// An unparseable finalUrl should trigger safe replay headers (conservative
115+
// cross-origin assumption), not crash.
116+
finalUrl: "http://exa mple.com/models",
117+
release,
118+
}));
119+
120+
await expect(
121+
fetchLiveProviderModelIds({
122+
providerId: "provider",
123+
endpoint: "https://provider.example.test/v1/models",
124+
fetchGuard: fetchGuardMock,
125+
}),
126+
).resolves.toEqual(["model-a"]);
127+
128+
expect(fetchGuardMock).toHaveBeenCalledTimes(1);
129+
});
130+
131+
it("reports incomplete pagination instead of crashing on malformed next URL with has_more", async () => {
132+
const release = vi.fn(async () => undefined);
133+
const fetchGuardMock: MockedFunction<LiveModelCatalogFetchGuard> = vi.fn(async () => ({
134+
response: new Response(
135+
JSON.stringify({
136+
data: [{ id: "model-a", object: "model" }],
137+
next: "http://exa mple.com/models?page=2",
138+
has_more: true,
139+
}),
140+
),
141+
finalUrl: "https://provider.example.test/v1/models",
142+
release,
143+
}));
144+
145+
await expect(
146+
fetchLiveProviderModelIds({
147+
providerId: "provider",
148+
endpoint: "https://provider.example.test/v1/models",
149+
fetchGuard: fetchGuardMock,
150+
}),
151+
).rejects.toThrow(
152+
"provider model discovery did not include a supported next page before the catalog completed",
153+
);
154+
155+
expect(fetchGuardMock).toHaveBeenCalledTimes(1);
156+
});
157+
});

0 commit comments

Comments
 (0)