Skip to content

Commit c4140fe

Browse files
fix(run): report a failed provider list as itself, not "Model not found"
`run --model` read a failed `provider.list` call as an empty list, so any server error was reported as "Model not found". It now throws an error naming the HTTP status and detail, which also makes the intermittent subprocess failure diagnosable. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_0174HUYiaceApqHP7EKMNgnb
1 parent b093196 commit c4140fe

3 files changed

Lines changed: 82 additions & 1 deletion

File tree

‎packages/opencode/src/cli/cmd/run.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ import { MessageID } from "../../session/schema"
4848
// altimate_change start — run implies run mode (fork-only module)
4949
import { applyRunModeDefault } from "./run/run-mode"
5050
// altimate_change end
51+
// altimate_change start — a failed provider list is reported as itself, not "Model not found" (fork-only module)
52+
import { providerListOrThrow } from "./run/provider-list"
53+
// altimate_change end
5154
// altimate_change start — run-mode-only idle-done fallback (fork-only modules).
5255
// Detection lives in idle-done.ts; the confirm-DONE challenge text and the DONE
5356
// token contract live in session/termination.ts; delivery goes through the nudge
@@ -622,7 +625,7 @@ You are speaking to a non-technical business executive. Follow these rules stric
622625
// `run` waiting until the process-level timeout kills it.
623626
if (args.model) {
624627
const parsed = Provider.parseModel(args.model)
625-
const providers = (await sdk.provider.list()).data?.all ?? []
628+
const providers = providerListOrThrow(await sdk.provider.list(), args.model)
626629
const provider = providers.find((item) => item.id === parsed.providerID)
627630
if (!provider?.models?.[parsed.modelID]) {
628631
throw new Provider.ModelNotFoundError({
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// `run --model` checks the model against the server's provider list before it starts a session.
2+
// A list call that failed used to read as an empty list, so a server error was reported as
3+
// "Model not found" and looked like a typo in --model. This returns the list, or throws an error
4+
// that says the call failed and why.
5+
6+
type ListResult<T> = {
7+
data?: { all: T[] }
8+
error?: unknown
9+
response?: Response
10+
}
11+
12+
export function providerListOrThrow<T>(result: ListResult<T>, model: string): T[] {
13+
// Checked at runtime: a body that is not the expected JSON (an HTML page, say) still arrives as `data`.
14+
const all: unknown = (result.data as { all?: unknown } | undefined)?.all
15+
if (Array.isArray(all)) return all as T[]
16+
throw new Error(`Could not load the provider list to check --model ${model} (${describeFailure(result)})`)
17+
}
18+
19+
function describeFailure(result: ListResult<unknown>): string {
20+
const status = result.response ? `HTTP ${result.response.status}` : "no response"
21+
const detail = errorDetail(result.error) ?? (result.response?.ok ? "unexpected response body" : undefined)
22+
return detail ? `${status}: ${detail.slice(0, 500)}` : status
23+
}
24+
25+
function errorDetail(error: unknown): string | undefined {
26+
if (error instanceof Error) return error.message
27+
if (typeof error === "string") return error || undefined
28+
if (error && typeof error === "object" && Object.keys(error).length > 0) return JSON.stringify(error)
29+
return undefined
30+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { describe, expect, test } from "bun:test"
2+
import { providerListOrThrow } from "../../../src/cli/cmd/run/provider-list"
3+
4+
const MODEL = "test/test-model"
5+
6+
function failure(result: Parameters<typeof providerListOrThrow>[0]): string {
7+
try {
8+
providerListOrThrow(result, MODEL)
9+
} catch (error) {
10+
return (error as Error).message
11+
}
12+
throw new Error("expected providerListOrThrow to throw")
13+
}
14+
15+
describe("run --model provider list check", () => {
16+
test("returns the providers from a successful list", () => {
17+
const all = [{ id: "test" }]
18+
expect(providerListOrThrow({ data: { all }, response: new Response(null, { status: 200 }) }, MODEL)).toBe(all)
19+
})
20+
21+
test("an empty list is still a list, so an unknown model stays a model error", () => {
22+
expect(providerListOrThrow({ data: { all: [] }, response: new Response(null, { status: 200 }) }, MODEL)).toEqual([])
23+
})
24+
25+
// Each failed call names the model and says what failed, never "Model not found".
26+
const cases: Array<[string, Parameters<typeof providerListOrThrow>[0], string]> = [
27+
[
28+
"a server error with a JSON body",
29+
{ error: { name: "UnknownError", message: "boom" }, response: new Response(null, { status: 500 }) },
30+
'HTTP 500: {"name":"UnknownError","message":"boom"}',
31+
],
32+
["a request that never got a response", { error: new Error("socket closed") }, "no response: socket closed"],
33+
["a request that failed without any detail", { error: {} }, "no response"],
34+
[
35+
"a 200 whose body is not the provider list",
36+
{ data: "<html></html>" as never, response: new Response(null, { status: 200 }) },
37+
"HTTP 200: unexpected response body",
38+
],
39+
["a non-2xx with an empty body", { error: "", response: new Response(null, { status: 503 }) }, "HTTP 503"],
40+
]
41+
for (const [name, result, detail] of cases) {
42+
test(name, () => {
43+
const message = failure(result)
44+
expect(message).toBe(`Could not load the provider list to check --model ${MODEL} (${detail})`)
45+
expect(message).not.toContain("Model not found")
46+
})
47+
}
48+
})

0 commit comments

Comments
 (0)