diff --git a/internal/provider/claude_dirs.go b/internal/provider/claude_dirs.go index 8f99dcd4b..4944f54b4 100644 --- a/internal/provider/claude_dirs.go +++ b/internal/provider/claude_dirs.go @@ -283,6 +283,7 @@ func claudeSavedDir(user string) (string, error) { return "", errors.New(why) } if has { + claudeOnboarded(dir, nil) return dir, nil } c, _ := parseClaudeCredentials(ls[i].Auth) @@ -297,17 +298,56 @@ func claudeSavedDir(user string) (string, error) { return "", err } // who it is, as Claude Code keeps it beside a sign-in it made - profile := filepath.Join(dir, ".claude.json") - if _, err := os.Stat(profile); os.IsNotExist(err) && len(ls[i].Profile) > 0 { - var acct map[string]any - if json.Unmarshal(ls[i].Profile, &acct) == nil { - pb, _ := json.MarshalIndent(map[string]any{"oauthAccount": acct, "hasCompletedOnboarding": true}, "", " ") - _ = writePrivate(profile, append(pb, '\n')) - } + var acct map[string]any + if len(ls[i].Profile) > 0 && json.Unmarshal(ls[i].Profile, &acct) != nil { + acct = nil } + claudeOnboarded(dir, acct) return dir, nil } +// claudeOnboarded marks Claude Code's onboarding done in config directory +// dir's .claude.json, as Claude Code does once a person has gone through +// it. Without the mark its interactive mode starts on its first-run +// screens, and from 2.1.316 on one that asks for a login method beside a +// sign-in that works (Jaxxxxnn on X), which a run can only end on. A +// directory -p alone ran in, or one made before magpie wrote the mark, +// has a .claude.json without it, so the mark is added to what is there, +// every other key kept. A new file gets the account's profile (acct) too. +// A file that can't be read or parsed, which Claude Code may be writing, +// is left as it is. +func claudeOnboarded(dir string, acct map[string]any) { + profile := filepath.Join(dir, ".claude.json") + b, err := os.ReadFile(profile) + if os.IsNotExist(err) { + m := map[string]any{"hasCompletedOnboarding": true} + if acct != nil { + m["oauthAccount"] = acct + } + pb, _ := json.MarshalIndent(m, "", " ") + _ = writePrivate(profile, append(pb, '\n')) + return + } + if err != nil { + return + } + var m map[string]any + d := json.NewDecoder(bytes.NewReader(b)) + d.UseNumber() // Claude Code's numbers go back as they were written + if d.Decode(&m) != nil || m == nil { + return + } + if done, _ := m["hasCompletedOnboarding"].(bool); done { + return + } + m["hasCompletedOnboarding"] = true + pb, err := json.MarshalIndent(m, "", " ") + if err != nil { + return + } + _ = writePrivate(profile, append(pb, '\n')) +} + // syncClaudeDir brings the saved Claude login l up to what Claude Code // keeps in the account's config directory: the sign-in it refreshed to // there, or, when it emptied that sign-in, Anthropic's refusal of the one diff --git a/internal/provider/claude_onboard_test.go b/internal/provider/claude_onboard_test.go new file mode 100644 index 000000000..ced57f197 --- /dev/null +++ b/internal/provider/claude_onboard_test.go @@ -0,0 +1,112 @@ +package provider + +import ( + "encoding/json" + "os" + "path/filepath" + "strings" + "testing" +) + +// Jaxxxxnn on X (magpie 0.1.2669, Claude Code 2.1.316): a saved account's +// config directory had a .claude.json without hasCompletedOnboarding, so +// Claude Code's interactive mode asked for a login method beside a sign-in +// that works, and every run and usage read there ended. The mark is added +// to the file that is there, every other key kept, both where the sign-in +// is put in and where it is there already. +func TestClaudeSavedDirMarksOnboardingDone(t *testing.T) { + claudeThree(t) + var users []string + for _, l := range Logins("claude") { + if !l.Active { + users = append(users, l.User) + } + } + if len(users) < 2 { + t.Fatalf("%d saved accounts beside the active one, want 2", len(users)) + } + read := func(dir string) map[string]any { + t.Helper() + b, err := os.ReadFile(filepath.Join(dir, ".claude.json")) + if err != nil { + t.Fatal(err) + } + var m map[string]any + if err := json.Unmarshal(b, &m); err != nil { + t.Fatalf("%v: %s", err, b) + } + return m + } + // a .claude.json without the mark, as -p or an older magpie left it + stale := `{"numStartups": 3, "firstStartTime": "2026-10-30T01:02:03.000Z", "userID": "abc", "big": 12345678901234567890}` + + // the sign-in not put in yet: the profile file there is kept + fresh := claudeAccountDir(users[0]) + if err := os.MkdirAll(fresh, 0o700); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(fresh, ".claude.json"), []byte(stale), 0o600); err != nil { + t.Fatal(err) + } + // the sign-in there already, put in by an earlier run + had, err := claudeSavedDir(users[1]) + if err != nil { + t.Fatal(err) + } + if m := read(had); m["hasCompletedOnboarding"] != true || m["oauthAccount"] == nil { + t.Fatalf("a new directory's .claude.json is %v, want the mark and the profile", m) + } + if err := os.WriteFile(filepath.Join(had, ".claude.json"), []byte(stale), 0o600); err != nil { + t.Fatal(err) + } + + for _, u := range users[:2] { + dir, err := claudeSavedDir(u) + if err != nil { + t.Fatal(err) + } + m := read(dir) + if m["hasCompletedOnboarding"] != true { + t.Fatalf("%s: onboarding not marked done: %v", u, m) + } + if m["numStartups"] != float64(3) || m["userID"] != "abc" || m["firstStartTime"] != "2026-10-30T01:02:03.000Z" { + t.Fatalf("%s: Claude Code's own keys not kept: %v", u, m) + } + if b, _ := os.ReadFile(filepath.Join(dir, ".claude.json")); !strings.Contains(string(b), "12345678901234567890") { + t.Fatalf("%s: a number Claude Code wrote was rewritten: %s", u, b) + } + } + + // a file being written (not JSON yet) is left as it is + half := `{"numStartups": 3, "userI` + if err := os.WriteFile(filepath.Join(had, ".claude.json"), []byte(half), 0o600); err != nil { + t.Fatal(err) + } + if _, err := claudeSavedDir(users[1]); err != nil { + t.Fatal(err) + } + if b, _ := os.ReadFile(filepath.Join(had, ".claude.json")); string(b) != half { + t.Fatalf("a half-written .claude.json was written over: %s", b) + } +} + +// A setup-token account's directory made before the mark was written, or +// one -p ran in, gets the mark too. +func TestClaudeTokenDirMarksOnboardingDone(t *testing.T) { + claudeHome(t) + dir := claudeAccountDir("token@x.com") + if err := os.MkdirAll(dir, 0o700); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(dir, ".claude.json"), []byte(`{"numStartups": 1}`), 0o600); err != nil { + t.Fatal(err) + } + if _, err := claudeTokenDir("token@x.com"); err != nil { + t.Fatal(err) + } + b, _ := os.ReadFile(filepath.Join(dir, ".claude.json")) + var m map[string]any + if err := json.Unmarshal(b, &m); err != nil || m["hasCompletedOnboarding"] != true || m["numStartups"] != float64(1) { + t.Fatalf(".claude.json is %s (%v), want the mark beside numStartups", b, err) + } +} diff --git a/internal/provider/claude_setup_token.go b/internal/provider/claude_setup_token.go index b345600bd..1d90bf471 100644 --- a/internal/provider/claude_setup_token.go +++ b/internal/provider/claude_setup_token.go @@ -165,9 +165,6 @@ func claudeTokenDir(user string) (string, error) { if err := os.Remove(filepath.Join(dir, ".credentials.json")); err != nil && !os.IsNotExist(err) { return "", fmt.Errorf("a sign-in left in %s: %w", dir, err) } - profile := filepath.Join(dir, ".claude.json") - if _, err := os.Stat(profile); os.IsNotExist(err) { - _ = writePrivate(profile, []byte("{\n \"hasCompletedOnboarding\": true\n}\n")) - } + claudeOnboarded(dir, nil) return dir, nil }