crysk 0.3.3 → 0.5.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 (116) hide show
  1. package/README.md +26 -17
  2. package/config/models/anthropic.json +1 -1
  3. package/dist/SKILL-dsnffnfp.md +140 -0
  4. package/dist/SKILL-gnrbph72.md +140 -0
  5. package/dist/SKILL-k4k0zn1w.md +143 -0
  6. package/dist/SKILL-kq0xejn3.md +553 -0
  7. package/dist/SKILL-qaqsbygw.md +66 -0
  8. package/dist/SKILL-vhb4c3vv.md +553 -0
  9. package/dist/SKILL-vws0aqb8.md +553 -0
  10. package/dist/SKILL-xpf6kmdw.md +74 -0
  11. package/dist/ci-admission-0ken6ehx.md +128 -0
  12. package/dist/ci-admission-t0f5qwth.md +128 -0
  13. package/dist/cli.js +3366 -1680
  14. package/dist/index.js +1884 -947
  15. package/dist/opencode-ci-ac5zksdn.md +106 -0
  16. package/dist/workflow-4pkyck01.yml +160 -0
  17. package/package.json +2 -2
  18. package/prompts/default/PROVENANCE.md +1 -0
  19. package/skills/crysk/SKILL.md +16 -16
  20. package/skills/crysk-ci-setup/SKILL.md +42 -39
  21. package/skills/crysk-ci-setup/assets/workflow.yml +31 -22
  22. package/skills/crysk-ci-setup/references/ci-admission.md +2 -2
  23. package/skills/crysk-ci-setup/references/opencode-ci.md +53 -88
  24. package/skills/crysk-loop/SKILL.md +20 -9
  25. package/skills/crysk-pr/SKILL.md +1 -1
  26. package/src/asset-manifest.ts +0 -4
  27. package/src/assets.ts +2 -2
  28. package/src/ci/admission-ledger.ts +4 -0
  29. package/src/ci/classify-error.ts +21 -24
  30. package/src/ci/cli-record.ts +53 -1
  31. package/src/ci/crysk-secrets.ts +409 -0
  32. package/src/ci/gates.ts +31 -42
  33. package/src/ci/outcome.ts +421 -330
  34. package/src/ci/platform-config.ts +127 -0
  35. package/src/ci/platform-report.ts +935 -0
  36. package/src/ci/platform-trace.ts +551 -0
  37. package/src/ci/platform.ts +465 -0
  38. package/src/ci/publish.ts +19 -6
  39. package/src/ci/reporter.ts +9 -7
  40. package/src/ci/review-admission.ts +8 -4
  41. package/src/ci/review-levels.ts +8 -7
  42. package/src/ci/run.ts +88 -0
  43. package/src/ci/setup.ts +547 -109
  44. package/src/ci/trace-bundle.ts +1282 -0
  45. package/src/ci/trace-transcripts.ts +415 -0
  46. package/src/ci/trigger.ts +176 -33
  47. package/src/cli.ts +11 -3
  48. package/src/commands/ci-setup-questions.ts +3 -3
  49. package/src/commands/ci-setup.ts +152 -6
  50. package/src/commands/config.ts +3 -0
  51. package/src/commands/doctor.ts +15 -6
  52. package/src/compare/loop.ts +9 -1
  53. package/src/compare/martian-adapter.ts +1 -1
  54. package/src/config/config.ts +16 -3
  55. package/src/diversity/clustering.ts +0 -1
  56. package/src/diversity/identity.ts +4 -5
  57. package/src/diversity/pipeline-integration.ts +1 -9
  58. package/src/doctor.ts +3 -3
  59. package/src/execution/attempt-evidence.ts +10 -5
  60. package/src/execution/claude-session-trace.ts +480 -0
  61. package/src/execution/contracts.ts +21 -4
  62. package/src/execution/harness.ts +105 -8
  63. package/src/execution/model-fallback.ts +239 -0
  64. package/src/execution/usage-normalized.ts +6 -4
  65. package/src/home-preflight.ts +1 -1
  66. package/src/index.ts +1 -1
  67. package/src/model/catalog.ts +13 -37
  68. package/src/model/free-discovery.ts +117 -8
  69. package/src/model/model-name.ts +404 -0
  70. package/src/model/provider-capabilities.ts +22 -7
  71. package/src/model/routing.ts +346 -467
  72. package/src/pr/admission.ts +71 -11
  73. package/src/pr/ci-admission-gate.ts +2 -1
  74. package/src/pr/finding-state-record.ts +58 -0
  75. package/src/pr/gate-record.ts +22 -0
  76. package/src/pr/inline.ts +3 -0
  77. package/src/pr/plan.ts +20 -4
  78. package/src/pr/posting.ts +4 -0
  79. package/src/pr/pr-status.ts +27 -7
  80. package/src/pr/pr.ts +39 -8
  81. package/src/pr/preflight.ts +44 -2
  82. package/src/pr/publish-outcome.ts +11 -3
  83. package/src/pr/review-pr.ts +20 -4
  84. package/src/pr/target.ts +12 -3
  85. package/src/production-runtime.ts +760 -331
  86. package/src/rereview/classify.ts +4 -0
  87. package/src/rereview/prepare.ts +47 -22
  88. package/src/rereview/state.ts +34 -8
  89. package/src/review/pipeline.ts +277 -101
  90. package/src/review/preflight.ts +201 -294
  91. package/src/review/report.ts +60 -40
  92. package/src/review/review.ts +20 -3
  93. package/src/review/route-preflight.ts +79 -11
  94. package/src/review/run.ts +28 -6
  95. package/src/review/scout.ts +1 -1
  96. package/src/review/spec.ts +3 -3
  97. package/src/review/step-runner.ts +70 -1
  98. package/src/runner-authority.ts +127 -9
  99. package/src/security/binding-policy.ts +1 -2
  100. package/src/security/claude-credential.ts +355 -0
  101. package/src/security/credential-broker.ts +56 -40
  102. package/src/security/evidence-redaction.ts +179 -4
  103. package/src/security/opencode-secret-manifest.ts +114 -0
  104. package/src/security/published-text.ts +161 -0
  105. package/src/security/redact.ts +45 -0
  106. package/src/system-tools.ts +48 -21
  107. package/src/transport-registry.ts +12 -2
  108. package/src/transports/claude-code-cli.ts +21 -2
  109. package/src/transports/opencode-sdk.ts +11 -9
  110. package/src/transports/opencode-server.ts +7 -10
  111. package/src/triage/triage.ts +2 -1
  112. package/src/ui/config-edit.ts +9 -20
  113. package/src/ui/config.ts +32 -27
  114. package/src/ui/plan.ts +76 -47
  115. package/src/updater.ts +10 -7
  116. package/prompts/default/deep-review-lifecycle.md +0 -362
@@ -0,0 +1,935 @@
1
+ // The action's report step: tell the Crysk platform how a run ended, so every
2
+ // run is recorded (#157) with its cost attributed (#139), then hand it the
3
+ // run's findings so the dashboard can show them.
4
+ //
5
+ // It runs only in platform mode (action.yml gates the step on
6
+ // `workflow_dispatch`, a non-empty `dispatch-id` and a run token being present),
7
+ // after the publish step. Standalone use never reaches it.
8
+ //
9
+ // WHY THE REPORT IS METADATA ONLY: the body carries counts, token totals,
10
+ // costs, model names and shas. It never carries a finding, a claim, a summary,
11
+ // a hop trail or any other text that can quote the customer's code. The
12
+ // builder below COPIES the numeric fields it names out of the run directory
13
+ // instead of forwarding a document, so a field added to findings.json tomorrow
14
+ // cannot leak by default.
15
+ //
16
+ // WHY THE FINDINGS GO SEPARATELY: they are the part that can quote customer
17
+ // code, so they travel in their own request to /runs/findings. The platform
18
+ // encrypts them and honors each repository's metadata-only mode on its side,
19
+ // but a metadata_only repository's code must never leave the runner at all:
20
+ // the action reads `data_mode` from the same token exchange as the run token
21
+ // (CRYSK_DATA_MODE) and, unless it is exactly `full`, this script reduces each
22
+ // finding to non-text fields (METADATA_ONLY_FINDING_FIELDS below). The body is
23
+ // exactly `{ schema_version, findings }`: the `findings` array as the engine
24
+ // wrote it (in `full` mode), never the document's `debug` arrays or its
25
+ // summary prose. It is sent only after the
26
+ // report succeeded (the report creates the run the findings attach to), and
27
+ // never for a run with no findings.json or zero findings.
28
+ //
29
+ // WHY it never fails the job: the review already succeeded or failed on its own
30
+ // merits and its outcome is posted. A platform that is down, a revoked token or
31
+ // a rejected field is reported as a `::warning::` with the HTTP status and the
32
+ // API's message, never a token, and the step exits 0. A findings upload that
33
+ // fails is a warning too, never a red job.
34
+ //
35
+ // WHY the base URL is the constant from ./platform and never an input: the run
36
+ // token goes to that URL (see platform.ts).
37
+ //
38
+ // WHY the run directory is read here: the report must also say what a review
39
+ // that wrote nothing did (skipped, size-gated, failed before writing), so a
40
+ // missing or unreadable directory is the zero report, not an error. The
41
+ // `run_dir` output is empty for a run where every hunter died, a fatal error
42
+ // and a cancelled job (action.yml "KNOWN GAP"), so those report zero counts
43
+ // even when a findings.json was written.
44
+ //
45
+ // RUNS BEFORE NO INSTALL, like publish: every import is a builtin or a module
46
+ // whose own import closure is package-free. test/ci/platform-report.test.ts
47
+ // walks that closure, so a package import added anywhere in it fails a test
48
+ // instead of failing the job at module load.
49
+
50
+ import { readFileSync } from "node:fs";
51
+ import path from "node:path";
52
+ import {
53
+ FINDING_STATE_FILE,
54
+ type FindingStateRecord,
55
+ } from "../pr/finding-state-record";
56
+ import { GATE_RECORD_FILE } from "../pr/gate-record";
57
+ import type { FindingsDocument } from "../review/findings";
58
+ import { validateFindingsDocument } from "../review/findings";
59
+ import { PLATFORM_API_URL, type PlatformDataMode } from "./platform";
60
+
61
+ // The platform caps `agents` at 64 entries.
62
+ const MAX_AGENTS = 64;
63
+
64
+ // The platform accepts at most 500 findings per run. The first 500 in the
65
+ // engine's order go out and the rest are named in a warning.
66
+ const MAX_FINDINGS = 500;
67
+
68
+ // Long enough for a slow API, short enough that a hung endpoint cannot hold the
69
+ // job's tail.
70
+ const REQUEST_TIMEOUT_MS = 30_000;
71
+
72
+ const SHA = /^[0-9a-f]{40}$/;
73
+
74
+ // The token split of pipeline.json's `usage_v2`, under the platform's names. Each key is sent only when the
75
+ // engine reported it as a non-negative number, so a provider that reports no cache sends no cache keys
76
+ // rather than a zero it never measured.
77
+ interface ReportTokens {
78
+ input_uncached?: number;
79
+ input_cache_read?: number;
80
+ input_cache_write?: number;
81
+ output?: number;
82
+ }
83
+
84
+ // Added beside the legacy fields, never replacing them: `cost_usd` stays the cash cost (0 on a
85
+ // subscription), and `list_cost_usd` is what the same tokens cost at list price whatever paid for them.
86
+ interface ReportUsageSplit {
87
+ tokens?: ReportTokens;
88
+ list_cost_usd?: number;
89
+ }
90
+
91
+ export interface RunReportAgent extends ReportUsageSplit {
92
+ agent_key: string;
93
+ model?: string;
94
+ tokens_in: number;
95
+ tokens_out: number;
96
+ cost_usd: number;
97
+ duration_ms?: number;
98
+ attempts?: number;
99
+ status?: string;
100
+ }
101
+
102
+ export interface RunReportBody {
103
+ outcome: string;
104
+ pr_number: number;
105
+ head_sha: string;
106
+ base_sha?: string | null;
107
+ run_status?: string;
108
+ schema_version?: string;
109
+ engine_version?: string;
110
+ model?: string;
111
+ // `blocking` and `advisory` are this run's findings. `unresolved_blocking` is what the gate decides on,
112
+ // earlier findings still open included; absent when the run recorded none.
113
+ // `refuted_by_hunter` counts what the refuter removed, which never reaches `findings[]`.
114
+ counts: {
115
+ blocking: number;
116
+ advisory: number;
117
+ unresolved_blocking?: number;
118
+ refuted_by_hunter?: Record<string, number>;
119
+ };
120
+ usage: ReportUsageSplit & {
121
+ tokens_in: number;
122
+ tokens_out: number;
123
+ cost_usd: number;
124
+ cost_is_floor?: boolean;
125
+ wall_ms?: number;
126
+ index_ms?: number;
127
+ sync_ms?: number;
128
+ billing?: "subscription" | "metered";
129
+ };
130
+ agents: RunReportAgent[];
131
+ // Which findings stay open, under ids that last across runs. Absent when the run recorded none.
132
+ finding_states?: FindingStateRecord;
133
+ }
134
+
135
+ // What the builder knows about the run directory. Either part is absent when
136
+ // the file is missing or unreadable.
137
+ export interface RunDirFacts {
138
+ findings?: unknown;
139
+ pipeline?: unknown;
140
+ gate?: unknown;
141
+ findingStates?: unknown;
142
+ }
143
+
144
+ export interface BuildRunReportInput {
145
+ // The action's `status` output for this run. Empty when the CLI never wrote
146
+ // one (a failure before it started); reported as `error`.
147
+ outcome: string;
148
+ prNumber: number;
149
+ // The trigger's resolved head, the fallback when no findings were written.
150
+ triggerHeadSha: string;
151
+ run: RunDirFacts;
152
+ }
153
+
154
+ // null when the run has no usable head: the platform requires a 40-hex sha, so
155
+ // a report without one cannot be sent.
156
+ export function buildRunReport(
157
+ input: BuildRunReportInput,
158
+ ): RunReportBody | null {
159
+ let doc: FindingsDocument | undefined;
160
+ if (input.run.findings !== undefined) {
161
+ try {
162
+ doc = validateFindingsDocument(input.run.findings);
163
+ } catch {
164
+ // An unreadable document is a run that wrote nothing usable.
165
+ doc = undefined;
166
+ }
167
+ }
168
+
169
+ const headSha = [doc?.head_sha, input.triggerHeadSha].find(
170
+ (sha): sha is string => typeof sha === "string" && SHA.test(sha),
171
+ );
172
+ if (headSha === undefined) return null;
173
+
174
+ if (doc === undefined) {
175
+ return {
176
+ outcome: input.outcome === "" ? "error" : input.outcome,
177
+ pr_number: input.prNumber,
178
+ head_sha: headSha,
179
+ counts: { blocking: 0, advisory: 0 },
180
+ usage: { tokens_in: 0, tokens_out: 0, cost_usd: 0 },
181
+ agents: [],
182
+ };
183
+ }
184
+
185
+ const telemetry = doc.telemetry;
186
+ let blocking = 0;
187
+ let advisory = 0;
188
+ for (const finding of doc.findings) {
189
+ if (finding.tier === "blocking") blocking += 1;
190
+ else if (finding.tier === "advisory") advisory += 1;
191
+ }
192
+
193
+ return {
194
+ outcome: input.outcome === "" ? "error" : input.outcome,
195
+ pr_number: input.prNumber,
196
+ head_sha: headSha,
197
+ ...(typeof doc.base_sha === "string" && SHA.test(doc.base_sha)
198
+ ? { base_sha: doc.base_sha }
199
+ : {}),
200
+ run_status: doc.run_status,
201
+ schema_version: doc.schema_version,
202
+ ...(doc.engine === undefined ? {} : { engine_version: doc.engine.version }),
203
+ ...(typeof doc.model === "string" && doc.model !== ""
204
+ ? { model: doc.model }
205
+ : {}),
206
+ counts: {
207
+ blocking,
208
+ advisory,
209
+ ...gateCount(input.run.gate),
210
+ refuted_by_hunter: refutedByHunter(doc),
211
+ },
212
+ usage: {
213
+ tokens_in: count(telemetry.tokens_in),
214
+ tokens_out: count(telemetry.tokens_out),
215
+ cost_usd: cost(telemetry.cost_usd_est),
216
+ ...(telemetry.cost_usd_est_is_floor === true
217
+ ? { cost_is_floor: true }
218
+ : {}),
219
+ wall_ms: count(telemetry.wall_ms),
220
+ // Zero means no index was built, so there is no time to report.
221
+ ...(count(telemetry.index_ms) > 0
222
+ ? { index_ms: count(telemetry.index_ms) }
223
+ : {}),
224
+ ...(telemetry.sync_ms === undefined
225
+ ? {}
226
+ : { sync_ms: count(telemetry.sync_ms) }),
227
+ ...runUsageSplit(input.run.pipeline),
228
+ },
229
+ agents: buildAgents(doc, input.run.pipeline),
230
+ ...findingStates(input.run.findingStates),
231
+ };
232
+ }
233
+
234
+ // The file is the run's own, but this goes off the runner, in metadata-only mode too: only the known
235
+ // fields of the known shapes pass, so a future field that quotes code cannot ride along. Rows past the
236
+ // cap count as dropped, so the platform says it does not know instead of reading them as resolved.
237
+ const STABLE_ID = /^R\d{3,}$/;
238
+ const FINDING_ID = /^[A-Za-z0-9_.-]{1,64}$/;
239
+ const OPEN_STATUSES = new Set([
240
+ "carried",
241
+ "unconfirmed",
242
+ "suppressed",
243
+ "deferred",
244
+ ]);
245
+ const TIERS = new Set(["blocking", "advisory", "unrecoverable"]);
246
+ const MAX_FINDING_STATES = 500;
247
+
248
+ function findingStates(record: unknown): {
249
+ finding_states?: FindingStateRecord;
250
+ } {
251
+ if (typeof record !== "object" || record === null) return {};
252
+ const { open, fresh, resolved, superseded, dropped } = record as Record<
253
+ string,
254
+ unknown
255
+ >;
256
+ if (
257
+ !Array.isArray(open) ||
258
+ typeof fresh !== "object" ||
259
+ fresh === null ||
260
+ Array.isArray(fresh) ||
261
+ !Array.isArray(resolved) ||
262
+ typeof dropped !== "number" ||
263
+ !Number.isInteger(dropped) ||
264
+ dropped < 0
265
+ ) {
266
+ return {};
267
+ }
268
+ const rows = open.flatMap((row): FindingStateRecord["open"] => {
269
+ const { id, status, tier } = (row ?? {}) as Record<string, unknown>;
270
+ return typeof id === "string" &&
271
+ STABLE_ID.test(id) &&
272
+ typeof status === "string" &&
273
+ OPEN_STATUSES.has(status) &&
274
+ typeof tier === "string" &&
275
+ TIERS.has(tier)
276
+ ? [{ id, status, tier }]
277
+ : [];
278
+ });
279
+ return {
280
+ finding_states: {
281
+ open: rows.slice(0, MAX_FINDING_STATES),
282
+ fresh: Object.fromEntries(
283
+ Object.entries(fresh as Record<string, unknown>)
284
+ .filter(
285
+ (entry): entry is [string, string] =>
286
+ FINDING_ID.test(entry[0]) &&
287
+ typeof entry[1] === "string" &&
288
+ STABLE_ID.test(entry[1]),
289
+ )
290
+ .slice(0, MAX_FINDING_STATES),
291
+ ),
292
+ resolved: resolved
293
+ .filter(
294
+ (id): id is string => typeof id === "string" && STABLE_ID.test(id),
295
+ )
296
+ .slice(0, MAX_FINDING_STATES),
297
+ superseded: (Array.isArray(superseded) ? superseded : [])
298
+ .filter(
299
+ (id): id is string => typeof id === "string" && STABLE_ID.test(id),
300
+ )
301
+ .slice(0, MAX_FINDING_STATES),
302
+ dropped: dropped + Math.max(0, rows.length - MAX_FINDING_STATES),
303
+ },
304
+ };
305
+ }
306
+
307
+ // Counts only, and only a plain name: this goes off the runner in metadata-only mode too, and a refuted
308
+ // finding's claim can quote the customer's code.
309
+ function refutedByHunter(doc: FindingsDocument): Record<string, number> {
310
+ // A Map, so a hunter named like an object's own property counts like any other.
311
+ const counts = new Map<string, number>();
312
+ for (const finding of doc.debug.refuted as unknown[]) {
313
+ const name = (finding as { hunter?: unknown } | null | undefined)?.hunter;
314
+ const hunter =
315
+ typeof name === "string" && FINDING_ID.test(name) ? name : "unknown";
316
+ counts.set(hunter, (counts.get(hunter) ?? 0) + 1);
317
+ }
318
+ return Object.fromEntries(counts);
319
+ }
320
+
321
+ function gateCount(gate: unknown): { unresolved_blocking?: number } {
322
+ const value = (gate as { unresolved_blocking?: unknown } | null | undefined)
323
+ ?.unresolved_blocking;
324
+ return typeof value === "number" && Number.isInteger(value) && value >= 0
325
+ ? { unresolved_blocking: value }
326
+ : {};
327
+ }
328
+
329
+ function count(value: unknown): number {
330
+ return typeof value === "number" && Number.isFinite(value) && value > 0
331
+ ? Math.round(value)
332
+ : 0;
333
+ }
334
+
335
+ function cost(value: unknown): number {
336
+ return typeof value === "number" && Number.isFinite(value) && value > 0
337
+ ? value
338
+ : 0;
339
+ }
340
+
341
+ // `telemetry.per_agent` is declared with the lab-shared minimum (tokens_total +
342
+ // duration_ms) but this engine writes the richer PerAgentUsage, so each field
343
+ // is read through a runtime check, as review/report.ts does. `detail` is never
344
+ // copied: it is the provider's own words and can echo prompt text.
345
+ function buildAgents(
346
+ doc: FindingsDocument,
347
+ pipeline: unknown,
348
+ ): RunReportAgent[] {
349
+ const steps = pipelineSteps(pipeline);
350
+ const perAgent = doc.telemetry.per_agent ?? {};
351
+ // validateFindingsDocument only checks that `telemetry` is an object, so a
352
+ // row that is not one is skipped instead of read.
353
+ const rows = Object.entries(perAgent as Record<string, unknown>).filter(
354
+ (entry): entry is [string, Record<string, unknown>] =>
355
+ typeof entry[1] === "object" &&
356
+ entry[1] !== null &&
357
+ !Array.isArray(entry[1]),
358
+ );
359
+ // Owners are decided over every row, the capped ones too, so a step of a row
360
+ // past the cap is dropped with it instead of landing on another row.
361
+ const owned = stepsByOwner(
362
+ steps,
363
+ rows.map(([key]) => key),
364
+ );
365
+ return rows.slice(0, MAX_AGENTS).map(([key, row]) => {
366
+ const model = modelOf(steps, key);
367
+ return {
368
+ agent_key: key,
369
+ ...(model === undefined ? {} : { model }),
370
+ tokens_in: count(row.tokens_in),
371
+ tokens_out: count(row.tokens_out),
372
+ cost_usd: cost(row.cost_usd_est),
373
+ duration_ms: count(row.duration_ms),
374
+ ...(typeof row.attempts === "number" && row.attempts > 0
375
+ ? { attempts: count(row.attempts) }
376
+ : {}),
377
+ ...(typeof row.status === "string" ? { status: row.status } : {}),
378
+ ...agentUsageSplit(owned.get(key) ?? []),
379
+ };
380
+ });
381
+ }
382
+
383
+ // pipeline.json's `steps[]` rows are `{ name, model, usage_v2, model_attempts, ... }`.
384
+ // Only what this file reads is kept, and `usage_v2` stays unread until the
385
+ // split below checks each of its fields.
386
+ interface PipelineStep {
387
+ name: string;
388
+ model?: string;
389
+ usage?: unknown;
390
+ modelUsages?: unknown[];
391
+ }
392
+
393
+ function pipelineSteps(pipeline: unknown): PipelineStep[] {
394
+ if (typeof pipeline !== "object" || pipeline === null) return [];
395
+ const steps = (pipeline as { steps?: unknown }).steps;
396
+ if (!Array.isArray(steps)) return [];
397
+ const rows: PipelineStep[] = [];
398
+ for (const step of steps) {
399
+ if (typeof step !== "object" || step === null) continue;
400
+ const { name, model, usage_v2, model_attempts } = step as Record<
401
+ string,
402
+ unknown
403
+ >;
404
+ if (typeof name !== "string") continue;
405
+ rows.push({
406
+ name,
407
+ ...(typeof model === "string" && model !== "" ? { model } : {}),
408
+ usage: usage_v2,
409
+ ...(Array.isArray(model_attempts)
410
+ ? {
411
+ modelUsages: model_attempts.map(
412
+ (entry) => (entry as { usage_v2?: unknown } | null)?.usage_v2,
413
+ ),
414
+ }
415
+ : {}),
416
+ });
417
+ }
418
+ return rows;
419
+ }
420
+
421
+ // A hunter's step is `hunter-<key>`; a refuter, verifier or adjudicator fans
422
+ // out one step per finding (`refuter-F001`) under one `per_agent` row. Three
423
+ // rows are keyed by something other than their steps' name: the summary row's
424
+ // step is `summarizer`, the verifier's are `verify-<id>` and the adjudicator's
425
+ // are `adjudicate-<id>` (pipeline.ts, rereview/verify.ts). Without the alias
426
+ // those rows would go out with no model.
427
+ const STEP_NAME_ALIASES: Readonly<Record<string, string>> = {
428
+ summary: "summarizer",
429
+ verifier: "verify",
430
+ adjudicator: "adjudicate",
431
+ };
432
+
433
+ // The one rule that says a step belongs to a `per_agent` row: the model a row
434
+ // reports and the usage it sums both follow it, so they cannot disagree about
435
+ // which steps are the row's. `exact` is a step named for the row itself; the
436
+ // rest is a fan-out step named after it.
437
+ function stepMatch(
438
+ stepName: string,
439
+ key: string,
440
+ ): "exact" | "fan-out" | undefined {
441
+ const stem = STEP_NAME_ALIASES[key] ?? key;
442
+ if (stepName === key || stepName === stem || stepName === `hunter-${key}`) {
443
+ return "exact";
444
+ }
445
+ return stepName.startsWith(`${stem}-`) ? "fan-out" : undefined;
446
+ }
447
+
448
+ function modelOf(
449
+ steps: ReadonlyArray<PipelineStep>,
450
+ key: string,
451
+ ): string | undefined {
452
+ return steps.find(
453
+ (step) => step.model !== undefined && stepMatch(step.name, key),
454
+ )?.model;
455
+ }
456
+
457
+ // Each step goes to one row at most, so no step is counted twice: a row named
458
+ // for the step wins over a row it fans out from, and otherwise the first row
459
+ // in `per_agent` order. A step no row claims (the warmups) is left out here and
460
+ // still counts in the run's own `usage_v2`.
461
+ function stepsByOwner(
462
+ steps: ReadonlyArray<PipelineStep>,
463
+ keys: ReadonlyArray<string>,
464
+ ): Map<string, PipelineStep[]> {
465
+ const owned = new Map<string, PipelineStep[]>();
466
+ for (const step of steps) {
467
+ const owner =
468
+ keys.find((key) => stepMatch(step.name, key) === "exact") ??
469
+ keys.find((key) => stepMatch(step.name, key) === "fan-out");
470
+ if (owner === undefined) continue;
471
+ owned.set(owner, [...(owned.get(owner) ?? []), step]);
472
+ }
473
+ return owned;
474
+ }
475
+
476
+ // The engine's normalized usage (execution/usage-normalized.ts), read field by
477
+ // field because this file only trusts what it checks: the run's own `usage_v2`
478
+ // and each step's, which already sums all of that step's attempts and models.
479
+ // Numbers and one enum only, so nothing the customer wrote can ride along. A
480
+ // field that is missing or malformed is left out on its own; the rest of the
481
+ // report never depends on it.
482
+ type UsageRecord = Record<string, unknown>;
483
+
484
+ function usageRecord(value: unknown): UsageRecord | undefined {
485
+ return typeof value === "object" && value !== null && !Array.isArray(value)
486
+ ? (value as UsageRecord)
487
+ : undefined;
488
+ }
489
+
490
+ function amount(value: unknown): number | undefined {
491
+ return typeof value === "number" && Number.isFinite(value) && value >= 0
492
+ ? value
493
+ : undefined;
494
+ }
495
+
496
+ // Absent when no source is: the same rule as `sumNormalizedUsage`, whose
497
+ // totals the run's `usage_v2` already carries.
498
+ function sumDefined(
499
+ values: ReadonlyArray<number | undefined>,
500
+ ): number | undefined {
501
+ return values.every((value) => value === undefined)
502
+ ? undefined
503
+ : values.reduce<number>((total, value) => total + (value ?? 0), 0);
504
+ }
505
+
506
+ function leaves(tokens: UsageRecord, names: readonly string[]) {
507
+ return sumDefined(names.map((name) => amount(tokens[name])));
508
+ }
509
+
510
+ // The leaves are disjoint by the §8 contract, so adding them never counts a
511
+ // token twice. Uncached input is `inputUncached`, plus `inputOther` where an
512
+ // inclusive provider total was split into leaves. Output is everything billed
513
+ // as output, reasoning included, as `outputTokensKnown` counts it.
514
+ function tokenSplit(usage: UsageRecord | undefined): ReportTokens {
515
+ const tokens = usageRecord(usage?.tokens);
516
+ if (tokens === undefined) return {};
517
+ const split: ReportTokens = {
518
+ input_uncached: leaves(tokens, ["inputUncached", "inputOther"]),
519
+ input_cache_read: leaves(tokens, ["inputCacheRead"]),
520
+ input_cache_write: leaves(tokens, ["inputCacheWrite"]),
521
+ output: leaves(tokens, ["outputVisible", "outputReasoning", "outputOther"]),
522
+ };
523
+ return Object.fromEntries(
524
+ Object.entries(split)
525
+ .filter((entry): entry is [string, number] => entry[1] !== undefined)
526
+ .map(([key, value]) => [key, Math.round(value)]),
527
+ );
528
+ }
529
+
530
+ // The list price of what one usage record consumed. The Claude CLI files the
531
+ // CLI's list total in exactly one field (transports/claude-code-cli.ts,
532
+ // `costFor`): `notionalCostUsd` on a subscription, which charges nothing, and
533
+ // `cashCostUsd` on a per-token key, where list price is the charge. Any other
534
+ // billing (free, or unknown because attempts disagreed) names no field that
535
+ // holds a list price, so it has none rather than a guessed one.
536
+ function listCost(usage: UsageRecord | undefined): number | undefined {
537
+ switch (usage?.billingMode) {
538
+ case "subscription":
539
+ return amount(usage.notionalCostUsd);
540
+ case "metered":
541
+ return amount(usage.cashCostUsd);
542
+ default:
543
+ return undefined;
544
+ }
545
+ }
546
+
547
+ // A step whose models billed differently reads `unknown` once summed, so its
548
+ // list cost is each model's own (`model_attempts[].usage_v2`, kept for exactly
549
+ // this). Unknown when any model that reported usage cannot be priced.
550
+ function stepListCost(step: PipelineStep): number | undefined {
551
+ const usage = usageRecord(step.usage);
552
+ const own = listCost(usage);
553
+ if (own !== undefined || step.modelUsages === undefined) return own;
554
+ return sumAll(
555
+ step.modelUsages.map(usageRecord).filter((entry) => entry !== undefined),
556
+ listCost,
557
+ );
558
+ }
559
+
560
+ // A sum only when every record that reported usage has a value: one unpriced
561
+ // part would make the total look smaller than what was spent.
562
+ function sumAll<T>(
563
+ items: readonly T[],
564
+ value: (item: T) => number | undefined,
565
+ ): number | undefined {
566
+ if (items.length === 0) return undefined;
567
+ let total = 0;
568
+ for (const item of items) {
569
+ const part = value(item);
570
+ if (part === undefined) return undefined;
571
+ total += part;
572
+ }
573
+ return total;
574
+ }
575
+
576
+ function splitFields(
577
+ tokens: ReportTokens,
578
+ listUsd: number | undefined,
579
+ ): ReportUsageSplit {
580
+ return {
581
+ ...(Object.keys(tokens).length === 0 ? {} : { tokens }),
582
+ ...(listUsd === undefined ? {} : { list_cost_usd: listUsd }),
583
+ };
584
+ }
585
+
586
+ // The run's tokens and billing come from its own `usage_v2`. Its list cost is the
587
+ // sum of every step's, by the rule an agent row uses: the rolled-up notional total
588
+ // counts only the steps that filed one while the tokens count them all, so a step
589
+ // with no list price drops the run's list cost rather than leaving it short. A
590
+ // pipeline with no per-step usage falls back to the run's own figure.
591
+ function runUsageSplit(
592
+ pipeline: unknown,
593
+ ): ReportUsageSplit & { billing?: "subscription" | "metered" } {
594
+ const usage = usageRecord(usageRecord(pipeline)?.usage_v2);
595
+ if (usage === undefined) return {};
596
+ const billing = usage.billingMode;
597
+ // Every step that filed usage, a malformed record included: one that cannot be
598
+ // read cannot be priced, so it drops the sum rather than shrinking it.
599
+ const priced = pipelineSteps(pipeline).filter(
600
+ (step) => step.usage !== undefined,
601
+ );
602
+ return {
603
+ ...splitFields(
604
+ tokenSplit(usage),
605
+ priced.length > 0 ? sumAll(priced, stepListCost) : listCost(usage),
606
+ ),
607
+ ...(billing === "subscription" || billing === "metered" ? { billing } : {}),
608
+ };
609
+ }
610
+
611
+ // A row's split is the sum of the steps it owns, refuter fan-out included.
612
+ function agentUsageSplit(steps: ReadonlyArray<PipelineStep>): ReportUsageSplit {
613
+ const usages = steps.filter((step) => usageRecord(step.usage) !== undefined);
614
+ if (usages.length === 0) return {};
615
+ const splits = usages.map((step) => tokenSplit(usageRecord(step.usage)));
616
+ const tokens = Object.fromEntries(
617
+ (
618
+ [
619
+ "input_uncached",
620
+ "input_cache_read",
621
+ "input_cache_write",
622
+ "output",
623
+ ] as const
624
+ ).flatMap((key) => {
625
+ const total = sumDefined(splits.map((split) => split[key]));
626
+ return total === undefined ? [] : [[key, total]];
627
+ }),
628
+ );
629
+ return splitFields(tokens, sumAll(usages, stepListCost));
630
+ }
631
+
632
+ // Reads findings.json and pipeline.json from a run directory. A missing or
633
+ // unparseable file is simply absent.
634
+ export function readRunDir(
635
+ runDir: string,
636
+ readText: (file: string) => string = (file) => readFileSync(file, "utf8"),
637
+ ): RunDirFacts {
638
+ const facts: RunDirFacts = {};
639
+ if (runDir === "") return facts;
640
+ for (const [key, file] of [
641
+ ["findings", "findings.json"],
642
+ ["pipeline", "pipeline.json"],
643
+ ["gate", GATE_RECORD_FILE],
644
+ ["findingStates", FINDING_STATE_FILE],
645
+ ] as const) {
646
+ try {
647
+ facts[key] = JSON.parse(readText(path.join(runDir, file)));
648
+ } catch {
649
+ // absent or malformed: see above
650
+ }
651
+ }
652
+ return facts;
653
+ }
654
+
655
+ export type SendReportResult =
656
+ | { ok: true; recorded: boolean; runId?: string }
657
+ // `message` names the HTTP status and the API's own `message`. It never
658
+ // carries the token.
659
+ | { ok: false; message: string };
660
+
661
+ export interface SendReportDeps {
662
+ fetch: typeof fetch;
663
+ baseUrl: string;
664
+ runToken: string;
665
+ }
666
+
667
+ type PostResult =
668
+ | { ok: true; reply: Record<string, unknown> }
669
+ | { ok: false; message: string };
670
+
671
+ // One authenticated JSON POST to the platform. Every failure, whether an HTTP
672
+ // status or a transport error, comes back as a message that never carries the
673
+ // token.
674
+ async function postJson(
675
+ deps: SendReportDeps,
676
+ endpoint: string,
677
+ body: unknown,
678
+ ): Promise<PostResult> {
679
+ try {
680
+ const response = await deps.fetch(`${deps.baseUrl}${endpoint}`, {
681
+ method: "POST",
682
+ headers: {
683
+ Authorization: `Bearer ${deps.runToken}`,
684
+ "Content-Type": "application/json",
685
+ },
686
+ body: JSON.stringify(body),
687
+ signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
688
+ });
689
+ if (!response.ok) {
690
+ const message = await apiMessage(response);
691
+ return {
692
+ ok: false,
693
+ message: `HTTP ${response.status}${message === undefined ? "" : ` — ${message}`}`,
694
+ };
695
+ }
696
+ const parsed: unknown = await response.json().catch(() => ({}));
697
+ return {
698
+ ok: true,
699
+ reply: typeof parsed === "object" && parsed !== null ? { ...parsed } : {},
700
+ };
701
+ } catch (error) {
702
+ const detail = error instanceof Error ? error.message : String(error);
703
+ return {
704
+ ok: false,
705
+ message: `the request could not complete: ${detail.split(deps.runToken).join("***")}`,
706
+ };
707
+ }
708
+ }
709
+
710
+ export async function sendRunReport(
711
+ deps: SendReportDeps,
712
+ body: RunReportBody,
713
+ ): Promise<SendReportResult> {
714
+ const result = await postJson(deps, "/runs/report", body);
715
+ if (!result.ok) return result;
716
+ // recorded=false is a repeat of the same run attempt: a success.
717
+ const { reply } = result;
718
+ return {
719
+ ok: true,
720
+ recorded: reply.recorded !== false,
721
+ ...(typeof reply.run_id === "string" ? { runId: reply.run_id } : {}),
722
+ };
723
+ }
724
+
725
+ export interface FindingsUploadBody {
726
+ schema_version: string;
727
+ // Each object exactly as the engine wrote it into findings.json.
728
+ findings: unknown[];
729
+ }
730
+
731
+ export interface FindingsUpload {
732
+ body: FindingsUploadBody;
733
+ // Findings beyond MAX_FINDINGS that were left out of the body.
734
+ dropped: number;
735
+ }
736
+
737
+ // The only fields of a finding a metadata_only repository's upload carries: ids,
738
+ // locations, enum-like labels and counters. NEVER the claim, proof_refs,
739
+ // hop_trail, symbol, dedupe_key or materiality (all can quote the code), and
740
+ // NEVER a key this list does not name, so a field added to the engine
741
+ // tomorrow stays on the runner by default.
742
+ const METADATA_ONLY_FINDING_FIELDS = [
743
+ "id",
744
+ "category",
745
+ "path",
746
+ "line",
747
+ "severity",
748
+ "tier",
749
+ "hunter",
750
+ "evidence_class",
751
+ "refuter_verdict",
752
+ "causal_disposition",
753
+ "root_cause_id",
754
+ "hops_used",
755
+ ] as const;
756
+
757
+ // Also the trace step's reduction of findings.json in a metadata trace
758
+ // (src/ci/trace-bundle.ts), so both uploads keep the same fields.
759
+ export function metadataOnlyFinding(finding: unknown): Record<string, unknown> {
760
+ const kept: Record<string, unknown> = {};
761
+ if (typeof finding !== "object" || finding === null) return kept;
762
+ for (const field of METADATA_ONLY_FINDING_FIELDS) {
763
+ const value: unknown = Reflect.get(finding, field);
764
+ // Scalars only: a structure under an allowlisted name is not the field the
765
+ // platform expects and could hold text.
766
+ if (
767
+ typeof value === "string" ||
768
+ typeof value === "number" ||
769
+ typeof value === "boolean"
770
+ ) {
771
+ kept[field] = value;
772
+ }
773
+ }
774
+ return kept;
775
+ }
776
+
777
+ // null when there is nothing to upload: no findings.json, one this engine
778
+ // cannot read, or zero findings. The document is validated only to know it is
779
+ // one; in `full` mode the objects sent are the raw parsed ones, so a field the
780
+ // validator does not model still reaches the platform untouched. In
781
+ // `metadata_only` mode each is reduced to METADATA_ONLY_FINDING_FIELDS.
782
+ export function buildFindingsUpload(
783
+ document: unknown,
784
+ dataMode: PlatformDataMode,
785
+ ): FindingsUpload | null {
786
+ let doc: FindingsDocument;
787
+ try {
788
+ doc = validateFindingsDocument(document);
789
+ } catch {
790
+ return null;
791
+ }
792
+ if (typeof document !== "object" || document === null) return null;
793
+ const raw = "findings" in document ? document.findings : undefined;
794
+ if (!Array.isArray(raw) || raw.length === 0) return null;
795
+ const sent: unknown[] = raw.slice(0, MAX_FINDINGS);
796
+ return {
797
+ body: {
798
+ schema_version: doc.schema_version,
799
+ findings: dataMode === "full" ? sent : sent.map(metadataOnlyFinding),
800
+ },
801
+ dropped: Math.max(0, raw.length - MAX_FINDINGS),
802
+ };
803
+ }
804
+
805
+ export type SendFindingsResult =
806
+ | {
807
+ ok: true;
808
+ stored: number;
809
+ recorded: boolean;
810
+ mode: "full" | "metadata_only" | undefined;
811
+ }
812
+ // Same message contract as SendReportResult.
813
+ | { ok: false; message: string };
814
+
815
+ export async function sendRunFindings(
816
+ deps: SendReportDeps,
817
+ body: FindingsUploadBody,
818
+ ): Promise<SendFindingsResult> {
819
+ const result = await postJson(deps, "/runs/findings", body);
820
+ if (!result.ok) return result;
821
+ const { reply } = result;
822
+ return {
823
+ ok: true,
824
+ stored: typeof reply.stored === "number" ? reply.stored : 0,
825
+ // recorded=false is a repeat of the same upload: a success.
826
+ recorded: reply.recorded !== false,
827
+ mode:
828
+ reply.mode === "full" || reply.mode === "metadata_only"
829
+ ? reply.mode
830
+ : undefined,
831
+ };
832
+ }
833
+
834
+ async function apiMessage(response: Response): Promise<string | undefined> {
835
+ try {
836
+ const parsed = (await response.json()) as { message?: unknown };
837
+ return typeof parsed.message === "string" && parsed.message !== ""
838
+ ? parsed.message
839
+ : undefined;
840
+ } catch {
841
+ return undefined;
842
+ }
843
+ }
844
+
845
+ function escapeCommandData(text: string): string {
846
+ return text.replace(/%/g, "%25").replace(/\r/g, "%0D").replace(/\n/g, "%0A");
847
+ }
848
+
849
+ export interface RunReportStepDeps {
850
+ fetch: typeof fetch;
851
+ env: Record<string, string | undefined>;
852
+ baseUrl: string;
853
+ readText?: (file: string) => string;
854
+ stdout: (text: string) => void;
855
+ }
856
+
857
+ // The step's shell. ALWAYS 0: see the header.
858
+ export async function runReportStep(deps: RunReportStepDeps): Promise<number> {
859
+ const runToken = deps.env.CRYSK_RUN_TOKEN ?? "";
860
+ const warn = (message: string): void =>
861
+ deps.stdout(
862
+ `::warning title=crysk::${escapeCommandData(`Crysk run report not recorded: ${message}`)}\n`,
863
+ );
864
+ const warnFindings = (message: string): void =>
865
+ deps.stdout(
866
+ `::warning title=crysk::${escapeCommandData(`Crysk findings not uploaded: ${message}`)}\n`,
867
+ );
868
+ // The whole step, not only the request: a run directory this engine wrote is
869
+ // read and shaped here too, and a throw from either must still be a warning.
870
+ try {
871
+ if (runToken === "") {
872
+ warn("no run token reached this step.");
873
+ return 0;
874
+ }
875
+ const prNumber = Number(deps.env.TRIGGER_PR ?? "");
876
+ if (!Number.isInteger(prNumber) || prNumber <= 0) {
877
+ warn("the run resolved no pull request number.");
878
+ return 0;
879
+ }
880
+ const run = readRunDir(deps.env.CRYSK_RUN_DIR ?? "", deps.readText);
881
+ const body = buildRunReport({
882
+ outcome: deps.env.CRYSK_OUTCOME ?? "",
883
+ prNumber,
884
+ triggerHeadSha: deps.env.TRIGGER_HEAD_SHA ?? "",
885
+ run,
886
+ });
887
+ if (body === null) {
888
+ warn("the run resolved no head sha.");
889
+ return 0;
890
+ }
891
+ const platform = { fetch: deps.fetch, baseUrl: deps.baseUrl, runToken };
892
+ const result = await sendRunReport(platform, body);
893
+ if (!result.ok) {
894
+ warn(result.message);
895
+ return 0;
896
+ }
897
+ // Only a recorded run has somewhere for findings to attach (409 otherwise).
898
+ try {
899
+ const upload = buildFindingsUpload(
900
+ run.findings,
901
+ deps.env.CRYSK_DATA_MODE === "full" ? "full" : "metadata_only",
902
+ );
903
+ if (upload === null) return 0;
904
+ if (upload.dropped > 0) {
905
+ warnFindings(
906
+ `only the first ${MAX_FINDINGS} findings are uploaded; ${upload.dropped} more were dropped.`,
907
+ );
908
+ }
909
+ const sent = await sendRunFindings(platform, upload.body);
910
+ if (!sent.ok) warnFindings(sent.message);
911
+ } catch (error) {
912
+ const detail = error instanceof Error ? error.message : String(error);
913
+ warnFindings(
914
+ `the findings could not be sent: ${detail.split(runToken).join("***")}`,
915
+ );
916
+ }
917
+ } catch (error) {
918
+ const detail = error instanceof Error ? error.message : String(error);
919
+ warn(
920
+ `the report could not be built: ${detail.split(runToken).join("***")}`,
921
+ );
922
+ }
923
+ return 0;
924
+ }
925
+
926
+ if (import.meta.main) {
927
+ process.exit(
928
+ await runReportStep({
929
+ fetch,
930
+ env: process.env,
931
+ baseUrl: PLATFORM_API_URL,
932
+ stdout: (text) => process.stdout.write(text),
933
+ }),
934
+ );
935
+ }