diff --git a/internal/gui/backend.go b/internal/gui/backend.go index cb2d76d01..1107f6f6e 100644 --- a/internal/gui/backend.go +++ b/internal/gui/backend.go @@ -9,6 +9,7 @@ import ( "github.com/yetone/magpie/internal/agent" "github.com/yetone/magpie/internal/catalog" + "github.com/yetone/magpie/internal/filememo" "github.com/yetone/magpie/internal/gateway" "github.com/yetone/magpie/internal/provider" "github.com/yetone/magpie/internal/update" @@ -43,7 +44,11 @@ func startBackend() (gw *gateway.Server) { go watchGateway(backendCtx, gatewayWatch) // the usage log parsed now, not when the tray panel first asks for // today's calls (CyberCaius) - go usage.ReadAhead() + go func() { + // its files looked at once a second, as the warm-up's (#1665) + defer filememo.Hold()() + usage.ReadAhead() + }() // Codex's background app-server, restarted when it has the list from // before a change and no codex session is on it (luci) go agent.KeepCodexDaemonCurrent(backendCtx) diff --git a/internal/gui/sessions.go b/internal/gui/sessions.go index 81ef991ee..08c3f262f 100644 --- a/internal/gui/sessions.go +++ b/internal/gui/sessions.go @@ -18,6 +18,7 @@ import ( "time" "github.com/yetone/magpie/internal/agent" + "github.com/yetone/magpie/internal/filememo" "github.com/yetone/magpie/internal/proc" "github.com/yetone/magpie/internal/sessions" "github.com/yetone/magpie/internal/settings" @@ -586,6 +587,10 @@ var warmCodexEffort = func() { usage.CodexModelEffort(usage.All) } // the next minutes: a magpie that had just started read ~470 MB for its // first two minutes at rest. func warmUp(steps ...func()) { + // each file read looked at once a second, not for each session or row + // that asks: an empty sandbox's warm-up looked at settings.json ~300 + // times on Windows (#1665); a write still reads the disk (edit.Read) + defer filememo.Hold()() for _, step := range steps { step() } diff --git a/internal/gui/warm_hold_test.go b/internal/gui/warm_hold_test.go new file mode 100644 index 000000000..0c563e4ef --- /dev/null +++ b/internal/gui/warm_hold_test.go @@ -0,0 +1,47 @@ +package gui + +import ( + "os" + "path/filepath" + "testing" + "time" + + "github.com/yetone/magpie/internal/filememo" +) + +// The warm-up's reads look at a file once a second, not for each session +// or row that asks (#1665: settings.json ~300 times on Windows): a file +// another app changed meanwhile is read as it was, inside that second. +func TestWarmUpHoldsWhatItReads(t *testing.T) { + p := filepath.Join(t.TempDir(), "f.json") + old := time.Now().Add(-time.Hour) + write := func(s string, at time.Time) { + if err := os.WriteFile(p, []byte(s), 0o600); err != nil { + t.Fatal(err) + } + if err := os.Chtimes(p, at, at); err != nil { + t.Fatal(err) + } + } + read := func() string { + b, err := filememo.Read("warm-hold-test", p, func(b []byte) (string, error) { return string(b), nil }) + if err != nil { + t.Fatal(err) + } + return string(b) + } + write("one", old) + var got string + warmUp(func() { + read() + write("two", old.Add(time.Minute)) + got = read() + }) + if got != "one" { + t.Fatalf("the warm-up looked at the file again for its second read: got %q", got) + } + // and nothing is held once it ends + if got := read(); got != "two" { + t.Fatalf("after the warm-up the file reads %q, not what is on the disk", got) + } +}