diff --git a/scripts/ai-accounts/ai-accounts b/scripts/ai-accounts/ai-accounts index cdc7a1fd..80946a42 100755 --- a/scripts/ai-accounts/ai-accounts +++ b/scripts/ai-accounts/ai-accounts @@ -46,6 +46,7 @@ from __future__ import annotations import argparse import base64 +import ctypes import datetime as dt import errno from email.utils import parsedate_to_datetime @@ -201,9 +202,20 @@ def sync_codex_home(home: Path, *, create: bool = False) -> None: _sync_codex_home(home) -def _copy_across_devices(entry: Path, target: Path) -> None: - import ctypes +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") + 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() + raise OSError(error, os.strerror(error), source, None, target) + +def _copy_across_devices(entry: Path, target: Path) -> None: # The private staging directory is also excluded from future mirroring # by shared() if the process is interrupted before cleanup. with tempfile.TemporaryDirectory(dir=target.parent, prefix=".codex-sync-", suffix=".lock") as directory: @@ -214,16 +226,7 @@ def _copy_across_devices(entry: Path, target: Path) -> None: else: shutil.copy2(entry, staged, follow_symlinks=False) - # Linux/WSL renameat2 with RENAME_NOREPLACE publishes the complete - # entry in one operation without replacing a competing destination. - rename = getattr(ctypes.CDLL(None, use_errno=True), "renameat2", None) - if rename is None: - raise OSError(errno.ENOSYS, "Atomic cross-device publication is unavailable", target) - if rename(-100, os.fsencode(staged), -100, os.fsencode(target), 1): - error = ctypes.get_errno() - if error == errno.EEXIST: - return - raise OSError(error, os.strerror(error), target) + _rename_noreplace(staged, target) if is_directory: shutil.rmtree(entry) else: @@ -237,13 +240,16 @@ 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: - pass + try: + _rename_noreplace(entry, target) + except OSError as exc: + if exc.errno != errno.EXDEV: + raise + _copy_across_devices(entry, target) except OSError as exc: - if exc.errno != errno.EXDEV: + if exc.errno not in {errno.EEXIST, errno.ENOENT, errno.ENOSYS, errno.EINVAL, + errno.EOPNOTSUPP}: raise - _copy_across_devices(entry, target) 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 cba117bd..c0260706 100644 --- a/tests/test_ai_accounts.py +++ b/tests/test_ai_accounts.py @@ -618,3 +618,28 @@ def stage(src, dst, **kwargs): helper.account_env("codex", "extra") assert entry.read_text() == "original" and entry.is_symlink() == (outcome == "published") assert list(Path(main).iterdir()) == ([] if outcome == "copy failed" else [Path(main) / entry.name]) + + +@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() + 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() + 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() and (home / "notes").read_text() == "promote"