Skip to content

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

Compare the active-session directory the way session listings do

Claudee7ea3060bdfb
diff --git a/cli/beacon/cmd/share.go b/cli/beacon/cmd/share.go
index 1079fd55..55865331 100644
--- a/cli/beacon/cmd/share.go
+++ b/cli/beacon/cmd/share.go
@@ -621,7 +621,7 @@ func resolveShareCurrent(sources []handoff.Source, harness string) (shareSubject
func shareEnvContradiction(sources []handoff.Source, dir string, subject shareSubject) error {
session := subject.session
advice := "pass --harness <runtime> for the runtime you are in, or --session <id>"
- if session.Directory != "" && !shareDirWithin(dir, session.Directory) {
+ if session.Directory != "" && !handoff.DirectoryWithin(dir, session.Directory) {
return fmt.Errorf("%s=%s names %s session %s, which ran in %s, not in %s or a directory above it; a runtime started from that session's shell inherits its variables: %s",
shareSessionEnv, session.ID, session.Harness, session.ID, session.Directory, dir, advice)
}
@@ -630,10 +630,9 @@ func shareEnvContradiction(sources []handoff.Source, dir string, subject shareSu
shareSessionEnv, session.ID, session.Harness, session.ID, harness, id, dir, at.Local().Format(time.RFC3339), session.Harness, advice)
}
if entries, err := activesession.Read(shareActiveSessionsPath()); err == nil {
- clean := activesession.CleanDir(dir)
now := shareNow()
for _, e := range entries {
- if e.WorkingDirectory != clean || now.Sub(e.LastSeen) > shareActiveMaxAge {
+ if !handoff.SameDirectory(e.WorkingDirectory, dir) || now.Sub(e.LastSeen) > shareActiveMaxAge {
continue
}
name, err := handoff.ParseHarness(e.Harness)
@@ -652,16 +651,6 @@ func shareEnvContradiction(sources []handoff.Source, dir string, subject shareSu
return nil
}
-// shareDirWithin reports whether dir is parent or below it.
-func shareDirWithin(dir, parent string) bool {
- clean := func(p string) string { return filepath.Clean(evalSymlinks(filepath.Clean(p))) }
- rel, err := filepath.Rel(clean(parent), clean(dir))
- if err != nil {
- return false
- }
- return rel == "." || (rel != ".." && !strings.HasPrefix(rel, ".."+string(filepath.Separator)))
-}
-
// shareActiveIsNewest reports whether the active-session entry may stand for this directory's
// session: only hooks write the record, so a runtime Beacon sees only through OTLP or a store (an
// OTLP-only or poll runtime) never updates it, and an older entry would otherwise win over that
@@ -729,10 +718,9 @@ func shareActiveSession(dir, harness string) (activesession.Entry, bool) {
if err != nil {
return activesession.Entry{}, false
}
- dir = activesession.CleanDir(dir)
now := shareNow()
for _, e := range entries {
- if e.WorkingDirectory != dir || now.Sub(e.LastSeen) > shareActiveMaxAge {
+ if !handoff.SameDirectory(e.WorkingDirectory, dir) || now.Sub(e.LastSeen) > shareActiveMaxAge {
continue
}
name, err := handoff.ParseHarness(e.Harness)
diff --git a/cli/beacon/cmd/share_current_test.go b/cli/beacon/cmd/share_current_test.go
index 4bdd0329..a9f7170b 100644
--- a/cli/beacon/cmd/share_current_test.go
+++ b/cli/beacon/cmd/share_current_test.go
@@ -4,6 +4,7 @@ import (
"encoding/json"
"os"
"path/filepath"
+ "runtime"
"strings"
"testing"
"time"
@@ -248,3 +249,60 @@ func TestShareCurrentPassesOverAnOlderActiveEntry(t *testing.T) {
t.Fatalf("external id = %q, want the active Claude Code session", got)
}
}
+
+// shareSymlinkedProject returns a symlink to the project directory, and the project's physical
+// path, which is what a hook records; the shell reports the symlink.
+func shareSymlinkedProject(t *testing.T, env *shareEnv) (link, physical string) {
+ t.Helper()
+ if runtime.GOOS == "windows" {
+ t.Skip("symlinked directories need POSIX symlinks")
+ }
+ physical, err := filepath.EvalSymlinks(env.projectDir)
+ if err != nil {
+ t.Fatal(err)
+ }
+ link = filepath.Join(t.TempDir(), "checkout")
+ if err := os.Symlink(physical, link); err != nil {
+ t.Skipf("symlink: %v", err)
+ }
+ return link, physical
+}
+
+func TestShareCurrentMatchesTheActiveRecordThroughASymlink(t *testing.T) {
+ env := shareCurrentEnv(t)
+ link, physical := shareSymlinkedProject(t, env)
+ srv := newFakeShareServer(t)
+ srv.signIn(account.ScopeTracesWrite)
+ t.Chdir(link)
+ if err := activesession.Record(shareActiveSessionsPath(), "gemini_cli", "gem-session-1", physical, time.Now()); err != nil {
+ t.Fatal(err)
+ }
+ stdout, stderr, err := runShareCommand(t, "--current", "--json", "--log-path", env.logPath)
+ if err != nil {
+ t.Fatalf("share --current: %v\n%s", err, stderr)
+ }
+ var result struct {
+ Data shareResult `json:"data"`
+ }
+ if err := json.Unmarshal([]byte(stdout), &result); err != nil {
+ t.Fatal(err)
+ }
+ if result.Data.SelectedBy != "active" || result.Data.ExternalID != "gemini_cli:gem-session-1" {
+ t.Fatalf("result = %+v, want the active session", result.Data)
+ }
+}
+
+func TestShareCurrentSeesAnInheritedEnvironmentThroughASymlink(t *testing.T) {
+ env := shareCurrentEnv(t)
+ shareEnvSessions(t, env)
+ link, physical := shareSymlinkedProject(t, env)
+ t.Chdir(link)
+ shareGetenv = shareEnvFor("claude_code", shareOlderSession)
+ if err := activesession.Record(shareActiveSessionsPath(), handoff.HarnessCodex, "codex-session-1", physical, time.Now().Add(time.Second)); err != nil {
+ t.Fatal(err)
+ }
+ _, _, err := runShareCommand(t, "--print", "--log-path", env.logPath)
+ if err == nil || !strings.Contains(err.Error(), "codex-session-1") {
+ t.Fatalf("err = %v, want the inherited-environment refusal", err)
+ }
+}
diff --git a/cli/beacon/internal/handoff/session.go b/cli/beacon/internal/handoff/session.go
index 5ee8ac1a..731b777e 100644
--- a/cli/beacon/internal/handoff/session.go
+++ b/cli/beacon/internal/handoff/session.go
@@ -220,6 +220,18 @@ func directoryMatcher(root string) func(dir string) bool {
}
}
+// DirectoryWithin reports whether dir is root or below it, compared the way session listings
+// compare directories (directoryMatcher): as written, then with symlinks resolved.
+func DirectoryWithin(dir, root string) bool {
+ return root != "" && directoryMatcher(root)(dir)
+}
+
+// SameDirectory reports whether a and b name the same directory, compared as DirectoryWithin
+// compares them.
+func SameDirectory(a, b string) bool {
+ return DirectoryWithin(a, b) && DirectoryWithin(b, a)
+}
+
// 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 {