Skip to content

The agent that grows with you

fix(auth): a global-root write-through invalidates the root store memo

kshitij683afe45ba10
diff --git a/hermes_cli/auth.py b/hermes_cli/auth.py
index 8bbcc5b795b..c6fb6a9c7f5 100644
--- a/hermes_cli/auth.py
+++ b/hermes_cli/auth.py
@@ -742,6 +742,11 @@ def _save_auth_store(auth_store: Dict[str, Any], target_path: Optional[Path] = N
auth_store["version"] = AUTH_STORE_VERSION
auth_store["updated_at"] = datetime.now(timezone.utc).isoformat()
_write_private_file_atomic(auth_file, json.dumps(auth_store, indent=2) + "\n", fsync_dir=True)
+ if target_path is not None:
+ # A write-through to the global root must not be masked by the mtime memo: on coarse-mtime
+ # filesystems a read-after-write in the same tick would keep serving the pre-write store.
+ global _global_auth_store_cache
+ _global_auth_store_cache = None
try:
auth_file.chmod(stat.S_IRUSR | stat.S_IWUSR)
except OSError:
diff --git a/tests/hermes_cli/test_auth_profile_fallback.py b/tests/hermes_cli/test_auth_profile_fallback.py
index 60a58eae403..d7bcbe72045 100644
--- a/tests/hermes_cli/test_auth_profile_fallback.py
+++ b/tests/hermes_cli/test_auth_profile_fallback.py
@@ -227,6 +227,25 @@ def test_codex_cooldown_clear_never_touches_root_when_profile_owns_rows(profile_
assert root_file.read_bytes() == before
+def test_root_write_through_is_visible_to_the_next_fallback_read(profile_env):
+ """``_save_auth_store(target_path=root)`` must invalidate the mtime memo: a same-tick
+ read-after-write (coarse-mtime filesystems) would otherwise keep serving the stale root."""
+ import os
+ from hermes_cli.auth import _save_auth_store, read_credential_pool
+
+ root_file = profile_env["global"] / "auth.json"
+ _write(root_file, _make_auth_store(pool={"openai-codex": [{"id": "glob", "access_token": "old"}]}))
+ _write(profile_env["profile"] / "auth.json", _make_auth_store(pool={"openai-codex": []}))
+ assert read_credential_pool("openai-codex")[0]["access_token"] == "old" # primes the memo
+ stat = root_file.stat()
+
+ _save_auth_store(_make_auth_store(pool={"openai-codex": [{"id": "glob", "access_token": "new"}]}),
+ target_path=root_file)
+ os.utime(root_file, ns=(stat.st_atime_ns, stat.st_mtime_ns)) # simulate a same-tick write
+
+ assert read_credential_pool("openai-codex")[0]["access_token"] == "new"
+
+
# ---------------------------------------------------------------------------
# Classic mode — no fallback path should ever trigger
# ---------------------------------------------------------------------------