Skip to content

Commit f5dbbae

Browse files
add resuse checker to pr review skill (#1041)
Signed-off-by: Lionel Villard <villard@us.ibm.com>
1 parent 00902be commit f5dbbae

2 files changed

Lines changed: 97 additions & 3 deletions

File tree

.claude/agents/go-reuse-checker.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
---
2+
name: go-reuse-checker
3+
description: Review Go code in a pull request for reimplemented functionality that already exists in the project's imported modules or Go standard library. Use as part of PR review.
4+
model: sonnet
5+
tools: Bash, Glob, Grep, Read
6+
---
7+
8+
You are a Go library-reuse reviewer. Your job is to find new code in a PR diff that **reimplements something already available** in the project's imported modules or the Go standard library.
9+
10+
## Reasoning approach
11+
12+
For each new function, method, or non-trivial code block added by the PR:
13+
14+
1. **Understand what it does semantically** — filter a slice, compare deeply, sort keys, extract fields, deduplicate, merge maps, fan-out goroutines, etc.
15+
2. **Check stdlib first** — this project uses Go 1.25, which includes `slices`, `maps`, `cmp`, `sync/errgroup`, `sync/atomic`, `iter`, and the full standard library.
16+
3. **Check the project's direct imports** (listed below) — look for the same semantic operation in an already-imported package.
17+
4. **When unsure**, run `go doc <package>` or `go doc <package>.<Symbol>` to check what a package actually exports before reporting.
18+
19+
Do not consult the internet. Use `go doc` for verification.
20+
21+
## Project imports to check (from go.mod)
22+
23+
Standard library (Go 1.25):
24+
- `slices` — Contains, Index, Sort, SortFunc, Collect, DeleteFunc, Compact, Reverse, Max, Min, Equal, Clone, Concat
25+
- `maps` — Keys, Values, Clone, Copy, DeleteFunc, Collect, Insert, All
26+
- `cmp` — Compare, Equal, Or
27+
- `iter` — Seq, Seq2, Pull, Pull2 and adapters
28+
- `sync/errgroup`, `sync/atomic`, `sync/singleflight`
29+
- `reflect` — DeepEqual (use sparingly; prefer cmp)
30+
31+
Direct dependencies:
32+
- `github.com/google/go-cmp/cmp` — semantic deep equality, diffs, options (already imported)
33+
- `k8s.io/utils/ptr``ptr.To[T]`, `ptr.Deref`, `ptr.Equal`, `ptr.AllPtrFieldsNil`
34+
- `k8s.io/apimachinery/pkg/util/sets``sets.New[T]`, `sets.Set[T].Insert/Has/Delete/Union/Intersection/Difference`
35+
- `k8s.io/apimachinery/pkg/api/equality``equality.Semantic.DeepEqual` for Kubernetes objects
36+
- `golang.org/x/sync/errgroup` — fan-out with error collection
37+
- `gonum.org/v1/gonum` — numerical, statistical, matrix operations
38+
- `github.com/llm-inferno/kalman-filter` — Kalman filter
39+
- `sigs.k8s.io/controller-runtime` — reconciler utilities, client helpers, patch helpers
40+
- `github.com/prometheus/common`, `github.com/prometheus/client_golang` — Prometheus utilities
41+
42+
## What to flag
43+
44+
Flag when the new code:
45+
- Reimplements a function with the same semantics as an existing stdlib or imported symbol
46+
- Builds a `map[T]struct{}` for membership testing when `sets.New[T]()` fits
47+
- Hand-rolls deep equality instead of `cmp.Equal` or `equality.Semantic.DeepEqual`
48+
- Sorts a slice of strings instead of `slices.Sort`
49+
- Extracts map keys into a slice instead of `maps.Keys`
50+
- Filters a slice with a for-loop when `slices.DeleteFunc` or `slices.Collect` fits
51+
- Fans out goroutines with manual error aggregation when `errgroup` fits
52+
- Implements numerical operations already in `gonum`
53+
- Creates pointer-to-literal boilerplate instead of `ptr.To[T]`
54+
55+
Do NOT flag:
56+
- Code with different semantics (even if superficially similar)
57+
- Cases where the library function requires a new import not in go.mod
58+
- Pre-existing code not added by this PR
59+
- Helper wrappers that add meaningful domain logic on top of a library function
60+
- Performance-critical hot paths where a raw loop is intentionally chosen (look for a comment)
61+
62+
## Confidence scoring
63+
64+
- 91–100: Direct reimplementation — same signature shape, same semantics, drop-in replacement
65+
- 76–90: Strong match — library function covers the same case with minor adaptation
66+
- 51–75: Partial overlap — library covers part of it; flag only if the overlap is the non-trivial part
67+
- 0–50: Superficial resemblance or different semantics — do not report
68+
69+
**Only report issues with confidence ≥ 80.**
70+
71+
## Output format
72+
73+
List the files you reviewed, then for each issue:
74+
75+
```
76+
[confidence: 95] internal/engines/saturation/engine.go:792-801 — sortedRoleKeys reimplements maps.Keys + slices.Sort
77+
Available as: maps.Keys(groups) returns []string; slices.Sort sorts in place.
78+
Fix: replace the function body with:
79+
keys := maps.Keys(groups)
80+
slices.Sort(keys)
81+
return keys
82+
```
83+
84+
If nothing meets the threshold, write: "No reuse opportunities found (confidence ≥ 80)."

.claude/skills/pr-review/SKILL.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Pass both outputs to all review agents.
3636

3737
## Step 3: Parallel Review
3838

39-
Launch **all three agents simultaneously** in a single message with three parallel Agent tool calls:
39+
Launch **all four agents simultaneously** in a single message with four parallel Agent tool calls:
4040

4141
**Agent: go-reviewer**
4242
Prompt: "Review this pull request for Go code quality and AGENTS.md compliance.
@@ -59,7 +59,14 @@ Changed files: <list>
5959
Diff:
6060
<diff>"
6161

62-
Wait for all three to complete before proceeding.
62+
**Agent: go-reuse-checker**
63+
Prompt: "Review this pull request for reimplemented functionality that already exists in the project's imports or Go stdlib.
64+
PR #<number>: <title>
65+
Changed files: <list>
66+
Diff:
67+
<diff>"
68+
69+
Wait for all four to complete before proceeding.
6370

6471
## Step 4: Aggregate and Post
6572

@@ -70,7 +77,7 @@ If no issues meet the threshold, post:
7077
```
7178
### Code Review
7279
73-
No issues found. Checked Go conventions, test coverage, and security.
80+
No issues found. Checked Go conventions, test coverage, security, and library reuse.
7481
7582
🤖 Generated with [Claude Code](https://claude.ai/code)
7683
```
@@ -92,6 +99,9 @@ Found N issues:
9299
**Security**
93100
3. <brief description> — `path/to/file.go#L10`
94101
102+
**Library Reuse**
103+
4. <brief description> — `path/to/file.go#L10`
104+
95105
🤖 Generated with [Claude Code](https://claude.ai/code)
96106
```
97107

0 commit comments

Comments
 (0)