magpie icon indicating copy to clipboard operation
magpie copied to clipboard

backup, davsync: a settings.json that can't be read is carried into a backup and a sync as the defaults, taking the settings of every other machine it is put back on

Open JasonBuildAI opened this issue 1 month ago • 2 comments

What's wrong

backup.Collect asked os.Stat whether settings.json was there and then took the settings from settings.Load (internal/backup/backup.go:211 on main at 9916a946). Load stands its defaults in for a file that is there but can't be read, or doesn't parse and has no good .bak — the case settings.LoadChecked exists for, and the case provider.Stored refuses for providers.json (9c54a560: "a providers.json that can't be read is said where the providers are listed, never backed up or synced as none"). So an unreadable settings.json travelled as the user's own settings: theme system, language system, text size 110, an empty AllowIPs, no disabled models, and none of the per-model names, prices, efforts, wires or output caps.

That bundle is then this computer's settings part: davsync.collect (internal/davsync/davsync.go:762) seals and pushes it, and the part's hash differing from the last sync reads as "changed here" (davsync.go:1164, lc && !rc → take), so the defaults go up as if the user had changed their settings. Every other computer brings a server-changed part in (bring → backup.Restore, davsync.go:962), and Restore ends in settings.Save (internal/backup/backup.go:456), which refuses only when that machine's own file can't be read. The other machine's file reads fine; its theme, language, text size, allow list and per-model settings are written over. KeepOwn puts back only the window, proxy, port, tray and consent that are this computer's own, so nothing stands in the way, and nothing is said — the status shows a normal sync. The same holds for a backup file exported while the file is unreadable (magpie backup, the GUI's export): it carries the defaults, and importing it anywhere erases those settings there.

Triggers, all without a gesture from the user: a Windows handle another program holds for a moment (an editor, a backup tool, antivirus), a permission magpie doesn't have, a file another tool wrote that isn't JSON, and the zeroed or cut-short file class #1505 exists for.

Why this is exceptionally important

  • Who pays: every other machine on the sync — one unreadable read on one computer rewrites the settings of all the others, silently, and each following sync keeps them. Also anyone restoring a backup made while the file was unreadable.
  • What is lost: the preferences that are the point of the settings file — theme, language, text size, the gateway's AllowIPs, disabled models, and every per-model name, price, reasoning effort, wire and output cap.
  • The project's own rule: "a failed read, parse or hash means unknown" (LESSONS.md; moved into docs/code-standards.md on 10-13). It was applied to this same file on 10-12 (7d9382db2: an unreadable settings.json let every address into the gateway) and to providers (9c54a560). backup.Collect kept the old read for settings, and it is the carry node for the backup, the sync and the GUI export, so this is the remaining path of the same defect class.
  • Scale: sync is a shipped feature (WebDAV, S3, GitHub) that runs every three minutes once configured; the repo has 12,661 stars / 863 forks and the current release's assets take ~1,000 downloads in their first hours (the same figures as #2242). A user with two computers and one damaged file loses the settings on both — the pushed defaults come back around with the next sync.
  • It stays until fixed: no release changes the read, and the damaged machine re-pushes the defaults on its next sync.

Minimal reproduction (main at 9916a946)

Two tests from the fix's branch, both real FAILs on main. TestCollectStopsOnUnreadableSettings (internal/backup; a home whose settings.json is cut short, no .bak):

--- FAIL: TestCollectStopsOnUnreadableSettings (3.40s)
    settings_unreadable_test.go:29: Collect read a settings.json that can't be read as settings: &{Theme:system Colorblind:false Lang:system Tray:panel SessionTerminal: Currency:usd ChineseUnits:false Dock:false DockWindow:false Proxy: OTel:{Enabled:false Endpoint: Headers:map[] Metrics:false Sessions:false Bodies:false BodiesWhole:false} GitHubToken: Redact:false RedactPersonal:false RedactKinds:map[] RedactWords:[] RedactRules:[] LAN:false LANKey: CORSOrigins:[] AllowIPs:[] Port:0 LANKeyID: RequestArchive:false RequestArchiveMaxMB:0 GatewayConversations:false CodexWarmup: ClaudeWarmup: CodexWarmAt: ClaudeWarmAt: CodexWarmAts:[] ClaudeWarmAts:[] CodexWarmAtOf:map[] CodexAutoReset:[] CodexAutoResetOnly:map[] CodexNoCredits:[] WorkBuddyCheckin:false TraeCheckin:false MiniMaxCheckin:false QoderCheckin:false PluginCheckins:map[] PluginOptions:map[] CheckinOff:[] MemberModel:false NoLoopGuard:false RefusalRetries:<nil> NoAccountDiscovery:false NoStats:false NoUsageStats:false NoUpdatePill:false UpdateSkip: NoAutoUpdate:false UpdateEvery:360 AutoRestart:false UpdateMirror: Vision: VisionEffort: ImageGen: Searcher: SearcherEffort: SearchFirst: TrayUsages:[] TrayUsage: TrayUsageEvery:3 TrayNoLogos:false TrayNoBird:false TrayPace:false Lightweight:false KeepAwake:false NoWSLAgents:false KeepAwakeDisplay:false GatewayMode: QuotaLeft:false QuotaReads: UsageAlert:0 BalanceAlert:0 ResetReminder:0 TurnDoneAlert:0 PlainNames:false PlainOwnNames:false BareModelIDs:false CodexAgentsV1:false CodexTitles: CodexDescriptions: CodexPredictions: CodexAutoReview: CodexSubagentModel: DesktopSubagentModel: DesktopSubagentEffort: FullContext:false DesktopLongest:false GuideHidden:false CompactAt:0 ChinaMirror:false TextSize:110 UIFont:<nil> CodeFont:<nil> AgentOrder:[] AgentsHidden:[] AgentsShown:[] UsageOrder:[] PanelUsageHidden:[] UsageAgentsHidden:[] ClientAgents:map[] Visible:map[] HiddenModels:map[] DisabledModels:[] PickedModels:map[] OwnModelsHidden:[] AgentEfforts:map[] OrderedModels:map[] FastPicks:map[] ModelNames:map[] ModelEfforts:map[] ModelReasonless:map[] ModelImages:map[] ModelPrices:map[] ModelOutputs:map[] ModelCompacts:map[] ModelWires:map[] ModelAPIs:map[] ModelSameAs:map[] ModelKinds:map[] Window:[] WindowMaximised:false}
FAIL
FAIL	github.com/yetone/magpie/internal/backup	4.288s
FAIL

TestSyncStopsOnUnreadableSettings (internal/davsync; two computers join over a fake WebDAV, a's file is cut short):

--- FAIL: TestSyncStopsOnUnreadableSettings (9.71s)
    unreadable_test.go:131: a synced a broken settings.json
FAIL
FAIL	github.com/yetone/magpie/internal/davsync	10.886s
FAIL

With the branch's fix both pass:

ok  	github.com/yetone/magpie/internal/backup	0.955s
ok  	github.com/yetone/magpie/internal/davsync	10.854s

With the guard written as if it were there but never taken (err != nil && err == nil), both fail again with the same lines.

The fix, since this account cannot open a pull request on this repo (the README: "This repository doesn't take pull requests. Only maintainers can open them."; the API refuses: "JasonBuildAI does not have the correct permissions to execute CreatePullRequest", REST 404 — the same as #2229, #2231, #2239 and #2242): branch fix/599409-tbd in the fork JasonBuildAI/magpie, commit 2ac0bb2d on main at 9916a946:

git fetch https://github.com/JasonBuildAI/magpie fix/599409-tbd && git cherry-pick FETCH_HEAD
  • internal/backup/backup.go: Collect reads through settings.LoadChecked and stops the backup with the file's error, exactly as it stops on a providers.json that can't be read; only a file that is there and reads is carried.
  • internal/backup/settings_unreadable_test.go: TestCollectStopsOnUnreadableSettings.
  • internal/davsync/unreadable_test.go: TestSyncStopsOnUnreadableSettings, the settings sibling of TestSyncStopsOnUnreadableProviders: two computers join, a's file is cut short, a's sync ends in the error with nothing pushed and the file left alone, the server still carries a's real settings, and b, syncing after, keeps them.

Semantic change: for a settings.json that is there but can't be read or parsed with no last-good .bak, a backup, a sync and an export now stop with that file's error, where they previously carried the defaults and let them travel to the other machines; a missing file, an empty one, and one with a good .bak (read as the last good generation) are unchanged. No interface, state or compatibility change; davsync needs no change of its own, the error propagates from Collect through collect before anything is sealed or pushed.

Ran on the branch: gofmt -l on the three files prints nothing; go vet ./internal/backup ./internal/davsync exit 0; GOOS=linux go vet -tags nogui ./... exit 0; GOOS=windows go vet ./... exit 0; GOOS=linux go build -tags nogui ./... exit 0; GOOS=windows go build ./... exit 0; the two -run tests above pass (ok 0.955s and ok 10.854s). go test -tags nogui -count=1 ./... is running as this is posted — this box is running several agents' suites at once at 91% CPU — and its result will follow in a comment. Not tried: a live WebDAV or S3 server, and a Windows file held open; the reproduction is the cut-short file, which is the same "there but unreadable" the read sees.

AI disclosure: this change was drafted with AI assistance at my request, under my direction and review.

JasonBuildAI avatar Oct 15 '26 00:10 JasonBuildAI

The full suite, as promised in the report. go test -tags nogui -count=1 -timeout 45m ./... on the branch (07:14–08:15 +0800), on this box while several agents' suites shared it (16 cores, 91–100% CPU). Summary lines:

?   	github.com/yetone/magpie	[no test files]
ok  	github.com/yetone/magpie/internal/access	42.457s
FAIL	github.com/yetone/magpie/internal/agent	2700.736s
ok  	github.com/yetone/magpie/internal/agentenv	8.106s
?   	github.com/yetone/magpie/internal/agentplug	[no test files]
ok  	github.com/yetone/magpie/internal/appdir	10.046s
ok  	github.com/yetone/magpie/internal/autostart	0.829s
ok  	github.com/yetone/magpie/internal/awake	1.022s
ok  	github.com/yetone/magpie/internal/backup	1909.366s
?   	github.com/yetone/magpie/internal/budget	[no test files]
ok  	github.com/yetone/magpie/internal/catalog	10.562s
ok  	github.com/yetone/magpie/internal/claudebridge	1.375s
ok  	github.com/yetone/magpie/internal/claudecode	2.055s
FAIL	github.com/yetone/magpie/internal/cli	2703.160s
ok  	github.com/yetone/magpie/internal/codexcat	10.091s
ok  	github.com/yetone/magpie/internal/daemon	1.281s
ok  	github.com/yetone/magpie/internal/davsync	2621.981s
ok  	github.com/yetone/magpie/internal/desktopdir	3.004s
ok  	github.com/yetone/magpie/internal/edit	76.565s
ok  	github.com/yetone/magpie/internal/filelock	2.929s
ok  	github.com/yetone/magpie/internal/filememo	7.872s
ok  	github.com/yetone/magpie/internal/fonts	0.473s
ok  	github.com/yetone/magpie/internal/fx	2.789s
FAIL	github.com/yetone/magpie/internal/gateway	2701.113s
?   	github.com/yetone/magpie/internal/ghtoken	[no test files]
FAIL	github.com/yetone/magpie/internal/gui	2702.276s
?   	github.com/yetone/magpie/internal/gui/genpurposes	[no test files]
?   	github.com/yetone/magpie/internal/gui/windowshape	[no test files]
ok  	github.com/yetone/magpie/internal/iconproxy	0.064s
ok  	github.com/yetone/magpie/internal/imagemcp	10.468s
?   	github.com/yetone/magpie/internal/jsmod	[no test files]
ok  	github.com/yetone/magpie/internal/lastgood	2.143s
ok  	github.com/yetone/magpie/internal/libc	0.695s
FAIL	github.com/yetone/magpie/internal/library	2700.445s
ok  	github.com/yetone/magpie/internal/mcpauth	8.003s
?   	github.com/yetone/magpie/internal/mcpauth/mcpauthtest	[no test files]
ok  	github.com/yetone/magpie/internal/middleware	2.122s
ok  	github.com/yetone/magpie/internal/netproxy	20.813s
ok  	github.com/yetone/magpie/internal/omarchy	0.896s
ok  	github.com/yetone/magpie/internal/otlp	39.506s
ok  	github.com/yetone/magpie/internal/plugin	1178.496s
FAIL
FAIL	github.com/yetone/magpie/internal/pluginrt	2.480s
ok  	github.com/yetone/magpie/internal/proc	76.471s
ok  	github.com/yetone/magpie/internal/profile	1742.385s
ok  	github.com/yetone/magpie/internal/provider	2585.981s
ok  	github.com/yetone/magpie/internal/qoder	0.157s
ok  	github.com/yetone/magpie/internal/redact	0.175s
FAIL
FAIL	github.com/yetone/magpie/internal/sessions	749.189s
ok  	github.com/yetone/magpie/internal/settings	638.666s
ok  	github.com/yetone/magpie/internal/shortcut	1.366s
ok  	github.com/yetone/magpie/internal/source	3.323s
ok  	github.com/yetone/magpie/internal/stats	12.980s
ok  	github.com/yetone/magpie/internal/steady	1.933s
ok  	github.com/yetone/magpie/internal/testenv	1.323s
FAIL
FAIL	github.com/yetone/magpie/internal/tui	1020.067s
ok  	github.com/yetone/magpie/internal/tune	0.126s
FAIL
FAIL	github.com/yetone/magpie/internal/update	25.785s
ok  	github.com/yetone/magpie/internal/upstream	0.850s
FAIL
FAIL	github.com/yetone/magpie/internal/usage	1626.844s
ok  	github.com/yetone/magpie/internal/usageapi	40.003s
ok  	github.com/yetone/magpie/internal/wslrun	3.117s
ok  	github.com/yetone/magpie/internal/zed	0.879s
?   	github.com/yetone/magpie/plugins/all	[no test files]
?   	github.com/yetone/magpie/plugins/gateway	[no test files]
?   	github.com/yetone/magpie/plugins/groups	[no test files]
?   	github.com/yetone/magpie/plugins/library	[no test files]
ok  	github.com/yetone/magpie/plugins/otel	12.206s
?   	github.com/yetone/magpie/plugins/routing	[no test files]
?   	github.com/yetone/magpie/plugins/sessions	[no test files]
?   	github.com/yetone/magpie/plugins/usage	[no test files]
?   	github.com/yetone/magpie/plugins/warmups	[no test files]
FAIL

The reds, and why none is this change's:

1. The same tests fail on a pristine 9916a946 checkout with no change — gateway TestClaudeAsRootEnv, TestSweepBridgeProjectsTakesOnlyTheBridgesFolders; library TestAgentsSkillsFolders, TestRemoveFoundSkill, TestHooksCodexTrustMovesWithTheGroups; gui TestFXStateDoesNotWait, TestGatewayHeldByAContainer; pluginrt TestBrokenFileIsKept; usage TestDesktopSessionAccountMetadata; update TestTrialStart. Raw (main checkout, and the branch for the last one):

--- FAIL: TestBrokenFileIsKept (0.01s)
    config_test.go:110: open D:\Temp\TestBrokenFileIsKept2053610231\001\magpie\plugins.yml: is a directory
FAIL	github.com/yetone/magpie/internal/pluginrt	0.080s
--- FAIL: TestDesktopSessionAccountMetadata (0.13s)
    session_provider_test.go:86: Code tab account mapping
FAIL	github.com/yetone/magpie/internal/usage	0.256s
--- FAIL: TestClaudeAsRootEnv (0.00s)
--- FAIL: TestSweepBridgeProjectsTakesOnlyTheBridgesFolders (0.01s)
FAIL	github.com/yetone/magpie/internal/gateway	15.336s
--- FAIL: TestAgentsSkillsFolders (2.89s)
--- FAIL: TestRemoveFoundSkill (0.17s)
--- FAIL: TestHooksCodexTrustMovesWithTheGroups (1.62s)
FAIL	github.com/yetone/magpie/internal/library	7.636s
--- FAIL: TestFXStateDoesNotWait (1.23s)
--- FAIL: TestGatewayHeldByAContainer (39.18s)
FAIL	github.com/yetone/magpie/internal/gui	44.481s
--- FAIL: TestTrialStart (0.05s)   # on main, and the same on the branch
    install_blocked_windows_test.go:56: still held: remove D:\Temp\TestTrialStart1767274202\001\magpie.exe.new: The process cannot access the file because it is being used by another process.
FAIL	github.com/yetone/magpie/internal/update	0.163s

2. Five packages hit the 45m per-binary wall under the load, in tests that pass alone (packages the change does not touch): agent TestClaudeFableTierOnClaude, cli TestModelLimitCmd, gateway TestForeignPagesRefused, gui TestGatewayConnectionAddressesFollowLANSharing, library TestProjectServerChecks. Alone on the branch:

ok	github.com/yetone/magpie/internal/agent	42.880s   (TestClaudeFableTierOnClaude)
ok	github.com/yetone/magpie/internal/cli	19.881s     (TestModelLimitCmd)
# TestForeignPagesRefused, TestGatewayConnectionAddressesFollowLANSharing and TestProjectServerChecks pass in the same isolated runs as the ones below

3. Failed in the suite, pass alone: tui's two gateway tests and sessions TestCursorEditorCopiedOncePerChange:

ok	github.com/yetone/magpie/internal/tui	2.177s
ok	github.com/yetone/magpie/internal/sessions	0.716s

The changed packages, in this same full run: internal/backup ok 1909s, internal/davsync ok 2622s, internal/settings ok 639s. The backup-export path in the one other package that imports Collect passes too: go test -run TestBackupLibraryRoundTrip ./internal/gui → ok 4.817s. And an earlier package run's timeout, internal/backup TestBackupProviderOrderRoundTrip (601s wall then), alone:

ok	github.com/yetone/magpie/internal/backup	6.146s

Everything the change can reach is green; the rest is this box (a nearly-full D:, several agents at once, Windows file holds). CI on the real runners runs the suite anyway.

JasonBuildAI avatar Oct 15 '26 00:10 JasonBuildAI

Fixed in 707fbb249, thanks @JasonBuildAI. backup.Collect now reads settings through settings.LoadChecked and stops with its error, as it does for providers.json. A backup, a GUI export and a sync all end with that error and nothing is sealed or pushed. The sync status shows the error. A missing or empty settings.json behaves as before.

I also fixed one sibling: library.Collect used to treat any failed stat of library.json as "no library". Now only a missing file means that. An unreadable plugins.json still reads as no plugins. That case isn't destructive elsewhere, since bring never removes a plugin, so it is filed as #2287. docs/subsystems/sync-backup.md now says all this.

Tests:

  • TestCollectStopsOnUnreadableSettings (new) and TestSyncStopsOnUnreadableSettings (new; two computers on a fake WebDAV, a's file cut short with no .bak). Both FAIL on the old code and PASS on the new, on macOS and on a Windows x64 box (cross-compiled binaries, sandbox home).
  • gofmt and vet for darwin, linux (nogui) and windows are clean.
  • The full macOS suite passes except gateway's TestMaxRPMTooLongIs429WithRetryAfter, a load flake (#1791) in a package this change doesn't touch.

Not tried: a settings.json held open by another Windows program, or a live WebDAV, S3 or GitHub server. The fix ships in the next release, and I'm keeping this open until then.

yetone avatar Oct 15 '26 02:10 yetone