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(b4): tear down remote relay connection on node close + cleanups

enesf42646e34069
diff --git a/src/main/remote/host-service.ts b/src/main/remote/host-service.ts
index 2d752f208..322e707f4 100644
--- a/src/main/remote/host-service.ts
+++ b/src/main/remote/host-service.ts
@@ -92,16 +92,13 @@ function str(value: unknown): string | undefined {
* `nextStreamId` lets tests assert deterministic ids; production uses a monotonic counter.
*/
export function createHostHandlers(pty: HostPtyManager, socket: HostRelaySocket): HostHandlers {
- // streamId -> Stream, plus the reverse (sessionId -> streamId) so PTY callbacks resolve fast.
+ // streamId -> Stream. PTY callbacks close over their own `streamId` directly, so no
+ // reverse (sessionId -> streamId) index is needed.
const streams = new Map<number, Stream>()
- const bySession = new Map<string, number>()
let streamCounter = 0
function dropStream(streamId: number): void {
- const stream = streams.get(streamId)
- if (!stream) return
streams.delete(streamId)
- bySession.delete(stream.sessionId)
}
function handleCreate(req: RpcRequest): void {
@@ -152,7 +149,6 @@ export function createHostHandlers(pty: HostPtyManager, socket: HostRelaySocket)
}
stream.sessionId = sessionId
streams.set(streamId, stream)
- bySession.set(sessionId, streamId)
socket.respond(req.id, true, { streamId })
}
@@ -203,7 +199,6 @@ export function createHostHandlers(pty: HostPtyManager, socket: HostRelaySocket)
pty.kill(stream.sessionId)
}
streams.clear()
- bySession.clear()
}
}
}
diff --git a/src/main/remote/relay-socket.ts b/src/main/remote/relay-socket.ts
index 6ee1fab85..d941cb281 100644
--- a/src/main/remote/relay-socket.ts
+++ b/src/main/remote/relay-socket.ts
@@ -367,6 +367,12 @@ export function connectRelay(opts: ConnectRelayOptions): RelaySocket {
}
}
+ // KNOWN MVP LIMITATION: this reconnect path re-dials with the *same* pairing token, which
+ // the real relay permanently rejects (the token is single-use and consumed on first pair,
+ // with a ~600s TTL). So a post-drop reconnect can never re-authenticate against the live
+ // relay — a working reconnect would need a freshly minted token, which is out of MVP scope.
+ // The machinery is kept (it's harmless and useful once token re-minting exists); don't be
+ // misled into thinking dropped connections currently recover on their own.
function scheduleReconnect(): void {
if (reconnectTimer || intentionallyClosed) {
return
diff --git a/src/renderer/canvas/Canvas.tsx b/src/renderer/canvas/Canvas.tsx
index 6f839124c..098755ba6 100644
--- a/src/renderer/canvas/Canvas.tsx
+++ b/src/renderer/canvas/Canvas.tsx
@@ -795,6 +795,20 @@ export function Canvas() {
// Remote terminals have no local persistent session — only destroy local ones.
if (set.has(n.id) && n.type === 'terminal' && !n.data.remote) transport.destroy(n.id)
})
+ // Tear down relay connections owned solely by the deleted remote node(s). The model is
+ // N:1 (one connection per remote node), but dedupe defensively: only disconnect a
+ // connectionId if no *surviving* remote node still uses it, so we never drop a live one.
+ const deletedConns = new Set<string>()
+ const survivingConns = new Set<string>()
+ nodesRef.current.forEach((n) => {
+ const conn = (n.data.remote as { connectionId: string } | undefined)?.connectionId
+ if (!conn) return
+ if (set.has(n.id)) deletedConns.add(conn)
+ else survivingConns.add(conn)
+ })
+ deletedConns.forEach((conn) => {
+ if (!survivingConns.has(conn)) void window.nodeTerminal.remoteClient.disconnect(conn)
+ })
setNodes((ns) => {
// Free children of any deleted group back to absolute positions.
const groupPos = new Map(
diff --git a/src/renderer/nodes/TerminalNode.tsx b/src/renderer/nodes/TerminalNode.tsx
index 685dcb072..97bab7622 100644
--- a/src/renderer/nodes/TerminalNode.tsx
+++ b/src/renderer/nodes/TerminalNode.tsx
@@ -174,6 +174,18 @@ export function TerminalNode({ id, data, selected }: NodeProps<CanvasNode>) {
let disposed = false
const cleanups: Array<() => void> = []
+ // Remote nodes: surface a dropped relay connection (host gone / socket closed) so the
+ // terminal isn't silently frozen. The actual relay teardown on node *deletion* happens
+ // in Canvas.deleteNodes (which can dedupe a connectionId shared by sibling nodes).
+ const remoteConn = (data.remote as { connectionId: string } | undefined)?.connectionId
+ if (remoteConn) {
+ cleanups.push(
+ window.nodeTerminal.remoteClient.onClosed(remoteConn, () => {
+ term.write('\r\n\x1b[31m[remote disconnected]\x1b[0m\r\n')
+ })
+ )
+ }
+
// Agent state (busy/idle/attention) comes from the agent's own hooks via the
// agent:status IPC (handled centrally in Canvas) — not from parsing the output here.
// We only surface the conversation topic from the terminal title, when the agent sets one.
@@ -553,7 +565,14 @@ export function TerminalNode({ id, data, selected }: NodeProps<CanvasNode>) {
className="term-node__close"
title="Close (ends the session)"
onClick={() => {
- transport.destroy(id)
+ const remoteConn = (data.remote as { connectionId: string } | undefined)?.connectionId
+ if (remoteConn) {
+ // Remote node: no local tmux to destroy; tear down the relay connection
+ // (socket + keepalive + main-process map entry). N:1, so this owns it.
+ void window.nodeTerminal.remoteClient.disconnect(remoteConn)
+ } else {
+ transport.destroy(id)
+ }
deleteElements({ nodes: [{ id }] })
}}
>