Skip to content
TypeScript

fix(claude): stream live command output on Linux when TMPDIR is set (#24176)

Victor Vogeld1777ea2f4f3
diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts
index 07124b02406..2ea6b7b8c2e 100644
--- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts
+++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts
@@ -8385,6 +8385,116 @@ describe("ClaudeAdapterV2 background wake turns", () => {
),
);
+ it.effect.skipIf(process.getuid === undefined)(
+ "tails foreground Bash output under the physical cwd the query launched in",
+ () =>
+ Effect.scoped(
+ Effect.gen(function* () {
+ const fileSystem = yield* FileSystem.FileSystem;
+ const path = yield* Path.Path;
+ const root = yield* fileSystem.makeTempDirectoryScoped({
+ prefix: "t3-claude-task-output-cwd-",
+ });
+ const physicalCwd = path.join(root, "real", "project");
+ const launchCwd = path.join(root, "link");
+ yield* fileSystem.makeDirectory(physicalCwd, { recursive: true });
+ yield* fileSystem.symlink(physicalCwd, launchCwd);
+ // The session opens in /workspace; a relative TMPDIR puts Claude's
+ // task files under whichever cwd the CLI was launched in. Linux,
+ // because Claude Code's macOS build ignores TMPDIR.
+ const harness = yield* makeWakeHarnessWithOptions({ environment: { TMPDIR: "../tmp" } });
+ const now = yield* DateTime.now;
+ const toolUseId = "toolu_foreground_bash";
+ const taskId = "foreground-bash-task";
+ const tasks = path.join(
+ root,
+ "real",
+ "tmp",
+ `claude-${process.getuid?.()}`,
+ "project",
+ WAKE_NATIVE_SESSION,
+ "tasks",
+ );
+ yield* fileSystem.makeDirectory(tasks, { recursive: true });
+ yield* fileSystem.writeFileString(path.join(tasks, `${taskId}.output`), "line 1\n");
+
+ yield* harness.runtime.startTurn(
+ makeClaudeTestTurnInput({
+ threadId: harness.threadId,
+ providerThread: harness.providerThread,
+ now,
+ attemptId: RunAttemptId.make("attempt-claude-task-output-cwd"),
+ text: "Run the build.",
+ attachments: [],
+ runtimePolicy: { ...CLAUDE_TEST_RUNTIME_POLICY, cwd: launchCwd },
+ }),
+ );
+ assert.equal(harness.getOpenedOptions()?.cwd, launchCwd);
+ for (const frame of [
+ claudeSdkFrame({
+ type: "assistant",
+ message: {
+ model: "claude-sonnet-4-6",
+ id: "msg_foreground_bash",
+ type: "message",
+ role: "assistant",
+ content: [
+ {
+ type: "tool_use",
+ id: toolUseId,
+ name: "Bash",
+ input: { command: "vp run build", description: "Build" },
+ },
+ ],
+ stop_reason: null,
+ stop_sequence: null,
+ usage: { input_tokens: 1, output_tokens: 1 },
+ },
+ parent_tool_use_id: null,
+ uuid: "00000000-0000-4000-8000-000000000a01",
+ session_id: WAKE_NATIVE_SESSION,
+ }),
+ claudeSdkFrame({
+ type: "system",
+ subtype: "task_started",
+ task_id: taskId,
+ tool_use_id: toolUseId,
+ description: "vp run build",
+ is_backgrounded: false,
+ task_type: "local_bash",
+ uuid: "00000000-0000-4000-8000-000000000a02",
+ session_id: WAKE_NATIVE_SESSION,
+ }),
+ claudeSdkFrame({
+ type: "user",
+ message: {
+ role: "user",
+ content: [{ type: "tool_result", tool_use_id: toolUseId, content: "line 1" }],
+ },
+ parent_tool_use_id: null,
+ uuid: "00000000-0000-4000-8000-000000000a03",
+ session_id: WAKE_NATIVE_SESSION,
+ tool_use_result: { stdout: "line 1\n", stderr: "", interrupted: false },
+ }),
+ makeResultFrame({ uuid: "00000000-0000-4000-8000-000000000a04", result: "Built." }),
+ ]) {
+ yield* Queue.offer(harness.sdkMessages, frame);
+ }
+ yield* harness.awaitTerminal(1);
+
+ const chunks = harness.events.flatMap((event) =>
+ event.type === "command_output.delta" ? [event.chunk] : [],
+ );
+ assert.deepEqual(chunks, ["line 1\n"]);
+ }).pipe(
+ Effect.provideService(HostProcess.Platform, "linux"),
+ Effect.provide(
+ Layer.mergeAll(IdAllocator.layer, McpProviderSessions.layer, NodeServices.layer),
+ ),
+ ),
+ ),
+ );
+
it.effect("preserves settled background work until an explicit stop", () =>
Effect.scoped(
Effect.gen(function* () {
diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.testkit.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.testkit.ts
index 082cae55de8..c975e022b37 100644
--- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.testkit.ts
+++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.testkit.ts
@@ -992,7 +992,12 @@ const writeReplayTaskOutputFiles = Effect.fn("ClaudeReplay.writeTaskOutputFiles"
const stderr = typeof result.stderr === "string" ? result.stderr : "";
outputByToolUseId.set(toolResult.tool_use_id, `${result.stdout}${stderr}`);
}
- const root = claudeTaskOutputRoot({ CLAUDE_CODE_TMPDIR: tmpRoot }, path);
+ const root = claudeTaskOutputRoot(
+ { CLAUDE_CODE_TMPDIR: tmpRoot },
+ path,
+ null,
+ yield* HostProcess.Platform,
+ );
for (const frame of frames) {
if (frame.type !== "system" || frame.subtype !== "task_started") continue;
if (frame.task_type !== "local_bash" || frame.is_backgrounded !== false) continue;
diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
index b1a94c2e1c0..df2c08a8445 100644
--- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
+++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
@@ -4042,6 +4042,9 @@ interface ClaudeLiveQueryContext {
permissionMode: PermissionMode;
stopping: boolean;
readonly subagentsFromEarlierProcesses: ReadonlySet<ActiveClaudeSubagent>;
+ // Per process, not per session: a relative temp dir follows the cwd this CLI
+ // was launched in, and a later turn's cwd can differ.
+ readonly taskOutputRoot: string | undefined;
}
interface ActiveClaudeToolCall {
@@ -4470,6 +4473,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
adapterOptions.environment,
).pipe(Effect.provideService(Path.Path, path));
const hostEnvironment = yield* HostProcess.Environment;
+ const hostPlatform = yield* HostProcess.Platform;
const customHome =
adapterOptions.settings.homePath.trim().length > 0 ||
adapterOptions.environment.CLAUDE_CONFIG_DIR !== hostEnvironment.CLAUDE_CONFIG_DIR;
@@ -4647,7 +4651,6 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
string,
{ readonly fiber: Fiber.Fiber<void>; readonly stop: Deferred.Deferred<void> }
>();
- const taskOutputRoot = claudeTaskOutputRoot(adapterOptions.environment, path);
// Waits for the tail's last read so its output lands before the call settles.
const stopTaskOutputTail = (toolUseId: string) =>
Effect.suspend(() => {
@@ -8785,7 +8788,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
? undefined
: findToolCall(context, message.tool_use_id);
if (
- taskOutputRoot !== undefined &&
+ liveQuery.taskOutputRoot !== undefined &&
message.task_type === "local_bash" &&
// Older Claude Code builds omit the flag on foreground calls.
message.is_backgrounded !== true &&
@@ -8798,7 +8801,7 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
});
const stop = yield* Deferred.make<void>();
const fiber = yield* tailClaudeTaskOutput({
- root: taskOutputRoot,
+ root: liveQuery.taskOutputRoot,
sessionId: message.session_id,
taskId: message.task_id,
stop,
@@ -10199,6 +10202,9 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
...(appendSystemPromptFileText === undefined ? {} : { appendSystemPromptFileText }),
...(toolSearchOverride === undefined ? {} : { toolSearchOverride }),
});
+ const resolvedTurnCwd = yield* fileSystem
+ .realPath(turnInput.runtimePolicy.cwd ?? ".")
+ .pipe(Effect.orElseSucceed(() => turnInput.runtimePolicy.cwd));
const querySession = yield* queryRunner
.open({
threadId: turnInput.threadId,
@@ -10248,6 +10254,12 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
(subagent) => subagent.task.status === "running",
),
),
+ taskOutputRoot: claudeTaskOutputRoot(
+ adapterOptions.environment,
+ path,
+ resolvedTurnCwd,
+ hostPlatform,
+ ),
};
yield* Ref.set(queryContext, context);
yield* querySession.messages.pipe(
diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeTaskOutputTail.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeTaskOutputTail.test.ts
index 87992bab891..8da21b20236 100644
--- a/apps/server/src/orchestration-v2/Adapters/ClaudeTaskOutputTail.test.ts
+++ b/apps/server/src/orchestration-v2/Adapters/ClaudeTaskOutputTail.test.ts
@@ -7,7 +7,7 @@ import * as FileSystem from "effect/FileSystem";
import * as Path from "effect/Path";
import * as Queue from "effect/Queue";
-import { tailClaudeTaskOutput } from "./ClaudeTaskOutputTail.ts";
+import { claudeTaskOutputRoot, tailClaudeTaskOutput } from "./ClaudeTaskOutputTail.ts";
const SESSION_ID = "0c91bb28-9e89-4889-bd22-3de9880233d3";
const TASK_ID = "bk6xreeye";
@@ -103,3 +103,39 @@ describe("tailClaudeTaskOutput", () => {
),
);
});
+
+describe("claudeTaskOutputRoot", () => {
+ it.effect.skipIf(process.getuid === undefined)(
+ "uses the temp dir Claude Code resolves from its environment on each platform",
+ () =>
+ Effect.gen(function* () {
+ const path = yield* Path.Path;
+ const user = `claude-${process.getuid?.()}`;
+ const root = (
+ environment: Record<string, string>,
+ platform: NodeJS.Platform,
+ cwd: string | null = null,
+ ) => claudeTaskOutputRoot(environment, path, cwd, platform);
+ assert.equal(root({ TMPDIR: "/var/folders/x/T/" }, "linux"), `/var/folders/x/T/${user}`);
+ assert.equal(root({ TMP: "/tmp/user/1000" }, "linux"), `/tmp/user/1000/${user}`);
+ assert.equal(root({}, "linux"), `/tmp/${user}`);
+ assert.equal(root({ TMPDIR: "/var/folders/x/T/" }, "darwin"), `/tmp/${user}`);
+ assert.equal(
+ root({ CLAUDE_CODE_TMPDIR: "/srv/claude", TMPDIR: "/var/folders/x/T/" }, "linux"),
+ `/srv/claude/${user}`,
+ );
+ assert.equal(
+ root({ CLAUDE_CODE_TMPDIR: "/srv/claude", TMPDIR: "/var/folders/x/T/" }, "darwin"),
+ `/srv/claude/${user}`,
+ );
+ assert.equal(
+ root({ TMPDIR: "tmp" }, "linux", "/work/project"),
+ `/work/project/tmp/${user}`,
+ );
+ assert.equal(
+ root({ CLAUDE_CODE_TMPDIR: "../claude" }, "darwin", "/work/project"),
+ `/work/claude/${user}`,
+ );
+ }).pipe(Effect.provide(NodeServices.layer)),
+ );
+});
diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeTaskOutputTail.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeTaskOutputTail.ts
index ff096cdad29..0bd369eb4df 100644
--- a/apps/server/src/orchestration-v2/Adapters/ClaudeTaskOutputTail.ts
+++ b/apps/server/src/orchestration-v2/Adapters/ClaudeTaskOutputTail.ts
@@ -17,11 +17,21 @@ const MAX_READ_BYTES = 256 * 1024;
export function claudeTaskOutputRoot(
environment: Readonly<Record<string, string | undefined>> | undefined,
path: Path.Path,
+ cwd: string | null,
+ platform: NodeJS.Platform,
): string | undefined {
const uid = process.getuid?.();
if (uid === undefined) return undefined;
- const base = environment?.CLAUDE_CODE_TMPDIR?.trim() || "/tmp";
- return path.join(base, `claude-${uid}`);
+ // Claude Code's macOS build falls back to /tmp and ignores the TMPDIR that
+ // launchd always sets. Its other builds use `os.tmpdir()`, which reads these
+ // from the environment it was launched with. A relative value is relative
+ // to Claude's working directory, not this server's.
+ const fallback =
+ platform === "darwin"
+ ? "/tmp"
+ : environment?.TMPDIR || environment?.TMP || environment?.TEMP || "/tmp";
+ const base = environment?.CLAUDE_CODE_TMPDIR?.trim() || fallback;
+ return path.resolve(cwd ?? ".", base, `claude-${uid}`);
}
const locate = Effect.fn("ClaudeTaskOutputTail.locate")(function* (input: {