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(workspace): poll connected SSH projects so mobile-started sessions reach the canvas

enesa20ef3430238
diff --git a/src/core/workspace-store.test.ts b/src/core/workspace-store.test.ts
index 5942f45dd..3831fa5bc 100644
--- a/src/core/workspace-store.test.ts
+++ b/src/core/workspace-store.test.ts
@@ -604,6 +604,60 @@ describe('ssh lineage safety', () => {
expect(files['~/app']).toContain('"id": "fresh1"')
})
+ // The mobile companion appends its new sessions to the server's project.json while the desktop
+ // is running. The desktop polls the file (refreshSshProject with pushIfStanding:false) so those
+ // nodes land on the live canvas — the poll must be read-only when our cache stands, or every
+ // tick would spam a mirror write.
+ it('poll: adopts a same-lineage remote that grew a node, and never writes when nothing is owed', async () => {
+ const { files, io } = cwdIO()
+ const writes: string[] = []
+ const origWrite = io.write.bind(io)
+ io.write = async (id: string, ssh: any, c: string) => (writes.push(id), origWrite(id, ssh, c))
+ const store = new WorkspaceStore(io)
+ await store.save(ws([project({ id: 'ps', ssh: sshConn, cwd: undefined })])) // reconcile + push rev 1
+ const writesAfterSave = writes.length
+
+ // The phone appends a node and bumps rev (same project id = same lineage).
+ const f = JSON.parse(files['~/app'])
+ f.rev = 2
+ f.nodes = [...f.nodes, { id: 'term-mobile-1', kind: 'terminal', position: { x: 0, y: 700 }, size: { width: 1, height: 1 }, title: 'Mobile', color: '#fff', group: null }]
+ files['~/app'] = JSON.stringify(f)
+
+ const adopted = await store.refreshSshProject('ps', { pushIfStanding: false })
+ expect(adopted?.nodes.map((n) => n.id)).toContain('term-mobile-1')
+ expect(writes.length).toBe(writesAfterSave) // read-only poll
+
+ // Unchanged remote → the next poll decides nothing and writes nothing.
+ expect(await store.refreshSshProject('ps', { pushIfStanding: false })).toBeNull()
+ expect(writes.length).toBe(writesAfterSave)
+ })
+
+ it('poll: still retries a mirror write that is owed (pushIfStanding:false does not cancel debts)', async () => {
+ const files: Record<string, string> = {}
+ let up = false
+ const io = {
+ read: async (_id: string, ssh: any) => {
+ if (!up) return { status: 'error' as const }
+ return files[ssh.remoteCwd] != null
+ ? { status: 'ok' as const, content: files[ssh.remoteCwd] }
+ : { status: 'absent' as const }
+ },
+ write: async (_id: string, ssh: any, c: string) => (up ? ((files[ssh.remoteCwd] = c), true) : false)
+ }
+ const store = new WorkspaceStore(io)
+ await store.save(ws([project({ id: 'ps', ssh: sshConn, cwd: undefined })])) // down: owed
+ up = true
+ expect(await store.refreshSshProject('ps', { pushIfStanding: false })).toBeNull()
+ expect(files['~/app']).toContain('"id": "ps"') // the owed mirror landed on the poll
+ })
+
+ it('sshProjectIds lists the ssh entries (the poll iterates these)', async () => {
+ const { io } = cwdIO()
+ const store = new WorkspaceStore(io)
+ await store.save(ws([project({ id: 'ps', ssh: sshConn, cwd: undefined }), project({ id: 'p2', name: 'inline' })]))
+ expect(store.sshProjectIds()).toEqual(['ps'])
+ })
+
it('same-lineage reconcile is untouched: an emptier remote with a higher rev still wins (user cleared their canvas)', async () => {
const { files, io } = cwdIO()
const store = new WorkspaceStore(io)
diff --git a/src/core/workspace-store.ts b/src/core/workspace-store.ts
index 9ac27b65d..140e2875c 100644
--- a/src/core/workspace-store.ts
+++ b/src/core/workspace-store.ts
@@ -334,19 +334,31 @@ export class WorkspaceStore {
}
/**
- * Called when an SSH project's connection comes up. Reconciles the server's
- * .nodeterm/project.json with our cached copy (see reconcileSsh): remote won →
- * adopt (returned; caller broadcasts it); otherwise our cache pushed up.
+ * Reconciles the server's .nodeterm/project.json with our cached copy (see reconcileSsh):
+ * remote won → adopt (returned; caller broadcasts it); otherwise our cache pushed up.
+ * Called on connect, and periodically while connected (the POLL — how a session the mobile
+ * companion appended to the server file reaches the live canvas). The poll passes
+ * `pushIfStanding: false`: when our cache simply stands, a poll must be read-only — only an
+ * OWED mirror (a previously dropped write) may still push.
*/
- async refreshSshProject(projectId: string): Promise<Project | null> {
+ async refreshSshProject(projectId: string, opts?: { pushIfStanding?: boolean }): Promise<Project | null> {
const e = this.index?.entries.find((x) => x.id === projectId && x.ssh)
if (!e?.ssh || !this.remoteIO) return null
- const adopted = await this.reconcileSsh(e)
- // The reconcile may have moved cache/rev/name — persist the index either way (cheap, atomic).
- await writeAtomic(this.indexPath, JSON.stringify(this.index))
+ const revBefore = e.cache?.rev
+ const adopted = await this.reconcileSsh(e, opts?.pushIfStanding ?? true)
+ // Persist the index only when the reconcile moved something — a quiet poll must not churn
+ // workspace.json every tick.
+ if (adopted || e.cache?.rev !== revBefore) {
+ await writeAtomic(this.indexPath, JSON.stringify(this.index))
+ }
return adopted
}
+ /** The ssh entry ids of the current index — what the connected-project poll iterates. */
+ sshProjectIds(): string[] {
+ return (this.index?.entries ?? []).filter((e) => e.ssh).map((e) => e.id)
+ }
+
/** A throttled trailing mirror write was acked but later dropped (connection died inside the
* throttle window): re-owe it so the next save retries. Wired from makeRemoteWorkspaceIO. */
markUnmirrored(projectId: string): void {
@@ -367,7 +379,7 @@ export class WorkspaceStore {
* so the terminals reattach); a push outbids the losing lineage's rev so it stays beaten.
* Returns the adopted project (for the caller to surface) or null when our cache stood/pushed.
*/
- private async reconcileSsh(e: IndexEntryV3): Promise<Project | null> {
+ private async reconcileSsh(e: IndexEntryV3, pushIfStanding = true): Promise<Project | null> {
if (!e.ssh || !this.remoteIO) return null
const res = await this.remoteIO.read(e.id, e.ssh)
if (res.status === 'error') {
@@ -400,7 +412,7 @@ export class WorkspaceStore {
this.unmirrored.delete(e.id) // the server copy IS the truth now — nothing owed
return fileToProject(adopted, { ssh: e.ssh, closed: e.closed, localExec: e.localExec })
}
- if (e.cache) {
+ if (e.cache && (pushIfStanding || this.unmirrored.has(e.id))) {
// Our cache stood. If it just beat a FOREIGN lineage on the merits (not on rev), outbid that
// lineage's rev so a later rev-only reconcile can't resurrect the losing side.
if (remote && !sameLineage && remote.rev >= e.cache.rev) {
diff --git a/src/main/index.ts b/src/main/index.ts
index 9fc464186..a4f715e0a 100644
--- a/src/main/index.ts
+++ b/src/main/index.ts
@@ -1110,6 +1110,28 @@ app.whenReady().then(async () => {
if (adopted) sendToMain(IPC.workspaceExternalChange, adopted)
})
})
+ // While connected, poll each SSH project's server file: the mobile companion appends the
+ // sessions it starts to <remoteCwd>/.nodeterm/project.json, and this is how those nodes reach
+ // the live canvas without a reconnect. Read-only unless a mirror write is owed
+ // (pushIfStanding:false), one `cat` per project per tick over the ControlMaster. The in-flight
+ // set keeps a hung read from stacking a second poll on the same project.
+ {
+ const REMOTE_WORKSPACE_POLL_MS = 15_000
+ const inFlight = new Set<string>()
+ setInterval(() => {
+ for (const projectId of workspaceStore.sshProjectIds()) {
+ if (inFlight.has(projectId) || !sshProjectManager?.refForProject(projectId)) continue
+ inFlight.add(projectId)
+ void workspaceStore
+ .refreshSshProject(projectId, { pushIfStanding: false })
+ .then((adopted) => {
+ if (adopted) sendToMain(IPC.workspaceExternalChange, adopted)
+ })
+ .catch(() => { /* fail-open: the next tick retries */ })
+ .finally(() => inFlight.delete(projectId))
+ }
+ }, REMOTE_WORKSPACE_POLL_MS)
+ }
// Route git-service + commit-message git ops over the active SSH project's master only — and only
// for that project's exact remoteCwd. Any other cwd (a local project, or a different connected
// project) resolves to undefined, so the local path stays byte-identical.