Skip to content

Commit dd059b4

Browse files
authored
feat(server): stop opted-in project actions when threads settle (#31857)
1 parent 0a2f590 commit dd059b4

23 files changed

Lines changed: 125 additions & 34 deletions

‎apps/mobile/src/features/terminal/ThreadTerminalRouteScreen.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,7 @@ function ThreadTerminal(props: {
487487
cols: initialAttachGridSize.cols,
488488
rows: initialAttachGridSize.rows,
489489
...(pendingLaunch?.env ? { env: pendingLaunch.env } : {}),
490+
...(pendingLaunch ? { stopOnSettle: pendingLaunch.stopOnSettle ?? false } : {}),
490491
...(pendingLaunch ? { restartIfNotRunning: true } : {}),
491492
}
492493
: null,

‎apps/mobile/src/features/terminal/terminalLaunchContext.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export interface PendingTerminalLaunch {
1717
readonly worktreePath: string | null;
1818
readonly env?: Record<string, string>;
1919
readonly initialInput?: string;
20+
readonly stopOnSettle?: boolean;
2021
}
2122

2223
const pendingTerminalLaunches = new Map<string, PendingTerminalLaunch>();
@@ -34,6 +35,7 @@ export function stagePendingTerminalLaunch(input: {
3435
worktreePath: input.launch.worktreePath,
3536
env: input.launch.env ? { ...input.launch.env } : undefined,
3637
initialInput: input.launch.initialInput,
38+
stopOnSettle: input.launch.stopOnSettle,
3739
});
3840
}
3941

‎apps/mobile/src/features/threads/ThreadRouteScreen.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,7 @@ function ThreadRouteContent(
767767
worktreePath: preferredWorktreePath,
768768
env,
769769
initialInput: projectScriptTerminalInput(script.command),
770+
stopOnSettle: script.stopOnSettle ?? false,
770771
},
771772
});
772773

‎apps/server/src/orchestration-v2/ThreadSettlementService.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,7 @@ const makeHarness = Effect.fn("makeThreadSettlementHarness")(function* (options:
682682
dispatch,
683683
}),
684684
Layer.mock(TerminalManager.TerminalManager)({
685-
closeIdle: (input) =>
685+
closeOnSettle: (input) =>
686686
Queue.offer(closedIdle, input).pipe(Effect.andThen(options.onCloseIdle?.() ?? Effect.void)),
687687
}),
688688
Layer.mock(ProjectSetupScriptRunner.ProjectSetupScriptRunner)({

‎apps/server/src/orchestration-v2/ThreadSettlementService.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ export const make = Effect.gen(function* () {
599599
function* (threadId: ThreadId) {
600600
const settled = yield* projections.getThread(threadId);
601601
if (settled.settledOverride !== "settled") return;
602-
yield* terminals.closeIdle({ threadId });
602+
yield* terminals.closeOnSettle({ threadId });
603603
const worktreePath = settled.worktreePath;
604604
if (worktreePath === null || !(yield* fileSystem.exists(worktreePath))) return;
605605
// Recheck after closing and worktree I/O; re-engaged threads must not be cleaned up.

‎apps/server/src/project/ProjectSetupScriptRunner.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ const makeHarness = Effect.fn(function* (options: {
6262
}),
6363
write: (input) => Queue.offer(writes, input.data).pipe(Effect.asVoid),
6464
closeIdle: () => Effect.void,
65+
closeOnSettle: () => Effect.void,
6566
});
6667
const runner = yield* ProjectSetupScriptRunner.make.pipe(
6768
Effect.provide(

‎apps/server/src/project/ProjectSetupScriptRunner.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,7 @@ export const make = Effect.gen(function* () {
466466
.open({
467467
threadId: input.threadId,
468468
terminalId,
469+
stopOnSettle: script.stopOnSettle ?? false,
469470
cwd,
470471
worktreePath: input.worktreePath,
471472
env: { ...env, NO_COLOR: "1", FORCE_COLOR: "0" },
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"claudeSystemAppendBytes": 5366,
33
"codexAdditionalContextBytes": 7096,
4-
"mcpToolListJsonBytes": 48051
4+
"mcpToolListJsonBytes": 48119
55
}

‎apps/server/src/terminal/Manager.test.ts‎

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -740,34 +740,52 @@ it.layer(
740740
}),
741741
);
742742

743-
it.effect("closes only a thread's idle shells, ignoring a helper forked from the shell", () =>
744-
Effect.gen(function* () {
745-
const { manager, ptyAdapter } = yield* createManager(5, {
746-
processTable: Effect.succeed([
747-
{ pid: 9000, ppid: 1, name: "zsh" },
748-
{ pid: 100, ppid: 9000, name: "zsh" },
749-
{ pid: 9001, ppid: 1, name: "zsh" },
750-
{ pid: 200, ppid: 9001, name: "node" },
751-
{ pid: 9002, ppid: 1, name: "zsh" },
752-
{ pid: 300, ppid: 9002, name: "zsh" },
753-
{ pid: 301, ppid: 300, name: "sleep" },
754-
{ pid: 9003, ppid: 1, name: "zsh" },
755-
]),
756-
}).pipe(Effect.provide(layerWithHostPlatform("linux")));
757-
yield* manager.open(openInput({ terminalId: "idle" }));
758-
yield* manager.open(openInput({ terminalId: "dev-server" }));
759-
yield* manager.open(openInput({ terminalId: "subshell" }));
760-
yield* manager.open(openInput({ threadId: "thread-2" }));
761-
762-
yield* manager.closeIdle({ threadId: "thread-1" });
763-
764-
expect(ptyAdapter.processes.map((process) => process.killed)).toEqual([
765-
true,
766-
false,
767-
false,
768-
false,
769-
]);
770-
}),
743+
it.effect(
744+
"closes idle shells and opted-in actions on settlement without stopping other commands",
745+
() =>
746+
Effect.gen(function* () {
747+
const { manager, ptyAdapter } = yield* createManager(5, {
748+
processTable: Effect.succeed([
749+
{ pid: 9000, ppid: 1, name: "zsh" },
750+
{ pid: 100, ppid: 9000, name: "zsh" },
751+
{ pid: 9001, ppid: 1, name: "zsh" },
752+
{ pid: 200, ppid: 9001, name: "node" },
753+
{ pid: 9002, ppid: 1, name: "zsh" },
754+
{ pid: 300, ppid: 9002, name: "zsh" },
755+
{ pid: 301, ppid: 300, name: "sleep" },
756+
{ pid: 9003, ppid: 1, name: "zsh" },
757+
{ pid: 9004, ppid: 1, name: "zsh" },
758+
{ pid: 400, ppid: 9004, name: "node" },
759+
]),
760+
}).pipe(Effect.provide(layerWithHostPlatform("linux")));
761+
yield* manager.open(openInput({ terminalId: "idle" }));
762+
yield* manager.open(openInput({ terminalId: "dev-server" }));
763+
yield* manager.open(openInput({ terminalId: "subshell" }));
764+
yield* manager.open(openInput({ threadId: "thread-2", stopOnSettle: true }));
765+
yield* manager.open(openInput({ terminalId: "managed-dev", stopOnSettle: true }));
766+
767+
yield* manager.closeIdle({ threadId: "thread-1" });
768+
769+
expect(ptyAdapter.processes.map((process) => process.killed)).toEqual([
770+
true,
771+
false,
772+
false,
773+
false,
774+
false,
775+
]);
776+
yield* manager.open(openInput({ terminalId: "managed-dev", stopOnSettle: false }));
777+
yield* manager.closeOnSettle({ threadId: "thread-1" });
778+
expect(ptyAdapter.processes[4]?.killed).toBe(false);
779+
yield* manager.open(openInput({ terminalId: "managed-dev", stopOnSettle: true }));
780+
yield* manager.closeOnSettle({ threadId: "thread-1" });
781+
expect(ptyAdapter.processes.map((process) => process.killed)).toEqual([
782+
true,
783+
false,
784+
false,
785+
false,
786+
true,
787+
]);
788+
}),
771789
);
772790

773791
it.effect("labels a running command by its whole process name, spaces included", () =>

‎apps/server/src/terminal/Manager.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,8 @@ export class TerminalManager extends Context.Service<
219219
/** Omit terminalId to close all sessions for the thread. */
220220
readonly close: (input: TerminalCloseInput) => Effect.Effect<void, TerminalError>;
221221

222+
readonly closeOnSettle: (input: { readonly threadId: string }) => Effect.Effect<void>;
223+
222224
readonly closeIdle: (input: {
223225
readonly threadId: string;
224226
readonly terminalId?: string;
@@ -304,6 +306,7 @@ interface TerminalSessionState {
304306
hasRunningSubprocess: boolean;
305307
childCommandLabel: string | null;
306308
runtimeEnv: Record<string, string> | null;
309+
stopOnSettle: boolean;
307310
}
308311

309312
interface TerminalGrid {
@@ -2871,6 +2874,7 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func
28712874
hasRunningSubprocess: false,
28722875
childCommandLabel: null,
28732876
runtimeEnv: normalizedRuntimeEnv(input.env),
2877+
stopOnSettle: input.stopOnSettle ?? false,
28742878
};
28752879

28762880
const createdSession = session;
@@ -2896,6 +2900,7 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func
28962900
}
28972901

28982902
const liveSession = existing.value;
2903+
if (input.stopOnSettle !== undefined) liveSession.stopOnSettle = input.stopOnSettle;
28992904
const nextRuntimeEnv = normalizedRuntimeEnv(input.env);
29002905
const currentRuntimeEnv = liveSession.runtimeEnv;
29012906
const targetCols = input.cols ?? liveSession.cols;
@@ -3453,6 +3458,23 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func
34533458
),
34543459
);
34553460

3461+
const closeOnSettle = Effect.fn("terminal.closeOnSettle")(function* (input: {
3462+
readonly threadId: string;
3463+
}) {
3464+
yield* withThreadLock(
3465+
input.threadId,
3466+
Effect.gen(function* () {
3467+
const sessions = yield* sessionsForThread(input.threadId);
3468+
yield* Effect.forEach(
3469+
sessions.filter((session) => session.stopOnSettle),
3470+
(session) => closeSession(input.threadId, session.terminalId, false),
3471+
{ discard: true },
3472+
);
3473+
}),
3474+
);
3475+
yield* closeIdle(input);
3476+
});
3477+
34563478
return TerminalManager.of({
34573479
open,
34583480
getShell,
@@ -3465,6 +3487,7 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func
34653487
clear,
34663488
close,
34673489
closeIdle,
3490+
closeOnSettle,
34683491
subscribe,
34693492
subscribeMetadata,
34703493
});

0 commit comments

Comments
 (0)