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.
- package/LICENSE +176 -0
- package/README.md +610 -2
- 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/adjudicator-dtg2axnh.md +86 -0
- package/dist/admission-config.example-a9vqmmad.json +9 -0
- package/dist/ci-admission-camfbx2b.md +122 -0
- package/dist/cli.js +42292 -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 +82 -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 +122 -0
- package/skills/crysk-ci-setup/references/opencode-ci.md +141 -0
- package/skills/crysk-loop/SKILL.md +496 -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 +923 -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 +901 -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 +847 -0
- package/src/pr/ci-admission-gate.ts +665 -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 +787 -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,323 @@
|
|
|
1
|
+
// The scout's PURE half — the diff-only pre-hunter stage (ROADMAP-DOORDASH M3,
|
|
2
|
+
// `docs/scout-design.md` §3.7-§3.9). Engine source, never prompt-set text: the
|
|
3
|
+
// contract string, the caps and the leads block are covered by the engine
|
|
4
|
+
// version, not by the prompt-set fingerprint.
|
|
5
|
+
//
|
|
6
|
+
// WHY this module exists in M4, one milestone before the stage is wired into
|
|
7
|
+
// review/pipeline.ts: so the scout-probe measures the SAME parsing, capping and
|
|
8
|
+
// rendering the engine will ship in M5. A probe carrying its own parser and its
|
|
9
|
+
// own caps would measure the probe's parser — and every number it produced
|
|
10
|
+
// about the PROMPT would be confounded by a second implementation that no
|
|
11
|
+
// production run ever executes.
|
|
12
|
+
|
|
13
|
+
import { normalizePath } from "#compare/compare";
|
|
14
|
+
import { wrapBlock } from "#review/boundary";
|
|
15
|
+
|
|
16
|
+
// Prose §3.7 turned into the engine-owned output contract, beside
|
|
17
|
+
// HUNTER/REFUTER/SUMMARY_OUTPUT_CONTRACT in review/pipeline.ts. This text is driver
|
|
18
|
+
// source: it is covered by the engine version, NOT by the prompt-set
|
|
19
|
+
// fingerprint.
|
|
20
|
+
export const SCOUT_OUTPUT_CONTRACT = [
|
|
21
|
+
"Your final message must be exactly one JSON object — no prose, no code",
|
|
22
|
+
"fences — of the shape",
|
|
23
|
+
'{"leads":[{"path":"...","line":123,"why":"one sentence"}]}. A lead',
|
|
24
|
+
"carries nothing else: no severity, no evidence class, no proof refs, no",
|
|
25
|
+
'hop trail. If the diff hides no suspicious place, return {"leads":[]} —',
|
|
26
|
+
"an empty array is a valid, expected result, not a failure.",
|
|
27
|
+
].join("\n");
|
|
28
|
+
|
|
29
|
+
export interface ScoutLead {
|
|
30
|
+
path: string;
|
|
31
|
+
line: number;
|
|
32
|
+
why: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// The scout gets the patch and the contract and NOTHING else — no priors, no
|
|
36
|
+
// gotchas, no PR title or body (§3.8). Feeding it the same {{PRIORS}}/
|
|
37
|
+
// {{GOTCHAS}} the hunters read would correlate its attention with theirs, and
|
|
38
|
+
// the independence of its pass is the entire reason it can add coverage.
|
|
39
|
+
// Byte-identical in shape to summarizerPrompt: patch, blank line, contract.
|
|
40
|
+
//
|
|
41
|
+
// The patch is the ONE attacker-controlled block this prompt carries and it is
|
|
42
|
+
// the first thing the scout reads, so it is wrapped in C4's nonced boundary
|
|
43
|
+
// tag (`docs/c4-preamble-design.md` §3.3): a diff that adds a line reading
|
|
44
|
+
// `</patch>` cannot end its own block and speak as the engine. `nonce` is
|
|
45
|
+
// REQUIRED rather than defaulted — a default would let a caller compose an
|
|
46
|
+
// unwrapped prompt by omission, which is the whole failure this closes.
|
|
47
|
+
export function scoutPrompt(patch: string, nonce: string): string {
|
|
48
|
+
return [wrapBlock("patch", nonce, patch), "", SCOUT_OUTPUT_CONTRACT].join(
|
|
49
|
+
"\n",
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export class ScoutValidationError extends Error {}
|
|
54
|
+
|
|
55
|
+
function must(condition: boolean, message: string): void {
|
|
56
|
+
if (!condition) throw new ScoutValidationError(message);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Structural validation BEFORE anything reaches the driver — the same role
|
|
60
|
+
// validateHunterDraft plays for a hunter's draft. A step whose final message
|
|
61
|
+
// does not survive these checks is "not delivered".
|
|
62
|
+
export function validateScoutLeads(candidate: unknown): ScoutLead[] {
|
|
63
|
+
must(
|
|
64
|
+
typeof candidate === "object" && candidate !== null,
|
|
65
|
+
"scout output must be an object",
|
|
66
|
+
);
|
|
67
|
+
const d = candidate as Record<string, unknown>;
|
|
68
|
+
// An ABSENT `leads` key is a failure, never an empty list: the contract makes
|
|
69
|
+
// {"leads":[]} the way a scout says "nothing suspicious here", so a model
|
|
70
|
+
// that omitted the key did not tell us it found nothing — it told us nothing.
|
|
71
|
+
must(Array.isArray(d.leads), "scout output .leads must be an array");
|
|
72
|
+
return (d.leads as unknown[]).map((entry, i) => {
|
|
73
|
+
must(
|
|
74
|
+
typeof entry === "object" && entry !== null,
|
|
75
|
+
`scout leads[${i}] must be an object`,
|
|
76
|
+
);
|
|
77
|
+
const lead = entry as Record<string, unknown>;
|
|
78
|
+
must(
|
|
79
|
+
typeof lead.path === "string" && lead.path.length > 0,
|
|
80
|
+
`scout leads[${i}].path required`,
|
|
81
|
+
);
|
|
82
|
+
must(
|
|
83
|
+
typeof lead.path === "string" && lead.path.length <= 400,
|
|
84
|
+
`scout leads[${i}].path must be at most 400 characters`,
|
|
85
|
+
);
|
|
86
|
+
// Git diff notation is not a path. A scout that writes `b/src/x.ts` copied
|
|
87
|
+
// the `+++ b/...` header it was given — it is being literal, not wrong —
|
|
88
|
+
// and the prompt's Coordinates section asks for the bare path. Stripping
|
|
89
|
+
// the prefix HERE, at the one boundary every lead crosses, is what makes
|
|
90
|
+
// that slip free: the caps' per-path grouping, the block the hunters read,
|
|
91
|
+
// §3.10's hunk metric and §3.9's ±25 attribution then all see one spelling.
|
|
92
|
+
// Left unstripped, a correct suspicion at the right line scores as a
|
|
93
|
+
// coverage MISS and the M4 gate silently measures prompt formatting
|
|
94
|
+
// instead of scouting. `compare/compare.ts`'s normalizePath is deliberately NOT
|
|
95
|
+
// the place for this: it defines "the same path" for the PR
|
|
96
|
+
// comparison rows, and widening it there would invent matches across
|
|
97
|
+
// `packages/`.
|
|
98
|
+
const leadPath = (lead.path as string).replace(/^[ab]\//, "");
|
|
99
|
+
must(leadPath.length > 0, `scout leads[${i}].path required`);
|
|
100
|
+
must(
|
|
101
|
+
typeof lead.line === "number" &&
|
|
102
|
+
Number.isInteger(lead.line) &&
|
|
103
|
+
lead.line >= 1,
|
|
104
|
+
`scout leads[${i}].line must be an integer >= 1`,
|
|
105
|
+
);
|
|
106
|
+
must(
|
|
107
|
+
typeof lead.why === "string" && lead.why.length > 0,
|
|
108
|
+
`scout leads[${i}].why required`,
|
|
109
|
+
);
|
|
110
|
+
// NO maximum on `why` here on purpose. The 240-char ceiling is a DRIVER cap
|
|
111
|
+
// applied in capScoutLeads, not grounds to discard a whole step's output:
|
|
112
|
+
// one over-long sentence would otherwise delete eleven good leads and the
|
|
113
|
+
// paid spawn that produced them.
|
|
114
|
+
//
|
|
115
|
+
// Extra keys are IGNORED, never rejected — the return value is narrowed to
|
|
116
|
+
// exactly {path, line, why}. A model that volunteers a `severity` has not
|
|
117
|
+
// failed the contract; silently dropping the key is what keeps the scout
|
|
118
|
+
// structurally incapable of producing a finding (§3.5 mechanism 3), and
|
|
119
|
+
// rejecting it instead would trade that guarantee for a lost step.
|
|
120
|
+
return {
|
|
121
|
+
path: leadPath,
|
|
122
|
+
line: lead.line as number,
|
|
123
|
+
why: lead.why as string,
|
|
124
|
+
};
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// The ceiling (§3.8). Hard, enforced by the driver, never by the prompt alone —
|
|
129
|
+
// a prompt-only cap is a request, and the failure mode DashBench names is a
|
|
130
|
+
// scout that leads on everything.
|
|
131
|
+
export const MAX_LEADS = 12; // above this it is a filter of nothing
|
|
132
|
+
export const MAX_LEADS_PER_PATH = 3; // stops one interesting file absorbing the whole budget
|
|
133
|
+
export const MAX_WHY_CHARS = 240; // one sentence; a paragraph is a finding in disguise
|
|
134
|
+
export const MAX_LEADS_BLOCK_CHARS = 3000; // bounded prompt growth per hunter, x4 hunters
|
|
135
|
+
|
|
136
|
+
// The verbatim block shape from §3.8. The header paragraph is the
|
|
137
|
+
// anti-anchoring guard, and it is what keeps §3.4's "bias" from decaying into
|
|
138
|
+
// "replace" in practice: without it a hunter reads a lead list as a work order
|
|
139
|
+
// and stops scanning the rest of the diff.
|
|
140
|
+
const LEADS_BLOCK_HEADER = [
|
|
141
|
+
"Scout leads — UNVERIFIED suspicions from a diff-only pass that read no",
|
|
142
|
+
"code. They are not findings, they carry no evidence, and confirming one",
|
|
143
|
+
"still requires your own proof_refs. Their absence is not evidence of",
|
|
144
|
+
"absence: your own scan of the whole diff is unchanged.",
|
|
145
|
+
].join("\n");
|
|
146
|
+
|
|
147
|
+
// Empty leads render to the EMPTY STRING, not to a header with no bullets: the
|
|
148
|
+
// block must be absent byte for byte when there is nothing to say. That is what
|
|
149
|
+
// makes M6's control arm a control arm — a scout run that found nothing must
|
|
150
|
+
// produce a hunter prompt identical to an unled run's.
|
|
151
|
+
export function renderLeadsBlock(leads: ScoutLead[]): string {
|
|
152
|
+
if (leads.length === 0) return "";
|
|
153
|
+
const bullets = leads.map((l) => `- ${l.path}:${l.line} — ${l.why}`);
|
|
154
|
+
return [LEADS_BLOCK_HEADER, "", ...bullets].join("\n");
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export interface CappedLeads {
|
|
158
|
+
leads: ScoutLead[];
|
|
159
|
+
dropped: number;
|
|
160
|
+
whyTruncated: number;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// Deterministic, INPUT ORDER, no re-ranking. Re-ranking would make the scout a
|
|
164
|
+
// filter rather than a bias, and a filter's cost lands entirely in the blind
|
|
165
|
+
// spot where neither M6 arm can see it: a lead ranked out is a place no hunter
|
|
166
|
+
// was pointed at and no artifact records.
|
|
167
|
+
//
|
|
168
|
+
// A truncation that fires routinely is a PROMPT defect to fix, never a cap to
|
|
169
|
+
// raise (§3.8) — which is why the probe reports raw and capped counts side by
|
|
170
|
+
// side instead of only what survived.
|
|
171
|
+
export function capScoutLeads(leads: ScoutLead[]): CappedLeads {
|
|
172
|
+
let whyTruncated = 0;
|
|
173
|
+
// (a) per-lead `why` truncation. Counted for every lead it fires on, even one
|
|
174
|
+
// a later step drops — the count answers "is the prompt writing paragraphs?",
|
|
175
|
+
// which is a property of the scout's output, not of what survived the caps.
|
|
176
|
+
const truncated = leads.map((lead) => {
|
|
177
|
+
if (lead.why.length <= MAX_WHY_CHARS) return lead;
|
|
178
|
+
whyTruncated++;
|
|
179
|
+
return { ...lead, why: lead.why.slice(0, MAX_WHY_CHARS) };
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
// (b) per-path: the first MAX_LEADS_PER_PATH per distinct path. Exact string
|
|
183
|
+
// equality, no normalization — the scout is instructed to use the diff's own
|
|
184
|
+
// path spelling (§3.7 Coordinates), so two spellings of one file are a prompt
|
|
185
|
+
// failure to see, not a cap to paper over.
|
|
186
|
+
const perPath = new Map<string, number>();
|
|
187
|
+
const pathCapped: ScoutLead[] = [];
|
|
188
|
+
for (const lead of truncated) {
|
|
189
|
+
const seen = perPath.get(lead.path) ?? 0;
|
|
190
|
+
if (seen >= MAX_LEADS_PER_PATH) continue;
|
|
191
|
+
perPath.set(lead.path, seen + 1);
|
|
192
|
+
pathCapped.push(lead);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// (c) total.
|
|
196
|
+
const kept = pathCapped.slice(0, MAX_LEADS);
|
|
197
|
+
|
|
198
|
+
// (d) block size, measured on the RENDERED block rather than on a sum of
|
|
199
|
+
// field lengths — the header and the bullet punctuation are prompt bytes too.
|
|
200
|
+
// Dropping from the END keeps input order meaningful: the leads that survive
|
|
201
|
+
// are always a prefix of what the scout emitted.
|
|
202
|
+
while (
|
|
203
|
+
kept.length > 0 &&
|
|
204
|
+
renderLeadsBlock(kept).length > MAX_LEADS_BLOCK_CHARS
|
|
205
|
+
) {
|
|
206
|
+
kept.pop();
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
return { leads: kept, dropped: leads.length - kept.length, whyTruncated };
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
export interface HunkRange {
|
|
213
|
+
path: string;
|
|
214
|
+
start: number;
|
|
215
|
+
end: number;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// Right-side (new-file) line ranges, one per hunk, in order of appearance.
|
|
219
|
+
// Both captures matter here, unlike pr/inline.ts's anchor scan: `d` is what turns
|
|
220
|
+
// a header into a RANGE, and `d === 0` is what marks a hunk with no right side.
|
|
221
|
+
const HUNK_HEADER = /^@@ -\d+(?:,\d+)? \+(\d+)(?:,(\d+))? @@/;
|
|
222
|
+
const TARGET_HEADER = /^\+\+\+ (.+)$/;
|
|
223
|
+
|
|
224
|
+
// Parsed from the `+++ b/...` headers plus each `@@` header's right-side counts.
|
|
225
|
+
//
|
|
226
|
+
// WHY a header scan and not pr/inline.ts's splitDiffRecords: this metric needs the
|
|
227
|
+
// hunk BOUNDARIES (a lead lands "inside hunk 3 of 7"), which the anchor-set
|
|
228
|
+
// representation deliberately throws away. The cost is the one structural hole
|
|
229
|
+
// splitDiffRecords closes for free — an ADDED body line whose own content
|
|
230
|
+
// starts with `++` renders as `+++ ...` and reads exactly like a target header.
|
|
231
|
+
// Guarded with state: a `+++` line only counts as a header when we are not
|
|
232
|
+
// inside a hunk body, and a `diff --git` line resets that state.
|
|
233
|
+
export function parseHunkRanges(patch: string): HunkRange[] {
|
|
234
|
+
const ranges: HunkRange[] = [];
|
|
235
|
+
let target: string | undefined;
|
|
236
|
+
let inHunk = false;
|
|
237
|
+
for (const line of patch.split("\n")) {
|
|
238
|
+
if (line.startsWith("diff --git ")) {
|
|
239
|
+
target = undefined;
|
|
240
|
+
inHunk = false;
|
|
241
|
+
continue;
|
|
242
|
+
}
|
|
243
|
+
const header = HUNK_HEADER.exec(line);
|
|
244
|
+
if (header !== null) {
|
|
245
|
+
inHunk = true;
|
|
246
|
+
if (target === undefined) continue;
|
|
247
|
+
const start = Number.parseInt(header[1] as string, 10);
|
|
248
|
+
// An absent count means exactly one line (`@@ -1 +1 @@`), which is git's
|
|
249
|
+
// own shorthand, not a malformed header.
|
|
250
|
+
const count =
|
|
251
|
+
header[2] === undefined ? 1 : Number.parseInt(header[2], 10);
|
|
252
|
+
// A pure-deletion hunk (`+12,0`) has NO right-side line at all. Skipped
|
|
253
|
+
// rather than recorded as an empty range: a lead cannot land on a line
|
|
254
|
+
// that does not exist in the new file, so counting it as a hunk the scout
|
|
255
|
+
// failed to cover would penalise the scout for the impossible.
|
|
256
|
+
if (count === 0) continue;
|
|
257
|
+
ranges.push({ path: target, start, end: start + count - 1 });
|
|
258
|
+
continue;
|
|
259
|
+
}
|
|
260
|
+
if (inHunk) continue;
|
|
261
|
+
const found = TARGET_HEADER.exec(line);
|
|
262
|
+
if (found === null) continue;
|
|
263
|
+
const raw = (found[1] as string).trim();
|
|
264
|
+
// `+++ /dev/null` is a deleted file: it contributes no right-side hunk, and
|
|
265
|
+
// leaving `target` undefined makes its `@@` headers fall through above.
|
|
266
|
+
target = raw === "/dev/null" ? undefined : raw.replace(/^[ab]\//, "");
|
|
267
|
+
}
|
|
268
|
+
return ranges;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
export interface HunkCoverage {
|
|
272
|
+
hunks: number;
|
|
273
|
+
hunksWithLead: number;
|
|
274
|
+
coverage: number;
|
|
275
|
+
unmatchedLeads: number;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// M4's RESTRAINT metric (§3.10 assertion 2). It measures SELECTIVITY — a
|
|
279
|
+
// property of the scout — precisely because cleanliness is a property of a PR
|
|
280
|
+
// we have not triaged: the six restraint PRs carry untriaged `prhero_only`
|
|
281
|
+
// rows, so "this PR is clean" is not a claim this project owns, and a
|
|
282
|
+
// precision number computed against it would be fiction.
|
|
283
|
+
//
|
|
284
|
+
// Stated as what it is: a PROXY. The failure mode DashBench names is not
|
|
285
|
+
// "flagged a clean PR", it is filtering nothing — being loud everywhere — and
|
|
286
|
+
// the fraction of changed hunks carrying a lead measures exactly that without
|
|
287
|
+
// knowing a single defect count.
|
|
288
|
+
//
|
|
289
|
+
// `unmatchedLeads` is the diagnostic beside it: a lead inside no hunk at all is
|
|
290
|
+
// either a miscounted line number (a prompt defect in the Coordinates section)
|
|
291
|
+
// or a suspicion about unchanged code (out of contract). Both are worth seeing,
|
|
292
|
+
// and neither is visible in the coverage fraction alone.
|
|
293
|
+
export function hunkCoverage(patch: string, leads: ScoutLead[]): HunkCoverage {
|
|
294
|
+
const hunks = parseHunkRanges(patch);
|
|
295
|
+
const normalizedLeads = leads.map((l) => ({
|
|
296
|
+
path: normalizePath(l.path),
|
|
297
|
+
line: l.line,
|
|
298
|
+
}));
|
|
299
|
+
const matchedLeads = new Set<number>();
|
|
300
|
+
let hunksWithLead = 0;
|
|
301
|
+
for (const hunk of hunks) {
|
|
302
|
+
const hunkPath = normalizePath(hunk.path);
|
|
303
|
+
let carries = false;
|
|
304
|
+
for (const [i, lead] of normalizedLeads.entries()) {
|
|
305
|
+
if (lead.path !== hunkPath) continue;
|
|
306
|
+
if (lead.line < hunk.start || lead.line > hunk.end) continue;
|
|
307
|
+
// Both loops run to completion: a hunk is counted once no matter how many
|
|
308
|
+
// leads land in it, and every lead that lands anywhere is marked so the
|
|
309
|
+
// unmatched tally stays a per-LEAD number.
|
|
310
|
+
matchedLeads.add(i);
|
|
311
|
+
carries = true;
|
|
312
|
+
}
|
|
313
|
+
if (carries) hunksWithLead++;
|
|
314
|
+
}
|
|
315
|
+
return {
|
|
316
|
+
hunks: hunks.length,
|
|
317
|
+
hunksWithLead,
|
|
318
|
+
// A patch with zero hunks is 0, never NaN — the restraint mean is averaged
|
|
319
|
+
// over runs, and one NaN would poison the whole gate silently.
|
|
320
|
+
coverage: hunks.length === 0 ? 0 : hunksWithLead / hunks.length,
|
|
321
|
+
unmatchedLeads: leads.length - matchedLeads.size,
|
|
322
|
+
};
|
|
323
|
+
}
|