No description
Find a file
Bartłomiej Zabłocki 2c4d686caa Rotate Codex accounts hourly instead of once a day
Codex is back to a rolling 5-hour usage window. Rotating once a day meant
one account burned its whole window in the morning peak and then sat
rate-limited while the other idled until the next 05:00 swap.

Shortening the period alone was not safe. ~/.codex/auth.json is a symlink
into the account profile, and Codex does not merely read it: it rewrites
the file on every access-token refresh, since that is where refresh_token
and last_refresh live. The write follows the symlink, so swapping the link
under a live process stores that process's credentials in the NEXT
account's file — both profiles collapse onto one account and the
overwritten refresh token is gone, recoverable only by a fresh login.

So the account is now decided at process start rather than at write time.
Callers that pin CODEX_HOME to the profile directory are immune to a swap
mid-run, and `autorotate` covers the ones that cannot pin — Codex started
by hand reads the shared, unpinned auth.json — by declining to rotate
while any codex process is alive.

- `autorotate`: rotate only when the interval has elapsed AND no Codex is
  running, under a non-blocking lock on the switch file. The systemd timer
  ticks every 10 minutes against a 1-hour interval, so a busy machine
  rotates at the first idle moment past the hour instead of only on the
  tick that happens to be idle.
- `rotate` now relinks the profiles and stamps the rotation, so a state
  file that appeared in the shared home after a profile was built is
  shared rather than duplicated inside the profile, and a manual rotation
  postpones the automatic one by a full interval.
- The systemd units ship with the tool, and install.sh wires both up.
- selftest.sh covers the round-robin, the relinking, the stamp and all
  three ways autorotate declines — against a fabricated HOME, so no real
  account file is ever touched.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0157XySjSE9v7dHftBKpK7en
2026-09-02 09:22:35 +02:00
systemd Rotate Codex accounts hourly instead of once a day 2026-09-02 09:22:35 +02:00
codex-sw Rotate Codex accounts hourly instead of once a day 2026-09-02 09:22:35 +02:00
install.sh Rotate Codex accounts hourly instead of once a day 2026-09-02 09:22:35 +02:00
selftest.sh Rotate Codex accounts hourly instead of once a day 2026-09-02 09:22:35 +02:00