Skip to content

Agent Zero AI framework

Support bind-mounted private OAuth auth files

Cooper Gamble47078c00ae52
diff --git a/plugins/_oauth/helpers/codex.py b/plugins/_oauth/helpers/codex.py
index 131f54f4d..da8f7ad36 100644
--- a/plugins/_oauth/helpers/codex.py
+++ b/plugins/_oauth/helpers/codex.py
@@ -1,6 +1,7 @@
from __future__ import annotations
import base64
+import errno
import hashlib
import json
import os
@@ -1047,7 +1048,13 @@ def _write_auth_file_unlocked(path: Path, data: dict[str, Any]) -> None:
handle.write(json.dumps(data, indent=2) + "\n")
handle.flush()
os.fsync(handle.fileno())
- os.replace(temporary_path, path)
+ try:
+ os.replace(temporary_path, path)
+ except OSError as exc:
+ if exc.errno != errno.EBUSY:
+ raise
+ # Linux rejects replacement when a supported custom auth path is a file bind mount.
+ _write_auth_file_in_place(path, data)
try:
path.chmod(0o600)
except OSError:
@@ -1056,6 +1063,13 @@ def _write_auth_file_unlocked(path: Path, data: dict[str, Any]) -> None:
temporary_path.unlink(missing_ok=True)
+def _write_auth_file_in_place(path: Path, data: dict[str, Any]) -> None:
+ with path.open("w", encoding="utf-8") as handle:
+ handle.write(json.dumps(data, indent=2) + "\n")
+ handle.flush()
+ os.fsync(handle.fileno())
+
+
def _validate_private_auth_path(path: Path) -> Path:
if _path_key(path) in {_path_key(candidate) for candidate in _known_codex_auth_paths()}:
raise RuntimeError(
diff --git a/tests/test_oauth_codex.py b/tests/test_oauth_codex.py
index e120e15de..7a75da025 100644
--- a/tests/test_oauth_codex.py
+++ b/tests/test_oauth_codex.py
@@ -281,6 +281,23 @@ def test_write_auth_file_uses_atomic_replace_and_private_permissions(tmp_path, m
assert list(tmp_path.glob(".auth.json.*.tmp")) == []
+def test_write_auth_file_falls_back_for_file_bind_mount(tmp_path, monkeypatch):
+ auth_path = tmp_path / "auth.json"
+ auth_path.write_text(json.dumps({"tokens": {"refresh_token": "refresh-0"}}), encoding="utf-8")
+
+ def reject_replace(source, destination):
+ raise OSError(codex.errno.EBUSY, "Device or resource busy", destination)
+
+ monkeypatch.setattr(codex.os, "replace", reject_replace)
+
+ codex.write_auth_file(auth_path, {"tokens": {"refresh_token": "refresh-1"}})
+
+ assert json.loads(auth_path.read_text(encoding="utf-8")) == {
+ "tokens": {"refresh_token": "refresh-1"}
+ }
+ assert list(tmp_path.glob(".auth.json.*.tmp")) == []
+
+
def test_load_auth_serializes_refresh_across_threads(tmp_path, monkeypatch):
auth_path = tmp_path / "auth.json"
_write_refreshable_auth(auth_path)