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(project-settings): read-before-write seeds rev + refuses conflicted settings.json

enes5c7d77713169
diff --git a/src/core/workspace-store.settings.test.ts b/src/core/workspace-store.settings.test.ts
index b074fb680..3756064ab 100644
--- a/src/core/workspace-store.settings.test.ts
+++ b/src/core/workspace-store.settings.test.ts
@@ -42,6 +42,33 @@ describe('project settings — local leg', () => {
expect(onDisk.rev).toBe(1)
})
+ it('a cold store reads before writing, so the rev sequence continues instead of restarting', async () => {
+ const store = new WorkspaceStore()
+ await store.save(ws([project({ cwd: projRoot })]))
+ await store.writeProjectSettings('p1', { terminal: { shell: '/bin/zsh' } }) // rev 1
+ const file = path.join(projRoot, '.nodeterm', 'settings.json')
+ expect(JSON.parse(await fs.readFile(file, 'utf-8')).rev).toBe(1)
+
+ // A fresh process: nothing has been read this run, so the write must look at the file first.
+ const store2 = new WorkspaceStore()
+ await store2.load()
+ expect(await store2.writeProjectSettings('p1', { terminal: { shell: '/bin/bash' } })).toBe(true)
+ const onDisk = JSON.parse(await fs.readFile(file, 'utf-8'))
+ expect(onDisk.rev).toBe(2)
+ expect(onDisk.terminal.shell).toBe('/bin/bash')
+ })
+
+ it('refuses to write over a git-conflicted settings.json, leaving the bytes untouched', async () => {
+ const store = new WorkspaceStore()
+ await store.save(ws([project({ cwd: projRoot })]))
+ const file = path.join(projRoot, '.nodeterm', 'settings.json')
+ const conflicted = '<<<<<<< a\n{}\n=======\n{}\n>>>>>>> b\n'
+ await fs.mkdir(path.dirname(file), { recursive: true })
+ await fs.writeFile(file, conflicted, 'utf-8')
+ expect(await store.writeProjectSettings('p1', { terminal: { shell: '/bin/zsh' } })).toBe(false)
+ expect(await fs.readFile(file, 'utf-8')).toBe(conflicted)
+ })
+
it('local overlay persists across store instances without a canvas save', async () => {
const store = new WorkspaceStore()
await store.save(ws([project({ cwd: projRoot })]))
diff --git a/src/core/workspace-store.ts b/src/core/workspace-store.ts
index a651d72cd..e22e0a146 100644
--- a/src/core/workspace-store.ts
+++ b/src/core/workspace-store.ts
@@ -480,15 +480,34 @@ export class WorkspaceStore {
* user's editor) may have changed since the caller read it, and silently merging into a file the
* caller never saw is how the shared half stops meaning what the repo says.
*
- * False = there is nowhere to write it: an unknown id, an inline canvas (no folder), an ssh
- * project (Task 7 gives it the remote leg), or the write itself failed.
+ * READS FIRST when this run has never read the file. Without that, a fresh process writes with
+ * `prev = null` — rev restarts at 1 over a file that was at rev N, and, worse, the write lands on
+ * whatever is there, including the git-conflict-marked file `readProjectSettings` deliberately
+ * refuses to parse. Same rule the project.json path already lives by: never write a file this
+ * store has not looked at.
+ *
+ * False = there is nowhere to write it, or writing would destroy something: an unknown id, an
+ * inline canvas (no folder), an ssh project (Task 7 gives it the remote leg), a conflicted file
+ * (the user's to resolve — untouched), an unreadable one (a failed read is never evidence of
+ * absence, so it may not be clobbered either), or a write that failed.
*/
async writeProjectSettings(projectId: string, doc: ProjectSettingsDoc): Promise<boolean> {
const e = this.index?.entries.find((x) => x.id === projectId)
if (!e || e.ssh || !e.cwd) return false
+ let prev = this.lastSharedSettings.get(projectId) ?? null
+ if (!prev) {
+ const read = await readProjectSettingsFile(e.cwd)
+ // absent → nothing to lose, start at rev 1. invalid → the read already sidelined the only
+ // copy to `.corrupt-<ts>`, so this write destroys nothing either.
+ if (read.status === 'conflict' || read.status === 'error') return false
+ if (read.status === 'ok') {
+ prev = read.file
+ this.lastSharedSettings.set(projectId, read.file)
+ }
+ }
try {
const written = await writeProjectSettingsFile(
- e.cwd, doc, this.lastSharedSettings.get(projectId) ?? null, new Date().toISOString()
+ e.cwd, doc, prev, new Date().toISOString()
)
this.lastSharedSettings.set(projectId, written)
return true