@intentius/chant 0.124.62 → 0.124.63
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/cli/handlers/approve.d.ts +18 -0
- package/dist/cli/handlers/approve.d.ts.map +1 -1
- package/dist/cli/handlers/approve.js +142 -16
- package/dist/cli/handlers/plan.d.ts +10 -0
- package/dist/cli/handlers/plan.d.ts.map +1 -0
- package/dist/cli/handlers/plan.js +71 -0
- package/dist/cli/handlers/release.d.ts +6 -4
- package/dist/cli/handlers/release.d.ts.map +1 -1
- package/dist/cli/handlers/release.js +13 -7
- package/dist/cli/main.d.ts.map +1 -1
- package/dist/cli/main.js +34 -5
- package/dist/cli/mcp/workspace-tools.d.ts.map +1 -1
- package/dist/cli/mcp/workspace-tools.js +2 -1
- package/dist/cli/parse-args.d.ts.map +1 -1
- package/dist/cli/parse-args.js +31 -0
- package/dist/cli/registry.d.ts +13 -1
- package/dist/cli/registry.d.ts.map +1 -1
- package/dist/config.d.ts +32 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/gate-decision.d.ts +1 -1
- package/dist/gate-decision.d.ts.map +1 -1
- package/dist/gate-decision.js +4 -2
- package/dist/gate-statement.schema.json +44 -0
- package/dist/lifecycle/__fixtures__/publish-fakes.d.ts +20 -0
- package/dist/lifecycle/__fixtures__/publish-fakes.d.ts.map +1 -1
- package/dist/lifecycle/__fixtures__/publish-fakes.js +119 -3
- package/dist/lifecycle/gate-ledger.d.ts +17 -3
- package/dist/lifecycle/gate-ledger.d.ts.map +1 -1
- package/dist/lifecycle/gate-seal.d.ts +39 -3
- package/dist/lifecycle/gate-seal.d.ts.map +1 -1
- package/dist/lifecycle/gate-seal.js +50 -10
- package/dist/lifecycle/gate-statement.d.ts +70 -0
- package/dist/lifecycle/gate-statement.d.ts.map +1 -0
- package/dist/lifecycle/gate-statement.js +74 -0
- package/dist/lifecycle/gate-webauthn.d.ts +79 -0
- package/dist/lifecycle/gate-webauthn.d.ts.map +1 -0
- package/dist/lifecycle/gate-webauthn.js +105 -0
- package/dist/lifecycle/git.d.ts +1 -1
- package/dist/lifecycle/git.d.ts.map +1 -1
- package/dist/lifecycle/git.js +24 -19
- package/dist/lifecycle/ledger-remote.d.ts +37 -0
- package/dist/lifecycle/ledger-remote.d.ts.map +1 -0
- package/dist/lifecycle/ledger-remote.js +42 -0
- package/dist/lifecycle/plan-stale.d.ts +76 -0
- package/dist/lifecycle/plan-stale.d.ts.map +1 -0
- package/dist/lifecycle/plan-stale.js +121 -0
- package/dist/lifecycle/publish-credentials.d.ts +1 -1
- package/dist/lifecycle/publish-credentials.d.ts.map +1 -1
- package/dist/lifecycle/publish-credentials.js +2 -0
- package/dist/lifecycle/publish-declaration.d.ts +87 -0
- package/dist/lifecycle/publish-declaration.d.ts.map +1 -1
- package/dist/lifecycle/publish-declaration.js +87 -1
- package/dist/lifecycle/publish-forge.d.ts +5 -1
- package/dist/lifecycle/publish-forge.d.ts.map +1 -1
- package/dist/lifecycle/publish-forge.js +14 -4
- package/dist/lifecycle/publish-github.d.ts +69 -0
- package/dist/lifecycle/publish-github.d.ts.map +1 -0
- package/dist/lifecycle/publish-github.js +153 -0
- package/dist/lifecycle/publish-homebrew.d.ts +66 -0
- package/dist/lifecycle/publish-homebrew.d.ts.map +1 -0
- package/dist/lifecycle/publish-homebrew.js +173 -0
- package/dist/lifecycle/publish-marketplace.d.ts +51 -0
- package/dist/lifecycle/publish-marketplace.d.ts.map +1 -0
- package/dist/lifecycle/publish-marketplace.js +254 -0
- package/dist/lifecycle/publish-targets.d.ts +6 -0
- package/dist/lifecycle/publish-targets.d.ts.map +1 -1
- package/dist/lifecycle/publish-targets.js +15 -0
- package/dist/lifecycle/publish-tauri.d.ts +61 -0
- package/dist/lifecycle/publish-tauri.d.ts.map +1 -0
- package/dist/lifecycle/publish-tauri.js +182 -0
- package/dist/lifecycle/release-publish.d.ts +5 -2
- package/dist/lifecycle/release-publish.d.ts.map +1 -1
- package/dist/lifecycle/snapshot-store.d.ts.map +1 -1
- package/dist/lifecycle/snapshot-store.js +17 -9
- package/dist/lifecycle/test-passkey.d.ts +25 -0
- package/dist/lifecycle/test-passkey.d.ts.map +1 -0
- package/dist/lifecycle/test-passkey.js +39 -0
- package/dist/notify.d.ts +23 -3
- package/dist/notify.d.ts.map +1 -1
- package/dist/notify.js +8 -2
- package/dist/notify.schema.json +12 -3
- package/dist/op/activities/activity-contracts.d.ts +4 -4
- package/dist/pr-forge.d.ts +20 -0
- package/dist/pr-forge.d.ts.map +1 -1
- package/dist/pr-forge.js +61 -2
- package/dist/release-floors.d.ts +1 -1
- package/dist/release-floors.js +1 -1
- package/dist/wave-runtime.mjs +331 -330
- package/dist/workspace/ci-builds.d.ts +21 -0
- package/dist/workspace/ci-builds.d.ts.map +1 -1
- package/dist/workspace/ci-builds.js +14 -0
- package/dist/workspace/ci-green.d.ts +45 -2
- package/dist/workspace/ci-green.d.ts.map +1 -1
- package/dist/workspace/ci-green.js +124 -5
- package/dist/workspace/ci-quarantine-report.d.ts +80 -0
- package/dist/workspace/ci-quarantine-report.d.ts.map +1 -0
- package/dist/workspace/ci-quarantine-report.js +205 -0
- package/dist/workspace/ci-quarantine.d.ts +73 -0
- package/dist/workspace/ci-quarantine.d.ts.map +1 -0
- package/dist/workspace/ci-quarantine.js +171 -0
- package/dist/workspace/ci-serve.d.ts.map +1 -1
- package/dist/workspace/ci-serve.js +7 -4
- package/dist/workspace/class-data.d.ts +3 -1
- package/dist/workspace/class-data.d.ts.map +1 -1
- package/dist/workspace/class-data.js +4 -1
- package/dist/workspace/declaration.d.ts +45 -2
- package/dist/workspace/declaration.d.ts.map +1 -1
- package/dist/workspace/declaration.js +45 -2
- package/dist/workspace/declaration.schema.json +51 -3
- package/dist/workspace/forge-work.d.ts +76 -0
- package/dist/workspace/forge-work.d.ts.map +1 -0
- package/dist/workspace/forge-work.js +160 -0
- package/dist/workspace/identity-gate-rule.d.ts +3 -2
- package/dist/workspace/identity-gate-rule.d.ts.map +1 -1
- package/dist/workspace/identity-gate-rule.js +5 -7
- package/dist/workspace/identity.d.ts +3 -2
- package/dist/workspace/identity.d.ts.map +1 -1
- package/dist/workspace/identity.js +6 -3
- package/dist/workspace/intent-cli.d.ts +3 -2
- package/dist/workspace/intent-cli.d.ts.map +1 -1
- package/dist/workspace/intent-cli.js +26 -5
- package/dist/workspace/intent-record.d.ts +3 -0
- package/dist/workspace/intent-record.d.ts.map +1 -1
- package/dist/workspace/intent-record.schema.json +32 -0
- package/dist/workspace/intent.d.ts +9 -0
- package/dist/workspace/intent.d.ts.map +1 -1
- package/dist/workspace/intent.js +3 -0
- package/dist/workspace/intent.schema.json +32 -0
- package/dist/workspace/kind-readers.d.ts.map +1 -1
- package/dist/workspace/kind-readers.js +2 -1
- package/dist/workspace/kinds.d.ts +6 -1
- package/dist/workspace/kinds.d.ts.map +1 -1
- package/dist/workspace/kinds.js +8 -3
- package/dist/workspace/plan-stale.schema.json +286 -0
- package/dist/workspace/release-candidates.d.ts +33 -6
- package/dist/workspace/release-candidates.d.ts.map +1 -1
- package/dist/workspace/release-candidates.js +49 -20
- package/dist/workspace/release-candidates.schema.json +48 -2
- package/dist/workspace/trust/policy.d.ts +10 -0
- package/dist/workspace/trust/policy.d.ts.map +1 -1
- package/dist/workspace/trust/policy.js +19 -1
- package/dist/workspace/trust/run-commits.d.ts.map +1 -1
- package/dist/workspace/trust/run-commits.js +2 -1
- package/dist/workspace/trust/seal.d.ts.map +1 -1
- package/dist/workspace/trust/seal.js +34 -3
- package/dist/workspace/trust/trust-files.d.ts.map +1 -1
- package/dist/workspace/trust/trust-files.js +6 -1
- package/package.json +2 -1
- package/src/cli/handlers/approve-external-signature.test.ts +226 -0
- package/src/cli/handlers/approve.ts +174 -16
- package/src/cli/handlers/plan.ts +74 -0
- package/src/cli/handlers/release.ts +12 -7
- package/src/cli/main.ts +34 -5
- package/src/cli/mcp/workspace-tools.ts +2 -1
- package/src/cli/parse-args.ts +24 -0
- package/src/cli/registry.ts +13 -1
- package/src/gate-decision-bundle.test.ts +4 -2
- package/src/gate-decision.ts +11 -2
- package/src/gate-statement.schema.json +44 -0
- package/src/lifecycle/__fixtures__/publish-fakes.ts +125 -5
- package/src/lifecycle/gate-ledger.ts +17 -3
- package/src/lifecycle/gate-seal.ts +62 -9
- package/src/lifecycle/gate-statement.ts +107 -0
- package/src/lifecycle/gate-webauthn.test.ts +77 -0
- package/src/lifecycle/gate-webauthn.ts +133 -0
- package/src/lifecycle/git.ts +23 -19
- package/src/lifecycle/ledger-remote.test.ts +183 -0
- package/src/lifecycle/ledger-remote.ts +46 -0
- package/src/lifecycle/plan-stale.test.ts +150 -0
- package/src/lifecycle/plan-stale.ts +173 -0
- package/src/lifecycle/publish-credentials.ts +2 -0
- package/src/lifecycle/publish-declaration.ts +93 -1
- package/src/lifecycle/publish-forge.ts +15 -4
- package/src/lifecycle/publish-github.ts +174 -0
- package/src/lifecycle/publish-homebrew.test.ts +179 -0
- package/src/lifecycle/publish-homebrew.ts +187 -0
- package/src/lifecycle/publish-marketplace.test.ts +254 -0
- package/src/lifecycle/publish-marketplace.ts +259 -0
- package/src/lifecycle/publish-target-context.test.ts +2 -2
- package/src/lifecycle/publish-targets.test.ts +23 -0
- package/src/lifecycle/publish-targets.ts +15 -0
- package/src/lifecycle/publish-tauri.test.ts +163 -0
- package/src/lifecycle/publish-tauri.ts +199 -0
- package/src/lifecycle/release-publish.ts +5 -3
- package/src/lifecycle/snapshot-store.ts +16 -9
- package/src/lifecycle/test-passkey.ts +55 -0
- package/src/notify-bundle.test.ts +3 -1
- package/src/notify.schema.json +12 -3
- package/src/notify.ts +20 -3
- package/src/override-gate-bundle.test.ts +2 -0
- package/src/pr-forge-bundle.test.ts +2 -2
- package/src/pr-forge.test.ts +38 -0
- package/src/pr-forge.ts +68 -5
- package/src/release-floors.ts +1 -1
- package/src/stage-hooks-bundle.test.ts +2 -0
- package/src/wave-runtime-bundle.test.ts +3 -2
- package/src/workspace/ci-builds.ts +27 -0
- package/src/workspace/ci-green-stale.test.ts +193 -0
- package/src/workspace/ci-green.ts +138 -6
- package/src/workspace/ci-quarantine-report.test.ts +146 -0
- package/src/workspace/ci-quarantine-report.ts +235 -0
- package/src/workspace/ci-quarantine.test.ts +161 -0
- package/src/workspace/ci-quarantine.ts +189 -0
- package/src/workspace/ci-serve.ts +7 -4
- package/src/workspace/class-data.ts +5 -1
- package/src/workspace/declaration.schema.json +51 -3
- package/src/workspace/declaration.test.ts +1 -0
- package/src/workspace/declaration.ts +88 -5
- package/src/workspace/forge-work.test.ts +143 -0
- package/src/workspace/forge-work.ts +195 -0
- package/src/workspace/identity-gate-rule.ts +6 -8
- package/src/workspace/identity.ts +6 -3
- package/src/workspace/intent-cli.ts +24 -5
- package/src/workspace/intent-record.schema.json +32 -0
- package/src/workspace/intent-record.ts +3 -0
- package/src/workspace/intent.schema.json +32 -0
- package/src/workspace/intent.ts +10 -0
- package/src/workspace/kind-readers.ts +2 -1
- package/src/workspace/kinds.test.ts +13 -0
- package/src/workspace/kinds.ts +12 -3
- package/src/workspace/plan-stale.schema.json +286 -0
- package/src/workspace/release-candidates.schema.json +48 -2
- package/src/workspace/release-candidates.test.ts +77 -4
- package/src/workspace/release-candidates.ts +62 -20
- package/src/workspace/trust/policy.ts +24 -1
- package/src/workspace/trust/run-commits.ts +2 -1
- package/src/workspace/trust/seal.ts +35 -3
- package/src/workspace/trust/trust-files.test.ts +5 -0
- package/src/workspace/trust/trust-files.ts +15 -1
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { afterEach, describe, expect, test, vi } from "vitest";
|
|
2
|
+
import { spawnSync } from "node:child_process";
|
|
3
|
+
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
|
4
|
+
import { tmpdir } from "node:os";
|
|
5
|
+
import { join } from "node:path";
|
|
6
|
+
import { chooseLedgerRemote, LIFECYCLE_REMOTE_ENV, lifecycleRemoteOverride } from "./ledger-remote";
|
|
7
|
+
import {
|
|
8
|
+
fetchLifecycleStatus,
|
|
9
|
+
fetchRef,
|
|
10
|
+
pushLifecycleRebasing,
|
|
11
|
+
pushLifecycleReport,
|
|
12
|
+
pushLifecycleStatus,
|
|
13
|
+
pushRef,
|
|
14
|
+
readRefSha,
|
|
15
|
+
updateRefCAS,
|
|
16
|
+
writeBlob,
|
|
17
|
+
writeBlobToPath,
|
|
18
|
+
} from "./git";
|
|
19
|
+
import { NO_RECORD_ENV } from "./no-record";
|
|
20
|
+
|
|
21
|
+
function git(args: string[], cwd: string): string {
|
|
22
|
+
const r = spawnSync("git", args, { cwd, encoding: "utf-8" });
|
|
23
|
+
if (r.status !== 0) throw new Error(`git ${args.join(" ")}: ${r.stderr}`);
|
|
24
|
+
return r.stdout.trim();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function tip(repo: string, ref = "refs/heads/chant/lifecycle"): string | null {
|
|
28
|
+
const r = spawnSync("git", ["rev-parse", "--verify", "--quiet", ref], { cwd: repo, encoding: "utf-8" });
|
|
29
|
+
return r.status === 0 ? r.stdout.trim() : null;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Two checkouts, A and B, cloned from a bare `origin` that is not where the
|
|
34
|
+
* ledger belongs, each with a second remote `ledger` that is.
|
|
35
|
+
*/
|
|
36
|
+
function setup(): { a: string; b: string; origin: string; ledger: string; root: string } {
|
|
37
|
+
const root = mkdtempSync(join(tmpdir(), "chant-ledger-remote-"));
|
|
38
|
+
const origin = join(root, "origin.git");
|
|
39
|
+
const ledger = join(root, "ledger.git");
|
|
40
|
+
git(["init", "-q", "--bare", "-b", "main", origin], root);
|
|
41
|
+
git(["init", "-q", "--bare", "-b", "main", ledger], root);
|
|
42
|
+
const seed = join(root, "seed");
|
|
43
|
+
git(["clone", "-q", origin, seed], root);
|
|
44
|
+
git(["config", "user.email", "seed@chant.dev"], seed);
|
|
45
|
+
git(["config", "user.name", "Seed"], seed);
|
|
46
|
+
writeFileSync(join(seed, "README.md"), "fixture\n");
|
|
47
|
+
git(["add", "README.md"], seed);
|
|
48
|
+
git(["commit", "-q", "-m", "init"], seed);
|
|
49
|
+
git(["push", "-q", "origin", "main"], seed);
|
|
50
|
+
const clone = (name: string): string => {
|
|
51
|
+
const dir = join(root, name);
|
|
52
|
+
git(["clone", "-q", origin, dir], root);
|
|
53
|
+
git(["config", "user.email", `${name}@chant.dev`], dir);
|
|
54
|
+
git(["config", "user.name", name], dir);
|
|
55
|
+
git(["remote", "add", "ledger", ledger], dir);
|
|
56
|
+
return dir;
|
|
57
|
+
};
|
|
58
|
+
return { a: clone("a"), b: clone("b"), origin, ledger, root };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
afterEach(() => {
|
|
62
|
+
vi.unstubAllEnvs();
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
describe("chooseLedgerRemote", () => {
|
|
66
|
+
test("origin, else the first remote, when nothing is named", () => {
|
|
67
|
+
expect(chooseLedgerRemote(["backup", "origin"], {})).toEqual({ remote: "origin" });
|
|
68
|
+
expect(chooseLedgerRemote(["backup", "mirror"], {})).toEqual({ remote: "backup" });
|
|
69
|
+
expect(chooseLedgerRemote([], {})).toBeNull();
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
test("the remote CHANT_LIFECYCLE_REMOTE names, over origin", () => {
|
|
73
|
+
expect(chooseLedgerRemote(["origin", "ledger"], { [LIFECYCLE_REMOTE_ENV]: "ledger" })).toEqual({ remote: "ledger" });
|
|
74
|
+
expect(chooseLedgerRemote(["origin", "ledger"], { [LIFECYCLE_REMOTE_ENV]: " ledger " })).toEqual({ remote: "ledger" });
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
test("a name the repository has no remote by fails, and never falls back to origin", () => {
|
|
78
|
+
expect(chooseLedgerRemote(["origin"], { [LIFECYCLE_REMOTE_ENV]: "ledger" })).toEqual({
|
|
79
|
+
failed: true,
|
|
80
|
+
stderr: `${LIFECYCLE_REMOTE_ENV} names the remote "ledger", and this repository's remotes are origin`,
|
|
81
|
+
named: "ledger",
|
|
82
|
+
});
|
|
83
|
+
expect(chooseLedgerRemote([], { [LIFECYCLE_REMOTE_ENV]: "ledger" })).toMatchObject({ failed: true });
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
test("empty counts as unset", () => {
|
|
87
|
+
expect(lifecycleRemoteOverride({ [LIFECYCLE_REMOTE_ENV]: " " })).toBeUndefined();
|
|
88
|
+
expect(chooseLedgerRemote(["origin"], { [LIFECYCLE_REMOTE_ENV]: "" })).toEqual({ remote: "origin" });
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
describe("CHANT_LIFECYCLE_REMOTE (#4647)", () => {
|
|
93
|
+
test("the branch is pushed to and fetched from the named remote, with its remote-tracking ref, and origin is left alone", async () => {
|
|
94
|
+
const { a, b, origin, ledger, root } = setup();
|
|
95
|
+
try {
|
|
96
|
+
vi.stubEnv(LIFECYCLE_REMOTE_ENV, "ledger");
|
|
97
|
+
await writeBlobToPath("prod", "plan.json", '{"plan":1}', "plan", { cwd: a });
|
|
98
|
+
expect(await pushLifecycleStatus({ cwd: a })).toMatchObject({ status: "pushed", remote: "ledger" });
|
|
99
|
+
const pushed = tip(ledger);
|
|
100
|
+
expect(pushed).toBe(tip(a));
|
|
101
|
+
expect(tip(a, "refs/remotes/ledger/chant/lifecycle")).toBe(pushed);
|
|
102
|
+
expect(tip(origin)).toBeNull();
|
|
103
|
+
expect(tip(a, "refs/remotes/origin/chant/lifecycle")).toBeNull();
|
|
104
|
+
|
|
105
|
+
expect(await fetchLifecycleStatus({ cwd: b })).toEqual({ status: "fetched" });
|
|
106
|
+
expect(tip(b)).toBe(pushed);
|
|
107
|
+
expect(tip(b, "refs/remotes/ledger/chant/lifecycle")).toBe(pushed);
|
|
108
|
+
} finally {
|
|
109
|
+
rmSync(root, { recursive: true, force: true });
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
test("a push another writer beat to the named remote replays on its tip and lands there", async () => {
|
|
114
|
+
const { a, b, origin, ledger, root } = setup();
|
|
115
|
+
try {
|
|
116
|
+
vi.stubEnv(LIFECYCLE_REMOTE_ENV, "ledger");
|
|
117
|
+
await writeBlobToPath("prod", "seed.json", "{}", "seed", { cwd: a });
|
|
118
|
+
expect((await pushLifecycleReport({ cwd: a })).pushed).toBe(true);
|
|
119
|
+
expect((await fetchLifecycleStatus({ cwd: b })).status).toBe("fetched");
|
|
120
|
+
|
|
121
|
+
// A moves the ledger on behind B's back.
|
|
122
|
+
await writeBlobToPath("prod", "wave.json", '{"wave":1}', "wave", { cwd: a });
|
|
123
|
+
expect((await pushLifecycleReport({ cwd: a })).pushed).toBe(true);
|
|
124
|
+
await writeBlobToPath("prod", "merge.json", '{"merge":1}', "merge", { cwd: b });
|
|
125
|
+
expect(await pushLifecycleStatus({ cwd: b })).toMatchObject({ status: "stale", remote: "ledger" });
|
|
126
|
+
|
|
127
|
+
expect(await pushLifecycleRebasing({ cwd: b })).toEqual({ pushed: true });
|
|
128
|
+
const landed = tip(ledger)!;
|
|
129
|
+
expect(git(["show", `${landed}:prod/wave.json`], ledger)).toBe('{"wave":1}');
|
|
130
|
+
expect(git(["show", `${landed}:prod/merge.json`], ledger)).toBe('{"merge":1}');
|
|
131
|
+
expect(tip(b, "refs/remotes/ledger/chant/lifecycle")).toBe(landed);
|
|
132
|
+
expect(tip(origin)).toBeNull();
|
|
133
|
+
} finally {
|
|
134
|
+
rmSync(root, { recursive: true, force: true });
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
test("a lease ref goes to the named remote too", async () => {
|
|
139
|
+
const { a, b, origin, ledger, root } = setup();
|
|
140
|
+
try {
|
|
141
|
+
vi.stubEnv(LIFECYCLE_REMOTE_ENV, "ledger");
|
|
142
|
+
const ref = "refs/chant/lease/converge";
|
|
143
|
+
const blob = await writeBlob('{"holder":"a"}', { cwd: a });
|
|
144
|
+
await updateRefCAS(ref, blob, null, { cwd: a });
|
|
145
|
+
expect(await pushRef(ref, { cwd: a })).toBe(true);
|
|
146
|
+
expect(tip(ledger, ref)).toBe(blob);
|
|
147
|
+
expect(tip(origin, ref)).toBeNull();
|
|
148
|
+
expect(await fetchRef(ref, { cwd: b })).toBe(true);
|
|
149
|
+
expect(await readRefSha(ref, { cwd: b })).toBe(blob);
|
|
150
|
+
} finally {
|
|
151
|
+
rmSync(root, { recursive: true, force: true });
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
test("a name with no such remote fails the push, the fetch and a write, and sends nothing to origin", async () => {
|
|
156
|
+
const { a, origin, root } = setup();
|
|
157
|
+
try {
|
|
158
|
+
await writeBlobToPath("prod", "plan.json", "{}", "plan", { cwd: a });
|
|
159
|
+
vi.stubEnv(LIFECYCLE_REMOTE_ENV, "elsewhere");
|
|
160
|
+
const pushed = await pushLifecycleStatus({ cwd: a });
|
|
161
|
+
expect(pushed).toMatchObject({ status: "failed", remote: "elsewhere" });
|
|
162
|
+
expect(pushed.status === "failed" ? pushed.stderr : "").toBe(`${LIFECYCLE_REMOTE_ENV} names the remote "elsewhere", and this repository's remotes are ledger, origin`);
|
|
163
|
+
expect(await fetchLifecycleStatus({ cwd: a })).toMatchObject({ status: "unreachable", remote: "elsewhere" });
|
|
164
|
+
expect(await pushRef("refs/heads/chant/lifecycle", { cwd: a })).toBe(false);
|
|
165
|
+
expect(tip(origin)).toBeNull();
|
|
166
|
+
} finally {
|
|
167
|
+
rmSync(root, { recursive: true, force: true });
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
test("a run that records nothing pushes nothing to the named remote", async () => {
|
|
172
|
+
const { a, ledger, root } = setup();
|
|
173
|
+
try {
|
|
174
|
+
vi.stubEnv(LIFECYCLE_REMOTE_ENV, "ledger");
|
|
175
|
+
vi.stubEnv(NO_RECORD_ENV, "1");
|
|
176
|
+
await writeBlobToPath("prod", "plan.json", "{}", "plan", { cwd: a });
|
|
177
|
+
expect(await pushLifecycleStatus({ cwd: a })).toEqual({ status: "not-recorded" });
|
|
178
|
+
expect(tip(ledger)).toBeNull();
|
|
179
|
+
} finally {
|
|
180
|
+
rmSync(root, { recursive: true, force: true });
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
});
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which remote `chant/lifecycle` is fetched from and pushed to (#4647).
|
|
3
|
+
*
|
|
4
|
+
* By default the ledger lives on `origin`, or on the first remote when there
|
|
5
|
+
* is no `origin`. `CHANT_LIFECYCLE_REMOTE=<name>` names another one, for a
|
|
6
|
+
* checkout whose `origin` is not where the ledger belongs (a deploy box that
|
|
7
|
+
* clones from a mirror, say). Every read and write of the ledger goes through
|
|
8
|
+
* here: the fetch, the push and its lease, the remote-tracking ref
|
|
9
|
+
* `refs/remotes/<remote>/chant/lifecycle`, and the lease and lock refs pushed
|
|
10
|
+
* beside the branch.
|
|
11
|
+
*
|
|
12
|
+
* A name the repository has no remote by is an error, never a fallback to
|
|
13
|
+
* `origin`: a ledger pushed to the wrong remote is what the setting exists to
|
|
14
|
+
* prevent.
|
|
15
|
+
*
|
|
16
|
+
* Kept free of git and the runtime adapter, so the synchronous readers (the
|
|
17
|
+
* trust checks, `ci base`, ci-serve) can use it too.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/** The environment variable that names the ledger's remote. */
|
|
21
|
+
export const LIFECYCLE_REMOTE_ENV = "CHANT_LIFECYCLE_REMOTE";
|
|
22
|
+
|
|
23
|
+
/** The remote `CHANT_LIFECYCLE_REMOTE` names, or undefined when it is unset or empty. */
|
|
24
|
+
export function lifecycleRemoteOverride(env: NodeJS.ProcessEnv = process.env): string | undefined {
|
|
25
|
+
const value = env[LIFECYCLE_REMOTE_ENV]?.trim();
|
|
26
|
+
return value ? value : undefined;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** What {@link chooseLedgerRemote} found: a remote, no remote at all, or a setting that names none of them. */
|
|
30
|
+
export type LedgerRemoteChoice = { remote: string } | { failed: true; stderr: string; named?: string } | null;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The ledger's remote among `names`, the output of `git remote`: the one
|
|
34
|
+
* `CHANT_LIFECYCLE_REMOTE` names, else `origin`, else the first. Null when
|
|
35
|
+
* there is no remote and nothing is named.
|
|
36
|
+
*/
|
|
37
|
+
export function chooseLedgerRemote(names: readonly string[], env: NodeJS.ProcessEnv = process.env): LedgerRemoteChoice {
|
|
38
|
+
const named = lifecycleRemoteOverride(env);
|
|
39
|
+
if (named !== undefined) {
|
|
40
|
+
if (names.includes(named)) return { remote: named };
|
|
41
|
+
const have = names.length === 0 ? "this repository has no remotes" : `this repository's remotes are ${names.join(", ")}`;
|
|
42
|
+
return { failed: true, stderr: `${LIFECYCLE_REMOTE_ENV} names the remote "${named}", and ${have}`, named };
|
|
43
|
+
}
|
|
44
|
+
if (names.length === 0) return null;
|
|
45
|
+
return { remote: names.includes("origin") ? "origin" : names[0] };
|
|
46
|
+
}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
import { mkdtempSync, realpathSync, rmSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { afterAll, describe, expect, test } from "vitest";
|
|
6
|
+
import type { LedgerReadPort } from "./ledger-port";
|
|
7
|
+
import { writeBlobToPath } from "./git";
|
|
8
|
+
import { clearMemberLedgerCache } from "./member-ledger";
|
|
9
|
+
import { PLAN_STALE_FLOOR, PLAN_STALE_SCHEMA_ID, planStaleness } from "./plan-stale";
|
|
10
|
+
import { loadQueryData } from "./query";
|
|
11
|
+
import { NEXT_RELEASE } from "../release-floors";
|
|
12
|
+
import { planStaleDocument } from "../cli/handlers/plan";
|
|
13
|
+
import { loadAjv2020 } from "../workspace/ajv2020";
|
|
14
|
+
import planStaleSchema from "../workspace/plan-stale.schema.json";
|
|
15
|
+
|
|
16
|
+
/** A port over a map of `<dir>/<file>` to text, the shape `gitLedgerReadPort` reads. */
|
|
17
|
+
function port(files: Record<string, string>): LedgerReadPort {
|
|
18
|
+
const dirs = (): string[] => [...new Set(Object.keys(files).map((p) => p.slice(0, p.indexOf("/"))))];
|
|
19
|
+
return {
|
|
20
|
+
read: async (dir, file) => files[`${dir}/${file}`] ?? null,
|
|
21
|
+
list: async (dir) => Object.keys(files).filter((p) => p.startsWith(`${dir}/`)).map((p) => p.slice(dir.length + 1)),
|
|
22
|
+
trees: async () => dirs(),
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const lines = (...rows: unknown[]): string => rows.map((r) => JSON.stringify(r)).join("\n") + "\n";
|
|
27
|
+
const P1 = `jcs1-sha256:${"1".repeat(64)}`;
|
|
28
|
+
const P2 = `jcs1-sha256:${"2".repeat(64)}`;
|
|
29
|
+
const P3 = `jcs1-sha256:${"3".repeat(64)}`;
|
|
30
|
+
|
|
31
|
+
/** The run that planned P1 reached gate `prod` of op `deploy` in env `prod` at 10:00. */
|
|
32
|
+
const planned = {
|
|
33
|
+
"_gates/deploy.jsonl": lines({ version: 1, kind: "pending", op: "deploy", gate: "prod", runId: "run-plan", gitSha: "a".repeat(40), timestamp: "2026-11-01T10:00:00Z", expiresAt: "2026-11-08T10:00:00Z", planDigest: P1 }),
|
|
34
|
+
"prod/runs__deploy.jsonl": lines({ version: 1, id: "run-plan", op: "deploy", env: "prod", started: "2026-11-01T09:59:00Z", ended: "2026-11-01T10:00:00Z", status: "gated", labels: { Env: "prod" }, outcomes: {}, phases: [], gate: { name: "prod", since: "2026-11-01T10:00:00Z" } }),
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const applied = (op: string, planDigest: string, timestamp: string, runId: string) => ({ version: 1, kind: "applied", op, gate: "prod", planDigest, approvedAt: timestamp, approvedBy: "carol", timestamp, runId, commit: "b".repeat(40) });
|
|
38
|
+
const deploy = (env: string, timestamp: string, actor: string, runId: string) => ({ version: 1, component: "api", env, digest: "sha256:aa", gitSha: "c".repeat(40), runId, runOrigin: { forge: "github", url: `https://example.test/run/${runId}` }, timestamp, actor, approver: "dave" });
|
|
39
|
+
|
|
40
|
+
async function stale(files: Record<string, string>, digest = P1, member: string | null = null) {
|
|
41
|
+
const { data } = await loadQueryData([{ member, port: port(files) }]);
|
|
42
|
+
return planStaleness(data, digest, "0.0.0-test");
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
describe("chant plan stale (#4648)", () => {
|
|
46
|
+
test("an apply under an approval from another run after the plan makes it stale, and names who, when and which run", async () => {
|
|
47
|
+
const doc = await stale({
|
|
48
|
+
...planned,
|
|
49
|
+
"_gates/deploy/applied.jsonl": lines(applied("deploy", P2, "2026-11-01T11:00:00Z", "run-laptop"), { version: 1, kind: "finished", op: "deploy", gate: "prod", planDigest: P2, applied: "2026-11-01T11:00:00Z", result: "failed", timestamp: "2026-11-01T11:05:00Z" }),
|
|
50
|
+
});
|
|
51
|
+
expect(doc).toMatchObject({
|
|
52
|
+
$schema: PLAN_STALE_SCHEMA_ID,
|
|
53
|
+
contract: 1,
|
|
54
|
+
digest: P1,
|
|
55
|
+
plan: { recordedAt: "2026-11-01T10:00:00Z", runId: "run-plan", gitSha: "a".repeat(40), targets: [{ member: null, op: "deploy", gate: "prod", environment: "prod" }] },
|
|
56
|
+
stale: true,
|
|
57
|
+
});
|
|
58
|
+
expect(!("error" in doc) && doc.applies).toEqual([
|
|
59
|
+
{ source: "applies", member: null, op: "deploy", gate: "prod", environment: null, component: null, planDigest: P2, at: "2026-11-01T11:00:00Z", actor: null, approvedBy: "carol", runId: "run-laptop", runUrl: null, gitSha: "b".repeat(40), result: "failed" },
|
|
60
|
+
]);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
test("a deploy to the plan's environment after it makes it stale", async () => {
|
|
64
|
+
const doc = await stale({ ...planned, "prod/releases.jsonl": lines(deploy("prod", "2026-11-01T12:00:00Z", "laptop-alex", "ci-77")) });
|
|
65
|
+
expect(doc).toMatchObject({ stale: true, applies: [{ source: "deploys", environment: "prod", component: "api", actor: "laptop-alex", approvedBy: "dave", runId: "ci-77", runUrl: "https://example.test/run/ci-77", at: "2026-11-01T12:00:00Z" }] });
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
test("applies to unrelated targets, before the plan, or of the plan itself leave it current", async () => {
|
|
69
|
+
const doc = await stale({
|
|
70
|
+
...planned,
|
|
71
|
+
// Another op, and this op before the plan was recorded.
|
|
72
|
+
"_gates/migrate/applied.jsonl": lines(applied("migrate", P3, "2026-11-01T11:00:00Z", "run-other")),
|
|
73
|
+
"_gates/deploy/applied.jsonl": lines(applied("deploy", P2, "2026-11-01T09:00:00Z", "run-earlier"), applied("deploy", P1, "2026-11-01T10:30:00Z", "run-this-plan")),
|
|
74
|
+
// Another environment, and this one before the plan.
|
|
75
|
+
"staging/releases.jsonl": lines(deploy("staging", "2026-11-01T12:00:00Z", "ci", "ci-1")),
|
|
76
|
+
"prod/releases.jsonl": lines(deploy("prod", "2026-11-01T09:30:00Z", "ci", "ci-0")),
|
|
77
|
+
});
|
|
78
|
+
expect(doc).toMatchObject({ stale: false, applies: [] });
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
test("another member's applies don't count", async () => {
|
|
82
|
+
const { data } = await loadQueryData([
|
|
83
|
+
{ member: "app", port: port(planned) },
|
|
84
|
+
{ member: "db", port: port({ "_gates/deploy/applied.jsonl": lines(applied("deploy", P2, "2026-11-01T11:00:00Z", "r")), "prod/releases.jsonl": lines(deploy("prod", "2026-11-01T12:00:00Z", "ci", "c")) }) },
|
|
85
|
+
]);
|
|
86
|
+
expect(planStaleness(data, P1, "0.0.0-test")).toMatchObject({ plan: { targets: [{ member: "app" }] }, stale: false, applies: [] });
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test("a component gate's own environment is the target, and a plan nobody recorded is null", async () => {
|
|
90
|
+
const component = {
|
|
91
|
+
"_gates/api.jsonl": lines({ version: 1, kind: "pending", op: "api", gate: "release", environment: "staging", timestamp: "2026-11-01T10:00:00Z", expiresAt: "2026-11-08T10:00:00Z", planDigest: P1 }),
|
|
92
|
+
"staging/releases.jsonl": lines(deploy("staging", "2026-11-01T10:01:00Z", "ci", "ci-2")),
|
|
93
|
+
};
|
|
94
|
+
expect(await stale(component)).toMatchObject({ plan: { targets: [{ op: "api", environment: "staging" }] }, stale: true, applies: [{ runId: "ci-2" }] });
|
|
95
|
+
expect(await stale(component, P3)).toMatchObject({ plan: null, stale: null, applies: [] });
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
test("its floor is the next release", () => {
|
|
99
|
+
expect(PLAN_STALE_FLOOR).toBe(NEXT_RELEASE);
|
|
100
|
+
expect(planStaleSchema.description).toContain(`written by chant ${NEXT_RELEASE} and newer`);
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
describe("chant plan stale on a real ledger (#4648)", () => {
|
|
105
|
+
const scratch: string[] = [];
|
|
106
|
+
afterAll(() => {
|
|
107
|
+
for (const d of scratch) rmSync(d, { recursive: true, force: true });
|
|
108
|
+
});
|
|
109
|
+
const git = (args: string[], cwd: string): void => {
|
|
110
|
+
const r = spawnSync("git", args, { cwd, encoding: "utf-8" });
|
|
111
|
+
if (r.status !== 0) throw new Error(`git ${args.join(" ")}: ${r.stderr}`);
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
test("reads the local chant/lifecycle branch, and the document matches its schema", async () => {
|
|
115
|
+
clearMemberLedgerCache();
|
|
116
|
+
const dir = realpathSync(mkdtempSync(join(tmpdir(), "chant-plan-stale-")));
|
|
117
|
+
scratch.push(dir);
|
|
118
|
+
git(["init", "-q", "-b", "main"], dir);
|
|
119
|
+
git(["config", "user.email", "test@chant.dev"], dir);
|
|
120
|
+
git(["config", "user.name", "Test"], dir);
|
|
121
|
+
writeFileSync(join(dir, "README.md"), "fixture\n");
|
|
122
|
+
git(["add", "README.md"], dir);
|
|
123
|
+
git(["commit", "-q", "-m", "init"], dir);
|
|
124
|
+
for (const [path, text] of Object.entries(planned)) {
|
|
125
|
+
const at = path.lastIndexOf("/");
|
|
126
|
+
await writeBlobToPath(path.slice(0, at), path.slice(at + 1), text, "fixture", { cwd: dir });
|
|
127
|
+
}
|
|
128
|
+
const Ajv = loadAjv2020();
|
|
129
|
+
const validate = new Ajv({ strict: false, allErrors: true }).compile(planStaleSchema) as ((d: unknown) => boolean) & { errors?: unknown };
|
|
130
|
+
|
|
131
|
+
const current = await planStaleDocument({ cwd: dir, digest: P1 });
|
|
132
|
+
expect(current).toMatchObject({ stale: false, applies: [] });
|
|
133
|
+
expect(validate(current), JSON.stringify(validate.errors)).toBe(true);
|
|
134
|
+
|
|
135
|
+
await writeBlobToPath("_gates/deploy", "applied.jsonl", lines(applied("deploy", P2, "2026-11-01T11:00:00Z", "run-other")), "Applied under prod", { cwd: dir });
|
|
136
|
+
const staled = await planStaleDocument({ cwd: dir, digest: P1 });
|
|
137
|
+
expect(staled).toMatchObject({ stale: true, applies: [{ source: "applies", runId: "run-other", approvedBy: "carol" }] });
|
|
138
|
+
expect(validate(staled), JSON.stringify(validate.errors)).toBe(true);
|
|
139
|
+
|
|
140
|
+
const unknown = await planStaleDocument({ cwd: dir, digest: P3 });
|
|
141
|
+
expect(unknown).toMatchObject({ plan: null, stale: null });
|
|
142
|
+
expect(validate(unknown), JSON.stringify(validate.errors)).toBe(true);
|
|
143
|
+
|
|
144
|
+
const nowhere = realpathSync(mkdtempSync(join(tmpdir(), "chant-plan-stale-nogit-")));
|
|
145
|
+
scratch.push(nowhere);
|
|
146
|
+
const failed = await planStaleDocument({ cwd: nowhere, digest: P1 });
|
|
147
|
+
expect(failed).toMatchObject({ error: { code: "not-a-git-repository" } });
|
|
148
|
+
expect(validate(failed), JSON.stringify(validate.errors)).toBe(true);
|
|
149
|
+
});
|
|
150
|
+
});
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `chant plan stale <digest> --json` (#4648): whether anything applied to a
|
|
3
|
+
* plan's targets after the plan was recorded, so a platform can ask before it
|
|
4
|
+
* puts the plan in front of a person to approve. chant checks the digest
|
|
5
|
+
* again when the approved run applies; this read answers the same question
|
|
6
|
+
* earlier, and names the applies.
|
|
7
|
+
*
|
|
8
|
+
* The plan is the pending gate facts that record `digest` (`_gates/<op>.jsonl`).
|
|
9
|
+
* It was recorded at the earliest of them. Its targets are each fact's
|
|
10
|
+
* member, op and environment: the fact's own `environment` (a component
|
|
11
|
+
* gate's), else the `env` of the run that reached the gate.
|
|
12
|
+
*
|
|
13
|
+
* An apply touches a target when it was recorded after the plan and:
|
|
14
|
+
*
|
|
15
|
+
* - it is an apply under an approval (`_gates/<op>/applied.jsonl`, the
|
|
16
|
+
* `applies` table) of the same member and op, for another plan; or
|
|
17
|
+
* - it is a deploy record (`<env>/releases.jsonl`, the `deploys` table) of
|
|
18
|
+
* the same member and environment, of any component.
|
|
19
|
+
*
|
|
20
|
+
* An apply of this same plan doesn't make it stale. Neither does anything in
|
|
21
|
+
* another member or environment.
|
|
22
|
+
*
|
|
23
|
+
* Pure over the rows `chant query` loads (`./query.ts` {@link loadQueryData}):
|
|
24
|
+
* the caller reads the ledgers, and this reads no git and writes nothing.
|
|
25
|
+
*/
|
|
26
|
+
import { samePlanDigest } from "./plan-digest";
|
|
27
|
+
import type { QueryData, QueryRow } from "./query";
|
|
28
|
+
import { NEXT_RELEASE } from "../release-floors";
|
|
29
|
+
|
|
30
|
+
/** The version of the `plan-stale` document this chant writes. */
|
|
31
|
+
export const PLAN_STALE_CONTRACT_VERSION = 1;
|
|
32
|
+
|
|
33
|
+
/** `$id` of the JSON Schema for `chant plan stale --json`. */
|
|
34
|
+
export const PLAN_STALE_SCHEMA_ID = "https://intentius.io/chant/schemas/workspace/plan-stale/v1/plan-stale.schema.json";
|
|
35
|
+
|
|
36
|
+
/** The first chant that answers `chant plan stale` (#4648). */
|
|
37
|
+
export const PLAN_STALE_FLOOR = NEXT_RELEASE;
|
|
38
|
+
|
|
39
|
+
/** Why the read stopped. Closed; each code is in `../workspace/reason-codes.ts`. */
|
|
40
|
+
export const PLAN_STALE_ERROR_CODES = ["not-a-git-repository", "ledger-unreadable"] as const;
|
|
41
|
+
|
|
42
|
+
/** One gate the plan waits at, and what it covers. */
|
|
43
|
+
export interface PlanStaleTarget {
|
|
44
|
+
member: string | null;
|
|
45
|
+
op: string;
|
|
46
|
+
gate: string;
|
|
47
|
+
/** The environment the plan is for, or null when neither the fact nor its run names one. */
|
|
48
|
+
environment: string | null;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** One apply that touched a target after the plan was recorded. */
|
|
52
|
+
export interface PlanStaleApply {
|
|
53
|
+
/** Where it was read: `applies` for an apply under an approval, `deploys` for a deploy record. */
|
|
54
|
+
source: "applies" | "deploys";
|
|
55
|
+
member: string | null;
|
|
56
|
+
/** The op it applied under; null for a deploy record. */
|
|
57
|
+
op: string | null;
|
|
58
|
+
gate: string | null;
|
|
59
|
+
/** The environment; null for an apply under an approval, whose line names none. */
|
|
60
|
+
environment: string | null;
|
|
61
|
+
/** The component a deploy record deployed; null for an apply under an approval. */
|
|
62
|
+
component: string | null;
|
|
63
|
+
/** The plan it applied; null for a deploy record. */
|
|
64
|
+
planDigest: string | null;
|
|
65
|
+
/** When it was recorded. */
|
|
66
|
+
at: string;
|
|
67
|
+
/** Who ran it: a deploy record's `actor`; null for an apply under an approval, which names only its approver. */
|
|
68
|
+
actor: string | null;
|
|
69
|
+
approvedBy: string | null;
|
|
70
|
+
runId: string | null;
|
|
71
|
+
runUrl: string | null;
|
|
72
|
+
gitSha: string | null;
|
|
73
|
+
/** How an apply under an approval ended: `applied`, `failed`, or null while it has no finished line. Null for a deploy record. */
|
|
74
|
+
result: string | null;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export type PlanStaleDocument =
|
|
78
|
+
| {
|
|
79
|
+
$schema: string;
|
|
80
|
+
contract: number;
|
|
81
|
+
chant: string;
|
|
82
|
+
digest: string;
|
|
83
|
+
/** The plan, or null when no pending gate fact records the digest. */
|
|
84
|
+
plan: { recordedAt: string; runId: string | null; gitSha: string | null; targets: PlanStaleTarget[] } | null;
|
|
85
|
+
/** Whether any apply touched a target after the plan was recorded; null when there is no plan. */
|
|
86
|
+
stale: boolean | null;
|
|
87
|
+
/** Those applies, oldest first. */
|
|
88
|
+
applies: PlanStaleApply[];
|
|
89
|
+
}
|
|
90
|
+
| { $schema: string; contract: number; chant: string; error: { code: (typeof PLAN_STALE_ERROR_CODES)[number]; message: string } };
|
|
91
|
+
|
|
92
|
+
const text = (v: QueryRow[string] | undefined): string | null => (typeof v === "string" ? v : null);
|
|
93
|
+
const time = (iso: string): number => new Date(iso).getTime();
|
|
94
|
+
|
|
95
|
+
/** The document's head: `$schema`, `contract` and `chant`. */
|
|
96
|
+
export function planStaleHead(chant: string): { $schema: string; contract: number; chant: string } {
|
|
97
|
+
return { $schema: PLAN_STALE_SCHEMA_ID, contract: PLAN_STALE_CONTRACT_VERSION, chant };
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** Whether `digest`'s plan is stale, read from the rows of the `gates`, `runs`, `applies` and `deploys` tables. */
|
|
101
|
+
export function planStaleness(data: Pick<QueryData, "gates" | "runs" | "applies" | "deploys">, digest: string, chant: string): PlanStaleDocument {
|
|
102
|
+
const head = planStaleHead(chant);
|
|
103
|
+
const facts = data.gates.filter((g) => g.kind === "pending" && samePlanDigest(text(g.plan_digest) ?? undefined, digest) && typeof g.timestamp === "string");
|
|
104
|
+
if (facts.length === 0) return { ...head, digest, plan: null, stale: null, applies: [] };
|
|
105
|
+
|
|
106
|
+
const first = facts.reduce((a, b) => (time(String(b.timestamp)) < time(String(a.timestamp)) ? b : a));
|
|
107
|
+
const recordedAt = String(first.timestamp);
|
|
108
|
+
const since = time(recordedAt);
|
|
109
|
+
const runEnv = (member: string | null, runId: string | null): string | null => {
|
|
110
|
+
if (runId === null) return null;
|
|
111
|
+
const run = data.runs.find((r) => r.member === member && r.id === runId);
|
|
112
|
+
return run ? text(run.env) : null;
|
|
113
|
+
};
|
|
114
|
+
const targets: PlanStaleTarget[] = [];
|
|
115
|
+
for (const f of facts) {
|
|
116
|
+
const member = text(f.member);
|
|
117
|
+
const target = { member, op: String(f.op), gate: String(f.gate), environment: text(f.environment) ?? runEnv(member, text(f.run_id)) };
|
|
118
|
+
if (!targets.some((t) => t.member === target.member && t.op === target.op && t.gate === target.gate && t.environment === target.environment)) targets.push(target);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const applies: PlanStaleApply[] = [];
|
|
122
|
+
for (const a of data.applies) {
|
|
123
|
+
const at = text(a.timestamp);
|
|
124
|
+
if (at === null || !(time(at) > since)) continue;
|
|
125
|
+
if (samePlanDigest(text(a.plan_digest) ?? undefined, digest)) continue;
|
|
126
|
+
if (!targets.some((t) => t.member === text(a.member) && t.op === a.op)) continue;
|
|
127
|
+
applies.push({
|
|
128
|
+
source: "applies",
|
|
129
|
+
member: text(a.member),
|
|
130
|
+
op: text(a.op),
|
|
131
|
+
gate: text(a.gate),
|
|
132
|
+
environment: null,
|
|
133
|
+
component: null,
|
|
134
|
+
planDigest: text(a.plan_digest),
|
|
135
|
+
at,
|
|
136
|
+
actor: null,
|
|
137
|
+
approvedBy: text(a.approved_by),
|
|
138
|
+
runId: text(a.run_id),
|
|
139
|
+
runUrl: null,
|
|
140
|
+
gitSha: text(a.git_sha),
|
|
141
|
+
result: text(a.result),
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
for (const d of data.deploys) {
|
|
145
|
+
const at = text(d.timestamp);
|
|
146
|
+
if (at === null || !(time(at) > since)) continue;
|
|
147
|
+
if (!targets.some((t) => t.environment !== null && t.member === text(d.member) && t.environment === d.env)) continue;
|
|
148
|
+
applies.push({
|
|
149
|
+
source: "deploys",
|
|
150
|
+
member: text(d.member),
|
|
151
|
+
op: null,
|
|
152
|
+
gate: null,
|
|
153
|
+
environment: text(d.env),
|
|
154
|
+
component: text(d.component),
|
|
155
|
+
planDigest: null,
|
|
156
|
+
at,
|
|
157
|
+
actor: text(d.actor),
|
|
158
|
+
approvedBy: text(d.approver),
|
|
159
|
+
runId: text(d.run_id),
|
|
160
|
+
runUrl: text(d.run_url),
|
|
161
|
+
gitSha: text(d.git_sha),
|
|
162
|
+
result: null,
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
applies.sort((x, y) => time(x.at) - time(y.at));
|
|
166
|
+
return {
|
|
167
|
+
...head,
|
|
168
|
+
digest,
|
|
169
|
+
plan: { recordedAt, runId: text(first.run_id), gitSha: text(first.git_sha), targets },
|
|
170
|
+
stale: applies.length > 0,
|
|
171
|
+
applies,
|
|
172
|
+
};
|
|
173
|
+
}
|