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: Codex and OpenCode logins are copied without a refresh token.** A session's copy of `.codex/auth.json` and `opencode/auth.json` now keeps the refresh key (both CLIs require it) but empties its value, like the Claude login since #18724, so the CLI in a session can no longer rotate the fleet's Codex or OpenCode login and race an agent's refresh into two diverged logins. The shared file the agents link to is the only refreshable holder; the hive replaces each session's copy when that file changes, adopts a complete newer login found in a session (a copy from an earlier release, or a sign-in inside the session) and then puts the session back on a refresh-less copy. A session now depends on an agent or a sign-in refreshing that login before its access token expires ([#18740](https://github.com/hivecommons/hive/issues/18740)).
24 changes: 14 additions & 10 deletions src/crux/agent/usersession_home.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ import (
// session write access to the shared file (and, for settings.json, a hook that
// every agent would run). Instead a fixed allowlist of the minimal login
// files per vendor (userSessionLoginFiles) is COPIED into the session HOME as
// read-only (0400), session-owned files; the Claude login is copied without
// its refresh token, so a session can never rotate the fleet's login
// (usersession_login.go). Nothing that can execute a command
// read-only (0400), session-owned files; the Claude, Codex and OpenCode
// logins are copied without their refresh tokens, so a session can never
// rotate the fleet's login (usersession_login.go,
// usersession_login_rotating.go). Nothing that can execute a command
// (settings.json, hooks, MCP/extension config) is ever copied, and ~/.config
// (gh's hosts.yml, goose extensions) is not bridged at all.
const (
Expand Down Expand Up @@ -168,10 +169,11 @@ 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).
//
// 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).
// Codex and OpenCode logins follow the same rules: the session gets them with
// the refresh token emptied, and a strictly newer login in the session HOME is
// adopted into the shared file first (usersession_login_rotating.go).
//
// For every vendor but Claude the copy is skipped when the session's file was
// For the other vendors 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 @@ -187,10 +189,12 @@ func (r *UserSessionRuntime) copyLoginFiles(id string, uid int) {
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)
// Codex and OpenCode: a newer login in the session goes into the
// shared file first, and the session gets a refresh-less copy
// (usersession_login_rotating.go). Reported inside; a failure
// must not block the session.
_ = r.syncRotatingLogin(id, sessionLoginTriggerStart, l, sessionLoginModeStart)
continue
}
src := filepath.Join(sharedAgentHome, rel)
dest := filepath.Join(home, rel)
Expand Down
5 changes: 5 additions & 0 deletions src/crux/agent/usersession_isolation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,11 @@ func TestUserSessionLoginsAreCopiedNeverSymlinkedOrBridgedConfig(t *testing.T) {
// (usersession_login.go); the fixture is synthetic.
content = string(bridgeTestCreds(t, "shared", time.Now().Add(time.Hour).UnixMilli()))
}
if l, ok := rotatingSessionLoginFor(rel); ok {
// Codex and OpenCode logins are copied only when they parse as a
// login of that vendor (usersession_login_rotating.go).
content = string(rotatingTestVendors()[l.vendor].login("shared", time.Now()))
}
writeSharedLogin(t, rel, content)
}
// Files that execute commands or hold unrelated secrets must never travel.
Expand Down
17 changes: 13 additions & 4 deletions src/crux/agent/usersession_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,18 +239,27 @@ func (o sessionLoginOps) writeSessionLogin(home, path, target string) (bool, err
if !ok {
return false, nil
}
if err := o.writeSessionCopy(home, path, copyBytes); err != nil {
return false, err
}
return true, nil
}

// writeSessionCopy replaces the session's login file at path with data,
// atomically, read-only (0400) and as the session's own uid.
func (o sessionLoginOps) writeSessionCopy(home, path string, data []byte) error {
uid, err := o.sessionUID(home)
if err != nil {
return false, err
return err
}
ctx, cancel := context.WithTimeout(context.Background(), userSessionCmdTimeout)
defer cancel()
// The script writes nothing to its output, so the output is safe to keep
// in the error.
if out, err := o.priv(ctx, userSessionSpec(uid), bytes.NewReader(copyBytes), "sh", "-c", userSessionCopyLoginScript, "sh", path); err != nil {
return false, outputErr("writing the session's login copy", err, out)
if out, err := o.priv(ctx, userSessionSpec(uid), bytes.NewReader(data), "sh", "-c", userSessionCopyLoginScript, "sh", path); err != nil {
return outputErr("writing the session's login copy", err, out)
}
return true, nil
return nil
}

// fileModTime is the modification time of path, zero when it cannot be read.
Expand Down
Loading
Loading