Files
magpie/docs/subsystems/sync-backup.md
T
yetone 93b87d0004 davsync: a GitHub backup too large for the Contents API goes up as a blob (Zhang-Wei, Discord)
Zhang-Wei (Discord, v0.1.1318, 102 skills) saw every sync fail with
"GitHub sync: HTTP 422: Sorry, the file is too large to be processed"
on magpie/magpie.magpie-backup; the last backup that went up was about
19 MB. The Contents API's PUT refuses files well under the 100 MB a blob
can hold.

A repository with commits is now written through the Git Data API:
the ref's head, the file's version at that head (it must still be the one
sync read, or errChanged), a base64 blob, a tree on the head's tree, a
commit with the head as its parent, and a ref update without force. Still
one commit per write or removal, and the version is the blob SHA as
before. A ref update that is not a fast-forward is built again on the new
head (the blob is kept), up to three times, then errChanged; if the new
head changed the backup it is errChanged at once, so another computer's
backup goes through the normal merge. A branch deleted meanwhile is an
error, not created. Usage files and their removal take the same path.

An empty repository has no head to build on, so its first write still
goes through the Contents API (one commit, unchanged). If that is refused
as too large (422 "too large" or 413), a short README.md in magpie's
folder makes the first commit and the backup follows as the second.

Reading was already right for large files: the object media type gives
encoding "none" over 1 MB and bytes() downloads by blob SHA; the new
test covers it at 21 MB.

The fixture now serves the Git Data API (blobs, commits, trees, a
fast-forward-only ref update) and refuses a Contents PUT over
contentsLimit with GitHub's 422 text. docs/subsystems/sync-backup.md says
how writes go and names the new tests.

Record for this push: 13 commits on origin/main ca4a1cde0, head tree
539c84e92. Go 1.26.8, sandbox HOME, GOMODCACHE/GOCACHE pinned. The full Go
run and vet were at tree d6393dfc8, the same Go code as this head: only
app.js comments (cut under the core script budget) and one ja string
changed after it.
- gofmt -l: nothing. go build ./...; go vet ./... (darwin), GOOS=linux
  go vet -tags nogui ./..., GOOS=windows go vet ./...: clean.
- go test -timeout 45m ./..., through tee, one suite on the machine:
  68 packages ok. The one red was internal/gui's
  TestCoreScriptBudget: app.js 1,335,314 > 1,335,000, since main grew
  app.js under the stack. The autoscroll commit's own comments were cut
  (to 1,334,629) rather than the budget raised, and go test -count=1
  ./internal/gui is ok at this head (113s). Packages the run took from
  Go's test cache had the same inputs.
- GUI: the 18 files this stack touches or names, both engines,
  202 of 204 pass (tree 068acf128, this push less the last commit). The
  two reds: gui-ja, red on main since c2636b7b5 and fixed by this push's
  last commit (gui-ja and i18n-keys 6 of 6 there), and usage-fold's
  chromium "several accounts" (#2163, red on main the same way).
- internal/davsync and internal/cli ok, the GitHub large-backup tests and
  TestGitHubCmd among them.
- s3-sync.test.cjs (WebKit en) is red alone on main too: #2448, the
  header over the form's tick. This commit doesn't touch the GUI.
  The whole GUI suite ran at 1c1fc7183 (this stack's first 12 commits on
  0ddcf28a1): 11713 of 11769 pass. Each of the 21 red files was then run
  alone at the stack and at origin/main 8278fd9b0. Eight are red the same
  way on main: plugin-card-update (#3079), quota-count-wrap and
  quota-name-translated (#2228), s3-sync (#2448), usage-agents-hide
  (#2276), usage-checkin (#3091), usage-fold's chromium "several
  accounts" (#2163), usage-ledger (#3104). The other 13 pass alone on both
  and were red only under that run's load (50 to 335): currency #3101,
  plugin-github #3218, provider-fetch-again #3292, rest-try-now #2564,
  routing-auto-reset #3293, routing-keys-fold #1612, routing-steady #1804,
  routing-wb-restricted #1905, sessions-folder-delete #2218,
  sessions-manage #3294, switch-only #3295, text-size #1866, usage-pack
  #3296.

Red on the old github.go (the new tests with the old code):
TestGitHubLargeBackup and TestGitHubEmptyRepositoryLargeBackup fail with
"HTTP 422: Sorry, the file is too large to be processed", TestGitHubRemote
too at its 1.26 MB put, and TestGitHubBranchMovedMeanwhile's three
subtests fail.

Broken by hand on the new code, each red:
- version check at the head skipped: TestGitHubRemote, LargeBackup's
  stale put, BranchMovedMeanwhile/the_backup
- no rebuild on a non-fast-forward: BranchMovedMeanwhile/another_file
  and /the_backup
- large read by base64 only: TestGitHubRemote, TestGitHubLargeBackup
- no README initialization: TestGitHubEmptyRepositoryLargeBackup (409)

Not tried against the real
GitHub API: the fixture follows GitHub's documented Git Data API, and
GitHub's exact Contents API size limit is not documented.
2026-10-23 01:58:00 +08:00

9.2 KiB

Sync and backup

internal/backup collects magpie's portable setup into a passphrase-sealed bundle. internal/davsync keeps that bundle in a WebDAV folder, an S3 bucket or a GitHub repository. It merges changes between computers; it does not clone a working tree or synchronize arbitrary files.

Sources of truth

  • Config, Configure, Status and syncOnce keep the active server in sync.json, and the last local/remote hashes, versions, notices and undo file in sync-state.json. These files live beside magpie's settings. Secrets are local, never in the status response.
  • The remote interface reads a version and conditionally replaces it. WebDAV and S3 use ETags; GitHub uses the file's blob SHA. github.go interprets github://owner/repo[/prefix], with Config.Branch selecting a branch or using the repository's default.
  • backup.go owns bundle contents and sealing. Sync divides the setup into providers, settings, profiles, agents and library. Provider keys, agent models, library and usage have their own inclusion switches.
  • usage.go shares separately sealed usage and quota files through a remote's files interface.
  • restore.go replaces local parts from the remote bundle, first keeping the local setup for Undo.
  • backup.go, the Settings form in app.js and webdav_cli.go reach the same sync operations.

What stays on each computer

The settings part carries the user's preferences, but not what is only about one computer. Settings.KeepOwn puts those back from this computer's settings whenever a sync, a join, a Restore, an Undo or a backup file brings settings in, and hashes blanks them, so changing one is no change to push. They are: the window's size and whether it was maximised, the fonts, the proxy, the gateway's port, sharing on the local network (LAN) and the addresses let in to it (AllowIPs), the private hosts magpie may download from (FetchPrivate), the Dock, gateway mode, keeping awake, lightweight windows, WSL lookups, account discovery, when allowances are read, recording gateway conversations, the Mac terminal a session resumes in (SessionTerminal) and what the menu bar shows. Launch at login is the system's own setting (internal/autostart) and the listen address is MAGPIE_ADDR, so neither is in a bundle.

Sharing turned on at one computer never turns it on or off at another (Lullaby on Discord). A backup restored onto a new computer leaves sharing as that computer has it, off on a fresh one: sharing opens the gateway to the network, and the user turns it on there by hand, as the port and proxy are set there. The default sharing key is different: it is in the gateway-key store, which rides the settings part with LANKey and LANKeyID, so every computer holds the same one, and sharing turned on at a second computer takes it up rather than making another (TestLANSharingStaysOnItsComputer).

Runtime and state

The gateway runs sync every three minutes by default. SetAuto can select a longer interval or manual sync only. Saving in Settings syncs immediately. Each operation holds the sync lock, so configuration changes cannot race a sync's state write.

A first computer creates a remote backup. Another computer joining takes the remote setup and keeps displaced local parts in its sync folder. Subsequent syncs compare each part with the hashes from the previous sync: local-only changes go up, remote-only changes come down, and a part changed on both sides keeps the newer version while retaining the replaced copy. A conditional write refused because another computer committed in between is retried through this same merge.

When a remote file is empty, damaged or another app's format, sync stops and describes it through ServerFile. Settings and the backend's upload command can replace it with this computer's setup, keeping the old file first. HTML and XML are not offered for replacement. GitHub diagnostics name the repository and magpie github upload, rather than WebDAV's recovery path.

A local file that is there but can't be read is unknown, never empty or the defaults. backup.Collect stops on an unreadable providers.json (with its groups and order), settings.json (settings.LoadChecked: unreadable, or not parsing with no good .bak) or library.json, and on a gateway key store that fails to export. A backup, an export and a sync all end with that error, nothing sealed or pushed; the sync's status shows it. A missing or empty settings.json is still none (TestCollectStopsOnUnreadableSettings, TestSyncStopsOnUnreadableProviders, TestSyncStopsOnUnreadableSettings, #2269).

Switching services keeps all inactive kinds in Config.Servers. Other remains the most recently left server for compatibility with older saved configurations and clients. Only the active server is used. Empty credential fields reuse a saved credential only for its matching account; GitHub restricts reuse to the same owner/repo. Branch or prefix changes within that repository can keep the token. The branch is part of the sync state key.

GitHub constraints and errors

  • The repository must exist and be private: prepare refuses one whose repository read doesn't say "private": true, since a public history hands every sealed version to anyone to guess the passphrase against (TestGitHubPublicRepositoryRefused). In an empty repository, the first encrypted backup initializes its default branch through the Contents API. When that API refuses it as too large, a short README.md in magpie's folder makes the first commit and the backup follows as the second. A selected branch other than the default must already exist. The token needs repository Contents read and write.
  • Backups are <prefix>/magpie/magpie.magpie-backup; optional usage files live beside them under usage/. Each write or removal is one commit, and unchanged setup does not create another backup commit.
  • A repository with commits is written through the Git Data API: a blob (up to 100 MB), a tree on the head commit's tree, a commit with the head as its parent, and a ref update without force. The Contents API's PUT refuses files well under that with 422 "the file is too large to be processed" (a 102-skill backup past ~19 MB, Discord). The file must still be at the version sync read, checked at the head commit, before the commit is built. A ref update that is not a fast-forward is built again on the new head, up to three times; if the new head changed the file it is errChanged. A branch deleted meanwhile is an error, never created.
  • Tokens are separate from the library's GitHub token, sent only to https://api.github.com, and must differ from the encryption passphrase. Repository history retains older encrypted versions.
  • Repository and branch access is checked before a file's 404 can mean "no backup yet." Failed reads and malformed metadata fail sync.
  • Only the ref endpoint's explicit 409: Git Repository is empty. allows initialization, after repository access and the default branch are checked. The initial write omits branch so GitHub creates its default branch; other conflicts and missing branches are not treated as an empty repository.
  • Large files are downloaded by blob SHA, preserving the version that was read. Sync files are limited to 64 MiB. Usage listing fails at the Contents API's 1,000-file limit rather than treating a truncated list as complete.
  • A stale SHA, a backup another commit changed, or a racing initial creation returns errChanged. Other 422 responses stay actionable errors. Authentication, permission and branch failures remain distinct from missing files. Rate limits use sync backoff.
  • Request archival remains an S3-only feature; its objects are separate from the encrypted setup and usage files.

Verification

go test -tags nogui ./internal/davsync
go test -tags nogui ./internal/cli -run 'TestGitHubCmd|TestWebdavCmd|TestS3Cmd'
node --test internal/gui/tests/github-sync.test.cjs internal/gui/tests/s3-sync.test.cjs internal/gui/tests/sync-other-kind.test.cjs

TestGitHubEmptyRepository covers the first encrypted backup initializing an empty repository and an unchanged second sync. TestGitHubEmptyRepositoryFailures covers missing branches, unrelated ref conflicts and another computer winning the initial write. TestGitHubLargeBackup writes a 21 MB backup past a fixture whose Contents API refuses it, as one commit, and reads it back by blob SHA. TestGitHubBranchMovedMeanwhile covers a head that moved before the ref update, and TestGitHubEmptyRepositoryLargeBackup a large first backup. TestGitHubNotBackup covers backend-specific recovery advice when the repository holds an empty or foreign file.

Go tests use testenv homes and HTTP fixtures. Browser tests use isolated API fixtures in Chromium and WebKit. A live GitHub write requires a test repository and token; fixture tests do not establish that a particular account's permissions or repository rules allow commits.