forgedock 1.52.0 → 1.54.0

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 (233) hide show
  1. package/.claude-plugin/marketplace.json +51 -0
  2. package/README.md +8 -2
  3. package/bin/auth/app-manifest.mjs +89 -0
  4. package/bin/auth/app-manifest.test.mjs +75 -0
  5. package/bin/auth/app-token.mjs +256 -0
  6. package/bin/auth/app-token.test.mjs +232 -0
  7. package/bin/brand.mjs +7 -0
  8. package/bin/config-history.mjs +151 -0
  9. package/bin/config-schema.mjs +115 -0
  10. package/bin/demo.mjs +65 -11
  11. package/bin/engine/outcome-codes.mjs +9 -3
  12. package/bin/engine/telemetry-seam.mjs +11 -3
  13. package/bin/engine/telemetry-seam.test.mjs +17 -0
  14. package/bin/engine-cli.mjs +110 -36
  15. package/bin/experiment.mjs +58 -6
  16. package/bin/first-run.mjs +122 -0
  17. package/bin/forge-utils.mjs +6 -2
  18. package/bin/forgedock.mjs +138 -142
  19. package/bin/help-text.mjs +144 -0
  20. package/bin/hooks/pre-tool-use.mjs +7 -0
  21. package/bin/journey.mjs +82 -83
  22. package/bin/lean/acceptance.mjs +313 -0
  23. package/bin/lean/acceptance.test.mjs +215 -0
  24. package/bin/lean/account-watch.mjs +28 -0
  25. package/bin/lean/account-watch.test.mjs +30 -0
  26. package/bin/lean/activation.mjs +38 -0
  27. package/bin/lean/activation.test.mjs +36 -1
  28. package/bin/lean/basered.mjs +115 -0
  29. package/bin/lean/basered.test.mjs +66 -0
  30. package/bin/lean/batch-health.mjs +114 -7
  31. package/bin/lean/batch-health.test.mjs +87 -1
  32. package/bin/lean/batch.mjs +41 -2
  33. package/bin/lean/batch.test.mjs +115 -11
  34. package/bin/lean/branchlock.mjs +54 -0
  35. package/bin/lean/branchlock.test.mjs +49 -0
  36. package/bin/lean/ceiling.mjs +72 -0
  37. package/bin/lean/ceiling.test.mjs +54 -0
  38. package/bin/lean/checkpoint-reconcile.mjs +43 -0
  39. package/bin/lean/checkpoint-reconcile.test.mjs +95 -0
  40. package/bin/lean/ci-budget.mjs +366 -0
  41. package/bin/lean/ci-budget.test.mjs +167 -0
  42. package/bin/lean/conformance.e2e.test.mjs +8 -1
  43. package/bin/lean/conformance.mjs +269 -38
  44. package/bin/lean/conformance.test.mjs +280 -7
  45. package/bin/lean/context.mjs +35 -6
  46. package/bin/lean/context.test.mjs +27 -1
  47. package/bin/lean/depcache.mjs +25 -1
  48. package/bin/lean/depcache.test.mjs +26 -0
  49. package/bin/lean/e2e.mjs +18 -2
  50. package/bin/lean/e2e.test.mjs +26 -1
  51. package/bin/lean/effects.mjs +47 -14
  52. package/bin/lean/execution.mjs +19 -3
  53. package/bin/lean/execution.test.mjs +63 -0
  54. package/bin/lean/facts.mjs +3 -3
  55. package/bin/lean/facts.test.mjs +5 -0
  56. package/bin/lean/findinglabel.mjs +16 -0
  57. package/bin/lean/findinglabel.test.mjs +39 -0
  58. package/bin/lean/fixround.mjs +14 -0
  59. package/bin/lean/fixround.test.mjs +25 -2
  60. package/bin/lean/gh-assoc.test.mjs +2 -1
  61. package/bin/lean/integration.mjs +100 -9
  62. package/bin/lean/integration.test.mjs +98 -0
  63. package/bin/lean/issuewatch-parity.mjs +64 -0
  64. package/bin/lean/issuewatch-parity.test.mjs +127 -0
  65. package/bin/lean/issuewatch-release.mjs +6 -2
  66. package/bin/lean/issuewatch-release.test.mjs +17 -1
  67. package/bin/lean/issuewatch.mjs +45 -6
  68. package/bin/lean/issuewatch.test.mjs +3 -2
  69. package/bin/lean/lanelock.mjs +14 -0
  70. package/bin/lean/lean.mjs +473 -100
  71. package/bin/lean/lean.test.mjs +686 -33
  72. package/bin/lean/light.mjs +48 -7
  73. package/bin/lean/light.test.mjs +37 -3
  74. package/bin/lean/limits.test.mjs +142 -9
  75. package/bin/lean/mechfix.mjs +124 -0
  76. package/bin/lean/mechfix.test.mjs +75 -0
  77. package/bin/lean/orchestrate-cancel.test.mjs +143 -0
  78. package/bin/lean/orchestrate.mjs +116 -28
  79. package/bin/lean/phaseledger.mjs +47 -5
  80. package/bin/lean/phaseledger.test.mjs +30 -1
  81. package/bin/lean/postdeploy-check.test.mjs +39 -0
  82. package/bin/lean/precheck.mjs +23 -4
  83. package/bin/lean/precheck.test.mjs +14 -1
  84. package/bin/lean/prereq.mjs +28 -0
  85. package/bin/lean/prereq.test.mjs +20 -0
  86. package/bin/lean/probe.mjs +4 -2
  87. package/bin/lean/prodparity.mjs +52 -0
  88. package/bin/lean/prodparity.test.mjs +67 -0
  89. package/bin/lean/prompts.mjs +77 -19
  90. package/bin/lean/quota.mjs +39 -0
  91. package/bin/lean/recovery.test.mjs +11 -1
  92. package/bin/lean/relaxation.mjs +73 -0
  93. package/bin/lean/relaxation.test.mjs +54 -0
  94. package/bin/lean/release-io.test.mjs +42 -0
  95. package/bin/lean/release-postmerge.mjs +125 -0
  96. package/bin/lean/release-preflight.mjs +58 -0
  97. package/bin/lean/release-preflight.test.mjs +114 -0
  98. package/bin/lean/release-rehearsal.mjs +64 -0
  99. package/bin/lean/release-rehearsal.test.mjs +93 -0
  100. package/bin/lean/release-review.e2e.test.mjs +77 -0
  101. package/bin/lean/release-review.mjs +254 -0
  102. package/bin/lean/release-review.test.mjs +307 -0
  103. package/bin/lean/release-snapshot.mjs +188 -0
  104. package/bin/lean/release-snapshot.test.mjs +190 -0
  105. package/bin/lean/release.mjs +296 -0
  106. package/bin/lean/release.test.mjs +387 -0
  107. package/bin/lean/render.mjs +1 -1
  108. package/bin/lean/replan.mjs +184 -0
  109. package/bin/lean/replan.test.mjs +110 -0
  110. package/bin/lean/resume.mjs +9 -7
  111. package/bin/lean/resume.test.mjs +16 -0
  112. package/bin/lean/rotator.mjs +150 -31
  113. package/bin/lean/rotator.test.mjs +105 -2
  114. package/bin/lean/sandbox-remote.mjs +9 -2
  115. package/bin/lean/schedd.mjs +83 -5
  116. package/bin/lean/schedd.test.mjs +108 -7
  117. package/bin/lean/selfreview.mjs +94 -0
  118. package/bin/lean/selfreview.test.mjs +58 -1
  119. package/bin/lean/selftest-gate.mjs +22 -0
  120. package/bin/lean/selftest-gate.test.mjs +18 -1
  121. package/bin/lean/selftest-journey.mjs +234 -0
  122. package/bin/lean/selftest-journey.test.mjs +130 -0
  123. package/bin/lean/selftest-live.mjs +2 -1
  124. package/bin/lean/seqlease.mjs +35 -1
  125. package/bin/lean/seqlease.test.mjs +15 -0
  126. package/bin/lean/seqreserve.e2e.test.mjs +135 -0
  127. package/bin/lean/seqreserve.mjs +73 -0
  128. package/bin/lean/seqreserve.test.mjs +134 -0
  129. package/bin/lean/sequence.mjs +35 -6
  130. package/bin/lean/seqversioned.mjs +147 -0
  131. package/bin/lean/stagestamp.mjs +28 -0
  132. package/bin/lean/stagestamp.test.mjs +32 -0
  133. package/bin/lean/surfaces.mjs +75 -0
  134. package/bin/lean/surfaces.test.mjs +59 -0
  135. package/bin/lean/toolplace.mjs +28 -0
  136. package/bin/lean/toolplace.test.mjs +24 -0
  137. package/bin/lean/toolstats.mjs +3 -1
  138. package/bin/lean/toolstats.test.mjs +11 -0
  139. package/bin/lean/validatebest.mjs +28 -0
  140. package/bin/lean/validatebest.test.mjs +27 -0
  141. package/bin/lean/verifier.mjs +3 -2
  142. package/bin/lean/verify.mjs +2 -1
  143. package/bin/lean/verify.test.mjs +6 -0
  144. package/bin/pool/agent.mjs +27 -4
  145. package/bin/pool/docker.mjs +5 -0
  146. package/bin/pool/router.mjs +74 -28
  147. package/bin/prereqs.mjs +111 -0
  148. package/bin/replay-timelapse.mjs +149 -0
  149. package/bin/review-staging.mjs +123 -16
  150. package/bin/review-staging.test.mjs +18 -0
  151. package/bin/runner.mjs +43 -6
  152. package/bin/splash-install.mjs +157 -0
  153. package/bin/stats/aggregate.mjs +8 -1
  154. package/bin/stats/conformance-outcomes.mjs +289 -0
  155. package/bin/stats/conformance-outcomes.test.mjs +274 -0
  156. package/bin/stats/depth.mjs +275 -0
  157. package/bin/stats/depth.test.mjs +158 -0
  158. package/bin/stats/efficiency.mjs +26 -5
  159. package/bin/stats/efficiency.test.mjs +45 -1
  160. package/bin/stats/experiment.mjs +7 -5
  161. package/bin/stats/first-review.mjs +77 -0
  162. package/bin/stats/first-review.test.mjs +36 -0
  163. package/bin/stats/firstmerge.mjs +40 -0
  164. package/bin/stats/firstmerge.test.mjs +41 -0
  165. package/bin/stats/github-source.mjs +53 -15
  166. package/bin/stats/index.mjs +147 -9
  167. package/bin/stats/local-source.mjs +12 -0
  168. package/bin/stats/origin.mjs +80 -0
  169. package/bin/stats/origin.test.mjs +58 -0
  170. package/bin/stats/query.mjs +69 -0
  171. package/bin/stats/render.mjs +3 -1
  172. package/bin/stats/scoreboard.mjs +137 -0
  173. package/bin/stats/scoreboard.test.mjs +87 -0
  174. package/bin/stats/selffiled.mjs +52 -0
  175. package/bin/stats/selffiled.test.mjs +71 -0
  176. package/bin/stats/stats.test.mjs +80 -0
  177. package/bin/tui.mjs +3 -2
  178. package/commands/bootstrap.md +15 -14
  179. package/commands/deploy-pr.md +18 -3
  180. package/commands/forge-stats.md +1 -1
  181. package/commands/forgedock-ci-setup.md +28 -0
  182. package/commands/forgedock-init.md +4 -5
  183. package/commands/issue.md +2 -0
  184. package/commands/milestone.md +9 -1
  185. package/commands/orchestrate.md +1 -1
  186. package/commands/replay.md +2 -0
  187. package/commands/review-pr-agents/security.md +4 -0
  188. package/commands/review-pr-staging.md +6 -1
  189. package/commands/work-on/build.md +4 -6
  190. package/commands/work-on/close.md +8 -0
  191. package/docs/CONFIG.md +170 -18
  192. package/docs/ISSUE-WATCH.md +25 -0
  193. package/docs/OUTCOME-CODES.md +4 -0
  194. package/docs/REPLAY.md +9 -0
  195. package/docs/SELFTEST.md +11 -0
  196. package/docs/STATS.md +61 -0
  197. package/docs/TELEMETRY.md +1 -1
  198. package/docs/eval/README.md +6 -0
  199. package/docs/eval/adversarial-bench.md +68 -0
  200. package/docs/eval/corpus/adversarial-v1.json +285 -0
  201. package/docs/eval/feature-bench.md +42 -0
  202. package/docs/eval/feature-specs/crud-auth/journeys/notes-crud-isolated.mjs +21 -0
  203. package/docs/eval/feature-specs/crud-auth/journeys/register-login.mjs +12 -0
  204. package/docs/eval/feature-specs/crud-auth/spec.md +15 -0
  205. package/docs/site/start-a-new-project.md +2 -1
  206. package/package.json +5 -2
  207. package/plugins/forgedock-splash/.claude-plugin/plugin.json +10 -0
  208. package/plugins/forgedock-splash/README.md +27 -0
  209. package/plugins/forgedock-splash/hooks/hooks.json +1 -0
  210. package/plugins/forgedock-splash/hooks/register.tsx +74 -0
  211. package/plugins/forgedock-splash/hooks/scene.ts +222 -0
  212. package/plugins/forgedock-splash/types/index.d.ts +10 -0
  213. package/pool/README.md +1 -1
  214. package/pool/image/cs-cred +14 -1
  215. package/scripts/backfill-finding-labels.mjs +50 -0
  216. package/scripts/backfill-finding-labels.test.mjs +36 -0
  217. package/scripts/bench-adversarial.mjs +216 -0
  218. package/scripts/bench-feature.mjs +170 -0
  219. package/scripts/bench-lightlane.mjs +2 -2
  220. package/scripts/bootstrap-create.mjs +2 -2
  221. package/scripts/bootstrap-validate.mjs +35 -2
  222. package/scripts/check-spec-size.sh +4 -3
  223. package/scripts/check-spec-size.test.sh +3 -0
  224. package/scripts/doctor-pipeline-state.sh +21 -6
  225. package/scripts/file-deferred-followups.sh +59 -251
  226. package/scripts/file-deferred-followups.test.sh +85 -206
  227. package/scripts/followup-route.mjs +12 -0
  228. package/scripts/owner-route-parity.test.sh +17 -1
  229. package/scripts/plan-validate-live.mjs +40 -6
  230. package/scripts/review-pr-side-effects.test.sh +40 -0
  231. package/scripts/review-pr-staging-scope.test.sh +1 -1
  232. package/scripts/review-staging-post.sh +23 -0
  233. package/scripts/review-staging-post.test.sh +8 -0
@@ -0,0 +1,188 @@
1
+ // SPDX-FileCopyrightText: Copyright (c) RapierCraft Studios
2
+ // SPDX-License-Identifier: AGPL-3.0-or-later
3
+ /**
4
+ * Snapshot releases of `forgedock release` (#5276): `release.mode: snapshot` cuts `release/<id>` from a staging SHA instead of
5
+ * freezing staging. The release PR head is that branch; blockers are fixed on staging and cherry-picked onto it, and every pick
6
+ * is appended to the `FORGE:RELEASE` record (kept in `state.release` and as a hidden marker in the PR body). `checkChain` is the
7
+ * provenance verifier: head = snapshot + exactly the recorded picks, each one a clean cherry-pick of a staging commit.
8
+ * Picks are built with plumbing (merge-tree + commit-tree), so no working tree is needed. Every effect goes through `git`
9
+ * (`(args, {env, input}) => stdout`, throws on failure) and `io.gh`, so the module is testable with a real temp repo.
10
+ */
11
+ const REMOTE = "origin";
12
+ import { BLOCKER_LABEL } from "./release-rehearsal.mjs";
13
+ export const DEFAULT_PATTERN = "release/{date}-{n}";
14
+ const MARKER = "FORGE:RELEASE";
15
+ const REF_RE = /^[A-Za-z0-9._/-]+$/;
16
+ const json = (s) => JSON.parse(String(s || "null"));
17
+
18
+ /** `<!-- FORGE:RELEASE {...} -->`: the record as a hidden PR-body marker. */
19
+ export const recordMarker = (rec) => `<!-- ${MARKER} ${JSON.stringify(rec)} -->`;
20
+ export function parseRecord(text) {
21
+ const m = /<!-- FORGE:RELEASE (\{.*?\}) -->/s.exec(String(text ?? ""));
22
+ if (!m) return null;
23
+ try { return JSON.parse(m[1]); } catch { return null; }
24
+ }
25
+ /** `body` with its FORGE:RELEASE marker replaced (or appended). */
26
+ export const withRecord = (body, rec) => {
27
+ const text = String(body ?? "").replace(/\n*<!-- FORGE:RELEASE \{.*?\} -->\n*/s, "\n").trimEnd();
28
+ return `${text}\n\n${recordMarker(rec)}\n`;
29
+ };
30
+
31
+ /** Ref prefix of `pattern` (up to the first `{`): every branch under it counts as a release in flight. */
32
+ export const patternPrefix = (pattern = DEFAULT_PATTERN) => {
33
+ const i = pattern.indexOf("{");
34
+ return i < 0 ? pattern : pattern.slice(0, i);
35
+ };
36
+ const dateOf = (now) => new Date(now).toISOString().slice(0, 10).replace(/-/g, "");
37
+ export function branchName(pattern, now, n) {
38
+ const date = dateOf(now);
39
+ const name = pattern.replace("{date}", date).replace("{n}", String(n));
40
+ if (!REF_RE.test(name) || name.includes("..") || name.startsWith("/") || name.endsWith("/")) throw new Error(`release.branch_pattern gives an invalid branch name: ${JSON.stringify(name)}`);
41
+ return name;
42
+ }
43
+
44
+ const isAncestor = (git, a, b) => { try { git(["merge-base", "--is-ancestor", a, b]); return true; } catch { return false; } };
45
+ const fetchRef = (git, ref) => git(["fetch", "--quiet", REMOTE, `+refs/heads/${ref}:refs/remotes/${REMOTE}/${ref}`]);
46
+ const tipOf = (git, ref) => git(["rev-parse", `refs/remotes/${REMOTE}/${ref}`]).trim();
47
+ const committer = (git) => {
48
+ const cfg = (k) => { try { return git(["config", k]).trim(); } catch { return ""; } };
49
+ return { GIT_COMMITTER_NAME: cfg("user.name") || "ForgeDock release", GIT_COMMITTER_EMAIL: cfg("user.email") || "release@forgedock.invalid" };
50
+ };
51
+
52
+ /** The tree of `orig` applied onto `onto` (what `git cherry-pick` would produce); throws on a conflict. */
53
+ function pickTree(git, onto, orig) {
54
+ const parents = git(["rev-list", "--parents", "-n", "1", orig]).trim().split(/\s+/).slice(1);
55
+ if (!parents.length) throw new Error(`${orig} is a root commit`);
56
+ return git(["merge-tree", "--write-tree", `--merge-base=${parents[0]}`, onto, orig]).split("\n")[0].trim();
57
+ }
58
+
59
+ /**
60
+ * Cut `branch` from the staging tip. One release in flight: any branch under the pattern prefix refuses.
61
+ * @returns {{refused: string}|{branch, record}}
62
+ */
63
+ export async function cutSnapshot({ io, git, repo, staging, def, pattern = DEFAULT_PATTERN, id, bundle = [] }) {
64
+ const prefix = patternPrefix(pattern);
65
+ const live = git(["ls-remote", "--heads", REMOTE, `${prefix}*`]).trim().split("\n").filter(Boolean).map((l) => l.split(/\s+/)[1]?.replace("refs/heads/", ""));
66
+ if (live.length) return { refused: `a release branch is already in flight (${live.join(", ")}); one release per repo: finish it with forgedock release --resume <id> or delete the branch` };
67
+ const day = pattern.replace("{date}", dateOf(io.now())).split("{n}")[0];
68
+ const prior = (json(await io.gh(["pr", "list", "--repo", repo, "--base", def, "--state", "all", "--limit", "100", "--json", "headRefName"])) ?? []).filter((p) => String(p.headRefName).startsWith(day)).length;
69
+ const branch = branchName(pattern, io.now(), prior + 1);
70
+ fetchRef(git, staging); fetchRef(git, def);
71
+ const snapshot = tipOf(git, staging);
72
+ git(["push", "--quiet", REMOTE, `${snapshot}:refs/heads/${branch}`]);
73
+ const issues = [...new Set(bundle.flatMap((p) => (p.closingIssuesReferences ?? []).map((i) => i.number)))].sort((a, b) => a - b);
74
+ return { branch, record: { v: 1, id, branch, snapshot, range: { base: tipOf(git, def), head: snapshot }, bundle: bundle.map((p) => p.number), issues, picks: [] } };
75
+ }
76
+
77
+ /** The PR head record into the PR body and the state. */
78
+ export async function writeRecord(ctx) {
79
+ const { io, repo, state } = ctx;
80
+ const { pr } = state.steps["release-pr"].result;
81
+ const body = json(await io.gh(["pr", "view", String(pr), "--repo", repo, "--json", "body"]))?.body ?? "";
82
+ await io.gh(["pr", "edit", String(pr), "--repo", repo, "--body", withRecord(body, state.release)]);
83
+ }
84
+
85
+ /** Merged staging PRs that fix a blocker: labelled `release-blocker`, or closing one of `issues` or an issue labelled so. */
86
+ async function blockerPrs({ io, repo, staging, issues }) {
87
+ const labelled = new Set((json(await io.gh(["issue", "list", "--repo", repo, "--label", BLOCKER_LABEL, "--state", "all", "--limit", "200", "--json", "number"])) ?? []).map((i) => i.number));
88
+ for (const n of issues) labelled.add(n);
89
+ const prs = json(await io.gh(["pr", "list", "--repo", repo, "--base", staging, "--state", "merged", "--limit", "200", "--json", "number,mergeCommit,mergedAt,labels,closingIssuesReferences"])) ?? [];
90
+ return prs
91
+ .filter((p) => p.mergeCommit?.oid && ((p.labels ?? []).some((l) => l.name === BLOCKER_LABEL) || (p.closingIssuesReferences ?? []).some((i) => labelled.has(i.number))))
92
+ .sort((a, b) => String(a.mergedAt).localeCompare(String(b.mergedAt)));
93
+ }
94
+
95
+ /**
96
+ * Cherry-pick the merged blocker fixes (not yet on the branch) onto `release/<id>`, fast-forward the branch and record the picks.
97
+ * The new picks are saved in the state before the push, so a kill in between is repaired by the push of the last pick at the next call.
98
+ * @returns {{picked: number[]}|{err: string}}
99
+ */
100
+ export async function pickFixes(ctx, { issues = [] } = {}) {
101
+ const { io, git, repo, staging, state } = ctx;
102
+ const rec = state.release;
103
+ if (!rec) return { err: "no FORGE:RELEASE record in the release state" };
104
+ const push = (sha) => git(["push", "--quiet", REMOTE, `${sha}:refs/heads/${rec.branch}`]);
105
+ if (rec.picks.length) { try { push(rec.picks.at(-1).sha); } catch { /* already there, or the objects are gone: the checks below decide */ } }
106
+ fetchRef(git, staging);
107
+ const stagingTip = tipOf(git, staging);
108
+ let head = rec.picks.at(-1)?.sha ?? rec.snapshot;
109
+ const picked = [];
110
+ for (const p of await blockerPrs({ io, repo, staging, issues })) {
111
+ const orig = p.mergeCommit.oid;
112
+ if (rec.picks.some((k) => k.orig === orig) || isAncestor(git, orig, head)) continue;
113
+ if (!isAncestor(git, orig, stagingTip)) return { err: `fix #${p.number} (${orig}) is not on ${staging}` };
114
+ let tree;
115
+ try { tree = pickTree(git, head, orig); } catch (e) { return { err: `cherry-pick of #${p.number} (${orig}) onto ${rec.branch} conflicts: resolve it by hand or use release.fix_mode: recut` }; }
116
+ const [an, ae, ad] = git(["log", "-1", "--format=%an%n%ae%n%aI", orig]).trim().split("\n");
117
+ const msg = `${git(["log", "-1", "--format=%B", orig]).trimEnd()}\n\n(cherry picked from commit ${orig})\n`;
118
+ const sha = git(["commit-tree", tree, "-p", head, "-F", "-"], { input: msg, env: { GIT_AUTHOR_NAME: an, GIT_AUTHOR_EMAIL: ae, GIT_AUTHOR_DATE: ad, ...committer(git) } }).trim();
119
+ rec.picks.push({ sha, orig, pr: p.number });
120
+ picked.push(p.number);
121
+ head = sha;
122
+ }
123
+ if (!picked.length) return { picked };
124
+ ctx.save();
125
+ push(head);
126
+ await writeRecord(ctx);
127
+ return { picked };
128
+ }
129
+
130
+ /** `fix_mode: recut`: move the (unmerged) release branch to the current staging tip and start the record's picks over. */
131
+ export async function recut(ctx) {
132
+ const { git, staging, state } = ctx;
133
+ const rec = state.release;
134
+ fetchRef(git, staging);
135
+ const tip = tipOf(git, staging);
136
+ if (tip === (rec.picks.at(-1)?.sha ?? rec.snapshot) && !rec.picks.length) return { recut: false };
137
+ rec.recuts = (rec.recuts ?? 0) + 1;
138
+ rec.snapshot = tip; rec.picks = []; rec.range = { ...rec.range, head: tip };
139
+ ctx.save();
140
+ git(["push", "--quiet", "--force", REMOTE, `${tip}:refs/heads/${rec.branch}`]);
141
+ await writeRecord(ctx);
142
+ return { recut: true, snapshot: tip };
143
+ }
144
+
145
+ /**
146
+ * Provenance check: `sha` = snapshot + the first k recorded picks (all of them with `exact`), linear, each commit the clean
147
+ * cherry-pick of its staging commit (same tree as `git cherry-pick` of it onto the previous commit).
148
+ * @returns {{ok: true, picks: number}|{ok: false, why: string}}
149
+ */
150
+ export function checkChain({ git, record, sha, staging, exact = false }) {
151
+ const no = (why) => ({ ok: false, why });
152
+ try {
153
+ fetchRef(git, staging);
154
+ const stagingTip = tipOf(git, staging);
155
+ if (!record?.snapshot) return no("no FORGE:RELEASE record");
156
+ if (!isAncestor(git, record.snapshot, stagingTip)) return no(`snapshot ${record.snapshot} is not on ${staging}`);
157
+ if (sha !== record.snapshot && !isAncestor(git, record.snapshot, sha)) return no(`${sha} does not descend from the snapshot ${record.snapshot}`);
158
+ const rows = git(["rev-list", "--reverse", "--parents", `${record.snapshot}..${sha}`]).trim().split("\n").filter(Boolean).map((l) => l.split(/\s+/));
159
+ if (rows.length > record.picks.length) return no(`${rows.length} commit(s) on the release branch, ${record.picks.length} recorded cherry-pick(s): ${rows.slice(record.picks.length).map((r) => r[0].slice(0, 12)).join(", ")} not recorded`);
160
+ if (exact && rows.length !== record.picks.length) return no(`head is at ${rows.length} of ${record.picks.length} recorded cherry-picks`);
161
+ let prev = record.snapshot;
162
+ for (const [i, [c, ...parents]] of rows.entries()) {
163
+ const pick = record.picks[i];
164
+ if (c !== pick.sha) return no(`commit ${c.slice(0, 12)} is not the recorded pick ${pick.sha.slice(0, 12)}`);
165
+ if (parents.length !== 1 || parents[0] !== prev) return no(`commit ${c.slice(0, 12)} is not a linear child of ${prev.slice(0, 12)}`);
166
+ if (!isAncestor(git, pick.orig, stagingTip)) return no(`${pick.orig} is not a ${staging} commit`);
167
+ if (!git(["log", "-1", "--format=%B", c]).includes(`(cherry picked from commit ${pick.orig})`)) return no(`commit ${c.slice(0, 12)} does not name ${pick.orig} as its cherry-pick source`);
168
+ if (git(["rev-parse", `${c}^{tree}`]).trim() !== pickTree(git, prev, pick.orig)) return no(`commit ${c.slice(0, 12)} differs from the cherry-pick of ${pick.orig.slice(0, 12)}`);
169
+ prev = c;
170
+ }
171
+ return { ok: true, picks: rows.length };
172
+ } catch (e) { return no(`could not verify: ${String(e.message).split("\n")[0]}`); }
173
+ }
174
+
175
+ /** `forgedock release verify <sha>`: the record from the PR whose head is `sha`, then `checkChain`. */
176
+ export async function verifyRelease({ io, git, repo, staging, sha }) {
177
+ let prs = [];
178
+ try { prs = json(await io.gh(["api", `repos/${repo}/commits/${sha}/pulls`, "--jq", "[.[] | {number, body}]"])) ?? []; } catch { /* unknown commit: no record below */ }
179
+ const found = prs.map((p) => ({ number: p.number, record: parseRecord(p.body) })).find((p) => p.record);
180
+ if (!found) return { ok: false, why: `no pull request with a FORGE:RELEASE record contains ${sha}` };
181
+ try { git(["fetch", "--quiet", REMOTE, sha]); } catch { /* sha may already be local, or not fetchable by id */ }
182
+ return { ...checkChain({ git, record: found.record, sha, staging }), pr: found.number };
183
+ }
184
+
185
+ /** Delete the release branch after post-merge; a missing branch is fine. */
186
+ export function cleanupSnapshot({ git, branch }) {
187
+ try { git(["push", "--quiet", REMOTE, "--delete", branch]); return { deleted: true }; } catch (e) { return { deleted: false, why: String(e.message).split("\n")[0] }; }
188
+ }
@@ -0,0 +1,190 @@
1
+ // SPDX-FileCopyrightText: Copyright (c) RapierCraft Studios
2
+ // SPDX-License-Identifier: AGPL-3.0-or-later
3
+ import { describe, it } from "node:test";
4
+ import assert from "node:assert/strict";
5
+ import { execFileSync } from "node:child_process";
6
+ import { writeFileSync } from "node:fs";
7
+ import { join } from "node:path";
8
+ import { tmpDir } from "../tests/_tmp.mjs";
9
+ import { cutSnapshot, pickFixes, recut, checkChain, verifyRelease, cleanupSnapshot, parseRecord, withRecord, branchName, patternPrefix } from "./release-snapshot.mjs";
10
+
11
+ const ENV = { GIT_AUTHOR_NAME: "t", GIT_AUTHOR_EMAIL: "t@t", GIT_COMMITTER_NAME: "t", GIT_COMMITTER_EMAIL: "t@t", GIT_CONFIG_GLOBAL: "/dev/null", GIT_CONFIG_NOSYSTEM: "1" };
12
+ const mk = (cwd) => (args, { env, input } = {}) => execFileSync("git", args, { cwd, encoding: "utf8", input, env: { ...process.env, ...ENV, ...env }, stdio: ["pipe", "pipe", "pipe"] });
13
+
14
+ /** A bare origin and a clone with main = staging = one commit; `commit` adds a file commit on staging and pushes it. */
15
+ function repo() {
16
+ const root = tmpDir("snap-"), origin = join(root, "origin.git"), work = join(root, "work");
17
+ execFileSync("git", ["init", "--bare", "-b", "main", origin], { env: { ...process.env, ...ENV } });
18
+ execFileSync("git", ["clone", "-q", origin, work], { env: { ...process.env, ...ENV }, stdio: "ignore" });
19
+ const git = mk(work);
20
+ git(["checkout", "-q", "-b", "main"]);
21
+ writeFileSync(join(work, "a.txt"), "a\n"); git(["add", "."]); git(["commit", "-qm", "init"]); git(["push", "-q", "origin", "main"]);
22
+ git(["checkout", "-q", "-b", "staging"]); git(["push", "-q", "origin", "staging"]);
23
+ const commit = (file, text, msg) => { writeFileSync(join(work, file), text); git(["add", "."]); git(["commit", "-qm", msg]); git(["push", "-q", "origin", "staging"]); return git(["rev-parse", "HEAD"]).trim(); };
24
+ return { git, work, origin, commit };
25
+ }
26
+
27
+ /** gh fake: merged staging PRs (`prs`), release-blocker issue numbers, and the body of the release PR. */
28
+ function fakeGh({ prs = [], issues = [], released = [] } = {}) {
29
+ const w = { body: "", edits: 0, prs, issues };
30
+ const gh = async (a) => {
31
+ const j = a.join(" ");
32
+ if (j.startsWith("pr list") && j.includes("--json headRefName")) return JSON.stringify(released.map((headRefName) => ({ headRefName })));
33
+ if (j.startsWith("pr list") && j.includes("closingIssuesReferences")) return JSON.stringify(w.prs);
34
+ if (j.startsWith("issue list")) return JSON.stringify(w.issues.map((number) => ({ number })));
35
+ if (j.startsWith("pr view") && j.includes("--json body")) return JSON.stringify({ body: w.body });
36
+ if (j.startsWith("pr edit")) { w.body = a[a.indexOf("--body") + 1]; w.edits++; return ""; }
37
+ if (a[0] === "api") return JSON.stringify([{ number: 9, body: w.body }]);
38
+ throw new Error(`unexpected gh ${j}`);
39
+ };
40
+ return { gh, w };
41
+ }
42
+
43
+ const setup = async (opts = {}) => {
44
+ const r = repo();
45
+ const { gh, w } = fakeGh(opts);
46
+ const io = { gh, now: () => Date.parse("2026-11-10T12:00:00Z") };
47
+ const ctx = { io, git: r.git, repo: "o/r", staging: "staging", def: "main", state: { id: "r1", steps: { "release-pr": { result: { pr: 9 } } } }, save() {} };
48
+ return { ...r, io, w, ctx };
49
+ };
50
+ const cut = async (s) => {
51
+ const c = await cutSnapshot({ io: s.io, git: s.git, repo: "o/r", staging: "staging", def: "main", id: "r1", bundle: [{ number: 3, closingIssuesReferences: [{ number: 30 }] }] });
52
+ s.ctx.state.release = c.record;
53
+ return c;
54
+ };
55
+
56
+ describe("snapshot cut (#5276)", () => {
57
+ it("branch names come from the pattern; the prefix is what counts as in flight", () => {
58
+ assert.equal(branchName("release/{date}-{n}", Date.parse("2026-11-10T00:00:00Z"), 2), "release/20261110-2");
59
+ assert.equal(patternPrefix("release/{date}-{n}"), "release/");
60
+ assert.throws(() => branchName("bad name/{n}", 0, 1), /invalid branch name/);
61
+ });
62
+
63
+ it("R5: cuts release/<id> at the staging tip and records snapshot, range, bundle and issues; later staging commits do not move it", async () => {
64
+ const s = await setup();
65
+ const tip = s.commit("b.txt", "b\n", "feat: b");
66
+ const c = await cut(s);
67
+ assert.equal(c.branch, "release/20261110-1");
68
+ assert.equal(c.record.snapshot, tip);
69
+ assert.deepEqual([c.record.bundle, c.record.issues, c.record.picks], [[3], [30], []]);
70
+ s.commit("c.txt", "c\n", "feat: c (merged during the release)");
71
+ assert.equal(s.git(["ls-remote", "origin", "refs/heads/release/20261110-1"]).split("\t")[0], tip);
72
+ assert.deepEqual(checkChain({ git: s.git, record: c.record, sha: tip, staging: "staging", exact: true }), { ok: true, picks: 0 });
73
+ });
74
+
75
+ it("R11: a second release branch in flight refuses the cut", async () => {
76
+ const s = await setup();
77
+ await cut(s);
78
+ const again = await cutSnapshot({ io: s.io, git: s.git, repo: "o/r", staging: "staging", def: "main", id: "r2" });
79
+ assert.match(again.refused, /already in flight \(release\/20261110-1\)/);
80
+ });
81
+
82
+ it("numbers the branch after the day's earlier release PRs", async () => {
83
+ const s = await setup({ released: ["release/20261110-1", "release/20261109-1"] });
84
+ assert.equal((await cut(s)).branch, "release/20261110-2");
85
+ });
86
+ });
87
+
88
+ describe("cherry-picks and the verifier (#5276)", () => {
89
+ const blockerWorld = async () => {
90
+ const s = await setup();
91
+ s.commit("b.txt", "b\n", "feat: b");
92
+ const c = await cut(s);
93
+ const fix = s.commit("fix.txt", "fixed\n", "fix: blocker (#31)\n\nbody line");
94
+ s.w.prs = [{ number: 40, mergeCommit: { oid: fix }, mergedAt: "2026-11-10T13:00:00Z", labels: [{ name: "release-blocker" }], closingIssuesReferences: [] }];
95
+ return { s, c, fix };
96
+ };
97
+
98
+ it("R2: a merged release-blocker fix is cherry-picked onto the branch and recorded in FORGE:RELEASE (state and PR body)", async () => {
99
+ const { s, c, fix } = await blockerWorld();
100
+ const out = await pickFixes(s.ctx);
101
+ assert.deepEqual(out, { picked: [40] });
102
+ const [pick] = s.ctx.state.release.picks;
103
+ assert.equal(pick.orig, fix);
104
+ assert.equal(s.git(["ls-remote", "origin", `refs/heads/${c.branch}`]).split("\t")[0], pick.sha);
105
+ assert.match(s.git(["log", "-1", "--format=%B", pick.sha]), new RegExp(`\\(cherry picked from commit ${fix}\\)`));
106
+ assert.equal(s.git(["show", `${pick.sha}:fix.txt`]), "fixed\n");
107
+ assert.deepEqual(parseRecord(s.w.body).picks.map((p) => p.orig), [fix]);
108
+ assert.deepEqual(await pickFixes(s.ctx), { picked: [] }, "idempotent: a fix already on the branch is not picked twice");
109
+ assert.deepEqual(checkChain({ git: s.git, record: s.ctx.state.release, sha: pick.sha, staging: "staging", exact: true }), { ok: true, picks: 1 });
110
+ });
111
+
112
+ it("picks a fix that closes a named issue even without the label", async () => {
113
+ const { s } = await blockerWorld();
114
+ s.w.prs = s.w.prs.map((p) => ({ ...p, labels: [], closingIssuesReferences: [{ number: 31 }] }));
115
+ assert.deepEqual(await pickFixes(s.ctx), { picked: [] });
116
+ assert.deepEqual(await pickFixes(s.ctx, { issues: [31] }), { picked: [40] });
117
+ });
118
+
119
+ it("R3: a commit pushed to the release branch that is not a recorded cherry-pick fails the check", async () => {
120
+ const { s } = await blockerWorld();
121
+ await pickFixes(s.ctx);
122
+ const rec = s.ctx.state.release;
123
+ // a hand-pushed commit on top of the recorded head
124
+ s.git(["checkout", "-q", "-B", "tmp", rec.picks[0].sha]);
125
+ writeFileSync(join(s.work, "evil.txt"), "x\n"); s.git(["add", "."]); s.git(["commit", "-qm", "sneaky"]);
126
+ const evil = s.git(["rev-parse", "HEAD"]).trim();
127
+ assert.match(checkChain({ git: s.git, record: rec, sha: evil, staging: "staging", exact: true }).why, /1 commit\(s\)|2 commit\(s\)/);
128
+ // a commit that names a staging commit as its source but carries other changes
129
+ s.git(["checkout", "-q", "-B", "tmp2", rec.snapshot]);
130
+ writeFileSync(join(s.work, "evil.txt"), "x\n"); s.git(["add", "."]); s.git(["commit", "-qm", `fix\n\n(cherry picked from commit ${rec.picks[0].orig})`]);
131
+ const fake = s.git(["rev-parse", "HEAD"]).trim();
132
+ const forged = { ...rec, picks: [{ ...rec.picks[0], sha: fake }] };
133
+ assert.match(checkChain({ git: s.git, record: forged, sha: fake, staging: "staging", exact: true }).why, /differs from the cherry-pick/);
134
+ // a recorded pick whose source is not a staging commit
135
+ const off = { ...rec, picks: [{ ...rec.picks[0], orig: rec.picks[0].sha }] };
136
+ assert.match(checkChain({ git: s.git, record: off, sha: rec.picks[0].sha, staging: "staging", exact: true }).why, /is not a staging commit/);
137
+ });
138
+
139
+ it("an earlier head is a valid prefix unless the check is exact", async () => {
140
+ const { s } = await blockerWorld();
141
+ await pickFixes(s.ctx);
142
+ const rec = s.ctx.state.release;
143
+ assert.equal(checkChain({ git: s.git, record: rec, sha: rec.snapshot, staging: "staging" }).ok, true);
144
+ assert.match(checkChain({ git: s.git, record: rec, sha: rec.snapshot, staging: "staging", exact: true }).why, /0 of 1/);
145
+ });
146
+
147
+ it("a conflicting pick returns an error and leaves the branch and record alone", async () => {
148
+ const s = await setup();
149
+ await cut(s);
150
+ const fix = s.commit("a.txt", "fixed\n", "fix: touches a");
151
+ s.git(["checkout", "-q", "-B", "side", "main"]); writeFileSync(join(s.work, "a.txt"), "other\n"); s.git(["commit", "-qam", "diverge"]);
152
+ s.ctx.state.release.snapshot = s.git(["rev-parse", "HEAD"]).trim(); // a branch head whose a.txt differs from the fix's parent
153
+ s.w.prs = [{ number: 41, mergeCommit: { oid: fix }, mergedAt: "t", labels: [{ name: "release-blocker" }], closingIssuesReferences: [] }];
154
+ const out = await pickFixes(s.ctx);
155
+ assert.match(out.err, /#41 .* conflicts/);
156
+ assert.deepEqual(s.ctx.state.release.picks, []);
157
+ });
158
+
159
+ it("recut moves the branch to the staging tip and resets the picks", async () => {
160
+ const { s, c } = await blockerWorld();
161
+ await pickFixes(s.ctx);
162
+ const tip = s.commit("d.txt", "d\n", "feat: d");
163
+ assert.deepEqual(await recut(s.ctx), { recut: true, snapshot: tip });
164
+ assert.deepEqual(s.ctx.state.release.picks, []);
165
+ assert.equal(s.git(["ls-remote", "origin", `refs/heads/${c.branch}`]).split("\t")[0], tip);
166
+ assert.equal(parseRecord(s.w.body).snapshot, tip);
167
+ });
168
+
169
+ it("R10: verifyRelease reads the record from the PR that contains the sha, then checks the chain", async () => {
170
+ const { s } = await blockerWorld();
171
+ await pickFixes(s.ctx);
172
+ const rec = s.ctx.state.release, head = rec.picks[0].sha;
173
+ assert.deepEqual(await verifyRelease({ io: s.io, git: s.git, repo: "o/r", staging: "staging", sha: head }), { ok: true, picks: 1, pr: 9 });
174
+ s.w.body = "no record";
175
+ assert.match((await verifyRelease({ io: s.io, git: s.git, repo: "o/r", staging: "staging", sha: head })).why, /no pull request/);
176
+ });
177
+
178
+ it("R9: cleanup deletes the release branch and tolerates a missing one", async () => {
179
+ const { s, c } = await blockerWorld();
180
+ assert.deepEqual(cleanupSnapshot({ git: s.git, branch: c.branch }), { deleted: true });
181
+ assert.equal(s.git(["ls-remote", "origin", `refs/heads/${c.branch}`]).trim(), "");
182
+ assert.equal(cleanupSnapshot({ git: s.git, branch: c.branch }).deleted, false);
183
+ });
184
+
185
+ it("the record marker replaces itself instead of piling up", () => {
186
+ const once = withRecord("body", { v: 1, a: 1 }), twice = withRecord(once, { v: 1, a: 2 });
187
+ assert.equal(twice.match(/FORGE:RELEASE/g).length, 1);
188
+ assert.equal(parseRecord(twice).a, 2);
189
+ });
190
+ });