From 2caa6acaa6458180e1631a9594fee3903e50464b Mon Sep 17 00:00:00 2001 From: eowca <4008965+eowca@users.noreply.github.com> Date: Thu, 26 Nov 2026 12:56:38 +0100 Subject: [PATCH 1/2] Promote shared Codex entries without replacing the destination Architecture-Review: unchanged; account mirroring retains its lock and layout while refusing concurrent destination replacement. Co-Authored-By: Codex --- scripts/ai-accounts/ai-accounts | 18 ++++++++++++++++-- tests/test_ai_accounts.py | 23 +++++++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/scripts/ai-accounts/ai-accounts b/scripts/ai-accounts/ai-accounts index 5c4c901d..f2757abc 100755 --- a/scripts/ai-accounts/ai-accounts +++ b/scripts/ai-accounts/ai-accounts @@ -46,7 +46,9 @@ from __future__ import annotations import argparse import base64 +import ctypes import datetime as dt +import errno from email.utils import parsedate_to_datetime import fcntl import hashlib @@ -200,6 +202,18 @@ def sync_codex_home(home: Path, *, create: bool = False) -> None: _sync_codex_home(home) +def _rename_noreplace(source: Path, target: Path) -> None: + """Publish a shared entry atomically, including directories.""" + libc = ctypes.CDLL(None, use_errno=True) + renameat2 = getattr(libc, "renameat2", None) + if renameat2 is None: + raise OSError(errno.ENOSYS, "atomic no-replace promotion requires renameat2") + at_fdcwd, rename_noreplace = -100, 1 + if renameat2(at_fdcwd, os.fsencode(source), at_fdcwd, os.fsencode(target), rename_noreplace): + error = ctypes.get_errno() + raise OSError(error, os.strerror(error), source, None, target) + + def _sync_codex_home(home: Path) -> None: """Mirror shared Codex files while holding the home lock.""" for entry in home.iterdir(): @@ -207,8 +221,8 @@ def _sync_codex_home(home: Path) -> None: if shared(entry) and not entry.is_symlink() and not target.exists() \ and not target.is_symlink(): try: - os.rename(entry, target) - except FileNotFoundError: + _rename_noreplace(entry, target) + except (FileExistsError, FileNotFoundError): pass for src in CODEX_MAIN.iterdir(): dst = home / src.name diff --git a/tests/test_ai_accounts.py b/tests/test_ai_accounts.py index c161c052..e02de851 100644 --- a/tests/test_ai_accounts.py +++ b/tests/test_ai_accounts.py @@ -593,3 +593,26 @@ def test_account_lock_preserves_existing_file_and_rejects_symlink(helper): with pytest.raises(OSError), helper._locked(".codex-sync.lock"): pytest.fail("redirected lock opened") assert lock.read_text() == "preserve" + + +def test_sync_preserves_a_shared_directory_created_during_promotion(helper, monkeypatch): + home = helper.codex_home("extra") + home.mkdir(parents=True) + helper.CODEX_MAIN.mkdir() + source = home / "sessions" + source.mkdir() + (source / "old").write_text("keep local") + (home / "notes").write_text("promote") + rename = helper._rename_noreplace + + def create_destination(entry, target): + if entry == source: + target.mkdir() + rename(entry, target) + + monkeypatch.setattr(helper, "_rename_noreplace", create_destination) + helper.sync_codex_home(home) + assert (source / "old").read_text() == "keep local" + assert list((helper.CODEX_MAIN / "sessions").iterdir()) == [] + assert (home / "notes").is_symlink() + assert (home / "notes").read_text() == "promote" From a6a2a19b89ded73aac7d7b4ff3bf51a4c02661a5 Mon Sep 17 00:00:00 2001 From: eowca <4008965+eowca@users.noreply.github.com> Date: Thu, 26 Nov 2026 12:58:57 +0100 Subject: [PATCH 2/2] Keep mirroring when no-replace promotion is unsupported Co-Authored-By: Codex --- scripts/ai-accounts/ai-accounts | 8 ++++++-- tests/test_ai_accounts.py | 8 +++++--- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/scripts/ai-accounts/ai-accounts b/scripts/ai-accounts/ai-accounts index f2757abc..f7702dc8 100755 --- a/scripts/ai-accounts/ai-accounts +++ b/scripts/ai-accounts/ai-accounts @@ -208,6 +208,7 @@ def _rename_noreplace(source: Path, target: Path) -> None: renameat2 = getattr(libc, "renameat2", None) if renameat2 is None: raise OSError(errno.ENOSYS, "atomic no-replace promotion requires renameat2") + renameat2.argtypes = (ctypes.c_int, ctypes.c_char_p, ctypes.c_int, ctypes.c_char_p, ctypes.c_uint) at_fdcwd, rename_noreplace = -100, 1 if renameat2(at_fdcwd, os.fsencode(source), at_fdcwd, os.fsencode(target), rename_noreplace): error = ctypes.get_errno() @@ -222,8 +223,11 @@ def _sync_codex_home(home: Path) -> None: and not target.is_symlink(): try: _rename_noreplace(entry, target) - except (FileExistsError, FileNotFoundError): - pass + except OSError as exc: + # A competing writer or unsupported no-replace operation leaves the source local. + if exc.errno not in {errno.EEXIST, errno.ENOENT, errno.ENOSYS, errno.EINVAL, + errno.EOPNOTSUPP, errno.EXDEV}: + raise for src in CODEX_MAIN.iterdir(): dst = home / src.name if shared(src) and not dst.exists() and not dst.is_symlink(): diff --git a/tests/test_ai_accounts.py b/tests/test_ai_accounts.py index e02de851..2464d7a2 100644 --- a/tests/test_ai_accounts.py +++ b/tests/test_ai_accounts.py @@ -595,7 +595,8 @@ def test_account_lock_preserves_existing_file_and_rejects_symlink(helper): assert lock.read_text() == "preserve" -def test_sync_preserves_a_shared_directory_created_during_promotion(helper, monkeypatch): +@pytest.mark.parametrize("unsupported", [None, "ENOSYS", "EINVAL"]) +def test_sync_preserves_a_shared_directory_created_during_promotion(helper, monkeypatch, unsupported): home = helper.codex_home("extra") home.mkdir(parents=True) helper.CODEX_MAIN.mkdir() @@ -608,11 +609,12 @@ def test_sync_preserves_a_shared_directory_created_during_promotion(helper, monk def create_destination(entry, target): if entry == source: target.mkdir() + if unsupported: + raise OSError(getattr(helper.errno, unsupported), "no-replace unsupported") rename(entry, target) monkeypatch.setattr(helper, "_rename_noreplace", create_destination) helper.sync_codex_home(home) assert (source / "old").read_text() == "keep local" assert list((helper.CODEX_MAIN / "sessions").iterdir()) == [] - assert (home / "notes").is_symlink() - assert (home / "notes").read_text() == "promote" + assert (home / "notes").is_symlink() and (home / "notes").read_text() == "promote"