architecture-harness 0.29.1 → 0.29.3
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 +1 -1
- package/src/cli/comment-watch.mjs +114 -19
- package/src/cli/comment-watch.test.mjs +349 -12
- package/src/cli/fleet-assign.mjs +1 -1
- package/src/cli/fleet-assign.test.mjs +9 -0
- package/src/cli/fleet-github.mjs +2 -1
- package/src/cli/fleet-load.mjs +1 -1
- package/src/cli/fleet-load.test.mjs +37 -0
- package/src/cli/fleet-message.mjs +1 -1
- package/src/cli/fleet-message.test.mjs +1 -1
- package/src/cli/fleet-node.mjs +14 -7
- package/src/cli/fleet-node.test.mjs +74 -6
- package/src/cli/fleet-state.mjs +1 -1
- package/src/cli/fleet-talk.mjs +42 -21
- package/src/cli/fleet-talk.test.mjs +158 -9
- package/src/cli/fleet.mjs +1 -1
- package/src/cli/watch.mjs +3 -2
|
@@ -18,7 +18,7 @@ import { assignTick } from "./fleet-assign.mjs";
|
|
|
18
18
|
const idleOS = { cpus: () => [{ times: { user: 10, idle: 90 } }], freemem: () => 50, totalmem: () => 100 };
|
|
19
19
|
const serveNode = options => runNode({ os: idleOS, ...options });
|
|
20
20
|
const heartbeat = options => sendHeartbeat({ os: idleOS, ...options });
|
|
21
|
-
const initialLoad = { cpu: null, mem: 0.5, rtt: null };
|
|
21
|
+
const initialLoad = { platform: process.platform, cpu: null, mem: 0.5, rtt: null };
|
|
22
22
|
|
|
23
23
|
const host = process.platform === "win32" ? "windows" : process.platform;
|
|
24
24
|
const apiResponse = args => JSON.stringify(args[1] === "user" ? { login: "me" } : []);
|
|
@@ -341,6 +341,48 @@ test("start names missing secret/address, times out and cleans up; stale records
|
|
|
341
341
|
assert.deepEqual(await startNode(p.deps), { running: true, already: false });
|
|
342
342
|
});
|
|
343
343
|
|
|
344
|
+
test("a dead pid record lets start and stop go on without matching health", async t => {
|
|
345
|
+
for (const action of ["start", "stop"]) await t.test(action, async t => {
|
|
346
|
+
const root = fixture(t), p = processDeps(root);
|
|
347
|
+
writeState(root, "node", { pid: 12345, leader: true, instance: "dead-instance" });
|
|
348
|
+
const deps = { ...p.deps,
|
|
349
|
+
spawn: (...args) => {
|
|
350
|
+
assert.deepEqual(readState(root, "node"), {}, "remove the dead record before spawning");
|
|
351
|
+
return p.deps.spawn(...args);
|
|
352
|
+
},
|
|
353
|
+
fetch: (...args) => {
|
|
354
|
+
assert.equal(p.spawned.length, 1, "only the new child needs a health check");
|
|
355
|
+
return p.deps.fetch(...args);
|
|
356
|
+
},
|
|
357
|
+
};
|
|
358
|
+
if (action === "start") {
|
|
359
|
+
assert.deepEqual(await startNode(deps), { running: true, already: false });
|
|
360
|
+
assert.equal(p.spawned.length, 1);
|
|
361
|
+
assert.equal(readState(root, "node").instance, p.spawned[0].options.env.QC_FLEET_INSTANCE);
|
|
362
|
+
} else {
|
|
363
|
+
assert.deepEqual(await stopNode(deps), { stopped: true, already: true });
|
|
364
|
+
assert.deepEqual(readState(root, "node"), {});
|
|
365
|
+
assert.equal(p.spawned.length, 0);
|
|
366
|
+
}
|
|
367
|
+
assert.ok(p.signals.every(signal => signal === 0), "never signal the dead process");
|
|
368
|
+
});
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
test("a live pid with a different health instance still throws the stale error", async t => {
|
|
372
|
+
for (const action of [startNode, stopNode]) await t.test(action.name, async t => {
|
|
373
|
+
const root = fixture(t), p = processDeps(root);
|
|
374
|
+
const record = { pid: 12345, leader: true, instance: "recorded-instance" };
|
|
375
|
+
writeState(root, "node", record);
|
|
376
|
+
p.setLive(true);
|
|
377
|
+
await assert.rejects(action({ ...p.deps,
|
|
378
|
+
fetch: async () => ({ status: 200, json: async () => ({ ok: true, instance: "different-instance" }) }),
|
|
379
|
+
}), /fleet node is stale: instance health does not match/);
|
|
380
|
+
assert.deepEqual(readState(root, "node"), record);
|
|
381
|
+
assert.equal(p.spawned.length, 0);
|
|
382
|
+
assert.ok(p.signals.every(signal => signal === 0), "never signal the unrelated live process");
|
|
383
|
+
});
|
|
384
|
+
});
|
|
385
|
+
|
|
344
386
|
test("status counts only known other senders in the inclusive 90-second window", async t => {
|
|
345
387
|
const root = fixture(t), p = processDeps(root);
|
|
346
388
|
writeState(root, "peers", { a: { time: now, leader: true }, b: { time: now - 90000, leader: true },
|
|
@@ -484,7 +526,7 @@ test("regression: lifecycle requires the saved instance before readiness or sign
|
|
|
484
526
|
assert.equal(await nodeStatus(p.deps), "node=stale leader=unknown peers=0/3");
|
|
485
527
|
await assert.rejects(stopNode(p.deps), /stale/);
|
|
486
528
|
await assert.rejects(startNode(p.deps), /stale/);
|
|
487
|
-
assert.deepEqual(p.signals, []);
|
|
529
|
+
assert.deepEqual(p.signals.filter(signal => signal !== 0), []);
|
|
488
530
|
assert.equal(p.spawned.length, 0);
|
|
489
531
|
assert.equal(readState(root, "node").instance, "saved-instance");
|
|
490
532
|
});
|
|
@@ -1013,6 +1055,32 @@ test("three nodes propose in leaders order at 90 seconds, save after one live ac
|
|
|
1013
1055
|
assert.equal(later.sent.filter(m => m.kind === "propose-leader").length, 0);
|
|
1014
1056
|
});
|
|
1015
1057
|
|
|
1058
|
+
test("a lost accept retries a Leader proposal at a higher term after 30 seconds and not before", async t => {
|
|
1059
|
+
const f = await electionNode(t);
|
|
1060
|
+
writeState(f.root, "peers", { a: { leader: true, term: 1, time: now }, c: { leader: false, term: 1, time: now + 90_000 } });
|
|
1061
|
+
const proposals = () => f.sent.filter(m => m.kind === "propose-leader").map(m => m.body.term);
|
|
1062
|
+
f.time(now + 90_000); await f.tick();
|
|
1063
|
+
assert.deepEqual(proposals(), [2]);
|
|
1064
|
+
f.time(now + 119_999); await f.tick();
|
|
1065
|
+
assert.deepEqual(proposals(), [2]);
|
|
1066
|
+
f.time(now + 120_000); await f.tick();
|
|
1067
|
+
assert.deepEqual(proposals(), [2, 3]);
|
|
1068
|
+
assert.equal(readState(f.root, "election").votedTerm, 3);
|
|
1069
|
+
await f.tick();
|
|
1070
|
+
assert.deepEqual(proposals(), [2, 3], "a retry never reuses a term");
|
|
1071
|
+
writeState(f.root, "queue", [message({ from: "c", kind: "leader", body: { term: 2, accept: true } })]);
|
|
1072
|
+
await f.tick();
|
|
1073
|
+
assert.deepEqual(readState(f.root, "proposal"), {}, "an old accept cannot confirm the retry");
|
|
1074
|
+
f.time(now + 150_000);
|
|
1075
|
+
writeState(f.root, "queue", [message({ from: "c", kind: "leader", body: { term: 3, accept: true } })]);
|
|
1076
|
+
await f.tick();
|
|
1077
|
+
assert.deepEqual(readState(f.root, "proposal"), { id: "b", term: 3 });
|
|
1078
|
+
f.time(now + 180_000); await f.tick();
|
|
1079
|
+
assert.deepEqual(proposals(), [2, 3], "an accepted proposal waits for confirmation without retrying");
|
|
1080
|
+
const { confirmLeader } = await import("./fleet-node.mjs");
|
|
1081
|
+
assert.equal(confirmLeader({ root: f.root, fleet: f.config, clock: () => now + 180_000 }), 3);
|
|
1082
|
+
});
|
|
1083
|
+
|
|
1016
1084
|
test("votes require a dead Leader and go once per term, including after a restart", async t => {
|
|
1017
1085
|
const f = await electionNode(t, "c");
|
|
1018
1086
|
const peers = { a: { leader: true, term: 1, time: now }, b: { leader: false, term: 1, time: now + 90_000 } };
|
|
@@ -1353,7 +1421,7 @@ test("platform child checks leave serve ticks free to heartbeat and retain the p
|
|
|
1353
1421
|
test("heartbeat samples fake OS and publishes the successful prior round median, including Leader load", async t => {
|
|
1354
1422
|
const root = fixture(t);
|
|
1355
1423
|
let time = now, busy = 10;
|
|
1356
|
-
const fakeOS = { cpus: () => [{ times: { user: busy, idle: 90 } }], freemem: () => 20, totalmem: () => 100 };
|
|
1424
|
+
const fakeOS = { platform: () => "darwin", cpus: () => [{ times: { user: busy, idle: 90 } }], freemem: () => 20, totalmem: () => 100 };
|
|
1357
1425
|
const nodes = { ...fleet, nodes: [fleet.nodes[0], ...["b", "c", "d", "e", "f"].map(id => ({ id, address: "localhost" }))] };
|
|
1358
1426
|
const received = [];
|
|
1359
1427
|
let index = 0;
|
|
@@ -1361,12 +1429,12 @@ test("heartbeat samples fake OS and publishes the successful prior round median,
|
|
|
1361
1429
|
const deps = { root, fleet: nodes, repo: "o/r", leader: true, os: fakeOS, clock: () => time,
|
|
1362
1430
|
fetch: async (_url, options) => { received.push(JSON.parse(options.body).body.load); time += durations[index % 5]; return { status: index++ % 5 === 2 ? 503 : 202 }; } };
|
|
1363
1431
|
await heartbeat(deps);
|
|
1364
|
-
assert.deepEqual(received, Array(5).fill({ cpu: null, mem: 0.8, rtt: null }));
|
|
1365
|
-
assert.deepEqual(readState(root, "peers").a.load, { cpu: null, mem: 0.8, rtt: 25 });
|
|
1432
|
+
assert.deepEqual(received, Array(5).fill({ platform: "darwin", cpu: null, mem: 0.8, rtt: null }));
|
|
1433
|
+
assert.deepEqual(readState(root, "peers").a.load, { platform: "darwin", cpu: null, mem: 0.8, rtt: 25 });
|
|
1366
1434
|
received.length = 0;
|
|
1367
1435
|
busy = 110;
|
|
1368
1436
|
await heartbeat(deps);
|
|
1369
|
-
assert.deepEqual(received, Array(5).fill({ cpu: 1, mem: 0.8, rtt: 25 }));
|
|
1437
|
+
assert.deepEqual(received, Array(5).fill({ platform: "darwin", cpu: 1, mem: 0.8, rtt: 25 }));
|
|
1370
1438
|
await heartbeat({ ...deps, fetch: async () => { time += 50; return { status: 503 }; } });
|
|
1371
1439
|
assert.equal(readState(root, "peers").a.load.rtt, null);
|
|
1372
1440
|
received.length = 0;
|
package/src/cli/fleet-state.mjs
CHANGED
|
@@ -3,7 +3,7 @@ import { mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "node
|
|
|
3
3
|
import path from "node:path";
|
|
4
4
|
import { commonDirOf } from "./ledger.mjs";
|
|
5
5
|
|
|
6
|
-
const FILES = ["election", "proposal", "work", "assignments", "questions", "seen", "queue", "outbox", "dead", "peers", "node", "plans", "answers", "inbox", "watched", "github"];
|
|
6
|
+
const FILES = ["election", "proposal", "work", "assignments", "questions", "seen", "queue", "outbox", "dead", "peers", "node", "plans", "answers", "said", "inbox", "watched", "github"];
|
|
7
7
|
|
|
8
8
|
/** State is private to git's shared directory, outside every worktree's tracked files. */
|
|
9
9
|
export function stateDir(root = process.cwd()) {
|
package/src/cli/fleet-talk.mjs
CHANGED
|
@@ -1,23 +1,25 @@
|
|
|
1
1
|
import { readFileSync } from "node:fs";
|
|
2
2
|
import { createMessage } from "./fleet-message.mjs";
|
|
3
3
|
import { flushOutbox, sendMessage } from "./fleet-node.mjs";
|
|
4
|
-
import { readState, writeState } from "./fleet-state.mjs";
|
|
4
|
+
import { livePeer, readState, writeState } from "./fleet-state.mjs";
|
|
5
5
|
import { roleComment } from "./role-line.mjs";
|
|
6
6
|
|
|
7
|
-
export const TALK_VERBS = ["plan", "ask", "approve", "answer", "show"];
|
|
7
|
+
export const TALK_VERBS = ["plan", "ask", "say", "approve", "answer", "show"];
|
|
8
8
|
const validText = text => typeof text === "string" && text.length <= 20000;
|
|
9
9
|
|
|
10
|
-
function requestError(message, assignments) {
|
|
10
|
+
function requestError(message, assignments, fleet) {
|
|
11
11
|
const { issue, epoch, text } = message.body ?? {};
|
|
12
|
-
if (!Number.isSafeInteger(issue) || issue <= 0 || !Number.isSafeInteger(epoch) || epoch
|
|
12
|
+
if (!Number.isSafeInteger(issue) || issue <= 0 || !Number.isSafeInteger(epoch) || epoch < 0 || !validText(text)) return "invalid request issue, epoch or text";
|
|
13
|
+
if (!fleet.nodes.some(node => node.id === message.from)) return "request sender is not a known fleet node";
|
|
13
14
|
const assignment = assignments[`${message.repo}#${issue}`];
|
|
15
|
+
if (epoch === 0 && assignment?.node !== message.from) return null;
|
|
14
16
|
if (!assignment || assignment.node !== message.from || assignment.epoch !== epoch) return "request sender does not hold this issue at this assignment epoch";
|
|
15
17
|
return null;
|
|
16
18
|
}
|
|
17
19
|
|
|
18
20
|
export function parseTalkArgs(verb, rest) {
|
|
19
21
|
const [target, ...flags] = rest;
|
|
20
|
-
if (!target || target.startsWith("--")) throw new Error(`${verb} needs an issue or message ID`);
|
|
22
|
+
if (!target || target.startsWith("--")) throw new Error(`${verb} needs an issue, node or message ID`);
|
|
21
23
|
if (["plan", "ask"].includes(verb) && (!/^[1-9]\d*$/.test(target) || !Number.isSafeInteger(Number(target)))) throw new Error("issue must be a positive integer");
|
|
22
24
|
const options = { verb, target };
|
|
23
25
|
for (let i = 0; i < flags.length; i++) {
|
|
@@ -30,7 +32,7 @@ export function parseTalkArgs(verb, rest) {
|
|
|
30
32
|
options[flag] = flags[++i];
|
|
31
33
|
}
|
|
32
34
|
}
|
|
33
|
-
if (["plan", "ask", "answer"].includes(verb) && !options["--file"]) throw new Error(`${verb} needs --file`);
|
|
35
|
+
if (["plan", "ask", "say", "answer"].includes(verb) && !options["--file"]) throw new Error(`${verb} needs --file`);
|
|
34
36
|
return options;
|
|
35
37
|
}
|
|
36
38
|
|
|
@@ -44,20 +46,30 @@ function fileText(file) {
|
|
|
44
46
|
export function receiveTalk(root, fleet, leader, now) {
|
|
45
47
|
const queue = readState(root, "queue"), peers = readState(root, "peers"), assignments = readState(root, "assignments");
|
|
46
48
|
const plans = readState(root, "plans"), answers = readState(root, "answers"), kept = [];
|
|
49
|
+
const said = readState(root, "said");
|
|
47
50
|
const inbox = readState(root, "inbox");
|
|
48
|
-
let changed = false, savedPlan = false, savedAnswer = false;
|
|
51
|
+
let changed = false, savedPlan = false, savedAnswer = false, savedSay = false;
|
|
49
52
|
for (const message of queue) {
|
|
53
|
+
if (message.kind === "say") {
|
|
54
|
+
changed = true;
|
|
55
|
+
if (!validText(message.body?.text)) continue;
|
|
56
|
+
Object.defineProperty(said, message.id, { value: message, enumerable: true, writable: true, configurable: true });
|
|
57
|
+
inbox.push({ id: message.id, kind: message.kind, from: message.from, repo: message.repo, time: message.time });
|
|
58
|
+
savedSay = true;
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
50
61
|
const request = leader && ["plan", "question"].includes(message.kind);
|
|
51
62
|
const reply = !leader && ["approve", "answer"].includes(message.kind);
|
|
52
63
|
if (!request && !reply) { kept.push(message); continue; }
|
|
53
64
|
changed = true;
|
|
54
65
|
const body = message.body ?? {}, { issue, epoch } = body;
|
|
55
|
-
if (!Number.isSafeInteger(issue) || issue <= 0 || !Number.isSafeInteger(epoch) || epoch
|
|
56
|
-
if (request ? requestError(message, assignments) : message.kind === "approve"
|
|
66
|
+
if (!Number.isSafeInteger(issue) || issue <= 0 || !Number.isSafeInteger(epoch) || epoch < 0) continue;
|
|
67
|
+
if (request ? requestError(message, assignments, fleet) : message.kind === "approve"
|
|
57
68
|
? typeof body.approved !== "boolean" || typeof body.plan !== "string" || !body.plan || !validText(body.note)
|
|
58
69
|
: typeof body.question !== "string" || !body.question || !validText(body.text)) continue;
|
|
59
70
|
const key = `${message.repo}#${issue}`, assignment = assignments[key];
|
|
60
|
-
if (
|
|
71
|
+
if (reply && (epoch === 0 ? assignment?.node === fleet.self
|
|
72
|
+
: !assignment || assignment.node !== fleet.self || assignment.epoch !== epoch)) continue;
|
|
61
73
|
if (reply && peers[message.from]?.leader !== true) {
|
|
62
74
|
if (peers[message.from]?.leader !== false && now - Date.parse(message.time) < 600_000) kept.push(message);
|
|
63
75
|
continue;
|
|
@@ -72,7 +84,8 @@ export function receiveTalk(root, fleet, leader, now) {
|
|
|
72
84
|
}
|
|
73
85
|
if (savedPlan) writeState(root, "plans", plans);
|
|
74
86
|
if (savedAnswer) writeState(root, "answers", answers);
|
|
75
|
-
if (
|
|
87
|
+
if (savedSay) writeState(root, "said", said);
|
|
88
|
+
if (savedPlan || savedAnswer || savedSay) {
|
|
76
89
|
const watched = readState(root, "watched");
|
|
77
90
|
let excess = inbox.filter(entry => Object.hasOwn(watched, entry.id)).length - 200;
|
|
78
91
|
writeState(root, "inbox", inbox.filter(entry => {
|
|
@@ -89,32 +102,40 @@ export function receiveTalk(root, fleet, leader, now) {
|
|
|
89
102
|
export async function runTalk(options, { root, fleet, repo, gh, say, node = {} }) {
|
|
90
103
|
const { verb, target } = options;
|
|
91
104
|
if (verb === "show") {
|
|
92
|
-
const message = [...readState(root, "queue"), ...Object.values(readState(root, "plans")), ...Object.values(readState(root, "answers"))].find(message => message.id === target);
|
|
105
|
+
const message = [...readState(root, "queue"), ...Object.values(readState(root, "plans")), ...Object.values(readState(root, "answers")), ...Object.values(readState(root, "said"))].find(message => message.id === target);
|
|
93
106
|
if (!message) throw new Error(`message not found: ${target}`);
|
|
94
|
-
|
|
107
|
+
const issue = message.kind === "say" ? "" : ` issue=#${message.body.issue} epoch=${message.body.epoch}`;
|
|
108
|
+
say(`${message.kind} from=${message.from}${issue}\n${message.body.text ?? message.body.note ?? ""}`);
|
|
95
109
|
return 0;
|
|
96
110
|
}
|
|
97
111
|
const request = ["plan", "ask"].includes(verb);
|
|
98
|
-
|
|
112
|
+
const direct = verb === "say";
|
|
113
|
+
if (!request && !direct && readState(root, "node").leader !== true) throw new Error(`${verb} runs on the Leader only`);
|
|
99
114
|
let recipient, body, kind, comment, queued;
|
|
100
|
-
if (!request) {
|
|
115
|
+
if (!request && !direct) {
|
|
101
116
|
queued = readState(root, "queue").find(message => message.id === target && message.repo === repo && message.kind === (verb === "approve" ? "plan" : "question"));
|
|
102
117
|
if (!queued) throw new Error(`${verb} request not found: ${target}`);
|
|
103
|
-
const reason = requestError(queued, readState(root, "assignments"));
|
|
118
|
+
const reason = requestError(queued, readState(root, "assignments"), fleet);
|
|
104
119
|
if (reason) {
|
|
105
120
|
writeState(root, "queue", readState(root, "queue").filter(message => message.id !== target));
|
|
106
121
|
throw new Error(reason);
|
|
107
122
|
}
|
|
108
123
|
}
|
|
109
124
|
const text = options["--file"] ? fileText(options["--file"]) : "";
|
|
110
|
-
if (
|
|
125
|
+
if (direct) {
|
|
126
|
+
const peers = readState(root, "peers"), now = (node.clock ?? Date.now)();
|
|
127
|
+
recipient = fleet.nodes.find(peer => peer.id !== fleet.self && peer.active !== false && peer.address && livePeer(peers[peer.id], now) &&
|
|
128
|
+
(target === "leader" ? peers[peer.id].leader === true : peer.id === target));
|
|
129
|
+
if (!recipient) throw new Error(`no live recipient heartbeat: ${target}`);
|
|
130
|
+
kind = "say";
|
|
131
|
+
body = { text };
|
|
132
|
+
} else if (request) {
|
|
111
133
|
const issue = Number(target), assignment = readState(root, "assignments")[`${repo}#${issue}`];
|
|
112
|
-
if (!assignment || assignment.node !== fleet.self) throw new Error(`this node does not hold #${issue}`);
|
|
113
134
|
const peers = readState(root, "peers");
|
|
114
135
|
recipient = fleet.nodes.find(peer => peer.id !== fleet.self && peer.active !== false && peers[peer.id]?.leader === true);
|
|
115
136
|
if (!recipient) throw new Error("no Leader heartbeat");
|
|
116
137
|
kind = verb === "plan" ? "plan" : "question";
|
|
117
|
-
body = { issue, epoch: assignment.epoch, text };
|
|
138
|
+
body = { issue, epoch: assignment?.node === fleet.self ? assignment.epoch : 0, text };
|
|
118
139
|
if (verb === "plan") comment = { role: "coder", kind, summary: "Plan for review", body: text };
|
|
119
140
|
} else {
|
|
120
141
|
recipient = fleet.nodes.find(peer => peer.id === queued.from);
|
|
@@ -127,7 +148,7 @@ export async function runTalk(options, { root, fleet, repo, gh, say, node = {} }
|
|
|
127
148
|
}
|
|
128
149
|
if (!recipient?.address) throw new Error("recipient needs a fleet address");
|
|
129
150
|
const message = createMessage({ from: fleet.self, repo, kind, body }, fleet.secret);
|
|
130
|
-
if (request) {
|
|
151
|
+
if (request || direct) {
|
|
131
152
|
const { sig: _sig, time: _time, ...fields } = message;
|
|
132
153
|
writeState(root, "outbox", [...readState(root, "outbox"), { node: recipient.id, message: fields, attempts: 0, nextAttempt: (node.clock ?? Date.now)() }]);
|
|
133
154
|
await flushOutbox({ ...node, root, fleet });
|
|
@@ -136,7 +157,7 @@ export async function runTalk(options, { root, fleet, repo, gh, say, node = {} }
|
|
|
136
157
|
const result = gh(["issue", "comment", String(body.issue), "--repo", repo, "--body", roleComment({ ...comment, node: fleet.self, ref: `#${body.issue}` }, { secret: fleet.secret })], { cwd: root });
|
|
137
158
|
if (result == null) throw new Error("gh issue comment failed");
|
|
138
159
|
}
|
|
139
|
-
if (!request) await sendMessage({ ...node, root, fleet, node: recipient, message });
|
|
160
|
+
if (!request && !direct) await sendMessage({ ...node, root, fleet, node: recipient, message });
|
|
140
161
|
if (queued) writeState(root, "queue", readState(root, "queue").filter(message => message.id !== target));
|
|
141
162
|
say(`${kind} ${message.id}`);
|
|
142
163
|
return 0;
|
|
@@ -5,26 +5,32 @@ import path from "node:path";
|
|
|
5
5
|
import { test } from "node:test";
|
|
6
6
|
import { runFleet } from "./fleet.mjs";
|
|
7
7
|
import { readState, writeState } from "./fleet-state.mjs";
|
|
8
|
+
import { receiveTalk } from "./fleet-talk.mjs";
|
|
9
|
+
import { createMessage } from "./fleet-message.mjs";
|
|
10
|
+
import { flushOutbox, receiveMessage } from "./fleet-node.mjs";
|
|
11
|
+
import { runWatch } from "./watch.mjs";
|
|
8
12
|
|
|
9
|
-
function fixture(t, leader = false) {
|
|
13
|
+
function fixture(t, leader = false, self = "a") {
|
|
14
|
+
const other = self === "a" ? "b" : "a";
|
|
10
15
|
const root = mkdtempSync(path.join(os.tmpdir(), "qc-talk-"));
|
|
11
16
|
mkdirSync(path.join(root, ".git"));
|
|
12
17
|
t.after(() => rmSync(root, { recursive: true, force: true, maxRetries: 10, retryDelay: 100 }));
|
|
13
18
|
const config = path.join(root, "fleet.json"), file = path.join(root, "text.txt");
|
|
14
|
-
|
|
19
|
+
const fleet = { self, secret: "ab".repeat(32), nodes: [
|
|
15
20
|
{ id: "a", weight: 1, address: "localhost" }, { id: "b", weight: 1, address: "localhost" },
|
|
16
|
-
] }
|
|
21
|
+
] };
|
|
22
|
+
writeFileSync(config, JSON.stringify(fleet));
|
|
17
23
|
writeFileSync(file, "First line\nSecond line");
|
|
18
24
|
writeState(root, "node", { leader });
|
|
19
|
-
writeState(root, "peers", {
|
|
20
|
-
writeState(root, "assignments", { "o/r#7": { repo: "o/r", issue: 7, node: leader ?
|
|
25
|
+
writeState(root, "peers", { [other]: { leader: !leader, time: Date.now() } });
|
|
26
|
+
writeState(root, "assignments", { "o/r#7": { repo: "o/r", issue: 7, node: leader ? other : self, epoch: 3 } });
|
|
21
27
|
const sent = [], comments = [], lines = [], errors = [];
|
|
22
28
|
const run = (args, extra = {}) => runFleet(args, { cwd: root, env: { QC_FLEET_FILE: config }, origin: () => "o/r",
|
|
23
29
|
say: text => lines.push(text), warn: text => errors.push(text),
|
|
24
30
|
gh: args => { comments.push(args); return ""; },
|
|
25
31
|
node: { fetch: async (url, options) => { sent.push({ url, ...JSON.parse(options.body) }); return { status: 202 }; } }, ...extra });
|
|
26
|
-
const queued = (kind, id = "request") => ({ id, from:
|
|
27
|
-
return { root, file, run, sent, comments, lines, errors, queued };
|
|
32
|
+
const queued = (kind, id = "request") => ({ id, from: other, repo: "o/r", kind, body: { issue: 7, epoch: 3, text: "First?\nSecond?" } });
|
|
33
|
+
return { root, fleet, file, run, sent, comments, lines, errors, queued };
|
|
28
34
|
}
|
|
29
35
|
|
|
30
36
|
test("plan and ask send held issue and epoch to the heartbeat Leader; only plan posts a Coder comment", async t => {
|
|
@@ -60,12 +66,11 @@ test("plan and ask refuse queued Leader-only messages when the node is not Leade
|
|
|
60
66
|
}
|
|
61
67
|
});
|
|
62
68
|
|
|
63
|
-
test("talk rejects invalid arguments, absent
|
|
69
|
+
test("talk rejects invalid arguments, absent Leader, oversized text and unreadable files", async t => {
|
|
64
70
|
const f = fixture(t);
|
|
65
71
|
for (const args of [["plan", "7"], ["ask", "0", "--file", f.file], ["ask", "7", "--changes"], ["plan", "7", "--file", f.file, "extra"], ["plan", "7", "--file", f.file, "--file", f.file]]) {
|
|
66
72
|
assert.equal(await f.run(args), 1);
|
|
67
73
|
}
|
|
68
|
-
assert.equal(await f.run(["ask", "8", "--file", f.file]), 1);
|
|
69
74
|
writeFileSync(f.file, "x".repeat(20001));
|
|
70
75
|
assert.equal(await f.run(["plan", "7", "--file", f.file]), 1);
|
|
71
76
|
assert.match(f.errors.at(-1), /20000/);
|
|
@@ -176,3 +181,147 @@ test("regression: plan and ask are durable before network sends and plan comment
|
|
|
176
181
|
assert.equal(pending[0].attempts, 1);
|
|
177
182
|
}
|
|
178
183
|
});
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
function deliverTo(f) {
|
|
187
|
+
return { fetch: async (_url, options) => {
|
|
188
|
+
const result = receiveMessage({ root: f.root, fleet: f.fleet, message: JSON.parse(options.body) });
|
|
189
|
+
return { status: result.ok ? 202 : 400, json: async () => result };
|
|
190
|
+
} };
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
test("ask and plan for an unheld issue reach the Leader at epoch zero and get a reply", async t => {
|
|
194
|
+
for (const verb of ["ask", "plan"]) {
|
|
195
|
+
for (const assignments of [{}, { "o/r#19": { node: "b", epoch: 5 } }]) {
|
|
196
|
+
const peer = fixture(t), leader = fixture(t, true, "b");
|
|
197
|
+
for (const f of [peer, leader]) writeState(f.root, "assignments", assignments);
|
|
198
|
+
assert.equal(await peer.run([verb, "19", "--file", peer.file], { node: deliverTo(leader) }), 0, peer.errors.join("\n"));
|
|
199
|
+
receiveTalk(leader.root, leader.fleet, true, Date.now());
|
|
200
|
+
const [request] = readState(leader.root, "queue");
|
|
201
|
+
assert.equal(request.body.epoch, 0);
|
|
202
|
+
assert.equal(request.body.issue, 19);
|
|
203
|
+
assert.equal(request.kind, verb === "ask" ? "question" : "plan");
|
|
204
|
+
const reply = verb === "ask" ? "answer" : "approve";
|
|
205
|
+
assert.equal(await leader.run([reply, request.id, "--file", leader.file], { node: deliverTo(peer) }), 0, leader.errors.join("\n"));
|
|
206
|
+
receiveTalk(peer.root, peer.fleet, false, Date.now());
|
|
207
|
+
const saved = verb === "ask" ? readState(peer.root, "answers")[request.id] : readState(peer.root, "plans")["o/r#19"];
|
|
208
|
+
assert.equal(saved.body.epoch, 0);
|
|
209
|
+
assert.equal(saved.from, "b");
|
|
210
|
+
assert.equal(saved.body.text ?? saved.body.note, "First line\nSecond line");
|
|
211
|
+
assert.deepEqual(readState(leader.root, "queue"), []);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
test("held issues still reject a zero or stale epoch", t => {
|
|
217
|
+
const leader = fixture(t, true, "b");
|
|
218
|
+
for (const epoch of [0, 2, 4, -1]) {
|
|
219
|
+
writeState(leader.root, "queue", [{ ...leader.queued("question"), body: { issue: 7, epoch, text: "Question" } }]);
|
|
220
|
+
receiveTalk(leader.root, leader.fleet, true, Date.now());
|
|
221
|
+
assert.deepEqual(readState(leader.root, "queue"), []);
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
test("say works peer to Leader and Leader to peer, persists text, and watch/show report it without comments", async t => {
|
|
226
|
+
const peer = fixture(t), leader = fixture(t, true, "b");
|
|
227
|
+
for (const [sender, receiver, target] of [[peer, leader, "leader"], [leader, peer, "a"]]) {
|
|
228
|
+
writeFileSync(sender.file, "Hello from " + sender.fleet.self);
|
|
229
|
+
assert.equal(await sender.run(["say", target, "--file", sender.file], { node: deliverTo(receiver) }), 0, sender.errors.join("\n"));
|
|
230
|
+
const [message] = readState(receiver.root, "queue");
|
|
231
|
+
assert.equal(message.kind, "say");
|
|
232
|
+
assert.deepEqual(message.body, { text: "Hello from " + sender.fleet.self });
|
|
233
|
+
receiveTalk(receiver.root, receiver.fleet, receiver === leader, Date.now());
|
|
234
|
+
assert.deepEqual(readState(receiver.root, "queue"), []);
|
|
235
|
+
assert.deepEqual(readState(receiver.root, "said")[message.id], message);
|
|
236
|
+
assert.equal(await receiver.run(["show", message.id]), 0, receiver.errors.join("\n"));
|
|
237
|
+
assert.match(receiver.lines.at(-1), new RegExp(`say from=${sender.fleet.self}\\nHello from ${sender.fleet.self}`));
|
|
238
|
+
const events = [];
|
|
239
|
+
assert.equal(await runWatch(["--fleet"], { cwd: receiver.root, say: line => events.push(line),
|
|
240
|
+
gh: () => { throw new Error("say watch must not call GitHub"); }, warn: line => assert.fail(line) }), 0);
|
|
241
|
+
assert.deepEqual(events, [`EVENT fleet:say ${sender.fleet.self} ${message.id}`]);
|
|
242
|
+
assert.equal(sender.comments.length, 0);
|
|
243
|
+
assert.equal(receiver.comments.length, 0);
|
|
244
|
+
}
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
test("say requires a file and a known, addressed, active node with a live heartbeat", async t => {
|
|
248
|
+
const f = fixture(t);
|
|
249
|
+
for (const args of [["say"], ["say", "b"], ["say", "b", "--file"], ["say", "b", "--changes", "--file", f.file],
|
|
250
|
+
["say", "missing", "--file", f.file], ["say", "a", "--file", f.file]]) {
|
|
251
|
+
assert.equal(await f.run(args), 1, args.join(" "));
|
|
252
|
+
}
|
|
253
|
+
for (const state of [{}, { b: { time: Date.now() - 90_001, leader: true } }, { b: { time: Date.now() + 60_000, leader: true } }]) {
|
|
254
|
+
writeState(f.root, "peers", state);
|
|
255
|
+
for (const target of ["b", "leader"]) assert.equal(await f.run(["say", target, "--file", f.file]), 1);
|
|
256
|
+
}
|
|
257
|
+
writeState(f.root, "peers", { b: { time: Date.now(), leader: false } });
|
|
258
|
+
assert.equal(await f.run(["say", "leader", "--file", f.file]), 1);
|
|
259
|
+
for (const extra of [{ active: false }, { address: undefined }]) {
|
|
260
|
+
const fleet = { ...f.fleet, nodes: f.fleet.nodes.map(node => node.id === "b" ? { ...node, ...extra } : node) };
|
|
261
|
+
writeFileSync(path.join(f.root, "fleet.json"), JSON.stringify(fleet));
|
|
262
|
+
assert.equal(await f.run(["say", "b", "--file", f.file]), 1);
|
|
263
|
+
}
|
|
264
|
+
assert.deepEqual(f.sent, []);
|
|
265
|
+
assert.deepEqual(f.comments, []);
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
test("say retries stay on the fleet transport and never fall back to a GitHub comment", async t => {
|
|
269
|
+
const f = fixture(t);
|
|
270
|
+
assert.equal(await f.run(["say", "leader", "--file", f.file], {
|
|
271
|
+
node: { fetch: async () => { throw new Error("offline"); } },
|
|
272
|
+
}), 0, f.errors.join("\n"));
|
|
273
|
+
const [pending] = readState(f.root, "outbox");
|
|
274
|
+
assert.equal(pending.message.kind, "say");
|
|
275
|
+
assert.equal(pending.node, "b");
|
|
276
|
+
writeState(f.root, "outbox", [{ ...pending, attempts: 10, nextAttempt: 0 }]);
|
|
277
|
+
await flushOutbox({ root: f.root, fleet: f.fleet, clock: () => Date.now() + 120_000,
|
|
278
|
+
fetch: async () => ({ status: 202 }), gh: () => assert.fail("say must never post a GitHub comment") });
|
|
279
|
+
assert.deepEqual(readState(f.root, "outbox"), []);
|
|
280
|
+
assert.deepEqual(f.comments, []);
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
test("unsigned, tampered, and unknown senders cannot enqueue say or epoch-zero requests", t => {
|
|
284
|
+
const f = fixture(t, true);
|
|
285
|
+
for (const kind of ["say", "question", "plan"]) {
|
|
286
|
+
const body = kind === "say" ? { text: "Hello" } : { issue: 19, epoch: 0, text: "Question" };
|
|
287
|
+
const message = createMessage({ from: "b", repo: "o/r", kind, body }, f.fleet.secret);
|
|
288
|
+
const { sig: _sig, ...unsigned } = message;
|
|
289
|
+
for (const invalid of [unsigned, { ...message, body: { ...body, text: "tampered" } },
|
|
290
|
+
createMessage({ from: "outsider", repo: "o/r", kind, body }, f.fleet.secret)]) {
|
|
291
|
+
assert.equal(receiveMessage({ root: f.root, fleet: f.fleet, message: invalid }).ok, false);
|
|
292
|
+
assert.deepEqual(readState(f.root, "queue"), []);
|
|
293
|
+
assert.deepEqual(readState(f.root, "seen"), {});
|
|
294
|
+
}
|
|
295
|
+
assert.equal(receiveMessage({ root: f.root, fleet: f.fleet, message }).ok, true);
|
|
296
|
+
writeState(f.root, "queue", []);
|
|
297
|
+
writeState(f.root, "seen", {});
|
|
298
|
+
}
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
test("all talk messages enforce the 20000-character text limit on send and receive", async t => {
|
|
302
|
+
for (const verb of ["ask", "plan", "say", "answer", "approve"]) {
|
|
303
|
+
const f = fixture(t, ["answer", "approve"].includes(verb));
|
|
304
|
+
const target = verb === "say" ? "b" : ["answer", "approve"].includes(verb) ? "request" : "7";
|
|
305
|
+
if (target === "request") writeState(f.root, "queue", [f.queued(verb === "answer" ? "question" : "plan")]);
|
|
306
|
+
writeFileSync(f.file, "x".repeat(20001));
|
|
307
|
+
assert.equal(await f.run([verb, target, "--file", f.file]), 1);
|
|
308
|
+
assert.match(f.errors.at(-1), /20000/);
|
|
309
|
+
assert.deepEqual(f.sent, []);
|
|
310
|
+
assert.deepEqual(f.comments, []);
|
|
311
|
+
writeFileSync(f.file, "x".repeat(20000));
|
|
312
|
+
assert.equal(await f.run([verb, target, "--file", f.file]), 0, f.errors.join("\n"));
|
|
313
|
+
assert.equal((f.sent.at(-1).body.text ?? f.sent.at(-1).body.note).length, 20000);
|
|
314
|
+
}
|
|
315
|
+
for (const kind of ["say", "question", "plan", "answer", "approve"]) {
|
|
316
|
+
const f = fixture(t, ["plan", "question"].includes(kind));
|
|
317
|
+
for (const length of [20001, 20000]) {
|
|
318
|
+
const message = createMessage({ from: "b", repo: "o/r", kind,
|
|
319
|
+
body: { issue: 7, epoch: 3, text: "x".repeat(length), note: "x".repeat(length), question: "request", plan: "request", approved: true } }, f.fleet.secret);
|
|
320
|
+
assert.equal(receiveMessage({ root: f.root, fleet: f.fleet, message }).ok, true);
|
|
321
|
+
receiveTalk(f.root, f.fleet, ["plan", "question"].includes(kind), Date.now());
|
|
322
|
+
const saved = kind === "say" ? Object.values(readState(f.root, "said")) : ["question", "plan"].includes(kind)
|
|
323
|
+
? readState(f.root, "queue") : Object.values(readState(f.root, kind === "answer" ? "answers" : "plans"));
|
|
324
|
+
assert.equal(saved.length, length === 20000 ? 1 : 0, `${kind} text length ${length}`);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
});
|
package/src/cli/fleet.mjs
CHANGED
|
@@ -17,7 +17,7 @@ import { createKey, validateKey, pairingDecision, readPairing } from "./fleet-pa
|
|
|
17
17
|
import { effectiveFleet, readDiscovered } from "./fleet-discover.mjs";
|
|
18
18
|
|
|
19
19
|
const SIZES = ["S", "M", "L"];
|
|
20
|
-
const USAGE = "usage: qc fleet init [--self <id>] [--nodes <id:weight:size,...>] [--port <port>] [--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 | allow <code> | deny <code> | accept <code> | confirm-leader | progress <issue> --step plan|code|review|merge [--branch <b>] [--head <sha40>] | owns <issue> | pushed <issue> --branch <b> --head <sha40>";
|
|
20
|
+
const USAGE = "usage: qc fleet init [--self <id>] [--nodes <id:weight:size,...>] [--port <port>] [--secret <hex>] [--addresses <id@host:port,...>] [--force] | next [--repo <o/r>] [--all] | status [--repo <o/r>] | plan <issue> --file <path> | ask <issue> --file <path> | say <node-id|leader> --file <path> | approve <message-id> [--changes] [--file <path>] | answer <message-id> --file <path> | show <message-id> | start [--leader] | serve [--leader] | stop | allow <code> | deny <code> | accept <code> | confirm-leader | progress <issue> --step plan|code|review|merge [--branch <b>] [--head <sha40>] | owns <issue> | pushed <issue> --branch <b> --head <sha40>";
|
|
21
21
|
|
|
22
22
|
function validAddress(address) {
|
|
23
23
|
if (typeof address !== "string" || !address || address.trim() !== address || address === "0.0.0.0") return false;
|
package/src/cli/watch.mjs
CHANGED
|
@@ -252,7 +252,8 @@ export async function runWatch(argv, { gh = readGh, say = console.log, warn = co
|
|
|
252
252
|
const watched = readState(cwd, "watched");
|
|
253
253
|
const leader = readState(cwd, "node").leader === true;
|
|
254
254
|
const messages = leader
|
|
255
|
-
? readState(cwd, "queue").filter(message => ["plan", "question"].includes(message.kind))
|
|
255
|
+
? [...readState(cwd, "queue").filter(message => ["plan", "question"].includes(message.kind)),
|
|
256
|
+
...readState(cwd, "inbox").filter(message => message.kind === "say")]
|
|
256
257
|
: readState(cwd, "inbox");
|
|
257
258
|
messages.push(...pairingMessages(cwd, leader, now()));
|
|
258
259
|
const proposal = readState(cwd, "proposal");
|
|
@@ -264,7 +265,7 @@ export async function runWatch(argv, { gh = readGh, say = console.log, warn = co
|
|
|
264
265
|
for (const message of fresh) {
|
|
265
266
|
if (Object.hasOwn(watched, message.id)) continue;
|
|
266
267
|
Object.defineProperty(watched, message.id, { value: true, enumerable: true, configurable: true });
|
|
267
|
-
say(`EVENT fleet:${message.kind} ${message.detail ?? message.term ?? message.id}`);
|
|
268
|
+
say(`EVENT fleet:${message.kind} ${message.kind === "say" ? `${message.from} ${message.id}` : message.detail ?? message.term ?? message.id}`);
|
|
268
269
|
}
|
|
269
270
|
writeState(cwd, "watched", watched);
|
|
270
271
|
return EXIT.PASSED;
|