Keep binary artifacts from exhausting source diff capture budget

This commit is contained in:
2026-10-18 23:17:13 +01:00
parent bfcfcde415
commit ae6bcff270
3 changed files with 55 additions and 7 deletions
+15 -7
View File
@@ -183,11 +183,6 @@ func Capture(ctx context.Context, directory string, previous ...*Snapshot) *Snap
s.skipped[name] = true
continue
}
if used+int(info.Size()) > maxSnapshotBytes {
s.skipped[name] = true
s.partial = true
continue
}
f, err := root.OpenFile(name, os.O_RDONLY|syscall.O_NONBLOCK|syscall.O_NOFOLLOW, 0)
if err != nil {
s.skipped[name] = true
@@ -205,9 +200,22 @@ func Capture(ctx context.Context, directory string, previous ...*Snapshot) *Snap
s.skipped[name] = true
continue
}
used += len(data)
binary := bytes.IndexByte(data, 0) >= 0 || !utf8.Valid(data)
s.files[name] = file{data: data, hash: sha256.Sum256(data), mode: opened.Mode().Perm(), binary: binary}
hash := sha256.Sum256(data)
// Binary changes need only metadata and a hash, never retained patch bytes.
// Counting screenshots against the retained-text budget can otherwise
// exhaust it before any source files are reached in sorted inventory order.
if binary {
data = nil
} else {
if used+len(data) > maxSnapshotBytes {
s.skipped[name] = true
s.partial = true
continue
}
used += len(data)
}
s.files[name] = file{data: data, hash: hash, mode: opened.Mode().Perm(), binary: binary}
}
return s
}
+28
View File
@@ -2,6 +2,7 @@ package filediff
import (
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
@@ -161,3 +162,30 @@ func TestPatchLimits(t *testing.T) {
t.Fatal(c)
}
}
func TestBinaryFilesDoNotStarveSourceDiffs(t *testing.T) {
dir := t.TempDir()
ctx := context.Background()
image := "\x00" + strings.Repeat("x", maxFileBytes-1)
for i := 0; i <= maxSnapshotBytes/maxFileBytes; i++ {
put(t, dir, fmt.Sprintf("image-%03d.png", i), image)
}
put(t, dir, "source.go", "before\n")
before := Capture(ctx, dir)
put(t, dir, "source.go", "after\n")
put(t, dir, "image-000.png", "\x00changed")
changes := Compare(before, Capture(ctx, dir, before))
if changes.Status != "complete" || len(changes.Files) != 2 {
t.Fatalf("binary inventory hid source changes: %+v", changes)
}
for _, change := range changes.Files {
if change.Path == "source.go" && !strings.Contains(change.Patch, "+after") {
t.Fatal("source patch missing", change)
}
}
for name, f := range before.files {
if f.binary && len(f.data) != 0 {
t.Fatalf("binary bytes unnecessarily retained for %s", name)
}
}
}