Skip to content

Add session auto-reload for fresh logins without restart#14

Merged
adambalogh merged 1 commit into
mainfrom
claude/funny-mendel-w4a9ns
Jun 16, 2026
Merged

Add session auto-reload for fresh logins without restart#14
adambalogh merged 1 commit into
mainfrom
claude/funny-mendel-w4a9ns

Conversation

@adambalogh

Copy link
Copy Markdown
Contributor

Summary

Implement session auto-reload functionality that allows a long-running background server to pick up fresh login credentials written to disk by another process (e.g., og-veil login) without requiring a restart.

Key Changes

  • Session file change detection: Track the modification time (mtime) of session.json to detect when another process rewrites it
  • Auto-reload on token access: Added _reload_if_changed() method that re-reads the session file from disk if its mtime differs from what we last saw
  • Thread-safe token access: Wrapped access_token() with a lock to serialize refresh and reload operations across concurrent request handlers in the threaded server
  • Smart refresh avoidance: The reload check happens before deciding whether to refresh, so a fresh valid token from disk won't trigger unnecessary refresh calls
  • Graceful error handling: If the session file is mid-write or unreadable, the reload is skipped and the in-memory session is retained

Implementation Details

  • _file_mtime() helper uses os.stat().st_mtime_ns for nanosecond precision to reliably detect file changes
  • The _mtime field is updated both when loading and saving the session to track our own writes
  • Reload failures (OSError, JSONDecodeError) are silently ignored to handle transient states during concurrent writes
  • Added comprehensive test coverage for the reload behavior, including edge cases like unchanged sessions and refresh write-back

https://claude.ai/code/session_01TYMUPb4VAjMhJ1WjssbyMy

…g server

The background server loads the Chat session once at startup and keeps it
in memory. After it is signed out upstream, requests fail with a 401 — and
crucially they keep failing even after a successful `og-veil login`, because
login writes a new token to disk while the running server keeps using its
stale (expired/revoked) in-memory copy. The only recovery was an undiscoverable
`og-veil restart`.

Make Session re-read session.json when another process rewrites it (tracked by
file mtime) before checking expiry/refreshing, so a fresh login is adopted by
the running server on the next request without a restart. Guard refresh/reload
with a lock since the server is threaded.
@adambalogh adambalogh marked this pull request as ready for review June 16, 2026 12:46
@adambalogh adambalogh merged commit bde6971 into main Jun 16, 2026
4 checks passed
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