@@ -75,9 +75,10 @@ import type {
7575} from "@anthropic-ai/claude-agent-sdk/sdk-tools";
7676// @effect-diagnostics-next-line nodeBuiltinImport:off -- the SDK takes a Node ChildProcess for the CLI.
7777import * as NodeChildProcess from "node:child_process";
78+ import * as NodeOS from "node:os";
7879import * as NodeUtil from "node:util";
7980import { parseCliArgs } from "@t3tools/shared/cliArgs";
80- import { shellStdoutFile } from "@t3tools/shared/shellStdoutFile";
81+ import { shellStdoutFile, type ShellStdoutFile } from "@t3tools/shared/shellStdoutFile";
8182import { fromLenientJson } from "@t3tools/shared/schemaJson";
8283import {
8384 claudeMcpToolName,
@@ -3244,6 +3245,8 @@ function claudePendingBackgroundTask(input: {
32443245}): OrchestrationV2PendingBackgroundTask {
32453246 return {
32463247 taskId: input.taskId,
3248+ // Every roster task is a local_bash task, which stop_task ends and which writes an output file.
3249+ individualControl: true,
32473250 kind: input.startedByMonitor
32483251 ? "monitor"
32493252 : ((input.taskType === null
@@ -5211,12 +5214,28 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
52115214 | { readonly kind: "redirect"; readonly filePath: string }
52125215 | { readonly kind: "claude"; readonly root: string; readonly sessionId: string }
52135216 >();
5214- // Git Bash on Windows spells C:\ as /c/.
5215- const resolveRedirectTarget = (target: string, cwd: string | undefined) =>
5216- path.resolve(
5217- cwd ?? ".",
5218- hostPlatform === "win32" ? target.replace(/^\/([a-zA-Z])(?=\/|$)/, "$1:") : target,
5219- );
5217+ // Git Bash on Windows spells C:\ as /c/ and mounts /tmp at the Windows temp dir. Its other
5218+ // POSIX roots live inside its install, so those paths stay unresolved.
5219+ const windowsPath = (target: string) => {
5220+ if (hostPlatform !== "win32" || !target.startsWith("/")) return target;
5221+ const drive = /^\/([a-zA-Z])(\/.*)?$/.exec(target);
5222+ if (drive) return `${drive[1]}:${drive[2] ?? "/"}`;
5223+ const temp = /^\/tmp(\/.*)?$/.exec(target);
5224+ if (!temp) return undefined;
5225+ const tempDir =
5226+ adapterOptions.environment.TEMP || adapterOptions.environment.TMP || NodeOS.tmpdir();
5227+ return path.join(tempDir, temp[1] ?? "");
5228+ };
5229+ const resolveStdoutFile = (stdout: ShellStdoutFile, cwd: string | undefined) => {
5230+ let directory = cwd ?? ".";
5231+ for (const change of stdout.cwdChanges) {
5232+ const next = windowsPath(change);
5233+ if (next === undefined) return undefined;
5234+ directory = path.resolve(directory, next);
5235+ }
5236+ const file = windowsPath(stdout.path);
5237+ return file === undefined ? undefined : path.resolve(directory, file);
5238+ };
52205239 // Waits for the tail's last read so its output lands before the call settles.
52215240 const stopTaskOutputTail = (toolUseId: string) =>
52225241 Effect.suspend(() => {
@@ -8947,7 +8966,9 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
89478966 launch?.classification.itemType === "command_execution"
89488967 ? firstStringInputField(launch.input, ["command"])
89498968 : undefined;
8969+ const output = taskOutputFiles.get(message.task_id);
89508970 yield* upsertPendingBackgroundTask(input.nativeThreadId, {
8971+ ...(output?.kind === "redirect" ? { outputFile: output.filePath } : {}),
89518972 ...(launch === undefined || command === undefined
89528973 ? {}
89538974 : {
@@ -9745,13 +9766,13 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
97459766 launch?.classification.itemType === "command_execution"
97469767 ? firstStringInputField(launch.input, ["command"])
97479768 : undefined;
9748- const redirect = command === undefined ? undefined : shellStdoutFile(command);
9769+ const stdout = command === undefined ? undefined : shellStdoutFile(command);
9770+ // A file it cannot place falls back to Claude's capture rather than a guessed path.
9771+ const redirect =
9772+ stdout === undefined ? undefined : resolveStdoutFile(stdout, liveQuery.bashCwd);
97499773 const output =
97509774 redirect !== undefined
9751- ? {
9752- kind: "redirect" as const,
9753- filePath: resolveRedirectTarget(redirect, liveQuery.bashCwd),
9754- }
9775+ ? { kind: "redirect" as const, filePath: redirect }
97559776 : liveQuery.taskOutputRoot !== undefined
97569777 ? {
97579778 kind: "claude" as const,
@@ -12444,42 +12465,45 @@ export const makeClaudeAdapterV2 = Effect.fn("makeClaudeAdapterV2")(function* (
1244412465 );
1244512466 }),
1244612467 backgroundTaskOutput: ({ providerThread, taskId }) =>
12447- Stream.callback<string, ProviderAdapter.ProviderAdapterV2Error>((queue) =>
12448- Effect.gen(function* () {
12449- const nativeThreadId = providerThread.nativeThreadRef?.nativeId;
12450- const file = taskOutputFiles.get(taskId);
12451- if (
12452- nativeThreadId == null ||
12453- file === undefined ||
12454- !(yield* hasPendingBackgroundTaskOnNativeThread(nativeThreadId, taskId))
12455- ) {
12456- return yield* new ProviderAdapter.ProviderAdapterProtocolError({
12457- driver: CLAUDE_PROVIDER,
12458- detail: "No readable background task output belongs to this provider thread",
12459- });
12460- }
12461- const stop = yield* Deferred.make<void>();
12462- yield* Effect.addFinalizer(() => Deferred.succeed(stop, undefined));
12463- const onChunk = (chunk: string) => Queue.offer(queue, chunk).pipe(Effect.asVoid);
12464- // Only a path from a command this thread's agent ran is ever read here.
12465- // Claude's capture ends the stream when Claude never writes it.
12466- yield* (
12467- file.kind === "redirect"
12468- ? tailOutputFile({ filePath: file.filePath, stop, onChunk })
12469- : tailClaudeTaskOutput({
12470- root: file.root,
12471- sessionId: file.sessionId,
12472- taskId,
12473- stop,
12474- onChunk,
12475- })
12476- ).pipe(
12477- Effect.andThen(Queue.end(queue)),
12478- Effect.provideService(FileSystem.FileSystem, fileSystem),
12479- Effect.provideService(Path.Path, path),
12480- Effect.forkScoped,
12481- );
12482- }),
12468+ Stream.callback<string, ProviderAdapter.ProviderAdapterV2Error>(
12469+ (queue) =>
12470+ Effect.gen(function* () {
12471+ const nativeThreadId = providerThread.nativeThreadRef?.nativeId;
12472+ const file = taskOutputFiles.get(taskId);
12473+ if (
12474+ nativeThreadId == null ||
12475+ file === undefined ||
12476+ !(yield* hasPendingBackgroundTaskOnNativeThread(nativeThreadId, taskId))
12477+ ) {
12478+ return yield* new ProviderAdapter.ProviderAdapterProtocolError({
12479+ driver: CLAUDE_PROVIDER,
12480+ detail: "No readable background task output belongs to this provider thread",
12481+ });
12482+ }
12483+ const stop = yield* Deferred.make<void>();
12484+ yield* Effect.addFinalizer(() => Deferred.succeed(stop, undefined));
12485+ const onChunk = (chunk: string) => Queue.offer(queue, chunk).pipe(Effect.asVoid);
12486+ // Only a path from a command this thread's agent ran is ever read here.
12487+ // Claude's capture ends the stream when Claude never writes it.
12488+ yield* (
12489+ file.kind === "redirect"
12490+ ? tailOutputFile({ filePath: file.filePath, stop, onChunk })
12491+ : tailClaudeTaskOutput({
12492+ root: file.root,
12493+ sessionId: file.sessionId,
12494+ taskId,
12495+ stop,
12496+ onChunk,
12497+ })
12498+ ).pipe(
12499+ Effect.andThen(Queue.end(queue)),
12500+ Effect.provideService(FileSystem.FileSystem, fileSystem),
12501+ Effect.provideService(Path.Path, path),
12502+ Effect.forkScoped,
12503+ );
12504+ }),
12505+ // A slow viewer pauses the tail instead of queueing reads without bound.
12506+ { bufferSize: 16, strategy: "suspend" },
1248312507 ),
1248412508 awaitBackgroundTasks: Effect.fn("ClaudeAdapterV2.awaitBackgroundTasks")(
1248512509 function* ({ providerThread, taskIds, awaited }) {
0 commit comments