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.
TypeScript adhdagent-orchestrationai-agentscanvasclaude-codecodexcoding-agentsdeveloper-toolselectrongeminilinuxmacosmultiplexeropencodeparallel-agentsterminalterminal-emulatorterminal-multiplexertmuxworkspace-manager
fix(workspace-store): cache the raw project.json bytes for self-write detection
enes8fea467597b7
diff --git a/src/core/workspace-store.test.ts b/src/core/workspace-store.test.tsindex d6889a6cc..4dbe7af2d 100644--- a/src/core/workspace-store.test.ts+++ b/src/core/workspace-store.test.ts@@ -453,10 +453,48 @@ describe('localProjectCwds (the phone bridge jail roots)', () => {})})+// Field bug 2026-08-10: `lastWritten` was populated on the READ paths with a RE-SERIALIZATION of+// the parsed file, not with the bytes actually on disk. Any project.json whose formatting differs+// (a teammate's editor, a git checkout, a trailing newline) therefore never matched isSelfWrite —+// so EVERY fs event on it read as an external change, forever: spurious reloads and conflict bars.+describe('watcher self-write detection compares the RAW file bytes', () => {+ const reformat = async (file: string): Promise<string> => {+ // Semantically identical, different bytes — exactly what another writer leaves behind.+ const raw = (await fs.readFile(file, 'utf-8')) + '\n'+ await fs.writeFile(file, raw)+ return raw+ }++ it('load() records the bytes on disk, not a re-serialization of them', async () => {+ await new WorkspaceStore().save(ws([project({ cwd: projRoot })]))+ const file = path.join(projRoot, '.nodeterm/project.json')+ const raw = await reformat(file)++ const store = new WorkspaceStore()+ await store.load()+ expect(store.isSelfWrite(file, raw)).toBe(true)+ // A genuine outside edit still reads as one.+ expect(store.isSelfWrite(file, raw.replace('"name"', '"nAme"'))).toBe(false)+ })++ it('readLocalRef records the bytes on disk too (the watcher re-read path)', async () => {+ const store = new WorkspaceStore()+ await store.save(ws([project({ cwd: projRoot })]))+ const file = path.join(projRoot, '.nodeterm/project.json')+ const raw = await reformat(file)++ expect(await store.readLocalRefByPath(file)).toMatchObject({ id: 'p1' })+ // Editors and git touch a file several times: the follow-up events for the SAME bytes must+ // not each re-broadcast an "external change".+ expect(store.isSelfWrite(file, raw)).toBe(true)+ })+})+describe('appendRemoteNode (phone-registered sessions over the relay)', () => {- it('appends into a local ref project file as an OUTSIDE edit (watcher must see it)', async () => {+ it('appends into a local ref project file and broadcasts the change itself', async () => {const store = new WorkspaceStore()await store.save(ws([project({ cwd: projRoot })]))+ fake.sent.length = 0const ok = await store.appendRemoteNode('p1', { id: 'term-zz1-1', title: 'Mobile' })expect(ok).toBe(true)const file = path.join(projRoot, '.nodeterm/project.json')@@ -464,9 +502,15 @@ describe('appendRemoteNode (phone-registered sessions over the relay)', () => {const f = JSON.parse(raw)expect(f.rev).toBe(2)expect(f.nodes.map((n: { id: string }) => n.id)).toContain('term-zz1-1')- // NOT a self-write: the workspace watcher must treat this as an outside edit and broadcast- // it, so the renderer adopts the node onto the live canvas.- expect(store.isSelfWrite(file, raw)).toBe(false)+ // This write is OURS. It used to be left out of `lastWritten` on purpose, so the watcher would+ // fire and notify the renderer — a side channel that only worked while the watcher's byte+ // comparison happened to be reliable. The notification is now explicit, so the write is+ // recorded like every other one of ours.+ expect(store.isSelfWrite(file, raw)).toBe(true)+ const broadcast = fake.sent.filter((s) => s.channel === 'workspace:external-change')+ expect(broadcast).toHaveLength(1)+ expect(broadcast[0].args[0]).toMatchObject({ id: 'p1', cwd: projRoot })+ expect(broadcast[0].args[0].nodes.map((n: { id: string }) => n.id)).toContain('term-zz1-1')})it('refuses unknown / ssh / cwd-less projects and corrupt files (nothing written)', async () => {diff --git a/src/core/workspace-store.ts b/src/core/workspace-store.tsindex ad44b2b42..c336f5f77 100644--- a/src/core/workspace-store.ts+++ b/src/core/workspace-store.ts@@ -27,6 +27,13 @@ export interface RemoteWorkspaceIO {const projectFilePath = (cwd: string): string => path.join(cwd, PROJECT_DIR, PROJECT_FILE)+/** A parsed project file together with the exact bytes it was parsed from. `lastWritten` must+ * record `raw` — see the field it caches. */+interface ProjectFileRead {+ file: ProjectFileV1+ raw: string+}+let tmpSeq = 0async function writeAtomic(filePath: string, content: string): Promise<void> {// Unique per write: writers that bypass each other's queue (a second app instance, the SSH@@ -44,7 +51,11 @@ async function writeAtomic(filePath: string, content: string): Promise<void> {* v2-shaped Workspace.*/export class WorkspaceStore {- /** file path -> exact content we last wrote (skip-unchanged + watcher self-write suppression). */+ /** file path -> exact content of the file as we last WROTE or READ it (skip-unchanged + watcher+ * self-write suppression). Always the RAW bytes, never a re-serialization: a project.json whose+ * on-disk formatting differs from ours (a teammate's editor, a git checkout) would otherwise+ * never match isSelfWrite, so every fs event on it read as an external change forever — endless+ * spurious reloads and conflict bars (field bug 2026-08-10). */private lastWritten = new Map<string, string>()/** project id -> rev of the last written/loaded file. */private revs = new Map<string, number>()@@ -133,10 +144,11 @@ export class WorkspaceStore {const { kanban, ...rest } = e.projectprojects.push(validKanban(kanban) ? e.project : rest)} else if (e.cwd) {- const p = await this.readProjectFile(e.cwd, sideline)- if (p) {+ const read = await this.readProjectFile(e.cwd, sideline)+ if (read) {+ const p = read.filethis.revs.set(p.id, p.rev)- this.lastWritten.set(projectFilePath(e.cwd), serializeProjectFile(p))+ this.lastWritten.set(projectFilePath(e.cwd), read.raw)projects.push(fileToProject(p, { cwd: e.cwd, closed: e.closed, localExec: this.execOverlay(e, p) }))@@ -204,7 +216,7 @@ export class WorkspaceStore {* Edition — and the watcher's readLocalRef*) pass false: a probe must never mutate the disk, and a* git-conflict-marked project.json mid-merge must be left in place so the user can hand-resolve it.*/- private async readProjectFile(cwd: string, sideline: boolean): Promise<ProjectFileV1 | null> {+ private async readProjectFile(cwd: string, sideline: boolean): Promise<ProjectFileRead | null> {const file = projectFilePath(cwd)let raw: stringtry {@@ -214,7 +226,8 @@ export class WorkspaceStore {}try {const parsed = JSON.parse(raw) as ProjectFileV1- if (parsed?.version === 1 && typeof parsed.id === 'string' && Array.isArray(parsed.nodes)) return parsed+ // `raw` travels with the parse so callers can record the BYTES on disk in `lastWritten`.+ if (parsed?.version === 1 && typeof parsed.id === 'string' && Array.isArray(parsed.nodes)) return { file: parsed, raw }// parses but isn't a ProjectFileV1 — sideline it too, so a later save can't overwrite the only copy.} catch { /* not JSON — sideline below */ }if (sideline) {@@ -382,11 +395,11 @@ export class WorkspaceStore {}async probeFolder(folder: string): Promise<Project | null> {- const f = await this.readProjectFile(folder, false)+ const read = await this.readProjectFile(folder, false)// No `localExec`: this folder is being ADOPTED (its project.json may have been cloned from// anywhere), so its nodes come up with no custom shell and no extra ssh args — the safe// defaults. Only values this machine typed itself are ever restored (@shared/node-exec).- return f ? fileToProject(f, { cwd: folder }) : null+ return read ? fileToProject(read.file, { cwd: folder }) : null}localRefPaths(): string[] {@@ -400,11 +413,11 @@ export class WorkspaceStore {async readLocalRef(projectId: string): Promise<Project | null> {const e = this.index?.entries.find((x) => x.id === projectId && x.cwd)if (!e?.cwd) return null- const f = await this.readProjectFile(e.cwd, false)- if (!f) return null- this.revs.set(f.id, f.rev)- this.lastWritten.set(projectFilePath(e.cwd), serializeProjectFile(f))- return fileToProject(f, { cwd: e.cwd, closed: e.closed, localExec: e.localExec })+ const read = await this.readProjectFile(e.cwd, false)+ if (!read) return null+ this.revs.set(read.file.id, read.file.rev)+ this.lastWritten.set(projectFilePath(e.cwd), read.raw)+ return fileToProject(read.file, { cwd: e.cwd, closed: e.closed, localExec: e.localExec })}/** Maps a watched file path back to its project and re-reads it. */@@ -594,11 +607,15 @@ export class WorkspaceStore {/*** Registers a PHONE-STARTED session as a node in a LOCAL ref project's file — the host side of- * the relay `projects.registerNode` verb. Deliberately written as an OUTSIDE edit- * (`lastWritten` untouched): the workspace watcher then broadcasts the change and the renderer- * adopts the node onto the live canvas, exactly like a git pull would. v1 scope: local-cwd- * projects only (an ssh ref's file lives on another machine; the phone reaches that one over- * its own SSH path).+ * the relay `projects.registerNode` verb. v1 scope: local-cwd projects only (an ssh ref's file+ * lives on another machine; the phone reaches that one over its own SSH path).+ *+ * The renderer must adopt the node onto the live canvas, so the change IS announced — but+ * explicitly, via workspaceExternalChange, not by leaving `lastWritten` stale so the watcher+ * fires. That side channel only worked while every self-write matched byte-for-byte, and it made+ * an OUR-write indistinguishable from a teammate's; the store's own caches (getNode,+ * persistedCanvases) were left holding a file they knew was outdated. Record the write like any+ * other and send the notification ourselves.*/async appendRemoteNode(projectId: string, input: RemoteNodeInput, now = new Date()): Promise<boolean> {const e = this.index?.entries.find((x) => x.id === projectId && x.cwd)@@ -614,10 +631,21 @@ export class WorkspaceStore {if (updated === null) return falsetry {await writeAtomic(file, updated)- return true} catch {return false}+ this.lastWritten.set(file, updated)+ // appendProjectNode only returns a string it produced from a valid ProjectFileV1, so this parse+ // cannot realistically fail — but a throw here would turn a landed write into a `false`.+ try {+ const parsed = JSON.parse(updated) as ProjectFileV1+ this.revs.set(parsed.id, parsed.rev)+ platform().broadcast(+ IPC.workspaceExternalChange,+ fileToProject(parsed, { cwd: e.cwd, closed: e.closed, localExec: e.localExec })+ )+ } catch { /* the file is written and cached; the next load/poll surfaces the node */ }+ return true}/**