diff --git a/internal/dirent/watch.go b/internal/dirent/watch.go index 8b6de3e6c..ccc32259a 100644 --- a/internal/dirent/watch.go +++ b/internal/dirent/watch.go @@ -28,16 +28,17 @@ import ( // does a listing older than listedFor, should an event never come. var watched struct { sync.Mutex - roots []watchRoot - seq uint64 // counts listings begun and changes heard - all uint64 // seq when everything last changed - dirty map[string]uint64 // a folder's seq when it last changed - at map[uintptr]listed // a listing's seq and time, by its array - since uint64 // seq when the watch last started - active bool - tried map[string]bool // roots asked for - synced time.Time // when every change before was last heard - probe probe + roots []watchRoot + seq uint64 // counts listings begun and changes heard + all uint64 // seq when everything last changed + dirty map[string]uint64 // a folder's seq when it last changed + at map[uintptr]listed // a listing's seq and time, by its array + since uint64 // seq when the watch last started + active bool + tried map[string]bool // roots asked for + synced time.Time // when every change before was last heard + vouched int // the number of the last probe whose write was heard + probe probe } type watchRoot struct { @@ -83,17 +84,38 @@ const ( // heard: a reader calls it before each read of dir's folders, and Stats // of a folder in it then skips listing one that hasn't changed. A Stats // with no Watch of its root in the last trustFor reads its folder. -func Watch(dir string) { +func Watch(dir string) { WatchSince(dir, Mark()) } + +// Mark is the point a WatchSince waits from: every change made before it. +func Mark() int { + watched.Lock() + defer watched.Unlock() + return watched.probe.n +} + +// WatchSince is Watch, waiting only until every change made before mark +// is heard: a probe begun after mark, its own or another's, vouches for +// them. A Sessions poll watches each Claude Code and Codex folder in turn, +// and each Watch wrote, waited on and removed a probe of its own, ~1/6 of +// the poll's CPU (#2278); the poll marks once, and its first probe does +// for every folder after. +func WatchSince(dir string, mark int) { dir = filepath.Clean(dir) starting.Lock() defer starting.Unlock() watched.Lock() tried := watched.tried[dir] + covered := watched.active && watched.vouched > mark watched.Unlock() - if !tried && !add(dir) { - return + if !tried { + if !add(dir) { + return + } + covered = false // a watch begun again hears only what comes after + } + if !covered { + sync1() } - sync1() } // starting is held while a watch is made or waited on, outside watched: @@ -189,6 +211,7 @@ func sync1() { watched.probe.heard, watched.probe.want = nil, "" if ok { watched.synced = at + watched.vouched = max(watched.vouched, p.n) return } watched.seq++ diff --git a/internal/dirent/watch_since_test.go b/internal/dirent/watch_since_test.go new file mode 100644 index 000000000..f58486a6b --- /dev/null +++ b/internal/dirent/watch_since_test.go @@ -0,0 +1,52 @@ +//go:build darwin && cgo + +package dirent + +import ( + "os" + "path/filepath" + "testing" +) + +// A Sessions poll watches each of its folders in turn, and each Watch +// wrote, waited on and removed a probe (#2278). Watches from one mark take +// one probe between them, and a change made before the mark is still seen +// by the next read; a Watch from a later mark probes again. +func TestWatchesFromOneMarkProbeOnce(t *testing.T) { + root, day, rollout := dayFolder(t) + other := filepath.Join(t.TempDir(), "projects") + if err := os.MkdirAll(other, 0o755); err != nil { + t.Fatal(err) + } + Watch(other) + if !quiet(t, root, day) { + t.Fatal("an unchanged watched folder was read again") + } + probes := func() int { watched.Lock(); defer watched.Unlock(); return watched.probe.n } + for i := range 20 { + f, err := os.OpenFile(rollout, os.O_APPEND|os.O_WRONLY, 0) + if err != nil { + t.Fatal(err) + } + f.WriteString(`{"type":"event"}` + "\n") + f.Close() + aged(t, rollout) + fi, _ := os.Stat(rollout) + mark, before := Mark(), probes() + WatchSince(root, mark) + WatchSince(other, mark) + WatchSince(root, mark) + if n := probes() - before; n != 1 { + t.Fatalf("round %d: %d probes from one mark", i, n) + } + l, _ := statsOf(t, day) + if s, _ := l.Get("rollout-a.jsonl"); s.Size != fi.Size() { + t.Fatalf("round %d: read %d bytes, the file has %d", i, s.Size, fi.Size()) + } + } + before := probes() + WatchSince(other, Mark()) + if probes() == before { + t.Fatal("a later mark took no probe of its own") + } +} diff --git a/internal/sessions/one_listing.go b/internal/sessions/one_listing.go index a6b4a4259..0a5795b19 100644 --- a/internal/sessions/one_listing.go +++ b/internal/sessions/one_listing.go @@ -3,6 +3,8 @@ package sessions import ( "slices" "sync" + + "github.com/yetone/magpie/internal/dirent" ) // held is the session folders' listings kept while a request that reads @@ -20,6 +22,9 @@ var held struct { // listing as its own (#4114) gen uint64 lists map[string]*heldList + // mark is dirent's as the latest hold began: its folders are watched + // from it, so a poll's first probe vouches for the rest (#2278) + mark int } type heldList struct { @@ -38,6 +43,7 @@ func OneListing() (done func()) { held.Lock() held.n++ held.gen++ + held.mark = dirent.Mark() held.Unlock() return letGo() } @@ -53,6 +59,7 @@ func ShareListing() (done func()) { defer held.Unlock() if held.n++; held.n == 1 { held.gen++ + held.mark = dirent.Mark() } return letGo() } @@ -67,6 +74,18 @@ func letGo() func() { }) } +// watch is dirent.Watch of dir, from the latest hold's mark under one. +func watch(dir string) { + held.Lock() + n, mark := held.n, held.mark + held.Unlock() + if n == 0 { + dirent.Watch(dir) + return + } + dirent.WatchSince(dir, mark) +} + // heldWaiting is a test's: called as a read waits for another's listing. var heldWaiting func() diff --git a/internal/sessions/sessions.go b/internal/sessions/sessions.go index 445c6e3c7..f98df5616 100644 --- a/internal/sessions/sessions.go +++ b/internal/sessions/sessions.go @@ -904,7 +904,7 @@ func joinedIs(s, a, b string, sep ...byte) bool { func listCCFiles(agent, dir string) []file { projects := filepath.Join(dir, "projects") - dirent.Watch(dir) // statFiles reads only the projects that changed + watch(dir) // statFiles reads only the projects that changed last, _ := ccListed.Load(dir) names := ccNames{last: func() []ccName { l, _ := last.([]ccName); return l }()} // listed into a list of its own kept from the last pass, and given as @@ -1234,7 +1234,7 @@ func codexFilesIn(dir string) []file { } // its unchanged day folders aren't read again; a change in dir itself // (history.jsonl, at each prompt) has them all read - dirent.Watch(dir) + watch(dir) var lists [2][]file for i, folder := range []string{"sessions", "archived_sessions"} { root := filepath.Join(dir, folder)