crewbus 10.0.10 → 10.0.11
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/CHANGELOG.md +28 -0
- package/bin/crewbus-hook.js +57 -2
- package/bin/crewbus-mcp.js +25 -3
- package/bin/crewbus.js +321 -57
- package/bin/lib/channels.js +11 -1
- package/bin/lib/identity.js +145 -1
- package/bin/lib/kidproc.js +92 -0
- package/bin/lib/launch.js +9 -4
- package/bin/lib/relay.js +4 -2
- package/bin/lib/spawn.js +54 -16
- package/bin/lib/store.js +2 -1
- package/bin/lib/web.js +6 -3
- package/docs/GROUPS.md +4 -2
- package/docs/IDENTITY.md +36 -0
- package/docs/LEAD_ASSIGN.md +10 -0
- package/docs/LONG_RUNS.md +3 -2
- package/docs/RBAC.md +73 -69
- package/docs/TROUBLESHOOTING.md +1 -0
- package/opencode/plugins/dm-watch.js +70 -8
- package/package.json +2 -2
package/bin/lib/channels.js
CHANGED
|
@@ -9,6 +9,7 @@ import path from "node:path";
|
|
|
9
9
|
import crypto from "node:crypto";
|
|
10
10
|
import { readJson, writeJson, newId, fail, cleanChannelName, listJson } from "./store.js";
|
|
11
11
|
import { isHigh, relTime } from "./mail.js";
|
|
12
|
+
import { authorizeCheck } from "./identity.js"; // member-scoped channel mirrors (C4; call-time use only — hoisted declarations tolerate the identity→groups→channels cycle)
|
|
12
13
|
|
|
13
14
|
// cleanChannelName lives in store.js (canonical home for all clean* helpers).
|
|
14
15
|
|
|
@@ -103,7 +104,16 @@ export function summarizePosts(posts, limit) {
|
|
|
103
104
|
|
|
104
105
|
export function mirrorToGroupChannels(d, { groups, from, body, subject, replyTo, batch, priority, rev, at }) {
|
|
105
106
|
const mirrored = [];
|
|
107
|
+
const skipped = [];
|
|
106
108
|
for (const g of groups || []) {
|
|
109
|
+
// Member-scoped mirrors (C4): a worker's brief lands on grp-<g> channel
|
|
110
|
+
// only if it is a member of <g> (or lead/admin). Non-members' DMs still
|
|
111
|
+
// deliver — the channel mirror is skipped with a visible note.
|
|
112
|
+
const gate = authorizeCheck(d, from, "channel-post", { channelGroup: g });
|
|
113
|
+
if (!gate || !gate.ok) {
|
|
114
|
+
skipped.push({ group: g, reason: (gate && gate.reason) || "member posting required" });
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
107
117
|
const chan = groupChannelName(g);
|
|
108
118
|
const post = { id: newId("ch"), from, body, at };
|
|
109
119
|
if (subject) post.subject = subject;
|
|
@@ -114,7 +124,7 @@ export function mirrorToGroupChannels(d, { groups, from, body, subject, replyTo,
|
|
|
114
124
|
appendChannelPost(d, chan, post);
|
|
115
125
|
mirrored.push({ group: g, channel: chan, id: post.id });
|
|
116
126
|
}
|
|
117
|
-
return mirrored;
|
|
127
|
+
return { mirrored, skipped };
|
|
118
128
|
}
|
|
119
129
|
|
|
120
130
|
export function lockHash(scope) {
|
package/bin/lib/identity.js
CHANGED
|
@@ -175,6 +175,8 @@ export function checkToken(d, agent, token) { // line 361
|
|
|
175
175
|
// as register, zero extra round-trip). Returns { created } so callers can
|
|
176
176
|
// print the token exactly once — it is never shown again via send.
|
|
177
177
|
export function ensureSender(d, agent, token) { // line 387
|
|
178
|
+
const sentinel = reservedNameReason(agent);
|
|
179
|
+
if (sentinel) fail(sentinel);
|
|
178
180
|
const rec = readAgent(d, agent);
|
|
179
181
|
if (!rec) {
|
|
180
182
|
if (isBoardFrozen(d)) fail(`board is frozen — new registrations refused (ask an admin to register --from <admin> --for ${agent})`);
|
|
@@ -228,6 +230,128 @@ export function ensureSender(d, agent, token) { // line 387
|
|
|
228
230
|
return { created: false };
|
|
229
231
|
}
|
|
230
232
|
|
|
233
|
+
// ---------------------------------------------------------------------------
|
|
234
|
+
// Identity recovery (C1): an agent that survives compaction/restart amnesia
|
|
235
|
+
// loses its name. The compaction hooks rehydrate from a session map; when
|
|
236
|
+
// even that is lost the harness texts used to mint throwaway identities
|
|
237
|
+
// (logs/unknown.token) that nobody owns. Two fixes: reserved sentinel names
|
|
238
|
+
// never mint, and a read-only recovery cross-check finds the real identity
|
|
239
|
+
// from what the board still knows.
|
|
240
|
+
// ---------------------------------------------------------------------------
|
|
241
|
+
|
|
242
|
+
// Scan logs/<name>.token: { name -> token | null } (best-effort; a missing
|
|
243
|
+
// or unreadable file is just "no evidence").
|
|
244
|
+
function listTokenFiles(d) {
|
|
245
|
+
const out = new Map();
|
|
246
|
+
try {
|
|
247
|
+
for (const f of fs.readdirSync(path.join(d.root, "logs"))) {
|
|
248
|
+
const m = /^([A-Za-z0-9_-]+)\.token$/.exec(f);
|
|
249
|
+
if (!m) continue;
|
|
250
|
+
try {
|
|
251
|
+
const t = fs.readFileSync(path.join(d.root, "logs", f), "utf8").trim();
|
|
252
|
+
if (t) out.set(m[1], t);
|
|
253
|
+
} catch {}
|
|
254
|
+
}
|
|
255
|
+
} catch {}
|
|
256
|
+
return out;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
// { name, role, sessionId, lastDir, lastSeen, tokenHash, salt } for the scan.
|
|
260
|
+
function findAgentRecords(d) {
|
|
261
|
+
const records = [];
|
|
262
|
+
try {
|
|
263
|
+
for (const f of fs.readdirSync(d.agents)) {
|
|
264
|
+
if (!f.endsWith(".json")) continue;
|
|
265
|
+
try {
|
|
266
|
+
const doc = readJson(path.join(d.agents, f));
|
|
267
|
+
if (!doc || !doc.name || doc.offboarded) continue;
|
|
268
|
+
records.push({
|
|
269
|
+
name: String(doc.name),
|
|
270
|
+
role: roleOfRecord(doc),
|
|
271
|
+
sessionId: doc.sessionId || undefined,
|
|
272
|
+
lastDir: doc.lastDir || undefined,
|
|
273
|
+
lastSeen: doc.lastSeen || undefined,
|
|
274
|
+
tokenHash: doc.tokenHash || undefined,
|
|
275
|
+
salt: doc.salt || undefined,
|
|
276
|
+
});
|
|
277
|
+
} catch {}
|
|
278
|
+
}
|
|
279
|
+
} catch {}
|
|
280
|
+
return records;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// Rank identity candidates for an amnesiac agent. Read-only, never throws.
|
|
284
|
+
// opts: { sessionID, cwd, hashToken?, loadTokenFile? } (injectors let the
|
|
285
|
+
// CLI do the token-file↔tokenHash cross-check; plugin copies ship a lite
|
|
286
|
+
// variant without hash-verify).
|
|
287
|
+
// Returns [{ name, role, score, proven, why }] best-first.
|
|
288
|
+
export function recoverIdentity(d, opts = {}) {
|
|
289
|
+
const records = findAgentRecords(d);
|
|
290
|
+
const tokens = listTokenFiles(d);
|
|
291
|
+
const cwd = opts.cwd ? String(opts.cwd) : undefined;
|
|
292
|
+
const sessionID = opts.sessionID ? String(opts.sessionID) : undefined;
|
|
293
|
+
const canVerify = typeof opts.hashToken === "function" && typeof opts.loadTokenFile === "function";
|
|
294
|
+
const cands = [];
|
|
295
|
+
for (const rec of records) {
|
|
296
|
+
let score = 0;
|
|
297
|
+
let proven = false;
|
|
298
|
+
const why = [];
|
|
299
|
+
// Register-time session binding (register --session <id>) is the
|
|
300
|
+
// strongest cross-check: it names THIS harness session explicitly.
|
|
301
|
+
if (sessionID && rec.sessionId && rec.sessionId === sessionID) {
|
|
302
|
+
score += 4;
|
|
303
|
+
why.push("session id matches register binding");
|
|
304
|
+
}
|
|
305
|
+
// Token-file evidence: existence is a hint; a real cross-check against
|
|
306
|
+
// the stored hash is proof (hashToken(token, salt) === tokenHash).
|
|
307
|
+
if (tokens.has(rec.name)) {
|
|
308
|
+
score += 1;
|
|
309
|
+
why.push("token file exists for this name");
|
|
310
|
+
if (canVerify && rec.tokenHash && rec.salt) {
|
|
311
|
+
try {
|
|
312
|
+
const t = opts.loadTokenFile(d.root, rec.name);
|
|
313
|
+
if (t && opts.hashToken(t, rec.salt) === rec.tokenHash) {
|
|
314
|
+
proven = true;
|
|
315
|
+
score += 3;
|
|
316
|
+
why.push("token file cross-checks the stored hash");
|
|
317
|
+
}
|
|
318
|
+
} catch {}
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
if (cwd && rec.lastDir && arePathsSameDir(rec.lastDir, cwd)) {
|
|
322
|
+
score += 1;
|
|
323
|
+
why.push("worked from this directory");
|
|
324
|
+
}
|
|
325
|
+
if (score <= 0) continue;
|
|
326
|
+
cands.push({ name: rec.name, role: rec.role, score, proven, why: why.join("; ") });
|
|
327
|
+
}
|
|
328
|
+
cands.sort((a, b) => b.score - a.score || String(a.name).localeCompare(String(b.name)));
|
|
329
|
+
return cands;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// Case-insensitive directory comparison for Windows case-insensitivity +
|
|
333
|
+
// symlink noise tolerance (best-effort: trailing slashes, backslashes).
|
|
334
|
+
function arePathsSameDir(a, b) {
|
|
335
|
+
const norm = (p) => String(p).replace(/[\\/]+$/, "").toLowerCase();
|
|
336
|
+
if (norm(a) === norm(b)) return true;
|
|
337
|
+
// Partial tolerance: a registered cwd may be a subpath of the board root.
|
|
338
|
+
const aN = norm(a).split(/[\\/]/);
|
|
339
|
+
const bN = norm(b).split(/[\\/]/);
|
|
340
|
+
return aN.length !== bN.length && (aN.join("/").includes(bN.join("/")) || bN.join("/").includes(aN.join("/")));
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// Reserved sentinel names never mint — they come from amnesia bugs, and
|
|
344
|
+
// minting them loses real identity data forever (the 10.0.9-run "lead"
|
|
345
|
+
// lockout came from exactly this).
|
|
346
|
+
export const RESERVED_SENTINELS = ["unknown", "me", "you"];
|
|
347
|
+
|
|
348
|
+
export function reservedNameReason(agent) {
|
|
349
|
+
const s = String(agent || "").trim().toLowerCase();
|
|
350
|
+
return RESERVED_SENTINELS.includes(s)
|
|
351
|
+
? `"${agent}" is a reserved sentinel name (amnesia-throwaway) — identity lost? run: crewbus recover, then re-register/claim your real name`
|
|
352
|
+
: null;
|
|
353
|
+
}
|
|
354
|
+
|
|
231
355
|
// ---------------------------------------------------------------------------
|
|
232
356
|
// Phase 1b RBAC + per-board ACLs + group-scoped send permissions.
|
|
233
357
|
//
|
|
@@ -329,7 +453,7 @@ export function authorizeCheck(d, agent, action, scope) { // line 537
|
|
|
329
453
|
const role = getRole(d, agent);
|
|
330
454
|
const act = String(action || "");
|
|
331
455
|
const ADMIN_ONLY = new Set(["prune", "acl-set", "role-grant", "offboard", "group-restrict", "serve-remote", "import", "snapshot-schedule", "quota-set", "hold-place", "hold-lift", "pairing"]);
|
|
332
|
-
const LEAD_PLUS = new Set(["spawn", "respawn", "pool", "group-manage", "
|
|
456
|
+
const LEAD_PLUS = new Set(["spawn", "respawn", "pool", "group-manage", "result-record", "race-close"]);
|
|
333
457
|
const WORKER_WRITES = new Set(["send", "ack", "redeliver", "lock", "artifact-write", "lesson-write"]);
|
|
334
458
|
const EXPORT_ROLES = new Set(["admin", "auditor"]);
|
|
335
459
|
if (act === "export") {
|
|
@@ -340,6 +464,26 @@ export function authorizeCheck(d, agent, action, scope) { // line 537
|
|
|
340
464
|
return { ok: false, reason: `role "${role}" cannot ${act} (need admin)` };
|
|
341
465
|
} else if (LEAD_PLUS.has(act)) {
|
|
342
466
|
if (role !== "lead") return { ok: false, reason: `role "${role}" cannot ${act} (need lead|admin)` };
|
|
467
|
+
} else if (act === "channel-post") {
|
|
468
|
+
// Member-scoped channel posting (C4, default-on): workers may post to the
|
|
469
|
+
// channels of groups they are a MEMBER of (scope.channelGroup); lead/admin
|
|
470
|
+
// post anywhere (board channels + every group channel). Everyone else,
|
|
471
|
+
// and workers targeting a foreign group's channel or a board channel with
|
|
472
|
+
// no member mapping, are refused.
|
|
473
|
+
if (role === "admin" || role === "lead") {
|
|
474
|
+
// board-wide + group channels
|
|
475
|
+
} else if (role !== "worker") {
|
|
476
|
+
return { ok: false, reason: `role "${role}" cannot ${act} (member-posting is scoped to group members; need lead|admin)` };
|
|
477
|
+
} else {
|
|
478
|
+
const targetGroup = scope && scope.channelGroup ? String(scope.channelGroup) : undefined;
|
|
479
|
+
if (!targetGroup) {
|
|
480
|
+
return { ok: false, reason: `role "worker" may post only to a group channel it belongs to (${targetGroup === undefined ? "board channels are lead|admin" : "need lead|admin"})` };
|
|
481
|
+
}
|
|
482
|
+
const gdoc = readGroup(d, targetGroup);
|
|
483
|
+
if (!gdoc) return { ok: false, reason: `channel group "${targetGroup}" not found (groups register their channels — board channels are lead|admin)` };
|
|
484
|
+
const members = Array.isArray(gdoc.members) ? gdoc.members : [];
|
|
485
|
+
if (!members.includes(agent)) return { ok: false, reason: `group channel "${targetGroup}" — members only (member or lead|admin)` };
|
|
486
|
+
}
|
|
343
487
|
} else if (act === "spawn-kill") {
|
|
344
488
|
if (role !== "lead") return { ok: false, reason: `role "${role}" cannot spawn-kill (need lead|admin)` };
|
|
345
489
|
const targets = (scope && Array.isArray(scope.targets)) ? scope.targets : [];
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
// bin/lib/kidproc.js — shell-mode child processes without DEP0190.
|
|
2
|
+
//
|
|
3
|
+
// Node >=22 deprecates `spawn(exe, args, { shell: true })` when `args` is
|
|
4
|
+
// non-empty (DEP0190) — args are passed through the shell UNESCAPED (the
|
|
5
|
+
// warning exists because the old wire is unsafe: `"arg one"` emitted raw
|
|
6
|
+
// splits into two argv tokens on the receiving process). This module
|
|
7
|
+
// reproduces the intended semantics with CORRECT quoting:
|
|
8
|
+
//
|
|
9
|
+
// Windows old: spawn(exe, args, { shell:true }) (unescaped raw concat)
|
|
10
|
+
// new: spawn("cmd.exe", ["/d","/s","/c", quoted line],
|
|
11
|
+
// { windowsVerbatimArguments: true })
|
|
12
|
+
// POSIX old: spawn(exe, args, { shell:true })
|
|
13
|
+
// new: spawn("/bin/sh", ["-c", quoted line], { shell:false })
|
|
14
|
+
//
|
|
15
|
+
// (On win32 `windowsVerbatimArguments` matters: without it node re-quotes the
|
|
16
|
+
// already-quoted outer string and cmd would double-parse the escapes.)
|
|
17
|
+
//
|
|
18
|
+
// NOTE: this is intentionally NOT bit-identical to the deprecated wire —
|
|
19
|
+
// the old wire mangled args (space splitting, quote eating) and let bare
|
|
20
|
+
// cmd metacharacters (& | ^) inject into the command line. The regression
|
|
21
|
+
// bar here is argv IDENTITY: what the child receives equals what the
|
|
22
|
+
// caller passed. Known cmd limitation documented here: a matched %VAR%
|
|
23
|
+
// pattern inside a quoted token still expands at the cmd layer (no escape
|
|
24
|
+
// exists for cmd quoted context) — unmatched % passes through intact.
|
|
25
|
+
// Standalone by design (see docs/SPLIT.md): zero local imports, cycle-free.
|
|
26
|
+
// Escape hatch for exactly one release: CREWBUS_SHELL_LEGACY=1 restores the
|
|
27
|
+
// pre-refactor `shell: true` behavior at call sites.
|
|
28
|
+
|
|
29
|
+
const WIN_SHELL = "cmd.exe";
|
|
30
|
+
const POSIX_SHELL = "/bin/sh";
|
|
31
|
+
|
|
32
|
+
// MS CRT / CreateProcess argument quoting, plus the cmd.exe pass layer:
|
|
33
|
+
// wrap on whitespace, quotes, AND cmd metacharacters (& | ^ < > %) — bare
|
|
34
|
+
// metachars split commands even inside the overall quoted /c line (probes:
|
|
35
|
+
// "a&b|c^d" quoted argv arrives intact; bare flexes). Inside the wrap:
|
|
36
|
+
// backslashes double before quotes per MS standard, % stays raw (cmd does
|
|
37
|
+
// NOT expand unmatched %; a matched %VAR% *does* expand even inside quotes —
|
|
38
|
+
// a known cmd limitation, no escape hatch exists — see docs note), ^ is
|
|
39
|
+
// literal inside quotes. Bare tokens without any of those pass unwrapped
|
|
40
|
+
// (already valid single argv tokens).
|
|
41
|
+
function msQuote(s) {
|
|
42
|
+
s = String(s);
|
|
43
|
+
if (s === "") return '""';
|
|
44
|
+
if (!/[\s"&|^<>%]/.test(s)) return s;
|
|
45
|
+
let out = '"';
|
|
46
|
+
let backslashes = 0;
|
|
47
|
+
for (const c of s) {
|
|
48
|
+
if (c === "\\") { backslashes++; continue; }
|
|
49
|
+
if (c === '"') {
|
|
50
|
+
out += "\\".repeat(backslashes * 2 + 1) + '"';
|
|
51
|
+
} else {
|
|
52
|
+
out += "\\".repeat(backslashes) + c;
|
|
53
|
+
}
|
|
54
|
+
backslashes = 0;
|
|
55
|
+
}
|
|
56
|
+
// Backslashes before the closing wrap double up too.
|
|
57
|
+
out += "\\".repeat(backslashes * 2);
|
|
58
|
+
return out + '"';
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// POSIX single-quote joining (' -> '\'').
|
|
62
|
+
function shQuote(s) {
|
|
63
|
+
s = String(s);
|
|
64
|
+
// A bare empty token would vanish outright (two words `a ''` parse to
|
|
65
|
+
// [a, ""] — `a ` parses to [a] only), losing the operand.
|
|
66
|
+
// (note: `;` is deliberately NOT in the safe class — POSIX shells treat a
|
|
67
|
+
// bare `;` as a command separator; cmd tolerates it. cmd metacharacters
|
|
68
|
+
// (& | ^ < > %) never appear here — msQuote owns the win line.)
|
|
69
|
+
if (s === "") return "''";
|
|
70
|
+
return /^[-#%+,.:=?@_a-zA-Z0-9\/]*$/.test(s) ? s : `'${s.replace(/'/g, `'\\''`)}'`;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// { exe, args } one should hand to spawn(..., { shell: false }) to keep the
|
|
74
|
+
// old shell:true semantics for (exe, args). Returns null when args is empty
|
|
75
|
+
// — callers keep their existing behavior in that case (empty args never hit
|
|
76
|
+
// DEP0190).
|
|
77
|
+
function shellChildSpec(exe, args, platform = process.platform) {
|
|
78
|
+
const a = (args || []).map(String);
|
|
79
|
+
if (a.length === 0) return null;
|
|
80
|
+
const line = [String(exe), ...a];
|
|
81
|
+
if (platform === "win32") {
|
|
82
|
+
return { exe: WIN_SHELL, args: ["/d", "/s", "/c", `"${line.map(msQuote).join(" ")}"`] };
|
|
83
|
+
}
|
|
84
|
+
return { exe: POSIX_SHELL, args: ["-c", line.map(shQuote).join(" ")] };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Spawn-time options a caller must add when using shellChildSpec output.
|
|
88
|
+
function shellChildOptions(platform = process.platform) {
|
|
89
|
+
return platform === "win32" ? { windowsVerbatimArguments: true } : {};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export { msQuote, shQuote, shellChildSpec, shellChildOptions };
|
package/bin/lib/launch.js
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
import fs from "node:fs";
|
|
7
7
|
import path from "node:path";
|
|
8
8
|
import { execFileSync } from "node:child_process";
|
|
9
|
+
import { shellChildSpec, shellChildOptions } from "./kidproc.js";
|
|
9
10
|
|
|
10
11
|
export const LAUNCH_PERMISSIONS = ["supervised", "autoEdits", "auto", "full"];
|
|
11
12
|
export const LAUNCH_LIFETIMES = ["oneshot", "persistent"];
|
|
@@ -42,14 +43,18 @@ export function launchDrivers() {
|
|
|
42
43
|
|
|
43
44
|
// Probe one binary for presence + version. Never throws: missing binaries
|
|
44
45
|
// report { found: false } ("not installed", not failure).
|
|
45
|
-
// On Windows, global npm CLIs are batch scripts (.cmd) which require shell
|
|
46
|
+
// On Windows, global npm CLIs are batch scripts (.cmd) which require a shell;
|
|
47
|
+
// the legacy spawn(exe, args, {shell:true}) shape is DEP0190 on modern Node —
|
|
48
|
+
// run cmd.exe as an explicit argv0 with the same wire format (bin/lib/kidproc.js).
|
|
46
49
|
export function probeBinary(binary, probeArgs) {
|
|
47
50
|
if (!binary) return { found: false, version: null, detail: "operator-supplied --cmd" };
|
|
48
51
|
try {
|
|
49
52
|
const isWin = process.platform === "win32";
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
}
|
|
53
|
+
const args = probeArgs && probeArgs.length > 0 ? probeArgs : ["--version"];
|
|
54
|
+
const spec = isWin ? shellChildSpec(binary, args) : { exe: binary, args };
|
|
55
|
+
const opts = { stdio: ["ignore", "pipe", "pipe"], timeout: 8000 };
|
|
56
|
+
if (isWin) Object.assign(opts, shellChildOptions());
|
|
57
|
+
const out = execFileSync(spec.exe, spec.args, opts);
|
|
53
58
|
const first = String(out || "").split("\n")[0].trim().slice(0, 80);
|
|
54
59
|
return { found: true, version: first || "installed", detail: null };
|
|
55
60
|
} catch (e) {
|
package/bin/lib/relay.js
CHANGED
|
@@ -42,7 +42,7 @@ import https from "node:https";
|
|
|
42
42
|
import { readJson, writeJson, listJson, getFlag, cleanWebName, gitRevForBoard, MAX_BODY_CHARS, MAX_FWD_DEPTH, webErr } from "./store.js";
|
|
43
43
|
import { readAgent, agentTokenMatches, mintToken, newSalt, hashToken, timingSafeEqualStr, writeAgentFile, isBoardFrozen, defaultRoleForNew, authorizeThrow, authorizeCheck, touchAgent } from "./identity.js";
|
|
44
44
|
import { httpJson } from "./sync.js";
|
|
45
|
-
import { killWorkers, pidAlive, bootWorker, parseAllowEnv } from "./spawn.js";
|
|
45
|
+
import { killWorkers, pidAlive, bootWorker, parseAllowEnv, isPidStale } from "./spawn.js";
|
|
46
46
|
import { expandGroups, heuristicSenderType } from "./groups.js";
|
|
47
47
|
import { deliverDMs, findMessageById } from "./mail.js";
|
|
48
48
|
import { appendChainRecord } from "./export.js";
|
|
@@ -176,7 +176,9 @@ export async function remoteSpawn(d, a, serveOpts) { // line 6770
|
|
|
176
176
|
if (recipients.length > maxSpawn) throw webErr(400, `spawn caps at ${maxSpawn} workers per call (got ${recipients.length})`);
|
|
177
177
|
for (const name of recipients) {
|
|
178
178
|
const rec = readAgent(d, name);
|
|
179
|
-
|
|
179
|
+
// Stale-pid guard (C2): pid reuse after a reboot is "not live" for spawn
|
|
180
|
+
// purposes — a different process recycled the pid, the worker is gone.
|
|
181
|
+
if (rec && typeof rec.spawnedPid === "number" && pidAlive(rec.spawnedPid) && !isPidStale(rec.spawnedPid, rec.spawnedAt)) {
|
|
180
182
|
throw webErr(400, `name "${name}" has a live worker (pid ${rec.spawnedPid})`);
|
|
181
183
|
}
|
|
182
184
|
}
|
package/bin/lib/spawn.js
CHANGED
|
@@ -28,6 +28,26 @@ import { fail, readJson, writeJson, writeExclusiveJson, cleanBranchPrefix } from
|
|
|
28
28
|
import { touchAgent, readAgent, writeAgentFile, assertMintWon, mintToken, newSalt, hashToken } from "./identity.js";
|
|
29
29
|
import { readVisible, isCheckpoint } from "./mail.js";
|
|
30
30
|
import { saveTokenFile, loadTokenFile } from "./tokenfile.js";
|
|
31
|
+
import { shellChildSpec, shellChildOptions } from "./kidproc.js";
|
|
32
|
+
|
|
33
|
+
// DEP0190 escape hatch: process through the pre-refactor shell:true path for
|
|
34
|
+
// exactly one release (see bin/lib/kidproc.js). Truthy string opts in.
|
|
35
|
+
function legacyShellMode() {
|
|
36
|
+
return /^(1|true|yes)$/i.test(String(process.env.CREWBUS_SHELL_LEGACY || ""));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function resolveShellShot(exe, args, shell) {
|
|
40
|
+
// Converts shell-needing targets to an explicit shell argv0 (no DEP0190);
|
|
41
|
+
// shell:false targets (resolved real exes) pass through untouched — their
|
|
42
|
+
// spawned pid IS the harness process (kill semantics depend on it).
|
|
43
|
+
// Returns { exe, args, shell, extraOptions } for spawn().
|
|
44
|
+
if (shell && !legacyShellMode()) {
|
|
45
|
+
const spec = shellChildSpec(exe, args);
|
|
46
|
+
if (spec) return { ...spec, shell: false, extraOptions: shellChildOptions() };
|
|
47
|
+
}
|
|
48
|
+
return { exe, args, shell: !!shell, extraOptions: {} };
|
|
49
|
+
}
|
|
50
|
+
export { resolveShellShot };
|
|
31
51
|
|
|
32
52
|
// cleanBranchPrefix lives in store.js (canonical home).
|
|
33
53
|
|
|
@@ -260,7 +280,7 @@ export function buildCrewContext({ name, from, lead: leadTarget, subject, groups
|
|
|
260
280
|
// `workers` is [{ name, id, harness }]; `batch` the batch id ("" when solo).
|
|
261
281
|
// Pure: unit-tested, no board IO — the caller boots it via bootWorker
|
|
262
282
|
// (prompt override) and stamps the log file itself.
|
|
263
|
-
export function buildCoordinatorPrompt({ name, from, subject, body, batch, workers, crew, cwd, root, auto, workbench }) {
|
|
283
|
+
export function buildCoordinatorPrompt({ name, from, subject, body, batch, workers, crew, cwd, root, auto, workbench, budgetTokens, budgetMinutes }) {
|
|
264
284
|
const boss = String(from || "boss");
|
|
265
285
|
const me = String(name || LEAD_NAME);
|
|
266
286
|
const ws = Array.isArray(workers) ? workers.filter((w) => w && w.name) : [];
|
|
@@ -278,6 +298,9 @@ export function buildCoordinatorPrompt({ name, from, subject, body, batch, worke
|
|
|
278
298
|
...(batch ? ["Worker briefs (thread ids, for --reply):", ...ws.map((w) => `- ${w.name}: brief ${w.id}${w.harness ? ` [${w.harness}]` : ""}`), ""] : []),
|
|
279
299
|
...(wbLines.length > 0 ? ["Workbench (lessons + artifacts from prior work on this board):", ...wbLines, ""] : []),
|
|
280
300
|
`Your identity: token saved at logs/${me}.token — read it into CREWBUS_TOKEN at the top of each shell that sends (PowerShell: \$env:CREWBUS_TOKEN = Get-Content .crewbus/logs/${me}.token). All sends run as --from ${me}. Never re-register over your own name — re-read the token file after any restart or compaction.`,
|
|
301
|
+
...(budgetTokens !== undefined || budgetMinutes !== undefined
|
|
302
|
+
? [`Budget mandate:${budgetTokens !== undefined ? ` ~${budgetTokens} est. tokens total${budgetMinutes !== undefined ? " and" : ""}` : ""}${budgetMinutes !== undefined ? ` ${budgetMinutes} wall-clock minutes total` : ""} — coordinate within it: batch your polls (listen --timeout 120000), digest-read first, and post the aggregate to ${boss} BEFORE the budget runs out (a budgeted coordinator gets reaped at the cap).`, ``]
|
|
303
|
+
: []),
|
|
281
304
|
``,
|
|
282
305
|
`Jobs, in order:`,
|
|
283
306
|
`0. Boot habit: crewbus agents + crewbus group list, so you know who is on the board (workers claim over the first minutes; re-check when triaging).`,
|
|
@@ -363,9 +386,11 @@ export function provisionAutoLead(d, opts) {
|
|
|
363
386
|
}
|
|
364
387
|
}
|
|
365
388
|
// Always (re)record how this coordinator came to be, without clobbering
|
|
366
|
-
// an existing live one.
|
|
389
|
+
// an existing live one. Standing = pid alive AND not stale (reboot + OS
|
|
390
|
+
// pid reuse: a different (unrelated) process recycled the pid — see
|
|
391
|
+
// isPidStale; stale never blocks a fresh boot, it just disqualifies reuse).
|
|
367
392
|
const rec = readAgent(d, leadName);
|
|
368
|
-
const standing = rec && typeof rec.spawnedPid === "number" && pidAlive(rec.spawnedPid);
|
|
393
|
+
const standing = rec && typeof rec.spawnedPid === "number" && pidAlive(rec.spawnedPid) && !isPidStale(rec.spawnedPid, rec.spawnedAt);
|
|
369
394
|
if (minted) saveTokenFile(d.root, leadName, token);
|
|
370
395
|
let booted = null;
|
|
371
396
|
if (standing) {
|
|
@@ -381,9 +406,10 @@ export function provisionAutoLead(d, opts) {
|
|
|
381
406
|
batch: o.batch, workers: o.workers, crew: o.crew,
|
|
382
407
|
cwd: (o.spawnOpts && o.spawnOpts.cwd) || process.cwd(),
|
|
383
408
|
root: d.root, auto: !!o.auto, workbench: o.workbench,
|
|
409
|
+
budgetTokens: o.budgetTokens, budgetMinutes: o.budgetMinutes,
|
|
384
410
|
}),
|
|
385
411
|
spawnedLifetime: "persistent",
|
|
386
|
-
budgetTokens:
|
|
412
|
+
budgetTokens: o.budgetTokens, budgetMinutes: o.budgetMinutes, deadlineAt: undefined,
|
|
387
413
|
});
|
|
388
414
|
} catch (e) {
|
|
389
415
|
const detail = String((e && (e.message || e)) || e).slice(0, 160);
|
|
@@ -431,7 +457,10 @@ export function resolveOpencodeExe() {
|
|
|
431
457
|
if (_opencodeExe !== undefined) return _opencodeExe;
|
|
432
458
|
_opencodeExe = null;
|
|
433
459
|
try {
|
|
434
|
-
|
|
460
|
+
// where/which are real binaries — no shell was ever needed for this
|
|
461
|
+
// probe (execFileSync + args + shell:true was DEP0190 on modern Node).
|
|
462
|
+
const probe = process.platform === "win32" ? "where" : "which";
|
|
463
|
+
const out = execFileSync(probe, ["opencode"], { encoding: "utf8", stdio: ["ignore", "pipe", "ignore"], timeout: 10000 });
|
|
435
464
|
for (const raw of String(out || "").split(/\r?\n/)) {
|
|
436
465
|
const shim = raw.trim();
|
|
437
466
|
if (!shim) continue;
|
|
@@ -947,17 +976,20 @@ export function syncWorkerSession(d, name, logPath, storageProbe) { // respawn s
|
|
|
947
976
|
export function launchWorkerProcess({ exe, args, shell, cwd, env, logPath, stdinPath }) { // respawn slice 2
|
|
948
977
|
const logFd = fs.openSync(logPath, "a");
|
|
949
978
|
let inFd = null;
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
979
|
+
let child = null;
|
|
980
|
+
try {
|
|
981
|
+
// claude reads the brief from stdin; everyone else takes paths/args, so
|
|
982
|
+
// the long prompt never travels through shell quoting. shell:true with
|
|
983
|
+
// non-empty args is DEP0190 on modern Node — resolveShellShot keeps the
|
|
984
|
+
// old cmd/sh wire format while spawning the shell as an explicit argv0.
|
|
985
|
+
const stdio = ["ignore", logFd, logFd];
|
|
986
|
+
if (stdinPath) {
|
|
987
|
+
inFd = fs.openSync(stdinPath, "r");
|
|
988
|
+
stdio[0] = inFd;
|
|
989
|
+
}
|
|
990
|
+
const shot = resolveShellShot(exe, args || [], shell);
|
|
991
|
+
child = spawn(shot.exe, shot.args, { cwd, env, detached: true, stdio, shell: shot.shell, windowsHide: true, ...shot.extraOptions });
|
|
992
|
+
} catch (e) {
|
|
961
993
|
try { fs.closeSync(logFd); } catch {}
|
|
962
994
|
try { if (inFd !== null) fs.closeSync(inFd); } catch {}
|
|
963
995
|
throw e;
|
|
@@ -1379,6 +1411,12 @@ export async function killWorkers(d, names) { // line 4529
|
|
|
1379
1411
|
out.push({ name, result: "already-exited", pid });
|
|
1380
1412
|
continue;
|
|
1381
1413
|
}
|
|
1414
|
+
// Safety guard (C2): a live pid that provably postdates the spawn record
|
|
1415
|
+
// belongs to an UNRELATED process (reboot + OS pid reuse). Never kill it.
|
|
1416
|
+
if (isPidStale(pid, doc.spawnedAt)) {
|
|
1417
|
+
out.push({ name, result: "pid-stale", pid, detail: "pid recycled after a reboot — it is not this worker; nothing killed" });
|
|
1418
|
+
continue;
|
|
1419
|
+
}
|
|
1382
1420
|
try {
|
|
1383
1421
|
if (process.platform === "win32") {
|
|
1384
1422
|
// /T takes the whole tree: shell shims (cmd) outlive nothing.
|
package/bin/lib/store.js
CHANGED
|
@@ -120,12 +120,13 @@ export function dirs(root) { // line 122
|
|
|
120
120
|
holds: path.join(root, "holds"),
|
|
121
121
|
artifacts: path.join(root, "artifacts"),
|
|
122
122
|
lessons: path.join(root, "lessons"),
|
|
123
|
+
dmArchive: path.join(root, "dm-archive"),
|
|
123
124
|
};
|
|
124
125
|
}
|
|
125
126
|
|
|
126
127
|
export function ensureBoard(root) { // line 142
|
|
127
128
|
const d = dirs(root);
|
|
128
|
-
for (const p of [d.root, d.agents, d.dm, d.delivered, d.broadcast, d.groups, d.channels, d.locks, d.results, d.tombstones, d.poolState, d.workerSessions, d.index, d.cursors, d.revoked, d.holds, d.artifacts, d.lessons, path.join(root, "logs")]) {
|
|
129
|
+
for (const p of [d.root, d.agents, d.dm, d.dmArchive, d.delivered, d.broadcast, d.groups, d.channels, d.locks, d.results, d.tombstones, d.poolState, d.workerSessions, d.index, d.cursors, d.revoked, d.holds, d.artifacts, d.lessons, path.join(root, "logs")]) {
|
|
129
130
|
fs.mkdirSync(p, { recursive: true });
|
|
130
131
|
}
|
|
131
132
|
const metaPath = path.join(d.root, "board.json");
|
package/bin/lib/web.js
CHANGED
|
@@ -19,7 +19,7 @@ import { fileURLToPath } from "node:url";
|
|
|
19
19
|
import { boardDir, requireBoard, getFlag, fail, writeJson, listJson, cleanWebName, readJson, readBoardMeta, writeBoardMeta, gitRevForBoard, newId, MAX_SPAWN, MAX_BODY_CHARS } from "./store.js";
|
|
20
20
|
import { readAgent, agentTokenMatches, authorizeCheck, ensureLoopbackOperator, mintToken, newSalt, hashToken, touchAgent } from "./identity.js";
|
|
21
21
|
import { readDMs, readVisible, ackedIds, ackedIdSet, findMessageById, deliverDMs, parseRecipients } from "./mail.js";
|
|
22
|
-
import { workerStatus, pidAlive, killWorkers, bootWorker, buildSpawnPrompt, buildSpawnTarget, formatSpawnCmd, installCliToUserPath, ensureBoardCliShim, buildCoordinatorPrompt, provisionAutoLead, LEAD_NAME } from "./spawn.js";
|
|
22
|
+
import { workerStatus, pidAlive, killWorkers, bootWorker, buildSpawnPrompt, buildSpawnTarget, formatSpawnCmd, installCliToUserPath, ensureBoardCliShim, buildCoordinatorPrompt, provisionAutoLead, LEAD_NAME, isPidStale } from "./spawn.js";
|
|
23
23
|
import { groupTelemetryData, collectBatch, gatherTelemetry, listBatches } from "./groups.js";
|
|
24
24
|
import { httpJson } from "./sync.js";
|
|
25
25
|
import { readChainRecords, verifyChainRecords, appendChainRecord, readHold, readBoardQuotas } from "./export.js";
|
|
@@ -261,7 +261,8 @@ export function resultsSnapshot(d) {
|
|
|
261
261
|
const a = e && e.data;
|
|
262
262
|
if (a && a.name && typeof a.spawnedPid === "number") {
|
|
263
263
|
try {
|
|
264
|
-
|
|
264
|
+
// Stale-pid guard (C2): pid-recycled records read dead.
|
|
265
|
+
aliveByName[a.name] = pidAlive(a.spawnedPid) && !isPidStale(a.spawnedPid, a.spawnedAt);
|
|
265
266
|
} catch {
|
|
266
267
|
aliveByName[a.name] = false;
|
|
267
268
|
}
|
|
@@ -2597,7 +2598,9 @@ export async function handleApiLaunch(d, body) {
|
|
|
2597
2598
|
let coordinator = undefined;
|
|
2598
2599
|
if (leadMode === "board") {
|
|
2599
2600
|
const existingLead = readAgent(d, LEAD_NAME);
|
|
2600
|
-
|
|
2601
|
+
// Stale-pid guard (C2): reuse only a live, non-stale coordinator; a
|
|
2602
|
+
// pid-recycled after reboot record gets the fresh-provision branch.
|
|
2603
|
+
const standing = existingLead && typeof existingLead.spawnedPid === "number" && pidAlive(existingLead.spawnedPid) && !isPidStale(existingLead.spawnedPid, existingLead.spawnedAt);
|
|
2601
2604
|
if (standing) {
|
|
2602
2605
|
coordinator = { name: LEAD_NAME, action: "reused", pid: existingLead.spawnedPid, note: "live coordinator already running" };
|
|
2603
2606
|
} else if (existingLead && existingLead.offboarded) {
|
package/docs/GROUPS.md
CHANGED
|
@@ -95,8 +95,10 @@ block assumes:
|
|
|
95
95
|
for approvals (`--priority high`) and scope disputes only.
|
|
96
96
|
- **Channels are the shared read surface.** `crewbus channel tail
|
|
97
97
|
grp-<group>` shows a group's briefs + mirrored progress; reads need
|
|
98
|
-
no token. Channel *posting* is
|
|
99
|
-
|
|
98
|
+
no token. Channel *posting* is member-scoped (default-on, no flag):
|
|
99
|
+
a worker may post to `grp-<group>` only when it is a MEMBER of that
|
|
100
|
+
group — board channels and foreign groups stay lead|admin-only
|
|
101
|
+
(see docs/RBAC.md).
|
|
100
102
|
- **Boot habit.** `crewbus agents` + `crewbus group list` once at
|
|
101
103
|
start, so a worker knows who else is here. A worker that never runs
|
|
102
104
|
a discovery command will work star-topology through the lead — that
|
package/docs/IDENTITY.md
CHANGED
|
@@ -128,3 +128,39 @@ long-running agent persists its token to a file it can re-read.
|
|
|
128
128
|
`.crewbus/logs/<name>.token`), then `CREWBUS_TOKEN` env. Re-register
|
|
129
129
|
is **impossible** for claimed names (first-claim-wins rejects it), so there
|
|
130
130
|
is no third option — protect the file.
|
|
131
|
+
|
|
132
|
+
## Post-compaction identity recovery (`crewbus recover`)
|
|
133
|
+
|
|
134
|
+
Compaction amnesia used to mint throwaway identities (`unknown` on the board,
|
|
135
|
+
`logs/unknown.token` nobody owns). Two layers now stop that:
|
|
136
|
+
|
|
137
|
+
- **Sentinel names never mint:** `unknown`, `me`, `you` are reserved —
|
|
138
|
+
`register` and `send` refuse them loudly (`ensureSender` guards them, so
|
|
139
|
+
every write path is covered). They exist only for tools that forgot who
|
|
140
|
+
they are, to force the recovery path instead of a silent identity loss.
|
|
141
|
+
- **Read-only candidate ranking:** `crewbus recover [--session <id>]
|
|
142
|
+
[--cwd <dir>]` scans `agents/*.json` ∩ `logs/*.token` and prints scored
|
|
143
|
+
candidates (best first): session-id register binding +4, token-file
|
|
144
|
+
cross-check against the stored `tokenHash`/`salt` +3 (proof), token file
|
|
145
|
+
exists +1, `lastDir` cwd match +1. It never writes or mints; it prints the
|
|
146
|
+
exact resume commands (CREWBUS_AGENT, token-file path, `inbox --unacked
|
|
147
|
+
--digest`). Offboarded agents are excluded for privacy-critical boards.
|
|
148
|
+
|
|
149
|
+
```
|
|
150
|
+
crewbus: triage on [board …/sandbox-1/.crewbus]
|
|
151
|
+
proven pilg (role worker, score 9) — session id matches register binding;
|
|
152
|
+
token file cross-checks the stored hash; worked from this directory
|
|
153
|
+
resume as 'pilg': …
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
- **Rehydrators resolve before naming "unknown":** the compaction card
|
|
157
|
+
(opencode plugin + `crewbus-hook compact`) first checks its session↔agent
|
|
158
|
+
map, then `CREWBUS_AGENT`, then a lite board-side score (binding + token
|
|
159
|
+
file + cwd, no hash cross-check in the standalone copies). Only when all
|
|
160
|
+
miss does it print the `crewbus recover` instruction — never a "you are
|
|
161
|
+
unknown" blessing.
|
|
162
|
+
- **Bindings survive daily traffic:** register stores `sessionId`; later
|
|
163
|
+
sessionless sends/re-registers/admin touches must NOT wipe it
|
|
164
|
+
(`mergeAgentDoc` deletes undefined patch keys — callers add `sessionId`
|
|
165
|
+
to the patch only when given). Heartbeats merge field-by-field and never
|
|
166
|
+
touch unrelated fields.
|
package/docs/LEAD_ASSIGN.md
CHANGED
|
@@ -60,6 +60,16 @@ crewbus spawn --from boss --count 3 --harness generic --lead none --body "Run te
|
|
|
60
60
|
- If the `lead` lane has been marked `offboarded: true` (deliberate teardown), auto-provisioning is skipped with a loud warning.
|
|
61
61
|
4. **Boss Detachment**:
|
|
62
62
|
- If `from === "lead"`, auto-provisioning skips into self-bootstrap mode to prevent recursion.
|
|
63
|
+
5. **Lead Revive (`crewbus spawn --count 0`)**:
|
|
64
|
+
- When the coordinator died (crash, reboot, budget reap), the crew still has
|
|
65
|
+
its briefs/threads on the board — but nobody triaging. `--count 0` boots
|
|
66
|
+
NOTHING except the auto-coordinator lane: zero worker boots, zero DMs
|
|
67
|
+
delivered; the mission text you pass becomes the coordinator's reminder
|
|
68
|
+
of what the crew was on about (worker threads and scope live on the board).
|
|
69
|
+
- Requires `--lead board` (the default under a coding harness); worker
|
|
70
|
+
operands (`--to`/`--to-file`/`--to-group`) are refused — this path has no
|
|
71
|
+
worker operands. A standing coordinator is reused, never duplicated;
|
|
72
|
+
an offboarded lane skips with a loud warning, same as any burst.
|
|
63
73
|
|
|
64
74
|
---
|
|
65
75
|
|
package/docs/LONG_RUNS.md
CHANGED
|
@@ -41,11 +41,12 @@ the board.
|
|
|
41
41
|
|
|
42
42
|
| Symptom | Fix |
|
|
43
43
|
|---|---|
|
|
44
|
-
| Post-compaction amnesia |
|
|
44
|
+
| Post-compaction amnesia | Identity card is auto-injected (hook compact / opencode plugin); unknown who you are? run `crewbus recover` — read-only candidate ranking (session binding → token-file↔tokenHash → cwd), then follow the printed resume script |
|
|
45
|
+
| Landed as "unknown" | Sentinel names ("unknown"/"me"/"you") never mint (register/send refuse them). Run `crewbus recover [--session <id>]` to re-find your real identity; only then re-register your true name |
|
|
45
46
|
| Token lost entirely | Lead `token revoke --target`, worker re-registers |
|
|
46
47
|
| Worker silent | `spawn-status`: no reply → read its log; re-brief or `respawn` |
|
|
47
48
|
| Supervisor gone | `pool-resume --id`; stale locks expire via TTL |
|
|
48
|
-
| Reboot | `doctor` → `spawn-status --all` (stale pids read dead, not running) → `respawn`/`pool-resume` |
|
|
49
|
+
| Reboot | `doctor` → `spawn-status --all` (stale pids read dead, not running) → `respawn`/`pool-resume`; dead coordinator: `crewbus spawn --count 0 --body "<what the crew was on>"` (C9 lead revive, workers untouched) |
|
|
49
50
|
| Primary relay down | `relay promote` on the standby (see `docs/HA.md`) |
|
|
50
51
|
|
|
51
52
|
## Bottom line
|