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(codex): make the remote import atomic — hardlink (ln), not mv + a check-then-act guard

enesd6c9d9a22710
diff --git a/src/main/index.ts b/src/main/index.ts
index e7a03ffd4..02bacffc0 100644
--- a/src/main/index.ts
+++ b/src/main/index.ts
@@ -410,8 +410,9 @@ async function loadCodexRelayBundle(): Promise<string> {
try {
const fd = openSync(join(__dirname, 'codex-relay.js'), 'r')
try {
- fstatSync(fd) // touch the open descriptor; the bundle is a plain file we just built
- codexRelayBundleCache = readFileSync(fd, 'utf8')
+ // fstat the OPEN descriptor (not the path) and read the SAME fd — a regular-file check with no
+ // stat-then-read window. A non-regular entry (fifo/dir/device) is treated as "no bundle".
+ codexRelayBundleCache = fstatSync(fd).isFile() ? readFileSync(fd, 'utf8') : ''
} finally {
closeSync(fd)
}
diff --git a/src/main/remote-ssh/ssh-project.test.ts b/src/main/remote-ssh/ssh-project.test.ts
index ac1c4a8e7..33fea28fc 100644
--- a/src/main/remote-ssh/ssh-project.test.ts
+++ b/src/main/remote-ssh/ssh-project.test.ts
@@ -2286,7 +2286,7 @@ describe('SshProjectManager — atomic remote import (Task 6.2, Property 2 + 11)
expect(runScp.mock.calls.length).toBe(0)
})
- it('installs atomically and NEVER overwrites: exit-17 guard present, mv is not forced', async () => {
+ it('installs with an ATOMIC hardlink that never overwrites (ln, not mv) — fails closed on EEXIST', async () => {
let phase = 0
const cm = await makeCodexMgr({
handler: (cmd) => {
@@ -2296,13 +2296,15 @@ describe('SshProjectManager — atomic remote import (Task 6.2, Property 2 + 11)
})
const out = await cm.mgr.remoteCodexImportThread('p1', 'acc1', 'thread1', REL, '/l/r.jsonl')
expect(out).toEqual({ imported: true })
- const install = cm.runCalls.find((c) => c.cmd.includes('exit 17'))
+ const install = cm.runCalls.find((c) => c.cmd.includes(' ln '))
expect(install).toBeTruthy()
- // The never-overwrite guard: refuse if the target exists, drop the staged file, exit 17.
- expect(install!.cmd).toMatch(/if \[ -e '[^']*rollout-x\.jsonl' \]; then rm -f '[^']*\.part'; exit 17; fi/)
- // The land is a bare `mv` (atomic within one fs), NEVER `mv -f` (which would clobber).
- expect(install!.cmd).toMatch(/(^| )mv '[^']*\.part' '[^']*rollout-x\.jsonl'/)
- expect(install!.cmd).not.toMatch(/mv -f /)
+ // The land is an atomic hardlink of the staged .part onto the target — link(2) fails with
+ // EEXIST if the target already exists, so there is no check-then-act window (PR 3 discipline).
+ expect(install!.cmd).toMatch(/ln '[^']*\.part' '[^']*rollout-x\.jsonl'/)
+ // `mv` is NEVER used: POSIX `mv` silently overwrites, which is exactly the racy primitive this
+ // avoids. On the ln-failure branch it drops the staged file and exits non-zero (17).
+ expect(install!.cmd).not.toMatch(/\bmv\b/)
+ expect(install!.cmd).toMatch(/else rm -f '[^']*\.part'; exit 17; fi/)
})
it('refuses when the install hits an existing target (exit 17 surfaces as a refusal)', async () => {
diff --git a/src/main/remote-ssh/ssh-project.ts b/src/main/remote-ssh/ssh-project.ts
index d557cb72d..50ab70846 100644
--- a/src/main/remote-ssh/ssh-project.ts
+++ b/src/main/remote-ssh/ssh-project.ts
@@ -1627,9 +1627,10 @@ export class SshProjectManager {
*
* The discipline PR 3 enforced locally, now over SSH:
* - **No-op if the thread already exists** on the target (never re-import).
- * - **Never overwrite** an existing remote target: the install `sh` `exit 17`s if the path is
- * already present, and this surfaces `exit 17` as a refusal — a partial/racing import can never
- * corrupt a target account's rollout.
+ * - **Never overwrite** an existing remote target: the install `sh` uses `ln` (hardlink), which
+ * fails ATOMICALLY with `EEXIST` when the target exists — the same never-overwrite primitive as
+ * PR 3's local `linkSync`, with no check-then-act race. `mv` is deliberately NOT used (it would
+ * silently clobber). A partial/racing import can never corrupt a target account's rollout.
* - **Verify-before-recycle:** after install, re-catalog and re-check discoverability; if the far
* side cannot see the thread, ROLL THE STAGED FILE BACK and refuse — no half-landed state.
*
@@ -1687,16 +1688,23 @@ export class SshProjectManager {
.catch(() => {})
throw new Error('Could not upload Codex conversation')
}
- // Atomic install that REFUSES to overwrite: if the target already exists, drop the staged file
- // and `exit 17` (never clobber a remote rollout — Property 2/11). Only on a clear target does the
- // `mv` (atomic within one filesystem) land it, `chmod 600` (credential-adjacent conversation).
+ // Atomic install that NEVER overwrites — the same discipline as PR 3's local `linkSync`
+ // (`commitCodexRolloutExposure`), now over SSH: `ln` (hardlink) is used, NOT `mv`, precisely
+ // because POSIX `mv` SILENTLY overwrites its destination. `link(2)` instead fails atomically
+ // with `EEXIST` (non-zero) when the target already exists — there is no check-then-act window a
+ // concurrent writer could race (a bare `[ -e ] && mv` would have one). Same-filesystem holds:
+ // staging (`$HOME/.nodeterm/codex-imports`) and target (`$HOME/.nodeterm/cx/<digest>/sessions`
+ // or `$HOME/.codex/sessions`) are both under the host `$HOME`; a cross-device `ln` (`EXDEV`)
+ // also fails closed here (exit 17), it is never silently copied. On success `chmod 600` the
+ // shared inode (credential-adjacent conversation) and drop the staging name, leaving the target.
const installed = await this.r.run(
childArgs(
c!.conn,
c!.controlPath,
- `if [ -e ${posixQuote(target)} ]; then rm -f ${posixQuote(staging)}; exit 17; fi; ` +
- `mkdir -p ${posixQuote(targetDir)} && chmod 700 ${posixQuote(targetDir)} && ` +
- `mv ${posixQuote(staging)} ${posixQuote(target)} && chmod 600 ${posixQuote(target)}`
+ `mkdir -p ${posixQuote(targetDir)} && chmod 700 ${posixQuote(targetDir)} || { rm -f ${posixQuote(staging)}; exit 70; }; ` +
+ `if ln ${posixQuote(staging)} ${posixQuote(target)} 2>/dev/null; then ` +
+ `chmod 600 ${posixQuote(target)}; rm -f ${posixQuote(staging)}; ` +
+ `else rm -f ${posixQuote(staging)}; exit 17; fi`
)
)
if (installed.code !== 0) {