Skip to content

Commit 05ec1d6

Browse files
committed
fix(server): Claude probes no longer spawn in the packaged server's home cwd
Packaged desktop builds deliberately start the Node server with cwd = $HOME (DesktopEnvironment's backendCwd). The Claude driver's instance-level auxiliary spawns — the SDK capability probe and the 'claude --version' health check — inherited that directory: the driver handed its startup cwd to the probe, and runClaudeCommand passed no cwd to ChildProcess.make at all. Third-party 'claude' shims that treat their cwd as the project boundary (container runtimes, mount-scoped sandboxes, governance shims) then fail the probe, which runs first and marks the provider Unavailable before any bound project is reached. The driver now passes the server's stateDir — the app's own user-data directory, present in dev, packaged, and WSL alike, and never a project — as the explicit spawn cwd, and runClaudeCommand threads that cwd into ChildProcess.make instead of silently inheriting. This is the narrow option-B fix from the issue: backendCwd (fix A) and the cross-driver env/cwd consolidation are untouched, and the per-workspace probe path (snapshotForCwd) keeps receiving the real workspace cwd. Model: GLM-5.3-Flash (Z.ai) · Harness: ZCode CLI
1 parent a01c48f commit 05ec1d6

5 files changed

Lines changed: 106 additions & 5 deletions

File tree

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,7 @@ const runClaudeCommand = Effect.fn("runClaudeCommand")(function* (
455455
claudeSettings: ClaudeSettings,
456456
args: ReadonlyArray<string>,
457457
environment?: NodeJS.ProcessEnv,
458+
cwd?: string,
458459
) {
459460
const claudeEnvironment = yield* makeClaudeEnvironment(claudeSettings, environment);
460461
const spawnCommand = yield* resolveSpawnCommand(claudeSettings.binaryPath, args, {
@@ -463,6 +464,10 @@ const runClaudeCommand = Effect.fn("runClaudeCommand")(function* (
463464
const command = ChildProcess.make(spawnCommand.command, spawnCommand.args, {
464465
env: claudeEnvironment,
465466
shell: spawnCommand.shell,
467+
// Auxiliary CLI calls must never silently inherit the server's process
468+
// cwd: packaged builds start the server in $HOME, and third-party `claude`
469+
// shims treat their cwd as the project boundary (see issue #2867).
470+
...(cwd ? { cwd } : {}),
466471
});
467472
return yield* spawnAndCollect(claudeSettings.binaryPath, command);
468473
});
@@ -533,6 +538,7 @@ export const checkClaudeProviderStatus = Effect.fn("checkClaudeProviderStatus")(
533538
claudeSettings,
534539
["--version"],
535540
resolvedEnvironment,
541+
cwd,
536542
).pipe(Effect.timeoutOption(DEFAULT_TIMEOUT_MS), Effect.result);
537543

538544
if (Result.isFailure(versionProbe)) {

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,17 @@ export const ClaudeDriver: ProviderDriver<ClaudeSettings, ClaudeDriverEnv, Path.
123123
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
124124
const fileSystem = yield* FileSystem.FileSystem;
125125
const path = yield* Path.Path;
126-
const { cwd } = yield* ServerConfig.ServerConfig;
126+
// The instance-level capability probes are account-scoped, not
127+
// project-scoped, but they must still spawn somewhere explicit. The
128+
// server's startup `cwd` is that spot only by accident: packaged builds
129+
// deliberately start the server in $HOME (DesktopEnvironment's
130+
// `backendCwd`), and third-party `claude` shims treat their cwd as the
131+
// project boundary, so probing from there fails or exposes the home
132+
// directory (issue #2867). The server's `stateDir` is the app's own
133+
// user-data directory — it exists in dev, packaged, and WSL alike and
134+
// is never a project. Per-workspace probes still receive the real
135+
// workspace cwd via `snapshotForCwd`.
136+
const { stateDir } = yield* ServerConfig.ServerConfig;
127137
const httpClient = yield* HttpClient.HttpClient;
128138
const latestVersions = yield* ProviderLatestVersions.ProviderLatestVersions;
129139
const resetCreditCoordinator = yield* ResetCreditCoordinator.ResetCreditCoordinator;
@@ -204,7 +214,7 @@ export const ClaudeDriver: ProviderDriver<ClaudeSettings, ClaudeDriverEnv, Path.
204214
timeToLive: CAPABILITIES_PROBE_TTL,
205215
lookup: () =>
206216
Effect.all([
207-
probeClaudeCapabilities(effectiveConfig, processEnv, cwd),
217+
probeClaudeCapabilities(effectiveConfig, processEnv, stateDir),
208218
ClaudeResetCredits.readClaudeOrganizationId(accountConfigPath),
209219
]).pipe(
210220
Effect.map(([capabilities, workspaceId]) =>
@@ -216,7 +226,7 @@ export const ClaudeDriver: ProviderDriver<ClaudeSettings, ClaudeDriverEnv, Path.
216226
});
217227
const capabilitiesCacheKey = yield* makeClaudeCapabilitiesCacheKey(
218228
effectiveConfig,
219-
cwd,
229+
stateDir,
220230
processEnv,
221231
);
222232

@@ -230,7 +240,7 @@ export const ClaudeDriver: ProviderDriver<ClaudeSettings, ClaudeDriverEnv, Path.
230240
effectiveConfig,
231241
() => Cache.get(capabilitiesProbeCache, capabilitiesCacheKey),
232242
processEnv,
233-
cwd,
243+
stateDir,
234244
catalog,
235245
scopedLimitNames,
236246
(version) =>

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,58 @@ describe("ProviderInstanceRegistry — multi-instance codex slice", () => {
437437
}).pipe(Effect.provide(layerTest)),
438438
);
439439

440+
it.live("spawns Claude capability probes at the server state dir, not the startup cwd", () =>
441+
Effect.gen(function* () {
442+
if (yield* HostProcess.isWindows) return;
443+
444+
const fixtures = yield* makeTildeProviderFixtures();
445+
const fs = yield* FileSystem.FileSystem;
446+
const path = yield* Path.Path;
447+
const cwdLogDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-claude-probe-cwd-" });
448+
const cwdLogPath = path.join(cwdLogDir, "probe-cwds.log");
449+
450+
const claudeId = ProviderInstanceId.make("claude_probe_cwd");
451+
const { registry } = yield* makeProviderInstanceRegistry<ClaudeDriverEnv>({
452+
drivers: [ClaudeDriver],
453+
configMap: {
454+
[claudeId]: {
455+
driver: ProviderDriverKind.make("claudeAgent"),
456+
enabled: true,
457+
environment: [{ name: "T3_FIXTURE_RECORD_CWD", value: cwdLogPath, sensitive: false }],
458+
config: makeClaudeConfig({
459+
enabled: true,
460+
binaryPath: fixtures.claudeBinaryPath,
461+
homePath: fixtures.claudeHomePath,
462+
}),
463+
},
464+
},
465+
});
466+
const claude = yield* registry.getInstance(claudeId);
467+
const claudeSnapshot = yield* claude!.snapshot.refresh;
468+
expect(claudeSnapshot).toMatchObject({
469+
status: "ready",
470+
installed: true,
471+
version: "2.1.219",
472+
});
473+
474+
// The packaged server's startup cwd is $HOME (DesktopEnvironment's
475+
// `backendCwd`), and shimmed `claude` binaries treat their cwd as the
476+
// project boundary — so every instance-level auxiliary spawn must land
477+
// somewhere explicit (the server's own state dir), never in the
478+
// inherited startup cwd (issue #2867).
479+
const { stateDir, cwd: serverStartupCwd } = yield* ServerConfig.ServerConfig;
480+
expect(stateDir).not.toEqual(serverStartupCwd);
481+
// The spawned CLI observes its cwd the way the kernel reports it (real
482+
// path), so compare against the resolved state dir.
483+
const resolvedStateDir = yield* fs.realPath(stateDir);
484+
const recordedCwds = (yield* fs.readFileString(cwdLogPath)).trim().split("\n");
485+
expect(recordedCwds.length).toBeGreaterThan(0);
486+
for (const recordedCwd of recordedCwds) {
487+
expect(recordedCwd).toEqual(resolvedStateDir);
488+
}
489+
}).pipe(Effect.provide(layerTest)),
490+
);
491+
440492
const redeemClaudeReset = (claim: { result: string; usageFailsAfterClaim: boolean }) =>
441493
Effect.gen(function* () {
442494
const fs = yield* FileSystem.FileSystem;

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ function recordingMockSpawnerLayer(
229229
const commands: Array<{
230230
readonly args: ReadonlyArray<string>;
231231
readonly env: NodeJS.ProcessEnv | undefined;
232+
readonly cwd: string | undefined;
232233
}> = [];
233234
const layer = Layer.succeed(
234235
ChildProcessSpawner.ChildProcessSpawner,
@@ -237,9 +238,10 @@ function recordingMockSpawnerLayer(
237238
args: ReadonlyArray<string>;
238239
options?: {
239240
readonly env?: NodeJS.ProcessEnv;
241+
readonly cwd?: string;
240242
};
241243
};
242-
commands.push({ args: cmd.args, env: cmd.options?.env });
244+
commands.push({ args: cmd.args, env: cmd.options?.env, cwd: cmd.options?.cwd });
243245
return Effect.succeed(mockHandle(handler(cmd.args)));
244246
}),
245247
);
@@ -3438,6 +3440,30 @@ it.layer(
34383440
}).pipe(Effect.provide(recorded.layer));
34393441
});
34403442

3443+
it.effect("spawns the version probe at an explicitly provided cwd", () => {
3444+
const recorded = recordingMockSpawnerLayer((args) => {
3445+
const joined = args.join(" ");
3446+
if (joined === "--version") return { stdout: "1.0.0\n", stderr: "", code: 0 };
3447+
throw new Error(`Unexpected args: ${joined}`);
3448+
});
3449+
3450+
return Effect.gen(function* () {
3451+
const status = yield* checkClaudeProviderStatus(
3452+
defaultClaudeSettings,
3453+
claudeCapabilities(),
3454+
undefined,
3455+
"/tmp/t3code-state-dir",
3456+
);
3457+
assert.strictEqual(status.status, "ready");
3458+
// Auxiliary spawns must never inherit the server's process cwd:
3459+
// packaged builds start the server in $HOME (issue #2867).
3460+
assert.deepStrictEqual(
3461+
recorded.commands.map((command) => command.cwd),
3462+
["/tmp/t3code-state-dir"],
3463+
);
3464+
}).pipe(Effect.provide(recorded.layer));
3465+
});
3466+
34413467
it.effect("includes probed claude slash commands in the provider snapshot", () =>
34423468
Effect.gen(function* () {
34433469
const status = yield* checkClaudeProviderStatus(

‎apps/server/src/provider/testing/ProviderInstanceRegistryLive.fixture.mjs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
#!/usr/bin/env node
22
import * as NodeFS from "node:fs";
33
import * as NodeReadline from "node:readline";
4+
// Opt-in test hook: record the cwd this mock CLI was spawned at so tests can
5+
// assert which directory auxiliary provider spawns actually ran in.
6+
if (process.env.T3_FIXTURE_RECORD_CWD) {
7+
try {
8+
NodeFS.appendFileSync(process.env.T3_FIXTURE_RECORD_CWD, `${process.cwd()}\n`);
9+
} catch {}
10+
}
411
if (process.argv.includes("--version")) {
512
process.stdout.write("claude 2.1.219\n");
613
process.exit(0);

0 commit comments

Comments
 (0)