Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- **Sessions: a Codex or OpenCode login refreshed inside a session is adopted back into the shared login, so the agents are no longer left on a dead one** ([#18740](https://github.com/hivecommons/hive/issues/18740)). A session gets a full copy of `.codex/auth.json` and `.local/share/opencode/auth.json`. Codex's ChatGPT login and an OpenCode OAuth provider entry rotate their refresh token on refresh, so a session that refreshed its copy held the only valid login: the agents, which link to the shared file, failed their next refresh and fell to a login prompt, and deleting the session deleted the login. The hive now adopts a strictly newer login from a session HOME into the shared file, the same safety net #18728 added for Claude: at boot, on every credential-bridge poll, at a session's start or resume, and before a session is deleted (a newer login that cannot be adopted keeps the session). Newer means the session's file was written after the shared one and, for Codex, records a later `last_refresh` with a different refresh token; for OpenCode, per provider entry, a later `expires` with a different refresh token. Only the refreshed OpenCode entries are replaced; other entries, API-key logins and providers the shared file does not hold are left alone. The replaced file is kept as `auth.json.stale-<UTC time>`, and `user_session_login_adopted` records the session, trigger, vendor and file, never token material. A session still holds a copy that can refresh (whether each CLI accepts a login without its refresh token is unverified), so a session and an agent refreshing in the same few seconds converge on the newer login at the next poll rather than being prevented.
11 changes: 10 additions & 1 deletion src/crux/agent/usersession_home.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,10 @@ func (r *UserSessionRuntime) prepareSandbox(id string, uid int) error {
// refresh token, and a newer login already in the session HOME is adopted
// into the shared file first (usersession_login.go).
//
// For every other vendor the copy is skipped when the session's file was
// A Codex or OpenCode login the session's CLI refreshed is adopted into the
// shared file first when it is strictly newer (usersession_login_rotating.go).
//
// For every vendor but Claude the copy is skipped when the session's file was
// written after the shared one. That is the state of an unchanged copy from
// an earlier start, and also of a login the CLI in the session rewrote itself
// (a token refresh): re-copying an older shared file over it on a resume
Expand All @@ -183,6 +186,12 @@ func (r *UserSessionRuntime) copyLoginFiles(id string, uid int) {
_ = r.syncClaudeLogin(id, sessionLoginTriggerStart, sessionLoginModeStart)
continue
}
if l, ok := rotatingSessionLoginFor(rel); ok {
// A login the session's CLI refreshed (Codex, OpenCode) goes into
// the shared file first (usersession_login_rotating.go). Reported
// inside; a failure must not block the session.
_ = r.syncRotatingLogin(id, sessionLoginTriggerStart, l)
}
src := filepath.Join(sharedAgentHome, rel)
dest := filepath.Join(home, rel)
if sessionLoginCopyIsCurrent(src, dest) {
Expand Down
21 changes: 15 additions & 6 deletions src/crux/agent/usersession_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,15 +386,20 @@ func (r *UserSessionRuntime) loginOps() sessionLoginOps {
// syncClaudeLogin checks one session's Claude login and reports the outcome.
func (r *UserSessionRuntime) syncClaudeLogin(id, trigger string, mode sessionLoginMode) error {
res, err := r.loginOps().checkSessionClaudeLogin(id, mode)
reportSessionLogin(r.logger, r.loginAudit(), id, trigger, res, err)
return err
}

// loginAudit is the audit function of a session login check made by the
// runtime, nil when no sink is installed.
func (r *UserSessionRuntime) loginAudit() func(string, map[string]any) {
r.auditMu.Lock()
sink := r.auditSink
r.auditMu.Unlock()
var audit func(string, map[string]any)
if sink != nil {
audit = func(action string, fields map[string]any) { sink.Record(auditActorSystem, action, "", fields) }
if sink == nil {
return nil
}
reportSessionLogin(r.logger, audit, id, trigger, res, err)
return err
return func(action string, fields map[string]any) { sink.Record(auditActorSystem, action, "", fields) }
}

// ---- manager side
Expand Down Expand Up @@ -461,7 +466,9 @@ func (w *sessionLoginCache) forget(keep map[string]bool) {
// syncUserSessionClaudeLogins checks the Claude login of every session
// sandbox on this hive: it adopts a strictly newer login a session holds into
// the shared file (with an audit entry) and brings every session's copy up to
// date with the shared one. It returns the ids it adopted a login from.
// date with the shared one. It returns the ids it adopted a Claude login
// from. The same sweep adopts a newer Codex or OpenCode login
// (syncUserSessionRotatingLogins).
//
// It runs at boot, on the credential-bridge poll and after a dashboard
// sign-in, whether or not any agent uses Claude: sessions use the shared
Expand All @@ -484,6 +491,8 @@ func (m *Manager) syncUserSessionClaudeLogins(trigger string) []string {
if !e.IsDir() || !userSessionIDPattern.MatchString(id) {
continue
}
// Codex and OpenCode logins (usersession_login_rotating.go).
m.syncUserSessionRotatingLogins(id, trigger, ops, force, present)
path := filepath.Join(userSessionHome(id), userSessionClaudeLoginRel)
stamp, ok := sessionLoginStampOf(path, target)
if !ok {
Expand Down
Loading
Loading