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): rev-field precedence + trust-store read-error safety

enes75f6d666fbc0
diff --git a/src/core/project-settings-files.test.ts b/src/core/project-settings-files.test.ts
index 3e9318c02..86890b63f 100644
--- a/src/core/project-settings-files.test.ts
+++ b/src/core/project-settings-files.test.ts
@@ -38,4 +38,17 @@ describe('project settings file IO', () => {
expect((await readProjectSettingsFile(root)).status).toBe('conflict')
expect(await fs.readFile(projectSettingsPath(root), 'utf-8')).toBe(raw)
})
+ it('bumps rev over prev even when the caller passes back the previously-read file as doc', async () => {
+ const v1 = await writeProjectSettingsFile(root, { terminal: { shell: '/bin/zsh' } }, null, 't1')
+ const read = await readProjectSettingsFile(root)
+ expect(read.status).toBe('ok')
+ if (read.status !== 'ok') return
+ // A caller may pass the READ FILE straight back as `doc` (it structurally satisfies
+ // ProjectSettingsDoc) — its stale version/rev/savedAt must not survive into the write.
+ const edited = { ...read.file, terminal: { shell: '/bin/fish' } }
+ const v2 = await writeProjectSettingsFile(root, edited, v1, 't2')
+ expect(v2.rev).toBe(v1.rev + 1)
+ expect(v2.savedAt).toBe('t2')
+ expect(v2.terminal?.shell).toBe('/bin/fish')
+ })
})
diff --git a/src/core/project-settings-files.ts b/src/core/project-settings-files.ts
index ae4f42e62..4c929bdb0 100644
--- a/src/core/project-settings-files.ts
+++ b/src/core/project-settings-files.ts
@@ -52,7 +52,10 @@ export async function writeProjectSettingsFile(
prev: ProjectSettingsFileV1 | null,
savedAt: string
): Promise<ProjectSettingsFileV1> {
- const candidate: ProjectSettingsFileV1 = { version: 1, rev: (prev?.rev ?? 0) + 1, savedAt, ...doc }
+ // `doc` spreads FIRST: a caller may pass back a previously-read ProjectSettingsFileV1 (it
+ // structurally satisfies ProjectSettingsDoc), and its stale version/rev/savedAt must never win
+ // over the bookkeeping this function computes — that would silently defeat rev monotonicity.
+ const candidate: ProjectSettingsFileV1 = { ...doc, version: 1, rev: (prev?.rev ?? 0) + 1, savedAt }
if (prev && sameProjectSettingsContent(prev, candidate)) return prev
const file = projectSettingsPath(cwd)
await fs.mkdir(path.dirname(file), { recursive: true })
diff --git a/src/core/project-trust-store.test.ts b/src/core/project-trust-store.test.ts
index 2cfc53e1b..567e5e6a5 100644
--- a/src/core/project-trust-store.test.ts
+++ b/src/core/project-trust-store.test.ts
@@ -1,5 +1,5 @@
-import { describe, it, expect, beforeEach, afterEach } from 'vitest'
-import { promises as fs } from 'fs'
+import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
+import { promises as fs, readFileSync } from 'fs'
import os from 'os'
import path from 'path'
import { initPlatform, resetPlatformForTests } from './platform'
@@ -59,4 +59,27 @@ describe('ProjectTrustStore', () => {
await store.record(key, 'setup', hashTrustContent('x'), 't')
expect(await new ProjectTrustStore().isTrusted(key, 'setup', hashTrustContent('x'))).toBe(true)
})
+ it('a non-ENOENT read failure fails closed WITHOUT clobbering the file, and heals once readable', async () => {
+ const key = localTrustKey('/proj')
+ const h = hashTrustContent('x')
+ const filePath = path.join(userData, 'project-trust.json')
+ // Seed an intact, persisted approval with a fresh store instance (its own uncorrupted cache).
+ await new ProjectTrustStore().record(key, 'setup', h, 't1')
+ const original = readFileSync(filePath, 'utf-8')
+
+ const store = new ProjectTrustStore() // fresh instance: cache is empty, forces a real read
+ const err = Object.assign(new Error('permission denied'), { code: 'EACCES' })
+ const spy = vi.spyOn(fs, 'readFile').mockRejectedValue(err)
+ try {
+ expect(await store.isTrusted(key, 'setup', h)).toBe(false)
+ await expect(store.record(key, 'setup', h, 't2')).rejects.toThrow()
+ // The failed record() must not have written anything — readFileSync bypasses the mocked
+ // promises API, so this reads the real on-disk bytes.
+ expect(readFileSync(filePath, 'utf-8')).toBe(original)
+ } finally {
+ spy.mockRestore()
+ }
+ // Once the fs is readable again, the original (never-clobbered) approval is still there.
+ expect(await store.isTrusted(key, 'setup', h)).toBe(true)
+ })
})
diff --git a/src/core/project-trust-store.ts b/src/core/project-trust-store.ts
index 08be8523c..b9fe208de 100644
--- a/src/core/project-trust-store.ts
+++ b/src/core/project-trust-store.ts
@@ -29,8 +29,12 @@ type TrustFileEntry = Partial<Record<ProjectTrustFamily, ProjectTrustRecord>>
type TrustFile = Record<string, TrustFileEntry>
/**
- * File: {userData}/project-trust.json. Lazy-loaded, cached; malformed file → empty store (fail
- * closed — an unreadable trust record must never be read back as "trusted").
+ * File: {userData}/project-trust.json. Lazy-loaded, cached; malformed (unparsable/wrong-shape)
+ * file → empty store (fail closed — an unreadable trust record must never be read back as
+ * "trusted"). A read ERROR (EACCES/EMFILE/EIO…) is different from ABSENT/malformed: it is not
+ * evidence the file is empty, so it is never cached and never healed by a write — `isTrusted` fails
+ * closed without caching the emptiness, and `record`/`revoke` reject rather than writeAtomic an
+ * empty store over the intact on-disk file.
*
* `record`/`revoke` serialize through an internal promise chain: both are read-modify-write over
* the same in-memory + on-disk store, and two overlapping calls racing the read half would let the
@@ -49,9 +53,16 @@ export class ProjectTrustStore {
let raw: string
try {
raw = await fs.readFile(this.filePath, 'utf-8')
- } catch {
- this.cache = {}
- return this.cache
+ } catch (e) {
+ if ((e as NodeJS.ErrnoException).code === 'ENOENT') {
+ this.cache = {}
+ return this.cache
+ }
+ // A non-ENOENT failure (EACCES/EMFILE/EIO…) is not evidence the file is absent — caching {}
+ // here would let the next mutate() writeAtomic an empty store over the intact on-disk file,
+ // silently destroying every recorded approval. Leave the cache unset so the next call
+ // retries the read, and let this failure propagate to the caller.
+ throw e
}
try {
const parsed: unknown = JSON.parse(raw)
@@ -63,7 +74,13 @@ export class ProjectTrustStore {
}
async isTrusted(key: string, family: ProjectTrustFamily, contentHash: string): Promise<boolean> {
- const store = await this.load()
+ let store: TrustFile
+ try {
+ store = await this.load()
+ } catch {
+ // Fail closed on a read error — never cached, so the next call retries the read.
+ return false
+ }
return store[key]?.[family]?.contentHash === contentHash
}