@intentius/chant 0.124.78 → 0.124.80

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 (184) hide show
  1. package/dist/action-pins.d.ts.map +1 -1
  2. package/dist/action-pins.js +1 -2
  3. package/dist/change-set.d.ts +10 -1
  4. package/dist/change-set.d.ts.map +1 -1
  5. package/dist/change-set.js +4 -1
  6. package/dist/ci-agent.d.ts +16 -2
  7. package/dist/ci-agent.d.ts.map +1 -1
  8. package/dist/ci-agent.js +6 -1
  9. package/dist/ci-commit.d.ts.map +1 -1
  10. package/dist/ci-commit.js +2 -3
  11. package/dist/ci-note-commands.d.ts +199 -0
  12. package/dist/ci-note-commands.d.ts.map +1 -0
  13. package/dist/ci-note-commands.js +372 -0
  14. package/dist/ci-product.d.ts +6 -2
  15. package/dist/ci-product.d.ts.map +1 -1
  16. package/dist/ci-product.js +5 -1
  17. package/dist/cli/handlers/build.d.ts.map +1 -1
  18. package/dist/cli/handlers/build.js +7 -1
  19. package/dist/cli/handlers/pr-notes.d.ts +13 -0
  20. package/dist/cli/handlers/pr-notes.d.ts.map +1 -0
  21. package/dist/cli/handlers/pr-notes.js +73 -0
  22. package/dist/cli/handlers/release.d.ts +8 -0
  23. package/dist/cli/handlers/release.d.ts.map +1 -1
  24. package/dist/cli/handlers/release.js +64 -2
  25. package/dist/cli/handlers/run-wave.js +1 -0
  26. package/dist/cli/main.d.ts.map +1 -1
  27. package/dist/cli/main.js +17 -1
  28. package/dist/cli/mcp/product-tools.d.ts +6 -0
  29. package/dist/cli/mcp/product-tools.d.ts.map +1 -1
  30. package/dist/cli/mcp/product-tools.js +6 -0
  31. package/dist/cli/mcp/read-only.d.ts +12 -0
  32. package/dist/cli/mcp/read-only.d.ts.map +1 -1
  33. package/dist/cli/mcp/read-only.js +16 -0
  34. package/dist/cli/mcp/server.d.ts +11 -0
  35. package/dist/cli/mcp/server.d.ts.map +1 -1
  36. package/dist/cli/mcp/server.js +26 -5
  37. package/dist/cli/mcp/types.d.ts +5 -1
  38. package/dist/cli/mcp/types.d.ts.map +1 -1
  39. package/dist/cli/parse-args.d.ts.map +1 -1
  40. package/dist/cli/parse-args.js +10 -0
  41. package/dist/cli/registry.d.ts +4 -0
  42. package/dist/cli/registry.d.ts.map +1 -1
  43. package/dist/components/capability.d.ts +53 -0
  44. package/dist/components/capability.d.ts.map +1 -1
  45. package/dist/components/group-digest.d.ts +17 -0
  46. package/dist/components/group-digest.d.ts.map +1 -0
  47. package/dist/components/group-digest.js +18 -0
  48. package/dist/components/groups.d.ts +2 -11
  49. package/dist/components/groups.d.ts.map +1 -1
  50. package/dist/components/groups.js +2 -12
  51. package/dist/components/index.d.ts +1 -1
  52. package/dist/components/index.d.ts.map +1 -1
  53. package/dist/components/pr-pipeline.d.ts +22 -0
  54. package/dist/components/pr-pipeline.d.ts.map +1 -1
  55. package/dist/components/pr-pipeline.js +26 -0
  56. package/dist/components/product-pipeline.d.ts +12 -0
  57. package/dist/components/product-pipeline.d.ts.map +1 -1
  58. package/dist/components/product-pipeline.js +5 -1
  59. package/dist/config.d.ts +14 -0
  60. package/dist/config.d.ts.map +1 -1
  61. package/dist/config.js +5 -0
  62. package/dist/lexicon.d.ts.map +1 -1
  63. package/dist/lexicon.js +1 -2
  64. package/dist/lifecycle/release-cut.d.ts +7 -1
  65. package/dist/lifecycle/release-cut.d.ts.map +1 -1
  66. package/dist/lifecycle/release-cut.js +1 -0
  67. package/dist/lifecycle/release-images.d.ts +12 -3
  68. package/dist/lifecycle/release-images.d.ts.map +1 -1
  69. package/dist/lifecycle/release-images.js +6 -7
  70. package/dist/lifecycle/release-publish.d.ts +8 -1
  71. package/dist/lifecycle/release-publish.d.ts.map +1 -1
  72. package/dist/lifecycle/release-publish.js +30 -3
  73. package/dist/lifecycle/release-version.d.ts +86 -0
  74. package/dist/lifecycle/release-version.d.ts.map +1 -0
  75. package/dist/lifecycle/release-version.js +208 -0
  76. package/dist/mcp/types.d.ts +26 -0
  77. package/dist/mcp/types.d.ts.map +1 -1
  78. package/dist/notify-product.d.ts.map +1 -1
  79. package/dist/notify-product.js +1 -2
  80. package/dist/op/activities/review-product.d.ts.map +1 -1
  81. package/dist/op/activities/review-product.js +1 -2
  82. package/dist/op/gate.d.ts +17 -0
  83. package/dist/op/gate.d.ts.map +1 -1
  84. package/dist/op/gate.js +36 -0
  85. package/dist/op/index.d.ts +8 -6
  86. package/dist/op/index.d.ts.map +1 -1
  87. package/dist/op/index.js +5 -4
  88. package/dist/op/op-waves-after-plan.d.ts +109 -1
  89. package/dist/op/op-waves-after-plan.d.ts.map +1 -1
  90. package/dist/op/op-waves-after-plan.js +180 -3
  91. package/dist/op/op-waves-capability-spec.js +1 -2
  92. package/dist/op/op-waves-capability.d.ts +93 -3
  93. package/dist/op/op-waves-capability.d.ts.map +1 -1
  94. package/dist/op/op-waves-capability.js +218 -10
  95. package/dist/op/op-waves-run-hooks.d.ts +104 -0
  96. package/dist/op/op-waves-run-hooks.d.ts.map +1 -0
  97. package/dist/op/op-waves-run-hooks.js +145 -0
  98. package/dist/op/op-waves-run.d.ts +21 -0
  99. package/dist/op/op-waves-run.d.ts.map +1 -1
  100. package/dist/op/op-waves-run.js +307 -21
  101. package/dist/op/op-waves.d.ts +11 -1
  102. package/dist/op/op-waves.d.ts.map +1 -1
  103. package/dist/op/op-waves.js +7 -1
  104. package/dist/op/runtime.d.ts +2 -0
  105. package/dist/op/runtime.d.ts.map +1 -1
  106. package/dist/pr-forge-reads.d.ts +151 -1
  107. package/dist/pr-forge-reads.d.ts.map +1 -1
  108. package/dist/pr-forge-reads.js +265 -1
  109. package/dist/pr-plan-note.d.ts.map +1 -1
  110. package/dist/pr-plan-note.js +1 -2
  111. package/dist/release-floors.d.ts +19 -1
  112. package/dist/release-floors.d.ts.map +1 -1
  113. package/dist/release-floors.js +23 -5
  114. package/dist/wave-runtime.mjs +312 -312
  115. package/dist/workspace/change-set.schema.json +7 -0
  116. package/dist/workspace/release-workflow.d.ts +7 -0
  117. package/dist/workspace/release-workflow.d.ts.map +1 -1
  118. package/dist/workspace/release-workflow.js +11 -1
  119. package/package.json +1 -1
  120. package/src/action-pins.test.ts +1 -2
  121. package/src/action-pins.ts +1 -3
  122. package/src/change-set.test.ts +20 -0
  123. package/src/change-set.ts +11 -1
  124. package/src/ci-agent.test.ts +9 -0
  125. package/src/ci-agent.ts +15 -2
  126. package/src/ci-commit.test.ts +2 -3
  127. package/src/ci-commit.ts +2 -3
  128. package/src/ci-note-commands.test.ts +377 -0
  129. package/src/ci-note-commands.ts +479 -0
  130. package/src/ci-product.ts +6 -2
  131. package/src/cli/handlers/build.ts +7 -1
  132. package/src/cli/handlers/pr-notes.ts +70 -0
  133. package/src/cli/handlers/release.ts +55 -2
  134. package/src/cli/handlers/run-wave.ts +1 -0
  135. package/src/cli/main.ts +17 -1
  136. package/src/cli/mcp/product-tools.test.ts +49 -0
  137. package/src/cli/mcp/product-tools.ts +7 -0
  138. package/src/cli/mcp/read-only.ts +19 -0
  139. package/src/cli/mcp/server.test.ts +30 -0
  140. package/src/cli/mcp/server.ts +28 -5
  141. package/src/cli/mcp/types.ts +6 -1
  142. package/src/cli/parse-args.ts +8 -0
  143. package/src/cli/registry.ts +4 -0
  144. package/src/components/capability.ts +52 -0
  145. package/src/components/group-digest.ts +20 -0
  146. package/src/components/groups.ts +3 -13
  147. package/src/components/index.ts +2 -0
  148. package/src/components/pr-pipeline.ts +35 -0
  149. package/src/components/product-pipeline.test.ts +14 -0
  150. package/src/components/product-pipeline.ts +13 -1
  151. package/src/config.ts +15 -0
  152. package/src/lexicon.ts +1 -2
  153. package/src/lifecycle/release-cut.ts +11 -0
  154. package/src/lifecycle/release-images-dockerfile.test.ts +10 -2
  155. package/src/lifecycle/release-images.ts +12 -8
  156. package/src/lifecycle/release-publish.ts +34 -3
  157. package/src/lifecycle/release-version.test.ts +155 -0
  158. package/src/lifecycle/release-version.ts +226 -0
  159. package/src/mcp/types.ts +27 -0
  160. package/src/notify-message-words.test.ts +1 -2
  161. package/src/notify-product.ts +1 -2
  162. package/src/op/activities/review-product.ts +1 -2
  163. package/src/op/gate.ts +46 -0
  164. package/src/op/index.ts +10 -4
  165. package/src/op/op-waves-after-plan-wave.test.ts +225 -0
  166. package/src/op/op-waves-after-plan.ts +228 -5
  167. package/src/op/op-waves-capability-spec.ts +1 -1
  168. package/src/op/op-waves-capability.test.ts +9 -1
  169. package/src/op/op-waves-capability.ts +246 -13
  170. package/src/op/op-waves-gate-port.test.ts +232 -0
  171. package/src/op/op-waves-run-hooks.test.ts +183 -0
  172. package/src/op/op-waves-run-hooks.ts +194 -0
  173. package/src/op/op-waves-run.ts +313 -25
  174. package/src/op/op-waves-step-wave.test.ts +276 -0
  175. package/src/op/op-waves.ts +15 -2
  176. package/src/op/runtime.ts +2 -0
  177. package/src/pr-forge-reads-compare.test.ts +145 -0
  178. package/src/pr-forge-reads.test.ts +203 -0
  179. package/src/pr-forge-reads.ts +398 -1
  180. package/src/pr-plan-note.ts +1 -2
  181. package/src/release-floors.ts +32 -5
  182. package/src/workspace/change-set.schema.json +7 -0
  183. package/src/workspace/release-workflow.test.ts +18 -0
  184. package/src/workspace/release-workflow.ts +21 -1
@@ -92,7 +92,8 @@ import { appendRunRecord, DEFAULT_RUN_ENV } from "../lifecycle/run-ledger";
92
92
  import { LifecyclePushLostError, pushLifecycleRebasing } from "../lifecycle/git";
93
93
  import { resolveCiJob } from "../lifecycle/release-ledger";
94
94
  import type { OpRunPolicyRow, OpRunRecordInput, OpRunStepRecord, OpRunWave, OpRunWavePlan, OpRunWaveTarget } from "./runtime";
95
- import { foldPolicyRows, opWaveAfterPlanProblems, runOpWaveAfterPlanHooks, type OpWaveAfterPlanHook, type OpWaveAfterPlanRun } from "./op-waves-after-plan";
95
+ import { opWaveRunHookList, opWaveRunHooksProblems, runOpWaveAfterApply, runOpWaveBeforeApply, runOpWaveBeforePlan, type OpWaveRunHooks } from "./op-waves-run-hooks";
96
+ import { foldPolicyRows, opWaveAfterPlanProblems, runOpWaveAfterPlanHooks, type OpWaveAfterPlanHook, type OpWaveAfterPlanRun, type OpWaveAfterPlanWaveOutcome, type OpWaveAnswerApproval } from "./op-waves-after-plan";
96
97
  import { activeOtlp, RunEventTrace, type CapabilityTraceFacts, type OtlpExporter } from "../otlp";
97
98
  import { joinRunEventSinks, resolveRunEventRun, type RunEventSink } from "../run-event-stream";
98
99
  import { OpWaveRunEvents } from "./op-waves-events";
@@ -117,11 +118,16 @@ import {
117
118
  opWaveStepChangeSet,
118
119
  planJsonOf,
119
120
  planOpWaveStep,
121
+ planOpWaveStepGroup,
122
+ discoverOpWaveStepRuns,
123
+ applyOpWaveStepGroup,
124
+ OP_WAVE_TOGETHER_MEMBER,
120
125
  type OpWaveHolds,
126
+ type OpWaveStepApply,
121
127
  type OpWaveStepPlan,
122
128
  } from "./op-waves-capability";
123
129
  import {
124
- changeInvolvement, quorumProblems, quorumRuleLine, quorumRulesFor, quorumWaiting, recordedReviewApprovals, ruleNeedsProof, samePrincipal, trustQuorumJudge,
130
+ changeInvolvement, quorumProblems, quorumRuleLine, quorumRulesFor, quorumWaiting, recordedReviewApprovals, recordedReviewOf, ruleNeedsProof, samePrincipal, trustQuorumJudge,
125
131
  type GateQuorumJudge, type GateQuorumRule, type GateQuorumRuleProgress, type QuorumJudgement,
126
132
  } from "./gate-quorum";
127
133
  import {
@@ -279,6 +285,8 @@ export interface OpWaveMember extends WaveMember {
279
285
  override?: { by: string; at: string; digest: string; reason: string };
280
286
  /** The product's policy holds the plan for a person (#4294). */
281
287
  held?: { rules: string[]; reason?: string };
288
+ /** Who may override this run's denial (#4842), from an after-plan hook, in place of the wave's overriders. */
289
+ overriders?: string[];
282
290
  }
283
291
 
284
292
  /** Where a wave's policy came from. */
@@ -394,6 +402,12 @@ export interface OpWaveDecision {
394
402
  overridesSpentAt?: string;
395
403
  /** Digest members that are no run, from the plans file (#3821), such as the wave's cost. */
396
404
  extras?: WaveMember[];
405
+ /**
406
+ * After an approval of other plans (#4842): the after-plan hook's report of
407
+ * the plans now and the one stored for the plans approved, written for the
408
+ * job that explains the refusal. Paths relative to the job's directory.
409
+ */
410
+ reports?: { current: string; approved?: string };
397
411
  /** On a discovering wave (#3821): how many waves the plan command found. */
398
412
  discovered?: { waves?: number };
399
413
  /**
@@ -702,10 +716,11 @@ function quorumAtBase(spec: OpWavesSpec, where: string): Pick<OpWaveBaseRules, "
702
716
  if (bad.length > 0) return { quorumRefused: `the approval rules in ${where} cannot be read (${bad.join("; ")})` };
703
717
  extra.involved = structuredClone(a.involved as OpWavesInvolved);
704
718
  }
705
- if (raw === undefined) return {};
719
+ // terragucci issue 1303: the review ledger and the involved count without rules too.
720
+ if (raw === undefined) return extra;
706
721
  const problems = Array.isArray(raw) ? quorumProblems(raw, "approvals.quorum", { notAuthor: true }) : ["`approvals.quorum` must be a list of rules"];
707
722
  if (problems.length > 0) return { quorumRefused: `the approval rules in ${where} cannot be read (${problems.join("; ")})` };
708
- return (raw as GateQuorumRule[]).length > 0 ? { quorum: raw as GateQuorumRule[], ...extra } : {};
723
+ return (raw as GateQuorumRule[]).length > 0 ? { quorum: raw as GateQuorumRule[], ...extra } : extra;
709
724
  }
710
725
 
711
726
  /** The holds at base (#4545), or why they cannot be read. */
@@ -870,6 +885,13 @@ export interface RunOpWaveOptions {
870
885
  * files go on the run record. See `./op-waves-after-plan.ts`.
871
886
  */
872
887
  afterPlan?: OpWaveAfterPlanHook | readonly OpWaveAfterPlanHook[];
888
+ /**
889
+ * Hooks around each run of a wave with a `step`, in the job's own process
890
+ * (#4842): `beforePlan` gives a run its engine's environment and a check
891
+ * after init, or refuses it; `beforeApply` and `afterApply` run around
892
+ * its apply. See `./op-waves-run-hooks.ts`.
893
+ */
894
+ runHooks?: OpWaveRunHooks | readonly OpWaveRunHooks[];
873
895
  /**
874
896
  * How the spec's command hooks run (#4614), when they call back into the
875
897
  * caller's own process: a product that runs the wave in process and serves
@@ -947,8 +969,14 @@ interface WaveContext {
947
969
  gateRule: { approval: GateApprovalSource; base: string; neverOverMcp: boolean };
948
970
  /** With a `step` (#4545): each run's plan through it, by target, which its apply hands back. */
949
971
  steps?: Map<string, OpWaveStepPlan>;
972
+ /** Each run's environment from the run hooks (#4842), by target, which its plan and its apply run with. */
973
+ runEnv?: Map<string, Record<string, string>>;
950
974
  /** The plans file's change set (#4551), which a `pull-request` gate checks the pull request's approval against (#4550). */
951
975
  changeSetDoc?: ChangeSetDocument;
976
+ /** An after-plan hook's report of the wave's plans (#4842), stored beside the pending fact. */
977
+ report?: string;
978
+ /** The approvals the after-plan hooks settled before the gate (terragucci issue 1303), recorded at it before it is decided. */
979
+ answerApprovals?: OpWaveAnswerApproval[];
952
980
  }
953
981
 
954
982
  function planRun(opts: { spec: OpWavesSpec; wave: number; cwd: string; exec: OpWaveExec }, waveName: string, run: OpWaveRun): OpWaveMember {
@@ -1116,13 +1144,45 @@ async function planRuns(at: { spec: OpWavesSpec; k: number; wave: OpWave; cwd: s
1116
1144
  };
1117
1145
  }
1118
1146
 
1147
+ /** Which job of the wave this is, as a hook names it. */
1148
+ function jobKind(options: Pick<RunOpWaveOptions, "decide" | "share" | "group">): "wave" | "decide" | "share" | "group" {
1149
+ return options.decide ? "decide" : options.group !== undefined ? "group" : options.share !== undefined ? "share" : "wave";
1150
+ }
1151
+
1152
+ /** A run as a run hook sees it (#4842). */
1153
+ function runHookRun(at: { spec: OpWavesSpec; k: number; wave: OpWave; cwd: string; options: Pick<RunOpWaveOptions, "decide" | "share" | "group"> }, run: OpWaveRun) {
1154
+ return { spec: at.spec.name, op: at.spec.op, wave: at.k, waveName: at.wave.name, job: jobKind(at.options), target: run.target, runOp: run.op ?? at.spec.op, cwd: at.cwd };
1155
+ }
1156
+
1157
+ /**
1158
+ * Apply one run through the spec's step (#4545) with the product's run hooks
1159
+ * around it (#4842): `beforeApply` first, which can stop it, then the apply
1160
+ * with the run's environment, then `afterApply`, whose files go on the
1161
+ * result and whose failure fails a run that applied.
1162
+ */
1163
+ async function applyStepRun(ctx: WaveContext, registry: CapabilityRegistry, run: OpWaveRun, plan: OpWaveStepPlan): Promise<OpWaveStepApply & { files?: string[] }> {
1164
+ const { spec, wave, k, options } = ctx;
1165
+ const hooks = opWaveRunHookList(options.runHooks);
1166
+ const env = ctx.runEnv?.get(run.target);
1167
+ if (hooks.length === 0) return applyOpWaveStep({ spec, wave, registry, cwd: ctx.cwd, ...(env ? { processEnv: env } : {}) }, run, plan);
1168
+ const planned = { ...runHookRun(ctx, run), planDigest: plan.planDigest, plan: opWavePlanPath(spec, k, run.target), ...(plan.artifact !== undefined ? { artifact: plan.artifact } : {}) };
1169
+ const stop = await runOpWaveBeforeApply(hooks, planned);
1170
+ if (stop !== undefined) return { outcome: "failed", reason: stop };
1171
+ const got = await applyOpWaveStep({ spec, wave, registry, cwd: ctx.cwd, ...(env ? { processEnv: env } : {}) }, run, plan);
1172
+ const after = await runOpWaveAfterApply(hooks, { ...planned, outcome: got.outcome, ...(got.outcome === "applied" ? { output: got.output } : { reason: got.reason }) });
1173
+ const files = after.files.length > 0 ? { files: after.files } : {};
1174
+ if (after.failed && got.outcome === "applied") return { outcome: "failed", reason: `applied, and then ${after.failed}`, ...(got.facts ? { facts: got.facts } : {}), ...files };
1175
+ if (after.failed) ctx.log(`${run.target}: ${after.failed}`);
1176
+ return { ...got, ...files };
1177
+ }
1178
+
1119
1179
  /**
1120
1180
  * Plan `runs` of wave `k` (#4545): through the spec's `step` when it names
1121
1181
  * one, each run's plan kept on `at.steps` for its apply, else with the
1122
1182
  * commands ({@link planRuns}).
1123
1183
  */
1124
1184
  async function planWaveRuns(
1125
- at: { spec: OpWavesSpec; k: number; wave: OpWave; cwd: string; exec: OpWaveExec; options: Pick<RunOpWaveOptions, "capabilities" | "lockGrain" | "base" | "waveExec">; steps?: Map<string, OpWaveStepPlan> },
1185
+ at: { spec: OpWavesSpec; k: number; wave: OpWave; cwd: string; exec: OpWaveExec; options: Pick<RunOpWaveOptions, "capabilities" | "lockGrain" | "base" | "waveExec" | "runHooks" | "decide" | "share" | "group">; steps?: Map<string, OpWaveStepPlan>; runEnv?: Map<string, Record<string, string>> },
1126
1186
  runs: readonly OpWaveRun[],
1127
1187
  discover = false,
1128
1188
  ): Promise<PlannedRuns> {
@@ -1133,11 +1193,42 @@ async function planWaveRuns(
1133
1193
  const lockGrain = at.options.lockGrain ?? (await declaredLockGrain(cwd, base));
1134
1194
  // #4613: a step's own policy engine (CrossGuard) reads its packs at the base the wave's rules were read at.
1135
1195
  const policyBase = base && base !== "HEAD" ? base : undefined;
1196
+ // #4842: a discovering wave's runs are the ones its step's capability finds for wave k.
1197
+ const found = discover ? await discoverOpWaveStepRuns({ spec, wave, k, registry, cwd }) : undefined;
1198
+ if (found) runs = found.runs;
1136
1199
  const plans: OpWaveStepPlan[] = [];
1137
- for (const run of runs) {
1138
- const plan = await planOpWaveStep({ spec, wave, cwd, registry, planPath: opWavePlanPath(spec, k, run.target), lockGrain, ...(policyBase ? { policyBase } : {}) }, run);
1139
- at.steps?.set(run.target, plan);
1140
- plans.push(plan);
1200
+ const hooks = opWaveRunHookList(at.options.runHooks);
1201
+ const beforePlan = async (run: OpWaveRun): Promise<{ env?: Record<string, string>; afterInit?: () => Promise<void> }> => {
1202
+ // #4842: the product's run hooks give the run its environment and a check after init, or refuse it.
1203
+ const before = hooks.length > 0 ? await runOpWaveBeforePlan(hooks, runHookRun(at, run)) : {};
1204
+ if ("refused" in before) throw new Error(`${run.target} was not planned: ${before.refused}`);
1205
+ if (before.env) {
1206
+ at.runEnv ??= new Map();
1207
+ at.runEnv.set(run.target, before.env);
1208
+ }
1209
+ return before;
1210
+ };
1211
+ let together: string | undefined;
1212
+ if (wave.together && runs.length > 0) {
1213
+ // #4842: one plan of the whole wave, each run's hooks first.
1214
+ const own = new Map<string, { env?: Record<string, string>; afterInit?: () => Promise<void> }>();
1215
+ for (const run of runs) own.set(run.target, await beforePlan(run));
1216
+ const runCtx = (target: string) => {
1217
+ const b = own.get(target) ?? {};
1218
+ return { ...(b.env ? { processEnv: b.env } : {}), ...(b.afterInit ? { afterInit: b.afterInit } : {}) };
1219
+ };
1220
+ for (const plan of await planOpWaveStepGroup({ spec, wave, cwd, registry, planPath: (target) => opWavePlanPath(spec, k, target), lockGrain, ...(policyBase ? { policyBase } : {}), runCtx }, runs)) {
1221
+ at.steps?.set(plan.target, plan);
1222
+ plans.push(plan);
1223
+ }
1224
+ together = plans[0]?.group?.digest;
1225
+ } else {
1226
+ for (const run of runs) {
1227
+ const before = await beforePlan(run);
1228
+ const plan = await planOpWaveStep({ spec, wave, cwd, registry, planPath: opWavePlanPath(spec, k, run.target), lockGrain, ...(policyBase ? { policyBase } : {}), ...(before.env ? { processEnv: before.env } : {}), ...(before.afterInit ? { afterInit: before.afterInit } : {}) }, run);
1229
+ at.steps?.set(run.target, plan);
1230
+ plans.push(plan);
1231
+ }
1141
1232
  }
1142
1233
  const members = plans.map((p) => ({
1143
1234
  member: p.target,
@@ -1149,7 +1240,14 @@ async function planWaveRuns(
1149
1240
  }));
1150
1241
  const parts = new Map(plans.flatMap((p) => (p.part ? [[p.target, p.part] as const] : [])));
1151
1242
  const changeSet = opWaveStepChangeSet(plans);
1152
- return { members, ...(parts.size > 0 ? { parts } : {}), ...(changeSet ? { changeSet } : {}) };
1243
+ return {
1244
+ members,
1245
+ ...(parts.size > 0 ? { parts } : {}),
1246
+ ...(changeSet ? { changeSet } : {}),
1247
+ // #4842: the capability's digest of the whole wave is a digest member of its own.
1248
+ ...(together ? { extras: [{ member: OP_WAVE_TOGETHER_MEMBER, planDigest: together }] } : {}),
1249
+ ...(found?.waves !== undefined ? { waves: found.waves } : {}),
1250
+ };
1153
1251
  }
1154
1252
 
1155
1253
  /**
@@ -1346,7 +1444,7 @@ async function runAfterPlan(
1346
1444
  members: OpWaveMember[],
1347
1445
  parts: ReadonlyMap<string, ChangeSetPart> | undefined,
1348
1446
  policyRows: ReadonlyMap<string, OpRunPolicyRow>,
1349
- ): Promise<{ failed?: string; holds: string[] }> {
1447
+ ): Promise<{ failed?: string; holds: string[]; wave?: OpWaveAfterPlanWaveOutcome }> {
1350
1448
  const { spec, k, wave, cwd, options, result } = ctx;
1351
1449
  const hooks = afterPlanHooks(options);
1352
1450
  const job: OpWaveAfterPlanRun["job"] = options.decide ? "decide" : options.group !== undefined ? "group" : options.share !== undefined ? "share" : "wave";
@@ -1387,11 +1485,12 @@ async function runAfterPlan(
1387
1485
  if (folded.denied) m.denied = folded.denied;
1388
1486
  if (folded.held) m.held = folded.held;
1389
1487
  }
1488
+ if (mine?.overriders) m.overriders = mine.overriders;
1390
1489
  if (rows.length === 0 && !mine?.files.length) continue;
1391
1490
  plans[m.member] = { planDigest: m.planDigest, ...(rows.length ? { policy: rows } : {}), ...(mine?.files.length ? { files: [...mine.files] } : {}) };
1392
1491
  }
1393
1492
  if (Object.keys(plans).length > 0) result.plans = plans;
1394
- return { ...(out?.failed ? { failed: out.failed } : {}), holds: out?.holds ?? [] };
1493
+ return { ...(out?.failed ? { failed: out.failed } : {}), holds: out?.holds ?? [], ...(out ? { wave: out.wave } : {}) };
1395
1494
  }
1396
1495
 
1397
1496
  /** One field of a plan file, or undefined when the file or the field is missing. */
@@ -1894,6 +1993,8 @@ export async function runOpWave(options: RunOpWaveOptions): Promise<RunOpWaveRes
1894
1993
  assertOpWavesSpec(options.spec);
1895
1994
  const afterPlanWrong = opWaveAfterPlanProblems(options.afterPlan);
1896
1995
  if (afterPlanWrong.length > 0) throw new Error(`Op waves "${options.spec.name}": ${afterPlanWrong.join("; ")}`);
1996
+ const runHooksWrong = opWaveRunHooksProblems(options.runHooks);
1997
+ if (runHooksWrong.length > 0) throw new Error(`Op waves "${options.spec.name}": ${runHooksWrong.join("; ")}`);
1897
1998
  // #4551: one run record per job, on every way the job ends.
1898
1999
  const started = new Date().toISOString();
1899
2000
  // #4555: the job's run events and its trace, when a reader or an exporter wants them.
@@ -1952,7 +2053,7 @@ function targetStarted(options: RunOpWaveOptions, k: number, target: string): nu
1952
2053
  }
1953
2054
 
1954
2055
  /** A target's apply ended (#4555): its time and facts on the result, and its `step-settle`. */
1955
- function targetSettled(options: RunOpWaveOptions, result: RunOpWaveResult, k: number, target: string, startedMs: number, status: "ok" | "failed" | "skipped", opts: { error?: string; facts?: CapabilityTraceFacts } = {}): void {
2056
+ function targetSettled(options: RunOpWaveOptions, result: RunOpWaveResult, k: number, target: string, startedMs: number, status: "ok" | "failed" | "skipped", opts: { error?: string; facts?: CapabilityTraceFacts; files?: string[] } = {}): void {
1956
2057
  const durationMs = Math.max(0, Date.now() - startedMs);
1957
2058
  result.targets = {
1958
2059
  ...result.targets,
@@ -1966,6 +2067,8 @@ function targetSettled(options: RunOpWaveOptions, result: RunOpWaveResult, k: nu
1966
2067
  ...(status === "failed" && opts.facts?.resources?.some((r) => r.status === "failed")
1967
2068
  ? { failedResources: opts.facts.resources.filter((r) => r.status === "failed").map((r) => r.name ?? r.resource) }
1968
2069
  : {}),
2070
+ // #4842: what the run hooks wrote after the apply.
2071
+ ...(opts.files?.length ? { files: opts.files } : {}),
1969
2072
  },
1970
2073
  };
1971
2074
  jobEvents.get(options)?.stepSettle(k, target, status, durationMs, opts);
@@ -2501,7 +2604,7 @@ async function planOpWave(options: RunOpWaveOptions, { k, pass }: WaveAttempt<Ru
2501
2604
  if (before.failed) return { end: failed(ctx, `a hook before the plan failed, so nothing in wave ${k} was planned or applied`) };
2502
2605
  if (before.waitingOn) return { end: await decided(ctx, decision, recordPath, before.waitingOn) };
2503
2606
  if (spec.step) ctx.steps = new Map();
2504
- const { members, pending, overriders, overridersSource, extras: planExtras, waves: found, changeSet, parts } = await planWaveRuns(ctx, wave.runs, wave.discover === true);
2607
+ const { members, pending: planPending, overriders: planOverriders, overridersSource: planOverridersSource, extras: planExtras, waves: found, changeSet, parts } = await planWaveRuns(ctx, wave.runs, wave.discover === true);
2505
2608
  // #4545: the holds at base (else the change's), run over the plans before the gate decides.
2506
2609
  let extras = planExtras;
2507
2610
  let policyRows = new Map<string, OpRunPolicyRow>();
@@ -2521,6 +2624,20 @@ async function planOpWave(options: RunOpWaveOptions, { k, pass }: WaveAttempt<Ru
2521
2624
  const inProcess = await runAfterPlan(ctx, members, parts, policyRows);
2522
2625
  if (inProcess.failed) return { end: failed(ctx, `${inProcess.failed}, so nothing in wave ${k} was approved or applied`) };
2523
2626
  holds.push(...inProcess.holds);
2627
+ // #4842: what the hooks added to the wave as a whole, over what a plans file gave.
2628
+ const answer = inProcess.wave;
2629
+ holds.push(...(answer?.holds ?? []));
2630
+ const pending = answer?.pending ? { ...planPending, ...answer.pending } : planPending;
2631
+ const overriders = answer?.overriders ?? planOverriders;
2632
+ const overridersSource = answer?.overriders ? answer.overridersSource : planOverridersSource;
2633
+ if (answer?.members.length) {
2634
+ const named = new Set((extras ?? []).map((m) => m.member));
2635
+ const clash = answer.members.find((m) => named.has(m.member));
2636
+ if (clash) return { end: failed(ctx, `wave ${k} (${wave.name}): an after-plan hook and the wave's holds both name the digest member ${clash.member}, so nothing in it was approved or applied`) };
2637
+ extras = [...(extras ?? []), ...answer.members];
2638
+ }
2639
+ if (answer?.report !== undefined) ctx.report = answer.report;
2640
+ if (answer?.approvals.length) ctx.answerApprovals = answer.approvals;
2524
2641
  if (wave.discover) {
2525
2642
  wave = { ...wave, runs: members.map((m) => ({ target: m.member, ...(m.op !== spec.op ? { op: m.op } : {}) })) };
2526
2643
  ctx.wave = wave;
@@ -2594,8 +2711,9 @@ async function decideOpWave(
2594
2711
  decision.members = members;
2595
2712
  // #4290: the rules at base whose targets glob matches one of the wave's runs.
2596
2713
  const takes = planned.rules.quorum ? quorumRulesFor(planned.rules.quorum, members.map((m) => m.member)) : [];
2597
- if (takes.length > 0) {
2598
- decision.quorum = takes;
2714
+ if (takes.length > 0) decision.quorum = takes;
2715
+ // terragucci issue 1303: a wave no rule covers still leaves the involved out, and counts a review the product recorded.
2716
+ if (takes.length > 0 || opWaveOneApprover(decision.approval, planned.rules)) {
2599
2717
  if (planned.rules.reviewLedger) decision.reviewLedger = planned.rules.reviewLedger;
2600
2718
  if (planned.rules.involved) decision.involved = planned.rules.involved;
2601
2719
  }
@@ -2638,10 +2756,13 @@ async function decideOpWave(
2638
2756
  decision.status = "approved";
2639
2757
  log(`wave ${k} (${wave.name}): planned again to the same digest, so the approval by ${was.approvedBy} still holds`);
2640
2758
  } else if (decision.status !== "refused" && (unseen || opWaveNeedsApproval(policy, members) || holds.length > 0)) {
2759
+ // terragucci issue 1303: what the product settled before the gate, recorded at it for these plans first.
2760
+ if (ctx.answerApprovals?.length) await recordAnswerApprovals(options, ctx, decision);
2641
2761
  await settleGate(options, ctx, decision);
2642
2762
  // Plans nobody previewed apply only under an approval of these very plans, whatever the gate policy says.
2643
2763
  if (unseen) refuseUnseen(ctx, decision);
2644
2764
  }
2765
+ if (ctx.report !== undefined) await keepWaveReport(options, ctx, decision, ctx.report);
2645
2766
  const code = await decided(ctx, decision, recordPath);
2646
2767
  if (code.exitCode !== 0) return { end: code };
2647
2768
  log(
@@ -2659,6 +2780,51 @@ async function decideOpWave(
2659
2780
  return { go: { runs: wave.runs, earlierHolds: [], recordPath } };
2660
2781
  }
2661
2782
 
2783
+ /** Where a wave's reports go for the job that explains a moved approval (#4842), relative to the job's directory. */
2784
+ export function opWaveReportDir(spec: Pick<OpWavesSpec, "name">, k: number): string {
2785
+ return `.chant/op-waves/${spec.name}/wave-${k}/report`;
2786
+ }
2787
+
2788
+ /**
2789
+ * Keep an after-plan hook's report of the wave's plans (#4842). While the
2790
+ * wave waits, it is stored beside the pending fact, once per digest. When
2791
+ * the plans moved after an approval, it and the report stored for the
2792
+ * approved plans are written to {@link opWaveReportDir} and named on the
2793
+ * decision. A report that cannot be kept is logged; it never decides the gate.
2794
+ */
2795
+ async function keepWaveReport(options: RunOpWaveOptions, ctx: WaveContext, decision: OpWaveDecision, report: string): Promise<void> {
2796
+ if (decision.status !== "waiting") return;
2797
+ const { spec, k, wave, cwd, log } = ctx;
2798
+ const port = options.gates ?? gitGateLedgerPort({ cwd });
2799
+ const label = `wave ${k} (${wave.name})`;
2800
+ if (port.storeGateReport) {
2801
+ try {
2802
+ await port.storeGateReport(spec.name, decision.gate, decision.digest, report);
2803
+ } catch (err) {
2804
+ log(`${label}: the report of these plans was not kept beside the pending fact: ${err instanceof Error ? err.message : String(err)}`);
2805
+ }
2806
+ }
2807
+ const was = decision.mismatch?.approved;
2808
+ if (was === undefined) return;
2809
+ let approved: string | null = null;
2810
+ try {
2811
+ approved = (await port.readGateReport?.(spec.name, decision.gate, was)) ?? null;
2812
+ } catch (err) {
2813
+ log(`${label}: the report of the approved plans could not be read: ${err instanceof Error ? err.message : String(err)}`);
2814
+ }
2815
+ if (approved === null) log(`${label}: no report was kept for the approved plans ${was}`);
2816
+ const dir = opWaveReportDir(spec, k);
2817
+ mkdirSync(join(cwd, dir), { recursive: true });
2818
+ const current = `${dir}/current.json`;
2819
+ writeFileSync(join(cwd, current), report);
2820
+ const reports: { current: string; approved?: string } = { current };
2821
+ if (approved !== null) {
2822
+ reports.approved = `${dir}/approved.json`;
2823
+ writeFileSync(join(cwd, reports.approved), approved);
2824
+ }
2825
+ decision.reports = reports;
2826
+ }
2827
+
2662
2828
  /**
2663
2829
  * Compare the wave's set digest with the one the merged pull request's head
2664
2830
  * recorded for it (`--record-plans`): the plans its reviewers saw. The pull
@@ -2858,14 +3024,16 @@ async function settleDenials(ctx: WaveContext, decision: OpWaveDecision, listed:
2858
3024
  let moved = false;
2859
3025
  for (const m of decision.members) {
2860
3026
  if (!m.denied || m.override) continue;
3027
+ // #4842: a run's own overriders, from an after-plan hook, count for it in place of the wave's.
3028
+ const mayOverride = m.overriders ?? overriders;
2861
3029
  const verdict = og.decideOverride({
2862
3030
  ledger,
2863
3031
  applied,
2864
3032
  subject: m.member,
2865
3033
  planDigest: m.planDigest,
2866
3034
  rules: m.denied.rules,
2867
- overriders,
2868
- overridersPath: listed ? (source ?? "the overriders the plans file names") : path,
3035
+ overriders: mayOverride,
3036
+ overridersPath: m.overriders ? (source ?? `the overriders an after-plan hook names for ${m.member}`) : listed ? (source ?? "the overriders the plans file names") : path,
2869
3037
  mode: sealed ? "sealed" : "ledger",
2870
3038
  ...(signers ? { signers } : {}),
2871
3039
  now,
@@ -2897,7 +3065,7 @@ async function settleDenials(ctx: WaveContext, decision: OpWaveDecision, listed:
2897
3065
  log(`${m.member}: the denial was not recorded for an override: ${err instanceof Error ? err.message : String(err)}`);
2898
3066
  }
2899
3067
  }
2900
- log(`${m.member}: ${overriders.length ? `${overriders.join(", ")} may` : "nobody listed may"} override it with: ${og.overrideCommand(m.member, m.denied.rules, sealed)}`);
3068
+ log(`${m.member}: ${mayOverride.length ? `${mayOverride.join(", ")} may` : "nobody listed may"} override it with: ${og.overrideCommand(m.member, m.denied.rules, sealed)}`);
2901
3069
  }
2902
3070
  if (still.length > 0) {
2903
3071
  decision.status = "refused";
@@ -2963,6 +3131,8 @@ function failed(ctx: WaveContext, line: string): RunOpWaveResult {
2963
3131
  async function settleGate(options: RunOpWaveOptions, ctx: WaveContext, decision: OpWaveDecision): Promise<void> {
2964
3132
  if (decision.point) return settlePointGate(options, ctx, decision, decision.point);
2965
3133
  if (decision.quorum?.length) return settleQuorumGate(options, ctx, decision, decision.quorum);
3134
+ // terragucci issue 1303: with no rule, `approvals.involved.exclude` and a review ledger still judge each approval, as one approver's rule does.
3135
+ if ((decision.involved?.exclude || decision.reviewLedger) && decision.approval !== "pull-request") return settleQuorumGate(options, ctx, decision, [{ count: 1 }], true);
2966
3136
  const { spec, k, wave, cwd } = ctx;
2967
3137
  const { gate, digest, approval, base, policy, members } = decision;
2968
3138
  // #4574: the run target a product's approve line names.
@@ -3118,7 +3288,7 @@ const gitCommitsBetween = (cwd: string) => (base: string, head: string): { sha:
3118
3288
  * the same pull request; with none, no approver can show they are someone
3119
3289
  * else and the rule waits.
3120
3290
  */
3121
- async function settleQuorumGate(options: RunOpWaveOptions, ctx: WaveContext, decision: OpWaveDecision, rules: GateQuorumRule[]): Promise<void> {
3291
+ async function settleQuorumGate(options: RunOpWaveOptions, ctx: WaveContext, decision: OpWaveDecision, rules: GateQuorumRule[], oneApprover = false): Promise<void> {
3122
3292
  const { spec, k, wave, cwd, log } = ctx;
3123
3293
  const { gate, digest, approval, base, policy, members } = decision;
3124
3294
  const { ledger, port } = await gatePort(options, cwd, gate, approval, base);
@@ -3223,7 +3393,11 @@ async function settleQuorumGate(options: RunOpWaveOptions, ctx: WaveContext, dec
3223
3393
  ...(reviewed ? { review: reviewed } : {}),
3224
3394
  ...(recorded.length > 0 ? { recorded } : {}),
3225
3395
  });
3226
- const judge: GateQuorumJudge = (r) => sayExcluded(judgeBase(r));
3396
+ // terragucci issue 1303: on the spec's own ledger a product's review line names every reviewer in one approver, and each reviewer counts on their own line.
3397
+ const judge: GateQuorumJudge = (r) =>
3398
+ decision.reviewLedger === spec.name && recordedReviewOf(r) !== null
3399
+ ? { proven: false, roles: [], excluded: `the review line by ${r.resolvedBy} counts each of its reviewers on their own` }
3400
+ : sayExcluded(judgeBase(r));
3227
3401
  const held = decision.heldBy?.length ? `, held by ${decision.heldBy.join(", ")}` : "";
3228
3402
  const check = await decideWaveGate(port, {
3229
3403
  op: spec.name,
@@ -3243,7 +3417,7 @@ async function settleQuorumGate(options: RunOpWaveOptions, ctx: WaveContext, dec
3243
3417
  decision.via = reviewed && check.resolution.review?.head === reviewed.head ? "pr-review" : "ledger";
3244
3418
  decision.approvedBy = by.join(", ");
3245
3419
  decision.approvedAt = check.resolution.timestamp;
3246
- log(`${label}: the approval rules at ${base.slice(0, 12)} are met by ${by.join(", ")}`);
3420
+ log(oneApprover ? `${label}: approved by ${by.join(", ")}, who had no hand in the change` : `${label}: the approval rules at ${base.slice(0, 12)} are met by ${by.join(", ")}`);
3247
3421
  return;
3248
3422
  }
3249
3423
  logRefused(log, label, check);
@@ -3251,7 +3425,8 @@ async function settleQuorumGate(options: RunOpWaveOptions, ctx: WaveContext, dec
3251
3425
  decision.recorded = check.recorded;
3252
3426
  decision.waitingSince = check.pending.timestamp;
3253
3427
  if (check.mismatch) decision.mismatch = check.mismatch;
3254
- if (check.quorum?.rules) {
3428
+ if (oneApprover) log(`${label}: no approval of ${digest} counts once the people behind the change are left out, so it waits for one`);
3429
+ else if (check.quorum?.rules) {
3255
3430
  decision.quorumProgress = check.quorum.rules;
3256
3431
  for (const p of check.quorum.rules) log(`${label}: approval rule ${quorumRuleLine(p)}`);
3257
3432
  log(`${label}: the approval rules at ${base.slice(0, 12)} are not met (${quorumWaiting(check.quorum.rules)}), so it waits for more approvals of ${digest}`);
@@ -3641,6 +3816,9 @@ async function applyLeased(
3641
3816
  log(`wave ${k} (${wave.name}): apply failed, so none of ${runs.map((r) => r.target).join(", ")} counts as applied`);
3642
3817
  result.failed.push(...runs.map((r) => r.target));
3643
3818
  }
3819
+ } else if (spec.step && wave.together) {
3820
+ // #4842: the wave's runs apply in one call of the step's capability.
3821
+ await applyStepRunsTogether(options, ctx, runs);
3644
3822
  } else if (spec.step && (wave.parallelism ?? spec.parallelism ?? 1) > 1) {
3645
3823
  // #4625: the step's runs apply several at once.
3646
3824
  await applyStepRunsAtOnce(options, ctx, runs, wave.parallelism ?? spec.parallelism!);
@@ -3665,10 +3843,11 @@ async function applyLeased(
3665
3843
  }
3666
3844
  const registry = await opWaveRegistry(options, ctx.cwd, options.capabilities);
3667
3845
  const startedMs = targetStarted(options, k, run.target);
3668
- const got = await applyOpWaveStep({ spec, wave, registry }, run, plan);
3846
+ const got = await applyStepRun(ctx, registry, run, plan);
3669
3847
  targetSettled(options, result, k, run.target, startedMs, got.outcome === "applied" ? "ok" : got.outcome === "moved" ? "skipped" : "failed", {
3670
3848
  ...(got.outcome !== "applied" ? { error: got.reason } : {}),
3671
3849
  ...(got.facts ? { facts: got.facts } : {}),
3850
+ ...(got.files ? { files: got.files } : {}),
3672
3851
  });
3673
3852
  if (got.outcome === "applied") result.applied.push(run.target);
3674
3853
  else if (got.outcome === "moved") {
@@ -3742,6 +3921,64 @@ async function applyLeased(
3742
3921
  if (recordPath && result.hooks.length > 0) writeRecord(recordPath, decision, result);
3743
3922
  }
3744
3923
 
3924
+ /**
3925
+ * A `together` wave's runs applied in one `runGroup` call (#4842), with the
3926
+ * run hooks around it: each run's `beforeApply` first, and any that stops
3927
+ * one fails every run with nothing applied, since the one call applies them
3928
+ * all; then the call, each run with its own environment; then each run's
3929
+ * `afterApply`, whose files go on the run and whose failure fails a run that
3930
+ * applied. The outcomes land in the result in the wave's order.
3931
+ */
3932
+ async function applyStepRunsTogether(options: RunOpWaveOptions, ctx: WaveContext, runs: readonly OpWaveRun[]): Promise<void> {
3933
+ const { spec, k, wave, log, result } = ctx;
3934
+ const missing = runs.filter((r) => !ctx.steps?.get(r.target)).map((r) => r.target);
3935
+ if (missing.length > 0) {
3936
+ log(`wave ${k} (${wave.name}): this job holds no plan of ${missing.join(", ")}, so none of its runs applied`);
3937
+ result.failed.push(...runs.map((r) => r.target));
3938
+ return;
3939
+ }
3940
+ const registry = await opWaveRegistry(options, ctx.cwd, options.capabilities);
3941
+ const hooks = opWaveRunHookList(options.runHooks);
3942
+ const planned = runs.map((run) => {
3943
+ const plan = ctx.steps!.get(run.target)!;
3944
+ return { run, plan, hook: { ...runHookRun(ctx, run), planDigest: plan.planDigest, plan: opWavePlanPath(spec, k, run.target), ...(plan.artifact !== undefined ? { artifact: plan.artifact } : {}) } };
3945
+ });
3946
+ const startedMs = new Map(runs.map((r) => [r.target, targetStarted(options, k, r.target)]));
3947
+ for (const p of hooks.length > 0 ? planned : []) {
3948
+ const stop = await runOpWaveBeforeApply(hooks, p.hook);
3949
+ if (stop === undefined) continue;
3950
+ log(`${p.run.target}: ${stop}, so none of wave ${k}'s runs applied`);
3951
+ for (const r of runs) targetSettled(options, result, k, r.target, startedMs.get(r.target)!, "failed", { error: r.target === p.run.target ? stop : `not applied: ${p.run.target}: ${stop}` });
3952
+ result.failed.push(...runs.map((r) => r.target));
3953
+ return;
3954
+ }
3955
+ const got = await applyOpWaveStepGroup({ spec, wave, registry, cwd: ctx.cwd, runEnv: (target) => ctx.runEnv?.get(target) }, planned);
3956
+ for (const [i, p] of planned.entries()) {
3957
+ let outcome = got[i]!;
3958
+ let files: string[] | undefined;
3959
+ if (hooks.length > 0) {
3960
+ const after = await runOpWaveAfterApply(hooks, { ...p.hook, outcome: outcome.outcome, ...(outcome.outcome === "applied" ? { output: outcome.output } : { reason: outcome.reason }) });
3961
+ if (after.files.length > 0) files = after.files;
3962
+ if (after.failed && outcome.outcome === "applied") outcome = { outcome: "failed", reason: `applied, and then ${after.failed}`, ...(outcome.facts ? { facts: outcome.facts } : {}) };
3963
+ else if (after.failed) log(`${p.run.target}: ${after.failed}`);
3964
+ }
3965
+ const target = p.run.target;
3966
+ targetSettled(options, result, k, target, startedMs.get(target)!, outcome.outcome === "applied" ? "ok" : outcome.outcome === "moved" ? "skipped" : "failed", {
3967
+ ...(outcome.outcome !== "applied" ? { error: outcome.reason } : {}),
3968
+ ...(outcome.facts ? { facts: outcome.facts } : {}),
3969
+ ...(files ? { files } : {}),
3970
+ });
3971
+ if (outcome.outcome === "applied") result.applied.push(target);
3972
+ else if (outcome.outcome === "moved") {
3973
+ log(`${target}: its plan moved since wave ${k} was decided, so it was not applied: ${outcome.reason}`);
3974
+ result.moved.push(target);
3975
+ } else {
3976
+ log(`${target}: apply failed: ${outcome.reason}`);
3977
+ result.failed.push(target);
3978
+ }
3979
+ }
3980
+ }
3981
+
3745
3982
  /**
3746
3983
  * A wave's step runs applied at most `limit` at once (#4625). A run waits
3747
3984
  * for each run the wave lists before it that it depends on, and is blocked,
@@ -3771,10 +4008,11 @@ async function applyStepRunsAtOnce(options: RunOpWaveOptions, ctx: WaveContext,
3771
4008
  return { failed: true };
3772
4009
  }
3773
4010
  const startedMs = targetStarted(options, k, run.target);
3774
- const got = await applyOpWaveStep({ spec, wave, registry }, run, plan);
4011
+ const got = await applyStepRun(ctx, registry, run, plan);
3775
4012
  targetSettled(options, result, k, run.target, startedMs, got.outcome === "applied" ? "ok" : got.outcome === "moved" ? "skipped" : "failed", {
3776
4013
  ...(got.outcome !== "applied" ? { error: got.reason } : {}),
3777
4014
  ...(got.facts ? { facts: got.facts } : {}),
4015
+ ...(got.files ? { files: got.files } : {}),
3778
4016
  });
3779
4017
  if (got.outcome === "applied") return { applied: true };
3780
4018
  if (got.outcome === "moved") {
@@ -4028,3 +4266,53 @@ async function writeHeadPlans(plans: HeadPlans, cwd: string): Promise<void> {
4028
4266
  const pushed = await pushLifecycleRebasing({ cwd });
4029
4267
  if (!pushed.pushed) throw new Error(`the plans were recorded locally only: ${pushed.pushWarning ?? "chant/lifecycle has no remote to push to"}`);
4030
4268
  }
4269
+
4270
+ /**
4271
+ * Whether a wave no approval rule covers is still judged approver by
4272
+ * approver (terragucci issue 1303): `approvals.involved.exclude`, or a
4273
+ * review ledger under `pr-review`, at base. Not under `pull-request`, whose
4274
+ * approval is the pull request's gate.
4275
+ */
4276
+ function opWaveOneApprover(approval: GateApprovalSource, rules: Pick<OpWaveBaseRules, "involved" | "reviewLedger">): boolean {
4277
+ if (approval === "pull-request") return false;
4278
+ return rules.involved?.exclude === true || (rules.reviewLedger !== undefined && approval === "pr-review");
4279
+ }
4280
+
4281
+ /**
4282
+ * Record the approvals the after-plan hooks settled before the gate
4283
+ * (terragucci issue 1303) at the wave's gate, for the digest it decides, on
4284
+ * the spec's op. An approval already recorded for the digest by the same
4285
+ * approver with the same note is not written again. A gate that asks a
4286
+ * decision point records none: only its answer passes the wave.
4287
+ */
4288
+ async function recordAnswerApprovals(options: RunOpWaveOptions, ctx: WaveContext, decision: OpWaveDecision): Promise<void> {
4289
+ const { spec, k, wave, cwd, log } = ctx;
4290
+ const label = `wave ${k} (${wave.name})`;
4291
+ const approvals = ctx.answerApprovals ?? [];
4292
+ if (decision.point) {
4293
+ log(`${label}: the gate asks a decision point, so the ${approvals.length} approval${approvals.length === 1 ? "" : "s"} the after-plan hooks gave ${approvals.length === 1 ? "is" : "are"} not recorded`);
4294
+ return;
4295
+ }
4296
+ const ledger = options.gates ?? gitGateLedgerPort({ cwd });
4297
+ if (!ledger.appendResolution) {
4298
+ log(`${label}: the gate ledger takes no approvals, so the ones the after-plan hooks gave are not recorded`);
4299
+ return;
4300
+ }
4301
+ const now = options.now ?? new Date().toISOString();
4302
+ const had = (await ledger.read(spec.name)).resolutions.filter((r) => r.gate === decision.gate && samePlanDigest(r.planDigest, decision.digest));
4303
+ for (const a of approvals) {
4304
+ if (had.some((r) => r.resolvedBy === a.resolvedBy && r.note === a.note)) continue;
4305
+ const review = a.review ? { via: "pr-review", pr: a.review.pr, head: a.review.head, reviewers: a.review.reviewers } : {};
4306
+ await ledger.appendResolution({
4307
+ op: spec.name,
4308
+ gate: decision.gate,
4309
+ resolvedBy: a.resolvedBy,
4310
+ timestamp: now,
4311
+ planDigest: decision.digest,
4312
+ note: a.note,
4313
+ ...(a.url ? { url: a.url } : {}),
4314
+ ...(review as object),
4315
+ } as Parameters<NonNullable<GateLedgerPort["appendResolution"]>>[0]);
4316
+ log(`${label}: ${a.resolvedBy}'s approval recorded for ${decision.digest}: ${a.note}`);
4317
+ }
4318
+ }