Skip to content

Commit 2fb99a7

Browse files
fix(server): only run provider updates through the installer that owns the binary (#9325)
Co-authored-by: Claude Code <[email protected]>
1 parent 94cc815 commit 2fb99a7

23 files changed

Lines changed: 1578 additions & 890 deletions

‎apps/server/src/provider/Drivers/ClaudeDriver.ts‎

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import { withInstanceIdentity } from "./instanceIdentity.ts";
4848
import { mergeProviderInstanceEnvironment } from "../ProviderInstanceEnvironment.ts";
4949
import {
5050
enrichProviderSnapshotWithVersionAdvisory,
51+
makeCachedProviderMaintenanceResolution,
5152
makePackageManagedProviderMaintenanceResolver,
5253
normalizeCommandPath,
5354
resolveProviderMaintenanceCapabilitiesEffect,
@@ -76,11 +77,8 @@ function isClaudeNativeCommandPath(commandPath: string): boolean {
7677
const UPDATE = makePackageManagedProviderMaintenanceResolver({
7778
provider: DRIVER_KIND,
7879
npmPackageName: "@anthropic-ai/claude-code",
79-
homebrewFormula: "claude-code",
8080
nativeUpdate: {
81-
executable: "claude",
8281
args: ["update"],
83-
lockKey: "claude-native",
8482
isCommandPath: isClaudeNativeCommandPath,
8583
},
8684
});
@@ -122,10 +120,16 @@ export const ClaudeDriver: ProviderDriver<ClaudeSettings, ClaudeDriverEnv> = {
122120
instanceId,
123121
});
124122
const effectiveConfig = { ...config, enabled } satisfies ClaudeSettings;
125-
const maintenanceCapabilities = yield* resolveProviderMaintenanceCapabilitiesEffect(UPDATE, {
126-
binaryPath: effectiveConfig.binaryPath,
127-
env: processEnv,
128-
});
123+
const resolveMaintenance = yield* makeCachedProviderMaintenanceResolution(
124+
resolveProviderMaintenanceCapabilitiesEffect(UPDATE, {
125+
binaryPath: effectiveConfig.binaryPath,
126+
env: processEnv,
127+
}).pipe(
128+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
129+
Effect.provideService(FileSystem.FileSystem, fileSystem),
130+
Effect.provideService(Path.Path, path),
131+
),
132+
);
129133
const continuationGroupKey = yield* makeClaudeContinuationGroupKey(effectiveConfig);
130134
const stampIdentity = withInstanceIdentity({
131135
instanceId,
@@ -189,7 +193,7 @@ export const ClaudeDriver: ProviderDriver<ClaudeSettings, ClaudeDriverEnv> = {
189193

190194
const snapshotSettings = makeProviderSnapshotSettingsSource(effectiveConfig, serverSettings);
191195
const snapshot = yield* makeManagedServerProvider<ProviderSnapshotSettings<ClaudeSettings>>({
192-
maintenanceCapabilities,
196+
resolveMaintenance,
193197
getSettings: snapshotSettings.getSettings,
194198
streamSettings: snapshotSettings.streamSettings,
195199
haveSettingsChanged: haveProviderSnapshotSettingsChanged,
@@ -202,9 +206,12 @@ export const ClaudeDriver: ProviderDriver<ClaudeSettings, ClaudeDriverEnv> = {
202206
),
203207
checkProvider,
204208
enrichSnapshot: ({ settings, snapshot, publishSnapshot }) =>
205-
enrichProviderSnapshotWithVersionAdvisory(snapshot, maintenanceCapabilities, {
206-
enableProviderUpdateChecks: settings.enableProviderUpdateChecks,
207-
}).pipe(
209+
resolveMaintenance().pipe(
210+
Effect.flatMap((maintenanceCapabilities) =>
211+
enrichProviderSnapshotWithVersionAdvisory(snapshot, maintenanceCapabilities, {
212+
enableProviderUpdateChecks: settings.enableProviderUpdateChecks,
213+
}),
214+
),
208215
Effect.provideService(HttpClient.HttpClient, httpClient),
209216
Effect.flatMap((enrichedSnapshot) => publishSnapshot(enrichedSnapshot)),
210217
),
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
// @effect-diagnostics nodeBuiltinImport:off
2+
import * as NodeServices from "@effect/platform-node/NodeServices";
3+
import * as NodeOS from "node:os";
4+
import * as NodePath from "node:path";
5+
import { expect, it } from "@effect/vitest";
6+
import { ProviderInstanceId } from "@t3tools/contracts";
7+
import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
8+
import * as Effect from "effect/Effect";
9+
import * as FileSystem from "effect/FileSystem";
10+
import * as Layer from "effect/Layer";
11+
import { HttpClient } from "effect/unstable/http";
12+
import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner";
13+
14+
import * as BackgroundPolicy from "../../background/BackgroundPolicy.ts";
15+
import { ServerConfig } from "../../config.ts";
16+
import { ServerSettingsService } from "../../serverSettings.ts";
17+
import { layerTest as codexResetCreditLayerTest } from "../Layers/codexResetCredit.ts";
18+
import { NoOpProviderEventLoggers, ProviderEventLoggers } from "../Layers/ProviderEventLoggers.ts";
19+
import * as ModelManifest from "../ModelManifest.ts";
20+
import { CodexDriver } from "./CodexDriver.ts";
21+
22+
const testLayer = ServerConfig.layerTest(process.cwd(), {
23+
prefix: "t3-codex-driver-maintenance-",
24+
}).pipe(
25+
Layer.provideMerge(NodeServices.layer),
26+
Layer.provideMerge(ServerSettingsService.layerTest()),
27+
Layer.provideMerge(ModelManifest.layerTest),
28+
Layer.provideMerge(codexResetCreditLayerTest),
29+
Layer.provideMerge(
30+
Layer.mock(BackgroundPolicy.BackgroundPolicy)({
31+
shouldRunScopeWork: () => Effect.succeed(false),
32+
}),
33+
),
34+
Layer.provideMerge(Layer.succeed(ProviderEventLoggers, NoOpProviderEventLoggers)),
35+
Layer.provideMerge(
36+
Layer.succeed(
37+
HttpClient.HttpClient,
38+
HttpClient.make(() => Effect.die("Disabled Codex must not make an HTTP request")),
39+
),
40+
),
41+
);
42+
43+
// The `#!/bin/sh` stub below cannot be resolved as an executable on Windows.
44+
const windowsHost = HostProcessPlatform.defaultValue() === "win32";
45+
46+
const noSpawn = ChildProcessSpawner.make(() =>
47+
Effect.die("Disabled Codex must not spawn a process"),
48+
);
49+
50+
it.layer(testLayer)("CodexDriver", (it) => {
51+
it.effect.skipIf(windowsHost)(
52+
"runs the standalone updater against the shared home, not the shadow home",
53+
() =>
54+
Effect.gen(function* () {
55+
const fs = yield* FileSystem.FileSystem;
56+
const tempDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-codex-driver-" });
57+
const sharedHome = NodePath.join(tempDir, "codex-home");
58+
const shadowHome = NodePath.join(tempDir, "codex-shadow");
59+
const binaryPath = NodePath.join(sharedHome, "packages", "standalone", "bin", "codex");
60+
yield* fs.makeDirectory(NodePath.dirname(binaryPath), { recursive: true });
61+
yield* fs.writeFileString(binaryPath, "#!/bin/sh\n");
62+
yield* fs.chmod(binaryPath, 0o755);
63+
64+
const instance = yield* CodexDriver.create({
65+
instanceId: ProviderInstanceId.make("codex-shadow"),
66+
displayName: "Codex test",
67+
enabled: false,
68+
environment: [],
69+
config: {
70+
...CodexDriver.defaultConfig(),
71+
binaryPath,
72+
homePath: sharedHome,
73+
shadowHomePath: shadowHome,
74+
},
75+
});
76+
77+
const capabilities = yield* instance.snapshot.resolveMaintenance();
78+
expect(capabilities.update).toMatchObject({
79+
executable: binaryPath,
80+
args: ["update"],
81+
lockKey: "codex-native",
82+
env: { CODEX_HOME: sharedHome },
83+
});
84+
}).pipe(
85+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, noSpawn),
86+
Effect.scoped,
87+
),
88+
);
89+
90+
it.effect("stays manual-only when the configured executable does not exist", () =>
91+
Effect.gen(function* () {
92+
const instance = yield* CodexDriver.create({
93+
instanceId: ProviderInstanceId.make("codex-missing"),
94+
displayName: "Codex test",
95+
enabled: false,
96+
environment: [],
97+
config: {
98+
...CodexDriver.defaultConfig(),
99+
binaryPath: NodePath.join(NodeOS.tmpdir(), "t3-codex-missing", "codex"),
100+
},
101+
});
102+
expect((yield* instance.snapshot.resolveMaintenance()).update).toBeNull();
103+
}).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, noSpawn), Effect.scoped),
104+
);
105+
});

‎apps/server/src/provider/Drivers/CodexDriver.ts‎

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ import { withInstanceIdentity } from "./instanceIdentity.ts";
5555
import { mergeProviderInstanceEnvironment } from "../ProviderInstanceEnvironment.ts";
5656
import {
5757
enrichProviderSnapshotWithVersionAdvisory,
58+
makeCachedProviderMaintenanceResolution,
5859
makePackageManagedProviderMaintenanceResolver,
60+
normalizeCommandPath,
5961
resolveProviderMaintenanceCapabilitiesEffect,
6062
} from "../providerMaintenance.ts";
6163
import {
@@ -71,12 +73,29 @@ import {
7173
const decodeCodexSettings = Schema.decodeSync(CodexSettings);
7274

7375
const DRIVER_KIND = ProviderDriverKind.make("codex");
74-
const UPDATE = makePackageManagedProviderMaintenanceResolver({
75-
provider: DRIVER_KIND,
76-
npmPackageName: "@openai/codex",
77-
homebrewFormula: "codex",
78-
nativeUpdate: null,
79-
});
76+
// The standalone installer lays out `<CODEX_HOME>/packages/standalone/…`;
77+
// CODEX_HOME is not always `~/.codex`.
78+
function isCodexStandaloneCommandPath(commandPath: string): boolean {
79+
return normalizeCommandPath(commandPath).includes("/packages/standalone/");
80+
}
81+
82+
/**
83+
* `codex update` replaces the standalone tree under `CODEX_HOME`. That tree
84+
* lives in the shared home even when an auth-overlay shadow home is in use
85+
* (the overlay only carries auth and a few local entries), so the updater
86+
* runs against `sharedHomePath` rather than the instance's effective home.
87+
*/
88+
function makeCodexMaintenanceResolver(sharedHomePath: string) {
89+
return makePackageManagedProviderMaintenanceResolver({
90+
provider: DRIVER_KIND,
91+
npmPackageName: "@openai/codex",
92+
nativeUpdate: {
93+
args: ["update"],
94+
isCommandPath: isCodexStandaloneCommandPath,
95+
env: { CODEX_HOME: sharedHomePath },
96+
},
97+
});
98+
}
8099

81100
/**
82101
* Services the driver needs to materialize an instance. Surfaced as the
@@ -108,6 +127,8 @@ export const CodexDriver: ProviderDriver<CodexSettings, CodexDriverEnv> = {
108127
Effect.gen(function* () {
109128
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
110129
const resetCreditCoordinator = yield* CodexResetCreditCoordinator;
130+
const fileSystem = yield* FileSystem.FileSystem;
131+
const pathService = yield* Path.Path;
111132
const httpClient = yield* HttpClient.HttpClient;
112133
const serverSettings = yield* ServerSettingsService;
113134
const eventLoggers = yield* ProviderEventLoggers;
@@ -138,10 +159,19 @@ export const CodexDriver: ProviderDriver<CodexSettings, CodexDriverEnv> = {
138159
enabled,
139160
homePath: homeLayout.effectiveHomePath ?? "",
140161
} satisfies CodexSettings;
141-
const maintenanceCapabilities = yield* resolveProviderMaintenanceCapabilitiesEffect(UPDATE, {
142-
binaryPath: effectiveConfig.binaryPath,
143-
env: processEnv,
144-
});
162+
const resolveMaintenance = yield* makeCachedProviderMaintenanceResolution(
163+
resolveProviderMaintenanceCapabilitiesEffect(
164+
makeCodexMaintenanceResolver(homeLayout.sharedHomePath),
165+
{
166+
binaryPath: effectiveConfig.binaryPath,
167+
env: processEnv,
168+
},
169+
).pipe(
170+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
171+
Effect.provideService(FileSystem.FileSystem, fileSystem),
172+
Effect.provideService(Path.Path, pathService),
173+
),
174+
);
145175

146176
// `makeCodexAdapter` and `makeCodexTextGeneration` have `never` error
147177
// channels at construction time — their failure modes are all on the
@@ -177,7 +207,7 @@ export const CodexDriver: ProviderDriver<CodexSettings, CodexDriverEnv> = {
177207
);
178208
const snapshotSettings = makeProviderSnapshotSettingsSource(effectiveConfig, serverSettings);
179209
const snapshot = yield* makeManagedServerProvider<ProviderSnapshotSettings<CodexSettings>>({
180-
maintenanceCapabilities,
210+
resolveMaintenance,
181211
getSettings: snapshotSettings.getSettings,
182212
streamSettings: snapshotSettings.streamSettings,
183213
haveSettingsChanged: haveProviderSnapshotSettingsChanged,
@@ -190,9 +220,12 @@ export const CodexDriver: ProviderDriver<CodexSettings, CodexDriverEnv> = {
190220
),
191221
checkProvider,
192222
enrichSnapshot: ({ settings, snapshot, publishSnapshot }) =>
193-
enrichProviderSnapshotWithVersionAdvisory(snapshot, maintenanceCapabilities, {
194-
enableProviderUpdateChecks: settings.enableProviderUpdateChecks,
195-
}).pipe(
223+
resolveMaintenance().pipe(
224+
Effect.flatMap((maintenanceCapabilities) =>
225+
enrichProviderSnapshotWithVersionAdvisory(snapshot, maintenanceCapabilities, {
226+
enableProviderUpdateChecks: settings.enableProviderUpdateChecks,
227+
}),
228+
),
196229
Effect.provideService(HttpClient.HttpClient, httpClient),
197230
Effect.flatMap((enrichedSnapshot) => publishSnapshot(enrichedSnapshot)),
198231
),

‎apps/server/src/provider/Drivers/CursorDriver.test.ts‎

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
// @effect-diagnostics nodeBuiltinImport:off
12
import * as NodeServices from "@effect/platform-node/NodeServices";
3+
import * as NodeOS from "node:os";
4+
import * as NodePath from "node:path";
25
import { expect, it } from "@effect/vitest";
36
import { ProviderInstanceId } from "@t3tools/contracts";
47
import { HostProcessPlatform } from "@t3tools/shared/hostProcess";
58
import * as Effect from "effect/Effect";
9+
import * as FileSystem from "effect/FileSystem";
610
import * as Layer from "effect/Layer";
711
import { HttpClient } from "effect/unstable/http";
812
import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner";
@@ -32,26 +36,59 @@ const testLayer = ServerConfig.layerTest(process.cwd(), {
3236
),
3337
);
3438

39+
// The `#!/bin/sh` stub below cannot be resolved as an executable on Windows.
40+
const windowsHost = HostProcessPlatform.defaultValue() === "win32";
41+
3542
it.layer(testLayer)("CursorDriver", (it) => {
36-
it.effect("formats a copied update command for the injected Windows environment", () =>
43+
it.effect.skipIf(windowsHost)(
44+
"quotes a configured executable path in the copyable update command",
45+
() =>
46+
Effect.gen(function* () {
47+
const fs = yield* FileSystem.FileSystem;
48+
const tempDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cursor-driver-" });
49+
const binaryPath = NodePath.join(tempDir, "Cursor Tools", "bin", "cursor-agent");
50+
yield* fs.makeDirectory(NodePath.dirname(binaryPath), { recursive: true });
51+
yield* fs.writeFileString(binaryPath, "#!/bin/sh\n");
52+
yield* fs.chmod(binaryPath, 0o755);
53+
54+
const instance = yield* CursorDriver.create({
55+
instanceId: ProviderInstanceId.make("cursor-copy-command"),
56+
displayName: "Cursor test",
57+
enabled: false,
58+
environment: [],
59+
config: { ...CursorDriver.defaultConfig(), binaryPath },
60+
});
61+
62+
const capabilities = yield* instance.snapshot.resolveMaintenance();
63+
expect(capabilities.update).toMatchObject({
64+
command: `'${binaryPath}' update`,
65+
executable: binaryPath,
66+
args: ["update"],
67+
});
68+
expect((yield* instance.snapshot.refresh).status).toBe("disabled");
69+
}).pipe(
70+
Effect.provideService(
71+
ChildProcessSpawner.ChildProcessSpawner,
72+
ChildProcessSpawner.make(() => Effect.die("Disabled Cursor must not spawn a process")),
73+
),
74+
Effect.scoped,
75+
),
76+
);
77+
78+
it.effect("stays manual-only when the configured executable does not exist", () =>
3779
Effect.gen(function* () {
38-
const binaryPath = "C:\\Users\\Example User\\.local\\bin\\cursor-agent.exe";
3980
const instance = yield* CursorDriver.create({
40-
instanceId: ProviderInstanceId.make("cursor-copy-command"),
81+
instanceId: ProviderInstanceId.make("cursor-missing"),
4182
displayName: "Cursor test",
4283
enabled: false,
4384
environment: [],
44-
config: { ...CursorDriver.defaultConfig(), binaryPath },
45-
});
46-
47-
expect(instance.snapshot.maintenanceCapabilities.update).toMatchObject({
48-
command: `& '${binaryPath}' update`,
49-
executable: binaryPath,
50-
args: ["update"],
85+
config: {
86+
...CursorDriver.defaultConfig(),
87+
binaryPath: NodePath.join(NodeOS.tmpdir(), "t3-cursor-missing", "cursor-agent"),
88+
},
5189
});
52-
expect((yield* instance.snapshot.refresh).status).toBe("disabled");
90+
expect((yield* instance.snapshot.resolveMaintenance()).update).toBeNull();
5391
}).pipe(
54-
Effect.provideService(HostProcessPlatform, "win32"),
5592
Effect.provideService(
5693
ChildProcessSpawner.ChildProcessSpawner,
5794
ChildProcessSpawner.make(() => Effect.die("Disabled Cursor must not spawn a process")),

0 commit comments

Comments
 (0)