-
Notifications
You must be signed in to change notification settings - Fork 1
feat: cost controls — pre-flight estimation, token budget, file priority #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| package diff | ||
|
|
||
| import ( | ||
| "path/filepath" | ||
| "sort" | ||
| "strings" | ||
| ) | ||
|
|
||
| // securityPatterns are path substrings that indicate security-sensitive files. | ||
| var securityPatterns = []string{ | ||
| "auth", "session", "token", "secret", "secrets", "password", "credential", | ||
| "crypto", "security", "permission", "rbac", "acl", "oauth", | ||
| "saml", "jwt", "cert", "tls", "ssl", "key", | ||
| } | ||
|
|
||
| // securityFiles are exact filenames or extensions that are security-sensitive. | ||
| var securityFiles = []string{ | ||
| ".env", "Dockerfile", "docker-compose", | ||
| "config.yaml", "config.yml", "config.json", | ||
| } | ||
|
|
||
| // generatedPatterns are path substrings that indicate generated code. | ||
| var generatedPatterns = []string{ | ||
| ".pb.go", ".pb.gw.go", "_generated", "_gen.go", | ||
| "generated.go", "mock_", "mocks/", "zz_generated", | ||
| "vendor/", "node_modules/", "dist/", | ||
| } | ||
|
|
||
| // FilePriority assigns a review priority score to a file diff. | ||
| // Higher score = review first. Used to decide which files to skip | ||
| // when a token budget is exceeded. | ||
| func FilePriority(fd FileDiff) int { | ||
| score := 0 | ||
| path := strings.ToLower(fd.NewPath) | ||
|
|
||
| // Security-sensitive files get highest priority. | ||
| if isSecuritySensitive(path) { | ||
| score += 100 | ||
| } | ||
|
|
||
| // More changed lines = higher priority. | ||
| score += fd.LineCount() | ||
|
|
||
| // New files get a boost — no prior review coverage. | ||
| if fd.IsNew { | ||
| score += 30 | ||
| } | ||
|
|
||
| // Penalize low-value files. | ||
| if isGenerated(path) { | ||
| score -= 50 | ||
| } | ||
| if isTestFile(path) { | ||
| score -= 10 // Still review tests, but prioritize prod code. | ||
| } | ||
| if fd.IsRename && fd.LineCount() == 0 { | ||
| score -= 40 // Pure rename with no content changes. | ||
| } | ||
|
|
||
| return score | ||
| } | ||
|
|
||
| // SortByPriority sorts diffs highest-priority first. | ||
| func SortByPriority(diffs []FileDiff) { | ||
| sort.SliceStable(diffs, func(i, j int) bool { | ||
| return FilePriority(diffs[i]) > FilePriority(diffs[j]) | ||
| }) | ||
| } | ||
|
|
||
| func isSecuritySensitive(path string) bool { | ||
| // Split into path components and check each against security patterns. | ||
| parts := strings.FieldsFunc(path, func(r rune) bool { | ||
| return r == '/' || r == '\\' || r == '_' || r == '-' || r == '.' | ||
| }) | ||
| for _, part := range parts { | ||
| part = strings.ToLower(part) | ||
| for _, p := range securityPatterns { | ||
| if part == p { | ||
| return true | ||
| } | ||
| } | ||
| } | ||
| base := filepath.Base(path) | ||
| for _, f := range securityFiles { | ||
| if base == f || strings.HasPrefix(base, f) { | ||
| return true | ||
| } | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| return false | ||
| } | ||
|
|
||
| func isGenerated(path string) bool { | ||
| for _, p := range generatedPatterns { | ||
| if strings.Contains(path, p) { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| func isTestFile(path string) bool { | ||
| base := filepath.Base(path) | ||
| return strings.HasSuffix(base, "_test.go") || | ||
| strings.HasSuffix(base, ".test.ts") || | ||
| strings.HasSuffix(base, ".test.js") || | ||
| strings.HasSuffix(base, ".spec.ts") || | ||
| strings.HasSuffix(base, ".spec.js") || | ||
| strings.HasSuffix(base, "Test.java") || | ||
| strings.HasSuffix(base, "Test.kt") || | ||
| strings.HasPrefix(base, "test_") || | ||
| strings.Contains(path, "/test/") || | ||
| strings.Contains(path, "/tests/") | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| package diff | ||
|
|
||
| import "testing" | ||
|
|
||
| func TestFilePriority_SecurityFiles(t *testing.T) { | ||
| tests := []struct { | ||
| path string | ||
| want bool // should score > 100 | ||
| }{ | ||
| {"internal/auth/handler.go", true}, | ||
| {"pkg/crypto/encrypt.go", true}, | ||
| {"config/secrets.yaml", true}, | ||
| {".env", true}, | ||
| {"internal/handler/list.go", false}, | ||
| {"README.md", false}, | ||
| // Negative cases — should NOT match despite containing security substrings. | ||
| {"pkg/monkey.go", false}, // contains "key" but is not security-related | ||
| {"docs/authors.md", false}, // contains "auth" but is not security-related | ||
| {"internal/tokenizer.go", false}, // contains "token" but is not security-related | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| fd := FileDiff{NewPath: tt.path, Hunks: []Hunk{{Lines: []DiffLine{{Type: LineAdded}}}}} | ||
| score := FilePriority(fd) | ||
| if tt.want && score < 100 { | ||
| t.Errorf("FilePriority(%q) = %d, expected >= 100 (security-sensitive)", tt.path, score) | ||
| } | ||
| if !tt.want && score >= 100 { | ||
| t.Errorf("FilePriority(%q) = %d, expected < 100 (not security-sensitive)", tt.path, score) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestFilePriority_NewVsModified(t *testing.T) { | ||
| newFile := FileDiff{ | ||
| NewPath: "pkg/handler.go", | ||
| IsNew: true, | ||
| Hunks: []Hunk{{Lines: []DiffLine{{Type: LineAdded}, {Type: LineAdded}}}}, | ||
| } | ||
| modFile := FileDiff{ | ||
| NewPath: "pkg/handler.go", | ||
| Hunks: []Hunk{{Lines: []DiffLine{{Type: LineAdded}, {Type: LineAdded}}}}, | ||
| } | ||
|
|
||
| newScore := FilePriority(newFile) | ||
| modScore := FilePriority(modFile) | ||
|
|
||
| if newScore <= modScore { | ||
| t.Errorf("new file (%d) should score higher than modified file (%d)", newScore, modScore) | ||
| } | ||
| } | ||
|
|
||
| func TestFilePriority_Generated(t *testing.T) { | ||
| gen := FileDiff{ | ||
| NewPath: "api/service.pb.go", | ||
| Hunks: []Hunk{{Lines: []DiffLine{{Type: LineAdded}}}}, | ||
| } | ||
| normal := FileDiff{ | ||
| NewPath: "api/service.go", | ||
| Hunks: []Hunk{{Lines: []DiffLine{{Type: LineAdded}}}}, | ||
| } | ||
|
|
||
| if FilePriority(gen) >= FilePriority(normal) { | ||
| t.Errorf("generated file should score lower than normal file") | ||
| } | ||
| } | ||
|
|
||
| func TestFilePriority_RenameOnly(t *testing.T) { | ||
| rename := FileDiff{ | ||
| NewPath: "pkg/new_name.go", | ||
| OldPath: "pkg/old_name.go", | ||
| IsRename: true, | ||
| // No hunks = no changed lines. | ||
| } | ||
| modified := FileDiff{ | ||
| NewPath: "pkg/handler.go", | ||
| Hunks: []Hunk{{Lines: []DiffLine{{Type: LineAdded}, {Type: LineAdded}, {Type: LineAdded}}}}, | ||
| } | ||
|
|
||
| if FilePriority(rename) >= FilePriority(modified) { | ||
| t.Errorf("pure rename (%d) should score lower than modified file (%d)", | ||
| FilePriority(rename), FilePriority(modified)) | ||
| } | ||
| } | ||
|
|
||
| func TestFilePriority_TestFile(t *testing.T) { | ||
| test := FileDiff{ | ||
| NewPath: "pkg/handler_test.go", | ||
| Hunks: []Hunk{{Lines: []DiffLine{{Type: LineAdded}, {Type: LineAdded}}}}, | ||
| } | ||
| prod := FileDiff{ | ||
| NewPath: "pkg/handler.go", | ||
| Hunks: []Hunk{{Lines: []DiffLine{{Type: LineAdded}, {Type: LineAdded}}}}, | ||
| } | ||
|
|
||
| if FilePriority(test) >= FilePriority(prod) { | ||
| t.Errorf("test file (%d) should score lower than prod file (%d)", | ||
| FilePriority(test), FilePriority(prod)) | ||
| } | ||
| } | ||
|
|
||
| func TestSortByPriority(t *testing.T) { | ||
| diffs := []FileDiff{ | ||
| {NewPath: "README.md"}, // low priority | ||
| {NewPath: "internal/auth/handler.go", IsNew: true, // high priority | ||
| Hunks: []Hunk{{Lines: []DiffLine{{Type: LineAdded}, {Type: LineAdded}}}}}, | ||
| {NewPath: "vendor/lib.go"}, // generated = very low | ||
| {NewPath: "pkg/server.go", | ||
| Hunks: []Hunk{{Lines: []DiffLine{{Type: LineAdded}}}}}, // medium | ||
| } | ||
|
|
||
| SortByPriority(diffs) | ||
|
|
||
| // Auth file should be first. | ||
| if diffs[0].NewPath != "internal/auth/handler.go" { | ||
| t.Errorf("expected auth file first, got %q", diffs[0].NewPath) | ||
| } | ||
| // Vendor file should be last. | ||
| if diffs[len(diffs)-1].NewPath != "vendor/lib.go" { | ||
| t.Errorf("expected vendor file last, got %q", diffs[len(diffs)-1].NewPath) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.