crewbus 10.0.10 → 10.0.12

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.
@@ -114,6 +114,46 @@ function resolveAgent(args, what) {
114
114
  return cleanName(getFlag(args, "--from") || process.env.CREWBUS_AGENT, what);
115
115
  }
116
116
 
117
+ // C1 lite recovery (self-contained: no bin/lib imports — the hook ships
118
+ // standalone). Same scoring spirit as `crewbus recover`, minus the
119
+ // token-hash cross-check: session binding +4, token file +1, cwd match +1.
120
+ // Returns [{ name, role, score, proven, why }] best-first.
121
+ function recoverCandidatesLite(root, opts = {}) {
122
+ const cands = [];
123
+ try {
124
+ const agentsDir = path.join(root, "agents");
125
+ const logsDir = path.join(root, "logs");
126
+ const tokenNames = new Set();
127
+ try {
128
+ for (const f of fs.readdirSync(logsDir)) if (f.endsWith(".token")) tokenNames.add(f.slice(0, -6).toLowerCase());
129
+ } catch {}
130
+ const cwd = String(opts.cwd || "").replace(/\\/g, "/").toLowerCase();
131
+ for (const f of fs.readdirSync(agentsDir)) {
132
+ if (!f.endsWith(".json")) continue;
133
+ const doc = readJsonSafe(path.join(agentsDir, f));
134
+ if (!doc || !doc.name || doc.offboarded) continue;
135
+ let score = 0;
136
+ const why = [];
137
+ if (opts.sessionID && doc.sessionId && String(doc.sessionId) === String(opts.sessionID)) {
138
+ score += 4;
139
+ why.push("session id matches register binding");
140
+ }
141
+ if (tokenNames.has(String(doc.name).toLowerCase())) {
142
+ score += 1;
143
+ why.push("token file exists for this name");
144
+ }
145
+ if (doc.lastDir && cwd && String(doc.lastDir).replace(/\\/g, "/").toLowerCase() === cwd) {
146
+ score += 1;
147
+ why.push("worked from this directory");
148
+ }
149
+ if (score <= 0) continue;
150
+ cands.push({ name: doc.name, role: doc.role || "peer", score, proven: false, why: why.join("; ") });
151
+ }
152
+ } catch {}
153
+ cands.sort((a, b) => b.score - a.score || String(a.name).localeCompare(String(b.name)));
154
+ return cands;
155
+ }
156
+
117
157
  function readJsonSafe(p) {
118
158
  try {
119
159
  return JSON.parse(fs.readFileSync(p, "utf8"));
@@ -466,10 +506,25 @@ async function cmdMonitor(args) {
466
506
 
467
507
  async function cmdCompact(args) {
468
508
  const root = boardDir(args);
469
- const agent = resolveAgent(args, "agent");
509
+ const agent0 = resolveAgent(args, "agent");
510
+ const sessionID = getFlag(args, "--session");
470
511
  // Best-effort reads only: a compaction hook must never break its harness,
471
512
  // so missing boards/docs degrade to "what is known", never a loud failure.
513
+ // When the invoked name is an amnesia sentinel ("unknown"), try board-side
514
+ // recovery BEFORE blessing it (post-compaction rehydration: C1).
515
+ let agent = agent0;
516
+ const RES = ["unknown", "me", "you"];
517
+ if (RES.includes(agent)) {
518
+ const cands = recoverCandidatesLite(root, { sessionID, cwd: process.cwd() });
519
+ if (cands[0] && cands[0].score >= 2) agent = cands[0].name;
520
+ }
472
521
  const doc = readJsonSafe(path.join(root, "agents", `${agent}.json`));
522
+ if (RES.includes(agent)) {
523
+ console.log(`crewbus: context refreshed after compaction, but this hook could not identify '${agent0}' on board ${root}.`);
524
+ console.log(`Recover (read-only, never mints): crewbus recover${sessionID ? ` --session ${sessionID}` : ""} — it ranks candidate identities (register session binding, token files, cwd history) and prints exact resume commands.`);
525
+ console.log(`NEVER re-register as 'unknown'/'me'/'you' — those sentinel names are reserved and refuse to mint; recover your real identity instead.`);
526
+ return;
527
+ }
473
528
  console.log(`crewbus: context refreshed after compaction. You are '${agent}' on board ${root}.`);
474
529
  console.log(`Token: read ${path.join(root, "logs", `${agent}.token`)} into CREWBUS_TOKEN (0600 file written at register/mint). Lost it? Ask your lead/admin to revoke it, then re-register for a fresh one.`);
475
530
  console.log(`Then: crewbus inbox --from ${agent} --unacked --digest (escalate to full reads on hits).`);
@@ -509,7 +564,7 @@ async function main() {
509
564
  " crewbus-hook monitor --from <you> [--timeout <sec>] [--interval <sec>] [--board <path>]\n" +
510
565
  " (blocking event stream for grok-build's monitor tool: prints each new batch to stdout on arrival,\n" +
511
566
  " silent otherwise. Defaults: 0s timeout (run until killed), 1s interval.)\n" +
512
- " crewbus-hook compact --from <you> [--board <path>]\n" +
567
+ " crewbus-hook compact --from <you> [--session <id>] [--board <path>]\n" +
513
568
  " (post-compaction identity card: who/where/token-path plus the inbox\n" +
514
569
  " next step; spawned workers also get their brief promptPath. Never\n" +
515
570
  " fails on missing files — prints what is known.)"
@@ -751,12 +751,24 @@ function authorizeMcp(d, agent, action, scope) {
751
751
  const role = getRoleMcp(d, agent);
752
752
  const act = String(action || "");
753
753
  const ADMIN_ONLY = new Set(["prune", "acl-set", "role-grant", "offboard", "group-restrict", "serve-remote"]);
754
- const LEAD_PLUS = new Set(["spawn", "respawn", "pool", "group-manage", "channel-post", "result-record", "race-close"]);
754
+ const LEAD_PLUS = new Set(["spawn", "respawn", "pool", "group-manage", "result-record", "race-close"]);
755
755
  const WORKER_WRITES = new Set(["send", "ack", "redeliver", "lock"]);
756
756
  if (role !== "admin") {
757
757
  if (ADMIN_ONLY.has(act)) throw new Error(`role "${role}" cannot ${act} (need admin)`);
758
758
  if (LEAD_PLUS.has(act) && role !== "lead") throw new Error(`role "${role}" cannot ${act} (need lead|admin)`);
759
759
  if (act === "spawn-kill" && role !== "lead") throw new Error(`role "${role}" cannot spawn-kill (need lead|admin)`);
760
+ if (act === "channel-post" && role !== "lead") {
761
+ // Member-scoped channel posting (C4): members post to their group's
762
+ // channel; board channels + foreign groups stay lead|admin.
763
+ const targetGroup = scope && scope.channelGroup ? String(scope.channelGroup) : undefined;
764
+ if (!targetGroup) {
765
+ throw new Error(`role "${role}" may post only to a group channel it belongs to (board channels are lead|admin)`);
766
+ }
767
+ const gdoc = readGroupMcp(d, targetGroup);
768
+ if (!gdoc) throw new Error(`channel group "${targetGroup}" not found (groups register their channels — board channels are lead|admin)`);
769
+ const members = Array.isArray(gdoc.members) ? gdoc.members : [];
770
+ if (!members.includes(agent)) throw new Error(`group channel "${targetGroup}" — members only (member or lead|admin)`);
771
+ }
760
772
  if (WORKER_WRITES.has(act) && role !== "lead" && role !== "worker") {
761
773
  throw new Error(`role "${role}" cannot ${act} (auditor is read-only)`);
762
774
  }
@@ -1048,7 +1060,16 @@ function callTool(name, args) {
1048
1060
  const mirror = (batch) => {
1049
1061
  if (a.also_channel !== true) return "";
1050
1062
  const mirrored = [];
1063
+ const skipped = [];
1051
1064
  for (const g of groupNames) {
1065
+ // Member-scoped mirrors (C4): non-members' DMs deliver; the channel
1066
+ // mirror is skipped with a visible note. Lead/admin pass trivially.
1067
+ try {
1068
+ authorizeMcp(d, from, "channel-post", { channelGroup: g });
1069
+ } catch (err) {
1070
+ skipped.push(g);
1071
+ continue;
1072
+ }
1052
1073
  const post = { id: newId("ch"), from, body, at };
1053
1074
  if (subject) post.subject = subject;
1054
1075
  if (replyTo) post.replyTo = replyTo;
@@ -1058,7 +1079,7 @@ function callTool(name, args) {
1058
1079
  appendChannelPostMcp(d, groupChannelNameMcp(g), post);
1059
1080
  mirrored.push(groupChannelNameMcp(g));
1060
1081
  }
1061
- return mirrored.length > 0 ? ` +channel ${mirrored.join(",")}` : "";
1082
+ return (mirrored.length > 0 ? ` +channel ${mirrored.join(",")}` : "") + (skipped.length > 0 ? ` [channel mirror skipped, members-only: ${skipped.join(",")}]` : "");
1062
1083
  };
1063
1084
  const isAll = recipients.length === 1 && recipients[0] === "@all";
1064
1085
  if (isAll || recipients.length > BROADCAST_AFTER) {
@@ -1294,7 +1315,8 @@ function callTool(name, args) {
1294
1315
  const subject = a.subject === undefined || a.subject === null || String(a.subject).trim() === "" ? undefined : String(a.subject).trim().slice(0, 120);
1295
1316
  const priority = cleanPriorityMcp(a.priority);
1296
1317
  const minted = ensureSender(d, from, resolveToken(a));
1297
- authorizeMcp(d, from, "channel-post"); // lead|admin only (worker refused)
1318
+ // Member-scoped posting (C4): grp-<g> channels admit members of <g>.
1319
+ authorizeMcp(d, from, "channel-post", { channelGroup: chan.startsWith("grp-") ? chan.slice(4) : undefined });
1298
1320
  touchAgent(d, from);
1299
1321
  const tokenHint = minted.created ? ` identity '${from}' claimed, token ${minted.token} (set CREWBUS_TOKEN=${minted.token})` : "";
1300
1322
  const post = { id: newId("ch"), from, body, at: new Date().toISOString() };