Skip to content

Every agent's model. One place. Codex on DeepSeek, Claude Code on Kimi, from the menu bar.

edit: a write of the bytes a file already holds leaves it alone (#3870)

yetone5a3292354298
diff --git a/internal/agent/sync_unchanged_test.go b/internal/agent/sync_unchanged_test.go
new file mode 100644
index 000000000..868788d5a
--- /dev/null
+++ b/internal/agent/sync_unchanged_test.go
@@ -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
+ })
+}
diff --git a/internal/edit/file.go b/internal/edit/file.go
index dd5e33ff6..8926bf8fb 100644
--- a/internal/edit/file.go
+++ b/internal/edit/file.go
@@ -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 {
diff --git a/internal/edit/unchanged_test.go b/internal/edit/unchanged_test.go
new file mode 100644
index 000000000..2453d2a39
--- /dev/null
+++ b/internal/edit/unchanged_test.go
@@ -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")
+ }
+ })
+ }
+}