crysk 0.4.0 → 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 (93) hide show
  1. package/README.md +26 -17
  2. package/config/models/anthropic.json +1 -1
  3. package/dist/SKILL-k4k0zn1w.md +143 -0
  4. package/dist/SKILL-vhb4c3vv.md +553 -0
  5. package/dist/SKILL-xpf6kmdw.md +74 -0
  6. package/dist/ci-admission-0ken6ehx.md +128 -0
  7. package/dist/cli.js +3056 -1623
  8. package/dist/index.js +1744 -938
  9. package/dist/opencode-ci-ac5zksdn.md +106 -0
  10. package/dist/workflow-4pkyck01.yml +160 -0
  11. package/package.json +1 -1
  12. package/prompts/default/PROVENANCE.md +1 -0
  13. package/skills/crysk/SKILL.md +16 -16
  14. package/skills/crysk-ci-setup/SKILL.md +41 -38
  15. package/skills/crysk-ci-setup/assets/workflow.yml +31 -22
  16. package/skills/crysk-ci-setup/references/ci-admission.md +1 -1
  17. package/skills/crysk-ci-setup/references/opencode-ci.md +53 -88
  18. package/skills/crysk-loop/SKILL.md +1 -1
  19. package/src/asset-manifest.ts +0 -4
  20. package/src/assets.ts +1 -1
  21. package/src/ci/admission-ledger.ts +4 -0
  22. package/src/ci/classify-error.ts +21 -24
  23. package/src/ci/cli-record.ts +53 -1
  24. package/src/ci/crysk-secrets.ts +409 -0
  25. package/src/ci/gates.ts +22 -36
  26. package/src/ci/outcome.ts +421 -330
  27. package/src/ci/platform-config.ts +2 -2
  28. package/src/ci/platform-report.ts +274 -41
  29. package/src/ci/platform-trace.ts +551 -0
  30. package/src/ci/platform.ts +28 -12
  31. package/src/ci/publish.ts +19 -6
  32. package/src/ci/review-levels.ts +8 -7
  33. package/src/ci/run.ts +88 -0
  34. package/src/ci/setup.ts +453 -118
  35. package/src/ci/trace-bundle.ts +1282 -0
  36. package/src/ci/trace-transcripts.ts +415 -0
  37. package/src/ci/trigger.ts +176 -33
  38. package/src/cli.ts +11 -3
  39. package/src/commands/ci-setup-questions.ts +3 -3
  40. package/src/commands/ci-setup.ts +152 -6
  41. package/src/commands/config.ts +3 -0
  42. package/src/commands/doctor.ts +15 -6
  43. package/src/config/config.ts +16 -3
  44. package/src/diversity/clustering.ts +0 -1
  45. package/src/diversity/identity.ts +4 -5
  46. package/src/diversity/pipeline-integration.ts +1 -9
  47. package/src/doctor.ts +3 -3
  48. package/src/execution/attempt-evidence.ts +10 -5
  49. package/src/execution/claude-session-trace.ts +480 -0
  50. package/src/execution/contracts.ts +21 -4
  51. package/src/execution/harness.ts +105 -8
  52. package/src/execution/model-fallback.ts +239 -0
  53. package/src/execution/usage-normalized.ts +6 -4
  54. package/src/home-preflight.ts +1 -1
  55. package/src/index.ts +1 -1
  56. package/src/model/catalog.ts +13 -37
  57. package/src/model/free-discovery.ts +117 -8
  58. package/src/model/model-name.ts +404 -0
  59. package/src/model/provider-capabilities.ts +22 -7
  60. package/src/model/routing.ts +346 -467
  61. package/src/pr/admission.ts +18 -0
  62. package/src/pr/plan.ts +20 -4
  63. package/src/pr/preflight.ts +1 -1
  64. package/src/pr/publish-outcome.ts +1 -1
  65. package/src/pr/review-pr.ts +15 -1
  66. package/src/pr/target.ts +12 -3
  67. package/src/production-runtime.ts +760 -331
  68. package/src/review/pipeline.ts +277 -101
  69. package/src/review/preflight.ts +201 -294
  70. package/src/review/report.ts +26 -26
  71. package/src/review/review.ts +20 -3
  72. package/src/review/route-preflight.ts +79 -11
  73. package/src/review/run.ts +28 -6
  74. package/src/review/scout.ts +1 -1
  75. package/src/review/spec.ts +3 -3
  76. package/src/review/step-runner.ts +70 -1
  77. package/src/runner-authority.ts +127 -9
  78. package/src/security/binding-policy.ts +1 -2
  79. package/src/security/claude-credential.ts +355 -0
  80. package/src/security/credential-broker.ts +56 -40
  81. package/src/security/evidence-redaction.ts +179 -4
  82. package/src/security/opencode-secret-manifest.ts +114 -0
  83. package/src/security/redact.ts +45 -0
  84. package/src/system-tools.ts +48 -21
  85. package/src/transport-registry.ts +12 -2
  86. package/src/transports/claude-code-cli.ts +21 -2
  87. package/src/transports/opencode-sdk.ts +11 -9
  88. package/src/transports/opencode-server.ts +7 -10
  89. package/src/ui/config-edit.ts +9 -20
  90. package/src/ui/config.ts +32 -27
  91. package/src/ui/plan.ts +76 -47
  92. package/src/updater.ts +10 -7
  93. package/prompts/default/deep-review-lifecycle.md +0 -362
@@ -0,0 +1,551 @@
1
+ // The action's trace step: send the platform the review's full trace, so every
2
+ // run on a platform-managed repository can be studied afterwards, the failed
3
+ // ones above all (odd/tasks/review-traces.md in the platform repository).
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
+ // under always(), after the report step and the cancelled-run refresh. A
8
+ // standalone run never reaches it, so it sends nothing anywhere.
9
+ //
10
+ // WHAT IT SENDS: the crysk.trace.v1 bundle src/ci/trace-bundle.ts builds from
11
+ // the run directory, gzip-compressed, to POST /runs/trace with the run token.
12
+ // Content (prompts, outputs, tool arguments and results, transcripts) goes only
13
+ // when the same exchange as the token said `trace_content` (CRYSK_TRACE_CONTENT
14
+ // is exactly `true`); otherwise metadata only.
15
+ //
16
+ // WHICH RUN DIRECTORY: the run step's `run_dir` output, else the run-dir record
17
+ // the CLI wrote into the outcome directory (src/ci/cli-record.ts recordRunDir).
18
+ // That output is empty when every hunter failed, on a fatal error and on a
19
+ // cancelled job, the runs a trace is most for. With neither, the trace is the
20
+ // run's root alone, built from cli.json.
21
+ //
22
+ // WHICH CODE: the one the publish step renders (decidePublication), from the
23
+ // same records, so the trace and the pull request name the same CRYSK-* code.
24
+ //
25
+ // WHY it never fails the job: tracing is best effort (S10). A 503 is retried
26
+ // once (the platform answers it when its storage failed and released the
27
+ // claim); any other failure is a `::warning::` and the step exits 0.
28
+ //
29
+ // BUILTINS ONLY: under always() this step can run when `bun install` never did.
30
+ // test/ci/platform-trace.test.ts walks the import closure.
31
+
32
+ import { existsSync, readdirSync, readFileSync, statSync } from "node:fs";
33
+ import path from "node:path";
34
+ import {
35
+ CLI_RECORD_FILE,
36
+ type CliRecord,
37
+ parseCliRecord,
38
+ parseSetupRecord,
39
+ parseTriggerRecord,
40
+ RUN_DIR_FILE,
41
+ } from "./outcome";
42
+ import { PLATFORM_API_URL } from "./platform";
43
+ import { decidePublication } from "./publish";
44
+ import {
45
+ encodeTrace,
46
+ type TraceAttemptInput,
47
+ type TraceOutcome,
48
+ type TraceRunInput,
49
+ type TraceStepInput,
50
+ } from "./trace-bundle";
51
+ import {
52
+ type Conversation,
53
+ openCodeConversation,
54
+ scanClaudeSessionFile,
55
+ withoutContent,
56
+ } from "./trace-transcripts";
57
+
58
+ // A 25 MB body over a slow link, but never the job's tail held for long.
59
+ const REQUEST_TIMEOUT_MS = 120_000;
60
+ const RETRY_DELAY_MS = 2_000;
61
+ // The CLI's session copy stops at 64 MiB (claude-session-trace.ts); a prompt or
62
+ // an output file past this is not read.
63
+ const MAX_TRANSCRIPT_BYTES = 65 * 1024 * 1024;
64
+ const MAX_FILE_BYTES = 8 * 1024 * 1024;
65
+ const SHA = /^[0-9a-f]{40}$/;
66
+ const OUTCOMES = new Set<string>([
67
+ "reviewed",
68
+ "hunters-failed",
69
+ "skipped",
70
+ "yielded",
71
+ "error",
72
+ ]);
73
+
74
+ function readText(file: string, max: number): string | undefined {
75
+ try {
76
+ if (statSync(file).size > max) return undefined;
77
+ return readFileSync(file, "utf8");
78
+ } catch {
79
+ return undefined;
80
+ }
81
+ }
82
+
83
+ function readJson(file: string): unknown {
84
+ const text = readText(file, MAX_FILE_BYTES);
85
+ if (text === undefined) return undefined;
86
+ try {
87
+ return JSON.parse(text) as unknown;
88
+ } catch {
89
+ return undefined;
90
+ }
91
+ }
92
+
93
+ function isDirectory(dir: string): boolean {
94
+ try {
95
+ return statSync(dir).isDirectory();
96
+ } catch {
97
+ return false;
98
+ }
99
+ }
100
+
101
+ function mtimeMs(file: string): number | undefined {
102
+ try {
103
+ return statSync(file).mtimeMs;
104
+ } catch {
105
+ return undefined;
106
+ }
107
+ }
108
+
109
+ function isRecord(value: unknown): value is Record<string, unknown> {
110
+ return typeof value === "object" && value !== null && !Array.isArray(value);
111
+ }
112
+
113
+ // The run step's output, else the CLI's record of it. The record is read only
114
+ // when it names this pull request's run directory (`pr-<n>-...`): the trigger
115
+ // clears it at job start, and this keeps a leftover from ever standing in.
116
+ export function locateRunDir(
117
+ env: Readonly<Record<string, string | undefined>>,
118
+ ): string | undefined {
119
+ const output = env.CRYSK_RUN_DIR ?? "";
120
+ if (output !== "" && isDirectory(output)) return output;
121
+ const outcomeDir = env.CRYSK_OUTCOME_DIR ?? "";
122
+ if (outcomeDir === "") return undefined;
123
+ const recorded = readText(path.join(outcomeDir, RUN_DIR_FILE), 4096)
124
+ ?.split("\n")[0]
125
+ ?.trim();
126
+ if (recorded === undefined || !path.isAbsolute(recorded)) return undefined;
127
+ const pr = env.TRIGGER_PR ?? "";
128
+ if (pr === "" || !path.basename(recorded).startsWith(`pr-${pr}-`)) {
129
+ return undefined;
130
+ }
131
+ return isDirectory(recorded) ? recorded : undefined;
132
+ }
133
+
134
+ const EVIDENCE = /^evidence\.(.+)\.attempt(\d+)\.json$/;
135
+ const CAPTURE = /^evidence\.(.+)\.attempt(\d+)\.capture\.json$/;
136
+ const TRANSCRIPT = /^trace\.(.+)\.attempt(\d+)\.jsonl$/;
137
+
138
+ // Every step the run directory holds files for: pipeline.json's steps, and any
139
+ // step whose evidence or transcript exists without a pipeline row (a run that
140
+ // stopped before it wrote pipeline.json).
141
+ //
142
+ // Without `content` no content is kept. The prompts and outputs are not read.
143
+ // Each Claude session (up to 65 MiB) is scanned line by line for its tool
144
+ // calls' names and times, its finish reason and its response model, never held
145
+ // whole (scanClaudeSessionFile); each OpenCode capture, which its writer caps
146
+ // at 4 MiB (src/transports/opencode-evidence.ts), is parsed whole, reduced the
147
+ // same way and dropped. So a metadata trace has the content trace's spans.
148
+ export function readRunSteps(
149
+ runDir: string,
150
+ pipeline: unknown,
151
+ content: boolean,
152
+ ): TraceStepInput[] {
153
+ const stepsDir = path.join(runDir, "steps");
154
+ let names: string[] = [];
155
+ try {
156
+ names = readdirSync(stepsDir);
157
+ } catch {
158
+ names = [];
159
+ }
160
+ const steps = new Map<string, Map<number, TraceAttemptInput>>();
161
+ const attemptOf = (step: string, n: number): TraceAttemptInput => {
162
+ const attempts = steps.get(step) ?? new Map<number, TraceAttemptInput>();
163
+ steps.set(step, attempts);
164
+ const found = attempts.get(n) ?? { attempt: n };
165
+ attempts.set(n, found);
166
+ return found;
167
+ };
168
+ for (const name of names.sort()) {
169
+ const file = path.join(stepsDir, name);
170
+ const capture = CAPTURE.exec(name);
171
+ if (capture?.[1] !== undefined) {
172
+ if (!content) {
173
+ // A session, when there is one, says more (parseAttempt reads it
174
+ // first); names sort captures before sessions.
175
+ const found = captureMetadata(file);
176
+ if (found !== undefined) {
177
+ const attempt = attemptOf(capture[1], Number(capture[2]));
178
+ attempt.transcriptMetadata ??= found;
179
+ }
180
+ continue;
181
+ }
182
+ const text = readText(file, MAX_TRANSCRIPT_BYTES);
183
+ if (text !== undefined) {
184
+ attemptOf(capture[1], Number(capture[2])).openCodeCapture = text;
185
+ }
186
+ continue;
187
+ }
188
+ const evidence = EVIDENCE.exec(name);
189
+ if (evidence?.[1] !== undefined) {
190
+ const parsed = readJson(file);
191
+ if (parsed !== undefined) {
192
+ attemptOf(evidence[1], Number(evidence[2])).evidence = parsed;
193
+ }
194
+ continue;
195
+ }
196
+ const transcript = TRANSCRIPT.exec(name);
197
+ if (transcript?.[1] !== undefined && !content) {
198
+ const found = scanClaudeSessionFile(file);
199
+ if (found !== undefined) {
200
+ attemptOf(transcript[1], Number(transcript[2])).transcriptMetadata =
201
+ found;
202
+ }
203
+ } else if (transcript?.[1] !== undefined) {
204
+ const text = readText(file, MAX_TRANSCRIPT_BYTES);
205
+ if (text !== undefined) {
206
+ attemptOf(transcript[1], Number(transcript[2])).sessionJsonl = text;
207
+ }
208
+ }
209
+ }
210
+ const metas =
211
+ isRecord(pipeline) && Array.isArray(pipeline.steps)
212
+ ? pipeline.steps.filter(isRecord)
213
+ : [];
214
+ const allNames = [
215
+ ...new Set([
216
+ ...metas.map((meta) => String(meta.name)),
217
+ ...[...steps.keys()].sort(),
218
+ ]),
219
+ ];
220
+ return allNames.map((name) => {
221
+ const attempts = [...(steps.get(name)?.values() ?? [])];
222
+ if (!content) return { name, attempts };
223
+ const meta = metas.find((item) => item.name === name);
224
+ // pipeline.json names absolute paths of the runner that wrote them; the
225
+ // files sit in steps/ under the same basename.
226
+ const inSteps = (value: unknown, fallback: string) =>
227
+ path.join(
228
+ stepsDir,
229
+ typeof value === "string" && value !== ""
230
+ ? path.basename(value)
231
+ : fallback,
232
+ );
233
+ const systemPrompt = readText(
234
+ inSteps(meta?.systemPromptPath, `${name}.system.md`),
235
+ MAX_FILE_BYTES,
236
+ );
237
+ const promptBody = readText(
238
+ path.join(stepsDir, `${name}.body.md`),
239
+ MAX_FILE_BYTES,
240
+ );
241
+ const output =
242
+ typeof meta?.outPath === "string"
243
+ ? readText(inSteps(meta.outPath, ""), MAX_FILE_BYTES)
244
+ : undefined;
245
+ return {
246
+ name,
247
+ ...(systemPrompt === undefined ? {} : { systemPrompt }),
248
+ ...(promptBody === undefined ? {} : { promptBody }),
249
+ ...(output === undefined ? {} : { output }),
250
+ attempts,
251
+ };
252
+ });
253
+ }
254
+
255
+ // An OpenCode capture's metadata: its conversation without content. The text
256
+ // and the parsed capture are dropped on return.
257
+ function captureMetadata(file: string): Conversation | undefined {
258
+ const parsed = readJson(file);
259
+ return parsed === undefined
260
+ ? undefined
261
+ : withoutContent(openCodeConversation(parsed));
262
+ }
263
+
264
+ interface OutcomeFacts {
265
+ outcome: TraceOutcome;
266
+ code: string | null;
267
+ cli?: CliRecord;
268
+ startedAtMs?: number;
269
+ }
270
+
271
+ // A cancelled job: job.status says so, or the cancelled-run refresh ran (it
272
+ // runs only on a cancel). The second signal is there because the job context's
273
+ // value inside a composite action is not documented.
274
+ function wasCancelled(
275
+ env: Readonly<Record<string, string | undefined>>,
276
+ ): boolean {
277
+ const refresh = env.CRYSK_CANCEL_REFRESH ?? "";
278
+ return (
279
+ env.CRYSK_JOB_STATUS === "cancelled" ||
280
+ (refresh !== "" && refresh !== "skipped")
281
+ );
282
+ }
283
+
284
+ // The outcome from cli.json (a killed CLI that left `reviewing`, or none, is an
285
+ // error), and the code the publisher renders from the same records.
286
+ export function readOutcome(
287
+ env: Readonly<Record<string, string | undefined>>,
288
+ ): OutcomeFacts {
289
+ const dir = env.CRYSK_OUTCOME_DIR ?? "";
290
+ const triggerFile = path.join(dir, "trigger.json");
291
+ const startedFile = path.join(dir, "cli-started");
292
+ const startedAtMs =
293
+ dir === "" ? undefined : (mtimeMs(startedFile) ?? mtimeMs(triggerFile));
294
+ const timing = startedAtMs === undefined ? {} : { startedAtMs };
295
+ // A cancel after the CLI wrote a terminal outcome (the job was cancelled
296
+ // during the steps after the review) keeps that outcome: the review ran.
297
+ if (wasCancelled(env)) {
298
+ const cli =
299
+ dir === ""
300
+ ? undefined
301
+ : parseCliRecord(
302
+ readText(path.join(dir, CLI_RECORD_FILE), MAX_FILE_BYTES) ?? "",
303
+ );
304
+ if (cli === undefined || !OUTCOMES.has(cli.outcome)) {
305
+ return {
306
+ outcome: "error",
307
+ code: "CRYSK-CANCELLED",
308
+ ...(cli === undefined ? {} : { cli }),
309
+ ...timing,
310
+ };
311
+ }
312
+ }
313
+ if (dir === "") return { outcome: "error", code: null };
314
+ const record = parseTriggerRecord(
315
+ readText(triggerFile, MAX_FILE_BYTES) ?? "",
316
+ );
317
+ const cliStarted = existsSync(startedFile);
318
+ const cli = cliStarted
319
+ ? parseCliRecord(
320
+ readText(path.join(dir, CLI_RECORD_FILE), MAX_FILE_BYTES) ?? "",
321
+ )
322
+ : undefined;
323
+ const setupText = readText(path.join(dir, "setup.json"), MAX_FILE_BYTES);
324
+ const setup =
325
+ setupText === undefined ? undefined : parseSetupRecord(setupText);
326
+ const outcome: TraceOutcome =
327
+ cli !== undefined && OUTCOMES.has(cli.outcome)
328
+ ? (cli.outcome as TraceOutcome)
329
+ : "error";
330
+ let code: string | null = null;
331
+ if (outcome === "error" || outcome === "hunters-failed") {
332
+ const plan = decidePublication({
333
+ record,
334
+ cliStarted,
335
+ ...(cli === undefined ? {} : { cliRecord: cli }),
336
+ ...(setup === undefined ? {} : { setup }),
337
+ });
338
+ code =
339
+ plan.kind === "remote" || plan.kind === "local"
340
+ ? plan.rendered.code
341
+ : null;
342
+ }
343
+ return { outcome, code, ...(cli === undefined ? {} : { cli }), ...timing };
344
+ }
345
+
346
+ // The engine that wrote the run's records, else the action's own package.json
347
+ // (CRYSK_ACTION_PATH, the step's github.action_path): a refusal before the review
348
+ // wrote anything still names its engine. Never derived from import.meta, which
349
+ // only src/assets.ts may do.
350
+ function engineVersion(
351
+ env: Readonly<Record<string, string | undefined>>,
352
+ ...docs: unknown[]
353
+ ): string | undefined {
354
+ for (const doc of docs) {
355
+ if (isRecord(doc) && isRecord(doc.engine)) {
356
+ const version = doc.engine.version;
357
+ if (typeof version === "string" && version !== "") return version;
358
+ }
359
+ }
360
+ const actionPath = env.CRYSK_ACTION_PATH ?? "";
361
+ if (actionPath === "") return undefined;
362
+ const pkg = readJson(path.join(actionPath, "package.json"));
363
+ return isRecord(pkg) && typeof pkg.version === "string" && pkg.version !== ""
364
+ ? pkg.version
365
+ : undefined;
366
+ }
367
+
368
+ export function readTraceRun(
369
+ env: Readonly<Record<string, string | undefined>>,
370
+ now: number,
371
+ content: boolean,
372
+ ): TraceRunInput {
373
+ const facts = readOutcome(env);
374
+ const runDir = locateRunDir(env);
375
+ const pipeline =
376
+ runDir === undefined
377
+ ? undefined
378
+ : readJson(path.join(runDir, "pipeline.json"));
379
+ const findings =
380
+ runDir === undefined
381
+ ? undefined
382
+ : readJson(path.join(runDir, "findings.json"));
383
+ const pr = Number(env.TRIGGER_PR ?? "");
384
+ const head = [
385
+ facts.cli?.head_sha,
386
+ isRecord(findings) ? findings.head_sha : undefined,
387
+ env.TRIGGER_HEAD_SHA,
388
+ ].find((sha): sha is string => typeof sha === "string" && SHA.test(sha));
389
+ const version = engineVersion(env, findings, pipeline);
390
+ return {
391
+ outcome: facts.outcome,
392
+ code: facts.code,
393
+ ...(facts.cli === undefined ? {} : { cli: facts.cli }),
394
+ ...(pipeline === undefined ? {} : { pipeline }),
395
+ ...(findings === undefined ? {} : { findings }),
396
+ steps: runDir === undefined ? [] : readRunSteps(runDir, pipeline, content),
397
+ ...(Number.isInteger(pr) && pr > 0 ? { prNumber: pr } : {}),
398
+ ...(head === undefined ? {} : { headSha: head }),
399
+ ...(version === undefined ? {} : { engineVersion: version }),
400
+ startedAtMs: Math.min(facts.startedAtMs ?? now, now),
401
+ endedAtMs: now,
402
+ };
403
+ }
404
+
405
+ export type SendTraceResult =
406
+ | { ok: true; recorded: boolean }
407
+ // `message` names the HTTP status and the API's own `message`. It never
408
+ // carries the token.
409
+ | { ok: false; message: string };
410
+
411
+ export interface SendTraceDeps {
412
+ fetch: typeof fetch;
413
+ baseUrl: string;
414
+ runToken: string;
415
+ sleep: (ms: number) => Promise<void>;
416
+ }
417
+
418
+ async function apiMessage(response: Response): Promise<string | undefined> {
419
+ try {
420
+ const parsed = (await response.json()) as { message?: unknown };
421
+ return typeof parsed.message === "string" && parsed.message !== ""
422
+ ? parsed.message
423
+ : undefined;
424
+ } catch {
425
+ return undefined;
426
+ }
427
+ }
428
+
429
+ export async function sendTrace(
430
+ deps: SendTraceDeps,
431
+ body: Uint8Array,
432
+ ): Promise<SendTraceResult> {
433
+ for (let attempt = 1; ; attempt++) {
434
+ try {
435
+ const response = await deps.fetch(`${deps.baseUrl}/runs/trace`, {
436
+ method: "POST",
437
+ headers: {
438
+ Authorization: `Bearer ${deps.runToken}`,
439
+ "Content-Type": "application/json",
440
+ "Content-Encoding": "gzip",
441
+ },
442
+ // A copy over a plain ArrayBuffer, the body type fetch accepts.
443
+ body: new Uint8Array(body),
444
+ signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
445
+ });
446
+ if (response.status === 503 && attempt === 1) {
447
+ await response.body?.cancel().catch(() => undefined);
448
+ await deps.sleep(RETRY_DELAY_MS);
449
+ continue;
450
+ }
451
+ if (!response.ok) {
452
+ const message = await apiMessage(response);
453
+ return {
454
+ ok: false,
455
+ message: `HTTP ${response.status}${message === undefined ? "" : ` — ${message}`}`,
456
+ };
457
+ }
458
+ const reply: unknown = await response.json().catch(() => ({}));
459
+ return {
460
+ ok: true,
461
+ recorded: !(isRecord(reply) && reply.recorded === false),
462
+ };
463
+ } catch (error) {
464
+ const detail = error instanceof Error ? error.message : String(error);
465
+ return {
466
+ ok: false,
467
+ message: `the request could not complete: ${detail.split(deps.runToken).join("***")}`,
468
+ };
469
+ }
470
+ }
471
+ }
472
+
473
+ function escapeCommandData(text: string): string {
474
+ return text.replace(/%/g, "%25").replace(/\r/g, "%0D").replace(/\n/g, "%0A");
475
+ }
476
+
477
+ export interface TraceStepDeps {
478
+ fetch: typeof fetch;
479
+ env: Readonly<Record<string, string | undefined>>;
480
+ baseUrl: string;
481
+ stdout: (text: string) => void;
482
+ now?: () => number;
483
+ sleep?: (ms: number) => Promise<void>;
484
+ }
485
+
486
+ // The step's shell. ALWAYS 0: see the header.
487
+ export async function runTraceStep(deps: TraceStepDeps): Promise<number> {
488
+ const runToken = deps.env.CRYSK_RUN_TOKEN ?? "";
489
+ const warn = (message: string): void =>
490
+ deps.stdout(
491
+ `::warning title=crysk::${escapeCommandData(`Crysk trace not uploaded: ${message}`)}\n`,
492
+ );
493
+ try {
494
+ if (runToken === "") {
495
+ warn("no run token reached this step.");
496
+ return 0;
497
+ }
498
+ const content = deps.env.CRYSK_TRACE_CONTENT === "true";
499
+ const run = readTraceRun(deps.env, (deps.now ?? Date.now)(), content);
500
+ const encoded = encodeTrace(run, content);
501
+ if (!encoded.ok) {
502
+ warn(
503
+ `the trace is too large even without its content (${encoded.jsonBytes} bytes of JSON).`,
504
+ );
505
+ return 0;
506
+ }
507
+ const result = await sendTrace(
508
+ {
509
+ fetch: deps.fetch,
510
+ baseUrl: deps.baseUrl,
511
+ runToken,
512
+ sleep:
513
+ deps.sleep ??
514
+ ((ms) => {
515
+ const { promise, resolve } = Promise.withResolvers<void>();
516
+ setTimeout(resolve, ms);
517
+ return promise;
518
+ }),
519
+ },
520
+ encoded.body,
521
+ );
522
+ if (!result.ok) {
523
+ warn(result.message);
524
+ return 0;
525
+ }
526
+ const dropped = [
527
+ encoded.droppedTranscripts > 0
528
+ ? `${encoded.droppedTranscripts} transcripts`
529
+ : "",
530
+ encoded.droppedContent ? "the content" : "",
531
+ ].filter((part) => part !== "");
532
+ deps.stdout(
533
+ `Crysk trace ${result.recorded ? "recorded" : "sent (not recorded again)"}: ${Math.ceil(encoded.body.byteLength / 1024)} KB gzip${dropped.length > 0 ? `, without ${dropped.join(" and ")} to fit the size cap` : ""}.\n`,
534
+ );
535
+ } catch (error) {
536
+ const detail = error instanceof Error ? error.message : String(error);
537
+ warn(`the trace could not be built: ${detail.split(runToken).join("***")}`);
538
+ }
539
+ return 0;
540
+ }
541
+
542
+ if (import.meta.main) {
543
+ process.exit(
544
+ await runTraceStep({
545
+ fetch,
546
+ env: process.env,
547
+ baseUrl: PLATFORM_API_URL,
548
+ stdout: (text) => process.stdout.write(text),
549
+ }),
550
+ );
551
+ }
@@ -104,12 +104,15 @@ export type PlatformTokenResult =
104
104
  // `config` is absent when the platform sent none or only empty objects.
105
105
  // `dataMode` is absent when the platform sent none or anything but exactly
106
106
  // `full` or `metadata_only`; the connect step reads absence as metadata_only.
107
+ // `traceContent` is present, and true, only when the platform sent exactly
108
+ // `true`; the connect step reads absence as false.
107
109
  | {
108
110
  ok: true;
109
111
  githubToken: string;
110
112
  runToken: string;
111
113
  githubActor?: string;
112
114
  dataMode?: PlatformDataMode;
115
+ traceContent?: true;
113
116
  config?: PlatformConfig;
114
117
  }
115
118
  // `message` names the HTTP status and the API's own `message` when there was
@@ -247,6 +250,7 @@ export async function exchangePlatformToken(
247
250
  run_token?: unknown;
248
251
  github_actor?: unknown;
249
252
  data_mode?: unknown;
253
+ trace_content?: unknown;
250
254
  config?: unknown;
251
255
  };
252
256
  const {
@@ -291,6 +295,10 @@ export async function exchangePlatformToken(
291
295
  ...(dataMode === "full" || dataMode === "metadata_only"
292
296
  ? { dataMode }
293
297
  : {}),
298
+ // WHY fail closed: whether a review trace may carry the customer's code
299
+ // is the platform's decision (`trace_content`, today its data mode), so
300
+ // an absent or malformed value sends metadata only.
301
+ ...(body.trace_content === true ? { traceContent: true as const } : {}),
294
302
  ...(config.config === undefined ? {} : { config: config.config }),
295
303
  };
296
304
  } catch (error) {
@@ -376,6 +384,10 @@ export async function runPlatformConnect(
376
384
  // run token always left its mode, and the report never pairs a fresh
377
385
  // token with the stale first-exchange mode.
378
386
  deps.appendOutput(`data-mode=${result.dataMode ?? "metadata_only"}\n`);
387
+ // The trace step (src/ci/platform-trace.ts) pairs it with this exchange's
388
+ // run token, like the report step does data-mode, so it is written before
389
+ // the tokens for the same reason.
390
+ deps.appendOutput(`trace-content=${result.traceContent === true}\n`);
379
391
  if (result.dataMode === undefined) {
380
392
  deps.stdout(
381
393
  "::notice title=crysk::The Crysk platform sent no recognised data_mode; treating this repository as metadata_only, so no finding text is uploaded.\n",
@@ -395,18 +407,22 @@ export async function runPlatformConnect(
395
407
  // output) and an empty config both skip everything.
396
408
  if (configFile !== undefined) {
397
409
  deps.appendOutput(`config-file=${configFile}\n`);
398
- // The same test the OpenCode install step makes of the routing input
399
- // (`contains(inputs.routing, 'opencode')`): platform routing that names
400
- // OpenCode needs the CLI installed too. Present whenever the platform
401
- // sent a routing, `false` included, because platform routing wins over
402
- // the input: action.yml reads an empty output as "use the input's" and
403
- // any value as the platform's answer, so a platform routing that does not
404
- // name OpenCode overrides an input that does.
405
- const routing = result.config?.person.routing;
406
- if (routing !== undefined) {
407
- deps.appendOutput(
408
- `routing-opencode=${JSON.stringify(routing).toLowerCase().includes("opencode")}\n`,
409
- );
410
+ // Whether the models in effect name OpenCode (S1): an `opencode:` model
411
+ // needs the CLI installed. Present whenever the platform sent a non-empty
412
+ // person layer, `false` included, because that layer wins whole over the
413
+ // `models` and `credentials` inputs (D1; the setup step ignores them),
414
+ // so a layer with no models leaves the default models in effect. The
415
+ // trigger's backend flag in action.yml reads an empty output as "use the
416
+ // input's" (`contains(inputs.models, 'opencode:')`) and any value as the
417
+ // platform's answer. The OpenCode install step also reads the setup
418
+ // step's `opencode-models`, its own reading of the models in effect. A
419
+ // platform `routing` is refused by the setup step's person parser.
420
+ const person = result.config?.person ?? {};
421
+ if (Object.keys(person).length > 0) {
422
+ const models = person.models;
423
+ const opencode =
424
+ models !== undefined && JSON.stringify(models).includes('"opencode:');
425
+ deps.appendOutput(`routing-opencode=${opencode}\n`);
410
426
  }
411
427
  }
412
428
  } catch (error) {