Skip to content

Commit 424bf70

Browse files
fix(server): settle OpenCode model catalog before caching it
The first non-empty model.list from a freshly spawned OpenCode 2 server can be partial. Wait for two consecutive agreeing reads before caching so cold starts keep the full catalog. Closes #15994.
1 parent 9bd1d80 commit 424bf70

2 files changed

Lines changed: 81 additions & 12 deletions

File tree

‎apps/server/src/provider/OpenCodeProvider.test.ts‎

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -797,14 +797,56 @@ it.effect("keeps the last OpenCode 2 model list while a fresh server's stays emp
797797
Effect.gen(function* () {
798798
let listed: ReadonlyArray<OpenCode2Model> = [bigPickle];
799799
const load = yield* makeOpenCode2ModelLoader(Effect.sync(() => listed));
800-
NodeAssert.deepEqual(yield* load, [bigPickle]);
800+
// Settling takes two agreeing reads, so the first load needs the clock too.
801+
const first = yield* load.pipe(Effect.forkChild);
802+
yield* TestClock.adjust("1 second");
803+
NodeAssert.deepEqual(yield* Fiber.join(first), [bigPickle]);
801804
listed = [];
802805
const fiber = yield* load.pipe(Effect.forkChild);
803806
yield* TestClock.adjust("6 seconds");
804807
NodeAssert.deepEqual(yield* Fiber.join(fiber), [bigPickle]);
805808
}).pipe(Effect.provide(TestClock.layer())),
806809
);
807810

811+
it.effect("waits for a growing OpenCode 2 catalog to settle before caching it", () =>
812+
Effect.gen(function* () {
813+
// A cold server lists part of its catalog first (here the second row
814+
// arrives one poll later). The loader must return the settled list, not
815+
// the first non-empty one, or the registry caches the partial list and
816+
// the picker marks the saved model Unavailable (#15994).
817+
const first: OpenCode2Model = {
818+
providerID: "opencode",
819+
id: "first",
820+
name: "First",
821+
variants: [],
822+
};
823+
const second: OpenCode2Model = {
824+
providerID: "opencode",
825+
id: "second",
826+
name: "Second",
827+
variants: [],
828+
};
829+
const replies: Array<ReadonlyArray<OpenCode2Model>> = [
830+
[first],
831+
[first, second],
832+
[first, second],
833+
];
834+
let reads = 0;
835+
const load = yield* makeOpenCode2ModelLoader(
836+
Effect.sync(() => {
837+
reads += 1;
838+
return replies.shift() ?? [first, second];
839+
}),
840+
);
841+
const fiber = yield* load.pipe(Effect.forkChild);
842+
yield* TestClock.adjust("5 seconds");
843+
NodeAssert.deepEqual(yield* Fiber.join(fiber), [first, second]);
844+
// Settling takes exactly three reads here; a loader that never settles
845+
// would poll ~20 times before the deadline instead.
846+
NodeAssert.equal(reads, 3);
847+
}).pipe(Effect.provide(TestClock.layer())),
848+
);
849+
808850
// What 2.0.18 lists for a directory once it has scanned it (live, 2026-09-29).
809851
const builtinCommands = [
810852
{ name: "init", description: "guided AGENTS.md setup" },

‎apps/server/src/provider/OpenCodeProvider.ts‎

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -386,27 +386,54 @@ export interface OpenCode2Model {
386386
readonly variants: ReadonlyArray<{ readonly id: string }>;
387387
}
388388

389+
const catalogFingerprint = (models: ReadonlyArray<OpenCode2Model>): string =>
390+
models
391+
.map((model) => `${model.providerID}/${model.id}\0${model.name}`)
392+
.toSorted()
393+
.join("\n");
394+
389395
/**
390396
* A server loads its catalog lazily and lists nothing for its first few
391397
* hundred milliseconds, and a local instance starts a fresh server for each
392-
* status check. So an empty list is read again for a while, and the last
393-
* non-empty list stands in if the catalog still has not loaded.
398+
* status check. Worse, the first non-empty list can still be partial: models
399+
* whose provider activates later are missing, and the registry then caches
400+
* that partial list with nothing scheduling another probe. So an empty list
401+
* is read again for a while, a non-empty list is only accepted once two
402+
* consecutive reads agree, and the last non-empty list stands in if the
403+
* catalog still has not settled.
394404
*/
395405
export const makeOpenCode2ModelLoader = <E>(
396406
list: Effect.Effect<ReadonlyArray<OpenCode2Model>, E>,
397407
) =>
398408
Effect.sync(() => {
399409
let lastLoaded: ReadonlyArray<OpenCode2Model> = [];
400-
return list.pipe(
401-
Effect.repeat({
402-
until: (models) => models.length > 0,
403-
schedule: Schedule.spaced("250 millis"),
404-
}),
410+
// Agreement state resets on every probe: a first read that merely repeats
411+
// the previous probe's list must not count as settled. `lastLoaded` stays
412+
// per loader so a timed-out probe still falls back to the last good list.
413+
const run = Effect.suspend(() => {
414+
let previous: ReadonlyArray<OpenCode2Model> | undefined;
415+
return list.pipe(
416+
Effect.tap((models) =>
417+
Effect.sync(() => {
418+
if (models.length > 0) lastLoaded = models;
419+
}),
420+
),
421+
Effect.repeat({
422+
until: (models) => {
423+
const settled =
424+
models.length > 0 &&
425+
previous !== undefined &&
426+
catalogFingerprint(previous) === catalogFingerprint(models);
427+
previous = models;
428+
return settled;
429+
},
430+
schedule: Schedule.spaced("250 millis"),
431+
}),
432+
);
433+
});
434+
return run.pipe(
405435
Effect.timeoutOption("5 seconds"),
406-
Effect.map((loaded) => {
407-
if (loaded._tag === "Some" && loaded.value.length > 0) lastLoaded = loaded.value;
408-
return lastLoaded;
409-
}),
436+
Effect.map(() => lastLoaded),
410437
);
411438
});
412439

0 commit comments

Comments
 (0)