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(project-settings): local settings.json IO with corrupt-sideline + conflict detection

enesa908d22aca13
diff --git a/src/core/project-settings-files.test.ts b/src/core/project-settings-files.test.ts
new file mode 100644
index 000000000..3e9318c02
--- /dev/null
+++ b/src/core/project-settings-files.test.ts
@@ -0,0 +1,41 @@
+import { describe, it, expect, beforeEach, afterEach } from 'vitest'
+import { promises as fs } from 'fs'
+import os from 'os'
+import path from 'path'
+import { readProjectSettingsFile, writeProjectSettingsFile, projectSettingsPath } from './project-settings-files'
+
+let root: string
+beforeEach(async () => { root = await fs.mkdtemp(path.join(os.tmpdir(), 'nt-psf-')) })
+afterEach(async () => { await fs.rm(root, { recursive: true, force: true }) })
+
+describe('project settings file IO', () => {
+ it('reports absent when the file (or .nodeterm) does not exist', async () => {
+ expect((await readProjectSettingsFile(root)).status).toBe('absent')
+ })
+ it('write → read round-trips and bumps rev only on content change', async () => {
+ const v1 = await writeProjectSettingsFile(root, { terminal: { shell: '/bin/zsh' } }, null, 't1')
+ expect(v1.rev).toBe(1)
+ const same = await writeProjectSettingsFile(root, { terminal: { shell: '/bin/zsh' } }, v1, 't2')
+ expect(same.rev).toBe(1) // unchanged content: no rewrite
+ const v2 = await writeProjectSettingsFile(root, { terminal: { shell: '/bin/bash' } }, v1, 't3')
+ expect(v2.rev).toBe(2)
+ const read = await readProjectSettingsFile(root)
+ expect(read.status).toBe('ok')
+ if (read.status === 'ok') expect(read.file.terminal?.shell).toBe('/bin/bash')
+ })
+ it('sidelines an unparsable file to .corrupt-<ts> and reports invalid', async () => {
+ await fs.mkdir(path.join(root, '.nodeterm'), { recursive: true })
+ await fs.writeFile(projectSettingsPath(root), '{broken', 'utf-8')
+ expect((await readProjectSettingsFile(root)).status).toBe('invalid')
+ const names = await fs.readdir(path.join(root, '.nodeterm'))
+ expect(names.some((n) => n.startsWith('settings.json.corrupt-'))).toBe(true)
+ expect(names.includes('settings.json')).toBe(false)
+ })
+ it('leaves a git-conflict-marked file in place and reports conflict', async () => {
+ await fs.mkdir(path.join(root, '.nodeterm'), { recursive: true })
+ const raw = '<<<<<<< HEAD\n{}\n=======\n{}\n>>>>>>> theirs\n'
+ await fs.writeFile(projectSettingsPath(root), raw, 'utf-8')
+ expect((await readProjectSettingsFile(root)).status).toBe('conflict')
+ expect(await fs.readFile(projectSettingsPath(root), 'utf-8')).toBe(raw)
+ })
+})
diff --git a/src/core/project-settings-files.ts b/src/core/project-settings-files.ts
new file mode 100644
index 000000000..ae4f42e62
--- /dev/null
+++ b/src/core/project-settings-files.ts
@@ -0,0 +1,61 @@
+import { promises as fs } from 'fs'
+import path from 'path'
+import {
+ parseProjectSettingsFile,
+ sameProjectSettingsContent,
+ serializeProjectSettingsFile,
+ PROJECT_SETTINGS_FILE,
+ type ProjectSettingsDoc,
+ type ProjectSettingsFileV1
+} from '../shared/project-settings'
+import { PROJECT_DIR } from './workspace-files'
+import { writeAtomic } from './workspace-store'
+
+/**
+ * Local disk IO for <cwd>/.nodeterm/settings.json — the sibling of workspace-store's project.json
+ * handling, same trust boundary (the file is git-shared hostile input; see shared/project-settings.ts).
+ */
+export type ProjectSettingsRead =
+ | { status: 'ok'; file: ProjectSettingsFileV1 }
+ | { status: 'absent' }
+ | { status: 'conflict' } // left in place, user must resolve
+ | { status: 'invalid' } // sidelined to .corrupt-<ts> (only copy protected)
+ | { status: 'error' } // fs error other than ENOENT
+
+export function projectSettingsPath(cwd: string): string {
+ return path.join(cwd, PROJECT_DIR, PROJECT_SETTINGS_FILE)
+}
+
+export async function readProjectSettingsFile(cwd: string): Promise<ProjectSettingsRead> {
+ const file = projectSettingsPath(cwd)
+ let raw: string
+ try {
+ raw = await fs.readFile(file, 'utf-8')
+ } catch (e) {
+ return (e as NodeJS.ErrnoException).code === 'ENOENT' ? { status: 'absent' } : { status: 'error' }
+ }
+ const parsed = parseProjectSettingsFile(raw)
+ if (parsed.status === 'ok') return parsed
+ if (parsed.status === 'conflict') return { status: 'conflict' } // left in place — user must resolve
+ // invalid: sideline the only copy so a later write can't overwrite unrecoverable content.
+ try {
+ await fs.rename(file, `${file}.corrupt-${Date.now()}`)
+ } catch { /* best effort — never destroy data */ }
+ return { status: 'invalid' }
+}
+
+/** Whole-document write. Bumps rev over `prev`; skips the write (returns prev) when content is
+ * unchanged. Creates .nodeterm/ if missing. */
+export async function writeProjectSettingsFile(
+ cwd: string,
+ doc: ProjectSettingsDoc,
+ prev: ProjectSettingsFileV1 | null,
+ savedAt: string
+): Promise<ProjectSettingsFileV1> {
+ const candidate: ProjectSettingsFileV1 = { version: 1, rev: (prev?.rev ?? 0) + 1, savedAt, ...doc }
+ if (prev && sameProjectSettingsContent(prev, candidate)) return prev
+ const file = projectSettingsPath(cwd)
+ await fs.mkdir(path.dirname(file), { recursive: true })
+ await writeAtomic(file, serializeProjectSettingsFile(candidate))
+ return candidate
+}
diff --git a/src/core/workspace-store.ts b/src/core/workspace-store.ts
index 8f7971156..cba17a7c1 100644
--- a/src/core/workspace-store.ts
+++ b/src/core/workspace-store.ts
@@ -47,7 +47,7 @@ interface LoadedEntry {
}
let tmpSeq = 0
-async function writeAtomic(filePath: string, content: string): Promise<void> {
+export async function writeAtomic(filePath: string, content: string): Promise<void> {
// Unique per write: writers that bypass each other's queue (a second app instance, the SSH
// poll's index write) must never share a tmp file — interleaved writes into one shared tmp
// published spliced JSON under the atomic rename.