@intentius/chant 0.124.85 → 0.124.87

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 (286) hide show
  1. package/dist/bundle-check.d.ts +102 -0
  2. package/dist/bundle-check.d.ts.map +1 -0
  3. package/dist/bundle-check.js +310 -0
  4. package/dist/chant-install.d.ts +100 -0
  5. package/dist/chant-install.d.ts.map +1 -0
  6. package/dist/chant-install.js +158 -0
  7. package/dist/ci-commit.d.ts +19 -6
  8. package/dist/ci-commit.d.ts.map +1 -1
  9. package/dist/ci-commit.js +23 -4
  10. package/dist/ci-evidence-jobs.d.ts +78 -0
  11. package/dist/ci-evidence-jobs.d.ts.map +1 -0
  12. package/dist/ci-evidence-jobs.js +85 -0
  13. package/dist/ci-image.d.ts +274 -0
  14. package/dist/ci-image.d.ts.map +1 -0
  15. package/dist/ci-image.js +635 -0
  16. package/dist/ci-login.d.ts +150 -0
  17. package/dist/ci-login.d.ts.map +1 -0
  18. package/dist/ci-login.js +240 -0
  19. package/dist/ci-product.d.ts +16 -0
  20. package/dist/ci-product.d.ts.map +1 -1
  21. package/dist/ci-product.js +17 -0
  22. package/dist/cli/commands/import-index.js +2 -2
  23. package/dist/cli/handlers/build.d.ts +8 -0
  24. package/dist/cli/handlers/build.d.ts.map +1 -1
  25. package/dist/cli/handlers/build.js +22 -4
  26. package/dist/cli/handlers/ci-check.d.ts.map +1 -1
  27. package/dist/cli/handlers/ci-check.js +61 -2
  28. package/dist/cli/handlers/ci-evidence.d.ts +3 -0
  29. package/dist/cli/handlers/ci-evidence.d.ts.map +1 -0
  30. package/dist/cli/handlers/ci-evidence.js +174 -0
  31. package/dist/cli/handlers/components.js +3 -3
  32. package/dist/cli/handlers/dead-guards.d.ts +3 -0
  33. package/dist/cli/handlers/dead-guards.d.ts.map +1 -0
  34. package/dist/cli/handlers/dead-guards.js +35 -0
  35. package/dist/cli/handlers/graph.js +4 -4
  36. package/dist/cli/handlers/lifecycle.d.ts +2 -1
  37. package/dist/cli/handlers/lifecycle.d.ts.map +1 -1
  38. package/dist/cli/handlers/lifecycle.js +5 -4
  39. package/dist/cli/handlers/local-chant.d.ts +16 -0
  40. package/dist/cli/handlers/local-chant.d.ts.map +1 -0
  41. package/dist/cli/handlers/local-chant.js +109 -0
  42. package/dist/cli/handlers/operator.js +2 -2
  43. package/dist/cli/handlers/search.js +5 -5
  44. package/dist/cli/main.d.ts.map +1 -1
  45. package/dist/cli/main.js +38 -0
  46. package/dist/cli/parse-args.d.ts.map +1 -1
  47. package/dist/cli/parse-args.js +28 -0
  48. package/dist/cli/registry.d.ts +16 -0
  49. package/dist/cli/registry.d.ts.map +1 -1
  50. package/dist/components/check-hooks.d.ts +49 -0
  51. package/dist/components/check-hooks.d.ts.map +1 -1
  52. package/dist/components/check-hooks.js +64 -4
  53. package/dist/components/check-steps.d.ts +17 -0
  54. package/dist/components/check-steps.d.ts.map +1 -1
  55. package/dist/components/check-steps.js +37 -5
  56. package/dist/components/product-pipeline.d.ts +23 -0
  57. package/dist/components/product-pipeline.d.ts.map +1 -1
  58. package/dist/components/product-pipeline.js +6 -2
  59. package/dist/components/product-plan.d.ts +143 -0
  60. package/dist/components/product-plan.d.ts.map +1 -0
  61. package/dist/components/product-plan.js +145 -0
  62. package/dist/components/release-compile.d.ts.map +1 -1
  63. package/dist/components/release-compile.js +2 -2
  64. package/dist/config.d.ts +39 -1
  65. package/dist/config.d.ts.map +1 -1
  66. package/dist/config.js +35 -3
  67. package/dist/dead-guards.d.ts +82 -0
  68. package/dist/dead-guards.d.ts.map +1 -0
  69. package/dist/dead-guards.js +436 -0
  70. package/dist/discovery/files.d.ts +3 -1
  71. package/dist/discovery/files.d.ts.map +1 -1
  72. package/dist/discovery/files.js +5 -3
  73. package/dist/drift-issue.d.ts +152 -0
  74. package/dist/drift-issue.d.ts.map +1 -0
  75. package/dist/drift-issue.js +167 -0
  76. package/dist/floor-table.d.ts +30 -0
  77. package/dist/floor-table.d.ts.map +1 -0
  78. package/dist/floor-table.data.d.ts +3 -0
  79. package/dist/floor-table.data.d.ts.map +1 -0
  80. package/dist/floor-table.data.js +179 -0
  81. package/dist/floor-table.js +56 -0
  82. package/dist/forge-flags.d.ts +90 -0
  83. package/dist/forge-flags.d.ts.map +1 -0
  84. package/dist/forge-flags.js +184 -0
  85. package/dist/lexicon.d.ts +69 -0
  86. package/dist/lexicon.d.ts.map +1 -1
  87. package/dist/lifecycle/gated-op.d.ts +7 -1
  88. package/dist/lifecycle/gated-op.d.ts.map +1 -1
  89. package/dist/lifecycle/gated-op.js +7 -4
  90. package/dist/local-chant.d.ts +98 -0
  91. package/dist/local-chant.d.ts.map +1 -0
  92. package/dist/local-chant.js +307 -0
  93. package/dist/op/activities/predict-behaviour.js +2 -2
  94. package/dist/op/op-waves-evidence-spec.d.ts +13 -0
  95. package/dist/op/op-waves-evidence-spec.d.ts.map +1 -0
  96. package/dist/op/op-waves-evidence-spec.js +21 -0
  97. package/dist/op/op-waves-evidence.d.ts +40 -0
  98. package/dist/op/op-waves-evidence.d.ts.map +1 -0
  99. package/dist/op/op-waves-evidence.js +58 -0
  100. package/dist/op/op-waves-run.d.ts +4 -0
  101. package/dist/op/op-waves-run.d.ts.map +1 -1
  102. package/dist/op/op-waves-run.js +24 -1
  103. package/dist/op/op-waves.d.ts +17 -1
  104. package/dist/op/op-waves.d.ts.map +1 -1
  105. package/dist/op/op-waves.js +23 -0
  106. package/dist/op/schedule-overdue.d.ts +41 -9
  107. package/dist/op/schedule-overdue.d.ts.map +1 -1
  108. package/dist/op/schedule-overdue.js +154 -25
  109. package/dist/plan-summary.d.ts +25 -0
  110. package/dist/plan-summary.d.ts.map +1 -1
  111. package/dist/plan-summary.js +31 -0
  112. package/dist/pr-plan-note.d.ts +35 -0
  113. package/dist/pr-plan-note.d.ts.map +1 -1
  114. package/dist/pr-plan-note.js +10 -0
  115. package/dist/pr-post.d.ts +23 -0
  116. package/dist/pr-post.d.ts.map +1 -1
  117. package/dist/pr-post.js +125 -3
  118. package/dist/release-floors.d.ts +33 -1
  119. package/dist/release-floors.d.ts.map +1 -1
  120. package/dist/release-floors.js +33 -1
  121. package/dist/state-migration.d.ts +24 -0
  122. package/dist/state-migration.d.ts.map +1 -1
  123. package/dist/state-migration.js +100 -5
  124. package/dist/wave-runtime.mjs +309 -302
  125. package/dist/workspace/ci-green.d.ts.map +1 -1
  126. package/dist/workspace/ci-green.js +4 -3
  127. package/dist/workspace/lexicon-outputs.d.ts.map +1 -1
  128. package/dist/workspace/lexicon-outputs.js +21 -7
  129. package/dist/workspace/reason-codes.d.ts +3 -0
  130. package/dist/workspace/reason-codes.d.ts.map +1 -1
  131. package/dist/workspace/reason-codes.js +4 -0
  132. package/dist/workspace/ref-gate.schema.json +85 -0
  133. package/dist/workspace/search-contract.d.ts +1 -1
  134. package/dist/workspace/search-contract.d.ts.map +1 -1
  135. package/dist/workspace/trust/check-evidence-report.d.ts +58 -0
  136. package/dist/workspace/trust/check-evidence-report.d.ts.map +1 -0
  137. package/dist/workspace/trust/check-evidence-report.js +109 -0
  138. package/dist/workspace/trust/check-evidence.d.ts +160 -0
  139. package/dist/workspace/trust/check-evidence.d.ts.map +1 -0
  140. package/dist/workspace/trust/check-evidence.js +309 -0
  141. package/dist/workspace/trust/ci-tag-statement.d.ts +2 -2
  142. package/dist/workspace/trust/ci-tag-statement.d.ts.map +1 -1
  143. package/dist/workspace/trust/dsse.d.ts +40 -2
  144. package/dist/workspace/trust/dsse.d.ts.map +1 -1
  145. package/dist/workspace/trust/dsse.js +34 -2
  146. package/dist/workspace/trust/evidence.d.ts +2 -2
  147. package/dist/workspace/trust/evidence.d.ts.map +1 -1
  148. package/dist/workspace/trust/keyless-fixture.d.ts +41 -0
  149. package/dist/workspace/trust/keyless-fixture.d.ts.map +1 -0
  150. package/dist/workspace/trust/keyless-fixture.js +161 -0
  151. package/dist/workspace/trust/keyless.d.ts +202 -0
  152. package/dist/workspace/trust/keyless.d.ts.map +1 -0
  153. package/dist/workspace/trust/keyless.js +488 -0
  154. package/dist/workspace/trust/local-evidence.d.ts +123 -0
  155. package/dist/workspace/trust/local-evidence.d.ts.map +1 -0
  156. package/dist/workspace/trust/local-evidence.js +259 -0
  157. package/dist/workspace/trust/op-run-statement.d.ts +2 -2
  158. package/dist/workspace/trust/op-run-statement.d.ts.map +1 -1
  159. package/dist/workspace/trust/policy.d.ts +140 -1
  160. package/dist/workspace/trust/policy.d.ts.map +1 -1
  161. package/dist/workspace/trust/policy.js +197 -3
  162. package/dist/workspace/trust/ref-gate-cli.d.ts +33 -0
  163. package/dist/workspace/trust/ref-gate-cli.d.ts.map +1 -0
  164. package/dist/workspace/trust/ref-gate-cli.js +234 -0
  165. package/dist/workspace/trust/ref-gate-fixture.d.ts +38 -0
  166. package/dist/workspace/trust/ref-gate-fixture.d.ts.map +1 -0
  167. package/dist/workspace/trust/ref-gate-fixture.js +97 -0
  168. package/dist/workspace/trust/ref-gate.d.ts +187 -0
  169. package/dist/workspace/trust/ref-gate.d.ts.map +1 -0
  170. package/dist/workspace/trust/ref-gate.js +432 -0
  171. package/dist/workspace/trust/run-statement.d.ts +3 -3
  172. package/dist/workspace/trust/run-statement.d.ts.map +1 -1
  173. package/dist/workspace/trust/runner-identity.d.ts +56 -0
  174. package/dist/workspace/trust/runner-identity.d.ts.map +1 -0
  175. package/dist/workspace/trust/runner-identity.js +65 -0
  176. package/dist/workspace/trust/trust-files.d.ts.map +1 -1
  177. package/dist/workspace/trust/trust-files.js +25 -1
  178. package/package.json +1 -1
  179. package/src/bundle-check.test.ts +140 -0
  180. package/src/bundle-check.ts +352 -0
  181. package/src/chant-install.test.ts +112 -0
  182. package/src/chant-install.ts +179 -0
  183. package/src/ci-commit.test.ts +45 -0
  184. package/src/ci-commit.ts +38 -4
  185. package/src/ci-evidence-jobs.ts +105 -0
  186. package/src/ci-image.test.ts +506 -0
  187. package/src/ci-image.ts +822 -0
  188. package/src/ci-login.test.ts +235 -0
  189. package/src/ci-login.ts +307 -0
  190. package/src/ci-product.ts +18 -0
  191. package/src/cli/commands/__fixtures__/source-dir-include/chant.config.json +1 -0
  192. package/src/cli/commands/__fixtures__/source-dir-include/infra.ts +1 -0
  193. package/src/cli/commands/__fixtures__/source-dir-include/scripts/run-on-load.ts +3 -0
  194. package/src/cli/commands/__fixtures__/source-dir-root/chant.config.json +1 -0
  195. package/src/cli/commands/__fixtures__/source-dir-root/infra.test.ts +1 -0
  196. package/src/cli/commands/__fixtures__/source-dir-root/infra.ts +1 -0
  197. package/src/cli/commands/__fixtures__/source-dir-root/scripts/run-on-load.ts +3 -0
  198. package/src/cli/commands/__fixtures__/source-dir-scoped/app.ts +3 -0
  199. package/src/cli/commands/__fixtures__/source-dir-scoped/chant.config.json +1 -0
  200. package/src/cli/commands/__fixtures__/source-dir-scoped/infra/net.ts +1 -0
  201. package/src/cli/commands/__fixtures__/source-dir-scoped/scripts/run-on-load.ts +3 -0
  202. package/src/cli/commands/import-index.ts +2 -2
  203. package/src/cli/handlers/build-source-dir.test.ts +64 -0
  204. package/src/cli/handlers/build.ts +21 -4
  205. package/src/cli/handlers/ci-check.ts +60 -3
  206. package/src/cli/handlers/ci-evidence.ts +167 -0
  207. package/src/cli/handlers/components.ts +3 -3
  208. package/src/cli/handlers/dead-guards.ts +37 -0
  209. package/src/cli/handlers/graph.ts +4 -4
  210. package/src/cli/handlers/lifecycle.ts +5 -4
  211. package/src/cli/handlers/local-chant.ts +106 -0
  212. package/src/cli/handlers/operator.ts +2 -2
  213. package/src/cli/handlers/search.ts +5 -5
  214. package/src/cli/main.test.ts +7 -0
  215. package/src/cli/main.ts +38 -0
  216. package/src/cli/parse-args.ts +21 -0
  217. package/src/cli/registry.ts +16 -0
  218. package/src/components/check-evidence-stage.test.ts +80 -0
  219. package/src/components/check-hooks.ts +84 -4
  220. package/src/components/check-local-evidence-stage.test.ts +141 -0
  221. package/src/components/check-steps.ts +56 -6
  222. package/src/components/product-pipeline.ts +28 -3
  223. package/src/components/product-plan.test.ts +87 -0
  224. package/src/components/product-plan.ts +230 -0
  225. package/src/components/release-compile.ts +2 -2
  226. package/src/config.ts +44 -3
  227. package/src/dead-guards.test.ts +206 -0
  228. package/src/dead-guards.ts +446 -0
  229. package/src/discovery/files.ts +5 -3
  230. package/src/drift-issue-bundle.test.ts +31 -0
  231. package/src/drift-issue.test.ts +206 -0
  232. package/src/drift-issue.ts +274 -0
  233. package/src/fit-note.test.ts +44 -0
  234. package/src/floor-table.data.ts +181 -0
  235. package/src/floor-table.ts +77 -0
  236. package/src/forge-flags.test.ts +110 -0
  237. package/src/forge-flags.ts +208 -0
  238. package/src/gate-decision-bundle.test.ts +1 -0
  239. package/src/lexicon.ts +68 -0
  240. package/src/lifecycle/gated-op.ts +10 -5
  241. package/src/local-chant.e2e.test.ts +94 -0
  242. package/src/local-chant.test.ts +162 -0
  243. package/src/local-chant.ts +341 -0
  244. package/src/op/activities/predict-behaviour.ts +2 -2
  245. package/src/op/op-waves-evidence-spec.ts +24 -0
  246. package/src/op/op-waves-evidence.test.ts +130 -0
  247. package/src/op/op-waves-evidence.ts +58 -0
  248. package/src/op/op-waves-run.ts +25 -1
  249. package/src/op/op-waves.ts +31 -1
  250. package/src/op/schedule-overdue.test.ts +85 -0
  251. package/src/op/schedule-overdue.ts +162 -26
  252. package/src/plan-summary.ts +40 -0
  253. package/src/pr-plan-note.ts +34 -0
  254. package/src/pr-post-plan-note.test.ts +146 -0
  255. package/src/pr-post.ts +127 -3
  256. package/src/release-floors.ts +47 -1
  257. package/src/stage-hooks-bundle.test.ts +1 -0
  258. package/src/state-migration.test.ts +52 -1
  259. package/src/state-migration.ts +107 -5
  260. package/src/workspace/ci-green.ts +4 -3
  261. package/src/workspace/lexicon-outputs.ts +23 -6
  262. package/src/workspace/reason-codes.test.ts +3 -0
  263. package/src/workspace/reason-codes.ts +4 -0
  264. package/src/workspace/ref-gate.schema.json +85 -0
  265. package/src/workspace/trust/check-evidence-report.ts +115 -0
  266. package/src/workspace/trust/check-evidence.test.ts +133 -0
  267. package/src/workspace/trust/check-evidence.ts +327 -0
  268. package/src/workspace/trust/ci-tag-statement.ts +2 -2
  269. package/src/workspace/trust/dsse.ts +48 -4
  270. package/src/workspace/trust/evidence.ts +2 -2
  271. package/src/workspace/trust/keyless-fixture.ts +214 -0
  272. package/src/workspace/trust/keyless.test.ts +220 -0
  273. package/src/workspace/trust/keyless.ts +557 -0
  274. package/src/workspace/trust/local-evidence.test.ts +127 -0
  275. package/src/workspace/trust/local-evidence.ts +268 -0
  276. package/src/workspace/trust/op-run-statement.ts +2 -2
  277. package/src/workspace/trust/policy.ts +263 -3
  278. package/src/workspace/trust/ref-gate-cli.ts +244 -0
  279. package/src/workspace/trust/ref-gate-fixture.ts +125 -0
  280. package/src/workspace/trust/ref-gate.e2e.test.ts +76 -0
  281. package/src/workspace/trust/ref-gate.test.ts +247 -0
  282. package/src/workspace/trust/ref-gate.ts +539 -0
  283. package/src/workspace/trust/run-statement.ts +3 -3
  284. package/src/workspace/trust/runner-identity.ts +92 -0
  285. package/src/workspace/trust/trust-files.test.ts +22 -0
  286. package/src/workspace/trust/trust-files.ts +54 -1
@@ -0,0 +1,112 @@
1
+ /**
2
+ * #4800 — the chant a product is installed with: this package itself, a
3
+ * walk up `node_modules` for terragucci's layout, the argv and PATH a child
4
+ * runs with, and plywood's resolve fallback for a repository with no chant.
5
+ */
6
+
7
+ import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
8
+ import { tmpdir } from "node:os";
9
+ import { delimiter, join } from "node:path";
10
+ import { afterAll, describe, expect, test } from "vitest";
11
+ import { bundledChant, CHANT_SPECIFIER, chantArgv, chantCommand, chantEnv, chantResolveFallback, installedChant } from "./chant-install";
12
+
13
+ const scratch = mkdtempSync(join(tmpdir(), "chant-install-"));
14
+ afterAll(() => rmSync(scratch, { recursive: true, force: true }));
15
+
16
+ /** A package directory with a manifest and, when `bin` is given, the bin file. */
17
+ function pkg(dir: string, manifest: Record<string, unknown>, bin?: string): string {
18
+ mkdirSync(dir, { recursive: true });
19
+ writeFileSync(join(dir, "package.json"), JSON.stringify(manifest));
20
+ if (bin) {
21
+ mkdirSync(join(dir, "bin"), { recursive: true });
22
+ writeFileSync(join(dir, bin), "#!/usr/bin/env node\n");
23
+ }
24
+ return dir;
25
+ }
26
+
27
+ describe("bundledChant", () => {
28
+ test("is the package this module belongs to, with its bin and version", () => {
29
+ const own = bundledChant();
30
+ expect(own).toBeDefined();
31
+ expect(own!.root).toBe(join(import.meta.dirname, ".."));
32
+ expect(own!.bin).toBe(join(own!.root, "bin", "chant"));
33
+ expect(own!.binDir).toBe(join(own!.root, "bin"));
34
+ expect(own!.version).toMatch(/^\d+\.\d+\.\d+/);
35
+ });
36
+ });
37
+
38
+ describe("installedChant", () => {
39
+ const product = join(scratch, "product");
40
+ const root = pkg(join(product, "node_modules", "@intentius", "chant"), { name: "@intentius/chant", version: "1.2.3", bin: { chant: "./bin/chant" } }, "bin/chant");
41
+ const deep = join(product, "node_modules", "@x", "tool", "dist");
42
+ mkdirSync(deep, { recursive: true });
43
+
44
+ test("walks up from each start to node_modules/@intentius/chant and reads bin.chant", () => {
45
+ expect(installedChant([deep])).toEqual({ root, bin: join(root, "bin", "chant"), binDir: join(root, "bin"), version: "1.2.3" });
46
+ });
47
+
48
+ test("takes the first start that has one, and skips a manifest that is not chant's or names no bin there", () => {
49
+ const repo = join(scratch, "repo");
50
+ pkg(join(repo, "node_modules", "@intentius", "chant"), { name: "@intentius/chant", bin: { chant: "./bin/missing" } });
51
+ const other = join(scratch, "other");
52
+ pkg(join(other, "node_modules", "@intentius", "chant"), { name: "not-chant", bin: "./bin/chant" }, "bin/chant");
53
+ expect(installedChant([repo, other, deep])?.root).toBe(root);
54
+ expect(installedChant([repo, other])).toBeUndefined();
55
+ });
56
+
57
+ test("a string bin is the chant bin too", () => {
58
+ const at = join(scratch, "string-bin");
59
+ pkg(join(at, "node_modules", "@intentius", "chant"), { name: "@intentius/chant", bin: "./bin/chant" }, "bin/chant");
60
+ expect(installedChant([at])?.bin).toBe(join(at, "node_modules", "@intentius", "chant", "bin", "chant"));
61
+ });
62
+ });
63
+
64
+ describe("running it", () => {
65
+ test("chantCommand: the named chant, else this package's bin", () => {
66
+ expect(chantCommand({ chant: "/opt/chant" })).toBe("/opt/chant");
67
+ expect(chantCommand()).toBe(bundledChant()!.bin);
68
+ });
69
+
70
+ test("chantArgv runs the bin with this node and the product's node arguments", () => {
71
+ const own = bundledChant()!;
72
+ expect(chantArgv(["build", "--json"], { nodeArgs: ["--import", "data:x"] })).toEqual([process.execPath, "--import", "data:x", own.bin, "build", "--json"]);
73
+ });
74
+
75
+ test("chantEnv puts the bin directory first on PATH", () => {
76
+ const env = chantEnv({ PATH: "/usr/bin", HOME: "/h" });
77
+ expect(env.PATH).toBe([bundledChant()!.binDir, "/usr/bin"].join(delimiter));
78
+ expect(env.HOME).toBe("/h");
79
+ });
80
+ });
81
+
82
+ describe("chantResolveFallback", () => {
83
+ const from = "file:///opt/product/dist/bin.js";
84
+ const missing = Object.assign(new Error("not found"), { code: "ERR_MODULE_NOT_FOUND" });
85
+
86
+ test("covers chant and its lexicons, with any subpath", () => {
87
+ for (const s of ["@intentius/chant", "@intentius/chant/op", "@intentius/chant-lexicon-pulumi", "@intentius/chant-lexicon-sql/config"]) expect(CHANT_SPECIFIER.test(s), s).toBe(true);
88
+ for (const s of ["@intentius/chantx", "@intentius/plywood", "chant"]) expect(CHANT_SPECIFIER.test(s), s).toBe(false);
89
+ });
90
+
91
+ test("retries a chant import the importer could not find from the product's install", () => {
92
+ const seen: Array<unknown> = [];
93
+ const hook = chantResolveFallback(from);
94
+ const next = (specifier: string, context?: Record<string, unknown>) => {
95
+ seen.push(context?.parentURL);
96
+ if (context?.parentURL !== from) throw missing;
97
+ return { url: `file:///opt/product/node_modules/${specifier}/index.js` };
98
+ };
99
+ expect(hook("@intentius/chant/op", { parentURL: "file:///repo/ops/up.op.ts" }, next).url).toBe("file:///opt/product/node_modules/@intentius/chant/op/index.js");
100
+ expect(seen).toEqual(["file:///repo/ops/up.op.ts", from]);
101
+ });
102
+
103
+ test("leaves any other specifier, any other failure, and the product's own imports as Node reports them", () => {
104
+ const hook = chantResolveFallback(from);
105
+ const fail = (e: Error) => () => {
106
+ throw e;
107
+ };
108
+ expect(() => hook("left-pad", { parentURL: "file:///repo/a.ts" }, fail(missing))).toThrow("not found");
109
+ expect(() => hook("@intentius/chant", { parentURL: "file:///repo/a.ts" }, fail(new Error("syntax")))).toThrow("syntax");
110
+ expect(() => hook("@intentius/chant", { parentURL: from }, fail(missing))).toThrow("not found");
111
+ });
112
+ });
@@ -0,0 +1,179 @@
1
+ /**
2
+ * #4800 — the chant a product is installed with, and how to run it.
3
+ *
4
+ * A product runs the `chant` CLI as a child for the commands it does not run
5
+ * in process, and the chant it runs must be the one in its own dependencies,
6
+ * not whatever `chant` is first on the PATH. Each product found it its own
7
+ * way: SQL Yodeler resolved `@intentius/chant` and ran `src/cli/main.ts`
8
+ * under its own tsx, terragucci walked up its directories for
9
+ * `node_modules/@intentius/chant/package.json` and read `bin.chant`, and
10
+ * plywood added a resolve hook so the Op files a repository holds find
11
+ * chant in plywood's install when the repository has none. This module is
12
+ * the one copy.
13
+ *
14
+ * {@link bundledChant} is the package this module belongs to, so a product
15
+ * that imports `@intentius/chant/chant-install` gets its own chant. When a
16
+ * product bundles this module into a file of its own, the module no longer
17
+ * sits in chant's package, and it walks up from where it is instead.
18
+ * {@link chantArgv} runs the package's `bin/chant` with the node running
19
+ * now: the bin picks the compiled CLI in an installed package and the source
20
+ * in a checkout (#3878), so a product never chooses between them.
21
+ */
22
+
23
+ import { existsSync, readFileSync } from "node:fs";
24
+ import * as nodeModule from "node:module";
25
+ import { delimiter, dirname, join, resolve } from "node:path";
26
+ import { fileURLToPath } from "node:url";
27
+
28
+ /** Where a chant package is installed. */
29
+ export interface ChantInstall {
30
+ /** The `@intentius/chant` package directory. */
31
+ root: string;
32
+ /** Its `chant` bin script, from the manifest's `bin.chant`. */
33
+ bin: string;
34
+ /** The directory holding the bin, to put first on a child's PATH. */
35
+ binDir: string;
36
+ /** The package's version, from its manifest. */
37
+ version?: string;
38
+ }
39
+
40
+ const PACKAGE = "@intentius/chant";
41
+
42
+ /** The install whose manifest is `<root>/package.json`, when it is chant's and names a bin that is there. */
43
+ function installAt(root: string): ChantInstall | undefined {
44
+ const manifest = join(root, "package.json");
45
+ if (!existsSync(manifest)) return undefined;
46
+ try {
47
+ const m = JSON.parse(readFileSync(manifest, "utf-8")) as { name?: unknown; version?: unknown; bin?: unknown };
48
+ if (m.name !== PACKAGE) return undefined;
49
+ const rel = typeof m.bin === "string" ? m.bin : m.bin && typeof m.bin === "object" ? (m.bin as Record<string, unknown>).chant : undefined;
50
+ if (typeof rel !== "string") return undefined;
51
+ const bin = resolve(root, rel);
52
+ if (!existsSync(bin)) return undefined;
53
+ return { root, bin, binDir: dirname(bin), ...(typeof m.version === "string" ? { version: m.version } : {}) };
54
+ } catch {
55
+ // An unreadable manifest is not an install.
56
+ return undefined;
57
+ }
58
+ }
59
+
60
+ /**
61
+ * The chant installed for the first of `from` that has one: each directory
62
+ * and every directory above it, through `node_modules/@intentius/chant`
63
+ * (terragucci's `installedChant`, which looked from its own file and then
64
+ * from the repository). The package exports no `./package.json`, so the
65
+ * manifest is read from disk, not resolved. Undefined when none has it.
66
+ */
67
+ export function installedChant(from: readonly string[]): ChantInstall | undefined {
68
+ for (const start of from) {
69
+ for (let dir = resolve(start); ; ) {
70
+ const found = installAt(join(dir, "node_modules", "@intentius", "chant"));
71
+ if (found) return found;
72
+ const up = dirname(dir);
73
+ if (up === dir) break;
74
+ dir = up;
75
+ }
76
+ }
77
+ return undefined;
78
+ }
79
+
80
+ let bundled: ChantInstall | null | undefined;
81
+
82
+ /**
83
+ * The chant this module is part of: `src/chant-install.ts` or
84
+ * `dist/chant-install.js`, one level under the package root. Bundled into a
85
+ * product's own file, the module is not in chant's package, and this is the
86
+ * chant {@link installedChant} finds from the module's directory. Undefined
87
+ * when neither has one.
88
+ */
89
+ export function bundledChant(): ChantInstall | undefined {
90
+ if (bundled === undefined) {
91
+ const here = dirname(fileURLToPath(import.meta.url));
92
+ bundled = installAt(dirname(here)) ?? installedChant([here]) ?? null;
93
+ }
94
+ return bundled ?? undefined;
95
+ }
96
+
97
+ /**
98
+ * The chant executable a product runs: `chant` when the caller names one
99
+ * (a `--chant` flag), else {@link bundledChant}'s bin, else the first of
100
+ * `from` {@link installedChant} finds, else `chant` on the PATH.
101
+ */
102
+ export function chantCommand(options: { chant?: string; from?: readonly string[] } = {}): string {
103
+ return options.chant ?? bundledChant()?.bin ?? installedChant(options.from ?? [])?.bin ?? "chant";
104
+ }
105
+
106
+ /**
107
+ * The argv to spawn `chant <args>` with: the node running now, `nodeArgs`
108
+ * (a product's own `--import` hooks), the install's bin and `args`. Running
109
+ * the bin through `process.execPath` works where a shebang does not
110
+ * (Windows, #4734). Throws when no install is found.
111
+ */
112
+ export function chantArgv(args: readonly string[], options: { install?: ChantInstall; nodeArgs?: readonly string[] } = {}): string[] {
113
+ const install = options.install ?? bundledChant();
114
+ if (!install) throw new Error(`${PACKAGE} is not installed beside this module; install it, or name the chant to run`);
115
+ return [process.execPath, ...(options.nodeArgs ?? []), install.bin, ...args];
116
+ }
117
+
118
+ /**
119
+ * `base` with the install's bin directory first on PATH, for a chant child:
120
+ * chant's own Op activities run `chant` themselves (`lifecycleDiff` runs
121
+ * `chant lifecycle diff`), and they must find the same one.
122
+ */
123
+ export function chantEnv(base: NodeJS.ProcessEnv = process.env, install: ChantInstall | undefined = bundledChant()): NodeJS.ProcessEnv {
124
+ if (!install) return { ...base };
125
+ return { ...base, PATH: [install.binDir, base.PATH ?? ""].filter(Boolean).join(delimiter) };
126
+ }
127
+
128
+ /** The specifiers {@link chantResolveFallback} covers: chant and its lexicons, with any subpath. */
129
+ export const CHANT_SPECIFIER = /^@intentius\/chant(?:-lexicon-[a-z0-9-]+)?(?:\/|$)/;
130
+
131
+ // `module.registerHooks` (Node 22.15 and later) is newer than the
132
+ // @types/node the repository pins, so its shape is declared here.
133
+ interface ResolveResult {
134
+ url: string;
135
+ format?: string | null;
136
+ shortCircuit?: boolean;
137
+ }
138
+ type NextResolve = (specifier: string, context?: Record<string, unknown>) => ResolveResult;
139
+ type RegisterHooks = (hooks: { resolve: (specifier: string, context: Record<string, unknown>, next: NextResolve) => ResolveResult }) => unknown;
140
+
141
+ /**
142
+ * A resolve hook (plywood's `chantResolveHook`): a chant or lexicon import
143
+ * the importing file's own `node_modules` cannot satisfy resolves from
144
+ * `from`, a file URL in the product's install. A repository that runs the
145
+ * product from a global install has no chant of its own, and the Op files
146
+ * it holds import `@intentius/chant/op`. Any other specifier, and any other
147
+ * failure, is left as Node reports it.
148
+ */
149
+ export function chantResolveFallback(from: string) {
150
+ return (specifier: string, context: Record<string, unknown>, next: NextResolve): ResolveResult => {
151
+ try {
152
+ return next(specifier, context);
153
+ } catch (err) {
154
+ const code = (err as { code?: unknown }).code;
155
+ if (!CHANT_SPECIFIER.test(specifier) || (code !== "ERR_MODULE_NOT_FOUND" && code !== "MODULE_NOT_FOUND")) throw err;
156
+ if (context.parentURL === from) throw err;
157
+ return next(specifier, { ...context, parentURL: from });
158
+ }
159
+ };
160
+ }
161
+
162
+ const FALLBACK = Symbol.for("chant.resolveFallback");
163
+
164
+ /**
165
+ * Install {@link chantResolveFallback} once in this process. `from` is a
166
+ * file URL in the product's install, by default this module's own, which
167
+ * sits in the chant the product depends on. Returns whether it is
168
+ * installed: false on a Node without `module.registerHooks`, where the
169
+ * repository needs chant in its own `node_modules`.
170
+ */
171
+ export function installChantResolveFallback(from: string = import.meta.url): boolean {
172
+ const flags = globalThis as { [FALLBACK]?: boolean };
173
+ if (flags[FALLBACK]) return true;
174
+ const register = (nodeModule as unknown as { registerHooks?: RegisterHooks }).registerHooks;
175
+ if (typeof register !== "function") return false;
176
+ register({ resolve: chantResolveFallback(from) });
177
+ flags[FALLBACK] = true;
178
+ return true;
179
+ }
@@ -24,6 +24,7 @@ import {
24
24
  COMMIT_PR_ENV,
25
25
  CI_COMMIT_ARG_FLOOR,
26
26
  CI_COMMIT_HARNESS_BREAK_FLOOR,
27
+ CI_COMMIT_REFUSAL_REPLY_FLOOR,
27
28
  COMMIT_HARNESS_BREAK_ENV,
28
29
  COMMIT_HARNESS_FORGE,
29
30
  commitHarnessBreaks,
@@ -36,6 +37,7 @@ import {
36
37
  } from "./ci-commit";
37
38
  import { assertProductWords, parseProductComment } from "./ci-product-words";
38
39
  import { PLYWOOD } from "./__fixtures__/product-words";
40
+ import { NEXT_RELEASE } from "./release-floors";
39
41
  import type { OpRunRecord, OpRunRecordInput } from "./op/runtime";
40
42
  import type { PullRequestHead } from "./pr-forge";
41
43
 
@@ -577,6 +579,49 @@ describe("the comment's argument (#4820)", () => {
577
579
  });
578
580
  });
579
581
 
582
+ describe("a comment refused as it is read gets a reply (#4838)", () => {
583
+ const named = () => settings({ commentArg: { pattern: "^[a-z0-9]+(?:[-_][a-z0-9]+)*$", position: 1 } });
584
+
585
+ test("a bad argument and a stray word are answered on the pull request, naming the comment and why, on GitHub and Forgejo", async () => {
586
+ // Forgejo's event has GitHub's shape, so the one path answers both.
587
+ for (const [s, body, why] of [
588
+ [named(), "/plywood fmt Bad", "`/plywood fmt` takes nothing, or one name after it matching `^[a-z0-9]+(?:[-_][a-z0-9]+)*$`"],
589
+ [settings(), "/plywood fmt now", "`/plywood fmt` takes nothing after it"],
590
+ ] as const) {
591
+ const w = world();
592
+ const { out } = await cycle(w, s, { event: comment(body) });
593
+ expect(out).toMatchObject({ result: "refused", reason: why, requestedBy: "ana" });
594
+ expect(w.replies).toEqual([`plywood: \`${body}\` was refused: ${why}. plywood-fmt did not run, and nothing was pushed.`]);
595
+ expect(w.records[0]!.outcomes.commit).toMatchObject({ result: "refused", pr: 7, requestedBy: "ana" });
596
+ expect(sh(["rev-parse", "refs/heads/feature"], w.r.origin).trim()).toBe(w.r.head);
597
+ }
598
+ });
599
+
600
+ test("GitLab answers a bad argument from its pipeline variables", async () => {
601
+ const w = world();
602
+ const env = { GITLAB_CI: "true", CHANT_COMMIT_PR: "7", CHANT_COMMIT: "fmt", [GITLAB_COMMIT_ARG_VAR]: "Bad", GITLAB_USER_LOGIN: "ana" };
603
+ const { out } = await cycle(w, named(), { event: {}, env });
604
+ expect(out.result).toBe("refused");
605
+ expect(w.replies).toEqual([expect.stringMatching(/^plywood: `\/plywood fmt Bad` was refused: `\/plywood fmt` takes nothing, or one name after it/)]);
606
+ });
607
+
608
+ test("a comment for another word gets no reply from this job, and a long or many-line comment is quoted by its first line, cut short", async () => {
609
+ const other = world();
610
+ await cycle(other, settings(), { event: comment("/plywood up") });
611
+ expect(other.replies).toEqual([]);
612
+ const many = world();
613
+ await cycle(many, settings(), { event: comment("/plywood fmt\nand more") });
614
+ expect(many.replies).toEqual(["plywood: `/plywood fmt` was refused: a command is one line, and nothing else in the comment. plywood-fmt did not run, and nothing was pushed."]);
615
+ const long = world();
616
+ await cycle(long, settings(), { event: comment(`/plywood fmt ${"x".repeat(200)}`) });
617
+ expect(long.replies[0]).toMatch(new RegExp(`^plywood: \`/plywood fmt x{84}\\.\\.\\.\` was refused`));
618
+ });
619
+
620
+ test("the floor is the next release until it ships", () => {
621
+ expect(CI_COMMIT_REFUSAL_REPLY_FLOOR).toBe(NEXT_RELEASE);
622
+ });
623
+ });
624
+
580
625
  describe("the harness's BREAK switch for land (#4836)", () => {
581
626
  const harness = (checks: string) => ({ [COMMIT_HARNESS_BREAK_ENV]: checks, BREAK: "1", GITHUB_SERVER_URL: COMMIT_HARNESS_FORGE });
582
627
 
package/src/ci-commit.ts CHANGED
@@ -29,6 +29,7 @@ import { spawn } from "node:child_process";
29
29
  import { existsSync, mkdirSync, readFileSync, statSync, writeFileSync } from "node:fs";
30
30
  import { join } from "node:path";
31
31
  import { CHANT_EXIT } from "./exit-codes";
32
+ import { NEXT_RELEASE } from "./release-floors";
32
33
  import { FORGE_TOKEN_VARS } from "./ci-handover";
33
34
  import { agentForbiddenPaths, AgentError, branchOk, errText, LOGIN, readSmall, SHA, type AgentGit } from "./ci-agent";
34
35
  import { COMMIT_COMMENT_ARG, parseProductComment, type CiProductWords } from "./ci-product-words";
@@ -53,6 +54,8 @@ export const CI_COMMIT_REPLY_FLOOR = "0.124.77";
53
54
  export const CI_COMMIT_ARG_FLOOR = "0.124.78";
54
55
  /** The first chant whose `commit land` reads {@link COMMIT_HARNESS_BREAK_ENV} in a BREAK arm of chant's e2e harness (#4836). */
55
56
  export const CI_COMMIT_HARNESS_BREAK_FLOOR = "0.124.78";
57
+ /** The first chant whose `commit land` replies to a comment refused as it is read, such as a bad argument or a stray word (#4838). */
58
+ export const CI_COMMIT_REFUSAL_REPLY_FLOOR = NEXT_RELEASE;
56
59
 
57
60
  /** What starts a commit run: a person's comment, or a check job's finding. */
58
61
  export type CommitTrigger = "comment" | "check";
@@ -442,6 +445,27 @@ export interface CommitRequest {
442
445
  arg?: string;
443
446
  }
444
447
 
448
+ /**
449
+ * A request refused (#4838). `replyOn` is set when the comment named this
450
+ * job's command and was refused as it was read (a bad argument, a stray
451
+ * word): the pull request, who asked, and the comment's first line, so land
452
+ * can say why on the pull request. A comment for another word has none,
453
+ * since that word's own job answers it.
454
+ */
455
+ export interface CommitRequestRefusal {
456
+ refused: string;
457
+ replyOn?: { pr: number; user: string; asked: string };
458
+ }
459
+
460
+ /** The most of a comment a refusal reply quotes (#4838). */
461
+ const ASKED_MAX = 100;
462
+
463
+ /** The comment's first line as a reply quotes it, cut at {@link ASKED_MAX}. */
464
+ function askedLine(body: string): string {
465
+ const line = body.trim().split(/\r?\n/)[0]!.trim();
466
+ return line.length > ASKED_MAX ? `${line.slice(0, ASKED_MAX - 3)}...` : line;
467
+ }
468
+
445
469
  /**
446
470
  * The request from the run. `comment`: the event's comment on GitHub and
447
471
  * Forgejo, read against the product's grammar, which must name this job's
@@ -450,7 +474,7 @@ export interface CommitRequest {
450
474
  * pull request the check ran for, from the `pull_request` event or the
451
475
  * merge request pipeline.
452
476
  */
453
- export function commitRequestFromRun(words: CiProductWords, s: Pick<CommitSettings, "on" | "comment"> & Partial<Pick<CommitSettings, "commentArg">>, env: Record<string, string | undefined>, event: unknown): CommitRequest | { refused: string } {
477
+ export function commitRequestFromRun(words: CiProductWords, s: Pick<CommitSettings, "on" | "comment"> & Partial<Pick<CommitSettings, "commentArg">>, env: Record<string, string | undefined>, event: unknown): CommitRequest | CommitRequestRefusal {
454
478
  const gitlab = env.GITLAB_CI === "true";
455
479
  let pr: unknown;
456
480
  if (s.on === "check") {
@@ -475,7 +499,12 @@ export function commitRequestFromRun(words: CiProductWords, s: Pick<CommitSettin
475
499
  if (typeof pr !== "number" || !Number.isInteger(pr) || pr < 1) return { refused: "the run names no pull request" };
476
500
  if (typeof user !== "string" || !LOGIN.test(user)) return { refused: "the run names nobody who asked" };
477
501
  const parsed = parseProductComment(words, body, env, commentArgPatterns(s));
478
- if (parsed?.kind === "refused") return { refused: parsed.reason };
502
+ if (parsed?.kind === "refused") {
503
+ // #4838: answered only when the comment's command is this job's, so two jobs never both reply.
504
+ const said = typeof body === "string" ? body.trim().split(/[ \t\r\n]+/) : [];
505
+ const ours = said[0] === `/${words.product}` && said[1] === word;
506
+ return { refused: parsed.reason, ...(ours ? { replyOn: { pr, user, asked: askedLine(body as string) } } : {}) };
507
+ }
479
508
  if (parsed?.kind !== "commit" || parsed.word !== word) return { refused: `the comment is not ${s.comment}` };
480
509
  return { pr, user, ...(parsed.arg !== undefined ? { arg: parsed.arg } : {}) };
481
510
  }
@@ -518,7 +547,7 @@ export async function resolveCommitRequest(
518
547
  env: Record<string, string | undefined>,
519
548
  event: unknown,
520
549
  forge?: Partial<Pick<PrForge, "findPullRequest">>,
521
- ): Promise<CommitRequest | { refused: string }> {
550
+ ): Promise<CommitRequest | CommitRequestRefusal> {
522
551
  const req = commitRequestFromRun(words, s, env, event);
523
552
  if (!("refused" in req) || s.on !== "check" || env.GITLAB_CI === "true" || !forge?.findPullRequest) return req;
524
553
  const branch = pushedBranch(env);
@@ -1014,7 +1043,12 @@ export async function landCommitChange(i: CommitLandInput): Promise<CommitLandOu
1014
1043
 
1015
1044
  // Who asked and for which pull request: from the event, never from the commit job's files.
1016
1045
  const req = await resolveCommitRequest(i.words, s, i.env, i.event, i.forge);
1017
- if ("refused" in req) return finish({ result: s.on === "check" ? "skipped" : "refused", reason: req.refused });
1046
+ if ("refused" in req) {
1047
+ // #4838: a comment refused as it was read is answered, naming it and why.
1048
+ if (!req.replyOn) return finish({ result: s.on === "check" ? "skipped" : "refused", reason: req.refused });
1049
+ ({ pr, user } = req.replyOn);
1050
+ return finish({ result: "refused", reason: req.refused }, `${code(req.replyOn.asked)} was refused: ${req.refused}. ${s.name} did not run, and nothing was pushed.`);
1051
+ }
1018
1052
  ({ pr, user } = req);
1019
1053
  // #4836: a BREAK arm of the harness may turn a check off; anywhere else the switch fails the land.
1020
1054
  const harness = commitHarnessBreaks(i.env);
@@ -0,0 +1,105 @@
1
+ /**
2
+ * What a generated pipeline adds to sign evidence for the named checks it
3
+ * runs (#4870), for the forge lexicons to render. No import beyond the
4
+ * words' bin swap, so the generators' bundles stay small.
5
+ *
6
+ * A project switches it on by naming the secret (GitHub, Forgejo) or CI/CD
7
+ * variable (GitLab) that holds its runner key, the PEM key whose public half
8
+ * `.chant/trust.json` lists under `runners`: the same secret the green
9
+ * workflow takes as `--runner-key-secret`. Without it nothing is added and
10
+ * every generated file is as before.
11
+ *
12
+ * - The plan note job (the product plan pipeline, GitHub and Forgejo) signs
13
+ * `plan` and `policy` from the plan job's report, after taking it back and
14
+ * before posting the note, which then says which checks were signed. The
15
+ * key is in that one step, in a job that runs none of the change's code.
16
+ * GitLab posts its notes from a target-branch pipeline that reads every
17
+ * open merge request, so there a job of its own after each plan job
18
+ * (`<plan job>-evidence`) signs from the plan job's artifact.
19
+ * - A wave job (the waves pipeline) gets the key on its `chant run wave`
20
+ * step, and signs `plan:<wave>` and `policy:<wave>` once the wave is
21
+ * planned (../op/op-waves-evidence.ts).
22
+ * - A product's check job runs `chant ci check` with the key in
23
+ * `CHANT_RUNNER_KEY`; the check runner signs each hook (./components/check-steps.ts).
24
+ *
25
+ * With `identity: "oidc"` (#4869) the same steps sign keyless from the
26
+ * job's OIDC identity: no secret, `CHANT_RUNNER_IDENTITY=oidc` on the
27
+ * signing step, and the job asks the forge for a token, so no product adds
28
+ * that itself: `id-token: write` on GitHub (Forgejo's dialect turns it into
29
+ * `enable-openid-connect: true`), and on GitLab `id_tokens:` with
30
+ * `CHANT_ID_TOKEN_SIGSTORE` for the signing audience. `.chant/trust.json`
31
+ * at base lists the workflow's identity under `keyless`.
32
+ */
33
+
34
+ import type { ForgeKind } from "./pr-forge";
35
+
36
+ /** The variable the signing steps read the key from. */
37
+ export const EVIDENCE_KEY_ENV = "CHANT_RUNNER_KEY";
38
+
39
+ /** The variable that makes a signing step sign keyless (#4869). */
40
+ export const EVIDENCE_IDENTITY_ENV = "CHANT_RUNNER_IDENTITY";
41
+
42
+ /** The GitLab `id_tokens` variable a keyless signing job's token comes in (#4869). */
43
+ export const EVIDENCE_ID_TOKEN_VAR = "CHANT_ID_TOKEN_SIGSTORE";
44
+
45
+ /** The audience a keyless signing job's token is requested for unless told: sigstore's. */
46
+ export const EVIDENCE_DEFAULT_AUDIENCE = "sigstore";
47
+
48
+ export { CHECK_EVIDENCE_FLOOR, KEYLESS_EVIDENCE_FLOOR, LOCAL_EVIDENCE_FLOOR } from "./release-floors";
49
+
50
+ /**
51
+ * A pipeline's runner identity (#4870): a runner key's secret, or (#4869)
52
+ * the job's own OIDC identity. Exactly one.
53
+ */
54
+ export interface PipelineEvidence {
55
+ /** The secret (GitHub, Forgejo) or CI/CD variable (GitLab) holding the runner key as PEM. */
56
+ runnerKeySecret?: string;
57
+ /** `oidc` (#4869): sign keyless from the job's OIDC identity, which `.chant/trust.json` at base lists under `keyless.identities`. */
58
+ identity?: "oidc";
59
+ /** With `identity`: the token's audience, as `keyless.audience` in `.chant/trust.json` says. Default `sigstore`. */
60
+ audience?: string;
61
+ }
62
+
63
+ const SECRET_NAME = /^[A-Za-z_][A-Za-z0-9_]*$/;
64
+
65
+ /** Refuse a secret name the forge could not hold, and an evidence block that names both identities or neither. */
66
+ export function assertPipelineEvidence(evidence: PipelineEvidence | undefined): void {
67
+ if (evidence === undefined) return;
68
+ if (evidence.identity !== undefined) {
69
+ if (evidence.runnerKeySecret !== undefined) throw new Error("evidence names a runnerKeySecret and an identity; a pipeline signs with one of them (#4869)");
70
+ if (evidence.identity !== "oidc") throw new Error(`evidence.identity ${JSON.stringify(evidence.identity)} is not oidc, the one identity chant signs with keyless`);
71
+ if (evidence.audience !== undefined && !/^[\x21-\x7e]+$/.test(evidence.audience)) throw new Error(`evidence.audience ${JSON.stringify(evidence.audience)} is not printable ASCII with no space`);
72
+ return;
73
+ }
74
+ if (!SECRET_NAME.test(evidence.runnerKeySecret ?? "")) throw new Error(`evidence.runnerKeySecret ${JSON.stringify(evidence.runnerKeySecret)} is not a secret name: letters, digits and _, not starting with a digit`);
75
+ }
76
+
77
+ /** Whether the pipeline signs keyless (#4869), so its signing jobs ask the forge for an OIDC token. */
78
+ export function evidenceKeyless(evidence: PipelineEvidence | undefined): boolean {
79
+ return evidence?.identity === "oidc";
80
+ }
81
+
82
+ /** The GitLab `id_tokens:` a keyless signing job requests (#4869); empty with a runner key. */
83
+ export function evidenceIdTokens(evidence: PipelineEvidence | undefined): Record<string, { aud: string }> {
84
+ return evidenceKeyless(evidence) ? { [EVIDENCE_ID_TOKEN_VAR]: { aud: evidence!.audience ?? EVIDENCE_DEFAULT_AUDIENCE } } : {};
85
+ }
86
+
87
+ /** The signing step's variables: the key from the forge's secret, or (#4869) `CHANT_RUNNER_IDENTITY=oidc`. */
88
+ export function evidenceKeyEnv(forge: ForgeKind, evidence: PipelineEvidence): Record<string, string> {
89
+ if (evidenceKeyless(evidence)) return { [EVIDENCE_IDENTITY_ENV]: "oidc" };
90
+ return { [EVIDENCE_KEY_ENV]: forge === "gitlab" ? `$${evidence.runnerKeySecret}` : `\${{ secrets.${evidence.runnerKeySecret} }}` };
91
+ }
92
+
93
+ /**
94
+ * The plan job's evidence step (#4870): fetch the planned commit (the note
95
+ * job checks out the target branch's), then sign `plan` and `policy` from
96
+ * the report directory. `result` is the plan job's result as the shell
97
+ * reads it.
98
+ */
99
+ export function planEvidenceLines(opts: { dir: string; at: string; base: string; result: string; bin?: string }): string[] {
100
+ const chant = opts.bin ?? "chant";
101
+ return [
102
+ `git fetch --quiet --no-tags --depth=1 origin ${opts.at} || true`,
103
+ `${chant} ci evidence sign --from-report ${opts.dir} --result ${opts.result} --at ${opts.at} --base ${opts.base}`,
104
+ ];
105
+ }