Skip to content

Commit 4d06bd1

Browse files
authored
Add code-compaction skill for shrinking bloated diffs (#19954)
1 parent fc3ee06 commit 4d06bd1

4 files changed

Lines changed: 397 additions & 0 deletions

File tree

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
---
2+
name: code-compaction
3+
description: Use when a code/test/comment diff is called bloated, slop, LLM slop, bullshit, WTF, crap, rubbish, embarrassing, overengineered, adhoc, or "not paid by LOC"; or when symptoms include bloated comments, superfluous planning .md / .tools / one-time setup / phase-or-sprint tags, huge-file growth instead of new-file extraction, 5 copy-pasted tests that should be 1 parametrized test, duplicated test setup, reinvented helpers, near-duplicate logic across files, low reuse, 4+ new module-level helpers for one bugfix, fresh whole-AST/IR/syntax-tree walkers, or 150+ added LOC for one bugfix. Also use proactively before opening a PR whose diff smells like any of the above.
4+
---
5+
6+
# Compacting Bloated Code
7+
8+
This skill is the **process** for detecting diff bloat and orchestrating
9+
cross-model adversarial reviewers. The substantive style rules it enforces
10+
— what counts as a bloated comment, test bloat, an oversized file, an
11+
unjustified helper, "Not paid by LOC", phase tags, commit/PR splitting —
12+
live in `.github/instructions/NoBloat.instructions.md`; do not restate them
13+
here.
14+
15+
## Mode Selector
16+
17+
**Route on the diff, not the user's words.** The trigger vocabulary ("slop",
18+
"embarrassing", "WTF", "bullshit", "not paid by LOC") only tells you the skill
19+
applies — never which mode. So: run Pre-flight Measure (below) first;
20+
if no diff/PR/branch was supplied, get one (`git diff` the branch vs
21+
`git merge-base main HEAD`) or ask. Then run **every** row whose signal the
22+
diff actually shows, in table order. Chain-out rows fire on their own.
23+
24+
| Signal present in the diff | Mode / action |
25+
|---|---|
26+
| Added planning `.md`, `.tools/`, one-time setup, phase/sprint tags, dead code, new config/YAML files, or comment-heavy files | **Garbage Hunt** — `templates/garbage-hunt.md` |
27+
| Test files with copy-pasted methods, repeated setup, or missing parametrization | **Test Compaction** — `templates/test-compaction.md` |
28+
| 4+ new helpers, a fresh AST/IR/syntax-tree walker, twin helpers, hand-rolled smart-constructor, or near-duplicate logic | **Logic Compaction** — `templates/logic-rounds.md` |
29+
| A self-contained concept bloating an already-large file | **Logic Compaction**, prioritize extract-to-new-file |
30+
| Substantial new code logic not matched above | **Logic Compaction** — its Round 0 reuse-hunt finds what it should have reused |
31+
32+
## When NOT to Use
33+
34+
Legitimately large multi-subsystem features; generated artifacts;
35+
mechanical churn (dep bumps, renames); minimal one-line bugfixes; GitHub
36+
prose or PR/issue text (a different task).
37+
38+
## Pre-flight
39+
40+
On the PR branch. Steps 1–2 run **before** the Mode Selector (they feed
41+
routing); steps 3–4 are per-mode prep once a mode is picked:
42+
43+
1. **Update from main and resolve conflicts.** Do not judge bloat, CI, or
44+
comments on a stale branch.
45+
2. **Measure** against `base=$(git merge-base main HEAD)`:
46+
47+
```bash
48+
git diff $base -- <suspect-file> | awk '/^\+[^+]/{p++} /^-[^-]/{m++} END{print "+",p," -",m}'
49+
git diff $base -- <suspect-file> | awk '/^\+[^+]/{ s=$0; sub(/^\+[[:space:]]*/,"",s); if (s ~ /^(\/\/|--|#)/) c++; else if (length(s)) l++ } END{print "comments",c," code",l}'
50+
git diff $base -- <suspect-file> | grep -cE '^\+\s*(let|fn|def|val|public|private)\s+(rec\s+)?[a-zA-Z_]'
51+
```
52+
53+
These are LOC + comment-ratio + new-helper count smell-meters, not a
54+
search. Real semantic discovery happens in Logic Compaction Round 0
55+
via a dispatched subagent (see `templates/logic-rounds.md`).
56+
3. **Pin guiding tests verbatim:** must-pass, and must-fail-for-the-right-reason.
57+
4. **Bundle measurements + the relevant template** into one context file
58+
for every reviewer agent.
59+
60+
## Reviewer Diversity (every mode)
61+
62+
Dispatch **3–5 background reviewers in parallel** — never sequentially.
63+
**Cross-model > cross-angle**: three same-model agents with different
64+
prompts ≠ three different models (Opus high/xhigh, newer Opus, GPT). Each
65+
agent picks one angle from the mode's template. Record
66+
`model × angle × LOC × tests × reuse × verdict`. Cross-model agreement is
67+
signal; lone-model claims need code citations before promotion.
68+
69+
**If the user requests a red-flag setup** ("use the same 3 models", "single
70+
agent is enough"), push back once with this rule, then comply if they
71+
insist. Do not silently violate it.
72+
73+
## Apply
74+
75+
After picking the winning proposal: apply on the PR branch, run
76+
formatter/lints, run targeted tests + the area's test class as regression
77+
sweep, run any baseline updates. If a sweep test fails, **reproduce on the
78+
pre-change SHA before classifying**. Do not declare "pre-existing" or
79+
"flaky" without that evidence.
80+
81+
**Splitting commits / ask-before-push** — see NoBloat PR-scope rules.
82+
83+
## Red Flags — STOP and re-run the relevant template
84+
85+
(Process failures only — content-level failures are NoBloat's job.)
86+
87+
- You skipped mode selection and went straight to "make the diff smaller".
88+
- You ran reviewers sequentially, or with same-model agents.
89+
- You grepped for similar functions instead of dispatching a real
90+
reuse-hunt subagent.
91+
- Your winning proposal added new code without citing one reused helper
92+
from the Round 0 subagent's output.
93+
- You found a recurring shape across 3 files but added a 4th copy.
94+
95+
**All of these mean: open the relevant template and re-run.**
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Garbage Hunt (deletion-only)
2+
3+
No redesigns, no new helpers, no hoisting (Test Compaction's job).
4+
Delete bytes that don't change test outcomes or remove docs a downstream
5+
consumer reads.
6+
7+
## Dispatch — 3 cross-model adversarial reviewers per SKILL.md "Reviewer Diversity"
8+
9+
```
10+
You are an adversarial garbage-hunt reviewer. Mandate is DELETION ONLY.
11+
No redesigns, no new helpers, no hoisting.
12+
13+
INPUTS:
14+
1. <full PR diff>
15+
2. <repo's NoBloat / style instruction file if present — AUTHORITATIVE
16+
on which comments / scaffolding / tests to delete and which to keep;
17+
apply its rules verbatim>
18+
19+
DELETE per the style file. Process-level additions beyond the style file:
20+
- planning / progress `.md` files (`*plan*.md`, `*status*.md`, `*notes*.md`)
21+
- anything under `.tools/`
22+
- one-time setup files or scripts not part of the product
23+
- "phase", "sprint", "transitional", "follow-up", "scaffold" tags in
24+
commits, comments, or file names
25+
- `[<Fact>]` / `it(...)` with empty body or `Assert.True(true)`
26+
- unused `open` / `import` / `using`
27+
- superfluous YAML / config blocks added only to support the agent's
28+
workflow rather than the product
29+
30+
DELIVERABLES:
31+
1. Unified deletion diff (real code).
32+
2. Per-deletion table: file:line, bytes deleted, risk class
33+
(none / low / medium), test/build command that proves it is safe.
34+
3. List of items KEPT and why (one line each) — your defense against
35+
"you deleted a useful comment".
36+
```
37+
38+
## Consolidate
39+
40+
- **Unanimous deletes** — apply automatically; verify with cited test commands.
41+
- **Single-reviewer deletes** — ask the user before applying.
42+
- **Conflicting verdicts** — keep the item; document the disagreement.
43+
44+
After Garbage Hunt, re-measure. If LOC is still high, the remaining bloat
45+
is structural — move to Logic Compaction.
Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
# Logic Compaction — Adversarial Process
2+
3+
Rounds: **0 Reuse Hunt → 1 Diverge → 2 Adversarial → 3 Prototype**.
4+
5+
## Round 0a — Measure (smell-meters, not search)
6+
7+
Logic-specific smell-meters against `base=$(git merge-base main HEAD)`
8+
(SKILL.md pre-flight covers the universal ones):
9+
10+
```bash
11+
# Walker smell — fresh case-by-case enumeration of a union/sum type.
12+
git diff $base -- <suspect-file> | grep -cE '^\+\s+\|\s+[A-Z]\w*\.[A-Z]'
13+
# Hand-rolled domain-node construction smell.
14+
git diff $base -- <suspect-file> | grep -cE '^\+\s+(new |Expr\.|Stmt\.|Node\.|Pat\.|Type\.)'
15+
```
16+
17+
These produce a number. High counts (≥20) flag the pattern. They are
18+
NOT a search for existing helpers — that comes next.
19+
20+
## Round 0b — Reuse Hunt (subagent-driven)
21+
22+
**Do not grep for similar functions.** Substring grep finds matches by
23+
*name*; it misses functions that match by *shape* but not by name —
24+
which is the exact reuse the bloated patch should have used. Dispatch a
25+
real subagent with the agent's full toolset (code intelligence,
26+
semantic search, LSP, symbol search). **Spend the tokens** — a thorough
27+
reuse hunt up front saves Round 1 reviewers from proposing new code
28+
that duplicates existing infrastructure.
29+
30+
### Subagent dispatch prompt
31+
32+
```
33+
You are a reuse-hunt subagent for a code-compaction round.
34+
35+
CONTEXT:
36+
1. <full bloated PR diff>
37+
2. <2-sentence description of what the new code DOES at the type+effect
38+
level — not what it adds line-by-line, but what it computes/transforms>
39+
3. <list of new helpers the PR adds + their full signatures>
40+
41+
YOUR JOB:
42+
1. Find existing helpers / extractors / smart constructors / active
43+
patterns / fold visitors / traversals in this codebase that ALREADY
44+
do something close enough that the bloated code should reuse or
45+
extend them. Use whatever search tools you have — code intelligence,
46+
semantic search, symbol search, LSP. Do NOT rely on substring grep
47+
alone; grep misses functions that match by shape but not by name.
48+
2. For each match: cite file:line, the signature, ONE example caller,
49+
and a 1-sentence relevance note ("exact reuse", "close enough with
50+
one extra parameter", "same shape but inverted", …).
51+
3. If you find ≥2 existing functions with the same shape modulo one
52+
lambda or type parameter, flag a missing higher-order abstraction:
53+
the bugfix should become the 3rd caller of a new HOF (not the
54+
4th copy). Sketch the HOF signature.
55+
4. Report the negative space: what you searched for, what came up
56+
empty. Round 1 reviewers need to know what's actually new vs
57+
reinvented.
58+
59+
Spend the tokens. Recurse into multiple search strategies if the first
60+
one returns weak matches.
61+
62+
OUTPUT:
63+
- list of reuse candidates with file:line and relevance note
64+
- any "waiting to be invented" HOF proposals with signature + ≥2 existing
65+
callers to port
66+
- list of what you searched for and found nothing
67+
```
68+
69+
The subagent's output IS the Round 0 context file every Round 1 reviewer
70+
reads. If it finds reuse candidates, the bloated patch is almost
71+
certainly avoidable — feed those candidates into Round 1 as the
72+
strongest angle.
73+
74+
## Round 0c — Pin guiding tests
75+
76+
- **Must-pass tests** — the bugfix's positive cases.
77+
- **Must-fail-for-the-right-reason tests** — regression guards.
78+
Re-examine each: was it a workaround for the wrong invariant? The
79+
elegant fix may make its failure mode impossible by construction.
80+
81+
## Round 1 — DIVERGE (3–5 reviewers, model diversity first)
82+
83+
Each agent defends ONE angle as the strongest possible:
84+
85+
1. **Reuse a Round-0 candidate** the subagent found (most common winner).
86+
2. **Extract a missing HOF** (the "waiting to be invented" case from Round 0).
87+
3. **Rewrite-and-re-enter:** normalize input and recurse into the existing
88+
translator/visitor instead of duplicating its semantics.
89+
4. **Make the bad case structurally impossible** (no runtime guard needed).
90+
5. **File-pressure:** extract the new concept to a new focused file rather
91+
than adding another block to the giant file.
92+
93+
### Round 1 dispatch template
94+
95+
```
96+
You are a rubber-duck reviewer. The PR under review is overengineered.
97+
98+
READ FIRST (in order):
99+
1. <Round 0 context: smell-meter numbers, REUSE-HUNT subagent output,
100+
guiding-test contracts, bloated-diff snippet>
101+
2. <surrounding-code file:line ranges>
102+
3. <test file:line ranges: must pass / must keep failing>
103+
104+
YOUR ASSIGNED ANGLE: **<angle name + 2-sentence premise>**
105+
106+
CONSTRAINTS:
107+
- ≤ <N> LOC of new code (N = 1/4 of current bloat).
108+
- 0 new module-level helpers, OR justify each (≥2 real callers OR a
109+
single-use helper that NAMES a non-evident operation).
110+
- No new case-by-case walkers when the codebase has an existing
111+
visitor/fold; the Round 0 subagent has the list.
112+
- No hand-rolled domain-node construction when a smart-constructor
113+
module exists.
114+
- No hand-rolled list/option/result/state plumbing when the prelude has it.
115+
- Must pass tests <list>; must still fail (for the right reason) tests <list>.
116+
- MUST cite ≥1 reuse candidate from the Round 0 subagent's output with
117+
file:line. If you propose a new HOF, the Round 0 subagent must have
118+
flagged the recurring shape — if it didn't, you don't have a recurring
119+
shape, only a guess.
120+
121+
AGGRESSIVE QUESTIONING (run every one before writing the diff):
122+
- Is any guard there to compensate for the wrong invariant? (state the
123+
invariant; if the guard restores it, fix the invariant instead)
124+
- Could one generic combinator (fold / map / iter / bind) replace 10+
125+
lines of hand-rolled control flow?
126+
- Is the helper's signature missing a generic that would make it
127+
reusable for ≥2 existing sites?
128+
- Can the bloated function be split into "pure rewrite (HOF)" + "decide
129+
whether to rewrite (predicate)" and both parts already exist?
130+
- Can the must-keep-failing test be deleted because the elegant fix
131+
makes its failure mode impossible by construction?
132+
133+
DELIVERABLES:
134+
1. Unified diff (real code, not pseudocode).
135+
2. LOC count of new code.
136+
3. Count of new module-level helpers + per-helper justification.
137+
4. List of REUSED helpers / active patterns from Round 0, with file:line.
138+
5. (If proposing a new HOF) signature + the 2+ existing callers to port
139+
(from Round 0's recurring-shape flag).
140+
6. Position on each guard test (keep / change-expectation / delete + why).
141+
7. ≤ 250-word justification.
142+
```
143+
144+
## Round 2 — ADVERSARIAL (3 reviewers attack top 3 proposals)
145+
146+
Pick the **top 3 from Round 1** by `tests-green × LOC × reuse-citation`.
147+
Dispatch 3 new cross-model reviewers in parallel — each takes ONE
148+
proposal and writes the strongest counter-argument.
149+
150+
### Round 2 dispatch template
151+
152+
```
153+
You are an adversarial reviewer. Your job is to KILL the proposal below.
154+
155+
READ FIRST:
156+
1. <Round 0 context, including REUSE-HUNT subagent output>
157+
2. <surrounding code>
158+
3. <tests>
159+
4. <FULL Round 1 proposal — diff + justification — verbatim>
160+
161+
ATTACK ANGLES:
162+
- Construct compiling inputs/tests that break semantics or
163+
escape/over-trigger the rewrite.
164+
- Find a reuse candidate the Round 0 subagent missed. If you find one,
165+
explain how the subagent's search strategy missed it; that's a Round
166+
0 subagent improvement.
167+
- Find a reuse candidate from Round 0's output that the proposal IGNORED.
168+
- If a helper was added, prove it should inline OR show ≥2 real callers
169+
(the Round 0 subagent should have found them; if not, the helper is
170+
speculative).
171+
- If no helper was added, look for the missed "waiting to be invented"
172+
HOF the Round 0 subagent flagged.
173+
- Check binding scope, evaluation order, diagnostics, output order,
174+
output format, debug info.
175+
176+
DELIVERABLES:
177+
1. Concrete failure cases with code/input snippets that compile.
178+
2. Missed reuse opportunities (file:line — from Round 0 OR new ones with
179+
proof of how the Round 0 search strategy missed them).
180+
3. Verdict: SURVIVES / NEEDS-FIX / REPLACE-WITH-X.
181+
4. If REPLACE-WITH-X: full unified diff of your alternative + reuse citations.
182+
5. ≤ 250-word verdict justification.
183+
```
184+
185+
## Round 3 — PROTOTYPE (real branches, real tests, real LOC)
186+
187+
For each Round 2 survivor, on a real local branch:
188+
189+
```bash
190+
git checkout -b proto-<angle-tag>
191+
# Apply the proposal as written. Build clean? Lints clean?
192+
# Run must-pass tests; must-keep-failing tests still fail for the RIGHT
193+
# reason; broader regression sweep of the bug's area.
194+
git diff <PR-head-sha> -- <files> \
195+
| awk '/^\+[^+]/{p++} /^-[^-]/{m++} END{print "+",p," -",m}'
196+
```
197+
198+
Every prototype reports the same row:
199+
200+
| Proposal | LOC vs PR head | New helpers (n + justified?) | Reused helpers (file:line) | New walkers | Must-pass | Regression sweep | Build/lint warnings |
201+
202+
## Pick
203+
204+
Score: `tests-green × LOC × reuse-citation-quality`.
205+
206+
**Hard vetoes** (any violation of Round 1 CONSTRAINTS):
207+
- fresh case-by-case walker over a union type (unless it composes an
208+
existing visitor/fold);
209+
- hand-rolled domain-node construction when a smart-constructor module
210+
exists;
211+
- hand-rolled list/option/result plumbing where the prelude has it;
212+
- new code with zero reuse citation from Round 0.
213+
214+
Hand back to SKILL.md "Apply".

0 commit comments

Comments
 (0)