Merge branch 'fix-worktree-diff-capture'

This commit is contained in:
2026-10-18 23:17:38 +01:00
3 changed files with 55 additions and 7 deletions
@@ -0,0 +1,12 @@
# Binary snapshots no longer consume the source patch budget
Tracked screenshots exhausted the 32 MiB retained snapshot budget before source
files were visited. Captures could therefore show empty file diffs for real code edits.
Binary files now retain only hashes and metadata; text remains bounded to 32 MiB,
with existing per-file, file-count, time and patch limits unchanged.
The operations skill reference's capture-limit explanation is stale: distinguish
retained text bytes from transient bounded binary reads. Existing historical empty
captures are not backfilled and remain incomplete. Commit-only stages may have no
stage-local content change; run summaries should retain earlier patches rather
than treating a commit or absent capture as proof of no code changes.
+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)
}
}
}