Skip to content

Commit 4d805ae

Browse files
anandgupta42claude
andcommitted
fix: harden the catalog guard against hangs and hollow payloads
Addresses review findings on the guard added in the previous commit. Bound the fetch with `AbortSignal.timeout(60s)`. A blackholed connection is the one failure `fetch` does not surface on its own — no error, no bytes, just a hang until the job's own timeout kills it with no useful message. The previous version claimed to fail loudly but would have hung in that case. Validate provider records, not just top-level keys. A payload can carry 50+ keys whose values are junk: that passed the old key-count check and would have shipped a catalog with nothing selectable. Each required provider must now carry a non-empty `models` object. Verified against a crafted payload of 80 junk keys and one where every provider is present but hollow. Also drops the duplicated `${modelsUrl}/api.json` construction and the repeated `process.env.MODELS_DEV_API_JSON` reads, and avoids an unsafe type assertion by reading the parsed catalog through a `Map<string, unknown>`. Not changed: the eight matrix builds still fetch independently, so a models.dev update mid-release could in principle give different binaries different catalogs. Fixing that needs a fetch-once job passing the catalog as an artifact, which is a larger change than this PR; upstream has the same property. Co-Authored-By: Claude Opus 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01VqnuBDGkh1ZT65Ti7e6DHZ
1 parent 3f864ec commit 4d805ae

1 file changed

Lines changed: 43 additions & 15 deletions

File tree

‎packages/opencode/script/build.ts‎

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,38 @@ const modelsUrl = process.env.OPENCODE_MODELS_URL || "https://models.dev"
3535
// a shipped binary visibly broken.
3636
const MIN_CATALOG_PROVIDERS = 50
3737
const REQUIRED_CATALOG_PROVIDERS = ["anthropic", "openai", "google"]
38+
// A blackholed connection is the one failure `fetch` will not surface on its own:
39+
// no error, no bytes, just a hang until the job's own timeout kills it with no
40+
// useful message. Bound it so the build fails with a reason instead.
41+
const CATALOG_FETCH_TIMEOUT_MS = 60_000
3842

39-
/** Fetch the models.dev catalog, failing loudly on a non-2xx.
43+
/** Fetch the models.dev catalog, failing loudly rather than hanging or
44+
* returning an error page.
4045
*
41-
* `fetch` resolves for 4xx/5xx, so without this check a load-balancer error page
42-
* flows straight into the snapshot. An HTML body would at least break the build
43-
* at parse time, but a JSON error body (`{"error": ...}`) is valid TypeScript and
44-
* would ship as a catalog with no providers in it. */
46+
* `fetch` resolves for 4xx/5xx, so without the `res.ok` check a load-balancer
47+
* error page flows straight into the snapshot. An HTML body would at least break
48+
* the build at parse time, but a JSON error body (`{"error": ...}`) is valid
49+
* TypeScript and would ship as a catalog with no providers in it. */
4550
async function fetchModelsCatalog(url: string): Promise<string> {
46-
const res = await fetch(url)
51+
let res: Response
52+
try {
53+
res = await fetch(url, { signal: AbortSignal.timeout(CATALOG_FETCH_TIMEOUT_MS) })
54+
} catch (e) {
55+
throw new Error(`models.dev fetch from ${url} failed or timed out after ${CATALOG_FETCH_TIMEOUT_MS}ms`, {
56+
cause: e,
57+
})
58+
}
4759
if (!res.ok) throw new Error(`models.dev fetch failed: HTTP ${res.status} ${res.statusText} from ${url}`)
4860
return await res.text()
4961
}
5062

5163
/** Reject a catalog that parses but is obviously not usable.
5264
*
53-
* Release builds embed this in every binary, so an empty or truncated payload
54-
* has to stop the release rather than ship a CLI that offers no models. */
65+
* Release builds embed this in every binary, so an empty, truncated or
66+
* structurally broken payload has to stop the release rather than ship a CLI
67+
* that offers no models. Checking the top-level key count alone is not enough:
68+
* a payload can carry 50+ keys whose values are junk, which parses fine and
69+
* ships a catalog with nothing selectable in it. */
5570
function assertUsableCatalog(text: string, origin: string): void {
5671
let parsed: unknown
5772
try {
@@ -61,24 +76,37 @@ function assertUsableCatalog(text: string, origin: string): void {
6176
}
6277
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed))
6378
throw new Error(`models.dev catalog from ${origin} is not a provider object`)
64-
const providers = Object.keys(parsed)
79+
const catalog = new Map<string, unknown>(Object.entries(parsed))
80+
const providers = [...catalog.keys()]
6581
if (providers.length < MIN_CATALOG_PROVIDERS)
6682
throw new Error(
6783
`models.dev catalog from ${origin} has only ${providers.length} providers, expected at least ${MIN_CATALOG_PROVIDERS}`,
6884
)
69-
const missing = REQUIRED_CATALOG_PROVIDERS.filter((p) => !providers.includes(p))
85+
const missing = REQUIRED_CATALOG_PROVIDERS.filter((p) => !catalog.has(p))
7086
if (missing.length > 0)
7187
throw new Error(`models.dev catalog from ${origin} is missing required providers: ${missing.join(", ")}`)
72-
console.log(`models.dev catalog from ${origin}: ${providers.length} providers`)
88+
// Every required provider must actually carry models, not just exist as a key.
89+
const modelCount = (id: string): number => {
90+
const entry = catalog.get(id)
91+
if (typeof entry !== "object" || entry === null || !("models" in entry)) return 0
92+
const models = entry.models
93+
return typeof models === "object" && models !== null && !Array.isArray(models) ? Object.keys(models).length : 0
94+
}
95+
const empty = REQUIRED_CATALOG_PROVIDERS.filter((p) => modelCount(p) === 0)
96+
if (empty.length > 0)
97+
throw new Error(`models.dev catalog from ${origin} has no usable models for: ${empty.join(", ")}`)
98+
console.log(
99+
`models.dev catalog from ${origin}: ${providers.length} providers ` +
100+
`(${REQUIRED_CATALOG_PROVIDERS.map((p) => `${p}=${modelCount(p)}`).join(", ")})`,
101+
)
73102
}
74103

75104
// Fetch and generate models.dev snapshot. MODELS_DEV_API_JSON pins the catalog to
76105
// a local file for hermetic builds (ci.yml, pre-release-check.ts); release builds
77106
// leave it unset so the shipped binary embeds a release-time catalog.
78-
const modelsOrigin = process.env.MODELS_DEV_API_JSON ?? `${modelsUrl}/api.json`
79-
const modelsData = process.env.MODELS_DEV_API_JSON
80-
? await Bun.file(process.env.MODELS_DEV_API_JSON).text()
81-
: await fetchModelsCatalog(`${modelsUrl}/api.json`)
107+
const modelsFile = process.env.MODELS_DEV_API_JSON
108+
const modelsOrigin = modelsFile ?? `${modelsUrl}/api.json`
109+
const modelsData = modelsFile ? await Bun.file(modelsFile).text() : await fetchModelsCatalog(modelsOrigin)
82110
assertUsableCatalog(modelsData, modelsOrigin)
83111
await Bun.write(
84112
path.join(dir, "src/provider/models-snapshot.ts"),

0 commit comments

Comments
 (0)