Skip to content

Commit 9eeeed0

Browse files
committed
fix(server): OpenCode model names no longer stale on startup
A freshly spawned OpenCode 2 server answers model.list with default model names until it applies the instance's configured ones, so the first status check cached pre-config names and nothing re-checked. Spawned servers are now lent only after the directory's model.updated event settles the catalog, with a timeout fallback.
1 parent 194c73f commit 9eeeed0

2 files changed

Lines changed: 147 additions & 0 deletions

File tree

‎apps/server/src/provider/opencode2/OpenCode2Server.test.ts‎

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import * as FileSystem from "effect/FileSystem";
1111
import * as Layer from "effect/Layer";
1212
import * as Path from "effect/Path";
1313
import * as Redacted from "effect/Redacted";
14+
import * as Stream from "effect/Stream";
1415
import * as TestClock from "effect/testing/TestClock";
1516
import { FetchHttpClient, HttpClient, HttpClientResponse } from "effect/http";
1617
import { describe } from "vite-plus/test";
@@ -165,6 +166,104 @@ describe("OpenCode2Server error details", () => {
165166
);
166167
});
167168

169+
describe("OpenCode2Server.settleModelCatalog", () => {
170+
const catalogEvent = (type: string, directory: string) =>
171+
({ type, location: { directory } }) as unknown as OpenCode2Client.OpenCode2StreamEvent;
172+
173+
const settle = <StreamE, EventsE>(input: {
174+
readonly reads: Array<string>;
175+
readonly read?: Effect.Effect<void>;
176+
readonly events: Effect.Effect<
177+
Stream.Stream<OpenCode2Client.OpenCode2StreamEvent, StreamE>,
178+
EventsE
179+
>;
180+
}) =>
181+
OpenCode2Server.settleModelCatalog({
182+
directory: "/project",
183+
read:
184+
input.read ??
185+
Effect.sync(() => {
186+
input.reads.push("read");
187+
}),
188+
events: input.events,
189+
});
190+
191+
it.effect("reads once and settles on the directory's model.updated", () =>
192+
Effect.gen(function* () {
193+
const reads: Array<string> = [];
194+
const settling = yield* settle({
195+
reads,
196+
events: Effect.succeed(
197+
Stream.make(
198+
catalogEvent("model.updated", "/elsewhere"),
199+
catalogEvent("provider.updated", "/project"),
200+
catalogEvent("model.updated", "/project"),
201+
).pipe(Stream.concat(Stream.never)),
202+
),
203+
}).pipe(Effect.forkChild);
204+
for (let i = 0; i < 20; i++) yield* Effect.yieldNow;
205+
// Settling on the matching event; waiting for the stream to end would hang.
206+
assert.strictEqual(settling.pollUnsafe()?._tag, "Success");
207+
assert.deepStrictEqual(reads, ["read"]);
208+
}).pipe(Effect.provide(TestClock.layer())),
209+
);
210+
211+
it.effect("stops waiting after the timeout when no matching event arrives", () =>
212+
Effect.gen(function* () {
213+
const reads: Array<string> = [];
214+
const settling = yield* settle({
215+
reads,
216+
events: Effect.succeed(
217+
Stream.make(catalogEvent("model.updated", "/elsewhere")).pipe(
218+
Stream.concat(Stream.never),
219+
),
220+
),
221+
}).pipe(Effect.forkChild);
222+
for (let i = 0; i < 20; i++) yield* Effect.yieldNow;
223+
assert.isUndefined(settling.pollUnsafe());
224+
yield* TestClock.adjust("5 seconds");
225+
assert.strictEqual(settling.pollUnsafe()?._tag, "Success");
226+
assert.deepStrictEqual(reads, ["read"]);
227+
}).pipe(Effect.provide(TestClock.layer())),
228+
);
229+
230+
it.effect("shares one deadline between the read and the wait", () =>
231+
Effect.gen(function* () {
232+
const reads: Array<string> = [];
233+
const settling = yield* settle({
234+
reads,
235+
read: Effect.sleep("4 seconds").pipe(
236+
Effect.andThen(
237+
Effect.sync(() => {
238+
reads.push("read");
239+
}),
240+
),
241+
),
242+
events: Effect.succeed(Stream.never),
243+
}).pipe(Effect.forkChild);
244+
for (let i = 0; i < 20; i++) yield* Effect.yieldNow;
245+
yield* TestClock.adjust("4 seconds");
246+
// The read is done and the wait is running; both share one five-second budget.
247+
assert.isUndefined(settling.pollUnsafe());
248+
yield* TestClock.adjust("1 second");
249+
assert.strictEqual(settling.pollUnsafe()?._tag, "Success");
250+
assert.deepStrictEqual(reads, ["read"]);
251+
}).pipe(Effect.provide(TestClock.layer())),
252+
);
253+
254+
it.effect("lends the server when the event stream is unavailable", () =>
255+
Effect.gen(function* () {
256+
const reads: Array<string> = [];
257+
const settling = yield* settle({ reads, events: Effect.fail("no event stream") }).pipe(
258+
Effect.forkChild,
259+
);
260+
for (let i = 0; i < 20; i++) yield* Effect.yieldNow;
261+
assert.strictEqual(settling.pollUnsafe()?._tag, "Success");
262+
assert.deepStrictEqual(reads, ["read"]);
263+
}).pipe(Effect.provide(TestClock.layer())),
264+
);
265+
});
266+
168267
describe("OpenCode2Server passwords", () => {
169268
it.effect("generates a distinct 256-bit password per server", () =>
170269
Effect.gen(function* () {

‎apps/server/src/provider/opencode2/OpenCode2Server.ts‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import * as Base64Url from "effect/encoding/Base64Url";
1313
import * as Layer from "effect/Layer";
1414
import * as P from "effect/Predicate";
1515
import * as Redacted from "effect/Redacted";
16+
import * as Stream from "effect/Stream";
1617
import * as HttpClientError from "effect/http/HttpClientError";
1718

1819
import { OpenCodeRuntimeError } from "../opencodeRuntime.ts";
@@ -134,6 +135,43 @@ export const verifyServer = (client: OpenCodeClient) =>
134135
Effect.map((info) => info.version),
135136
);
136137

138+
/** How long a spawned server has to settle its model catalog before it is lent out anyway. */
139+
const CATALOG_SETTLE_TIMEOUT = "5 seconds";
140+
141+
/**
142+
* A freshly spawned OpenCode 2 server builds its model catalog lazily, and
143+
* until the build finishes it answers `model.list` with default names instead
144+
* of the instance's configured ones. The directory's `model.updated` event
145+
* marks the settled catalog, so subscribe, read once to start the build, and
146+
* wait for that event. A server that cannot be subscribed to, that fails the
147+
* read, or that never emits the event is still lent once the timeout passes.
148+
*/
149+
export const settleModelCatalog = <ReadA, ReadE, StreamE, EventsE>(input: {
150+
readonly directory: string;
151+
readonly read: Effect.Effect<ReadA, ReadE>;
152+
readonly events: Effect.Effect<
153+
Stream.Stream<OpenCode2Client.OpenCode2StreamEvent, StreamE>,
154+
EventsE
155+
>;
156+
}): Effect.Effect<void> =>
157+
Effect.gen(function* () {
158+
const events = yield* input.events.pipe(Effect.option);
159+
const read = yield* input.read.pipe(Effect.option);
160+
if (events._tag === "None" || read._tag === "None") return;
161+
yield* events.value.pipe(
162+
Stream.filter(
163+
(event) =>
164+
"location" in event &&
165+
event.type === "model.updated" &&
166+
event.location?.directory === input.directory,
167+
),
168+
Stream.take(1),
169+
Stream.runDrain,
170+
Effect.ignore,
171+
Effect.asVoid,
172+
);
173+
}).pipe(Effect.timeoutOption(CATALOG_SETTLE_TIMEOUT), Effect.ignore, Effect.asVoid);
174+
137175
/**
138176
* One server per provider instance. With a `serverUrl` it connects to that
139177
* server with the configured password; otherwise it spawns `binaryPath serve`
@@ -153,6 +191,16 @@ export const make = Effect.fn("OpenCode2Server.make")(function* (input: {
153191
Effect.gen(function* () {
154192
const api = yield* opencode.connect({ baseUrl: url, password });
155193
const version = yield* verifyServer(api.client);
194+
// A server T3 spawned is lent only after its catalog reflects the
195+
// instance's configured model names; an external one was not started
196+
// here, and waiting on its event stream would only cost latency.
197+
if (!external) {
198+
yield* settleModelCatalog({
199+
directory: input.directory,
200+
read: api.client.model.list({ location: { directory: input.directory } }),
201+
events: api.events,
202+
});
203+
}
156204
return { ...api, url, version, external } satisfies OpenCode2Connection;
157205
});
158206
let latest: OpenCode2Connection | undefined;

0 commit comments

Comments
 (0)