The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 17:15 UTC
fix(opencode): models load once OpenCode 2 finishes activating its plugins (#30263)
Julius Marminge committed
Nov 13, 2026 at 09:41 UTC
a877d40b74797f006d102684d0a8106432be2a5a
3 files changed
+81
-38
packages/provider-opencode/src/server/openCode2Catalog.test.ts
+56
-24
@@ -1,4 +1,5 @@
1
import { assert, it } from "@effect/vitest";
2
+import * as Clock from "effect/Clock";
3
import * as Effect from "effect/Effect";
4
import * as Fiber from "effect/Fiber";
5
import * as Layer from "effect/Layer";
@@ -7,40 +8,48 @@ import { HttpClient, HttpClientResponse } from "effect/http";
8
import { describe } from "vite-plus/test";
9
10
import { loadOpenCode2Catalog } from "./openCode2Catalog.ts";
11
+import { makeOpenCode2ModelLoader } from "./status.ts";
12
import * as OpenCode2Client from "./v2/OpenCode2Client.ts";
13
14
const MODELS_BODY = `{"location":{"directory":"/project"},"data":[{"id":"step-5-preview-free","modelID":"step-5-preview-free","providerID":"opencode-go","name":"Step 5 Preview Free","compatibility":{"reasoningField":"reasoning_content"},"package":"@opencode/ai/providers/openai-compatible","settings":{"apiKey":"sk-demo-not-real","baseURL":"http://127.0.0.1:48990/v1","provider":"opencode-go"},"capabilities":{"tools":true,"input":["text","image","video"],"output":["text"]},"variants":[{"id":"low","settings":{"reasoningEffort":"low"}},{"id":"medium","settings":{"reasoningEffort":"medium"}},{"id":"high","settings":{"reasoningEffort":"high"}}],"time":{"released":1789516800000},"cost":[{"input":0,"output":0,"cache":{"read":0,"write":0}}],"status":"active","enabled":true,"limit":{"context":1000000,"input":1000000,"output":65536}},{"id":"step-5-preview-free","modelID":"step-5-preview-free","providerID":"opencode","name":"Step 5 Preview Free","compatibility":{"reasoningField":"reasoning_content"},"package":"@opencode/ai/providers/openai-compatible","settings":{"apiKey":"public","baseURL":"https://opencode.ai/zen/v1","provider":"opencode"},"capabilities":{"tools":true,"input":["text","image","video"],"output":["text"]},"variants":[{"id":"low","settings":{"reasoningEffort":"low"}},{"id":"medium","settings":{"reasoningEffort":"medium"}},{"id":"high","settings":{"reasoningEffort":"high"}}],"time":{"released":1789516800000},"cost":[{"input":0,"output":0,"cache":{"read":0,"write":0}}],"status":"active","enabled":true,"limit":{"context":1000000,"input":1000000,"output":65536}}]}`;
15
16
const PROVIDERS_BODY = `{"location":{"directory":"/project"},"data":[{"id":"opencode-go","integrationID":"opencode-go","name":"OpenCode Go","activation":"enabled","package":"@opencode/ai/providers/openai-compatible","settings":{"apiKey":"sk-demo-not-real","baseURL":"http://127.0.0.1:48990/v1","provider":"opencode-go"}},{"id":"opencode","integrationID":"opencode","name":"OpenCode Zen","activation":"enabled","package":"@opencode/ai/providers/openai-compatible","settings":{"apiKey":"public","baseURL":"https://opencode.ai/zen/v1"}}]}`;
17
18
+const PLUGINS_ACTIVATING_BODY =
19
+ '{"_tag":"ServiceUnavailableError","message":"Location plugins did not finish activating","service":"plugin"}';
20
+
21
+type Reply = string | { readonly status: number; readonly body: string };
22
+
23
const layerServerReplying = (
17
- bodies: Readonly<Record<string, string>>,
24
+ bodies: Readonly<Record<string, Reply | Effect.Effect<Reply>>>,
25
hanging: ReadonlyArray<string> = [],
26
) =>
27
Layer.succeed(
28
HttpClient.HttpClient,
29
HttpClient.make((request) =>
23
- Effect.suspend(() => {
30
+ Effect.gen(function* () {
31
const path = new URL(request.url).pathname;
32
if (hanging.includes(path)) {
26
- return Effect.never;
33
+ return yield* Effect.never;
34
}
28
- const body = bodies[path];
29
- return Effect.succeed(
30
- HttpClientResponse.fromWeb(
31
- request,
32
- new Response(body ?? '{"_tag":"NotFoundError","message":"not recorded"}', {
33
- status: body === undefined ? 404 : 200,
34
- headers: { "content-type": "application/json" },
35
- }),
36
- ),
35
+ const recorded = bodies[path];
36
+ const reply = Effect.isEffect(recorded) ? yield* recorded : recorded;
37
+ const { status, body } =
38
+ reply === undefined
39
+ ? { status: 404, body: '{"_tag":"NotFoundError","message":"not recorded"}' }
40
+ : typeof reply === "string"
41
+ ? { status: 200, body: reply }
42
+ : reply;
43
+ return HttpClientResponse.fromWeb(
44
+ request,
45
+ new Response(body, { status, headers: { "content-type": "application/json" } }),
46
);
47
}),
48
),
49
);
50
51
const readCatalog = (
43
- bodies: Readonly<Record<string, string>>,
52
+ bodies: Readonly<Record<string, Reply | Effect.Effect<Reply>>>,
53
hanging: ReadonlyArray<string> = [],
54
) =>
55
Effect.gen(function* () {
@@ -86,17 +95,6 @@ describe("loadOpenCode2Catalog", () => {
95
}),
96
);
97
89
- it.effect("lists no models yet when the model list does not answer", () =>
90
- Effect.gen(function* () {
91
- const fiber = yield* Effect.forkChild(
92
- readCatalog({ "/api/provider": PROVIDERS_BODY }, ["/api/model"]),
93
- );
94
- yield* TestClock.adjust("5 seconds");
95
-
96
- assert.deepStrictEqual(yield* Fiber.join(fiber), []);
97
- }),
98
- );
99
-
98
// regression: #17424 only the provider list is optional; a model-list failure fails the read
99
it.effect("fails the read when the model list fails", () =>
100
Effect.gen(function* () {
@@ -109,4 +107,38 @@ describe("loadOpenCode2Catalog", () => {
107
assert.match(error.detail, /could not list its models/i);
108
}),
109
);
110
+
111
+ // regression: #30251 a busy cold OpenCode 2 holds each model read past 5 seconds, then 503s
112
+ it.effect("lists models once OpenCode 2 finishes activating its plugins", () =>
113
+ Effect.gen(function* () {
114
+ const activatedAt = (yield* Clock.currentTimeMillis) + 60_000;
115
+ // Timings measured from OpenCode 2.2.7 on a CPU-throttled cold start.
116
+ const listModels = Effect.gen(function* () {
117
+ if ((yield* Clock.currentTimeMillis) >= activatedAt) {
118
+ return yield* Effect.as(Effect.sleep("9 seconds"), MODELS_BODY);
119
+ }
120
+ yield* Effect.sleep("14 seconds");
121
+ return { status: 503, body: PLUGINS_ACTIVATING_BODY };
122
+ });
123
+ const load = yield* makeOpenCode2ModelLoader(
124
+ readCatalog({ "/api/model": listModels, "/api/provider": PROVIDERS_BODY }),
125
+ );
126
+ const fiber = yield* Effect.forkChild(load);
127
+ yield* TestClock.adjust("2 minutes");
128
+
129
+ assert.strictEqual((yield* Fiber.join(fiber)).length, 2);
130
+ }),
131
+ );
132
+
133
+ it.effect("lists no models once a model list that never answers reaches the deadline", () =>
134
+ Effect.gen(function* () {
135
+ const load = yield* makeOpenCode2ModelLoader(
136
+ readCatalog({ "/api/provider": PROVIDERS_BODY }, ["/api/model"]),
137
+ );
138
+ const fiber = yield* Effect.forkChild(load);
139
+ yield* TestClock.adjust("2 minutes");
140
+
141
+ assert.deepStrictEqual(yield* Fiber.join(fiber), []);
142
+ }),
143
+ );
144
});
packages/provider-opencode/src/server/openCode2Catalog.ts
+2
-6
@@ -6,8 +6,6 @@ import type { OpenCode2Model } from "./status.ts";
6
7
// Cold catalogs take seconds; model reads already retry empty catalogs.
8
const PROVIDER_NAME_TIMEOUT = "3 seconds";
9
-// Bounds the request alone (server start has its own bounds); a slow list reads as not listed yet.
10
-const MODEL_LIST_TIMEOUT = "5 seconds";
9
10
const listProviders = (client: OpenCodeClient, directory: string) =>
11
client.provider.list({ location: { directory } }).pipe(
@@ -22,10 +20,8 @@ const readCatalog = <E>(
20
): Effect.Effect<ReadonlyArray<OpenCode2Model>, OpenCodeRuntime.OpenCodeRuntimeError> =>
21
Effect.all(
22
{
25
- models: client.model.list({ location: { directory } }).pipe(
26
- Effect.timeoutOption(MODEL_LIST_TIMEOUT),
27
- Effect.map((list) => (list._tag === "Some" ? list.value.data : [])),
28
- ),
23
+ // Callers bound the read: a busy host can hold it past OpenCode's own activation wait.
24
+ models: client.model.list({ location: { directory } }).pipe(Effect.map((list) => list.data)),
25
providers,
26
},
27
{ concurrency: "unbounded" },
packages/provider-opencode/src/server/status.ts
+23
-8
@@ -26,6 +26,7 @@ import {
26
import * as OpenCodeRuntime from "./OpenCodeRuntime.ts";
27
import type { ProbedOpenCode } from "./versionProbe.ts";
28
import type { Agent, ProviderListResponse } from "@opencode-ai/sdk/v2";
29
+import { ServiceUnavailableError } from "@opencode/protocol/errors";
30
import * as OpenCodeServerOwner from "./OpenCodeServerOwner.ts";
31
32
const OPENCODE_PRESENTATION = {
@@ -498,6 +499,15 @@ const catalogFingerprint = (models: ReadonlyArray<OpenCode2Model>) =>
499
.toSorted()
500
.join("\n");
501
502
+// A cold OpenCode 2.2.5 503s model reads for about a minute while its location plugins activate.
503
+const PLUGIN_ACTIVATION_DEADLINE = "2 minutes";
504
+
505
+const isServiceUnavailable = Schema.is(ServiceUnavailableError);
506
+const isActivatingPlugins = (error: unknown): boolean =>
507
+ OpenCodeRuntime.isOpenCodeRuntimeError(error)
508
+ ? isActivatingPlugins(error.cause)
509
+ : isServiceUnavailable(error) && error.service === "plugin";
510
+
511
/** A cold server can list a partial catalog; two agreeing reads reduce that startup race. */
512
const settleOpenCode2Catalog = <E>(
513
list: Effect.Effect<ReadonlyArray<OpenCode2Model>, E>,
@@ -536,17 +546,22 @@ export const makeOpenCode2ModelLoader = <E>(
546
),
547
);
548
// The first read can include a cold server start, so only settling shares the budget.
549
+ // Each retry borrows the managed server, so retries well inside its idle TTL keep it alive.
550
return tracked.pipe(
551
+ Effect.retry({ while: isActivatingPlugins, schedule: Schedule.spaced("1 second") }),
552
+ Effect.timeoutOption(PLUGIN_ACTIVATION_DEADLINE),
553
Effect.flatMap((first) =>
541
- settleOpenCode2Catalog(tracked, first).pipe(
542
- Effect.timeoutOption(
543
- resolveProviderProbeTimeoutMs(
544
- "opencode",
545
- OPENCODE_2_CATALOG_SETTLE_TIMEOUT_MS,
546
- environment,
554
+ Option.isNone(first)
555
+ ? Effect.succeedNone
556
+ : settleOpenCode2Catalog(tracked, first.value).pipe(
557
+ Effect.timeoutOption(
558
+ resolveProviderProbeTimeoutMs(
559
+ "opencode",
560
+ OPENCODE_2_CATALOG_SETTLE_TIMEOUT_MS,
561
+ environment,
562
+ ),
563
+ ),
564
),
548
- ),
549
- ),
565
),
566
Effect.catch((error) =>
567
latest.length > 0 ? Effect.succeedSome(latest) : Effect.fail(error),