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,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
+ }