@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.
- package/dist/__fixtures__/agent-guard/plywood.d.ts +8 -0
- package/dist/__fixtures__/agent-guard/plywood.d.ts.map +1 -0
- package/dist/__fixtures__/agent-guard/plywood.js +19 -0
- package/dist/__fixtures__/agent-guard/terragucci.d.ts +8 -0
- package/dist/__fixtures__/agent-guard/terragucci.d.ts.map +1 -0
- package/dist/__fixtures__/agent-guard/terragucci.js +30 -0
- package/dist/__fixtures__/gated-ledger.d.ts +10 -0
- package/dist/__fixtures__/gated-ledger.d.ts.map +1 -0
- package/dist/__fixtures__/gated-ledger.js +51 -0
- package/dist/__fixtures__/notify-messages/events.d.ts +9 -0
- package/dist/__fixtures__/notify-messages/events.d.ts.map +1 -0
- package/dist/__fixtures__/notify-messages/events.js +21 -0
- package/dist/__fixtures__/product-words.d.ts +14 -0
- package/dist/__fixtures__/product-words.d.ts.map +1 -1
- package/dist/__fixtures__/product-words.js +11 -0
- package/dist/__fixtures__/terragucci-notify.d.ts +66 -0
- package/dist/__fixtures__/terragucci-notify.d.ts.map +1 -0
- package/dist/__fixtures__/terragucci-notify.js +133 -0
- package/dist/action-pins.d.ts +8 -0
- package/dist/action-pins.d.ts.map +1 -1
- package/dist/action-pins.js +9 -0
- package/dist/agent-guard.d.ts +143 -0
- package/dist/agent-guard.d.ts.map +1 -0
- package/dist/agent-guard.js +455 -0
- package/dist/ci-commit.d.ts +120 -5
- package/dist/ci-commit.d.ts.map +1 -1
- package/dist/ci-commit.js +214 -23
- package/dist/ci-own-jobs.d.ts +81 -0
- package/dist/ci-own-jobs.d.ts.map +1 -0
- package/dist/ci-own-jobs.js +233 -0
- package/dist/ci-product-words.d.ts +18 -4
- package/dist/ci-product-words.d.ts.map +1 -1
- package/dist/ci-product-words.js +26 -6
- package/dist/ci-product.d.ts +40 -1
- package/dist/ci-product.d.ts.map +1 -1
- package/dist/ci-product.js +40 -1
- package/dist/cli/handlers/approve.d.ts +5 -1
- package/dist/cli/handlers/approve.d.ts.map +1 -1
- package/dist/cli/handlers/approve.js +17 -11
- package/dist/cli/handlers/build.d.ts +1 -1
- package/dist/cli/handlers/build.d.ts.map +1 -1
- package/dist/cli/handlers/build.js +88 -2
- package/dist/cli/handlers/commit.d.ts.map +1 -1
- package/dist/cli/handlers/commit.js +3 -1
- package/dist/cli/handlers/notify-relay.d.ts +18 -1
- package/dist/cli/handlers/notify-relay.d.ts.map +1 -1
- package/dist/cli/handlers/notify-relay.js +90 -14
- package/dist/cli/handlers/pr.d.ts.map +1 -1
- package/dist/cli/handlers/pr.js +3 -1
- package/dist/cli/handlers/release.d.ts +5 -1
- package/dist/cli/handlers/release.d.ts.map +1 -1
- package/dist/cli/handlers/release.js +21 -9
- package/dist/cli/handlers/run-generate.d.ts.map +1 -1
- package/dist/cli/handlers/run-generate.js +2 -1
- package/dist/cli/main.d.ts.map +1 -1
- package/dist/cli/main.js +21 -1
- package/dist/cli/parse-args.d.ts.map +1 -1
- package/dist/cli/parse-args.js +60 -4
- package/dist/cli/registry.d.ts +16 -0
- package/dist/cli/registry.d.ts.map +1 -1
- package/dist/components/pr-env-components.d.ts +65 -0
- package/dist/components/pr-env-components.d.ts.map +1 -0
- package/dist/components/pr-env-components.js +162 -0
- package/dist/components/pr-policy.d.ts +15 -0
- package/dist/components/pr-policy.d.ts.map +1 -1
- package/dist/components/pr-policy.js +24 -1
- package/dist/components/release-compile.d.ts +15 -3
- package/dist/components/release-compile.d.ts.map +1 -1
- package/dist/components/release-compile.js +66 -15
- package/dist/config.d.ts +14 -8
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +16 -1
- package/dist/gate-policy.d.ts +14 -1
- package/dist/gate-policy.d.ts.map +1 -1
- package/dist/gate-policy.js +16 -6
- package/dist/lexicon.d.ts +25 -1
- package/dist/lexicon.d.ts.map +1 -1
- package/dist/lexicon.js +10 -0
- package/dist/lifecycle/gated-op.d.ts +95 -0
- package/dist/lifecycle/gated-op.d.ts.map +1 -0
- package/dist/lifecycle/gated-op.js +96 -0
- package/dist/lifecycle/git.d.ts.map +1 -1
- package/dist/lifecycle/git.js +83 -2
- package/dist/lifecycle/release-images.d.ts +77 -3
- package/dist/lifecycle/release-images.d.ts.map +1 -1
- package/dist/lifecycle/release-images.js +132 -6
- package/dist/notify-product.d.ts +11 -2
- package/dist/notify-product.d.ts.map +1 -1
- package/dist/notify-product.js +16 -4
- package/dist/notify-relay-repo.d.ts +131 -0
- package/dist/notify-relay-repo.d.ts.map +1 -0
- package/dist/notify-relay-repo.js +202 -0
- package/dist/notify-relay.d.ts +25 -1
- package/dist/notify-relay.d.ts.map +1 -1
- package/dist/notify-relay.js +27 -2
- package/dist/notify.d.ts +36 -4
- package/dist/notify.d.ts.map +1 -1
- package/dist/notify.js +31 -21
- package/dist/notify.schema.json +1 -0
- package/dist/op/activities/review-product.d.ts +111 -0
- package/dist/op/activities/review-product.d.ts.map +1 -0
- package/dist/op/activities/review-product.js +116 -0
- package/dist/op/op-waves-capability.js +2 -2
- package/dist/policy-engine.d.ts +95 -0
- package/dist/policy-engine.d.ts.map +1 -0
- package/dist/policy-engine.js +157 -0
- package/dist/pr-forge-reads.d.ts +124 -0
- package/dist/pr-forge-reads.d.ts.map +1 -0
- package/dist/pr-forge-reads.js +322 -0
- package/dist/pr-forge.d.ts +14 -0
- package/dist/pr-forge.d.ts.map +1 -1
- package/dist/pr-forge.js +21 -13
- package/dist/pr-plan-note.d.ts +293 -0
- package/dist/pr-plan-note.d.ts.map +1 -0
- package/dist/pr-plan-note.js +362 -0
- package/dist/release-floors.d.ts +9 -1
- package/dist/release-floors.d.ts.map +1 -1
- package/dist/release-floors.js +9 -1
- package/dist/state-export.d.ts +126 -0
- package/dist/state-export.d.ts.map +1 -0
- package/dist/state-export.js +156 -0
- package/dist/state-migration.d.ts +148 -0
- package/dist/state-migration.d.ts.map +1 -0
- package/dist/state-migration.js +146 -0
- package/dist/state-unlock.d.ts +167 -0
- package/dist/state-unlock.d.ts.map +1 -0
- package/dist/state-unlock.js +196 -0
- package/dist/wave-runtime.mjs +332 -332
- package/dist/workspace/declaration.schema.json +1 -1
- package/dist/workspace/release-workflow.d.ts +10 -0
- package/dist/workspace/release-workflow.d.ts.map +1 -1
- package/dist/workspace/release-workflow.js +10 -4
- package/package.json +1 -1
- package/src/__fixtures__/agent-guard/plywood.ts +27 -0
- package/src/__fixtures__/agent-guard/terragucci.ts +39 -0
- package/src/__fixtures__/gated-ledger.ts +58 -0
- package/src/__fixtures__/notify-messages/events.ts +23 -0
- package/src/__fixtures__/notify-messages/messages.json +50 -0
- package/src/__fixtures__/pr-loop/terragucci-plan.note.golden.md +53 -0
- package/src/__fixtures__/pr-loop/terragucci-plan.note.json +83 -0
- package/src/__fixtures__/product-words.ts +12 -0
- package/src/__fixtures__/review-agent/terragucci-review.json +94 -0
- package/src/__fixtures__/terragucci-notify.ts +170 -0
- package/src/action-pins.test.ts +12 -1
- package/src/action-pins.ts +11 -0
- package/src/agent-guard.test.ts +319 -0
- package/src/agent-guard.ts +547 -0
- package/src/ci-commit.test.ts +213 -2
- package/src/ci-commit.ts +258 -27
- package/src/ci-own-jobs.test.ts +108 -0
- package/src/ci-own-jobs.ts +252 -0
- package/src/ci-product-words.ts +35 -7
- package/src/ci-product.ts +81 -1
- package/src/cli/handlers/approve-cwd.test.ts +112 -0
- package/src/cli/handlers/approve.ts +22 -11
- package/src/cli/handlers/build-own-jobs.test.ts +80 -0
- package/src/cli/handlers/build-pr-loop-control.test.ts +87 -0
- package/src/cli/handlers/build-run-group.test.ts +59 -0
- package/src/cli/handlers/build.ts +83 -3
- package/src/cli/handlers/commit.ts +3 -0
- package/src/cli/handlers/notify-relay.ts +99 -17
- package/src/cli/handlers/pr.ts +3 -1
- package/src/cli/handlers/release.ts +20 -10
- package/src/cli/handlers/run-generate.ts +2 -1
- package/src/cli/main.ts +21 -1
- package/src/cli/parse-args.ts +45 -3
- package/src/cli/registry.ts +16 -0
- package/src/components/pr-env-components.test.ts +137 -0
- package/src/components/pr-env-components.ts +213 -0
- package/src/components/pr-policy.test.ts +41 -1
- package/src/components/pr-policy.ts +25 -1
- package/src/components/release-compile.test.ts +47 -0
- package/src/components/release-compile.ts +70 -13
- package/src/config.ts +23 -3
- package/src/gate-policy-bundle.test.ts +14 -0
- package/src/gate-policy.test.ts +7 -0
- package/src/gate-policy.ts +22 -7
- package/src/lexicon.ts +28 -1
- package/src/lifecycle/gated-op.ts +158 -0
- package/src/lifecycle/git.test.ts +92 -0
- package/src/lifecycle/git.ts +71 -2
- package/src/lifecycle/release-images-dockerfile.test.ts +221 -0
- package/src/lifecycle/release-images.ts +167 -8
- package/src/notify-message-words.test.ts +186 -0
- package/src/notify-product.ts +17 -4
- package/src/notify-relay-repo.test.ts +284 -0
- package/src/notify-relay-repo.ts +234 -0
- package/src/notify-relay.test.ts +12 -0
- package/src/notify-relay.ts +29 -3
- package/src/notify.schema.json +1 -0
- package/src/notify.ts +59 -19
- package/src/op/activities/review-product.test.ts +116 -0
- package/src/op/activities/review-product.ts +200 -0
- package/src/op/op-waves-capability.ts +2 -2
- package/src/op/schedule-overdue.test.ts +26 -0
- package/src/policy-engine.test.ts +162 -0
- package/src/policy-engine.ts +200 -0
- package/src/pr-forge-reads.test.ts +285 -0
- package/src/pr-forge-reads.ts +424 -0
- package/src/pr-forge.ts +30 -16
- package/src/pr-plan-note.test.ts +105 -0
- package/src/pr-plan-note.ts +521 -0
- package/src/release-floors.ts +13 -1
- package/src/state-export.test.ts +124 -0
- package/src/state-export.ts +227 -0
- package/src/state-migration.test.ts +133 -0
- package/src/state-migration.ts +236 -0
- package/src/state-unlock.test.ts +165 -0
- package/src/state-unlock.ts +293 -0
- package/src/workspace/declaration.schema.json +1 -1
- package/src/workspace/release-workflow.test.ts +34 -0
- package/src/workspace/release-workflow.ts +19 -4
|
@@ -0,0 +1,227 @@
|
|
|
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";
|
|
37
|
+
import { gatedOp, type GatedRequestLedger } from "./lifecycle/gated-op";
|
|
38
|
+
import type { GatedRequestRules } from "./lifecycle/gated-request";
|
|
39
|
+
|
|
40
|
+
/** The first chant with the engine-neutral state export (#4762). */
|
|
41
|
+
export const STATE_EXPORT_FLOOR = "0.124.77";
|
|
42
|
+
|
|
43
|
+
/** The version a store holds now. */
|
|
44
|
+
export interface StateVersion {
|
|
45
|
+
/** Whether there is a state at all. */
|
|
46
|
+
exists: boolean;
|
|
47
|
+
/** Its id, when the store keeps versions and can give this one back later. */
|
|
48
|
+
versionId?: string;
|
|
49
|
+
/** Why the store keeps no version of it, when it keeps none. */
|
|
50
|
+
off?: string;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** An engine's state, read by version: the adapter a product's engine supplies. */
|
|
54
|
+
export interface StateVersionSource {
|
|
55
|
+
/** Where the state is, as messages and the record name it. Never a credential. */
|
|
56
|
+
readonly location: string;
|
|
57
|
+
/** The version the store holds now. */
|
|
58
|
+
current(): Promise<StateVersion>;
|
|
59
|
+
/** Whether the store holds that version, without reading it. */
|
|
60
|
+
hasVersion(id: string): Promise<boolean>;
|
|
61
|
+
/** That version's bytes, or undefined when the store holds no such version. */
|
|
62
|
+
readVersion(id: string): Promise<string | Uint8Array | undefined>;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** The product's words for an export. */
|
|
66
|
+
export interface StateExportWords {
|
|
67
|
+
/** The op the requests wait at, such as `tf-state-export`. */
|
|
68
|
+
op: string;
|
|
69
|
+
/** The request's kind. Default `state-export`. */
|
|
70
|
+
kind?: string;
|
|
71
|
+
/** The command that approves a request, as the product prints it. Default chant's `chant approve <op> <subject> --plan <digest>`. */
|
|
72
|
+
approveCommand?: (subject: string, digest: string) => string;
|
|
73
|
+
/** The file's name in its directory. Default `<subject>.<version>.state`, each with what is not a word character replaced. */
|
|
74
|
+
fileName?: (subject: string, version: string) => string;
|
|
75
|
+
/** The prefix of the private directory a default destination is made in. Default `chant-export-`. */
|
|
76
|
+
tmpPrefix?: string;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** The CI variables that mark a job, where an export would become an artifact. */
|
|
80
|
+
export const CI_MARKERS: readonly string[] = ["CI", "GITHUB_ACTIONS", "GITLAB_CI", "FORGEJO_ACTIONS", "BUILDKITE", "JENKINS_URL"];
|
|
81
|
+
|
|
82
|
+
/** The CI marker set in `env`, or undefined when none is. */
|
|
83
|
+
export function ciMarker(env: NodeJS.ProcessEnv | Record<string, string | undefined>): string | undefined {
|
|
84
|
+
return CI_MARKERS.find((k) => env[k] !== undefined && env[k] !== "" && env[k] !== "false");
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** The git working tree `path` sits in, or undefined. */
|
|
88
|
+
export function enclosingWorkTree(path: string): string | undefined {
|
|
89
|
+
let dir = resolve(path);
|
|
90
|
+
try {
|
|
91
|
+
dir = realpathSync(dir);
|
|
92
|
+
} catch {
|
|
93
|
+
// A path that does not exist is walked as given.
|
|
94
|
+
}
|
|
95
|
+
for (;;) {
|
|
96
|
+
if (existsSync(join(dir, ".git"))) return dir;
|
|
97
|
+
const up = dirname(dir);
|
|
98
|
+
if (up === dir) return undefined;
|
|
99
|
+
dir = up;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** Why an export may not go to `out`, or undefined when it may. */
|
|
104
|
+
export function exportRefusal(out: string): string | undefined {
|
|
105
|
+
const abs = resolve(out);
|
|
106
|
+
if (!existsSync(dirname(abs))) return `${dirname(abs)} is not a directory`;
|
|
107
|
+
if (existsSync(abs)) return `${abs} is there already, and an export never writes over a file; name a new one`;
|
|
108
|
+
const tree = enclosingWorkTree(dirname(abs));
|
|
109
|
+
if (tree) return `${abs} is inside the git working tree ${tree}, where a state could be committed; name a path outside it`;
|
|
110
|
+
return undefined;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** A refusal before anything is asked: a version the store does not have, a destination the export may not use. */
|
|
114
|
+
export class StateExportError extends Error {
|
|
115
|
+
constructor(message: string) {
|
|
116
|
+
super(message);
|
|
117
|
+
this.name = "StateExportError";
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export interface StateExportInput {
|
|
122
|
+
/** What is exported, and the gate the request waits at: a root, a stack. */
|
|
123
|
+
subject: string;
|
|
124
|
+
/** The requester. Their own approval never counts. */
|
|
125
|
+
by: string;
|
|
126
|
+
source: StateVersionSource;
|
|
127
|
+
/** The version to export. Default the one the store holds now. */
|
|
128
|
+
version?: string;
|
|
129
|
+
/** Where the file goes. Default a new private directory in the system's temp dir. */
|
|
130
|
+
out?: string;
|
|
131
|
+
words: StateExportWords;
|
|
132
|
+
rules: GatedRequestRules;
|
|
133
|
+
/** Where the ledger is. Default the process's. */
|
|
134
|
+
cwd?: string;
|
|
135
|
+
base?: string;
|
|
136
|
+
/** How long a request waits for its approval. Default 48h. */
|
|
137
|
+
expiry?: string;
|
|
138
|
+
now?: string;
|
|
139
|
+
/** The environment the CI markers are read from. Default the process's. */
|
|
140
|
+
env?: NodeJS.ProcessEnv | Record<string, string | undefined>;
|
|
141
|
+
/** Export in a CI job too. Default false: a CI job refuses. */
|
|
142
|
+
allowCi?: boolean;
|
|
143
|
+
ledger?: GatedRequestLedger;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** How an export ended. `exit` is the code its command exits with. */
|
|
147
|
+
export type StateExportOutcome =
|
|
148
|
+
| { status: "written"; exit: 0; digest: string; version: string; location: string; file: string; approvedBy: string; contentDigest: string; recorded: boolean }
|
|
149
|
+
| { status: "waiting"; exit: 3; digest: string; version: string; location: string; command: string }
|
|
150
|
+
/** An approval stands and does not count, such as the requester's own. The request still waits. */
|
|
151
|
+
| { status: "refused"; exit: 3; digest: string; version: string; location: string; command: string; by: string; reason: string }
|
|
152
|
+
/** The request exported already; another export asks again. */
|
|
153
|
+
| { status: "spent"; exit: 1; digest: string; version: string; location: string }
|
|
154
|
+
| { status: "failed"; exit: 1; digest: string; version: string; location: string; error: Error; recorded: boolean };
|
|
155
|
+
|
|
156
|
+
const sha = (body: string | Uint8Array): string => `sha256:${createHash("sha256").update(body).digest("hex")}`;
|
|
157
|
+
|
|
158
|
+
const defaultName = (subject: string, version: string): string => `${subject.replace(/[^\w.-]+/g, "_")}.${version.replace(/[^\w.-]+/g, "_")}.state`;
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Ask for, or with an approval write, one version of a state. Throws
|
|
162
|
+
* {@link StateExportError} before anything is asked when the version or the
|
|
163
|
+
* destination is refused.
|
|
164
|
+
*/
|
|
165
|
+
export async function exportStateVersion(input: StateExportInput): Promise<StateExportOutcome> {
|
|
166
|
+
const { source, words } = input;
|
|
167
|
+
const env = input.env ?? process.env;
|
|
168
|
+
const marker = input.allowCi ? undefined : ciMarker(env);
|
|
169
|
+
if (marker) 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`);
|
|
170
|
+
if (input.out !== undefined) {
|
|
171
|
+
const why = exportRefusal(input.out);
|
|
172
|
+
if (why) throw new StateExportError(why);
|
|
173
|
+
}
|
|
174
|
+
const location = source.location;
|
|
175
|
+
let version = input.version;
|
|
176
|
+
if (!version) {
|
|
177
|
+
const now = await source.current();
|
|
178
|
+
if (!now.exists) throw new StateExportError(`${location} holds no state`);
|
|
179
|
+
if (!now.versionId) throw new StateExportError(`${location} keeps no versions, so there is no version id to export: ${now.off ?? "the store gave none"}`);
|
|
180
|
+
version = now.versionId;
|
|
181
|
+
} else if (!(await source.hasVersion(version))) {
|
|
182
|
+
throw new StateExportError(`${location} has no version ${version}; a lifecycle rule may have expired it`);
|
|
183
|
+
}
|
|
184
|
+
const versionId = version;
|
|
185
|
+
const result = await gatedOp(
|
|
186
|
+
{
|
|
187
|
+
op: words.op,
|
|
188
|
+
request: { kind: words.kind ?? "state-export", subject: input.subject, by: input.by, fields: { location, version_id: versionId } },
|
|
189
|
+
rules: input.rules,
|
|
190
|
+
description: `state export: ${input.subject} version ${versionId}, for ${input.by}`,
|
|
191
|
+
expiry: input.expiry ?? "48h",
|
|
192
|
+
...(input.base ? { base: input.base } : {}),
|
|
193
|
+
...(input.cwd ? { cwd: input.cwd } : {}),
|
|
194
|
+
...(input.now ? { now: input.now } : {}),
|
|
195
|
+
...(input.ledger ? { ledger: input.ledger } : {}),
|
|
196
|
+
},
|
|
197
|
+
async ({ approvedBy }) => {
|
|
198
|
+
const name = (words.fileName ?? defaultName)(input.subject, versionId);
|
|
199
|
+
const file = input.out !== undefined ? resolve(input.out) : join(mkdtempSync(join(tmpdir(), words.tmpPrefix ?? "chant-export-")), basename(name));
|
|
200
|
+
// Checked again: the destination may have changed while the request waited.
|
|
201
|
+
const why = exportRefusal(file);
|
|
202
|
+
if (why) throw new StateExportError(why);
|
|
203
|
+
const body = await source.readVersion(versionId);
|
|
204
|
+
if (body === undefined) throw new StateExportError(`${location} has no version ${versionId}; it was deleted since the request`);
|
|
205
|
+
writeFileSync(file, body, { mode: 0o600, flag: "wx" });
|
|
206
|
+
const contentDigest = sha(body);
|
|
207
|
+
return { output: { file, contentDigest }, detail: { by: input.by, location, version_id: versionId, content_digest: contentDigest, approved_by: approvedBy } };
|
|
208
|
+
},
|
|
209
|
+
);
|
|
210
|
+
const command = (digest: string, fallback: string): string => (words.approveCommand ? words.approveCommand(input.subject, digest) : fallback);
|
|
211
|
+
const at = { digest: result.digest, version: versionId, location };
|
|
212
|
+
switch (result.status) {
|
|
213
|
+
case "done":
|
|
214
|
+
return { status: "written", exit: CHANT_EXIT.done, ...at, file: result.output.file, approvedBy: result.approvedBy, contentDigest: result.output.contentDigest, recorded: result.recorded };
|
|
215
|
+
case "waiting":
|
|
216
|
+
return { status: "waiting", exit: CHANT_EXIT.waiting, ...at, command: command(result.digest, result.command) };
|
|
217
|
+
case "refused":
|
|
218
|
+
return { status: "refused", exit: CHANT_EXIT.waiting, ...at, command: command(result.digest, result.command), by: result.by, reason: result.reason };
|
|
219
|
+
case "spent":
|
|
220
|
+
return { status: "spent", exit: CHANT_EXIT.failed, ...at };
|
|
221
|
+
case "held":
|
|
222
|
+
// An export takes no last check, so nothing holds it; kept for the type.
|
|
223
|
+
return { status: "failed", exit: CHANT_EXIT.failed, ...at, error: new StateExportError(result.reason), recorded: false };
|
|
224
|
+
case "failed":
|
|
225
|
+
return { status: "failed", exit: CHANT_EXIT.failed, ...at, error: result.error, recorded: result.recorded };
|
|
226
|
+
}
|
|
227
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* #4762: the engine-neutral state migration, on an in-memory engine and an
|
|
3
|
+
* in-memory gated-request ledger. Nothing reaches a store or git.
|
|
4
|
+
*/
|
|
5
|
+
import { describe, expect, test } from "vitest";
|
|
6
|
+
import { fakeLedger } from "./__fixtures__/gated-ledger";
|
|
7
|
+
import { runStateMigration, STATE_MIGRATION_FLOOR, writeStateMigration, type MigrationRootPlan, type MigrationStateAt, type StateMigrationEngine } from "./state-migration";
|
|
8
|
+
|
|
9
|
+
const plan = (proof: string[] = []): MigrationRootPlan[] => [
|
|
10
|
+
{ root: "queues", location: "s3://acme/queues.tfstate", before: { version_id: "q1", digest: "sha256:q-before" }, after: { digest: "sha256:q-after" }, proof: { changes: [], summary: "No changes." } },
|
|
11
|
+
{ root: "platform", location: "s3://acme/platform.tfstate", before: { version_id: "p1", digest: "sha256:p-before" }, after: { digest: "sha256:p-after" }, proof: { changes: proof, summary: proof.length ? "Plan: 1 to add" : "No changes." } },
|
|
12
|
+
];
|
|
13
|
+
|
|
14
|
+
/** An engine over states in memory: `now` is what `current` reads, `write` moves every root to its new version. */
|
|
15
|
+
function engine(opts: { moved?: string; failWrite?: boolean; leftChange?: string } = {}): StateMigrationEngine & { calls: string[]; locked: boolean } {
|
|
16
|
+
const calls: string[] = [];
|
|
17
|
+
const now = new Map<string, MigrationStateAt>([
|
|
18
|
+
["queues", { version_id: "q1", digest: "sha256:q-before" }],
|
|
19
|
+
["platform", { version_id: opts.moved === "platform" ? "p2" : "p1", digest: "sha256:p-before" }],
|
|
20
|
+
]);
|
|
21
|
+
const e = {
|
|
22
|
+
calls,
|
|
23
|
+
locked: false,
|
|
24
|
+
async lock() {
|
|
25
|
+
calls.push("lock");
|
|
26
|
+
e.locked = true;
|
|
27
|
+
return async () => {
|
|
28
|
+
calls.push("release");
|
|
29
|
+
e.locked = false;
|
|
30
|
+
};
|
|
31
|
+
},
|
|
32
|
+
async current(root: string) {
|
|
33
|
+
calls.push(`current ${root}`);
|
|
34
|
+
return now.get(root)!;
|
|
35
|
+
},
|
|
36
|
+
async write() {
|
|
37
|
+
calls.push("write");
|
|
38
|
+
if (opts.failWrite) throw new Error("state push in queues failed");
|
|
39
|
+
},
|
|
40
|
+
async verify(root: string) {
|
|
41
|
+
calls.push(`verify ${root}`);
|
|
42
|
+
return { changes: root === opts.leftChange ? ["aws_sqs_queue.jobs: create"] : [], summary: "No changes.", version_id: `${root[0]}2` };
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
return e;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const base = (ledger: ReturnType<typeof fakeLedger>, e: StateMigrationEngine, roots = plan()) => ({
|
|
49
|
+
name: "move-queues",
|
|
50
|
+
by: "ana",
|
|
51
|
+
roots,
|
|
52
|
+
engine: e,
|
|
53
|
+
words: { op: "tf-migrate", approveCommand: (s: string, d: string) => `product approve ${s} --plan ${d}` },
|
|
54
|
+
fields: { file: "migrations/move-queues.yml", file_digest: "sha256:file" },
|
|
55
|
+
rules: { mode: "ledger" as const },
|
|
56
|
+
ledger,
|
|
57
|
+
now: "2026-11-08T09:00:00Z",
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
describe("runStateMigration", () => {
|
|
61
|
+
test("a proof that shows a change is refused before anything is asked", async () => {
|
|
62
|
+
const ledger = fakeLedger();
|
|
63
|
+
const e = engine();
|
|
64
|
+
const r = await runStateMigration(base(ledger, e, plan(["aws_sqs_queue.jobs: create"])));
|
|
65
|
+
expect(r).toMatchObject({ status: "proof-failed", exit: 1, failing: ["platform"] });
|
|
66
|
+
expect(ledger.writes).toEqual([]);
|
|
67
|
+
expect(e.calls).toEqual([]);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
test("waits for an approval of the proof, then writes under the locks, checks the states, and records each root's versions", async () => {
|
|
71
|
+
const ledger = fakeLedger();
|
|
72
|
+
const e = engine();
|
|
73
|
+
const first = await runStateMigration(base(ledger, e));
|
|
74
|
+
expect(first).toMatchObject({ status: "waiting", exit: 3 });
|
|
75
|
+
expect(first.status === "waiting" && first.command).toBe(`product approve move-queues --plan ${first.status === "waiting" ? first.digest : ""}`);
|
|
76
|
+
expect(e.calls).toEqual([]);
|
|
77
|
+
ledger.approve("bo");
|
|
78
|
+
const second = await runStateMigration(base(ledger, e));
|
|
79
|
+
expect(second).toMatchObject({ status: "applied", exit: 0, approvedBy: "bo", digest: first.status === "waiting" ? first.digest : "" });
|
|
80
|
+
expect(e.calls).toEqual(["lock", "current platform", "current queues", "write", "verify platform", "verify queues", "release"]);
|
|
81
|
+
expect(ledger.writes).toEqual(["pending", "applied", "finished:applied"]);
|
|
82
|
+
expect(ledger.details[0]).toMatchObject({ result: "applied", roots: "platform,queues", "platform.before": "p1", "platform.after": "p2", "queues.location": "s3://acme/queues.tfstate", approved_by: "bo", by: "ana" });
|
|
83
|
+
expect(await runStateMigration(base(ledger, e))).toMatchObject({ status: "spent", exit: 0 });
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
test("a state that is not the one planned refuses the write under the lock", async () => {
|
|
87
|
+
const ledger = fakeLedger();
|
|
88
|
+
ledger.approve("bo");
|
|
89
|
+
const e = engine({ moved: "platform" });
|
|
90
|
+
const r = await runStateMigration(base(ledger, e));
|
|
91
|
+
expect(r).toMatchObject({ status: "moved", exit: 4 });
|
|
92
|
+
expect(r.status === "moved" && r.result.moved).toEqual(["platform"]);
|
|
93
|
+
expect(e.calls).not.toContain("write");
|
|
94
|
+
expect(e.locked).toBe(false);
|
|
95
|
+
expect(ledger.writes.at(-1)).toBe("finished:failed");
|
|
96
|
+
expect(ledger.details[0]).toMatchObject({ result: "refused", moved: "platform" });
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
test("a write that fails, or a change left after it, fails the migration and releases the locks", async () => {
|
|
100
|
+
const ledger = fakeLedger();
|
|
101
|
+
ledger.approve("bo");
|
|
102
|
+
const broken = engine({ failWrite: true });
|
|
103
|
+
const r = await runStateMigration(base(ledger, broken));
|
|
104
|
+
expect(r).toMatchObject({ status: "failed", exit: 1 });
|
|
105
|
+
expect(r.status === "failed" && r.error).toMatch(/the write failed: state push in queues failed/);
|
|
106
|
+
expect(broken.calls.at(-1)).toBe("release");
|
|
107
|
+
|
|
108
|
+
const other = fakeLedger();
|
|
109
|
+
other.approve("bo");
|
|
110
|
+
const left = engine({ leftChange: "queues" });
|
|
111
|
+
const l = await runStateMigration(base(other, left));
|
|
112
|
+
expect(l).toMatchObject({ status: "failed", exit: 1, error: "after the write, queues plan changes" });
|
|
113
|
+
expect(other.writes.at(-1)).toBe("finished:failed");
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
test("an approval by the requester does not count", async () => {
|
|
117
|
+
const ledger = fakeLedger();
|
|
118
|
+
ledger.approve("ana");
|
|
119
|
+
expect(await runStateMigration(base(ledger, engine()))).toMatchObject({ status: "refused", exit: 3 });
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
test("a write a wave's hold approved runs without a request of its own", async () => {
|
|
123
|
+
const e = engine();
|
|
124
|
+
const r = await writeStateMigration(e, plan());
|
|
125
|
+
expect(r.status).toBe("applied");
|
|
126
|
+
expect(e.calls[0]).toBe("lock");
|
|
127
|
+
expect(e.calls.at(-1)).toBe("release");
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
test("its floor is the next release", () => {
|
|
131
|
+
expect(STATE_MIGRATION_FLOOR).toBe("0.124.77");
|
|
132
|
+
});
|
|
133
|
+
});
|
|
@@ -0,0 +1,236 @@
|
|
|
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";
|
|
33
|
+
import { gatedOp, type GatedRequestLedger } from "./lifecycle/gated-op";
|
|
34
|
+
import type { GatedRequestRules } from "./lifecycle/gated-request";
|
|
35
|
+
|
|
36
|
+
/** The first chant with the engine-neutral state migration (#4762). */
|
|
37
|
+
export const STATE_MIGRATION_FLOOR = "0.124.77";
|
|
38
|
+
|
|
39
|
+
/** A state as a migration binds it: its version id, when the store keeps versions, and its digest, null for no state. */
|
|
40
|
+
export interface MigrationStateAt {
|
|
41
|
+
version_id?: string;
|
|
42
|
+
digest: string | null;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** What a plan of a root against a state found. */
|
|
46
|
+
export interface MigrationCheck {
|
|
47
|
+
/** Each change, as `<address>: <actions>`. None: the root plans clean. */
|
|
48
|
+
changes: string[];
|
|
49
|
+
/** The engine's own summary line. */
|
|
50
|
+
summary: string;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** One root of a migration's plan: never its state's contents. */
|
|
54
|
+
export interface MigrationRootPlan {
|
|
55
|
+
root: string;
|
|
56
|
+
/** Where its state is, as the record names it. */
|
|
57
|
+
location?: string;
|
|
58
|
+
before: MigrationStateAt;
|
|
59
|
+
/** The new state's digest, and its version id once written. */
|
|
60
|
+
after: { digest: string; version_id?: string };
|
|
61
|
+
/** The plan against the new state, before anything is written. */
|
|
62
|
+
proof: MigrationCheck;
|
|
63
|
+
/** The plan against the real state, after the write. */
|
|
64
|
+
verify?: MigrationCheck;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** The engine's half of a migration's write. */
|
|
68
|
+
export interface StateMigrationEngine {
|
|
69
|
+
/** Take the lock of every state the migration writes or reads. Throws when one is held, having released what it took. Answers the release. */
|
|
70
|
+
lock(): Promise<() => Promise<void>>;
|
|
71
|
+
/** A root's state now, read under the locks. */
|
|
72
|
+
current(root: string): Promise<MigrationStateAt>;
|
|
73
|
+
/** Write every new state, in the engine's order. Throws at the first that fails. */
|
|
74
|
+
write(): Promise<void>;
|
|
75
|
+
/** 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). */
|
|
76
|
+
verify(root: string): Promise<MigrationCheck & { version_id?: string; before_version_id?: string }>;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** The product's words for a migration. */
|
|
80
|
+
export interface StateMigrationWords {
|
|
81
|
+
/** The op the requests wait at, such as `tf-migrate`. */
|
|
82
|
+
op: string;
|
|
83
|
+
/** The request's kind. Default `migrate`. */
|
|
84
|
+
kind?: string;
|
|
85
|
+
approveCommand?: (subject: string, digest: string) => string;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export interface StateMigrationInput {
|
|
89
|
+
/** The migration's name: the gate its request waits at. */
|
|
90
|
+
name: string;
|
|
91
|
+
/** The requester. Their own approval never counts. */
|
|
92
|
+
by: string;
|
|
93
|
+
roots: MigrationRootPlan[];
|
|
94
|
+
engine: StateMigrationEngine;
|
|
95
|
+
words: StateMigrationWords;
|
|
96
|
+
/** The product's facts the request binds, such as the migration file's path and digest. */
|
|
97
|
+
fields?: Record<string, string>;
|
|
98
|
+
/** The product's facts the finished line adds, given how it ended. */
|
|
99
|
+
detail?: (result: StateMigrationResult) => Record<string, string | number | boolean>;
|
|
100
|
+
rules: GatedRequestRules;
|
|
101
|
+
description?: string;
|
|
102
|
+
cwd?: string;
|
|
103
|
+
base?: string;
|
|
104
|
+
/** How long a request waits for its approval. Default 48h. */
|
|
105
|
+
expiry?: string;
|
|
106
|
+
now?: string;
|
|
107
|
+
runId?: string;
|
|
108
|
+
commit?: string;
|
|
109
|
+
ledger?: GatedRequestLedger;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** How the write ended, once approved. */
|
|
113
|
+
export interface StateMigrationResult {
|
|
114
|
+
status: "applied" | "moved" | "failed";
|
|
115
|
+
roots: MigrationRootPlan[];
|
|
116
|
+
/** The roots whose state is not the one planned. */
|
|
117
|
+
moved?: string[];
|
|
118
|
+
error?: string;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** How a migration ended. `exit` is the code its command exits with. */
|
|
122
|
+
export type StateMigrationOutcome =
|
|
123
|
+
| { status: "proof-failed"; exit: 1; roots: MigrationRootPlan[]; failing: string[] }
|
|
124
|
+
| { status: "waiting"; exit: 3; digest: string; command: string }
|
|
125
|
+
| { status: "refused"; exit: 3; digest: string; command: string; by: string; reason: string }
|
|
126
|
+
/** It applied already; a migration applies once. */
|
|
127
|
+
| { status: "spent"; exit: 0; digest: string }
|
|
128
|
+
| { status: "applied"; exit: 0; digest: string; approvedBy: string; result: StateMigrationResult; recorded: boolean }
|
|
129
|
+
/** Under the locks a state was not the one planned: nothing was written, and the approval is spent. */
|
|
130
|
+
| { status: "moved"; exit: 4; digest: string; approvedBy: string; result: StateMigrationResult; recorded: boolean }
|
|
131
|
+
| { status: "failed"; exit: 1; digest: string; error: string; result?: StateMigrationResult; recorded: boolean };
|
|
132
|
+
|
|
133
|
+
const same = (a: MigrationStateAt, b: MigrationStateAt): boolean => (a.version_id ?? undefined) === (b.version_id ?? undefined) && a.digest === b.digest;
|
|
134
|
+
|
|
135
|
+
/** The finished line's flat facts: how it ended, and each root's location, versions and digests. */
|
|
136
|
+
export function migrationFacts(result: StateMigrationResult): Record<string, string | number | boolean> {
|
|
137
|
+
const out: Record<string, string | number | boolean> = {
|
|
138
|
+
result: result.status === "moved" ? "refused" : result.status,
|
|
139
|
+
roots: result.roots.map((r) => r.root).join(","),
|
|
140
|
+
...(result.moved?.length ? { moved: result.moved.join(",") } : {}),
|
|
141
|
+
...(result.error ? { error: result.error.slice(0, 400) } : {}),
|
|
142
|
+
};
|
|
143
|
+
for (const r of result.roots) {
|
|
144
|
+
if (r.location) out[`${r.root}.location`] = r.location;
|
|
145
|
+
if (r.before.version_id) out[`${r.root}.before`] = r.before.version_id;
|
|
146
|
+
if (r.after.version_id) out[`${r.root}.after`] = r.after.version_id;
|
|
147
|
+
if (r.before.digest) out[`${r.root}.before_digest`] = r.before.digest;
|
|
148
|
+
if (r.after.digest) out[`${r.root}.after_digest`] = r.after.digest;
|
|
149
|
+
}
|
|
150
|
+
return out;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Write the migration under the locks: check nothing moved, write, verify.
|
|
155
|
+
* The locks are released whatever happens. {@link runStateMigration} runs it
|
|
156
|
+
* once its request is approved; a migration whose approval an op wave's hold
|
|
157
|
+
* decided already (the wave's own gate) runs it directly.
|
|
158
|
+
*/
|
|
159
|
+
export async function writeStateMigration(engine: StateMigrationEngine, roots: MigrationRootPlan[]): Promise<StateMigrationResult> {
|
|
160
|
+
const release = await engine.lock();
|
|
161
|
+
try {
|
|
162
|
+
const moved: string[] = [];
|
|
163
|
+
for (const r of roots) if (!same(await engine.current(r.root), r.before)) moved.push(r.root);
|
|
164
|
+
if (moved.length > 0) return { status: "moved", roots, moved };
|
|
165
|
+
try {
|
|
166
|
+
await engine.write();
|
|
167
|
+
} catch (e) {
|
|
168
|
+
return { status: "failed", roots, error: `the write failed: ${(e as Error).message}` };
|
|
169
|
+
}
|
|
170
|
+
const after: MigrationRootPlan[] = [];
|
|
171
|
+
for (const r of roots) {
|
|
172
|
+
const v = await engine.verify(r.root);
|
|
173
|
+
after.push({
|
|
174
|
+
...r,
|
|
175
|
+
...(v.before_version_id ? { before: { ...r.before, version_id: v.before_version_id } } : {}),
|
|
176
|
+
after: { ...r.after, ...(v.version_id ? { version_id: v.version_id } : {}) },
|
|
177
|
+
verify: { changes: v.changes, summary: v.summary },
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
const changed = after.filter((r) => r.verify!.changes.length > 0).map((r) => r.root);
|
|
181
|
+
return changed.length > 0 ? { status: "failed", roots: after, error: `after the write, ${changed.join(", ")} plan changes` } : { status: "applied", roots: after };
|
|
182
|
+
} finally {
|
|
183
|
+
await release();
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Ask for, or with an approval write, a proved migration. Refuses a plan
|
|
189
|
+
* whose proof shows a change before anything is asked.
|
|
190
|
+
*/
|
|
191
|
+
export async function runStateMigration(input: StateMigrationInput): Promise<StateMigrationOutcome> {
|
|
192
|
+
const failing = input.roots.filter((r) => r.proof.changes.length > 0).map((r) => r.root);
|
|
193
|
+
if (failing.length > 0) return { status: "proof-failed", exit: CHANT_EXIT.failed, roots: input.roots, failing };
|
|
194
|
+
const roots = [...input.roots].sort((a, b) => (a.root < b.root ? -1 : a.root > b.root ? 1 : 0));
|
|
195
|
+
// The attachment binds each root's state before and after; the proof's wording is the engine's, not the migration.
|
|
196
|
+
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`;
|
|
197
|
+
const result = await gatedOp(
|
|
198
|
+
{
|
|
199
|
+
op: input.words.op,
|
|
200
|
+
request: { kind: input.words.kind ?? "migrate", subject: input.name, by: input.by, fields: { ...(input.fields ?? {}), roots: roots.map((r) => r.root).join(",") } },
|
|
201
|
+
rules: input.rules,
|
|
202
|
+
description: input.description ?? `migration ${input.name}: ${roots.map((r) => r.root).join(", ")}`,
|
|
203
|
+
expiry: input.expiry ?? "48h",
|
|
204
|
+
attachment,
|
|
205
|
+
...(input.base ? { base: input.base } : {}),
|
|
206
|
+
...(input.cwd ? { cwd: input.cwd } : {}),
|
|
207
|
+
...(input.now ? { now: input.now } : {}),
|
|
208
|
+
...(input.runId ? { runId: input.runId } : {}),
|
|
209
|
+
...(input.commit ? { commit: input.commit } : {}),
|
|
210
|
+
...(input.ledger ? { ledger: input.ledger } : {}),
|
|
211
|
+
},
|
|
212
|
+
async ({ approvedBy }) => {
|
|
213
|
+
const written = await writeStateMigration(input.engine, roots);
|
|
214
|
+
return { output: written, detail: { by: input.by, approved_by: approvedBy, ...migrationFacts(written), ...(input.detail?.(written) ?? {}) }, failed: written.status !== "applied" };
|
|
215
|
+
},
|
|
216
|
+
);
|
|
217
|
+
const command = (fallback: string): string => (input.words.approveCommand ? input.words.approveCommand(input.name, result.digest) : fallback);
|
|
218
|
+
switch (result.status) {
|
|
219
|
+
case "waiting":
|
|
220
|
+
return { status: "waiting", exit: CHANT_EXIT.waiting, digest: result.digest, command: command(result.command) };
|
|
221
|
+
case "refused":
|
|
222
|
+
return { status: "refused", exit: CHANT_EXIT.waiting, digest: result.digest, command: command(result.command), by: result.by, reason: result.reason };
|
|
223
|
+
case "spent":
|
|
224
|
+
return { status: "spent", exit: CHANT_EXIT.done, digest: result.digest };
|
|
225
|
+
case "held":
|
|
226
|
+
return { status: "failed", exit: CHANT_EXIT.failed, digest: result.digest, error: result.reason, recorded: false };
|
|
227
|
+
case "failed":
|
|
228
|
+
return { status: "failed", exit: CHANT_EXIT.failed, digest: result.digest, error: result.error.message, recorded: result.recorded };
|
|
229
|
+
case "done": {
|
|
230
|
+
const r = result.output;
|
|
231
|
+
if (r.status === "applied") return { status: "applied", exit: CHANT_EXIT.done, digest: result.digest, approvedBy: result.approvedBy, result: r, recorded: result.recorded };
|
|
232
|
+
if (r.status === "moved") return { status: "moved", exit: CHANT_EXIT.moved, digest: result.digest, approvedBy: result.approvedBy, result: r, recorded: result.recorded };
|
|
233
|
+
return { status: "failed", exit: CHANT_EXIT.failed, digest: result.digest, error: r.error ?? "the migration failed", result: r, recorded: result.recorded };
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|