diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts index 1941620cd8b01..e4b05df825d0b 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts @@ -510,6 +510,43 @@ function claudeDescendants( return found; } +/** What a CLI started, by pid and start time, recorded from each process table read while it runs. */ +export function makeClaudeDescendantTracker( + cliPid: number, + readProcessTable: Effect.Effect>, +) { + const tracked = new Map(); + const identity = (row: ClaudeProcessRow) => `${row.pid} ${row.started}`; + // The poll, a session close and the exit read one at a time, so each table is newer than all + // recorded here, and a pid and start time it no longer lists never run again. + const reading = Semaphore.makeUnsafe(1); + // After exit the CLI's pid may belong to an unrelated process, so only tracked jobs are roots. + const refresh = Effect.fn("ClaudeAgentSdkQueryRunner.trackDescendants")( + function* (cliRunning: boolean) { + const rows = yield* readProcessTable; + const running = new Set(rows.map(identity)); + // `ps -A` lists itself, so no rows is a failed read. + if (rows.length === 0) return running; + const roots = cliRunning ? [cliPid] : []; + for (const [key, row] of tracked) { + if (running.has(key)) roots.push(row.pid); + else tracked.delete(key); + } + for (const row of claudeDescendants(rows, roots)) tracked.set(identity(row), row); + return running; + }, + (effect) => reading.withPermits(1)(effect), + ); + return { + tracked: tracked as ReadonlyMap, + refresh: refresh(true).pipe(Effect.asVoid), + /** Reads the table after the CLI exited: what it left that is still the same process. */ + leftovers: refresh(false).pipe( + Effect.map((running) => [...tracked.values()].filter((row) => running.has(identity(row)))), + ), + }; +} + export const ClaudeProviderCapabilitiesV2 = { sessions: { supportsMultipleProviderThreadsPerSession: false, @@ -1429,29 +1466,13 @@ export const layerQueryRunner: Layer.Layer< // re-parented once the CLI exits. So the CLI's tree is recorded while it runs, and what is // still the same process (pid and start time) is stopped after it exits. const trackDescendants = (cliPid: number) => { - const tracked = new Map(); - const identity = (row: ClaudeProcessRow) => `${row.pid} ${row.started}`; - // After exit the CLI's pid may belong to an unrelated process, so only tracked jobs are roots. - const refresh = Effect.fn("ClaudeAgentSdkQueryRunner.trackDescendants")(function* ( - cliRunning: boolean, - ) { - const rows = yield* readProcessTable; - const running = new Set(rows.map(identity)); - const roots = [ - ...(cliRunning ? [cliPid] : []), - ...[...tracked.values()].flatMap((row) => (running.has(identity(row)) ? [row.pid] : [])), - ]; - for (const row of claudeDescendants(rows, roots)) tracked.set(identity(row), row); - return running; - }); - const stop = refresh(false).pipe( - Effect.map((running) => { - for (const row of tracked.values()) { - if (running.has(identity(row))) killClaudeDescendant(row); - } + const tracker = makeClaudeDescendantTracker(cliPid, readProcessTable); + const stop = tracker.leftovers.pipe( + Effect.map((rows) => { + for (const row of rows) killClaudeDescendant(row); }), ); - return { refresh: refresh(true).pipe(Effect.asVoid), stop }; + return { refresh: tracker.refresh, stop }; }; // The open span below is the tracing boundary for both attempts. diff --git a/apps/server/src/orchestration-v2/Adapters/ClaudeAgentSdkQueryRunner.test.ts b/apps/server/src/orchestration-v2/Adapters/ClaudeAgentSdkQueryRunner.test.ts index f1ef42a005e41..af3242f668063 100644 --- a/apps/server/src/orchestration-v2/Adapters/ClaudeAgentSdkQueryRunner.test.ts +++ b/apps/server/src/orchestration-v2/Adapters/ClaudeAgentSdkQueryRunner.test.ts @@ -31,6 +31,7 @@ import { ClaudeAgentSdkQueryRunner, type ClaudeAgentSdkQueryOpenInput, layerQueryRunner, + makeClaudeDescendantTracker, spawnClaudeProcessGroup, } from "./ClaudeAdapterV2.ts"; @@ -692,6 +693,124 @@ describe.skipIf(HostProcess.Platform.defaultValue() === "win32")("Claude CLI pro }), ); + const processRow = (pid: number, ppid: number, startedSecond: number) => ({ + pid, + ppid, + pgid: pid, + started: `Sun Nov 15 10:00:0${startedSecond} 2026`, + }); + const byNumber = (left: number, right: number) => left - right; + + it.effect( + "keeps only the descendants the CLI still has running, however many jobs it ran", + () => { + let table: ReadonlyArray> = []; + const tracker = makeClaudeDescendantTracker( + 1000, + Effect.sync(() => table), + ); + const jobs = (poll: number) => [0, 1, 2, 3].map((job) => 1100 + poll * 4 + job); + return Effect.gen(function* () { + for (let poll = 0; poll < 50; poll++) { + table = [ + processRow(1000, 10, 0), + processRow(1001, 1000, 0), + // A job that outlives the shell that started it is re-parented. + ...(poll === 0 ? [processRow(1003, 1000, 0)] : []), + processRow(1002, poll === 0 ? 1003 : 1, 0), + ...jobs(poll).map((pid) => processRow(pid, 1001, 1)), + ]; + yield* tracker.refresh; + // A failed `ps` reads as no rows. + table = []; + yield* tracker.refresh; + } + assert.strictEqual(tracker.tracked.size, 6); + assert.deepStrictEqual( + [...tracker.tracked.values()].map((row) => row.pid).toSorted(byNumber), + [1001, 1002, ...jobs(49)], + ); + }); + }, + ); + + it.effect( + "stops at exit only what the CLI left running, never a process that took a job's pid", + () => { + const orphan = processRow(1300, 1, 1); + // Unrelated processes take both finished jobs' pids, one in the second its job started. + const unrelated = [processRow(1100, 1, 2), processRow(1200, 1, 1)]; + const tables = [ + [ + processRow(1000, 10, 0), + processRow(1001, 1000, 0), + processRow(1100, 1001, 1), + processRow(1200, 1001, 1), + processRow(1300, 1001, 1), + ], + [processRow(1000, 10, 0), orphan], + [processRow(1000, 10, 0), ...unrelated, orphan], + [...unrelated, orphan], + ]; + const tracker = makeClaudeDescendantTracker( + 1000, + Effect.sync(() => tables.shift() ?? []), + ); + return Effect.gen(function* () { + yield* tracker.refresh; + yield* tracker.refresh; + yield* tracker.refresh; + assert.deepStrictEqual( + (yield* tracker.leftovers).map((row) => row.pid), + [1300], + ); + }); + }, + ); + + // The poll and a session close read the table independently. The older table still lists a + // re-parented job and the child it just started; the newer one lists a job started since. + it.effect( + "stops at exit what the poll and a session close found, reading at the same time", + () => { + type Table = ReadonlyArray>; + const cli = processRow(1000, 10, 0); + const olderReadStarted = Deferred.makeUnsafe(); + const olderRead = Deferred.makeUnsafe(); + let read: Effect.Effect
= Effect.succeed([ + cli, + processRow(1001, 1000, 0), + processRow(1100, 1001, 1), + ]); + const tracker = makeClaudeDescendantTracker( + 1000, + Effect.suspend(() => read), + ); + return Effect.gen(function* () { + yield* tracker.refresh; + read = Deferred.succeed(olderReadStarted, undefined).pipe( + Effect.andThen(Deferred.await(olderRead)), + ); + const older = yield* tracker.refresh.pipe(Effect.forkChild); + yield* Deferred.await(olderReadStarted); + read = Effect.succeed([cli, processRow(1200, 1, 1), processRow(1300, 1000, 2)]); + const newer = yield* tracker.refresh.pipe(Effect.forkChild({ startImmediately: true })); + yield* Deferred.succeed(olderRead, [ + cli, + processRow(1100, 1, 1), + processRow(1200, 1100, 1), + ]); + yield* Fiber.join(older); + yield* Fiber.join(newer); + read = Effect.succeed([processRow(1200, 1, 1), processRow(1300, 1, 2)]); + assert.deepStrictEqual( + (yield* tracker.leftovers).map((row) => row.pid).toSorted(byNumber), + [1200, 1300], + ); + }); + }, + ); + const rootRefusal = "--dangerously-skip-permissions cannot be used with root/sudo privileges for security reasons";