From 2b2d253c9afeb239958ee8a5cb9be2cc8ce0cb1f Mon Sep 17 00:00:00 2001 From: Matt Miller Date: Thu, 20 Aug 2026 13:32:01 -0700 Subject: [PATCH 1/2] fix(pr-size): require a rename's SOURCE to be linguist-generated too (BE-8137) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit classify() excluded a rename on the linguist-generated attribute of the DESTINATION path alone, while the three sibling exclusion buckets on the adjacent lines each also require OldPath to match. Renaming a hand-written file into a linguist-generated tree and editing it in the same commit therefore hid the edit from BOTH the size count and the --reviewed-diff-out patch fed to cursor-review. Feed OldPath into the batched `git check-attr` lookup so its attribute is resolved, then require both ends to carry the attribute — mirroring how IsTestPath / IsLockfile / extras.Generated already treat renames. Strictly conservative: every changed direction moves toward counting and reviewing MORE. The base-ref invariant is unchanged; attributes still come from the base .gitattributes, never the PR head. The omission dates to ab2066a, which extended the both-paths rule to the lockfile and extra-glob buckets but left the attribute one bare. --- scripts/check-pr-size/main.go | 18 +++++-- scripts/check-pr-size/main_test.go | 86 ++++++++++++++++++++++++++++++ 2 files changed, 101 insertions(+), 3 deletions(-) diff --git a/scripts/check-pr-size/main.go b/scripts/check-pr-size/main.go index 40e61a0f..dc18ce66 100644 --- a/scripts/check-pr-size/main.go +++ b/scripts/check-pr-size/main.go @@ -320,11 +320,16 @@ func attrTrusted(useSource, attrModified, bypass bool) bool { // caller opted to exclude it. // // A RENAME is classified on BOTH its old and new path for every exclusion -// bucket this function decides — lockfile, extra-generated glob, and test. +// bucket this function decides — lockfile, extra-generated glob, the +// linguist-generated attribute, and test. // `git diff --numstat` books a rename's deletions against the destination, so // classifying on the destination alone would let e.g. `git mv src/big.go // tests/big.go` (or `git mv src/big.go go.sum`) charge removed PRODUCTION // lines to an excluded bucket, silently slipping the change under the cap. +// The attribute bucket has the same hole with an extra sting: a rename into a +// `linguist-generated` tree hides the moved file from the --reviewed-diff-out +// patch as well as from the count, so an edit made in the same commit reaches +// neither the size verdict nor the review panel. // Requiring both paths to agree keeps every one of these failures in the safe, // over-counting direction. // @@ -343,6 +348,13 @@ func classify(files []FileChange, base, head string, attr attrPolicy, extras Ext paths := make([]string, 0, len(files)) for i := range files { paths = append(paths, files[i].Path) + // A rename's source path needs its attribute resolved too, or the + // both-paths check below has nothing to read. check-attr resolves + // rules for arbitrary path strings, so batching it is safe whether + // or not the path still exists. + if files[i].OldPath != "" { + paths = append(paths, files[i].OldPath) + } } attrGen = attrGeneratedBatch(paths, attr.source, attr.useSource) } @@ -353,8 +365,8 @@ func classify(files []FileChange, base, head string, attr attrPolicy, extras Ext f.Test = IsTestPath(f.Path) && (f.OldPath == "" || IsTestPath(f.OldPath)) lockfile := IsLockfile(f.Path) && (f.OldPath == "" || IsLockfile(f.OldPath)) extraGen := extras.Generated(f.Path) && (f.OldPath == "" || extras.Generated(f.OldPath)) - if lockfile || extraGen || - attrGen[f.Path] || + attrGenOK := attrGen[f.Path] && (f.OldPath == "" || attrGen[f.OldPath]) + if lockfile || extraGen || attrGenOK || (!f.Binary && contentGenerated(f.Path, base, head, markerFromBase)) { f.Generated = true } diff --git a/scripts/check-pr-size/main_test.go b/scripts/check-pr-size/main_test.go index 54080f11..640967e9 100644 --- a/scripts/check-pr-size/main_test.go +++ b/scripts/check-pr-size/main_test.go @@ -338,6 +338,92 @@ func TestClassifyHonorsLegitBaseGitattributes(t *testing.T) { } } +// TestClassifyRenameIntoAttrGeneratedTreeStillCounts is the attribute half of +// the rename anti-gaming guard (the path-based buckets are covered by +// TestRenameIntoExclusionBucketsStillCounts, which cannot reach this one — the +// attribute lookup needs a real repo). Renaming a hand-written file INTO a +// `linguist-generated` tree and editing it in the same commit must not hide the +// edit: classifying on the destination alone excluded it from both the size +// count and the --reviewed-diff-out patch fed to the review panel. A rename +// whose BOTH ends carry the attribute is still excluded, so genuine moves +// inside a generated tree are unaffected. +func TestClassifyRenameIntoAttrGeneratedTreeStillCounts(t *testing.T) { + dir := initTestRepo(t) + body := strings.Repeat("{\"k\": \"v\"}\n", 40) + writeFile(t, dir, ".gitattributes", "gen/** linguist-generated=true\n") + writeFile(t, dir, "handwritten/x.json", body) + writeFile(t, dir, "gen/keep.json", body) + base := commitAll(t, dir, "base") + + if !checkAttrSourceSupported(base) { + t.Skip("git too old for check-attr --source") + } + + // PR head, touching NOTHING under .gitattributes: a hand-written file moves + // into the generated tree carrying an injected edit, and a genuinely + // generated file moves within that tree. + gitRun(t, dir, "mv", "handwritten/x.json", "gen/x.json") + writeFile(t, dir, "gen/x.json", body+"{\"injected\": \"marker\"}\n") + gitRun(t, dir, "mv", "gen/keep.json", "gen/keep2.json") + head := commitAll(t, dir, "head") + t.Chdir(dir) + + files, err := diffFiles(base, head) + if err != nil { + t.Fatalf("diffFiles: %v", err) + } + attr := attrPolicy{source: base, useSource: true} + attr.trusted = attrTrusted(attr.useSource, TouchesGitattributes(files), false) + if !attr.trusted { + t.Fatal("attribute path should be trusted (PR does not touch .gitattributes)") + } + classify(files, base, head, attr, Extras{}, false) + + byPath := make(map[string]FileChange, len(files)) + for _, f := range files { + byPath[f.Path] = f + } + moved, ok := byPath["gen/x.json"] + if !ok { + t.Fatalf("git did not report gen/x.json as changed; got %+v", files) + } + if moved.OldPath != "handwritten/x.json" { + t.Fatalf("gen/x.json OldPath = %q, want handwritten/x.json (rename detection did not fire)", moved.OldPath) + } + if moved.Generated { + t.Error("a hand-written file renamed INTO a linguist-generated tree must not be classified generated — its edit would vanish from both the count and the reviewed diff") + } + kept, ok := byPath["gen/keep2.json"] + if !ok { + t.Fatalf("git did not report gen/keep2.json as changed; got %+v", files) + } + if !kept.Generated { + t.Error("a rename whose source AND destination both carry linguist-generated should stay excluded") + } + + res := Evaluate(files, Policy{Max: 1000}) + if res.Counted == 0 { + t.Errorf("Counted = %d, want the injected edit counted", res.Counted) + } + + // End-to-end on the second symptom: the injected edit must reach the patch + // the review panel reads, while the genuinely generated rename stays out. + out := filepath.Join(t.TempDir(), "pr-diff.patch") + if err := writeReviewedDiff(base, head, nil, files, out); err != nil { + t.Fatalf("writeReviewedDiff: %v", err) + } + patch, err := os.ReadFile(out) + if err != nil { + t.Fatal(err) + } + if !strings.Contains(string(patch), "injected") { + t.Error("the injected edit is missing from the --reviewed-diff-out patch — the review panel would never see it") + } + if strings.Contains(string(patch), "keep2.json") { + t.Error("a rename entirely inside a generated tree should still be dropped from the reviewed diff") + } +} + // TestWriteReviewedDiff proves the end-to-end --reviewed-diff-out path against // real git: the emitted patch keeps the hand-written file's section, drops the // generated file's section entirely, and honors a verbatim exclude pathspec — From 76bf67ab10448b8f4d0245781c02edb1281b2b81 Mon Sep 17 00:00:00 2001 From: Matt Miller Date: Thu, 20 Aug 2026 13:59:43 -0700 Subject: [PATCH 2/2] fix(pr-size): pair renames in annotateDiscounts so the guard isn't cancelled (BE-8137) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Review round on the both-paths rename guard. annotateDiscounts built its patch with a pathspec naming only the DESTINATION, so git could not pair a rename and re-emitted it as a whole-file ADDITION. Every blank/comment line in the file then counted as a discount and the `d > Changed()` clamp drove Counted() to 0 — silently cancelling the count half of classify()'s rename guard for any caller running with --ignore-comments (cursor-review's ignore_comments input feeds exactly that). Verified end to end: `git mv handwritten/big.go gen/big.go` plus a one-line edit reported `Counted = 0` before, `Counted = 1` after. Feed OldPath as an extra pathspec, mirroring what classify() now does for check-attr. Discounts stay keyed by destination, so an unpaired old-path section simply finds no entry and the file counts raw. Test hardening on the new attribute test: - give the two base files distinct bodies, so gen/keep2.json is an unambiguous exact-rename of gen/keep.json; byte-identical bodies left the pairing to git's hashmap tie-break and could invert both OldPath assertions. - move the `check-attr --source` probe after t.Chdir, so it runs where `base` is a valid object; from the outer repo it failed as "not a valid object", which the helper reports as supported, so the skip could never fire. Documented two residuals raised in review rather than changing behavior: the content-marker bucket is destination-only by design (it fails closed under --marker-from-base, and without that flag a rename grants no evasion an in-place marker paste does not already grant), and the both-paths rule inherits whatever pairing git chooses, which a same-content file planted in the generated tree at base can bias. --- scripts/check-pr-size/main.go | 44 ++++++++++++++-- scripts/check-pr-size/main_test.go | 81 ++++++++++++++++++++++++++++-- 2 files changed, 117 insertions(+), 8 deletions(-) diff --git a/scripts/check-pr-size/main.go b/scripts/check-pr-size/main.go index dc18ce66..23bb9ed5 100644 --- a/scripts/check-pr-size/main.go +++ b/scripts/check-pr-size/main.go @@ -241,6 +241,18 @@ func diffFiles(base, head string) ([]FileChange, error) { // that file counts raw — a failure can only make the cap STRICTER, never sneak a // large change under it. Restricts the patch to the counted paths, so a large // generated diff is never fetched or parsed. +// +// A RENAME contributes BOTH of its paths to that pathspec. Naming only the +// destination hides the deletion side from the filtered diff, so git cannot pair +// the rename and re-emits the destination as a whole-file ADDITION — every +// blank/comment line in the file then counts as a discount, and the `d > +// Changed()` clamp below drives the rename's Counted() to 0. That would silently +// cancel classify()'s both-paths rename guard for any caller running with +// --ignore-comments: `git mv handwritten/big.go gen/big.go` plus a large edit is +// correctly classified non-generated and then discounted back down to nothing. +// Discounts stay keyed by DESTINATION (idx holds f.Path only), matching the +// paired diff's `+++` header; an unpaired old-path section finds no idx entry +// and is skipped, so the file just counts raw. func annotateDiscounts(files []FileChange, base, head string) { idx := make(map[string]*FileChange, len(files)) paths := make([]string, 0, len(files)) @@ -250,6 +262,9 @@ func annotateDiscounts(files []FileChange, base, head string) { continue } paths = append(paths, f.Path) + if f.OldPath != "" { + paths = append(paths, f.OldPath) + } idx[f.Path] = f } if len(paths) == 0 { @@ -319,9 +334,11 @@ func attrTrusted(useSource, attrModified, bypass bool) bool { // regardless of policy, so the report can show the test total whether or not the // caller opted to exclude it. // -// A RENAME is classified on BOTH its old and new path for every exclusion -// bucket this function decides — lockfile, extra-generated glob, the -// linguist-generated attribute, and test. +// A RENAME is classified on BOTH its old and new path for every PATH- and +// ATTRIBUTE-based exclusion bucket this function decides — lockfile, +// extra-generated glob, the linguist-generated attribute, and test. (The +// content-marker bucket is destination-only; see contentGenerated for why that +// is not a rename hole.) // `git diff --numstat` books a rename's deletions against the destination, so // classifying on the destination alone would let e.g. `git mv src/big.go // tests/big.go` (or `git mv src/big.go go.sum`) charge removed PRODUCTION @@ -333,6 +350,15 @@ func attrTrusted(useSource, attrModified, bypass bool) bool { // Requiring both paths to agree keeps every one of these failures in the safe, // over-counting direction. // +// KNOWN LIMIT of the both-paths rule: it can only be as good as git's rename +// pairing, which an adversary with commit access to the base can bias. If the +// generated tree already holds a file byte-identical at base to the one being +// moved in, moving both in one PR lets the exact-rename phase pair the untouched +// destination with the hand-written source, leaving the EDITED destination +// paired with the in-tree generated source — both attribute-carrying, so the +// edit is excluded again. It needs a same-content generated file planted at +// base, so it is a documented residual, not the reported defect. +// // Binary files are classified by the path- and attribute-based rules too (none // of those read content), so a binary lockfile or a binary matching an extra // glob is still excluded from a --reviewed-diff-out consumer's diff; only the @@ -472,6 +498,18 @@ func isUnknownFlagError(stderr string) bool { // code by prepending one comment line. Base-only means a file new in the PR // never matches the marker (it is counted and reviewed — conservative); files // generated at base stay excluded. +// +// Unlike classify's path/attribute buckets, this one reads the DESTINATION only, +// and deliberately so — a rename grants no evasion here that an in-place edit +// does not already grant. With markerFromBase (what --reviewed-diff-out sets) a +// rename's destination does not exist at base, the show fails, and the bucket +// returns false: it fails CLOSED, counting and reviewing the file. Without it, +// the head read can be self-marked — but `git mv handwritten/big.go gen/big.go` +// plus a pasted marker excludes exactly what pasting the marker into +// handwritten/big.go in place already excludes, so the move adds nothing. That +// residual is the accepted count-only tradeoff described above, not a rename +// hole; the fix for it is --marker-from-base, which every evadability-sensitive +// caller already passes. func contentGenerated(path, base, head string, markerFromBase bool) bool { if !strings.HasSuffix(path, ".go") { return false diff --git a/scripts/check-pr-size/main_test.go b/scripts/check-pr-size/main_test.go index 640967e9..6878574e 100644 --- a/scripts/check-pr-size/main_test.go +++ b/scripts/check-pr-size/main_test.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "os" "os/exec" "path/filepath" @@ -349,16 +350,18 @@ func TestClassifyHonorsLegitBaseGitattributes(t *testing.T) { // inside a generated tree are unaffected. func TestClassifyRenameIntoAttrGeneratedTreeStillCounts(t *testing.T) { dir := initTestRepo(t) + // The two base files must NOT be byte-identical: gen/keep2.json below is an + // exact-blob rename of gen/keep.json, and if handwritten/x.json carried the + // same bytes git's exact-rename phase could pair keep2 with the hand-written + // source instead (neither basename matches, so the tie-break is hashmap + // order and varies by git version), inverting both OldPath assertions. body := strings.Repeat("{\"k\": \"v\"}\n", 40) + keepBody := strings.Repeat("{\"keep\": \"generated\"}\n", 25) writeFile(t, dir, ".gitattributes", "gen/** linguist-generated=true\n") writeFile(t, dir, "handwritten/x.json", body) - writeFile(t, dir, "gen/keep.json", body) + writeFile(t, dir, "gen/keep.json", keepBody) base := commitAll(t, dir, "base") - if !checkAttrSourceSupported(base) { - t.Skip("git too old for check-attr --source") - } - // PR head, touching NOTHING under .gitattributes: a hand-written file moves // into the generated tree carrying an injected edit, and a genuinely // generated file moves within that tree. @@ -368,6 +371,14 @@ func TestClassifyRenameIntoAttrGeneratedTreeStillCounts(t *testing.T) { head := commitAll(t, dir, "head") t.Chdir(dir) + // Probe AFTER the chdir: base is a SHA in this throwaway repo, so running it + // from the outer repo would fail for "not a valid object" — which + // checkAttrSourceSupported deliberately reports as supported — and the skip + // could never fire on a runner whose git predates check-attr --source. + if !checkAttrSourceSupported(base) { + t.Skip("git too old for check-attr --source") + } + files, err := diffFiles(base, head) if err != nil { t.Fatalf("diffFiles: %v", err) @@ -424,6 +435,66 @@ func TestClassifyRenameIntoAttrGeneratedTreeStillCounts(t *testing.T) { } } +// TestAnnotateDiscountsPairsRenamesSoTheEditStillCounts guards the downstream +// half of the rename guard. classify() correctly leaves a hand-written file +// renamed out of its tree non-generated, but annotateDiscounts then re-derives a +// patch from git; naming only the DESTINATION in that pathspec hid the deletion +// side, git could not pair the rename, and it re-emitted the file as a +// whole-file ADDITION whose every comment/blank line became a discount. Clamped +// at Changed(), that drove the rename's Counted() to 0 — silently cancelling the +// guard for any caller running with --ignore-comments (cursor-review's +// ignore_comments input feeds exactly that). +func TestAnnotateDiscountsPairsRenamesSoTheEditStillCounts(t *testing.T) { + dir := initTestRepo(t) + // Comment-heavy on purpose: unpaired, the whole-file addition discounts far + // more lines than the rename actually changed, so the clamp zeroes it out. + var sb strings.Builder + sb.WriteString("package hw\n") + for i := 0; i < 200; i++ { + fmt.Fprintf(&sb, "// comment line %d\n\nvar V%d = %d\n", i, i, i) + } + writeFile(t, dir, "handwritten/big.go", sb.String()) + base := commitAll(t, dir, "base") + + if err := os.MkdirAll(filepath.Join(dir, "moved"), 0o755); err != nil { + t.Fatal(err) + } + gitRun(t, dir, "mv", "handwritten/big.go", "moved/big.go") + writeFile(t, dir, "moved/big.go", sb.String()+"var Injected = 42\n") + head := commitAll(t, dir, "head") + t.Chdir(dir) + + files, err := diffFiles(base, head) + if err != nil { + t.Fatalf("diffFiles: %v", err) + } + classify(files, base, head, attrPolicy{}, Extras{}, false) + + var moved *FileChange + for i := range files { + if files[i].Path == "moved/big.go" { + moved = &files[i] + } + } + if moved == nil { + t.Fatalf("git did not report moved/big.go as changed; got %+v", files) + } + if moved.OldPath != "handwritten/big.go" { + t.Fatalf("moved/big.go OldPath = %q, want handwritten/big.go (rename detection did not fire)", moved.OldPath) + } + if moved.Generated { + t.Fatal("a plain hand-written rename must not be classified generated") + } + + annotateDiscounts(files, base, head) + if moved.Discounted != 0 { + t.Errorf("Discounted = %d, want 0 — the rename was re-read as a whole-file addition", moved.Discounted) + } + if got := Evaluate(files, Policy{Max: 1000}).Counted; got != 1 { + t.Errorf("Counted = %d, want 1 (the injected line); discounting cancelled the rename guard", got) + } +} + // TestWriteReviewedDiff proves the end-to-end --reviewed-diff-out path against // real git: the emitted patch keeps the hand-written file's section, drops the // generated file's section entirely, and honors a verbatim exclude pathspec —