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): tmp hygiene parity — cleanup-on-failure + stale-tmp sweep

enes789152ce866a
diff --git a/src/core/workspace-store.test.ts b/src/core/workspace-store.test.ts
index 7911ba0c4..288fd03bd 100644
--- a/src/core/workspace-store.test.ts
+++ b/src/core/workspace-store.test.ts
@@ -1004,6 +1004,49 @@ describe('save corruption hardening', () => {
expect(await fs.readFile(path.join(userData, 'workspace.json'), 'utf-8')).toBe(before)
})
+ it('a failed atomic write removes its own temp file (index write)', async () => {
+ // Force the rename to fail: the target is a directory. The unique temp name never self-heals
+ // the way the old fixed one did, so the failed write must clean up after itself.
+ await fs.mkdir(path.join(userData, 'workspace.json'))
+ await expect(new WorkspaceStore().save(ws([project({ cwd: projRoot })]))).rejects.toThrow()
+ const litter = (await fs.readdir(userData)).filter((n) => n.endsWith('.tmp'))
+ expect(litter).toEqual([])
+ })
+
+ it('a swallowed project-file write failure still leaves no temp litter in .nodeterm', async () => {
+ const store = new WorkspaceStore()
+ await store.save(ws([project({ cwd: projRoot })]))
+ const file = path.join(projRoot, '.nodeterm', 'project.json')
+ await fs.rm(file)
+ await fs.mkdir(file) // rename now fails; save() swallows per-file errors by design
+ await store.save(ws([project({ cwd: projRoot, name: 'renamed' })]))
+ const litter = (await fs.readdir(path.join(projRoot, '.nodeterm'))).filter((n) => n.endsWith('.tmp'))
+ expect(litter).toEqual([])
+ })
+
+ it('load() sweeps stale tmp litter from dead writers, sparing this process and other files', async () => {
+ const store = new WorkspaceStore()
+ await store.save(ws([project({ cwd: projRoot })]))
+ const nodeterm = path.join(projRoot, '.nodeterm')
+ await fs.writeFile(path.join(userData, 'workspace.json.tmp'), 'x') // legacy fixed name
+ await fs.writeFile(path.join(userData, 'workspace.json.99999.1.tmp'), 'x') // dead pid
+ await fs.writeFile(path.join(nodeterm, 'project.json.99999.2.tmp'), 'x') // dead pid
+ const mine = `project.json.${process.pid}.7.tmp` // a live writer of THIS process — never swept
+ await fs.writeFile(path.join(nodeterm, mine), 'x')
+ const loaded = await new WorkspaceStore().load()
+ expect(loaded.projects[0]?.nodes.map((n) => n.id)).toEqual(['term-1']) // real files untouched
+ expect((await fs.readdir(userData)).filter((n) => n.endsWith('.tmp'))).toEqual([])
+ expect((await fs.readdir(nodeterm)).filter((n) => n.endsWith('.tmp'))).toEqual([mine])
+ })
+
+ it('a read-only load (sideline: false) sweeps nothing', async () => {
+ await new WorkspaceStore().save(ws([project({ cwd: projRoot })]))
+ await fs.writeFile(path.join(userData, 'workspace.json.99999.1.tmp'), 'x')
+ await new WorkspaceStore().load({ sideline: false })
+ expect((await fs.readdir(userData)).filter((n) => n.endsWith('.tmp')))
+ .toEqual(['workspace.json.99999.1.tmp'])
+ })
+
it('closing every project after a successful load still persists the empty workspace', async () => {
await new WorkspaceStore().save(ws([project({ cwd: projRoot })]))
const store = new WorkspaceStore()
diff --git a/src/core/workspace-store.ts b/src/core/workspace-store.ts
index dac6b04f7..c77595170 100644
--- a/src/core/workspace-store.ts
+++ b/src/core/workspace-store.ts
@@ -40,8 +40,36 @@ async function writeAtomic(filePath: string, content: string): Promise<void> {
// poll's index write) must never share a tmp file — interleaved writes into one shared tmp
// published spliced JSON under the atomic rename.
const tmp = `${filePath}.${process.pid}.${++tmpSeq}.tmp`
- await fs.writeFile(tmp, content, 'utf-8')
- await fs.rename(tmp, filePath)
+ try {
+ await fs.writeFile(tmp, content, 'utf-8')
+ await fs.rename(tmp, filePath)
+ } catch (e) {
+ // A unique name never self-heals the way the old fixed one did (the next save just reused
+ // it), so a failed write removes its own temp — project.json temps live in the USER'S repo,
+ // where litter is visible. The error still propagates; per-file callers swallow it by design.
+ await fs.rm(tmp, { force: true }).catch(() => {})
+ throw e
+ }
+}
+
+/** Remove tmp litter next to `target` left by writers that died mid-write: the legacy fixed
+ * `<file>.tmp` name and any `<file>.<pid>.<seq>.tmp` from another (dead) pid. Our own pid's
+ * temps are in-flight writes and stay. Same family rule as provider-cookie's sweep. */
+async function sweepStaleTmp(target: string): Promise<void> {
+ try {
+ const dir = path.dirname(target)
+ const base = path.basename(target)
+ for (const entry of await fs.readdir(dir)) {
+ if (!entry.startsWith(base) || !entry.endsWith('.tmp')) continue
+ const middle = entry.slice(base.length, -'.tmp'.length) // '' or '.<pid>.<seq>'
+ const owner = /^\.(\d+)\.\d+$/.exec(middle)?.[1]
+ if (middle === '' || (owner && owner !== String(process.pid))) {
+ await fs.rm(path.join(dir, entry), { force: true }).catch(() => undefined)
+ }
+ }
+ } catch {
+ // A dir we cannot read is not a reason to fail the load.
+ }
}
/**
@@ -108,6 +136,9 @@ export class WorkspaceStore {
}
private async loadInner(sideline: boolean): Promise<Workspace> {
+ // Read-only loads (sideline: false — the relay blob path) must not mutate the disk, so the
+ // litter sweep rides the same flag as the corrupt-file sideline.
+ if (sideline) await sweepStaleTmp(this.indexPath)
let raw: string
try {
raw = await fs.readFile(this.indexPath, 'utf-8')
@@ -176,6 +207,7 @@ export class WorkspaceStore {
const { kanban, ...rest } = e.project
projects.push(validKanban(kanban) ? e.project : rest)
} else if (e.cwd) {
+ if (sideline) await sweepStaleTmp(projectFilePath(e.cwd))
const read = await this.readProjectFile(e.cwd, sideline)
if (read) {
const p = read.file