diff --git a/scripts/check-pr-size/main.go b/scripts/check-pr-size/main.go index 40e61a0f..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,15 +334,31 @@ 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, 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 // 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. // +// 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 @@ -343,6 +374,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 +391,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 } @@ -460,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 54080f11..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" @@ -338,6 +339,162 @@ 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) + // 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", keepBody) + base := commitAll(t, dir, "base") + + // 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) + + // 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) + } + 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") + } +} + +// 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 —