@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,432 @@
1
+ /**
2
+ * The ref gate (#4867): a ref update is refused when the commit lacks the
3
+ * runner evidence the policy at base requires for that ref.
4
+ *
5
+ * chant judges evidence at the green tick, a land, a gate and a release cut,
6
+ * all after the push. A change that was never going to pass still reached
7
+ * the remote and started CI. The gate judges it at the ref update instead,
8
+ * from three places that all call {@link decideRefUpdate}:
9
+ *
10
+ * - a plain git remote: the `pre-receive` hook `chant workspace ref-gate
11
+ * install` writes, which refuses the push;
12
+ * - a forge: `chant workspace ref-gate status`, a commit status the forge
13
+ * requires, so the merge is refused where the push can't be;
14
+ * - the developer's side: the `pre-push` hook, which refuses before the
15
+ * network and records the request for an override.
16
+ *
17
+ * The rules are `refs` in `.chant/trust.json`, read at base like every other
18
+ * part of the trust policy (./policy.ts): the old tip of the ref, so a change
19
+ * can neither drop a check nor add the runner key that signs its own
20
+ * evidence. Evidence is the runner evidence of ./evidence.ts, kept as git
21
+ * notes on the commit under {@link EVIDENCE_NOTES_REF}, one envelope per
22
+ * line, and verified offline against the runner keys at base. A check has
23
+ * current evidence when an envelope for it verifies and every record it
24
+ * names hashes the same at the pushed commit; an envelope that names no
25
+ * records counts only for a commit with the tree it was signed over.
26
+ *
27
+ * An override is the override gate's (../../override-gate.ts): a person
28
+ * the overriders file at base lists records a reasoned approval of one
29
+ * commit and one check, subject `ref-gate-<sha12>-<check>`, and it is spent
30
+ * by the first ref update it admits. Every decision is a line of the gate's
31
+ * ledger, a JSON-lines file in the git directory ({@link refGateLedgerPath}),
32
+ * since a pre-receive hook may not move a ref.
33
+ */
34
+ import { execFileSync, spawnSync } from "node:child_process";
35
+ import { appendFileSync, existsSync, mkdirSync, readFileSync } from "node:fs";
36
+ import { dirname, isAbsolute, join } from "node:path";
37
+ import { verifyEvidence } from "./evidence.js";
38
+ import { policyAtBase } from "./provenance.js";
39
+ import { runnersAt } from "./policy.js";
40
+ import { OVERRIDE_APPLIED, OVERRIDE_LEDGER, OVERRIDERS_PATH, decideOverride, overrideCommand, overridersAt, parseAppliedOverrides, } from "../../override-gate.js";
41
+ import { parseGateLedger } from "../../lifecycle/gate-ledger.js";
42
+ import { gateSignersAt } from "../../gate-decision.js";
43
+ /** Where runner evidence for a commit is kept (#4867): a git note on the commit, one DSSE envelope per line. */
44
+ export const EVIDENCE_NOTES_REF = "refs/notes/chant/evidence";
45
+ /** The ledger branch whose override lines the gate reads. */
46
+ export const LIFECYCLE_REF = "refs/heads/chant/lifecycle";
47
+ /** The commit status a forge requires when the rule names none. */
48
+ export const REF_GATE_STATUS = "chant/ref-gate";
49
+ /** git's id for no object, in hook input. */
50
+ const ZERO = /^0+$/;
51
+ /** Why the gate cannot decide at all. Each is in `reason-codes.ts` and `ref-gate.schema.json`. */
52
+ export const REF_GATE_ERROR_CODES = ["not-a-git-repository", "revision-unknown", "trust-policy-unreadable", "ref-update-invalid"];
53
+ /** Why a required check has no current evidence. The envelope codes are those of `evidence verify`. */
54
+ export const REF_GATE_CHECK_CODES = [
55
+ "ref-evidence-missing",
56
+ "ref-evidence-stale",
57
+ "envelope-invalid",
58
+ "envelope-untrusted",
59
+ "evidence-payload-type",
60
+ "evidence-statement-invalid",
61
+ "evidence-runner-mismatch",
62
+ ];
63
+ function git(repo, args) {
64
+ return execFileSync("git", args, { cwd: repo, encoding: "utf-8", stdio: ["ignore", "pipe", "pipe"], maxBuffer: 256 * 1024 * 1024 });
65
+ }
66
+ function tryGit(repo, args) {
67
+ const r = spawnSync("git", args, { cwd: repo, encoding: "utf-8", maxBuffer: 256 * 1024 * 1024 });
68
+ return r.status === 0 ? r.stdout : undefined;
69
+ }
70
+ /** The commit `rev` names, peeled, or undefined. */
71
+ export function commitOf(repo, rev) {
72
+ return tryGit(repo, ["rev-parse", "--verify", "-q", `${rev}^{commit}`])?.trim() || undefined;
73
+ }
74
+ /** A ref pattern as a RegExp: `**` matches across `/`, `*` within one segment. */
75
+ export function refPatternRegExp(pattern) {
76
+ const body = pattern
77
+ .split("**")
78
+ .map((part) => part.split("*").map((s) => s.replace(/[.+?^${}()|[\]\\]/g, "\\$&")).join("[^/]*"))
79
+ .join(".*");
80
+ return new RegExp(`^${body}$`);
81
+ }
82
+ /**
83
+ * The checks the policy requires for `ref`: every matching rule's, each check
84
+ * once. A check two rules name is `block` when either blocks it; the first
85
+ * rule that names a status or an override mode gives it.
86
+ */
87
+ export function rulesFor(policy, ref) {
88
+ const matching = (policy.refs ?? []).filter((r) => refPatternRegExp(r.ref).test(ref));
89
+ const checks = new Map();
90
+ for (const r of matching) {
91
+ for (const c of r.checks) {
92
+ const before = checks.get(c.check);
93
+ if (!before)
94
+ checks.set(c.check, { ...c });
95
+ else if (c.action === "block")
96
+ before.action = "block";
97
+ }
98
+ }
99
+ const overrides = matching.find((r) => r.overrides)?.overrides;
100
+ return {
101
+ matched: matching.length > 0,
102
+ checks: [...checks.values()],
103
+ status: matching.find((r) => r.status)?.status ?? REF_GATE_STATUS,
104
+ ...(overrides ? { overrides } : {}),
105
+ };
106
+ }
107
+ // ── evidence notes ───────────────────────────────────────────────────────
108
+ /** The envelopes in a note's text: one per line, or the whole note as one envelope or an array of them. */
109
+ export function parseEvidenceNote(text) {
110
+ try {
111
+ const whole = JSON.parse(text);
112
+ return Array.isArray(whole) ? whole : [whole];
113
+ }
114
+ catch {
115
+ const out = [];
116
+ for (const line of text.split("\n").map((l) => l.trim()).filter(Boolean)) {
117
+ try {
118
+ out.push(JSON.parse(line));
119
+ }
120
+ catch {
121
+ out.push(line);
122
+ }
123
+ }
124
+ return out;
125
+ }
126
+ }
127
+ /** The note on `commit` in the notes commit `notes`, which may fan its paths out. */
128
+ function noteText(repo, notes, commit) {
129
+ const list = tryGit(repo, ["ls-tree", "-r", "--full-tree", notes]);
130
+ if (!list)
131
+ return undefined;
132
+ for (const line of list.split("\n")) {
133
+ const tab = line.indexOf("\t");
134
+ if (tab < 0)
135
+ continue;
136
+ if (line.slice(tab + 1).replace(/\//g, "") !== commit)
137
+ continue;
138
+ const blob = line.slice(0, tab).split(" ")[2];
139
+ return tryGit(repo, ["cat-file", "blob", blob]);
140
+ }
141
+ return undefined;
142
+ }
143
+ /** The envelopes attached to `commit`, read from the notes commit `notes` (default: {@link EVIDENCE_NOTES_REF}'s tip). */
144
+ export function evidenceFor(repo, commit, notes) {
145
+ const tip = notes === undefined ? commitOf(repo, EVIDENCE_NOTES_REF) : notes;
146
+ if (!tip)
147
+ return [];
148
+ const text = noteText(repo, tip, commit);
149
+ return text === undefined ? [] : parseEvidenceNote(text);
150
+ }
151
+ /**
152
+ * Attach `envelope` to `commit` as one line of its evidence note. Pushing `refs/notes/chant/evidence` is the caller's.
153
+ * `notesRef` is another notes ref, for local evidence (#4868), which the ref gate never reads.
154
+ */
155
+ export function attachEvidence(repo, commit, envelope, notesRef = EVIDENCE_NOTES_REF) {
156
+ git(repo, [...notesIdentity(repo), "notes", `--ref=${notesRef}`, "append", "-m", JSON.stringify(envelope), commit]);
157
+ }
158
+ /**
159
+ * A notes commit needs an author and a committer, and a CI job often has no
160
+ * git identity configured, so `git notes append` failed there ("Author
161
+ * identity unknown"). Where the repository names none, the evidence notes
162
+ * are written as chant's. The envelope's signature, not the notes commit's
163
+ * author, says who signed the evidence.
164
+ */
165
+ function notesIdentity(repo) {
166
+ const named = (key) => !!tryGit(repo, ["config", "--get", key])?.trim();
167
+ return [
168
+ ...(named("user.name") || process.env.GIT_AUTHOR_NAME ? [] : ["-c", "user.name=chant evidence"]),
169
+ ...(named("user.email") || process.env.GIT_AUTHOR_EMAIL ? [] : ["-c", "user.email=chant-evidence@localhost"]),
170
+ ];
171
+ }
172
+ /** The check an envelope claims to be for, read without verifying, so a refusal can say which check a bad envelope was meant for. */
173
+ function claimedCheck(envelope) {
174
+ try {
175
+ const payload = envelope.payload;
176
+ if (typeof payload !== "string")
177
+ return undefined;
178
+ const check = JSON.parse(Buffer.from(payload, "base64").toString("utf8")).predicate?.check;
179
+ return typeof check === "string" ? check : undefined;
180
+ }
181
+ catch {
182
+ return undefined;
183
+ }
184
+ }
185
+ /** The best evidence among `envelopes` for `check` at `commit`: current beats stale beats an envelope that does not verify beats none. */
186
+ function judgeCheck(repo, policy, envelopes, check, commit, tree) {
187
+ let stale;
188
+ let bad;
189
+ for (const env of envelopes) {
190
+ // #4869: keys and keyless identities; under keyless.since, keys only for a commit before it.
191
+ const v = verifyEvidence(env, runnersAt(policy, { repo, commit }), repo, commit);
192
+ if (!v.ok) {
193
+ if (claimedCheck(env) === check && !bad)
194
+ bad = { code: v.code, reason: v.reason };
195
+ continue;
196
+ }
197
+ if (v.statement.predicate.check !== check)
198
+ continue;
199
+ const signedAt = v.statement.predicate.commit.slice(0, 8);
200
+ if (v.subjects.length === 0 && v.statement.predicate.tree !== tree) {
201
+ stale ?? (stale = `the evidence from ${v.runner} names no records and was signed over another tree, at ${signedAt}`);
202
+ continue;
203
+ }
204
+ if (v.status === "current")
205
+ return { status: "current", runner: v.runner };
206
+ const changed = v.subjects.filter((s) => !s.matches).map((s) => (s.now === null ? `${s.name} is gone` : `${s.name} changed`));
207
+ stale ?? (stale = `the evidence from ${v.runner} was signed at ${signedAt}, and since then ${changed.join(", ")}`);
208
+ }
209
+ if (stale)
210
+ return { status: "stale", code: "ref-evidence-stale", reason: stale };
211
+ if (bad)
212
+ return { status: "missing", code: bad.code, reason: `an envelope for ${check} does not count: ${bad.reason}` };
213
+ return { status: "missing", code: "ref-evidence-missing", reason: `no evidence for ${check} is attached to ${commit.slice(0, 8)} under ${EVIDENCE_NOTES_REF}` };
214
+ }
215
+ // ── overrides ────────────────────────────────────────────────────────────
216
+ /** The subject an override of `check` for `commit` names. */
217
+ export function refGateSubject(commit, check) {
218
+ return `ref-gate-${commit.slice(0, 12)}-${check}`;
219
+ }
220
+ function blobAt(repo, rev, path) {
221
+ return tryGit(repo, ["cat-file", "blob", `${rev}:${path}`]);
222
+ }
223
+ /** Decide one ref update against the policy at base. Never throws for a policy or an envelope it can't read: it refuses. */
224
+ export function decideRefUpdate(input) {
225
+ const { repo, update } = input;
226
+ const head = { ref: update.ref, old: update.old, new: update.new, base: input.base };
227
+ const refuse = (code, message) => ({ ...head, decision: "refuse", status: null, checks: [], error: { code, message } });
228
+ if (input.base === null)
229
+ return { ...head, decision: "admit", status: null, checks: [] };
230
+ const policy = policyAtBase(repo, { commit: input.base, from: "flag" });
231
+ if (policy.problems.length > 0)
232
+ return refuse("trust-policy-unreadable", `the policy at base ${input.base.slice(0, 8)} cannot be read, so the gate refuses every update: ${policy.problems.join("; ")}`);
233
+ const rules = rulesFor(policy, update.ref);
234
+ if (!rules.matched)
235
+ return { ...head, decision: "admit", status: null, checks: [] };
236
+ // A deletion carries no commit to judge; a forge or the remote protects deletions on its own.
237
+ if (update.new === null)
238
+ return { ...head, decision: "admit", status: rules.status, checks: [] };
239
+ const commit = commitOf(repo, update.new);
240
+ if (!commit)
241
+ return refuse("revision-unknown", `${update.new} is not a commit this repository holds`);
242
+ const tree = git(repo, ["rev-parse", `${commit}^{tree}`]).trim();
243
+ const envelopes = evidenceFor(repo, commit, input.notes);
244
+ const lifecycle = input.lifecycle === undefined ? commitOf(repo, LIFECYCLE_REF) : input.lifecycle;
245
+ const ledgerText = lifecycle ? blobAt(repo, lifecycle, OVERRIDE_LEDGER) : undefined;
246
+ const appliedText = lifecycle ? blobAt(repo, lifecycle, OVERRIDE_APPLIED) : undefined;
247
+ const ledger = ledgerText ? parseGateLedger(ledgerText) : { resolutions: [], pending: [] };
248
+ const applied = [...(appliedText ? parseAppliedOverrides(appliedText) : []), ...(input.spent ?? [])];
249
+ const overriders = overridersAt(repo, input.base);
250
+ const mode = rules.overrides ?? (policy.active ? "sealed" : "ledger");
251
+ let signers;
252
+ const checks = rules.checks.map((c) => {
253
+ const subject = refGateSubject(commit, c.check);
254
+ const judged = judgeCheck(repo, policy, envelopes, c.check, commit, tree);
255
+ const base = { check: c.check, action: c.action, command: c.command, subject };
256
+ if (judged.status === "current")
257
+ return { ...base, status: "current", reason: `current evidence from ${judged.runner}`, runner: judged.runner };
258
+ if (c.action === "block") {
259
+ if (mode === "sealed" && !signers)
260
+ signers = gateSignersAt(repo, input.base);
261
+ const verdict = decideOverride({
262
+ ledger,
263
+ applied,
264
+ subject,
265
+ planDigest: commit,
266
+ rules: [c.check],
267
+ overriders: overriders.principals,
268
+ overridersPath: OVERRIDERS_PATH,
269
+ mode,
270
+ ...(signers ? { signers } : {}),
271
+ ...(input.now ? { now: input.now } : {}),
272
+ });
273
+ if (verdict.status === "overridden") {
274
+ const o = verdict.override;
275
+ return { ...base, status: "overridden", code: judged.code, reason: `${judged.reason}; let through under ${o.by}'s override: ${o.reason}`, override: { subject, by: o.by, at: o.at, reason: o.reason, digest: o.digest } };
276
+ }
277
+ const why = verdict.refusals.length ? `; ${verdict.refusals.join("; ")}` : "";
278
+ return { ...base, status: judged.status, code: judged.code, reason: `${judged.reason}${why}` };
279
+ }
280
+ return { ...base, status: judged.status, code: judged.code, reason: judged.reason };
281
+ });
282
+ const refused = checks.some((c) => c.action === "block" && (c.status === "missing" || c.status === "stale"));
283
+ return { ...head, decision: refused ? "refuse" : "admit", status: rules.status, checks };
284
+ }
285
+ /** The gate's ledger file in the git directory of `repo`, `<git dir>/chant/ref-gate.jsonl`. */
286
+ export function refGateLedgerPath(repo) {
287
+ const dir = git(repo, ["rev-parse", "--git-common-dir"]).trim();
288
+ return join(isAbsolute(dir) ? dir : join(repo, dir), "chant", "ref-gate.jsonl");
289
+ }
290
+ export function appendRefGateLedger(path, lines) {
291
+ if (lines.length === 0)
292
+ return;
293
+ mkdirSync(dirname(path), { recursive: true });
294
+ appendFileSync(path, lines.map((l) => JSON.stringify(l)).join("\n") + "\n");
295
+ }
296
+ export function readRefGateLedger(path) {
297
+ if (!existsSync(path))
298
+ return [];
299
+ const out = [];
300
+ for (const line of readFileSync(path, "utf-8").split("\n").filter((l) => l.trim())) {
301
+ try {
302
+ const r = JSON.parse(line);
303
+ if (r.version === 1 && r.kind === "ref-update")
304
+ out.push(r);
305
+ }
306
+ catch {
307
+ continue;
308
+ }
309
+ }
310
+ return out;
311
+ }
312
+ /** The overrides the ledger's admitted updates spent: an update the remote took, or a status that passed, under an override. A pre-push or a plain check spends nothing. */
313
+ export function spentOverrides(lines) {
314
+ const out = [];
315
+ for (const l of lines) {
316
+ if (l.decision !== "admit" || l.push === "refused" || (l.site !== "pre-receive" && l.site !== "status"))
317
+ continue;
318
+ for (const c of l.checks) {
319
+ if (!c.override)
320
+ continue;
321
+ out.push({ version: 1, kind: "applied", op: "policy-override", gate: c.override.subject, planDigest: c.override.digest, approvedAt: c.override.at, approvedBy: c.override.by, timestamp: l.timestamp, ...(l.new ? { commit: l.new } : {}) });
322
+ }
323
+ }
324
+ return out;
325
+ }
326
+ // ── hook input ───────────────────────────────────────────────────────────
327
+ const OBJECT_ID = /^[0-9a-f]{40}([0-9a-f]{24})?$/;
328
+ const oid = (s) => (ZERO.test(s) ? null : s);
329
+ /** pre-receive's stdin: `<old> <new> <ref>` per line. */
330
+ export function parsePreReceive(text) {
331
+ return text
332
+ .split("\n")
333
+ .map((l) => l.trim())
334
+ .filter(Boolean)
335
+ .map((line) => {
336
+ const [old, nu, ref, ...rest] = line.split(/\s+/);
337
+ if (rest.length || !old || !nu || !ref || !OBJECT_ID.test(old) || !OBJECT_ID.test(nu))
338
+ throw new RefUpdateInputError(`not a pre-receive line: ${JSON.stringify(line)}`);
339
+ return { ref, old: oid(old), new: oid(nu) };
340
+ });
341
+ }
342
+ /** pre-push's stdin: `<local ref> <local sha> <remote ref> <remote sha>` per line, as updates of the remote ref. */
343
+ export function parsePrePush(text) {
344
+ return text
345
+ .split("\n")
346
+ .map((l) => l.trim())
347
+ .filter(Boolean)
348
+ .map((line) => {
349
+ const [, local, ref, remote, ...rest] = line.split(/\s+/);
350
+ if (rest.length || !local || !ref || !remote || !OBJECT_ID.test(local) || !OBJECT_ID.test(remote))
351
+ throw new RefUpdateInputError(`not a pre-push line: ${JSON.stringify(line)}`);
352
+ return { ref, old: oid(remote), new: oid(local) };
353
+ });
354
+ }
355
+ export class RefUpdateInputError extends Error {
356
+ }
357
+ /**
358
+ * The remote's pre-receive hook: decide every update of the push, with the
359
+ * notes and the `chant/lifecycle` tip the push itself brings when it brings
360
+ * them, and write one ledger line per update. git refuses the whole push when
361
+ * the hook fails, so overrides are spent only when every update is admitted.
362
+ */
363
+ export function runPreReceive(repo, stdin, opts = {}) {
364
+ const updates = parsePreReceive(stdin);
365
+ const pushed = (ref) => {
366
+ const u = updates.find((x) => x.ref === ref);
367
+ return u ? (u.new ? commitOf(repo, u.new) ?? null : null) : undefined;
368
+ };
369
+ const notes = pushed(EVIDENCE_NOTES_REF);
370
+ const lifecycle = pushed(LIFECYCLE_REF);
371
+ const ledger = opts.ledger ?? refGateLedgerPath(repo);
372
+ const spent = spentOverrides(readRefGateLedger(ledger));
373
+ const now = opts.now ?? new Date().toISOString();
374
+ const decisions = [];
375
+ for (const u of updates) {
376
+ const base = u.old ? commitOf(repo, u.old) ?? null : commitOf(repo, "HEAD") ?? null;
377
+ const d = decideRefUpdate({ repo, update: u, base, ...(notes !== undefined ? { notes } : {}), ...(lifecycle !== undefined ? { lifecycle } : {}), spent, now });
378
+ decisions.push(d);
379
+ // Two refs of one push may not spend one override twice.
380
+ spent.push(...spentOverrides([{ ...d, version: 1, kind: "ref-update", site: "pre-receive", timestamp: now }]));
381
+ }
382
+ const ok = decisions.every((d) => d.decision === "admit");
383
+ appendRefGateLedger(ledger, decisions.map((d) => ({ ...d, version: 1, kind: "ref-update", site: "pre-receive", timestamp: now, ...(ok ? {} : { push: "refused" }) })));
384
+ return { ok, decisions, ledger };
385
+ }
386
+ /**
387
+ * The developer's pre-push hook: the same decision against the local
388
+ * repository, before the network. The policy is read at the remote's tip
389
+ * when the repository holds it, else at the default base. It spends nothing.
390
+ */
391
+ export function runPrePush(repo, stdin, opts = {}) {
392
+ const updates = parsePrePush(stdin);
393
+ const ledger = opts.ledger ?? refGateLedgerPath(repo);
394
+ const now = opts.now ?? new Date().toISOString();
395
+ const decisions = updates.map((u) => {
396
+ const base = (u.old ? commitOf(repo, u.old) : undefined) ?? opts.base?.(repo) ?? null;
397
+ return decideRefUpdate({ repo, update: u, base, now });
398
+ });
399
+ appendRefGateLedger(ledger, decisions.map((d) => ({ ...d, version: 1, kind: "ref-update", site: "pre-push", timestamp: now })));
400
+ return { ok: decisions.every((d) => d.decision === "admit"), decisions, ledger };
401
+ }
402
+ // ── words ────────────────────────────────────────────────────────────────
403
+ const short = (s) => (s ? s.slice(0, 8) : "(none)");
404
+ /** What the gate says about one decision: each missing check and its command, and how to override a blocked one. */
405
+ export function describeDecision(d) {
406
+ const out = [];
407
+ const verb = d.decision === "admit" ? "admitted" : "refused";
408
+ out.push(`chant ref gate: ${verb} ${d.ref} ${short(d.old)}..${short(d.new)}`);
409
+ if (d.error) {
410
+ out.push(` ${d.error.message} (${d.error.code})`);
411
+ return out;
412
+ }
413
+ for (const c of d.checks) {
414
+ if (c.status === "current") {
415
+ out.push(` ${c.check}: ${c.reason}`);
416
+ continue;
417
+ }
418
+ const what = c.status === "overridden" ? "overridden" : c.action === "warn" ? "warning" : "missing";
419
+ out.push(` ${c.check} (${what}): ${c.reason}${c.code ? ` (${c.code})` : ""}`);
420
+ if (c.status !== "overridden")
421
+ out.push(` run: ${c.command}`);
422
+ }
423
+ const blocked = d.checks.filter((c) => c.action === "block" && (c.status === "missing" || c.status === "stale"));
424
+ if (blocked.length > 0) {
425
+ out.push(` Attach each check's evidence to ${short(d.new)} (chant workspace ref-gate attach --envelope <file> --at ${short(d.new)}) and push ${EVIDENCE_NOTES_REF} with the ref.`);
426
+ out.push(` To let ${short(d.new)} through without a check, a person listed in ${OVERRIDERS_PATH} at base can override it once the request is recorded`);
427
+ out.push(` (the pre-push hook records it, or chant workspace ref-gate check --ref ${d.ref} --at ${short(d.new)}):`);
428
+ for (const c of blocked)
429
+ out.push(` ${overrideCommand(c.subject, [c.check])}`);
430
+ }
431
+ return out;
432
+ }
@@ -25,7 +25,7 @@ import { z } from "zod";
25
25
  import type { RunEndLine, RunStartLine, RunView } from "../runs.js";
26
26
  import { type DsseEnvelope } from "./dsse.js";
27
27
  import type { KeyObject } from "node:crypto";
28
- import type { RunnerKey } from "./policy.js";
28
+ import type { Runner, RunnerKey } from "./policy.js";
29
29
  export declare const AGENT_RUN_PREDICATE = "https://intentius.io/chant/agent-run/v1";
30
30
  /** The statement a runner signs for a run. Strict, so a verifier never acts on a field it does not know. */
31
31
  export declare const runStatementSchema: z.ZodObject<{
@@ -103,7 +103,7 @@ export type RunStatementVerdict = {
103
103
  * well-formed statement whose signer is that key's principal, for this run,
104
104
  * and agreeing with its record. Never throws.
105
105
  */
106
- export declare function verifyRunStatement(envelope: unknown, runners: readonly RunnerKey[], run: RunView): RunStatementVerdict;
106
+ export declare function verifyRunStatement(envelope: unknown, runners: readonly Runner[], run: RunView): RunStatementVerdict;
107
107
  /** A run's attestation: its best statement, or why it has none. */
108
108
  export interface RunAttestation {
109
109
  /**
@@ -124,5 +124,5 @@ export interface RunAttestation {
124
124
  statements: RunStatementVerdict[];
125
125
  }
126
126
  /** Judge every statement the ledger holds for `run`. */
127
- export declare function attestRun(run: RunView, runners: readonly RunnerKey[]): RunAttestation;
127
+ export declare function attestRun(run: RunView, runners: readonly Runner[]): RunAttestation;
128
128
  //# sourceMappingURL=run-statement.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"run-statement.d.ts","sourceRoot":"","sources":["../../../src/workspace/trust/run-statement.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAGH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAGxB,OAAO,KAAK,EAAE,UAAU,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AACjE,OAAO,EAAsD,KAAK,YAAY,EAAE,MAAM,QAAQ,CAAC;AAE/F,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAC7C,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAE1C,eAAO,MAAM,mBAAmB,4CAA4C,CAAC;AAO7E,4GAA4G;AAC5G,eAAO,MAAM,kBAAkB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;kBA0BpB,CAAC;AACZ,MAAM,MAAM,YAAY,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,kBAAkB,CAAC,CAAC;AAE9D;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,YAAY,EAAE,GAAG,EAAE,UAAU,GAAG,MAAM,CAE5E;AAED,mHAAmH;AACnH,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,GAAG,YAAY,CAiB5E;AAED,8HAA8H;AAC9H,wBAAgB,mBAAmB,CAAC,SAAS,EAAE,YAAY,GAAG,MAAM,CAEnE;AAED,wBAAgB,gBAAgB,CAAC,SAA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,EAAE,MAAM,CAAC;IACf,6EAA6E;IAC7E,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,yDAAyD;IACzD,UAAU,EAAE,mBAAmB,EAAE,CAAC;CACnC;AAID,wDAAwD;AACxD,wBAAgB,SAAS,CAAC,GAAG,EAAE,OAAO,EAAE,OAAO,EAAE,SAAS,MAAM,EAAE,GAAG,cAAc,CAqBlF"}
@@ -0,0 +1,56 @@
1
+ /**
2
+ * The runner identity an init writes into `.chant/trust.json` (#4869).
3
+ *
4
+ * None of the CI products' inits wrote `.chant/trust.json`, a runner key
5
+ * or an identity, so a keyless runner had nowhere to come from. These are
6
+ * the pieces an init calls: {@link ciRunnerIdentity} names the identity the
7
+ * generated workflow signs as, on the forge it runs on, and
8
+ * {@link withRunnerIdentity} adds it to the file's text under `keyless`,
9
+ * with the principal in `roles.runner`, which ci tags and Op runs need.
10
+ *
11
+ * The subject is the certificate's subject alternative name the
12
+ * certificate authority writes for that job: on GitHub the workflow file at
13
+ * a ref (`https://github.com/<owner>/<repo>/<workflow path>@<ref>`), on
14
+ * GitLab the CI config at a ref (`<server>/<project>//<config path>@<ref>`).
15
+ * A Forgejo job's certificate comes from a private authority, whose
16
+ * issuer and subject the project names itself.
17
+ */
18
+ /** The trusted root an init points `keyless.trustedRoot` at, unless told. */
19
+ export declare const DEFAULT_TRUSTED_ROOT_PATH = ".chant/trusted_root.json";
20
+ /** One entry of `keyless.identities`. */
21
+ export interface KeylessIdentityEntry {
22
+ principal: string;
23
+ class: "runner" | "service";
24
+ issuer: string;
25
+ subject: string;
26
+ }
27
+ export interface CiRunnerIdentityInput {
28
+ forge: "github" | "gitlab" | "forgejo";
29
+ /** `owner/repo` on GitHub and Forgejo, the project path on GitLab. */
30
+ repository: string;
31
+ /** The workflow file (GitHub, Forgejo), such as `.github/workflows/chant-pr.yml`, or the CI config path (GitLab, default `.gitlab-ci.yml`). A `*` matches within the file name. */
32
+ workflow?: string;
33
+ /** The ref the job runs at. Default `refs/heads/main`. A pull request job runs at another ref, such as `refs/pull/*\/merge`. */
34
+ ref?: string;
35
+ /** The forge's URL. Default `https://github.com` or `https://gitlab.com`; required on Forgejo. */
36
+ server?: string;
37
+ /** The OIDC issuer. Default GitHub's or the GitLab server; required on Forgejo. */
38
+ issuer?: string;
39
+ /** The subject pattern, instead of the one the forge's job would have. Required on Forgejo. */
40
+ subject?: string;
41
+ /** Default `ci`. */
42
+ principal?: string;
43
+ }
44
+ /** The `keyless.identities` entry for the generated workflow's jobs on `forge`. Throws when the forge's identity cannot be worked out. */
45
+ export declare function ciRunnerIdentity(input: CiRunnerIdentityInput): KeylessIdentityEntry;
46
+ /**
47
+ * `.chant/trust.json`'s text (or none, for a new file) with `identity` under
48
+ * `keyless.identities` and its principal in `roles.runner`. An identity
49
+ * with the same issuer and subject is not added twice. `trustedRoot` is
50
+ * written when the file sets none. Throws when the result is not a valid
51
+ * trust config.
52
+ */
53
+ export declare function withRunnerIdentity(text: string | undefined, identity: KeylessIdentityEntry, opts?: {
54
+ trustedRoot?: string;
55
+ }): string;
56
+ //# sourceMappingURL=runner-identity.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"runner-identity.d.ts","sourceRoot":"","sources":["../../../src/workspace/trust/runner-identity.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAIH,6EAA6E;AAC7E,eAAO,MAAM,yBAAyB,6BAA6B,CAAC;AAEpE,yCAAyC;AACzC,MAAM,WAAW,oBAAoB;IACnC,SAAS,EAAE,MAAM,CAAC;IAClB,KAAK,EAAE,QAAQ,GAAG,SAAS,CAAC;IAC5B,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,qBAAqB;IACpC,KAAK,EAAE,QAAQ,GAAG,QAAQ,GAAG,SAAS,CAAC;IACvC,sEAAsE;IACtE,UAAU,EAAE,MAAM,CAAC;IACnB,mLAAmL;IACnL,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,gIAAgI;IAChI,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,kGAAkG;IAClG,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,mFAAmF;IACnF,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,+FAA+F;IAC/F,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,oBAAoB;IACpB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAID,0IAA0I;AAC1I,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,qBAAqB,GAAG,oBAAoB,CAiBnF;AAED;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,EAAE,QAAQ,EAAE,oBAAoB,EAAE,IAAI,GAAE;IAAE,WAAW,CAAC,EAAE,MAAM,CAAA;CAAO,GAAG,MAAM,CAaxI"}
@@ -0,0 +1,65 @@
1
+ /**
2
+ * The runner identity an init writes into `.chant/trust.json` (#4869).
3
+ *
4
+ * None of the CI products' inits wrote `.chant/trust.json`, a runner key
5
+ * or an identity, so a keyless runner had nowhere to come from. These are
6
+ * the pieces an init calls: {@link ciRunnerIdentity} names the identity the
7
+ * generated workflow signs as, on the forge it runs on, and
8
+ * {@link withRunnerIdentity} adds it to the file's text under `keyless`,
9
+ * with the principal in `roles.runner`, which ci tags and Op runs need.
10
+ *
11
+ * The subject is the certificate's subject alternative name the
12
+ * certificate authority writes for that job: on GitHub the workflow file at
13
+ * a ref (`https://github.com/<owner>/<repo>/<workflow path>@<ref>`), on
14
+ * GitLab the CI config at a ref (`<server>/<project>//<config path>@<ref>`).
15
+ * A Forgejo job's certificate comes from a private authority, whose
16
+ * issuer and subject the project names itself.
17
+ */
18
+ import { trustConfigSchema } from "./policy.js";
19
+ /** The trusted root an init points `keyless.trustedRoot` at, unless told. */
20
+ export const DEFAULT_TRUSTED_ROOT_PATH = ".chant/trusted_root.json";
21
+ const trim = (u) => u.replace(/\/+$/, "");
22
+ /** The `keyless.identities` entry for the generated workflow's jobs on `forge`. Throws when the forge's identity cannot be worked out. */
23
+ export function ciRunnerIdentity(input) {
24
+ const ref = input.ref ?? "refs/heads/main";
25
+ const principal = input.principal ?? "ci";
26
+ if (!/^[^\s/][^\s]*\/[^\s]+$/.test(input.repository))
27
+ throw new Error(`repository ${JSON.stringify(input.repository)} is not an owner/name path`);
28
+ if (input.forge === "github") {
29
+ if (!input.workflow)
30
+ throw new Error("a GitHub runner identity names its workflow file, such as .github/workflows/chant-pr.yml");
31
+ const server = trim(input.server ?? "https://github.com");
32
+ return { principal, class: "runner", issuer: input.issuer ?? "https://token.actions.githubusercontent.com", subject: input.subject ?? `${server}/${input.repository}/${input.workflow}@${ref}` };
33
+ }
34
+ if (input.forge === "gitlab") {
35
+ const server = trim(input.server ?? "https://gitlab.com");
36
+ return { principal, class: "runner", issuer: input.issuer ?? server, subject: input.subject ?? `${server}/${input.repository}//${input.workflow ?? ".gitlab-ci.yml"}@${ref}` };
37
+ }
38
+ if (!input.issuer || !input.subject) {
39
+ throw new Error("a Forgejo job's certificate comes from the certificate authority the project runs, so its runner identity names that authority's issuer and the subject it writes");
40
+ }
41
+ return { principal, class: "runner", issuer: input.issuer, subject: input.subject };
42
+ }
43
+ /**
44
+ * `.chant/trust.json`'s text (or none, for a new file) with `identity` under
45
+ * `keyless.identities` and its principal in `roles.runner`. An identity
46
+ * with the same issuer and subject is not added twice. `trustedRoot` is
47
+ * written when the file sets none. Throws when the result is not a valid
48
+ * trust config.
49
+ */
50
+ export function withRunnerIdentity(text, identity, opts = {}) {
51
+ const config = text === undefined || text.trim() === "" ? { schema: 1 } : JSON.parse(text);
52
+ const keyless = config.keyless ?? { trustedRoot: opts.trustedRoot ?? DEFAULT_TRUSTED_ROOT_PATH, identities: [] };
53
+ if (!keyless.identities.some((i) => i.issuer === identity.issuer && i.subject === identity.subject))
54
+ keyless.identities = [...keyless.identities, identity];
55
+ const runners = config.roles?.runner ?? [];
56
+ const next = {
57
+ ...config,
58
+ roles: { ...(config.roles ?? {}), runner: runners.includes(identity.principal) ? runners : [...runners, identity.principal] },
59
+ keyless,
60
+ };
61
+ const parsed = trustConfigSchema.safeParse(next);
62
+ if (!parsed.success)
63
+ throw new Error(`the trust config with the runner identity is invalid: ${parsed.error.issues.map((i) => `${i.path.join(".") || "/"}: ${i.message}`).join("; ")}`);
64
+ return `${JSON.stringify(next, null, 2)}\n`;
65
+ }
@@ -1 +1 @@
1
- {"version":3,"file":"trust-files.d.ts","sourceRoot":"","sources":["../../../src/workspace/trust/trust-files.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAIH,0GAA0G;AAC1G,eAAO,MAAM,iBAAiB,sBAAsB,CAAC;AAErD,mEAAmE;AACnE,eAAO,MAAM,oBAAoB,2BAA2B,CAAC;AAe7D,uEAAuE;AACvE,MAAM,WAAW,MAAM;IACrB,SAAS,EAAE,MAAM,CAAC;IAClB,0CAA0C;IAC1C,GAAG,EAAE,MAAM,CAAC;IACZ,kGAAkG;IAClG,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,wCAAwC;IACxC,IAAI,EAAE,MAAM,CAAC;CACd;AAED,4DAA4D;AAC5D,MAAM,WAAW,cAAc;IAC7B,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,SAAS;IACxB,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,QAAQ,EAAE,cAAc,EAAE,CAAC;CAC5B;AAmDD;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,CAkD3D;AAED,8FAA8F;AAC9F,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,MAAM,CAM9D;AASD,yEAAyE;AACzE,wBAAgB,UAAU,CAAC,CAAC,EAAE,OAAO,GAAG,CAAC,IAAI,MAAM,CAElD;AAOD;;;;;GAKG;AACH,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,OAAO,GAAG,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