Files
magpie/internal/appdir/claude_homes.go
T
yetone 26d1c5619a sessions: every Claude Code folder is read, not only one (@fivesmallq on X)
Accounts run with their own CLAUDE_CONFIG_DIR (~/.claude-work beside
~/.claude) had only one folder's sessions and token use counted. Claude
folders are now found as Codex's are (appdir.ClaudeHomes): the one in use,
siblings named claude*/.claude* (or one level down) that hold projects/, and
the folders of Claude accounts saved in magpie. The sessions list, calls
(Usage), traces, the read-permission check, Dirs and a deleted session's
companion folders (file-history etc., taken from the session's own folder)
all go through it. A session id in two folders is kept once.

No setting to add folders by hand and no GUI change: discovery needs no
configuration; a folder with another name is not found.

Run (mote): go test appdir, sessions, cli, usage ok; gofmt clean; go vet on
darwin, linux and windows for appdir and sessions. New tests
(TestClaudeHomesIn, TestCallsReadEveryClaudeFolder,
TestListReadsEveryClaudeFolderOnce) fail on the old code for the second
folder's sessions. Broke by hand: dropping the key dedupe in claudeFiles
still passes, since the list and calls dedupe by key later; it is kept as a
guard, not tested alone.
Not tried: real second Claude account, Windows/WSL, GUI.
2026-11-11 07:24:28 +08:00

81 lines
2.3 KiB
Go

package appdir
import (
"os"
"path/filepath"
"strings"
)
// ClaudeHomes are every Claude Code folder of the user's, ClaudeDir first.
// More than one Claude account is Claude Code run with CLAUDE_CONFIG_DIR set
// to a folder of each's (~/.claude-work beside ~/.claude), a session is
// written into the folder of the account it ran under, and magpie read only
// the one it runs with (@fivesmallq on X: sessions and token use counted one
// Claude folder). Nothing records those folders, so they are looked for as
// CodexHomes looks for Codex's: in the home folder, and beside ClaudeDir, a
// folder whose name starts with "claude" or ".claude", or one folder down in
// such a folder (~/.claude-profiles/work), that holds projects/; and the
// folders magpie runs a saved account's Claude Code in (claude-accounts).
func ClaudeHomes() []string {
home, _ := os.UserHomeDir()
return ClaudeHomesIn(home)
}
// ClaudeHomesIn is ClaudeHomes with home as the user's home folder.
func ClaudeHomesIn(home string) []string {
main := ClaudeDirIn(home)
out := []string{main}
seen := map[string]bool{sameDir(main): true}
add := func(dir string) {
if k := sameDir(dir); !seen[k] {
seen[k] = true
out = append(out, dir)
}
}
roots := []string{home}
if p := filepath.Dir(main); sameDir(p) != sameDir(home) {
roots = append(roots, p)
}
for _, root := range roots {
if root == "" {
continue
}
es, _ := os.ReadDir(root)
for _, e := range es {
name := strings.ToLower(strings.TrimPrefix(e.Name(), "."))
if !strings.HasPrefix(name, "claude") {
continue
}
dir := filepath.Join(root, e.Name())
if fi, err := os.Stat(dir); err != nil || !fi.IsDir() {
continue
}
if claudeHomeAt(dir) {
add(dir)
continue
}
subs, _ := os.ReadDir(dir)
for _, s := range subs {
if sub := filepath.Join(dir, s.Name()); claudeHomeAt(sub) {
add(sub)
}
}
}
}
// the folders of the Claude accounts saved in magpie
saved := filepath.Join(Config(), "claude-accounts")
es, _ := os.ReadDir(saved)
for _, e := range es {
if dir := filepath.Join(saved, e.Name()); claudeHomeAt(dir) {
add(dir)
}
}
return out
}
// claudeHomeAt says dir is a folder Claude Code has kept a session in.
func claudeHomeAt(dir string) bool {
fi, err := os.Stat(filepath.Join(dir, "projects"))
return err == nil && fi.IsDir()
}