crysk 0.5.0 → 0.6.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 (59) hide show
  1. package/README.md +2 -1
  2. package/package.json +3 -2
  3. package/skills/crysk-ci-setup/assets/workflow.yml +3 -0
  4. package/src/asset-manifest.ts +11 -0
  5. package/src/assets.ts +1 -1
  6. package/src/ci/credential-refresh.ts +524 -0
  7. package/src/ci/crysk-secrets.ts +20 -6
  8. package/src/ci/force-comment.ts +14 -1
  9. package/src/ci/gates.ts +64 -21
  10. package/src/ci/outcome.ts +165 -15
  11. package/src/ci/platform-estimate.ts +111 -0
  12. package/src/ci/platform-trace.ts +30 -0
  13. package/src/ci/platform.ts +183 -5
  14. package/src/ci/reporter.ts +14 -4
  15. package/src/ci/setup.ts +96 -6
  16. package/src/ci/trace-bundle.ts +231 -27
  17. package/src/ci/trace-transcripts.ts +65 -0
  18. package/src/ci/trigger.ts +20 -4
  19. package/src/commands/ci-setup.ts +12 -3
  20. package/src/commands/doctor.ts +25 -23
  21. package/src/diversity/identity.ts +11 -1
  22. package/src/diversity/pipeline-integration.ts +19 -6
  23. package/src/doctor.ts +133 -4
  24. package/src/execution/contracts.ts +15 -3
  25. package/src/harness/materiality-judge.ts +153 -0
  26. package/src/harness/pipeline-plan.ts +133 -0
  27. package/src/harness/replay-rows.ts +172 -0
  28. package/src/harness/route-runner.ts +288 -0
  29. package/src/harness/single-step.ts +153 -0
  30. package/src/index.ts +1 -1
  31. package/src/model/catalog.ts +10 -7
  32. package/src/model/model-name.ts +14 -0
  33. package/src/model/profiles/claude.ts +34 -0
  34. package/src/model/profiles/openai.ts +44 -0
  35. package/src/model/profiles.ts +213 -0
  36. package/src/model/routing.ts +155 -50
  37. package/src/pr/ci-admission-gate.ts +2 -0
  38. package/src/pr/discovery.ts +18 -0
  39. package/src/pr/plan.ts +41 -2
  40. package/src/pr/review-pr.ts +43 -6
  41. package/src/pr/target.ts +67 -15
  42. package/src/production-runtime.ts +100 -13
  43. package/src/review/pipeline.ts +193 -58
  44. package/src/review/preflight.ts +91 -9
  45. package/src/review/prompt-set.ts +68 -0
  46. package/src/review/report.ts +4 -0
  47. package/src/review/review.ts +21 -11
  48. package/src/review/route-preflight.ts +117 -67
  49. package/src/review/run.ts +50 -4
  50. package/src/security/credential-broker.ts +21 -9
  51. package/src/security/oauth-providers.ts +101 -0
  52. package/src/transport-registry.ts +33 -0
  53. package/src/transports/opencode-sdk.ts +33 -1
  54. package/src/ui/config-edit.ts +9 -0
  55. package/src/ui/config.ts +1 -0
  56. package/src/ui/menu.ts +9 -2
  57. package/src/ui/plan.ts +8 -3
  58. package/src/updater.ts +21 -13
  59. package/src/wizard.ts +23 -0
package/src/ci/gates.ts CHANGED
@@ -190,7 +190,10 @@ function sizeLimitsText(maxLines: number, maxFiles: number): string {
190
190
  return `${lines} / ${files}`;
191
191
  }
192
192
 
193
- function buildSizeSkipComment(data: SkipSizeSummary): string {
193
+ function buildSizeSkipComment(
194
+ data: SkipSizeSummary,
195
+ platformConnected: boolean,
196
+ ): string {
194
197
  const lines = [
195
198
  SKIP_SIZE_COMMENT_MARKER,
196
199
  "## Crysk review skipped",
@@ -199,16 +202,28 @@ function buildSizeSkipComment(data: SkipSizeSummary): string {
199
202
  `${data.changedFiles} file(s), exceeding the configured size gate ` +
200
203
  `(${sizeLimitsText(data.maxChangedLines, data.maxChangedFiles)}).`,
201
204
  "",
202
- "Crysk did not run to avoid reviewing an unbounded diff. Split the " +
203
- "PR or raise `max-changed-lines` / `max-changed-files` to change the " +
204
- "limit for later pushes.",
205
+ // A platform repository sets its limits in the dashboard, not as inputs.
206
+ platformConnected
207
+ ? "Crysk did not run to avoid reviewing an unbounded diff. Split the " +
208
+ "PR or raise the size limits in the Crysk dashboard to change them " +
209
+ "for later pushes."
210
+ : "Crysk did not run to avoid reviewing an unbounded diff. Split the " +
211
+ "PR or raise `max-changed-lines` / `max-changed-files` to change the " +
212
+ "limit for later pushes.",
205
213
  "",
206
- ciForceReviewHintLine(data.prNumber),
214
+ ciForceReviewHintLine(data.prNumber, platformConnected),
207
215
  ];
208
216
  return `${lines.join("\n")}\n`;
209
217
  }
210
218
 
219
+ // What a platform repository can do about a budget skip (cryskai/platform#12):
220
+ // it has no `budget-usd` input to raise, and what a review costs is the model
221
+ // setup chosen in the dashboard.
222
+ const PLATFORM_BUDGET_SKIP_HINT =
223
+ "To change what reviews cost, change the model setup in the Crysk dashboard.";
224
+
211
225
  function buildBudgetSkipComment(data: SkipBudgetSummary): string {
226
+ const platformConnected = data.platformConnected === true;
212
227
  const lines = [
213
228
  SKIP_BUDGET_COMMENT_MARKER,
214
229
  "## Crysk review skipped",
@@ -216,10 +231,12 @@ function buildBudgetSkipComment(data: SkipBudgetSummary): string {
216
231
  `⚠️ The estimated review cost (${usd(data.estimatedCostUsd)}) exceeds ` +
217
232
  `the configured CI budget ceiling (${usd(data.budgetUsd)}).`,
218
233
  "",
219
- "Crysk did not run to stay within the configured `--budget-usd` " +
220
- "ceiling.",
234
+ platformConnected
235
+ ? PLATFORM_BUDGET_SKIP_HINT
236
+ : "Crysk did not run to stay within the configured `--budget-usd` " +
237
+ "ceiling.",
221
238
  "",
222
- ciForceReviewHintLine(data.prNumber),
239
+ ciForceReviewHintLine(data.prNumber, platformConnected),
223
240
  ];
224
241
  return `${lines.join("\n")}\n`;
225
242
  }
@@ -240,6 +257,9 @@ export interface CiSizeGateSkipInput {
240
257
  // failed (size-gate.ts), but the summary/comment show both metrics.
241
258
  maxChangedLines: number;
242
259
  maxChangedFiles: number;
260
+ // The run came through the Crysk platform (CRYSK_PLATFORM_CONNECTED): the
261
+ // comment names only what applies to a platform repository.
262
+ platformConnected?: boolean;
243
263
  }
244
264
 
245
265
  // `null` means "nothing to publish": either this isn't a CI run, or the
@@ -254,8 +274,12 @@ export function ciSizeGateSkip(input: CiSizeGateSkipInput): CiGateSkip | null {
254
274
  changedFiles: input.verdict.effectiveFiles,
255
275
  maxChangedLines: input.maxChangedLines,
256
276
  maxChangedFiles: input.maxChangedFiles,
277
+ ...(input.platformConnected === true ? { platformConnected: true } : {}),
278
+ };
279
+ return {
280
+ comment: buildSizeSkipComment(summary, input.platformConnected === true),
281
+ summary,
257
282
  };
258
- return { comment: buildSizeSkipComment(summary), summary };
259
283
  }
260
284
 
261
285
  export interface CiBudgetGateSkipInput {
@@ -263,6 +287,9 @@ export interface CiBudgetGateSkipInput {
263
287
  estimatedCostUsd: number;
264
288
  budgetUsd: number;
265
289
  prNumber: number;
290
+ // The run came through the Crysk platform (CRYSK_PLATFORM_CONNECTED): the
291
+ // skip names only what applies to a platform repository.
292
+ platformConnected?: boolean;
266
293
  }
267
294
 
268
295
  export function ciBudgetGateSkip(
@@ -275,6 +302,7 @@ export function ciBudgetGateSkip(
275
302
  prNumber: input.prNumber,
276
303
  estimatedCostUsd: input.estimatedCostUsd,
277
304
  budgetUsd: input.budgetUsd,
305
+ ...(input.platformConnected === true ? { platformConnected: true } : {}),
278
306
  };
279
307
  return { comment: buildBudgetSkipComment(summary), summary };
280
308
  }
@@ -382,19 +410,19 @@ export type CiBillingMode = "subscription" | "metered";
382
410
  // through that one decision — this CI gate is not that caller, and never
383
411
  // should be.
384
412
  //
385
- // Optional `openCodeAuthPresent` is the OpenCode half of the same ceiling
413
+ // Optional `openCodeBillsMetered` is the OpenCode half of the same ceiling
386
414
  // question — "could this run invoice?" — ORed beside the Anthropic env
387
415
  // predicate, never folded into it. `envBillsMetered` stamps Claude CLI
388
416
  // usage and must stay Anthropic-env-only. Omit the option (or leave it
389
- // undefined/false) and this function stays env-only. Presence of the 0600
390
- // auth.json is enough: we cannot rule out an invoice, same conservative
391
- // doctrine as Anthropic keys. The instance is the file, not a parsed
392
- // provider name.
417
+ // undefined/false) and this function stays env-only. The caller derives it
418
+ // from the routes the run resolves (openCodeRoutesBillMetered, ci/setup.ts):
419
+ // an OpenCode provider bound to an API key can invoice; an auth file that only
420
+ // holds providers no model uses cannot (cryskai/platform#12).
393
421
  export function deriveCiBillingMode(
394
422
  env: Record<string, string | undefined>,
395
- opts?: { openCodeAuthPresent?: boolean },
423
+ opts?: { openCodeBillsMetered?: boolean },
396
424
  ): CiBillingMode {
397
- return envBillsMetered(env) || opts?.openCodeAuthPresent === true
425
+ return envBillsMetered(env) || opts?.openCodeBillsMetered === true
398
426
  ? "metered"
399
427
  : "subscription";
400
428
  }
@@ -655,6 +683,7 @@ const COVERAGE_REASON_LABEL: Record<
655
683
  function buildCoverageSkipComment(
656
684
  data: SkipCoverageSummary,
657
685
  reason: CiReviewSkipReason,
686
+ platformConnected: boolean,
658
687
  ): string {
659
688
  // A stop is a maintainer's decision, not a score: the score line would
660
689
  // blame the push for a skip it did not cause.
@@ -677,7 +706,7 @@ function buildCoverageSkipComment(
677
706
  "The existing review comment still describes the last head Crysk " +
678
707
  "reviewed.",
679
708
  "",
680
- ciForceReviewHintLine(data.prNumber),
709
+ ciForceReviewHintLine(data.prNumber, platformConnected),
681
710
  ];
682
711
  return `${lines.join("\n")}\n`;
683
712
  }
@@ -686,7 +715,10 @@ function buildCoverageSkipComment(
686
715
  // it by the repository's own rules, says what the gate does with the next pushes, and how to ask for more.
687
716
  // Stage 2 #12: no review reads those pushes, so the gate holds a green instead of carrying it, and the
688
717
  // override is how a human merges a head no review verified.
689
- function buildManualRequiredComment(data: ManualRequiredSummary): string {
718
+ function buildManualRequiredComment(
719
+ data: ManualRequiredSummary,
720
+ platformConnected: boolean,
721
+ ): string {
690
722
  const manualOnly = data.admissionReason === "manual-only-policy";
691
723
  const rules = [
692
724
  ...(data.policyMode === undefined
@@ -720,7 +752,7 @@ function buildManualRequiredComment(data: ManualRequiredSummary): string {
720
752
  "",
721
753
  "The existing review comment still describes the last head Crysk reviewed.",
722
754
  "",
723
- `Want more review rounds? ${ciForceReviewHintLine(data.prNumber)}`,
755
+ `Want more review rounds? ${ciForceReviewHintLine(data.prNumber, platformConnected)}`,
724
756
  ];
725
757
  return `${lines.join("\n")}\n`;
726
758
  }
@@ -780,6 +812,8 @@ export function planCiReviewSkip(input: {
780
812
  verdict: Extract<CiReviewAdmissionVerdict, { action: "skip" }>;
781
813
  admission?: AdmissionContext;
782
814
  ciMode: CiMode;
815
+ // The run came through the Crysk platform: the hint names only `/crysk review`.
816
+ platformConnected?: boolean;
783
817
  }): CiGateSkipPlan {
784
818
  const detail = ciReviewSkipDetail(input.verdict);
785
819
  const summary: SkipCoverageSummary = {
@@ -794,7 +828,11 @@ export function planCiReviewSkip(input: {
794
828
  ...skipAdmissionSummaryFields(input.verdict, input.admission),
795
829
  };
796
830
  return {
797
- comment: buildCoverageSkipComment(summary, input.verdict.reason),
831
+ comment: buildCoverageSkipComment(
832
+ summary,
833
+ input.verdict.reason,
834
+ input.platformConnected === true,
835
+ ),
798
836
  markerPrefix: SKIP_COVERAGE_COMMENT_MARKER,
799
837
  summaryMarkdown: renderStepSummary(summary, input.ciMode),
800
838
  outputs: ciGateSkipOutputs("skipped-coverage", 0),
@@ -806,6 +844,8 @@ export function planCiReviewManualRequired(input: {
806
844
  verdict: Extract<CiReviewAdmissionVerdict, { action: "manual-required" }>;
807
845
  admission?: AdmissionContext;
808
846
  ciMode: CiMode;
847
+ // The run came through the Crysk platform: the hint names only `/crysk review`.
848
+ platformConnected?: boolean;
809
849
  }): CiGateSkipPlan {
810
850
  const detail = ciReviewManualRequiredDetail(input.verdict);
811
851
  const summary: ManualRequiredSummary = {
@@ -822,7 +862,10 @@ export function planCiReviewManualRequired(input: {
822
862
  ...manualAdmissionSummaryFields(input.verdict, input.admission),
823
863
  };
824
864
  return {
825
- comment: buildManualRequiredComment(summary),
865
+ comment: buildManualRequiredComment(
866
+ summary,
867
+ input.platformConnected === true,
868
+ ),
826
869
  markerPrefix: MANUAL_REQUIRED_COMMENT_MARKER,
827
870
  summaryMarkdown: renderStepSummary(summary, input.ciMode),
828
871
  outputs: ciGateSkipOutputs("manual-required", 0),
package/src/ci/outcome.ts CHANGED
@@ -23,7 +23,9 @@
23
23
 
24
24
  import { commandHint, MARKER_OPEN, matchMarkerPrefix } from "../brand";
25
25
  import type {
26
+ ClaudeFailureCredential,
26
27
  FailureCredential,
28
+ OpenCodeFailureCredential,
27
29
  TransportFailureCause,
28
30
  } from "../execution/contracts";
29
31
  import type { CredentialProjectionFailureClass } from "../security/credential-broker";
@@ -45,11 +47,12 @@ export type ErrorClass =
45
47
  }
46
48
  | {
47
49
  kind: "config-parse";
48
- // `models-input` and `credentials-input`: the setup step's reading of the
49
- // `models` and `credentials` inputs (D1), never the CLI's.
50
+ // `models-input`, `credentials-input` and `profile-input`: the setup
51
+ // step's reading of those inputs (D1), never the CLI's.
50
52
  source:
51
53
  | "models-input"
52
54
  | "credentials-input"
55
+ | "profile-input"
53
56
  | "repo-config"
54
57
  | "person-config"
55
58
  | "extra-hunters";
@@ -253,7 +256,10 @@ export type CliErrorClass =
253
256
  | { kind: "trigger"; check: "no-credential" }
254
257
  | {
255
258
  kind: "credential";
256
- failureClass: "missing_provider_record" | "missing_subscription_record";
259
+ failureClass:
260
+ | "missing_provider_record"
261
+ | "missing_subscription_record"
262
+ | "subscription_expiring";
257
263
  }
258
264
  | { kind: "capability"; issue: "sdk_unavailable" | "version_unsupported" }
259
265
  | {
@@ -324,7 +330,8 @@ export interface HunterFailureRecord {
324
330
  // One short line saying why the hunter failed, in the provider's words (see
325
331
  // safeFailureDetail). Present only when the transport knew it.
326
332
  detail?: string;
327
- // The secret the hunter ran on (ANTHROPIC_API_KEY or CLAUDE_CODE_OAUTH_TOKEN), so a
333
+ // The secret the hunter ran on (ANTHROPIC_API_KEY or CLAUDE_CODE_OAUTH_TOKEN for a
334
+ // Claude route, CRYSK_OPENCODE_<P>_API_KEY or _OAUTH for an OpenCode one), so a
328
335
  // credential or quota failure can name which one to replace or top up.
329
336
  credential?: FailureCredential;
330
337
  }
@@ -379,21 +386,51 @@ export function safeModelName(value: unknown): string | undefined {
379
386
  : undefined;
380
387
  }
381
388
 
382
- // Every FailureCredential, as a runtime lookup (tied to the union by `satisfies`, like the
383
- // cause list above: this file cannot import the runtime list from execution/contracts).
389
+ // Every Claude FailureCredential, as a runtime lookup (tied to the union by `satisfies`,
390
+ // like the cause list above: this file cannot import the runtime list from
391
+ // execution/contracts).
384
392
  const FAILURE_CREDENTIAL_LOOKUP = {
385
393
  ANTHROPIC_API_KEY: true,
386
394
  ANTHROPIC_AUTH_TOKEN: true,
387
395
  CLAUDE_CODE_OAUTH_TOKEN: true,
388
- } satisfies Record<FailureCredential, true>;
396
+ } satisfies Record<ClaudeFailureCredential, true>;
397
+
398
+ // An OpenCode FailureCredential: a CRYSK_OPENCODE_<P>_API_KEY or _OAUTH secret name. A
399
+ // COPY of OPENCODE_SECRET_NAME_RE in crysk-secrets.ts, which this module cannot import at
400
+ // runtime (the builtins-only guard); test/ci/opencode-failure-credential.test.ts pins the
401
+ // two to the same answers.
402
+ const OPENCODE_FAILURE_CREDENTIAL_RE =
403
+ /^CRYSK_OPENCODE_([A-Z0-9]+(?:_[A-Z0-9]+)*)_(API_KEY|OAUTH)$/;
404
+
405
+ function isOpenCodeCredential(
406
+ value: string,
407
+ ): value is OpenCodeFailureCredential {
408
+ return OPENCODE_FAILURE_CREDENTIAL_RE.test(value);
409
+ }
389
410
 
390
411
  export function knownFailureCredential(
391
412
  value: unknown,
392
413
  ): FailureCredential | undefined {
393
- return typeof value === "string" &&
394
- Object.hasOwn(FAILURE_CREDENTIAL_LOOKUP, value)
395
- ? (value as FailureCredential)
396
- : undefined;
414
+ if (typeof value !== "string") return undefined;
415
+ if (Object.hasOwn(FAILURE_CREDENTIAL_LOOKUP, value)) {
416
+ return value as ClaudeFailureCredential;
417
+ }
418
+ return isOpenCodeCredential(value) ? value : undefined;
419
+ }
420
+
421
+ // The OpenCode variant of a credential's fix, or undefined for a Claude credential. An
422
+ // `_OAUTH` secret is a subscription record (openai only, crysk-secrets.ts), so its fix is
423
+ // a new `opencode auth login`; an `_API_KEY` secret is replaced with a new key.
424
+ function openCodeCredentialVariant(
425
+ secret: string | undefined,
426
+ ): "opencode_api_key" | "opencode_oauth" | undefined {
427
+ const kind =
428
+ secret === undefined
429
+ ? undefined
430
+ : OPENCODE_FAILURE_CREDENTIAL_RE.exec(secret)?.[2];
431
+ if (kind === "API_KEY") return "opencode_api_key";
432
+ if (kind === "OAUTH") return "opencode_oauth";
433
+ return undefined;
397
434
  }
398
435
 
399
436
  // A failure detail, as the record may carry it. Unlike a provider or model name it is the
@@ -472,7 +509,9 @@ function parseSetupClass(value: unknown): ErrorClass | undefined {
472
509
  if (!isRecordObject(value)) return undefined;
473
510
  if (
474
511
  value.kind === "config-parse" &&
475
- (value.source === "models-input" || value.source === "credentials-input")
512
+ (value.source === "models-input" ||
513
+ value.source === "credentials-input" ||
514
+ value.source === "profile-input")
476
515
  ) {
477
516
  return { kind: "config-parse", source: value.source };
478
517
  }
@@ -634,7 +673,8 @@ function parseCliClass(value: unknown): CliErrorClass | undefined {
634
673
  if (value.kind === "credential") {
635
674
  if (
636
675
  value.failureClass === "missing_provider_record" ||
637
- value.failureClass === "missing_subscription_record"
676
+ value.failureClass === "missing_subscription_record" ||
677
+ value.failureClass === "subscription_expiring"
638
678
  ) {
639
679
  return { kind: "credential", failureClass: value.failureClass };
640
680
  }
@@ -827,6 +867,7 @@ export function codeOf(cls: ErrorClass): ErrorCode {
827
867
  switch (cls.source) {
828
868
  case "models-input":
829
869
  case "credentials-input":
870
+ case "profile-input":
830
871
  case "repo-config":
831
872
  case "person-config":
832
873
  return "CRYSK-CONFIG-INVALID";
@@ -853,6 +894,7 @@ export function codeOf(cls: ErrorClass): ErrorCode {
853
894
  switch (cls.failureClass) {
854
895
  case "missing_provider_record":
855
896
  case "missing_subscription_record":
897
+ case "subscription_expiring":
856
898
  case "malformed_payload":
857
899
  case "source_read_failed":
858
900
  case "env-absent":
@@ -961,6 +1003,7 @@ export function ownerOf(cls: ErrorClass, ctx: OutcomeContext): Owner {
961
1003
  switch (cls.source) {
962
1004
  case "models-input":
963
1005
  case "credentials-input":
1006
+ case "profile-input":
964
1007
  case "repo-config":
965
1008
  case "person-config":
966
1009
  case "extra-hunters":
@@ -975,6 +1018,7 @@ export function ownerOf(cls: ErrorClass, ctx: OutcomeContext): Owner {
975
1018
  switch (cls.failureClass) {
976
1019
  case "missing_provider_record":
977
1020
  case "missing_subscription_record":
1021
+ case "subscription_expiring":
978
1022
  case "malformed_payload":
979
1023
  case "source_read_failed":
980
1024
  case "env-absent":
@@ -1106,6 +1150,8 @@ const CREDENTIAL_PHRASE: Record<
1106
1150
  "the expected subscription login is missing or of the wrong kind",
1107
1151
  missing_provider_record:
1108
1152
  "the OpenCode auth store has no entry for this provider",
1153
+ subscription_expiring:
1154
+ "the subscription login expires before the review would finish",
1109
1155
  "env-absent": "the secret is not set",
1110
1156
  };
1111
1157
 
@@ -1166,6 +1212,10 @@ const CLAUDE_SUBSCRIPTION_FIXES = [
1166
1212
  "Log in again with `claude setup-token`, then set `CRYSK_CLAUDE_OAUTH_TOKEN`:",
1167
1213
  "$ claude setup-token && gh secret set CRYSK_CLAUDE_OAUTH_TOKEN",
1168
1214
  ];
1215
+ // An OpenCode route uses an OpenAI subscription record while its _OAUTH secret is passed
1216
+ // (chooseOpenCodeSecret, subscription first), so billing per token needs both steps.
1217
+ const OPENAI_API_KEY_INSTEAD =
1218
+ "Or bill per token: set CRYSK_OPENCODE_OPENAI_API_KEY and stop passing {secret} in the crysk-secrets input (the subscription is used while it is passed):";
1169
1219
  const OPENCODE_SUBSCRIPTION_FIXES = [
1170
1220
  "Provider `openai` expects a ChatGPT login: run `opencode auth login` and choose ChatGPT, then set its record, base64 on one line (on macOS, drop -w0):",
1171
1221
  "$ opencode auth login",
@@ -1305,6 +1355,7 @@ const OPENCODE_CREDENTIAL_FIXES = [
1305
1355
  // the inputs above. The scaffolded workflow (src/ci/setup.ts) writes these lines, so
1306
1356
  // a fix that quotes them cannot disagree with the file the person has.
1307
1357
  export const MODELS_INPUT_LINE = "models: $" + "{{ vars.CRYSK_MODELS }}";
1358
+ export const PROFILE_INPUT_LINE = "profile: $" + "{{ vars.CRYSK_PROFILE }}";
1308
1359
  export const CREDENTIALS_INPUT_LINE =
1309
1360
  "credentials: $" + "{{ vars.CRYSK_CREDENTIALS }}";
1310
1361
  // One line of the crysk-secrets input as a workflow writes it, spliced like the
@@ -1429,6 +1480,19 @@ export const ERROR_CATALOG: Readonly<Record<ErrorCode, CatalogEntry>> = {
1429
1480
  ? "run `opencode auth login` and choose ChatGPT for provider `openai`, then set CRYSK_OPENCODE_OPENAI_OAUTH from its record, or set CRYSK_OPENCODE_OPENAI_API_KEY to bill per token"
1430
1481
  : "log in again with `claude setup-token` and set `CRYSK_CLAUDE_OAUTH_TOKEN` (`gh secret set CRYSK_CLAUDE_OAUTH_TOKEN`)",
1431
1482
  },
1483
+ // The review would outlive the login's access token, and a review never renews
1484
+ // a rotating provider's login itself (src/security/oauth-providers.ts).
1485
+ subscription_expiring: {
1486
+ what: "The {provider} subscription login for {step} expires before the review would finish",
1487
+ why: "A review never renews a subscription login whose provider rotates its refresh token: renewing it inside the run would spend the stored login and lose the new one with the run's copy",
1488
+ fixes: [
1489
+ "On a repository connected to Crysk, the platform renews the login before it expires. If it did not, reconnect the provider in the Crysk dashboard.",
1490
+ "On a workflow you manage yourself, run `opencode auth login` again and set the `CRYSK_OPENCODE_<PROVIDER>_OAUTH` secret from its new record: the secret is a snapshot, and nothing renews it for you.",
1491
+ "Locally, run any `opencode` request once (it renews your login in place), or `opencode auth login` again.",
1492
+ ],
1493
+ annotationFix:
1494
+ "reconnect {provider} in the Crysk dashboard, or on a self-managed workflow run `opencode auth login` and set its `_OAUTH` secret again",
1495
+ },
1432
1496
  malformed_payload: {
1433
1497
  // WHY no route in the sentence: this class is raised for the OpenCode auth
1434
1498
  // store and for a Claude keychain payload alike, and {detail} names which.
@@ -1487,6 +1551,35 @@ export const ERROR_CATALOG: Readonly<Record<ErrorCode, CatalogEntry>> = {
1487
1551
  annotationFix:
1488
1552
  "log in again with `claude setup-token` and set `{secret}` (`gh secret set {secret}`)",
1489
1553
  },
1554
+ // S7: an OpenCode route names its CRYSK_OPENCODE_* secret, so the fix is that
1555
+ // provider's, never a Claude login.
1556
+ opencode_api_key: {
1557
+ why: "the provider refused the API key in that secret: it is invalid, expired or revoked",
1558
+ fixes: [
1559
+ "Create a new key with the provider and set it:",
1560
+ "$ gh secret set {secret}",
1561
+ "Then ask for a new review:",
1562
+ REVIEW_AGAIN,
1563
+ LEGACY_REVIEW_NOTE,
1564
+ ],
1565
+ annotationFix:
1566
+ "create a new key with the provider and run `gh secret set {secret}`",
1567
+ },
1568
+ opencode_oauth: {
1569
+ why: "the ChatGPT login recorded in that secret is expired or revoked",
1570
+ fixes: [
1571
+ "Log in again with `opencode auth login` and choose ChatGPT, then set the provider's record, base64 on one line (on macOS, drop -w0):",
1572
+ "$ opencode auth login",
1573
+ OPENAI_OAUTH_SECRET,
1574
+ OPENAI_API_KEY_INSTEAD,
1575
+ "$ gh secret set CRYSK_OPENCODE_OPENAI_API_KEY",
1576
+ "Then ask for a new review:",
1577
+ REVIEW_AGAIN,
1578
+ LEGACY_REVIEW_NOTE,
1579
+ ],
1580
+ annotationFix:
1581
+ "log in again with `opencode auth login`, choose ChatGPT and set {secret} from its record, or set CRYSK_OPENCODE_OPENAI_API_KEY and stop passing {secret} to bill per token",
1582
+ },
1490
1583
  default: {
1491
1584
  why: "it is expired, revoked, or a short-lived /login session token",
1492
1585
  fixes: [
@@ -1588,6 +1681,20 @@ export const ERROR_CATALOG: Readonly<Record<ErrorCode, CatalogEntry>> = {
1588
1681
  annotationFix:
1589
1682
  "fix the credentials JSON ({detail}) and set it again with `gh variable set CRYSK_CREDENTIALS`, the variable the workflow passes as `vars.CRYSK_CREDENTIALS`",
1590
1683
  },
1684
+ // The `profile` input, from vars.CRYSK_PROFILE (openai-profile S6).
1685
+ "profile-input": {
1686
+ what: "The profile input is invalid: {detail}",
1687
+ why: "Crysk refuses to guess which models the review should run",
1688
+ fixes: [
1689
+ "Set the variable to a known profile, for example:",
1690
+ "$ gh variable set CRYSK_PROFILE --body openai",
1691
+ `The workflow passes it as \`${PROFILE_INPUT_LINE}\`.`,
1692
+ "Then re-run the workflow:",
1693
+ "$ gh run rerun {runId}",
1694
+ ],
1695
+ annotationFix:
1696
+ "set a known profile ({detail}) with `gh variable set CRYSK_PROFILE`, the variable the workflow passes as `vars.CRYSK_PROFILE`",
1697
+ },
1591
1698
  },
1592
1699
  },
1593
1700
  "CRYSK-TOKEN-PERMISSION": {
@@ -1779,6 +1886,34 @@ export const ERROR_CATALOG: Readonly<Record<ErrorCode, CatalogEntry>> = {
1779
1886
  annotationFix:
1780
1887
  "re-run after the limit resets, or set CRYSK_CLAUDE_API_KEY and put api_key first in `credentials.claude` to bill per token",
1781
1888
  },
1889
+ // S7: the OpenCode secret every failed hunter ran on. The OpenCode transport reports
1890
+ // no provider words, so these sentences name the secret instead of quoting them.
1891
+ opencode_api_key: {
1892
+ what: "the account behind {secret} reached a rate limit or quota",
1893
+ why: "the provider refuses requests on this key until the limit resets or the account has credit again, so an immediate retry would fail the same way",
1894
+ fixes: [
1895
+ "Wait for the limit to reset, or add credit or raise the quota on the provider account behind the key, then ask for a new review:",
1896
+ REVIEW_AGAIN,
1897
+ LEGACY_REVIEW_NOTE,
1898
+ "Or set a key from an account that has quota:",
1899
+ "$ gh secret set {secret}",
1900
+ ],
1901
+ annotationFix:
1902
+ "wait for the limit to reset or add credit to the provider account behind {secret}, or set another key with `gh secret set {secret}`",
1903
+ },
1904
+ opencode_oauth: {
1905
+ what: "the ChatGPT subscription behind {secret} reached its usage limit",
1906
+ why: "the subscription refuses requests until its usage window resets, so an immediate retry would fail the same way",
1907
+ fixes: [
1908
+ "Re-run after the limit resets:",
1909
+ REVIEW_AGAIN,
1910
+ LEGACY_REVIEW_NOTE,
1911
+ OPENAI_API_KEY_INSTEAD,
1912
+ "$ gh secret set CRYSK_OPENCODE_OPENAI_API_KEY",
1913
+ ],
1914
+ annotationFix:
1915
+ "re-run after the limit resets, or set CRYSK_OPENCODE_OPENAI_API_KEY and stop passing {secret} to bill per token",
1916
+ },
1782
1917
  },
1783
1918
  },
1784
1919
  "CRYSK-PROVIDER-UNAVAILABLE": {
@@ -1932,6 +2067,18 @@ export function renderHunterCause(
1932
2067
  // subscription window by waiting for its reset. Without the words the default wording
1933
2068
  // stands.
1934
2069
  if (cause === "quota_exhausted") {
2070
+ // An OpenCode secret names its own fix with or without the provider's words: the
2071
+ // OpenCode transport reports no detail today, and the Claude variants below would
2072
+ // send the user to the Anthropic account.
2073
+ const openCode = openCodeCredentialVariant(shared.credential);
2074
+ if (openCode !== undefined && shared.credential !== undefined) {
2075
+ return renderCode(
2076
+ "CRYSK-PROVIDER-LIMITED",
2077
+ { ...params, secret: secretNameOf(shared.credential, ctx) },
2078
+ ctx,
2079
+ openCode,
2080
+ );
2081
+ }
1935
2082
  if (shared.detail === undefined) {
1936
2083
  return renderCode("CRYSK-PROVIDER-LIMITED", params, ctx);
1937
2084
  }
@@ -1963,8 +2110,9 @@ export function renderHunterCause(
1963
2110
  // variable was filled from the CRYSK_* secret the workflow passes in the
1964
2111
  // crysk-secrets input (S8, U5), so `gh secret set` must name that secret. A local
1965
2112
  // run exports the CLI's variable itself, so it keeps that name.
2113
+ // An OpenCode credential already is the CRYSK_* secret (S7), so it is named as it is.
1966
2114
  const CRYSK_SECRET_OF_CREDENTIAL: Readonly<
1967
- Partial<Record<FailureCredential, string>>
2115
+ Partial<Record<ClaudeFailureCredential, string>>
1968
2116
  > = {
1969
2117
  ANTHROPIC_API_KEY: "CRYSK_CLAUDE_API_KEY",
1970
2118
  CLAUDE_CODE_OAUTH_TOKEN: "CRYSK_CLAUDE_OAUTH_TOKEN",
@@ -1973,7 +2121,7 @@ function secretNameOf(
1973
2121
  credential: FailureCredential,
1974
2122
  ctx: OutcomeContext,
1975
2123
  ): string {
1976
- if (ctx !== "action") return credential;
2124
+ if (ctx !== "action" || isOpenCodeCredential(credential)) return credential;
1977
2125
  return CRYSK_SECRET_OF_CREDENTIAL[credential] ?? credential;
1978
2126
  }
1979
2127
 
@@ -2058,6 +2206,8 @@ function paramVariant(
2058
2206
  ) {
2059
2207
  return "oauth";
2060
2208
  }
2209
+ const openCode = openCodeCredentialVariant(params.secret);
2210
+ if (openCode !== undefined) return openCode;
2061
2211
  }
2062
2212
  // A caller that names a variant keeps it, so the default wording stays
2063
2213
  // reachable with a detail present.
@@ -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
+ }