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.

feat(ssh-project): pure control-master + remote-tmux builders + shared types

enes5d3509af2f66
diff --git a/src/main/remote-ssh/control-master.test.ts b/src/main/remote-ssh/control-master.test.ts
new file mode 100644
index 000000000..67573d55e
--- /dev/null
+++ b/src/main/remote-ssh/control-master.test.ts
@@ -0,0 +1,64 @@
+import { describe, expect, it } from 'vitest'
+import {
+ controlPathFor,
+ masterArgs,
+ childArgs,
+ remoteTmuxHasSessionArgs,
+ listDirArgs,
+ RMT_TMUX_SOCKET
+} from './control-master'
+
+const conn = { host: 'h.example.com', user: 'deploy', port: 2222, identityFile: '/k/id' }
+
+describe('controlPathFor', () => {
+ it('puts a per-project socket under <userData>/ssh-cm', () => {
+ expect(controlPathFor('/ud', 'proj1')).toBe('/ud/ssh-cm/proj1.sock')
+ })
+})
+
+describe('masterArgs', () => {
+ it('builds a backgrounded multiplexing master with the control path + identity + port', () => {
+ expect(masterArgs(conn, '/ud/ssh-cm/p.sock')).toEqual([
+ '-M', '-N',
+ '-o', 'ControlMaster=auto',
+ '-o', 'ControlPath=/ud/ssh-cm/p.sock',
+ '-o', 'ControlPersist=300',
+ '-o', 'BatchMode=no',
+ '-p', '2222',
+ '-i', '/k/id',
+ ])
+ })
+})
+
+describe('childArgs', () => {
+ it('reuses the master socket (no new master) and appends a remote command', () => {
+ expect(childArgs(conn, '/s.sock', 'tmux ls')).toEqual([
+ '-o', 'ControlMaster=no',
+ '-o', 'ControlPath=/s.sock',
+ '-p', '2222',
+ 'tmux ls'
+ ])
+ })
+})
+
+describe('remoteTmuxHasSessionArgs', () => {
+ it('checks the remote socket for the node session', () => {
+ expect(remoteTmuxHasSessionArgs(conn, '/s.sock', 'nt-x')).toEqual([
+ '-o', 'ControlMaster=no', '-o', 'ControlPath=/s.sock', '-p', '2222',
+ `tmux -L ${RMT_TMUX_SOCKET} has-session -t nt-x`
+ ])
+ })
+})
+
+describe('listDirArgs', () => {
+ it('lists directory entries of a quoted path', () => {
+ expect(listDirArgs(conn, '/s.sock', '/srv/app')).toEqual([
+ '-o', 'ControlMaster=no', '-o', 'ControlPath=/s.sock', '-p', '2222',
+ `ls -1Ap '/srv/app'`
+ ])
+ })
+})
diff --git a/src/main/remote-ssh/control-master.ts b/src/main/remote-ssh/control-master.ts
new file mode 100644
index 000000000..c5eb1aabe
--- /dev/null
+++ b/src/main/remote-ssh/control-master.ts
@@ -0,0 +1,85 @@
+// Pure ssh/tmux argv + remote-command builders for SSH projects. No electron/node-pty imports
+// — unit-testable in isolation. The electron/spawn wiring lives in ssh-project.ts + pty-manager.
+import { posixQuote, remoteTmuxCommand, type SshConnection } from '../../shared/ssh'
+
+/** Dedicated remote tmux socket so an SSH project never collides with the user's own tmux. */
+export const RMT_TMUX_SOCKET = 'nodeterm-rmt'
+
+/** Per-project ControlMaster socket path under <userData>/ssh-cm. */
+export function controlPathFor(userDataDir: string, projectId: string): string {
+ return `${userDataDir}/ssh-cm/${projectId}.sock`
+}
+
+function target(conn: SshConnection): string {
+ return `${conn.user}@${conn.host}`
+}
+
+function portArgs(conn: SshConnection): string[] {
+ return ['-p', String(conn.port ?? 22)]
+}
+
+/** Args for the backgrounded multiplexing master (the one auth happens here). */
+export function masterArgs(conn: SshConnection, controlPath: string): string[] {
+ const args = [
+ '-M',
+ '-N',
+ '-o',
+ 'ControlMaster=auto',
+ '-o',
+ `ControlPath=${controlPath}`,
+ '-o',
+ 'ControlPersist=300',
+ '-o',
+ 'BatchMode=no',
+ ...portArgs(conn)
+ ]
+ if (conn.identityFile) args.push('-i', conn.identityFile)
+ args.push(target(conn))
+ return args
+}
+
+/** Args for a child ssh that reuses the master socket; `remote` is an optional remote command. */
+export function childArgs(conn: SshConnection, controlPath: string, remote?: string): string[] {
+ const args = ['-o', 'ControlMaster=no', '-o', `ControlPath=${controlPath}`, ...portArgs(conn), target(conn)]
+ if (remote !== undefined) args.push(remote)
+ return args
+}
+
+export function checkMasterArgs(conn: SshConnection, controlPath: string): string[] {
+ return ['-O', 'check', '-o', `ControlPath=${controlPath}`, ...portArgs(conn), target(conn)]
+}
+export function exitMasterArgs(conn: SshConnection, controlPath: string): string[] {
+ return ['-O', 'exit', '-o', `ControlPath=${controlPath}`, ...portArgs(conn), target(conn)]
+}
+export function remoteTmuxHasSessionArgs(conn: SshConnection, controlPath: string, sessionId: string): string[] {
+ return childArgs(conn, controlPath, `tmux -L ${RMT_TMUX_SOCKET} has-session -t ${sessionId}`)
+}
+export function remoteTmuxKillArgs(conn: SshConnection, controlPath: string, sessionId: string): string[] {
+ return childArgs(conn, controlPath, `tmux -L ${RMT_TMUX_SOCKET} kill-session -t ${sessionId}`)
+}
+export function remoteCapturePaneArgs(conn: SshConnection, controlPath: string, sessionId: string, full: boolean): string[] {
+ return childArgs(
+ conn,
+ controlPath,
+ `tmux -L ${RMT_TMUX_SOCKET} capture-pane -p -e -t ${sessionId} -S ${full ? '-' : '-200'}`
+ )
+}
+export function tmuxProbeArgs(conn: SshConnection, controlPath: string): string[] {
+ return childArgs(conn, controlPath, 'command -v tmux')
+}
+export function listDirArgs(conn: SshConnection, controlPath: string, path: string): string[] {
+ return childArgs(conn, controlPath, `ls -1Ap ${posixQuote(path)}`)
+}
+
+/** The remote PTY program is `ssh <childArgs> host -t '<remoteTmuxCommand>'`. */
+export function remoteTmuxPtyArgs(
+ conn: SshConnection,
+ controlPath: string,
+ sessionId: string,
+ remoteCwd: string,
+ program?: string,
+ programArgs?: string[]
+): string[] {
+ const cmd = remoteTmuxCommand({ sessionId, remoteCwd, program, programArgs, socket: RMT_TMUX_SOCKET })
+ return ['-t', ...childArgs(conn, controlPath, cmd)]
+}
diff --git a/src/shared/ssh.test.ts b/src/shared/ssh.test.ts
index 81f09880d..a341b4abf 100644
--- a/src/shared/ssh.test.ts
+++ b/src/shared/ssh.test.ts
@@ -1,5 +1,12 @@
import { describe, expect, it } from 'vitest'
-import { buildSshArgs, parseExtraArgs, parseSshConfig } from './ssh'
+import {
+ buildSshArgs,
+ parseExtraArgs,
+ parseSshConfig,
+ posixQuote,
+ remoteTmuxCommand,
+ parseLsDirs
+} from './ssh'
describe('parseSshConfig', () => {
it('parses a named host with HostName/User/Port/IdentityFile', () => {
@@ -78,3 +85,29 @@ describe('parseExtraArgs', () => {
expect(parseExtraArgs(' ')).toEqual([])
})
})
+
+describe('posixQuote', () => {
+ it('single-quotes and escapes embedded quotes', () => {
+ expect(posixQuote(`a b`)).toBe(`'a b'`)
+ expect(posixQuote(`it's`)).toBe(`'it'\\''s'`)
+ })
+})
+
+describe('remoteTmuxCommand', () => {
+ it('builds attach-or-create on the remote socket with a quoted cwd', () => {
+ expect(remoteTmuxCommand({ sessionId: 'nt-x', remoteCwd: '/srv/app' })).toBe(
+ `tmux -L nodeterm-rmt new-session -A -s 'nt-x' -c '/srv/app'`
+ )
+ })
+ it('appends a quoted program + args when given', () => {
+ expect(
+ remoteTmuxCommand({ sessionId: 'nt-x', remoteCwd: '/a', program: 'ssh', programArgs: ['-A', 'h'] })
+ ).toBe(`tmux -L nodeterm-rmt new-session -A -s 'nt-x' -c '/a' 'ssh' '-A' 'h'`)
+ })
+})
+
+describe('parseLsDirs', () => {
+ it('keeps only directory entries from `ls -1Ap`, dropping ./ and ../', () => {
+ expect(parseLsDirs('./\n../\nsrc/\nREADME.md\n.git/\nbin/\n')).toEqual(['.git', 'bin', 'src'])
+ })
+})
diff --git a/src/shared/ssh.ts b/src/shared/ssh.ts
index f0358b69e..00eba282d 100644
--- a/src/shared/ssh.ts
+++ b/src/shared/ssh.ts
@@ -62,6 +62,48 @@ export function buildSshArgs(conn: SshConnection): string[] {
return args
}
+/** Single-quote a string for use as ONE POSIX shell token (safe inside a remote command). */
+export function posixQuote(s: string): string {
+ return `'${s.replace(/'/g, `'\\''`)}'`
+}
+
+/** Build the remote shell command that attaches-or-creates this node's remote tmux session. */
+export function remoteTmuxCommand(opts: {
+ sessionId: string
+ remoteCwd: string
+ program?: string
+ programArgs?: string[]
+ socket?: string
+}): string {
+ const socket = opts.socket ?? 'nodeterm-rmt'
+ const parts = [
+ 'tmux',
+ '-L',
+ socket,
+ 'new-session',
+ '-A',
+ '-s',
+ posixQuote(opts.sessionId),
+ '-c',
+ posixQuote(opts.remoteCwd)
+ ]
+ if (opts.program) {
+ parts.push(posixQuote(opts.program))
+ for (const a of opts.programArgs ?? []) parts.push(posixQuote(a))
+ }
+ return parts.join(' ')
+}
+
+/** Parse `ls -1Ap <dir>` output into sorted directory names (trailing `/`), excluding . and .. */
+export function parseLsDirs(stdout: string): string[] {
+ return stdout
+ .split('\n')
+ .map((l) => l.trim())
+ .filter((l) => l.endsWith('/') && l !== './' && l !== '../')
+ .map((l) => l.slice(0, -1))
+ .sort((a, b) => a.localeCompare(b))
+}
+
/** A host parsed from `~/.ssh/config`, ready to seed a saved server (no id yet). */
export interface ParsedSshHost {
/** The `Host` alias (display label). */
diff --git a/src/shared/types.ts b/src/shared/types.ts
index 93c42e05c..8a568c376 100644
--- a/src/shared/types.ts
+++ b/src/shared/types.ts
@@ -22,6 +22,8 @@ export interface PtyCreateOptions {
* real value in a later phase.
*/
agentId?: AgentId
+ /** When set, this PTY runs on a remote host over the project's ssh ControlMaster, in remote tmux. */
+ sshRemote?: { controlPath: string; conn: import('./ssh').SshConnection; remoteCwd: string }
}
/**
@@ -111,6 +113,8 @@ export interface Project {
color: string
/** Default working directory for new terminals created in this project. */
cwd?: string
+ /** When set, this is an SSH project: its terminals run on `server` in `remoteCwd` (remote tmux). */
+ ssh?: { server: import('./ssh').SshConnection; remoteCwd: string }
viewport: Viewport
nodes: CanvasNodeState[]
/** Bridge links between Claude nodes (optional; absent in pre-bridge files). */
@@ -305,6 +309,18 @@ export interface SshApi {
importCandidates(): Promise<import('./ssh').ParsedSshHost[]>
}
+export type SshProjectStatus = 'connecting' | 'connected' | 'disconnected' | 'reconnecting' | 'error'
+
+export interface SshProjectApi {
+ /** Open (or reuse) the ControlMaster for an SSH project; resolves once connected. */
+ connect(projectId: string, server: import('./ssh').SshConnection): Promise<{ controlPath: string }>
+ /** Tear down the master (remote tmux is unaffected). */
+ disconnect(projectId: string): Promise<void>
+ /** List remote sub-directories of `path` (default ~). */
+ listDir(projectId: string, path: string): Promise<{ path: string; dirs: string[] }>
+ onStatus(cb: (e: { projectId: string; status: SshProjectStatus; error?: string }) => void): () => void
+}
+
export interface GitFileChange {
path: string
/** Single-letter status: M (modified), A (added), D (deleted), R (renamed), U (untracked). */
@@ -720,6 +736,8 @@ export interface NodeTerminalApi {
dialog: DialogApi
settings: SettingsApi
ssh: SshApi
+ /** Optional until the preload wiring task lands its implementation (SSH Projects Phase 1). */
+ sshProject?: SshProjectApi
git: GitApi
clipboard: ClipboardApi
shell: ShellApi