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(ssh): an attachment always dials — a cached controlPath is not "connected"

Claude Code9c7894bca7d1
diff --git a/src/renderer/canvas/Canvas.tsx b/src/renderer/canvas/Canvas.tsx
index 5b95054be..d96b8f880 100644
--- a/src/renderer/canvas/Canvas.tsx
+++ b/src/renderer/canvas/Canvas.tsx
@@ -196,6 +196,8 @@ import { isSafeQuickOpenRelPath } from '@shared/quick-open-filter'
* reached for inside it, so that helper stays testable without an Electron preload. */
const sshConnect: SshConnectFn = (scopeId, conn, remoteCwd) =>
window.nodeTerminal.sshProject.connect(scopeId, conn, remoteCwd)
+const sshDisconnect = (scopeId: string): Promise<unknown> =>
+ window.nodeTerminal.sshProject.disconnect(scopeId)
import { opensInEditor } from '../lib/openTarget'
import { newEntryPath, parentDir } from '../lib/explorerCreate'
import { useProjects } from '../state/projects'
@@ -1740,7 +1742,8 @@ export function Canvas() {
remoteCwd: attachment.remoteCwd,
ownerProjectId: project.id
},
- sshConnect
+ sshConnect,
+ sshDisconnect
)
}
loadingRef.current = true
@@ -7718,7 +7721,7 @@ export function Canvas() {
// still reachable here. Reconnect it on its own loop (its master is its own) and leave git
// routing alone: the owning project is local, or points somewhere else entirely.
const attached = useSshConn.getState().getAttachment(scopeId)
- if (attached) return connectHostAttachment(scopeId, attached, sshConnect)
+ if (attached) return connectHostAttachment(scopeId, attached, sshConnect, sshDisconnect)
const projectId = scopeId
const project = useProjects.getState().getProject(projectId)
if (!project?.ssh) return false
diff --git a/src/renderer/lib/sshAttachments.test.ts b/src/renderer/lib/sshAttachments.test.ts
index 2789eb025..5c4bef0c0 100644
--- a/src/renderer/lib/sshAttachments.test.ts
+++ b/src/renderer/lib/sshAttachments.test.ts
@@ -148,17 +148,38 @@ describe('connectHostAttachment', () => {
expect(connect).toHaveBeenCalledTimes(1)
})
- it('short-circuits on a live master but still refreshes the record', async () => {
- const connect = vi.fn(async () => ({ controlPath: '/tmp/other' }))
- useSshConn.getState().setConn('a1', { controlPath: '/tmp/live' })
+ it('still dials when a controlPath is already cached — the reconnect path depends on it', async () => {
+ // The regression this guards: treating a cached path as "already connected". NOTHING in the
+ // renderer clears it when a connection drops, so the reconnect coordinator's connect dep
+ // would answer true forever after one success — no `-O check`, no redial — and the respawned
+ // ptys would run against a dead socket, which `ControlMaster=auto` silently turns into a
+ // DIRECT connection per node (the ~72k-logins/day pattern). Only main can tell a live master
+ // from a dead one, and its reuse branch costs one mux'd `-O check`.
+ const connect = vi.fn(async () => ({ controlPath: '/tmp/fresh' }))
+ useSshConn.getState().setConn('a1', { controlPath: '/tmp/stale-after-a-drop' })
expect(await connectHostAttachment('a1', ATTACHMENT, connect)).toBe(true)
- expect(connect).not.toHaveBeenCalled()
- // A tab switch back adopts the live master; the NEXT drop still needs the endpoint on record.
+ expect(connect).toHaveBeenCalledTimes(1)
+ expect(useSshConn.getState().getControlPath('a1')).toBe('/tmp/fresh')
expect(useSshConn.getState().getAttachment('a1')).toEqual(ATTACHMENT)
})
+ it('a dial that lands after the canvas was deleted is handed back, not recorded', async () => {
+ let release: (v: { controlPath: string }) => void
+ const connect = vi.fn(() => new Promise<{ controlPath: string }>((r) => (release = r)))
+ const disconnect = vi.fn(async () => {})
+
+ const pending = connectHostAttachment('a1', ATTACHMENT, connect, disconnect)
+ useSshConn.getState().clearAttachment('a1') // the project was deleted mid-dial
+ release!({ controlPath: '/tmp/cm' })
+
+ expect(await pending).toBe(false)
+ // Neither resurrected in the store nor leaked on the host.
+ expect(useSshConn.getState().getControlPath('a1')).toBeUndefined()
+ expect(disconnect).toHaveBeenCalledWith('a1')
+ })
+
it('dials each machine separately', async () => {
const connect = vi.fn(async () => ({ controlPath: '/tmp/cm' }))
diff --git a/src/renderer/lib/sshAttachments.ts b/src/renderer/lib/sshAttachments.ts
index a9408b5bc..2005dc375 100644
--- a/src/renderer/lib/sshAttachments.ts
+++ b/src/renderer/lib/sshAttachments.ts
@@ -88,22 +88,34 @@ const dialing = new Map<string, Promise<boolean>>()
* 3. Concurrent callers share one dial. Every node on a machine calls this at spawn; without the
* in-flight map they would each open a master.
*
- * Idempotent and safe to call on every spawn: a live `controlPath` short-circuits.
+ * It ALWAYS dials — a cached `controlPath` is deliberately not treated as "already connected".
+ * Nothing in the renderer clears that path when a connection drops, so a short-circuit on it made
+ * the reconnect coordinator a permanent no-op for attachments: after one successful connect it
+ * would answer `true` forever, and a sleep/wake respawn would run its pty against a dead socket
+ * under `ControlMaster=auto`, which silently falls back to a DIRECT connection per node — the
+ * ~72k-logins/day pattern documented on `SshProjectManager.startWatchdog`. Main is the thing that
+ * knows whether the master is alive: its connect coalesces per scope and reuse costs one mux'd
+ * `-O check`, which doubles as the keepalive that resets the ControlPersist counter. A redundant
+ * dial through main is cheap and self-healing; a skipped one is the failure above.
*/
export function connectHostAttachment(
scopeId: string,
attachment: SshAttachment,
- connect: SshConnectFn
+ connect: SshConnectFn,
+ disconnect?: (scopeId: string) => Promise<unknown>
): Promise<boolean> {
- const store = useSshConn.getState()
- // Registered even on the short-circuit path: an attachment adopted from an already-live master
- // (a tab switch back) still needs its endpoint on record for the NEXT drop.
- store.registerAttachment(scopeId, attachment)
- if (store.getControlPath(scopeId)) return Promise.resolve(true)
+ useSshConn.getState().registerAttachment(scopeId, attachment)
const inFlight = dialing.get(scopeId)
if (inFlight) return inFlight
const attempt = connect(scopeId, attachment.conn, attachment.remoteCwd)
.then((info) => {
+ // The canvas was deleted while we were dialing: its teardown pass already ran, and writing
+ // the connection back now would resurrect a scope nothing owns — a live master that no
+ // delete path will ever visit again. Hand it straight back instead.
+ if (!useSshConn.getState().getAttachment(scopeId)) {
+ void disconnect?.(scopeId)
+ return false
+ }
useSshConn.getState().setConn(scopeId, info)
return true
})
diff --git a/src/renderer/nodes/TerminalNode.tsx b/src/renderer/nodes/TerminalNode.tsx
index e9a795bcf..fd111ba9b 100644
--- a/src/renderer/nodes/TerminalNode.tsx
+++ b/src/renderer/nodes/TerminalNode.tsx
@@ -221,7 +221,8 @@ export async function resolveSshRemote(
remoteCwd: cwd,
ownerProjectId: activeProjectId
},
- (scopeId, c, remoteCwd) => window.nodeTerminal.sshProject.connect(scopeId, c, remoteCwd)
+ (scopeId, c, remoteCwd) => window.nodeTerminal.sshProject.connect(scopeId, c, remoteCwd),
+ (scopeId) => window.nodeTerminal.sshProject.disconnect(scopeId)
)
}
let controlPath = useSshConn.getState().getControlPath(projectId)
diff --git a/src/shared/ssh.test.ts b/src/shared/ssh.test.ts
index e9463efe2..fc404909a 100644
--- a/src/shared/ssh.test.ts
+++ b/src/shared/ssh.test.ts
@@ -54,6 +54,21 @@ describe('sshConnectionIdForProject', () => {
expect(sshConnectionIdForProject('project-1', ubuntu, { ...ubuntu })).toBe('project-1')
})
+ it('a node with no host is never served from the project (unroutable fails safe)', () => {
+ // `undefined === undefined` would otherwise hand a hostless node a LOCAL project's id — a
+ // master that was never opened for it. Failing as an attachment ends at `requireRemote`.
+ // `undefined` specifically: a LOCAL project passes no server at all, so `projectServer?.host`
+ // is also undefined and the two compare EQUAL.
+ const hostless = { host: undefined, user: 'corvin' } as unknown as typeof ubuntu
+ expect(sshConnectionIdForProject('local-1', hostless)).not.toBe('local-1')
+ expect(sshConnectionIdForProject('local-1', hostless)).toBe(
+ sshAttachmentId('local-1', hostless)
+ )
+ // An empty host is just as unroutable.
+ const empty = { host: '', user: 'corvin' } as unknown as typeof ubuntu
+ expect(sshConnectionIdForProject('local-1', empty)).not.toBe('local-1')
+ })
+
it('a LOCAL project has no binding, so every remote node on it is an attachment', () => {
expect(sshConnectionIdForProject('local-1', ubuntu, undefined)).toBe(
sshAttachmentId('local-1', ubuntu)
diff --git a/src/shared/ssh.ts b/src/shared/ssh.ts
index 75a415fd0..330215469 100644
--- a/src/shared/ssh.ts
+++ b/src/shared/ssh.ts
@@ -169,7 +169,13 @@ export function sshConnectionIdForProject(
conn: SshConnection,
projectServer?: SshConnection
): string {
- return projectServer?.host === conn.host ? projectId : sshAttachmentId(projectId, conn)
+ // `conn.host` is checked explicitly: a node whose binding lost its host (bad persisted data)
+ // must not match a LOCAL project's absent one through `undefined === undefined` and get served
+ // from a master that was never opened for it. Unroutable either way — but failing as an
+ // attachment ends at `requireRemote` refusing the spawn, which is the safe direction.
+ return conn.host && projectServer?.host === conn.host
+ ? projectId
+ : sshAttachmentId(projectId, conn)
}
/**