The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu
Go agent-ai-cliagent-harnessesagent-securitybrowser-extensionciclaude-codecloudcodexcursordetection-engineeringjevknowledge-basememorymobile-device-managementobservabilitysecuritysecurity-information-and-event-managementsecurity-toolstelemetrytraces
fix(harness2session): follow a symlinked runs directory and survive a broken root
Claude9716d83b2ee6
diff --git a/cli/beacon/internal/harness2session/collect.go b/cli/beacon/internal/harness2session/collect.goindex 0eb90ef4..5487ece3 100644--- a/cli/beacon/internal/harness2session/collect.go+++ b/cli/beacon/internal/harness2session/collect.go@@ -170,10 +170,10 @@ func CollectOnce(opts CollectOptions) (summary Summary, err error) {}summary.Roots = rootsstore := &Store{Roots: roots}- refs, err := store.List()- if err != nil {- return summary, err- }+ // A root that cannot be read is an error for the sweep, not the end of it: the other roots,+ // the default among them, are still collected.+ refs, listErrs := store.list()+ summary.Errors += len(listErrs)if len(opts.RunsDirs) > 0 {state.remember(roots)}@@ -185,7 +185,7 @@ func CollectOnce(opts CollectOptions) (summary Summary, err error) {refs = sessionwindow.Recent(refs, opts.Since, func(r TrajectoryRef) int64 { return r.ModTimeUnixMS })summary.Trajectories = len(refs)- var errs []error+ errs := listErrsopts.guard = sessionsync.NewRetentionGuard(opts.Print, opts.Write)var stopErr errorfor _, ref := range refs {diff --git a/cli/beacon/internal/harness2session/collect_test.go b/cli/beacon/internal/harness2session/collect_test.goindex f7c6bf0e..3809d07a 100644--- a/cli/beacon/internal/harness2session/collect_test.go+++ b/cli/beacon/internal/harness2session/collect_test.go@@ -311,3 +311,23 @@ func TestCollectOnceKeepsWhatItWritesAcrossTheRotationWindow(t *testing.T) {return sessionsynctest.Result{EventsEmitted: s.EventsEmitted, Errors: s.Errors, Retention: s.Retention}, err})}++// A remembered --runs-dir that later broke must not stop every scheduled sync: the sweep reports it+// and still collects the other roots.+func TestCollectOnceKeepsCollectingPastABrokenRoot(t *testing.T) {+ env := newSweepEnv(t)+ bad := filepath.Join(t.TempDir(), "gone")+ if err := os.WriteFile(bad, []byte("x"), 0o644); err != nil {+ t.Fatal(err)+ }+ summary, err := CollectOnce(CollectOptions{RunsDirs: []string{bad, env.root}, StatePath: env.statePath, LogPath: env.logPath, Write: true, UserMode: true})+ if err == nil || !strings.Contains(err.Error(), "not a directory") {+ t.Fatalf("err = %v, want the broken root reported", err)+ }+ if summary.Errors != 1 || summary.EventsEmitted != 21 {+ t.Fatalf("summary = %+v, want one error and the good root's 21 events", summary)+ }+ if len(env.logged(t)) != 21 {+ t.Fatal("the good root was not collected")+ }+}diff --git a/cli/beacon/internal/harness2session/store.go b/cli/beacon/internal/harness2session/store.goindex f3d46b30..ecd39863 100644--- a/cli/beacon/internal/harness2session/store.go+++ b/cli/beacon/internal/harness2session/store.go@@ -104,14 +104,23 @@ func (s *Store) Exists() bool {}// List returns every trajectory under the store's roots, oldest first. A root that does not exist-// contributes nothing; one that cannot be walked is an error.+// contributes nothing. One that cannot be read -- unreadable, or not a directory -- contributes+// nothing either, and is reported in the returned error while the other roots are still listed: a+// remembered --runs-dir that later broke must not stop every scheduled sync from reading the rest.func (s *Store) List() ([]TrajectoryRef, error) {+ refs, errs := s.list()+ return refs, errors.Join(errs...)+}++// list is List with one error per root that could not be read.+func (s *Store) list() ([]TrajectoryRef, []error) {var refs []TrajectoryRef+ var errs []errorseen := map[string]bool{}for _, root := range s.Roots {found, err := listRoot(root)if err != nil {- return refs, err+ errs = append(errs, err)}for _, ref := range found {// Nested roots (a runs directory and one of its benchmark directories) would otherwise@@ -129,7 +138,7 @@ func (s *Store) List() ([]TrajectoryRef, error) {}return refs[i].Path < refs[j].Path})- return refs, nil+ return refs, errs}func listRoot(root string) ([]TrajectoryRef, error) {@@ -138,11 +147,20 @@ func listRoot(root string) ([]TrajectoryRef, error) {if errors.Is(err, os.ErrNotExist) {return nil, nil}- return nil, err+ return nil, fmt.Errorf("Harness² runs directory %s: %w", root, err)}if !info.IsDir() {return nil, fmt.Errorf("Harness² runs directory %s is not a directory", root)}+ // A runs directory is often a symlink to a larger disk. Walking the link itself would visit only+ // the link -- filepath.WalkDir does not descend into a symlinked root -- and find nothing, so the+ // walk starts from the directory it names. A confined read walks through the home's os.Root,+ // which descends on its own and must not be handed a path the link resolves to outside the home.+ if !sessionfs.Confined(root) {+ if resolved, err := filepath.EvalSymlinks(root); err == nil {+ root = resolved+ }+ }var refs []TrajectoryReferr = sessionfs.WalkDir(root, func(path string, d fs.DirEntry, walkErr error) error {if walkErr != nil {@@ -192,7 +210,10 @@ func listRoot(root string) ([]TrajectoryRef, error) {})return nil})- return refs, err+ if err != nil {+ return refs, fmt.Errorf("Harness² runs directory %s: %w", root, err)+ }+ return refs, nil}// candidateName is the cheap filter ahead of reading a file: JSONL, plus the one name WorkBuddydiff --git a/cli/beacon/internal/harness2session/store_test.go b/cli/beacon/internal/harness2session/store_test.goindex f05cc1c7..305b011a 100644--- a/cli/beacon/internal/harness2session/store_test.go+++ b/cli/beacon/internal/harness2session/store_test.go@@ -213,3 +213,37 @@ func TestReadLeavesAPartialTailForLater(t *testing.T) {got.LineCount, len(got.Lines), got.Lines[len(got.Lines)-1].Number)}}++// A runs directory is often a symlink to a bigger disk. The walk must follow it: filepath.WalkDir+// alone visits only the link and finds nothing, while the store still reports itself present.+func TestListFollowsASymlinkedRunsDirectory(t *testing.T) {+ target := copyRuns(t)+ link := filepath.Join(t.TempDir(), "runs")+ if err := os.Symlink(target, link); err != nil {+ t.Skipf("symlinks unavailable: %v", err)+ }+ refs := listFixtures(t, link)+ if len(refs) != 3 {+ t.Fatalf("listed %d trajectories through the symlink, want 3", len(refs))+ }+ if ref := refs[labRel]; ref.Context.RunID == "" || ref.Format != FormatCodexExec {+ t.Fatalf("lab ref through the symlink = %+v", ref)+ }+}++// One root that cannot be read is reported, and the others are still listed.+func TestListReportsABrokenRootAndKeepsTheRest(t *testing.T) {+ good := copyRuns(t)+ bad := filepath.Join(t.TempDir(), "runs")+ if err := os.WriteFile(bad, []byte("x"), 0o644); err != nil {+ t.Fatal(err)+ }+ store, _ := NewStore([]string{bad, good})+ refs, err := store.List()+ if err == nil || !strings.Contains(err.Error(), bad) {+ t.Fatalf("err = %v, want the broken root named", err)+ }+ if len(refs) != 3 {+ t.Fatalf("listed %d trajectories, want the good root's 3", len(refs))+ }+}