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): catch a same-size index rewrite on coarse-mtime filesystems by inode

enesb44dcc80392e
diff --git a/CLAUDE.md b/CLAUDE.md
index 7eb0adbf9..cb6fded57 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -419,7 +419,7 @@ Persistence has two layers:
- **Live terminal sessions** (tmux): terminals continue where they left off across node
remounts *and* full app restarts, including running processes. See below.
-**Autosave does no unchanged work** (`WorkspaceStore.save`): the parse of each `lastWritten` value is cached per raw string, and `workspace.json` is not rewritten when its bytes equal the store's last write AND the file still has the size+mtime that write left — so another instance's rewrite is still answered, a store's first save and a v2 migration always write, and every other index writer of ours clears the record.
+**Autosave does no unchanged work** (`WorkspaceStore.save`): the parse of each `lastWritten` value is cached per raw string, and `workspace.json` is not rewritten when its bytes equal the store's last write AND the file still has the size+mtime+inode that write left — so another instance's rewrite is still answered (the inode catches a same-size rewrite on a coarse-mtime filesystem: every writer publishes by rename), a store's first save and a v2 migration always write, and every other index writer of ours clears the record.
`settings.json` is a separate store (`core/settings-store.ts`, `state/settings.ts`).
diff --git a/src/core/workspace-store.test.ts b/src/core/workspace-store.test.ts
index 3f7dea3cc..761992334 100644
--- a/src/core/workspace-store.test.ts
+++ b/src/core/workspace-store.test.ts
@@ -1266,6 +1266,29 @@ describe('autosave skips unchanged work', () => {
expect((await readIndex()).entries.length).toBeGreaterThan(0)
})
+ // A coarse-mtime filesystem (whole seconds) cannot tell a same-size rewrite inside the same
+ // second from no write at all. Every writer publishes by rename, so the inode can.
+ it('rewrites after a same-size, same-mtime rewrite on a coarse-mtime filesystem (new inode)', async () => {
+ const realStat = fs.stat.bind(fs)
+ vi.spyOn(fs, 'stat').mockImplementation((async (p: string) => {
+ const st = await realStat(p)
+ return Object.assign(st, { mtimeMs: Math.floor(st.mtimeMs / 1000) * 1000 })
+ }) as typeof fs.stat)
+ const store = new WorkspaceStore()
+ await store.save(ws([project({ cwd: projRoot })]))
+ const ours = await realStat(indexPath())
+ // Another instance rewrites the SAME bytes via rename (new inode), same second.
+ const tmp = indexPath() + '.other'
+ await fs.writeFile(tmp, await fs.readFile(indexPath()))
+ await fs.utimes(tmp, ours.atime, ours.mtime)
+ await fs.rename(tmp, indexPath())
+ const theirs = await realStat(indexPath())
+ expect(theirs.ino).not.toBe(ours.ino)
+ expect(theirs.size).toBe(ours.size)
+ await store.save(ws([project({ cwd: projRoot })]))
+ expect((await realStat(indexPath())).ino).not.toBe(theirs.ino) // we wrote ours back
+ })
+
it('a changed index is still written', 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 0e3672884..37f89d76c 100644
--- a/src/core/workspace-store.ts
+++ b/src/core/workspace-store.ts
@@ -134,10 +134,12 @@ export class WorkspaceStore {
* its last write on EVERY autosave; re-parsing each file each time was pure waste. Keyed by the
* raw string, so any `lastWritten.set` elsewhere invalidates it by construction. */
private lastWrittenParsed = new Map<string, { raw: string; parsed: ProjectFileV1 }>()
- /** The index bytes we last wrote and the file's size/mtime right after, so an unchanged index is
- * not rewritten on every autosave — but one another writer changed on disk still is. Any other
- * index write of ours clears it, so it only ever describes the last write `save()` made. */
- private lastIndexWrite: { json: string; size: number; mtimeMs: number } | null = null
+ /** The index bytes we last wrote and the file's size/mtime/inode right after, so an unchanged
+ * index is not rewritten on every autosave — but one another writer changed on disk still is.
+ * The inode is what catches a same-size rewrite on a coarse-mtime filesystem: every writer
+ * publishes by rename, so any rewrite is a new inode. Any other index write of ours clears it, so
+ * it only ever describes the last write `save()` made. */
+ private lastIndexWrite: { json: string; size: number; mtimeMs: number; ino: number } | null = null
/** project id -> rev of the last written/loaded file. */
private revs = new Map<string, number>()
/** Entries whose one-time exec migration could NOT run (their project file was unreadable at load).
@@ -1145,20 +1147,27 @@ export class WorkspaceStore {
}
// Compact index, atomic — same reasoning as the old single-file store. Skipped when the bytes
- // equal our last write AND the file still has the size/mtime that write left (another instance
- // sharing this userData rewrote it otherwise). A migration always writes: the v3 flip is the
- // point of that save.
+ // equal our last write AND the file still has the size/mtime/inode that write left (another
+ // instance sharing this userData rewrote it otherwise). A migration always writes: the v3 flip
+ // is the point of that save.
const indexJson = JSON.stringify(index)
- const onDisk = !migrating && this.lastIndexWrite?.json === indexJson
+ const last = this.lastIndexWrite
+ const onDisk = !migrating && last?.json === indexJson
? await fs.stat(this.indexPath).catch(() => null)
: null
const unchanged =
- !!onDisk && onDisk.size === this.lastIndexWrite!.size && onDisk.mtimeMs === this.lastIndexWrite!.mtimeMs
+ !!onDisk &&
+ !!last &&
+ onDisk.size === last.size &&
+ onDisk.mtimeMs === last.mtimeMs &&
+ onDisk.ino === last.ino
if (!unchanged) {
this.lastIndexWrite = null
await writeAtomic(this.indexPath, indexJson)
const st = await fs.stat(this.indexPath).catch(() => null)
- this.lastIndexWrite = st ? { json: indexJson, size: st.size, mtimeMs: st.mtimeMs } : null
+ this.lastIndexWrite = st
+ ? { json: indexJson, size: st.size, mtimeMs: st.mtimeMs, ino: st.ino }
+ : null
}
await this.sweepRemovedDataFiles(previousIndex, index)
this.index = index