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): handedOffTo guards every remote write; keep sshProjectIds as identity

review2719d0a845fa
diff --git a/src/core/workspace-store.kanban.test.ts b/src/core/workspace-store.kanban.test.ts
index 1f115a78a..36a608f56 100644
--- a/src/core/workspace-store.kanban.test.ts
+++ b/src/core/workspace-store.kanban.test.ts
@@ -265,6 +265,32 @@ describe('an SSH project (the case the phone can never write itself)', () => {
const entry = index.entries.find((x: { id: string }) => x.id === 'ssh1')
expect(entry.cache.kanban.columns).toHaveLength(3)
})
+
+ // "Share with team": once the project is handed to a hosted team, the server core is the only
+ // writer of its project.json. A phone board verb must be refused here, before the transform, so
+ // the local cache never diverges and the renderer is never told about a change that will not land.
+ it('refuses every board verb on a project handed to a hosted team: no read, no write, no broadcast', async () => {
+ const io = fakeRemote()
+ let reads = 0
+ const store = new WorkspaceStore({ ...io, read: async (id: string, ssh: never) => { reads++; return io.read(id, ssh) } } as never)
+ const handedOffTo = { hostId: 'h1', projectId: 'project-9', at: 1 }
+ await store.save(ws([
+ { ...sshProject({ columns: [{ id: 'kcol-a', title: 'To Do', color: '#0a84ff' }], assignments: [] }), handedOffTo },
+ // A board-less one, so `ensureRemoteBoard` would have something to seed.
+ project({ id: 'ssh2', cwd: undefined, ssh: { ...SSH!, remoteCwd: '~/y' }, handedOffTo })
+ ]))
+ const indexBefore = await fs.readFile(path.join(userData, 'workspace.json'), 'utf-8')
+ fake.sent.length = 0
+
+ expect(await store.setRemoteCardColumn('ssh1', 'term-1', 'kcol-a')).toBe(false)
+ expect(await store.editRemoteCardLabels('ssh1', 'term-1', { create: [{ name: 'ui', color: 'blue' }] })).toBeNull()
+ expect(await store.ensureRemoteBoard('ssh2')).toBeNull()
+
+ expect(reads).toBe(0)
+ expect(io.writes).toBe(0)
+ expect(externalChanges()).toEqual([])
+ expect(await fs.readFile(path.join(userData, 'workspace.json'), 'utf-8')).toBe(indexBefore)
+ })
})
describe('editRemoteCardLabels (the phone labelling a session)', () => {
diff --git a/src/core/workspace-store.settings.test.ts b/src/core/workspace-store.settings.test.ts
index a3d35ab04..3e8257e6f 100644
--- a/src/core/workspace-store.settings.test.ts
+++ b/src/core/workspace-store.settings.test.ts
@@ -257,6 +257,31 @@ describe('project settings — ssh leg', () => {
expect(remoteFs.get('/srv/app/.nodeterm/settings.json')).toContain('offline-edit')
})
+ // "Share with team": after the handover the server core owns the host's `.nodeterm/`, so this
+ // desktop keeps its edit in the cache and never pushes it — neither on a write nor as a "heal" of
+ // a host file that is absent or older on a read.
+ it('a project handed to a hosted team never has its settings pushed to the host', async () => {
+ let pushes = 0
+ const store = new WorkspaceStore({
+ ...fakeIO,
+ writeSettings: async (id: string, ssh: { remoteCwd: string }, content: string) => {
+ pushes++
+ return fakeIO.writeSettings(id, ssh, content)
+ }
+ })
+ await store.save(ws([{ ...sshProject(), handedOffTo: { hostId: 'h1', projectId: 'project-9', at: 1 } }]))
+ expect(await store.writeProjectSettings('ps1', { setup: { setupScript: 'kept-local' } })).toBe(true)
+ // The host file is absent and then older than the cache: both would be healed by a push.
+ await store.readProjectSettings('ps1')
+ remoteFs.set('/srv/app/.nodeterm/settings.json', JSON.stringify(
+ { version: 1, rev: 0, savedAt: 't', setup: { setupScript: 'server-owned' } }))
+ await store.readProjectSettings('ps1')
+ expect(pushes).toBe(0)
+ expect(remoteFs.get('/srv/app/.nodeterm/settings.json')).toContain('server-owned')
+ const idx = JSON.parse(await fs.readFile(path.join(userData, 'workspace.json'), 'utf-8'))
+ expect(idx.entries[0].settingsCache.setup.setupScript).toBe('kept-local')
+ })
+
it('a conflict-marked remote file is reported as such and never overwritten', async () => {
const store = new WorkspaceStore(fakeIO)
await store.save(ws([sshProject()]))
diff --git a/src/core/workspace-store.test.ts b/src/core/workspace-store.test.ts
index a1cf511e0..47f8e78bf 100644
--- a/src/core/workspace-store.test.ts
+++ b/src/core/workspace-store.test.ts
@@ -945,7 +945,7 @@ describe('handedOffTo — a project shared to a hosted team is never written aga
expect(state.writes).toBe(0)
})
- it('sshProjectIds() leaves it out of the connected-project poll and still lists the others', async () => {
+ it('pollableSshProjectIds() leaves it out of the poll; sshProjectIds() still names it as remote', async () => {
const { io } = countingIO()
const store = new WorkspaceStore(io)
await store.save(ws([
@@ -953,7 +953,10 @@ describe('handedOffTo — a project shared to a hosted team is never written aga
project({ id: 'other', ssh: { ...sshConn, remoteCwd: '~/other' }, cwd: undefined }),
project({ id: 'local', cwd: projRoot })
]))
- expect(store.sshProjectIds()).toEqual(['other'])
+ expect(store.pollableSshProjectIds()).toEqual(['other'])
+ // IDENTITY, not liveness: a handed-off project still runs on someone else's machine, and the
+ // session-memory / dev-ports scope checks read this list to know it.
+ expect(store.sshProjectIds()).toEqual(['ps', 'other'])
})
it('survives a restart: load() returns it, and the reloaded store still never touches the file', async () => {
@@ -966,7 +969,7 @@ describe('handedOffTo — a project shared to a hosted team is never written aga
const loaded = await restarted.load()
expect(loaded.projects.find((p) => p.id === 'ps')!.handedOffTo).toEqual(handed)
expect(loaded.projects.find((p) => p.id === 'other')!.handedOffTo).toBeUndefined()
- expect(restarted.sshProjectIds()).toEqual(['other'])
+ expect(restarted.pollableSshProjectIds()).toEqual(['other'])
expect(await restarted.refreshSshProject('ps')).toBeNull()
await restarted.save(loaded)
// The other SSH project is reconciled and mirrored as usual; the handed-off one never is.
@@ -975,6 +978,22 @@ describe('handedOffTo — a project shared to a hosted team is never written aga
expect(state.writeIds).not.toContain('ps')
})
+ // Every remote write funnels through `mirrorSshCache` or `reconcileSsh`; both refuse a handed-off
+ // entry on their own, so a future caller that forgets its own guard still cannot write the file.
+ it('mirrorSshCache itself refuses a handed-off entry, whoever calls it', async () => {
+ const { state, io } = countingIO()
+ const store = new WorkspaceStore(io)
+ await store.save(ws([project({ id: 'ps', ssh: sshConn, cwd: undefined, handedOffTo: handed })]))
+ const internals = store as unknown as {
+ index: { entries: { id: string }[] }
+ mirrorSshCache(e: unknown): Promise<void>
+ }
+ const entry = internals.index.entries.find((x) => x.id === 'ps')
+ await internals.mirrorSshCache(entry)
+ expect(state.reads).toBe(0)
+ expect(state.writes).toBe(0)
+ })
+
it('an unreadable (cache-less) handed-off entry keeps the record through its placeholder', async () => {
await fs.writeFile(path.join(userData, 'workspace.json'), JSON.stringify({
version: 3, activeProjectId: 'ps',
@@ -1001,7 +1020,7 @@ describe('handedOffTo — a project shared to a hosted team is never written aga
expect(loaded.projects.find((p) => p.id === 'ps')!.handedOffTo).toEqual({ at: 5 })
// No timestamp: not a record at all, so the project is an ordinary SSH project again.
expect('handedOffTo' in loaded.projects.find((p) => p.id === 'junk')!).toBe(false)
- expect(store.sshProjectIds()).toEqual(['junk'])
+ expect(store.pollableSshProjectIds()).toEqual(['junk'])
})
})
diff --git a/src/core/workspace-store.ts b/src/core/workspace-store.ts
index 8a666e8a3..a5ad186ca 100644
--- a/src/core/workspace-store.ts
+++ b/src/core/workspace-store.ts
@@ -889,6 +889,9 @@ export class WorkspaceStore {
ssh: NonNullable<Project['ssh']>,
file: ProjectSettingsFileV1
): Promise<boolean> {
+ // Handed to a hosted team: the server core owns the host's `.nodeterm/` now. The cache keeps
+ // this machine's copy; nothing is pushed over the server's.
+ if (this.index?.entries.find((x) => x.id === projectId)?.handedOffTo) return false
const io = this.remoteIO
if (!io?.writeSettings) return false
try {
@@ -1517,6 +1520,7 @@ export class WorkspaceStore {
for (const e of index.entries) {
if (!e.ssh || !e.cache) continue
if (e.handedOffTo) continue // handed to a hosted team: the server core is the only writer now
+ // (`markUnmirrored` may still add a handed-off id; every consumer of that debt refuses it.)
const prevRev = this.revs.get(e.id) ?? 0
const previousCache = this.index?.entries.find((old) => old.id === e.id && old.cache)?.cache
const changedSinceLoad = !(previousCache && sameProjectContent(previousCache, e.cache))
@@ -1899,9 +1903,17 @@ export class WorkspaceStore {
return adopted
}
- /** The ssh entry ids of the current index — what the connected-project poll iterates — except
- * one handed to a hosted team (never polled again). */
+ /** The ssh entry ids of the current index: the IDENTITY list ("this project runs on someone
+ * else's machine", connected or not, handed off or not), which the session-memory and dev-ports
+ * scope checks read. */
sshProjectIds(): string[] {
+ return (this.index?.entries ?? []).filter((e) => e.ssh).map((e) => e.id)
+ }
+
+ /** The ssh entry ids this desktop still keeps in sync — what the connected-project poll and the
+ * agent-status push iterate. Leaves out a project handed to a hosted team: the server core on its
+ * host owns its file and sessions now, so it is never polled again. */
+ pollableSshProjectIds(): string[] {
return (this.index?.entries ?? []).filter((e) => e.ssh && !e.handedOffTo).map((e) => e.id)
}
@@ -2610,6 +2622,10 @@ export class WorkspaceStore {
if (!e) return null
if (e.ssh && e.cache) {
+ // Handed to a hosted team: the server core is the only writer of this file now. Refused
+ // BEFORE the transform, so the local cache never diverges and the renderer is never told
+ // about a change that will not land.
+ if (e.handedOffTo) return null
const updated = transform(serializeProjectFile(e.cache))
if (updated === null) return { file: e.cache, written: false }
let parsed: ProjectFileV1
@@ -2703,6 +2719,9 @@ export class WorkspaceStore {
* session — the canvas node is gone on both machines while the tmux session keeps running.
*/
private async mirrorSshCache(e: IndexEntryV3): Promise<void> {
+ // Handed to a hosted team: the server core is the only writer now. Together with
+ // `reconcileSsh`'s guard this covers every `remoteIO.write`, whatever the caller checked.
+ if (e.handedOffTo) return
if (!e.ssh || !e.cache || !this.remoteIO) return
const rescued = await this.rescueRemoteNodes(e)
// AFTER the rescue: it replaces e.cache with the merged copy, which is what must land.
diff --git a/src/main/handed-off-wiring.test.ts b/src/main/handed-off-wiring.test.ts
new file mode 100644
index 000000000..245990f67
--- /dev/null
+++ b/src/main/handed-off-wiring.test.ts
@@ -0,0 +1,30 @@
+import { describe, it, expect } from 'vitest'
+import { readFileSync } from 'node:fs'
+
+/**
+ * SOURCE-LEVEL pins for which SSH-project list each desktop consumer reads. `sshProjectIds()` is
+ * the IDENTITY list ("this project is someone else's machine", connected or not), and
+ * `pollableSshProjectIds()` leaves out a project handed to a hosted team, whose file and sessions
+ * the server core on its host now owns. Both are `string[]`, so swapping one for the other compiles
+ * and passes every behavioral test of the consumer: a handed-off project would silently be polled
+ * again, or be scoped as this machine. The store's own behavior is pinned in
+ * `core/workspace-store.test.ts`.
+ */
+const main = readFileSync(new URL('./index.ts', import.meta.url), 'utf8').replace(/\r\n/g, '\n')
+
+describe('desktop main: identity list vs pollable list', () => {
+ it('the 15 s connected-project poll skips a handed-off project', () => {
+ expect(main).toMatch(/planRemoteWorkspacePoll\(\{\s*sshProjectIds: workspaceStore\.pollableSshProjectIds\(\),/)
+ })
+
+ it('the agent-status push to SSH hosts skips a handed-off project', () => {
+ const push = main.slice(main.indexOf('initRemoteStatusPush({'))
+ expect(push.slice(0, 300)).toContain('sshProjectIds: () => workspaceStore.pollableSshProjectIds(),')
+ })
+
+ it('the session-memory and dev-ports scope checks keep the identity list', () => {
+ const scopes = main.match(/sshScopePredicate\(\{\s*sshProjectIds: \(\) => workspaceStore\.\w+\(\)/g) ?? []
+ expect(scopes).toHaveLength(2)
+ for (const s of scopes) expect(s).toMatch(/workspaceStore\.sshProjectIds\(\)$/)
+ })
+})
diff --git a/src/main/index.ts b/src/main/index.ts
index 2e3238ea2..db77e0e1a 100644
--- a/src/main/index.ts
+++ b/src/main/index.ts
@@ -2550,7 +2550,9 @@ app.whenReady().then(async () => {
initRemoteStatusPush({
onFlush: onMirrorFlush,
flush: flushAgentStatusMirror,
- sshProjectIds: () => workspaceStore.sshProjectIds(),
+ // A project handed to a hosted team runs on the server core now: this desktop has no status
+ // to push for it.
+ sshProjectIds: () => workspaceStore.pollableSshProjectIds(),
nodeIdsFor: (projectId) => workspaceStore.sshProjectNodeIds(projectId),
push: (projectId, json) =>
sshProjectManager ? sshProjectManager.pushAgentStatus(projectId, json) : Promise.resolve(),
@@ -4897,7 +4899,7 @@ app.whenReady().then(async () => {
const mgr = sshProjectManager
if (!mgr) return
const plan = planRemoteWorkspacePoll({
- sshProjectIds: workspaceStore.sshProjectIds(),
+ sshProjectIds: workspaceStore.pollableSshProjectIds(),
hasLiveRef: (projectId) => !!mgr.refForProject(projectId),
busy: (projectId) => inFlight.has(projectId),
// Reuse-only gate: no socket file ⇒ no master to adopt ⇒ this project is left alone.