Files
magpie/internal/agent/sync_unchanged_test.go
T
yetone 5a32923542 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:
- At 68bbcc01c + 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/main ad3833512,
  3 of 3: #3854) and update's TestRelaunchOpenTriedAgain (TempDir
  cleanup under load ~90, passes 3 of 3 alone: #3881).
- At the pushed head (rebased on ad3833512): 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.
2026-10-27 17:26:30 +08:00

58 lines
1.6 KiB
Go

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
})
}