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(storage): route the drift's new stores through the atomic helper

enes3df6c2b08ba8
diff --git a/src/core/codex-accounts-core.ts b/src/core/codex-accounts-core.ts
index 0c8d53c1d..582112136 100644
--- a/src/core/codex-accounts-core.ts
+++ b/src/core/codex-accounts-core.ts
@@ -15,10 +15,10 @@ import {
mkdirSync,
readdirSync,
realpathSync,
- renameSync,
statSync,
unlinkSync
} from 'fs'
+import { renameAtomicSync } from './fs-atomic'
import os from 'os'
import path from 'path'
import { ACCOUNT_ID_RE, isSafeAccountId } from '../shared/codex-account'
@@ -77,7 +77,7 @@ export function migrateLegacyCodexAccountHome(
const target = codexAccountHome(userDataDir, accountId, shortRoot)
if (legacy === target || !existsSync(legacy) || existsSync(target)) return target
mkdirSync(path.dirname(target), { recursive: true, mode: 0o700 })
- renameSync(legacy, target)
+ renameAtomicSync(legacy, target)
return target
}
diff --git a/src/core/project-settings-files.ts b/src/core/project-settings-files.ts
index 8168e29b3..ed49cc1c1 100644
--- a/src/core/project-settings-files.ts
+++ b/src/core/project-settings-files.ts
@@ -1,5 +1,6 @@
import { promises as fs } from 'fs'
import path from 'path'
+import { renameAtomic } from './fs-atomic'
import {
parseProjectSettingsFile,
sameProjectSettingsContent,
@@ -40,7 +41,7 @@ export async function readProjectSettingsFile(cwd: string): Promise<ProjectSetti
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()}`)
+ await renameAtomic(file, `${file}.corrupt-${Date.now()}`)
} catch { /* best effort — never destroy data */ }
return { status: 'invalid' }
}
diff --git a/src/main/codex-relay-daemon.ts b/src/main/codex-relay-daemon.ts
index a287bd30d..1d1fc8a51 100644
--- a/src/main/codex-relay-daemon.ts
+++ b/src/main/codex-relay-daemon.ts
@@ -48,12 +48,12 @@ import {
openSync,
readFileSync,
readdirSync,
- renameSync,
rmSync,
statSync,
unlinkSync,
writeFileSync
} from 'fs'
+import { renameAtomicSync, tempNameFor } from '../core/fs-atomic'
import { createServer, request } from 'http'
import { homedir } from 'os'
import path from 'path'
@@ -647,7 +647,7 @@ async function bind(route: Route, threadId: string, name?: string): Promise<bool
return notifyElectron(route, threadId, name)
}
mkdirSync(path.dirname(file), { recursive: true, mode: 0o700 })
- const tmp = `${file}.${process.pid}.tmp`
+ const tmp = tempNameFor(file)
const quarantined: Array<{ source: string; quarantine: string }> = []
const mappings = path.join(root, 'codex-thread-nodes')
try {
@@ -676,16 +676,16 @@ async function bind(route: Route, threadId: string, name?: string): Promise<bool
path.basename(other.file) === threadId)
) {
const quarantine = `${other.file}.transfer-${process.pid}-${Date.now()}-${quarantined.length}`
- renameSync(other.file, quarantine)
+ renameAtomicSync(other.file, quarantine)
quarantined.push({ source: other.file, quarantine })
}
}
}
- renameSync(tmp, file)
+ renameAtomicSync(tmp, file)
} catch {
for (const item of quarantined.reverse()) {
try {
- renameSync(item.quarantine, item.source)
+ renameAtomicSync(item.quarantine, item.source)
} catch {}
}
try {
@@ -1543,18 +1543,26 @@ function serve(): void {
server.off('error', onListenError)
const addr = server.address()
if (!addr || typeof addr === 'string') process.exit(1)
- const tmp = `${statePath}.${process.pid}.tmp`
- writeFileSync(
- tmp,
- JSON.stringify({
- version: VERSION,
- pid: process.pid,
- port: addr.port,
- token
- }),
- { mode: 0o600 }
- )
- renameSync(tmp, statePath)
+ const tmp = tempNameFor(statePath)
+ try {
+ writeFileSync(
+ tmp,
+ JSON.stringify({
+ version: VERSION,
+ pid: process.pid,
+ port: addr.port,
+ token
+ }),
+ { mode: 0o600 }
+ )
+ renameAtomicSync(tmp, statePath)
+ } catch (e) {
+ // A unique temp never self-heals the way the old fixed name did; remove it on failure.
+ try {
+ unlinkSync(tmp)
+ } catch {}
+ throw e
+ }
releaseLock()
})
}