davsync: a GitHub backup too large for one request goes up in parts (Zhang-Wei, Discord)
Zhang-Wei (Discord, v0.1.1327) saw every GitHub sync fail with "GitHub sync: HTTP 422: Sorry, your input was too large to process. Consider creating the blob in a local clone of the repository and then pushing it to GitHub." Their exported backup is 50.8 MB; with the library's sync off it works.93b87d000sends a backup as one base64 blob in one POST /git/blobs, so 50.8 MB is about 68 MB of request body. GitHub documents 100 MB per blob (on read) but no request size for creating one; a 19 MB file's Contents PUT (about 25 MB of body) did go through. A backup over 16 MiB (githubPart) now goes up in parts, in the same one commit: magpie.magpie-backup.parts/1, /2, ... beside it, and at the backup's own path a short list (format magpie-backup, version 2, split github-parts, total size, each part's blob SHA and size). The version is the list's blob SHA, and the list names each part by blob SHA, so one version pins one whole set. get() reads the list, downloads each part by that SHA (as large single files already were), checks each part's git blob SHA and the sizes, and returns the backup whole; a list that doesn't add up or a part that isn't the one named is an error. A write removes the parts the list at the head named and the new backup doesn't use, in the same commit (only paths known to be there, so the tree never names a missing path). Blobs already sent are kept across a non-fast-forward retry. The errChanged check at the head, the ref update without force and the three rebuilds are unchanged. Backups up to 16 MiB stay one file; the 64 MiB total (four parts) stays, the bound the library's skill budget is sized for. An empty repository's first backup in parts skips the Contents API and follows the README note commit. Why parts, not compression: the sealed bundle is uncompressed JSON with skill files base64 inside, so compressing would shrink it, but that is a backup format change for every backend and every export, every other computer would need the new magpie even for WebDAV and S3, and it still bounds nothing on GitHub's side. Parts keep the format and touch GitHub only. Older magpies: the list opens in backup.Open (unchanged sinceefbe5660b) as format magpie-backup with no kdf and version 2, so it says "this backup was made by a newer magpie; update magpie to open it" and stops: not ErrCorrupt or ErrNotBackup, so no rebuild over it and no replace offered. A backup in one file, from any magpie, still reads. Library: nothing found that shouldn't be carried, so no second commit. readSkills already skips .git and caps skill files at 32 MiB (about 57 MiB sealed). The skills on this machine (~/.claude/skills, 64 skills, 43 MB; ~/.agents/skills) hold no node_modules, __pycache__ or .venv; the large files are real assets (images). Leaving node_modules out could break a skill that ships its dependencies. Zhang-Wei's folders weren't seen. Tests (fixture only, no network): the fixture refuses a blob POST over 24 MiB with GitHub's text, and serves any blob by SHA. New in github_parts_test.go: TestGitHubSplitBackup (50.8 MB in four parts, one commit, byte for byte back, errNotModified, stale put errChanged, then two parts and back to one file with no part left), TestGitHubOneFileBackupStillReads (old small and 30 MB single files read; parts replace one), TestGitHubPartsOlderMagpie, TestGitHubPartsDamaged, TestGitHubPartsBranchMovedMeanwhile, TestGitHubEmptyRepositoryPartsBackup and TestGitHubSyncLargeLibrary (a ~38 MB instruction text syncs to a second computer through Now). TestGitHubLargeBackup now uses 15 MB, to keep covering one blob over the Contents API's limit. docs/subsystems/sync-backup.md says how parts work and names the tests. Record, at base origin/maine435b8867plus this change (Go 1.26.8, GOMODCACHE/GOCACHE pinned): - gofmt -l internal/davsync: nothing. go vet ./... (darwin): clean. GOOS=linux go vet -tags nogui ./internal/davsync/..., GOOS=windows go vet ./internal/davsync/...: clean. - go test -tags nogui -count=1 ./internal/davsync/... ./internal/settings/... ./internal/cli/... ./internal/backup/... ./internal/library/...: all ok (davsync 58s, settings 30s, cli 47s, backup 8s, library 127s). Red on the old github.go (new tests, a shim for the new names): every new test fails, the split ones with the reporter's 422 text, OlderMagpie with "not a magpie backup" (the old code wrote the backup as is). Broken by hand on the new code, each red: - stale parts kept: TestGitHubSplitBackup - part SHA not checked: TestGitHubPartsDamaged/a_part_changed - blobs sent again on retry: PartsBranchMovedMeanwhile/another_file - version check at the head skipped: SplitBackup, PartsBranchMoved/ the_backup, and the older Remote, LargeBackup, BranchMoved tests - no split (threshold at 64 MiB): SplitBackup, OneFileBackupStillReads, PartsOlderMagpie, PartsDamaged - sizes not added up: PartsDamaged/sizes - list's format "magpie-backup-parts": PartsOlderMagpie - empty repository tries the Contents API first: EmptyRepositoryPartsBackup - list's version 1: OlderMagpie still passes, since old Open also stops on the missing kdf; version 2 is a second guard, not the only one. Not tried: the real GitHub API (no test repository here); the 16 MiB part is chosen under the largest body seen accepted, not a documented limit. The full go test ./... and internal/gui's tests were not run: the disk had under 500 MB free. -race not run; no goroutine was added.
This commit is contained in:
@@ -44,6 +44,13 @@ type githubFixture struct {
|
||||
// contentsLimit is the largest file the Contents API's PUT takes;
|
||||
// GitHub answers 422 "too large" over its own (undocumented) limit.
|
||||
contentsLimit int
|
||||
// blobLimit is the largest blob one POST takes. GitHub documents no
|
||||
// request size; it refused a 50.8 MB blob (about 68 MB of base64 body)
|
||||
// with 422 "Sorry, your input was too large to process" (Discord),
|
||||
// and took a 19 MB file's Contents PUT (about 25 MB of body).
|
||||
blobLimit int
|
||||
// biggestBlob is the largest blob a POST sent.
|
||||
biggestBlob int
|
||||
// beforeRef runs, under the lock, before a ref update is judged: a
|
||||
// test commits there as another computer would.
|
||||
beforeRef func()
|
||||
@@ -118,7 +125,7 @@ func githubSHA(b []byte) string {
|
||||
func newGithubFixture(t *testing.T) *githubFixture {
|
||||
t.Helper()
|
||||
f := &githubFixture{objects: map[string][]byte{}, defaultBranch: "main", heads: map[string]string{},
|
||||
blobs: map[string][]byte{}, trees: map[string]fixtureTree{}, commits: map[string]fixtureCommit{}, contentsLimit: 1 << 20}
|
||||
blobs: map[string][]byte{}, trees: map[string]fixtureTree{}, commits: map[string]fixtureCommit{}, contentsLimit: 1 << 20, blobLimit: 24 << 20}
|
||||
srv := httptest.NewServer(http.HandlerFunc(f.serve))
|
||||
t.Cleanup(srv.Close)
|
||||
target, _ := url.Parse(srv.URL)
|
||||
@@ -176,12 +183,18 @@ func (f *githubFixture) serve(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
if strings.HasPrefix(r.URL.Path, base+"/git/blobs/") {
|
||||
sha := strings.TrimPrefix(r.URL.Path, base+"/git/blobs/")
|
||||
for _, b := range f.objects {
|
||||
if githubSHA(b) == sha && r.Header.Get("Accept") == "application/vnd.github.raw+json" {
|
||||
w.Write(b)
|
||||
return
|
||||
// GitHub serves any blob the repository has by its SHA, one a
|
||||
// later commit no longer holds too.
|
||||
b, ok := f.blobs[sha]
|
||||
for _, o := range f.objects {
|
||||
if !ok && githubSHA(o) == sha {
|
||||
b, ok = o, true
|
||||
}
|
||||
}
|
||||
if ok && r.Header.Get("Accept") == "application/vnd.github.raw+json" {
|
||||
w.Write(b)
|
||||
return
|
||||
}
|
||||
fail(404, "Not Found")
|
||||
return
|
||||
}
|
||||
@@ -291,6 +304,11 @@ func (f *githubFixture) serveGit(w http.ResponseWriter, r *http.Request, base st
|
||||
fail(422, "Invalid blob")
|
||||
return true
|
||||
}
|
||||
if len(b) > f.blobLimit {
|
||||
fail(422, "Sorry, your input was too large to process. Consider creating the blob in a local clone of the repository and then pushing it to GitHub.")
|
||||
return true
|
||||
}
|
||||
f.biggestBlob = max(f.biggestBlob, len(b))
|
||||
f.blobs[githubSHA(b)] = b
|
||||
send(201, map[string]string{"sha": githubSHA(b)})
|
||||
case strings.HasPrefix(rest, "commits/") && r.Method == http.MethodGet:
|
||||
@@ -769,7 +787,7 @@ func TestGitHubPublicRepositoryRefused(t *testing.T) {
|
||||
// is read back by its blob SHA.
|
||||
func TestGitHubLargeBackup(t *testing.T) {
|
||||
f := newGithubFixture(t)
|
||||
f.contentsLimit = 19 << 20
|
||||
f.contentsLimit = 10 << 20
|
||||
g, err := newGitHub(githubConfig())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -780,14 +798,14 @@ func TestGitHubLargeBackup(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
large := make([]byte, 21<<20)
|
||||
large := make([]byte, 15<<20) // over the Contents API's, under githubPart
|
||||
for i := range large {
|
||||
large[i] = byte(i*7919 + i>>11)
|
||||
}
|
||||
before := f.writes
|
||||
v, err = g.put(ctx, large, v.ETag)
|
||||
if err != nil {
|
||||
t.Fatalf("a 21 MB backup: %v", err)
|
||||
t.Fatalf("a 15 MB backup: %v", err)
|
||||
}
|
||||
if v.ETag != githubSHA(large) || f.writes != before+1 {
|
||||
t.Fatalf("one commit per update, versioned by the blob: %+v, commits %d", v, f.writes-before)
|
||||
@@ -797,7 +815,7 @@ func TestGitHubLargeBackup(t *testing.T) {
|
||||
}
|
||||
b, got, err := g.get(ctx, version{})
|
||||
if err != nil || got != v || !bytesEqual(b, large) {
|
||||
t.Fatalf("reading the 21 MB backup back: size %d, %+v, %v", len(b), got, err)
|
||||
t.Fatalf("reading the 15 MB backup back: size %d, %+v, %v", len(b), got, err)
|
||||
}
|
||||
if !slices.Contains(f.requests, "GET /repos/alice/private/git/blobs/"+v.ETag) {
|
||||
t.Fatal("a file over 1 MB must be downloaded by its blob SHA")
|
||||
|
||||
Reference in New Issue
Block a user