architecture-harness 0.26.0 → 0.27.1
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/package.json +3 -2
- package/src/cli/ask.mjs +3 -1
- package/src/cli/ask.test.mjs +17 -1
- package/src/cli/bash-command-guard.mjs +37 -5
- package/src/cli/bash-command-guard.test.mjs +18 -0
- package/src/cli/check-full.test.mjs +4 -3
- package/src/cli/check.mjs +1 -1
- package/src/cli/ci-template.test.mjs +37 -2
- package/src/cli/cli.test.mjs +8 -0
- package/src/cli/codex-implement.mjs +5 -4
- package/src/cli/codex-implement.test.mjs +93 -0
- package/src/cli/commit-msg.mjs +4 -0
- package/src/cli/commit-msg.test.mjs +10 -0
- package/src/cli/fleet-assign.mjs +46 -0
- package/src/cli/fleet-assign.test.mjs +114 -0
- package/src/cli/fleet-github.mjs +124 -0
- package/src/cli/fleet-github.test.mjs +243 -0
- package/src/cli/fleet-message.mjs +50 -0
- package/src/cli/fleet-message.test.mjs +72 -0
- package/src/cli/fleet-node.mjs +555 -0
- package/src/cli/fleet-node.test.mjs +1284 -0
- package/src/cli/fleet-platform.mjs +200 -0
- package/src/cli/fleet-platform.test.mjs +316 -0
- package/src/cli/fleet-state.mjs +75 -0
- package/src/cli/fleet-state.test.mjs +114 -0
- package/src/cli/fleet-talk.mjs +143 -0
- package/src/cli/fleet-talk.test.mjs +178 -0
- package/src/cli/fleet.mjs +142 -13
- package/src/cli/fleet.test.mjs +314 -11
- package/src/cli/legs.mjs +3 -3
- package/src/cli/legs.test.mjs +24 -1
- package/src/cli/merge-guard.mjs +8 -6
- package/src/cli/merge-guard.test.mjs +38 -0
- package/src/cli/platform-risk.mjs +95 -1
- package/src/cli/platform-risk.test.mjs +185 -0
- package/src/cli/qc.mjs +6 -0
- package/src/cli/repl-commands.mjs +168 -0
- package/src/cli/repl-commands.test.mjs +133 -0
- package/src/cli/role-line.mjs +49 -0
- package/src/cli/role-line.test.mjs +55 -0
- package/src/cli/watch.mjs +35 -2
- package/src/cli/watch.test.mjs +106 -0
- package/src/cli/workflow-hooks.test.mjs +13 -4
- package/src/config.mjs +2 -2
package/src/cli/fleet.mjs
CHANGED
|
@@ -1,15 +1,27 @@
|
|
|
1
1
|
// A static fleet shares node settings across PCs; only the local self id differs.
|
|
2
|
-
import { createHash } from "node:crypto";
|
|
2
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
3
3
|
import { closeSync, fchmodSync, mkdirSync, openSync, readFileSync, writeFileSync } from "node:fs";
|
|
4
|
+
import { isIP } from "node:net";
|
|
4
5
|
import os from "node:os";
|
|
5
6
|
import path from "node:path";
|
|
7
|
+
import { fileURLToPath } from "node:url";
|
|
6
8
|
import { load } from "../config.mjs";
|
|
7
9
|
import { checkoutRootOf } from "./checkout-root.mjs";
|
|
8
10
|
import { runGh } from "./gh-run.mjs";
|
|
9
11
|
import { gitOut, originOf } from "./git-read.mjs";
|
|
12
|
+
import { sendPushed, validPlatformBranch } from "./fleet-platform.mjs";
|
|
13
|
+
import { TALK_VERBS, parseTalkArgs, runTalk } from "./fleet-talk.mjs";
|
|
14
|
+
import { highestTerm, latestLeader, readState, writeState } from "./fleet-state.mjs";
|
|
15
|
+
import { confirmLeader, nodeStatus, serveNode, startNode, stopNode } from "./fleet-node.mjs";
|
|
10
16
|
|
|
11
17
|
const SIZES = ["S", "M", "L"];
|
|
12
|
-
const USAGE = "usage: qc fleet init --self <id> --nodes <id:weight:size,...> [--force] | next [--repo <o/r>] [--all] | status [--repo <o/r>]";
|
|
18
|
+
const USAGE = "usage: qc fleet init --self <id> --nodes <id:weight:size,...> [--secret <hex>] [--addresses <id@host:port,...>] [--force] | next [--repo <o/r>] [--all] | status [--repo <o/r>] | plan <issue> --file <path> | ask <issue> --file <path> | approve <message-id> [--changes] [--file <path>] | answer <message-id> --file <path> | show <message-id> | start [--leader] | serve [--leader] | stop | confirm-leader | progress <issue> --step plan|code|review|merge [--branch <b>] [--head <sha40>] | owns <issue> | pushed <issue> --branch <b> --head <sha40>";
|
|
19
|
+
|
|
20
|
+
function validAddress(address) {
|
|
21
|
+
if (typeof address !== "string" || !address || address.trim() !== address || address === "0.0.0.0") return false;
|
|
22
|
+
if (isIP(address) === 6) return new URL(`http://[${address}]/`).hostname !== "[::]";
|
|
23
|
+
return address.length <= 253 && /^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)*[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.?$/i.test(address);
|
|
24
|
+
}
|
|
13
25
|
|
|
14
26
|
/** The fleet settings live on this PC, outside the repository. */
|
|
15
27
|
export function fleetPath(env = process.env, platform = process.platform, home = os.homedir()) {
|
|
@@ -34,10 +46,17 @@ export function validateFleet(value) {
|
|
|
34
46
|
if (!SIZES.includes(maxSize)) throw new Error(`${field}.maxSize must be S, M or L`);
|
|
35
47
|
const active = node.active === undefined ? true : node.active;
|
|
36
48
|
if (typeof active !== "boolean") throw new Error(`${field}.active must be a boolean`);
|
|
37
|
-
|
|
49
|
+
if (node.address !== undefined && !validAddress(node.address)) throw new Error(`${field}.address must be a host other than 0.0.0.0 or ::`);
|
|
50
|
+
if (node.port !== undefined && (!Number.isInteger(node.port) || node.port < 1024 || node.port > 65535)) throw new Error(`${field}.port must be an integer from 1024 to 65535`);
|
|
51
|
+
return { id: node.id, weight: node.weight, maxSize, active,
|
|
52
|
+
...(node.address === undefined ? {} : { address: node.address }),
|
|
53
|
+
...(node.port === undefined ? {} : { port: node.port }) };
|
|
38
54
|
});
|
|
39
55
|
if (typeof value.self !== "string" || !seen.has(value.self)) throw new Error("self must name a node");
|
|
40
|
-
|
|
56
|
+
if (value.secret !== undefined && (typeof value.secret !== "string" || value.secret.length < 64 || /[^0-9a-f]/i.test(value.secret))) throw new Error("secret must be hex with at least 64 characters");
|
|
57
|
+
const leaders = value.leaders === undefined ? nodes.map(node => node.id) : value.leaders;
|
|
58
|
+
if (!Array.isArray(leaders) || !leaders.length || leaders.some(id => !seen.has(id)) || new Set(leaders).size !== leaders.length) throw new Error("leaders must be a nonempty list of unique node IDs");
|
|
59
|
+
return { self: value.self, nodes, leaders: [...leaders], ...(value.secret === undefined ? {} : { secret: value.secret }) };
|
|
41
60
|
}
|
|
42
61
|
|
|
43
62
|
/** Weighted rendezvous hashing keeps other owners stable when a PC goes inactive. */
|
|
@@ -66,6 +85,22 @@ function readList(gh, args, cwd, valid) {
|
|
|
66
85
|
return list;
|
|
67
86
|
}
|
|
68
87
|
|
|
88
|
+
/** Read this node's open work without changing the serve loop's saved snapshot. */
|
|
89
|
+
export function readOpenAssignments({ root, fleet, repo, gh = runGh, warn = () => {} }) {
|
|
90
|
+
const assignments = readState(root, "assignments");
|
|
91
|
+
const held = Object.entries(assignments).filter(([, row]) => row.repo === repo && row.node === fleet.self);
|
|
92
|
+
if (!held.length) return [];
|
|
93
|
+
let open;
|
|
94
|
+
try {
|
|
95
|
+
open = new Set(readList(gh, ["issue", "list", "--state", "open", "--limit", "500", "--json", "number", "--repo", repo], root,
|
|
96
|
+
row => Number.isSafeInteger(row?.number) && row.number > 0).map(row => row.number));
|
|
97
|
+
} catch (error) {
|
|
98
|
+
warn(`fleet node: ${error.message}`);
|
|
99
|
+
return held.map(([, row]) => row);
|
|
100
|
+
}
|
|
101
|
+
return held.filter(([, row]) => open.has(row.issue)).map(([, row]) => row);
|
|
102
|
+
}
|
|
103
|
+
|
|
69
104
|
/** Read free open issues and count those already taken by a PR or an origin branch. */
|
|
70
105
|
export function freeIssues(repo, cwd, { gh = runGh, git = gitOut, holdLabel = "hold", remote = "origin" } = {}) {
|
|
71
106
|
const issues = readList(gh, ["issue", "list", "--state", "open", "--limit", "500", "--json", "number,labels,createdAt", "--repo", repo], cwd,
|
|
@@ -97,21 +132,35 @@ export function freeIssues(repo, cwd, { gh = runGh, git = gitOut, holdLabel = "h
|
|
|
97
132
|
}
|
|
98
133
|
|
|
99
134
|
function parseArgs(argv) {
|
|
100
|
-
const [verb, ...
|
|
101
|
-
|
|
102
|
-
|
|
135
|
+
const [verb, ...args] = argv;
|
|
136
|
+
if (TALK_VERBS.includes(verb)) return parseTalkArgs(verb, args);
|
|
137
|
+
const hasIssue = ["progress", "owns", "pushed"].includes(verb);
|
|
138
|
+
const rest = hasIssue ? args.slice(1) : args;
|
|
139
|
+
const allowed = verb === "init" ? ["--self", "--nodes", "--secret", "--addresses", "--force"] : verb === "next" ? ["--repo", "--all"] : verb === "status" ? ["--repo"] : ["start", "serve"].includes(verb) ? ["--leader"] : verb === "pushed" ? ["--branch", "--head"] : verb === "progress" ? ["--step", "--branch", "--head"] : ["stop", "owns", "confirm-leader"].includes(verb) ? [] : null;
|
|
140
|
+
if (!allowed) throw new Error(USAGE);
|
|
103
141
|
const options = { verb };
|
|
142
|
+
if (hasIssue) {
|
|
143
|
+
if (!/^[1-9][0-9]*$/.test(args[0] ?? "") || !Number.isSafeInteger(Number(args[0]))) throw new Error(`${verb} needs a positive issue number`);
|
|
144
|
+
options.issue = Number(args[0]);
|
|
145
|
+
}
|
|
104
146
|
for (let i = 0; i < rest.length; i++) {
|
|
105
147
|
const flag = rest[i];
|
|
106
148
|
if (!allowed.includes(flag)) throw new Error(`unknown argument ${flag}`);
|
|
107
149
|
if (Object.hasOwn(options, flag)) throw new Error(`duplicate argument ${flag}`);
|
|
108
|
-
if (
|
|
150
|
+
if (["--force", "--all", "--leader"].includes(flag)) options[flag] = true;
|
|
109
151
|
else {
|
|
110
152
|
if (!rest[i + 1] || rest[i + 1].startsWith("--")) throw new Error(`${flag} needs a value`);
|
|
111
153
|
options[flag] = rest[++i];
|
|
112
154
|
}
|
|
113
155
|
}
|
|
114
156
|
if (options["--repo"] && !/^[a-z0-9][a-z0-9-]*\/[a-z0-9_.-]+$/i.test(options["--repo"])) throw new Error("--repo needs owner/repository");
|
|
157
|
+
if (verb === "pushed" && (!validPlatformBranch(options["--branch"]) || !/^[0-9a-f]{40}$/i.test(options["--head"] ?? ""))) throw new Error("pushed needs --branch and a 40-character --head");
|
|
158
|
+
if (verb === "progress") {
|
|
159
|
+
if (!["plan", "code", "review", "merge"].includes(options["--step"])) throw new Error("--step needs plan, code, review or merge");
|
|
160
|
+
if (options["--head"] !== undefined && !/^[0-9a-f]{40}$/i.test(options["--head"])) throw new Error("--head needs a 40-character SHA");
|
|
161
|
+
const branch = options["--branch"];
|
|
162
|
+
if (branch !== undefined && (!branch || /[\s~^:?*\[\\\x00-\x1f\x7f]/.test(branch) || /(^[-/.]|[/.]$|\.\.|@\{|\/\/|\.lock(?:\/|$)|\/\.)/.test(branch) || branch === "@")) throw new Error("--branch needs a valid branch name");
|
|
163
|
+
}
|
|
115
164
|
return options;
|
|
116
165
|
}
|
|
117
166
|
|
|
@@ -122,7 +171,20 @@ function initialize(options, file, say) {
|
|
|
122
171
|
if (extra.length) throw new Error("--nodes needs id:weight:size entries");
|
|
123
172
|
return { id, weight: Number(weight), maxSize };
|
|
124
173
|
});
|
|
125
|
-
|
|
174
|
+
if (options["--addresses"]) {
|
|
175
|
+
const addressed = new Set();
|
|
176
|
+
for (const entry of options["--addresses"].split(",")) {
|
|
177
|
+
const match = /^([a-z0-9-]+)@(?:\[([^\]]+)\]|([^@:\s]+)):(\d+)$/.exec(entry);
|
|
178
|
+
if (!match) throw new Error("--addresses needs id@host:port entries (bracket IPv6 hosts)");
|
|
179
|
+
const [, id, ipv6, host, port] = match;
|
|
180
|
+
const node = nodes.find(node => node.id === id);
|
|
181
|
+
if (!node || addressed.has(id)) throw new Error("--addresses must name each known node at most once");
|
|
182
|
+
addressed.add(id);
|
|
183
|
+
Object.assign(node, { address: ipv6 ?? host, port: Number(port) });
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
const secret = options["--secret"] ?? randomBytes(32).toString("hex");
|
|
187
|
+
const fleet = validateFleet({ self: options["--self"], nodes, secret });
|
|
126
188
|
mkdirSync(path.dirname(file), { recursive: true });
|
|
127
189
|
let fd;
|
|
128
190
|
try {
|
|
@@ -136,11 +198,12 @@ function initialize(options, file, say) {
|
|
|
136
198
|
if (fd !== undefined) closeSync(fd);
|
|
137
199
|
}
|
|
138
200
|
say(`fleet initialized: ${file}`);
|
|
201
|
+
if (!options["--secret"]) say(`pass the same secret on the other PCs: --secret ${secret}`);
|
|
139
202
|
return 0;
|
|
140
203
|
}
|
|
141
204
|
|
|
142
|
-
/**
|
|
143
|
-
export function runFleet(argv, { cwd = process.cwd(), env = process.env, gh = runGh, git, origin = originOf, say = console.log, warn = console.error } = {}) {
|
|
205
|
+
/** Fleet verbs return exit codes; injected readers and node operations keep tests local. */
|
|
206
|
+
export function runFleet(argv, { cwd = process.cwd(), env = process.env, gh = runGh, git, origin = originOf, say = console.log, warn = console.error, node = {} } = {}) {
|
|
144
207
|
try {
|
|
145
208
|
const options = parseArgs(argv);
|
|
146
209
|
const file = fleetPath(env);
|
|
@@ -153,11 +216,56 @@ export function runFleet(argv, { cwd = process.cwd(), env = process.env, gh = ru
|
|
|
153
216
|
let value;
|
|
154
217
|
try { value = JSON.parse(text); } catch { throw new Error(`fleet file has invalid JSON: ${file}`); }
|
|
155
218
|
const fleet = validateFleet(value);
|
|
219
|
+
if (["start", "stop", "serve"].includes(options.verb)) {
|
|
220
|
+
return runNodeVerb(options, { root: cwd, fleet, env, gh, git, origin, say, warn, node }).catch(error => { warn(error.message); return 1; });
|
|
221
|
+
}
|
|
222
|
+
if (options.verb === "confirm-leader") {
|
|
223
|
+
const term = confirmLeader({ ...node, root: cwd, fleet });
|
|
224
|
+
say(`Leader ${fleet.self} term ${term}`);
|
|
225
|
+
return 0;
|
|
226
|
+
}
|
|
156
227
|
const localRepo = origin(cwd);
|
|
157
228
|
const repo = (options["--repo"] ?? localRepo)?.toLowerCase();
|
|
158
229
|
if (!repo) throw new Error("--repo is required when origin has no repository");
|
|
230
|
+
if (TALK_VERBS.includes(options.verb)) {
|
|
231
|
+
return runTalk(options, { root: cwd, fleet, repo, gh, say, node }).catch(error => { warn(error.message); return 1; });
|
|
232
|
+
}
|
|
233
|
+
const checkout = checkoutRootOf(cwd);
|
|
234
|
+
if (["progress", "owns", "pushed"].includes(options.verb)) {
|
|
235
|
+
if (!checkout) throw new Error("fleet work needs a git checkout");
|
|
236
|
+
const key = `${repo}#${options.issue}`, row = readState(checkout, "assignments")[key];
|
|
237
|
+
if (!row || row.node !== fleet.self) throw new Error(`no own assignment for #${options.issue}`);
|
|
238
|
+
if (["owns", "pushed"].includes(options.verb)) {
|
|
239
|
+
const leader = latestLeader(readState(checkout, "peers"), highestTerm(checkout));
|
|
240
|
+
if (!leader) throw new Error("no Leader heartbeat yet");
|
|
241
|
+
const listed = leader.assigned?.find(item => item.repo === repo && item.issue === options.issue);
|
|
242
|
+
if (!listed) throw new Error(`Leader does not list an assignment for #${options.issue}`);
|
|
243
|
+
if (listed.epoch !== row.epoch) throw new Error(`#${options.issue} epoch ${row.epoch} differs from Leader epoch ${listed.epoch}`);
|
|
244
|
+
if (options.verb === "pushed") return sendPushed({ root: checkout, fleet, repo, issue: options.issue, epoch: row.epoch,
|
|
245
|
+
branch: options["--branch"], head: options["--head"], node, say }).catch(error => { warn(error.message); return 1; });
|
|
246
|
+
return 0;
|
|
247
|
+
}
|
|
248
|
+
const work = readState(checkout, "work"), saved = work[key]?.epoch === row.epoch ? work[key] : {};
|
|
249
|
+
work[key] = { repo, issue: options.issue, epoch: row.epoch, step: options["--step"],
|
|
250
|
+
branch: options["--branch"] ?? saved.branch ?? null, head: options["--head"] ?? saved.head ?? null };
|
|
251
|
+
writeState(checkout, "work", work);
|
|
252
|
+
say(`#${options.issue} epoch=${row.epoch} step=${options["--step"]}`);
|
|
253
|
+
return 0;
|
|
254
|
+
}
|
|
255
|
+
if (options.verb === "next" && checkout) {
|
|
256
|
+
const assigned = readOpenAssignments({ root: checkout, fleet, repo, gh });
|
|
257
|
+
if (assigned.length) {
|
|
258
|
+
const plans = readState(checkout, "plans");
|
|
259
|
+
for (const row of assigned) {
|
|
260
|
+
const plan = plans[`${repo}#${row.issue}`]?.body;
|
|
261
|
+
const decision = plan?.epoch === row.epoch ? plan.approved ? " plan approved" : " plan needs changes" : "";
|
|
262
|
+
say(`#${row.issue} epoch=${row.epoch}${row.branch || row.head ? ` from ${row.branch ?? "?"}@${row.head ?? "?"}` : ""}${decision}`);
|
|
263
|
+
}
|
|
264
|
+
return 0;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
159
267
|
const remote = localRepo?.toLowerCase() === repo ? "origin" : `https://github.com/${repo}.git`;
|
|
160
|
-
const config = load(
|
|
268
|
+
const config = load(checkout ?? cwd);
|
|
161
269
|
const { free, taken } = freeIssues(repo, cwd, { gh, git, remote, holdLabel: config.merge.holdLabel });
|
|
162
270
|
const owned = free.map(row => ({ ...row, owner: ownerOf(repo, row.number, row.size, fleet.nodes) }));
|
|
163
271
|
if (options.verb === "next") {
|
|
@@ -172,9 +280,30 @@ export function runFleet(argv, { cwd = process.cwd(), env = process.env, gh = ru
|
|
|
172
280
|
}
|
|
173
281
|
const omitted = fleet.nodes.length > 14 ? `; ${fleet.nodes.length - 14} more nodes` : "";
|
|
174
282
|
say(`taken=${taken} unassigned=${owned.filter(row => row.owner === null).length}${omitted}`);
|
|
175
|
-
return
|
|
283
|
+
return nodeStatus({ ...node, root: cwd, fleet })
|
|
284
|
+
.then(status => { for (const line of status.split("\n")) say(line); return 0; })
|
|
285
|
+
.catch(error => { warn(error.message); return 1; });
|
|
176
286
|
} catch (error) {
|
|
177
287
|
warn(error.message);
|
|
178
288
|
return 1;
|
|
179
289
|
}
|
|
180
290
|
}
|
|
291
|
+
|
|
292
|
+
async function runNodeVerb(options, { root, fleet, env, gh, git, origin, say, warn, node }) {
|
|
293
|
+
const deps = { gh, git, ...node, root, fleet, env, leader: options["--leader"] === true };
|
|
294
|
+
if (options.verb === "serve") {
|
|
295
|
+
const kitRoot = fileURLToPath(new URL("../../../../", import.meta.url));
|
|
296
|
+
const kit = gitOut(kitRoot, "rev-parse", "HEAD")?.trim() || null;
|
|
297
|
+
const holdLabel = load(checkoutRootOf(root) ?? root).merge.holdLabel;
|
|
298
|
+
const server = await serveNode({ ...deps, holdLabel, repo: origin(root) ?? "qc/fleet", kit, warn });
|
|
299
|
+
return new Promise(resolve => server.once("close", () => resolve(0)));
|
|
300
|
+
}
|
|
301
|
+
if (options.verb === "start") {
|
|
302
|
+
const result = await startNode(deps);
|
|
303
|
+
say(`fleet node ${result.already ? "already running" : "started"}: ${fleet.self}`);
|
|
304
|
+
} else {
|
|
305
|
+
const result = await stopNode(deps);
|
|
306
|
+
say(`fleet node ${result.already ? "already stopped" : "stopped"}: ${fleet.self}`);
|
|
307
|
+
}
|
|
308
|
+
return 0;
|
|
309
|
+
}
|
package/src/cli/fleet.test.mjs
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { strict as assert } from "node:assert";
|
|
2
2
|
import { spawnSync } from "node:child_process";
|
|
3
3
|
import { createHash } from "node:crypto";
|
|
4
|
-
import { mkdtempSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
4
|
+
import { mkdirSync, mkdtempSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
5
5
|
import os from "node:os";
|
|
6
6
|
import path from "node:path";
|
|
7
7
|
import { test } from "node:test";
|
|
8
8
|
import { fileURLToPath } from "node:url";
|
|
9
9
|
import { fleetPath, validateFleet, ownerOf, freeIssues, runFleet } from "./fleet.mjs";
|
|
10
|
+
import { readState, writeState } from "./fleet-state.mjs";
|
|
10
11
|
|
|
11
12
|
const nodes = [
|
|
12
13
|
{ id: "a1", weight: 1, maxSize: "L", active: true },
|
|
@@ -18,6 +19,7 @@ const qc = fileURLToPath(new URL("./qc.mjs", import.meta.url));
|
|
|
18
19
|
|
|
19
20
|
function fixture(t) {
|
|
20
21
|
const cwd = mkdtempSync(path.join(os.tmpdir(), "qc-fleet-"));
|
|
22
|
+
mkdirSync(path.join(cwd, ".git"));
|
|
21
23
|
t.after(() => rmSync(cwd, { recursive: true, force: true, maxRetries: 10, retryDelay: 100 }));
|
|
22
24
|
const env = { QC_FLEET_FILE: path.join(cwd, "config", "fleet.json") };
|
|
23
25
|
const lines = [], errors = [];
|
|
@@ -25,7 +27,8 @@ function fixture(t) {
|
|
|
25
27
|
const run = (args, extra = {}) => {
|
|
26
28
|
lines.length = errors.length = 0;
|
|
27
29
|
const code = runFleet(args, { ...deps, ...extra });
|
|
28
|
-
|
|
30
|
+
const result = value => ({ code: value, lines: [...lines], errors: [...errors] });
|
|
31
|
+
return code instanceof Promise ? code.then(result) : result(code);
|
|
29
32
|
};
|
|
30
33
|
return { cwd, env, run };
|
|
31
34
|
}
|
|
@@ -34,7 +37,7 @@ function fakeGh(issues, prs = []) {
|
|
|
34
37
|
return (args, options) => {
|
|
35
38
|
assert.ok(options.cwd);
|
|
36
39
|
const expected = args[0] === "issue"
|
|
37
|
-
? ["issue", "list", "--state", "open", "--limit", "500", "--json", "number,labels,createdAt", "--repo", "o/r"]
|
|
40
|
+
? ["issue", "list", "--state", "open", "--limit", "500", "--json", args.includes("number") ? "number" : "number,labels,createdAt", "--repo", "o/r"]
|
|
38
41
|
: ["pr", "list", "--state", "open", "--limit", "200", "--json", "number,headRefName,body", "--repo", "o/r"];
|
|
39
42
|
assert.deepEqual(args, expected);
|
|
40
43
|
return JSON.stringify(args[0] === "issue" ? issues : prs);
|
|
@@ -43,7 +46,7 @@ function fakeGh(issues, prs = []) {
|
|
|
43
46
|
|
|
44
47
|
test("fleet validates every field, defaults, paths, missing file and unreadable inputs", t => {
|
|
45
48
|
const valid = { self: "a1", nodes: [{ id: "a1", weight: 1 }] };
|
|
46
|
-
assert.deepEqual(validateFleet(valid), { self: "a1", nodes: [nodes[0]] });
|
|
49
|
+
assert.deepEqual(validateFleet(valid), { self: "a1", nodes: [nodes[0]], leaders: ["a1"] });
|
|
47
50
|
for (const [value, field] of [
|
|
48
51
|
[null, "fleet"], [{}, "nodes"], [{ self: "a1", nodes: [] }, "self"],
|
|
49
52
|
[{ ...valid, self: "other" }, "self"],
|
|
@@ -79,6 +82,53 @@ test("fleet validates every field, defaults, paths, missing file and unreadable
|
|
|
79
82
|
assert.equal(f.run(["next"], { origin: () => null }).code, 1);
|
|
80
83
|
});
|
|
81
84
|
|
|
85
|
+
test("fleet settings accept optional secrets, ordered leaders and node endpoints, including old files", () => {
|
|
86
|
+
const old = { self: "a1", nodes };
|
|
87
|
+
assert.deepEqual(validateFleet(old), { ...old, leaders: ["a1", "b2", "c3"] });
|
|
88
|
+
for (const secret of ["ab".repeat(32), "AB".repeat(40), "a".repeat(65)]) {
|
|
89
|
+
const settings = { ...old, secret, leaders: ["c3", "a1"], nodes: [{ ...nodes[0], address: "localhost", port: 1024 }, { ...nodes[1], address: "::1", port: 65535 }, nodes[2]] };
|
|
90
|
+
assert.deepEqual(validateFleet(settings), settings);
|
|
91
|
+
}
|
|
92
|
+
assert.deepEqual(validateFleet({ ...old, nodes: [{ ...nodes[0], address: "node.local" }, { ...nodes[1], port: 1234 }, nodes[2]] }).nodes,
|
|
93
|
+
[{ ...nodes[0], address: "node.local" }, { ...nodes[1], port: 1234 }, nodes[2]]);
|
|
94
|
+
for (const [changes, field] of [
|
|
95
|
+
...[null, "", "a".repeat(63), "g".repeat(64), `${"a".repeat(64)}\n`, 42].map(secret => [{ secret }, "secret"]),
|
|
96
|
+
...[null, "a1", ["missing"], ["a1", "a1"], []].map(leaders => [{ leaders }, "leaders"]),
|
|
97
|
+
...["0.0.0.0", "::", "[::]", "0:0:0:0:0:0:0:0", "", " host ", "host/path", "host:8080", null, 42].map(address => [{ nodes: [{ ...nodes[0], address }] }, "address"]),
|
|
98
|
+
...[null, 1023, 65536, 1.5, "8080", NaN, Infinity].map(port => [{ nodes: [{ ...nodes[0], port }] }, "port"]),
|
|
99
|
+
]) assert.throws(() => validateFleet({ ...old, ...changes }), new RegExp(field));
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
test("init accepts explicit secrets and addresses and generates a reusable secret by default", t => {
|
|
103
|
+
const f = fixture(t);
|
|
104
|
+
const args = ["init", "--self", "a1", "--nodes", "a1:1:L,b2:1:S"];
|
|
105
|
+
const secret = "ab".repeat(32);
|
|
106
|
+
const explicit = f.run([...args, "--secret", secret, "--addresses", "a1@localhost:8080,b2@[::1]:9090"]);
|
|
107
|
+
assert.equal(explicit.code, 0, explicit.errors.join("\n"));
|
|
108
|
+
let saved = JSON.parse(readFileSync(f.env.QC_FLEET_FILE, "utf8"));
|
|
109
|
+
assert.equal(saved.secret, secret);
|
|
110
|
+
assert.deepEqual(saved.leaders, ["a1", "b2"]);
|
|
111
|
+
assert.deepEqual(saved.nodes.map(({ id, address, port }) => ({ id, address, port })), [{ id: "a1", address: "localhost", port: 8080 }, { id: "b2", address: "::1", port: 9090 }]);
|
|
112
|
+
assert.ok(!explicit.lines.join("\n").includes(secret));
|
|
113
|
+
for (const addresses of ["unknown@host:8080", "a1@host:8080,a1@other:8080", "a1@0.0.0.0:8080", "a1@[::]:8080", "a1@host:1023", "a1@host:65536", "a1@host:no", "a1@host:8e3", "a1@host", "a1@:8080", "a1@host:8080,", "a1@host:8080:90"]) {
|
|
114
|
+
const result = f.run([...args, "--force", "--addresses", addresses]);
|
|
115
|
+
assert.equal(result.code, 1, addresses);
|
|
116
|
+
assert.match(result.errors.join("\n"), /address|port/);
|
|
117
|
+
assert.equal(JSON.parse(readFileSync(f.env.QC_FLEET_FILE, "utf8")).secret, secret);
|
|
118
|
+
}
|
|
119
|
+
for (const flags of [["--secret", "bad"], ["--secret"], ["--addresses"], ["--secret", secret, "--secret", secret]]) assert.equal(f.run([...args, "--force", ...flags]).code, 1);
|
|
120
|
+
const generated = f.run([...args, "--force"]);
|
|
121
|
+
assert.equal(generated.code, 0);
|
|
122
|
+
saved = JSON.parse(readFileSync(f.env.QC_FLEET_FILE, "utf8"));
|
|
123
|
+
assert.match(saved.secret, /^[0-9a-f]{64}$/);
|
|
124
|
+
const instruction = generated.lines.filter(line => line.includes(saved.secret));
|
|
125
|
+
assert.equal(instruction.length, 1);
|
|
126
|
+
assert.match(instruction[0], /pass the same secret on the other PCs/);
|
|
127
|
+
assert.match(instruction[0], /--secret/);
|
|
128
|
+
assert.equal(f.run([...args, "--force"]).code, 0);
|
|
129
|
+
assert.notEqual(JSON.parse(readFileSync(f.env.QC_FLEET_FILE, "utf8")).secret, saved.secret);
|
|
130
|
+
});
|
|
131
|
+
|
|
82
132
|
test("ownerOf repeats across calls and repo case, using the specified SHA-256 score", () => {
|
|
83
133
|
for (let number = 1; number <= 100; number++) {
|
|
84
134
|
const expected = [...nodes].sort((a, b) => {
|
|
@@ -143,7 +193,7 @@ test("free issues exclude hold labels and taken PR bodies, PR branches and origi
|
|
|
143
193
|
}).free.map(row => row.number), [1]);
|
|
144
194
|
});
|
|
145
195
|
|
|
146
|
-
test("next prints the oldest five or all, exits 3 when empty, and status
|
|
196
|
+
test("next prints the oldest five or all, exits 3 when empty, and status adds the node line", async t => {
|
|
147
197
|
const remoteFixture = fixture(t);
|
|
148
198
|
const git = (...args) => {
|
|
149
199
|
const result = spawnSync("git", args, { cwd: remoteFixture.cwd, encoding: "utf8" });
|
|
@@ -175,19 +225,21 @@ test("next prints the oldest five or all, exits 3 when empty, and status stays w
|
|
|
175
225
|
assert.equal(f.run(["next", "--repo", "o/r"], { ...extra, gh: fakeGh([issue(99, ["custom-hold"])]) }).code, 3);
|
|
176
226
|
const empty = f.run(["next", "--repo", "o/r"], { ...extra, gh: fakeGh([]) });
|
|
177
227
|
assert.deepEqual(empty, { code: 3, lines: [], errors: ["no free issue for a1"] });
|
|
178
|
-
const status = f.run(["status", "--repo", "o/r"], extra);
|
|
228
|
+
const status = await f.run(["status", "--repo", "o/r"], extra);
|
|
179
229
|
assert.equal(status.code, 0);
|
|
180
|
-
assert.equal(status.lines.length,
|
|
230
|
+
assert.equal(status.lines.length, 4);
|
|
181
231
|
assert.match(status.lines[0], /a1.*1.*L.*true.*12.*12,11,10,9,8,7,6,5,4,3$/);
|
|
182
232
|
assert.match(status.lines[1], /b2.*1.*S.*true.*0/);
|
|
183
233
|
assert.match(status.lines[2], /taken.*0.*unassigned.*0/);
|
|
234
|
+
assert.equal(status.lines[3], "node=stopped leader=unknown peers=0/1");
|
|
184
235
|
const many = Array.from({ length: 20 }, (_, i) => ({ id: `pc${i}`, weight: 1, maxSize: "L", active: true }));
|
|
185
236
|
writeFileSync(f.env.QC_FLEET_FILE, JSON.stringify({ self: "pc0", nodes: many }));
|
|
186
|
-
const capped = f.run(["status", "--repo", "o/r"], extra);
|
|
187
|
-
assert.equal(capped.lines.length,
|
|
188
|
-
assert.match(capped.lines.at(-
|
|
237
|
+
const capped = await f.run(["status", "--repo", "o/r"], extra);
|
|
238
|
+
assert.equal(capped.lines.length, 16);
|
|
239
|
+
assert.match(capped.lines.at(-2), /6 more nodes/);
|
|
240
|
+
assert.equal(capped.lines.at(-1), "node=stopped leader=unknown peers=0/19");
|
|
189
241
|
writeFileSync(f.env.QC_FLEET_FILE, JSON.stringify({ self: "a1", nodes: [{ ...nodes[0], active: false }] }));
|
|
190
|
-
assert.match(f.run(["status", "--repo", "o/r"], extra).lines.at(-
|
|
242
|
+
assert.match((await f.run(["status", "--repo", "o/r"], extra)).lines.at(-2), /unassigned.*12/);
|
|
191
243
|
});
|
|
192
244
|
|
|
193
245
|
test("init routes through qc from an empty folder, creates mode 0600 and refuses replacement", t => {
|
|
@@ -211,3 +263,254 @@ test("init routes through qc from an empty folder, creates mode 0600 and refuses
|
|
|
211
263
|
for (const nodesArg of ["a1:1:L:extra", "a1:no:L", "a1:1:XL"]) assert.equal(call(["init", "--self", "a1", "--nodes", nodesArg, "--force"]).status, 1);
|
|
212
264
|
assert.equal(call(["init", "--self", "a1"]).status, 1);
|
|
213
265
|
});
|
|
266
|
+
|
|
267
|
+
test("CLI starts, starts twice, reports running and stops without reading GitHub", async t => {
|
|
268
|
+
const f = fixture(t), lines = [], errors = [];
|
|
269
|
+
assert.equal(f.run(["init", "--self", "a1", "--nodes", "a1:1:L,b2:1:L", "--addresses", "a1@127.0.0.1:8080"]).code, 0);
|
|
270
|
+
let live = false, spawns = 0, instance;
|
|
271
|
+
const node = { spawn: (_command, _args, options) => { instance = options.env.QC_FLEET_INSTANCE; live = true; spawns++; return { pid: 45678, unref() {}, once() {} }; },
|
|
272
|
+
kill: (_pid, signal) => { if (!live) throw Object.assign(new Error("gone"), { code: "ESRCH" }); if (signal !== 0) live = false; },
|
|
273
|
+
fetch: async () => ({ status: 200, json: async () => ({ ok: true, instance }) }), clock: () => 1_000_000 };
|
|
274
|
+
const deps = { cwd: f.cwd, env: f.env, node, origin: () => null, gh: () => { throw new Error("unexpected GitHub read"); },
|
|
275
|
+
say: line => lines.push(line), warn: line => errors.push(line) };
|
|
276
|
+
assert.equal(await runFleet(["start", "--leader"], deps), 0, errors.join("\n"));
|
|
277
|
+
assert.deepEqual(lines.splice(0), ["fleet node started: a1"]);
|
|
278
|
+
assert.equal(await runFleet(["start"], deps), 0);
|
|
279
|
+
assert.deepEqual(lines.splice(0), ["fleet node already running: a1"]);
|
|
280
|
+
assert.equal(spawns, 1);
|
|
281
|
+
assert.equal(readState(f.cwd, "node").leader, true);
|
|
282
|
+
writeState(f.cwd, "peers", { b2: { time: 999999, kit: null, leader: false } });
|
|
283
|
+
assert.equal(await runFleet(["status", "--repo", "o/r"], { ...deps, gh: fakeGh([]), git: () => "" }), 0);
|
|
284
|
+
assert.equal(lines.splice(0).at(-1), "node=running leader=a1 peers=1/1");
|
|
285
|
+
assert.equal(await runFleet(["stop"], deps), 0);
|
|
286
|
+
assert.deepEqual(lines.splice(0), ["fleet node stopped: a1"]);
|
|
287
|
+
assert.equal(await runFleet(["stop"], deps), 0);
|
|
288
|
+
assert.deepEqual(lines.splice(0), ["fleet node already stopped: a1"]);
|
|
289
|
+
const saved = JSON.parse(readFileSync(f.env.QC_FLEET_FILE, "utf8"));
|
|
290
|
+
delete saved.secret;
|
|
291
|
+
writeFileSync(f.env.QC_FLEET_FILE, JSON.stringify(saved));
|
|
292
|
+
assert.equal(await runFleet(["start"], deps), 1);
|
|
293
|
+
assert.match(errors.pop(), /secret/);
|
|
294
|
+
saved.secret = "ab".repeat(32);
|
|
295
|
+
delete saved.nodes[0].address;
|
|
296
|
+
writeFileSync(f.env.QC_FLEET_FILE, JSON.stringify(saved));
|
|
297
|
+
assert.equal(await runFleet(["start"], deps), 1);
|
|
298
|
+
assert.match(errors.pop(), /address.*a1/);
|
|
299
|
+
for (const argv of [["start", "--leader", "--leader"], ["stop", "--leader"], ["serve", "--repo", "o/r"]]) assert.equal(await runFleet(argv, deps), 1);
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
test("CLI serve runs the node loop and exits after an injected stop signal", async t => {
|
|
303
|
+
const f = fixture(t);
|
|
304
|
+
assert.equal(f.run(["init", "--self", "a1", "--nodes", "a1:1:L", "--addresses", "a1@127.0.0.1:8080"]).code, 0);
|
|
305
|
+
const signals = new Map();
|
|
306
|
+
let started;
|
|
307
|
+
const ready = new Promise(resolve => { started = resolve; });
|
|
308
|
+
const errors = [];
|
|
309
|
+
const serving = runFleet(["serve", "--leader"], { cwd: f.cwd, env: f.env, origin: () => "o/r", warn: line => errors.push(line),
|
|
310
|
+
gh: () => { throw new Error("unexpected GitHub read"); }, node: {
|
|
311
|
+
listen: { port: 0 }, timer: () => 1, clearTimer: () => {},
|
|
312
|
+
onSignal: (name, fn) => { signals.set(name, fn); if (name === "SIGINT") started(); }, offSignal: name => signals.delete(name),
|
|
313
|
+
} });
|
|
314
|
+
await ready;
|
|
315
|
+
assert.equal(readState(f.cwd, "node").leader, true);
|
|
316
|
+
signals.get("SIGINT")();
|
|
317
|
+
assert.equal(await serving, 0, errors.join("\n"));
|
|
318
|
+
assert.deepEqual(readState(f.cwd, "node"), {});
|
|
319
|
+
assert.equal(signals.size, 0);
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
test("next prints this node's open repo assignments before free-issue reads", t => {
|
|
323
|
+
const f = fixture(t);
|
|
324
|
+
f.run(["init", "--self", "a1", "--nodes", "a1:1:L,b2:1:L"]);
|
|
325
|
+
writeState(f.cwd, "assignments", {
|
|
326
|
+
"o/r#7": { repo: "o/r", issue: 7, node: "a1", epoch: 2, time: 1 },
|
|
327
|
+
"o/r#8": { repo: "o/r", issue: 8, node: "b2", epoch: 1, time: 1 },
|
|
328
|
+
"else/r#9": { repo: "else/r", issue: 9, node: "a1", epoch: 1, time: 1 },
|
|
329
|
+
});
|
|
330
|
+
const extra = { origin: () => "o/r", gh: fakeGh([issue(7)]) };
|
|
331
|
+
const result = f.run(["next"], extra);
|
|
332
|
+
assert.deepEqual(result, { code: 0, lines: ["#7 epoch=2"], errors: [] });
|
|
333
|
+
const nested = path.join(f.cwd, "nested");
|
|
334
|
+
mkdirSync(nested);
|
|
335
|
+
assert.deepEqual(f.run(["next"], { ...extra, cwd: nested }), result);
|
|
336
|
+
});
|
|
337
|
+
|
|
338
|
+
|
|
339
|
+
test("next retains free-issue output outside a checkout", t => {
|
|
340
|
+
const f = fixture(t);
|
|
341
|
+
f.run(["init", "--self", "a1", "--nodes", "a1:1:L"]);
|
|
342
|
+
rmSync(path.join(f.cwd, ".git"), { recursive: true, force: true, maxRetries: 10, retryDelay: 100 });
|
|
343
|
+
assert.deepEqual(f.run(["next", "--repo", "o/r"], { origin: () => null, gh: fakeGh([issue(1)]), git: () => "" }),
|
|
344
|
+
{ code: 0, lines: ["1"], errors: [] });
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
test("regression: next hides closed local assignments and preserves them on an unreadable open list", t => {
|
|
348
|
+
const f = fixture(t);
|
|
349
|
+
f.run(["init", "--self", "a1", "--nodes", "a1:1:L"]);
|
|
350
|
+
const held = {
|
|
351
|
+
"o/r#7": { repo: "o/r", issue: 7, node: "a1", epoch: 2, time: 1 },
|
|
352
|
+
"o/r#8": { repo: "o/r", issue: 8, node: "a1", epoch: 1, time: 1 },
|
|
353
|
+
"else/r#9": { repo: "else/r", issue: 9, node: "a1", epoch: 1, time: 1 },
|
|
354
|
+
};
|
|
355
|
+
writeState(f.cwd, "assignments", held);
|
|
356
|
+
const calls = [];
|
|
357
|
+
const result = f.run(["next"], { origin: () => "o/r", gh: args => { calls.push(args); return JSON.stringify([{ number: 8 }]); } });
|
|
358
|
+
assert.deepEqual(result, { code: 0, lines: ["#8 epoch=1"], errors: [] });
|
|
359
|
+
assert.deepEqual(calls, [["issue", "list", "--state", "open", "--limit", "500", "--json", "number", "--repo", "o/r"]]);
|
|
360
|
+
assert.deepEqual(readState(f.cwd, "assignments"), held);
|
|
361
|
+
const unfiltered = { code: 0, lines: ["#7 epoch=2", "#8 epoch=1"], errors: [] };
|
|
362
|
+
for (const reply of [null, "bad", "{}", '[{"number":"8"}]']) {
|
|
363
|
+
const kept = readState(f.cwd, "assignments");
|
|
364
|
+
assert.deepEqual(f.run(["next"], { origin: () => "o/r", gh: () => reply }), unfiltered);
|
|
365
|
+
assert.deepEqual(readState(f.cwd, "assignments"), kept);
|
|
366
|
+
}
|
|
367
|
+
assert.deepEqual(f.run(["next"], { origin: () => "o/r", gh: () => { throw new Error("offline"); } }), unfiltered);
|
|
368
|
+
const fallback = f.run(["next"], { origin: () => "o/r", gh: fakeGh([issue(10)]), git: () => "" });
|
|
369
|
+
assert.deepEqual(fallback, { code: 0, lines: ["10"], errors: [] });
|
|
370
|
+
assert.deepEqual(readState(f.cwd, "assignments"), held);
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
test("regression: next hides closed assignments without overwriting a concurrent serve update", t => {
|
|
374
|
+
const f = fixture(t);
|
|
375
|
+
f.run(["init", "--self", "a1", "--nodes", "a1:1:L"]);
|
|
376
|
+
const held = {
|
|
377
|
+
"o/r#7": { repo: "o/r", issue: 7, node: "a1", epoch: 1, time: 1 },
|
|
378
|
+
"o/r#8": { repo: "o/r", issue: 8, node: "a1", epoch: 1, time: 1 },
|
|
379
|
+
};
|
|
380
|
+
writeState(f.cwd, "assignments", held);
|
|
381
|
+
const latest = { ...held, "o/r#9": { repo: "o/r", issue: 9, node: "a1", epoch: 2, time: 2 } };
|
|
382
|
+
const result = f.run(["next"], { origin: () => "o/r", gh: () => {
|
|
383
|
+
// The serve loop writes while next is reading GitHub's open-issue list.
|
|
384
|
+
writeState(f.cwd, "assignments", latest);
|
|
385
|
+
return JSON.stringify([{ number: 8 }, { number: 9 }]);
|
|
386
|
+
} });
|
|
387
|
+
assert.deepEqual(result, { code: 0, lines: ["#8 epoch=1"], errors: [] });
|
|
388
|
+
assert.deepEqual(readState(f.cwd, "assignments"), latest);
|
|
389
|
+
});
|
|
390
|
+
|
|
391
|
+
test("progress records each step at the own saved epoch and preserves optional branch/head", t => {
|
|
392
|
+
const f = fixture(t), head = "a".repeat(40);
|
|
393
|
+
f.run(["init", "--self", "a1", "--nodes", "a1:1:L,b2:1:L"]);
|
|
394
|
+
const held = { repo: "o/r", issue: 7, node: "a1", epoch: 2, time: 1 };
|
|
395
|
+
writeState(f.cwd, "assignments", { "o/r#7": held });
|
|
396
|
+
const deps = { origin: () => "O/R", gh: () => { throw new Error("progress must be local"); } };
|
|
397
|
+
for (const step of ["plan", "code", "review", "merge"]) {
|
|
398
|
+
const result = f.run(["progress", "7", "--step", step, ...(step === "plan" ? ["--branch", "feat/7", "--head", head] : [])], deps);
|
|
399
|
+
assert.equal(result.code, 0, result.errors.join("\n"));
|
|
400
|
+
assert.deepEqual(readState(f.cwd, "work")["o/r#7"], { repo: "o/r", issue: 7, epoch: 2, step, branch: "feat/7", head });
|
|
401
|
+
}
|
|
402
|
+
writeState(f.cwd, "assignments", { "o/r#7": { ...held, epoch: 3 } });
|
|
403
|
+
assert.equal(f.run(["progress", "7", "--step", "code"], deps).code, 0);
|
|
404
|
+
assert.deepEqual(readState(f.cwd, "work")["o/r#7"], { repo: "o/r", issue: 7, epoch: 3, step: "code", branch: null, head: null });
|
|
405
|
+
const before = readState(f.cwd, "work");
|
|
406
|
+
for (const args of [["progress", "8", "--step", "plan"], ["progress", "7"], ["progress", "7", "--step", "push"],
|
|
407
|
+
["progress", "0", "--step", "plan"], ["progress", "7.5", "--step", "plan"], ["progress", "7", "--step", "plan", "--head", "bad"],
|
|
408
|
+
["progress", "7", "--step", "plan", "--branch", "bad branch"], ["progress", "7", "--step", "plan", "--step", "code"]]) {
|
|
409
|
+
assert.equal(f.run(args, deps).code, 1, args.join(" "));
|
|
410
|
+
assert.deepEqual(readState(f.cwd, "work"), before);
|
|
411
|
+
}
|
|
412
|
+
writeState(f.cwd, "assignments", { "o/r#7": { ...held, node: "b2" } });
|
|
413
|
+
assert.equal(f.run(["progress", "7", "--step", "code"], deps).code, 1);
|
|
414
|
+
});
|
|
415
|
+
|
|
416
|
+
test("next prints takeover source branch/head", t => {
|
|
417
|
+
const f = fixture(t), head = "b".repeat(40);
|
|
418
|
+
f.run(["init", "--self", "a1", "--nodes", "a1:1:L"]);
|
|
419
|
+
writeState(f.cwd, "assignments", { "o/r#7": { repo: "o/r", issue: 7, node: "a1", epoch: 2, branch: "feat/7-old", head } });
|
|
420
|
+
assert.deepEqual(f.run(["next"], { origin: () => "o/r", gh: fakeGh([issue(7)]) }).lines, [`#7 epoch=2 from feat/7-old@${head}`]);
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
test("owns requires the latest Leader snapshot and the saved own epoch, with local failure reasons", t => {
|
|
424
|
+
const f = fixture(t);
|
|
425
|
+
f.run(["init", "--self", "a1", "--nodes", "a1:1:L,b2:1:L,c3:1:L"]);
|
|
426
|
+
const held = { repo: "o/r", issue: 7, node: "a1", epoch: 2, time: 1 };
|
|
427
|
+
const deps = { origin: () => "o/r", gh: () => { throw new Error("owns must be local"); } };
|
|
428
|
+
const check = reason => { const result = f.run(["owns", "7"], deps); assert.equal(result.code, 1); assert.match([...result.lines, ...result.errors].join("\n"), reason); };
|
|
429
|
+
check(/assignment/i);
|
|
430
|
+
writeState(f.cwd, "assignments", { "o/r#7": held });
|
|
431
|
+
check(/Leader heartbeat/i);
|
|
432
|
+
writeState(f.cwd, "peers", { b2: { time: 1, leader: true, assigned: [{ repo: "o/r", issue: 7, epoch: 2 }] } });
|
|
433
|
+
assert.equal(f.run(["owns", "7"], deps).code, 0);
|
|
434
|
+
writeState(f.cwd, "peers", { b2: { time: 1, leader: true, assigned: [{ repo: "o/r", issue: 7, epoch: 2 }] }, c3: { time: 2, leader: true, assigned: [{ repo: "o/r", issue: 7, epoch: 3 }] } });
|
|
435
|
+
check(/epoch/i);
|
|
436
|
+
writeState(f.cwd, "peers", { b2: { time: 3, leader: true, assigned: [] } });
|
|
437
|
+
check(/list|assign/i);
|
|
438
|
+
writeState(f.cwd, "assignments", { "o/r#7": { ...held, node: "b2" } });
|
|
439
|
+
check(/assignment/i);
|
|
440
|
+
assert.equal(f.run(["owns", "0"], deps).code, 1);
|
|
441
|
+
});
|
|
442
|
+
|
|
443
|
+
test("next adds the saved plan decision only for the assignment epoch", async t => {
|
|
444
|
+
const { cwd, run } = fixture(t);
|
|
445
|
+
run(["init", "--self", "a1", "--nodes", "a1:1:L"]);
|
|
446
|
+
writeState(cwd, "assignments", Object.fromEntries([7, 8, 9].map(issue => [`o/r#${issue}`, { repo: "o/r", issue, node: "a1", epoch: 3 }])));
|
|
447
|
+
writeState(cwd, "plans", Object.fromEntries([7, 8, 9].map(issue => [`o/r#${issue}`, { kind: "approve", body: { issue, epoch: issue === 9 ? 2 : 3, approved: issue !== 8 } }])));
|
|
448
|
+
const result = await run(["next"], { origin: () => "o/r", gh: () => JSON.stringify([7, 8, 9].map(number => ({ number }))) });
|
|
449
|
+
assert.equal(result.code, 0);
|
|
450
|
+
assert.deepEqual(result.lines, ["#7 epoch=3 plan approved", "#8 epoch=3 plan needs changes", "#9 epoch=3"]);
|
|
451
|
+
});
|
|
452
|
+
|
|
453
|
+
test("confirm-leader requires a current proposal in three nodes and rebuilds highest live work epochs", t => {
|
|
454
|
+
const f = fixture(t), time = Date.now(), deps = { origin: () => "o/r", node: { clock: () => time } };
|
|
455
|
+
f.run(["init", "--self", "b2", "--nodes", "a1:1:L,b2:1:L,c3:1:L"]);
|
|
456
|
+
writeState(f.cwd, "node", { pid: 123, leader: false, instance: "serve" });
|
|
457
|
+
writeState(f.cwd, "peers", { a1: { leader: true, term: 1, time: time - 90_000 },
|
|
458
|
+
c3: { term: 1, time, work: [{ repo: "o/r", issue: 7, epoch: 4, step: "review", branch: "feat/7", head: "a".repeat(40) }] } });
|
|
459
|
+
writeState(f.cwd, "assignments", { "o/r#7": { repo: "o/r", issue: 7, node: "b2", epoch: 2 }, "o/r#8": { repo: "o/r", issue: 8, node: "a1", epoch: 9 } });
|
|
460
|
+
writeState(f.cwd, "work", { "o/r#7": { repo: "o/r", issue: 7, epoch: 2, step: "code" } });
|
|
461
|
+
assert.equal(f.run(["confirm-leader"], deps).code, 1);
|
|
462
|
+
writeState(f.cwd, "proposal", { id: "b2", term: 2 });
|
|
463
|
+
assert.equal(f.run(["confirm-leader"], deps).code, 0);
|
|
464
|
+
assert.deepEqual(readState(f.cwd, "node"), { pid: 123, leader: true, term: 2, instance: "serve" });
|
|
465
|
+
assert.deepEqual(readState(f.cwd, "assignments"), { "o/r#7": { repo: "o/r", issue: 7, epoch: 4, node: "c3", time, branch: "feat/7", head: "a".repeat(40), step: "review" } });
|
|
466
|
+
assert.deepEqual(readState(f.cwd, "proposal"), {});
|
|
467
|
+
});
|
|
468
|
+
|
|
469
|
+
test("two nodes confirm without a proposal only after Leader death; stale proposals cannot win", t => {
|
|
470
|
+
const f = fixture(t), time = Date.now(), deps = { origin: () => "o/r", node: { clock: () => time } };
|
|
471
|
+
f.run(["init", "--self", "b2", "--nodes", "a1:1:L,b2:1:L"]);
|
|
472
|
+
writeState(f.cwd, "node", { leader: false, started: time - 90_000 });
|
|
473
|
+
writeState(f.cwd, "peers", { a1: { leader: true, term: 5, time: time - 89_999 } });
|
|
474
|
+
assert.equal(f.run(["confirm-leader"], deps).code, 1);
|
|
475
|
+
writeState(f.cwd, "peers", { a1: { leader: true, term: 5, time: time - 90_000 } });
|
|
476
|
+
assert.equal(f.run(["confirm-leader"], deps).code, 0);
|
|
477
|
+
assert.equal(readState(f.cwd, "node").term, 6);
|
|
478
|
+
writeState(f.cwd, "node", { leader: false });
|
|
479
|
+
writeState(f.cwd, "proposal", { id: "b2", term: 4 });
|
|
480
|
+
assert.equal(f.run(["confirm-leader"], deps).code, 1);
|
|
481
|
+
});
|
|
482
|
+
|
|
483
|
+
test("status prints the pending Leader proposal", async t => {
|
|
484
|
+
const f = fixture(t);
|
|
485
|
+
f.run(["init", "--self", "b2", "--nodes", "a1:1:L,b2:1:L,c3:1:L"]);
|
|
486
|
+
writeState(f.cwd, "proposal", { id: "b2", term: 2 });
|
|
487
|
+
const result = await f.run(["status"], { origin: () => "o/r", gh: fakeGh([]), git: () => "" });
|
|
488
|
+
assert.equal(result.code, 0);
|
|
489
|
+
assert.ok(result.lines.includes("proposed Leader b2 term 2: run npx architecture-harness fleet confirm-leader"));
|
|
490
|
+
});
|
|
491
|
+
|
|
492
|
+
|
|
493
|
+
test("pushed requires owns and strict branch/head and sends the held epoch to the Leader", async t => {
|
|
494
|
+
const f = fixture(t);
|
|
495
|
+
f.run(["init", "--self", "a1", "--nodes", "a1:1:L,b2:1:L", "--addresses", "a1@localhost:8080,b2@localhost:8081"]);
|
|
496
|
+
const head = "b".repeat(40), sent = [];
|
|
497
|
+
const deps = { origin: () => "o/r", gh: () => { throw new Error("pushed needs no gh"); }, node: {
|
|
498
|
+
fetch: async (url, options) => { sent.push({ url, message: JSON.parse(options.body) }); return { status: 202 }; }
|
|
499
|
+
} };
|
|
500
|
+
const args = ["pushed", "7", "--branch", "feat/7", "--head", head];
|
|
501
|
+
assert.equal((await f.run(args, deps)).code, 1);
|
|
502
|
+
writeState(f.cwd, "assignments", { "o/r#7": { repo: "o/r", issue: 7, node: "a1", epoch: 2 } });
|
|
503
|
+
assert.equal((await f.run(args, deps)).code, 1);
|
|
504
|
+
writeState(f.cwd, "peers", { b2: { time: 1, leader: true, assigned: [{ repo: "o/r", issue: 7, epoch: 1 }] } });
|
|
505
|
+
assert.equal((await f.run(args, deps)).code, 1);
|
|
506
|
+
writeState(f.cwd, "peers", { b2: { time: 2, leader: true, assigned: [{ repo: "o/r", issue: 7, epoch: 2 }] } });
|
|
507
|
+
for (const invalid of [["pushed", "7"], ["pushed", "7", "--branch", "bad;branch", "--head", head],
|
|
508
|
+
["pushed", "7", "--branch", "feat/7", "--head", "bad"], ["pushed", "7", "--branch", "feat/7"]]) {
|
|
509
|
+
assert.equal((await f.run(invalid, deps)).code, 1);
|
|
510
|
+
}
|
|
511
|
+
assert.equal(sent.length, 0);
|
|
512
|
+
assert.equal((await f.run(args, deps)).code, 0);
|
|
513
|
+
assert.equal(sent[0].url, "http://localhost:8081/message");
|
|
514
|
+
assert.equal(sent[0].message.kind, "pushed");
|
|
515
|
+
assert.deepEqual(sent[0].message.body, { issue: 7, epoch: 2, branch: "feat/7", head });
|
|
516
|
+
});
|