@intentius/chant 0.124.76 → 0.124.78

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 (212) hide show
  1. package/dist/__fixtures__/agent-guard/plywood.d.ts +8 -0
  2. package/dist/__fixtures__/agent-guard/plywood.d.ts.map +1 -0
  3. package/dist/__fixtures__/agent-guard/plywood.js +19 -0
  4. package/dist/__fixtures__/agent-guard/terragucci.d.ts +8 -0
  5. package/dist/__fixtures__/agent-guard/terragucci.d.ts.map +1 -0
  6. package/dist/__fixtures__/agent-guard/terragucci.js +30 -0
  7. package/dist/__fixtures__/gated-ledger.d.ts +10 -0
  8. package/dist/__fixtures__/gated-ledger.d.ts.map +1 -0
  9. package/dist/__fixtures__/gated-ledger.js +51 -0
  10. package/dist/__fixtures__/notify-messages/events.d.ts +9 -0
  11. package/dist/__fixtures__/notify-messages/events.d.ts.map +1 -0
  12. package/dist/__fixtures__/notify-messages/events.js +21 -0
  13. package/dist/__fixtures__/product-words.d.ts +14 -0
  14. package/dist/__fixtures__/product-words.d.ts.map +1 -1
  15. package/dist/__fixtures__/product-words.js +11 -0
  16. package/dist/__fixtures__/terragucci-notify.d.ts +66 -0
  17. package/dist/__fixtures__/terragucci-notify.d.ts.map +1 -0
  18. package/dist/__fixtures__/terragucci-notify.js +133 -0
  19. package/dist/action-pins.d.ts +8 -0
  20. package/dist/action-pins.d.ts.map +1 -1
  21. package/dist/action-pins.js +9 -0
  22. package/dist/agent-guard.d.ts +143 -0
  23. package/dist/agent-guard.d.ts.map +1 -0
  24. package/dist/agent-guard.js +455 -0
  25. package/dist/ci-commit.d.ts +120 -5
  26. package/dist/ci-commit.d.ts.map +1 -1
  27. package/dist/ci-commit.js +214 -23
  28. package/dist/ci-own-jobs.d.ts +81 -0
  29. package/dist/ci-own-jobs.d.ts.map +1 -0
  30. package/dist/ci-own-jobs.js +233 -0
  31. package/dist/ci-product-words.d.ts +18 -4
  32. package/dist/ci-product-words.d.ts.map +1 -1
  33. package/dist/ci-product-words.js +26 -6
  34. package/dist/ci-product.d.ts +40 -1
  35. package/dist/ci-product.d.ts.map +1 -1
  36. package/dist/ci-product.js +40 -1
  37. package/dist/cli/handlers/approve.d.ts +5 -1
  38. package/dist/cli/handlers/approve.d.ts.map +1 -1
  39. package/dist/cli/handlers/approve.js +17 -11
  40. package/dist/cli/handlers/build.d.ts +1 -1
  41. package/dist/cli/handlers/build.d.ts.map +1 -1
  42. package/dist/cli/handlers/build.js +88 -2
  43. package/dist/cli/handlers/commit.d.ts.map +1 -1
  44. package/dist/cli/handlers/commit.js +3 -1
  45. package/dist/cli/handlers/notify-relay.d.ts +18 -1
  46. package/dist/cli/handlers/notify-relay.d.ts.map +1 -1
  47. package/dist/cli/handlers/notify-relay.js +90 -14
  48. package/dist/cli/handlers/pr.d.ts.map +1 -1
  49. package/dist/cli/handlers/pr.js +3 -1
  50. package/dist/cli/handlers/release.d.ts +5 -1
  51. package/dist/cli/handlers/release.d.ts.map +1 -1
  52. package/dist/cli/handlers/release.js +21 -9
  53. package/dist/cli/handlers/run-generate.d.ts.map +1 -1
  54. package/dist/cli/handlers/run-generate.js +2 -1
  55. package/dist/cli/main.d.ts.map +1 -1
  56. package/dist/cli/main.js +21 -1
  57. package/dist/cli/parse-args.d.ts.map +1 -1
  58. package/dist/cli/parse-args.js +60 -4
  59. package/dist/cli/registry.d.ts +16 -0
  60. package/dist/cli/registry.d.ts.map +1 -1
  61. package/dist/components/pr-env-components.d.ts +65 -0
  62. package/dist/components/pr-env-components.d.ts.map +1 -0
  63. package/dist/components/pr-env-components.js +162 -0
  64. package/dist/components/pr-policy.d.ts +15 -0
  65. package/dist/components/pr-policy.d.ts.map +1 -1
  66. package/dist/components/pr-policy.js +24 -1
  67. package/dist/components/release-compile.d.ts +15 -3
  68. package/dist/components/release-compile.d.ts.map +1 -1
  69. package/dist/components/release-compile.js +66 -15
  70. package/dist/config.d.ts +14 -8
  71. package/dist/config.d.ts.map +1 -1
  72. package/dist/config.js +16 -1
  73. package/dist/gate-policy.d.ts +14 -1
  74. package/dist/gate-policy.d.ts.map +1 -1
  75. package/dist/gate-policy.js +16 -6
  76. package/dist/lexicon.d.ts +25 -1
  77. package/dist/lexicon.d.ts.map +1 -1
  78. package/dist/lexicon.js +10 -0
  79. package/dist/lifecycle/gated-op.d.ts +95 -0
  80. package/dist/lifecycle/gated-op.d.ts.map +1 -0
  81. package/dist/lifecycle/gated-op.js +96 -0
  82. package/dist/lifecycle/git.d.ts.map +1 -1
  83. package/dist/lifecycle/git.js +83 -2
  84. package/dist/lifecycle/release-images.d.ts +77 -3
  85. package/dist/lifecycle/release-images.d.ts.map +1 -1
  86. package/dist/lifecycle/release-images.js +132 -6
  87. package/dist/notify-product.d.ts +11 -2
  88. package/dist/notify-product.d.ts.map +1 -1
  89. package/dist/notify-product.js +16 -4
  90. package/dist/notify-relay-repo.d.ts +131 -0
  91. package/dist/notify-relay-repo.d.ts.map +1 -0
  92. package/dist/notify-relay-repo.js +202 -0
  93. package/dist/notify-relay.d.ts +25 -1
  94. package/dist/notify-relay.d.ts.map +1 -1
  95. package/dist/notify-relay.js +27 -2
  96. package/dist/notify.d.ts +36 -4
  97. package/dist/notify.d.ts.map +1 -1
  98. package/dist/notify.js +31 -21
  99. package/dist/notify.schema.json +1 -0
  100. package/dist/op/activities/review-product.d.ts +111 -0
  101. package/dist/op/activities/review-product.d.ts.map +1 -0
  102. package/dist/op/activities/review-product.js +116 -0
  103. package/dist/op/op-waves-capability.js +2 -2
  104. package/dist/policy-engine.d.ts +95 -0
  105. package/dist/policy-engine.d.ts.map +1 -0
  106. package/dist/policy-engine.js +157 -0
  107. package/dist/pr-forge-reads.d.ts +124 -0
  108. package/dist/pr-forge-reads.d.ts.map +1 -0
  109. package/dist/pr-forge-reads.js +322 -0
  110. package/dist/pr-forge.d.ts +14 -0
  111. package/dist/pr-forge.d.ts.map +1 -1
  112. package/dist/pr-forge.js +21 -13
  113. package/dist/pr-plan-note.d.ts +293 -0
  114. package/dist/pr-plan-note.d.ts.map +1 -0
  115. package/dist/pr-plan-note.js +362 -0
  116. package/dist/release-floors.d.ts +9 -1
  117. package/dist/release-floors.d.ts.map +1 -1
  118. package/dist/release-floors.js +9 -1
  119. package/dist/state-export.d.ts +126 -0
  120. package/dist/state-export.d.ts.map +1 -0
  121. package/dist/state-export.js +156 -0
  122. package/dist/state-migration.d.ts +148 -0
  123. package/dist/state-migration.d.ts.map +1 -0
  124. package/dist/state-migration.js +146 -0
  125. package/dist/state-unlock.d.ts +167 -0
  126. package/dist/state-unlock.d.ts.map +1 -0
  127. package/dist/state-unlock.js +196 -0
  128. package/dist/wave-runtime.mjs +332 -332
  129. package/dist/workspace/declaration.schema.json +1 -1
  130. package/dist/workspace/release-workflow.d.ts +10 -0
  131. package/dist/workspace/release-workflow.d.ts.map +1 -1
  132. package/dist/workspace/release-workflow.js +10 -4
  133. package/package.json +1 -1
  134. package/src/__fixtures__/agent-guard/plywood.ts +27 -0
  135. package/src/__fixtures__/agent-guard/terragucci.ts +39 -0
  136. package/src/__fixtures__/gated-ledger.ts +58 -0
  137. package/src/__fixtures__/notify-messages/events.ts +23 -0
  138. package/src/__fixtures__/notify-messages/messages.json +50 -0
  139. package/src/__fixtures__/pr-loop/terragucci-plan.note.golden.md +53 -0
  140. package/src/__fixtures__/pr-loop/terragucci-plan.note.json +83 -0
  141. package/src/__fixtures__/product-words.ts +12 -0
  142. package/src/__fixtures__/review-agent/terragucci-review.json +94 -0
  143. package/src/__fixtures__/terragucci-notify.ts +170 -0
  144. package/src/action-pins.test.ts +12 -1
  145. package/src/action-pins.ts +11 -0
  146. package/src/agent-guard.test.ts +319 -0
  147. package/src/agent-guard.ts +547 -0
  148. package/src/ci-commit.test.ts +213 -2
  149. package/src/ci-commit.ts +258 -27
  150. package/src/ci-own-jobs.test.ts +108 -0
  151. package/src/ci-own-jobs.ts +252 -0
  152. package/src/ci-product-words.ts +35 -7
  153. package/src/ci-product.ts +81 -1
  154. package/src/cli/handlers/approve-cwd.test.ts +112 -0
  155. package/src/cli/handlers/approve.ts +22 -11
  156. package/src/cli/handlers/build-own-jobs.test.ts +80 -0
  157. package/src/cli/handlers/build-pr-loop-control.test.ts +87 -0
  158. package/src/cli/handlers/build-run-group.test.ts +59 -0
  159. package/src/cli/handlers/build.ts +83 -3
  160. package/src/cli/handlers/commit.ts +3 -0
  161. package/src/cli/handlers/notify-relay.ts +99 -17
  162. package/src/cli/handlers/pr.ts +3 -1
  163. package/src/cli/handlers/release.ts +20 -10
  164. package/src/cli/handlers/run-generate.ts +2 -1
  165. package/src/cli/main.ts +21 -1
  166. package/src/cli/parse-args.ts +45 -3
  167. package/src/cli/registry.ts +16 -0
  168. package/src/components/pr-env-components.test.ts +137 -0
  169. package/src/components/pr-env-components.ts +213 -0
  170. package/src/components/pr-policy.test.ts +41 -1
  171. package/src/components/pr-policy.ts +25 -1
  172. package/src/components/release-compile.test.ts +47 -0
  173. package/src/components/release-compile.ts +70 -13
  174. package/src/config.ts +23 -3
  175. package/src/gate-policy-bundle.test.ts +14 -0
  176. package/src/gate-policy.test.ts +7 -0
  177. package/src/gate-policy.ts +22 -7
  178. package/src/lexicon.ts +28 -1
  179. package/src/lifecycle/gated-op.ts +158 -0
  180. package/src/lifecycle/git.test.ts +92 -0
  181. package/src/lifecycle/git.ts +71 -2
  182. package/src/lifecycle/release-images-dockerfile.test.ts +221 -0
  183. package/src/lifecycle/release-images.ts +167 -8
  184. package/src/notify-message-words.test.ts +186 -0
  185. package/src/notify-product.ts +17 -4
  186. package/src/notify-relay-repo.test.ts +284 -0
  187. package/src/notify-relay-repo.ts +234 -0
  188. package/src/notify-relay.test.ts +12 -0
  189. package/src/notify-relay.ts +29 -3
  190. package/src/notify.schema.json +1 -0
  191. package/src/notify.ts +59 -19
  192. package/src/op/activities/review-product.test.ts +116 -0
  193. package/src/op/activities/review-product.ts +200 -0
  194. package/src/op/op-waves-capability.ts +2 -2
  195. package/src/op/schedule-overdue.test.ts +26 -0
  196. package/src/policy-engine.test.ts +162 -0
  197. package/src/policy-engine.ts +200 -0
  198. package/src/pr-forge-reads.test.ts +285 -0
  199. package/src/pr-forge-reads.ts +424 -0
  200. package/src/pr-forge.ts +30 -16
  201. package/src/pr-plan-note.test.ts +105 -0
  202. package/src/pr-plan-note.ts +521 -0
  203. package/src/release-floors.ts +13 -1
  204. package/src/state-export.test.ts +124 -0
  205. package/src/state-export.ts +227 -0
  206. package/src/state-migration.test.ts +133 -0
  207. package/src/state-migration.ts +236 -0
  208. package/src/state-unlock.test.ts +165 -0
  209. package/src/state-unlock.ts +293 -0
  210. package/src/workspace/declaration.schema.json +1 -1
  211. package/src/workspace/release-workflow.test.ts +34 -0
  212. package/src/workspace/release-workflow.ts +19 -4
@@ -99,7 +99,8 @@ async function runOpWavesGenerate(ctx: CommandContext, provider: string, specPat
99
99
  console.error(formatError({ message: err instanceof Error ? err.message : String(err) }));
100
100
  return 1;
101
101
  }
102
- const result = await generateOpWavesPipeline(spec, provider, { ...options, specFile: specPath.replace(/^\.\//, "") });
102
+ // #4771: --run-group, or the spec's options.runGroup, gives each Forgejo workflow a run group.
103
+ const result = await generateOpWavesPipeline(spec, provider, { ...options, ...(args.runGroup ? { runGroup: args.runGroup } : {}), specFile: specPath.replace(/^\.\//, "") });
103
104
  if (!result.success) {
104
105
  console.error(formatError({ message: result.error ?? "Failed to generate the Op waves pipeline" }));
105
106
  return 1;
package/src/cli/main.ts CHANGED
@@ -737,7 +737,12 @@ Ops:
737
737
  CHANT_RELAY_KEY, SLACK_SIGNING_SECRET,
738
738
  TEAMS_WEBHOOK_SECRET, CHANT_RELAY_PRINCIPAL,
739
739
  CHANT_RELAY_MAX_AGE, CHANT_RELAY_HOST (default
740
- 127.0.0.1) and CHANT_RELAY_PORT (default 8787)
740
+ 127.0.0.1, or --host) and CHANT_RELAY_PORT (default
741
+ 8787, or --port). --repo <url> --token-env <VAR>
742
+ (#4759) refuses a token that is more than
743
+ approve-only and clones the repo's default branch
744
+ itself, the token passed to git as a header only;
745
+ --forge names the forge off github.com and gitlab.com
741
746
 
742
747
  lock <target>... Lock targets for a pull request on chant/lifecycle
743
748
  (--pr <n>, --stage plan|apply|lock, --head <sha>,
@@ -1519,6 +1524,21 @@ Options:
1519
1524
  { ops, options }
1520
1525
  --promote-to <env> (build --components --generate) Add a job that
1521
1526
  promotes the deployed releases to <env>
1527
+ --own-jobs <file> (build --components --generate) Add the jobs of a
1528
+ YAML file, in the forge's syntax, after chant's; a
1529
+ name chant's file has is refused
1530
+ --keep-own-jobs (build --components --generate) Keep the jobs of the
1531
+ old --output file that run neither chant nor the
1532
+ product's bin
1533
+ --run-group <key> (build --components --generate, run --generate with
1534
+ an op waves spec) Give a Forgejo workflow a run
1535
+ group: commit or pull-request. GitHub and GitLab
1536
+ files are unchanged
1537
+ --control <repo> (build --pr-loop) The control repo: the note job
1538
+ reads its records for the cross-stack edges, and an
1539
+ apply on merge tells it. With --control-setting
1540
+ <key>=<value> (repeatable) for op, member,
1541
+ readTokenSecret and the other control settings
1522
1542
  --no-release-record Skip auto-emitting a release-ledger record after a
1523
1543
  successful \`run --components\` deploy (default: on;
1524
1544
  also settable via chant.config.ts's
@@ -814,9 +814,23 @@ export function parseArgs(args: string[]): ParsedArgs {
814
814
  result.ciSecrets = args[++i];
815
815
  if (!result.ciSecrets || result.ciSecrets.startsWith("-")) throw new Error("--secrets needs the host's secrets file, a dotenv or JSON file only its owner can read: --secrets <file>");
816
816
  } else if (arg === "--repo") {
817
- // `chant ci serve --repo <owner/repo>` (#4095): the repository whose pull requests and target branch it polls.
818
- result.ciRepo = args[++i];
819
- if (!result.ciRepo || !/^[^/\s-][^/\s]*\/[^/\s]+$/.test(result.ciRepo)) throw new Error("--repo needs the repository as owner/name: --repo <owner/repo>");
817
+ const value = args[++i];
818
+ if (value && /^https?:\/\//.test(value)) {
819
+ // `chant notify relay --repo <url>` (#4759): the repository the relay clones.
820
+ result.repoUrl = value;
821
+ } else {
822
+ // `chant ci serve --repo <owner/repo>` (#4095): the repository whose pull requests and target branch it polls.
823
+ result.ciRepo = value;
824
+ if (!result.ciRepo || !/^[^/\s-][^/\s]*\/[^/\s]+$/.test(result.ciRepo)) throw new Error("--repo needs the repository as owner/name, or for chant notify relay its https clone address: --repo <owner/repo>, --repo <url>");
825
+ }
826
+ } else if (arg === "--token-env") {
827
+ // `chant notify relay --token-env <VAR>` (#4759): the variable holding the relay's approve-only token.
828
+ result.tokenEnv = args[++i];
829
+ if (!result.tokenEnv || !/^[A-Za-z_][A-Za-z0-9_]*$/.test(result.tokenEnv)) throw new Error("--token-env needs a variable's name: --token-env <VAR>");
830
+ } else if (arg === "--host") {
831
+ // `chant notify relay --host <address>` (#4759): where the relay listens; 0.0.0.0 for every interface.
832
+ result.host = args[++i];
833
+ if (!result.host || result.host.startsWith("-")) throw new Error("--host needs an address to listen on, such as 0.0.0.0: --host <address>");
820
834
  } else if (arg === "--run-index-limit") {
821
835
  // `chant ci run --run-index-limit <n>` (#4098): how many runs the index at the report URL keeps.
822
836
  const n = Number(args[++i]);
@@ -868,6 +882,30 @@ export function parseArgs(args: string[]): ParsedArgs {
868
882
  if (!result.by || result.by.startsWith("-")) throw new Error("--by needs the reviewer: --by <principal>");
869
883
  // Repeatable for `workspace points answer` (#2739): each person who answered.
870
884
  (result.bys ??= []).push(result.by);
885
+ } else if (arg === "--own-jobs") {
886
+ // `chant build --components --generate <forge> --own-jobs <file>` (#4769).
887
+ const value = args[++i];
888
+ if (value === undefined || value.startsWith("-")) throw new Error("--own-jobs needs the YAML file of your own jobs: --own-jobs <file>");
889
+ result.ownJobs = value;
890
+ } else if (arg === "--keep-own-jobs") {
891
+ // `chant build --components --generate <forge> --keep-own-jobs` (#4769).
892
+ result.keepOwnJobs = true;
893
+ } else if (arg === "--run-group") {
894
+ // `chant build --components --generate forgejo --run-group <key>`, and `chant run --generate` for op waves (#4771).
895
+ const value = args[++i];
896
+ if (value !== "commit" && value !== "pull-request") throw new Error(`--run-group takes commit or pull-request: got "${value ?? ""}"`);
897
+ result.runGroup = value;
898
+ } else if (arg === "--control") {
899
+ // `chant build --pr-loop --control <repo>` (#4772).
900
+ const value = args[++i];
901
+ if (value === undefined || value.startsWith("-")) throw new Error("--control needs the control repo, such as github.com/acme/control");
902
+ result.controlRepo = value;
903
+ } else if (arg === "--control-setting") {
904
+ // `chant build --pr-loop --control-setting <key>=<value>` (#4772), repeatable.
905
+ const raw = args[++i] ?? "";
906
+ const m = /^(op|member|readTokenSecret|tokenSecret|forge|ref|apiBase|sentCheck|markSent)=(.+)$/s.exec(raw);
907
+ if (!m) throw new Error(`--control-setting needs <key>=<value>, the key one of op, member, readTokenSecret, tokenSecret, forge, ref, apiBase, sentCheck, markSent: got "${raw}"`);
908
+ (result.controlSettings ??= {})[m[1]!] = m[2]!;
871
909
  } else if (arg === "--stale-notes") {
872
910
  // `chant build --pr-loop --stale-notes` (#4628).
873
911
  result.staleNotes = true;
@@ -1308,6 +1346,10 @@ export function parseArgs(args: string[]): ParsedArgs {
1308
1346
  "--promote-to needs build --components --generate <lexicon>: it adds a promote job to the generated CI pipeline, and only generate mode emits one.",
1309
1347
  );
1310
1348
  }
1349
+ // #4769: own jobs go into the pipeline generate mode writes, and nowhere else.
1350
+ if ((result.ownJobs !== undefined || result.keepOwnJobs) && !(result.command === "build" && result.components && result.generate)) {
1351
+ throw new Error(`${result.ownJobs !== undefined ? "--own-jobs" : "--keep-own-jobs"} needs build --components --generate <forge>: it adds your own jobs to the pipeline file generate mode writes.`);
1352
+ }
1311
1353
 
1312
1354
  return result;
1313
1355
  }
@@ -567,6 +567,16 @@ export interface ParsedArgs {
567
567
  bys?: string[];
568
568
  /** `chant workspace points --open` (#2739): only the questions still open. */
569
569
  open?: boolean;
570
+ /** `chant build --components --generate <forge> --own-jobs <file>` (#4769): a YAML map of jobs, in the forge's syntax, merged after chant's jobs; a name chant's file has is refused. */
571
+ ownJobs?: string;
572
+ /** `chant build --components --generate <forge> --keep-own-jobs` (#4769): the jobs of the old output file that run neither chant nor the product's bin are kept after chant's. */
573
+ keepOwnJobs?: boolean;
574
+ /** `chant build --components --generate forgejo --run-group <key>` and `chant run --generate forgejo --spec <waves> --run-group <key>` (#4771): the Forgejo workflow's run group, `commit` or `pull-request`. */
575
+ runGroup?: "commit" | "pull-request";
576
+ /** `chant build --pr-loop --control <repo>` (#4772): the control repo, as a project key such as github.com/acme/control. The note job reads its records for the cross-stack edges, and an apply on merge tells it. */
577
+ controlRepo?: string;
578
+ /** `chant build --pr-loop --control-setting <key>=<value>` (#4772), repeatable: the rest of the control settings (op, member, readTokenSecret, tokenSecret, forge, ref, apiBase, sentCheck, markSent). */
579
+ controlSettings?: Record<string, string>;
570
580
  /** `chant build --pr-loop --stale-notes` (#4628): the note carries its target commit, and a push to the target branch marks open notes stale. */
571
581
  staleNotes?: boolean;
572
582
  /** `chant components pr-post --stale` (#4628): mark the open pull requests' notes stale after the target branch moved. */
@@ -657,6 +667,12 @@ export interface ParsedArgs {
657
667
  runIndexLimit?: number;
658
668
  /** `chant ci serve --repo <owner/repo>` (#4095): the repository it polls. */
659
669
  ciRepo?: string;
670
+ /** `chant notify relay --repo <url>` (#4759): the https clone address of the repository the relay clones. */
671
+ repoUrl?: string;
672
+ /** `chant notify relay --token-env <VAR>` (#4759): the variable holding the relay's approve-only token. */
673
+ tokenEnv?: string;
674
+ /** `chant notify relay --host <address>` (#4759): where the relay listens. */
675
+ host?: string;
660
676
  /** `chant ci serve --max-jobs <n>` (#4095): how many jobs run at once. Default 1. */
661
677
  maxJobs?: number;
662
678
  /** `chant ci serve --author <login>`, repeatable (#4095): plan only pull requests these logins opened. */
@@ -0,0 +1,137 @@
1
+ /**
2
+ * #4762: a pull request's environment of separately planned components,
3
+ * through prEnvUp and prEnvDown with in-memory ports and a component planner
4
+ * that records what it was asked. No engine, ledger or forge is reached.
5
+ */
6
+ import { describe, expect, test } from "vitest";
7
+ import { componentsPrEnvAdapter, componentsPrEnvTeardown, PR_ENV_COMPONENTS_FLOOR, type PrEnvComponentPlan } from "./pr-env-components";
8
+ import { prEnvDown, prEnvUp, type PrEnvApproval, type PrEnvPorts } from "./pr-env-run";
9
+ import { livePrEnvironments, type PrEnvRecord } from "./pr-environment";
10
+
11
+ const HEAD = "b".repeat(40);
12
+ const NOW = () => new Date("2026-11-08T10:00:00.000Z");
13
+
14
+ function ports(approval: PrEnvApproval, records: PrEnvRecord[] = []): PrEnvPorts & { records: PrEnvRecord[]; digests: string[] } {
15
+ const digests: string[] = [];
16
+ return {
17
+ records,
18
+ digests,
19
+ readRecords: async () => [...records],
20
+ appendRecord: async (r) => void records.push(r),
21
+ decideGate: async (input) => {
22
+ digests.push(input.digest);
23
+ return approval;
24
+ },
25
+ };
26
+ }
27
+
28
+ /** A planner over named components: each plans `changes` creates at `<name>-<env>`, unless told to fail. */
29
+ function planner(opts: { changes?: number; failPlan?: string; failApply?: string } = {}) {
30
+ const applied: string[] = [];
31
+ const planned: string[] = [];
32
+ const plan = (name: string, env: string, destroy = false): PrEnvComponentPlan => {
33
+ planned.push(`${destroy ? "destroy " : ""}${name}`);
34
+ if (name === opts.failPlan) return { component: name, planDigest: "", create: 0, update: 0, replace: 0, delete: 0, error: "plan failed", apply: async () => {} };
35
+ return {
36
+ component: name,
37
+ planDigest: `sha256:${name}${destroy ? "-destroy" : ""}`,
38
+ create: destroy ? 0 : (opts.changes ?? 1),
39
+ update: 0,
40
+ replace: 0,
41
+ delete: destroy ? 1 : 0,
42
+ location: `s3://acme/${name}-${env}.tfstate`,
43
+ apply: async () => {
44
+ if (name === opts.failApply) throw new Error("apply broke");
45
+ applied.push(`${destroy ? "destroy " : ""}${name}`);
46
+ },
47
+ };
48
+ };
49
+ return { applied, planned, plan };
50
+ }
51
+
52
+ const up = (p: PrEnvPorts, adapter: ReturnType<typeof componentsPrEnvAdapter>) => prEnvUp({ pr: 7, head: HEAD, template: "copy", stack: "product", adapter, ports: p, now: NOW });
53
+
54
+ describe("componentsPrEnvAdapter", () => {
55
+ test("plans every component, waits for an approval of the set digest, then applies in order and records where each copy is", async () => {
56
+ const pl = planner();
57
+ const adapter = componentsPrEnvAdapter({ components: async () => ["network", "app"], plan: (n, _i, t) => Promise.resolve(pl.plan(n, t.env)) });
58
+ const waiting = ports({ status: "pending" });
59
+ const first = await up(waiting, adapter);
60
+ expect(first.exit).toBe(3);
61
+ expect(pl.applied).toEqual([]);
62
+ const p = ports({ status: "approved", approvedBy: ["bo"] });
63
+ const second = await up(p, adapter);
64
+ expect(second.exit).toBe(0);
65
+ expect(p.digests).toEqual(waiting.digests);
66
+ expect(pl.applied).toEqual(["network", "app"]);
67
+ expect(p.records[0]).toMatchObject({ components: ["network", "app"], locations: { network: "s3://acme/network-pr-7.tfstate", app: "s3://acme/app-pr-7.tfstate" }, result: "applied" });
68
+ });
69
+
70
+ test("an empty plan applies with no approval, and no components gives the environment nothing", async () => {
71
+ const pl = planner({ changes: 0 });
72
+ const p = ports({ status: "pending" });
73
+ expect((await up(p, componentsPrEnvAdapter({ components: async () => ["app"], plan: (n, _i, t) => Promise.resolve(pl.plan(n, t.env)) }))).exit).toBe(0);
74
+ expect(p.digests).toEqual([]);
75
+ expect(pl.applied).toEqual(["app"]);
76
+ const none = await up(ports({ status: "pending" }), componentsPrEnvAdapter({ components: async () => [], plan: async () => pl.plan("x", "pr-7"), nothing: () => "no root matches" }));
77
+ expect(none.report).toMatchObject({ status: "nothing", message: "no root matches" });
78
+ });
79
+
80
+ test("a component that does not plan stops the environment, and one that does not apply stops the rest", async () => {
81
+ const pl = planner({ failPlan: "app" });
82
+ const failedPlan = await up(ports({ status: "approved" }), componentsPrEnvAdapter({ components: async () => ["network", "app"], plan: (n, _i, t) => Promise.resolve(pl.plan(n, t.env)) }));
83
+ expect(failedPlan.report.status).toBe("plan-failed");
84
+ expect(pl.applied).toEqual([]);
85
+ const pa = planner({ failApply: "network" });
86
+ const p = ports({ status: "approved" });
87
+ const failedApply = await up(p, componentsPrEnvAdapter({ components: async () => ["network", "app"], plan: (n, _i, t) => Promise.resolve(pa.plan(n, t.env)) }));
88
+ expect(failedApply.exit).toBe(1);
89
+ expect(failedApply.report.components.map((c) => c.status)).toEqual(["failed", "not applied"]);
90
+ expect(p.records[0]!.result).toBe("failed");
91
+ });
92
+
93
+ test("a refusal thrown while planning is kept for the caller", async () => {
94
+ const adapter = componentsPrEnvAdapter({ components: async () => ["app"], plan: async () => Promise.reject(new Error("a cloud block")) });
95
+ const r = await up(ports({ status: "approved" }), adapter);
96
+ expect(r.report.status).toBe("plan-failed");
97
+ expect(adapter.refused?.message).toBe("a cloud block");
98
+ });
99
+ });
100
+
101
+ describe("componentsPrEnvTeardown", () => {
102
+ test("destroys the components it owns in reverse apply order, and leaves a hole for a location that is not the environment's", async () => {
103
+ const pl = planner();
104
+ const p = ports({ status: "approved" });
105
+ await up(p, componentsPrEnvAdapter({ components: async () => ["network", "app", "dns"], plan: (n, _i, t) => Promise.resolve(pl.plan(n, t.env)) }));
106
+ const rec = p.records[0]!;
107
+ rec.locations!.dns = "s3://acme/dns.tfstate";
108
+ let released = 0;
109
+ const teardown = componentsPrEnvTeardown({
110
+ name: "product",
111
+ type: "terraform_root",
112
+ owns: (location, env) => location.endsWith(`-${env}.tfstate`),
113
+ checkout: async () => ({ plan: async (n) => pl.plan(n, "pr-7", true), done: () => void released++ }),
114
+ });
115
+ const down = await prEnvDown({ pr: 7, reason: "closed", ports: p, adapters: [teardown], commit: HEAD, now: NOW });
116
+ expect(pl.applied.filter((a) => a.startsWith("destroy"))).toEqual(["destroy app", "destroy network"]);
117
+ expect(released).toBe(1);
118
+ expect(down.report.status).toBe("incomplete");
119
+ expect(livePrEnvironments(p.records).get("pr-7")?.teardownIncomplete).toBe(true);
120
+ });
121
+
122
+ test("a destroy that fails is that component's failure alone", async () => {
123
+ const pl = planner({ failApply: "app" });
124
+ const p = ports({ status: "approved" });
125
+ await up(p, componentsPrEnvAdapter({ components: async () => ["network"], plan: (n, _i, t) => Promise.resolve(pl.plan(n, t.env)) }));
126
+ p.records[0]!.components.push("app");
127
+ p.records[0]!.locations!.app = "s3://acme/app-pr-7.tfstate";
128
+ const teardown = componentsPrEnvTeardown({ name: "product", type: "terraform_root", owns: () => true, checkout: async () => ({ plan: async (n) => pl.plan(n, "pr-7", true) }) });
129
+ const down = await prEnvDown({ pr: 7, reason: "closed", ports: p, adapters: [teardown], commit: HEAD, now: NOW });
130
+ expect(pl.applied).toContain("destroy network");
131
+ expect(down.report.status).toBe("incomplete");
132
+ });
133
+
134
+ test("its floor is the next release", () => {
135
+ expect(PR_ENV_COMPONENTS_FLOOR).toBe("0.124.77");
136
+ });
137
+ });
@@ -0,0 +1,213 @@
1
+ /**
2
+ * A pull request's environment made of separately planned components
3
+ * (#4762): the adapter and teardown `prEnvUp`, `prEnvDown` and `prEnvSweep`
4
+ * (./pr-env-run.ts) take, built from one function that plans a component.
5
+ *
6
+ * Engine-neutral. An engine supplies {@link PrEnvComponentPlan}s: a
7
+ * component's plan, what it changes, where its copy lives, and how to apply
8
+ * that plan. The terraform lexicon plans each root's copy under a suffixed
9
+ * state key (`@intentius/chant-lexicon-terraform/pr-env`); a Pulumi review
10
+ * stack, or a database branch per pull request, plans the same way. The
11
+ * rules they share live here:
12
+ *
13
+ * - every component plans before anything applies, and one that does not
14
+ * plan stops the whole environment;
15
+ * - the digest an approval binds is the set digest over each component's
16
+ * plan digest, as a wave's is;
17
+ * - the plans apply in order, and the first that fails stops the rest, with
18
+ * what applied kept on the record so the teardown finds it;
19
+ * - the teardown takes only the components whose recorded location the
20
+ * engine says is the environment's own (`owns`), and destroys them in the
21
+ * reverse of the order they applied in, since a later one may read an
22
+ * earlier one's outputs.
23
+ */
24
+ import { waveSetDigest } from "../gated-waves";
25
+ import type { TeardownOutcome } from "../lexicon";
26
+ import type { PrEnvAdapter, PrEnvPlanned, PrEnvTarget } from "./pr-env-run";
27
+ import type { LivePrEnvironment, PrEnvReportRow, PrEnvTeardownAdapter } from "./pr-environment";
28
+
29
+ /** The first chant with the component adapter and teardown, and the terraform lexicon's copies on them (#4762). */
30
+ export const PR_ENV_COMPONENTS_FLOOR = "0.124.77";
31
+
32
+ /** One component's plan, as an engine answers it. */
33
+ export interface PrEnvComponentPlan {
34
+ component: string;
35
+ /** The plan's digest, which the environment's set digest covers. Empty when it did not plan. */
36
+ planDigest: string;
37
+ create: number;
38
+ update: number;
39
+ replace: number;
40
+ delete: number;
41
+ /** Where its copy lives, kept on the record for the teardown, such as a suffixed state key. */
42
+ location?: string;
43
+ /** Why it did not plan. */
44
+ error?: string;
45
+ /** Apply exactly this plan. Throws when it fails. */
46
+ apply(): Promise<void>;
47
+ }
48
+
49
+ const changes = (p: PrEnvComponentPlan): number => p.create + p.update + p.replace + p.delete;
50
+
51
+ const rowOf = (p: PrEnvComponentPlan, status: string): PrEnvReportRow => ({
52
+ component: p.component,
53
+ status,
54
+ create: p.create,
55
+ update: p.update,
56
+ replace: p.replace,
57
+ delete: p.delete,
58
+ ...(p.location ? { location: p.location } : {}),
59
+ });
60
+
61
+ const messageOf = (e: unknown): string => (e instanceof Error ? e.message : String(e));
62
+
63
+ export interface PrEnvComponentsInput {
64
+ /** The components of this environment, in apply order. None: the change reaches nothing, and the environment gets nothing. */
65
+ components(target: PrEnvTarget): Promise<string[]>;
66
+ /** Plan one component. An error it answers is a plan that failed; a throw refuses the environment and is kept in `refused`. */
67
+ plan(component: string, index: number, target: PrEnvTarget): Promise<PrEnvComponentPlan>;
68
+ /** What an environment with no components says. */
69
+ nothing?: (target: PrEnvTarget) => string;
70
+ log?: (line: string) => void;
71
+ }
72
+
73
+ /**
74
+ * The adapter `prEnvUp` takes, over a component planner. `refused` holds an
75
+ * error `components` or `plan` threw, which `prEnvUp` reports as a failed
76
+ * plan, so a caller that treats it as a config error can raise it.
77
+ */
78
+ export function componentsPrEnvAdapter(input: PrEnvComponentsInput): PrEnvAdapter & { refused?: Error } {
79
+ const log = input.log ?? (() => {});
80
+ let plans: PrEnvComponentPlan[] = [];
81
+ const adapter: PrEnvAdapter & { refused?: Error } = {
82
+ async plan(target): Promise<PrEnvPlanned> {
83
+ try {
84
+ const names = await input.components(target);
85
+ if (names.length === 0) {
86
+ const message = input.nothing?.(target) ?? `nothing in this change has a copy in ${target.env}`;
87
+ log(message);
88
+ return { digest: "", empty: true, rows: [], message };
89
+ }
90
+ plans = [];
91
+ for (const [i, name] of names.entries()) {
92
+ const p = await input.plan(name, i, target);
93
+ plans.push(p);
94
+ log(p.error ? `${p.component}: ${p.error}` : `${p.component}: ${changes(p)} to change${p.location ? ` at ${p.location}` : ""}`);
95
+ }
96
+ const failed = plans.filter((p) => p.error);
97
+ if (failed.length > 0) {
98
+ const message = `${failed.map((p) => p.component).join(", ")} did not plan, so nothing of ${target.env} applies`;
99
+ return { digest: "", empty: false, failed: true, rows: plans.map((p) => rowOf(p, p.error ? "plan-failed" : "planned")), message };
100
+ }
101
+ const digest = waveSetDigest(plans.map((p) => ({ member: p.component, planDigest: p.planDigest })));
102
+ return { digest, empty: plans.every((p) => changes(p) === 0), rows: plans.map((p) => rowOf(p, "planned")) };
103
+ } catch (e) {
104
+ adapter.refused = e instanceof Error ? e : new Error(String(e));
105
+ throw e;
106
+ }
107
+ },
108
+ async apply() {
109
+ const rows: PrEnvReportRow[] = [];
110
+ let failed: string | undefined;
111
+ for (const p of plans) {
112
+ if (failed) {
113
+ rows.push(rowOf(p, "not applied"));
114
+ continue;
115
+ }
116
+ try {
117
+ await p.apply();
118
+ rows.push(rowOf(p, "applied"));
119
+ log(`applied ${p.component}${p.location ? ` at ${p.location}` : ""}`);
120
+ } catch (e) {
121
+ failed = `${p.component} did not apply: ${messageOf(e)}`;
122
+ rows.push(rowOf(p, "failed"));
123
+ log(`FAILED ${failed}`);
124
+ }
125
+ }
126
+ const locations = Object.fromEntries(plans.filter((p) => p.location).map((p) => [p.component, p.location!]));
127
+ return { status: failed ? "failed" : "applied", rows, ...(Object.keys(locations).length ? { locations } : {}), ...(failed ? { message: failed } : {}) };
128
+ },
129
+ };
130
+ return adapter;
131
+ }
132
+
133
+ /** What a teardown plans the destroys with: the code the environment applied from, prepared, and released after. */
134
+ export interface PrEnvTeardownCheckout {
135
+ /** Plan one component's destroy. An error it answers, or a throw, fails that component alone. */
136
+ plan(component: string, index: number): Promise<PrEnvComponentPlan>;
137
+ /** Release what the checkout holds, after every destroy. */
138
+ done?(): void;
139
+ }
140
+
141
+ export interface PrEnvComponentsTeardownInput {
142
+ /** The name outcomes are attributed to. */
143
+ name: string;
144
+ /** The lexicons this teardown stands in for. */
145
+ lexicons?: string[];
146
+ /** The candidates' resource type, such as `terraform_root`. */
147
+ type: string;
148
+ /** Whether a component's recorded location is this environment's own copy and not the component's own state. */
149
+ owns(location: string, env: string): boolean;
150
+ /** Prepare the code the environment last applied from (`live.commit`). */
151
+ checkout(live: LivePrEnvironment): Promise<PrEnvTeardownCheckout>;
152
+ log?: (line: string) => void;
153
+ }
154
+
155
+ /**
156
+ * The teardown `prEnvDown` and `prEnvSweep` take, over a component planner.
157
+ * A component whose recorded location the engine does not own, or that has
158
+ * no location recorded, is a hole, so nothing at a component's own state is
159
+ * destroyed.
160
+ */
161
+ export function componentsPrEnvTeardown(input: PrEnvComponentsTeardownInput): PrEnvTeardownAdapter {
162
+ const log = input.log ?? (() => {});
163
+ return {
164
+ name: input.name,
165
+ ...(input.lexicons ? { lexicons: input.lexicons } : {}),
166
+ async owned({ live, marker }) {
167
+ const candidates = [];
168
+ const holes = [];
169
+ for (const name of live.components) {
170
+ const location = live.locations?.[name] ?? "";
171
+ if (location && input.owns(location, live.env)) candidates.push({ name, type: input.type, physicalId: location, marker: { stack: marker.stack, env: live.env } });
172
+ else holes.push({ name, type: input.type, reason: "read-failed" as const, detail: location ? `its recorded location ${location} is not ${live.env}'s own copy` : `the location of its copy in ${live.env} is not recorded` });
173
+ }
174
+ return { candidates, holes };
175
+ },
176
+ async teardown({ candidates, live }) {
177
+ const outcomes: TeardownOutcome[] = [];
178
+ const order = [...live.components].reverse();
179
+ const rank = (name: string): number => (order.includes(name) ? order.indexOf(name) : order.length);
180
+ const checkout = await input.checkout(live);
181
+ try {
182
+ for (const [i, c] of [...candidates].sort((a, b) => rank(a.name) - rank(b.name)).entries()) {
183
+ const failed = (detail: string): void => {
184
+ log(`FAILED ${c.name}: ${detail}`);
185
+ outcomes.push({ name: c.name, type: input.type, outcome: "failed", detail });
186
+ };
187
+ let p: PrEnvComponentPlan;
188
+ try {
189
+ p = await checkout.plan(c.name, i);
190
+ } catch (e) {
191
+ failed(messageOf(e));
192
+ continue;
193
+ }
194
+ if (p.error) {
195
+ failed(p.error);
196
+ continue;
197
+ }
198
+ try {
199
+ await p.apply();
200
+ } catch (e) {
201
+ failed(`the destroy did not apply: ${messageOf(e)}`);
202
+ continue;
203
+ }
204
+ log(`destroyed ${c.name}'s copy${p.location ? ` at ${p.location}` : ""}`);
205
+ outcomes.push({ name: c.name, type: input.type, outcome: "deleted", detail: `destroyed${p.location ? ` at ${p.location}` : ""}, plan ${p.planDigest}` });
206
+ }
207
+ } finally {
208
+ checkout.done?.();
209
+ }
210
+ return { outcomes };
211
+ },
212
+ };
213
+ }
@@ -6,6 +6,7 @@
6
6
  */
7
7
  import { afterAll, describe, expect, test } from "vitest";
8
8
  import { execFileSync } from "node:child_process";
9
+ import { createHash } from "node:crypto";
9
10
  import { mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, writeFileSync } from "node:fs";
10
11
  import { join } from "node:path";
11
12
  import { tmpdir } from "node:os";
@@ -16,7 +17,7 @@ import { overrideDigest } from "../override-gate";
16
17
  import type { GatePolicyExec } from "../gate-policy";
17
18
  import type { FoundComment, MarkerMatch, PrForge } from "../pr-forge";
18
19
  import { reviewMarker, reviewNoteBody } from "../op/activities/review-agent";
19
- import { decidePrPolicyOverride, policyReviewOf, prLoopPolicyAt, prPolicyReport, REVIEW_NOTE_AUTHORS, runPrPolicy, spendPrPolicyOverride, type PrPolicyInput } from "./pr-policy";
20
+ import { decidePrPolicyOverride, policyReviewOf, prLoopPolicyAt, preparePrPolicyEngine, prPolicyReport, REVIEW_NOTE_AUTHORS, runPrPolicy, spendPrPolicyOverride, type PrPolicyInput } from "./pr-policy";
20
21
 
21
22
  const scratch: string[] = [];
22
23
  afterAll(() => {
@@ -206,3 +207,42 @@ describe("input.review: a policy reads the review's risk (#4326)", () => {
206
207
  expect(runPrPolicy(read, { ...input(base), review: await policyReviewOf(forgeWith([]), 7) }, denyHighRisk)!.result).toBe("passed");
207
208
  });
208
209
  });
210
+
211
+ describe("ci.policy.install: the engine at chant's pinned release (#4774)", () => {
212
+ const pin = (body: Uint8Array) => ({
213
+ cache: tmp(),
214
+ platform: "linux-x64" as const,
215
+ version: "1.0.0",
216
+ sha256: { "linux-x64": createHash("sha256").update(body).digest("hex") },
217
+ probe: () => false,
218
+ });
219
+
220
+ test("the engine is installed, and the policy runs it from the cache", async () => {
221
+ const { repo, base } = repoWithPolicy();
222
+ const read = { ...prLoopPolicyAt(repo, base), settings: { engine: "opa" as const, path: "policy", install: true } };
223
+ const urls: string[] = [];
224
+ const body = new Uint8Array(Buffer.from("opa"));
225
+ const prepared = await preparePrPolicyEngine(read, { ...pin(body), download: async (url) => (urls.push(url), body) });
226
+ expect(urls).toEqual(["https://github.com/open-policy-agent/opa/releases/download/v1.0.0/opa_linux_amd64_static"]);
227
+ expect(prepared.binary).toMatch(/opa-1\.0\.0-linux-x64\/opa$/);
228
+ let ran = "";
229
+ const verdict = runPrPolicy(prepared, input(base), (argv) => ((ran = argv[0]!), { status: 0, stdout: opaOut({}), stderr: "" }))!;
230
+ expect(ran).toBe(prepared.binary);
231
+ expect(verdict.result).toBe("passed");
232
+ expect(await preparePrPolicyEngine(prepared, { download: async () => { throw new Error("fetched twice"); } })).toBe(prepared);
233
+ });
234
+
235
+ test("an engine that cannot be installed is an error, which stops the apply", async () => {
236
+ const { repo, base } = repoWithPolicy();
237
+ const read = { ...prLoopPolicyAt(repo, base), settings: { engine: "opa" as const, path: "policy", install: true } };
238
+ const prepared = await preparePrPolicyEngine(read, { ...pin(new Uint8Array([1])), download: async () => new Uint8Array([2]) });
239
+ expect(prepared.error).toMatch(/^ci.policy.install: opa could not be installed: opa 1.0.0 from .* does not match its pinned sha256/);
240
+ expect(runPrPolicy(prepared, input(base), () => ({ status: 0, stdout: opaOut({}), stderr: "" }))).toMatchObject({ result: "error" });
241
+ });
242
+
243
+ test("without install nothing is fetched", async () => {
244
+ const { repo, base } = repoWithPolicy();
245
+ const read = prLoopPolicyAt(repo, base);
246
+ expect(await preparePrPolicyEngine(read, { download: async () => { throw new Error("fetched"); } })).toBe(read);
247
+ });
248
+ });
@@ -29,6 +29,11 @@
29
29
  * per job (`../policy-source.ts`), and the commit its ref named goes on the
30
30
  * verdict and the report, so a decision names the rules it used. A source
31
31
  * that cannot be fetched is an error, and nothing applies.
32
+ *
33
+ * `ci.policy.install` (#4774) fetches conftest or OPA at chant's pinned
34
+ * release when the engine is not on PATH ({@link preparePrPolicyEngine},
35
+ * `../policy-engine.ts`). An engine that cannot be installed is an error,
36
+ * and nothing applies.
32
37
  */
33
38
 
34
39
  import { readDeclaration, WorkspaceReadError } from "../workspace/declaration";
@@ -43,6 +48,7 @@ import {
43
48
  type GatePolicySettings,
44
49
  type GatePolicyVerdict,
45
50
  } from "../gate-policy";
51
+ import { policyEngineBinary, type PolicyEngineBinaryOptions } from "../policy-engine";
46
52
  import type { ChangeSetDocument } from "../change-set";
47
53
  import type { GateLedgerPort } from "../op/gate";
48
54
  import type { PrPolicyReport } from "../pr-loop";
@@ -67,6 +73,24 @@ export interface PrLoopPolicy {
67
73
  error?: string;
68
74
  /** A line for the log when no policy runs because the base has no declaration, or could not be read. */
69
75
  note?: string;
76
+ /** The engine's executable with `ci.policy.install`, set by {@link preparePrPolicyEngine}. */
77
+ binary?: string;
78
+ }
79
+
80
+ /**
81
+ * With `ci.policy.install` (#4774): the read with the engine's executable,
82
+ * the one on PATH or chant's pinned release, installed. An engine that
83
+ * cannot be installed makes every run an error. Any other read is returned
84
+ * as it is.
85
+ */
86
+ export async function preparePrPolicyEngine(read: PrLoopPolicy, options: PolicyEngineBinaryOptions = {}): Promise<PrLoopPolicy> {
87
+ if (!read.settings?.install || read.settings.command || read.error !== undefined || read.binary) return read;
88
+ const engine = read.settings.engine ?? "conftest";
89
+ try {
90
+ return { ...read, binary: await policyEngineBinary(engine, options) };
91
+ } catch (err) {
92
+ return { ...read, error: `ci.policy.install: ${engine} could not be installed: ${err instanceof Error ? err.message : String(err)}` };
93
+ }
70
94
  }
71
95
 
72
96
  /** Whether the working tree's declaration names a `ci.policy`. */
@@ -192,7 +216,7 @@ export function runPrPolicy(read: PrLoopPolicy, input: PrPolicyInput, exec?: Gat
192
216
  return { result: "error", engine, denials: [], rules: [], holds: [], warnings: [], error: read.source ? `the policy directory ${path} does not exist in ${where}` : exported.error, ...source };
193
217
  }
194
218
  try {
195
- return { ...checkGatePolicy({ settings: read.settings, input, policyDir: exported.dir, cwd: process.cwd(), ...(exec ? { exec } : {}) }), ...source };
219
+ return { ...checkGatePolicy({ settings: read.settings, input, policyDir: exported.dir, cwd: process.cwd(), ...(exec ? { exec } : {}), ...(read.binary ? { binary: read.binary } : {}) }), ...source };
196
220
  } finally {
197
221
  exported.cleanup();
198
222
  }