Files
magpie/internal/sessions/link_entry_test.go
T
yetone 2b0037f41d sessions: a Claude Code, Qoder or Cowork project folder, a session's folder or a Hermes profile behind a junction is read, not skipped (#2268, JasonBuildAI)
On Windows os.ReadDir lists a junction as ModeIrregular, neither IsDir
nor ModeSymlink, so a reader that goes into only IsDir() entries, or
IsDir() and ModeSymlink ones, walked past a folder kept on another drive
with a junction left in its place. ccFiles (Claude Code's own store,
Qoder's, Qoder CN's and Cowork's) skipped such a project folder whole, a
session's own folder (its subagents) and a workflow run's; HermesDirs
skipped such a profile. Sessions and Usage lost all of them, silently.
#2233/#2250 fixed walk roots (walkLinked); this is the entry check one
level in.

dirent.IsFolder is the one check now: an entry is a folder to read into
when it is one, or when it is a symlink or irregular entry that os.Stat
follows to a folder. A link to a file, a broken link and other irregular
entries are none. It follows only the entry asked about; none of its
callers recurses through one, so no visited set is needed.

Used at every reader of an agent's folder that picks entries this way:
- sessions: ccFiles' project, session-folder and workflow-run checks,
  HermesDirs' profile check, agyCallFiles' conversation check (IsDir only
  before, so even a symlink was skipped);
- agent: hermesProfiles and ompProfiles (IsDir only), so a linked profile
  still gets magpie's provider and its own row;
- library: desktopSkillRoots' org and account folders (IsDir only).
Left as they are, on purpose: WalkDir copies, hashes and deletes in
library and sessions/manage.go (they must not follow links), the
relocate code (it tells a link from a folder on purpose), library's
linkEntry (asks "is this a link", not "is this a folder"), magpie's own
folders (calls cache, gateway logs, OthersDir, backups, Bun, Devin
homes), and install/version detection (claudecode versions, VS Code
extensions, Desktop's folder name, Hanako's agents).

The issue's patch had sessions' own copy of library's linkEntry without
a Stat; this is a shared package and Stat-checked, so an irregular entry
that isn't a folder (a dedup or cloud placeholder file) isn't read as one.
docs/subsystems/gateway-sessions.md says how the readers treat links.

Run:
- New tests make real links, a junction (mklink /J) on Windows and a
  symlink elsewhere: TestIsFolder; TestClaudeProjectFolderBehindALink,
  TestClaudeSessionFolderBehindALink and TestHermesProfileBehindALink (the
  issue's), TestClaudeWorkflowRunBehindALink,
  TestAgyConversationBehindALink; agent's TestHermesProfileBehindALink and
  TestOmpProfileBehindALink; library's
  TestClaudeDesktopSkillRootsBehindALink.
- Broken by hand on macOS (IsFolder = e.IsDir()): all nine fail. Broken
  as before on the Windows box (IsDir() || ModeSymlink, no Stat):
  TestIsFolder and all eight reader tests fail there (e.g. "Claude
  session files behind a link: [], want [...]", "Hermes sessions behind
  a link: 1, want 2", "agy calls behind a link: 3, want 5"); with the
  fix all pass. Windows test binaries cross-built with go1.26.8
  (windows/amd64, nogui) and run on the box with a fresh sandbox
  USERPROFILE/HOME/APPDATA/LOCALAPPDATA/TEMP per run.
- Whole packages on the Windows box: dirent and sessions pass. usage,
  agent and library each have reds that fail the same with binaries
  built from origin/main 69d7ee33a: usage's
  TestDesktopSessionAccountMetadata (#1356/#1827), agent's
  TestFolderVarsAreListed (reads ..\gateway source the copied layout
  lacks), library's TestAgentsSkillsFolders,
  TestHooksCodexTrustMovesWithTheGroups and TestTargetsLeaveGoneAgentsOut
  (filed as #2275).
- gofmt -l: nothing. go vet ./... (darwin), GOOS=linux go vet -tags nogui
  ./..., GOOS=windows go vet ./... and -tags nogui: clean.
- macOS: set -o pipefail; go test -p 4 -timeout 40m ./... on bc470c6b6
  plus this change: 56 packages ok, none failed. After rebasing onto
  origin/main 3a0b61275 (#2261's subagents, which touch sessions.go and
  the same doc): go test ./internal/sessions/ ./internal/dirent/ again.
2026-10-15 08:33:54 +08:00

152 lines
4.7 KiB
Go

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)
}
}