edit: a write of the bytes a file already holds leaves it alone (#3870)
Every SyncCatalog wrote agents' config files again with the same bytes: a new mtime that wakes every watcher of the file (filememo, the Codex daemon loop, the agent's own reload), and a temp-and-rename that swaps the file under an agent reading it. #3870 named Kimi Code, Grok and AtomCode; with every agent connected in a sandbox HOME, a second sync rewrote 15 files: .claude/settings.json, Cline's providers.json, CodeBuddy, Copilot BYOK, Factory, Qwen, Snow (both), WorkBuddy (both), ZCode, Kimi Code, Grok, AtomCode and magpie's stash.json. WriteAtomic, which every TOML, JSON, YAML and dotenv writer goes through, now reads the target after the filter and BOM are applied and returns when its bytes are the same. A real change is written as before. Tests: - internal/edit TestUnchangedWriteKeepsFile: TOML table/key/top, JSON, YAML and .env; mtime and inode kept on the same bytes, a changed value written. Red in all 6 with the check turned off by hand. - internal/agent TestSecondSyncWritesNothing: every connectable agent connected, second SyncCatalog + dshWiredOnce writes no file. Red with the old WriteAtomic (15 files), green -count=3 with this one. Run: - At68bbcc01c+ this change: gofmt clean; go vet -tags nogui internal/edit and internal/agent for linux, windows, darwin ok; go test -timeout 45m over every importer of internal/edit (access, agent, autostart, backup, cli, codexcat, daemon, davsync, edit, gateway, library, mcpauth, plugin, profile, provider, settings, update, usage): all ok but provider's TestFetchAsksASharedBaseOnce and two siblings (red the same on unchanged origin/mainad3833512, 3 of 3: #3854) and update's TestRelaunchOpenTriedAgain (TempDir cleanup under load ~90, passes 3 of 3 alone: #3881). - At the pushed head (rebased onad3833512): gofmt, 3-OS vet, all of internal/edit, and internal/agent's new test plus Disconnect, Codex hand-edit, Kimi, Grok and AtomCode tests: ok. internal/gui, which imports internal/edit, was not run. Not tried: the real Kimi Code / Grok / AtomCode apps watching the file.
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
package agent
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// A sync that changes nothing writes nothing: with every agent connected,
|
||||
// a second SyncCatalog (and what a running gateway keeps up) leaves each
|
||||
// file's mtime as it was. A rewrite of the same bytes wakes every watcher
|
||||
// of the file and swaps it under an agent reading it (#3870); before, 15
|
||||
// files here were written again on each sync.
|
||||
func TestSecondSyncWritesNothing(t *testing.T) {
|
||||
home, _ := codexHome(t, "", "")
|
||||
managed := claudeManaged
|
||||
claudeManaged = func() string { return filepath.Join(home, "managed-settings.json") }
|
||||
t.Cleanup(func() { claudeManaged = managed })
|
||||
for _, a := range All() {
|
||||
if a.Launch != nil || a.Native != nil || a.WSL != "" || len(a.Fields) == 0 {
|
||||
continue
|
||||
}
|
||||
if _, err := a.ConnectHow(); err != nil {
|
||||
continue
|
||||
}
|
||||
if err := a.Connect(); err != nil {
|
||||
t.Logf("%s: %v", a.ID, err)
|
||||
}
|
||||
}
|
||||
SyncCatalog()
|
||||
dshWiredOnce()
|
||||
old := time.Now().Add(-time.Hour).Truncate(time.Second)
|
||||
files := 0
|
||||
filepath.WalkDir(home, func(p string, e os.DirEntry, err error) error {
|
||||
if err == nil && !e.IsDir() {
|
||||
os.Chtimes(p, old, old)
|
||||
files++
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if files < 20 {
|
||||
t.Fatalf("only %d files under home: agents weren't connected", files)
|
||||
}
|
||||
SyncCatalog()
|
||||
dshWiredOnce()
|
||||
filepath.WalkDir(home, func(p string, e os.DirEntry, err error) error {
|
||||
if err != nil || e.IsDir() {
|
||||
return nil
|
||||
}
|
||||
rel, _ := filepath.Rel(home, p)
|
||||
if st, err := os.Stat(p); err == nil && !st.ModTime().Equal(old) {
|
||||
t.Errorf("second sync wrote %s", rel)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
@@ -106,6 +106,12 @@ func WriteAtomic(path string, data []byte) error {
|
||||
data = (*f)(path, data)
|
||||
}
|
||||
data = keepBOM(path, data)
|
||||
// A file that already holds these bytes is left alone: a new mtime
|
||||
// wakes every watcher of it, and a rename swaps the file under an
|
||||
// agent reading it, for nothing (#3870).
|
||||
if cur, err := os.ReadFile(path); err == nil && bytes.Equal(cur, data) {
|
||||
return nil
|
||||
}
|
||||
if hardLinked(path) {
|
||||
f, err := os.OpenFile(path, os.O_WRONLY, 0)
|
||||
if err == nil {
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
package edit
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// A write of the bytes a file already holds leaves it alone: its mtime
|
||||
// and inode stay, so a watcher isn't woken and an agent reading it keeps
|
||||
// its file (#3870). Each format's setter is tried, then a real change.
|
||||
func TestUnchangedWriteKeepsFile(t *testing.T) {
|
||||
old := time.Now().Add(-time.Hour).Truncate(time.Second)
|
||||
for _, tc := range []struct {
|
||||
name, file string
|
||||
set func(string, any) error
|
||||
}{
|
||||
{"toml table", "config.toml", func(p string, v any) error {
|
||||
return SetTOMLTable(p, "providers.magpie", KV{Path: "base_url", Value: v})
|
||||
}},
|
||||
{"toml key", "config.toml", func(p string, v any) error {
|
||||
return SetTOMLKey(p, "providers.magpie", "base_url", v)
|
||||
}},
|
||||
{"toml top", "config.toml", func(p string, v any) error {
|
||||
return SetTOMLTop(p, KV{Path: "model", Value: v})
|
||||
}},
|
||||
{"json", "settings.json", func(p string, v any) error {
|
||||
return SetJSON(p, KV{Path: "env.BASE_URL", Value: v})
|
||||
}},
|
||||
{"yaml", "config.yaml", func(p string, v any) error {
|
||||
return SetYAML(p, KV{Path: "model.base_url", Value: v})
|
||||
}},
|
||||
{"env", ".env", func(p string, v any) error {
|
||||
return SetEnvFile(p, KV{Path: "BASE_URL", Value: v})
|
||||
}},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
p := filepath.Join(t.TempDir(), tc.file)
|
||||
if err := tc.set(p, "http://127.0.0.1:3425"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Chtimes(p, old, old); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
before, _ := os.Stat(p)
|
||||
if err := tc.set(p, "http://127.0.0.1:3425"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
after, _ := os.Stat(p)
|
||||
if !after.ModTime().Equal(old) || !os.SameFile(before, after) {
|
||||
t.Fatalf("same bytes rewrote the file: mtime %v (was %v), same file %v",
|
||||
after.ModTime(), old, os.SameFile(before, after))
|
||||
}
|
||||
if err := tc.set(p, "http://127.0.0.1:3426"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if st, _ := os.Stat(p); st.ModTime().Equal(old) {
|
||||
t.Fatal("a real change wasn't written")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user