crewbus 10.0.8 → 10.0.10

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/bin/lib/store.js CHANGED
@@ -118,12 +118,14 @@ export function dirs(root) { // line 122
118
118
  cursors: path.join(root, "cursors"),
119
119
  revoked: path.join(root, "revoked"),
120
120
  holds: path.join(root, "holds"),
121
+ artifacts: path.join(root, "artifacts"),
122
+ lessons: path.join(root, "lessons"),
121
123
  };
122
124
  }
123
125
 
124
126
  export function ensureBoard(root) { // line 142
125
127
  const d = dirs(root);
126
- 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, path.join(root, "logs")]) {
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")]) {
127
129
  fs.mkdirSync(p, { recursive: true });
128
130
  }
129
131
  const metaPath = path.join(d.root, "board.json");
@@ -224,7 +226,7 @@ export const VALUE_FLAGS = new Set(["--from", "--to", "--to-file", "--to-group",
224
226
  // reads the same comma/newline-separated list from a file so large fan-outs
225
227
  // don't hit Windows argv limits (~8191 chars).
226
228
  // §4.4 value-taking flags (skipped with their value by restArgs).
227
- for (const _f of ["--sender-type", "--fwd", "--secret", "--device", "--pair-token", "--pair-label", "--label", "--ttl", "--id", "--allow-env", "--allow-cmd", "--workdir-root", "--budget-tokens", "--budget-minutes", "--tls-cert", "--tls-key", "--tls-ca", "--mtls-ca", "--mtls-cert", "--mtls-key", "--oidc-issuer", "--oidc-audience", "--issuer", "--client-id", "--bearer", "--oidc-token", "--expires-in", "--service", "--offboard", "--target", "--reason", "--role", "--for", "--default-role", "--freeze", "--unfreeze", "--out", "--in", "--into", "--key-env", "--key-file", "--every", "--keep", "--out-dir", "--max-bytes", "--max-agents", "--max-channels", "--tenant", "--audit-forward", "--audit-forward-key", "--standby", "--promote-on-miss", "--fence", "--relay-interval", "--relays", "--weights", "--relay-auth", "--via", "--weight"]) VALUE_FLAGS.add(_f); // line 673
229
+ for (const _f of ["--sender-type", "--fwd", "--secret", "--device", "--pair-token", "--pair-label", "--label", "--ttl", "--id", "--allow-env", "--allow-cmd", "--workdir-root", "--budget-tokens", "--budget-minutes", "--tls-cert", "--tls-key", "--tls-ca", "--mtls-ca", "--mtls-cert", "--mtls-key", "--oidc-issuer", "--oidc-audience", "--issuer", "--client-id", "--bearer", "--oidc-token", "--expires-in", "--service", "--offboard", "--target", "--reason", "--role", "--for", "--default-role", "--freeze", "--unfreeze", "--out", "--in", "--into", "--key-env", "--key-file", "--every", "--keep", "--out-dir", "--max-bytes", "--max-agents", "--max-channels", "--tenant", "--audit-forward", "--audit-forward-key", "--standby", "--promote-on-miss", "--fence", "--relay-interval", "--relays", "--weights", "--relay-auth", "--via", "--weight", "--needs-by", "--deps", "--path", "--text", "--note"]) VALUE_FLAGS.add(_f); // line 673
228
230
 
229
231
  export function cleanSenderType(raw) { // line 1181
230
232
  if (raw === undefined || raw === null || String(raw).trim() === "") return undefined;
@@ -306,7 +308,7 @@ export function requireBoard(root) { // line 3105
306
308
  );
307
309
  }
308
310
  const d = dirs(root);
309
- for (const p of [d.root, d.agents, d.dm, d.delivered, d.broadcast, d.groups, d.tombstones, d.poolState, d.index, d.cursors, d.revoked]) {
311
+ for (const p of [d.root, d.agents, d.dm, d.delivered, d.broadcast, d.groups, d.tombstones, d.poolState, d.index, d.cursors, d.revoked, d.artifacts, d.lessons]) {
310
312
  if (!p) continue;
311
313
  fs.mkdirSync(p, { recursive: true });
312
314
  }
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 } from "./spawn.js";
22
+ import { workerStatus, pidAlive, killWorkers, bootWorker, buildSpawnPrompt, buildSpawnTarget, formatSpawnCmd, installCliToUserPath, ensureBoardCliShim, buildCoordinatorPrompt, provisionAutoLead, LEAD_NAME } 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";
@@ -782,7 +782,12 @@ details.tech-details summary{cursor:pointer;font-weight:500;user-select:none}
782
782
  <button type="button" data-perm="auto">auto</button>
783
783
  <button type="button" data-perm="full">full</button>
784
784
  </span>
785
- <label class="dim" style="cursor:pointer"><input id="launch-dry" type="checkbox" checked> Dry-run</label>
785
+ <span class="dim" style="margin-left:8px">Coordinator:</span>
786
+ <select id="launch-lead" style="background:var(--panel);color:var(--ink);border:1px solid var(--line);border-radius:4px;padding:3px 8px;font-size:12px" title="Observer coordinator lane: auto provisions a persistent background coordinator (lead) to triage workers and report to you">
787
+ <option value="auto">auto (Observer coordinator)</option>
788
+ <option value="none">none (direct to me)</option>
789
+ </select>
790
+ <label class="dim" style="cursor:pointer;margin-left:6px"><input id="launch-dry" type="checkbox" checked> Dry-run</label>
786
791
  </div>
787
792
 
788
793
  <div class="composer-cwd-row" style="display:flex;align-items:center;gap:6px;margin-bottom:8px;font-size:12px">
@@ -801,7 +806,7 @@ details.tech-details summary{cursor:pointer;font-weight:500;user-select:none}
801
806
 
802
807
  <div class="composer-bottom">
803
808
  <div class="launch-identity" title="Desktop launches as the board owner (auto-provisioned) — no token step needed">
804
- <span class="dim">launching as <b id="launch-as">lead</b> (auto)</span>
809
+ <span class="dim">launching as <b id="launch-as">boss</b> (auto)</span>
805
810
  <input id="launch-from" type="hidden" value="">
806
811
  <input id="launch-token" type="password" style="display:none" value="">
807
812
  </div>
@@ -1152,7 +1157,8 @@ async function refresh(){
1152
1157
  }).join('')||'<tr><td colspan="7" class="dim">no spawned workers</td></tr>';
1153
1158
  Array.prototype.forEach.call(document.querySelectorAll('[data-kill]'),function(b){b.onclick=function(){kill([b.getAttribute('data-kill')]);};});
1154
1159
  document.getElementById('agents').innerHTML=s.agents.map(function(a){
1155
- return '<tr><td><b>'+esc(a.name)+'</b></td><td>'+(a.active?DOT_SVG+' active':RING_SVG+' stale')+'</td><td title="'+esc(a.lastSeen||'?')+'">'+esc(ago(a.lastSeen))+'</td><td>'+esc(a.sessionId||'—')+'</td><td>'+a.dmCount+'</td><td>'+a.unacked+'</td></tr>';
1160
+ var roleBadge = a.role ? ' <span class="dim" style="font-size:11px">['+esc(a.role)+']</span>' : '';
1161
+ return '<tr><td><b>'+esc(a.name)+'</b>'+roleBadge+'</td><td>'+(a.active?DOT_SVG+' active':RING_SVG+' stale')+'</td><td title="'+esc(a.lastSeen||'?')+'">'+esc(ago(a.lastSeen))+'</td><td>'+esc(a.sessionId||'—')+'</td><td>'+a.dmCount+'</td><td>'+a.unacked+'</td></tr>';
1156
1162
  }).join('')||'<tr><td colspan="6" class="dim">no agents yet</td></tr>';
1157
1163
  document.getElementById('bcast').innerHTML=s.broadcasts.map(function(b){
1158
1164
  var to=Array.isArray(b.to)?b.to.join(','):String(b.to||'');
@@ -1204,8 +1210,22 @@ async function refresh(){
1204
1210
  return '<tr><td>'+esc(m.id)+'</td><td>'+esc(m.from)+'</td><td>'+(m.subject?'<b>'+esc(m.subject)+'</b><br>':'')+esc(m.head)+'</td><td><button data-ack="'+esc(m.id)+'">ack</button></td></tr>';
1205
1211
  }).join('')||'<tr><td colspan="4" class="dim">inbox zero for '+esc(c.from)+'</td></tr>';
1206
1212
  var ap=(tj.items||[]).filter(function(m){return m.subject&&m.subject.indexOf('approval: ')===0;});
1213
+ if(c.from!=='lead'){
1214
+ try{
1215
+ var lr=await fetch('/api/inbox?agent=lead&unacked=1&limit=50',{cache:'no-store'});
1216
+ if(lr.ok){
1217
+ var lj=await lr.json();
1218
+ var lap=(lj.items||[]).filter(function(m){return m.subject&&m.subject.indexOf('approval: ')===0;});
1219
+ for(var k=0;k<lap.length;k++){
1220
+ lap[k]._forLead=true;
1221
+ if(!ap.some(function(x){return x.id===lap[k].id;})) ap.push(lap[k]);
1222
+ }
1223
+ }
1224
+ }catch(_){}
1225
+ }
1207
1226
  document.getElementById('approvals').innerHTML=ap.map(function(m){
1208
- return '<tr><td>'+esc(m.id)+'</td><td>'+esc(m.from)+'</td><td>'+(m.subject?'<b>'+esc(m.subject)+'</b><br>':'')+esc(m.head)+'</td><td><input data-reason="'+esc(m.id)+'" maxlength="500" size="18" placeholder="optional reason"></td><td><button data-approve="'+esc(m.id)+'">approve</button> <button class="danger" data-deny="'+esc(m.id)+'">deny</button></td></tr>';
1227
+ var tag=m._forLead?' <span class="badge" style="font-size:10px;background:var(--panel);border:1px solid var(--line);padding:1px 4px;border-radius:3px">to lead</span>':'';
1228
+ return '<tr><td>'+esc(m.id)+tag+'</td><td>'+esc(m.from)+'</td><td>'+(m.subject?'<b>'+esc(m.subject)+'</b><br>':'')+esc(m.head)+'</td><td><input data-reason="'+esc(m.id)+'" maxlength="500" size="18" placeholder="optional reason"></td><td><button data-approve="'+esc(m.id)+'">approve</button> <button class="danger" data-deny="'+esc(m.id)+'">deny</button></td></tr>';
1209
1229
  }).join('')||'<tr><td colspan="5" class="dim">no approval requests for '+esc(c.from)+'</td></tr>';
1210
1230
  }else{
1211
1231
  document.getElementById('triage').innerHTML='<tr><td colspan="4" class="dim">set identity above to triage</td></tr>';
@@ -1466,12 +1486,15 @@ async function doLaunch(dry){
1466
1486
  }
1467
1487
  var cwdEl=document.getElementById('launch-cwd');
1468
1488
  var cwdVal=cwdEl?cwdEl.value.trim():'';
1489
+ var leadEl=document.getElementById('launch-lead');
1490
+ var leadVal=leadEl?leadEl.value.trim():'auto';
1469
1491
  var payload={
1470
1492
  from:lf, token:lt,
1471
1493
  harness:selectedHarnesses.join(','),
1472
1494
  harnesses:selectedHarnesses,
1473
1495
  body:document.getElementById('launch-brief').value,
1474
1496
  permission:document.getElementById('launch-permission').value,
1497
+ lead:leadVal,
1475
1498
  dryRun:dry
1476
1499
  };
1477
1500
  if(cwdVal){
@@ -1488,11 +1511,22 @@ async function doLaunch(dry){
1488
1511
  if(j.dryRun){
1489
1512
  var warn=(j.warnings||[]).length?('warnings: '+j.warnings.join('; ')+'\\n'):'';
1490
1513
  var cwdNotice=j.cwd?('target directory (cwd): '+j.cwd+'\\n'):'';
1491
- try{renderLaunchDiff(j);}catch(_){}out.textContent=warn+cwdNotice+(j.commands||[]).map(function(c){return c.to+' ['+(c.harness||'')+']: '+c.command;}).join('\\n')+'\\n(dry-run — booted nothing)';
1514
+ var coordNotice=j.coordinator?('\\ncoordinator ('+j.coordinator.name+'): '+(j.coordinator.action==='reused'?('reused standing pid '+j.coordinator.pid):(j.coordinator.action==='minted'?'would auto-provision':'skipped ('+(j.coordinator.reason||'')+')'))):'';
1515
+ try{renderLaunchDiff(j);}catch(_){}out.textContent=warn+cwdNotice+(j.commands||[]).map(function(c){return c.to+' ['+(c.harness||'')+']: '+c.command;}).join('\\n')+coordNotice+'\\n(dry-run — booted nothing)';
1492
1516
  }else{
1493
1517
  try{showUndoToast((j.workers||[]).map(function(w){return w.name;}));}catch(_){}
1494
1518
  var cwdNotice=j.cwd?(' [cwd: '+j.cwd+']'):'';
1495
- out.textContent='launched: '+(j.workers||[]).map(function(w){return w.name+' ['+(w.driver||'')+']'+(w.pid?(' (pid '+w.pid+')'):'')+(w.error?(' ERROR '+w.error):'');}).join(', ')+cwdNotice;
1519
+ var coordNotice='';
1520
+ if(j.coordinator){
1521
+ if(j.coordinator.action==='minted'&&j.coordinator.pid){
1522
+ coordNotice=' · coordinator: lead (pid '+j.coordinator.pid+')';
1523
+ }else if(j.coordinator.action==='reused'){
1524
+ coordNotice=' · coordinator: lead (reused'+(j.coordinator.pid?' pid '+j.coordinator.pid:'')+')';
1525
+ }else if(j.coordinator.reason){
1526
+ coordNotice=' · coordinator: skipped ('+j.coordinator.reason+')';
1527
+ }
1528
+ }
1529
+ out.textContent='launched: '+(j.workers||[]).map(function(w){return w.name+' ['+(w.driver||'')+']'+(w.pid?(' (pid '+w.pid+')'):'')+(w.error?(' ERROR '+w.error):'');}).join(', ')+coordNotice+cwdNotice;
1496
1530
  }
1497
1531
  }catch(e){out.textContent='launch failed: '+e.message;}
1498
1532
  refresh();
@@ -2220,23 +2254,25 @@ export async function handleWebDashboardRoute(req, res, url, d) {
2220
2254
  res.end(JSON.stringify({ error: "loopback only" }));
2221
2255
  return true;
2222
2256
  }
2223
- const tokenFile = path.join(d.root, "logs", "lead.token");
2224
- let tok = "";
2225
- try { tok = fs.readFileSync(tokenFile, "utf8").trim(); } catch {}
2226
- // Verify the file token still matches the agent record: a stale file
2227
- // (revoked/rotated lead) must not prefill a dead token.
2228
- if (tok) {
2229
- try {
2230
- const rec = readAgent(d, "lead");
2231
- if (rec && agentTokenMatches(rec, tok)) {
2232
- res.writeHead(200, { "content-type": "application/json; charset=utf-8", "cache-control": "no-store" });
2233
- res.end(JSON.stringify({ ok: true, from: "lead", token: tok }));
2234
- return true;
2235
- }
2236
- } catch {}
2257
+ for (const name of ["boss", "lead"]) {
2258
+ const tokenFile = path.join(d.root, "logs", `${name}.token`);
2259
+ let tok = "";
2260
+ try { tok = fs.readFileSync(tokenFile, "utf8").trim(); } catch {}
2261
+ // Verify the file token still matches the agent record: a stale file
2262
+ // (revoked/rotated operator) must not prefill a dead token.
2263
+ if (tok) {
2264
+ try {
2265
+ const rec = readAgent(d, name);
2266
+ if (rec && agentTokenMatches(rec, tok)) {
2267
+ res.writeHead(200, { "content-type": "application/json; charset=utf-8", "cache-control": "no-store" });
2268
+ res.end(JSON.stringify({ ok: true, from: name, token: tok }));
2269
+ return true;
2270
+ }
2271
+ } catch {}
2272
+ }
2237
2273
  }
2238
2274
  res.writeHead(404, { "content-type": "application/json; charset=utf-8", "cache-control": "no-store" });
2239
- res.end(JSON.stringify({ ok: false, error: "no lead.token found" }));
2275
+ res.end(JSON.stringify({ ok: false, error: "no operator token found" }));
2240
2276
  return true;
2241
2277
  }
2242
2278
  // Workspace memory (server-side, survives the random-port origin wipe):
@@ -2525,6 +2561,15 @@ export async function handleApiLaunch(d, body) {
2525
2561
  if (!v.ok) return { status: 400, payload: { error: "launch plan invalid", errors: v.errors, warnings: v.warnings } };
2526
2562
  const p = v.plan;
2527
2563
  const harnesses = Array.isArray(p.harnesses) && p.harnesses.length > 0 ? p.harnesses : [p.harness || "opencode"];
2564
+ const hasCodingHarness = harnesses.some((h) => h !== "generic");
2565
+ let leadMode = p.lead || "auto";
2566
+ if (leadMode === "auto") {
2567
+ leadMode = hasCodingHarness ? "board" : "none";
2568
+ }
2569
+ if (leadMode === "board" && !hasCodingHarness) {
2570
+ return { status: 400, payload: { error: 'lead "board" needs a coding harness for the auto-coordinator (add a harness like opencode, or use lead "none")' } };
2571
+ }
2572
+ const leadTarget = leadMode === "board" ? LEAD_NAME : from;
2528
2573
  const names = p.to ? String(p.to).split(",").map((s) => cleanWebName(s)).filter(Boolean)
2529
2574
  : Array.from({ length: p.count }, (_, i) => `${cleanWebName(p.prefix || "w") || "w"}-${i + 1}`);
2530
2575
  if (names.length === 0) return { status: 400, payload: { error: "no worker names (pass to or count)" } };
@@ -2537,9 +2582,10 @@ export async function handleApiLaunch(d, body) {
2537
2582
  return { status: 400, payload: { error: 'generic harness needs cmd "..."' } };
2538
2583
  }
2539
2584
  if (v.plan.dryRun) {
2585
+ const previewBatch = names.length > 1 ? newId("batch") : undefined;
2540
2586
  const rows = names.map((to, i) => {
2541
2587
  const driver = harnesses[i % harnesses.length];
2542
- const previewPrompt = buildSpawnPrompt({ name: to, from, subject: p.subject, body: p.body.trim(), replyId: "msg-<id>", rev, cwd, root: d.root });
2588
+ const previewPrompt = buildSpawnPrompt({ name: to, from, lead: leadTarget, subject: p.subject, body: p.body.trim(), replyId: previewBatch ? `${previewBatch}-${i + 1}` : "msg-<id>", rev, cwd, root: d.root });
2543
2589
  const t = buildSpawnTarget({
2544
2590
  harness: driver, cmd: body && body.cmd ? String(body.cmd) : undefined, model: p.model,
2545
2591
  auto: p.permission === "auto" || p.permission === "full" ? true : undefined,
@@ -2548,7 +2594,28 @@ export async function handleApiLaunch(d, body) {
2548
2594
  });
2549
2595
  return { to, harness: driver, command: formatSpawnCmd(t) };
2550
2596
  });
2551
- return { status: 200, payload: { ok: true, dryRun: true, plan: p, cwd, warnings: v.warnings, commands: rows } };
2597
+ let coordinator = undefined;
2598
+ if (leadMode === "board") {
2599
+ const existingLead = readAgent(d, LEAD_NAME);
2600
+ const standing = existingLead && typeof existingLead.spawnedPid === "number" && pidAlive(existingLead.spawnedPid);
2601
+ if (standing) {
2602
+ coordinator = { name: LEAD_NAME, action: "reused", pid: existingLead.spawnedPid, note: "live coordinator already running" };
2603
+ } else if (existingLead && existingLead.offboarded) {
2604
+ coordinator = { name: LEAD_NAME, action: "skipped", reason: "lane is offboarded" };
2605
+ } else {
2606
+ const coordDriver = harnesses.find((h) => h !== "generic") || "opencode";
2607
+ const previewCoordPrompt = buildCoordinatorPrompt({
2608
+ name: LEAD_NAME, from, subject: p.subject, body: p.body.trim(), batch: previewBatch,
2609
+ workers: names.map((name, i) => ({ name, id: previewBatch ? `${previewBatch}-${i + 1}` : "msg-<id>", harness: harnesses[i % harnesses.length] })),
2610
+ cwd, root: d.root, auto: p.permission === "auto" || p.permission === "full",
2611
+ });
2612
+ coordinator = {
2613
+ name: LEAD_NAME, action: "minted", harness: coordDriver, wouldProvision: true,
2614
+ promptPreview: previewCoordPrompt.slice(0, 400),
2615
+ };
2616
+ }
2617
+ }
2618
+ return { status: 200, payload: { ok: true, dryRun: true, plan: p, cwd, warnings: v.warnings, commands: rows, coordinator } };
2552
2619
  }
2553
2620
  const auto = p.permission === "auto" || p.permission === "full" ? true : undefined;
2554
2621
  const at = new Date().toISOString();
@@ -2567,11 +2634,26 @@ export async function handleApiLaunch(d, body) {
2567
2634
  const driver = harnesses[i % harnesses.length];
2568
2635
  const spawnOpts = { harness: driver, cmd: body && body.cmd ? String(body.cmd) : undefined, model: p.model, auto, maxTurns: p.maxTurns, allowTools: p.allowTools, cwd, root: d.root, prompt: null };
2569
2636
  try {
2570
- const r = bootWorker(d, spawnOpts, { to, id, from, subject: p.subject, body: p.body.trim(), rev, logDir });
2637
+ const r = bootWorker(d, spawnOpts, { to, id, from, lead: leadTarget, subject: p.subject, body: p.body.trim(), rev, logDir });
2571
2638
  workers.push({ name: to, pid: r.pid, replyId: id, log: r.logPath, driver });
2572
2639
  } catch (e) {
2573
2640
  workers.push({ name: to, replyId: id, driver, error: String((e && e.message) || e) });
2574
2641
  }
2575
2642
  }
2576
- return { status: 200, payload: { ok: true, cwd, workers, warnings: v.warnings } };
2643
+ let coordinator = undefined;
2644
+ if (leadMode === "board") {
2645
+ const coordHarness = harnesses.find((h) => h !== "generic") || "opencode";
2646
+ const coordSpawnOpts = { harness: coordHarness, cmd: body && body.cmd ? String(body.cmd) : undefined, model: p.model, auto, cwd, root: d.root, prompt: null };
2647
+ try {
2648
+ coordinator = provisionAutoLead(d, {
2649
+ spawnOpts: coordSpawnOpts,
2650
+ from, subject: p.subject, body: p.body.trim(), rev, logDir,
2651
+ workers: res.items.map((it, i) => ({ name: it.to, id: it.id, harness: harnesses[i % harnesses.length] })),
2652
+ batch: res.batch, auto,
2653
+ });
2654
+ } catch (e) {
2655
+ coordinator = { action: "skipped", reason: `coordinator boot error: ${String((e && e.message) || e)}`, leadName: LEAD_NAME };
2656
+ }
2657
+ }
2658
+ return { status: 200, payload: { ok: true, cwd, workers, coordinator, warnings: v.warnings } };
2577
2659
  }
@@ -0,0 +1,188 @@
1
+ // bin/lib/workbench.js — Artifacts + lessons: the compound-memory layer.
2
+ // Purpose: make worker output load-bearing for teammates instead of
3
+ // DM-status-only (feed-forward, the Minecraft-paper lever set):
4
+ // artifacts = board objects with depends-on edges; adoption + verification
5
+ // are recorded ON the object, so a later crew can see what was reused and
6
+ // what held up (artifact publish|list|show|adopt|verify).
7
+ // lessons = scoped one-liners auto-injected into later spawn briefs
8
+ // (lesson post|list), so a lesson learned once is paid forward without
9
+ // anyone re-deriving it.
10
+ // Zero-dependency Node 18+ ESM. Side-effect-free at top level. Storage:
11
+ // artifacts/<id>.json {id, from, at, path, summary?, deps[], adoptedBy[], verifiedBy[]}
12
+ // lessons/<id>.json {id, from, scope, text, at}
13
+ import fs from "node:fs";
14
+ import path from "node:path";
15
+ import { fail, readJson, writeJson, listJson, newId } from "./store.js";
16
+
17
+ // Artifact ids minted via newId("art"): art-<YYMMDD>-<HHMMSS>-<8hex>.
18
+ export const ARTIFACT_ID_RE = /\bart-[0-9]{6}-[0-9]{6}-[0-9a-f]{8}\b/g;
19
+
20
+ export function artifactPath(d, id) {
21
+ return path.join(d.artifacts, `${id}.json`);
22
+ }
23
+
24
+ export function readArtifactById(d, id) {
25
+ if (!id) return null;
26
+ try {
27
+ const a = readJson(artifactPath(d, id));
28
+ return a && a.id === id ? a : null;
29
+ } catch {
30
+ return null;
31
+ }
32
+ }
33
+
34
+ // Newest first (board reads answer "what exists lately", not history).
35
+ export function listArtifacts(d, { limit } = {}) {
36
+ const items = listJson(d.artifacts)
37
+ .map((e) => e && e.data)
38
+ .filter((a) => a && a.id && a.path);
39
+ items.sort((a, b) => String(b.at).localeCompare(String(a.at)) || String(b.id).localeCompare(String(a.id)));
40
+ return typeof limit === "number" ? items.slice(0, Math.max(0, limit)) : items;
41
+ }
42
+
43
+ export function cleanArtifactId(raw, flag) {
44
+ const f = flag || "--id";
45
+ if (raw === undefined || raw === null || String(raw).trim() === "") fail(`missing ${f} <artifact-id>`);
46
+ const c = String(raw).trim().toLowerCase();
47
+ if (!/^art-[0-9]{6}-[0-9]{6}-[0-9a-f]{8}$/.test(c)) fail(`invalid ${f} "${raw}" (want an art-<id> from artifact list)`);
48
+ return c;
49
+ }
50
+
51
+ // Lesson scopes: freeform short names like groups; missing reads as
52
+ // "global" so one-off lessons still reach every later brief.
53
+ export function cleanLessonScope(raw, flag) {
54
+ const f = flag || "--scope";
55
+ if (raw === undefined || raw === null || String(raw).trim() === "") return "global";
56
+ const c = String(raw).trim().toLowerCase().replace(/[^a-z0-9_.-]/g, "-").slice(0, 60);
57
+ if (!c) fail(`invalid ${f} "${raw}"`);
58
+ return c;
59
+ }
60
+
61
+ export function parseArtifactDeps(raw) {
62
+ if (raw === undefined || raw === null || String(raw).trim() === "") return [];
63
+ return String(raw).split(",").map((s) => cleanArtifactId(s, "--deps")).filter((x, i, a) => a.indexOf(x) === i);
64
+ }
65
+
66
+ export function publishArtifact(d, { from, ref, summary, deps, at }) {
67
+ const cleanRef = String(ref === undefined || ref === null ? "" : ref).trim();
68
+ if (!cleanRef) fail('missing --path <path-or-url> (where the artifact lives; reference, never inline secrets)');
69
+ const doc = {
70
+ id: newId("art"),
71
+ from,
72
+ at,
73
+ path: cleanRef.slice(0, 500),
74
+ summary: String(summary || "").trim().slice(0, 500) || undefined,
75
+ deps: Array.isArray(deps) ? deps : [],
76
+ adoptedBy: [],
77
+ verifiedBy: [],
78
+ };
79
+ for (const dep of doc.deps) {
80
+ if (!readArtifactById(d, dep)) fail(`dep ${dep} not found on this board (publish the dependency first, or drop it)`);
81
+ }
82
+ writeJson(artifactPath(d, doc.id), doc);
83
+ return doc;
84
+ }
85
+
86
+ export function adoptArtifact(d, { id, from, note, at }) {
87
+ const a = readArtifactById(d, id);
88
+ if (!a) fail(`unknown artifact ${id} (artifact list to see what's on the board)`);
89
+ if (!a.adoptedBy.some((e) => e.by === from)) {
90
+ a.adoptedBy.push({ by: from, at, note: String(note || "").trim().slice(0, 200) || undefined });
91
+ writeJson(artifactPath(d, id), a);
92
+ }
93
+ return a;
94
+ }
95
+
96
+ export function verifyArtifact(d, { id, from, note, at }) {
97
+ const a = readArtifactById(d, id);
98
+ if (!a) fail(`unknown artifact ${id} (artifact list to see what's on the board)`);
99
+ if (!a.verifiedBy.some((e) => e.by === from)) {
100
+ a.verifiedBy.push({ by: from, at, note: String(note || "").trim().slice(0, 200) || undefined });
101
+ writeJson(artifactPath(d, id), a);
102
+ }
103
+ return a;
104
+ }
105
+
106
+ export function postLesson(d, { from, scope, text, at }) {
107
+ const t = String(text === undefined || text === null ? "" : text).trim();
108
+ if (!t) fail('missing --text "..." (one or two sentences: what happened + what to do differently)');
109
+ const doc = { id: newId("les"), from, scope: cleanLessonScope(scope), text: t.slice(0, 800), at };
110
+ writeJson(path.join(d.lessons, `${doc.id}.json`), doc);
111
+ return doc;
112
+ }
113
+
114
+ // Oldest first (a lesson list is a history read, unlike artifacts).
115
+ export function listLessons(d, { scope } = {}) {
116
+ let items = listJson(d.lessons)
117
+ .map((e) => e && e.data)
118
+ .filter((l) => l && l.id && l.text);
119
+ items.sort((a, b) => String(a.at).localeCompare(String(b.at)) || String(a.id).localeCompare(String(b.id)));
120
+ if (scope !== undefined) items = items.filter((l) => l.scope === cleanLessonScope(scope));
121
+ return items;
122
+ }
123
+
124
+ // Brief injection selection: lessons whose scope matches one of this
125
+ // spawn's groups (or global), newest last-N reversed so the freshest
126
+ // lessons land first in the brief. Cap keeps prompts bounded.
127
+ export function selectLessons(d, { scopes = [], limit = 8 } = {}) {
128
+ const scopeSet = new Set(["global", ...(scopes || []).map(String)]);
129
+ const picked = listLessons(d).filter((l) => scopeSet.has(l.scope));
130
+ return picked.slice(-Math.max(0, limit)).reverse();
131
+ }
132
+
133
+ // Pure renderer: the Workbench: block for spawn briefs. Returns bullet
134
+ // lines (no header); caller joins under a header. Empty board -> [].
135
+ export function renderWorkbenchBlock({ lessons, artifacts, me }) {
136
+ const les = (lessons || []).slice(0, 8);
137
+ const arts = (artifacts || []).slice(0, 6);
138
+ const out = [];
139
+ for (const l of les) out.push(`- lesson [${l.scope}] ${l.text} (from ${l.from})`);
140
+ for (const a of arts) {
141
+ const state = [];
142
+ if (a.adoptedBy && a.adoptedBy.length) state.push(`adopted x${a.adoptedBy.length}`);
143
+ if (a.verifiedBy && a.verifiedBy.length) state.push("verified");
144
+ out.push(`- artifact ${a.id} ${a.path}${a.summary ? ` — ${a.summary}` : ""}${state.length ? ` (${state.join(", ")})` : ""}`);
145
+ }
146
+ if (out.length === 0) return out;
147
+ out.push(`(verify before reuse; publish yours: artifact publish --path <p> [--deps <id,id>] / lesson post --scope <s> --text "..."; reuse a piece: artifact adopt|verify --id <id> --from ${String(me || "you")})`);
148
+ return out;
149
+ }
150
+
151
+ // Gather metrics (pure given a lookup fn): scan transcript messages for
152
+ // artifact references (the m.artifact field + art- ids in bodies), then
153
+ // score feed-forward: an edge is a message whose sender is NOT the
154
+ // artifact's producer — someone downstream actually consumed upstream
155
+ // output on the board itself.
156
+ export function workbenchMetrics(items, lookup) {
157
+ const seenEdges = new Set();
158
+ const refs = new Map();
159
+ const producers = new Set();
160
+ let feedForward = 0;
161
+ let adopted = 0;
162
+ let verified = 0;
163
+ for (const m of items || []) {
164
+ if (!m) continue;
165
+ const text = `${m.artifact || ""} ${m.body || ""}`;
166
+ const ids = new Set(String(text).match(ARTIFACT_ID_RE) || []);
167
+ for (const id of ids) {
168
+ const a = lookup(id);
169
+ if (!a) continue;
170
+ // Adoption/verification are per-artifact facts; count each artifact
171
+ // once no matter how many transcript messages cite it.
172
+ if (!refs.has(id)) {
173
+ refs.set(id, a);
174
+ producers.add(a.from);
175
+ if (a.adoptedBy && a.adoptedBy.length) adopted++;
176
+ if (a.verifiedBy && a.verifiedBy.length) verified++;
177
+ }
178
+ if (m.from && m.from !== a.from) {
179
+ const key = `${m.id}|${id}`;
180
+ if (!seenEdges.has(key)) {
181
+ seenEdges.add(key);
182
+ feedForward++;
183
+ }
184
+ }
185
+ }
186
+ }
187
+ return { artifactRefs: refs.size, producers: producers.size, feedForward, adopted, verified };
188
+ }
@@ -0,0 +1,75 @@
1
+ # Auto-assigned Lead & Observer Lane Model
2
+
3
+ Crewbus separates **spawning** from **leading**. Instead of the interactive spawner remaining the central router for all worker communications (sub-agent hub pattern), Crewbus defaults to an autonomous **lead** lane provisioned on the board, while workers collaborate peer-to-peer.
4
+
5
+ This architecture is modeled after decentralized multi-agent coordination patterns (e.g. Minecraft multi-agent peer collaboration):
6
+ - **Peers** communicate directly, self-organize strategy, and write findings to shared ambient memory (the board / Workbench).
7
+ - An **Observer** (`lead`) monitors health, chases failures/timeouts, fail-closes approvals, and verifies claims against actual artifacts.
8
+ - The **Boss** sets the objective and monitors milestones asynchronously without micromanaging execution.
9
+
10
+ ---
11
+
12
+ ## 1. The Lane Model
13
+
14
+ | Lane | Actor | Authority & Responsibilities |
15
+ |---|---|---|
16
+ | **Boss** | User / Calling Session | • Sets the objective and mission parameters.<br>• Reads progress via dashboard, status commands, or milestone DMs.<br>• Resolves out-of-scope mission changes or irreconcilable disputes.<br>• Never routes routine peer messages. |
17
+ | **Lead** | Autonomous Board Coordinator (`lead`) | • **Observer Role**: Environment repairs, TTL chasing, respawn health (`spawn-status --all`, `respawn`).<br>• **Approvals**: Decides on irreversible/high-risk actions (`fail-closed`).<br>• **Verification**: Audits worker claims against actual files/logs before aggregating.<br>• **Milestones**: Relays aggregate outcomes back to the boss.<br>• *Never* settles peer strategy disputes or does workers' implementation tasks. |
18
+ | **Peers** | Worker Agents (`sec-1`, `sec-2`, ...) | • Own task strategy, execution, and local decisions.<br>• Direct peer-to-peer DMs for targeted cooperation and rescue calls.<br>• Post lessons and artifacts directly to the board Workbench.<br>• Checkpoint progress along threads.<br>• Consult groups first; escalate to lead only for scope/approvals. |
19
+
20
+ ---
21
+
22
+ ## 2. CLI Control: `--lead auto|board|none`
23
+
24
+ When spawning workers with `crewbus spawn`:
25
+
26
+ ```sh
27
+ # Default: auto-assigns lead if a coding harness is present
28
+ crewbus spawn --from boss --count 3 --harness opencode --body "Implement feature X"
29
+
30
+ # Explicit board coordinator
31
+ crewbus spawn --from boss --count 3 --harness opencode --lead board --body "Implement feature X"
32
+
33
+ # Legacy star topology (replies pin directly to boss)
34
+ crewbus spawn --from boss --count 3 --harness generic --lead none --body "Run test suite"
35
+ ```
36
+
37
+ ### Modes:
38
+ - **`auto` (default)**:
39
+ - If any coding harness (`opencode`, `claude`, `codex`, `grok`, `antigravity`, `cursor`) is used: selects `board` mode.
40
+ - If all workers use `generic` harnesses (shell commands without an LLM harness): falls back to `none` mode.
41
+ - **`board`**:
42
+ - Auto-provisions or reuses the board's standing `lead` lane.
43
+ - Workers' brief prompts pin reply targets and approval requests to `--to lead`.
44
+ - Requires at least one coding harness to run the coordinator prompt; fails loudly if attempted with pure `generic` harnesses.
45
+ - **`none`**:
46
+ - Legacy star topology: replies and approvals pin directly to the spawner (`--to <from>`).
47
+ - No autonomous coordinator is provisioned.
48
+
49
+ ---
50
+
51
+ ## 3. Provisioning & Collision Rules
52
+
53
+ 1. **First-Claim-Wins**:
54
+ - On a fresh board without a `lead` agent, `provisionAutoLead` mints the `lead` identity with `role: "lead"`.
55
+ - The token is saved to `logs/lead.token` according to the token-file convention.
56
+ 2. **Reuse Standing Coordinators**:
57
+ - If an active coordinator is already running (live PID on the `lead` lane), subsequent `spawn` bursts reuse the standing coordinator rather than duplicating it.
58
+ - Workers from new bursts are transferred to the lead lane (`spawnedBy: "lead"`), preserving own-crew RBAC permissions for health tracking and `spawn-kill`.
59
+ 3. **Offboarded Safety**:
60
+ - If the `lead` lane has been marked `offboarded: true` (deliberate teardown), auto-provisioning is skipped with a loud warning.
61
+ 4. **Boss Detachment**:
62
+ - If `from === "lead"`, auto-provisioning skips into self-bootstrap mode to prevent recursion.
63
+
64
+ ---
65
+
66
+ ## 4. Prompt Engineering
67
+
68
+ - **Workers (`buildSpawnPrompt`)**:
69
+ - Directs completion summaries, checkpoints, and approval requests to `--to lead`.
70
+ - Informs the worker that the spawner stepped back to boss and reads progress via the lead.
71
+ - Reinforces peer-first habits: consult groups before escalating to the lead.
72
+ - **Coordinator (`buildCoordinatorPrompt`)**:
73
+ - Injected with the verbatim mission text from the boss.
74
+ - Lists worker brief IDs for easy threading and reply tracking.
75
+ - Enforces polling discipline (`listen` / `inbox --digest`), fail-closed approval timeouts, and maximum 2 respawn attempts per lane before escalating stalls to the boss.
package/docs/SPLIT.md CHANGED
@@ -2,10 +2,10 @@
2
2
 
3
3
  Status: landed. `bin/crewbus.js` is a dispatcher that intentionally
4
4
  retains the `cmd*` wrappers, `USAGE`, `main()`, and the opencode
5
- tool/plugin embeds; domain logic lives in the 12 lib modules
5
+ tool/plugin embeds; domain logic lives in the 13 lib modules
6
6
  (`bin/lib/` holds channels, export, groups, identity, launch, mail,
7
- relay, spawn, store, sync, tokenfile, web; import block,
8
- `bin/crewbus.js` lines 44–55). No behavior change is intended, with one deliberate exception recorded in CHANGELOG (parseRecipients full-version canonicalization).
7
+ relay, spawn, store, sync, tokenfile, web, workbench; import block,
8
+ `bin/crewbus.js` lines 44–56). No behavior change is intended, with one deliberate exception recorded in CHANGELOG (parseRecipients full-version canonicalization).
9
9
 
10
10
  ## Module map
11
11
 
@@ -23,9 +23,10 @@ relay, spawn, store, sync, tokenfile, web; import block,
23
23
  | `bin/lib/channels.js` | Shared channels + locks: log IO, per-reader cursors, digest/summarize, group mirrors, advisory locks, channel text-merge | `appendChannelPost`, `readChannelPosts`, `tailChannelPosts`, `parseChannelText`, `mergeChannelText`, `mirrorToGroupChannels`, `acquireLockDoc`, `releaseLockDoc` |
24
24
  | `bin/lib/web.js` | Dashboard + API: snapshots, HTML render, ack/kill/launch handlers, `cmdWeb`; control-plane additions: `handleApiLaunch`, `POST /api/launch`, `GET /api/harnesses\|routes\|holds\|quotas`, Holds/Quotas/AppRoot sections | `boardSnapshot`, `fleetSnapshot`, `channelsSnapshot`, `resultsSnapshot`, `auditSnapshot`, `renderBoardHtml`, `handleApiAck`, `handleApiKill`, `handleApiLaunch`, `cmdWeb` |
25
25
  | `bin/lib/tokenfile.js` | Token-file convention for long runs: save/re-read agent tokens across compaction/restart amnesia (0600 best-effort); standalone, no local imports (cycle-free) | `tokenFilePath`, `saveTokenFile`, `loadTokenFile` |
26
+ | `bin/lib/workbench.js` | Artifacts + lessons (compound-memory layer): artifact objects with deps/adoption/verification edges, scoped lessons, spawn-brief Workbench block, gather metrics; pure helpers, monolith keeps `cmdArtifact`/`cmdLesson` | `publishArtifact`, `adoptArtifact`, `verifyArtifact`, `readArtifactById`, `listArtifacts`, `listLessons`, `postLesson`, `selectLessons`, `renderWorkbenchBlock`, `workbenchMetrics`, `cleanArtifactId`, `cleanLessonScope`, `parseArtifactDeps`, `ARTIFACT_ID_RE` |
26
27
 
27
28
  Full export lists: see the import block at the top of `bin/crewbus.js`
28
- (lines 44–55). Entry-point `cmd*` functions (e.g. `cmdSend`, `cmdSync`,
29
+ (lines 44–56). Entry-point `cmd*` functions (e.g. `cmdSend`, `cmdSync`,
29
30
  `cmdServe`) stay in the monolith; lib modules carry the computation helpers
30
31
  those verbs call.
31
32
 
@@ -1,6 +1,6 @@
1
1
  # Version skew (mixed-relay operation)
2
2
 
3
- How crewbus versions interoperate. Evidence: `package.json:2` (`10.0.8`),
3
+ How crewbus versions interoperate. Evidence: `package.json:2` (`10.0.10`),
4
4
  `CHANGELOG.md` (10.0.0 2026-10-08; 7.0.0 2026-10-05; 6.4.0–6.0.0; 4.1.0/4.0.0 baseline).
5
5
  Capabilities: `RELAY_CAPS` + `launch` per `packages/contracts/board-caps.json:6-8`.
6
6
  Cell behavior is the NEW side's verified behavior (`bin/lib/sync.js` refs);