Skip to content

Commit 502ce3c

Browse files
fix(server): settle OpenCode model catalog before caching it (pingdotgg#16448)
Co-authored-by: Ignacio Rasetti <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
1 parent 954d3bd commit 502ce3c

2 files changed

Lines changed: 171 additions & 18 deletions

File tree

‎packages/provider-opencode/src/server/status.test.ts‎

Lines changed: 132 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -842,14 +842,145 @@ it.effect("keeps the last OpenCode 2 model list while a fresh server's stays emp
842842
Effect.gen(function* () {
843843
let listed: ReadonlyArray<OpenCode2Model> = [bigPickle];
844844
const load = yield* makeOpenCode2ModelLoader(Effect.sync(() => listed));
845-
NodeAssert.deepEqual(yield* load, [bigPickle]);
845+
const first = yield* load.pipe(Effect.forkChild);
846+
yield* TestClock.adjust("1 second");
847+
NodeAssert.deepEqual(yield* Fiber.join(first), [bigPickle]);
846848
listed = [];
847849
const fiber = yield* load.pipe(Effect.forkChild);
848850
yield* TestClock.adjust("6 seconds");
849851
NodeAssert.deepEqual(yield* Fiber.join(fiber), [bigPickle]);
850852
}).pipe(Effect.provide(TestClock.layer())),
851853
);
852854

855+
it.effect("waits for a growing OpenCode 2 catalog to settle before caching it", () =>
856+
Effect.gen(function* () {
857+
const first: OpenCode2Model = {
858+
providerID: "opencode",
859+
id: "first",
860+
name: "First",
861+
variants: [],
862+
};
863+
const second: OpenCode2Model = {
864+
providerID: "opencode",
865+
id: "second",
866+
name: "Second",
867+
variants: [],
868+
};
869+
const replies: Array<ReadonlyArray<OpenCode2Model>> = [
870+
[first],
871+
[first, second],
872+
[first, second],
873+
];
874+
const load = yield* makeOpenCode2ModelLoader(
875+
Effect.sync(() => replies.shift() ?? [first, second]),
876+
);
877+
const fiber = yield* load.pipe(Effect.forkChild);
878+
yield* TestClock.adjust("1 second");
879+
NodeAssert.ok(fiber.pollUnsafe(), "A settled catalog should finish before the load deadline");
880+
NodeAssert.deepEqual(yield* Fiber.join(fiber), [first, second]);
881+
}).pipe(Effect.provide(TestClock.layer())),
882+
);
883+
884+
it.effect("waits for OpenCode 2 model names, provider labels, and variants to settle", () =>
885+
Effect.gen(function* () {
886+
const renamed = { ...bigPickle, name: "Bigger Pickle" };
887+
const labeled = { ...renamed, providerName: "OpenCode Zen" };
888+
const withVariants = { ...labeled, variants: [{ id: "low" }] };
889+
const complete = { ...labeled, variants: [{ id: "high" }, { id: "medium" }] };
890+
const reordered = { ...complete, variants: [{ id: "medium" }, { id: "high" }] };
891+
const replies = [[bigPickle], [renamed], [labeled], [withVariants], [complete], [reordered]];
892+
const load = yield* makeOpenCode2ModelLoader(Effect.sync(() => replies.shift() ?? [complete]));
893+
const fiber = yield* load.pipe(Effect.forkChild);
894+
yield* TestClock.adjust("1500 millis");
895+
NodeAssert.ok(
896+
fiber.pollUnsafe(),
897+
"Equivalent variant orders should settle before the deadline",
898+
);
899+
NodeAssert.deepEqual(yield* Fiber.join(fiber), [reordered]);
900+
}).pipe(Effect.provide(TestClock.layer())),
901+
);
902+
903+
it.effect("bounds an unsettled OpenCode 2 catalog and keeps its latest nonempty observation", () =>
904+
Effect.gen(function* () {
905+
let reads = 0;
906+
let latest: ReadonlyArray<OpenCode2Model> = [];
907+
const changing = () => {
908+
latest = [{ ...bigPickle, name: `Pickle ${++reads}` }];
909+
return latest;
910+
};
911+
let list = changing;
912+
const load = yield* makeOpenCode2ModelLoader(Effect.sync(() => list()));
913+
const cold = yield* load.pipe(Effect.forkChild);
914+
yield* TestClock.adjust("5 seconds");
915+
NodeAssert.ok(cold.pollUnsafe());
916+
NodeAssert.ok(reads > 1);
917+
NodeAssert.deepEqual(yield* Fiber.join(cold), latest);
918+
919+
list = () => [];
920+
const empty = yield* load.pipe(Effect.forkChild);
921+
yield* TestClock.adjust("5 seconds");
922+
NodeAssert.ok(empty.pollUnsafe());
923+
NodeAssert.deepEqual(yield* Fiber.join(empty), latest);
924+
925+
list = () => [bigPickle];
926+
const settled = yield* load.pipe(Effect.forkChild);
927+
yield* TestClock.adjust("1 second");
928+
NodeAssert.deepEqual(yield* Fiber.join(settled), [bigPickle]);
929+
930+
reads = 0;
931+
list = () => (reads++ === 0 ? [bigPickle] : changing());
932+
const fresh = yield* load.pipe(Effect.forkChild);
933+
yield* TestClock.adjust("5 seconds");
934+
NodeAssert.ok(fresh.pollUnsafe());
935+
NodeAssert.ok(reads > 1);
936+
NodeAssert.deepEqual(yield* Fiber.join(fresh), latest);
937+
}).pipe(Effect.provide(TestClock.layer())),
938+
);
939+
940+
it.effect(
941+
"refreshes a warm OpenCode 2 catalog when slow reads cannot settle before the deadline",
942+
() =>
943+
Effect.gen(function* () {
944+
let listed: ReadonlyArray<OpenCode2Model> = [bigPickle];
945+
let slow = false;
946+
const load = yield* makeOpenCode2ModelLoader(
947+
Effect.gen(function* () {
948+
if (slow) yield* Effect.sleep("3 seconds");
949+
return listed;
950+
}),
951+
);
952+
const warm = yield* load.pipe(Effect.forkChild);
953+
yield* TestClock.adjust("1 second");
954+
NodeAssert.deepEqual(yield* Fiber.join(warm), [bigPickle]);
955+
956+
listed = [{ ...bigPickle, name: "Fresh Pickle" }];
957+
slow = true;
958+
const fresh = yield* load.pipe(Effect.forkChild);
959+
yield* TestClock.adjust("5 seconds");
960+
NodeAssert.ok(fresh.pollUnsafe());
961+
NodeAssert.deepEqual(yield* Fiber.join(fresh), listed);
962+
}).pipe(Effect.provide(TestClock.layer())),
963+
);
964+
965+
it.effect(
966+
"keeps a fresh OpenCode 2 observation if confirmation fails but propagates an initial failure",
967+
() =>
968+
Effect.gen(function* () {
969+
const error = new OpenCodeRuntime.OpenCodeRuntimeError({
970+
operation: "model.list",
971+
detail: "Model list failed",
972+
});
973+
let reads = 0;
974+
const load = yield* makeOpenCode2ModelLoader(
975+
Effect.suspend(() => (reads++ === 0 ? Effect.succeed([bigPickle]) : Effect.fail(error))),
976+
);
977+
const fresh = yield* load.pipe(Effect.forkChild);
978+
yield* TestClock.adjust("250 millis");
979+
NodeAssert.deepEqual(yield* Fiber.join(fresh), [bigPickle]);
980+
NodeAssert.equal(yield* load.pipe(Effect.flip), error);
981+
}).pipe(Effect.provide(TestClock.layer())),
982+
);
983+
853984
const builtinCommands = [
854985
{ name: "init", description: "guided AGENTS.md setup" },
855986
{ name: "review", description: "review changes [commit|branch|pr], defaults to uncommitted" },

‎packages/provider-opencode/src/server/status.ts‎

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -396,28 +396,50 @@ function subProviderLabelFor(input: {
396396
: label;
397397
}
398398

399-
/**
400-
* A server loads its catalog lazily and lists nothing for its first few
401-
* hundred milliseconds, and a local instance starts a fresh server for each
402-
* status check. So an empty list is read again for a while, and the last
403-
* non-empty list stands in if the catalog still has not loaded.
404-
*/
399+
const catalogFingerprint = (models: ReadonlyArray<OpenCode2Model>) =>
400+
models
401+
.map((model) =>
402+
JSON.stringify([
403+
model.providerID,
404+
model.id,
405+
model.name,
406+
model.providerName,
407+
model.variants.map((variant) => variant.id).toSorted(),
408+
]),
409+
)
410+
.toSorted()
411+
.join("\n");
412+
413+
/** A cold server can list a partial catalog; two agreeing reads reduce that startup race. */
405414
export const makeOpenCode2ModelLoader = <E>(
406415
list: Effect.Effect<ReadonlyArray<OpenCode2Model>, E>,
407416
) =>
408417
Effect.sync(() => {
409418
let lastLoaded: ReadonlyArray<OpenCode2Model> = [];
410-
return list.pipe(
411-
Effect.repeat({
412-
until: (models) => models.length > 0,
413-
schedule: Schedule.spaced("250 millis"),
414-
}),
415-
Effect.timeoutOption("5 seconds"),
416-
Effect.map((loaded) => {
417-
if (loaded._tag === "Some" && loaded.value.length > 0) lastLoaded = loaded.value;
418-
return lastLoaded;
419-
}),
420-
);
419+
return Effect.suspend(() => {
420+
let previous: string | undefined;
421+
let latest: ReadonlyArray<OpenCode2Model> = [];
422+
return list.pipe(
423+
Effect.repeat({
424+
until: (models) => {
425+
if (models.length > 0) latest = models;
426+
const fingerprint = models.length > 0 ? catalogFingerprint(models) : undefined;
427+
const settled = fingerprint !== undefined && fingerprint === previous;
428+
previous = fingerprint;
429+
return settled;
430+
},
431+
schedule: Schedule.spaced("250 millis"),
432+
}),
433+
Effect.catch((error) => (latest.length > 0 ? Effect.succeed(latest) : Effect.fail(error))),
434+
Effect.timeoutOption("5 seconds"),
435+
Effect.map((loaded) => {
436+
// Fresh progress may still be partial, but a warm cache must not freeze slow catalogs.
437+
if (loaded._tag === "Some") lastLoaded = loaded.value;
438+
else if (latest.length > 0) lastLoaded = latest;
439+
return lastLoaded;
440+
}),
441+
);
442+
});
421443
});
422444

423445
export interface OpenCode2Workspace {

0 commit comments

Comments
 (0)