From ffd43b41ec7386adc1190579e3ab59935e570f16 Mon Sep 17 00:00:00 2001 From: Theo Browne Date: Sun, 9 Aug 2026 14:51:48 -0700 Subject: [PATCH] fix(server): second round of review findings - Restore the powershell/ps literal union on TerminalSubprocessCheckError.command; the widening to Schema.String was unnecessary once the capture path got its own error class. - Decide session liveness before registerTerminalProcesses, so a terminal that exited or restarted mid-tick cannot get its old process tree re-registered with port discovery. Co-Authored-By: Claude Fable 5 --- apps/server/src/terminal/Manager.ts | 57 +++++++++++++++++++---------- 1 file changed, 38 insertions(+), 19 deletions(-) diff --git a/apps/server/src/terminal/Manager.ts b/apps/server/src/terminal/Manager.ts index 46773043a3..013ac7b349 100644 --- a/apps/server/src/terminal/Manager.ts +++ b/apps/server/src/terminal/Manager.ts @@ -91,7 +91,7 @@ class TerminalSubprocessCheckError extends Schema.TaggedErrorClass { + const outcome = yield* modifyManagerState<{ + readonly live: boolean; + readonly event: Option.Option; + }>((state) => { const liveSession: Option.Option = Option.fromNullishOr( state.sessions.get(toSessionKey(session.threadId, session.terminalId)), ); + // Liveness is decided here, before process registration, so a + // terminal that exited or restarted mid-tick cannot get its old + // process tree re-registered with port discovery. if ( Option.isNone(liveSession) || liveSession.value.status !== "running" || - liveSession.value.pid !== terminalPid || - (liveSession.value.hasRunningSubprocess === next.hasRunningSubprocess && - liveSession.value.childCommandLabel === nextChildLabel) + liveSession.value.pid !== terminalPid ) { - return [Option.none(), state] as const; + return [{ live: false, event: Option.none() }, state] as const; + } + if ( + liveSession.value.hasRunningSubprocess === next.hasRunningSubprocess && + liveSession.value.childCommandLabel === nextChildLabel + ) { + return [{ live: true, event: Option.none() }, state] as const; } liveSession.value.hasRunningSubprocess = next.hasRunningSubprocess; @@ -2073,18 +2078,32 @@ export const makeWithOptions = Effect.fn("TerminalManager.makeWithOptions")(func const eventStamp = advanceEventSequence(liveSession.value); return [ - Option.some({ - type: "activity" as const, - threadId: liveSession.value.threadId, - terminalId: liveSession.value.terminalId, - sequence: eventStamp.sequence, - hasRunningSubprocess: next.hasRunningSubprocess, - label: terminalWireLabel(liveSession.value), - }), + { + live: true, + event: Option.some({ + type: "activity" as const, + threadId: liveSession.value.threadId, + terminalId: liveSession.value.terminalId, + sequence: eventStamp.sequence, + hasRunningSubprocess: next.hasRunningSubprocess, + label: terminalWireLabel(liveSession.value), + }), + }, state, ] as const; }); + if (!outcome.live) { + return; + } + + yield* registerTerminalProcesses({ + threadId: session.threadId, + terminalId: session.terminalId, + processIds: next.processIds, + }); + + const event = outcome.event; if (Option.isSome(event)) { // The poll tick runs with tracing suppressed; an actual state flip is // the only part worth a span, so tracing is re-enabled just for it.