crysk 0.0.0-stage → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (229) hide show
  1. package/LICENSE +176 -0
  2. package/README.md +610 -2
  3. package/bin/pr-hero.js +166 -0
  4. package/config/models/anthropic.json +5 -0
  5. package/dist/SKILL-7e3725zn.md +496 -0
  6. package/dist/SKILL-7q35wcaz.md +66 -0
  7. package/dist/SKILL-9at22381.md +74 -0
  8. package/dist/SKILL-ex2hxxh3.md +134 -0
  9. package/dist/adjudicator-dtg2axnh.md +86 -0
  10. package/dist/admission-config.example-a9vqmmad.json +9 -0
  11. package/dist/ci-admission-camfbx2b.md +122 -0
  12. package/dist/cli.js +42292 -0
  13. package/dist/consolidate-ac0mnfyj.md +24 -0
  14. package/dist/deep-review-lifecycle-m698ybjp.md +362 -0
  15. package/dist/deep-review-logic-a6b1gczf.md +156 -0
  16. package/dist/deep-review-parity-0ec904db.md +120 -0
  17. package/dist/deep-review-reliability-qyakbjb9.md +125 -0
  18. package/dist/deep-review-resilience-3tz8y26e.md +118 -0
  19. package/dist/index.js +20190 -0
  20. package/dist/opencode-ci-7b0be64p.md +141 -0
  21. package/dist/review-refuter-j4e9gezy.md +71 -0
  22. package/dist/scout-yvp153q7.md +140 -0
  23. package/dist/summarizer-zca44nxa.md +18 -0
  24. package/dist/workflow-w1g9szna.yml +151 -0
  25. package/package.json +82 -4
  26. package/prompts/consolidate.md +24 -0
  27. package/prompts/default/PROVENANCE.md +18 -0
  28. package/prompts/default/deep-review-lifecycle.md +362 -0
  29. package/prompts/default/deep-review-logic.md +156 -0
  30. package/prompts/default/deep-review-parity.md +120 -0
  31. package/prompts/default/deep-review-reliability.md +125 -0
  32. package/prompts/default/deep-review-resilience.md +118 -0
  33. package/prompts/default/review-refuter.md +71 -0
  34. package/prompts/scout.md +140 -0
  35. package/prompts/summarizer.md +18 -0
  36. package/skills/crysk/SKILL.md +74 -0
  37. package/skills/crysk-ci-setup/SKILL.md +134 -0
  38. package/skills/crysk-ci-setup/assets/admission-config.example.json +9 -0
  39. package/skills/crysk-ci-setup/assets/workflow.yml +151 -0
  40. package/skills/crysk-ci-setup/references/ci-admission.md +122 -0
  41. package/skills/crysk-ci-setup/references/opencode-ci.md +141 -0
  42. package/skills/crysk-loop/SKILL.md +496 -0
  43. package/skills/crysk-loop/adjudicator.md +86 -0
  44. package/skills/crysk-pr/SKILL.md +66 -0
  45. package/src/agent-env.ts +1084 -0
  46. package/src/asset-manifest.ts +83 -0
  47. package/src/assets.d.ts +6 -0
  48. package/src/assets.ts +183 -0
  49. package/src/brand-paths.ts +155 -0
  50. package/src/brand.ts +255 -0
  51. package/src/ci/admission-ledger.ts +350 -0
  52. package/src/ci/classify-error.ts +73 -0
  53. package/src/ci/cli-record.ts +271 -0
  54. package/src/ci/force-comment.ts +39 -0
  55. package/src/ci/gates.ts +923 -0
  56. package/src/ci/outcome.ts +2181 -0
  57. package/src/ci/override.ts +187 -0
  58. package/src/ci/publish.ts +1639 -0
  59. package/src/ci/reporter.ts +626 -0
  60. package/src/ci/review-admission.ts +901 -0
  61. package/src/ci/review-levels.ts +310 -0
  62. package/src/ci/review-policy.ts +18 -0
  63. package/src/ci/review-risk.ts +184 -0
  64. package/src/ci/setup.ts +844 -0
  65. package/src/ci/trigger.ts +1353 -0
  66. package/src/cli.ts +348 -0
  67. package/src/commands/activity.ts +67 -0
  68. package/src/commands/ci-setup-questions.ts +397 -0
  69. package/src/commands/ci-setup.ts +381 -0
  70. package/src/commands/config.ts +99 -0
  71. package/src/commands/doctor.ts +108 -0
  72. package/src/commands/init.ts +139 -0
  73. package/src/commands/mcp.ts +10 -0
  74. package/src/commands/menu.ts +129 -0
  75. package/src/commands/post.ts +302 -0
  76. package/src/commands/skills.ts +83 -0
  77. package/src/commands/status.ts +173 -0
  78. package/src/commands/triage.ts +342 -0
  79. package/src/commands/uninstall.ts +37 -0
  80. package/src/commands/upgrade.ts +214 -0
  81. package/src/commands/usage.ts +98 -0
  82. package/src/compare/compare.ts +28 -0
  83. package/src/compare/floor-test.ts +471 -0
  84. package/src/compare/martian-adapter.ts +80 -0
  85. package/src/config/config.ts +267 -0
  86. package/src/corpus/corpus.ts +1094 -0
  87. package/src/corpus/preflight.ts +1789 -0
  88. package/src/diversity/accounting.ts +90 -0
  89. package/src/diversity/adjudication.ts +145 -0
  90. package/src/diversity/admission.ts +60 -0
  91. package/src/diversity/benchmark.ts +195 -0
  92. package/src/diversity/clustering.ts +285 -0
  93. package/src/diversity/errors.ts +7 -0
  94. package/src/diversity/identity.ts +285 -0
  95. package/src/diversity/index.ts +11 -0
  96. package/src/diversity/pipeline-integration.ts +448 -0
  97. package/src/diversity/projection.ts +57 -0
  98. package/src/diversity/self-contained.ts +68 -0
  99. package/src/diversity/target-validation.ts +179 -0
  100. package/src/doctor-migration.ts +209 -0
  101. package/src/doctor.ts +723 -0
  102. package/src/errors.ts +83 -0
  103. package/src/execution/admission.ts +75 -0
  104. package/src/execution/atomic-write.ts +35 -0
  105. package/src/execution/attempt-evidence.ts +415 -0
  106. package/src/execution/bucket-id.ts +136 -0
  107. package/src/execution/concurrency-limiter.ts +175 -0
  108. package/src/execution/contracts.ts +443 -0
  109. package/src/execution/failure-classification.ts +42 -0
  110. package/src/execution/failure-policy.ts +240 -0
  111. package/src/execution/harness.ts +2548 -0
  112. package/src/execution/settlement.ts +348 -0
  113. package/src/execution/spawned-process.ts +66 -0
  114. package/src/execution/spend-limiter.ts +424 -0
  115. package/src/execution/step-artifacts.ts +55 -0
  116. package/src/execution/usage-normalized.ts +469 -0
  117. package/src/git/git.ts +396 -0
  118. package/src/git/identity.ts +64 -0
  119. package/src/git/refs.ts +140 -0
  120. package/src/home-preflight.ts +443 -0
  121. package/src/home.ts +243 -0
  122. package/src/ignore-file.ts +336 -0
  123. package/src/ignore-read.ts +118 -0
  124. package/src/index.ts +34 -0
  125. package/src/legacy-launchd.ts +29 -0
  126. package/src/mcp/mcp.ts +150 -0
  127. package/src/mcp/preflight.ts +407 -0
  128. package/src/menu-context.ts +192 -0
  129. package/src/model/catalog.ts +251 -0
  130. package/src/model/exec-snapshots.ts +390 -0
  131. package/src/model/free-discovery.ts +302 -0
  132. package/src/model/provider-capabilities.ts +814 -0
  133. package/src/model/routing.ts +600 -0
  134. package/src/pr/admission.ts +847 -0
  135. package/src/pr/ci-admission-gate.ts +665 -0
  136. package/src/pr/ci-publish.ts +76 -0
  137. package/src/pr/discovery.ts +432 -0
  138. package/src/pr/inline.ts +507 -0
  139. package/src/pr/plan.ts +366 -0
  140. package/src/pr/posting.ts +115 -0
  141. package/src/pr/pr-status.ts +787 -0
  142. package/src/pr/pr.ts +3642 -0
  143. package/src/pr/preflight.ts +1031 -0
  144. package/src/pr/publish-outcome.ts +361 -0
  145. package/src/pr/range.ts +119 -0
  146. package/src/pr/reverts-preflight.ts +606 -0
  147. package/src/pr/reverts.ts +377 -0
  148. package/src/pr/review-pr.ts +927 -0
  149. package/src/pr/status.ts +154 -0
  150. package/src/pr/target.ts +388 -0
  151. package/src/pr/teardown.ts +437 -0
  152. package/src/pr/worktree-setup.ts +60 -0
  153. package/src/production-runtime.ts +1877 -0
  154. package/src/progress.ts +487 -0
  155. package/src/rereview/adjudicate.ts +89 -0
  156. package/src/rereview/classify.ts +362 -0
  157. package/src/rereview/identity.ts +211 -0
  158. package/src/rereview/plan.ts +308 -0
  159. package/src/rereview/prepare.ts +1025 -0
  160. package/src/rereview/state.ts +767 -0
  161. package/src/rereview/verify.ts +241 -0
  162. package/src/review/boundary.ts +105 -0
  163. package/src/review/consolidate.ts +246 -0
  164. package/src/review/dedupe.ts +368 -0
  165. package/src/review/drafts.ts +448 -0
  166. package/src/review/findings-conformance.ts +120 -0
  167. package/src/review/findings.ts +714 -0
  168. package/src/review/pipeline.ts +3977 -0
  169. package/src/review/preflight.ts +3575 -0
  170. package/src/review/prompt-set.ts +177 -0
  171. package/src/review/proof-refs.ts +187 -0
  172. package/src/review/report.ts +1821 -0
  173. package/src/review/review.ts +490 -0
  174. package/src/review/root-cause.ts +120 -0
  175. package/src/review/route-preflight.ts +289 -0
  176. package/src/review/run.ts +601 -0
  177. package/src/review/scout.ts +323 -0
  178. package/src/review/size-gate.ts +595 -0
  179. package/src/review/spec.ts +235 -0
  180. package/src/review/step-runner.ts +233 -0
  181. package/src/runner-authority.ts +596 -0
  182. package/src/security/binding-policy.ts +115 -0
  183. package/src/security/credential-broker.ts +773 -0
  184. package/src/security/evidence-redaction.ts +62 -0
  185. package/src/security/execution-authority.ts +27 -0
  186. package/src/security/redact.ts +71 -0
  187. package/src/security/workspace-read-broker.ts +466 -0
  188. package/src/server/client.ts +190 -0
  189. package/src/server/preflight.ts +217 -0
  190. package/src/server/server.ts +327 -0
  191. package/src/signal-handler.ts +50 -0
  192. package/src/store/activity.ts +413 -0
  193. package/src/store/backfill-preflight.ts +49 -0
  194. package/src/store/backfill.ts +141 -0
  195. package/src/store/gc-preflight.ts +206 -0
  196. package/src/store/gc.ts +424 -0
  197. package/src/store/metrics-preflight.ts +229 -0
  198. package/src/store/metrics.ts +236 -0
  199. package/src/store/preflight.ts +748 -0
  200. package/src/store/store.ts +693 -0
  201. package/src/system-tools.ts +355 -0
  202. package/src/transport-registry.ts +1124 -0
  203. package/src/transports/claude-code-cli.ts +1348 -0
  204. package/src/transports/opencode-admission.ts +515 -0
  205. package/src/transports/opencode-client.ts +3328 -0
  206. package/src/transports/opencode-evidence.ts +557 -0
  207. package/src/transports/opencode-mcp.ts +248 -0
  208. package/src/transports/opencode-sdk.ts +2294 -0
  209. package/src/transports/opencode-server.ts +535 -0
  210. package/src/triage/adjudication.ts +113 -0
  211. package/src/triage/reply.ts +125 -0
  212. package/src/triage/triage.ts +255 -0
  213. package/src/ui/activity.ts +173 -0
  214. package/src/ui/config-edit.ts +816 -0
  215. package/src/ui/config.ts +462 -0
  216. package/src/ui/menu.ts +1325 -0
  217. package/src/ui/plan.ts +1039 -0
  218. package/src/ui/primitives.ts +300 -0
  219. package/src/ui/progress.ts +298 -0
  220. package/src/ui/result.ts +552 -0
  221. package/src/ui/review-menu.ts +312 -0
  222. package/src/ui/select.ts +528 -0
  223. package/src/ui/tree.ts +178 -0
  224. package/src/uninstaller.ts +479 -0
  225. package/src/updater.ts +836 -0
  226. package/src/usage.ts +77 -0
  227. package/src/watch/preflight.ts +1389 -0
  228. package/src/watch/watch.ts +1235 -0
  229. package/src/wizard.ts +877 -0
@@ -0,0 +1,368 @@
1
+ // Prose Step 5 as a pure function: merge every hunter's drafts, collapse
2
+ // duplicates, renumber survivors. Deterministic — same input, same output.
3
+
4
+ import type { DraftFinding } from "./drafts";
5
+ import type { Severity } from "./findings";
6
+
7
+ // A survivor is a draft whose id has been renumbered to the canonical
8
+ // F001, F002, … sequence (and, for a cross-category winner, whose proof_refs
9
+ // absorbed its losers' evidence).
10
+ export type DedupedSurvivor = DraftFinding;
11
+
12
+ // A loser is the original hunter-assigned draft VERBATIM plus the surviving
13
+ // finding's post-renumber id — the shape debug.deduped[] requires so a
14
+ // benchmark miss stays attributable.
15
+ export type DedupeLoser = DraftFinding & { merged_into: string };
16
+
17
+ const SEVERITY_RANK: Record<Severity, number> = {
18
+ BLOCKER: 0,
19
+ CRITICAL: 1,
20
+ WARNING: 2,
21
+ SUGGESTION: 3,
22
+ };
23
+
24
+ // Pass 2 collapses same-path:symbol findings only when their lines sit
25
+ // inside this window of each other (connected components). JUDGEMENT, but
26
+ // measured: Musive PR 1727 had a same-defect pair 43 lines apart (935 vs
27
+ // 978, one try/catch/finally) and a distinct-defect pair 391 apart (544 vs
28
+ // 935, tap-to-audio vs timer leak). 100 clears the same-block pair with
29
+ // slack and still splits 544 from 935. Widen only with another measurement.
30
+ // Direction of error is under-merge: a duplicate comment is visible, an
31
+ // over-merge that dumps an in-diff finding into the summary is not.
32
+ export const DEDUPE_SYMBOL_LINE_WINDOW = 100;
33
+
34
+ // Tokens that carry no defect identity. A closed list on purpose: the
35
+ // threshold below is only meaningful under the exact tokenizer and stopword
36
+ // list that produced it, so this list is part of the calibration and a test
37
+ // re-derives the corpus scores through it.
38
+ const CLAIM_STOPWORDS = new Set(
39
+ (
40
+ "a an the is are was were be been being and or but if then so that this " +
41
+ "these those it its of to in on for with as at by from into no not " +
42
+ "never every any all when after before once each other same which whose " +
43
+ "there here"
44
+ ).split(" "),
45
+ );
46
+
47
+ // Claim overlap that licenses a discard. JUDGEMENT, but measured: over the 20
48
+ // within-arm draft pairs in test/fixtures/dedupe-142-drafts.json (issue #153's
49
+ // corpus, ground truth known), same-defect pairs score 0.286-0.667 and
50
+ // different-defect pairs score 0.143-0.310. 0.35 sits above EVERY
51
+ // different-defect pair, so the direction of error is under-merge: a duplicate
52
+ // comment is visible, a destroyed BLOCKER is not.
53
+ //
54
+ // State the weakness plainly. The margin is thin (0.310 vs 0.35) and the
55
+ // calibration is one 29-line fixture. Musive PR 1727's must-merge pair
56
+ // (935/978) survives only as line numbers in the comment above — its claim
57
+ // text was never recorded — so this design CANNOT guarantee that pair still
58
+ // merges, and duplicates that used to collapse may now post twice. The
59
+ // severe-loser trace in review/report.ts's "Not reported" section is what keeps the
60
+ // residual risk visible instead of silent.
61
+ export const DEDUPE_CLAIM_OVERLAP_THRESHOLD = 0.35;
62
+
63
+ // Below this many distinct tokens a claim is refused as a fold candidate: a
64
+ // short claim scores high overlap against anything that happens to contain its
65
+ // few tokens, so the coefficient stops discriminating. The observed minimum on
66
+ // the real corpus is 14 distinct tokens, so this guard never fires on the
67
+ // calibration set — it is a guard against a degenerate case that set does not
68
+ // contain.
69
+ export const DEDUPE_CLAIM_MIN_TOKENS = 8;
70
+
71
+ function claimTokens(claim: string): Set<string> {
72
+ const tokens = new Set<string>();
73
+ for (const raw of claim.toLowerCase().split(/[^a-z0-9_]+/)) {
74
+ // Length 1 drops the array/index noise ("a", "i", "x") that survives the
75
+ // stopword list without naming anything.
76
+ if (raw.length <= 1) continue;
77
+ if (CLAIM_STOPWORDS.has(raw)) continue;
78
+ tokens.add(raw);
79
+ }
80
+ return tokens;
81
+ }
82
+
83
+ // OVERLAP COEFFICIENT, |A n B| / min(|A|,|B|) — deliberately not Jaccard.
84
+ // Jaccard does not separate on this corpus: same-defect pairs score
85
+ // 0.143-0.417 and different-defect pairs 0.049-0.156, ranges that CROSS, so no
86
+ // Jaccard threshold exists that folds duplicates without also folding distinct
87
+ // defects. Overlap normalises by the shorter claim, which is what makes a
88
+ // terse hunter's paraphrase of a verbose hunter's claim score as the same
89
+ // defect.
90
+ //
91
+ // Returns 0 when either claim is under DEDUPE_CLAIM_MIN_TOKENS: refusing to
92
+ // score is refusing permission to discard.
93
+ export function claimSimilarity(a: string, b: string): number {
94
+ const left = claimTokens(a);
95
+ const right = claimTokens(b);
96
+ const smaller = Math.min(left.size, right.size);
97
+ if (smaller < DEDUPE_CLAIM_MIN_TOKENS) return 0;
98
+ let shared = 0;
99
+ for (const token of left) {
100
+ if (right.has(token)) shared++;
101
+ }
102
+ return shared / smaller;
103
+ }
104
+
105
+ // The permission to DISCARD a claim. Grouping (exact key, path proximity)
106
+ // stays a wide recall net; this is the narrow gate in front of the delete.
107
+ function mayFold(loser: DraftFinding, winner: DraftFinding): boolean {
108
+ return (
109
+ claimSimilarity(loser.claim, winner.claim) >= DEDUPE_CLAIM_OVERLAP_THRESHOLD
110
+ );
111
+ }
112
+
113
+ // Richest claim: higher severity first, then more proof_refs; a full tie
114
+ // keeps the earlier finding (first appearance), which keeps the pass stable.
115
+ function pickRichest(group: DraftFinding[]): DraftFinding {
116
+ return group.reduce((best, candidate) => {
117
+ const bySeverity =
118
+ SEVERITY_RANK[candidate.severity] - SEVERITY_RANK[best.severity];
119
+ if (bySeverity < 0) return candidate;
120
+ if (
121
+ bySeverity === 0 &&
122
+ candidate.proof_refs.length > best.proof_refs.length
123
+ ) {
124
+ return candidate;
125
+ }
126
+ return best;
127
+ });
128
+ }
129
+
130
+ // Connected components on |lineA - lineB| <= window, preserving the group's
131
+ // first-appearance order so survivors still emit where the cluster first
132
+ // showed up. Transitive on purpose: 935 and 978 join through each other
133
+ // without a middle finding; 544 cannot join them without a finding every
134
+ // ~100 lines in between, which 1727 did not have.
135
+ function clusterByProximity(
136
+ group: DraftFinding[],
137
+ window: number,
138
+ ): DraftFinding[][] {
139
+ const n = group.length;
140
+ const parent = Array.from({ length: n }, (_, i) => i);
141
+ const find = (i: number): number => {
142
+ let root = i;
143
+ while (parent[root] !== root) root = parent[root];
144
+ let cursor = i;
145
+ while (parent[cursor] !== root) {
146
+ const next = parent[cursor];
147
+ parent[cursor] = root;
148
+ cursor = next;
149
+ }
150
+ return root;
151
+ };
152
+ for (let i = 0; i < n; i++) {
153
+ for (let j = i + 1; j < n; j++) {
154
+ const a = group[i];
155
+ const b = group[j];
156
+ if (a === undefined || b === undefined) continue;
157
+ if (Math.abs(a.line - b.line) > window) continue;
158
+ const ri = find(i);
159
+ const rj = find(j);
160
+ if (ri !== rj) parent[ri] = rj;
161
+ }
162
+ }
163
+ const clusters: DraftFinding[][] = [];
164
+ const indexByRoot = new Map<number, number>();
165
+ for (let i = 0; i < n; i++) {
166
+ const member = group[i];
167
+ if (member === undefined) continue;
168
+ const root = find(i);
169
+ const existing = indexByRoot.get(root);
170
+ if (existing === undefined) {
171
+ indexByRoot.set(root, clusters.length);
172
+ clusters.push([member]);
173
+ } else {
174
+ clusters[existing]?.push(member);
175
+ }
176
+ }
177
+ return clusters;
178
+ }
179
+
180
+ // One round of folding: the richest remaining member leads, and every other
181
+ // remaining member folds into it ONLY IF the claim gate says so. Members that
182
+ // do not fold stay for the next round and get their own survivor. Repeat until
183
+ // the group is empty.
184
+ //
185
+ // Similarity is measured ONLY against the round's winner, never member to
186
+ // member. Text similarity is not transitive: a "bridge" claim that reads like
187
+ // both of two dissimilar claims would let them chain into one component and
188
+ // discard a real defect — the same failure mode the proximity net already has,
189
+ // re-introduced through prose. Measuring against the leader means every
190
+ // discard was licensed pairwise against the claim that actually survives.
191
+ //
192
+ // Returned in first-appearance order of each winner within `group`, so the
193
+ // callers' "survivor surfaces where its cluster first appeared" ordering holds
194
+ // whether a group yields one winner or five.
195
+ function foldGroup(
196
+ group: DraftFinding[],
197
+ ): Array<{ winner: DraftFinding; folded: DraftFinding[] }> {
198
+ const rounds: Array<{ winner: DraftFinding; folded: DraftFinding[] }> = [];
199
+ let remaining = group;
200
+ while (remaining.length > 0) {
201
+ const winner = pickRichest(remaining);
202
+ const folded: DraftFinding[] = [];
203
+ const carried: DraftFinding[] = [];
204
+ for (const member of remaining) {
205
+ if (member === winner) continue;
206
+ if (mayFold(member, winner)) folded.push(member);
207
+ else carried.push(member);
208
+ }
209
+ rounds.push({ winner, folded });
210
+ remaining = carried;
211
+ }
212
+ const orderOf = new Map<DraftFinding, number>();
213
+ group.forEach((member, i) => {
214
+ if (!orderOf.has(member)) orderOf.set(member, i);
215
+ });
216
+ return rounds.sort(
217
+ (a, b) =>
218
+ (orderOf.get(a.winner) as number) - (orderOf.get(b.winner) as number),
219
+ );
220
+ }
221
+
222
+ export function mergeAndDedupe(drafts: DraftFinding[]): {
223
+ survivors: DedupedSurvivor[];
224
+ deduped: DedupeLoser[];
225
+ } {
226
+ // Loser → the finding it merged into. Chained at the end: a pass-1 loser
227
+ // whose winner then loses pass 2 must point at the ultimate survivor.
228
+ const winnerOf = new Map<DraftFinding, DraftFinding>();
229
+
230
+ // Pass 1 — exact dedupe_key groups. Map preserves insertion order, so
231
+ // winners come out in first-appearance order of their groups.
232
+ //
233
+ // An exact key match is NOT proof of the same defect, and #153 paid for
234
+ // that: in the opencode arm `src/authz.ts:authorize:6` was the key of both
235
+ // the stale-cache draft (L16) and the fail-open draft (L26) — one key, two
236
+ // planted BLOCKERs — and take-all destroyed one of them before pass 2 ever
237
+ // ran. The key is built from `symbol`, which is free text a model writes,
238
+ // so it is a recall net like any other. A member that does not fold emits
239
+ // as its own survivor and flows into pass 2.
240
+ //
241
+ // Pass 1 deliberately does NOT fold proof_refs (it never has); leaving that
242
+ // alone keeps this change about the discard decision only.
243
+ const byKey = new Map<string, DraftFinding[]>();
244
+ for (const draft of drafts) {
245
+ const group = byKey.get(draft.dedupe_key);
246
+ if (group) group.push(draft);
247
+ else byKey.set(draft.dedupe_key, [draft]);
248
+ }
249
+ const pass1: DraftFinding[] = [];
250
+ for (const group of byKey.values()) {
251
+ for (const { winner, folded } of foldGroup(group)) {
252
+ pass1.push(winner);
253
+ for (const loser of folded) winnerOf.set(loser, winner);
254
+ }
255
+ }
256
+
257
+ // Pass 2 — cross-category collapse on PATH, with `symbol` dropped as an
258
+ // identity axis entirely (#153). It was free text a model writes, and it
259
+ // failed both ways on the same 29-line file: three hunters spelled one
260
+ // defect `authorize`, `authorize / roleCache` and `roleCache` and got three
261
+ // findings, while two hunters agreeing on `authorize` had their independent
262
+ // BLOCKERs merged into a claim about a different defect.
263
+ //
264
+ // Symbol-less findings now participate too. The old exclusion was written
265
+ // because path-alone keying would over-merge distinct defects file-wide —
266
+ // true, and the warning still names a real risk, but it assumed
267
+ // DISCARD-ON-COLLISION. Grouping no longer discards anything: the claim gate
268
+ // does, and a conservative gate does not discard non-duplicates.
269
+ //
270
+ // Grouping stays a recall net for "same defect, different hunter category",
271
+ // not "everything in this file is one bug". Musive PR 1727 collapsed a
272
+ // tap-to-audio finding at playAudio:544 into a timer-leak finding at :935
273
+ // because both carried symbol `playAudio`; pickRichest kept line 544
274
+ // (off-diff) and the in-diff leak posted only as Evidence inside the
275
+ // summary. Proximity still forms the candidate cluster — only the connected
276
+ // component whose lines fall inside DEDUPE_SYMBOL_LINE_WINDOW — and greedy
277
+ // leader-based folding then decides, inside that cluster, what may be
278
+ // discarded.
279
+ const byPath = new Map<string, DraftFinding[]>();
280
+ for (const draft of pass1) {
281
+ const group = byPath.get(draft.path);
282
+ if (group) group.push(draft);
283
+ else byPath.set(draft.path, [draft]);
284
+ }
285
+ // A winner's FOLDED members, not its whole proximity cluster: the cluster is
286
+ // the candidate net, and folding refs from a candidate that was never
287
+ // discarded would attach one defect's evidence to another's finding.
288
+ const foldedInto = new Map<DraftFinding, DraftFinding[]>();
289
+ const clusterWinnerOf = new Map<DraftFinding, DraftFinding>();
290
+ for (const group of byPath.values()) {
291
+ for (const cluster of clusterByProximity(
292
+ group,
293
+ DEDUPE_SYMBOL_LINE_WINDOW,
294
+ )) {
295
+ for (const { winner, folded } of foldGroup(cluster)) {
296
+ foldedInto.set(winner, folded);
297
+ clusterWinnerOf.set(winner, winner);
298
+ for (const loser of folded) {
299
+ clusterWinnerOf.set(loser, winner);
300
+ winnerOf.set(loser, winner);
301
+ }
302
+ }
303
+ }
304
+ }
305
+
306
+ // Emit survivors in first-appearance order: a collapsed cluster's winner
307
+ // surfaces at the position where the cluster first appeared. The winner
308
+ // keeps its own category; losers' proof_refs fold in after the winner's
309
+ // own, deduplicated. Two clusters that share a path:symbol (1727: 544 vs
310
+ // 935/978) each emit — emittedKeys used to be path:symbol and would have
311
+ // dropped the second cluster.
312
+ const survivorsRaw: DraftFinding[] = [];
313
+ const emittedWinners = new Set<DraftFinding>();
314
+ // Folded survivors are new objects; winnerOf chains still point at the
315
+ // original winner, so renumbering has to key on that original.
316
+ const originalOf = new Map<DraftFinding, DraftFinding>();
317
+ for (const draft of pass1) {
318
+ const winner = clusterWinnerOf.get(draft) as DraftFinding;
319
+ if (emittedWinners.has(winner)) continue;
320
+ emittedWinners.add(winner);
321
+ const losers = foldedInto.get(winner) as DraftFinding[];
322
+ if (losers.length === 0) {
323
+ survivorsRaw.push(winner);
324
+ originalOf.set(winner, winner);
325
+ continue;
326
+ }
327
+ const foldedRefs = [...winner.proof_refs];
328
+ for (const member of losers) {
329
+ for (const ref of member.proof_refs) {
330
+ if (!foldedRefs.includes(ref)) foldedRefs.push(ref);
331
+ }
332
+ }
333
+ const folded = { ...winner, proof_refs: foldedRefs };
334
+ survivorsRaw.push(folded);
335
+ originalOf.set(folded, winner);
336
+ }
337
+
338
+ // Renumbering happens HERE, before the refuter (a JD finding): the refuter
339
+ // batch, verdict mapping, and losers' merged_into all use these final ids —
340
+ // renumbering later would leave debug.deduped[] pointing at dead ids.
341
+ const finalId = new Map<DraftFinding, string>();
342
+ const survivors = survivorsRaw.map((survivor, i) => {
343
+ const id = `F${String(i + 1).padStart(3, "0")}`;
344
+ const original = originalOf.get(survivor) ?? survivor;
345
+ finalId.set(original, id);
346
+ return { ...survivor, id };
347
+ });
348
+
349
+ const resolve = (loser: DraftFinding): string => {
350
+ let current = loser;
351
+ let next = winnerOf.get(current);
352
+ while (next) {
353
+ current = next;
354
+ next = winnerOf.get(current);
355
+ }
356
+ return finalId.get(current) as string;
357
+ };
358
+
359
+ // Losers in original input order, each verbatim plus merged_into.
360
+ const deduped: DedupeLoser[] = [];
361
+ for (const draft of drafts) {
362
+ if (winnerOf.has(draft)) {
363
+ deduped.push({ ...draft, merged_into: resolve(draft) });
364
+ }
365
+ }
366
+
367
+ return { survivors, deduped };
368
+ }