Files
magpie/internal/filememo/watch.go
T
yetone de49433ff8 filememo: another process's writes to magpie's files are watched for, not polled every 5s (#3185)
An idle magpie still woke a dozen times on each 5s beat: the loops that
follow plugins.yml, settings.json, providers.json, the plugin list, tools
and access each looked at the disk for what the CLI or a second magpie
wrote. The gateway now watches those files' folders (fsnotify: FSEvents,
inotify, ReadDirectoryChangesW), and a change from another process wakes
the same loops at once, as magpie's own writes do (filememo.Written).
With the watch running the loops look for a change they missed only once
a minute (OthersEvery); where the folders can't be watched, or the watch
errs, they go on looking every 5s.

Run at 466cafa0c + this change. The rebase onto 13f9c7fca brought in
internal/gateway's claude bridge and redact files, none of the files here;
after it, go build ./... and the filememo, pluginrt, awake tests ran again ok:
- go test -timeout 45m ./internal/filememo ./internal/pluginrt
  ./internal/middleware ./internal/otlp ./internal/gateway: all ok
  (gateway 384s).
- TestWatchWakesOnOthersWrites -count=3 ok; red with wake() taken out.
- gofmt -l clean; GOOS=linux and GOOS=windows go vet -tags nogui on the
  changed packages ok.
- Real gateway, sandbox HOME, idle: proc_pid_rusage wakeups over 120s
  after a 90s warm-up, two rounds each: v0.1.1561 2.1 / 2.7 per second,
  this 1.9 / 2.0 (and 1.8 in a third run).
- Same gateway: plugins.yml replaced from outside with broken YAML; the
  gateway logged "plugins: ... doesn't parse" 0.02s after the write
  (it could take up to 5s before). File restored after.

Not tried: Linux and Windows real runs (vet only).
2026-10-26 11:22:20 +08:00

84 lines
2.0 KiB
Go

package filememo
import (
"context"
"log"
"path/filepath"
"sync/atomic"
"time"
"github.com/fsnotify/fsnotify"
)
// OthersEvery is how often a loop that follows magpie's files (Written)
// looks for what another process (the CLI, a second magpie) wrote: every
// few seconds, or, while Watch is told of their changes, only now and then
// for one it missed (#3185: the looks woke an idle magpie on each beat,
// a dozen times every five seconds).
func OthersEvery() time.Duration {
if watching.Load() {
return othersWatched
}
return othersPolled
}
const (
othersPolled = 5 * time.Second
othersWatched = time.Minute
)
// watching is whether a Watch is told of the files' changes.
var watching atomic.Bool
// Watch has a change another process makes to one of paths seen at once,
// as a write of magpie's own is (Written), until ctx ends. Their folders
// are watched, not the files: a file written whole is a new one put in
// its place. Where the folders can't be watched, the loops go on looking
// every few seconds.
func Watch(ctx context.Context, paths ...string) {
w, err := fsnotify.NewWatcher()
if err != nil {
log.Printf("filememo: can't watch for changes: %v", err)
return
}
defer w.Close()
ours := map[string]bool{}
dirs := map[string]bool{}
for _, p := range paths {
p = filepath.Clean(p)
ours[p] = true
dirs[filepath.Dir(p)] = true
}
for d := range dirs {
if err := w.Add(d); err != nil {
log.Printf("filememo: can't watch %s for changes: %v", d, err)
return
}
}
watching.Store(true)
defer watching.Store(false)
for {
select {
case <-ctx.Done():
return
case e, ok := <-w.Events:
if !ok {
return
}
if p := filepath.Clean(e.Name); ours[p] {
Look(p)
wake()
}
case err, ok := <-w.Errors:
if !ok {
return
}
// a change may be lost (the queue overflowed): looked for at
// once, and every few seconds again from here
log.Printf("filememo: watching for changes: %v", err)
wake()
return
}
}
}