diff --git a/docs/subsystems/gateway-sessions.md b/docs/subsystems/gateway-sessions.md index a0e309112..d4a4b3a0d 100644 --- a/docs/subsystems/gateway-sessions.md +++ b/docs/subsystems/gateway-sessions.md @@ -371,6 +371,16 @@ writes for a subagent a subagent started (`sessions.SubagentParent`, through to a file row keeps the gateway's own. Both were checked against Claude Code 2.1.296 spawning a subagent from a subagent. +A folder these readers go into may be a link: a symlink, or on Windows a +junction, which Go lists as irregular rather than as a folder or a symlink. +Each reader that picks the entries it goes into (a Claude Code, Qoder or +Cowork project folder, a session's own folder and a workflow run's, a Hermes +profile, an Antigravity CLI conversation) asks `dirent.IsFolder`, which +follows such an entry with `os.Stat`; none of them recurses through one. A +walk root that is a link is walked where it leads, its files still named +under the agent's folder (`walkLinked`, #2233). `TestIsFolder` and the +`*BehindALink` tests make real links, junctions on Windows (#2268). + Codex session identity is read from native thread metadata, rather than inferred from a shared session header or `Kind` (`x-openai-subagent`). The sessions API adds `role` (`main`, `subagent` or `unknown`), and when native metadata diff --git a/internal/agent/hermes.go b/internal/agent/hermes.go index aa295180d..05517a03c 100644 --- a/internal/agent/hermes.go +++ b/internal/agent/hermes.go @@ -29,6 +29,7 @@ import ( "regexp" "strings" + "github.com/yetone/magpie/internal/dirent" "github.com/yetone/magpie/internal/edit" ) @@ -209,7 +210,7 @@ func hermesProfiles(at place) []string { es, _ := os.ReadDir(ps) for _, e := range es { name := e.Name() - if !e.IsDir() || name == "default" || !hermesProfileID.MatchString(name) { + if !dirent.IsFolder(ps, e) || name == "default" || !hermesProfileID.MatchString(name) { continue } if _, err := os.Stat(filepath.Join(ps, ".deleted", name)); err == nil { diff --git a/internal/agent/omp.go b/internal/agent/omp.go index 4625e3a80..da9104d34 100644 --- a/internal/agent/omp.go +++ b/internal/agent/omp.go @@ -20,6 +20,7 @@ import ( "github.com/yetone/magpie/internal/appdir" "github.com/yetone/magpie/internal/catalog" + "github.com/yetone/magpie/internal/dirent" "github.com/yetone/magpie/internal/edit" "github.com/yetone/magpie/internal/gateway" "github.com/yetone/magpie/internal/provider" @@ -163,7 +164,7 @@ func ompProfiles(home string) []*Agent { var names []string for _, e := range entries { name := e.Name() - if !e.IsDir() || !ompProfileOK(name) { + if !dirent.IsFolder(filepath.Join(root, "profiles"), e) || !ompProfileOK(name) { continue } if filepath.Join(root, "profiles", name, "agent") == cur { diff --git a/internal/agent/profile_link_test.go b/internal/agent/profile_link_test.go new file mode 100644 index 000000000..ee111eaa6 --- /dev/null +++ b/internal/agent/profile_link_test.go @@ -0,0 +1,67 @@ +package agent + +import ( + "os" + "os/exec" + "path/filepath" + "runtime" + "testing" +) + +// moveBehindLink moves the folder at live elsewhere and leaves a link to it +// in its place: a junction on Windows, which Go lists as irregular rather +// than as a folder or a symlink, and a symlink elsewhere. +func moveBehindLink(t *testing.T, live string) { + t.Helper() + elsewhere := filepath.Join(t.TempDir(), filepath.Base(live)) + if err := os.Rename(live, elsewhere); err != nil { + t.Fatal(err) + } + if runtime.GOOS == "windows" { + if out, err := exec.Command("cmd", "/c", "mklink", "/J", live, elsewhere).CombinedOutput(); err != nil { + t.Fatalf("mklink /J: %v %s", err, out) + } + return + } + if err := os.Symlink(elsewhere, live); err != nil { + t.Fatal(err) + } +} + +// A Hermes profile folder that is a link is still a profile whose config +// magpie writes (#2268). +func TestHermesProfileBehindALink(t *testing.T) { + home := syncHome(t) + root := filepath.Join(home, ".hermes") + for _, p := range []string{"config.yaml", "profiles/work/config.yaml"} { + writeFile(t, filepath.Join(root, p), "model:\n default: x\n") + } + t.Setenv("HERMES_HOME", root) + if got := hermesProfiles(here(home)); len(got) != 2 { + t.Fatalf("profiles: %v", got) + } + moveBehindLink(t, filepath.Join(root, "profiles", "work")) + if got := hermesProfiles(here(home)); len(got) != 2 || got[1] != filepath.Join(root, "profiles", "work", "config.yaml") { + t.Fatalf("profiles behind a link: %v", got) + } +} + +// An omp profile folder that is a link is still a profile row (#2268). +func TestOmpProfileBehindALink(t *testing.T) { + home := syncHome(t) + ompProfileAt(t, home, "work", "b/model-b") + ids := func() map[string]bool { + got := map[string]bool{} + for _, a := range ompProfiles(home) { + got[a.ID] = true + } + return got + } + if got := ids(); !got["omp#work"] { + t.Fatalf("profiles %v", got) + } + moveBehindLink(t, filepath.Join(home, ".omp", "profiles", "work")) + if got := ids(); len(got) != 1 || !got["omp#work"] { + t.Fatalf("profiles behind a link %v", got) + } +} diff --git a/internal/dirent/dirent.go b/internal/dirent/dirent.go new file mode 100644 index 000000000..b8cdeb072 --- /dev/null +++ b/internal/dirent/dirent.go @@ -0,0 +1,31 @@ +// Package dirent decides whether an entry a reader lists is a folder to go +// into, links included. +package dirent + +import ( + "io/fs" + "os" + "path/filepath" +) + +// IsFolder is whether e, an entry listed in dir, is a folder to read into: +// a folder of its own, or a link that leads to one. os.ReadDir reports a +// symlink as ModeSymlink and, on Windows, a junction as ModeIrregular (Go +// 1.23 and later), and neither as IsDir, so a reader that goes into only +// IsDir() entries, or IsDir() and ModeSymlink ones, walks past a folder +// kept on another drive with a junction left where it was (#2268). Such an +// entry is followed with os.Stat. A link to a file, a broken link and +// anything else irregular are no folder. +// +// It follows only the entry it is asked about. A reader that recurses on +// it must keep its own record of the folders it has been in. +func IsFolder(dir string, e fs.DirEntry) bool { + if e.IsDir() { + return true + } + if e.Type()&(fs.ModeSymlink|fs.ModeIrregular) == 0 { + return false + } + fi, err := os.Stat(filepath.Join(dir, e.Name())) + return err == nil && fi.IsDir() +} diff --git a/internal/dirent/dirent_test.go b/internal/dirent/dirent_test.go new file mode 100644 index 000000000..1aab97b66 --- /dev/null +++ b/internal/dirent/dirent_test.go @@ -0,0 +1,71 @@ +package dirent + +import ( + "os" + "os/exec" + "path/filepath" + "runtime" + "testing" +) + +// link makes path a link to the folder target: a junction on Windows, the +// link an agent's folder kept on another drive has there, and a symlink +// elsewhere. +func link(t *testing.T, target, path string) { + t.Helper() + if runtime.GOOS == "windows" { + if out, err := exec.Command("cmd", "/c", "mklink", "/J", path, target).CombinedOutput(); err != nil { + t.Fatalf("mklink /J: %v %s", err, out) + } + return + } + if err := os.Symlink(target, path); err != nil { + t.Fatal(err) + } +} + +// A folder, and a link (a junction on Windows) to a folder, are folders to +// read into; a file, a link to a file and a broken link are not. +func TestIsFolder(t *testing.T) { + dir, elsewhere := t.TempDir(), t.TempDir() + if err := os.Mkdir(filepath.Join(dir, "own"), 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(dir, "file"), []byte("x"), 0o644); err != nil { + t.Fatal(err) + } + if err := os.Mkdir(filepath.Join(elsewhere, "kept"), 0o755); err != nil { + t.Fatal(err) + } + link(t, filepath.Join(elsewhere, "kept"), filepath.Join(dir, "linked")) + if err := os.Mkdir(filepath.Join(elsewhere, "gone"), 0o755); err != nil { + t.Fatal(err) + } + link(t, filepath.Join(elsewhere, "gone"), filepath.Join(dir, "broken")) + if err := os.Remove(filepath.Join(elsewhere, "gone")); err != nil { + t.Fatal(err) + } + want := map[string]bool{"own": true, "linked": true, "file": false, "broken": false} + if runtime.GOOS != "windows" { + if err := os.WriteFile(filepath.Join(elsewhere, "f"), []byte("x"), 0o644); err != nil { + t.Fatal(err) + } + if err := os.Symlink(filepath.Join(elsewhere, "f"), filepath.Join(dir, "tofile")); err != nil { + t.Fatal(err) + } + want["tofile"] = false + } + es, err := os.ReadDir(dir) + if err != nil { + t.Fatal(err) + } + got := map[string]bool{} + for _, e := range es { + got[e.Name()] = IsFolder(dir, e) + } + for name, w := range want { + if g, ok := got[name]; !ok || g != w { + t.Errorf("IsFolder(%s) = %v (listed %v), want %v", name, g, ok, w) + } + } +} diff --git a/internal/dirent/main_test.go b/internal/dirent/main_test.go new file mode 100644 index 000000000..9f1daa854 --- /dev/null +++ b/internal/dirent/main_test.go @@ -0,0 +1,10 @@ +package dirent + +import ( + "testing" + + "github.com/yetone/magpie/internal/testenv" +) + +// The tests run in a home of their own, never the user's real files. +func TestMain(m *testing.M) { testenv.Main(m) } diff --git a/internal/library/desktop_link_test.go b/internal/library/desktop_link_test.go new file mode 100644 index 000000000..2f90928bb --- /dev/null +++ b/internal/library/desktop_link_test.go @@ -0,0 +1,52 @@ +package library + +import ( + "os" + "os/exec" + "path/filepath" + "runtime" + "slices" + "testing" +) + +// cmdExe is found before a test's sandbox takes PATH away. +var cmdExe, _ = exec.LookPath("cmd") + +// folderLink makes path a link to the folder target: a junction on +// Windows, which Go lists as irregular rather than as a folder or a +// symlink, and a symlink elsewhere. +func folderLink(t *testing.T, target, path string) { + t.Helper() + if runtime.GOOS == "windows" { + if out, err := exec.Command(cmdExe, "/c", "mklink", "/J", path, target).CombinedOutput(); err != nil { + t.Fatalf("mklink /J: %v %s", err, out) + } + return + } + if err := os.Symlink(target, path); err != nil { + t.Fatal(err) + } +} + +// A Claude Desktop org's or account's skills-plugin folder that is a link +// (a junction on Windows) is still one of Desktop's skill roots (#2268). +func TestClaudeDesktopSkillRootsBehindALink(t *testing.T) { + sandbox(t) + dir := t.TempDir() + base := filepath.Join(dir, "local-agent-mode-sessions", "skills-plugin") + org := desktopAccount(t, dir, "org1", "acct1", `{"skills": []}`) + acct := desktopAccount(t, dir, "org2", "acct2", `{"skills": []}`) + if got := desktopSkillRoots(dir); !slices.Contains(got, org) || !slices.Contains(got, acct) { + t.Fatalf("roots %v, want %s and %s", got, org, acct) + } + for _, live := range []string{filepath.Join(base, "org1"), acct} { + elsewhere := filepath.Join(t.TempDir(), filepath.Base(live)) + if err := os.Rename(live, elsewhere); err != nil { + t.Fatal(err) + } + folderLink(t, elsewhere, live) + } + if got := desktopSkillRoots(dir); !slices.Contains(got, org) || !slices.Contains(got, acct) { + t.Fatalf("roots behind links %v, want %s and %s", got, org, acct) + } +} diff --git a/internal/library/desktop_skills.go b/internal/library/desktop_skills.go index 45d6e16fc..f5c332039 100644 --- a/internal/library/desktop_skills.go +++ b/internal/library/desktop_skills.go @@ -31,6 +31,7 @@ import ( "github.com/tidwall/gjson" "github.com/yetone/magpie/internal/desktopdir" + "github.com/yetone/magpie/internal/dirent" "github.com/yetone/magpie/internal/edit" ) @@ -51,13 +52,13 @@ func desktopSkillRoots(desktopDir string) []string { base := filepath.Join(d, "local-agent-mode-sessions", "skills-plugin") orgs, _ := os.ReadDir(base) for _, o := range orgs { - if !o.IsDir() { + if !dirent.IsFolder(base, o) { continue } accts, _ := os.ReadDir(filepath.Join(base, o.Name())) for _, a := range accts { p := filepath.Join(base, o.Name(), a.Name()) - if a.IsDir() && (isFile(filepath.Join(p, desktopManifest)) || isFile(filepath.Join(p, ".claude-plugin", "plugin.json"))) { + if dirent.IsFolder(filepath.Join(base, o.Name()), a) && (isFile(filepath.Join(p, desktopManifest)) || isFile(filepath.Join(p, ".claude-plugin", "plugin.json"))) { out = append(out, p) } } diff --git a/internal/sessions/calls_agy.go b/internal/sessions/calls_agy.go index 1d655e977..f5d21ca80 100644 --- a/internal/sessions/calls_agy.go +++ b/internal/sessions/calls_agy.go @@ -11,6 +11,8 @@ import ( "strconv" "strings" "time" + + "github.com/yetone/magpie/internal/dirent" ) // Antigravity CLI's (agy's) calls (#2097, parkavenue9639: a call agy made @@ -47,7 +49,7 @@ func agyCallFiles() []file { dirs, _ := os.ReadDir(filepath.Join(AgyDir(), "brain")) var out []file for _, d := range dirs { - if !d.IsDir() { + if !dirent.IsFolder(filepath.Join(AgyDir(), "brain"), d) { continue } p := filepath.Join(AgyDir(), "brain", d.Name(), ".system_generated", "logs", "transcript_full.jsonl") diff --git a/internal/sessions/hermes.go b/internal/sessions/hermes.go index ecd888b1a..1c1869660 100644 --- a/internal/sessions/hermes.go +++ b/internal/sessions/hermes.go @@ -15,6 +15,7 @@ import ( "time" "github.com/yetone/magpie/internal/appdir" + "github.com/yetone/magpie/internal/dirent" ) // Hermes stores canonical, non-overlapping input/cache buckets. Reasoning is @@ -34,7 +35,7 @@ func HermesDirs() []string { } dirs := []string{root} for _, e := range readDirectory(filepath.Join(root, "profiles")) { - if !strings.HasPrefix(e.Name(), ".") && (e.IsDir() || e.Type()&os.ModeSymlink != 0) { + if !strings.HasPrefix(e.Name(), ".") && dirent.IsFolder(filepath.Join(root, "profiles"), e) { dirs = append(dirs, filepath.Join(root, "profiles", e.Name())) } } diff --git a/internal/sessions/link_entry_test.go b/internal/sessions/link_entry_test.go new file mode 100644 index 000000000..b508884be --- /dev/null +++ b/internal/sessions/link_entry_test.go @@ -0,0 +1,151 @@ +package sessions + +import ( + "os" + "path/filepath" + "reflect" + "slices" + "sort" + "testing" + "time" +) + +// The tests below move a folder a reader goes into elsewhere and leave a +// link where it was: a junction on Windows, where Go lists one as +// irregular rather than as a folder or a symlink, and a symlink elsewhere +// (#2268, JasonBuildAI; its first three tests are the issue's). + +// ccFilePaths is every session file ccFiles found, by path: one that stops +// being listed, or is listed from somewhere else, stands out. +func ccFilePaths(fs []file) []string { + out := make([]string, 0, len(fs)) + for _, f := range fs { + out = append(out, f.path) + } + sort.Strings(out) + return out +} + +// moveBehindLink moves the folder at live elsewhere and leaves a link to it +// in its place. +func moveBehindLink(t *testing.T, live string) { + t.Helper() + elsewhere := filepath.Join(t.TempDir(), filepath.Base(live)) + if err := os.Rename(live, elsewhere); err != nil { + t.Fatal(err) + } + linkFolder(t, elsewhere, live) +} + +// A Claude Code project folder inside projects/ that is a link is read all +// the same: Sessions listed none of its sessions and Usage none of its +// calls. ccFiles reads Qoder's, Qoder CN's and Cowork's stores too. +func TestClaudeProjectFolderBehindALink(t *testing.T) { + claude, _ := setup(t) + before := ccFiles("claude", claude) + if len(before) == 0 { + t.Fatal("the fixture has no Claude session files") + } + listed := len(ListAgent("claude")) + moveBehindLink(t, filepath.Join(claude, "projects", "-work-app")) + + after := ccFiles("claude", claude) + if !reflect.DeepEqual(ccFilePaths(after), ccFilePaths(before)) { + t.Fatalf("Claude session files behind a link: %v, want %v", ccFilePaths(after), ccFilePaths(before)) + } + if got := len(ListAgent("claude")); got != listed { + t.Fatalf("Claude sessions behind a link: %d, want %d", got, listed) + } +} + +// A session's own folder, where its subagents live, being a link is read +// too. +func TestClaudeSessionFolderBehindALink(t *testing.T) { + claude, _ := setup(t) + before := ccFiles("claude", claude) + if len(before) == 0 { + t.Fatal("the fixture has no Claude session files") + } + moveBehindLink(t, filepath.Join(claude, "projects", "-work-app", "11111111-2222-3333-4444-555555555555")) + + after := ccFiles("claude", claude) + if !reflect.DeepEqual(ccFilePaths(after), ccFilePaths(before)) { + t.Fatalf("a Claude session folder behind a link: %v, want %v", ccFilePaths(after), ccFilePaths(before)) + } +} + +// A workflow run's folder (ultracode's agents) being a link is read too. +func TestClaudeWorkflowRunBehindALink(t *testing.T) { + claude, _ := setup(t) + subs := filepath.Join(claude, "projects", "-work-app", "11111111-2222-3333-4444-555555555555", "subagents") + b, err := os.ReadFile(filepath.Join(subs, "agent-a1.jsonl")) + if err != nil { + t.Fatal(err) + } + run := filepath.Join(subs, "workflows", "run-1") + if err := os.MkdirAll(run, 0o755); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(run, "agent-w1.jsonl"), b, 0o644); err != nil { + t.Fatal(err) + } + before := ccFilePaths(ccFiles("claude", claude)) + if !slices.Contains(before, filepath.Join(run, "agent-w1.jsonl")) { + t.Fatalf("the workflow's agent isn't read: %v", before) + } + moveBehindLink(t, run) + if after := ccFilePaths(ccFiles("claude", claude)); !reflect.DeepEqual(after, before) { + t.Fatalf("a workflow run behind a link: %v, want %v", after, before) + } +} + +// A Hermes profile folder that is a link is read too: HermesDirs skipped it +// and the profile's sessions were gone from Sessions and Usage. +func TestHermesProfileBehindALink(t *testing.T) { + setup(t) + root := filepath.Join(t.TempDir(), "hermes") + t.Setenv("HERMES_HOME", root) + hermesFixture(t, root, "", "link-default", true) + hermesFixture(t, root, "work", "link-work", true) + if got := hermesSessions(); got != 2 { + t.Fatalf("Hermes sessions: %d, want 2", got) + } + moveBehindLink(t, filepath.Join(root, "profiles", "work")) + if got := hermesSessions(); got != 2 { + t.Fatalf("Hermes sessions behind a link: %d, want 2", got) + } +} + +func hermesSessions() int { + n := 0 + for _, s := range List(0) { + if s.Agent == "hermes" { + n++ + } + } + return n +} + +// An Antigravity CLI conversation folder that is a link keeps its calls. +func TestAgyConversationBehindALink(t *testing.T) { + claude, _ := setup(t) + agy := filepath.Join(filepath.Dir(claude), "home", ".gemini", "antigravity-cli") + copyTree(t, "testdata/agy", agy) + calls := func() int { + n := 0 + for _, c := range Calls(time.Time{}) { + if c.Agent == "agy" { + n++ + } + } + return n + } + before := calls() + if before == 0 { + t.Fatal("the fixture has no agy calls") + } + moveBehindLink(t, filepath.Join(agy, "brain", "2b0e6c1a-1d3e-4f6a-9a51-0f1c2d3e4f5a")) + if got := calls(); got != before { + t.Fatalf("agy calls behind a link: %d, want %d", got, before) + } +} diff --git a/internal/sessions/sessions.go b/internal/sessions/sessions.go index 41f4cbf46..ad1206e23 100644 --- a/internal/sessions/sessions.go +++ b/internal/sessions/sessions.go @@ -34,6 +34,7 @@ import ( "github.com/klauspost/compress/zstd" "github.com/yetone/magpie/internal/appdir" "github.com/yetone/magpie/internal/catalog" + "github.com/yetone/magpie/internal/dirent" "github.com/yetone/magpie/internal/proc" "github.com/yetone/magpie/internal/provider" "github.com/yetone/magpie/internal/settings" @@ -424,7 +425,7 @@ func ccFiles(agent, dir string) []file { projects := filepath.Join(dir, "projects") var out []file for _, project := range readDirectory(projects) { - if !project.IsDir() && project.Type()&os.ModeSymlink == 0 { + if !dirent.IsFolder(projects, project) { continue } root := filepath.Join(projects, project.Name()) @@ -435,7 +436,7 @@ func ccFiles(agent, dir string) []file { if stat(&f) { out = append(out, f) } - } else if e.IsDir() || e.Type()&os.ModeSymlink != 0 { + } else if dirent.IsFolder(root, e) { subs := filepath.Join(path, "subagents") for _, sub := range readDirectory(subs) { if sub.IsDir() || !strings.HasSuffix(sub.Name(), ".jsonl") { @@ -447,7 +448,7 @@ func ccFiles(agent, dir string) []file { } } for _, run := range readDirectory(filepath.Join(subs, "workflows")) { - if !run.IsDir() { + if !dirent.IsFolder(filepath.Join(subs, "workflows"), run) { continue } dir := filepath.Join(subs, "workflows", run.Name())