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): main ssh-store + ipc + preload bridge

enesa7d203f61153
diff --git a/src/main/index.ts b/src/main/index.ts
index 7648dd193..88d49dd9e 100644
--- a/src/main/index.ts
+++ b/src/main/index.ts
@@ -5,6 +5,7 @@ import * as fsOps from './fs-ops'
import { PtyManager } from './pty-manager'
import { WorkspaceStore } from './workspace-store'
import { SettingsStore } from './settings-store'
+import { SshStore } from './ssh-store'
import { GitService } from './git-service'
import { generateCommitMessage, generateTerminalName } from './commit-message'
import { initUpdater } from './updater'
@@ -35,6 +36,7 @@ const NT_MULTI = !app.isPackaged && !!process.env.NT_MULTI
if (NT_MULTI && process.env.NT_USER_DATA) app.setPath('userData', process.env.NT_USER_DATA)
const settingsStore = new SettingsStore()
+const sshStore = new SshStore()
const ptyManager = new PtyManager()
const workspaceStore = new WorkspaceStore()
const gitService = new GitService()
@@ -115,6 +117,7 @@ app.whenReady().then(async () => {
if (!gotSingleInstanceLock) return // losing second instance — quitting; don't touch tmux
settingsStore.init()
settingsStore.registerIpc()
+ sshStore.registerIpc()
ptyManager.init(() => settingsStore.get())
ptyManager.registerIpc()
workspaceStore.registerIpc()
diff --git a/src/main/ssh-store.test.ts b/src/main/ssh-store.test.ts
new file mode 100644
index 000000000..485da8b28
--- /dev/null
+++ b/src/main/ssh-store.test.ts
@@ -0,0 +1,39 @@
+import { describe, expect, it } from 'vitest'
+import { promises as fs } from 'node:fs'
+import os from 'node:os'
+import path from 'node:path'
+import { SshStore } from './ssh-store'
+
+async function tmpFile(): Promise<string> {
+ const dir = await fs.mkdtemp(path.join(os.tmpdir(), 'ssh-store-'))
+ return path.join(dir, 'ssh-servers.json')
+}
+
+describe('SshStore', () => {
+ it('is empty before any save', async () => {
+ const store = new SshStore(await tmpFile())
+ expect(store.list()).toEqual([])
+ })
+
+ it('save adds, save with same id updates, remove deletes', async () => {
+ const store = new SshStore(await tmpFile())
+ store.save({ id: 'a', label: 'A', host: 'h1', user: 'u1' })
+ expect(store.list().length).toBe(1)
+ store.save({ id: 'a', label: 'A2', host: 'h1', user: 'u1', port: 2200 })
+ expect(store.list().length).toBe(1)
+ expect(store.list()[0].label).toBe('A2')
+ expect(store.list()[0].port).toBe(2200)
+ store.remove('a')
+ expect(store.list()).toEqual([])
+ })
+
+ it('persists to disk and reloads', async () => {
+ const file = await tmpFile()
+ const s1 = new SshStore(file)
+ s1.save({ id: 'x', label: 'X', host: 'h', user: 'u' })
+ await new Promise((r) => setTimeout(r, 50)) // wait for the async flush
+ const s2 = new SshStore(file)
+ expect(s2.list().length).toBe(1)
+ expect(s2.list()[0].id).toBe('x')
+ })
+})
diff --git a/src/main/ssh-store.ts b/src/main/ssh-store.ts
new file mode 100644
index 000000000..57c83b46c
--- /dev/null
+++ b/src/main/ssh-store.ts
@@ -0,0 +1,65 @@
+import { promises as fs, readFileSync } from 'fs'
+import path from 'path'
+import { app, ipcMain } from 'electron'
+import { IPC } from '../shared/ipc'
+import type { SshServer } from '../shared/ssh'
+
+/**
+ * Stores saved SSH servers in ssh-servers.json. Keeps a synchronous cache so reads are
+ * immediate; writes are atomic (temp + rename). The file path is injectable for tests.
+ */
+export class SshStore {
+ private cache: SshServer[] = []
+ private readonly path: string
+ /** Serializes flushes so concurrent un-awaited saves can't race on the shared temp file. */
+ private writeChain: Promise<void> = Promise.resolve()
+
+ constructor(filePath?: string) {
+ this.path = filePath ?? path.join(app.getPath('userData'), 'ssh-servers.json')
+ try {
+ const raw = readFileSync(this.path, 'utf-8')
+ const parsed = JSON.parse(raw)
+ if (Array.isArray(parsed)) this.cache = parsed as SshServer[]
+ } catch {
+ this.cache = []
+ }
+ }
+
+ list(): SshServer[] {
+ return this.cache
+ }
+
+ save(server: SshServer): SshServer[] {
+ const i = this.cache.findIndex((s) => s.id === server.id)
+ if (i >= 0) this.cache[i] = server
+ else this.cache.push(server)
+ void this.flush()
+ return this.cache
+ }
+
+ remove(id: string): SshServer[] {
+ this.cache = this.cache.filter((s) => s.id !== id)
+ void this.flush()
+ return this.cache
+ }
+
+ private flush(): Promise<void> {
+ // Snapshot the cache now; chain after any in-flight write so the shared temp
+ // file is never written/renamed by two flushes at once.
+ const snapshot = JSON.stringify(this.cache, null, 2)
+ const tmp = `${this.path}.tmp`
+ this.writeChain = this.writeChain
+ .catch(() => {})
+ .then(async () => {
+ await fs.writeFile(tmp, snapshot, 'utf-8')
+ await fs.rename(tmp, this.path)
+ })
+ return this.writeChain
+ }
+
+ registerIpc(): void {
+ ipcMain.handle(IPC.sshList, () => this.list())
+ ipcMain.handle(IPC.sshSave, (_e, server: SshServer) => this.save(server))
+ ipcMain.handle(IPC.sshDelete, (_e, id: string) => this.remove(id))
+ }
+}
diff --git a/src/preload/index.ts b/src/preload/index.ts
index 1c016892d..36e8e6734 100644
--- a/src/preload/index.ts
+++ b/src/preload/index.ts
@@ -72,6 +72,11 @@ const api: NodeTerminalApi = {
load: () => ipcRenderer.invoke(IPC.settingsLoad),
save: (settings) => ipcRenderer.invoke(IPC.settingsSave, settings)
},
+ ssh: {
+ list: () => ipcRenderer.invoke(IPC.sshList),
+ save: (server) => ipcRenderer.invoke(IPC.sshSave, server),
+ remove: (id) => ipcRenderer.invoke(IPC.sshDelete, id)
+ },
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 0606391cc..4fccd1b85 100644
--- a/src/shared/ipc.ts
+++ b/src/shared/ipc.ts
@@ -56,6 +56,9 @@ export const IPC = {
fsWrite: 'fs:write',
settingsLoad: 'settings:load',
settingsSave: 'settings:save',
+ sshList: 'ssh:list',
+ sshSave: 'ssh:save',
+ sshDelete: 'ssh:delete',
gitStatus: 'git:status',
gitInit: 'git:init',
gitClone: 'git:clone',
diff --git a/src/shared/types.ts b/src/shared/types.ts
index a569025e1..531b769f8 100644
--- a/src/shared/types.ts
+++ b/src/shared/types.ts
@@ -268,6 +268,12 @@ export interface SettingsApi {
save(settings: Settings): Promise<void>
}
+export interface SshApi {
+ list(): Promise<import('./ssh').SshServer[]>
+ save(server: import('./ssh').SshServer): Promise<import('./ssh').SshServer[]>
+ remove(id: string): Promise<import('./ssh').SshServer[]>
+}
+
export interface GitFileChange {
path: string
/** Single-letter status: M (modified), A (added), D (deleted), R (renamed), U (untracked). */
@@ -640,6 +646,7 @@ export interface NodeTerminalApi {
workspace: WorkspaceApi
dialog: DialogApi
settings: SettingsApi
+ ssh: SshApi
git: GitApi
clipboard: ClipboardApi
shell: ShellApi