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): ControlMaster manager + listDir + status IPC

enes18eb8dbc9f26
diff --git a/src/main/index.ts b/src/main/index.ts
index d35adc852..9a02b00e7 100644
--- a/src/main/index.ts
+++ b/src/main/index.ts
@@ -28,6 +28,7 @@ import { initClaudeUsage } from './claude-usage'
import { initLicense } from './license'
import { initRemoteHost } from './remote/host-service'
import { initRemoteClient } from './remote/client-service'
+import { initSshProject } from './remote-ssh/ssh-project'
// Dev-only: NT_MULTI lets a SECOND instance run (host + client testing on one machine) with an
// isolated userData via NT_USER_DATA — its own device-id/session/license/workspace. Never active
@@ -350,6 +351,7 @@ app.whenReady().then(async () => {
initLicense(win)
initRemoteHost(win, ptyManager)
initRemoteClient(win, { isPackaged: app.isPackaged })
+ initSshProject(win)
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow()
diff --git a/src/main/remote-ssh/ssh-project.test.ts b/src/main/remote-ssh/ssh-project.test.ts
new file mode 100644
index 000000000..6d8685bb3
--- /dev/null
+++ b/src/main/remote-ssh/ssh-project.test.ts
@@ -0,0 +1,41 @@
+import { describe, expect, it, vi } from 'vitest'
+import { SshProjectManager } from './ssh-project'
+
+const conn = { host: 'h', user: 'u' }
+
+function makeMgr() {
+ const statuses: string[] = []
+ // spawnMaster: returns a fake child that "stays up"; run: resolves stdout for one-shot ssh.
+ const spawnMaster = vi.fn(() => ({ kill: vi.fn(), on: vi.fn() }))
+ const run = vi.fn(async (_args: string[]) => ({ code: 0, stdout: 'src/\nbin/\n' }))
+ const mgr = new SshProjectManager({
+ userDataDir: '/ud',
+ spawnMaster,
+ run,
+ onStatus: (e) => statuses.push(e.status)
+ })
+ return { mgr, statuses, spawnMaster, run }
+}
+
+describe('SshProjectManager', () => {
+ it('connect emits connecting→connected and returns the control path', async () => {
+ const { mgr, statuses } = makeMgr()
+ const { controlPath } = await mgr.connect('p1', conn)
+ expect(controlPath).toBe('/ud/ssh-cm/p1.sock')
+ expect(statuses).toEqual(['connecting', 'connected'])
+ })
+
+ it('connect is idempotent — second call reuses the live master', async () => {
+ const { mgr, spawnMaster } = makeMgr()
+ await mgr.connect('p1', conn)
+ await mgr.connect('p1', conn)
+ expect(spawnMaster).toHaveBeenCalledTimes(1)
+ })
+
+ it('listDir parses remote dir entries', async () => {
+ const { mgr } = makeMgr()
+ await mgr.connect('p1', conn)
+ const { dirs } = await mgr.listDir('p1', '~')
+ expect(dirs).toEqual(['bin', 'src'])
+ })
+})
diff --git a/src/main/remote-ssh/ssh-project.ts b/src/main/remote-ssh/ssh-project.ts
new file mode 100644
index 000000000..59aae289f
--- /dev/null
+++ b/src/main/remote-ssh/ssh-project.ts
@@ -0,0 +1,131 @@
+import { promises as fs } from 'fs'
+import path from 'path'
+import { spawn, execFile, execFileSync } from 'child_process'
+import { app, ipcMain, type BrowserWindow } from 'electron'
+import { IPC } from '../../shared/ipc'
+import { parseLsDirs, type SshConnection } from '../../shared/ssh'
+import type { SshProjectStatus } from '../../shared/types'
+import {
+ controlPathFor,
+ masterArgs,
+ listDirArgs,
+ exitMasterArgs,
+ checkMasterArgs
+} from './control-master'
+
+interface Runners {
+ userDataDir: string
+ /** Spawn the long-lived master; returns a handle we can kill. */
+ spawnMaster: (args: string[]) => { kill: () => void; on: (ev: string, cb: (...a: unknown[]) => void) => void }
+ /** Run a one-shot ssh, resolving its stdout + exit code. */
+ run: (args: string[]) => Promise<{ code: number; stdout: string }>
+ onStatus: (e: { projectId: string; status: SshProjectStatus; error?: string }) => void
+}
+
+interface Conn {
+ conn: SshConnection
+ controlPath: string
+ master: ReturnType<Runners['spawnMaster']>
+}
+
+/**
+ * Resolve an absolute ssh path; GUI apps don't inherit the shell PATH.
+ * Mirrors findSsh() in pty-manager.ts: a cached login-shell `command -v ssh` lookup with
+ * common-location fallbacks. (Do NOT use the brief's always-returns-first stub.)
+ */
+let cachedSsh: string | null | undefined
+function sshBin(): string {
+ if (cachedSsh !== undefined) return cachedSsh ?? 'ssh'
+ try {
+ const out = execFileSync(process.env.SHELL || '/bin/bash', ['-lc', 'command -v ssh'], {
+ encoding: 'utf-8'
+ }).trim()
+ cachedSsh = out || null
+ } catch {
+ cachedSsh = null
+ }
+ if (!cachedSsh) {
+ for (const p of ['/usr/bin/ssh', '/usr/local/bin/ssh', '/opt/homebrew/bin/ssh']) {
+ try {
+ execFileSync(p, ['-V'], { stdio: 'ignore' })
+ cachedSsh = p
+ break
+ } catch {
+ // keep trying
+ }
+ }
+ }
+ return cachedSsh ?? 'ssh'
+}
+
+export class SshProjectManager {
+ private conns = new Map<string, Conn>()
+ constructor(private r: Runners) {}
+
+ async connect(projectId: string, conn: SshConnection): Promise<{ controlPath: string }> {
+ const existing = this.conns.get(projectId)
+ if (existing) return { controlPath: existing.controlPath }
+ const controlPath = controlPathFor(this.r.userDataDir, projectId)
+ // Best-effort: the socket dir lives under <userData> in production. If it can't be made,
+ // the master/`-O check` loop below fails and we report an error status anyway.
+ try {
+ await fs.mkdir(path.dirname(controlPath), { recursive: true, mode: 0o700 })
+ } catch {
+ // ignore — keeps the manager unit-testable with a synthetic userDataDir
+ }
+ this.r.onStatus({ projectId, status: 'connecting' })
+ const master = this.r.spawnMaster(masterArgs(conn, controlPath))
+ this.conns.set(projectId, { conn, controlPath, master })
+ // Wait until the master answers `-O check`, retrying briefly.
+ for (let i = 0; i < 50; i++) {
+ const { code } = await this.r.run(checkMasterArgs(conn, controlPath))
+ if (code === 0) {
+ this.r.onStatus({ projectId, status: 'connected' })
+ return { controlPath }
+ }
+ await new Promise((res) => setTimeout(res, 100))
+ }
+ this.disconnect(projectId)
+ this.r.onStatus({ projectId, status: 'error', error: 'Could not establish the SSH connection.' })
+ throw new Error('Could not establish the SSH connection.')
+ }
+
+ async listDir(projectId: string, dir: string): Promise<{ path: string; dirs: string[] }> {
+ const c = this.conns.get(projectId)
+ if (!c) throw new Error('Not connected.')
+ const { stdout } = await this.r.run(listDirArgs(c.conn, c.controlPath, dir))
+ return { path: dir, dirs: parseLsDirs(stdout) }
+ }
+
+ disconnect(projectId: string): void {
+ const c = this.conns.get(projectId)
+ if (!c) return
+ void this.r.run(exitMasterArgs(c.conn, c.controlPath))
+ c.master.kill()
+ this.conns.delete(projectId)
+ this.r.onStatus({ projectId, status: 'disconnected' })
+ }
+}
+
+export function initSshProject(win: BrowserWindow): SshProjectManager {
+ const ssh = sshBin()
+ const mgr = new SshProjectManager({
+ userDataDir: app.getPath('userData'),
+ spawnMaster: (args) => spawn(ssh, args, { stdio: 'ignore' }),
+ run: (args) =>
+ new Promise((resolve) =>
+ execFile(ssh, args, { timeout: 15000 }, (err, stdout) =>
+ resolve({ code: err ? ((err as { code?: number }).code ?? 1) : 0, stdout: stdout ?? '' })
+ )
+ ),
+ onStatus: (e) => {
+ if (!win.isDestroyed()) win.webContents.send(IPC.sshProjectStatus, e)
+ }
+ })
+ ipcMain.handle(IPC.sshConnectProject, (_e, projectId: string, conn: SshConnection) =>
+ mgr.connect(projectId, conn)
+ )
+ ipcMain.handle(IPC.sshDisconnectProject, (_e, projectId: string) => mgr.disconnect(projectId))
+ ipcMain.handle(IPC.sshListDir, (_e, projectId: string, dir: string) => mgr.listDir(projectId, dir))
+ return mgr
+}
diff --git a/src/preload/index.ts b/src/preload/index.ts
index 4bbf53253..8d463e7a7 100644
--- a/src/preload/index.ts
+++ b/src/preload/index.ts
@@ -84,6 +84,16 @@ const api: NodeTerminalApi = {
remove: (id) => ipcRenderer.invoke(IPC.sshDelete, id),
importCandidates: () => ipcRenderer.invoke(IPC.sshImport)
},
+ sshProject: {
+ connect: (projectId, conn) => ipcRenderer.invoke(IPC.sshConnectProject, projectId, conn),
+ disconnect: (projectId) => ipcRenderer.invoke(IPC.sshDisconnectProject, projectId),
+ listDir: (projectId, dir) => ipcRenderer.invoke(IPC.sshListDir, projectId, dir),
+ onStatus: (cb) => {
+ const h = (_e: unknown, e: unknown) => cb(e as never)
+ ipcRenderer.on(IPC.sshProjectStatus, h)
+ return () => ipcRenderer.removeListener(IPC.sshProjectStatus, h)
+ }
+ },
git: {
status: (cwd) => ipcRenderer.invoke(IPC.gitStatus, cwd),
init: (cwd) => ipcRenderer.invoke(IPC.gitInit, cwd),
diff --git a/src/shared/ipc.ts b/src/shared/ipc.ts
index e1b3ad39a..5ad957ab9 100644
--- a/src/shared/ipc.ts
+++ b/src/shared/ipc.ts
@@ -64,6 +64,10 @@ export const IPC = {
sshSave: 'ssh:save',
sshDelete: 'ssh:delete',
sshImport: 'ssh:import-candidates',
+ sshConnectProject: 'ssh:connect-project',
+ sshDisconnectProject: 'ssh:disconnect-project',
+ sshListDir: 'ssh:list-dir',
+ sshProjectStatus: 'ssh-project:status',
gitStatus: 'git:status',
gitInit: 'git:init',
gitClone: 'git:clone',
diff --git a/src/shared/types.ts b/src/shared/types.ts
index 8a568c376..1ed509136 100644
--- a/src/shared/types.ts
+++ b/src/shared/types.ts
@@ -736,8 +736,7 @@ export interface NodeTerminalApi {
dialog: DialogApi
settings: SettingsApi
ssh: SshApi
- /** Optional until the preload wiring task lands its implementation (SSH Projects Phase 1). */
- sshProject?: SshProjectApi
+ sshProject: SshProjectApi
git: GitApi
clipboard: ClipboardApi
shell: ShellApi