@devries-cameron20 / t3code-1 / commits / 502ce3cf65
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): settle OpenCode model catalog before caching it (#16448)

Co-authored-by: Ignacio Rasetti <ignaciorasetti@users.noreply.github.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>

Ignacio Rasetti committed Oct 19, 2026 at 15:47 UTC 502ce3cf65d962ce36ef9b5f928337a54f97b109
2 files changed +171 -18
packages/provider-opencode/src/server/status.test.ts
+132 -1
@@ -842,7 +842,9 @@ it.effect("keeps the last OpenCode 2 model list while a fresh server's stays emp
842 Effect.gen(function* () {
843 let listed: ReadonlyArray<OpenCode2Model> = [bigPickle];
844 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]);
848 listed = [];
849 const fiber = yield* load.pipe(Effect.forkChild);
850 yield* TestClock.adjust("6 seconds");
@@ -850,6 +852,135 @@ it.effect("keeps the last OpenCode 2 model list while a fresh server's stays emp
852 }).pipe(Effect.provide(TestClock.layer())),
853 );
854
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 +
984 const builtinCommands = [
985 { name: "init", description: "guided AGENTS.md setup" },
986 { name: "review", description: "review changes [commit|branch|pr], defaults to uncommitted" },
packages/provider-opencode/src/server/status.ts
+39 -17
@@ -396,28 +396,50 @@ function subProviderLabelFor(input: {
396 : label;
397 }
398
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. */
414 export const makeOpenCode2ModelLoader = <E>(
415 list: Effect.Effect<ReadonlyArray<OpenCode2Model>, E>,
416 ) =>
417 Effect.sync(() => {
418 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 + });
443 });
444
445 export interface OpenCode2Workspace {