Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

Resolve relative --dir in the command and match paths that no longer exist

Claude7636745096af
diff --git a/cli/beacon/cmd/handoff.go b/cli/beacon/cmd/handoff.go
index 3db8c98d..dab02e1a 100644
--- a/cli/beacon/cmd/handoff.go
+++ b/cli/beacon/cmd/handoff.go
@@ -4,7 +4,7 @@ import (
"encoding/json"
"fmt"
"io"
- "os"
+ "path/filepath"
"strings"
"text/tabwriter"
"time"
@@ -61,11 +61,15 @@ func runHandoffList(cmd *cobra.Command, args []string) error {
if filter.Directory != "" {
return fmt.Errorf("--here and --dir cannot be combined")
}
- wd, err := os.Getwd()
+ filter.Directory = "."
+ }
+ if filter.Directory != "" {
+ // Recorded session directories are absolute; a relative --dir means relative to here.
+ abs, err := filepath.Abs(filter.Directory)
if err != nil {
- return fmt.Errorf("resolve current directory: %w", err)
+ return fmt.Errorf("resolve --dir %s: %w", filter.Directory, err)
}
- filter.Directory = wd
+ filter.Directory = abs
}
sessions, listErr := handoff.List(handoffSources(handoffOpts.dirs), filter)
out := cmd.OutOrStdout()
diff --git a/cli/beacon/cmd/handoff_test.go b/cli/beacon/cmd/handoff_test.go
index 04a0df12..ce4022e4 100644
--- a/cli/beacon/cmd/handoff_test.go
+++ b/cli/beacon/cmd/handoff_test.go
@@ -182,6 +182,15 @@ func TestHandoffListHereUsesTheWorkingDirectory(t *testing.T) {
if !strings.Contains(out, "in-here") || strings.Contains(out, "elsewhere") {
t.Fatalf("--here listing:\n%s", out)
}
+ for _, rel := range []string{".", "pkg", filepath.Join("pkg", "..")} {
+ out, _, err := runHandoff(t, "list", "--dir", rel)
+ if err != nil || !strings.Contains(out, "in-here") || strings.Contains(out, "elsewhere") {
+ t.Fatalf("--dir %s is relative to the working directory: %v\n%s", rel, err, out)
+ }
+ }
+ if out, _, _ := runHandoff(t, "list", "--dir", "other"); strings.Contains(out, "in-here") {
+ t.Fatalf("--dir other must not match a sibling:\n%s", out)
+ }
if _, _, err := runHandoff(t, "list", "--here", "--dir", "/x"); err == nil || !strings.Contains(err.Error(), "cannot be combined") {
t.Fatalf("--here with --dir err = %v", err)
}
diff --git a/cli/beacon/internal/handoff/session.go b/cli/beacon/internal/handoff/session.go
index 1ff1c65c..fd703e51 100644
--- a/cli/beacon/internal/handoff/session.go
+++ b/cli/beacon/internal/handoff/session.go
@@ -161,10 +161,7 @@ func directoryMatcher(root string) func(dir string) bool {
if root == "" {
return func(string) bool { return true }
}
- resolvedRoot, err := filepath.EvalSymlinks(root)
- if err != nil {
- resolvedRoot = ""
- }
+ resolvedRoot := resolveExisting(root)
return func(dir string) bool {
if dir == "" {
return false
@@ -178,8 +175,26 @@ func directoryMatcher(root string) func(dir string) bool {
if withinDirectory(dir, resolvedRoot) {
return true
}
- resolved, err := filepath.EvalSymlinks(dir)
- return err == nil && withinDirectory(resolved, resolvedRoot)
+ resolved := resolveExisting(dir)
+ return resolved != "" && withinDirectory(resolved, resolvedRoot)
+ }
+}
+
+// resolveExisting resolves the symlinks in path. A path that no longer exists (a deleted project
+// directory) resolves through its nearest existing ancestor, with the rest rejoined as written.
+func resolveExisting(path string) string {
+ path = filepath.Clean(path)
+ var rest []string
+ for {
+ if resolved, err := filepath.EvalSymlinks(path); err == nil {
+ return filepath.Join(append([]string{resolved}, rest...)...)
+ }
+ parent := filepath.Dir(path)
+ if parent == path {
+ return ""
+ }
+ rest = append([]string{filepath.Base(path)}, rest...)
+ path = parent
}
}
@@ -190,17 +205,6 @@ func withinDirectory(dir, root string) bool {
}
dir = filepath.Clean(dir)
root = filepath.Clean(root)
- if !filepath.IsAbs(root) {
- if abs, err := filepath.Abs(root); err == nil {
- root = abs
- }
- }
- if resolved, err := filepath.EvalSymlinks(root); err == nil {
- root = resolved
- }
- if resolved, err := filepath.EvalSymlinks(dir); err == nil {
- dir = resolved
- }
if dir == root {
return true
}
diff --git a/cli/beacon/internal/handoff/session_test.go b/cli/beacon/internal/handoff/session_test.go
index 0978d700..de5e08ba 100644
--- a/cli/beacon/internal/handoff/session_test.go
+++ b/cli/beacon/internal/handoff/session_test.go
@@ -315,15 +315,18 @@ func TestListDirectoryFilterFollowsSymlinks(t *testing.T) {
{Harness: HarnessClaude, ID: "recorded-link", Directory: link},
{Harness: HarnessClaude, ID: "elsewhere", Directory: other},
{Harness: HarnessClaude, ID: "gone", Directory: filepath.Join(base, "deleted")},
+ {Harness: HarnessClaude, ID: "deleted-under-real", Directory: filepath.Join(real, "removed", "sub")},
}}}
for _, tc := range []struct {
root, want string
}{
- {link, "claude_code/recorded-link,claude_code/recorded-real"},
- {real, "claude_code/recorded-link,claude_code/recorded-real"},
+ {link, "claude_code/deleted-under-real,claude_code/recorded-link,claude_code/recorded-real"},
+ {real, "claude_code/deleted-under-real,claude_code/recorded-link,claude_code/recorded-real"},
+ {filepath.Join(link, "removed"), "claude_code/deleted-under-real"},
{filepath.Join(link, "pkg"), "claude_code/recorded-real"},
{other, "claude_code/elsewhere"},
{filepath.Join(base, "missing-root"), ""},
+ {filepath.Join(link, "not-created"), ""},
} {
sessions, err := List(sources, Filter{Directory: tc.root})
if err != nil {
@@ -336,3 +339,16 @@ func TestListDirectoryFilterFollowsSymlinks(t *testing.T) {
}
}
}
+
+func TestResolveExisting(t *testing.T) {
+ base := t.TempDir()
+ real, _ := filepath.EvalSymlinks(base)
+ for _, tc := range []struct{ in, want string }{
+ {base, real},
+ {filepath.Join(base, "a", "b"), filepath.Join(real, "a", "b")},
+ } {
+ if got := resolveExisting(tc.in); got != tc.want {
+ t.Fatalf("resolveExisting(%q) = %q, want %q", tc.in, got, tc.want)
+ }
+ }
+}