crysk 0.5.1 → 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 (48) 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/outcome.ts +165 -15
  9. package/src/ci/platform.ts +155 -7
  10. package/src/ci/setup.ts +64 -6
  11. package/src/ci/trigger.ts +20 -4
  12. package/src/commands/ci-setup.ts +12 -3
  13. package/src/commands/doctor.ts +25 -23
  14. package/src/diversity/identity.ts +11 -1
  15. package/src/diversity/pipeline-integration.ts +19 -6
  16. package/src/doctor.ts +133 -4
  17. package/src/execution/contracts.ts +15 -3
  18. package/src/harness/materiality-judge.ts +153 -0
  19. package/src/harness/pipeline-plan.ts +133 -0
  20. package/src/harness/replay-rows.ts +172 -0
  21. package/src/harness/route-runner.ts +288 -0
  22. package/src/harness/single-step.ts +153 -0
  23. package/src/index.ts +1 -1
  24. package/src/model/catalog.ts +10 -7
  25. package/src/model/model-name.ts +14 -0
  26. package/src/model/profiles/claude.ts +34 -0
  27. package/src/model/profiles/openai.ts +44 -0
  28. package/src/model/profiles.ts +213 -0
  29. package/src/model/routing.ts +155 -50
  30. package/src/pr/plan.ts +6 -1
  31. package/src/pr/review-pr.ts +32 -6
  32. package/src/pr/target.ts +16 -7
  33. package/src/production-runtime.ts +100 -13
  34. package/src/review/pipeline.ts +193 -58
  35. package/src/review/preflight.ts +91 -9
  36. package/src/review/prompt-set.ts +68 -0
  37. package/src/review/review.ts +21 -11
  38. package/src/review/route-preflight.ts +117 -67
  39. package/src/review/run.ts +34 -4
  40. package/src/security/credential-broker.ts +21 -9
  41. package/src/security/oauth-providers.ts +101 -0
  42. package/src/transport-registry.ts +33 -0
  43. package/src/transports/opencode-sdk.ts +33 -1
  44. package/src/ui/config-edit.ts +9 -0
  45. package/src/ui/config.ts +1 -0
  46. package/src/ui/menu.ts +9 -2
  47. package/src/updater.ts +21 -13
  48. package/src/wizard.ts +23 -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.
@@ -19,8 +19,24 @@
19
19
  // WHY ONLY BUILTINS: like the trigger, this runs before `bun install`. Keep
20
20
  // every import `node:` or `bun`, and `fetch` is the runtime's own.
21
21
 
22
- import { appendFileSync, mkdirSync, writeFileSync } from "node:fs";
22
+ import {
23
+ appendFileSync,
24
+ existsSync,
25
+ mkdirSync,
26
+ readFileSync,
27
+ writeFileSync,
28
+ } from "node:fs";
23
29
  import path from "node:path";
30
+ // Import-free, like the registry below.
31
+ import { readBrandEnv } from "../brand";
32
+ // The profile registry: data modules with no runtime imports of their own
33
+ // (test/architecture/profile-registry.test.ts), so the builtins-only rule still
34
+ // holds, and the step reads the same profiles the review runs.
35
+ import {
36
+ FALLBACK_PROFILE_ID,
37
+ MODEL_PROFILES,
38
+ profileModels,
39
+ } from "../model/profiles";
24
40
  // A sibling with no imports of its own, so the builtins-only rule still holds.
25
41
  import {
26
42
  PLATFORM_ESTIMATE_CAPABILITY,
@@ -31,6 +47,18 @@ import {
31
47
 
32
48
  export const PLATFORM_API_URL = "https://api.crysk.dev";
33
49
 
50
+ // The engine accepts the person config's `profile` key (openai-profile S7). An
51
+ // engine without it refuses the key as unknown, so the platform sends a
52
+ // profile only to a run that names this capability.
53
+ export const PLATFORM_PROFILE_CAPABILITY = "profile";
54
+
55
+ // What this engine reads from the `/runs/token` response, as the platform's
56
+ // comma-separated `X-Crysk-Capabilities` list.
57
+ export const PLATFORM_CAPABILITIES = [
58
+ PLATFORM_ESTIMATE_CAPABILITY,
59
+ PLATFORM_PROFILE_CAPABILITY,
60
+ ].join(",");
61
+
34
62
  // The audience the platform verifies. An OIDC token minted for any other
35
63
  // audience is refused with a 401.
36
64
  const OIDC_AUDIENCE = "crysk";
@@ -246,10 +274,11 @@ export async function exchangePlatformToken(
246
274
  Authorization: `Bearer ${oidc}`,
247
275
  "X-Crysk-Dispatch-Id": dispatchId,
248
276
  // Only the first connect step asks: its estimate is the one the budget
249
- // gate reads, and the platform records each estimate it sends.
277
+ // gate reads, the platform records each estimate it sends, and only
278
+ // that step reads the config a profile arrives in.
250
279
  ...(deps.ignoreConfig === true
251
280
  ? {}
252
- : { "X-Crysk-Capabilities": PLATFORM_ESTIMATE_CAPABILITY }),
281
+ : { "X-Crysk-Capabilities": PLATFORM_CAPABILITIES }),
253
282
  },
254
283
  signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
255
284
  });
@@ -449,10 +478,21 @@ export async function runPlatformConnect(
449
478
  // platform `routing` is refused by the setup step's person parser.
450
479
  const person = result.config?.person ?? {};
451
480
  if (Object.keys(person).length > 0) {
452
- const models = person.models;
453
- const opencode =
454
- models !== undefined && JSON.stringify(models).includes('"opencode:');
455
- deps.appendOutput(`routing-opencode=${opencode}\n`);
481
+ const customAgents = customAgentsMayApply(
482
+ person,
483
+ result.config?.repo ?? {},
484
+ deps.env,
485
+ );
486
+ deps.appendOutput(
487
+ `routing-opencode=${personMayNeedOpenCode(person)}\n`,
488
+ );
489
+ // Whether any step may run on the Claude CLI (model-profiles S6). The
490
+ // Install Claude Code CLI step skips only on exactly `false`, so an
491
+ // absent output (a standalone run, an empty person layer, an older
492
+ // engine) installs it, as every run always did.
493
+ deps.appendOutput(
494
+ `routing-claude=${personMayNeedClaude(person, { customAgents })}\n`,
495
+ );
456
496
  }
457
497
  }
458
498
  } catch (error) {
@@ -465,6 +505,114 @@ export async function runPlatformConnect(
465
505
  return 0;
466
506
  }
467
507
 
508
+ // Whether the person layer's `models` may send any step to the Claude CLI.
509
+ // `false` only when it is proven not to: a `default` list, so no role falls
510
+ // through to the `claude` profile, and every entry of every role, fallbacks and
511
+ // the judge included, an `opencode:` model. With no `models.judge` the judge
512
+ // joins a plan only beside a step on its own backend (routing.ts), so it stays
513
+ // off Claude here whatever `materiality_gate` says. Anything else, a shape this
514
+ // walk does not know included, is `true`: the engine's parser stays the
515
+ // authority (this step cannot import it), and a wrong `false` would leave a
516
+ // Claude run without its CLI. src/doctor.ts reviewNeedsClaude answers the same
517
+ // question from the real route plan; test/ci/platform.test.ts holds both to
518
+ // one table.
519
+ //
520
+ // A role `models` does not set runs the person's `profile` (openai-profile S6),
521
+ // so with no `models.default` the answer is the profile's: `false` only for a
522
+ // registered profile whose every route is OpenCode. No profile is the fallback
523
+ // profile, `claude`; an unknown one is `true`, and the engine refuses it.
524
+ export function personModelsMayNeedClaude(
525
+ models: unknown,
526
+ profile?: unknown,
527
+ ): boolean {
528
+ if (models !== undefined) {
529
+ if (!isPlainObject(models)) return true;
530
+ for (const list of Object.values(models)) {
531
+ if (!Array.isArray(list) || list.length === 0) return true;
532
+ for (const entry of list) {
533
+ const name = isPlainObject(entry) ? entry.model : entry;
534
+ if (typeof name !== "string" || !name.startsWith("opencode:")) {
535
+ return true;
536
+ }
537
+ }
538
+ }
539
+ if (Array.isArray(models.default)) return false;
540
+ }
541
+ const routes = profileRouteModels(profile);
542
+ return routes === undefined || routes.some((m) => !m.startsWith("opencode:"));
543
+ }
544
+
545
+ // Whether the models in effect run any step on OpenCode: an `opencode:` model
546
+ // in `models`, or, for a role `models` does not set, in the profile's routes.
547
+ export function personMayNeedOpenCode(
548
+ person: Record<string, unknown>,
549
+ ): boolean {
550
+ const models = person.models;
551
+ if (models !== undefined && JSON.stringify(models).includes('"opencode:')) {
552
+ return true;
553
+ }
554
+ if (isPlainObject(models) && Array.isArray(models.default)) return false;
555
+ return (profileRouteModels(person.profile) ?? []).some((m) =>
556
+ m.startsWith("opencode:"),
557
+ );
558
+ }
559
+
560
+ // The registered profile's route models, or undefined for one this step does
561
+ // not know (the engine's parser refuses it).
562
+ function profileRouteModels(profile: unknown): string[] | undefined {
563
+ const id = profile ?? FALLBACK_PROFILE_ID;
564
+ if (typeof id !== "string" || !Object.hasOwn(MODEL_PROFILES, id)) {
565
+ return undefined;
566
+ }
567
+ const registered = MODEL_PROFILES[id];
568
+ return registered === undefined ? undefined : profileModels(registered);
569
+ }
570
+
571
+ // The person layer's answer: its `models`, plus the one other way a person
572
+ // layer names a model. A legacy `summary.model` (only "haiku" parses) becomes
573
+ // `models.summary: claude:haiku-latest` when `models.summary` is absent
574
+ // (preflight.ts parseSummaryConfig), so any `summary.model` keeps the CLI.
575
+ //
576
+ // `customAgents` (PR #521 R003): a custom agents_dir's frontmatter outranks
577
+ // the profile's seat (routing.ts resolveStepChoices), and this step cannot
578
+ // read those prompts, so with no `models.default` the CLI stays required.
579
+ export function personMayNeedClaude(
580
+ person: Record<string, unknown>,
581
+ context: { customAgents?: boolean } = {},
582
+ ): boolean {
583
+ if (isPlainObject(person.summary) && "model" in person.summary) return true;
584
+ const coversEveryRole =
585
+ isPlainObject(person.models) && Array.isArray(person.models.default);
586
+ if (context.customAgents === true && !coversEveryRole) return true;
587
+ return personModelsMayNeedClaude(person.models, person.profile);
588
+ }
589
+
590
+ // Whether a custom agents_dir may apply to this run: named in the platform's
591
+ // person or repo layer, in the checkout's own repo config (read raw, since this
592
+ // step runs before `bun install`), or in the AGENTS_DIR env. A repo config this
593
+ // step cannot read counts as one: the answer must never be a wrong `false`.
594
+ export function customAgentsMayApply(
595
+ person: Record<string, unknown>,
596
+ repo: Record<string, unknown>,
597
+ env: Readonly<Record<string, string | undefined>>,
598
+ ): boolean {
599
+ if ("agents_dir" in person || "agents_dir" in repo) return true;
600
+ if ((readBrandEnv(env, "AGENTS_DIR")?.value ?? "") !== "") return true;
601
+ const workspace = env.GITHUB_WORKSPACE ?? "";
602
+ if (workspace === "") return false;
603
+ for (const dir of [".crysk", ".prhero"]) {
604
+ const file = path.join(workspace, dir, "config.json");
605
+ if (!existsSync(file)) continue;
606
+ try {
607
+ const parsed: unknown = JSON.parse(readFileSync(file, "utf8"));
608
+ return !isPlainObject(parsed) || "agents_dir" in parsed;
609
+ } catch {
610
+ return true;
611
+ }
612
+ }
613
+ return false;
614
+ }
615
+
468
616
  // Appends to the file the runner named in GITHUB_OUTPUT. Unset or empty is an
469
617
  // error, not a no-op: see runPlatformConnect.
470
618
  export function appendGithubOutput(