@intentius/chant 0.124.76 → 0.124.78

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 (212) hide show
  1. package/dist/__fixtures__/agent-guard/plywood.d.ts +8 -0
  2. package/dist/__fixtures__/agent-guard/plywood.d.ts.map +1 -0
  3. package/dist/__fixtures__/agent-guard/plywood.js +19 -0
  4. package/dist/__fixtures__/agent-guard/terragucci.d.ts +8 -0
  5. package/dist/__fixtures__/agent-guard/terragucci.d.ts.map +1 -0
  6. package/dist/__fixtures__/agent-guard/terragucci.js +30 -0
  7. package/dist/__fixtures__/gated-ledger.d.ts +10 -0
  8. package/dist/__fixtures__/gated-ledger.d.ts.map +1 -0
  9. package/dist/__fixtures__/gated-ledger.js +51 -0
  10. package/dist/__fixtures__/notify-messages/events.d.ts +9 -0
  11. package/dist/__fixtures__/notify-messages/events.d.ts.map +1 -0
  12. package/dist/__fixtures__/notify-messages/events.js +21 -0
  13. package/dist/__fixtures__/product-words.d.ts +14 -0
  14. package/dist/__fixtures__/product-words.d.ts.map +1 -1
  15. package/dist/__fixtures__/product-words.js +11 -0
  16. package/dist/__fixtures__/terragucci-notify.d.ts +66 -0
  17. package/dist/__fixtures__/terragucci-notify.d.ts.map +1 -0
  18. package/dist/__fixtures__/terragucci-notify.js +133 -0
  19. package/dist/action-pins.d.ts +8 -0
  20. package/dist/action-pins.d.ts.map +1 -1
  21. package/dist/action-pins.js +9 -0
  22. package/dist/agent-guard.d.ts +143 -0
  23. package/dist/agent-guard.d.ts.map +1 -0
  24. package/dist/agent-guard.js +455 -0
  25. package/dist/ci-commit.d.ts +120 -5
  26. package/dist/ci-commit.d.ts.map +1 -1
  27. package/dist/ci-commit.js +214 -23
  28. package/dist/ci-own-jobs.d.ts +81 -0
  29. package/dist/ci-own-jobs.d.ts.map +1 -0
  30. package/dist/ci-own-jobs.js +233 -0
  31. package/dist/ci-product-words.d.ts +18 -4
  32. package/dist/ci-product-words.d.ts.map +1 -1
  33. package/dist/ci-product-words.js +26 -6
  34. package/dist/ci-product.d.ts +40 -1
  35. package/dist/ci-product.d.ts.map +1 -1
  36. package/dist/ci-product.js +40 -1
  37. package/dist/cli/handlers/approve.d.ts +5 -1
  38. package/dist/cli/handlers/approve.d.ts.map +1 -1
  39. package/dist/cli/handlers/approve.js +17 -11
  40. package/dist/cli/handlers/build.d.ts +1 -1
  41. package/dist/cli/handlers/build.d.ts.map +1 -1
  42. package/dist/cli/handlers/build.js +88 -2
  43. package/dist/cli/handlers/commit.d.ts.map +1 -1
  44. package/dist/cli/handlers/commit.js +3 -1
  45. package/dist/cli/handlers/notify-relay.d.ts +18 -1
  46. package/dist/cli/handlers/notify-relay.d.ts.map +1 -1
  47. package/dist/cli/handlers/notify-relay.js +90 -14
  48. package/dist/cli/handlers/pr.d.ts.map +1 -1
  49. package/dist/cli/handlers/pr.js +3 -1
  50. package/dist/cli/handlers/release.d.ts +5 -1
  51. package/dist/cli/handlers/release.d.ts.map +1 -1
  52. package/dist/cli/handlers/release.js +21 -9
  53. package/dist/cli/handlers/run-generate.d.ts.map +1 -1
  54. package/dist/cli/handlers/run-generate.js +2 -1
  55. package/dist/cli/main.d.ts.map +1 -1
  56. package/dist/cli/main.js +21 -1
  57. package/dist/cli/parse-args.d.ts.map +1 -1
  58. package/dist/cli/parse-args.js +60 -4
  59. package/dist/cli/registry.d.ts +16 -0
  60. package/dist/cli/registry.d.ts.map +1 -1
  61. package/dist/components/pr-env-components.d.ts +65 -0
  62. package/dist/components/pr-env-components.d.ts.map +1 -0
  63. package/dist/components/pr-env-components.js +162 -0
  64. package/dist/components/pr-policy.d.ts +15 -0
  65. package/dist/components/pr-policy.d.ts.map +1 -1
  66. package/dist/components/pr-policy.js +24 -1
  67. package/dist/components/release-compile.d.ts +15 -3
  68. package/dist/components/release-compile.d.ts.map +1 -1
  69. package/dist/components/release-compile.js +66 -15
  70. package/dist/config.d.ts +14 -8
  71. package/dist/config.d.ts.map +1 -1
  72. package/dist/config.js +16 -1
  73. package/dist/gate-policy.d.ts +14 -1
  74. package/dist/gate-policy.d.ts.map +1 -1
  75. package/dist/gate-policy.js +16 -6
  76. package/dist/lexicon.d.ts +25 -1
  77. package/dist/lexicon.d.ts.map +1 -1
  78. package/dist/lexicon.js +10 -0
  79. package/dist/lifecycle/gated-op.d.ts +95 -0
  80. package/dist/lifecycle/gated-op.d.ts.map +1 -0
  81. package/dist/lifecycle/gated-op.js +96 -0
  82. package/dist/lifecycle/git.d.ts.map +1 -1
  83. package/dist/lifecycle/git.js +83 -2
  84. package/dist/lifecycle/release-images.d.ts +77 -3
  85. package/dist/lifecycle/release-images.d.ts.map +1 -1
  86. package/dist/lifecycle/release-images.js +132 -6
  87. package/dist/notify-product.d.ts +11 -2
  88. package/dist/notify-product.d.ts.map +1 -1
  89. package/dist/notify-product.js +16 -4
  90. package/dist/notify-relay-repo.d.ts +131 -0
  91. package/dist/notify-relay-repo.d.ts.map +1 -0
  92. package/dist/notify-relay-repo.js +202 -0
  93. package/dist/notify-relay.d.ts +25 -1
  94. package/dist/notify-relay.d.ts.map +1 -1
  95. package/dist/notify-relay.js +27 -2
  96. package/dist/notify.d.ts +36 -4
  97. package/dist/notify.d.ts.map +1 -1
  98. package/dist/notify.js +31 -21
  99. package/dist/notify.schema.json +1 -0
  100. package/dist/op/activities/review-product.d.ts +111 -0
  101. package/dist/op/activities/review-product.d.ts.map +1 -0
  102. package/dist/op/activities/review-product.js +116 -0
  103. package/dist/op/op-waves-capability.js +2 -2
  104. package/dist/policy-engine.d.ts +95 -0
  105. package/dist/policy-engine.d.ts.map +1 -0
  106. package/dist/policy-engine.js +157 -0
  107. package/dist/pr-forge-reads.d.ts +124 -0
  108. package/dist/pr-forge-reads.d.ts.map +1 -0
  109. package/dist/pr-forge-reads.js +322 -0
  110. package/dist/pr-forge.d.ts +14 -0
  111. package/dist/pr-forge.d.ts.map +1 -1
  112. package/dist/pr-forge.js +21 -13
  113. package/dist/pr-plan-note.d.ts +293 -0
  114. package/dist/pr-plan-note.d.ts.map +1 -0
  115. package/dist/pr-plan-note.js +362 -0
  116. package/dist/release-floors.d.ts +9 -1
  117. package/dist/release-floors.d.ts.map +1 -1
  118. package/dist/release-floors.js +9 -1
  119. package/dist/state-export.d.ts +126 -0
  120. package/dist/state-export.d.ts.map +1 -0
  121. package/dist/state-export.js +156 -0
  122. package/dist/state-migration.d.ts +148 -0
  123. package/dist/state-migration.d.ts.map +1 -0
  124. package/dist/state-migration.js +146 -0
  125. package/dist/state-unlock.d.ts +167 -0
  126. package/dist/state-unlock.d.ts.map +1 -0
  127. package/dist/state-unlock.js +196 -0
  128. package/dist/wave-runtime.mjs +332 -332
  129. package/dist/workspace/declaration.schema.json +1 -1
  130. package/dist/workspace/release-workflow.d.ts +10 -0
  131. package/dist/workspace/release-workflow.d.ts.map +1 -1
  132. package/dist/workspace/release-workflow.js +10 -4
  133. package/package.json +1 -1
  134. package/src/__fixtures__/agent-guard/plywood.ts +27 -0
  135. package/src/__fixtures__/agent-guard/terragucci.ts +39 -0
  136. package/src/__fixtures__/gated-ledger.ts +58 -0
  137. package/src/__fixtures__/notify-messages/events.ts +23 -0
  138. package/src/__fixtures__/notify-messages/messages.json +50 -0
  139. package/src/__fixtures__/pr-loop/terragucci-plan.note.golden.md +53 -0
  140. package/src/__fixtures__/pr-loop/terragucci-plan.note.json +83 -0
  141. package/src/__fixtures__/product-words.ts +12 -0
  142. package/src/__fixtures__/review-agent/terragucci-review.json +94 -0
  143. package/src/__fixtures__/terragucci-notify.ts +170 -0
  144. package/src/action-pins.test.ts +12 -1
  145. package/src/action-pins.ts +11 -0
  146. package/src/agent-guard.test.ts +319 -0
  147. package/src/agent-guard.ts +547 -0
  148. package/src/ci-commit.test.ts +213 -2
  149. package/src/ci-commit.ts +258 -27
  150. package/src/ci-own-jobs.test.ts +108 -0
  151. package/src/ci-own-jobs.ts +252 -0
  152. package/src/ci-product-words.ts +35 -7
  153. package/src/ci-product.ts +81 -1
  154. package/src/cli/handlers/approve-cwd.test.ts +112 -0
  155. package/src/cli/handlers/approve.ts +22 -11
  156. package/src/cli/handlers/build-own-jobs.test.ts +80 -0
  157. package/src/cli/handlers/build-pr-loop-control.test.ts +87 -0
  158. package/src/cli/handlers/build-run-group.test.ts +59 -0
  159. package/src/cli/handlers/build.ts +83 -3
  160. package/src/cli/handlers/commit.ts +3 -0
  161. package/src/cli/handlers/notify-relay.ts +99 -17
  162. package/src/cli/handlers/pr.ts +3 -1
  163. package/src/cli/handlers/release.ts +20 -10
  164. package/src/cli/handlers/run-generate.ts +2 -1
  165. package/src/cli/main.ts +21 -1
  166. package/src/cli/parse-args.ts +45 -3
  167. package/src/cli/registry.ts +16 -0
  168. package/src/components/pr-env-components.test.ts +137 -0
  169. package/src/components/pr-env-components.ts +213 -0
  170. package/src/components/pr-policy.test.ts +41 -1
  171. package/src/components/pr-policy.ts +25 -1
  172. package/src/components/release-compile.test.ts +47 -0
  173. package/src/components/release-compile.ts +70 -13
  174. package/src/config.ts +23 -3
  175. package/src/gate-policy-bundle.test.ts +14 -0
  176. package/src/gate-policy.test.ts +7 -0
  177. package/src/gate-policy.ts +22 -7
  178. package/src/lexicon.ts +28 -1
  179. package/src/lifecycle/gated-op.ts +158 -0
  180. package/src/lifecycle/git.test.ts +92 -0
  181. package/src/lifecycle/git.ts +71 -2
  182. package/src/lifecycle/release-images-dockerfile.test.ts +221 -0
  183. package/src/lifecycle/release-images.ts +167 -8
  184. package/src/notify-message-words.test.ts +186 -0
  185. package/src/notify-product.ts +17 -4
  186. package/src/notify-relay-repo.test.ts +284 -0
  187. package/src/notify-relay-repo.ts +234 -0
  188. package/src/notify-relay.test.ts +12 -0
  189. package/src/notify-relay.ts +29 -3
  190. package/src/notify.schema.json +1 -0
  191. package/src/notify.ts +59 -19
  192. package/src/op/activities/review-product.test.ts +116 -0
  193. package/src/op/activities/review-product.ts +200 -0
  194. package/src/op/op-waves-capability.ts +2 -2
  195. package/src/op/schedule-overdue.test.ts +26 -0
  196. package/src/policy-engine.test.ts +162 -0
  197. package/src/policy-engine.ts +200 -0
  198. package/src/pr-forge-reads.test.ts +285 -0
  199. package/src/pr-forge-reads.ts +424 -0
  200. package/src/pr-forge.ts +30 -16
  201. package/src/pr-plan-note.test.ts +105 -0
  202. package/src/pr-plan-note.ts +521 -0
  203. package/src/release-floors.ts +13 -1
  204. package/src/state-export.test.ts +124 -0
  205. package/src/state-export.ts +227 -0
  206. package/src/state-migration.test.ts +133 -0
  207. package/src/state-migration.ts +236 -0
  208. package/src/state-unlock.test.ts +165 -0
  209. package/src/state-unlock.ts +293 -0
  210. package/src/workspace/declaration.schema.json +1 -1
  211. package/src/workspace/release-workflow.test.ts +34 -0
  212. package/src/workspace/release-workflow.ts +19 -4
@@ -19,5 +19,13 @@
19
19
  * this constant to the next patch (#4501). The check fails while a floor
20
20
  * still names this constant after its version shipped.
21
21
  */
22
- export declare const NEXT_RELEASE = "0.124.74";
22
+ export declare const NEXT_RELEASE = "0.124.78";
23
+ /** The first chant whose sql lexicon's `yodelCommitSpec("new", ...)` takes `envs`, so `--replace-own` reads those histories as the reader (#4815). */
24
+ export declare const YODEL_NEW_ENVS_FLOOR = "0.124.78";
25
+ /** The first chant whose sql lexicon's `yodelRefusalReply` reads yodel rebase's conflict refusal as yodel prints it, `<a> and the migrations after <p> up to <b>` or `<a> and the chain ending at <b>` (#4821). */
26
+ export declare const YODEL_CONFLICT_REPLY_FLOOR = "0.124.78";
27
+ /** The first chant whose `@intentius/chant/pr-forge-reads` exports the forge reads beside the clients (#4809). */
28
+ export declare const PR_FORGE_READS_FLOOR = "0.124.78";
29
+ /** The first chant whose GitLab `scheduledRuns` takes `schedule`, the selector of an Op's `gitlab.scheduleRule` (#4813). */
30
+ export declare const SCHEDULED_RUNS_SELECTOR_FLOOR = "0.124.78";
23
31
  //# sourceMappingURL=release-floors.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"release-floors.d.ts","sourceRoot":"","sources":["../src/release-floors.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,eAAO,MAAM,YAAY,aAAa,CAAC"}
1
+ {"version":3,"file":"release-floors.d.ts","sourceRoot":"","sources":["../src/release-floors.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,eAAO,MAAM,YAAY,aAAa,CAAC;AAEvC,sJAAsJ;AACtJ,eAAO,MAAM,oBAAoB,aAAe,CAAC;AAEjD,mNAAmN;AACnN,eAAO,MAAM,0BAA0B,aAAe,CAAC;AAEvD,kHAAkH;AAClH,eAAO,MAAM,oBAAoB,aAAe,CAAC;AAEjD,4HAA4H;AAC5H,eAAO,MAAM,6BAA6B,aAAe,CAAC"}
@@ -19,4 +19,12 @@
19
19
  * this constant to the next patch (#4501). The check fails while a floor
20
20
  * still names this constant after its version shipped.
21
21
  */
22
- export const NEXT_RELEASE = "0.124.74";
22
+ export const NEXT_RELEASE = "0.124.78";
23
+ /** The first chant whose sql lexicon's `yodelCommitSpec("new", ...)` takes `envs`, so `--replace-own` reads those histories as the reader (#4815). */
24
+ export const YODEL_NEW_ENVS_FLOOR = NEXT_RELEASE;
25
+ /** The first chant whose sql lexicon's `yodelRefusalReply` reads yodel rebase's conflict refusal as yodel prints it, `<a> and the migrations after <p> up to <b>` or `<a> and the chain ending at <b>` (#4821). */
26
+ export const YODEL_CONFLICT_REPLY_FLOOR = NEXT_RELEASE;
27
+ /** The first chant whose `@intentius/chant/pr-forge-reads` exports the forge reads beside the clients (#4809). */
28
+ export const PR_FORGE_READS_FLOOR = NEXT_RELEASE;
29
+ /** The first chant whose GitLab `scheduledRuns` takes `schedule`, the selector of an Op's `gitlab.scheduleRule` (#4813). */
30
+ export const SCHEDULED_RUNS_SELECTOR_FLOOR = NEXT_RELEASE;
@@ -0,0 +1,126 @@
1
+ import { type GatedRequestLedger } from "./lifecycle/gated-op.js";
2
+ import type { GatedRequestRules } from "./lifecycle/gated-request.js";
3
+ /** The first chant with the engine-neutral state export (#4762). */
4
+ export declare const STATE_EXPORT_FLOOR = "0.124.77";
5
+ /** The version a store holds now. */
6
+ export interface StateVersion {
7
+ /** Whether there is a state at all. */
8
+ exists: boolean;
9
+ /** Its id, when the store keeps versions and can give this one back later. */
10
+ versionId?: string;
11
+ /** Why the store keeps no version of it, when it keeps none. */
12
+ off?: string;
13
+ }
14
+ /** An engine's state, read by version: the adapter a product's engine supplies. */
15
+ export interface StateVersionSource {
16
+ /** Where the state is, as messages and the record name it. Never a credential. */
17
+ readonly location: string;
18
+ /** The version the store holds now. */
19
+ current(): Promise<StateVersion>;
20
+ /** Whether the store holds that version, without reading it. */
21
+ hasVersion(id: string): Promise<boolean>;
22
+ /** That version's bytes, or undefined when the store holds no such version. */
23
+ readVersion(id: string): Promise<string | Uint8Array | undefined>;
24
+ }
25
+ /** The product's words for an export. */
26
+ export interface StateExportWords {
27
+ /** The op the requests wait at, such as `tf-state-export`. */
28
+ op: string;
29
+ /** The request's kind. Default `state-export`. */
30
+ kind?: string;
31
+ /** The command that approves a request, as the product prints it. Default chant's `chant approve <op> <subject> --plan <digest>`. */
32
+ approveCommand?: (subject: string, digest: string) => string;
33
+ /** The file's name in its directory. Default `<subject>.<version>.state`, each with what is not a word character replaced. */
34
+ fileName?: (subject: string, version: string) => string;
35
+ /** The prefix of the private directory a default destination is made in. Default `chant-export-`. */
36
+ tmpPrefix?: string;
37
+ }
38
+ /** The CI variables that mark a job, where an export would become an artifact. */
39
+ export declare const CI_MARKERS: readonly string[];
40
+ /** The CI marker set in `env`, or undefined when none is. */
41
+ export declare function ciMarker(env: NodeJS.ProcessEnv | Record<string, string | undefined>): string | undefined;
42
+ /** The git working tree `path` sits in, or undefined. */
43
+ export declare function enclosingWorkTree(path: string): string | undefined;
44
+ /** Why an export may not go to `out`, or undefined when it may. */
45
+ export declare function exportRefusal(out: string): string | undefined;
46
+ /** A refusal before anything is asked: a version the store does not have, a destination the export may not use. */
47
+ export declare class StateExportError extends Error {
48
+ constructor(message: string);
49
+ }
50
+ export interface StateExportInput {
51
+ /** What is exported, and the gate the request waits at: a root, a stack. */
52
+ subject: string;
53
+ /** The requester. Their own approval never counts. */
54
+ by: string;
55
+ source: StateVersionSource;
56
+ /** The version to export. Default the one the store holds now. */
57
+ version?: string;
58
+ /** Where the file goes. Default a new private directory in the system's temp dir. */
59
+ out?: string;
60
+ words: StateExportWords;
61
+ rules: GatedRequestRules;
62
+ /** Where the ledger is. Default the process's. */
63
+ cwd?: string;
64
+ base?: string;
65
+ /** How long a request waits for its approval. Default 48h. */
66
+ expiry?: string;
67
+ now?: string;
68
+ /** The environment the CI markers are read from. Default the process's. */
69
+ env?: NodeJS.ProcessEnv | Record<string, string | undefined>;
70
+ /** Export in a CI job too. Default false: a CI job refuses. */
71
+ allowCi?: boolean;
72
+ ledger?: GatedRequestLedger;
73
+ }
74
+ /** How an export ended. `exit` is the code its command exits with. */
75
+ export type StateExportOutcome = {
76
+ status: "written";
77
+ exit: 0;
78
+ digest: string;
79
+ version: string;
80
+ location: string;
81
+ file: string;
82
+ approvedBy: string;
83
+ contentDigest: string;
84
+ recorded: boolean;
85
+ } | {
86
+ status: "waiting";
87
+ exit: 3;
88
+ digest: string;
89
+ version: string;
90
+ location: string;
91
+ command: string;
92
+ }
93
+ /** An approval stands and does not count, such as the requester's own. The request still waits. */
94
+ | {
95
+ status: "refused";
96
+ exit: 3;
97
+ digest: string;
98
+ version: string;
99
+ location: string;
100
+ command: string;
101
+ by: string;
102
+ reason: string;
103
+ }
104
+ /** The request exported already; another export asks again. */
105
+ | {
106
+ status: "spent";
107
+ exit: 1;
108
+ digest: string;
109
+ version: string;
110
+ location: string;
111
+ } | {
112
+ status: "failed";
113
+ exit: 1;
114
+ digest: string;
115
+ version: string;
116
+ location: string;
117
+ error: Error;
118
+ recorded: boolean;
119
+ };
120
+ /**
121
+ * Ask for, or with an approval write, one version of a state. Throws
122
+ * {@link StateExportError} before anything is asked when the version or the
123
+ * destination is refused.
124
+ */
125
+ export declare function exportStateVersion(input: StateExportInput): Promise<StateExportOutcome>;
126
+ //# sourceMappingURL=state-export.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"state-export.d.ts","sourceRoot":"","sources":["../src/state-export.ts"],"names":[],"mappings":"AAoCA,OAAO,EAAW,KAAK,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AACxE,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,2BAA2B,CAAC;AAEnE,oEAAoE;AACpE,eAAO,MAAM,kBAAkB,aAAa,CAAC;AAE7C,qCAAqC;AACrC,MAAM,WAAW,YAAY;IAC3B,uCAAuC;IACvC,MAAM,EAAE,OAAO,CAAC;IAChB,8EAA8E;IAC9E,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,gEAAgE;IAChE,GAAG,CAAC,EAAE,MAAM,CAAC;CACd;AAED,mFAAmF;AACnF,MAAM,WAAW,kBAAkB;IACjC,kFAAkF;IAClF,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,uCAAuC;IACvC,OAAO,IAAI,OAAO,CAAC,YAAY,CAAC,CAAC;IACjC,gEAAgE;IAChE,UAAU,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC;IACzC,+EAA+E;IAC/E,WAAW,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,UAAU,GAAG,SAAS,CAAC,CAAC;CACnE;AAED,yCAAyC;AACzC,MAAM,WAAW,gBAAgB;IAC/B,8DAA8D;IAC9D,EAAE,EAAE,MAAM,CAAC;IACX,kDAAkD;IAClD,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,qIAAqI;IACrI,cAAc,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,KAAK,MAAM,CAAC;IAC7D,8HAA8H;IAC9H,QAAQ,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,KAAK,MAAM,CAAC;IACx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AIG;AACH,wBAAsB,kBAAkB,CAAC,KAAK,EAAE,gBAAgB,GAAG,OAAO,CAAC,kBAAkB,CAAC,CA8D7F"}
@@ -0,0 +1,156 @@
1
+ /**
2
+ * A state export behind a gated request (#4762): one version of a state,
3
+ * written to the machine of the person who asks once someone else approved
4
+ * it, and recorded with who exported which version.
5
+ *
6
+ * Engine-neutral. The engine's adapter is a {@link StateVersionSource}: where
7
+ * the state is, the version it holds now, and one version's bytes. The
8
+ * terraform lexicon builds one from a root's backend
9
+ * (`@intentius/chant-lexicon-terraform/state-export`); a Pulumi stack's
10
+ * export (`pulumi stack export --version`) fits the same three calls. The
11
+ * product passes its words: the op the requests wait at, the approve
12
+ * command it prints, the name of the file.
13
+ *
14
+ * A state holds every secret its resources hold, so the export goes to the
15
+ * requester only:
16
+ *
17
+ * - never in CI, where a file can become a job artifact readable by everyone
18
+ * who can read the repository ({@link CI_MARKERS}), unless the caller
19
+ * says otherwise;
20
+ * - never inside a git working tree, where it could be committed;
21
+ * - never over a file, and mode 0600. With no `out`, a new private
22
+ * directory in the system's temp dir.
23
+ *
24
+ * The first call reads the version's metadata only, records the pending
25
+ * request (kind `state-export`, subject the root or stack, fields the
26
+ * location and the version id) and answers `waiting` with the approve
27
+ * command. Once someone other than the requester approved its digest, the
28
+ * next call spends the approval, reads the version, writes the file, and the
29
+ * finished line records the version id and the digest of the bytes written,
30
+ * never their contents. A request exports once.
31
+ */
32
+ import { createHash } from "node:crypto";
33
+ import { existsSync, mkdtempSync, realpathSync, writeFileSync } from "node:fs";
34
+ import { tmpdir } from "node:os";
35
+ import { basename, dirname, join, resolve } from "node:path";
36
+ import { CHANT_EXIT } from "./exit-codes.js";
37
+ import { gatedOp } from "./lifecycle/gated-op.js";
38
+ /** The first chant with the engine-neutral state export (#4762). */
39
+ export const STATE_EXPORT_FLOOR = "0.124.77";
40
+ /** The CI variables that mark a job, where an export would become an artifact. */
41
+ export const CI_MARKERS = ["CI", "GITHUB_ACTIONS", "GITLAB_CI", "FORGEJO_ACTIONS", "BUILDKITE", "JENKINS_URL"];
42
+ /** The CI marker set in `env`, or undefined when none is. */
43
+ export function ciMarker(env) {
44
+ return CI_MARKERS.find((k) => env[k] !== undefined && env[k] !== "" && env[k] !== "false");
45
+ }
46
+ /** The git working tree `path` sits in, or undefined. */
47
+ export function enclosingWorkTree(path) {
48
+ let dir = resolve(path);
49
+ try {
50
+ dir = realpathSync(dir);
51
+ }
52
+ catch {
53
+ // A path that does not exist is walked as given.
54
+ }
55
+ for (;;) {
56
+ if (existsSync(join(dir, ".git")))
57
+ return dir;
58
+ const up = dirname(dir);
59
+ if (up === dir)
60
+ return undefined;
61
+ dir = up;
62
+ }
63
+ }
64
+ /** Why an export may not go to `out`, or undefined when it may. */
65
+ export function exportRefusal(out) {
66
+ const abs = resolve(out);
67
+ if (!existsSync(dirname(abs)))
68
+ return `${dirname(abs)} is not a directory`;
69
+ if (existsSync(abs))
70
+ return `${abs} is there already, and an export never writes over a file; name a new one`;
71
+ const tree = enclosingWorkTree(dirname(abs));
72
+ if (tree)
73
+ return `${abs} is inside the git working tree ${tree}, where a state could be committed; name a path outside it`;
74
+ return undefined;
75
+ }
76
+ /** A refusal before anything is asked: a version the store does not have, a destination the export may not use. */
77
+ export class StateExportError extends Error {
78
+ constructor(message) {
79
+ super(message);
80
+ this.name = "StateExportError";
81
+ }
82
+ }
83
+ const sha = (body) => `sha256:${createHash("sha256").update(body).digest("hex")}`;
84
+ const defaultName = (subject, version) => `${subject.replace(/[^\w.-]+/g, "_")}.${version.replace(/[^\w.-]+/g, "_")}.state`;
85
+ /**
86
+ * Ask for, or with an approval write, one version of a state. Throws
87
+ * {@link StateExportError} before anything is asked when the version or the
88
+ * destination is refused.
89
+ */
90
+ export async function exportStateVersion(input) {
91
+ const { source, words } = input;
92
+ const env = input.env ?? process.env;
93
+ const marker = input.allowCi ? undefined : ciMarker(env);
94
+ if (marker)
95
+ throw new StateExportError(`an export goes to the requester's machine only, and ${marker} is set, so this is a CI job whose files can become artifacts; ask for it from your own machine`);
96
+ if (input.out !== undefined) {
97
+ const why = exportRefusal(input.out);
98
+ if (why)
99
+ throw new StateExportError(why);
100
+ }
101
+ const location = source.location;
102
+ let version = input.version;
103
+ if (!version) {
104
+ const now = await source.current();
105
+ if (!now.exists)
106
+ throw new StateExportError(`${location} holds no state`);
107
+ if (!now.versionId)
108
+ throw new StateExportError(`${location} keeps no versions, so there is no version id to export: ${now.off ?? "the store gave none"}`);
109
+ version = now.versionId;
110
+ }
111
+ else if (!(await source.hasVersion(version))) {
112
+ throw new StateExportError(`${location} has no version ${version}; a lifecycle rule may have expired it`);
113
+ }
114
+ const versionId = version;
115
+ const result = await gatedOp({
116
+ op: words.op,
117
+ request: { kind: words.kind ?? "state-export", subject: input.subject, by: input.by, fields: { location, version_id: versionId } },
118
+ rules: input.rules,
119
+ description: `state export: ${input.subject} version ${versionId}, for ${input.by}`,
120
+ expiry: input.expiry ?? "48h",
121
+ ...(input.base ? { base: input.base } : {}),
122
+ ...(input.cwd ? { cwd: input.cwd } : {}),
123
+ ...(input.now ? { now: input.now } : {}),
124
+ ...(input.ledger ? { ledger: input.ledger } : {}),
125
+ }, async ({ approvedBy }) => {
126
+ const name = (words.fileName ?? defaultName)(input.subject, versionId);
127
+ const file = input.out !== undefined ? resolve(input.out) : join(mkdtempSync(join(tmpdir(), words.tmpPrefix ?? "chant-export-")), basename(name));
128
+ // Checked again: the destination may have changed while the request waited.
129
+ const why = exportRefusal(file);
130
+ if (why)
131
+ throw new StateExportError(why);
132
+ const body = await source.readVersion(versionId);
133
+ if (body === undefined)
134
+ throw new StateExportError(`${location} has no version ${versionId}; it was deleted since the request`);
135
+ writeFileSync(file, body, { mode: 0o600, flag: "wx" });
136
+ const contentDigest = sha(body);
137
+ return { output: { file, contentDigest }, detail: { by: input.by, location, version_id: versionId, content_digest: contentDigest, approved_by: approvedBy } };
138
+ });
139
+ const command = (digest, fallback) => (words.approveCommand ? words.approveCommand(input.subject, digest) : fallback);
140
+ const at = { digest: result.digest, version: versionId, location };
141
+ switch (result.status) {
142
+ case "done":
143
+ return { status: "written", exit: CHANT_EXIT.done, ...at, file: result.output.file, approvedBy: result.approvedBy, contentDigest: result.output.contentDigest, recorded: result.recorded };
144
+ case "waiting":
145
+ return { status: "waiting", exit: CHANT_EXIT.waiting, ...at, command: command(result.digest, result.command) };
146
+ case "refused":
147
+ return { status: "refused", exit: CHANT_EXIT.waiting, ...at, command: command(result.digest, result.command), by: result.by, reason: result.reason };
148
+ case "spent":
149
+ return { status: "spent", exit: CHANT_EXIT.failed, ...at };
150
+ case "held":
151
+ // An export takes no last check, so nothing holds it; kept for the type.
152
+ return { status: "failed", exit: CHANT_EXIT.failed, ...at, error: new StateExportError(result.reason), recorded: false };
153
+ case "failed":
154
+ return { status: "failed", exit: CHANT_EXIT.failed, ...at, error: result.error, recorded: result.recorded };
155
+ }
156
+ }
@@ -0,0 +1,148 @@
1
+ import { type GatedRequestLedger } from "./lifecycle/gated-op.js";
2
+ import type { GatedRequestRules } from "./lifecycle/gated-request.js";
3
+ /** The first chant with the engine-neutral state migration (#4762). */
4
+ export declare const STATE_MIGRATION_FLOOR = "0.124.77";
5
+ /** A state as a migration binds it: its version id, when the store keeps versions, and its digest, null for no state. */
6
+ export interface MigrationStateAt {
7
+ version_id?: string;
8
+ digest: string | null;
9
+ }
10
+ /** What a plan of a root against a state found. */
11
+ export interface MigrationCheck {
12
+ /** Each change, as `<address>: <actions>`. None: the root plans clean. */
13
+ changes: string[];
14
+ /** The engine's own summary line. */
15
+ summary: string;
16
+ }
17
+ /** One root of a migration's plan: never its state's contents. */
18
+ export interface MigrationRootPlan {
19
+ root: string;
20
+ /** Where its state is, as the record names it. */
21
+ location?: string;
22
+ before: MigrationStateAt;
23
+ /** The new state's digest, and its version id once written. */
24
+ after: {
25
+ digest: string;
26
+ version_id?: string;
27
+ };
28
+ /** The plan against the new state, before anything is written. */
29
+ proof: MigrationCheck;
30
+ /** The plan against the real state, after the write. */
31
+ verify?: MigrationCheck;
32
+ }
33
+ /** The engine's half of a migration's write. */
34
+ export interface StateMigrationEngine {
35
+ /** Take the lock of every state the migration writes or reads. Throws when one is held, having released what it took. Answers the release. */
36
+ lock(): Promise<() => Promise<void>>;
37
+ /** A root's state now, read under the locks. */
38
+ current(root: string): Promise<MigrationStateAt>;
39
+ /** Write every new state, in the engine's order. Throws at the first that fails. */
40
+ write(): Promise<void>;
41
+ /** After the write: the root planned against its real state, its version now, and its version before when only the write named it (a snapshot). */
42
+ verify(root: string): Promise<MigrationCheck & {
43
+ version_id?: string;
44
+ before_version_id?: string;
45
+ }>;
46
+ }
47
+ /** The product's words for a migration. */
48
+ export interface StateMigrationWords {
49
+ /** The op the requests wait at, such as `tf-migrate`. */
50
+ op: string;
51
+ /** The request's kind. Default `migrate`. */
52
+ kind?: string;
53
+ approveCommand?: (subject: string, digest: string) => string;
54
+ }
55
+ export interface StateMigrationInput {
56
+ /** The migration's name: the gate its request waits at. */
57
+ name: string;
58
+ /** The requester. Their own approval never counts. */
59
+ by: string;
60
+ roots: MigrationRootPlan[];
61
+ engine: StateMigrationEngine;
62
+ words: StateMigrationWords;
63
+ /** The product's facts the request binds, such as the migration file's path and digest. */
64
+ fields?: Record<string, string>;
65
+ /** The product's facts the finished line adds, given how it ended. */
66
+ detail?: (result: StateMigrationResult) => Record<string, string | number | boolean>;
67
+ rules: GatedRequestRules;
68
+ description?: string;
69
+ cwd?: string;
70
+ base?: string;
71
+ /** How long a request waits for its approval. Default 48h. */
72
+ expiry?: string;
73
+ now?: string;
74
+ runId?: string;
75
+ commit?: string;
76
+ ledger?: GatedRequestLedger;
77
+ }
78
+ /** How the write ended, once approved. */
79
+ export interface StateMigrationResult {
80
+ status: "applied" | "moved" | "failed";
81
+ roots: MigrationRootPlan[];
82
+ /** The roots whose state is not the one planned. */
83
+ moved?: string[];
84
+ error?: string;
85
+ }
86
+ /** How a migration ended. `exit` is the code its command exits with. */
87
+ export type StateMigrationOutcome = {
88
+ status: "proof-failed";
89
+ exit: 1;
90
+ roots: MigrationRootPlan[];
91
+ failing: string[];
92
+ } | {
93
+ status: "waiting";
94
+ exit: 3;
95
+ digest: string;
96
+ command: string;
97
+ } | {
98
+ status: "refused";
99
+ exit: 3;
100
+ digest: string;
101
+ command: string;
102
+ by: string;
103
+ reason: string;
104
+ }
105
+ /** It applied already; a migration applies once. */
106
+ | {
107
+ status: "spent";
108
+ exit: 0;
109
+ digest: string;
110
+ } | {
111
+ status: "applied";
112
+ exit: 0;
113
+ digest: string;
114
+ approvedBy: string;
115
+ result: StateMigrationResult;
116
+ recorded: boolean;
117
+ }
118
+ /** Under the locks a state was not the one planned: nothing was written, and the approval is spent. */
119
+ | {
120
+ status: "moved";
121
+ exit: 4;
122
+ digest: string;
123
+ approvedBy: string;
124
+ result: StateMigrationResult;
125
+ recorded: boolean;
126
+ } | {
127
+ status: "failed";
128
+ exit: 1;
129
+ digest: string;
130
+ error: string;
131
+ result?: StateMigrationResult;
132
+ recorded: boolean;
133
+ };
134
+ /** The finished line's flat facts: how it ended, and each root's location, versions and digests. */
135
+ export declare function migrationFacts(result: StateMigrationResult): Record<string, string | number | boolean>;
136
+ /**
137
+ * Write the migration under the locks: check nothing moved, write, verify.
138
+ * The locks are released whatever happens. {@link runStateMigration} runs it
139
+ * once its request is approved; a migration whose approval an op wave's hold
140
+ * decided already (the wave's own gate) runs it directly.
141
+ */
142
+ export declare function writeStateMigration(engine: StateMigrationEngine, roots: MigrationRootPlan[]): Promise<StateMigrationResult>;
143
+ /**
144
+ * Ask for, or with an approval write, a proved migration. Refuses a plan
145
+ * whose proof shows a change before anything is asked.
146
+ */
147
+ export declare function runStateMigration(input: StateMigrationInput): Promise<StateMigrationOutcome>;
148
+ //# sourceMappingURL=state-migration.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"state-migration.d.ts","sourceRoot":"","sources":["../src/state-migration.ts"],"names":[],"mappings":"AAgCA,OAAO,EAAW,KAAK,kBAAkB,EAAE,MAAM,sBAA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,CAAA;CAAE,CAAC;AAInH,oGAAoG;AACpG,wBAAgB,cAAc,CAAC,MAAM,EAAE,oBAAoB,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,CAetG;AAED;;;;;GAKG;AACH,wBAAsB,mBAAmB,CAAC,MAAM,EAAE,oBAAoB,EAAE,KAAK,EAAE,iBAAiB,EAAE,GAAG,OAAO,CAAC,oBAAoB,CAAC,CA0BjI;AAED;;;GAGG;AACH,wBAAsB,iBAAiB,CAAC,KAAK,EAAE,mBAAmB,GAAG,OAAO,CAAC,qBAAqB,CAAC,CA6ClG"}
@@ -0,0 +1,146 @@
1
+ /**
2
+ * A state migration behind a gated request (#4762): new states for several
3
+ * roots or stacks, proved before anyone approves them, written once someone
4
+ * other than the requester approved that proof, under every state's lock,
5
+ * and checked again after the write.
6
+ *
7
+ * Engine-neutral. The engine plans the migration (each root's state before,
8
+ * its new state's digest, and a proof that the root plans no change against
9
+ * it) and supplies a {@link StateMigrationEngine} for the write: take the
10
+ * locks, read a root's state now, write the new states, and plan each root
11
+ * again against its real state. The terraform lexicon plans moves of
12
+ * resources between roots and reverts to a recorded version
13
+ * (`@intentius/chant-lexicon-terraform/state-migration`); a move of
14
+ * resources between Pulumi stacks fits the same calls. The product passes
15
+ * its words: the op, the migration's name, the approve command, and the
16
+ * facts its own history reads back.
17
+ *
18
+ * 1. A plan whose proof shows a change is refused before anything is
19
+ * asked (`proof-failed`).
20
+ * 2. The request (kind `migrate`, subject the migration's name) carries
21
+ * the plan as its attachment, so its digest binds every root's state
22
+ * before and after. A state that moves before the approval asks again.
23
+ * 3. Approved, the approval is spent, every lock is taken, and each root's
24
+ * state is read again. A state that is not the one planned refuses the
25
+ * write (`moved`), with nothing written.
26
+ * 4. The new states are written, every root plans against its real state,
27
+ * and a change left anywhere fails the migration. The locks are
28
+ * released whatever happens. The finished line holds each root's
29
+ * location and its version and digest before and after, never a
30
+ * state's contents.
31
+ */
32
+ import { CHANT_EXIT } from "./exit-codes.js";
33
+ import { gatedOp } from "./lifecycle/gated-op.js";
34
+ /** The first chant with the engine-neutral state migration (#4762). */
35
+ export const STATE_MIGRATION_FLOOR = "0.124.77";
36
+ const same = (a, b) => (a.version_id ?? undefined) === (b.version_id ?? undefined) && a.digest === b.digest;
37
+ /** The finished line's flat facts: how it ended, and each root's location, versions and digests. */
38
+ export function migrationFacts(result) {
39
+ const out = {
40
+ result: result.status === "moved" ? "refused" : result.status,
41
+ roots: result.roots.map((r) => r.root).join(","),
42
+ ...(result.moved?.length ? { moved: result.moved.join(",") } : {}),
43
+ ...(result.error ? { error: result.error.slice(0, 400) } : {}),
44
+ };
45
+ for (const r of result.roots) {
46
+ if (r.location)
47
+ out[`${r.root}.location`] = r.location;
48
+ if (r.before.version_id)
49
+ out[`${r.root}.before`] = r.before.version_id;
50
+ if (r.after.version_id)
51
+ out[`${r.root}.after`] = r.after.version_id;
52
+ if (r.before.digest)
53
+ out[`${r.root}.before_digest`] = r.before.digest;
54
+ if (r.after.digest)
55
+ out[`${r.root}.after_digest`] = r.after.digest;
56
+ }
57
+ return out;
58
+ }
59
+ /**
60
+ * Write the migration under the locks: check nothing moved, write, verify.
61
+ * The locks are released whatever happens. {@link runStateMigration} runs it
62
+ * once its request is approved; a migration whose approval an op wave's hold
63
+ * decided already (the wave's own gate) runs it directly.
64
+ */
65
+ export async function writeStateMigration(engine, roots) {
66
+ const release = await engine.lock();
67
+ try {
68
+ const moved = [];
69
+ for (const r of roots)
70
+ if (!same(await engine.current(r.root), r.before))
71
+ moved.push(r.root);
72
+ if (moved.length > 0)
73
+ return { status: "moved", roots, moved };
74
+ try {
75
+ await engine.write();
76
+ }
77
+ catch (e) {
78
+ return { status: "failed", roots, error: `the write failed: ${e.message}` };
79
+ }
80
+ const after = [];
81
+ for (const r of roots) {
82
+ const v = await engine.verify(r.root);
83
+ after.push({
84
+ ...r,
85
+ ...(v.before_version_id ? { before: { ...r.before, version_id: v.before_version_id } } : {}),
86
+ after: { ...r.after, ...(v.version_id ? { version_id: v.version_id } : {}) },
87
+ verify: { changes: v.changes, summary: v.summary },
88
+ });
89
+ }
90
+ const changed = after.filter((r) => r.verify.changes.length > 0).map((r) => r.root);
91
+ return changed.length > 0 ? { status: "failed", roots: after, error: `after the write, ${changed.join(", ")} plan changes` } : { status: "applied", roots: after };
92
+ }
93
+ finally {
94
+ await release();
95
+ }
96
+ }
97
+ /**
98
+ * Ask for, or with an approval write, a proved migration. Refuses a plan
99
+ * whose proof shows a change before anything is asked.
100
+ */
101
+ export async function runStateMigration(input) {
102
+ const failing = input.roots.filter((r) => r.proof.changes.length > 0).map((r) => r.root);
103
+ if (failing.length > 0)
104
+ return { status: "proof-failed", exit: CHANT_EXIT.failed, roots: input.roots, failing };
105
+ const roots = [...input.roots].sort((a, b) => (a.root < b.root ? -1 : a.root > b.root ? 1 : 0));
106
+ // The attachment binds each root's state before and after; the proof's wording is the engine's, not the migration.
107
+ const attachment = `${JSON.stringify({ name: input.name, roots: roots.map((r) => ({ root: r.root, location: r.location ?? null, before: { version_id: r.before.version_id ?? null, digest: r.before.digest }, after: r.after.digest })) }, null, 2)}\n`;
108
+ const result = await gatedOp({
109
+ op: input.words.op,
110
+ request: { kind: input.words.kind ?? "migrate", subject: input.name, by: input.by, fields: { ...(input.fields ?? {}), roots: roots.map((r) => r.root).join(",") } },
111
+ rules: input.rules,
112
+ description: input.description ?? `migration ${input.name}: ${roots.map((r) => r.root).join(", ")}`,
113
+ expiry: input.expiry ?? "48h",
114
+ attachment,
115
+ ...(input.base ? { base: input.base } : {}),
116
+ ...(input.cwd ? { cwd: input.cwd } : {}),
117
+ ...(input.now ? { now: input.now } : {}),
118
+ ...(input.runId ? { runId: input.runId } : {}),
119
+ ...(input.commit ? { commit: input.commit } : {}),
120
+ ...(input.ledger ? { ledger: input.ledger } : {}),
121
+ }, async ({ approvedBy }) => {
122
+ const written = await writeStateMigration(input.engine, roots);
123
+ return { output: written, detail: { by: input.by, approved_by: approvedBy, ...migrationFacts(written), ...(input.detail?.(written) ?? {}) }, failed: written.status !== "applied" };
124
+ });
125
+ const command = (fallback) => (input.words.approveCommand ? input.words.approveCommand(input.name, result.digest) : fallback);
126
+ switch (result.status) {
127
+ case "waiting":
128
+ return { status: "waiting", exit: CHANT_EXIT.waiting, digest: result.digest, command: command(result.command) };
129
+ case "refused":
130
+ return { status: "refused", exit: CHANT_EXIT.waiting, digest: result.digest, command: command(result.command), by: result.by, reason: result.reason };
131
+ case "spent":
132
+ return { status: "spent", exit: CHANT_EXIT.done, digest: result.digest };
133
+ case "held":
134
+ return { status: "failed", exit: CHANT_EXIT.failed, digest: result.digest, error: result.reason, recorded: false };
135
+ case "failed":
136
+ return { status: "failed", exit: CHANT_EXIT.failed, digest: result.digest, error: result.error.message, recorded: result.recorded };
137
+ case "done": {
138
+ const r = result.output;
139
+ if (r.status === "applied")
140
+ return { status: "applied", exit: CHANT_EXIT.done, digest: result.digest, approvedBy: result.approvedBy, result: r, recorded: result.recorded };
141
+ if (r.status === "moved")
142
+ return { status: "moved", exit: CHANT_EXIT.moved, digest: result.digest, approvedBy: result.approvedBy, result: r, recorded: result.recorded };
143
+ return { status: "failed", exit: CHANT_EXIT.failed, digest: result.digest, error: r.error ?? "the migration failed", result: r, recorded: result.recorded };
144
+ }
145
+ }
146
+ }