@devries-cameron20 / t3code Mirror · read-only / commits / 655977ab8c
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 04:30 UTC

fix(server): provider settings changed mid-refresh no longer revert to the old status (#29894)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Alex Southwell committed Nov 3, 2026 at 23:39 UTC 655977ab8cd548072db1609d559771e33df11abc
2 files changed +166 -31
apps/server/src/provider/ProviderRegistry.test.ts
+133
@@ -1934,6 +1934,139 @@ it.layer(
1934 }),
1935 );
1936
1937 + it.effect("ignores a probe from an instance that settings replaced mid-refresh", () =>
1938 + Effect.gen(function* () {
1939 + const scenario = Effect.fn(function* (
1940 + probeFails: boolean,
1941 + run: (
1942 + registry: ProviderRegistry.ProviderRegistry["Service"],
1943 + ) => Effect.Effect<ReadonlyArray<ServerProvider>>,
1944 + ) {
1945 + const driver = ProviderDriverKind.make("codex");
1946 + const instanceId = ProviderInstanceId.make("codex");
1947 + const provider = {
1948 + instanceId,
1949 + driver,
1950 + status: "ready",
1951 + enabled: true,
1952 + installed: true,
1953 + auth: { status: "authenticated" },
1954 + checkedAt: "2026-06-10T00:00:00.000Z",
1955 + version: "2.0.0",
1956 + models: [],
1957 + slashCommands: [],
1958 + skills: [],
1959 + } as const satisfies ServerProvider;
1960 + const retiredProbe = {
1961 + ...provider,
1962 + status: "error",
1963 + installed: false,
1964 + version: "1.0.0",
1965 + message: "Codex CLI is not installed.",
1966 + } as const satisfies ServerProvider;
1967 + const probeStarted = yield* Deferred.make<void>();
1968 + const releaseProbe = yield* Deferred.make<void>();
1969 + const makeInstance = (snapshot: ServerProvider, refresh: Effect.Effect<void>) =>
1970 + ({
1971 + instanceId,
1972 + driverKind: driver,
1973 + continuationIdentity: { driverKind: driver, continuationKey: "codex:instance:codex" },
1974 + displayName: undefined,
1975 + enabled: true,
1976 + snapshot: {
1977 + resolveMaintenance: () =>
1978 + Effect.succeed(
1979 + makeManualOnlyProviderMaintenanceCapabilities({
1980 + provider: driver,
1981 + packageName: null,
1982 + }),
1983 + ),
1984 + getSnapshot: Effect.succeed(snapshot),
1985 + refresh: Effect.as(refresh, snapshot),
1986 + streamChanges: Stream.empty,
1987 + applyUsageLimits: () => Effect.void,
1988 + },
1989 + orchestrationAdapter: {} as ProviderInstance["orchestrationAdapter"],
1990 + textGeneration: {} as ProviderInstance["textGeneration"],
1991 + }) satisfies ProviderInstance;
1992 + const retiredInstance = makeInstance(
1993 + retiredProbe,
1994 + Deferred.succeed(probeStarted, undefined).pipe(
1995 + Effect.andThen(Deferred.await(releaseProbe)),
1996 + Effect.andThen(probeFails ? Effect.die(new Error("probe failed")) : Effect.void),
1997 + ),
1998 + );
1999 + const rebuiltInstance = makeInstance(provider, Effect.void);
2000 + const instances = yield* Ref.make<ReadonlyArray<ProviderInstance>>([retiredInstance]);
2001 + const changes = yield* PubSub.unbounded<void>();
2002 + const instanceRegistryLayer = Layer.succeed(
2003 + ProviderInstanceRegistry.ProviderInstanceRegistry,
2004 + {
2005 + getInstance: (requestedId) =>
2006 + Ref.get(instances).pipe(
2007 + Effect.map((current) =>
2008 + current.find((instance) => instance.instanceId === requestedId),
2009 + ),
2010 + ),
2011 + listInstances: Ref.get(instances),
2012 + listUnavailable: Effect.succeed([]),
2013 + streamChanges: Stream.empty,
2014 + subscribeChanges: PubSub.subscribe(changes),
2015 + },
2016 + );
2017 + const scope = yield* Scope.make();
2018 + yield* Effect.addFinalizer(() => Scope.close(scope, Exit.void));
2019 + const runtimeServices = yield* Layer.build(
2020 + ProviderRegistry.layer.pipe(
2021 + Layer.provide(layerUnusedMutator),
2022 + Layer.provideMerge(instanceRegistryLayer),
2023 + Layer.provideMerge(
2024 + ServerConfig.layerTest(process.cwd(), {
2025 + prefix: "t3-provider-registry-retired-refresh-",
2026 + }),
2027 + ),
2028 + Layer.provideMerge(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)),
2029 + ),
2030 + ).pipe(Scope.provide(scope));
2031 +
2032 + return yield* Effect.gen(function* () {
2033 + const registry = yield* ProviderRegistry.ProviderRegistry;
2034 + const rebuiltPublished = yield* Stream.toPull(
2035 + registry.streamChanges.pipe(
2036 + Stream.filter((providers) => providers[0]?.version === provider.version),
2037 + ),
2038 + );
2039 + const refresh = yield* run(registry).pipe(Effect.forkChild);
2040 + yield* Deferred.await(probeStarted);
2041 + yield* Ref.set(instances, [rebuiltInstance]);
2042 + yield* PubSub.publish(changes, undefined);
2043 + yield* rebuiltPublished;
2044 + yield* Deferred.succeed(releaseProbe, undefined);
2045 + return {
2046 + provider,
2047 + result: yield* Fiber.join(refresh),
2048 + providers: yield* registry.getProviders,
2049 + };
2050 + }).pipe(Effect.provide(runtimeServices));
2051 + });
2052 + const driver = ProviderDriverKind.make("codex");
2053 + const instanceId = ProviderInstanceId.make("codex");
2054 +
2055 + const manual = yield* scenario(false, (registry) => registry.refresh());
2056 + expect(manual.result).toEqual([expect.objectContaining(manual.provider)]);
2057 + expect(manual.providers).toEqual([expect.objectContaining(manual.provider)]);
2058 +
2059 + // Neither the retired probe's failure nor its cached row can stand in for the replacement.
2060 + for (const probeFails of [true, false]) {
2061 + const afterUpdate = yield* scenario(probeFails, (registry) =>
2062 + registry.refreshAfterUpdate(driver, instanceId),
2063 + );
2064 + expect(afterUpdate.result).toEqual([]);
2065 + expect(afterUpdate.providers).toEqual([expect.objectContaining(afterUpdate.provider)]);
2066 + }
2067 + }),
2068 + );
2069 +
2070 it.effect("keeps an enriched snapshot that streams in before a manual refresh returns", () =>
2071 Effect.gen(function* () {
2072 const driver = ProviderDriverKind.make("acpRegistry");
apps/server/src/provider/ProviderRegistry.ts
+33 -31
@@ -444,10 +444,6 @@ export const layer = Layer.effect(
444 );
445 const syncSemaphore = yield* Semaphore.make(1);
446
447 - const getLiveSources: Effect.Effect<ReadonlyArray<ProviderSnapshotSource>> = Ref.get(
448 - liveSubsRef,
449 - ).pipe(Effect.map((map) => Array.from(map.values(), buildSnapshotSource)));
450 -
447 const persistProvider = Effect.fn("ProviderRegistry.persistProvider")(function* (
448 provider: ServerProvider,
449 ) {
@@ -601,17 +597,25 @@ export const layer = Layer.effect(
597 },
598 );
599
604 - const refreshOneSource = Effect.fn("refreshOneSource")(function* (
605 - providerSource: ProviderSnapshotSource,
606 - ) {
600 + const isCurrentInstance = (instance: ProviderInstance) =>
601 + instanceRegistry
602 + .getInstance(instance.instanceId)
603 + .pipe(Effect.map((current) => current === instance));
604 +
605 + const refreshOneSource = Effect.fn("refreshOneSource")(function* (instance: ProviderInstance) {
606 + const providerSource = buildSnapshotSource(instance);
607 // Enrichment can publish a newer snapshot before refresh returns its pre-enrichment probe.
608 - return yield* providerSource.refresh.pipe(
608 + const probe = yield* providerSource.refresh.pipe(
609 Effect.andThen(providerSource.getSnapshot),
610 - Effect.flatMap((nextProvider) =>
611 - correlateSnapshotWithSource(providerSource, nextProvider).pipe(
612 - Effect.flatMap(syncProvider),
613 - ),
614 - ),
610 + Effect.exit,
611 + );
612 + // Settings can replace or remove the instance mid-probe; its late result or failure is stale.
613 + if (!(yield* isCurrentInstance(instance))) {
614 + return yield* Ref.get(providersRef);
615 + }
616 + const nextProvider = yield* probe;
617 + return yield* correlateSnapshotWithSource(providerSource, nextProvider).pipe(
618 + Effect.flatMap(syncProvider),
619 );
620 });
621
@@ -640,9 +644,9 @@ export const layer = Layer.effect(
644 // Detach shared passes so the first caller's interruption cannot cancel other callers' work.
645 // Syncing first keeps a refresh right after a rebuild from probing the replaced instance.
646 yield* syncLiveSourcesAndContinue.pipe(
643 - Effect.andThen(getLiveSources),
644 - Effect.flatMap((sources) =>
645 - Effect.forEach(sources, (source) => refreshOneSource(source), {
647 + Effect.andThen(Ref.get(liveSubsRef)),
648 + Effect.flatMap((instances) =>
649 + Effect.forEach(instances.values(), (instance) => refreshOneSource(instance), {
650 concurrency: "unbounded",
651 discard: true,
652 }),
@@ -663,27 +667,22 @@ export const layer = Layer.effect(
667 if (provider === undefined) {
668 return yield* refreshAll();
669 }
666 - const defaultInstanceId = defaultInstanceIdForDriver(provider);
667 - const sources = yield* getLiveSources;
668 - const providerSource = sources.find(
669 - (candidate) => candidate.instanceId === defaultInstanceId,
670 - );
671 - if (!providerSource) {
670 + const instance = (yield* Ref.get(liveSubsRef)).get(defaultInstanceIdForDriver(provider));
671 + if (!instance) {
672 return yield* Ref.get(providersRef);
673 }
674 - return yield* refreshOneSource(providerSource);
674 + return yield* refreshOneSource(instance);
675 });
676
677 const refreshInstance = Effect.fn("refreshInstance")(function* (
678 instanceId: ProviderInstanceId,
679 ) {
680 yield* syncLiveSourcesAndContinue;
681 - const sources = yield* getLiveSources;
682 - const providerSource = sources.find((candidate) => candidate.instanceId === instanceId);
683 - if (!providerSource) {
681 + const instance = (yield* Ref.get(liveSubsRef)).get(instanceId);
682 + if (!instance) {
683 return yield* Ref.get(providersRef);
684 }
686 - return yield* refreshOneSource(providerSource);
685 + return yield* refreshOneSource(instance);
686 });
687
688 const reloadSecretsAndRefresh = Effect.fn("reloadSecretsAndRefresh")(function* (
@@ -744,15 +743,18 @@ export const layer = Layer.effect(
743 yield* Effect.forEach(
744 instances.filter((instance) => !failedInstances.has(instance.instanceId)),
745 (instance) =>
747 - refreshOneSource(buildSnapshotSource(instance)).pipe(
746 + refreshOneSource(instance).pipe(
747 Effect.catchCause((cause) => recoverInstanceFailure(instance.instanceId, cause)),
748 ),
749 { concurrency: "unbounded", discard: true },
750 );
751 + // A replaced instance's cached row did not come from this pass's probe.
752 + const refreshedInstances = yield* Effect.filter(
753 + instances.filter((instance) => !failedInstances.has(instance.instanceId)),
754 + isCurrentInstance,
755 + );
756 const refreshedInstanceIds = new Set(
753 - instances
754 - .filter((instance) => !failedInstances.has(instance.instanceId))
755 - .map((instance) => instance.instanceId),
757 + refreshedInstances.map((instance) => instance.instanceId),
758 );
759 return (yield* Ref.get(providersRef)).filter((candidate) =>
760 refreshedInstanceIds.has(candidate.instanceId),