@intentius/chant 0.124.30 → 0.124.32

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/bin/chant +7 -2
  2. package/dist/cli/commands/check-lexicon-plugin.d.ts +7 -4
  3. package/dist/cli/commands/check-lexicon-plugin.d.ts.map +1 -1
  4. package/dist/cli/commands/check-lexicon-plugin.js +10 -7
  5. package/dist/cli/commands/check-lexicon.d.ts.map +1 -1
  6. package/dist/cli/commands/check-lexicon.js +18 -12
  7. package/dist/cli/commands/import.d.ts +10 -0
  8. package/dist/cli/commands/import.d.ts.map +1 -1
  9. package/dist/cli/commands/import.js +37 -0
  10. package/dist/cli/commands/init-lexicon/templates/project.d.ts.map +1 -1
  11. package/dist/cli/commands/init-lexicon/templates/project.js +4 -3
  12. package/dist/cli/commands/onboard.d.ts.map +1 -1
  13. package/dist/cli/commands/onboard.js +8 -4
  14. package/dist/cli/commands/update.d.ts.map +1 -1
  15. package/dist/cli/commands/update.js +2 -1
  16. package/dist/cli/dependencies.d.ts +18 -0
  17. package/dist/cli/dependencies.d.ts.map +1 -0
  18. package/dist/cli/dependencies.js +120 -0
  19. package/dist/cli/handlers/build.d.ts.map +1 -1
  20. package/dist/cli/handlers/build.js +23 -4
  21. package/dist/cli/handlers/fan-out.d.ts.map +1 -1
  22. package/dist/cli/handlers/fan-out.js +12 -4
  23. package/dist/cli/handlers/graph.d.ts.map +1 -1
  24. package/dist/cli/handlers/graph.js +16 -6
  25. package/dist/cli/handlers/lint.d.ts.map +1 -1
  26. package/dist/cli/handlers/lint.js +6 -0
  27. package/dist/cli/handlers/operator.d.ts +2 -2
  28. package/dist/cli/handlers/operator.js +3 -3
  29. package/dist/cli/handlers/pr.d.ts +14 -0
  30. package/dist/cli/handlers/pr.d.ts.map +1 -1
  31. package/dist/cli/handlers/pr.js +488 -32
  32. package/dist/cli/handlers/promote.d.ts.map +1 -1
  33. package/dist/cli/handlers/promote.js +3 -1
  34. package/dist/cli/handlers/run.d.ts +13 -1
  35. package/dist/cli/handlers/run.d.ts.map +1 -1
  36. package/dist/cli/handlers/run.js +38 -15
  37. package/dist/cli/main.d.ts.map +1 -1
  38. package/dist/cli/main.js +74 -12
  39. package/dist/cli/parse-args.d.ts.map +1 -1
  40. package/dist/cli/parse-args.js +65 -0
  41. package/dist/cli/registry.d.ts +29 -2
  42. package/dist/cli/registry.d.ts.map +1 -1
  43. package/dist/cli/registry.js +6 -2
  44. package/dist/codegen/generate-post-synth-barrel.js +2 -2
  45. package/dist/codegen/lexicon-regen.d.ts +4 -0
  46. package/dist/codegen/lexicon-regen.d.ts.map +1 -1
  47. package/dist/codegen/lexicon-regen.js +1 -1
  48. package/dist/codegen/pinned-upgrade.js +2 -2
  49. package/dist/components/capability.d.ts +44 -3
  50. package/dist/components/capability.d.ts.map +1 -1
  51. package/dist/components/capability.js +21 -0
  52. package/dist/components/changed-files.d.ts +7 -1
  53. package/dist/components/changed-files.d.ts.map +1 -1
  54. package/dist/components/changed-files.js +9 -1
  55. package/dist/components/driver.d.ts +38 -0
  56. package/dist/components/driver.d.ts.map +1 -1
  57. package/dist/components/driver.js +47 -6
  58. package/dist/components/fan-out-record.d.ts +6 -1
  59. package/dist/components/fan-out-record.d.ts.map +1 -1
  60. package/dist/components/fan-out-record.js +24 -0
  61. package/dist/components/fan-out-run.d.ts +20 -0
  62. package/dist/components/fan-out-run.d.ts.map +1 -1
  63. package/dist/components/fan-out-run.js +21 -1
  64. package/dist/components/pr-pipeline.d.ts +65 -2
  65. package/dist/components/pr-pipeline.d.ts.map +1 -1
  66. package/dist/components/pr-pipeline.js +107 -15
  67. package/dist/components/pr-run.d.ts +127 -29
  68. package/dist/components/pr-run.d.ts.map +1 -1
  69. package/dist/components/pr-run.js +290 -70
  70. package/dist/components/wave-plan.d.ts +5 -2
  71. package/dist/components/wave-plan.d.ts.map +1 -1
  72. package/dist/components/wave-plan.js +12 -1
  73. package/dist/discovery/fold-import.d.ts.map +1 -1
  74. package/dist/discovery/fold-import.js +16 -6
  75. package/dist/discovery/sandbox/bundle.d.ts.map +1 -1
  76. package/dist/discovery/sandbox/bundle.js +6 -0
  77. package/dist/discovery/specifier-resolve.d.ts.map +1 -1
  78. package/dist/discovery/specifier-resolve.js +8 -2
  79. package/dist/fold/fold.d.ts.map +1 -1
  80. package/dist/fold/fold.js +68 -1
  81. package/dist/fold/subset.d.ts +37 -0
  82. package/dist/fold/subset.d.ts.map +1 -1
  83. package/dist/fold/subset.js +88 -0
  84. package/dist/lexicon-module.d.ts +10 -0
  85. package/dist/lexicon-module.d.ts.map +1 -1
  86. package/dist/lexicon-module.js +53 -8
  87. package/dist/lexicon-plugin-helpers.d.ts.map +1 -1
  88. package/dist/lexicon-plugin-helpers.js +6 -4
  89. package/dist/lexicon.d.ts +2 -0
  90. package/dist/lexicon.d.ts.map +1 -1
  91. package/dist/lifecycle/affected.d.ts.map +1 -1
  92. package/dist/lifecycle/affected.js +7 -1
  93. package/dist/lifecycle/release-ledger.d.ts +21 -0
  94. package/dist/lifecycle/release-ledger.d.ts.map +1 -1
  95. package/dist/lifecycle/release-ledger.js +24 -0
  96. package/dist/lifecycle/work-dir.d.ts +45 -0
  97. package/dist/lifecycle/work-dir.d.ts.map +1 -0
  98. package/dist/lifecycle/work-dir.js +78 -0
  99. package/dist/lint/discover.d.ts +17 -9
  100. package/dist/lint/discover.d.ts.map +1 -1
  101. package/dist/lint/discover.js +62 -54
  102. package/dist/module-source.d.ts +57 -0
  103. package/dist/module-source.d.ts.map +1 -0
  104. package/dist/module-source.js +104 -0
  105. package/dist/op/activities/factory.js +1 -1
  106. package/dist/op/discover.d.ts.map +1 -1
  107. package/dist/op/discover.js +13 -3
  108. package/dist/op/gate.d.ts.map +1 -1
  109. package/dist/op/gate.js +3 -0
  110. package/dist/op/local-executor.d.ts +12 -0
  111. package/dist/op/local-executor.d.ts.map +1 -1
  112. package/dist/op/local-executor.js +22 -3
  113. package/dist/op/op-waves-run.d.ts.map +1 -1
  114. package/dist/op/op-waves-run.js +20 -5
  115. package/dist/op/operator.d.ts +2 -2
  116. package/dist/op/operator.js +2 -2
  117. package/dist/op/redact.d.ts +34 -0
  118. package/dist/op/redact.d.ts.map +1 -0
  119. package/dist/op/redact.js +94 -0
  120. package/dist/op/step-console.d.ts +2 -0
  121. package/dist/op/step-console.d.ts.map +1 -1
  122. package/dist/op/step-console.js +5 -2
  123. package/dist/op/steward.d.ts +6 -4
  124. package/dist/op/steward.d.ts.map +1 -1
  125. package/dist/op/steward.js +5 -3
  126. package/dist/op/work-lease-run.d.ts +20 -7
  127. package/dist/op/work-lease-run.d.ts.map +1 -1
  128. package/dist/op/work-lease-run.js +56 -12
  129. package/dist/pr-forge.d.ts +27 -0
  130. package/dist/pr-forge.d.ts.map +1 -1
  131. package/dist/pr-forge.js +51 -6
  132. package/dist/pr-loop.d.ts +141 -4
  133. package/dist/pr-loop.d.ts.map +1 -1
  134. package/dist/pr-loop.js +122 -6
  135. package/dist/pr-report-url.d.ts +10 -2
  136. package/dist/pr-report-url.d.ts.map +1 -1
  137. package/dist/pr-report-url.js +52 -6
  138. package/dist/ts-loader.d.ts +38 -4
  139. package/dist/ts-loader.d.ts.map +1 -1
  140. package/dist/ts-loader.js +190 -16
  141. package/dist/wave-runtime.mjs +208 -208
  142. package/dist/workspace/chant-migrations/fountain-retired-kinds.d.ts +34 -0
  143. package/dist/workspace/chant-migrations/fountain-retired-kinds.d.ts.map +1 -0
  144. package/dist/workspace/chant-migrations/fountain-retired-kinds.js +176 -0
  145. package/dist/workspace/chant-migrations.d.ts.map +1 -1
  146. package/dist/workspace/chant-migrations.js +2 -0
  147. package/dist/workspace/checks/boxes.d.ts +6 -6
  148. package/dist/workspace/checks/boxes.js +9 -9
  149. package/dist/workspace/ci-green-forge.d.ts +6 -4
  150. package/dist/workspace/ci-green-forge.d.ts.map +1 -1
  151. package/dist/workspace/ci-green.d.ts +28 -12
  152. package/dist/workspace/ci-green.d.ts.map +1 -1
  153. package/dist/workspace/ci-green.js +49 -21
  154. package/dist/workspace/ci-list.schema.json +12 -6
  155. package/dist/workspace/ci-report-url.d.ts +18 -0
  156. package/dist/workspace/ci-report-url.d.ts.map +1 -0
  157. package/dist/workspace/ci-report-url.js +33 -0
  158. package/dist/workspace/ci-run-index.d.ts +108 -0
  159. package/dist/workspace/ci-run-index.d.ts.map +1 -0
  160. package/dist/workspace/ci-run-index.js +164 -0
  161. package/dist/workspace/ci-run-index.schema.json +52 -0
  162. package/dist/workspace/ci-run.d.ts +159 -9
  163. package/dist/workspace/ci-run.d.ts.map +1 -1
  164. package/dist/workspace/ci-run.js +419 -50
  165. package/dist/workspace/ci-runner-key.d.ts +133 -0
  166. package/dist/workspace/ci-runner-key.d.ts.map +1 -0
  167. package/dist/workspace/ci-runner-key.js +237 -0
  168. package/dist/workspace/ci-secrets.d.ts +65 -0
  169. package/dist/workspace/ci-secrets.d.ts.map +1 -0
  170. package/dist/workspace/ci-secrets.js +211 -0
  171. package/dist/workspace/ci-serve.d.ts +67 -4
  172. package/dist/workspace/ci-serve.d.ts.map +1 -1
  173. package/dist/workspace/ci-serve.js +240 -35
  174. package/dist/workspace/declaration.d.ts +8 -0
  175. package/dist/workspace/declaration.d.ts.map +1 -1
  176. package/dist/workspace/declaration.js +11 -2
  177. package/dist/workspace/declaration.schema.json +7 -2
  178. package/dist/workspace/export.d.ts +1 -1
  179. package/dist/workspace/export.d.ts.map +1 -1
  180. package/dist/workspace/export.js +7 -3
  181. package/dist/workspace/ls.d.ts +6 -1
  182. package/dist/workspace/ls.d.ts.map +1 -1
  183. package/dist/workspace/ls.js +1 -0
  184. package/dist/workspace/ls.schema.json +7 -1
  185. package/dist/workspace/pin-integrity.d.ts.map +1 -1
  186. package/dist/workspace/pin-integrity.js +3 -2
  187. package/dist/workspace/pr-report.schema.json +74 -7
  188. package/dist/workspace/reason-codes.d.ts +7 -3
  189. package/dist/workspace/reason-codes.d.ts.map +1 -1
  190. package/dist/workspace/reason-codes.js +9 -4
  191. package/dist/workspace/run-event.schema.json +3 -1
  192. package/dist/workspace/status.schema.json +3 -1
  193. package/dist/workspace/trust/trust-write.d.ts +109 -0
  194. package/dist/workspace/trust/trust-write.d.ts.map +1 -0
  195. package/dist/workspace/trust/trust-write.js +320 -0
  196. package/dist/workspace/trust-write.schema.json +265 -0
  197. package/dist/workspace/write-scope.d.ts +9 -0
  198. package/dist/workspace/write-scope.d.ts.map +1 -1
  199. package/dist/workspace/write-scope.js +11 -1
  200. package/package.json +1 -1
  201. package/src/__fixtures__/pr-loop/pr-apply-wave-waiting.note.golden.md +48 -0
  202. package/src/cli/commands/__fixtures__/init-lexicon-output/package.json +3 -3
  203. package/src/cli/commands/audit.test.ts +2 -2
  204. package/src/cli/commands/check-lexicon-plugin.test.ts +9 -0
  205. package/src/cli/commands/check-lexicon-plugin.ts +11 -8
  206. package/src/cli/commands/check-lexicon.test.ts +1 -1
  207. package/src/cli/commands/check-lexicon.ts +16 -11
  208. package/src/cli/commands/import.ts +40 -0
  209. package/src/cli/commands/init-lexicon/templates/project.ts +4 -3
  210. package/src/cli/commands/init-lexicon.test.ts +8 -5
  211. package/src/cli/commands/onboard.test.ts +45 -0
  212. package/src/cli/commands/onboard.ts +8 -4
  213. package/src/cli/commands/update.test.ts +6 -0
  214. package/src/cli/commands/update.ts +4 -1
  215. package/src/cli/compiled-cli.e2e.test.ts +76 -11
  216. package/src/cli/dependencies.e2e.test.ts +101 -0
  217. package/src/cli/dependencies.test.ts +93 -0
  218. package/src/cli/dependencies.ts +122 -0
  219. package/src/cli/handlers/build.test.ts +69 -0
  220. package/src/cli/handlers/build.ts +20 -4
  221. package/src/cli/handlers/fan-out.test.ts +73 -0
  222. package/src/cli/handlers/fan-out.ts +11 -4
  223. package/src/cli/handlers/graph.test.ts +76 -0
  224. package/src/cli/handlers/graph.ts +19 -12
  225. package/src/cli/handlers/lint.test.ts +42 -0
  226. package/src/cli/handlers/lint.ts +6 -0
  227. package/src/cli/handlers/operator.ts +3 -3
  228. package/src/cli/handlers/pr-waves.test.ts +449 -0
  229. package/src/cli/handlers/pr.test.ts +134 -0
  230. package/src/cli/handlers/pr.ts +523 -31
  231. package/src/cli/handlers/promote.test.ts +26 -1
  232. package/src/cli/handlers/promote.ts +3 -2
  233. package/src/cli/handlers/run-components-exit.test.ts +99 -0
  234. package/src/cli/handlers/run-signer.test.ts +79 -0
  235. package/src/cli/handlers/run.ts +34 -16
  236. package/src/cli/main.test.ts +19 -0
  237. package/src/cli/main.ts +73 -12
  238. package/src/cli/parse-args.ts +48 -0
  239. package/src/cli/registry.ts +35 -4
  240. package/src/codegen/generate-post-synth-barrel.ts +2 -2
  241. package/src/codegen/lexicon-regen.ts +1 -1
  242. package/src/codegen/pinned-upgrade.ts +2 -2
  243. package/src/codegen/post-synth-barrel-guards.test.ts +6 -11
  244. package/src/components/capability.ts +51 -3
  245. package/src/components/changed-files.test.ts +16 -1
  246. package/src/components/changed-files.ts +9 -1
  247. package/src/components/driver.test.ts +36 -1
  248. package/src/components/driver.ts +76 -4
  249. package/src/components/fan-out-record.ts +29 -1
  250. package/src/components/fan-out-run.test.ts +35 -0
  251. package/src/components/fan-out-run.ts +40 -1
  252. package/src/components/pr-pipeline.test.ts +100 -0
  253. package/src/components/pr-pipeline.ts +132 -17
  254. package/src/components/pr-run.test.ts +323 -18
  255. package/src/components/pr-run.ts +354 -75
  256. package/src/components/wave-plan.ts +16 -4
  257. package/src/discovery/fold-host-method.test.ts +87 -0
  258. package/src/discovery/fold-import.ts +15 -5
  259. package/src/discovery/fold-package-composite.test.ts +27 -0
  260. package/src/discovery/fold-specifiers.test.ts +8 -0
  261. package/src/discovery/sandbox/bundle.ts +6 -0
  262. package/src/discovery/specifier-resolve.ts +8 -2
  263. package/src/fold/fold.test.ts +72 -0
  264. package/src/fold/fold.ts +71 -0
  265. package/src/fold/subset.test.ts +31 -0
  266. package/src/fold/subset.ts +99 -0
  267. package/src/lexicon-dist.e2e.test.ts +254 -0
  268. package/src/lexicon-module.test.ts +47 -0
  269. package/src/lexicon-module.ts +49 -7
  270. package/src/lexicon-plugin-helpers.ts +6 -4
  271. package/src/lexicon.ts +2 -0
  272. package/src/lifecycle/affected.test.ts +7 -0
  273. package/src/lifecycle/affected.ts +6 -1
  274. package/src/lifecycle/release-ledger.ts +34 -0
  275. package/src/lifecycle/work-dir.test.ts +77 -0
  276. package/src/lifecycle/work-dir.ts +82 -0
  277. package/src/lint/discover.test.ts +46 -2
  278. package/src/lint/discover.ts +55 -49
  279. package/src/module-source.test.ts +85 -0
  280. package/src/module-source.ts +112 -0
  281. package/src/op/activities/factory.ts +1 -1
  282. package/src/op/discover-root.test.ts +31 -2
  283. package/src/op/discover.ts +14 -4
  284. package/src/op/gate.test.ts +30 -0
  285. package/src/op/gate.ts +3 -0
  286. package/src/op/local-executor.ts +31 -3
  287. package/src/op/op-waves-run.ts +20 -5
  288. package/src/op/op-waves.test.ts +49 -0
  289. package/src/op/operator.ts +2 -2
  290. package/src/op/redact.ts +93 -0
  291. package/src/op/step-console.ts +7 -2
  292. package/src/op/steward.ts +6 -4
  293. package/src/op/work-lease-run.test.ts +88 -2
  294. package/src/op/work-lease-run.ts +52 -12
  295. package/src/pr-forge-bundle.test.ts +2 -2
  296. package/src/pr-forge.test.ts +71 -0
  297. package/src/pr-forge.ts +70 -6
  298. package/src/pr-loop-bundle.test.ts +2 -1
  299. package/src/pr-loop.test.ts +117 -1
  300. package/src/pr-loop.ts +252 -10
  301. package/src/pr-report-url.ts +58 -6
  302. package/src/terraform/__fixtures__/advise.test.ts +21 -2
  303. package/src/ts-loader.test.ts +89 -1
  304. package/src/ts-loader.ts +180 -13
  305. package/src/workspace/changes.test.ts +1 -1
  306. package/src/workspace/chant-migrations/fountain-retired-kinds.test.ts +33 -0
  307. package/src/workspace/chant-migrations/fountain-retired-kinds.ts +190 -0
  308. package/src/workspace/chant-migrations.e2e.test.ts +5 -0
  309. package/src/workspace/chant-migrations.ts +2 -0
  310. package/src/workspace/checks/boxes.ts +9 -9
  311. package/src/workspace/ci-green-forge.ts +6 -4
  312. package/src/workspace/ci-green-runs.test.ts +53 -11
  313. package/src/workspace/ci-green.test.ts +1 -1
  314. package/src/workspace/ci-green.ts +50 -22
  315. package/src/workspace/ci-list.schema.json +12 -6
  316. package/src/workspace/ci-report-url.test.ts +220 -0
  317. package/src/workspace/ci-report-url.ts +33 -0
  318. package/src/workspace/ci-run-index.schema.json +52 -0
  319. package/src/workspace/ci-run-index.test.ts +392 -0
  320. package/src/workspace/ci-run-index.ts +206 -0
  321. package/src/workspace/ci-run.test.ts +520 -42
  322. package/src/workspace/ci-run.ts +483 -51
  323. package/src/workspace/ci-runner-key.test.ts +204 -0
  324. package/src/workspace/ci-runner-key.ts +275 -0
  325. package/src/workspace/ci-secrets.test.ts +106 -0
  326. package/src/workspace/ci-secrets.ts +213 -0
  327. package/src/workspace/ci-serve.test.ts +282 -39
  328. package/src/workspace/ci-serve.ts +231 -34
  329. package/src/workspace/declaration.schema.json +7 -2
  330. package/src/workspace/declaration.test.ts +23 -0
  331. package/src/workspace/declaration.ts +19 -3
  332. package/src/workspace/export.ts +6 -3
  333. package/src/workspace/ls.schema.json +7 -1
  334. package/src/workspace/ls.ts +7 -2
  335. package/src/workspace/pin-integrity.test.ts +5 -0
  336. package/src/workspace/pin-integrity.ts +3 -2
  337. package/src/workspace/pr-report.schema.json +74 -7
  338. package/src/workspace/reason-codes.test.ts +2 -0
  339. package/src/workspace/reason-codes.ts +9 -4
  340. package/src/workspace/run-event.schema.json +3 -1
  341. package/src/workspace/status.schema.json +3 -1
  342. package/src/workspace/trust/trust-write.test.ts +179 -0
  343. package/src/workspace/trust/trust-write.ts +377 -0
  344. package/src/workspace/trust-write.schema.json +265 -0
  345. package/src/workspace/write-scope.test.ts +29 -1
  346. package/src/workspace/write-scope.ts +12 -1
@@ -0,0 +1,52 @@
1
+ {
2
+ "$schema": "https://json-schema.org/draft/2020-12/schema",
3
+ "$id": "https://intentius.io/chant/schemas/workspace/ci-run-index/v1/ci-run-index.schema.json",
4
+ "title": "chant ci run index",
5
+ "description": "The runs the chant runner has run, kept at <report-url>/runs/index.json (#4098, epic #4101) so a reader outside CI finds them without a forge or a workflow file. `chant ci run` adds an entry to the front after each job settles when --report-url is set, so the list is newest first, and keeps the newest 200 entries unless --run-index-limit says otherwise. `chant ci runs --json` prints the same document. An entry's `key` is the folder the job's report is under, below the report URL. The update is a read, a change and a write of the whole file, through the same s3:// and file:// paths the report upload uses; a file:// write goes through a temporary name and a rename. Two runners updating one index at once can lose an entry. That is out of scope, because `chant ci serve` is one host. Version 1 of the workspace read contract. Readers ignore fields they do not know; a field is only ever added within a version. Written by chant 0.124.31 and newer. Added within contract 1 at chant 0.124.32: an entry's signer (#4156) and exitCode (#4165); an entry from an older chant lacks them.",
6
+ "type": "object",
7
+ "required": ["$schema", "contract", "chant", "updated", "runs"],
8
+ "properties": {
9
+ "$schema": { "const": "https://intentius.io/chant/schemas/workspace/ci-run-index/v1/ci-run-index.schema.json" },
10
+ "contract": { "const": 1 },
11
+ "chant": { "type": "string", "description": "The chant that last wrote the index." },
12
+ "updated": { "type": "string", "description": "When the index was last written." },
13
+ "runs": {
14
+ "type": "array",
15
+ "description": "Newest first, at most the limit the writer was given (200 by default).",
16
+ "items": { "$ref": "#/$defs/run" }
17
+ }
18
+ },
19
+ "$defs": {
20
+ "run": {
21
+ "type": "object",
22
+ "required": ["job", "pr", "commit", "status", "started", "ended", "runId", "key", "runOrigin"],
23
+ "if": { "properties": { "status": { "const": "fail" } } },
24
+ "then": { "required": ["failure"], "properties": { "failure": { "type": "object" } } },
25
+ "properties": {
26
+ "job": { "enum": ["plan", "apply"] },
27
+ "pr": { "type": ["integer", "null"], "minimum": 1, "description": "The pull or merge request number: the one the job's report names, else --pr. null when neither has one." },
28
+ "commit": { "type": "string", "pattern": "^[0-9a-f]{40,64}$", "description": "The full commit the job ran at." },
29
+ "status": { "enum": ["ok", "fail"] },
30
+ "failure": {
31
+ "type": "object",
32
+ "required": ["code"],
33
+ "description": "Present when status is fail. The same code the run record's failure carries: setup-failed, build-failed, a report's refusal or status, or command-failed.",
34
+ "properties": { "code": { "type": "string" } }
35
+ },
36
+ "member": { "type": "string", "pattern": "^[a-z0-9][a-z0-9-]{0,39}$", "description": "The workspace member whose pipeline the job ran for. Absent outside a member." },
37
+ "started": { "type": "string", "description": "ISO-8601 instant the job started." },
38
+ "ended": { "type": "string", "description": "ISO-8601 instant the job settled." },
39
+ "runId": { "type": "string", "description": "The Op run id, as in the run ledger on chant/lifecycle." },
40
+ "key": { "type": "string", "description": "reportKey(pr, commit): <pr>/<commit>, with /<member> after for a member, and unknown for a missing pr. The job's report is <report-url>/<key>/pr-<job>.json." },
41
+ "signer": { "type": ["string", "null"], "description": "Added within contract 1 by #4156, written by chant 0.124.32 and newer. The runner whose key signed the run's record, or null for an unsigned run. Signing is optional, and an unsigned run is listed like any other." },
42
+ "exitCode": { "type": "integer", "minimum": 0, "maximum": 255, "description": "Added within contract 1 by #4165, written by chant 0.124.32 and newer. The code chant ci run exited with for the job: 0 for a passed job, else the command's own code when it is 1 to 255, such as 3 for a plan that moved after review or a wave waiting at its gate, else 1. A reader tells a held run from a failed one by it when the report carries no refusal. An entry from an older chant lacks it." },
43
+ "runOrigin": {
44
+ "type": "object",
45
+ "required": ["forge"],
46
+ "description": "Where the run says it ran. { forge: \"chant\" } for a job the chant runner ran. From chant 0.124.32 a forge job that runs chant ci run names the forge's run (#4156): forge github (GitHub and Forgejo) or gitlab, with repo, url, jobId and attempt when the forge gives them.",
47
+ "properties": { "forge": { "type": "string" }, "repo": { "type": "string" }, "url": { "type": "string" }, "jobId": { "type": "string" }, "attempt": { "type": "integer", "minimum": 1 } }
48
+ }
49
+ }
50
+ }
51
+ }
52
+ }
@@ -0,0 +1,392 @@
1
+ /**
2
+ * #4098: the run index `chant ci run --report-url` keeps at the report URL.
3
+ *
4
+ * The runs are real `ci run` jobs in a temp repository with a stub chant and
5
+ * a file:// report URL in another temp directory. The jobs do not push, so
6
+ * nothing reaches chant/lifecycle or a remote. No test makes a network call.
7
+ */
8
+
9
+ import { chmodSync, existsSync, mkdirSync, readFileSync, readdirSync, writeFileSync } from "node:fs";
10
+ import { execFileSync } from "node:child_process";
11
+ import { join } from "node:path";
12
+ import { pathToFileURL } from "node:url";
13
+ import { afterAll, describe, expect, test } from "vitest";
14
+ import { cleanScratch, contract, declaration, git, repoFixture, scratchDir, validSchema, writeFiles } from "./__fixtures__/contract-repo";
15
+ import { ciJobExitCode, ciRun } from "./ci-run";
16
+ import { readCiSecrets } from "./ci-secrets";
17
+ import {
18
+ addRunEntry,
19
+ CI_RUN_INDEX_KEY,
20
+ CI_RUN_INDEX_LIMIT,
21
+ CI_RUN_INDEX_SCHEMA_ID,
22
+ formatCiRunIndex,
23
+ readCiRunIndex,
24
+ updateCiRunIndex,
25
+ type CiRunIndex,
26
+ type CiRunIndexEntry,
27
+ } from "./ci-run-index";
28
+ import { parseReportUrl, reportGet, reportKey, type ReportTarget } from "../pr-report-url";
29
+ import schema from "./ci-run-index.schema.json";
30
+
31
+ afterAll(cleanScratch);
32
+
33
+ const JOB_IMAGE = "chant-ci-run-index-test:local";
34
+ const HOST = { PATH: process.env.PATH, HOME: process.env.HOME, GITHUB_TOKEN: "t", GITHUB_REPOSITORY: "acme/estate" };
35
+
36
+ /** The repository every test starts from, built once: one member, and two commits. */
37
+ const repoTemplate = repoFixture(() => {
38
+ const root = scratchDir("chant-ci-run-index-template-");
39
+ git(root, "init", "-q");
40
+ git(root, "symbolic-ref", "HEAD", "refs/heads/main");
41
+ git(root, "config", "user.name", "t");
42
+ git(root, "config", "user.email", "t@example.com");
43
+ writeFiles(root, {
44
+ "chant.workspace.json": declaration([{ name: "app", dir: "app", kind: "other", because: "the app" }]),
45
+ ".gitignore": ".chant/pr/\n",
46
+ "app/a.txt": "a\n",
47
+ });
48
+ const commit = (msg: string) => {
49
+ execFileSync("git", ["-c", "commit.gpgsign=false", "add", "-A"], { cwd: root, stdio: "ignore" });
50
+ execFileSync("git", ["-c", "commit.gpgsign=false", "commit", "-q", "-m", msg], { cwd: root, stdio: "ignore" });
51
+ return git(root, "rev-parse", "HEAD");
52
+ };
53
+ commit("init");
54
+ writeFiles(root, { "app/a.txt": "b\n" });
55
+ commit("change");
56
+ return root;
57
+ }, "chant-ci-run-index-");
58
+
59
+ /** A repository with one member, and a stub chant that writes the report FAKE_REPORT gives it and exits $FAKE_EXIT. */
60
+ function workspace(): { app: string; base: string; sha: string; chant: string } {
61
+ const root = repoTemplate();
62
+ const base = git(root, "rev-parse", "HEAD~1");
63
+ const sha = git(root, "rev-parse", "HEAD");
64
+ const file = join(scratchDir("chant-ci-run-index-bin-"), "chant");
65
+ writeFileSync(
66
+ file,
67
+ [
68
+ "#!/bin/sh",
69
+ "mkdir -p .chant/pr",
70
+ 'if [ -n "$FAKE_REPORT" ]; then printf "%s" "$FAKE_REPORT" > ".chant/pr/$2.json"; else echo "{}" > ".chant/pr/$2.json"; fi',
71
+ 'exit "${FAKE_EXIT:-0}"',
72
+ "",
73
+ ].join("\n"),
74
+ );
75
+ chmodSync(file, 0o755);
76
+ return { app: join(root, "app"), base, sha, chant: file };
77
+ }
78
+
79
+ const reportsDir = () => scratchDir("chant-ci-run-index-reports-");
80
+ const urlOf = (dir: string) => pathToFileURL(dir).href;
81
+ const indexOf = (dir: string): CiRunIndex => JSON.parse(readFileSync(join(dir, CI_RUN_INDEX_KEY), "utf-8"));
82
+
83
+ const { expectValid } = contract(schema);
84
+
85
+ async function job(w: ReturnType<typeof workspace>, dir: string, over: Partial<Parameters<typeof ciRun>[0]> & { hostEnv?: Record<string, string | undefined> } = {}) {
86
+ return ciRun({
87
+ image: JOB_IMAGE,
88
+ cwd: w.app,
89
+ job: "plan",
90
+ env: "prod",
91
+ forge: "forgejo",
92
+ base: w.base,
93
+ pr: 7,
94
+ chant: w.chant,
95
+ hostEnv: HOST,
96
+ push: false,
97
+ output: join(scratchDir("chant-ci-run-index-out-"), "pr"),
98
+ loop: { reportUrl: urlOf(dir) },
99
+ ...over,
100
+ });
101
+ }
102
+
103
+ const ENTRY: CiRunIndexEntry = {
104
+ job: "plan",
105
+ pr: 1,
106
+ commit: "a".repeat(40),
107
+ status: "ok",
108
+ started: "2026-10-18T12:00:00.000Z",
109
+ ended: "2026-10-18T12:00:01.000Z",
110
+ runId: "r0",
111
+ key: reportKey(1, "a".repeat(40)),
112
+ runOrigin: { forge: "chant" },
113
+ };
114
+
115
+ describe("the ci-run-index schema (#4098)", () => {
116
+ test("is a valid schema with the contract's id", () => {
117
+ expect(validSchema(schema)).toBe(true);
118
+ expect(schema.$id).toBe(CI_RUN_INDEX_SCHEMA_ID);
119
+ });
120
+
121
+ test("a failed entry without a failure code does not validate", () => {
122
+ const doc = { $schema: CI_RUN_INDEX_SCHEMA_ID, contract: 1, chant: "0.124.30", updated: "2026-10-18T12:00:00.000Z", runs: [{ ...ENTRY, status: "fail" }] };
123
+ expect(contract(schema).validate(doc)).toBe(false);
124
+ expectValid({ ...doc, runs: [{ ...ENTRY, status: "fail", failure: { code: "command-failed" } }] });
125
+ });
126
+
127
+ test("exitCode is an integer from 0 to 255, and an index whose older entries lack it validates (#4165)", () => {
128
+ const doc = { $schema: CI_RUN_INDEX_SCHEMA_ID, contract: 1, chant: "0.124.32", updated: "2026-10-18T12:00:00.000Z" };
129
+ // A newer entry beside two from chant 0.124.31, which wrote neither signer nor exitCode.
130
+ expectValid({ ...doc, runs: [{ ...ENTRY, runId: "r2", signer: null, exitCode: 0 }, { ...ENTRY, runId: "r1" }, { ...ENTRY, status: "fail", failure: { code: "command-failed" } }] });
131
+ for (const exitCode of [-1, 256, 1.5, "3"]) expect(contract(schema).validate({ ...doc, runs: [{ ...ENTRY, exitCode }] })).toBe(false);
132
+ });
133
+ });
134
+
135
+ describe("chant ci run with --report-url keeps the run index (#4098)", () => {
136
+ test("the index is created, grows newest first, carries every field and validates", async () => {
137
+ const w = workspace();
138
+ const dir = reportsDir();
139
+ const plan = await job(w, dir, { runId: "run-plan" });
140
+ expect(plan.status).toBe("ok");
141
+ expect(plan.warnings).toEqual([]);
142
+ const apply = await job(w, dir, { job: "apply", pr: undefined, runId: "run-apply", hostEnv: { ...HOST, FAKE_REPORT: JSON.stringify({ pr: 7, status: "applied" }) }, passEnv: ["FAKE_REPORT"] });
143
+ expect(apply.status).toBe("ok");
144
+
145
+ const index = indexOf(dir);
146
+ expectValid(index);
147
+ expect(index.runs.map((r) => r.runId)).toEqual(["run-apply", "run-plan"]);
148
+ expect(index.runs[1]).toEqual({
149
+ job: "plan",
150
+ pr: 7,
151
+ commit: w.sha,
152
+ status: "ok",
153
+ started: plan.record.started,
154
+ ended: plan.record.ended,
155
+ runId: "run-plan",
156
+ key: `7/${w.sha}/app`,
157
+ member: "app",
158
+ runOrigin: { forge: "chant" },
159
+ // #4156: no key, and the run is listed all the same, with no signer.
160
+ signer: null,
161
+ // #4165: the code ci run exited with.
162
+ exitCode: 0,
163
+ });
164
+ // The apply found its pull request in its own report.
165
+ expect(index.runs[0]).toMatchObject({ job: "apply", pr: 7, key: `7/${w.sha}/app` });
166
+ expect(readdirSync(join(dir, "runs"))).toEqual(["index.json"]);
167
+ expect(formatCiRunIndex(index).split("\n")).toHaveLength(2);
168
+ });
169
+
170
+ test("a job with no pull request is keyed unknown", async () => {
171
+ const w = workspace();
172
+ const dir = reportsDir();
173
+ await job(w, dir, { job: "apply", pr: undefined, runId: "r-apply" });
174
+ const [run] = indexOf(dir).runs;
175
+ expect(run).toMatchObject({ pr: null, key: `unknown/${w.sha}/app` });
176
+ expectValid(indexOf(dir));
177
+ });
178
+
179
+ test("the index keeps the newest entries up to the limit", async () => {
180
+ const w = workspace();
181
+ const dir = reportsDir();
182
+ for (const n of [1, 2, 3]) await job(w, dir, { runId: `run-${n}`, runIndexLimit: 2 });
183
+ expect(indexOf(dir).runs.map((r) => r.runId)).toEqual(["run-3", "run-2"]);
184
+ expect(CI_RUN_INDEX_LIMIT).toBe(200);
185
+ });
186
+
187
+ test("a failed job carries its failure code", async () => {
188
+ const w = workspace();
189
+ const dir = reportsDir();
190
+ const failed = await job(w, dir, { runId: "run-fail", hostEnv: { ...HOST, FAKE_EXIT: "3" }, passEnv: ["FAKE_EXIT"] });
191
+ expect(failed.status).toBe("fail");
192
+ const [run] = indexOf(dir).runs;
193
+ expect(run).toMatchObject({ status: "fail", failure: { code: "command-failed" }, runId: "run-fail", exitCode: 3 });
194
+ expectValid(indexOf(dir));
195
+
196
+ const refused = await job(w, dir, {
197
+ job: "apply",
198
+ runId: "run-refused",
199
+ hostEnv: { ...HOST, FAKE_EXIT: "1", FAKE_REPORT: JSON.stringify({ pr: 7, status: "refused", refusal: "not-approved" }) },
200
+ passEnv: ["FAKE_EXIT", "FAKE_REPORT"],
201
+ });
202
+ expect(refused.status).toBe("fail");
203
+ expect(indexOf(dir).runs[0]).toMatchObject({ failure: { code: "not-approved" }, exitCode: 1 });
204
+ });
205
+
206
+ test("each entry records the code ci run exited with: 0, 1, and 3 for a moved plan or a waiting wave (#4165)", async () => {
207
+ const w = workspace();
208
+ const dir = reportsDir();
209
+ const run = (runId: string, exit: string, report: object) =>
210
+ job(w, dir, { job: "apply", runId, hostEnv: { ...HOST, FAKE_EXIT: exit, FAKE_REPORT: JSON.stringify({ pr: 7, ...report }) }, passEnv: ["FAKE_EXIT", "FAKE_REPORT"] });
211
+ const passed = await run("run-ok", "0", { status: "applied" });
212
+ const failed = await run("run-failed", "1", { status: "failed" });
213
+ const moved = await run("run-moved", "3", { status: "refused", refusal: "plan-changed", message: "the plan changed after review" });
214
+ const waiting = await run("run-waiting", "3", { status: "waiting", waves: [{ wave: 2, status: "waiting" }] });
215
+ expect([passed, failed, moved, waiting].map((r) => ciJobExitCode(r))).toEqual([0, 1, 3, 3]);
216
+ const index = indexOf(dir);
217
+ expectValid(index);
218
+ expect(index.runs.map((r) => [r.runId, r.status, r.exitCode])).toEqual([
219
+ ["run-waiting", "fail", 3],
220
+ ["run-moved", "fail", 3],
221
+ ["run-failed", "fail", 1],
222
+ ["run-ok", "ok", 0],
223
+ ]);
224
+ expect(index.runs[1]!.failure).toEqual({ code: "plan-changed" });
225
+ });
226
+
227
+ test("a fail with no code from its command, or one outside 1 to 255, is 1, and a pass is 0 (#4165)", () => {
228
+ expect(ciJobExitCode({ status: "fail" })).toBe(1);
229
+ expect(ciJobExitCode({ status: "fail", exitCode: 0 })).toBe(1);
230
+ expect(ciJobExitCode({ status: "fail", exitCode: 300 })).toBe(1);
231
+ expect(ciJobExitCode({ status: "ok", exitCode: 3 })).toBe(0);
232
+ });
233
+
234
+ test("a secret the job's report names as its failure never reaches the index (#4096)", async () => {
235
+ const w = workspace();
236
+ const dir = reportsDir();
237
+ const SECRET = "s3cret-pass-1";
238
+ const secretsFile = join(scratchDir("chant-ci-run-index-secrets-"), "secrets.env");
239
+ writeFileSync(secretsFile, `DB_PASSWORD=${SECRET}\n`, { mode: 0o600 });
240
+ const leaky = join(scratchDir("chant-ci-run-index-bin-"), "chant");
241
+ writeFileSync(
242
+ leaky,
243
+ [
244
+ "#!/bin/sh",
245
+ "mkdir -p .chant/pr",
246
+ // The refusal becomes the index's failure code, and the message and output carry the secret too.
247
+ 'printf \'{"stage":"apply","pr":7,"status":"refused","refusal":"denied-%s","message":"failed with %s"}\' "$DB_PASSWORD" "$DB_PASSWORD" > .chant/pr/pr-apply.json',
248
+ 'echo "login refused for $DB_PASSWORD" >&2',
249
+ "exit 3",
250
+ "",
251
+ ].join("\n"),
252
+ );
253
+ chmodSync(leaky, 0o755);
254
+ const r = await job(w, dir, { job: "apply", chant: leaky, runId: "run-leak", secrets: readCiSecrets(secretsFile) });
255
+ expect(r.status).toBe("fail");
256
+ const text = readFileSync(join(dir, CI_RUN_INDEX_KEY), "utf-8");
257
+ expect(text).not.toContain(SECRET);
258
+ expect(indexOf(dir).runs[0]).toMatchObject({ status: "fail", failure: { code: "denied-***" } });
259
+ expectValid(indexOf(dir));
260
+ });
261
+
262
+ test("a corrupt index is replaced with a warning and the job stays ok", async () => {
263
+ const w = workspace();
264
+ const dir = reportsDir();
265
+ mkdirSync(join(dir, "runs"));
266
+ writeFileSync(join(dir, CI_RUN_INDEX_KEY), "{ not json");
267
+ const r = await job(w, dir, { runId: "run-after-corrupt" });
268
+ expect(r.status).toBe("ok");
269
+ expect(r.warnings).toHaveLength(1);
270
+ expect(r.warnings[0]).toMatch(/run index at .*runs\/index\.json is replaced, since it is not JSON/);
271
+ expect(indexOf(dir).runs.map((x) => x.runId)).toEqual(["run-after-corrupt"]);
272
+ expectValid(indexOf(dir));
273
+ });
274
+
275
+ test("an index that cannot be read or written warns and leaves the job ok", async () => {
276
+ const w = workspace();
277
+ const dir = reportsDir();
278
+ // A file where the runs folder goes: the index can be neither read nor written.
279
+ writeFileSync(join(dir, "runs"), "in the way");
280
+ const r = await job(w, dir, { runId: "run-blocked" });
281
+ expect(r.status).toBe("ok");
282
+ expect(r.warnings).toHaveLength(1);
283
+ expect(r.warnings[0]).toMatch(/could not read the run index at .*, so it is not updated/);
284
+ expect(readFileSync(join(dir, "runs"), "utf-8")).toBe("in the way");
285
+ });
286
+
287
+ test("a report URL that does not parse warns, and without --report-url nothing is written", async () => {
288
+ const w = workspace();
289
+ const bad = await job(w, reportsDir(), { loop: { reportUrl: "ftp://example.invalid/x" } });
290
+ expect(bad.status).toBe("ok");
291
+ expect(bad.warnings[0]).toMatch(/the run index is not updated: --report-url ftp/);
292
+ const dir = reportsDir();
293
+ const none = await job(w, dir, { loop: {} });
294
+ expect(none.warnings).toEqual([]);
295
+ expect(existsSync(join(dir, "runs"))).toBe(false);
296
+ });
297
+ });
298
+
299
+ describe("updating the index", () => {
300
+ const target = (dir: string): ReportTarget => parseReportUrl(urlOf(dir)) as ReportTarget;
301
+
302
+ test("an upload error warns and nothing throws", async () => {
303
+ const warnings: string[] = [];
304
+ await updateCiRunIndex({
305
+ target: target(reportsDir()),
306
+ entry: ENTRY,
307
+ warn: (m) => warnings.push(m),
308
+ put: async () => {
309
+ throw new Error("aws s3 cp exited 1: AccessDenied");
310
+ },
311
+ });
312
+ expect(warnings).toEqual([expect.stringMatching(/could not write the run index at .*: aws s3 cp exited 1: AccessDenied/)]);
313
+ });
314
+
315
+ test("a read error other than a missing object leaves the index alone", async () => {
316
+ const warnings: string[] = [];
317
+ let wrote = false;
318
+ await updateCiRunIndex({
319
+ target: target(reportsDir()),
320
+ entry: ENTRY,
321
+ warn: (m) => warnings.push(m),
322
+ get: async () => {
323
+ throw new Error("aws s3 cp exited 1: AccessDenied");
324
+ },
325
+ put: async () => {
326
+ wrote = true;
327
+ },
328
+ });
329
+ expect(wrote).toBe(false);
330
+ expect(warnings[0]).toMatch(/could not read the run index/);
331
+ });
332
+
333
+ test("an index of a newer contract is left as it is", async () => {
334
+ const warnings: string[] = [];
335
+ let wrote = false;
336
+ await updateCiRunIndex({
337
+ target: target(reportsDir()),
338
+ entry: ENTRY,
339
+ warn: (m) => warnings.push(m),
340
+ get: async () => JSON.stringify({ contract: 2, runs: [] }),
341
+ put: async () => {
342
+ wrote = true;
343
+ },
344
+ });
345
+ expect(wrote).toBe(false);
346
+ expect(warnings[0]).toMatch(/is contract 2, which this chant does not write/);
347
+ });
348
+
349
+ test("updates in one process take turns, and a run id is kept once", async () => {
350
+ const dir = reportsDir();
351
+ const warn = (m: string) => {
352
+ throw new Error(m);
353
+ };
354
+ await Promise.all([1, 2, 3, 4].map((n) => updateCiRunIndex({ target: target(dir), entry: { ...ENTRY, runId: `r${n}` }, warn })));
355
+ expect(indexOf(dir).runs.map((r) => r.runId).sort()).toEqual(["r1", "r2", "r3", "r4"]);
356
+ await updateCiRunIndex({ target: target(dir), entry: { ...ENTRY, runId: "r2", status: "fail", failure: { code: "x" } }, warn });
357
+ const runs = indexOf(dir).runs;
358
+ expect(runs).toHaveLength(4);
359
+ expect(runs[0]).toMatchObject({ runId: "r2", status: "fail" });
360
+ expect(addRunEntry([ENTRY], { ...ENTRY, runId: "r9" }, 1).map((r) => r.runId)).toEqual(["r9"]);
361
+ });
362
+
363
+ test("the file write goes through a rename, so no temporary file is left", async () => {
364
+ const dir = reportsDir();
365
+ await updateCiRunIndex({ target: target(dir), entry: ENTRY, warn: () => {} });
366
+ expect(readdirSync(join(dir, "runs"))).toEqual(["index.json"]);
367
+ });
368
+
369
+ test("readCiRunIndex reads what was written, null for none, and says why for a bad one", async () => {
370
+ const dir = reportsDir();
371
+ expect(await readCiRunIndex(target(dir))).toBeNull();
372
+ await updateCiRunIndex({ target: target(dir), entry: ENTRY, warn: () => {} });
373
+ expect((await readCiRunIndex(target(dir)))?.runs).toEqual([ENTRY]);
374
+ writeFileSync(join(dir, CI_RUN_INDEX_KEY), "nope");
375
+ await expect(readCiRunIndex(target(dir))).rejects.toThrow(/is unusable: it is not JSON/);
376
+ });
377
+
378
+ test("an s3 get treats a missing object as none, with a stub in place of the AWS CLI", async () => {
379
+ const s3 = parseReportUrl("s3://bucket/prefix") as ReportTarget;
380
+ const calls: string[][] = [];
381
+ const absent = reportGet(s3, async (args) => {
382
+ calls.push(args);
383
+ throw new Error("aws s3 cp exited 1: An error occurred (404) when calling the HeadObject operation: Key not found");
384
+ });
385
+ expect(await absent(CI_RUN_INDEX_KEY)).toBeNull();
386
+ expect(calls[0]).toEqual(["s3", "cp", "s3://bucket/prefix/runs/index.json", "-", "--only-show-errors"]);
387
+ const denied = reportGet(s3, async () => {
388
+ throw new Error("aws s3 cp exited 1: AccessDenied");
389
+ });
390
+ await expect(denied(CI_RUN_INDEX_KEY)).rejects.toThrow(/AccessDenied/);
391
+ });
392
+ });
@@ -0,0 +1,206 @@
1
+ /**
2
+ * The run index (#4098, epic #4101): where the chant runner says which jobs it
3
+ * ran, at the `--report-url` root, for behold and the demo kiosk to find runs
4
+ * without a forge or a workflow file.
5
+ *
6
+ * ```
7
+ * <url>/runs/index.json
8
+ * ```
9
+ *
10
+ * One document, `ci-run-index/v1` (`ci-run-index.schema.json`). Each settled
11
+ * `chant ci run` job with `--report-url` set adds an entry to its front, so
12
+ * the newest run is first, and the list is cut to the newest `limit` entries,
13
+ * 200 by default. An entry's `key` is the folder the job's report went to
14
+ * (`reportKey`, `<pr>/<commit>`), so a reader goes from the index to
15
+ * `<url>/<key>/pr-<job>.json`.
16
+ *
17
+ * The update reads the index, changes it and writes it back through the same
18
+ * `file://` and `s3://` paths the report upload uses (pr-report-url.ts). A
19
+ * `file://` write goes through a temporary name and a rename. Two runners
20
+ * updating one index at once can lose an entry, and that is out of scope:
21
+ * `chant ci serve` is one host. Within one process the updates for one URL
22
+ * take turns, so `ci serve --max-jobs 2` does not race itself.
23
+ *
24
+ * An index that can't be parsed is replaced with a warning. An index this
25
+ * chant can't read for any other reason, or a newer contract, is left alone
26
+ * with a warning. Nothing here fails a job.
27
+ */
28
+
29
+ import type { RunOrigin } from "../lifecycle/release-ledger";
30
+ import type { CommandContext } from "../cli/registry";
31
+ import { formatError } from "../cli/format";
32
+ import { reportUrlOf } from "./ci-report-url";
33
+ import { parseReportUrl, reportGet, reportPut, type ReportGet, type ReportPut, type ReportTarget } from "../pr-report-url";
34
+ import { readerVersion } from "./declaration";
35
+
36
+ export const CI_RUN_INDEX_SCHEMA_ID = "https://intentius.io/chant/schemas/workspace/ci-run-index/v1/ci-run-index.schema.json";
37
+ export const CI_RUN_INDEX_CONTRACT = 1;
38
+ /** The first chant that writes the index. */
39
+ export const CI_RUN_INDEX_FLOOR = "0.124.31";
40
+ /** Where the index is kept, below the report URL. */
41
+ export const CI_RUN_INDEX_KEY = "runs/index.json";
42
+ /** How many entries the index keeps unless told otherwise. */
43
+ export const CI_RUN_INDEX_LIMIT = 200;
44
+
45
+ /** One job the runner ran. */
46
+ export interface CiRunIndexEntry {
47
+ job: "plan" | "apply";
48
+ /** The pull request, or null when the job found none. */
49
+ pr: number | null;
50
+ commit: string;
51
+ status: "ok" | "fail";
52
+ /** Why a failed job failed: the `failure.code` of its run record. */
53
+ failure?: { code: string };
54
+ member?: string;
55
+ started: string;
56
+ ended: string;
57
+ runId: string;
58
+ /** The folder the job's report is under, below the report URL: `<pr>/<commit>[/<member>]`. */
59
+ key: string;
60
+ runOrigin: RunOrigin;
61
+ /**
62
+ * The runner whose key signed the run's record, or null for an unsigned
63
+ * run (#4156). Written by chant 0.124.32 and newer; an entry from an older
64
+ * chant lacks it.
65
+ */
66
+ signer?: string | null;
67
+ /**
68
+ * The code `chant ci run` exited with for the job: 0, its command's own
69
+ * code such as 3 for a moved plan or a waiting wave, else 1 (#4165).
70
+ * Written by chant 0.124.32 and newer; an entry from an older chant lacks it.
71
+ */
72
+ exitCode?: number;
73
+ }
74
+
75
+ export interface CiRunIndex {
76
+ $schema: typeof CI_RUN_INDEX_SCHEMA_ID;
77
+ contract: typeof CI_RUN_INDEX_CONTRACT;
78
+ chant: string;
79
+ /** When the index was last written. */
80
+ updated: string;
81
+ /** Newest first. */
82
+ runs: CiRunIndexEntry[];
83
+ }
84
+
85
+ const messageOf = (err: unknown): string => (err instanceof Error ? err.message : String(err));
86
+
87
+ /** The index as `text` holds it, or why it can't be used: `corrupt` is replaced, `newer` is left alone. */
88
+ export function parseCiRunIndex(text: string): { index: CiRunIndex } | { corrupt: string } | { newer: number } {
89
+ let doc: unknown;
90
+ try {
91
+ doc = JSON.parse(text);
92
+ } catch (err) {
93
+ return { corrupt: `it is not JSON (${messageOf(err)})` };
94
+ }
95
+ if (doc === null || typeof doc !== "object" || Array.isArray(doc)) return { corrupt: "it is not an object" };
96
+ const d = doc as { contract?: unknown; runs?: unknown };
97
+ if (typeof d.contract === "number" && d.contract > CI_RUN_INDEX_CONTRACT) return { newer: d.contract };
98
+ if (d.contract !== CI_RUN_INDEX_CONTRACT) return { corrupt: "it is not a ci-run-index document" };
99
+ if (!Array.isArray(d.runs)) return { corrupt: "it has no runs list" };
100
+ const runs = d.runs.filter((r): r is CiRunIndexEntry => r !== null && typeof r === "object" && typeof (r as { runId?: unknown }).runId === "string");
101
+ return { index: { ...(doc as CiRunIndex), runs } };
102
+ }
103
+
104
+ /** `entry` at the front of `runs`, replacing an entry with its run id, cut to the newest `limit`. */
105
+ export function addRunEntry(runs: readonly CiRunIndexEntry[], entry: CiRunIndexEntry, limit: number): CiRunIndexEntry[] {
106
+ return [entry, ...runs.filter((r) => r.runId !== entry.runId)].slice(0, limit);
107
+ }
108
+
109
+ /** The turn each index URL is waiting for, so updates in one process run one after another. */
110
+ const turns = new Map<string, Promise<void>>();
111
+
112
+ /**
113
+ * Add `entry` to the index at `target`. Never throws: a problem is a warning
114
+ * and the index stays as it was, or is replaced when it was unreadable JSON.
115
+ */
116
+ export function updateCiRunIndex(input: {
117
+ target: ReportTarget;
118
+ entry: CiRunIndexEntry;
119
+ limit?: number;
120
+ warn: (message: string) => void;
121
+ now?: () => Date;
122
+ put?: ReportPut;
123
+ get?: ReportGet;
124
+ }): Promise<void> {
125
+ const where = `${input.target.url}/${CI_RUN_INDEX_KEY}`;
126
+ const put = input.put ?? reportPut(input.target);
127
+ const get = input.get ?? reportGet(input.target);
128
+ const limit = Math.max(1, Math.floor(input.limit ?? CI_RUN_INDEX_LIMIT));
129
+ const work = async (): Promise<void> => {
130
+ let runs: CiRunIndexEntry[] = [];
131
+ try {
132
+ const text = await get(CI_RUN_INDEX_KEY);
133
+ if (text !== null) {
134
+ const parsed = parseCiRunIndex(text);
135
+ if ("corrupt" in parsed) input.warn(`the run index at ${where} is replaced, since ${parsed.corrupt}`);
136
+ else if ("newer" in parsed) return input.warn(`the run index at ${where} is contract ${parsed.newer}, which this chant does not write; it is left as it is`);
137
+ else runs = parsed.index.runs;
138
+ }
139
+ } catch (err) {
140
+ return input.warn(`could not read the run index at ${where}, so it is not updated: ${messageOf(err)}`);
141
+ }
142
+ const index: CiRunIndex = {
143
+ $schema: CI_RUN_INDEX_SCHEMA_ID,
144
+ contract: CI_RUN_INDEX_CONTRACT,
145
+ chant: readerVersion(),
146
+ updated: (input.now?.() ?? new Date()).toISOString(),
147
+ runs: addRunEntry(runs, input.entry, limit),
148
+ };
149
+ try {
150
+ await put(CI_RUN_INDEX_KEY, JSON.stringify(index, null, 2) + "\n", "application/json");
151
+ } catch (err) {
152
+ input.warn(`could not write the run index at ${where}: ${messageOf(err)}`);
153
+ }
154
+ };
155
+ const previous = turns.get(where) ?? Promise.resolve();
156
+ const next = previous.then(work, work);
157
+ const settled = next.catch(() => undefined);
158
+ turns.set(where, settled);
159
+ void settled.then(() => {
160
+ if (turns.get(where) === settled) turns.delete(where);
161
+ });
162
+ return next.catch((err) => input.warn(`could not update the run index at ${where}: ${messageOf(err)}`));
163
+ }
164
+
165
+ /** The index at `target`, or null when none is there. Throws a line a person can read when it can't be read. */
166
+ export async function readCiRunIndex(target: ReportTarget, get: ReportGet = reportGet(target)): Promise<CiRunIndex | null> {
167
+ const where = `${target.url}/${CI_RUN_INDEX_KEY}`;
168
+ const text = await get(CI_RUN_INDEX_KEY);
169
+ if (text === null) return null;
170
+ const parsed = parseCiRunIndex(text);
171
+ if ("corrupt" in parsed) throw new Error(`the run index at ${where} is unusable: ${parsed.corrupt}`);
172
+ if ("newer" in parsed) throw new Error(`the run index at ${where} is contract ${parsed.newer}; this chant reads contract ${CI_RUN_INDEX_CONTRACT}`);
173
+ return parsed.index;
174
+ }
175
+
176
+ /** The text `chant ci runs` prints: one line per run, newest first. */
177
+ export function formatCiRunIndex(index: CiRunIndex): string {
178
+ if (index.runs.length === 0) return "no runs in the index";
179
+ return index.runs
180
+ .map((r) => {
181
+ const pr = r.pr === null ? "no PR" : `PR ${r.pr}`;
182
+ const why = r.status === "fail" ? ` (${r.failure?.code ?? "failed"})` : "";
183
+ return `${r.ended} ${r.job.padEnd(5)} ${r.status}${why} ${pr} ${r.commit.slice(0, 12)} ${r.runId} ${r.key}${r.signer ? ` signed by ${r.signer}` : ""}`;
184
+ })
185
+ .join("\n");
186
+ }
187
+
188
+ const RUNS_USAGE = "chant ci runs [--report-url <s3://<bucket>/<prefix> | file://<directory>>] [--json]";
189
+
190
+ /** `chant ci runs` (#4098): list the index at the report URL. */
191
+ export async function runCiRuns(ctx: CommandContext): Promise<number> {
192
+ try {
193
+ // #4160: without --report-url, the declaration's ci.reportUrl.
194
+ const raw = await reportUrlOf(ctx.args.reportUrl, process.cwd());
195
+ if (!raw) throw new Error("--report-url names where the index is kept, unless the workspace declares ci.reportUrl");
196
+ const target = parseReportUrl(raw);
197
+ if ("error" in target) throw new Error(target.error);
198
+ const index = await readCiRunIndex(target);
199
+ if (index === null) throw new Error(`there is no run index at ${target.url}/${CI_RUN_INDEX_KEY}`);
200
+ console.log(ctx.args.json ? JSON.stringify(index, null, 2) : formatCiRunIndex(index));
201
+ return 0;
202
+ } catch (err) {
203
+ console.error(formatError({ message: `chant ci runs: ${messageOf(err)}`, hint: RUNS_USAGE }));
204
+ return 1;
205
+ }
206
+ }