@intentius/chant 0.124.40 → 0.124.42

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 (346) hide show
  1. package/dist/__fixtures__/oidc-emulator.d.ts +49 -0
  2. package/dist/__fixtures__/oidc-emulator.d.ts.map +1 -0
  3. package/dist/__fixtures__/oidc-emulator.js +89 -0
  4. package/dist/ci-handover.d.ts +117 -0
  5. package/dist/ci-handover.d.ts.map +1 -0
  6. package/dist/ci-handover.js +217 -0
  7. package/dist/ci-identity.d.ts +190 -0
  8. package/dist/ci-identity.d.ts.map +1 -0
  9. package/dist/ci-identity.js +219 -0
  10. package/dist/cli/handlers/approve.d.ts +2 -1
  11. package/dist/cli/handlers/approve.d.ts.map +1 -1
  12. package/dist/cli/handlers/approve.js +20 -6
  13. package/dist/cli/handlers/build.d.ts.map +1 -1
  14. package/dist/cli/handlers/build.js +18 -1
  15. package/dist/cli/handlers/fan-out.d.ts.map +1 -1
  16. package/dist/cli/handlers/fan-out.js +42 -5
  17. package/dist/cli/handlers/gate-resume.d.ts +3 -0
  18. package/dist/cli/handlers/gate-resume.d.ts.map +1 -1
  19. package/dist/cli/handlers/gate-resume.js +15 -2
  20. package/dist/cli/handlers/lock.d.ts +8 -0
  21. package/dist/cli/handlers/lock.d.ts.map +1 -0
  22. package/dist/cli/handlers/lock.js +197 -0
  23. package/dist/cli/handlers/pr-env.d.ts +34 -0
  24. package/dist/cli/handlers/pr-env.d.ts.map +1 -0
  25. package/dist/cli/handlers/pr-env.js +436 -0
  26. package/dist/cli/handlers/pr.d.ts +76 -0
  27. package/dist/cli/handlers/pr.d.ts.map +1 -1
  28. package/dist/cli/handlers/pr.js +615 -55
  29. package/dist/cli/handlers/promote.d.ts.map +1 -1
  30. package/dist/cli/handlers/promote.js +9 -0
  31. package/dist/cli/handlers/query.d.ts +6 -0
  32. package/dist/cli/handlers/query.d.ts.map +1 -0
  33. package/dist/cli/handlers/query.js +80 -0
  34. package/dist/cli/handlers/run.d.ts.map +1 -1
  35. package/dist/cli/handlers/run.js +11 -0
  36. package/dist/cli/main.d.ts.map +1 -1
  37. package/dist/cli/main.js +49 -4
  38. package/dist/cli/parse-args.d.ts.map +1 -1
  39. package/dist/cli/parse-args.js +46 -2
  40. package/dist/cli/registry.d.ts +31 -2
  41. package/dist/cli/registry.d.ts.map +1 -1
  42. package/dist/components/capability.d.ts +47 -0
  43. package/dist/components/capability.d.ts.map +1 -1
  44. package/dist/components/capability.js +44 -0
  45. package/dist/components/cli-support.d.ts +6 -0
  46. package/dist/components/cli-support.d.ts.map +1 -1
  47. package/dist/components/cli-support.js +8 -1
  48. package/dist/components/driver.d.ts +10 -0
  49. package/dist/components/driver.d.ts.map +1 -1
  50. package/dist/components/driver.js +52 -2
  51. package/dist/components/fan-out-run.d.ts +37 -0
  52. package/dist/components/fan-out-run.d.ts.map +1 -1
  53. package/dist/components/fan-out-run.js +44 -4
  54. package/dist/components/gate-plan.d.ts +2 -0
  55. package/dist/components/gate-plan.d.ts.map +1 -1
  56. package/dist/components/gate-plan.js +1 -0
  57. package/dist/components/pr-environment.d.ts +298 -0
  58. package/dist/components/pr-environment.d.ts.map +1 -0
  59. package/dist/components/pr-environment.js +406 -0
  60. package/dist/components/pr-hooks.d.ts +66 -0
  61. package/dist/components/pr-hooks.d.ts.map +1 -0
  62. package/dist/components/pr-hooks.js +119 -0
  63. package/dist/components/pr-pipeline.d.ts +92 -2
  64. package/dist/components/pr-pipeline.d.ts.map +1 -1
  65. package/dist/components/pr-pipeline.js +150 -21
  66. package/dist/components/pr-policy.d.ts +109 -0
  67. package/dist/components/pr-policy.d.ts.map +1 -0
  68. package/dist/components/pr-policy.js +150 -0
  69. package/dist/components/pr-run.d.ts +6 -0
  70. package/dist/components/pr-run.d.ts.map +1 -1
  71. package/dist/components/pr-run.js +8 -3
  72. package/dist/components/wave-plan.d.ts +14 -1
  73. package/dist/components/wave-plan.d.ts.map +1 -1
  74. package/dist/components/wave-plan.js +34 -2
  75. package/dist/gate-decision.d.ts +15 -2
  76. package/dist/gate-decision.d.ts.map +1 -1
  77. package/dist/gate-decision.js +17 -2
  78. package/dist/gate-policy.d.ts +140 -0
  79. package/dist/gate-policy.d.ts.map +1 -0
  80. package/dist/gate-policy.js +242 -0
  81. package/dist/lifecycle/gate-checkpoint.d.ts +1 -1
  82. package/dist/lifecycle/gate-checkpoint.js +1 -1
  83. package/dist/{workspace/trust → lifecycle}/gate-seal.d.ts +1 -1
  84. package/dist/lifecycle/gate-seal.d.ts.map +1 -0
  85. package/dist/lifecycle/query.d.ts +68 -0
  86. package/dist/lifecycle/query.d.ts.map +1 -0
  87. package/dist/lifecycle/query.js +246 -0
  88. package/dist/lifecycle/run-ledger.d.ts +13 -0
  89. package/dist/lifecycle/run-ledger.d.ts.map +1 -1
  90. package/dist/lifecycle/run-ledger.js +16 -0
  91. package/dist/lifecycle/target-lock.d.ts +97 -0
  92. package/dist/lifecycle/target-lock.d.ts.map +1 -0
  93. package/dist/lifecycle/target-lock.js +286 -0
  94. package/dist/op/activities/activity-contracts.d.ts +30 -0
  95. package/dist/op/activities/activity-contracts.d.ts.map +1 -1
  96. package/dist/op/activities/activity-contracts.js +26 -0
  97. package/dist/op/activities/index.d.ts +2 -0
  98. package/dist/op/activities/index.d.ts.map +1 -1
  99. package/dist/op/activities/index.js +4 -0
  100. package/dist/op/activities/pr-refs.d.ts +10 -0
  101. package/dist/op/activities/pr-refs.d.ts.map +1 -0
  102. package/dist/op/activities/pr-refs.js +26 -0
  103. package/dist/op/activities/predict-behaviour.d.ts +2 -4
  104. package/dist/op/activities/predict-behaviour.d.ts.map +1 -1
  105. package/dist/op/activities/predict-behaviour.js +2 -21
  106. package/dist/op/activities/review-agent.d.ts +137 -0
  107. package/dist/op/activities/review-agent.d.ts.map +1 -0
  108. package/dist/op/activities/review-agent.js +266 -0
  109. package/dist/op/builders.d.ts +9 -0
  110. package/dist/op/builders.d.ts.map +1 -1
  111. package/dist/op/builders.js +11 -0
  112. package/dist/op/cron.d.ts +8 -0
  113. package/dist/op/cron.d.ts.map +1 -1
  114. package/dist/op/cron.js +51 -0
  115. package/dist/op/gate-approval.d.ts +20 -13
  116. package/dist/op/gate-approval.d.ts.map +1 -1
  117. package/dist/op/gate-approval.js +13 -18
  118. package/dist/op/gate-quorum.d.ts +131 -0
  119. package/dist/op/gate-quorum.d.ts.map +1 -0
  120. package/dist/op/gate-quorum.js +183 -0
  121. package/dist/op/gate-resume.d.ts +4 -2
  122. package/dist/op/gate-resume.d.ts.map +1 -1
  123. package/dist/op/gate-resume.js +5 -4
  124. package/dist/op/gate-review.js +1 -1
  125. package/dist/op/gate.d.ts +32 -4
  126. package/dist/op/gate.d.ts.map +1 -1
  127. package/dist/op/gate.js +40 -24
  128. package/dist/op/index.d.ts +6 -4
  129. package/dist/op/index.d.ts.map +1 -1
  130. package/dist/op/index.js +4 -3
  131. package/dist/op/local-executor.d.ts.map +1 -1
  132. package/dist/op/local-executor.js +9 -3
  133. package/dist/op/op-waves-run.d.ts +28 -43
  134. package/dist/op/op-waves-run.d.ts.map +1 -1
  135. package/dist/op/op-waves-run.js +170 -120
  136. package/dist/op/op-waves.d.ts +55 -3
  137. package/dist/op/op-waves.d.ts.map +1 -1
  138. package/dist/op/op-waves.js +63 -8
  139. package/dist/op/schedule-overdue.d.ts +57 -0
  140. package/dist/op/schedule-overdue.d.ts.map +1 -0
  141. package/dist/op/schedule-overdue.js +138 -0
  142. package/dist/op/stage-hooks.d.ts +132 -0
  143. package/dist/op/stage-hooks.d.ts.map +1 -0
  144. package/dist/op/stage-hooks.js +142 -0
  145. package/dist/override-gate.d.ts +2 -2
  146. package/dist/override-gate.d.ts.map +1 -1
  147. package/dist/override-gate.js +1 -1
  148. package/dist/pr-forge.d.ts +64 -0
  149. package/dist/pr-forge.d.ts.map +1 -1
  150. package/dist/pr-forge.js +331 -44
  151. package/dist/pr-loop.d.ts +107 -1
  152. package/dist/pr-loop.d.ts.map +1 -1
  153. package/dist/pr-loop.js +84 -3
  154. package/dist/pr-post.d.ts +72 -0
  155. package/dist/pr-post.d.ts.map +1 -0
  156. package/dist/pr-post.js +134 -0
  157. package/dist/pr-requires.d.ts +38 -0
  158. package/dist/pr-requires.d.ts.map +1 -0
  159. package/dist/pr-requires.js +48 -0
  160. package/dist/wave-blocked.d.ts +79 -0
  161. package/dist/wave-blocked.d.ts.map +1 -0
  162. package/dist/wave-blocked.js +245 -0
  163. package/dist/wave-engine.d.ts +6 -0
  164. package/dist/wave-engine.d.ts.map +1 -1
  165. package/dist/wave-engine.js +2 -0
  166. package/dist/wave-runtime.mjs +243 -243
  167. package/dist/workspace/__fixtures__/two-member-workspace.d.ts +1 -0
  168. package/dist/workspace/__fixtures__/two-member-workspace.d.ts.map +1 -1
  169. package/dist/workspace/__fixtures__/two-member-workspace.js +1 -1
  170. package/dist/workspace/checks/ci-workflow.d.ts.map +1 -1
  171. package/dist/workspace/checks/ci-workflow.js +19 -4
  172. package/dist/workspace/ci-branches.d.ts +51 -0
  173. package/dist/workspace/ci-branches.d.ts.map +1 -0
  174. package/dist/workspace/ci-branches.js +69 -0
  175. package/dist/workspace/ci-cloud-identity.d.ts +11 -0
  176. package/dist/workspace/ci-cloud-identity.d.ts.map +1 -0
  177. package/dist/workspace/ci-cloud-identity.js +24 -0
  178. package/dist/workspace/ci-init.d.ts.map +1 -1
  179. package/dist/workspace/ci-init.js +5 -4
  180. package/dist/workspace/ci-locks.d.ts +12 -0
  181. package/dist/workspace/ci-locks.d.ts.map +1 -0
  182. package/dist/workspace/ci-locks.js +6 -0
  183. package/dist/workspace/ci-run.d.ts +17 -1
  184. package/dist/workspace/ci-run.d.ts.map +1 -1
  185. package/dist/workspace/ci-run.js +66 -4
  186. package/dist/workspace/ci-workflow-at.d.ts +14 -0
  187. package/dist/workspace/ci-workflow-at.d.ts.map +1 -0
  188. package/dist/workspace/ci-workflow-at.js +17 -0
  189. package/dist/workspace/ci-workflow.d.ts +11 -3
  190. package/dist/workspace/ci-workflow.d.ts.map +1 -1
  191. package/dist/workspace/ci-workflow.js +10 -2
  192. package/dist/workspace/declaration.d.ts +69 -3
  193. package/dist/workspace/declaration.d.ts.map +1 -1
  194. package/dist/workspace/declaration.js +72 -1
  195. package/dist/workspace/declaration.schema.json +149 -2
  196. package/dist/workspace/identity-gate-rule.d.ts +1 -1
  197. package/dist/workspace/identity-gate-rule.d.ts.map +1 -1
  198. package/dist/workspace/identity-gate-rule.js +1 -1
  199. package/dist/workspace/identity.d.ts +1 -1
  200. package/dist/workspace/identity.d.ts.map +1 -1
  201. package/dist/workspace/lineage-upgrade.d.ts.map +1 -1
  202. package/dist/workspace/lineage-upgrade.js +8 -5
  203. package/dist/workspace/member-pipeline.d.ts +6 -0
  204. package/dist/workspace/member-pipeline.d.ts.map +1 -1
  205. package/dist/workspace/member-pipeline.js +23 -0
  206. package/dist/workspace/pr-report.schema.json +94 -3
  207. package/dist/workspace/reason-codes.d.ts +1 -1
  208. package/dist/workspace/reason-codes.d.ts.map +1 -1
  209. package/dist/workspace/reason-codes.js +1 -1
  210. package/dist/workspace/security.d.ts.map +1 -1
  211. package/dist/workspace/security.js +4 -0
  212. package/dist/workspace/status-gates.d.ts +3 -2
  213. package/dist/workspace/status-gates.d.ts.map +1 -1
  214. package/dist/workspace/status-gates.js +8 -7
  215. package/dist/workspace/status.d.ts.map +1 -1
  216. package/dist/workspace/status.js +4 -1
  217. package/dist/workspace/trust/seal.d.ts +2 -2
  218. package/dist/workspace/trust/seal.d.ts.map +1 -1
  219. package/dist/workspace/trust/seal.js +2 -2
  220. package/package.json +1 -1
  221. package/src/__fixtures__/oidc-emulator.ts +118 -0
  222. package/src/ci-handover.test.ts +209 -0
  223. package/src/ci-handover.ts +237 -0
  224. package/src/ci-identity.test.ts +201 -0
  225. package/src/ci-identity.ts +310 -0
  226. package/src/cli/handlers/approve.ts +18 -5
  227. package/src/cli/handlers/build.ts +18 -1
  228. package/src/cli/handlers/fan-out.ts +43 -6
  229. package/src/cli/handlers/gate-resume.ts +16 -2
  230. package/src/cli/handlers/lock.test.ts +98 -0
  231. package/src/cli/handlers/lock.ts +202 -0
  232. package/src/cli/handlers/operator.test.ts +3 -1
  233. package/src/cli/handlers/pr-before-merge.test.ts +276 -0
  234. package/src/cli/handlers/pr-env.ts +462 -0
  235. package/src/cli/handlers/pr-waves-blocked.test.ts +247 -0
  236. package/src/cli/handlers/pr.test.ts +184 -0
  237. package/src/cli/handlers/pr.ts +605 -61
  238. package/src/cli/handlers/promote.ts +9 -0
  239. package/src/cli/handlers/query.ts +78 -0
  240. package/src/cli/handlers/run.ts +10 -0
  241. package/src/cli/main.ts +49 -4
  242. package/src/cli/parse-args.ts +34 -2
  243. package/src/cli/registry.ts +31 -2
  244. package/src/components/capability-targets.test.ts +162 -0
  245. package/src/components/capability.ts +71 -0
  246. package/src/components/cli-support.ts +14 -1
  247. package/src/components/driver.ts +54 -3
  248. package/src/components/fan-out-run.ts +58 -3
  249. package/src/components/fan-out-waves.test.ts +45 -1
  250. package/src/components/gate-plan.ts +3 -0
  251. package/src/components/pr-environment.test.ts +249 -0
  252. package/src/components/pr-environment.ts +538 -0
  253. package/src/components/pr-hooks.test.ts +118 -0
  254. package/src/components/pr-hooks.ts +157 -0
  255. package/src/components/pr-pipeline.ts +194 -21
  256. package/src/components/pr-policy.test.ts +145 -0
  257. package/src/components/pr-policy.ts +211 -0
  258. package/src/components/pr-run.ts +14 -2
  259. package/src/components/wave-plan.ts +38 -3
  260. package/src/gate-decision-bundle.test.ts +4 -3
  261. package/src/gate-decision.ts +39 -2
  262. package/src/gate-policy-bundle.test.ts +28 -0
  263. package/src/gate-policy.test.ts +162 -0
  264. package/src/gate-policy.ts +298 -0
  265. package/src/lifecycle/gate-checkpoint.ts +1 -1
  266. package/src/{workspace/trust → lifecycle}/gate-seal.ts +1 -1
  267. package/src/lifecycle/query.test.ts +91 -0
  268. package/src/lifecycle/query.ts +315 -0
  269. package/src/lifecycle/run-ledger.ts +18 -0
  270. package/src/lifecycle/target-lock.test.ts +195 -0
  271. package/src/lifecycle/target-lock.ts +347 -0
  272. package/src/op/activities/activity-contracts.ts +31 -0
  273. package/src/op/activities/index.ts +6 -0
  274. package/src/op/activities/pr-refs.ts +28 -0
  275. package/src/op/activities/predict-behaviour.ts +3 -23
  276. package/src/op/activities/review-agent.test.ts +240 -0
  277. package/src/op/activities/review-agent.ts +355 -0
  278. package/src/op/builders.ts +13 -0
  279. package/src/op/cron.ts +44 -0
  280. package/src/op/gate-alias.test.ts +2 -2
  281. package/src/op/gate-approval.test.ts +30 -9
  282. package/src/op/gate-approval.ts +23 -26
  283. package/src/op/gate-quorum.test.ts +139 -0
  284. package/src/op/gate-quorum.ts +248 -0
  285. package/src/op/gate-resume.ts +6 -3
  286. package/src/op/gate-review.ts +1 -1
  287. package/src/op/gate.ts +64 -23
  288. package/src/op/index.ts +13 -1
  289. package/src/op/local-executor.ts +9 -2
  290. package/src/op/op-waves-hook-targets.test.ts +126 -0
  291. package/src/op/op-waves-policy-hold.test.ts +66 -0
  292. package/src/op/op-waves-quorum.test.ts +101 -0
  293. package/src/op/op-waves-run.ts +197 -183
  294. package/src/op/op-waves.ts +83 -10
  295. package/src/op/schedule-overdue.test.ts +220 -0
  296. package/src/op/schedule-overdue.ts +161 -0
  297. package/src/op/stage-hooks.ts +251 -0
  298. package/src/override-gate-bundle.test.ts +1 -1
  299. package/src/override-gate.ts +2 -2
  300. package/src/pr-forge-bundle.test.ts +2 -2
  301. package/src/pr-forge.test.ts +33 -0
  302. package/src/pr-forge.ts +347 -35
  303. package/src/pr-loop-bundle.test.ts +2 -2
  304. package/src/pr-loop.test.ts +58 -0
  305. package/src/pr-loop.ts +170 -4
  306. package/src/pr-post.test.ts +141 -0
  307. package/src/pr-post.ts +159 -0
  308. package/src/pr-requires.test.ts +184 -0
  309. package/src/pr-requires.ts +79 -0
  310. package/src/review-agent-bundle.test.ts +35 -0
  311. package/src/stage-hooks-bundle.test.ts +38 -0
  312. package/src/wave-blocked-bundle.test.ts +25 -0
  313. package/src/wave-blocked.test.ts +146 -0
  314. package/src/wave-blocked.ts +271 -0
  315. package/src/wave-engine.ts +8 -0
  316. package/src/workspace/__fixtures__/two-member-workspace.ts +2 -2
  317. package/src/workspace/checks/ci-workflow.test.ts +9 -0
  318. package/src/workspace/checks/ci-workflow.ts +18 -4
  319. package/src/workspace/ci-branches.test.ts +62 -0
  320. package/src/workspace/ci-branches.ts +79 -0
  321. package/src/workspace/ci-cloud-identity.test.ts +53 -0
  322. package/src/workspace/ci-cloud-identity.ts +23 -0
  323. package/src/workspace/ci-init.ts +5 -4
  324. package/src/workspace/ci-locks.ts +16 -0
  325. package/src/workspace/ci-run-index.test.ts +2 -1
  326. package/src/workspace/ci-run.test.ts +107 -8
  327. package/src/workspace/ci-run.ts +74 -5
  328. package/src/workspace/ci-workflow-at.ts +23 -0
  329. package/src/workspace/ci-workflow.ts +13 -2
  330. package/src/workspace/declaration.schema.json +149 -2
  331. package/src/workspace/declaration.test.ts +76 -0
  332. package/src/workspace/declaration.ts +140 -5
  333. package/src/workspace/identity-gate-rule.ts +1 -1
  334. package/src/workspace/identity.ts +1 -1
  335. package/src/workspace/lineage-upgrade.ts +11 -8
  336. package/src/workspace/member-pipeline.test.ts +29 -0
  337. package/src/workspace/member-pipeline.ts +30 -0
  338. package/src/workspace/pr-report.schema.json +94 -3
  339. package/src/workspace/reason-codes.ts +1 -1
  340. package/src/workspace/security.ts +4 -0
  341. package/src/workspace/status-gates.ts +9 -6
  342. package/src/workspace/status.ts +4 -1
  343. package/src/workspace/trust/gate-seal.test.ts +1 -1
  344. package/src/workspace/trust/seal.ts +2 -2
  345. package/dist/workspace/trust/gate-seal.d.ts.map +0 -1
  346. /package/dist/{workspace/trust → lifecycle}/gate-seal.js +0 -0
@@ -0,0 +1,49 @@
1
+ /**
2
+ * A local emulator of every service the OIDC trade (#4296, ../ci-identity.ts)
3
+ * talks to, for tests: the forge's token request URL, AWS STS, Google's STS
4
+ * and IAM credentials, and Entra's token endpoint. It answers with test
5
+ * values only and keeps every request, so a test can say which identity
6
+ * was asked for and how many tokens were minted.
7
+ */
8
+ import type { CloudTradeSpec } from "../ci-identity.js";
9
+ /** What the emulator was asked. */
10
+ export interface EmulatorLog {
11
+ /** Each OIDC token minted: its audience. */
12
+ minted: string[];
13
+ /** Each AWS role assumed, with the token it was assumed with. */
14
+ assumed: Array<{
15
+ role: string;
16
+ token: string;
17
+ }>;
18
+ /** Each GCP service account impersonated. */
19
+ impersonated: string[];
20
+ /** Each Azure client a token was traded for, with the token. */
21
+ clients: Array<{
22
+ clientId: string;
23
+ token: string;
24
+ }>;
25
+ }
26
+ export interface OidcEmulator {
27
+ url: string;
28
+ log: EmulatorLog;
29
+ /** The forge's token request variables, as a GitHub or Forgejo job with `id-token: write` has them. */
30
+ requestEnv: {
31
+ ACTIONS_ID_TOKEN_REQUEST_URL: string;
32
+ ACTIONS_ID_TOKEN_REQUEST_TOKEN: string;
33
+ };
34
+ /** `spec` with each cloud pointed at the emulator. */
35
+ pointAt(spec: CloudTradeSpec): CloudTradeSpec;
36
+ close(): Promise<void>;
37
+ }
38
+ /** The request token the emulator accepts, and the prefix of every token it mints. */
39
+ export declare const EMULATED_REQUEST_TOKEN = "emulated-oidc-request-token-4296";
40
+ export declare const EMULATED_JWT_PREFIX = "eyJemulated4296";
41
+ /** The test credentials the emulator hands out for an identity: they name it, so a test can tell which was traded. */
42
+ export declare function emulatedCredentials(identity: string): {
43
+ accessKeyId: string;
44
+ secret: string;
45
+ token: string;
46
+ };
47
+ /** Start the emulator on a free local port. */
48
+ export declare function startOidcEmulator(): Promise<OidcEmulator>;
49
+ //# sourceMappingURL=oidc-emulator.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"oidc-emulator.d.ts","sourceRoot":"","sources":["../../src/__fixtures__/oidc-emulator.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAIH,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAErD,mCAAmC;AACnC,MAAM,WAAW,WAAW;IAC1B,4CAA4C;IAC5C,MAAM,EAAE,MAAM,EAAE,CAAC;IACjB,iEAAiE;IACjE,OAAO,EAAE,KAAK,CAAC;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IAChD,6CAA6C;IAC7C,YAAY,EAAE,MAAM,EAAE,CAAC;IACvB,gEAAgE;IAChE,OAAO,EAAE,KAAK,CAAC;QAAE,QAAQ,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;CACrD;AAED,MAAM,WAAW,YAAY;IAC3B,GAAG,EAAE,MAAM,CAAC;IACZ,GAAG,EAAE,WAAW,CAAC;IACjB,uGAAuG;IACvG,UAAU,EAAE;QAAE,4BAA4B,EAAE,MAAM,CAAC;QAAC,8BAA8B,EAAE,MAAM,CAAA;KAAE,CAAC;IAC7F,sDAAsD;IACtD,OAAO,CAAC,IAAI,EAAE,cAAc,GAAG,cAAc,CAAC;IAC9C,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACxB;AAED,sFAAsF;AACtF,eAAO,MAAM,sBAAsB,qCAAqC,CAAC;AACzE,eAAO,MAAM,mBAAmB,oBAAoB,CAAC;AAErD,sHAAsH;AACtH,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,MAAM,GAAG;IAAE,WAAW,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAG5G;AASD,+CAA+C;AAC/C,wBAAsB,iBAAiB,IAAI,OAAO,CAAC,YAAY,CAAC,CAiE/D"}
@@ -0,0 +1,89 @@
1
+ /**
2
+ * A local emulator of every service the OIDC trade (#4296, ../ci-identity.ts)
3
+ * talks to, for tests: the forge's token request URL, AWS STS, Google's STS
4
+ * and IAM credentials, and Entra's token endpoint. It answers with test
5
+ * values only and keeps every request, so a test can say which identity
6
+ * was asked for and how many tokens were minted.
7
+ */
8
+ import { createServer } from "node:http";
9
+ /** The request token the emulator accepts, and the prefix of every token it mints. */
10
+ export const EMULATED_REQUEST_TOKEN = "emulated-oidc-request-token-4296";
11
+ export const EMULATED_JWT_PREFIX = "eyJemulated4296";
12
+ /** The test credentials the emulator hands out for an identity: they name it, so a test can tell which was traded. */
13
+ export function emulatedCredentials(identity) {
14
+ const tag = identity.replace(/[^A-Za-z0-9]/g, "").slice(-24);
15
+ return { accessKeyId: `ASIATEST${tag}`, secret: `secret-${tag}`, token: `session-${tag}` };
16
+ }
17
+ const body = (req) => new Promise((ok) => {
18
+ let b = "";
19
+ req.on("data", (c) => (b += c));
20
+ req.on("end", () => ok(b));
21
+ });
22
+ /** Start the emulator on a free local port. */
23
+ export async function startOidcEmulator() {
24
+ const log = { minted: [], assumed: [], impersonated: [], clients: [] };
25
+ const server = createServer(async (req, res) => {
26
+ const url = new URL(req.url ?? "/", "http://127.0.0.1");
27
+ const text = await body(req);
28
+ const send = (status, payload, type = "application/json") => {
29
+ res.writeHead(status, { "content-type": type });
30
+ res.end(typeof payload === "string" ? payload : JSON.stringify(payload));
31
+ };
32
+ if (url.pathname === "/oidc") {
33
+ if (req.headers.authorization !== `bearer ${EMULATED_REQUEST_TOKEN}`)
34
+ return send(401, { message: "bad request token" });
35
+ const aud = url.searchParams.get("audience") ?? "";
36
+ log.minted.push(aud);
37
+ return send(200, { value: `${EMULATED_JWT_PREFIX}.${Buffer.from(aud).toString("base64url")}.${log.minted.length}` });
38
+ }
39
+ if (url.pathname === "/aws/") {
40
+ const form = new URLSearchParams(text);
41
+ const role = form.get("RoleArn") ?? "";
42
+ const token = form.get("WebIdentityToken") ?? "";
43
+ if (form.get("Action") !== "AssumeRoleWithWebIdentity" || !token.startsWith("eyJ"))
44
+ return send(400, "<Error>bad request</Error>", "text/xml");
45
+ log.assumed.push({ role, token });
46
+ const c = emulatedCredentials(role);
47
+ return send(200, `<AssumeRoleWithWebIdentityResponse><AssumeRoleWithWebIdentityResult><Credentials><AccessKeyId>${c.accessKeyId}</AccessKeyId><SecretAccessKey>${c.secret}</SecretAccessKey><SessionToken>${c.token}</SessionToken><Expiration>2026-10-22T23:00:00Z</Expiration></Credentials></AssumeRoleWithWebIdentityResult></AssumeRoleWithWebIdentityResponse>`, "text/xml");
48
+ }
49
+ if (url.pathname === "/gcp-sts/v1/token") {
50
+ const j = JSON.parse(text);
51
+ if (!j.subjectToken?.startsWith("eyJ"))
52
+ return send(400, { error: "bad subject token" });
53
+ return send(200, { access_token: "federated-test-token", token_type: "Bearer" });
54
+ }
55
+ const sa = /^\/gcp-iam\/v1\/projects\/-\/serviceAccounts\/([^/:]+):generateAccessToken$/.exec(url.pathname);
56
+ if (sa) {
57
+ if (req.headers.authorization !== "Bearer federated-test-token")
58
+ return send(401, { error: "bad federated token" });
59
+ const name = decodeURIComponent(sa[1]);
60
+ log.impersonated.push(name);
61
+ return send(200, { accessToken: `ya29.${emulatedCredentials(name).token}`, expireTime: "2026-10-22T23:00:00Z" });
62
+ }
63
+ if (/^\/azure\/[^/]+\/oauth2\/v2\.0\/token$/.test(url.pathname)) {
64
+ const form = new URLSearchParams(text);
65
+ const clientId = form.get("client_id") ?? "";
66
+ const token = form.get("client_assertion") ?? "";
67
+ if (form.get("grant_type") !== "client_credentials" || !token.startsWith("eyJ"))
68
+ return send(400, { error: "bad grant" });
69
+ log.clients.push({ clientId, token });
70
+ return send(200, { access_token: `azure.${emulatedCredentials(clientId).token}`, token_type: "Bearer" });
71
+ }
72
+ send(404, { message: `no route ${url.pathname}` });
73
+ });
74
+ await new Promise((ok) => server.listen(0, "127.0.0.1", ok));
75
+ const url = `http://127.0.0.1:${server.address().port}`;
76
+ return {
77
+ url,
78
+ log,
79
+ requestEnv: { ACTIONS_ID_TOKEN_REQUEST_URL: `${url}/oidc?api-version=2.0`, ACTIONS_ID_TOKEN_REQUEST_TOKEN: EMULATED_REQUEST_TOKEN },
80
+ pointAt(spec) {
81
+ return {
82
+ ...(spec.aws ? { aws: { ...spec.aws, endpoint: `${url}/aws` } } : {}),
83
+ ...(spec.gcp ? { gcp: { ...spec.gcp, stsEndpoint: `${url}/gcp-sts`, iamEndpoint: `${url}/gcp-iam` } } : {}),
84
+ ...(spec.azure ? { azure: { ...spec.azure, authority: `${url}/azure` } } : {}),
85
+ };
86
+ },
87
+ close: () => new Promise((ok) => server.close(() => ok())),
88
+ };
89
+ }
@@ -0,0 +1,117 @@
1
+ /**
2
+ * The forge token handover (#4295): no step that runs a pull request's code
3
+ * holds a forge token.
4
+ *
5
+ * A pull request's plan runs its code: the synth of its components, the
6
+ * plans their capabilities run, its build and lint, and its hooks. A token
7
+ * that can post a note or set a status can also push, so the generated plan
8
+ * job uses the job's token for the checkout's fetches alone and drops it
9
+ * before anything else runs. The ordering is terragucci's `handover.ts`
10
+ * (INTENTIUS/terragucci e6cb431, packages/terragucci/src/handover.ts):
11
+ *
12
+ * 1. The checkout is a script ({@link checkoutScript}), not actions/checkout,
13
+ * whose post step runs at the job's end with the token as its input. The
14
+ * token reaches its fetches through the environment, never the git config.
15
+ * 2. Every later step starts again without the token variables
16
+ * ({@link dropForgeTokens}).
17
+ * 3. The plan step is the job's last. A step after it would start beside
18
+ * whatever the change's code left running, with what it wrote to
19
+ * GITHUB_ENV and GITHUB_PATH. So the plan step hands its report directory
20
+ * on as the job's outputs ({@link handoverTrap}) in place of an artifact
21
+ * upload.
22
+ * 4. A fresh job, `plan-note`, checks out the target branch's commit, takes
23
+ * the report back ({@link takeOverScript}), keeps it as the artifact, and
24
+ * posts the note and the status with the token (`chant components
25
+ * pr-post`, ./pr-post.ts). It reads the report as data.
26
+ *
27
+ * `chant ci run plan` keeps the same rule: no step of the plan job gets a
28
+ * forge token, and the runner posts from the report once the steps settle
29
+ * (../workspace/ci-run.ts).
30
+ */
31
+ import type { ForgeKind } from "./pr-forge.js";
32
+ /**
33
+ * The variables a runner, a job or a secret may hold a forge token in.
34
+ * A step that runs the change's code starts without them. The list is
35
+ * terragucci's `STEP_TOKEN_VARS` (render.ts:1787) with chant's own names
36
+ * (`FORGE_TOKEN_ENV` in ./pr-forge.ts) and GitLab's added.
37
+ */
38
+ export declare const FORGE_TOKEN_VARS: readonly ["CHANT_FORGE_TOKEN", "CHANT_GITHUB_TOKEN", "CHANT_FORGEJO_TOKEN", "CHANT_GITLAB_TOKEN", "CHANT_CHECKOUT_TOKEN", "GITHUB_TOKEN", "GH_TOKEN", "FORGEJO_TOKEN", "GITEA_TOKEN", "GITLAB_TOKEN", "CI_JOB_TOKEN", "ACTIONS_RUNTIME_TOKEN"];
39
+ /** `env` without {@link FORGE_TOKEN_VARS}. */
40
+ export declare function withoutForgeTokens<T extends Record<string, string | undefined>>(env: T): T;
41
+ /** Whether `env` holds a forge token under one of {@link FORGE_TOKEN_VARS}. */
42
+ export declare function holdsForgeToken(env: Record<string, string | undefined>): boolean;
43
+ /** The marker a step {@link dropForgeTokens} restarted carries. */
44
+ export declare const NO_FORGE_TOKEN_MARKER = "CHANT_NO_FORGE_TOKEN";
45
+ /**
46
+ * The first line of a step that runs the change's code: restart the step's
47
+ * shell once without the token variables in its process environment, or,
48
+ * when it cannot restart, unset them. Unsetting alone leaves the values in
49
+ * the shell's own initial environment, which a child reads on Linux from
50
+ * `/proc/<pid>/environ`; the restart replaces it. A forge runs a step's
51
+ * script as a file (`sh -e {0}` in a GitHub or Forgejo container job), so
52
+ * `$0` is that file. terragucci's `restartWithout` (render.ts:1826).
53
+ */
54
+ export declare function dropForgeTokens(shell?: "sh" | "bash", extra?: readonly string[]): string;
55
+ /** The line a GitLab plan job starts its script with: GitLab runs the script in one shell, so the unset holds for every line after it. */
56
+ export declare const GITLAB_DROP_FORGE_TOKENS: string;
57
+ /** The forges whose plan job hands its report on through job outputs. GitLab passes the artifact to the next job itself. */
58
+ export type HandoverForge = Exclude<ForgeKind, "gitlab">;
59
+ /**
60
+ * How many outputs the plan job hands its report on in, and the most
61
+ * characters each holds. Forgejo keeps each output's value to 1 MiB. GitHub
62
+ * keeps a job's outputs to 1 MB together, sized as UTF-16. terragucci's
63
+ * `HANDOVER_LIMITS` (handover.ts).
64
+ */
65
+ export declare const HANDOVER_LIMITS: Record<HandoverForge, {
66
+ chunk: number;
67
+ parts: number;
68
+ }>;
69
+ /** The outputs' names on a forge: report_0, report_1, ... */
70
+ export declare function handoverKeys(forge: HandoverForge): string[];
71
+ /** The files the note job reads, which a report too large to hand on whole is cut to. */
72
+ export declare const HANDOVER_NOTE_FILES: readonly ["pr-plan.json", "pr-note.md"];
73
+ /**
74
+ * Packs the report directory (argv 1) into the step's output file (argv 2):
75
+ * every regular file, found without following a link, as gzipped JSON in
76
+ * base64, cut into the forge's chunks. A report too large is cut to
77
+ * {@link HANDOVER_NOTE_FILES}, and to none when even those are too large.
78
+ */
79
+ export declare function packJs(forge: HandoverForge): string;
80
+ /**
81
+ * Writes back, under the directory argv 1 names, the files the plan job
82
+ * handed on: stdin holds its outputs, one JSON string a line. A path is kept
83
+ * only when it is plain segments (no empty one, no `.` or `..`, no backslash
84
+ * or control character), so nothing lands outside the directory.
85
+ */
86
+ export declare const UNPACK_JS: string;
87
+ /**
88
+ * The lines of the plan step, after {@link dropForgeTokens}: when the step
89
+ * ends, however it ends, its report directory (`dir`, from the directory the
90
+ * step starts in) goes into the step's outputs.
91
+ */
92
+ export declare function handoverTrap(dir: string, forge: HandoverForge): string[];
93
+ /** A job's outputs that carry the report its step `step` handed on. */
94
+ export declare function handoverOutputs(step: string, forge: HandoverForge): Record<string, string>;
95
+ /**
96
+ * The script of the step that takes the report job `job` handed on into
97
+ * `dir`. Each output comes in as toJSON's string, one line of a quoted
98
+ * heredoc whose end no JSON string can be, so the change's outputs stay data.
99
+ */
100
+ export declare function takeOverScript(job: string, dir: string, forge: HandoverForge): string;
101
+ /** The variable the checkout script reads the job's token from. */
102
+ export declare const CHECKOUT_TOKEN_ENV = "CHANT_CHECKOUT_TOKEN";
103
+ /**
104
+ * Checks the run's commit out with git, as actions/checkout does without
105
+ * credentials kept: `GITHUB_SHA` under `GITHUB_REF`. With `history`, every
106
+ * branch and tag too, since the plan measures the change with git. The job's
107
+ * token ({@link CHECKOUT_TOKEN_ENV}) reaches the fetches alone, through the
108
+ * environment, never the git config. A workspace already checked out is
109
+ * cleaned first, as actions/checkout does. terragucci's `checkoutScript`
110
+ * (handover.ts). With `ledger`, the `chant/lifecycle` branch is fetched
111
+ * into a local branch too, while the token is there: the plan reads the
112
+ * gate ledger from it, and a later fetch without the token cannot reach a
113
+ * private repository (`requireLifecycleLedger` in ./lifecycle/git.ts reads
114
+ * the local branch when the fetch fails).
115
+ */
116
+ export declare function checkoutScript(history: boolean, ledger?: boolean): string;
117
+ //# sourceMappingURL=ci-handover.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ci-handover.d.ts","sourceRoot":"","sources":["../src/ci-handover.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAE5C;;;;;GAKG;AACH,eAAO,MAAM,gBAAgB,YAC3B,mBAAmB,EACnB,oBAAoB,EACpB,qBAAqB,EACrB,oBAAoB,EACpB,sBAAsB,EACtB,cAAc,EACd,UAAU,EACV,eAAe,EACf,aAAa,EACb,cAAc,EACd,cAAc,EACd,uBAAuB,CACf,CAAC;AAEX,8CAA8C;AAC9C,wBAAgB,kBAAkB,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC,CAI1F;AAED,+EAA+E;AAC/E,wBAAgB,eAAe,CAAC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,GAAG,OAAO,CAEhF;AAED,mEAAmE;AACnE,eAAO,MAAM,qBAAqB,yBAAyB,CAAC;AAE5D;;;;;;;;GAQG;AACH,wBAAgB,eAAe,CAAC,KAAK,GAAE,IAAI,GAAG,MAAa,EAAE,KAAK,GAAE,SAAS,MAAM,EAAO,GAAG,MAAM,CAOlG;AAED,0IAA0I;AAC1I,eAAO,MAAM,wBAAwB,QAAwC,CAAC;AAE9E,4HAA4H;AAC5H,MAAM,MAAM,aAAa,GAAG,OAAO,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC;AAEzD;;;;;GAKG;AACH,eAAO,MAAM,eAAe,EAAE,MAAM,CAAC,aAAa,EAAE;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAGnF,CAAC;AAEF,6DAA6D;AAC7D,wBAAgB,YAAY,CAAC,KAAK,EAAE,aAAa,GAAG,MAAM,EAAE,CAE3D;AAED,yFAAyF;AACzF,eAAO,MAAM,mBAAmB,YAAI,cAAc,EAAE,YAAY,CAAU,CAAC;AAE3E;;;;;GAKG;AACH,wBAAgB,MAAM,CAAC,KAAK,EAAE,aAAa,GAAG,MAAM,CAiBnD;AAED;;;;;GAKG;AACH,eAAO,MAAM,SAAS,QAaZ,CAAC;AAEX;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,aAAa,GAAG,MAAM,EAAE,CAMxE;AAED,uEAAuE;AACvE,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,aAAa,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAE1F;AAED;;;;GAIG;AACH,wBAAgB,cAAc,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,aAAa,GAAG,MAAM,CAOrF;AAED,mEAAmE;AACnE,eAAO,MAAM,kBAAkB,yBAAyB,CAAC;AAEzD;;;;;;;;;;;;GAYG;AACH,wBAAgB,cAAc,CAAC,OAAO,EAAE,OAAO,EAAE,MAAM,UAAQ,GAAG,MAAM,CA8BvE"}
@@ -0,0 +1,217 @@
1
+ /**
2
+ * The forge token handover (#4295): no step that runs a pull request's code
3
+ * holds a forge token.
4
+ *
5
+ * A pull request's plan runs its code: the synth of its components, the
6
+ * plans their capabilities run, its build and lint, and its hooks. A token
7
+ * that can post a note or set a status can also push, so the generated plan
8
+ * job uses the job's token for the checkout's fetches alone and drops it
9
+ * before anything else runs. The ordering is terragucci's `handover.ts`
10
+ * (INTENTIUS/terragucci e6cb431, packages/terragucci/src/handover.ts):
11
+ *
12
+ * 1. The checkout is a script ({@link checkoutScript}), not actions/checkout,
13
+ * whose post step runs at the job's end with the token as its input. The
14
+ * token reaches its fetches through the environment, never the git config.
15
+ * 2. Every later step starts again without the token variables
16
+ * ({@link dropForgeTokens}).
17
+ * 3. The plan step is the job's last. A step after it would start beside
18
+ * whatever the change's code left running, with what it wrote to
19
+ * GITHUB_ENV and GITHUB_PATH. So the plan step hands its report directory
20
+ * on as the job's outputs ({@link handoverTrap}) in place of an artifact
21
+ * upload.
22
+ * 4. A fresh job, `plan-note`, checks out the target branch's commit, takes
23
+ * the report back ({@link takeOverScript}), keeps it as the artifact, and
24
+ * posts the note and the status with the token (`chant components
25
+ * pr-post`, ./pr-post.ts). It reads the report as data.
26
+ *
27
+ * `chant ci run plan` keeps the same rule: no step of the plan job gets a
28
+ * forge token, and the runner posts from the report once the steps settle
29
+ * (../workspace/ci-run.ts).
30
+ */
31
+ /**
32
+ * The variables a runner, a job or a secret may hold a forge token in.
33
+ * A step that runs the change's code starts without them. The list is
34
+ * terragucci's `STEP_TOKEN_VARS` (render.ts:1787) with chant's own names
35
+ * (`FORGE_TOKEN_ENV` in ./pr-forge.ts) and GitLab's added.
36
+ */
37
+ export const FORGE_TOKEN_VARS = [
38
+ "CHANT_FORGE_TOKEN",
39
+ "CHANT_GITHUB_TOKEN",
40
+ "CHANT_FORGEJO_TOKEN",
41
+ "CHANT_GITLAB_TOKEN",
42
+ "CHANT_CHECKOUT_TOKEN",
43
+ "GITHUB_TOKEN",
44
+ "GH_TOKEN",
45
+ "FORGEJO_TOKEN",
46
+ "GITEA_TOKEN",
47
+ "GITLAB_TOKEN",
48
+ "CI_JOB_TOKEN",
49
+ "ACTIONS_RUNTIME_TOKEN",
50
+ ];
51
+ /** `env` without {@link FORGE_TOKEN_VARS}. */
52
+ export function withoutForgeTokens(env) {
53
+ const out = { ...env };
54
+ for (const name of FORGE_TOKEN_VARS)
55
+ delete out[name];
56
+ return out;
57
+ }
58
+ /** Whether `env` holds a forge token under one of {@link FORGE_TOKEN_VARS}. */
59
+ export function holdsForgeToken(env) {
60
+ return FORGE_TOKEN_VARS.some((name) => (env[name] ?? "") !== "");
61
+ }
62
+ /** The marker a step {@link dropForgeTokens} restarted carries. */
63
+ export const NO_FORGE_TOKEN_MARKER = "CHANT_NO_FORGE_TOKEN";
64
+ /**
65
+ * The first line of a step that runs the change's code: restart the step's
66
+ * shell once without the token variables in its process environment, or,
67
+ * when it cannot restart, unset them. Unsetting alone leaves the values in
68
+ * the shell's own initial environment, which a child reads on Linux from
69
+ * `/proc/<pid>/environ`; the restart replaces it. A forge runs a step's
70
+ * script as a file (`sh -e {0}` in a GitHub or Forgejo container job), so
71
+ * `$0` is that file. terragucci's `restartWithout` (render.ts:1826).
72
+ */
73
+ export function dropForgeTokens(shell = "sh", extra = []) {
74
+ const again = shell === "bash" ? 'bash --noprofile --norc -e -o pipefail "$0"' : 'sh -e "$0"';
75
+ const marker = NO_FORGE_TOKEN_MARKER;
76
+ // #4296: a plan job that traded its OIDC token drops the variables a token is requested with too.
77
+ const vars = [...FORGE_TOKEN_VARS, ...extra];
78
+ const can = `[ -z "\${${marker}:-}" ] && [ "\${0#*/}" != "$0" ] && [ -f "$0" ] && ! { [ "\${0##*/}" = sh ] || [ "\${0##*/}" = bash ] || [ "\${0##*/}" = dash ]; } && command -v env >/dev/null 2>&1 && command -v ${shell} >/dev/null 2>&1`;
79
+ return `if ${can}; then exec env ${vars.map((v) => `-u ${v}`).join(" ")} ${marker}=1 ${again}; fi; unset ${vars.join(" ")}`;
80
+ }
81
+ /** The line a GitLab plan job starts its script with: GitLab runs the script in one shell, so the unset holds for every line after it. */
82
+ export const GITLAB_DROP_FORGE_TOKENS = `unset ${FORGE_TOKEN_VARS.join(" ")}`;
83
+ /**
84
+ * How many outputs the plan job hands its report on in, and the most
85
+ * characters each holds. Forgejo keeps each output's value to 1 MiB. GitHub
86
+ * keeps a job's outputs to 1 MB together, sized as UTF-16. terragucci's
87
+ * `HANDOVER_LIMITS` (handover.ts).
88
+ */
89
+ export const HANDOVER_LIMITS = {
90
+ forgejo: { chunk: 1000000, parts: 4 },
91
+ github: { chunk: 450000, parts: 1 },
92
+ };
93
+ /** The outputs' names on a forge: report_0, report_1, ... */
94
+ export function handoverKeys(forge) {
95
+ return Array.from({ length: HANDOVER_LIMITS[forge].parts }, (_, i) => `report_${i}`);
96
+ }
97
+ /** The files the note job reads, which a report too large to hand on whole is cut to. */
98
+ export const HANDOVER_NOTE_FILES = ["pr-plan.json", "pr-note.md"];
99
+ /**
100
+ * Packs the report directory (argv 1) into the step's output file (argv 2):
101
+ * every regular file, found without following a link, as gzipped JSON in
102
+ * base64, cut into the forge's chunks. A report too large is cut to
103
+ * {@link HANDOVER_NOTE_FILES}, and to none when even those are too large.
104
+ */
105
+ export function packJs(forge) {
106
+ const { chunk, parts } = HANDOVER_LIMITS[forge];
107
+ return [
108
+ 'const fs=require("fs"),p=require("path"),z=require("zlib");',
109
+ "const [dir,out]=process.argv.slice(1);",
110
+ `const CHUNK=${chunk},PARTS=${parts},NOTE=${JSON.stringify(HANDOVER_NOTE_FILES)};`,
111
+ "const all={};",
112
+ 'const walk=(d,r)=>{let n=[];try{n=fs.readdirSync(d).sort()}catch{return}for(const x of n){const f=p.join(d,x),k=r?r+"/"+x:x;let s;try{s=fs.lstatSync(f)}catch{continue}if(s.isDirectory())walk(f,k);else if(s.isFile())all[k]=fs.readFileSync(f).toString("base64")}};',
113
+ 'walk(dir,"");',
114
+ 'const pack=(o)=>z.gzipSync(JSON.stringify(o)).toString("base64");',
115
+ "let b=pack(all);",
116
+ "if(b.length>CHUNK*PARTS){b=pack(Object.fromEntries(Object.entries(all).filter(([k])=>NOTE.includes(k))));",
117
+ 'if(b.length>CHUNK*PARTS){b=pack({});console.error("chant: the plan report is too large to hand to the note job, even cut to the report and the note; it hands on none of it")}',
118
+ 'else console.error("chant: the plan report is too large to hand to the note job whole; it hands on pr-plan.json and pr-note.md")}',
119
+ 'let o="";for(let i=0;i*CHUNK<b.length;i++)o+="report_"+i+"="+b.slice(i*CHUNK,(i+1)*CHUNK)+"\\n";',
120
+ "fs.appendFileSync(out,o);",
121
+ ].join("");
122
+ }
123
+ /**
124
+ * Writes back, under the directory argv 1 names, the files the plan job
125
+ * handed on: stdin holds its outputs, one JSON string a line. A path is kept
126
+ * only when it is plain segments (no empty one, no `.` or `..`, no backslash
127
+ * or control character), so nothing lands outside the directory.
128
+ */
129
+ export const UNPACK_JS = [
130
+ 'const fs=require("fs"),p=require("path"),z=require("zlib");',
131
+ "const dir=process.argv[1];",
132
+ 'let b="";',
133
+ 'for(const l of fs.readFileSync(0,"utf8").split("\\n")){if(!l.trim())continue;let s="";try{s=JSON.parse(l)}catch{}if(typeof s==="string")b+=s}',
134
+ 'if(!b){console.log("chant: the plan job handed on no report");process.exit(0)}',
135
+ 'let files;try{files=JSON.parse(z.gunzipSync(Buffer.from(b,"base64"),{maxOutputLength:268435456}).toString("utf8"))}catch(e){console.log("chant: the report the plan job handed on does not read: "+e.message);process.exit(0)}',
136
+ 'if(!files||typeof files!=="object"||Array.isArray(files)){console.log("chant: the report the plan job handed on is not a set of files");process.exit(0)}',
137
+ "let n=0;",
138
+ 'for(const [k,v] of Object.entries(files)){const parts=k.split("/");',
139
+ 'if(typeof v!=="string"||k.length>1024||parts.some((x)=>!x||x==="."||x===".."||/[\\\\\\x00-\\x1f]/.test(x))){console.log("chant: left out "+JSON.stringify(k));continue}',
140
+ 'try{const f=p.join(dir,...parts);fs.mkdirSync(p.dirname(f),{recursive:true});fs.writeFileSync(f,Buffer.from(v,"base64"));n++}catch(e){console.log("chant: could not write "+JSON.stringify(k)+": "+e.message)}}',
141
+ 'console.log("chant: took "+n+" report files from the plan job")',
142
+ ].join("");
143
+ /**
144
+ * The lines of the plan step, after {@link dropForgeTokens}: when the step
145
+ * ends, however it ends, its report directory (`dir`, from the directory the
146
+ * step starts in) goes into the step's outputs.
147
+ */
148
+ export function handoverTrap(dir, forge) {
149
+ return [
150
+ `chant_report="$PWD/${dir}"`,
151
+ `chant_handover() { node -e '${packJs(forge)}' "$chant_report" "\${GITHUB_OUTPUT:-/dev/null}" || echo "chant: the plan report could not be handed to the note job" >&2; }`,
152
+ "trap chant_handover EXIT",
153
+ ];
154
+ }
155
+ /** A job's outputs that carry the report its step `step` handed on. */
156
+ export function handoverOutputs(step, forge) {
157
+ return Object.fromEntries(handoverKeys(forge).map((k) => [k, `\${{ steps.${step}.outputs.${k} }}`]));
158
+ }
159
+ /**
160
+ * The script of the step that takes the report job `job` handed on into
161
+ * `dir`. Each output comes in as toJSON's string, one line of a quoted
162
+ * heredoc whose end no JSON string can be, so the change's outputs stay data.
163
+ */
164
+ export function takeOverScript(job, dir, forge) {
165
+ return [
166
+ "set -u",
167
+ `node -e '${UNPACK_JS}' ${dir} <<'END_OF_REPORT'`,
168
+ ...handoverKeys(forge).map((k) => `\${{ toJSON(needs.${job}.outputs.${k}) }}`),
169
+ "END_OF_REPORT",
170
+ ].join("\n");
171
+ }
172
+ /** The variable the checkout script reads the job's token from. */
173
+ export const CHECKOUT_TOKEN_ENV = "CHANT_CHECKOUT_TOKEN";
174
+ /**
175
+ * Checks the run's commit out with git, as actions/checkout does without
176
+ * credentials kept: `GITHUB_SHA` under `GITHUB_REF`. With `history`, every
177
+ * branch and tag too, since the plan measures the change with git. The job's
178
+ * token ({@link CHECKOUT_TOKEN_ENV}) reaches the fetches alone, through the
179
+ * environment, never the git config. A workspace already checked out is
180
+ * cleaned first, as actions/checkout does. terragucci's `checkoutScript`
181
+ * (handover.ts). With `ledger`, the `chant/lifecycle` branch is fetched
182
+ * into a local branch too, while the token is there: the plan reads the
183
+ * gate ledger from it, and a later fetch without the token cannot reach a
184
+ * private repository (`requireLifecycleLedger` in ./lifecycle/git.ts reads
185
+ * the local branch when the fetch fails).
186
+ */
187
+ export function checkoutScript(history, ledger = false) {
188
+ const fetchLine = history
189
+ ? `git -c protocol.version=2 fetch -q --prune --no-recurse-submodules origin '+refs/heads/*:refs/remotes/origin/*' '+refs/tags/*:refs/tags/*' "+$want:$target"`
190
+ : 'git -c protocol.version=2 fetch -q --no-tags --prune --no-recurse-submodules --depth=1 origin "+$want:$target"';
191
+ return [
192
+ "set -eu",
193
+ 'git config --global --add safe.directory "$PWD" 2>/dev/null || true',
194
+ "[ -d .git ] || git init -q .",
195
+ 'git remote get-url origin >/dev/null 2>&1 || git remote add origin "$GITHUB_SERVER_URL/$GITHUB_REPOSITORY"',
196
+ `if [ -n "\${${CHECKOUT_TOKEN_ENV}:-}" ]; then`,
197
+ ` chant_auth="$(printf 'x-access-token:%s' "$${CHECKOUT_TOKEN_ENV}" | base64 | tr -d '\\n')"`,
198
+ ' export GIT_CONFIG_COUNT=1 GIT_CONFIG_KEY_0=http.extraheader GIT_CONFIG_VALUE_0="AUTHORIZATION: basic $chant_auth"',
199
+ " unset chant_auth",
200
+ "fi",
201
+ 'ref="$GITHUB_REF"',
202
+ 'want="$GITHUB_SHA"',
203
+ 'case "$ref" in',
204
+ ' refs/heads/*) target="refs/remotes/origin/${ref#refs/heads/}" ;;',
205
+ ' refs/tags/*) target="$ref" ;;',
206
+ ' refs/*) target="refs/remotes/${ref#refs/}" ;;',
207
+ ' *) target="refs/remotes/origin/chant-head" ;;',
208
+ "esac",
209
+ "if git rev-parse -q --verify HEAD >/dev/null; then git clean -ffdxq; fi",
210
+ fetchLine,
211
+ ...(ledger ? ["git -c protocol.version=2 fetch -q --no-tags origin '+refs/heads/chant/lifecycle:refs/heads/chant/lifecycle' 2>/dev/null || true"] : []),
212
+ 'case "$ref" in',
213
+ ' refs/heads/*) git checkout -q --force -B "${ref#refs/heads/}" "$target" ;;',
214
+ ' *) git checkout -q --force --detach "$target" ;;',
215
+ "esac",
216
+ ].join("\n");
217
+ }