diff --git a/changelog.d/fixed-claude-bridge-keep-unreadable-credential.md b/changelog.d/fixed-claude-bridge-keep-unreadable-credential.md new file mode 100644 index 0000000000..10c03cf168 --- /dev/null +++ b/changelog.d/fixed-claude-bridge-keep-unreadable-credential.md @@ -0,0 +1 @@ +- Claude credential bridge: on a per-UID hive the hive server cannot read the private `0600` file an agent's token refresh leaves in place of its credential link, and the repair took that read error for "no token". It moved the agent's fresh token aside and relinked the agent to the shared login, whose grant the refresh had just rotated, so Claude agents logged out about 8 hours after each sign-in. At launch and on the credential watchdog, the repair now leaves a private file it cannot read where it is and logs one WARN per agent and file version with the path and errno; the agent keeps its private copy until the file is readable or the operator signs in again. The repair that follows a dashboard sign-in still moves such a file aside and relinks the agent, because the new shared login is newer than any private copy. `claude.WriteCredentials` now refuses a credential with no access token (#17253, #17590). diff --git a/src/crux/agent/claude_credential_bridge.go b/src/crux/agent/claude_credential_bridge.go index 4ca94d05f0..ca5f341690 100644 --- a/src/crux/agent/claude_credential_bridge.go +++ b/src/crux/agent/claude_credential_bridge.go @@ -59,6 +59,25 @@ import ( // to /data/home/.claude): that "private file" is the fleet's login. Such an // agent is skipped with one warning; its next launch retires the directory // link (setupPerAgentCLIHome) before the repair runs. +// +// A private file this process cannot read. In a per-UID deployment the hive +// server runs as its own uid and the private file is the AGENT's, mode 0600, +// so reading it fails. That read error used to look exactly like "no token": +// the shared file was kept, the agent's file was moved aside, and the agent +// was relinked to the shared login. But the refresh that wrote the private +// file also rotated the grant the shared file still holds, so the repair was +// moving aside the only fresh token in the fleet and putting the agent back +// on a dead one. Now, on the launch and watchdog triggers, a private file the +// repair cannot read (any read error other than "not there") is left exactly +// where it is and the link is not restored. The hive log says so once per +// agent and file version (WARN, with the path and errno), and the dashboard +// keeps showing the bridge as broken. The agent keeps its private copy until +// the file is readable or the operator signs in again. +// +// The sign-in trigger is the exception. A dashboard sign-in has just written +// a new shared login, which is newer than any private copy by construction, +// so on that trigger an unreadable private file is still moved aside and the +// agent relinked to the shared login, as before. // Repair triggers, recorded in the audit detail and the log. const ( @@ -79,9 +98,13 @@ const claudeSharedCredentialMode = 0o660 // Test seams. chownClaudeCredential applies the previous shared file's owner // to the promoted replacement; it is a seam so tests run without root or real // agent uids. claudeBridgeNow stamps the moved-aside file names. +// claudeBridgeReadTokens reads a credential for the repair; tests replace it to +// stand in for another uid's 0600 file, which a test running as one user (or +// as root) cannot produce for real. var ( - chownClaudeCredential = os.Chown - claudeBridgeNow = time.Now + chownClaudeCredential = os.Chown + claudeBridgeNow = time.Now + claudeBridgeReadTokens = claude.ReadTokens ) // claudeBridgeMu serializes repairs: the launch path, the watchdog and the @@ -193,6 +216,18 @@ func claudeBridgeLinkIsTarget(link, target string) (bool, error) { return os.SameFile(li, ti), nil } +// errClaudeBridgeAgentUnreadable: the agent's private credential exists but +// this process cannot read it (in a per-UID deployment, the agent's 0600 file +// and the hive server's own uid). Its token cannot be judged, so on the launch +// and watchdog triggers the repair leaves it exactly where it is. +var errClaudeBridgeAgentUnreadable = errors.New("agent's private credential cannot be read by the hive process") + +// claudeBridgeUnreadableWarned holds, per credential link, the modification +// time of the unreadable private file last logged, so the watchdog logs once +// per agent and file version instead of every cycle. A new refresh (a new +// mtime) is logged again. +var claudeBridgeUnreadableWarned sync.Map + // preferAgentClaudeCredential decides which credential the shared file keeps. // See the file comment for the rule. func preferAgentClaudeCredential(agent, shared *claude.OAuthTokens, now time.Time) bool { @@ -210,8 +245,10 @@ func preferAgentClaudeCredential(agent, shared *claude.OAuthTokens, now time.Tim // repair (a symlink, an absent link, a directory, which is not this failure, // or a link or target directory that cannot be resolved). It returns an error // wrapping errClaudeBridgeLinkIsTarget, and touches nothing, when the link -// path resolves to the shared credential itself. -func (m *Manager) repairClaudeCredentialBridge(agentName, link, target string) (*claudeBridgeRepair, error) { +// path resolves to the shared credential itself, and, unless trigger is +// claudeBridgeTriggerSignIn, one wrapping errClaudeBridgeAgentUnreadable, +// again touching nothing, when this process cannot read the private file. +func (m *Manager) repairClaudeCredentialBridge(agentName, link, target, trigger string) (*claudeBridgeRepair, error) { claudeBridgeMu.Lock() defer claudeBridgeMu.Unlock() @@ -235,6 +272,21 @@ func (m *Manager) repairClaudeCredentialBridge(agentName, link, target string) ( if same { return nil, fmt.Errorf("%w: %s", errClaudeBridgeLinkIsTarget, link) } + + // Never move aside a file whose token cannot be judged, except right + // after a dashboard sign-in (see the file comment). An unreadable private + // copy is, in a per-UID deployment, the normal case right after a + // refresh, and it may be the only fresh token in the fleet. + keepUnreadable := trigger != claudeBridgeTriggerSignIn + if keepUnreadable { + if _, rerr := claudeBridgeReadTokens(link); rerr != nil { + if errors.Is(rerr, fs.ErrNotExist) { + // Gone since the Lstat: the CLI is mid-rename. Next cycle. + return nil, nil + } + return nil, fmt.Errorf("%w: %w", errClaudeBridgeAgentUnreadable, rerr) + } + } now := claudeBridgeNow() // Move the private file aside FIRST. After the rename the CLI can no @@ -244,9 +296,24 @@ func (m *Manager) repairClaudeCredentialBridge(agentName, link, target string) ( if err != nil { return nil, fmt.Errorf("move private credential aside: %w", err) } + agentTokens, rerr := claudeBridgeReadTokens(staleAgent) + if rerr != nil && keepUnreadable { + // The file renamed is not the one the check above read (the CLI + // replaced it in between) and this one cannot be read either. Put it + // back unless the CLI has already written yet another file there. + if _, lerr := os.Lstat(link); errors.Is(lerr, fs.ErrNotExist) { + if berr := os.Rename(staleAgent, link); berr != nil { + m.logger.Error("claude credential bridge: an unreadable private credential could not be put back; it is kept at the stale path", + "agent", agentName, "stale", staleAgent, "error", berr) + } + } + return nil, fmt.Errorf("%w: %w", errClaudeBridgeAgentUnreadable, rerr) + } + // On the sign-in trigger an unreadable private file reads as no token + // (agentTokens is nil), so the new shared login is kept. rep := &claudeBridgeRepair{agent: agentName, kept: "shared", staleAgentPath: staleAgent} - if preferAgentClaudeCredential(claude.LoadTokens(staleAgent), claude.LoadTokens(target), now) { + if preferAgentClaudeCredential(agentTokens, claude.LoadTokens(target), now) { staleShared, perr := promoteClaudeCredential(staleAgent, target, now) if perr != nil { // Put the private file back: the agent's copy was the only usable @@ -373,7 +440,7 @@ func preserveClaudeCredential(path string, now time.Time) (string, error) { // outcome. It is the single entry point the launch path, the watchdog and the // sign-in hook share, so all three log and audit identically. func (m *Manager) rebridgeClaudeCredential(agentName, link, target, trigger string) *claudeBridgeRepair { - rep, err := m.repairClaudeCredentialBridge(agentName, link, target) + rep, err := m.repairClaudeCredentialBridge(agentName, link, target, trigger) if errors.Is(err, errClaudeBridgeLinkIsTarget) { // The legacy layout: skipped, and said once per agent rather than on // every watchdog cycle. Not audited (nothing changed). @@ -386,6 +453,11 @@ func (m *Manager) rebridgeClaudeCredential(agentName, link, target, trigger stri // Any other outcome clears the mark, so a legacy layout that shows up // again later is logged again. claudeBridgeLegacyWarned.Delete(link) + if errors.Is(err, errClaudeBridgeAgentUnreadable) { + m.logClaudeBridgeUnreadable(agentName, link, trigger, err) + return nil + } + claudeBridgeUnreadableWarned.Delete(link) if err != nil { // Logged, not audited: a repair that keeps failing (an unprivileged // hive that cannot enter the agent's 0700 home) would otherwise add an @@ -415,6 +487,27 @@ func (m *Manager) rebridgeClaudeCredential(agentName, link, target, trigger stri return rep } +// logClaudeBridgeUnreadable says, once per agent and private-file version, +// that the repair left an agent on a private credential it could not read. +// WARN, like the bridge's other lines: the agent keeps working on its own +// copy, and nothing changed, so it is not audited. +func (m *Manager) logClaudeBridgeUnreadable(agentName, link, trigger string, err error) { + var stamp int64 + if info, lerr := os.Lstat(link); lerr == nil { + stamp = info.ModTime().UnixNano() + } + if prev, loaded := claudeBridgeUnreadableWarned.Swap(link, stamp); loaded && prev == stamp { + return + } + errno := "" + var en syscall.Errno + if errors.As(err, &en) { + errno = fmt.Sprintf("%d (%s)", int(en), en.Error()) + } + m.logger.Warn("claude credential bridge: the hive process cannot read this agent's private credential, so its token cannot be judged; leaving the agent on its private copy (not moved aside, not re-linked) until the file is readable or the operator signs in again from the dashboard", + "agent", agentName, "path", link, "trigger", trigger, "errno", errno, "error", err) +} + // RepairClaudeCredentialBridges checks every per-agent Claude credential // bridge and repairs each one a regular file has replaced. It returns the // names of the agents it re-bridged, sorted. Agents are snapshotted under m.mu diff --git a/src/crux/agent/claude_credential_bridge_internal_test.go b/src/crux/agent/claude_credential_bridge_internal_test.go index 9d4d9ce5e3..391a4a8ed2 100644 --- a/src/crux/agent/claude_credential_bridge_internal_test.go +++ b/src/crux/agent/claude_credential_bridge_internal_test.go @@ -3,6 +3,7 @@ package agent import ( "bytes" "errors" + "io/fs" "log/slog" "os" "path/filepath" @@ -213,7 +214,7 @@ func TestRepairClaudeCredentialBridge_LegacyDirLinkUsableSharedUntouched(t *test sharedJSON := fakeClaudeCredential("fake-shared-access", time.Now().Add(8*time.Hour), "fake-shared-refresh") link, target, before := legacyDirLinkFixture(t, shared, "reviewer", 2005, sharedJSON) - rep, err := m.repairClaudeCredentialBridge("reviewer", link, target) + rep, err := m.repairClaudeCredentialBridge("reviewer", link, target, claudeBridgeTriggerWatchdog) if rep != nil || !errors.Is(err, errClaudeBridgeLinkIsTarget) { t.Fatalf("repair = (%+v, %v), want (nil, errClaudeBridgeLinkIsTarget)", rep, err) } @@ -237,7 +238,7 @@ func TestRepairClaudeCredentialBridge_LegacyDirLinkTokenlessSharedNotSelfLinked( const sharedJSON = `{}` link, target, before := legacyDirLinkFixture(t, shared, "scanner", 2003, sharedJSON) - if _, err := m.repairClaudeCredentialBridge("scanner", link, target); !errors.Is(err, errClaudeBridgeLinkIsTarget) { + if _, err := m.repairClaudeCredentialBridge("scanner", link, target, claudeBridgeTriggerWatchdog); !errors.Is(err, errClaudeBridgeLinkIsTarget) { t.Fatalf("repair error = %v, want errClaudeBridgeLinkIsTarget", err) } assertSharedCredentialUntouched(t, target, before, sharedJSON) @@ -291,7 +292,7 @@ func TestRepairClaudeCredentialBridge_RefusesLinkEqualToTarget(t *testing.T) { t.Fatal(err) } - rep, err := m.repairClaudeCredentialBridge("coder", target, target) + rep, err := m.repairClaudeCredentialBridge("coder", target, target, claudeBridgeTriggerWatchdog) if rep != nil || !errors.Is(err, errClaudeBridgeLinkIsTarget) { t.Fatalf("repair = (%+v, %v), want (nil, errClaudeBridgeLinkIsTarget)", rep, err) } @@ -335,3 +336,211 @@ func TestSetupInteractiveHome_LegacyDirLinkLaunchRetiresBeforeRebridge(t *testin } assertBridgeRestored(t, link, target) } + +// withUnreadableClaudeCredential makes the repair's credential reader fail +// with EACCES for every path unreadable(path) accepts, as the hive server's +// uid does on an agent's 0600 file in a per-UID deployment. A test cannot +// produce that for real without root and a second uid. +func withUnreadableClaudeCredential(t *testing.T, unreadable func(path string) bool) { + t.Helper() + orig := claudeBridgeReadTokens + claudeBridgeReadTokens = func(path string) (*claude.OAuthTokens, error) { + if unreadable(path) { + return nil, &fs.PathError{Op: "open", Path: path, Err: syscall.EACCES} + } + return orig(path) + } + t.Cleanup(func() { claudeBridgeReadTokens = orig }) +} + +// TestRepairClaudeCredentialBridge_UnreadablePrivateCredentialLeftInPlace: an +// agent's refresh left a fresh 0600 private file (the only live token: its +// refresh rotated the grant the shared file holds) that the hive server cannot +// read. On the v6 tip the read error looked like "no token": the file was +// moved aside, kept=shared, and the agent was relinked to the dead grant. Now, +// on the watchdog trigger, nothing moves, nothing is linked, the dashboard +// still reports the bridge as broken, and the WARN is logged once per file +// version rather than every watchdog cycle. +func TestRepairClaudeCredentialBridge_UnreadablePrivateCredentialLeftInPlace(t *testing.T) { + shared := withSharedAgentHome(t) + m, sink := testManagerWithSink(t) + var logs bytes.Buffer + m.logger = slog.New(slog.NewTextHandler(&logs, nil)) + now := time.Now() + sharedJSON := fakeClaudeCredential("fake-rotated-access", now.Add(3*time.Minute), "fake-rotated-refresh") + agentJSON := fakeClaudeCredential("fake-fresh-access", now.Add(8*time.Hour), "fake-fresh-refresh") + link, target := brokenBridgeFixture(t, shared, "quality", sharedJSON, agentJSON) + agentDir := filepath.Dir(link) + withUnreadableClaudeCredential(t, func(path string) bool { return filepath.Dir(path) == agentDir }) + + rep, err := m.repairClaudeCredentialBridge("quality", link, target, claudeBridgeTriggerWatchdog) + if rep != nil || !errors.Is(err, errClaudeBridgeAgentUnreadable) || !errors.Is(err, fs.ErrPermission) { + t.Fatalf("repair = (%+v, %v), want (nil, errClaudeBridgeAgentUnreadable wrapping EACCES)", rep, err) + } + for cycle := 0; cycle < 3; cycle++ { + if got := m.rebridgeClaudeCredential("quality", link, target, claudeBridgeTriggerWatchdog); got != nil { + t.Fatalf("cycle %d: rebridge = %+v, want nil (left in place)", cycle, got) + } + } + + info, err := os.Lstat(link) + if err != nil || !info.Mode().IsRegular() { + t.Fatalf("private credential must stay a regular file at the link path: info=%v err=%v", info, err) + } + if got := readFileString(t, link); got != agentJSON { + t.Errorf("private credential content changed: %s", got) + } + if got := staleFiles(t, agentDir); len(got) != 0 { + t.Errorf("an unreadable private credential was moved aside: %v", got) + } + if got := readFileString(t, target); got != sharedJSON { + t.Errorf("shared credential changed: %s", got) + } + if got := staleFiles(t, filepath.Dir(target)); len(got) != 0 { + t.Errorf("shared credential was moved aside: %v", got) + } + if !claudeCredentialBridgeBroken("quality", 2007, "claude") { + t.Error("the dashboard must keep reporting the bridge as broken while the private copy is in place") + } + if ev := sink.find("claude_credential_rebridged"); ev != nil { + t.Errorf("nothing changed, so nothing may be audited as a re-bridge: %+v", ev) + } + const msg = "cannot read this agent's private credential" + if n := strings.Count(logs.String(), msg); n != 1 { + t.Fatalf("unreadable-credential warning logged %d times over 3 cycles, want 1:\n%s", n, logs.String()) + } + if !strings.Contains(logs.String(), "level=WARN") || !strings.Contains(logs.String(), "permission denied") || + !strings.Contains(logs.String(), link) { + t.Errorf("log must be a WARN naming the path and the errno:\n%s", logs.String()) + } + if strings.Contains(logs.String(), "fake-") { + t.Errorf("log carries token material:\n%s", logs.String()) + } + + // The CLI refreshes again: a new file version is worth one more line. + later := time.Now().Add(time.Hour) + if err := os.Chtimes(link, later, later); err != nil { + t.Fatal(err) + } + m.rebridgeClaudeCredential("quality", link, target, claudeBridgeTriggerWatchdog) + if n := strings.Count(logs.String(), msg); n != 2 { + t.Errorf("a new private file version must be logged again: got %d lines, want 2", n) + } +} + +// TestRepairClaudeCredentialBridge_UnreadableAfterMoveIsPutBack: the file at +// the link was readable when checked, but the one actually renamed (the CLI +// replaced it in between) is not. It is put back at the link path, not left +// at a stale name with the agent relinked to the shared file. +func TestRepairClaudeCredentialBridge_UnreadableAfterMoveIsPutBack(t *testing.T) { + shared := withSharedAgentHome(t) + m, _ := testManagerWithSink(t) + now := time.Now() + sharedJSON := fakeClaudeCredential("fake-shared-access", now.Add(time.Hour), "fake-shared-refresh") + agentJSON := fakeClaudeCredential("fake-fresh-access", now.Add(8*time.Hour), "fake-fresh-refresh") + link, target := brokenBridgeFixture(t, shared, "scanner", sharedJSON, agentJSON) + withUnreadableClaudeCredential(t, func(path string) bool { + return strings.Contains(filepath.Base(path), claudeCredentialStaleInfix) + }) + + rep, err := m.repairClaudeCredentialBridge("scanner", link, target, claudeBridgeTriggerWatchdog) + if rep != nil || !errors.Is(err, errClaudeBridgeAgentUnreadable) { + t.Fatalf("repair = (%+v, %v), want (nil, errClaudeBridgeAgentUnreadable)", rep, err) + } + info, err := os.Lstat(link) + if err != nil || !info.Mode().IsRegular() || readFileString(t, link) != agentJSON { + t.Fatalf("private credential must be back at the link path: info=%v err=%v", info, err) + } + if got := staleFiles(t, filepath.Dir(link)); len(got) != 0 { + t.Errorf("nothing may stay at a stale name: %v", got) + } + if got := readFileString(t, target); got != sharedJSON { + t.Errorf("shared credential changed: %s", got) + } +} + +// TestRepairClaudeCredentialBridges_UnreadablePrivateCredentialByTrigger: the +// launch and watchdog triggers leave an unreadable private file in place; the +// sign-in trigger, which follows a new shared login that is newer than any +// private copy, still moves it aside and relinks the agent to the shared +// login. On the v6 tip every trigger moved it aside. +func TestRepairClaudeCredentialBridges_UnreadablePrivateCredentialByTrigger(t *testing.T) { + cases := []struct { + trigger string + wantRepair bool + wantInPlace bool + }{ + {claudeBridgeTriggerWatchdog, false, true}, + {claudeBridgeTriggerLaunch, false, true}, + {claudeBridgeTriggerSignIn, true, false}, + } + for _, c := range cases { + t.Run(c.trigger, func(t *testing.T) { + shared := withSharedAgentHome(t) + m, sink := testManagerWithSink(t) + m.mu.Lock() + m.agents["builder"] = &AgentProcess{Name: "builder", UID: 2008, Config: config.AgentConfig{Backend: "claude"}} + m.mu.Unlock() + now := time.Now() + sharedJSON := fakeClaudeCredential("fake-signin-access", now.Add(8*time.Hour), "fake-signin-refresh") + agentJSON := fakeClaudeCredential("fake-private-access", now.Add(time.Hour), "fake-private-refresh") + link, target := brokenBridgeFixture(t, shared, "builder", sharedJSON, agentJSON) + agentDir := filepath.Dir(link) + withUnreadableClaudeCredential(t, func(path string) bool { return filepath.Dir(path) == agentDir }) + + got := m.RepairClaudeCredentialBridges(c.trigger) + if c.wantRepair != (len(got) == 1 && got[0] == "builder") { + t.Fatalf("RepairClaudeCredentialBridges(%q) = %v, want repair=%v", c.trigger, got, c.wantRepair) + } + if got := readFileString(t, target); got != sharedJSON { + t.Errorf("shared credential changed: %s", got) + } + stale := staleFiles(t, agentDir) + if c.wantInPlace { + info, err := os.Lstat(link) + if err != nil || !info.Mode().IsRegular() || readFileString(t, link) != agentJSON { + t.Fatalf("private credential must stay at the link path: info=%v err=%v", info, err) + } + if len(stale) != 0 { + t.Errorf("an unreadable private credential was moved aside on %s: %v", c.trigger, stale) + } + return + } + assertBridgeRestored(t, link, target) + if len(stale) != 1 || readFileString(t, stale[0]) != agentJSON { + t.Fatalf("the private credential must be kept at one stale name, got %v", stale) + } + if ev := sink.find("claude_credential_rebridged"); ev == nil || ev.Fields["trigger"] != "sign-in" || ev.Fields["kept"] != "shared" { + t.Errorf("audit entry = %+v, want trigger=sign-in kept=shared", ev) + } + }) + } +} + +// TestReloadClaudeToken_SignInRebridgesUnreadablePrivateCredential drives the +// sign-in exception through the path a dashboard sign-in takes: with a usable +// shared login on file, ReloadClaudeToken relinks an agent whose private file +// the hive server cannot read. +func TestReloadClaudeToken_SignInRebridgesUnreadablePrivateCredential(t *testing.T) { + shared := withSharedAgentHome(t) + m, sink := testManagerWithSink(t) + m.mu.Lock() + m.agents["reviewer"] = &AgentProcess{Name: "reviewer", UID: 2005, Config: config.AgentConfig{Backend: "claude"}} + m.mu.Unlock() + now := time.Now() + link, target := brokenBridgeFixture(t, shared, "reviewer", + fakeClaudeCredential("fake-signin-access", now.Add(8*time.Hour), "fake-signin-refresh"), + fakeClaudeCredential("fake-private-access", now.Add(time.Hour), "fake-private-refresh")) + agentDir := filepath.Dir(link) + withUnreadableClaudeCredential(t, func(path string) bool { return filepath.Dir(path) == agentDir }) + + m.ReloadClaudeToken() + + assertBridgeRestored(t, link, target) + if got := staleFiles(t, agentDir); len(got) != 1 { + t.Errorf("the private credential must be moved aside, got %v", got) + } + if ev := sink.find("claude_credential_rebridged"); ev == nil || ev.Fields["trigger"] != "sign-in" || ev.Fields["kept"] != "shared" { + t.Errorf("audit entry = %+v, want trigger=sign-in kept=shared", ev) + } +} diff --git a/src/crux/claude/oauth.go b/src/crux/claude/oauth.go index 8b262a88fa..a58a20631c 100644 --- a/src/crux/claude/oauth.go +++ b/src/crux/claude/oauth.go @@ -6,6 +6,7 @@ import ( "crypto/sha256" "encoding/base64" "encoding/json" + "errors" "fmt" "io" "net/http" @@ -342,8 +343,20 @@ func ApplyProfile(tokens *OAuthTokens, prof Profile, fetchErr error, previous *O return tokens.SubscriptionType != "" || tokens.RateLimitTier != "" } -// WriteCredentials atomically writes tokens to the credentials file. +// ErrNoAccessToken is returned by WriteCredentials for a token set with no +// access token. Writing one would replace whatever login is on file with a +// signed-out credential. +var ErrNoAccessToken = errors.New("refusing to write a Claude credential with no access token") + +// WriteCredentials atomically writes tokens to the credentials file. It +// refuses a nil token set or one with an empty access token +// (ErrNoAccessToken), so no hive caller can replace the shared login with a +// signed-out credential; the dashboard sign-in, its only caller in the hive, +// never produces one (ExchangeCode rejects an empty access token). func WriteCredentials(tokens *OAuthTokens, path string) error { + if tokens == nil || tokens.AccessToken == "" { + return ErrNoAccessToken + } dir := filepath.Dir(path) if err := os.MkdirAll(dir, 0o2775); err != nil { return fmt.Errorf("create credentials dir: %w", err) @@ -366,18 +379,32 @@ func WriteCredentials(tokens *OAuthTokens, path string) error { } // LoadTokens parses the credentials file and returns the stored token set, or -// nil when the file is absent, malformed, or carries no token block. It never -// applies an expiry rule — callers decide what a stale token means to them. +// nil when the file is absent, unreadable, malformed, or carries no token +// block. It never applies an expiry rule — callers decide what a stale token +// means to them. Callers that must tell "this process cannot read the file" +// apart from "the file holds no usable token" use ReadTokens. func LoadTokens(path string) *OAuthTokens { + tokens, _ := ReadTokens(path) + return tokens +} + +// ReadTokens is LoadTokens with the read error kept. The error is non-nil +// only when the file could not be read at all (absent, or not readable by +// this process, e.g. EACCES on another uid's 0600 file); test it with +// errors.Is(err, fs.ErrNotExist) or fs.ErrPermission. A file that was read +// but is malformed, or carries no token block, returns (nil, nil): it was +// readable, and it holds nothing usable. The error never carries file +// content. +func ReadTokens(path string) (*OAuthTokens, error) { data, err := os.ReadFile(path) if err != nil { - return nil + return nil, err } var creds Credentials if err := json.Unmarshal(data, &creds); err != nil { - return nil + return nil, nil } - return creds.ClaudeAIOAuth + return creds.ClaudeAIOAuth, nil } // expired reports whether a millisecond epoch stamp is in the past. A zero or diff --git a/src/crux/claude/oauth_read_tokens_test.go b/src/crux/claude/oauth_read_tokens_test.go new file mode 100644 index 0000000000..a812371e50 --- /dev/null +++ b/src/crux/claude/oauth_read_tokens_test.go @@ -0,0 +1,77 @@ +package claude + +import ( + "errors" + "io/fs" + "os" + "path/filepath" + "testing" + "time" +) + +// TestReadTokensKeepsTheReadError: the credential bridge must tell "this +// process cannot read the file" apart from "the file holds no usable token". +// LoadTokens folds both into nil; ReadTokens keeps the read error and returns +// (nil, nil) for a file it read but could not use. +func TestReadTokensKeepsTheReadError(t *testing.T) { + dir := t.TempDir() + + if tok, err := ReadTokens(filepath.Join(dir, "absent.json")); tok != nil || !errors.Is(err, fs.ErrNotExist) { + t.Errorf("absent file: ReadTokens = (%v, %v), want (nil, ErrNotExist)", tok, err) + } + + // A directory cannot be read as a file, as root or not: the stand-in for + // a read error other than "not there" (EACCES needs a second uid). + if tok, err := ReadTokens(dir); tok != nil || err == nil || errors.Is(err, fs.ErrNotExist) { + t.Errorf("unreadable path: ReadTokens = (%v, %v), want (nil, a non-ErrNotExist error)", tok, err) + } + if LoadTokens(dir) != nil { + t.Error("LoadTokens must keep returning nil for an unreadable path") + } + + malformed := filepath.Join(dir, "malformed.json") + if err := os.WriteFile(malformed, []byte("not json"), 0o600); err != nil { + t.Fatal(err) + } + if tok, err := ReadTokens(malformed); tok != nil || err != nil { + t.Errorf("malformed file: ReadTokens = (%v, %v), want (nil, nil)", tok, err) + } + + good := filepath.Join(dir, "good.json") + exp := time.Now().Add(time.Hour).UnixMilli() + if err := WriteCredentials(&OAuthTokens{AccessToken: "fake-a", RefreshToken: "fake-r", ExpiresAt: exp}, good); err != nil { + t.Fatal(err) + } + if tok, err := ReadTokens(good); err != nil || tok == nil || tok.ExpiresAt != exp { + t.Errorf("good file: ReadTokens = (%+v, %v), want the stored tokens", tok, err) + } +} + +// TestWriteCredentialsRefusesSignedOutTokens: no hive writer may put a +// signed-out credential (no access token) over the login on file. +func TestWriteCredentialsRefusesSignedOutTokens(t *testing.T) { + path := filepath.Join(t.TempDir(), ".credentials.json") + const onFile = `{"claudeAiOauth":{"accessToken":"fake-live"}}` + if err := os.WriteFile(path, []byte(onFile), 0o660); err != nil { + t.Fatal(err) + } + for name, tok := range map[string]*OAuthTokens{ + "nil": nil, + "empty access token": {RefreshToken: "fake-r", ExpiresAt: time.Now().Add(time.Hour).UnixMilli()}, + "empty token set": {}, + } { + if err := WriteCredentials(tok, path); !errors.Is(err, ErrNoAccessToken) { + t.Errorf("%s: WriteCredentials error = %v, want ErrNoAccessToken", name, err) + } + } + got, err := os.ReadFile(path) + if err != nil { + t.Fatal(err) + } + if string(got) != onFile { + t.Errorf("the credential on file changed: %s", got) + } + if _, err := os.Stat(path + ".tmp"); !errors.Is(err, fs.ErrNotExist) { + t.Errorf("a refused write must not leave a temp file: %v", err) + } +} diff --git a/src/docs/agent-configuration.md b/src/docs/agent-configuration.md index af61e9df89..8555ec93b4 100644 --- a/src/docs/agent-configuration.md +++ b/src/docs/agent-configuration.md @@ -703,6 +703,10 @@ The repair never deletes a token: Each repair writes a `claude_credential_rebridged` Audit Log entry naming the agent, the trigger (`launch`, `watchdog` or `sign-in`), which credential was kept (`shared` or `agent`) and where the moved-aside file went. It never records token material. Until the repair runs, the agent card shows **credential bridge broken (private stale file)** instead of a generic "needs login". Moved-aside files are not cleaned up automatically; remove them by hand once you no longer need them. +The hive server can only judge a private file it can read. On a per-UID hive the server runs as its own uid, and the agent's `0600` file is not readable to it. At launch and on a watchdog cycle the repair then leaves that file where it is: it does not move it aside and does not restore the link. The agent keeps running on its private copy, which is the token it was already using and, right after a refresh, the newest one the fleet has (the refresh rotated the grant the shared file still holds). The card keeps showing the broken bridge, and the hive log has one `WARN` per agent and file version, naming the path and the errno: `claude credential bridge: the hive process cannot read this agent's private credential`. The agent stays on its private copy until the file becomes readable or you sign in again from the dashboard. + +A dashboard sign-in is the exception. It has just written a new shared login, which is newer than any private copy, so the repair that follows it still moves an unreadable private file aside and relinks the agent to the shared login. Signing in again therefore still brings every agent back onto one login. + The repair skips an agent whose whole `~/.claude` is still the old directory symlink to `/data/home/.claude`. That happens to an agent that has been paused or stopped since before the per-agent split, because only a launch replaces the directory link. Through that link the agent's `.credentials.json` is the shared login itself, so there is nothing private to move aside. The hive log says so once per agent (`claude credential bridge: this agent's .claude directory resolves to the shared login directory`), the card does not show a broken bridge, and the agent's next launch replaces the directory link with its own `~/.claude`. ### CLI auto-memory (`agents.cli_memory`) diff --git a/src/docs/agent-watchdog.md b/src/docs/agent-watchdog.md index 8b6ee4e26f..17917ade64 100644 --- a/src/docs/agent-watchdog.md +++ b/src/docs/agent-watchdog.md @@ -131,6 +131,8 @@ A regular file here, instead of a symlink to `/data/home/.claude/.credentials.js Hive repairs this on its own: at the agent's next launch, on the next credential-watchdog tick, and right after the next dashboard sign-in. It moves the private file aside as `.credentials.json.stale-`, keeps whichever token is newer and usable in the shared file, restores the symlink, and writes a `claude_credential_rebridged` Audit Log entry. See [When a Claude token refresh breaks the credential link](agent-configuration.md#when-a-claude-token-refresh-breaks-the-credential-link) for the rule. You do not need to move files by hand. If the card still shows the broken bridge after a watchdog cycle, the hive log has a `claude credential bridge: repair failed` warning that names the file and the error. +The hive log may instead say `claude credential bridge: the hive process cannot read this agent's private credential`. That means the hive server left the file alone because it cannot read it, so it cannot tell whether the file holds the newest token. The agent keeps working on its own copy. The next dashboard sign-in moves the file aside and relinks the agent to the shared login. + ## Configuration Everything lives under `governor.watchdog` and is optional; an absent block