Skip to content

Commit 439ad47

Browse files
OrbitMPGHclaude
andcommitted
fix(web): Running only offers per-task Stop and output where the provider supports them
Addresses review findings on the Running section: - Pending tasks gain individualControl and outputFile, set by the Claude adapter on its Bash tasks. Other providers' tasks and pull request watches keep the details dialog and the whole-thread Stop agent work. - Running reads the active provider thread's roster directly, so commands stay listed while a new turn runs. - shellStdoutFile only follows the last command's stdout, tracks inline cd, and skips comments and heredocs. Paths it cannot place, including Git Bash roots other than /c/ and /tmp on Windows, fall back to Claude's own output. The server, not the client, decides the followed file. - The output stream uses a bounded buffer and ends with a running:false frame. - Claude task and session ids must be plain ids before they are joined into output paths (#26489). - A Stop that the provider never confirms can be retried after 10 seconds. - Service tests for stopBackgroundTask and subscribeBackgroundTaskOutput. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent f1e90d1 commit 439ad47

17 files changed

Lines changed: 558 additions & 131 deletions

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.test.ts‎

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10826,7 +10826,12 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1082610826
const roster = providerThreadRosterEvents(harness.events).at(-1)?.providerThread
1082710827
.pendingBackgroundTasks;
1082810828
assert.deepEqual(roster, [
10829-
{ taskId: longRunning.taskId, kind: "monitor", description: "Monitor 0" },
10829+
{
10830+
taskId: longRunning.taskId,
10831+
kind: "monitor",
10832+
description: "Monitor 0",
10833+
individualControl: true,
10834+
},
1083010835
]);
1083110836
}).pipe(
1083210837
Effect.provide(
@@ -11533,13 +11538,16 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1153311538
]) {
1153411539
yield* Queue.offer(harness.sdkMessages, frame);
1153511540
}
11536-
while (
11537-
!(yield* Queue.take(
11541+
let listed: OrchestrationV2PendingBackgroundTask | undefined;
11542+
while (listed === undefined) {
11543+
listed = (yield* Queue.take(
1153811544
harness.providerThreadReceipts,
11539-
)).providerThread.pendingBackgroundTasks?.some(
11545+
)).providerThread.pendingBackgroundTasks?.find(
1154011546
(task) => task.taskId === "redirected-task",
11541-
)
11542-
);
11547+
);
11548+
}
11549+
// Clients label the output with the file the server follows.
11550+
assert.equal(listed.outputFile, logFile);
1154311551
const redirected = { ...target, taskId: "redirected-task" };
1154411552
assert.deepEqual(
1154511553
Array.from(yield* Stream.runCollect(output(redirected).pipe(Stream.take(1)))),
@@ -12245,7 +12253,7 @@ describe("ClaudeAdapterV2 background wake turns", () => {
1224512253
(event) => event.providerThread.id === providerThreadA.id,
1224612254
)?.providerThread.pendingBackgroundTasks;
1224712255
assert.deepEqual(rosterAAfterSettle ?? [], [
12248-
{ taskId: taskA, description: "work on A", kind: "command" },
12256+
{ taskId: taskA, description: "work on A", kind: "command", individualControl: true },
1224912257
]);
1225012258
assert.isTrue(yield* hasPendingBackgroundWork);
1225112259
assert.isTrue(yield* hasPendingBackgroundWorkForThread(providerThreadA));

‎apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts‎

Lines changed: 72 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
7777
import * as NodeChildProcess from "node:child_process";
78+
import * as NodeOS from "node:os";
7879
import * as NodeUtil from "node:util";
7980
import { parseCliArgs } from "@t3tools/shared/cliArgs";
80-
import { shellStdoutFile } from "@t3tools/shared/shellStdoutFile";
81+
import { shellStdoutFile, type ShellStdoutFile } from "@t3tools/shared/shellStdoutFile";
8182
import { fromLenientJson } from "@t3tools/shared/schemaJson";
8283
import {
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 }) {

‎apps/server/src/orchestration-v2/Adapters/ClaudeTaskOutputTail.test.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,39 @@ const makeTaskFile = Effect.gen(function* () {
2222

2323
// TestClock cannot interleave with real filesystem I/O; tests that wait on a poll use the live clock.
2424
describe("tailClaudeTaskOutput", () => {
25+
it.effect("reads nothing for ids that would leave the session's tasks folder", () =>
26+
Effect.scoped(
27+
Effect.gen(function* () {
28+
const fileSystem = yield* FileSystem.FileSystem;
29+
const path = yield* Path.Path;
30+
const { root } = yield* makeTaskFile;
31+
// Where `../../secret` as a task id, and `..` as a session id, would lead.
32+
for (const outside of [
33+
path.join(root, "-private-tmp-project", "secret.output"),
34+
path.join(root, "tasks", "secret.output"),
35+
]) {
36+
yield* fileSystem.makeDirectory(path.dirname(outside), { recursive: true });
37+
yield* fileSystem.writeFileString(outside, "secret\n");
38+
}
39+
const stop = yield* Deferred.make<void>();
40+
yield* Deferred.succeed(stop, undefined);
41+
const chunks: Array<string> = [];
42+
for (const ids of [
43+
{ sessionId: SESSION_ID, taskId: "../../secret" },
44+
{ sessionId: "..", taskId: "secret" },
45+
]) {
46+
yield* tailClaudeTaskOutput({
47+
root,
48+
...ids,
49+
stop,
50+
onChunk: (chunk) => Effect.sync(() => chunks.push(chunk)),
51+
});
52+
}
53+
assert.deepEqual(chunks, []);
54+
}),
55+
).pipe(Effect.provide(NodeServices.layer)),
56+
);
57+
2558
it.live("follows a file that appears after the task starts, across split characters", () =>
2659
Effect.scoped(
2760
Effect.gen(function* () {

‎apps/server/src/orchestration-v2/Adapters/ClaudeTaskOutputTail.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import * as Path from "effect/Path";
1212
const CLAUDE_TASK_OUTPUT_POLL_MS = 250;
1313
/** Bound discovery when Claude never writes the file. */
1414
const MAX_LOCATE_ATTEMPTS = 40;
15+
const CLAUDE_TASK_PATH_ID = /^[\w-]+$/;
1516
/** Skip ahead of fast writers; only the tail is displayed. */
1617
const MAX_READ_BYTES = 256 * 1024;
1718

@@ -50,6 +51,10 @@ const locate = Effect.fn("ClaudeTaskOutputTail.locate")(function* (input: {
5051
readonly sessionId: string;
5152
readonly taskId: string;
5253
}) {
54+
// Both ids come from the provider stream; anything but a plain id could leave the root.
55+
if (!CLAUDE_TASK_PATH_ID.test(input.sessionId) || !CLAUDE_TASK_PATH_ID.test(input.taskId)) {
56+
return Option.none<string>();
57+
}
5358
const fileSystem = yield* FileSystem.FileSystem;
5459
const path = yield* Path.Path;
5560
const projects = yield* fileSystem.readDirectory(input.root).pipe(Effect.orElseSucceed(() => []));

0 commit comments

Comments
 (0)