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

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

fix(server): prevent auto-settle while terminal has active subprocess (#39835)

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

GekoV committed Nov 20, 2026 at 20:28 UTC 6cbda0cd0aa982b7cbf22d9790bd74e68b2d3dbe
4 files changed +69
apps/server/src/orchestration-v2/ThreadSettlementService.test.ts
+22
@@ -588,6 +588,7 @@ interface HarnessOptions {
588 input: ProjectSetupScriptRunner.ProjectSetupScriptRunnerInput,
589 ) => Effect.Effect<void>;
590 readonly onCloseIdle?: () => Effect.Effect<void>;
591 + readonly hasActiveSubprocess?: (input: { readonly threadId: string }) => Effect.Effect<boolean>;
592 readonly currentThreads?: ReadonlyArray<OrchestrationV2AppThread>;
593 readonly projections?: ProjectionStore.ProjectionStoreV2Shape;
594 }
@@ -721,6 +722,7 @@ const makeHarness = Effect.fn("makeThreadSettlementHarness")(function* (options:
722 Layer.mock(TerminalManager.TerminalManager)({
723 closeOnSettle: (input) =>
724 Queue.offer(closedIdle, input).pipe(Effect.andThen(options.onCloseIdle?.() ?? Effect.void)),
725 + hasActiveSubprocess: (input) => options.hasActiveSubprocess?.(input) ?? Effect.succeed(false),
726 }),
727 Layer.mock(ProjectSetupScriptRunner.ProjectSetupScriptRunner)({
728 runForThread: (input) =>
@@ -939,6 +941,26 @@ describe("ThreadSettlementServiceV2 worker", () => {
941 }),
942 );
943
944 + it.effect("does not auto-settle a thread whose terminal has an active subprocess", () =>
945 + Effect.gen(function* () {
946 + yield* TestClock.setTime(Date.parse(NOW));
947 + const thread = makeThread("active-terminal");
948 + const fixture = yield* makeHarness({
949 + snapshot: makeSnapshot([thread]),
950 + settings: {
951 + ...DEFAULT_SERVER_SETTINGS,
952 + sidebarAutoSettleAfterDays: 3,
953 + },
954 + hasActiveSubprocess: ({ threadId }) => Effect.succeed(threadId === thread.id),
955 + });
956 + yield* Effect.gen(function* () {
957 + const service = yield* ThreadSettlementService.ThreadSettlementServiceV2;
958 + yield* startHarness(service, fixture.activation, fixture.snapshotReads);
959 + expect((yield* Ref.get(fixture.commands)).map((command) => command.threadId)).toEqual([]);
960 + }).pipe(Effect.provide(fixture.layer));
961 + }),
962 + );
963 +
964 it.effect("settles only the project opted in while environment settlement is disabled", () =>
965 Effect.gen(function* () {
966 yield* TestClock.setTime(Date.parse(NOW));
apps/server/src/orchestration-v2/ThreadSettlementService.ts
+3
@@ -370,6 +370,7 @@ export const make = Effect.gen(function* () {
370 const settleThread = Effect.fnUntraced(
371 function* (thread: (typeof candidates)[number], pullRequest: SettlementPullRequest | null) {
372 if (thread.lineage.relationshipToParent === "subagent") {
373 + if (yield* terminals.hasActiveSubprocess({ threadId: thread.id })) return null;
374 // The store returns a subagent only once its parent is done, so it
375 // settles with the parent and never runs its own lookup.
376 const uuid = yield* random.uuidV4();
@@ -396,6 +397,8 @@ export const make = Effect.gen(function* () {
397 autoSettleKeepPullRequests: currentSettings.sidebarAutoSettleKeepPullRequests,
398 });
399 if (settledAt === null) return thread;
400 + // A terminal still running a program keeps its thread open; a later sweep settles it.
401 + if (yield* terminals.hasActiveSubprocess({ threadId: thread.id })) return null;
402 const uuid = yield* random.uuidV4();
403 yield* orchestrator.dispatch({
404 type: "thread.auto-settle",
apps/server/src/terminal/Manager.test.ts
+19
@@ -993,6 +993,25 @@ it.layer(
993 }),
994 );
995
996 + it.effect("reports whether any terminal session for a thread has an active subprocess", () =>
997 + Effect.gen(function* () {
998 + const { manager } = yield* createManager(5, {
999 + processTable: Effect.succeed([
1000 + { pid: 9000, ppid: 1, name: "zsh" },
1001 + { pid: 100, ppid: 9000, name: "zsh" },
1002 + { pid: 9001, ppid: 1, name: "zsh" },
1003 + { pid: 200, ppid: 9001, name: "node" },
1004 + ]),
1005 + }).pipe(Effect.provide(layerWithHostPlatform("linux")));
1006 + yield* manager.open(openInput({ terminalId: "idle", threadId: "thread-idle" }));
1007 + yield* manager.open(openInput({ terminalId: "dev-server", threadId: "thread-active" }));
1008 +
1009 + expect(yield* manager.hasActiveSubprocess({ threadId: "thread-none" })).toBe(false);
1010 + expect(yield* manager.hasActiveSubprocess({ threadId: "thread-idle" })).toBe(false);
1011 + expect(yield* manager.hasActiveSubprocess({ threadId: "thread-active" })).toBe(true);
1012 + }),
1013 + );
1014 +
1015 it.effect.each([
1016 ["linux", ["SIGHUP", "SIGTERM"]],
1017 ["win32", ["SIGTERM"]],
apps/server/src/terminal/Manager.ts
+25
@@ -242,6 +242,8 @@ export class TerminalManager extends Context.Service<
242 readonly terminalId?: string;
243 }) => Effect.Effect<void>;
244
245 + readonly hasActiveSubprocess: (input: { readonly threadId: string }) => Effect.Effect<boolean>;
246 +
247 readonly subscribe: (
248 listener: (event: TerminalEvent) => Effect.Effect<void>,
249 registerCleanup?: (stop: () => void) => void,
@@ -3938,6 +3940,28 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func
3940 yield* closeIdle(input);
3941 });
3942
3943 + const hasActiveSubprocess = Effect.fn("terminal.hasActiveSubprocess")(function* (input: {
3944 + readonly threadId: string;
3945 + }) {
3946 + const running = (yield* sessionsForThread(input.threadId)).filter(
3947 + (session): session is TerminalSessionState & { pid: number } =>
3948 + session.status === "running" && Number.isInteger(session.pid),
3949 + );
3950 + if (running.length === 0) return false;
3951 + const { inspector } = yield* acquireSubprocessInspector(
3952 + platform === "win32" ? CLOSE_IDLE_QUERY_TIMEOUT.win32 : CLOSE_IDLE_QUERY_TIMEOUT.posix,
3953 + );
3954 + // A failed process query falls back to the last poll's answer.
3955 + const isActive = (session: (typeof running)[number]) =>
3956 + Result.isFailure(inspector)
3957 + ? Effect.succeed(session.hasRunningSubprocess)
3958 + : inspector.success(session.pid, session.shell).pipe(
3959 + Effect.map((result) => result.hasRunningSubprocess),
3960 + Effect.orElseSucceed(() => session.hasRunningSubprocess),
3961 + );
3962 + return Option.isSome(yield* Effect.findFirst(running, isActive));
3963 + });
3964 +
3965 return TerminalManager.of({
3966 open,
3967 getShell,
@@ -3951,6 +3975,7 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func
3975 close,
3976 closeIdle,
3977 closeOnSettle,
3978 + hasActiveSubprocess,
3979 subscribe,
3980 subscribeMetadata,
3981 });