TypeScript
fix(server): keep workspace skills when provider health fails (#18309)
Nikita Maximilian Grimmcef6f7ae5ebf
diff --git a/apps/server/src/provider/Drivers/CodexDriver.test.ts b/apps/server/src/provider/Drivers/CodexDriver.test.tsindex 51301ef43c5..ab2caf9aafb 100644--- a/apps/server/src/provider/Drivers/CodexDriver.test.ts+++ b/apps/server/src/provider/Drivers/CodexDriver.test.ts@@ -9,6 +9,7 @@ import { expect, it } from "@effect/vitest";import { EnvironmentId, ProviderInstanceId, ProviderSessionId, ThreadId } from "@t3tools/contracts";import * as HostProcess from "@t3tools/shared/HostProcess";import * as Effect from "effect/Effect";+import * as Exit from "effect/Exit";import * as Deferred from "effect/Deferred";import * as Fiber from "effect/Fiber";import * as FileSystem from "effect/FileSystem";@@ -140,9 +141,17 @@ it.layer(layerTest)("CodexDriver", (it) => {> = [];let interruptedChecks = 0;+ let failAcquire = false;const startupChecksInterrupted = yield* Deferred.make<void>();const acquire = () =>Effect.suspend(() => {+ if (failAcquire)+ return Effect.fail(+ new CodexInstallation.CodexInstallationError({+ operation: "acquire",+ detail: "The fixture runtime is unavailable",+ }),+ );if (interruptedChecks === 2) return Effect.succeed(executable);interruptedChecks += 1;return (@@ -207,6 +216,18 @@ it.layer(layerTest)("CodexDriver", (it) => {expect(restored.runtimePaths?.shadowHomePath).toContain(`providers/codex/${instanceId}/shadow`,);+ // The fixture app-server cannot start, so the workspace scan fails+ // instead of passing the machine snapshot off as the workspace's skills.+ const scan = yield* instance.snapshotForCwd!(serverConfig.stateDir).pipe(Effect.exit);+ expect(Exit.isFailure(scan)).toBe(true);+ // The account is still signed in, so a runtime that cannot start is a+ // failed scan too, not the machine snapshot's inventory.+ failAcquire = true;+ const runtimeScan = yield* instance.snapshotForCwd!(serverConfig.stateDir).pipe(+ Effect.exit,+ );+ failAcquire = false;+ expect(Exit.isFailure(runtimeScan)).toBe(true);yield* Deferred.await(observedAccount);const threadId = ThreadId.make("managed-account-thread");@@ -238,6 +259,10 @@ it.layer(layerTest)("CodexDriver", (it) => {expect(after.installed).toBe(true);expect(after.models).toEqual([]);expect(Option.isNone(yield* store.get)).toBe(true);+ // Signed out, there is no runtime to scan with, so the machine+ // snapshot's empty inventory stands instead of a failed scan.+ const signedOutScan = yield* instance.snapshotForCwd!(serverConfig.stateDir);+ expect(signedOutScan.skills).toEqual([]);}).pipe(Effect.provideService(ServerSecretStore.ServerSecretStore, secrets),Effect.provideService(diff --git a/apps/server/src/provider/Drivers/CodexManagedProvider.ts b/apps/server/src/provider/Drivers/CodexManagedProvider.tsindex eb443458402..e3cede11fde 100644--- a/apps/server/src/provider/Drivers/CodexManagedProvider.ts+++ b/apps/server/src/provider/Drivers/CodexManagedProvider.ts@@ -284,21 +284,44 @@ export const makeManagedCodexProvider = Effect.fn("makeManagedCodexProvider")(fusnapshotForCwd: (cwd: string) =>enabled? resolveRuntime.pipe(- Effect.flatMap((effective) =>- probeCodexSkillsForCwd({- binaryPath: effective.config.binaryPath,- homePath: effective.config.homePath,- launchArgs: effective.config.launchArgs,- cwd,- environment: effective.environment,- }),- ),- Effect.flatMap((skills) =>- snapshot.getSnapshot.pipe(Effect.map((draft) => ({ ...draft, skills }))),- ),+ Effect.matchEffect({+ // Signed out or not set up, there is nothing to scan with and the+ // machine snapshot's empty inventory says as much. Otherwise the+ // runtime failed for a signed-in account (a token refresh or+ // reconnect) or before the first check, and the scan failed.+ onFailure: (cause) =>+ snapshot.getSnapshot.pipe(+ Effect.filterOrFail(+ (machine) => machine.auth.status === "unauthenticated",+ () => cause,+ ),+ ),+ onSuccess: (effective) =>+ probeCodexSkillsForCwd({+ binaryPath: effective.config.binaryPath,+ homePath: effective.config.homePath,+ launchArgs: effective.config.launchArgs,+ cwd,+ environment: effective.environment,+ }).pipe(+ Effect.flatMap((skills) =>+ snapshot.getSnapshot.pipe(Effect.map((draft) => ({ ...draft, skills }))),+ ),+ ),+ }),Effect.scoped,Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),- Effect.catch(() => snapshot.getSnapshot),+ // Fail rather than return the machine snapshot: the registry+ // publishes any returned snapshot as this workspace's inventory.+ Effect.mapError(+ (cause) =>+ new ProviderDriverError({+ driver: DRIVER,+ instanceId,+ detail: `Failed to probe Codex skills for '${cwd}'`,+ cause,+ }),+ ),): snapshot.getSnapshot,} satisfies ProviderInstance;diff --git a/apps/server/src/provider/ProviderRegistry.test.ts b/apps/server/src/provider/ProviderRegistry.test.tsindex 725ddfe0c55..fdaec710e60 100644--- a/apps/server/src/provider/ProviderRegistry.test.ts+++ b/apps/server/src/provider/ProviderRegistry.test.ts@@ -65,6 +65,7 @@ import type {} from "@t3tools/provider-core/server/driver";import * as ProviderInstanceRegistry from "./ProviderInstanceRegistry.ts";import * as ProviderRegistry from "./ProviderRegistry.ts";+import { ProviderDriverError } from "./Errors.ts";import { makeManualOnlyProviderMaintenanceCapabilities } from "@t3tools/provider-core/server/maintenanceResolver";const decodeServerSettings = Schema.decodeSync(ServerSettings);const encodeServerSettings = Schema.encodeSync(ServerSettings);@@ -1866,12 +1867,6 @@ it.layer(slashCommands: [{ name: "project" }],skills: [{ name: "project", path: "/workspace/SKILL.md", enabled: true }],} as const satisfies ServerProvider;- const pendingScopedProvider = {- ...scopedProvider,- status: "error",- installed: false,- slashCommands: [],- } as const satisfies ServerProvider;const snapshotCalls = yield* Ref.make(0);const scopedResult = yield* Ref.make<ProviderWorkspaceSnapshot>({...scopedProvider,@@ -1884,7 +1879,7 @@ it.layer(readonly started: Deferred.Deferred<void>;readonly release: Deferred.Deferred<void>;} | null>(null);- const returnPendingSnapshot = yield* Ref.make(true);+ const failScan = yield* Ref.make(true);const probeStarted = yield* Deferred.make<void>();const releaseProbe = yield* Deferred.make<void>();const makeInstance = (@@ -1920,7 +1915,13 @@ it.layer(const firstInstance = makeInstance(machineProvider, () =>Effect.gen(function* () {yield* Ref.update(snapshotCalls, (count) => count + 1);- if (yield* Ref.get(returnPendingSnapshot)) return pendingScopedProvider;+ if (yield* Ref.get(failScan)) {+ return yield* new ProviderDriverError({+ driver,+ instanceId,+ detail: "The workspace scan failed.",+ });+ }yield* Deferred.succeed(probeStarted, undefined);yield* Deferred.await(releaseProbe);const result = yield* Ref.get(scopedResult);@@ -1977,7 +1978,7 @@ it.layer(const registry = yield* ProviderRegistry.ProviderRegistry;yield* registry.refreshWorkspaceSnapshot({ instanceId, cwd: "/workspace" });assert.strictEqual((yield* registry.getProviders)[0]?.workspaceSnapshots, undefined);- yield* Ref.set(returnPendingSnapshot, false);+ yield* Ref.set(failScan, false);const workspaceUpdate = yield* registry.streamChanges.pipe(Stream.runHead,Effect.forkChild,@@ -2089,6 +2090,138 @@ it.layer(}),);+ it.effect("publishes workspace skills while machine health is in error", () =>+ Effect.gen(function* () {+ const driver = ProviderDriverKind.make("codex");+ const instanceId = ProviderInstanceId.make("codex");+ const healthyProvider = {+ instanceId,+ driver,+ status: "ready",+ enabled: true,+ installed: true,+ auth: { status: "authenticated" },+ checkedAt: "2026-06-10T00:00:00.000Z",+ version: "1.0.0",+ models: [],+ slashCommands: [COMPACT_SLASH_COMMAND],+ skills: [],+ } as const satisfies ServerProvider;+ // Codex's account check failed; its skill scan did not.+ const unhealthyProvider = {+ ...healthyProvider,+ status: "error",+ auth: { status: "unknown" },+ message: "Codex app-server provider probe failed: unauthorized (401).",+ slashCommands: [],+ } as const satisfies ServerProvider;+ const firstSkills = [{ name: "first", path: "/workspace/first/SKILL.md", enabled: true }];+ const laterSkills = [{ name: "later", path: "/workspace/later/SKILL.md", enabled: true }];+ const scanResult = yield* Ref.make<ProviderWorkspaceSnapshot>({+ ...healthyProvider,+ skills: firstSkills,+ });+ const instance: ProviderInstance = {+ instanceId,+ driverKind: driver,+ continuationIdentity: { driverKind: driver, continuationKey: "codex:instance:codex" },+ displayName: undefined,+ enabled: true,+ snapshot: {+ resolveMaintenance: () =>+ Effect.succeed(+ makeManualOnlyProviderMaintenanceCapabilities({+ provider: driver,+ packageName: null,+ }),+ ),+ getSnapshot: Effect.succeed(unhealthyProvider),+ refresh: Effect.succeed(unhealthyProvider),+ streamChanges: Stream.empty,+ applyUsageLimits: () => Effect.void,+ },+ snapshotForCwd: () => Ref.get(scanResult),+ orchestrationAdapter: {} as ProviderInstance["orchestrationAdapter"],+ textGeneration: {} as ProviderInstance["textGeneration"],+ };+ const layerInstanceRegistry = Layer.succeed(+ ProviderInstanceRegistry.ProviderInstanceRegistry,+ {+ getInstance: (requestedId) =>+ Effect.succeed(requestedId === instanceId ? instance : undefined),+ listInstances: Effect.succeed([instance]),+ listUnavailable: Effect.succeed([]),+ streamChanges: Stream.empty,+ subscribeChanges: Effect.flatMap(PubSub.unbounded<void>(), PubSub.subscribe),+ },+ );+ const scope = yield* Scope.make();+ yield* Effect.addFinalizer(() => Scope.close(scope, Exit.void));+ const runtimeServices = yield* Layer.build(+ ProviderRegistry.layer.pipe(+ Layer.provideMerge(layerInstanceRegistry),+ Layer.provideMerge(+ ServerConfig.layerTest(process.cwd(), {+ prefix: "t3-provider-registry-unhealthy-workspace-",+ }),+ ),+ Layer.provideMerge(NodeServices.layer),+ ),+ ).pipe(Scope.provide(scope));++ yield* Effect.gen(function* () {+ const registry = yield* ProviderRegistry.ProviderRegistry;+ const workspaceOf = Effect.map(+ registry.getProviders,+ (providers) => providers[0]?.workspaceSnapshots?.[0],+ );+ yield* registry.refreshWorkspaceSnapshot({ instanceId, cwd: "/workspace" });+ assert.deepStrictEqual((yield* workspaceOf)?.slashCommands, [COMPACT_SLASH_COMMAND]);++ // An expired scan runs while the account check fails: the skills are+ // current, but commands that ride on the health check are unknown.+ yield* TestClock.adjust(PROVIDER_WORKSPACE_SNAPSHOT_TTL_MS);+ yield* Ref.set(scanResult, { ...unhealthyProvider, skills: laterSkills });+ yield* registry.refreshWorkspaceSnapshot({ instanceId, cwd: "/workspace" });+ const unhealthy = yield* workspaceOf;+ assert.deepStrictEqual(unhealthy?.skills, laterSkills);+ assert.deepStrictEqual(unhealthy?.slashCommands, [COMPACT_SLASH_COMMAND]);+ assert.strictEqual(unhealthy?.slashCommandsPending, true);+ const provider = (yield* registry.getProviders)[0];+ assert.strictEqual(provider?.status, "error");+ assert.strictEqual(provider?.message, unhealthyProvider.message);++ // A pending entry is rescanned on the next request; an empty scan+ // removes the skills.+ yield* Ref.set(scanResult, { ...unhealthyProvider, skills: [] });+ yield* registry.refreshWorkspaceSnapshot({ instanceId, cwd: "/workspace" });+ assert.deepStrictEqual((yield* workspaceOf)?.skills, []);++ // A driver that discovers its own commands reports them even while+ // health is in error.+ const discoveredCommand = { name: "review", description: "Review changes" };+ yield* Ref.set(scanResult, {+ ...unhealthyProvider,+ slashCommands: [discoveredCommand],+ slashCommandsPending: false,+ });+ yield* registry.refreshWorkspaceSnapshot({ instanceId, cwd: "/workspace" });+ const discovered = yield* workspaceOf;+ assert.deepStrictEqual(discovered?.slashCommands, [discoveredCommand]);+ assert.strictEqual(discovered?.slashCommandsPending, undefined);++ // Once health recovers, the next scan restores the full entry.+ yield* TestClock.adjust(PROVIDER_WORKSPACE_SNAPSHOT_TTL_MS);+ yield* Ref.set(scanResult, { ...healthyProvider, skills: laterSkills });+ yield* registry.refreshWorkspaceSnapshot({ instanceId, cwd: "/workspace" });+ const recovered = yield* workspaceOf;+ assert.deepStrictEqual(recovered?.skills, laterSkills);+ assert.deepStrictEqual(recovered?.slashCommands, [COMPACT_SLASH_COMMAND]);+ assert.strictEqual(recovered?.slashCommandsPending, undefined);+ }).pipe(Effect.provide(runtimeServices));+ }),+ );+it.effect("refreshes OpenCode catalogs and preserves other providers", () =>Effect.gen(function* () {const codexDriver = ProviderDriverKind.make("codex");diff --git a/apps/server/src/provider/ProviderRegistry.ts b/apps/server/src/provider/ProviderRegistry.tsindex 2b884af5576..80382fc1927 100644--- a/apps/server/src/provider/ProviderRegistry.ts+++ b/apps/server/src/provider/ProviderRegistry.ts@@ -910,25 +910,30 @@ export const layer = Layer.effect(: Effect.void;return yield* refreshMachineSnapshot.pipe(Effect.andThen(instance.snapshotForCwd(input.cwd)),+ // A failed scan fails this effect, so a returned snapshot is a real+ // inventory even while the provider's health check reports an error.Effect.flatMap((scopedSnapshot) =>- scopedSnapshot.status === "error" && scopedSnapshot.slashCommandsPending === undefined- ? Ref.get(providersRef)- : instanceRegistry.getInstance(input.instanceId).pipe(- Effect.flatMap((currentInstance) => {- if (currentInstance !== instance) return Ref.get(providersRef);- return updateProviders((currentProviders) =>- currentProviders.map((candidate) =>- candidate.instanceId === input.instanceId &&- Equal.equals(workspaceSnapshotOf(candidate), scannedFrom)- ? upsertProviderWorkspaceSnapshot(candidate, input.cwd, {- ...scopedSnapshot,- checkedAt: scannedAt,- })- : candidate,- ),- );- }),- ),+ instanceRegistry.getInstance(input.instanceId).pipe(+ Effect.flatMap((currentInstance) => {+ if (currentInstance !== instance) return Ref.get(providersRef);+ return updateProviders((currentProviders) =>+ currentProviders.map((candidate) =>+ candidate.instanceId === input.instanceId &&+ Equal.equals(workspaceSnapshotOf(candidate), scannedFrom)+ ? upsertProviderWorkspaceSnapshot(candidate, input.cwd, {+ ...scopedSnapshot,+ checkedAt: scannedAt,+ // Unless the driver reports its own command discovery,+ // commands come from the health check. A failed check+ // leaves them unknown, so keep the last ones and retry.+ slashCommandsPending:+ scopedSnapshot.slashCommandsPending ?? scopedSnapshot.status === "error",+ })+ : candidate,+ ),+ );+ }),+ ),),Effect.ensuring(claimeddiff --git a/packages/provider-opencode/src/server/driver.ts b/packages/provider-opencode/src/server/driver.tsindex cd1476d0168..9c522d554ce 100644--- a/packages/provider-opencode/src/server/driver.ts+++ b/packages/provider-opencode/src/server/driver.ts@@ -380,10 +380,18 @@ export const OpenCodeDriver: ProviderDriver<skills: openCodeRuntime.loadOpenCodeSkills(client),commands: OpenCodeRuntime.loadOpenCodeCommands(client).pipe(Effect.timeout("10 seconds"),- Effect.orElseSucceed(() => []),+ Effect.option,),},{ concurrency: "unbounded" },+ ).pipe(+ // Only a finished lookup is reported as complete. A failed one leaves+ // the flag unset, so the registry decides from the provider's health.+ Effect.map(({ skills, commands }) => ({+ skills,+ commands: Option.getOrElse(commands, () => []),+ slashCommandsPending: Option.isSome(commands) ? false : undefined,+ })),);const loadWorkspaceForCwd = (cwd: string) =>effectiveConfig.serverUrl.trim().length > 0@@ -487,16 +495,20 @@ export const OpenCodeDriver: ProviderDriver<...machineSnapshot,skills: openCode2SkillsToServerProviderSkills(skills),slashCommands: openCode2CommandsToServerProviderSlashCommands(commands),+ // A finished scan lists at least the built-ins. None means it+ // timed out, so the registry decides from the provider's health.+ slashCommandsPending: commands.length > 0 ? false : undefined,})),),v1: Effect.all([snapshot.getSnapshot,loadWorkspaceForCwd(cwd).pipe(Effect.timeout("20 seconds")),]).pipe(- Effect.map(([machineSnapshot, { skills, commands }]) => ({+ Effect.map(([machineSnapshot, { skills, commands, slashCommandsPending }]) => ({...machineSnapshot,skills: openCodeSkillsToServerProviderSkills(skills),slashCommands: openCodeCommandsToServerProviderSlashCommands(commands),+ slashCommandsPending,})),Effect.mapError((cause) =>diff --git a/packages/provider-pi/src/server/driver.ts b/packages/provider-pi/src/server/driver.tsindex eebdf7d864d..42e240fdc7b 100644--- a/packages/provider-pi/src/server/driver.ts+++ b/packages/provider-pi/src/server/driver.ts@@ -202,7 +202,11 @@ export const PiDriver: ProviderDriver<PiSettings, PiDriverEnv, Path.Path> = {),),]).pipe(- Effect.map(([machineSnapshot, commands]) => ({ ...machineSnapshot, ...commands })),+ Effect.map(([machineSnapshot, commands]) => ({+ ...machineSnapshot,+ ...commands,+ slashCommandsPending: false,+ })),),orchestrationAdapter,textGeneration,