Skip to content

Commit bb7cb38

Browse files
committed
fix(opencode): drop retained models when a rebuild repoints the model source
1 parent 232cf28 commit bb7cb38

4 files changed

Lines changed: 180 additions & 1 deletion

File tree

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

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1929,6 +1929,156 @@ it.layer(
19291929
}),
19301930
);
19311931

1932+
it.effect("drops retained models when a rebuild points at a different model source", () =>
1933+
Effect.gen(function* () {
1934+
const driver = ProviderDriverKind.make("opencode");
1935+
const instanceId = ProviderInstanceId.make("opencode");
1936+
const readyProvider = {
1937+
instanceId,
1938+
driver,
1939+
status: "ready",
1940+
enabled: true,
1941+
installed: true,
1942+
auth: { status: "authenticated" },
1943+
checkedAt: "2026-06-10T00:00:00.000Z",
1944+
version: "2.0.0",
1945+
models: [
1946+
{
1947+
slug: "opencode/big-pickle",
1948+
name: "Big Pickle",
1949+
subProvider: "OpenCode",
1950+
isCustom: false,
1951+
capabilities: null,
1952+
},
1953+
],
1954+
slashCommands: [],
1955+
skills: [],
1956+
} as const satisfies ServerProvider;
1957+
// A catalog still loading reports a warning and no models. Retention keeps
1958+
// the previous models while it is pending, which is only correct when the
1959+
// pending catalog comes from the same server.
1960+
const loadingProvider = {
1961+
...readyProvider,
1962+
status: "warning",
1963+
auth: { status: "unknown" },
1964+
checkedAt: "2026-06-10T00:01:00.000Z",
1965+
models: [],
1966+
message: "OpenCode 2 is still loading its model catalog.",
1967+
} satisfies ServerProvider;
1968+
const registryChanges = yield* PubSub.unbounded<void>();
1969+
const makeInstance = (
1970+
provider: ServerProvider,
1971+
modelSourceIdentity: string,
1972+
): ProviderInstance => ({
1973+
instanceId,
1974+
driverKind: driver,
1975+
continuationIdentity: {
1976+
driverKind: driver,
1977+
continuationKey: "opencode:instance:opencode",
1978+
},
1979+
modelSourceIdentity,
1980+
displayName: undefined,
1981+
enabled: true,
1982+
snapshot: {
1983+
resolveMaintenance: () =>
1984+
Effect.succeed(
1985+
makeManualOnlyProviderMaintenanceCapabilities({
1986+
provider: driver,
1987+
packageName: null,
1988+
}),
1989+
),
1990+
getSnapshot: Effect.succeed(provider),
1991+
refresh: Effect.succeed(provider),
1992+
streamChanges: Stream.empty,
1993+
applyUsageLimits: () => Effect.void,
1994+
},
1995+
orchestrationAdapter: {} as ProviderInstance["orchestrationAdapter"],
1996+
textGeneration: {} as ProviderInstance["textGeneration"],
1997+
});
1998+
const instancesRef = yield* Ref.make<ReadonlyArray<ProviderInstance>>([
1999+
makeInstance(readyProvider, "http://127.0.0.1:4096"),
2000+
]);
2001+
const layerInstanceRegistry = Layer.succeed(
2002+
ProviderInstanceRegistry.ProviderInstanceRegistry,
2003+
{
2004+
getInstance: (requestedId) =>
2005+
Ref.get(instancesRef).pipe(
2006+
Effect.map((instances) =>
2007+
instances.find((instance) => instance.instanceId === requestedId),
2008+
),
2009+
),
2010+
listInstances: Ref.get(instancesRef),
2011+
listUnavailable: Effect.succeed([]),
2012+
streamChanges: Stream.fromPubSub(registryChanges),
2013+
subscribeChanges: PubSub.subscribe(registryChanges),
2014+
},
2015+
);
2016+
const scope = yield* Scope.make();
2017+
yield* Effect.addFinalizer(() => Scope.close(scope, Exit.void));
2018+
const runtimeServices = yield* Layer.build(
2019+
ProviderRegistry.layer.pipe(
2020+
Layer.provideMerge(layerInstanceRegistry),
2021+
Layer.provideMerge(
2022+
ServerConfig.layerTest(process.cwd(), {
2023+
prefix: "t3-provider-registry-model-source-",
2024+
}),
2025+
),
2026+
Layer.provideMerge(NodeServices.layer),
2027+
),
2028+
).pipe(Scope.provide(scope));
2029+
2030+
yield* Effect.gen(function* () {
2031+
const registry = yield* ProviderRegistry.ProviderRegistry;
2032+
const settleOn = (expectedCheckedAt: string) =>
2033+
Effect.gen(function* () {
2034+
let current = yield* registry.getProviders;
2035+
for (
2036+
let attempt = 0;
2037+
attempt < 5000 && current[0]?.checkedAt !== expectedCheckedAt;
2038+
attempt += 1
2039+
) {
2040+
// `it.effect` runs on a test clock, so this must not sleep:
2041+
// real-time sleeps never advance here.
2042+
yield* Effect.yieldNow;
2043+
current = yield* registry.getProviders;
2044+
}
2045+
assert.strictEqual(
2046+
current[0]?.checkedAt,
2047+
expectedCheckedAt,
2048+
"the rebuilt instance was never published",
2049+
);
2050+
return current;
2051+
});
2052+
2053+
const publishRebuild = (instance: ProviderInstance) =>
2054+
Effect.gen(function* () {
2055+
yield* Ref.set(instancesRef, [instance]);
2056+
yield* PubSub.publish(registryChanges, undefined);
2057+
});
2058+
2059+
yield* publishRebuild(makeInstance(readyProvider, "http://127.0.0.1:4096"));
2060+
const published = yield* settleOn(readyProvider.checkedAt);
2061+
assert.deepStrictEqual(published[0]?.models, readyProvider.models);
2062+
2063+
// Same server, still loading: the pending catalog is a continuation of
2064+
// the one already listed, so its models stay visible.
2065+
yield* publishRebuild(makeInstance(loadingProvider, "http://127.0.0.1:4096"));
2066+
const sameSource = yield* settleOn(loadingProvider.checkedAt);
2067+
assert.deepStrictEqual(sameSource[0]?.models, readyProvider.models);
2068+
2069+
// Different server, still loading: the pending catalog belongs to a
2070+
// server whose models were never listed, so the old ones must go.
2071+
const otherSourceProvider = {
2072+
...loadingProvider,
2073+
checkedAt: "2026-06-10T00:02:00.000Z",
2074+
} satisfies ServerProvider;
2075+
yield* publishRebuild(makeInstance(otherSourceProvider, "http://127.0.0.1:5099"));
2076+
const otherSource = yield* settleOn(otherSourceProvider.checkedAt);
2077+
assert.deepStrictEqual(otherSource[0]?.models, []);
2078+
}).pipe(Effect.provide(runtimeServices));
2079+
}),
2080+
);
2081+
19322082
it.effect("refreshes OpenCode catalogs and preserves other providers", () =>
19332083
Effect.gen(function* () {
19342084
const codexDriver = ProviderDriverKind.make("codex");

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -738,13 +738,31 @@ export const layer = Layer.effect(
738738
.filter((instanceId) => previousSubs.has(instanceId)),
739739
);
740740
if (rebuiltInstanceIds.size > 0) {
741+
// A rebuild keeps the instance id, so its models may come from a
742+
// different server than the ones being carried over. Retention can only
743+
// hold them while it still knows which server listed them.
744+
const repointedInstanceIds = new Set(
745+
newlyAdded
746+
.filter(([instanceId]) => rebuiltInstanceIds.has(instanceId))
747+
.filter(([instanceId, instance]) => {
748+
const previousSource = previousSubs.get(instanceId)?.modelSourceIdentity;
749+
return (
750+
previousSource !== undefined &&
751+
instance.modelSourceIdentity !== undefined &&
752+
previousSource !== instance.modelSourceIdentity
753+
);
754+
})
755+
.map(([instanceId]) => instanceId),
756+
);
741757
const [previousProviders, providers] = yield* Ref.modify(
742758
providersRef,
743759
(previousProviders) => {
744760
const providers = previousProviders.map((provider) => {
745761
if (!rebuiltInstanceIds.has(provider.instanceId)) return provider;
746762
const { workspaceSnapshots: _workspaceSnapshots, ...machineSnapshot } = provider;
747-
return machineSnapshot;
763+
return repointedInstanceIds.has(provider.instanceId)
764+
? { ...machineSnapshot, models: [] }
765+
: machineSnapshot;
748766
});
749767
return [[previousProviders, providers] as const, providers];
750768
},

‎packages/provider-core/src/server/driver.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ export interface ProviderInstance {
4343
readonly instanceId: ProviderInstanceId;
4444
readonly driverKind: ProviderDriverKind;
4545
readonly continuationIdentity: ProviderContinuationIdentity;
46+
/**
47+
* Identity of the source this instance reads models from. Two instances can
48+
* share an `instanceId` and a `continuationIdentity` while reading different
49+
* servers, so anything that keeps a previous catalog across a rebuild has to
50+
* compare this before treating the old models as still current.
51+
*/
52+
readonly modelSourceIdentity?: string | undefined;
4653
readonly displayName: string | undefined;
4754
readonly accentColor?: string | undefined;
4855
readonly enabled: boolean;

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,10 @@ export const OpenCodeDriver: ProviderDriver<
534534
instanceId,
535535
driverKind: DRIVER_KIND,
536536
continuationIdentity,
537+
// The catalog comes from this endpoint, so a change to it makes the
538+
// previous models a different server's catalog rather than a stale copy
539+
// of this one.
540+
modelSourceIdentity: effectiveConfig.serverUrl.trim(),
537541
displayName,
538542
accentColor,
539543
enabled,

0 commit comments

Comments
 (0)