Skip to content

fix(auth): write Anthropic OAuth token files atomically to prevent corruption - #5095

Closed
maymuneth wants to merge 1 commit into
NousResearch:mainfrom
maymuneth:fix/anthropic-oauth-token-atomic-write
Closed

fix(auth): write Anthropic OAuth token files atomically to prevent corruption#5095
maymuneth wants to merge 1 commit into
NousResearch:mainfrom
maymuneth:fix/anthropic-oauth-token-atomic-write

Conversation

@maymuneth

Copy link
Copy Markdown
Contributor

What does this PR do?

agent/anthropic_adapter.py writes Anthropic OAuth token files using
write_text() directly — a non-atomic operation. If the process is
killed mid-write (crash, OOM, SIGKILL), the file is left partially
written and corrupt.

On the next startup, json.loads() fails to parse the corrupt token
file — the user's Anthropic OAuth session is permanently broken and
they must re-authenticate manually.

Two write sites are affected:

  • _HERMES_OAUTH_FILE — the main Hermes OAuth token store
  • cred_path — per-credential token files

Fix

Write to a .tmp sibling file first, then use Path.replace() which
is atomic on POSIX systems (rename syscall).

# Before (non-atomic)
_HERMES_OAUTH_FILE.write_text(json.dumps(data, indent=2), encoding="utf-8")

# After (atomic)
_tmp = _HERMES_OAUTH_FILE.with_suffix(".tmp")
_tmp.write_text(json.dumps(data, indent=2), encoding="utf-8")
_tmp.replace(_HERMES_OAUTH_FILE)

This is consistent with the atomic write pattern already used in
hermes_cli/auth.py (_save_auth_store), gateway/run.py, and
tools/skill_manager_tool.py.

Type of Change

  • 🐛 Bug fix (reliability)

Checklist

  • Read the Contributing Guide
  • Commit messages follow Conventional Commits
  • Consistent with existing atomic write pattern in Hermes
  • No behavior change under normal operation
  • Prevents OAuth token loss after process crash

@teknium1

Copy link
Copy Markdown
Contributor

Thanks for this fix, @maymuneth — the atomic write pattern you introduced has been merged into main via the consolidated OAuth salvage PR #15175 (merged 2026-04-24).

Evidence:

This is an automated hermes-sweeper review. Closing as implemented on main.

@teknium1 teknium1 closed this Apr 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants