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.
TypeScript adhdagent-orchestrationai-agentscanvasclaude-codecodexcoding-agentsdeveloper-toolselectrongeminilinuxmacosmultiplexeropencodeparallel-agentsterminalterminal-emulatorterminal-multiplexertmuxworkspace-manager
fix(core): a kill by NAME must try every socket that name could be on
enes61cc90cc584a
diff --git a/src/core/pty-manager.ts b/src/core/pty-manager.tsindex dd5367dda..4e2033d8e 100644--- a/src/core/pty-manager.ts+++ b/src/core/pty-manager.ts@@ -23,6 +23,8 @@ import {remoteHookEnvArgs,remoteTmuxHasSessionArgs,remoteTmuxKillArgs,+ localKillSockets,+ localTmuxKillArgs,remoteTmuxPtyArgs,remoteTmuxSendKeysArgs,remoteCapturePaneArgs,@@ -2237,10 +2239,20 @@ export class PtyManager {// such session", which is already the ignored case below.}if (!this.tmuxPath) return- try {- await runAsync(this.tmuxPath, ['-L', TMUX_SOCKET, 'kill-session', '-t', sessionName(persistKey)])- } catch {- // session may not exist; ignore+ // Which socket(s) to aim at. Holding the session ourselves means we know: it is the local one.+ // Holding NOTHING means the name is all we have, and on this machine a `nt-<id>` can also be+ // living on the `nodeterm-rmt` socket — that is where ANOTHER machine's nodeterm puts the+ // sessions it SSHes in to spawn. The session-memory panel lists those (it sweeps both sockets)+ // and offers to end them, so a kill that only ever tried `node-terminal` showed a confirm+ // saying "this stops its tmux session" and then did nothing. The common path — deleting a node+ // whose session we hold — is unchanged, and every extra call is the already-ignored+ // "no such session".+ for (const socket of localKillSockets(dying ? TMUX_SOCKET : null)) {+ try {+ await runAsync(this.tmuxPath, localTmuxKillArgs(socket, sessionName(persistKey)))+ } catch {+ // session may not exist on this socket; ignore+ }}}diff --git a/src/core/pty-single-user.test.ts b/src/core/pty-single-user.test.tsindex b9fb4392d..271394e3c 100644--- a/src/core/pty-single-user.test.ts+++ b/src/core/pty-single-user.test.ts@@ -364,8 +364,33 @@ describe('SINGLE-USER REGRESSION: co-attach must not change the solo path', () =await create(80, 24)await m.destroySession(SOLO, 'solo-1')const kills = tmuxCalls('kill-session')+ // ONE kill: we hold this session, so we know which socket it is on and the destroy aims only+ // there. A destroy with NO live session cannot know, and fans out over both sockets+ // (`localKillSockets`) — this length is what keeps that off the solo path.expect(kills).toHaveLength(1)- expect(kills[0].args[kills[0].args.indexOf('-t') + 1]).toBe(sessionName('solo-1'))+ expect(kills[0].args[kills[0].args.indexOf('-L') + 1]).toBe('node-terminal')+ // `=` forces an EXACT tmux target. Node ids end in a counter, so `nt-a-1` is a prefix of+ // `nt-a-12`, and tmux falls back to prefix matching whenever the exact name is not found.+ expect(kills[0].args[kills[0].args.indexOf('-t') + 1]).toBe(`=${sessionName('solo-1')}`)+ })++ // Destroying a name we hold NOTHING for is how the session-memory panel ends an ORPHAN row — a+ // tmux session with no node on any canvas. It is also every session that outlived the process+ // that spawned it. We cannot know which socket carries it, and on this machine a `nt-<id>` may be+ // on `nodeterm-rmt`: that is where ANOTHER machine's nodeterm puts the sessions it SSHes in to+ // spawn, and the panel sweeps (and offers to end) both sockets. Aiming only at `node-terminal`+ // meant a confirm reading "this stops its tmux session" that killed nothing at all.+ it('destroying a session we do not hold tries EVERY socket it could be on', async () => {+ const m = await tmuxManager()+ await m.destroySession(SOLO, 'never-opened-here')+ const kills = tmuxCalls('kill-session')+ expect(kills.map((c) => c.args[c.args.indexOf('-L') + 1]).sort()).toEqual([+ 'node-terminal',+ 'nodeterm-rmt'+ ])+ for (const k of kills) {+ expect(k.args[k.args.indexOf('-t') + 1]).toBe(`=${sessionName('never-opened-here')}`)+ }})// ── recycle (move into worktree) = destroy, minus the "someone closed it" fan-out ─────────@@ -379,8 +404,8 @@ describe('SINGLE-USER REGRESSION: co-attach must not change the solo path', () =await (fake.senderListeners[IPC.ptyRecycle](SOLO, 'solo-1') as unknown as Promise<void>)const kills = tmuxCalls('kill-session')- expect(kills).toHaveLength(1)- expect(kills[0].args[kills[0].args.indexOf('-t') + 1]).toBe(sessionName('solo-1'))+ expect(kills).toHaveLength(1) // we hold the session; one socket, one kill+ expect(kills[0].args[kills[0].args.indexOf('-t') + 1]).toBe(`=${sessionName('solo-1')}`)expect(spawned[0].killed).toBe(true) // the old pty client is releasedexpect(fake.sent).toEqual([]) // solo: nobody to tell, so nothing is sentdiff --git a/src/core/remote-ssh/control-master.test.ts b/src/core/remote-ssh/control-master.test.tsindex e65353e9b..de0967d13 100644--- a/src/core/remote-ssh/control-master.test.ts+++ b/src/core/remote-ssh/control-master.test.ts@@ -15,6 +15,11 @@ import {listDirArgs,mkDirArgs,RMT_TMUX_SOCKET,+ KILL_TMUX_SOCKETS,+ localKillSockets,+ localTmuxKillArgs,+ remoteTmuxKillArgs,+ remoteTmuxKillEverySocketArgs,hookForwardArgs,hookForwardCancelArgs,remoteHookEnvArgs,@@ -381,3 +386,51 @@ describe('probeSaysAbsent (has-session probe discrimination)', () => {expect(probeSaysAbsent(null)).toBe(false)})})++describe('killing a session by name (both sockets, exact target)', () => {+ // Two nodeterm tmux sockets live on every host at once: `node-terminal` for a nodeterm running ON+ // the machine, `nodeterm-rmt` for one SSH-ing INTO it. The session-memory sweep lists BOTH, so a+ // kill routed to one of them promised something it could not do for every row off the other — a+ // host running its own nodeterm-server is exactly that shape.+ it('covers both sockets a nodeterm session can live on', () => {+ expect([...KILL_TMUX_SOCKETS].sort()).toEqual(['node-terminal', 'nodeterm-rmt'])+ })++ it('targets the session EXACTLY — never tmux prefix matching', () => {+ // A speculative kill misses by design, and a miss is when tmux falls back to fnmatch and then+ // to prefix matching. Node ids end in a counter, so `nt-a-1` is a prefix of `nt-a-12`: without+ // `=` a miss could kill a different session.+ expect(localTmuxKillArgs('node-terminal', 'nt-a-1')).toEqual([+ '-L', 'node-terminal', 'kill-session', '-t', '=nt-a-1'+ ])+ expect(remoteTmuxKillArgs(conn, '/s.sock', 'nt-a-1').at(-1)).toContain('-t =nt-a-1')+ })++ it('aims a local destroy at the socket we hold, and at every socket when we hold nothing', () => {+ // Holding the session means we know which socket it is on. Holding nothing (an orphan row in+ // the session-memory panel, or any session that outlived the process that spawned it) means the+ // name is all we have.+ expect(localKillSockets('node-terminal')).toEqual(['node-terminal'])+ expect([...localKillSockets(null)].sort()).toEqual(['node-terminal', 'nodeterm-rmt'])+ })++ it('keeps the remote default on the ssh socket, and overrides it explicitly', () => {+ expect(remoteTmuxKillArgs(conn, '/s.sock', 'nt-x').at(-1)).toBe(+ `tmux -L ${RMT_TMUX_SOCKET} kill-session -t =nt-x`+ )+ expect(remoteTmuxKillArgs(conn, '/s.sock', 'nt-x', 'node-terminal').at(-1)).toBe(+ 'tmux -L node-terminal kill-session -t =nt-x'+ )+ })++ it('kills a name on EVERY remote socket, one ssh invocation each', () => {+ const runs = remoteTmuxKillEverySocketArgs(conn, '/s.sock', 'nt-x')+ expect(runs).toHaveLength(2)+ expect(runs.map((r) => r.at(-1)).sort()).toEqual([+ 'tmux -L node-terminal kill-session -t =nt-x',+ `tmux -L ${RMT_TMUX_SOCKET} kill-session -t =nt-x`+ ])+ // Each is a complete child-ssh argv, not a bare command.+ for (const r of runs) expect(r.slice(0, childPrefix.length)).toEqual(childPrefix)+ })+})diff --git a/src/core/remote-ssh/control-master.ts b/src/core/remote-ssh/control-master.tsindex ec730a3bb..816d212a9 100644--- a/src/core/remote-ssh/control-master.ts+++ b/src/core/remote-ssh/control-master.ts@@ -4,6 +4,7 @@ import os from 'os'import path from 'path'import { createHash } from 'crypto'import { posixQuote, quoteRemotePath, remoteTmuxCommand, type SshConnection } from '../../shared/ssh'+import { TMUX_SOCKET } from '../tmux-naming'import { canControlCanvas } from '../../shared/agents/config'import { bracketedInjection } from '../paste-injection'// Dependency-free (no node-pty): safe to import from these pure builders.@@ -252,8 +253,71 @@ export function remoteTmuxSendKeysArgs(export function probeSaysAbsent(err: unknown): boolean {return (err as { code?: unknown } | null)?.code === 1}-export function remoteTmuxKillArgs(conn: SshConnection, controlPath: string, sessionId: string): string[] {- return childArgs(conn, controlPath, `tmux -L ${RMT_TMUX_SOCKET} kill-session -t ${sessionId}`)+/**+ * Every tmux socket a nodeterm session can be living on, on ONE machine — used when we are asked to+ * end a session by NAME and do not know which socket holds it.+ *+ * Both exist on the same host all the time: `node-terminal` is what a nodeterm running ON that+ * machine uses (desktop or Server Edition), `nodeterm-rmt` is what a nodeterm SSH-ing INTO it uses.+ * The session-memory sweep lists both (`session-memory.ts`, `session-memory-remote.ts`) — so a kill+ * that only ever targeted one of them promised something it could not do for every row that came+ * off the other. That is not exotic: a host that runs its own `nodeterm-server` and is also SSH'd+ * into is exactly this shape.+ *+ * Deliberately NOT the same constant the sweep and the reaper use, even though it holds the same two+ * names: for THEM the order is load-bearing (the sweep's `bySession` is first-wins, so the order+ * decides which socket a duplicate name is attributed to), and for a kill it means nothing. Sharing+ * one array would silently couple a de-duplication preference to a kill list.+ */+export const KILL_TMUX_SOCKETS: readonly string[] = [TMUX_SOCKET, RMT_TMUX_SOCKET]++/**+ * Which local sockets a destroy must attempt. `liveSocket` is the socket of the session we actually+ * hold (which we know is the right one); `null` = we hold nothing, so the name is all we have and+ * every socket that could carry it has to be tried. An orphan row in the session-memory panel — and+ * any node whose session outlived the process that spawned it — takes the `null` branch.+ */+export function localKillSockets(liveSocket: string | null): readonly string[] {+ return liveSocket ? [liveSocket] : KILL_TMUX_SOCKETS+}++/**+ * `=` forces tmux to match the target EXACTLY. Without it tmux falls back to fnmatch and then to+ * PREFIX matching when the name is not found — and "not found" is the normal case for every kill we+ * fire speculatively at a socket. Node ids end in a counter (`term-<base36 ms>-1`), so `nt-…-1` is a+ * prefix of `nt-…-12`: a miss could kill a DIFFERENT session. The reaper already kills this way+ * (`session-budget.ts`) for the same reason.+ */+function exactTarget(sessionId: string): string {+ return `=${sessionId}`+}++/** Local `tmux kill-session` argv (the binary is resolved by the caller). */+export function localTmuxKillArgs(socket: string, sessionId: string): string[] {+ return ['-L', socket, 'kill-session', '-t', exactTarget(sessionId)]+}++export function remoteTmuxKillArgs(+ conn: SshConnection,+ controlPath: string,+ sessionId: string,+ socket: string = RMT_TMUX_SOCKET+): string[] {+ return childArgs(conn, controlPath, `tmux -L ${socket} kill-session -t ${exactTarget(sessionId)}`)+}++/**+ * One kill per socket, for a caller that knows only the session NAME (`SshProjectManager.+ * killSessions`, which the session-memory panel and project deletion both use). Best-effort by+ * contract: tmux exits non-zero with "can't find session" on every socket that does not hold it,+ * and that is an expected, ignored outcome rather than a failure.+ */+export function remoteTmuxKillEverySocketArgs(+ conn: SshConnection,+ controlPath: string,+ sessionId: string+): string[][] {+ return KILL_TMUX_SOCKETS.map((socket) => remoteTmuxKillArgs(conn, controlPath, sessionId, socket))}export function remoteCapturePaneArgs(conn: SshConnection, controlPath: string, sessionId: string, full: boolean): string[] {return childArgs(diff --git a/src/main/remote-ssh/ssh-project.ts b/src/main/remote-ssh/ssh-project.tsindex 045234af8..65f828f4e 100644--- a/src/main/remote-ssh/ssh-project.ts+++ b/src/main/remote-ssh/ssh-project.ts@@ -21,7 +21,7 @@ import {mkDirArgs,exitMasterArgs,checkMasterArgs,- remoteTmuxKillArgs,+ remoteTmuxKillEverySocketArgs,childArgs,scpArgs,scpDownArgs,@@ -869,17 +869,26 @@ export class SshProjectManager {* regardless of whether the nodes were mounted (only the active project's nodes are). `nodeIds`* are raw node ids; we map each to its `nt-<id>` session name (the same name `spawnSession` /* `remoteTmuxHasSessionArgs` use). Best-effort per id, a missing session is ignored.+ *+ * The kill goes to EVERY tmux socket on the host, not just the `nodeterm-rmt` one an SSH project+ * spawns on. Callers here know only a session NAME: project deletion knows its own nodes (always+ * remote), but the session-memory panel passes rows swept off BOTH of the host's sockets — and a+ * host that runs its own `nodeterm-server` keeps those sessions on `node-terminal`. Those rows+ * used to get a confirm reading "this stops its tmux session" followed by a kill aimed at the+ * other socket, so nothing died. See `KILL_TMUX_SOCKETS`.*/async killSessions(projectId: string, nodeIds: string[]): Promise<void> {const c = this.conns.get(projectId)if (!c) returnawait Promise.all(- nodeIds.map((id) =>- this.r.run(remoteTmuxKillArgs(c.conn, c.controlPath, sessionName(id))).then(- () => undefined,- () => undefined+ nodeIds+ .flatMap((id) => remoteTmuxKillEverySocketArgs(c.conn, c.controlPath, sessionName(id)))+ .map((args) =>+ this.r.run(args).then(+ () => undefined,+ () => undefined+ ))- ))}