Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

fix(windows): session-host paneCommand answers a shell only when no non-shell runs under it

review8c38d791f96f
diff --git a/docs/windows-session-host.md b/docs/windows-session-host.md
index 5abf4f1de..264f5e4ce 100644
--- a/docs/windows-session-host.md
+++ b/docs/windows-session-host.md
@@ -665,11 +665,13 @@ you chose and why") rather than an oversight.
the resulting process. A genuinely Windows-reachable tmux found by the existing resolver still
follows the tmux path; a `tmux` installed only inside WSL is not on the native Windows PATH.
- **`paneCommand`'s answer can be imprecise** when a session has multiple concurrent child
- processes (an agent CLI plus MCP servers) — see `process-tree.ts`'s doc comment. The one caller
- that depends on it (in-place agent restart) only needs "is a shell back in charge of this pane
- yet", which every non-shell answer satisfies equally, so this imprecision is harmless for that
- caller specifically; a future caller with finer-grained needs should not assume more precision
- than this.
+ processes (an agent CLI plus MCP servers) — see `process-tree.ts`'s doc comment. Its callers
+ (in-place agent restart, wake, trigger delivery, RunBar, session rename) only need "is a shell
+ back in charge of this pane yet", so the walk answers a SHELL name only when every live process in
+ the tree is a shell; otherwise it answers a non-shell. This matters since `isShellCommand`
+ recognises `pwsh.exe` / `cmd.exe` / `bash.exe`: a shell that is merely a leaf under a running agent
+ (claude's Bash/PowerShell tool, an MCP server launched via `cmd /c npx`) must not read as "the CLI
+ let go". A future caller with finer-grained needs should not assume more precision than this.
- **The node-pty actuator is global, so ownership is ledgered rather than guessed.** Within one app
process, `pty-manager.ts` keeps a per-viewer/owner `pausedBy` ledger. The standalone host then
keeps a second set keyed by authenticated socket: the first socket to pause actuates node-pty,
diff --git a/src/session-host/process-tree.test.ts b/src/session-host/process-tree.test.ts
new file mode 100644
index 000000000..5e6945de4
--- /dev/null
+++ b/src/session-host/process-tree.test.ts
@@ -0,0 +1,43 @@
+import { describe, expect, it } from 'vitest'
+import { paneCommandFromRows, type ProcRow } from './process-tree'
+
+const row = (pid: number, ppid: number, name: string): ProcRow => ({ pid, ppid, name })
+
+describe('paneCommandFromRows', () => {
+ it('answers the idle shell when it has no children', () => {
+ expect(paneCommandFromRows([row(10, 1, 'pwsh.exe')], 10)).toBe('pwsh.exe')
+ })
+
+ it('answers null when the root is gone', () => {
+ expect(paneCommandFromRows([row(11, 1, 'pwsh.exe')], 10)).toBeNull()
+ })
+
+ it('answers a nested shell the user typed into (pwsh -> cmd)', () => {
+ expect(paneCommandFromRows([row(10, 1, 'pwsh.exe'), row(20, 10, 'cmd.exe')], 10)).toBe('cmd.exe')
+ })
+
+ it('answers the agent, not the shell its tool spawned as a leaf (pwsh -> node -> bash.exe)', () => {
+ const rows = [row(10, 1, 'pwsh.exe'), row(20, 10, 'node.exe'), row(30, 20, 'bash.exe')]
+ expect(paneCommandFromRows(rows, 10)).toBe('node.exe')
+ })
+
+ it('answers a non-shell for an MCP server launched via cmd /c (node -> cmd -> node)', () => {
+ const rows = [
+ row(10, 1, 'pwsh.exe'),
+ row(20, 10, 'cmd.exe'),
+ row(30, 20, 'node.exe'),
+ row(40, 30, 'cmd.exe'),
+ row(50, 30, 'powershell.exe')
+ ]
+ expect(paneCommandFromRows(rows, 10)).toBe('node.exe')
+ })
+
+ it('still answers the deepest non-shell process otherwise', () => {
+ const rows = [row(10, 1, 'pwsh.exe'), row(20, 10, 'node.exe'), row(30, 20, 'codex.exe')]
+ expect(paneCommandFromRows(rows, 10)).toBe('codex.exe')
+ })
+
+ it('answers a non-shell root (a session spawned straight into a program)', () => {
+ expect(paneCommandFromRows([row(10, 1, 'node.exe'), row(20, 10, 'cmd.exe')], 10)).toBe('node.exe')
+ })
+})
diff --git a/src/session-host/process-tree.ts b/src/session-host/process-tree.ts
index c7f0ff27d..d9a7341ba 100644
--- a/src/session-host/process-tree.ts
+++ b/src/session-host/process-tree.ts
@@ -1,4 +1,5 @@
import { execFile } from 'child_process'
+import { isShellCommand } from '../shared/agents/pane'
/**
* The `#{pane_current_command}` equivalent for a backend with no tmux underneath it. tmux tracks
@@ -18,7 +19,7 @@ import { execFile } from 'child_process'
* known limitation in docs/windows-session-host.md rather than pretended away.
*/
-interface ProcRow {
+export interface ProcRow {
pid: number
ppid: number
name: string
@@ -88,22 +89,43 @@ function readPosix(): Promise<ProcRow[]> {
})
}
-/** The deepest live descendant of `rootPid` (the pty's own child), by name — or null when the
- * process table couldn't be read or `rootPid` is no longer in it (the session just exited). */
-export async function paneCommand(rootPid: number): Promise<string | null> {
- const rows = process.platform === 'win32' ? await readWin32() : await readPosix()
- if (rows.length === 0) return null
+/**
+ * The pane-command answer for `rootPid` over one process-table read, or null when `rootPid` is
+ * not in it. Pure, so the rule is testable on any platform.
+ *
+ * A SHELL name is answered only when every live process in the tree is a shell. Callers read a
+ * shell answer as "the CLI has let go of the pane" (restart exit poll, wake, trigger delivery,
+ * RunBar), so a shell that is merely a LEAF somewhere under a running agent (claude's Bash tool
+ * runs `bash.exe`, its PowerShell tool `pwsh.exe`, an MCP server is launched via `cmd /c npx`)
+ * must not answer for the pane. Once `isShellCommand` learned the Windows image names, that leaf
+ * stopped being an unrecognised (= "not yet") answer, so this walk has to say "non-shell"
+ * whenever one exists. A non-shell answer is the deepest-visited non-shell, as before.
+ */
+export function paneCommandFromRows(rows: readonly ProcRow[], rootPid: number): string | null {
const { table, kids } = indexRows(rows)
if (!table.has(rootPid)) return null
let deepest = rootPid
+ let deepestNonShell: number | null = isShellCommand(table.get(rootPid)?.name) ? null : rootPid
const seen = new Set<number>([rootPid])
const stack = [...(kids.get(rootPid) ?? [])]
while (stack.length > 0) {
const pid = stack.pop() as number
if (seen.has(pid)) continue
seen.add(pid)
- if (table.has(pid)) deepest = pid
+ const row = table.get(pid)
+ if (row) {
+ deepest = pid
+ if (!isShellCommand(row.name)) deepestNonShell = pid
+ }
for (const k of kids.get(pid) ?? []) stack.push(k)
}
- return table.get(deepest)?.name || null
+ return table.get(deepestNonShell ?? deepest)?.name || null
+}
+
+/** The pane-command answer for the pty's own child (`paneCommandFromRows`) — or null when the
+ * process table couldn't be read or `rootPid` is no longer in it (the session just exited). */
+export async function paneCommand(rootPid: number): Promise<string | null> {
+ const rows = process.platform === 'win32' ? await readWin32() : await readPosix()
+ if (rows.length === 0) return null
+ return paneCommandFromRows(rows, rootPid)
}