Files
magpie/internal/cli
yetone 71fd92ddf7 saves: a routing group or provider saved answers before the agents' files are synced
CyberCaius on Discord (native Windows): "路由组和供应商编辑保存有时候保存会卡
很久才保存成功", at least 3s, while an agent's request runs.

Cause: every write of providers.json ends in catalog.Touched -> Changed,
which was agent.SyncCatalog, run inside the HTTP request. It rewrites
every agent's model list that differs. On Windows, a file another
program holds open refuses the rename over it, and steady.Rename retries
for 0.5s before giving up. So each such file costs a group save half a
second, and a provider save, which stores several times, costs more. In
WSL the same sync goes through wsl.exe and \\wsl.localhost.

Fix: catalog.Changed is agent.SyncCatalogSoon, which runs SyncCatalog
in the background (it already folds overlapping runs into one more
round). A CLI command waits for those rounds at the end of run()
(SyncCatalogSettled), so `magpie group add` still ends with the files
written, and run() puts catalog.Changed back for tests. The GUI's direct
SyncCatalog calls in providers.go (arrange, keeplogin, switchonly, login
actions, sign-in import) use SyncCatalogSoon too.

Run at the pushed head (parent d792e7ce4, gateway-only since the run
below): go build ./... and go test -tags nogui internal/cli: ok.
On parent a84d2ec80: go vet on darwin and windows for agent, cli and
gui, then go test -tags nogui -count=1 -timeout 45m for internal/agent,
cli, gui and provider: all ok.

Run one rebase earlier (on aa3112f4e, before d08b6118b and a84d2ec80):
- go test -tags nogui -count=1 -timeout 45m internal/agent, cli, gui,
  provider, catalog, gateway: all ok. (Before the rebase, gui failed
  TestCoreScriptBudget on app.js, which this change doesn't touch;
  aa3112f4e fixed it.)
- After that run, only comments changed. Then go vet (darwin, windows)
  on agent and cli, and -race on TestSaveAnswersBeforeTheAgentsSync and
  TestSaveSharesOneCatalogBuild: ok.
- gofmt -l clean; go vet ./... on darwin, GOOS=linux -tags nogui and
  GOOS=windows; go build on darwin, windows, linux nogui: ok.
- GUI: routing-save, routing-save-blank, routing-save-keeps-the-row and
  provider-levels-save: 40/40 (Chromium and WebKit).

Broken by hand:
- TestSaveAnswersBeforeTheAgentsSync (new) holds the sync on Pi's
  models.json as a FIFO. With syncAgentsOnChange set back to
  agent.SyncCatalog, both the group and the provider save waited the
  test's full 20s and failed. With the fix, they answer in 0.06s and 1.1s,
  and the sync is seen running afterwards.
- Native Windows (the test box, sandbox HOME, a cross-built test binary
  with a scratch probe, not committed): a save during a streaming
  request through the gateway took 120ms (group) and 245ms (provider)
  with the old code, so a request in flight alone doesn't stall it. With
  Pi's models.json held open by another handle, the old code took 650ms
  and 785ms; the fix took 18ms and 204ms.

Not tried:
- CyberCaius's own machine. Which of their agents holds a file open
  while it works is not known, so this is the likeliest cause, not the
  proven one for them.
- agent.RenameProvider still syncs inline: its reply names the agents it
  moved, and rename tests read the files after.
- The provider save's reply still runs agent.Detected.
2026-10-25 00:43:36 +08:00
..