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(workspace): ssh project file always lands on the server (retry owed mirror writes)

enes8b856ecc2468
diff --git a/src/core/workspace-store.test.ts b/src/core/workspace-store.test.ts
index 1b2818189..fcba6ffe4 100644
--- a/src/core/workspace-store.test.ts
+++ b/src/core/workspace-store.test.ts
@@ -297,3 +297,67 @@ describe('refreshSshProject', () => {
expect(remote['ps']).toContain('"id": "ps"')
})
})
+
+describe('ssh mirror guarantee (unmirrored retry)', () => {
+ const sshConn = { server: { host: 'h', user: 'u' } as any, remoteCwd: '~/app' }
+
+ /** Fake IO whose write succeeds only while `up` is true (simulates the
+ * ControlMaster still connecting during the first save). */
+ const flakyIO = () => {
+ const state = { up: false, writes: 0, remote: {} as Record<string, string> }
+ const io = {
+ read: async (id: string) => state.remote[id] ?? null,
+ write: async (id: string, _s: any, c: string) => {
+ state.writes++
+ if (!state.up) return false
+ state.remote[id] = c
+ return true
+ }
+ }
+ return { state, io }
+ }
+
+ it('retries a dropped mirror write on the next save even with unchanged content', async () => {
+ const { state, io } = flakyIO()
+ const store = new WorkspaceStore(io)
+ const w = ws([project({ id: 'ps', ssh: sshConn, cwd: undefined })])
+
+ await store.save(w) // connection not up yet → write dropped
+ expect(state.writes).toBe(1)
+ expect(state.remote['ps']).toBeUndefined()
+
+ state.up = true
+ await store.save(w) // content unchanged, but the mirror is still owed
+ expect(state.writes).toBe(2)
+ expect(state.remote['ps']).toContain('"id": "ps"')
+ expect(JSON.parse(state.remote['ps']).rev).toBe(1) // retry does not bump rev
+
+ await store.save(w) // confirmed → unchanged saves stop writing
+ expect(state.writes).toBe(2)
+ })
+
+ it('refreshSshProject records a failed push-up so the next save retries', async () => {
+ const { state, io } = flakyIO()
+ const store = new WorkspaceStore(io)
+ const w = ws([project({ id: 'ps', ssh: sshConn, cwd: undefined })])
+ await store.save(w) // dropped (down)
+
+ expect(await store.refreshSshProject('ps')).toBeNull() // still down: read null, push-up fails
+ state.up = true
+ await store.save(w) // unchanged content, retried because refresh confirmed it is owed
+ expect(state.remote['ps']).toContain('"id": "ps"')
+ })
+
+ it('a successful refresh push-up clears the debt (no redundant write on next save)', async () => {
+ const { state, io } = flakyIO()
+ const store = new WorkspaceStore(io)
+ const w = ws([project({ id: 'ps', ssh: sshConn, cwd: undefined })])
+ await store.save(w) // dropped
+ state.up = true
+ expect(await store.refreshSshProject('ps')).toBeNull() // push-up succeeds
+ expect(state.remote['ps']).toContain('"id": "ps"')
+ const writesAfterRefresh = state.writes
+ await store.save(w) // unchanged + already mirrored → no write
+ expect(state.writes).toBe(writesAfterRefresh)
+ })
+})
diff --git a/src/core/workspace-store.ts b/src/core/workspace-store.ts
index 5042ca0d7..22fc5d581 100644
--- a/src/core/workspace-store.ts
+++ b/src/core/workspace-store.ts
@@ -39,6 +39,10 @@ export class WorkspaceStore {
private revs = new Map<string, number>()
/** Raw v2 file content, kept until the first save backs it up (migration). */
private pendingV2Backup: string | null = null
+ /** ssh project ids whose last mirror write was dropped (connection down). Retried on every
+ * save/connect until a write confirms — guarantees the server file lands regardless of node
+ * type or creation timing. Runtime-only, never persisted. */
+ private unmirrored = new Set<string>()
/** Last index written/loaded — lets readLocalRef/refresh resolve entries without a full load. */
private index: WorkspaceIndexV3 | null = null
/** Optional hook fired after every load()/save() — the watcher re-syncs its watch set (Task 5). */
@@ -187,8 +191,13 @@ export class WorkspaceStore {
)
e.cache.rev = changedSinceLoad ? prevRev + 1 : prevRev
this.revs.set(e.id, e.cache.rev)
- if (this.remoteIO && changedSinceLoad) {
- void this.remoteIO.write(e.id, e.ssh, serializeProjectFile(e.cache))
+ // Mirror on change, and re-mirror while a previous write is still owed (the first save
+ // often races the ControlMaster coming up — its write is dropped fail-open, and without
+ // the retry nothing rewrites until the next real content change).
+ if (this.remoteIO && (changedSinceLoad || this.unmirrored.has(e.id))) {
+ const ok = await this.remoteIO.write(e.id, e.ssh, serializeProjectFile(e.cache))
+ if (ok) this.unmirrored.delete(e.id)
+ else this.unmirrored.add(e.id)
}
}
@@ -260,10 +269,17 @@ export class WorkspaceStore {
if (remote && remote.rev > cacheRev) {
e.cache = remote
this.revs.set(projectId, remote.rev)
+ this.unmirrored.delete(projectId) // the server copy IS the truth now — nothing owed
await writeAtomic(this.indexPath, JSON.stringify(this.index))
return fileToProject(remote, { ssh: e.ssh, closed: e.closed })
}
- if (e.cache) void this.remoteIO.write(projectId, e.ssh, serializeProjectFile(e.cache))
+ if (e.cache) {
+ // Push-up runs with the master just up, but record the outcome anyway: a failed write
+ // (connection flapped) stays owed so the next save retries it.
+ const ok = await this.remoteIO.write(projectId, e.ssh, serializeProjectFile(e.cache))
+ if (ok) this.unmirrored.delete(projectId)
+ else this.unmirrored.add(projectId)
+ }
return null
}
}