crysk 0.4.0 → 0.5.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/README.md +26 -17
- package/config/models/anthropic.json +1 -1
- package/dist/SKILL-k4k0zn1w.md +143 -0
- package/dist/SKILL-vhb4c3vv.md +553 -0
- package/dist/SKILL-xpf6kmdw.md +74 -0
- package/dist/ci-admission-0ken6ehx.md +128 -0
- package/dist/cli.js +3056 -1623
- package/dist/index.js +1744 -938
- package/dist/opencode-ci-ac5zksdn.md +106 -0
- package/dist/workflow-4pkyck01.yml +160 -0
- package/package.json +1 -1
- package/prompts/default/PROVENANCE.md +1 -0
- package/skills/crysk/SKILL.md +16 -16
- package/skills/crysk-ci-setup/SKILL.md +41 -38
- package/skills/crysk-ci-setup/assets/workflow.yml +31 -22
- package/skills/crysk-ci-setup/references/ci-admission.md +1 -1
- package/skills/crysk-ci-setup/references/opencode-ci.md +53 -88
- package/skills/crysk-loop/SKILL.md +1 -1
- package/src/asset-manifest.ts +0 -4
- package/src/assets.ts +1 -1
- package/src/ci/admission-ledger.ts +4 -0
- package/src/ci/classify-error.ts +21 -24
- package/src/ci/cli-record.ts +53 -1
- package/src/ci/crysk-secrets.ts +409 -0
- package/src/ci/force-comment.ts +14 -1
- package/src/ci/gates.ts +86 -57
- package/src/ci/outcome.ts +421 -330
- package/src/ci/platform-config.ts +2 -2
- package/src/ci/platform-estimate.ts +111 -0
- package/src/ci/platform-report.ts +274 -41
- package/src/ci/platform-trace.ts +581 -0
- package/src/ci/platform.ts +58 -12
- package/src/ci/publish.ts +19 -6
- package/src/ci/reporter.ts +14 -4
- package/src/ci/review-levels.ts +8 -7
- package/src/ci/run.ts +88 -0
- package/src/ci/setup.ts +485 -118
- package/src/ci/trace-bundle.ts +1486 -0
- package/src/ci/trace-transcripts.ts +480 -0
- package/src/ci/trigger.ts +176 -33
- package/src/cli.ts +11 -3
- package/src/commands/ci-setup-questions.ts +3 -3
- package/src/commands/ci-setup.ts +152 -6
- package/src/commands/config.ts +3 -0
- package/src/commands/doctor.ts +15 -6
- package/src/config/config.ts +16 -3
- package/src/diversity/clustering.ts +0 -1
- package/src/diversity/identity.ts +4 -5
- package/src/diversity/pipeline-integration.ts +1 -9
- package/src/doctor.ts +3 -3
- package/src/execution/attempt-evidence.ts +10 -5
- package/src/execution/claude-session-trace.ts +480 -0
- package/src/execution/contracts.ts +21 -4
- package/src/execution/harness.ts +105 -8
- package/src/execution/model-fallback.ts +239 -0
- package/src/execution/usage-normalized.ts +6 -4
- package/src/home-preflight.ts +1 -1
- package/src/index.ts +1 -1
- package/src/model/catalog.ts +13 -37
- package/src/model/free-discovery.ts +117 -8
- package/src/model/model-name.ts +404 -0
- package/src/model/provider-capabilities.ts +22 -7
- package/src/model/routing.ts +346 -467
- package/src/pr/admission.ts +18 -0
- package/src/pr/ci-admission-gate.ts +2 -0
- package/src/pr/discovery.ts +18 -0
- package/src/pr/plan.ts +55 -5
- package/src/pr/preflight.ts +1 -1
- package/src/pr/publish-outcome.ts +1 -1
- package/src/pr/review-pr.ts +26 -1
- package/src/pr/target.ts +63 -11
- package/src/production-runtime.ts +760 -331
- package/src/review/pipeline.ts +277 -101
- package/src/review/preflight.ts +201 -294
- package/src/review/report.ts +30 -26
- package/src/review/review.ts +20 -3
- package/src/review/route-preflight.ts +79 -11
- package/src/review/run.ts +44 -6
- package/src/review/scout.ts +1 -1
- package/src/review/spec.ts +3 -3
- package/src/review/step-runner.ts +70 -1
- package/src/runner-authority.ts +127 -9
- package/src/security/binding-policy.ts +1 -2
- package/src/security/claude-credential.ts +355 -0
- package/src/security/credential-broker.ts +56 -40
- package/src/security/evidence-redaction.ts +179 -4
- package/src/security/opencode-secret-manifest.ts +114 -0
- package/src/security/redact.ts +45 -0
- package/src/system-tools.ts +48 -21
- package/src/transport-registry.ts +12 -2
- package/src/transports/claude-code-cli.ts +21 -2
- package/src/transports/opencode-sdk.ts +11 -9
- package/src/transports/opencode-server.ts +7 -10
- package/src/ui/config-edit.ts +9 -20
- package/src/ui/config.ts +32 -27
- package/src/ui/plan.ts +84 -50
- package/src/updater.ts +10 -7
- package/prompts/default/deep-review-lifecycle.md +0 -362
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
// mergeConfig + CONFIG_DIRECTION) resolves it against the repository's
|
|
4
4
|
// `.crysk/config.json` with each key's own rule.
|
|
5
5
|
//
|
|
6
|
-
// `person` is the runner's personal layer: setup.ts writes it
|
|
7
|
-
// config file
|
|
6
|
+
// `person` is the runner's personal layer: setup.ts writes it as the personal
|
|
7
|
+
// config file (see materializeCiOpenCodeData).
|
|
8
8
|
// `repo` is used ONLY when the checkout has no repo config of its own, and then
|
|
9
9
|
// it reaches the engine through the existing `--config` override, which
|
|
10
10
|
// repoints the repo layer and nothing else. The file lives next to the platform
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
// The review cost the Crysk platform estimated for this run (cryskai/platform#12).
|
|
2
|
+
// The connect step asks for it with `X-Crysk-Capabilities: estimate` and hands it
|
|
3
|
+
// to the run step as `CRYSK_PLATFORM_ESTIMATE`, where the CI budget gate reads it
|
|
4
|
+
// instead of the engine's own band (cryskai/crysk#138): the platform knows the
|
|
5
|
+
// setup's models, their catalog prices and what its recent reviews cost; the
|
|
6
|
+
// band only knows the diff's size and prices every model as Claude.
|
|
7
|
+
//
|
|
8
|
+
// The platform already checked it, and the engine checks again: a value that is
|
|
9
|
+
// not a usable range is dropped, and the band decides, as without a platform.
|
|
10
|
+
|
|
11
|
+
export type PlatformEstimateBasis = "history" | "catalog";
|
|
12
|
+
|
|
13
|
+
export interface PlatformEstimate {
|
|
14
|
+
readonly usdLow: number;
|
|
15
|
+
readonly usdHigh: number;
|
|
16
|
+
// `history`: the setup's recent reviews scaled to this pull request.
|
|
17
|
+
// `catalog`: the catalog prices of the setup's models.
|
|
18
|
+
readonly basis: PlatformEstimateBasis;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export const PLATFORM_ESTIMATE_CAPABILITY = "estimate";
|
|
22
|
+
|
|
23
|
+
const usable = (value: unknown): value is number =>
|
|
24
|
+
typeof value === "number" && Number.isFinite(value) && value >= 0;
|
|
25
|
+
|
|
26
|
+
// The `/runs/token` field, `{ usd_low, usd_high, basis }`, or undefined when it
|
|
27
|
+
// is absent or not a usable range.
|
|
28
|
+
export function parsePlatformEstimate(
|
|
29
|
+
value: unknown,
|
|
30
|
+
): PlatformEstimate | undefined {
|
|
31
|
+
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
const {
|
|
35
|
+
usd_low: low,
|
|
36
|
+
usd_high: high,
|
|
37
|
+
basis,
|
|
38
|
+
} = value as Record<string, unknown>;
|
|
39
|
+
if (!usable(low) || !usable(high) || low > high) return undefined;
|
|
40
|
+
if (basis !== "history" && basis !== "catalog") return undefined;
|
|
41
|
+
return { usdLow: low, usdHigh: high, basis };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// What the connect step writes to its `estimate` output: one line of JSON in the
|
|
45
|
+
// response's own field names, numbers and an enum only.
|
|
46
|
+
export function serializePlatformEstimate(estimate: PlatformEstimate): string {
|
|
47
|
+
return JSON.stringify({
|
|
48
|
+
usd_low: estimate.usdLow,
|
|
49
|
+
usd_high: estimate.usdHigh,
|
|
50
|
+
basis: estimate.basis,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// The run step's read of `CRYSK_PLATFORM_ESTIMATE`. Empty (a standalone run, an
|
|
55
|
+
// older platform, nothing priceable) or malformed reads as no estimate.
|
|
56
|
+
export function readPlatformEstimate(
|
|
57
|
+
env: Readonly<Record<string, string | undefined>>,
|
|
58
|
+
): PlatformEstimate | undefined {
|
|
59
|
+
const text = env.CRYSK_PLATFORM_ESTIMATE;
|
|
60
|
+
if (text === undefined || text === "") return undefined;
|
|
61
|
+
try {
|
|
62
|
+
return parsePlatformEstimate(JSON.parse(text));
|
|
63
|
+
} catch {
|
|
64
|
+
return undefined;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// The estimate as the plan and the CI budget gate read a cost band (report.ts's
|
|
69
|
+
// CostEstimate, structurally: this module imports nothing).
|
|
70
|
+
export function platformCostEstimate(estimate: PlatformEstimate): {
|
|
71
|
+
low: number;
|
|
72
|
+
high: number;
|
|
73
|
+
basis: string;
|
|
74
|
+
platform: PlatformEstimateBasis;
|
|
75
|
+
} {
|
|
76
|
+
return {
|
|
77
|
+
low: estimate.usdLow,
|
|
78
|
+
high: estimate.usdHigh,
|
|
79
|
+
basis:
|
|
80
|
+
estimate.basis === "history"
|
|
81
|
+
? "The Crysk platform's estimate, from this setup's recent reviews scaled to this pull request."
|
|
82
|
+
: "The Crysk platform's estimate, from the catalog prices of this setup's models.",
|
|
83
|
+
platform: estimate.basis,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// The estimate the run gates and plans on. The platform prices the whole pull
|
|
88
|
+
// request from the setup's own models, before the engine knows how much of it
|
|
89
|
+
// this run reviews. A narrowed run (a re-review whose discovery reads only what
|
|
90
|
+
// changed since the last review, or one that only verifies earlier findings)
|
|
91
|
+
// passes `fullBand`, the band for a full review of the pull request: the
|
|
92
|
+
// platform's figure is scaled by the share of that work the run does
|
|
93
|
+
// (band / fullBand, at most 1). The band measures the share only; its prices,
|
|
94
|
+
// which treat every model as Claude, never replace the platform's. A full review
|
|
95
|
+
// uses the platform's figure; without one the band decides, as before.
|
|
96
|
+
export function runCostEstimate<Band extends { high: number }>(
|
|
97
|
+
platform: PlatformEstimate | undefined,
|
|
98
|
+
band: Band,
|
|
99
|
+
fullBand: { high: number } | undefined,
|
|
100
|
+
): Band | ReturnType<typeof platformCostEstimate> {
|
|
101
|
+
if (platform === undefined) return band;
|
|
102
|
+
const estimate = platformCostEstimate(platform);
|
|
103
|
+
if (fullBand === undefined || !(fullBand.high > 0)) return estimate;
|
|
104
|
+
const share = Math.min(1, Math.max(0, band.high / fullBand.high));
|
|
105
|
+
return {
|
|
106
|
+
...estimate,
|
|
107
|
+
low: estimate.low * share,
|
|
108
|
+
high: estimate.high * share,
|
|
109
|
+
basis: `${estimate.basis} Scaled to the ${Math.round(share * 100)}% of a full review this run does.`,
|
|
110
|
+
};
|
|
111
|
+
}
|
|
@@ -71,7 +71,24 @@ const REQUEST_TIMEOUT_MS = 30_000;
|
|
|
71
71
|
|
|
72
72
|
const SHA = /^[0-9a-f]{40}$/;
|
|
73
73
|
|
|
74
|
-
|
|
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 {
|
|
75
92
|
agent_key: string;
|
|
76
93
|
model?: string;
|
|
77
94
|
tokens_in: number;
|
|
@@ -100,7 +117,7 @@ export interface RunReportBody {
|
|
|
100
117
|
unresolved_blocking?: number;
|
|
101
118
|
refuted_by_hunter?: Record<string, number>;
|
|
102
119
|
};
|
|
103
|
-
usage: {
|
|
120
|
+
usage: ReportUsageSplit & {
|
|
104
121
|
tokens_in: number;
|
|
105
122
|
tokens_out: number;
|
|
106
123
|
cost_usd: number;
|
|
@@ -108,6 +125,7 @@ export interface RunReportBody {
|
|
|
108
125
|
wall_ms?: number;
|
|
109
126
|
index_ms?: number;
|
|
110
127
|
sync_ms?: number;
|
|
128
|
+
billing?: "subscription" | "metered";
|
|
111
129
|
};
|
|
112
130
|
agents: RunReportAgent[];
|
|
113
131
|
// Which findings stay open, under ids that last across runs. Absent when the run recorded none.
|
|
@@ -206,6 +224,7 @@ export function buildRunReport(
|
|
|
206
224
|
...(telemetry.sync_ms === undefined
|
|
207
225
|
? {}
|
|
208
226
|
: { sync_ms: count(telemetry.sync_ms) }),
|
|
227
|
+
...runUsageSplit(input.run.pipeline),
|
|
209
228
|
},
|
|
210
229
|
agents: buildAgents(doc, input.run.pipeline),
|
|
211
230
|
...findingStates(input.run.findingStates),
|
|
@@ -327,47 +346,74 @@ function buildAgents(
|
|
|
327
346
|
doc: FindingsDocument,
|
|
328
347
|
pipeline: unknown,
|
|
329
348
|
): RunReportAgent[] {
|
|
330
|
-
const
|
|
349
|
+
const steps = pipelineSteps(pipeline);
|
|
331
350
|
const perAgent = doc.telemetry.per_agent ?? {};
|
|
332
351
|
// validateFindingsDocument only checks that `telemetry` is an object, so a
|
|
333
352
|
// row that is not one is skipped instead of read.
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
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
|
+
});
|
|
357
381
|
}
|
|
358
382
|
|
|
359
|
-
// pipeline.json's `steps[]` rows are `{ name, model, ... }`.
|
|
360
|
-
|
|
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[] {
|
|
361
394
|
if (typeof pipeline !== "object" || pipeline === null) return [];
|
|
362
395
|
const steps = (pipeline as { steps?: unknown }).steps;
|
|
363
396
|
if (!Array.isArray(steps)) return [];
|
|
364
|
-
const rows:
|
|
397
|
+
const rows: PipelineStep[] = [];
|
|
365
398
|
for (const step of steps) {
|
|
366
399
|
if (typeof step !== "object" || step === null) continue;
|
|
367
|
-
const { name, model } = step as
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
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
|
+
});
|
|
371
417
|
}
|
|
372
418
|
return rows;
|
|
373
419
|
}
|
|
@@ -384,20 +430,205 @@ const STEP_NAME_ALIASES: Readonly<Record<string, string>> = {
|
|
|
384
430
|
adjudicator: "adjudicate",
|
|
385
431
|
};
|
|
386
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
|
+
|
|
387
448
|
function modelOf(
|
|
388
|
-
steps: ReadonlyArray<
|
|
449
|
+
steps: ReadonlyArray<PipelineStep>,
|
|
389
450
|
key: string,
|
|
390
451
|
): string | undefined {
|
|
391
|
-
const stem = STEP_NAME_ALIASES[key] ?? key;
|
|
392
452
|
return steps.find(
|
|
393
|
-
(step) =>
|
|
394
|
-
step.name === key ||
|
|
395
|
-
step.name === stem ||
|
|
396
|
-
step.name === `hunter-${key}` ||
|
|
397
|
-
step.name.startsWith(`${stem}-`),
|
|
453
|
+
(step) => step.model !== undefined && stepMatch(step.name, key),
|
|
398
454
|
)?.model;
|
|
399
455
|
}
|
|
400
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
|
+
|
|
401
632
|
// Reads findings.json and pipeline.json from a run directory. A missing or
|
|
402
633
|
// unparseable file is simply absent.
|
|
403
634
|
export function readRunDir(
|
|
@@ -523,7 +754,9 @@ const METADATA_ONLY_FINDING_FIELDS = [
|
|
|
523
754
|
"hops_used",
|
|
524
755
|
] as const;
|
|
525
756
|
|
|
526
|
-
|
|
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> {
|
|
527
760
|
const kept: Record<string, unknown> = {};
|
|
528
761
|
if (typeof finding !== "object" || finding === null) return kept;
|
|
529
762
|
for (const field of METADATA_ONLY_FINDING_FIELDS) {
|