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): roll back a wrongly-published target link when commit fails

Claude Code66d0cf438799
diff --git a/src/core/codex-accounts-core.test.ts b/src/core/codex-accounts-core.test.ts
index b72d8084f..f19ea3bbb 100644
--- a/src/core/codex-accounts-core.test.ts
+++ b/src/core/codex-accounts-core.test.ts
@@ -423,6 +423,40 @@ describe('cross-account rollout exposure (atomic, never-overwrite)', () => {
expect(readdirSync(escape)).toEqual([])
})
+ it('refuses a symlink at a DEEPER target segment without writing through it', () => {
+ const root = newRoot()
+ const homeA = makeHome(root, 'a')
+ const sourcePath = writeRollout(homeA)
+ // homeB/sessions is a real dir, but homeB/sessions/2026 is a symlink to an escape directory.
+ const homeB = makeHome(root, 'b')
+ const escape = path.join(root, 'escape')
+ mkdirSync(escape, { recursive: true })
+ symlinkSync(escape, path.join(homeB, 'sessions', '2026'))
+
+ const plan = planCodexRolloutExposure(homeA, homeB, sourcePath, THREAD)
+ expect(() => commitCodexRolloutExposure(plan)).toThrow(
+ 'Target Codex rollout path contains an unsafe directory'
+ )
+ // The deeper-segment guard also refuses to write through: the escape tree stays empty.
+ expect(readdirSync(escape)).toEqual([])
+ })
+
+ it('refuses a symlinked source rollout (plan, never followed)', () => {
+ const root = newRoot()
+ const homeA = makeHome(root, 'a')
+ const homeB = makeHome(root, 'b')
+ // A real rollout file elsewhere, and a symlink to it sitting at the canonical source pathname.
+ const realFile = path.join(root, 'real.jsonl')
+ writeFileSync(realFile, '{"line":1}\n')
+ const symlinkedSource = path.join(homeA, REL)
+ mkdirSync(path.dirname(symlinkedSource), { recursive: true })
+ symlinkSync(realFile, symlinkedSource)
+
+ expect(() => planCodexRolloutExposure(homeA, homeB, symlinkedSource, THREAD)).toThrow(
+ 'Source Codex rollout is not a regular file'
+ )
+ })
+
it('removes its temporary link when the source inode changes before link creation', () => {
const root = newRoot()
const homeA = makeHome(root, 'a')
@@ -473,6 +507,9 @@ describe('cross-account rollout exposure (atomic, never-overwrite)', () => {
expect(() => commitCodexRolloutExposure(plan, hijackPublish)).toThrow(
'Target Codex rollout did not preserve the verified source inode'
)
+ // Rollback: a failed commit leaves NOTHING published — the wrongly-linked target is removed, so
+ // no foreign inode is left permanently occupying the target account's real sessions path.
+ expect(existsSync(plan.targetPath)).toBe(false)
})
it('surfaces a named error on a cross-mount (EXDEV) link — no silent copy fallback', () => {
diff --git a/src/core/codex-accounts-core.ts b/src/core/codex-accounts-core.ts
index c93343805..62ec305ea 100644
--- a/src/core/codex-accounts-core.ts
+++ b/src/core/codex-accounts-core.ts
@@ -438,6 +438,19 @@ export function commitCodexRolloutExposure(
return false
}
}
+ // The inode our own publish link(2) created at `plan.targetPath`, if it succeeded. Used to roll
+ // that link back on a later failure — but ONLY while the target still names that exact inode, so
+ // a legitimate entry a concurrent process raced into the pathname AFTER us is never deleted.
+ let publishedTarget: { dev: number; ino: number } | null = null
+ const publishedTargetStillOurs = (): boolean => {
+ if (!publishedTarget) return false
+ try {
+ const currentTarget = lstatSync(plan.targetPath)
+ return currentTarget.dev === publishedTarget.dev && currentTarget.ino === publishedTarget.ino
+ } catch {
+ return false
+ }
+ }
try {
if (!isVerifiedRollout(temporaryPath)) {
throw new Error('Temporary Codex rollout did not preserve the verified source inode')
@@ -446,13 +459,27 @@ export function commitCodexRolloutExposure(
// link(2) is no-overwrite. Publishing from the verified private name prevents cleanup from
// ever deleting an unrelated entry raced into the final pathname.
link(temporaryPath, plan.targetPath)
+ // We created this exact entry: remember its inode so a post-link failure can undo it.
+ const created = lstatSync(plan.targetPath)
+ publishedTarget = { dev: created.dev, ino: created.ino }
} catch (error) {
if ((error as NodeJS.ErrnoException).code !== 'EEXIST' || !isVerifiedRollout(plan.targetPath))
throw error
+ // EEXIST + verified ⇒ idempotent re-expose; the target was NOT created by us, never rolled back.
}
if (!isVerifiedRollout(plan.targetPath)) {
throw new Error('Target Codex rollout did not preserve the verified source inode')
}
+ } catch (error) {
+ // Roll back a target THIS call published so a failed commit leaves the target as it was found
+ // (nothing published). Guarded to our own inode: an idempotent EEXIST match or a foreign entry
+ // raced in after us is left untouched.
+ if (publishedTargetStillOurs()) {
+ try {
+ unlinkSync(plan.targetPath)
+ } catch {}
+ }
+ throw error
} finally {
// The private pathname may itself have been replaced. Delete it only while it still names the
// exact inode created by our link(2), even when a source race made that inode invalid.