@intentius/chant 0.124.56 → 0.124.58
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/ci-audit/index.d.ts.map +1 -1
- package/dist/ci-audit/index.js +1 -2
- package/dist/ci-product-words.d.ts +47 -2
- package/dist/ci-product-words.d.ts.map +1 -1
- package/dist/ci-product-words.js +59 -1
- package/dist/ci-product.d.ts +26 -2
- package/dist/ci-product.d.ts.map +1 -1
- package/dist/ci-product.js +30 -7
- package/dist/cli/commands/init.d.ts +2 -0
- package/dist/cli/commands/init.d.ts.map +1 -1
- package/dist/cli/commands/init.js +1 -1
- package/dist/cli/handlers/build.d.ts.map +1 -1
- package/dist/cli/handlers/build.js +5 -1
- package/dist/cli/handlers/init.d.ts.map +1 -1
- package/dist/cli/handlers/init.js +16 -0
- package/dist/cli/handlers/lifecycle.d.ts.map +1 -1
- package/dist/cli/handlers/lifecycle.js +14 -0
- package/dist/cli/handlers/pr.d.ts.map +1 -1
- package/dist/cli/handlers/pr.js +90 -35
- package/dist/cli/handlers/serve.d.ts.map +1 -1
- package/dist/cli/handlers/serve.js +9 -0
- package/dist/cli/main.d.ts.map +1 -1
- package/dist/cli/main.js +6 -2
- package/dist/cli/mcp/product-tools.d.ts +40 -7
- package/dist/cli/mcp/product-tools.d.ts.map +1 -1
- package/dist/cli/mcp/product-tools.js +59 -15
- package/dist/cli/mcp/read-only.d.ts +2 -1
- package/dist/cli/mcp/read-only.d.ts.map +1 -1
- package/dist/cli/mcp/read-only.js +2 -1
- package/dist/cli/mcp/server.d.ts +11 -0
- package/dist/cli/mcp/server.d.ts.map +1 -1
- package/dist/cli/mcp/server.js +15 -3
- package/dist/cli/mcp-config.d.ts +4 -1
- package/dist/cli/mcp-config.d.ts.map +1 -1
- package/dist/cli/mcp-config.js +6 -2
- package/dist/cli/parse-args.d.ts.map +1 -1
- package/dist/cli/parse-args.js +9 -0
- package/dist/cli/registry.d.ts +8 -0
- package/dist/cli/registry.d.ts.map +1 -1
- package/dist/components/auto-release.d.ts +5 -2
- package/dist/components/auto-release.d.ts.map +1 -1
- package/dist/components/auto-release.js +9 -4
- package/dist/components/capability.d.ts +10 -0
- package/dist/components/capability.d.ts.map +1 -1
- package/dist/components/deploy-units.d.ts +12 -0
- package/dist/components/deploy-units.d.ts.map +1 -1
- package/dist/components/deploy-units.js +20 -1
- package/dist/components/pr-pipeline.d.ts +64 -2
- package/dist/components/pr-pipeline.d.ts.map +1 -1
- package/dist/components/pr-pipeline.js +66 -5
- package/dist/components/product-pipeline.d.ts +18 -1
- package/dist/components/product-pipeline.d.ts.map +1 -1
- package/dist/components/product-pipeline.js +8 -2
- package/dist/components/row-lease.d.ts +22 -1
- package/dist/components/row-lease.d.ts.map +1 -1
- package/dist/components/row-lease.js +32 -6
- package/dist/e2e-harness/driver.d.ts.map +1 -1
- package/dist/e2e-harness/driver.js +97 -0
- package/dist/e2e-harness/forgejo.d.ts +5 -1
- package/dist/e2e-harness/forgejo.d.ts.map +1 -1
- package/dist/e2e-harness/forgejo.js +35 -0
- package/dist/e2e-harness/index.d.ts +7 -0
- package/dist/e2e-harness/index.d.ts.map +1 -1
- package/dist/e2e-harness/index.js +11 -2
- package/dist/e2e-harness/ports.d.ts +31 -0
- package/dist/e2e-harness/ports.d.ts.map +1 -1
- package/dist/e2e-harness/scenario.d.ts.map +1 -1
- package/dist/e2e-harness/scenario.js +9 -5
- package/dist/e2e-harness/schemas/e2e-scenario.schema.json +43 -3
- package/dist/e2e-harness/schemas/index.d.ts +22 -1
- package/dist/e2e-harness/schemas/index.d.ts.map +1 -1
- package/dist/e2e-harness/schemas/index.js +1 -1
- package/dist/e2e-harness/steps.d.ts.map +1 -1
- package/dist/e2e-harness/steps.js +4 -0
- package/dist/lexicon.d.ts +9 -0
- package/dist/lexicon.d.ts.map +1 -1
- package/dist/lifecycle/git.d.ts +39 -3
- package/dist/lifecycle/git.d.ts.map +1 -1
- package/dist/lifecycle/git.js +223 -15
- package/dist/lifecycle/lock-rows.d.ts +23 -0
- package/dist/lifecycle/lock-rows.d.ts.map +1 -1
- package/dist/lifecycle/lock-rows.js +47 -8
- package/dist/lifecycle/resource-lock.d.ts +70 -0
- package/dist/lifecycle/resource-lock.d.ts.map +1 -1
- package/dist/lifecycle/resource-lock.js +241 -5
- package/dist/mcp/types.d.ts +9 -0
- package/dist/mcp/types.d.ts.map +1 -1
- package/dist/op/gate.js +21 -4
- package/dist/op/op-waves-release-port.js +1 -1
- package/dist/op/op-waves-release.d.ts +2 -1
- package/dist/op/op-waves-release.d.ts.map +1 -1
- package/dist/op/op-waves-run.d.ts +8 -0
- package/dist/op/op-waves-run.d.ts.map +1 -1
- package/dist/op/op-waves-run.js +101 -29
- package/dist/op/op-waves.d.ts +18 -0
- package/dist/op/op-waves.d.ts.map +1 -1
- package/dist/op/op-waves.js +16 -0
- package/dist/pr-loop.d.ts.map +1 -1
- package/dist/pr-loop.js +1 -2
- package/dist/release-floors.d.ts +1 -1
- package/dist/release-floors.js +1 -1
- package/dist/template-request/index.d.ts +9 -0
- package/dist/template-request/index.d.ts.map +1 -0
- package/dist/template-request/index.js +8 -0
- package/dist/template-request/request-body.d.ts +21 -0
- package/dist/template-request/request-body.d.ts.map +1 -0
- package/dist/template-request/request-body.js +86 -0
- package/dist/template-request/template-request.d.ts +100 -0
- package/dist/template-request/template-request.d.ts.map +1 -0
- package/dist/template-request/template-request.js +150 -0
- package/dist/template-request/template-request.schema.json +100 -0
- package/dist/template-request/template-spec.d.ts +83 -0
- package/dist/template-request/template-spec.d.ts.map +1 -0
- package/dist/template-request/template-spec.js +146 -0
- package/dist/template-request/template.schema.json +114 -0
- package/dist/template-request/validate.d.ts +4 -0
- package/dist/template-request/validate.d.ts.map +1 -0
- package/dist/template-request/validate.js +21 -0
- package/dist/wave-runtime.mjs +310 -300
- package/package.json +8 -1
- package/src/ci-audit/index.ts +1 -2
- package/src/ci-product-words.test.ts +23 -0
- package/src/ci-product-words.ts +68 -3
- package/src/ci-product.ts +36 -6
- package/src/cli/commands/init.ts +3 -1
- package/src/cli/handlers/build.ts +5 -1
- package/src/cli/handlers/init.ts +18 -0
- package/src/cli/handlers/lifecycle.ts +13 -0
- package/src/cli/handlers/pr-before-merge.test.ts +1 -1
- package/src/cli/handlers/pr-deploy-inputs.test.ts +1 -1
- package/src/cli/handlers/pr-endpoint.test.ts +1 -1
- package/src/cli/handlers/pr-green.test.ts +1 -1
- package/src/cli/handlers/pr-plan-comment-targets.test.ts +1 -1
- package/src/cli/handlers/pr-plan-flags.test.ts +1 -1
- package/src/cli/handlers/pr-plan-targets.test.ts +1 -1
- package/src/cli/handlers/pr-record.test.ts +34 -2
- package/src/cli/handlers/pr-waves-blocked.test.ts +1 -1
- package/src/cli/handlers/pr-waves.test.ts +1 -1
- package/src/cli/handlers/pr.test.ts +1 -1
- package/src/cli/handlers/pr.ts +86 -31
- package/src/cli/handlers/serve.ts +9 -0
- package/src/cli/main.ts +6 -2
- package/src/cli/mcp/product-tools.test.ts +57 -1
- package/src/cli/mcp/product-tools.ts +67 -15
- package/src/cli/mcp/read-only.ts +2 -1
- package/src/cli/mcp/server.ts +20 -5
- package/src/cli/mcp-config.test.ts +52 -2
- package/src/cli/mcp-config.ts +6 -3
- package/src/cli/parse-args.ts +7 -0
- package/src/cli/registry.ts +8 -0
- package/src/components/auto-release.test.ts +4 -2
- package/src/components/auto-release.ts +9 -5
- package/src/components/capability.ts +10 -0
- package/src/components/deploy-units.test.ts +18 -1
- package/src/components/deploy-units.ts +26 -1
- package/src/components/pr-pipeline.ts +99 -5
- package/src/components/product-actions.test.ts +76 -0
- package/src/components/product-pipeline.ts +21 -2
- package/src/components/row-lease.test.ts +61 -1
- package/src/components/row-lease.ts +48 -7
- package/src/e2e-harness/driver.test.ts +163 -0
- package/src/e2e-harness/driver.ts +86 -1
- package/src/e2e-harness/forgejo.ts +42 -1
- package/src/e2e-harness/index.ts +12 -2
- package/src/e2e-harness/ports.ts +33 -0
- package/src/e2e-harness/scenario.ts +8 -6
- package/src/e2e-harness/schemas/README.md +11 -2
- package/src/e2e-harness/schemas/e2e-scenario.schema.json +43 -3
- package/src/e2e-harness/schemas/index.ts +5 -1
- package/src/e2e-harness/schemas.test.ts +44 -1
- package/src/e2e-harness/steps.ts +4 -0
- package/src/lexicon.ts +9 -0
- package/src/lifecycle/git.test.ts +155 -0
- package/src/lifecycle/git.ts +213 -17
- package/src/lifecycle/lock-rows.test.ts +6 -1
- package/src/lifecycle/lock-rows.ts +58 -7
- package/src/lifecycle/resource-lock-wait.test.ts +292 -0
- package/src/lifecycle/resource-lock.ts +261 -7
- package/src/mcp/types.ts +9 -0
- package/src/op/gate.test.ts +7 -5
- package/src/op/gate.ts +20 -4
- package/src/op/op-waves-after-plan.test.ts +1 -2
- package/src/op/op-waves-ledger-lock.test.ts +208 -0
- package/src/op/op-waves-release-port.ts +1 -1
- package/src/op/op-waves-release.ts +1 -1
- package/src/op/op-waves-run.ts +104 -29
- package/src/op/op-waves.ts +18 -0
- package/src/pr-loop-bundle.test.ts +0 -2
- package/src/pr-loop.ts +1 -2
- package/src/release-floors.ts +1 -1
- package/src/template-request/__fixtures__/request-body.md +41 -0
- package/src/template-request/index.ts +8 -0
- package/src/template-request/request-body.ts +91 -0
- package/src/template-request/template-request.schema.json +100 -0
- package/src/template-request/template-request.test.ts +316 -0
- package/src/template-request/template-request.ts +235 -0
- package/src/template-request/template-spec.ts +176 -0
- package/src/template-request/template.schema.json +114 -0
- package/src/template-request/validate.ts +28 -0
|
@@ -24,6 +24,19 @@
|
|
|
24
24
|
* for one release), or, for a run on this host outside CI, when its
|
|
25
25
|
* process is. Anything nobody can answer counts as alive.
|
|
26
26
|
* - A release never throws: a row left held is taken over once its run is gone.
|
|
27
|
+
* - A take may wait (ruling for ci-products 2026-10-28, "Ledger lock"): the
|
|
28
|
+
* `target` and `repo` grains, a stock Pulumi stack's `stack` row, a
|
|
29
|
+
* Terraform root's `root` row or the `repo` row. A waiting run adds itself
|
|
30
|
+
* to `waiting` in the file, and takes its rows only once no live run holds
|
|
31
|
+
* any of them and no live waiter for a row that meets one of them comes
|
|
32
|
+
* first. First is the older commit (`commitAt`, the commit time of the
|
|
33
|
+
* commit the run applies), then the lower run id (both numbers, else by
|
|
34
|
+
* text), then the commit sha: the run that started first among runs of one
|
|
35
|
+
* commit. A waiter that is gone is dropped by the next writer, as a holder
|
|
36
|
+
* is. A wait that reaches its deadline (`CHANT_LOCK_WAIT` seconds, which
|
|
37
|
+
* the waves pipelines set from the job's timeout) leaves the queue and
|
|
38
|
+
* fails with {@link LockWaitTimeoutError}, the table's `held`, exit 5. A
|
|
39
|
+
* take on the resource grain never waits: a held row refuses it at once.
|
|
27
40
|
*
|
|
28
41
|
* The writes go through chant's ledger code (./git.ts) with the same
|
|
29
42
|
* read-then-CAS and push the target locks use, in place of terragucci's raw
|
|
@@ -35,6 +48,7 @@
|
|
|
35
48
|
* exit 5, and names the run that holds it.
|
|
36
49
|
*/
|
|
37
50
|
import { hostname } from "node:os";
|
|
51
|
+
import { getRuntime } from "../runtime-adapter";
|
|
38
52
|
import { pushLifecycleStatus, readBlobBySha, readPathSha, RefCASConflictError, writeBlobToPath } from "./git";
|
|
39
53
|
import { lockRowsConflict, sortedLockRows, type LockRow } from "./lock-rows";
|
|
40
54
|
import { dropRefusedWrite, fetchForLocks, localTip, LOCKS_DIR, targetLockGateProblem } from "./target-lock";
|
|
@@ -57,12 +71,21 @@ export interface LockRowHolder {
|
|
|
57
71
|
sha?: string;
|
|
58
72
|
}
|
|
59
73
|
|
|
74
|
+
/** A run waiting for rows: who it is, the rows it waits for, and the commit time that orders it. */
|
|
75
|
+
export interface LockRowWaiter extends LockRowHolder {
|
|
76
|
+
keys: string[];
|
|
77
|
+
/** The commit time (ISO 8601) of the commit the run applies. Absent sorts after every waiter that has one. */
|
|
78
|
+
commitAt?: string;
|
|
79
|
+
}
|
|
80
|
+
|
|
60
81
|
/** `_locks/rows/<gate>.json`. */
|
|
61
82
|
export interface LockRowsFile {
|
|
62
83
|
version: 1;
|
|
63
84
|
holders: Record<string, LockRowHolder>;
|
|
64
85
|
/** Row key to the run that holds it. */
|
|
65
86
|
rows: Record<string, string>;
|
|
87
|
+
/** The runs waiting for rows (ruling for ci-products 2026-10-28), by run. Left out of the file when empty. */
|
|
88
|
+
waiting?: Record<string, LockRowWaiter>;
|
|
66
89
|
}
|
|
67
90
|
|
|
68
91
|
/** A row another run holds, conflicting with `wanted`. */
|
|
@@ -161,7 +184,24 @@ export function parseLockRows(text: string | null): LockRowsFile {
|
|
|
161
184
|
}
|
|
162
185
|
const rows: Record<string, string> = {};
|
|
163
186
|
for (const [k, id] of Object.entries(doc.rows as Record<string, unknown>)) if (typeof id === "string" && holders[id]) rows[k] = id;
|
|
164
|
-
|
|
187
|
+
const waiting: Record<string, LockRowWaiter> = {};
|
|
188
|
+
const rawWaiting = (doc as { waiting?: unknown }).waiting;
|
|
189
|
+
if (rawWaiting && typeof rawWaiting === "object") {
|
|
190
|
+
for (const [id, raw] of Object.entries(rawWaiting as Record<string, Record<string, unknown>>)) {
|
|
191
|
+
if (typeof raw?.run !== "string" || typeof raw.at !== "string" || !Array.isArray(raw.keys)) continue;
|
|
192
|
+
const keys = (raw.keys as unknown[]).filter((k): k is string => typeof k === "string");
|
|
193
|
+
if (keys.length === 0) continue;
|
|
194
|
+
waiting[id] = {
|
|
195
|
+
run: raw.run,
|
|
196
|
+
at: raw.at,
|
|
197
|
+
keys,
|
|
198
|
+
...(typeof raw.url === "string" ? { url: raw.url } : {}),
|
|
199
|
+
...(typeof raw.sha === "string" ? { sha: raw.sha } : {}),
|
|
200
|
+
...(typeof raw.commitAt === "string" ? { commitAt: raw.commitAt } : {}),
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
return { version: 1, holders, rows, ...(Object.keys(waiting).length > 0 ? { waiting } : {}) };
|
|
165
205
|
}
|
|
166
206
|
} catch {
|
|
167
207
|
// An unreadable file holds no rows.
|
|
@@ -172,7 +212,8 @@ export function parseLockRows(text: string | null): LockRowsFile {
|
|
|
172
212
|
/** The file's text, keys sorted, so two writes of the same rows are the same blob. */
|
|
173
213
|
export function renderLockRows(file: LockRowsFile): string {
|
|
174
214
|
const sort = <T>(r: Record<string, T>) => Object.fromEntries(Object.keys(r).sort().map((k) => [k, r[k]]));
|
|
175
|
-
|
|
215
|
+
const waiting = file.waiting && Object.keys(file.waiting).length > 0 ? { waiting: sort(file.waiting) } : {};
|
|
216
|
+
return `${JSON.stringify({ version: 1, holders: sort(file.holders), rows: sort(file.rows), ...waiting }, null, 2)}\n`;
|
|
176
217
|
}
|
|
177
218
|
|
|
178
219
|
/** Who else holds a row that meets any of `keys`, split into runs still going and runs that are gone. */
|
|
@@ -197,19 +238,68 @@ export async function heldLockRowsIn(file: LockRowsFile, keys: readonly string[]
|
|
|
197
238
|
* `gone`. Pure, so the decision can be checked without git.
|
|
198
239
|
*/
|
|
199
240
|
export function withRowsTaken(file: LockRowsFile, keys: readonly string[], me: LockRowHolder, gone: readonly string[]): LockRowsFile {
|
|
200
|
-
const next
|
|
241
|
+
const next = copyRows(file);
|
|
201
242
|
for (const id of gone) {
|
|
202
243
|
delete next.holders[id];
|
|
203
244
|
for (const [k, h] of Object.entries(next.rows)) if (h === id) delete next.rows[k];
|
|
245
|
+
delete next.waiting?.[id];
|
|
204
246
|
}
|
|
205
|
-
next.
|
|
247
|
+
delete next.waiting?.[me.run];
|
|
248
|
+
next.holders[me.run] = { run: me.run, at: me.at, ...(me.url ? { url: me.url } : {}), ...(me.sha ? { sha: me.sha } : {}) };
|
|
206
249
|
for (const k of keys) next.rows[k] = me.run;
|
|
250
|
+
return tidyWaiting(next);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function copyRows(file: LockRowsFile): LockRowsFile {
|
|
254
|
+
return { version: 1, holders: { ...file.holders }, rows: { ...file.rows }, ...(file.waiting ? { waiting: { ...file.waiting } } : {}) };
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function tidyWaiting(file: LockRowsFile): LockRowsFile {
|
|
258
|
+
if (file.waiting && Object.keys(file.waiting).length === 0) delete file.waiting;
|
|
259
|
+
return file;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/** The rows file with `me` in the queue for `keys`, less the waiters in `gone`. Pure. */
|
|
263
|
+
export function withWaiter(file: LockRowsFile, me: LockRowWaiter, gone: readonly string[] = []): LockRowsFile {
|
|
264
|
+
const next = copyRows(file);
|
|
265
|
+
next.waiting = { ...(next.waiting ?? {}) };
|
|
266
|
+
for (const id of gone) delete next.waiting[id];
|
|
267
|
+
next.waiting[me.run] = { ...me, keys: [...new Set(me.keys)].sort() };
|
|
207
268
|
return next;
|
|
208
269
|
}
|
|
209
270
|
|
|
271
|
+
/** The rows file with `me` out of the queue. Pure. */
|
|
272
|
+
export function withoutWaiter(file: LockRowsFile, me: string): LockRowsFile {
|
|
273
|
+
const next = copyRows(file);
|
|
274
|
+
delete next.waiting?.[me];
|
|
275
|
+
return tidyWaiting(next);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* Whether waiter `a` goes before waiter `b`: the older commit first, then the
|
|
280
|
+
* lower run id (as numbers when both are, else as text), then the commit
|
|
281
|
+
* sha. A waiter with no commit time goes after every one that has one.
|
|
282
|
+
*/
|
|
283
|
+
export function waiterBefore(a: Pick<LockRowWaiter, "run" | "commitAt" | "sha">, b: Pick<LockRowWaiter, "run" | "commitAt" | "sha">): boolean {
|
|
284
|
+
const time = (w: { commitAt?: string }) => {
|
|
285
|
+
const t = w.commitAt ? new Date(w.commitAt).getTime() : NaN;
|
|
286
|
+
return Number.isFinite(t) ? t : Number.POSITIVE_INFINITY;
|
|
287
|
+
};
|
|
288
|
+
const ta = time(a);
|
|
289
|
+
const tb = time(b);
|
|
290
|
+
if (ta !== tb) return ta < tb;
|
|
291
|
+
if (a.run !== b.run) {
|
|
292
|
+
const na = Number(a.run);
|
|
293
|
+
const nb = Number(b.run);
|
|
294
|
+
if (/^\d+$/.test(a.run) && /^\d+$/.test(b.run) && na !== nb) return na < nb;
|
|
295
|
+
return a.run < b.run;
|
|
296
|
+
}
|
|
297
|
+
return (a.sha ?? "") < (b.sha ?? "");
|
|
298
|
+
}
|
|
299
|
+
|
|
210
300
|
/** The rows file after `me` lets go of `keys`; its holder entry goes once it holds no row. */
|
|
211
301
|
export function withRowsReleased(file: LockRowsFile, keys: readonly string[], me: string): LockRowsFile {
|
|
212
|
-
const next
|
|
302
|
+
const next = copyRows(file);
|
|
213
303
|
for (const k of keys) if (next.rows[k] === me) delete next.rows[k];
|
|
214
304
|
if (!Object.values(next.rows).includes(me)) delete next.holders[me];
|
|
215
305
|
return next;
|
|
@@ -292,6 +382,161 @@ export async function takeLockRows(gate: string, rows: readonly LockRow[], me: L
|
|
|
292
382
|
throw new Error(`could not hold the rows of ${gate}: ${STATE_BRANCH} kept moving; check that the job may push to it`);
|
|
293
383
|
}
|
|
294
384
|
|
|
385
|
+
/** A wait for rows reached its deadline (ruling for ci-products 2026-10-28): nothing applied. Exit 5, as a held row is. */
|
|
386
|
+
export class LockWaitTimeoutError extends Error {
|
|
387
|
+
readonly lockRowsHeld = true;
|
|
388
|
+
constructor(
|
|
389
|
+
message: string,
|
|
390
|
+
public readonly held: HeldLockRow[],
|
|
391
|
+
public readonly ahead: LockRowWaiter[],
|
|
392
|
+
public readonly waitedSeconds: number,
|
|
393
|
+
) {
|
|
394
|
+
super(message);
|
|
395
|
+
this.name = "LockWaitTimeoutError";
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
/** How a take waits for its rows. */
|
|
400
|
+
export interface LockRowsWait {
|
|
401
|
+
/** When the wait gives up, in ms since the epoch. */
|
|
402
|
+
deadline: number;
|
|
403
|
+
/** The commit time (ISO 8601) of the commit the run applies, which orders the queue. */
|
|
404
|
+
commitAt?: string;
|
|
405
|
+
/** How long between reads of the rows file, in ms. Default `CHANT_LOCK_POLL` seconds, else 10 s. */
|
|
406
|
+
pollMs?: number;
|
|
407
|
+
/** Where the wait's lines go. Default: none. */
|
|
408
|
+
log?: (line: string) => void;
|
|
409
|
+
/** The clock and the pause, for tests. */
|
|
410
|
+
now?: () => number;
|
|
411
|
+
sleep?: (ms: number) => Promise<void>;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
/** The default wait when a job names no timeout: just under three hours, a Forgejo runner's default job timeout. */
|
|
415
|
+
export const DEFAULT_LOCK_WAIT_SECONDS = 3 * 3600 - 300;
|
|
416
|
+
|
|
417
|
+
/** How long a take waits, in seconds: `CHANT_LOCK_WAIT`, else {@link DEFAULT_LOCK_WAIT_SECONDS}. */
|
|
418
|
+
export function lockWaitSeconds(env: Record<string, string | undefined>): number {
|
|
419
|
+
const n = Number(env.CHANT_LOCK_WAIT);
|
|
420
|
+
return env.CHANT_LOCK_WAIT !== undefined && Number.isFinite(n) && n >= 0 ? n : DEFAULT_LOCK_WAIT_SECONDS;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
/** The poll interval in ms: `CHANT_LOCK_POLL` seconds, else 10 s. */
|
|
424
|
+
export function lockPollMs(env: Record<string, string | undefined>): number {
|
|
425
|
+
const n = Number(env.CHANT_LOCK_POLL);
|
|
426
|
+
return env.CHANT_LOCK_POLL !== undefined && Number.isFinite(n) && n > 0 ? n * 1000 : 10_000;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/** The commit time of `rev` in `cwd` (ISO 8601), or undefined when git cannot say. */
|
|
430
|
+
export async function commitTimeOf(rev: string, cwd?: string): Promise<string | undefined> {
|
|
431
|
+
const out = await getRuntime().spawn(["git", "show", "-s", "--format=%cI", rev], { cwd });
|
|
432
|
+
const t = out.exitCode === 0 ? out.stdout.trim() : "";
|
|
433
|
+
return t && Number.isFinite(new Date(t).getTime()) ? t : undefined;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/** The live waiters for a row that meets one of `keys` who go before `me`, and the waiters that are gone. */
|
|
437
|
+
export async function waitersAhead(file: LockRowsFile, keys: readonly string[], me: LockRowWaiter, alive: LockLiveness): Promise<{ ahead: LockRowWaiter[]; gone: string[] }> {
|
|
438
|
+
const ahead: LockRowWaiter[] = [];
|
|
439
|
+
const gone: string[] = [];
|
|
440
|
+
for (const [id, w] of Object.entries(file.waiting ?? {})) {
|
|
441
|
+
if (id === me.run || w.run === me.run) continue;
|
|
442
|
+
if (!w.keys.some((k) => keys.some((mine) => lockRowsConflict(k, mine)))) continue;
|
|
443
|
+
const verdict = await alive(w);
|
|
444
|
+
if (verdict === "dead") {
|
|
445
|
+
gone.push(id);
|
|
446
|
+
continue;
|
|
447
|
+
}
|
|
448
|
+
if (waiterBefore(w, me)) ahead.push(w);
|
|
449
|
+
}
|
|
450
|
+
return { ahead: ahead.sort((a, b) => (waiterBefore(a, b) ? -1 : 1)), gone: gone.sort() };
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
function describeWait(held: readonly HeldLockRow[], ahead: readonly LockRowWaiter[], rows: readonly LockRow[]): string {
|
|
454
|
+
const parts: string[] = [];
|
|
455
|
+
if (held.length > 0) parts.push(describeHeldLockRows(held, rows));
|
|
456
|
+
if (ahead.length > 0) parts.push(`${ahead.map((w) => `run ${w.run}${w.url ? ` (${w.url})` : ""}`).join(", ")} ${ahead.length === 1 ? "waits" : "wait"} ahead of it with an older commit`);
|
|
457
|
+
return parts.join("; ");
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
/**
|
|
461
|
+
* Take `rows` for `me`, waiting in the queue until no live run holds any of
|
|
462
|
+
* them and no live waiter that goes first ({@link waiterBefore}) waits for a
|
|
463
|
+
* row that meets one of them. Throws {@link LockWaitTimeoutError} at the
|
|
464
|
+
* deadline, after leaving the queue.
|
|
465
|
+
*/
|
|
466
|
+
export async function waitLockRows(gate: string, rows: readonly LockRow[], me: LockRowHolder, wait: LockRowsWait, opts: LockRowsOptions = {}): Promise<{ ok: true; tookOver: LockRowHolder[]; pushed: boolean; waitedSeconds: number; waited: boolean }> {
|
|
467
|
+
checkGate(gate);
|
|
468
|
+
const keys = sortedLockRows(rows).map((r) => r.key);
|
|
469
|
+
if (keys.length === 0) return { ok: true, tookOver: [], pushed: false, waitedSeconds: 0, waited: false };
|
|
470
|
+
const alive = opts.alive ?? forgeLockLiveness(process.env);
|
|
471
|
+
const now = wait.now ?? Date.now;
|
|
472
|
+
const sleep = wait.sleep ?? pause;
|
|
473
|
+
const poll = wait.pollMs ?? lockPollMs(process.env);
|
|
474
|
+
const stale = lockStaleSeconds(process.env) * 1000;
|
|
475
|
+
const started = now();
|
|
476
|
+
const waiter = (): LockRowWaiter => ({ ...me, keys, ...(wait.commitAt ? { commitAt: wait.commitAt } : {}) });
|
|
477
|
+
let queued: LockRowWaiter | undefined;
|
|
478
|
+
let said = "";
|
|
479
|
+
await fetchForLocks(opts.cwd);
|
|
480
|
+
for (let raced = 0; ; ) {
|
|
481
|
+
const { sha, file } = await readCurrent(gate, opts.cwd);
|
|
482
|
+
const mine = queued ?? waiter();
|
|
483
|
+
const { live, gone } = await heldLockRowsIn(file, keys, me.run, alive);
|
|
484
|
+
const { ahead, gone: goneWaiters } = await waitersAhead(file, keys, mine, alive);
|
|
485
|
+
if (live.length === 0 && ahead.length === 0) {
|
|
486
|
+
const next = withRowsTaken(file, keys, me, gone);
|
|
487
|
+
for (const id of goneWaiters) delete next.waiting?.[id];
|
|
488
|
+
const took = gone.length ? `, taken over from run ${gone.join(", ")}` : "";
|
|
489
|
+
const wrote = await writeAndPush(gate, sha, tidyWaiting(next), `Hold ${keys.length} row${keys.length === 1 ? "" : "s"} of ${gate} for run ${me.run}${took}`, opts.cwd);
|
|
490
|
+
if (wrote) return { ok: true, tookOver: gone.map((id) => file.holders[id]!), pushed: wrote.pushed, waitedSeconds: Math.round((now() - started) / 1000), waited: queued !== undefined };
|
|
491
|
+
if (++raced >= ATTEMPTS) throw new Error(`could not hold the rows of ${gate}: ${STATE_BRANCH} kept moving; check that the job may push to it`);
|
|
492
|
+
await sleep(200 + Math.floor(Math.random() * 800));
|
|
493
|
+
continue;
|
|
494
|
+
}
|
|
495
|
+
const why = describeWait(live, ahead, rows);
|
|
496
|
+
if (now() >= wait.deadline) {
|
|
497
|
+
await leaveQueue(gate, me.run, opts.cwd);
|
|
498
|
+
const waited = Math.round((now() - started) / 1000);
|
|
499
|
+
throw new LockWaitTimeoutError(`waited ${waited}s for ${keys.join(", ")} of ${gate} and gave up at the job's deadline: ${why}`, live, ahead, waited);
|
|
500
|
+
}
|
|
501
|
+
const inFile = file.waiting?.[me.run];
|
|
502
|
+
const refresh = inFile ? now() - new Date(inFile.at).getTime() > stale / 2 : true;
|
|
503
|
+
if (!inFile || refresh || goneWaiters.length > 0) {
|
|
504
|
+
const entry: LockRowWaiter = { ...waiter(), at: new Date(now()).toISOString() };
|
|
505
|
+
const wrote = await writeAndPush(gate, sha, withWaiter(file, entry, goneWaiters), `Run ${me.run} waits for ${keys.length} row${keys.length === 1 ? "" : "s"} of ${gate}`, opts.cwd);
|
|
506
|
+
if (!wrote) {
|
|
507
|
+
if (++raced >= ATTEMPTS) throw new Error(`could not join the queue for the rows of ${gate}: ${STATE_BRANCH} kept moving; check that the job may push to it`);
|
|
508
|
+
await sleep(200 + Math.floor(Math.random() * 800));
|
|
509
|
+
continue;
|
|
510
|
+
}
|
|
511
|
+
queued = entry;
|
|
512
|
+
} else {
|
|
513
|
+
queued ??= inFile;
|
|
514
|
+
}
|
|
515
|
+
raced = 0;
|
|
516
|
+
if (why !== said) {
|
|
517
|
+
wait.log?.(`waits for ${keys.join(", ")} of ${gate}: ${why}`);
|
|
518
|
+
said = why;
|
|
519
|
+
}
|
|
520
|
+
await sleep(Math.max(0, Math.min(poll, wait.deadline - now())));
|
|
521
|
+
await fetchForLocks(opts.cwd);
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
/** Take run `me` out of `gate`'s queue. Never throws. */
|
|
526
|
+
async function leaveQueue(gate: string, me: string, cwd?: string): Promise<void> {
|
|
527
|
+
try {
|
|
528
|
+
await fetchForLocks(cwd);
|
|
529
|
+
for (let attempt = 0; attempt < ATTEMPTS; attempt++) {
|
|
530
|
+
const { sha, file } = await readCurrent(gate, cwd);
|
|
531
|
+
if (!file.waiting?.[me]) return;
|
|
532
|
+
if (await writeAndPush(gate, sha, withoutWaiter(file, me), `Run ${me} stops waiting for rows of ${gate}`, cwd)) return;
|
|
533
|
+
await pause(200 + Math.floor(Math.random() * 800));
|
|
534
|
+
}
|
|
535
|
+
} catch {
|
|
536
|
+
// A waiter left behind is dropped once its run is gone.
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
|
|
295
540
|
/** Let go of `keys` where run `me` still holds them. Never throws: a row left held is taken over once the run is gone. */
|
|
296
541
|
export async function releaseLockRows(gate: string, keys: readonly string[], me: string, opts: { cwd?: string } = {}): Promise<boolean> {
|
|
297
542
|
try {
|
|
@@ -342,12 +587,21 @@ export interface LockRowsLease {
|
|
|
342
587
|
export async function leaseLockRows(
|
|
343
588
|
gate: string,
|
|
344
589
|
rows: readonly LockRow[],
|
|
345
|
-
opts: LockRowsOptions & { holder?: LockRowHolder } = {},
|
|
346
|
-
): Promise<{ ok: true; lease: LockRowsLease; tookOver: LockRowHolder[] } | { ok: false; held: HeldLockRow[]; message: string }> {
|
|
590
|
+
opts: LockRowsOptions & { holder?: LockRowHolder; wait?: LockRowsWait } = {},
|
|
591
|
+
): Promise<{ ok: true; lease: LockRowsLease; tookOver: LockRowHolder[]; waitedSeconds?: number; waited?: boolean } | { ok: false; held: HeldLockRow[]; message: string; timedOut?: true }> {
|
|
347
592
|
const holder = opts.holder ?? lockRowHolder(process.env);
|
|
348
593
|
const keys = sortedLockRows(rows).map((r) => r.key);
|
|
349
594
|
const lease: LockRowsLease = { gate, holder, keys, release: () => (keys.length === 0 ? Promise.resolve(true) : releaseLockRows(gate, keys, holder.run, { ...(opts.cwd ? { cwd: opts.cwd } : {}) })) };
|
|
350
595
|
if (keys.length === 0) return { ok: true, lease, tookOver: [] };
|
|
596
|
+
if (opts.wait) {
|
|
597
|
+
try {
|
|
598
|
+
const waited = await waitLockRows(gate, rows, holder, opts.wait, opts);
|
|
599
|
+
return { ok: true, lease, tookOver: waited.tookOver, ...(waited.waited ? { waited: true, waitedSeconds: waited.waitedSeconds } : {}) };
|
|
600
|
+
} catch (err) {
|
|
601
|
+
if (err instanceof LockWaitTimeoutError) return { ok: false, held: err.held, message: err.message, timedOut: true };
|
|
602
|
+
throw err;
|
|
603
|
+
}
|
|
604
|
+
}
|
|
351
605
|
const taken = await takeLockRows(gate, rows, holder, opts);
|
|
352
606
|
if (!taken.ok) return { ok: false, held: taken.held, message: describeHeldLockRows(taken.held, rows) };
|
|
353
607
|
return { ok: true, lease, tookOver: taken.tookOver };
|
package/src/mcp/types.ts
CHANGED
|
@@ -18,6 +18,15 @@ export interface McpToolContribution {
|
|
|
18
18
|
* that leaves it out is served only by the ordinary server.
|
|
19
19
|
*/
|
|
20
20
|
readOnly?: boolean;
|
|
21
|
+
/**
|
|
22
|
+
* A CI product's tool that writes to the forge and nowhere else (chant#4655):
|
|
23
|
+
* it opens a pull request or posts a comment, and runs no Op, resolves no
|
|
24
|
+
* gate, approves, merges and applies nothing. `chant serve mcp --read-only
|
|
25
|
+
* --propose` serves a product tool that declares this; the read-only server
|
|
26
|
+
* without `--propose` refuses to start with one, and the ordinary server
|
|
27
|
+
* serves it as any other tool. Lexicon tools do not use it.
|
|
28
|
+
*/
|
|
29
|
+
forgeWrite?: true;
|
|
21
30
|
}
|
|
22
31
|
|
|
23
32
|
export interface McpResourceContribution {
|
package/src/op/gate.test.ts
CHANGED
|
@@ -571,12 +571,14 @@ describe("op/gate — a rejected ledger push is reported, not swallowed (#2310)"
|
|
|
571
571
|
timestamp: "2026-09-09T05:10:00.000Z",
|
|
572
572
|
expiresAt: "2026-09-10T05:10:00.000Z",
|
|
573
573
|
});
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
expect(third.
|
|
577
|
-
|
|
578
|
-
// the #2309 author's reasoning; only the report was missing.
|
|
574
|
+
// plywood#22 finding 9: the refused push replays the third fact on the
|
|
575
|
+
// remote's tip and pushes again, so it lands beside the other writer's.
|
|
576
|
+
expect(third.pushed).toBe(true);
|
|
577
|
+
expect(third.pushWarning).toBeUndefined();
|
|
579
578
|
expect(third.record.gate).toBe("approve-third-gate");
|
|
579
|
+
const onRemote = git(["log", "-p", "chant/lifecycle"], remote).stdout;
|
|
580
|
+
expect(onRemote).toContain("approve-other-gate");
|
|
581
|
+
expect(onRemote).toContain("approve-third-gate");
|
|
580
582
|
});
|
|
581
583
|
|
|
582
584
|
test("evaluateGate carries the rejection onto the gate check it returns, recorded but not pushed", async () => {
|
package/src/op/gate.ts
CHANGED
|
@@ -59,7 +59,7 @@ import {
|
|
|
59
59
|
quorumNeed, quorumRuleProgress, quorumRules, ruleNeedsProof, samePrincipal, trustQuorumJudge, UNPROVEN_JUDGE,
|
|
60
60
|
type GateQuorumJudge, type GateQuorumRuleProgress, type GateQuorumSpec,
|
|
61
61
|
} from "./gate-quorum";
|
|
62
|
-
import {
|
|
62
|
+
import { LifecyclePushLostError, pushLifecycleRebasing, readBlobBySha, readBlobFromPath, readPathSha, requireLifecycleLedger, writeBlobToPath, RefCASConflictError } from "../lifecycle/git";
|
|
63
63
|
import { parseDuration } from "./duration";
|
|
64
64
|
import { ciRunIdFromEnv, resolveGateRunLocator } from "./gate-run-locator";
|
|
65
65
|
import { resolveCommitSha } from "../lifecycle/commit-sha";
|
|
@@ -202,11 +202,11 @@ export function gitGateLedgerPort(opts?: { cwd?: string }): GateLedgerPort {
|
|
|
202
202
|
},
|
|
203
203
|
async appendResolution(input) {
|
|
204
204
|
const { record } = await appendGateResolution(input, opts);
|
|
205
|
-
return { record, ...(await
|
|
205
|
+
return { record, ...(await pushGateLine(opts)) };
|
|
206
206
|
},
|
|
207
207
|
async appendPending(input) {
|
|
208
208
|
const { record } = await appendPendingGate(input, opts);
|
|
209
|
-
return { record, ...(await
|
|
209
|
+
return { record, ...(await pushGateLine(opts)) };
|
|
210
210
|
},
|
|
211
211
|
async writeCheckpoint(checkpoint) {
|
|
212
212
|
await writeGateCheckpoint(checkpoint, opts);
|
|
@@ -255,7 +255,7 @@ export function gitGateLedgerPort(opts?: { cwd?: string }): GateLedgerPort {
|
|
|
255
255
|
...opts,
|
|
256
256
|
expectPriorPathSha: prior,
|
|
257
257
|
});
|
|
258
|
-
return
|
|
258
|
+
return pushGateLine(opts);
|
|
259
259
|
} catch (err) {
|
|
260
260
|
if (!(err instanceof RefCASConflictError)) throw err;
|
|
261
261
|
lastErr = err;
|
|
@@ -1120,3 +1120,19 @@ export async function gateIsSealed(gate: string, cwd: string = process.cwd()): P
|
|
|
1120
1120
|
return false;
|
|
1121
1121
|
}
|
|
1122
1122
|
}
|
|
1123
|
+
|
|
1124
|
+
/**
|
|
1125
|
+
* Push the gate line just written (plywood#22 finding 9): a push another
|
|
1126
|
+
* writer beat replays this checkout's commits on the remote's tip and pushes
|
|
1127
|
+
* again (`pushLifecycleRebasing`). One that never lands is reported as not
|
|
1128
|
+
* pushed, with `LifecyclePushLostError`'s message, so the spend line's
|
|
1129
|
+
* caller fails the wave as it does for any push that did not land.
|
|
1130
|
+
*/
|
|
1131
|
+
async function pushGateLine(opts?: { cwd?: string }): Promise<{ pushed: boolean; pushWarning?: string }> {
|
|
1132
|
+
try {
|
|
1133
|
+
return await pushLifecycleRebasing(opts);
|
|
1134
|
+
} catch (err) {
|
|
1135
|
+
if (err instanceof LifecyclePushLostError) return { pushed: false, pushWarning: `LifecyclePushLostError: ${err.message}` };
|
|
1136
|
+
throw err;
|
|
1137
|
+
}
|
|
1138
|
+
}
|
|
@@ -11,7 +11,6 @@ import type { OpWavesSpec } from "./op-waves";
|
|
|
11
11
|
import { assertOpWavesSpec } from "./op-waves";
|
|
12
12
|
import { runOpWave, opWavePlanPath, type OpWaveShowAtBase } from "./op-waves-run";
|
|
13
13
|
import { memoryGateLedgerPort } from "./gate";
|
|
14
|
-
import { NEXT_RELEASE } from "../release-floors";
|
|
15
14
|
import { CapabilityRegistry, type Capability } from "../components/capability";
|
|
16
15
|
import type { ChangeSetPart } from "../change-set";
|
|
17
16
|
import type { OpRunRecordInput } from "./runtime";
|
|
@@ -280,7 +279,7 @@ describe("opWavePlanPath", () => {
|
|
|
280
279
|
test("is exported from the op entry beside opWavePlansPath, and names one target's plan file", async () => {
|
|
281
280
|
const entry = await import("./index");
|
|
282
281
|
expect(entry.opWavePlanPath).toBe(opWavePlanPath);
|
|
283
|
-
expect(entry.OP_WAVE_PLAN_PATH_FLOOR).toBe(
|
|
282
|
+
expect(entry.OP_WAVE_PLAN_PATH_FLOOR).toBe("0.124.56");
|
|
284
283
|
const spec = { name: "demo" };
|
|
285
284
|
expect(entry.opWavePlanPath(spec, 2, "stacks/a b")).toBe(".chant/op-waves/demo/wave-2/stacks_a_b.plan.json");
|
|
286
285
|
expect(entry.opWavePlanPath(spec, 2, "x")).not.toBe(entry.opWavePlansPath(spec, 2));
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Op waves under the ruling for ci-products (2026-10-28), "Ledger lock",
|
|
3
|
+
* and plywood#22 finding 9, on a bare remote with one clone per writer:
|
|
4
|
+
*
|
|
5
|
+
* - a run on the `target` grain whose lexicon names its target's row waits
|
|
6
|
+
* for it on the ledger, then applies; at the deadline it applies nothing,
|
|
7
|
+
* held, exit 5, by name;
|
|
8
|
+
* - a run on the resource grain never waits: a held row is exit 5 at once;
|
|
9
|
+
* - a run whose plan changes nothing takes no row;
|
|
10
|
+
* - the job's run record lands when another writer pushed to the same
|
|
11
|
+
* ledger file first.
|
|
12
|
+
*/
|
|
13
|
+
import { afterAll, describe, expect, test } from "vitest";
|
|
14
|
+
import { execFileSync } from "node:child_process";
|
|
15
|
+
import { mkdtempSync, realpathSync, rmSync, writeFileSync } from "node:fs";
|
|
16
|
+
import { tmpdir } from "node:os";
|
|
17
|
+
import { join } from "node:path";
|
|
18
|
+
import type { OpWavesSpec } from "./op-waves";
|
|
19
|
+
import { gitOpWaveRunPort, runOpWave, type OpWaveShowAtBase } from "./op-waves-run";
|
|
20
|
+
import { memoryGateLedgerPort } from "./gate";
|
|
21
|
+
import { CapabilityRegistry, type Capability } from "../components/capability";
|
|
22
|
+
import type { ChangeSetPart } from "../change-set";
|
|
23
|
+
import { rootRowKey, type LockGrain } from "../lifecycle/lock-rows";
|
|
24
|
+
import { leaseLockRows, readLockRows } from "../lifecycle/resource-lock";
|
|
25
|
+
import { appendRunRecord, readRunLedger } from "../lifecycle/run-ledger";
|
|
26
|
+
import { pushLifecycle } from "../lifecycle/git";
|
|
27
|
+
import { CHANT_EXIT } from "../exit-codes";
|
|
28
|
+
|
|
29
|
+
const dirs: string[] = [];
|
|
30
|
+
afterAll(() => {
|
|
31
|
+
for (const d of dirs) rmSync(d, { recursive: true, force: true });
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
function tmp(prefix: string): string {
|
|
35
|
+
const d = realpathSync(mkdtempSync(join(tmpdir(), prefix)));
|
|
36
|
+
dirs.push(d);
|
|
37
|
+
return d;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function git(cwd: string, ...args: string[]): string {
|
|
41
|
+
return execFileSync("git", args, { cwd, encoding: "utf-8", stdio: ["ignore", "pipe", "pipe"] }).trim();
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function writers(n: number): string[] {
|
|
45
|
+
const remote = tmp("op-waves-lock-remote-");
|
|
46
|
+
git(remote, "init", "-q", "--bare", "-b", "main");
|
|
47
|
+
const seed = tmp("op-waves-lock-seed-");
|
|
48
|
+
git(seed, "init", "-q", "-b", "main");
|
|
49
|
+
git(seed, "config", "user.email", "seed@chant.dev");
|
|
50
|
+
git(seed, "config", "user.name", "Seed");
|
|
51
|
+
git(seed, "config", "commit.gpgsign", "false");
|
|
52
|
+
writeFileSync(join(seed, "README.md"), "# t\n");
|
|
53
|
+
git(seed, "add", "README.md");
|
|
54
|
+
git(seed, "commit", "-q", "-m", "init");
|
|
55
|
+
git(seed, "remote", "add", "origin", remote);
|
|
56
|
+
git(seed, "push", "-q", "origin", "main");
|
|
57
|
+
return Array.from({ length: n }, (_, i) => {
|
|
58
|
+
const c = tmp(`op-waves-lock-clone-${i}-`);
|
|
59
|
+
git(c, "clone", "-q", remote, ".");
|
|
60
|
+
git(c, "config", "user.email", `w${i}@chant.dev`);
|
|
61
|
+
git(c, "config", "user.name", `Writer ${i}`);
|
|
62
|
+
git(c, "config", "commit.gpgsign", "false");
|
|
63
|
+
return c;
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const digest = (n: string) => `sha256:${n.repeat(64)}`;
|
|
68
|
+
const alive = async () => "alive" as const;
|
|
69
|
+
|
|
70
|
+
/** A root capability on `grain`: the target grain names `root <root>`, the resource grain one row per root. */
|
|
71
|
+
function rootCapability(grain: LockGrain, action: "create" | "no-op" = "create"): { registry: CapabilityRegistry; ran: string[] } {
|
|
72
|
+
const ran: string[] = [];
|
|
73
|
+
const part = (member: string): ChangeSetPart => ({
|
|
74
|
+
member: { member, lexicon: "terraform", planner: "chant", status: "planned", planDigest: digest("9"), holes: [] },
|
|
75
|
+
entries: [{ member, address: `aws_s3_bucket.${member}`, type: "aws_s3_bucket", action } as ChangeSetPart["entries"][number]],
|
|
76
|
+
});
|
|
77
|
+
const cap: Capability<Record<string, unknown>, unknown> = {
|
|
78
|
+
kind: "fake-root",
|
|
79
|
+
async plan(_ctx, input) {
|
|
80
|
+
const root = String(input.root);
|
|
81
|
+
return { member: root, planDigest: digest(root === "network" ? "1" : "2"), artifact: { root }, changeSet: part(root) };
|
|
82
|
+
},
|
|
83
|
+
async run(_ctx, input) {
|
|
84
|
+
ran.push(String(input.root));
|
|
85
|
+
return { applied: input.root };
|
|
86
|
+
},
|
|
87
|
+
defaultLockGrain: () => grain,
|
|
88
|
+
targetLockRow: (input) => ({ key: rootRowKey(String(input.root)), target: String(input.root), address: String(input.root) }),
|
|
89
|
+
lockRows: (input) => [{ key: `root ${String(input.root)} aws_s3_bucket.${String(input.root)}`, target: String(input.root), address: `aws_s3_bucket.${String(input.root)}` }],
|
|
90
|
+
};
|
|
91
|
+
return { registry: new CapabilityRegistry().register(cap), ran };
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const spec: OpWavesSpec = {
|
|
95
|
+
name: "roots",
|
|
96
|
+
op: "tf-apply",
|
|
97
|
+
step: { kind: "fake-root", root: "{target}" },
|
|
98
|
+
waves: [{ name: "dev", runs: [{ target: "network" }], gate: "never" }],
|
|
99
|
+
};
|
|
100
|
+
const show: OpWaveShowAtBase = () => ({ sha: "b".repeat(40), text: JSON.stringify(spec) });
|
|
101
|
+
|
|
102
|
+
describe("op waves and the ledger lock", () => {
|
|
103
|
+
test("a root on the target grain waits for its root row, then applies once the other run lets go", async () => {
|
|
104
|
+
const [other, job] = writers(2) as [string, string];
|
|
105
|
+
const held = await leaseLockRows("pr-apply", [{ key: rootRowKey("network"), target: "network", address: "network" }], { cwd: other, alive, holder: { run: "71", at: new Date().toISOString() } });
|
|
106
|
+
expect(held.ok).toBe(true);
|
|
107
|
+
if (!held.ok) return;
|
|
108
|
+
const { registry, ran } = rootCapability("target");
|
|
109
|
+
const lines: string[] = [];
|
|
110
|
+
// The other run lets go once the job shows in the queue, so the job waits whatever the machine's speed.
|
|
111
|
+
let stop = false;
|
|
112
|
+
const letGo = (async () => {
|
|
113
|
+
while (!stop) {
|
|
114
|
+
const now = await readLockRows("pr-apply", { cwd: other });
|
|
115
|
+
if (Object.keys(now.waiting ?? {}).length > 0) return held.lease.release();
|
|
116
|
+
await new Promise((r) => setTimeout(r, 50));
|
|
117
|
+
}
|
|
118
|
+
return false;
|
|
119
|
+
})();
|
|
120
|
+
try {
|
|
121
|
+
const result = await runOpWave({
|
|
122
|
+
spec,
|
|
123
|
+
specFile: "waves.json",
|
|
124
|
+
wave: 1,
|
|
125
|
+
cwd: job,
|
|
126
|
+
show,
|
|
127
|
+
gates: memoryGateLedgerPort(),
|
|
128
|
+
capabilities: registry,
|
|
129
|
+
lockLiveness: alive,
|
|
130
|
+
lockPollMs: 50,
|
|
131
|
+
lockWaitSeconds: 60,
|
|
132
|
+
log: (l) => lines.push(l),
|
|
133
|
+
});
|
|
134
|
+
expect(result).toMatchObject({ exitCode: 0, applied: ["network"] });
|
|
135
|
+
} finally {
|
|
136
|
+
stop = true;
|
|
137
|
+
}
|
|
138
|
+
expect(await letGo).toBe(true);
|
|
139
|
+
expect(ran).toEqual(["network"]);
|
|
140
|
+
const text = lines.join("\n");
|
|
141
|
+
expect(text).toContain("wave 1 (dev): waits for root network of pr-apply: run 71 is applying network: network");
|
|
142
|
+
expect(text).toMatch(/wave 1 \(dev\): holds 1 row of pr-apply after waiting \d+s while it applies/);
|
|
143
|
+
// The job let go of the row when the wave ended.
|
|
144
|
+
expect((await readLockRows("pr-apply", { cwd: other })).rows).toEqual({});
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
test("a wait that reaches the job's deadline applies nothing, held, exit 5, by name", async () => {
|
|
148
|
+
const [other, job] = writers(2) as [string, string];
|
|
149
|
+
const held = await leaseLockRows("pr-apply", [{ key: rootRowKey("network"), target: "network", address: "network" }], { cwd: other, alive, holder: { run: "72", at: new Date().toISOString() } });
|
|
150
|
+
expect(held.ok).toBe(true);
|
|
151
|
+
const { registry, ran } = rootCapability("target");
|
|
152
|
+
const result = await runOpWave({ spec, specFile: "waves.json", wave: 1, cwd: job, show, gates: memoryGateLedgerPort(), capabilities: registry, lockLiveness: alive, lockPollMs: 10, lockWaitSeconds: 0, log: () => {} });
|
|
153
|
+
expect(result.exitCode).toBe(CHANT_EXIT.held);
|
|
154
|
+
expect(result.outcome).toMatchObject({ outcome: "held", holder: { run: "72" } });
|
|
155
|
+
expect(result.outcome!.reason).toMatch(/^LockWaitTimeoutError: waited \d+s for root network of pr-apply/);
|
|
156
|
+
expect(ran).toEqual([]);
|
|
157
|
+
expect((await readLockRows("pr-apply", { cwd: other })).waiting).toBeUndefined();
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
test("a run on the resource grain never waits: a held row is exit 5 at once", async () => {
|
|
161
|
+
const [other, job] = writers(2) as [string, string];
|
|
162
|
+
const key = "root network aws_s3_bucket.network";
|
|
163
|
+
const held = await leaseLockRows("pr-apply", [{ key, target: "network", address: "aws_s3_bucket.network" }], { cwd: other, alive, holder: { run: "73", at: new Date().toISOString() } });
|
|
164
|
+
expect(held.ok).toBe(true);
|
|
165
|
+
const { registry, ran } = rootCapability("resource");
|
|
166
|
+
const result = await runOpWave({ spec, specFile: "waves.json", wave: 1, cwd: job, show, gates: memoryGateLedgerPort(), capabilities: registry, lockLiveness: alive, lockPollMs: 10, lockWaitSeconds: 60, log: () => {} });
|
|
167
|
+
expect(result.exitCode).toBe(CHANT_EXIT.held);
|
|
168
|
+
expect(result.outcome!.reason).not.toContain("LockWaitTimeoutError");
|
|
169
|
+
expect(ran).toEqual([]);
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
test("a run whose plan changes nothing takes no row and applies beside the holder", async () => {
|
|
173
|
+
const [other, job] = writers(2) as [string, string];
|
|
174
|
+
const held = await leaseLockRows("pr-apply", [{ key: rootRowKey("network"), target: "network", address: "network" }], { cwd: other, alive, holder: { run: "74", at: new Date().toISOString() } });
|
|
175
|
+
expect(held.ok).toBe(true);
|
|
176
|
+
const { registry } = rootCapability("target", "no-op");
|
|
177
|
+
const result = await runOpWave({ spec, specFile: "waves.json", wave: 1, cwd: job, show, gates: memoryGateLedgerPort(), capabilities: registry, lockLiveness: alive, lockPollMs: 10, lockWaitSeconds: 0, log: () => {} });
|
|
178
|
+
expect(result.exitCode).toBe(0);
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
describe("plywood#22 finding 9: the waves run's run record", () => {
|
|
183
|
+
test("lands on the remote when another writer appended to the same run ledger first", async () => {
|
|
184
|
+
const [a, b] = writers(2) as [string, string];
|
|
185
|
+
const record = (ended: string) => ({
|
|
186
|
+
op: "pulumi-up",
|
|
187
|
+
env: "dev",
|
|
188
|
+
started: "2026-10-28T23:39:08.000Z",
|
|
189
|
+
ended,
|
|
190
|
+
status: "ok" as const,
|
|
191
|
+
labels: { Env: "dev" },
|
|
192
|
+
outcomes: {},
|
|
193
|
+
phases: [],
|
|
194
|
+
});
|
|
195
|
+
// B's job wrote nothing yet; A, the other push's job, appends to dev/pulumi-up's ledger and pushes first.
|
|
196
|
+
await appendRunRecord(record("2026-10-28T23:40:03.000Z"), { cwd: a });
|
|
197
|
+
expect(await pushLifecycle({ cwd: a })).toBe(true);
|
|
198
|
+
git(b, "fetch", "-q", "origin", "chant/lifecycle:chant/lifecycle");
|
|
199
|
+
await appendRunRecord(record("2026-10-28T23:40:30.000Z"), { cwd: a });
|
|
200
|
+
expect(await pushLifecycle({ cwd: a })).toBe(true);
|
|
201
|
+
// B appends on the copy it fetched before A's second push, and its push replays on A's tip.
|
|
202
|
+
const port = gitOpWaveRunPort({ cwd: b });
|
|
203
|
+
expect(await port.append(record("2026-10-28T23:41:17.000Z"))).toEqual({});
|
|
204
|
+
git(a, "fetch", "-q", "origin", "chant/lifecycle:chant/lifecycle");
|
|
205
|
+
const { records } = await readRunLedger("dev", "pulumi-up", { cwd: a });
|
|
206
|
+
expect(records.map((r) => r.ended)).toEqual(["2026-10-28T23:40:03.000Z", "2026-10-28T23:40:30.000Z", "2026-10-28T23:41:17.000Z"]);
|
|
207
|
+
});
|
|
208
|
+
});
|