|
| 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