crysk 0.0.0 → 0.3.1
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.
- package/LICENSE +176 -0
- package/README.md +611 -3
- package/bin/pr-hero.js +166 -0
- package/config/models/anthropic.json +5 -0
- package/dist/SKILL-7e3725zn.md +496 -0
- package/dist/SKILL-7q35wcaz.md +66 -0
- package/dist/SKILL-9at22381.md +74 -0
- package/dist/SKILL-ex2hxxh3.md +134 -0
- package/dist/SKILL-y8rv8msh.md +134 -0
- package/dist/SKILL-ygka1bs0.md +515 -0
- package/dist/adjudicator-dtg2axnh.md +86 -0
- package/dist/admission-config.example-a9vqmmad.json +9 -0
- package/dist/ci-admission-camfbx2b.md +122 -0
- package/dist/ci-admission-qk9r9taq.md +124 -0
- package/dist/cli.js +42501 -0
- package/dist/consolidate-ac0mnfyj.md +24 -0
- package/dist/deep-review-lifecycle-m698ybjp.md +362 -0
- package/dist/deep-review-logic-a6b1gczf.md +156 -0
- package/dist/deep-review-parity-0ec904db.md +120 -0
- package/dist/deep-review-reliability-qyakbjb9.md +125 -0
- package/dist/deep-review-resilience-3tz8y26e.md +118 -0
- package/dist/index.js +20190 -0
- package/dist/opencode-ci-7b0be64p.md +141 -0
- package/dist/review-refuter-j4e9gezy.md +71 -0
- package/dist/scout-yvp153q7.md +140 -0
- package/dist/summarizer-zca44nxa.md +18 -0
- package/dist/workflow-w1g9szna.yml +151 -0
- package/package.json +81 -4
- package/prompts/consolidate.md +24 -0
- package/prompts/default/PROVENANCE.md +18 -0
- package/prompts/default/deep-review-lifecycle.md +362 -0
- package/prompts/default/deep-review-logic.md +156 -0
- package/prompts/default/deep-review-parity.md +120 -0
- package/prompts/default/deep-review-reliability.md +125 -0
- package/prompts/default/deep-review-resilience.md +118 -0
- package/prompts/default/review-refuter.md +71 -0
- package/prompts/scout.md +140 -0
- package/prompts/summarizer.md +18 -0
- package/skills/crysk/SKILL.md +74 -0
- package/skills/crysk-ci-setup/SKILL.md +134 -0
- package/skills/crysk-ci-setup/assets/admission-config.example.json +9 -0
- package/skills/crysk-ci-setup/assets/workflow.yml +151 -0
- package/skills/crysk-ci-setup/references/ci-admission.md +124 -0
- package/skills/crysk-ci-setup/references/opencode-ci.md +141 -0
- package/skills/crysk-loop/SKILL.md +515 -0
- package/skills/crysk-loop/adjudicator.md +86 -0
- package/skills/crysk-pr/SKILL.md +66 -0
- package/src/agent-env.ts +1084 -0
- package/src/asset-manifest.ts +83 -0
- package/src/assets.d.ts +6 -0
- package/src/assets.ts +183 -0
- package/src/brand-paths.ts +155 -0
- package/src/brand.ts +255 -0
- package/src/ci/admission-ledger.ts +350 -0
- package/src/ci/classify-error.ts +73 -0
- package/src/ci/cli-record.ts +271 -0
- package/src/ci/force-comment.ts +39 -0
- package/src/ci/gates.ts +945 -0
- package/src/ci/outcome.ts +2181 -0
- package/src/ci/override.ts +187 -0
- package/src/ci/publish.ts +1639 -0
- package/src/ci/reporter.ts +626 -0
- package/src/ci/review-admission.ts +910 -0
- package/src/ci/review-levels.ts +310 -0
- package/src/ci/review-policy.ts +18 -0
- package/src/ci/review-risk.ts +184 -0
- package/src/ci/setup.ts +844 -0
- package/src/ci/trigger.ts +1353 -0
- package/src/cli.ts +348 -0
- package/src/commands/activity.ts +67 -0
- package/src/commands/ci-setup-questions.ts +397 -0
- package/src/commands/ci-setup.ts +381 -0
- package/src/commands/config.ts +99 -0
- package/src/commands/doctor.ts +108 -0
- package/src/commands/init.ts +139 -0
- package/src/commands/mcp.ts +10 -0
- package/src/commands/menu.ts +129 -0
- package/src/commands/post.ts +302 -0
- package/src/commands/skills.ts +83 -0
- package/src/commands/status.ts +173 -0
- package/src/commands/triage.ts +342 -0
- package/src/commands/uninstall.ts +37 -0
- package/src/commands/upgrade.ts +214 -0
- package/src/commands/usage.ts +98 -0
- package/src/compare/compare.ts +28 -0
- package/src/compare/floor-test.ts +471 -0
- package/src/compare/martian-adapter.ts +80 -0
- package/src/config/config.ts +267 -0
- package/src/corpus/corpus.ts +1094 -0
- package/src/corpus/preflight.ts +1789 -0
- package/src/diversity/accounting.ts +90 -0
- package/src/diversity/adjudication.ts +145 -0
- package/src/diversity/admission.ts +60 -0
- package/src/diversity/benchmark.ts +195 -0
- package/src/diversity/clustering.ts +285 -0
- package/src/diversity/errors.ts +7 -0
- package/src/diversity/identity.ts +285 -0
- package/src/diversity/index.ts +11 -0
- package/src/diversity/pipeline-integration.ts +448 -0
- package/src/diversity/projection.ts +57 -0
- package/src/diversity/self-contained.ts +68 -0
- package/src/diversity/target-validation.ts +179 -0
- package/src/doctor-migration.ts +209 -0
- package/src/doctor.ts +723 -0
- package/src/errors.ts +83 -0
- package/src/execution/admission.ts +75 -0
- package/src/execution/atomic-write.ts +35 -0
- package/src/execution/attempt-evidence.ts +415 -0
- package/src/execution/bucket-id.ts +136 -0
- package/src/execution/concurrency-limiter.ts +175 -0
- package/src/execution/contracts.ts +443 -0
- package/src/execution/failure-classification.ts +42 -0
- package/src/execution/failure-policy.ts +240 -0
- package/src/execution/harness.ts +2548 -0
- package/src/execution/settlement.ts +348 -0
- package/src/execution/spawned-process.ts +66 -0
- package/src/execution/spend-limiter.ts +424 -0
- package/src/execution/step-artifacts.ts +55 -0
- package/src/execution/usage-normalized.ts +469 -0
- package/src/git/git.ts +396 -0
- package/src/git/identity.ts +64 -0
- package/src/git/refs.ts +140 -0
- package/src/home-preflight.ts +443 -0
- package/src/home.ts +243 -0
- package/src/ignore-file.ts +336 -0
- package/src/ignore-read.ts +118 -0
- package/src/index.ts +34 -0
- package/src/legacy-launchd.ts +29 -0
- package/src/mcp/mcp.ts +150 -0
- package/src/mcp/preflight.ts +407 -0
- package/src/menu-context.ts +192 -0
- package/src/model/catalog.ts +251 -0
- package/src/model/exec-snapshots.ts +390 -0
- package/src/model/free-discovery.ts +302 -0
- package/src/model/provider-capabilities.ts +814 -0
- package/src/model/routing.ts +600 -0
- package/src/pr/admission.ts +984 -0
- package/src/pr/answered-findings.ts +169 -0
- package/src/pr/ci-admission-gate.ts +743 -0
- package/src/pr/ci-publish.ts +76 -0
- package/src/pr/discovery.ts +432 -0
- package/src/pr/inline.ts +507 -0
- package/src/pr/plan.ts +366 -0
- package/src/pr/posting.ts +115 -0
- package/src/pr/pr-status.ts +845 -0
- package/src/pr/pr.ts +3642 -0
- package/src/pr/preflight.ts +1031 -0
- package/src/pr/publish-outcome.ts +361 -0
- package/src/pr/range.ts +119 -0
- package/src/pr/reverts-preflight.ts +606 -0
- package/src/pr/reverts.ts +377 -0
- package/src/pr/review-pr.ts +927 -0
- package/src/pr/status.ts +154 -0
- package/src/pr/target.ts +388 -0
- package/src/pr/teardown.ts +437 -0
- package/src/pr/worktree-setup.ts +60 -0
- package/src/production-runtime.ts +1877 -0
- package/src/progress.ts +487 -0
- package/src/rereview/adjudicate.ts +89 -0
- package/src/rereview/classify.ts +362 -0
- package/src/rereview/identity.ts +211 -0
- package/src/rereview/plan.ts +308 -0
- package/src/rereview/prepare.ts +1025 -0
- package/src/rereview/state.ts +767 -0
- package/src/rereview/verify.ts +241 -0
- package/src/review/boundary.ts +105 -0
- package/src/review/consolidate.ts +246 -0
- package/src/review/dedupe.ts +368 -0
- package/src/review/drafts.ts +448 -0
- package/src/review/findings-conformance.ts +120 -0
- package/src/review/findings.ts +714 -0
- package/src/review/pipeline.ts +3977 -0
- package/src/review/preflight.ts +3575 -0
- package/src/review/prompt-set.ts +177 -0
- package/src/review/proof-refs.ts +187 -0
- package/src/review/report.ts +1821 -0
- package/src/review/review.ts +490 -0
- package/src/review/root-cause.ts +120 -0
- package/src/review/route-preflight.ts +289 -0
- package/src/review/run.ts +601 -0
- package/src/review/scout.ts +323 -0
- package/src/review/size-gate.ts +595 -0
- package/src/review/spec.ts +235 -0
- package/src/review/step-runner.ts +233 -0
- package/src/runner-authority.ts +596 -0
- package/src/security/binding-policy.ts +115 -0
- package/src/security/credential-broker.ts +773 -0
- package/src/security/evidence-redaction.ts +62 -0
- package/src/security/execution-authority.ts +27 -0
- package/src/security/redact.ts +71 -0
- package/src/security/workspace-read-broker.ts +466 -0
- package/src/server/client.ts +190 -0
- package/src/server/preflight.ts +217 -0
- package/src/server/server.ts +327 -0
- package/src/signal-handler.ts +50 -0
- package/src/store/activity.ts +413 -0
- package/src/store/backfill-preflight.ts +49 -0
- package/src/store/backfill.ts +141 -0
- package/src/store/gc-preflight.ts +206 -0
- package/src/store/gc.ts +424 -0
- package/src/store/metrics-preflight.ts +229 -0
- package/src/store/metrics.ts +236 -0
- package/src/store/preflight.ts +748 -0
- package/src/store/store.ts +693 -0
- package/src/system-tools.ts +355 -0
- package/src/transport-registry.ts +1124 -0
- package/src/transports/claude-code-cli.ts +1348 -0
- package/src/transports/opencode-admission.ts +515 -0
- package/src/transports/opencode-client.ts +3328 -0
- package/src/transports/opencode-evidence.ts +557 -0
- package/src/transports/opencode-mcp.ts +248 -0
- package/src/transports/opencode-sdk.ts +2294 -0
- package/src/transports/opencode-server.ts +535 -0
- package/src/triage/adjudication.ts +113 -0
- package/src/triage/reply.ts +125 -0
- package/src/triage/triage.ts +255 -0
- package/src/ui/activity.ts +173 -0
- package/src/ui/config-edit.ts +816 -0
- package/src/ui/config.ts +462 -0
- package/src/ui/menu.ts +1325 -0
- package/src/ui/plan.ts +1039 -0
- package/src/ui/primitives.ts +300 -0
- package/src/ui/progress.ts +298 -0
- package/src/ui/result.ts +552 -0
- package/src/ui/review-menu.ts +312 -0
- package/src/ui/select.ts +528 -0
- package/src/ui/tree.ts +178 -0
- package/src/uninstaller.ts +479 -0
- package/src/updater.ts +836 -0
- package/src/usage.ts +77 -0
- package/src/watch/preflight.ts +1389 -0
- package/src/watch/watch.ts +1235 -0
- package/src/wizard.ts +877 -0
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import type { Severity } from "#review/findings";
|
|
3
|
+
|
|
4
|
+
export interface FindingObservation {
|
|
5
|
+
readonly observationId: string;
|
|
6
|
+
readonly specialty: string;
|
|
7
|
+
readonly legId: string;
|
|
8
|
+
readonly backend: string;
|
|
9
|
+
readonly provider: string;
|
|
10
|
+
readonly gateway?: string;
|
|
11
|
+
readonly modelFamily: string;
|
|
12
|
+
readonly modelSnapshot: string;
|
|
13
|
+
readonly modelVariant?: string;
|
|
14
|
+
readonly replicate: number;
|
|
15
|
+
readonly attempt: number;
|
|
16
|
+
readonly promptFingerprint: string;
|
|
17
|
+
readonly routeFingerprint: string;
|
|
18
|
+
readonly path: string;
|
|
19
|
+
readonly line?: number;
|
|
20
|
+
readonly symbol?: string;
|
|
21
|
+
readonly category: number;
|
|
22
|
+
readonly severity: Severity;
|
|
23
|
+
readonly claim: string;
|
|
24
|
+
readonly evidence: string;
|
|
25
|
+
readonly proofRefs: readonly string[];
|
|
26
|
+
readonly causalHypothesis: string;
|
|
27
|
+
readonly artifactSha256: string;
|
|
28
|
+
readonly dedupeKey?: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export type ObservationRelation =
|
|
32
|
+
| "strong_same_defect"
|
|
33
|
+
| "ambiguous"
|
|
34
|
+
| "distinct";
|
|
35
|
+
|
|
36
|
+
export interface ProofAnchor {
|
|
37
|
+
readonly path: string;
|
|
38
|
+
readonly symbol?: string;
|
|
39
|
+
readonly anchorHash: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function canonicalProofAnchor(
|
|
43
|
+
path: string,
|
|
44
|
+
symbol: string | undefined,
|
|
45
|
+
span: string,
|
|
46
|
+
): ProofAnchor {
|
|
47
|
+
const normalizedPath = path.replace(/\\/g, "/");
|
|
48
|
+
const anchorHash = createHash("sha256")
|
|
49
|
+
.update(`${normalizedPath}|${symbol ?? ""}|${span}`)
|
|
50
|
+
.digest("hex");
|
|
51
|
+
return { path: normalizedPath, symbol, anchorHash };
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function normalizedDedupeKey(observation: FindingObservation): string {
|
|
55
|
+
const anchors = [...observationAnchorKeys(observation)].sort().join(";");
|
|
56
|
+
return createHash("sha256")
|
|
57
|
+
.update(
|
|
58
|
+
`${observation.specialty}|${observation.path}|${observation.symbol ?? ""}|${observation.category}|${anchors}`,
|
|
59
|
+
)
|
|
60
|
+
.digest("hex");
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function observationAnchorKeys(
|
|
64
|
+
observation: FindingObservation,
|
|
65
|
+
): readonly string[] {
|
|
66
|
+
return observation.proofRefs.map(
|
|
67
|
+
(proofRef) =>
|
|
68
|
+
canonicalProofAnchor(observation.path, observation.symbol, proofRef)
|
|
69
|
+
.anchorHash,
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function compareObservations(
|
|
74
|
+
left: FindingObservation,
|
|
75
|
+
right: FindingObservation,
|
|
76
|
+
lineWindow = 100,
|
|
77
|
+
): ObservationRelation {
|
|
78
|
+
if (left.specialty !== right.specialty) return "distinct";
|
|
79
|
+
const leftAnchors = new Set(observationAnchorKeys(left));
|
|
80
|
+
const rightAnchors = new Set(observationAnchorKeys(right));
|
|
81
|
+
const sharedAnchors = [...leftAnchors].filter((anchor) =>
|
|
82
|
+
rightAnchors.has(anchor),
|
|
83
|
+
);
|
|
84
|
+
if (sharedAnchors.length > 0) {
|
|
85
|
+
return left.symbol === right.symbol ? "strong_same_defect" : "ambiguous";
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const sameSymbol =
|
|
89
|
+
left.symbol !== undefined &&
|
|
90
|
+
right.symbol !== undefined &&
|
|
91
|
+
left.symbol === right.symbol;
|
|
92
|
+
if (sameSymbol && left.line !== undefined && right.line !== undefined) {
|
|
93
|
+
if (Math.abs(left.line - right.line) <= lineWindow) {
|
|
94
|
+
return "ambiguous";
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
if (left.path !== right.path) return "distinct";
|
|
98
|
+
return "ambiguous";
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export interface FindingCluster {
|
|
102
|
+
readonly clusterId: string;
|
|
103
|
+
readonly observations: readonly FindingObservation[];
|
|
104
|
+
readonly schedulingSeverity: Severity;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export interface AdjudicationGroup {
|
|
108
|
+
readonly groupId: string;
|
|
109
|
+
readonly clusters: readonly FindingCluster[];
|
|
110
|
+
readonly ambiguous: boolean;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const SEVERITY_RANK: Record<Severity, number> = {
|
|
114
|
+
BLOCKER: 0,
|
|
115
|
+
CRITICAL: 1,
|
|
116
|
+
WARNING: 2,
|
|
117
|
+
SUGGESTION: 3,
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
function maxSeverity(observations: readonly FindingObservation[]): Severity {
|
|
121
|
+
return observations.reduce<Severity>((best, observation) => {
|
|
122
|
+
return SEVERITY_RANK[observation.severity] < SEVERITY_RANK[best]
|
|
123
|
+
? observation.severity
|
|
124
|
+
: best;
|
|
125
|
+
}, "SUGGESTION");
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function sortObservations(
|
|
129
|
+
observations: readonly FindingObservation[],
|
|
130
|
+
): FindingObservation[] {
|
|
131
|
+
return [...observations].sort((a, b) => {
|
|
132
|
+
const path = a.path.localeCompare(b.path);
|
|
133
|
+
if (path !== 0) return path;
|
|
134
|
+
const symbol = (a.symbol ?? "").localeCompare(b.symbol ?? "");
|
|
135
|
+
if (symbol !== 0) return symbol;
|
|
136
|
+
const line = (a.line ?? -1) - (b.line ?? -1);
|
|
137
|
+
if (line !== 0) return line;
|
|
138
|
+
return a.observationId.localeCompare(b.observationId);
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function isCompleteGraph(
|
|
143
|
+
edges: Set<string>,
|
|
144
|
+
nodes: readonly string[],
|
|
145
|
+
): boolean {
|
|
146
|
+
if (nodes.length <= 1) return true;
|
|
147
|
+
for (let i = 0; i < nodes.length; i++) {
|
|
148
|
+
for (let j = i + 1; j < nodes.length; j++) {
|
|
149
|
+
const a = nodes[i];
|
|
150
|
+
const b = nodes[j];
|
|
151
|
+
if (!edges.has(`${a}|${b}`) && !edges.has(`${b}|${a}`)) return false;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return true;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export function buildClusters(
|
|
158
|
+
observations: readonly FindingObservation[],
|
|
159
|
+
): readonly FindingCluster[] {
|
|
160
|
+
const sorted = sortObservations(observations);
|
|
161
|
+
const strongEdges = new Set<string>();
|
|
162
|
+
for (let i = 0; i < sorted.length; i++) {
|
|
163
|
+
for (let j = i + 1; j < sorted.length; j++) {
|
|
164
|
+
const left = sorted[i];
|
|
165
|
+
const right = sorted[j];
|
|
166
|
+
if (!left || !right) continue;
|
|
167
|
+
if (compareObservations(left, right) === "strong_same_defect") {
|
|
168
|
+
strongEdges.add(`${left.observationId}|${right.observationId}`);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const visited = new Set<string>();
|
|
174
|
+
const clusters: FindingCluster[] = [];
|
|
175
|
+
for (const seed of sorted) {
|
|
176
|
+
if (visited.has(seed.observationId)) continue;
|
|
177
|
+
const component = sorted.filter((candidate) => {
|
|
178
|
+
if (candidate.observationId === seed.observationId) return true;
|
|
179
|
+
return (
|
|
180
|
+
strongEdges.has(`${seed.observationId}|${candidate.observationId}`) ||
|
|
181
|
+
strongEdges.has(`${candidate.observationId}|${seed.observationId}`)
|
|
182
|
+
);
|
|
183
|
+
});
|
|
184
|
+
const ids = component.map((observation) => observation.observationId);
|
|
185
|
+
if (!isCompleteGraph(strongEdges, ids)) {
|
|
186
|
+
for (const observation of component) {
|
|
187
|
+
clusters.push({
|
|
188
|
+
clusterId: createHash("sha256")
|
|
189
|
+
.update(`singleton:${observation.observationId}`)
|
|
190
|
+
.digest("hex")
|
|
191
|
+
.slice(0, 16),
|
|
192
|
+
observations: [observation],
|
|
193
|
+
schedulingSeverity: observation.severity,
|
|
194
|
+
});
|
|
195
|
+
visited.add(observation.observationId);
|
|
196
|
+
}
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
for (const observation of component) visited.add(observation.observationId);
|
|
200
|
+
const clusterId = createHash("sha256")
|
|
201
|
+
.update(ids.sort().join(";"))
|
|
202
|
+
.digest("hex")
|
|
203
|
+
.slice(0, 16);
|
|
204
|
+
clusters.push({
|
|
205
|
+
clusterId,
|
|
206
|
+
observations: component,
|
|
207
|
+
schedulingSeverity: maxSeverity(component),
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
return clusters.sort((a, b) =>
|
|
211
|
+
sortObservations(a.observations)[0]?.observationId.localeCompare(
|
|
212
|
+
sortObservations(b.observations)[0]?.observationId ?? "",
|
|
213
|
+
),
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export function buildAdjudicationGroups(
|
|
218
|
+
observations: readonly FindingObservation[],
|
|
219
|
+
): readonly AdjudicationGroup[] {
|
|
220
|
+
const clusters = buildClusters(observations);
|
|
221
|
+
const ambiguousPairs: Array<[string, string]> = [];
|
|
222
|
+
for (let i = 0; i < observations.length; i++) {
|
|
223
|
+
for (let j = i + 1; j < observations.length; j++) {
|
|
224
|
+
const left = observations[i];
|
|
225
|
+
const right = observations[j];
|
|
226
|
+
if (!left || !right) continue;
|
|
227
|
+
if (compareObservations(left, right) === "ambiguous") {
|
|
228
|
+
ambiguousPairs.push([left.observationId, right.observationId]);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
const parent = new Map<string, string>();
|
|
234
|
+
const find = (id: string): string => {
|
|
235
|
+
const current = parent.get(id) ?? id;
|
|
236
|
+
if (current === id) return id;
|
|
237
|
+
const root = find(current);
|
|
238
|
+
parent.set(id, root);
|
|
239
|
+
return root;
|
|
240
|
+
};
|
|
241
|
+
const union = (a: string, b: string): void => {
|
|
242
|
+
parent.set(find(a), find(b));
|
|
243
|
+
};
|
|
244
|
+
for (const cluster of clusters) {
|
|
245
|
+
for (const observation of cluster.observations) {
|
|
246
|
+
parent.set(observation.observationId, observation.observationId);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
for (const [left, right] of ambiguousPairs) union(left, right);
|
|
250
|
+
|
|
251
|
+
const grouped = new Map<string, FindingCluster[]>();
|
|
252
|
+
for (const cluster of clusters) {
|
|
253
|
+
const root = find(
|
|
254
|
+
cluster.observations[0]?.observationId ?? cluster.clusterId,
|
|
255
|
+
);
|
|
256
|
+
const existing = grouped.get(root) ?? [];
|
|
257
|
+
existing.push(cluster);
|
|
258
|
+
grouped.set(root, existing);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
const groups: AdjudicationGroup[] = [];
|
|
262
|
+
for (const [root, groupClusters] of grouped) {
|
|
263
|
+
const clusterObservationIds = new Set(
|
|
264
|
+
groupClusters.flatMap((cluster) =>
|
|
265
|
+
cluster.observations.map((observation) => observation.observationId),
|
|
266
|
+
),
|
|
267
|
+
);
|
|
268
|
+
const ambiguous = ambiguousPairs.some(
|
|
269
|
+
([left, right]) =>
|
|
270
|
+
clusterObservationIds.has(left) && clusterObservationIds.has(right),
|
|
271
|
+
);
|
|
272
|
+
groups.push({
|
|
273
|
+
groupId: root,
|
|
274
|
+
clusters: groupClusters,
|
|
275
|
+
ambiguous,
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
return groups.sort((a, b) => a.groupId.localeCompare(b.groupId));
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
export function projectClusterIds(
|
|
282
|
+
observations: readonly FindingObservation[],
|
|
283
|
+
): readonly string[] {
|
|
284
|
+
return buildClusters(observations).map((cluster) => cluster.clusterId);
|
|
285
|
+
}
|
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import {
|
|
3
|
+
agentStepKey,
|
|
4
|
+
parseLogicalIdentity,
|
|
5
|
+
type ResolvedStepRoute,
|
|
6
|
+
type RoutingConfig,
|
|
7
|
+
resolveStepRoute,
|
|
8
|
+
} from "#model/routing";
|
|
9
|
+
import {
|
|
10
|
+
type AgentSpec,
|
|
11
|
+
type ReviewSpec,
|
|
12
|
+
resolveSpecialty,
|
|
13
|
+
} from "#review/spec";
|
|
14
|
+
import { DiversityAdmissionError } from "./errors";
|
|
15
|
+
|
|
16
|
+
export interface BenchmarkTarget {
|
|
17
|
+
readonly repoId: string;
|
|
18
|
+
readonly pr: number;
|
|
19
|
+
readonly baseSha: string;
|
|
20
|
+
readonly headSha: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface DiversityLeg {
|
|
24
|
+
readonly legId: string;
|
|
25
|
+
readonly specialty: string;
|
|
26
|
+
readonly agentKey: string;
|
|
27
|
+
readonly logicalModel: string;
|
|
28
|
+
readonly model: string;
|
|
29
|
+
readonly routeFingerprint: string;
|
|
30
|
+
readonly executionKey: string;
|
|
31
|
+
readonly stepKey: string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface DiversityPlan {
|
|
35
|
+
readonly armId: string;
|
|
36
|
+
readonly feature: "multi-model-diversity";
|
|
37
|
+
readonly c2SchemaVersion: string;
|
|
38
|
+
readonly target?: BenchmarkTarget;
|
|
39
|
+
readonly buildFingerprint?: string;
|
|
40
|
+
readonly promptFingerprint?: string;
|
|
41
|
+
readonly legs: readonly DiversityLeg[];
|
|
42
|
+
readonly maxLegs: number;
|
|
43
|
+
readonly cashCapUsd: number;
|
|
44
|
+
readonly planFingerprint: string;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface ExpandedAgentSpec extends AgentSpec {
|
|
48
|
+
readonly legId: string;
|
|
49
|
+
readonly executionKey: string;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function deriveLegId(
|
|
53
|
+
specialty: string,
|
|
54
|
+
modelCanonical: string,
|
|
55
|
+
routeFingerprint: string,
|
|
56
|
+
): string {
|
|
57
|
+
return createHash("sha256")
|
|
58
|
+
.update(`${specialty}|${modelCanonical}|${routeFingerprint}`)
|
|
59
|
+
.digest("hex")
|
|
60
|
+
.slice(0, 16);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function deriveExecutionKey(agentKey: string, legId: string): string {
|
|
64
|
+
return `${agentKey}@${legId}`;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function diversityLegAgentKey(baseKey: string, legId: string): string {
|
|
68
|
+
return `${baseKey}--${legId}`;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function canonicalPlanPayload(
|
|
72
|
+
plan: Omit<DiversityPlan, "planFingerprint">,
|
|
73
|
+
): string {
|
|
74
|
+
const legs = [...plan.legs]
|
|
75
|
+
.map(
|
|
76
|
+
(leg) =>
|
|
77
|
+
`${leg.legId}:${leg.specialty}:${leg.agentKey}:${leg.logicalModel}:${leg.model}:${leg.routeFingerprint}:${leg.executionKey}:${leg.stepKey}`,
|
|
78
|
+
)
|
|
79
|
+
.sort()
|
|
80
|
+
.join(";");
|
|
81
|
+
const target = plan.target
|
|
82
|
+
? `${plan.target.repoId}|${plan.target.pr}|${plan.target.baseSha}|${plan.target.headSha}`
|
|
83
|
+
: "";
|
|
84
|
+
return [
|
|
85
|
+
plan.armId,
|
|
86
|
+
plan.feature,
|
|
87
|
+
plan.c2SchemaVersion,
|
|
88
|
+
target,
|
|
89
|
+
plan.buildFingerprint ?? "",
|
|
90
|
+
plan.promptFingerprint ?? "",
|
|
91
|
+
String(plan.maxLegs),
|
|
92
|
+
String(plan.cashCapUsd),
|
|
93
|
+
legs,
|
|
94
|
+
].join(";");
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function freezeDiversityPlan(
|
|
98
|
+
plan: Omit<DiversityPlan, "planFingerprint">,
|
|
99
|
+
): DiversityPlan {
|
|
100
|
+
const planFingerprint = createHash("sha256")
|
|
101
|
+
.update(canonicalPlanPayload(plan))
|
|
102
|
+
.digest("hex");
|
|
103
|
+
return Object.freeze({ ...plan, planFingerprint });
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export interface BuildDiversityPlanInput {
|
|
107
|
+
readonly spec: ReviewSpec;
|
|
108
|
+
readonly c2SchemaVersion: string;
|
|
109
|
+
readonly routingConfig?: RoutingConfig;
|
|
110
|
+
readonly cliModel?: string;
|
|
111
|
+
readonly frontmatterModel?: (agentKey: string) => string | undefined;
|
|
112
|
+
readonly target?: BenchmarkTarget;
|
|
113
|
+
readonly buildFingerprint?: string;
|
|
114
|
+
readonly promptFingerprint?: string;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export function buildDiversityPlan(
|
|
118
|
+
input: BuildDiversityPlanInput,
|
|
119
|
+
): DiversityPlan {
|
|
120
|
+
const diversity = input.spec.multiModelDiversity;
|
|
121
|
+
if (!diversity?.enabled) {
|
|
122
|
+
throw new DiversityAdmissionError(
|
|
123
|
+
"buildDiversityPlan requires multiModelDiversity.enabled",
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const legs: DiversityLeg[] = [];
|
|
128
|
+
for (const agent of input.spec.agents) {
|
|
129
|
+
if (agent.role !== "hunter") continue;
|
|
130
|
+
const models = agent.models;
|
|
131
|
+
if (!models || models.length === 0) continue;
|
|
132
|
+
const specialty = resolveSpecialty(agent);
|
|
133
|
+
for (const model of models) {
|
|
134
|
+
const parsed = parseLogicalIdentity(model);
|
|
135
|
+
const provisionalKey = diversityLegAgentKey(
|
|
136
|
+
agent.key,
|
|
137
|
+
deriveLegId(specialty, parsed.canonical, "pending"),
|
|
138
|
+
);
|
|
139
|
+
const stepRoute = resolveStepRoute({
|
|
140
|
+
stepKey: agentStepKey({ key: provisionalKey, role: "hunter" }),
|
|
141
|
+
role: "hunter",
|
|
142
|
+
cliModel: input.cliModel,
|
|
143
|
+
specModel: model,
|
|
144
|
+
frontmatterModel: input.frontmatterModel?.(agent.key),
|
|
145
|
+
routingConfig: input.routingConfig,
|
|
146
|
+
});
|
|
147
|
+
const legId = deriveLegId(
|
|
148
|
+
specialty,
|
|
149
|
+
parsed.canonical,
|
|
150
|
+
stepRoute.routeFingerprint,
|
|
151
|
+
);
|
|
152
|
+
const agentKey = diversityLegAgentKey(agent.key, legId);
|
|
153
|
+
const executionKey = deriveExecutionKey(agent.key, legId);
|
|
154
|
+
const stepKey = agentStepKey({ key: agentKey, role: "hunter" });
|
|
155
|
+
legs.push({
|
|
156
|
+
legId,
|
|
157
|
+
specialty,
|
|
158
|
+
agentKey,
|
|
159
|
+
logicalModel: model,
|
|
160
|
+
model: parsed.canonical,
|
|
161
|
+
routeFingerprint: stepRoute.routeFingerprint,
|
|
162
|
+
executionKey,
|
|
163
|
+
stepKey,
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
if (legs.length === 0) {
|
|
169
|
+
throw new DiversityAdmissionError(
|
|
170
|
+
"multiModelDiversity enabled but no hunter carries models[]",
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
if (legs.length > diversity.maxLegs) {
|
|
174
|
+
throw new DiversityAdmissionError(
|
|
175
|
+
`leg count ${legs.length} exceeds maxLegs ${diversity.maxLegs}`,
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const legIds = new Set(legs.map((leg) => leg.legId));
|
|
180
|
+
if (legIds.size !== legs.length) {
|
|
181
|
+
throw new DiversityAdmissionError("duplicate legId after expansion");
|
|
182
|
+
}
|
|
183
|
+
const executionKeys = new Set(legs.map((leg) => leg.executionKey));
|
|
184
|
+
if (executionKeys.size !== legs.length) {
|
|
185
|
+
throw new DiversityAdmissionError("duplicate executionKey after expansion");
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
return freezeDiversityPlan({
|
|
189
|
+
armId: diversity.armId,
|
|
190
|
+
feature: "multi-model-diversity",
|
|
191
|
+
c2SchemaVersion: input.c2SchemaVersion,
|
|
192
|
+
target: input.target,
|
|
193
|
+
buildFingerprint: input.buildFingerprint,
|
|
194
|
+
promptFingerprint: input.promptFingerprint,
|
|
195
|
+
legs,
|
|
196
|
+
maxLegs: diversity.maxLegs,
|
|
197
|
+
cashCapUsd: diversity.cashCapUsd,
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
export function expandDiversityAgents(
|
|
202
|
+
spec: ReviewSpec,
|
|
203
|
+
plan: DiversityPlan,
|
|
204
|
+
): readonly ExpandedAgentSpec[] {
|
|
205
|
+
const expanded: ExpandedAgentSpec[] = [];
|
|
206
|
+
for (const agent of spec.agents) {
|
|
207
|
+
if (agent.role === "refuter") {
|
|
208
|
+
expanded.push({
|
|
209
|
+
...agent,
|
|
210
|
+
legId: agent.key,
|
|
211
|
+
executionKey: agent.key,
|
|
212
|
+
});
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
const models = agent.models;
|
|
216
|
+
if (!models || models.length === 0) {
|
|
217
|
+
expanded.push({
|
|
218
|
+
...agent,
|
|
219
|
+
legId: agent.key,
|
|
220
|
+
executionKey: agent.key,
|
|
221
|
+
});
|
|
222
|
+
continue;
|
|
223
|
+
}
|
|
224
|
+
for (const leg of plan.legs) {
|
|
225
|
+
if (!leg.agentKey.startsWith(`${agent.key}--`)) continue;
|
|
226
|
+
expanded.push({
|
|
227
|
+
...agent,
|
|
228
|
+
key: leg.agentKey,
|
|
229
|
+
specialty: leg.specialty,
|
|
230
|
+
model: leg.logicalModel,
|
|
231
|
+
models: undefined,
|
|
232
|
+
legId: leg.legId,
|
|
233
|
+
executionKey: leg.executionKey,
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
if (
|
|
237
|
+
models.length > 0 &&
|
|
238
|
+
!plan.legs.some((leg) => leg.agentKey.startsWith(`${agent.key}--`))
|
|
239
|
+
) {
|
|
240
|
+
throw new DiversityAdmissionError(
|
|
241
|
+
`no expanded legs found for hunter ${agent.key}`,
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
return expanded;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
export function routeStepsForDiversityPlan(
|
|
249
|
+
plan: DiversityPlan,
|
|
250
|
+
routes: readonly ResolvedStepRoute[],
|
|
251
|
+
): readonly ResolvedStepRoute[] {
|
|
252
|
+
const byStep = new Map(routes.map((route) => [route.stepKey, route]));
|
|
253
|
+
return plan.legs.map((leg) => {
|
|
254
|
+
const route = byStep.get(leg.stepKey);
|
|
255
|
+
if (!route) {
|
|
256
|
+
throw new DiversityAdmissionError(
|
|
257
|
+
`missing resolved route for leg step ${leg.stepKey}`,
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
return route;
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
export function assertRouteFingerprintStable(
|
|
265
|
+
frozen: string,
|
|
266
|
+
current: string,
|
|
267
|
+
legId: string,
|
|
268
|
+
): void {
|
|
269
|
+
if (frozen !== current) {
|
|
270
|
+
throw new DiversityAdmissionError(
|
|
271
|
+
`route drift for leg ${legId}: frozen ${frozen} != current ${current}`,
|
|
272
|
+
);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
export function assertSpendUnderCap(
|
|
277
|
+
plan: DiversityPlan,
|
|
278
|
+
projectedCashUsd: number,
|
|
279
|
+
): void {
|
|
280
|
+
if (projectedCashUsd > plan.cashCapUsd) {
|
|
281
|
+
throw new DiversityAdmissionError(
|
|
282
|
+
`projected cash ${projectedCashUsd} exceeds cap ${plan.cashCapUsd}`,
|
|
283
|
+
);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export * from "./accounting";
|
|
2
|
+
export * from "./adjudication";
|
|
3
|
+
export * from "./admission";
|
|
4
|
+
export * from "./benchmark";
|
|
5
|
+
export * from "./clustering";
|
|
6
|
+
export * from "./errors";
|
|
7
|
+
export * from "./identity";
|
|
8
|
+
export * from "./pipeline-integration";
|
|
9
|
+
export * from "./projection";
|
|
10
|
+
export * from "./self-contained";
|
|
11
|
+
export * from "./target-validation";
|