@devries-cameron20 / t3code-1 / commits / 6b73ca9e4b
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(server): the Claude process poll forgets jobs that have ended (#38800)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

Navid Nadali committed Nov 19, 2026 at 06:13 UTC 6b73ca9e4bef9a51e5b38c55a20ec974e36a4398
2 files changed +161 -21
apps/server/src/orchestration-v2/Adapters/ClaudeAdapterV2.ts
+42 -21
@@ -510,6 +510,43 @@ function claudeDescendants(
510 return found;
511 }
512
513 +/** What a CLI started, by pid and start time, recorded from each process table read while it runs. */
514 +export function makeClaudeDescendantTracker(
515 + cliPid: number,
516 + readProcessTable: Effect.Effect<ReadonlyArray<ClaudeProcessRow>>,
517 +) {
518 + const tracked = new Map<string, ClaudeProcessRow>();
519 + const identity = (row: ClaudeProcessRow) => `${row.pid} ${row.started}`;
520 + // The poll, a session close and the exit read one at a time, so each table is newer than all
521 + // recorded here, and a pid and start time it no longer lists never run again.
522 + const reading = Semaphore.makeUnsafe(1);
523 + // After exit the CLI's pid may belong to an unrelated process, so only tracked jobs are roots.
524 + const refresh = Effect.fn("ClaudeAgentSdkQueryRunner.trackDescendants")(
525 + function* (cliRunning: boolean) {
526 + const rows = yield* readProcessTable;
527 + const running = new Set(rows.map(identity));
528 + // `ps -A` lists itself, so no rows is a failed read.
529 + if (rows.length === 0) return running;
530 + const roots = cliRunning ? [cliPid] : [];
531 + for (const [key, row] of tracked) {
532 + if (running.has(key)) roots.push(row.pid);
533 + else tracked.delete(key);
534 + }
535 + for (const row of claudeDescendants(rows, roots)) tracked.set(identity(row), row);
536 + return running;
537 + },
538 + (effect) => reading.withPermits(1)(effect),
539 + );
540 + return {
541 + tracked: tracked as ReadonlyMap<string, ClaudeProcessRow>,
542 + refresh: refresh(true).pipe(Effect.asVoid),
543 + /** Reads the table after the CLI exited: what it left that is still the same process. */
544 + leftovers: refresh(false).pipe(
545 + Effect.map((running) => [...tracked.values()].filter((row) => running.has(identity(row)))),
546 + ),
547 + };
548 +}
549 +
550 export const ClaudeProviderCapabilitiesV2 = {
551 sessions: {
552 supportsMultipleProviderThreadsPerSession: false,
@@ -1433,29 +1470,13 @@ export const layerQueryRunner: Layer.Layer<
1470 // re-parented once the CLI exits. So the CLI's tree is recorded while it runs, and what is
1471 // still the same process (pid and start time) is stopped after it exits.
1472 const trackDescendants = (cliPid: number) => {
1436 - const tracked = new Map<string, ClaudeProcessRow>();
1437 - const identity = (row: ClaudeProcessRow) => `${row.pid} ${row.started}`;
1438 - // After exit the CLI's pid may belong to an unrelated process, so only tracked jobs are roots.
1439 - const refresh = Effect.fn("ClaudeAgentSdkQueryRunner.trackDescendants")(function* (
1440 - cliRunning: boolean,
1441 - ) {
1442 - const rows = yield* readProcessTable;
1443 - const running = new Set(rows.map(identity));
1444 - const roots = [
1445 - ...(cliRunning ? [cliPid] : []),
1446 - ...[...tracked.values()].flatMap((row) => (running.has(identity(row)) ? [row.pid] : [])),
1447 - ];
1448 - for (const row of claudeDescendants(rows, roots)) tracked.set(identity(row), row);
1449 - return running;
1450 - });
1451 - const stop = refresh(false).pipe(
1452 - Effect.map((running) => {
1453 - for (const row of tracked.values()) {
1454 - if (running.has(identity(row))) killClaudeDescendant(row);
1455 - }
1473 + const tracker = makeClaudeDescendantTracker(cliPid, readProcessTable);
1474 + const stop = tracker.leftovers.pipe(
1475 + Effect.map((rows) => {
1476 + for (const row of rows) killClaudeDescendant(row);
1477 }),
1478 );
1458 - return { refresh: refresh(true).pipe(Effect.asVoid), stop };
1479 + return { refresh: tracker.refresh, stop };
1480 };
1481
1482 // The open span below is the tracing boundary for both attempts.
apps/server/src/orchestration-v2/Adapters/ClaudeAgentSdkQueryRunner.test.ts
+119
@@ -31,6 +31,7 @@ import {
31 ClaudeAgentSdkQueryRunner,
32 type ClaudeAgentSdkQueryOpenInput,
33 layerQueryRunner,
34 + makeClaudeDescendantTracker,
35 spawnClaudeProcessGroup,
36 } from "./ClaudeAdapterV2.ts";
37
@@ -721,6 +722,124 @@ describe.skipIf(HostProcess.Platform.defaultValue() === "win32")("Claude CLI pro
722 }),
723 );
724
725 + const processRow = (pid: number, ppid: number, startedSecond: number) => ({
726 + pid,
727 + ppid,
728 + pgid: pid,
729 + started: `Sun Nov 15 10:00:0${startedSecond} 2026`,
730 + });
731 + const byNumber = (left: number, right: number) => left - right;
732 +
733 + it.effect(
734 + "keeps only the descendants the CLI still has running, however many jobs it ran",
735 + () => {
736 + let table: ReadonlyArray<ReturnType<typeof processRow>> = [];
737 + const tracker = makeClaudeDescendantTracker(
738 + 1000,
739 + Effect.sync(() => table),
740 + );
741 + const jobs = (poll: number) => [0, 1, 2, 3].map((job) => 1100 + poll * 4 + job);
742 + return Effect.gen(function* () {
743 + for (let poll = 0; poll < 50; poll++) {
744 + table = [
745 + processRow(1000, 10, 0),
746 + processRow(1001, 1000, 0),
747 + // A job that outlives the shell that started it is re-parented.
748 + ...(poll === 0 ? [processRow(1003, 1000, 0)] : []),
749 + processRow(1002, poll === 0 ? 1003 : 1, 0),
750 + ...jobs(poll).map((pid) => processRow(pid, 1001, 1)),
751 + ];
752 + yield* tracker.refresh;
753 + // A failed `ps` reads as no rows.
754 + table = [];
755 + yield* tracker.refresh;
756 + }
757 + assert.strictEqual(tracker.tracked.size, 6);
758 + assert.deepStrictEqual(
759 + [...tracker.tracked.values()].map((row) => row.pid).toSorted(byNumber),
760 + [1001, 1002, ...jobs(49)],
761 + );
762 + });
763 + },
764 + );
765 +
766 + it.effect(
767 + "stops at exit only what the CLI left running, never a process that took a job's pid",
768 + () => {
769 + const orphan = processRow(1300, 1, 1);
770 + // Unrelated processes take both finished jobs' pids, one in the second its job started.
771 + const unrelated = [processRow(1100, 1, 2), processRow(1200, 1, 1)];
772 + const tables = [
773 + [
774 + processRow(1000, 10, 0),
775 + processRow(1001, 1000, 0),
776 + processRow(1100, 1001, 1),
777 + processRow(1200, 1001, 1),
778 + processRow(1300, 1001, 1),
779 + ],
780 + [processRow(1000, 10, 0), orphan],
781 + [processRow(1000, 10, 0), ...unrelated, orphan],
782 + [...unrelated, orphan],
783 + ];
784 + const tracker = makeClaudeDescendantTracker(
785 + 1000,
786 + Effect.sync(() => tables.shift() ?? []),
787 + );
788 + return Effect.gen(function* () {
789 + yield* tracker.refresh;
790 + yield* tracker.refresh;
791 + yield* tracker.refresh;
792 + assert.deepStrictEqual(
793 + (yield* tracker.leftovers).map((row) => row.pid),
794 + [1300],
795 + );
796 + });
797 + },
798 + );
799 +
800 + // The poll and a session close read the table independently. The older table still lists a
801 + // re-parented job and the child it just started; the newer one lists a job started since.
802 + it.effect(
803 + "stops at exit what the poll and a session close found, reading at the same time",
804 + () => {
805 + type Table = ReadonlyArray<ReturnType<typeof processRow>>;
806 + const cli = processRow(1000, 10, 0);
807 + const olderReadStarted = Deferred.makeUnsafe<void>();
808 + const olderRead = Deferred.makeUnsafe<Table>();
809 + let read: Effect.Effect<Table> = Effect.succeed([
810 + cli,
811 + processRow(1001, 1000, 0),
812 + processRow(1100, 1001, 1),
813 + ]);
814 + const tracker = makeClaudeDescendantTracker(
815 + 1000,
816 + Effect.suspend(() => read),
817 + );
818 + return Effect.gen(function* () {
819 + yield* tracker.refresh;
820 + read = Deferred.succeed(olderReadStarted, undefined).pipe(
821 + Effect.andThen(Deferred.await(olderRead)),
822 + );
823 + const older = yield* tracker.refresh.pipe(Effect.forkChild);
824 + yield* Deferred.await(olderReadStarted);
825 + read = Effect.succeed([cli, processRow(1200, 1, 1), processRow(1300, 1000, 2)]);
826 + const newer = yield* tracker.refresh.pipe(Effect.forkChild({ startImmediately: true }));
827 + yield* Deferred.succeed(olderRead, [
828 + cli,
829 + processRow(1100, 1, 1),
830 + processRow(1200, 1100, 1),
831 + ]);
832 + yield* Fiber.join(older);
833 + yield* Fiber.join(newer);
834 + read = Effect.succeed([processRow(1200, 1, 1), processRow(1300, 1, 2)]);
835 + assert.deepStrictEqual(
836 + (yield* tracker.leftovers).map((row) => row.pid).toSorted(byNumber),
837 + [1200, 1300],
838 + );
839 + });
840 + },
841 + );
842 +
843 const rootRefusal =
844 "--dangerously-skip-permissions cannot be used with root/sudo privileges for security reasons";
845