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/launch.js CHANGED
@@ -276,6 +276,9 @@ export function validateLaunchPlan(raw) {
276
276
  const model = r.model === undefined || r.model === null || String(r.model).trim() === "" ? undefined : String(r.model).trim();
277
277
  if (model && model.length > 100) errors.push("model name too long (max 100 chars)");
278
278
 
279
+ const lead = r.lead === undefined || r.lead === null || String(r.lead).trim() === "" ? "auto" : String(r.lead).trim().toLowerCase();
280
+ if (!["auto", "board", "none"].includes(lead)) errors.push(`bad lead mode "${r.lead}" (want auto|board|none)`);
281
+
279
282
  const plan = {
280
283
  harness, harnesses: harnesses.length > 0 ? harnesses : ["generic"],
281
284
  model, body, count: Number.isInteger(count) && count > 0 ? count : 1,
@@ -285,7 +288,7 @@ export function validateLaunchPlan(raw) {
285
288
  priority: r.priority === undefined ? undefined : String(r.priority),
286
289
  maxTurns: r.maxTurns === undefined ? undefined : String(r.maxTurns),
287
290
  allowTools: r.allowTools === undefined ? undefined : String(r.allowTools),
288
- permission, lifetime,
291
+ permission, lifetime, lead,
289
292
  isolate: !!r.isolate, worktree: r.worktree, branch: r.branch,
290
293
  budgetTokens: r.budgetTokens, budgetMinutes: r.budgetMinutes, timeout: r.timeout,
291
294
  target: r.target === undefined ? "local" : r.target,
package/bin/lib/mail.js CHANGED
@@ -69,7 +69,7 @@ export function findMessageById(d, id) {
69
69
  return null;
70
70
  }
71
71
 
72
- export function deliverDMs(d, { from, recipients, body, subject, replyTo, artifact, priority, checkpoint, senderType, fwd, rev, at, forceBroadcast, forceDirect }) {
72
+ export function deliverDMs(d, { from, recipients, body, subject, replyTo, artifact, priority, checkpoint, senderType, fwd, rev, at, forceBroadcast, forceDirect, needsBy }) {
73
73
  const isAll = recipients.length === 1 && recipients[0] === "@all";
74
74
  // Large fan-outs (> BROADCAST_AFTER) and @all go to ONE broadcast file;
75
75
  // small fan-outs keep one copy per recipient (unique id each, shared batch).
@@ -84,6 +84,7 @@ export function deliverDMs(d, { from, recipients, body, subject, replyTo, artifa
84
84
  if (checkpoint === true) msg.checkpoint = true;
85
85
  if (senderType) msg.senderType = senderType;
86
86
  if (typeof fwd === "number") msg.fwd = fwd;
87
+ if (needsBy) msg.needsBy = needsBy;
87
88
  if (rev) msg.rev = rev;
88
89
  const sig = signMessage({ ...msg, to: msg.to });
89
90
  if (sig) msg.sig = sig;
@@ -106,6 +107,7 @@ export function deliverDMs(d, { from, recipients, body, subject, replyTo, artifa
106
107
  if (checkpoint === true) msg.checkpoint = true;
107
108
  if (senderType) msg.senderType = senderType;
108
109
  if (typeof fwd === "number") msg.fwd = fwd;
110
+ if (needsBy) msg.needsBy = needsBy;
109
111
  if (batch) msg.batch = batch;
110
112
  if (rev) msg.rev = rev;
111
113
  const sig = signMessage(msg);
@@ -293,6 +295,7 @@ export function printMsg(m, showTo, json) {
293
295
  console.log(` ${untrustedEnvelope(m.from, m.senderType || "peer")}`);
294
296
  if (m.subject) console.log(` subj: ${m.subject}`);
295
297
  if (m.artifact) console.log(` artifact: ${m.artifact}`);
298
+ if (m.needsBy) console.log(` needs-by: ${m.needsBy}${isOverdue(m) ? " (OVERDUE)" : ""}`);
296
299
  if (isHigh(m)) console.log(` priority: high`);
297
300
  if (isCheckpoint(m)) console.log(` checkpoint: progress (not a final summary)`);
298
301
  if (typeof m.fwd === "number") console.log(` fwd: ${m.fwd}/${MAX_FWD_DEPTH}`);
@@ -330,6 +333,15 @@ export function isHigh(m) {
330
333
  return m && String(m.priority || "").toLowerCase() === "high";
331
334
  }
332
335
 
336
+ // Needs-by deadline (L4 handoff): present on messages the sender stamped
337
+ // with --needs-by; overdue once the timestamp passes. Read-only helpers,
338
+ // shared by printMsg / printDigest / inbox --overdue.
339
+ export function isOverdue(m) {
340
+ if (!m || !m.needsBy) return false;
341
+ const t = Date.parse(m.needsBy);
342
+ return !Number.isNaN(t) && t < Date.now();
343
+ }
344
+
333
345
  // Progress checkpoints ride the DM thread (replyTo the brief) but are not
334
346
  // work awaiting acceptance: triage views (--unacked, ack timeout hints,
335
347
  // spawn-status reply detection) skip them; transcripts show them labeled.
@@ -372,7 +384,7 @@ export function enforceMaxChars(items, maxChars) {
372
384
  export function printDigest(items) {
373
385
  for (const m of items) {
374
386
  const head = String(m.body || "").split("\n")[0].slice(0, 140);
375
- console.log(`${m.id} [peer:${m.from}]${isHigh(m) ? " [!HIGH]" : ""}${isCheckpoint(m) ? " [checkpoint]" : ""}${m.subject ? ` subj:${String(m.subject).slice(0, 80)}` : ""} :: ${head}`);
387
+ console.log(`${m.id} [peer:${m.from}]${isHigh(m) ? " [!HIGH]" : ""}${isCheckpoint(m) ? " [checkpoint]" : ""}${isOverdue(m) ? " [OVERDUE]" : m.needsBy ? " [needs-by]" : ""}${m.subject ? ` subj:${String(m.subject).slice(0, 80)}` : ""}${m.needsBy ? ` by:${String(m.needsBy).slice(0, 16)}` : ""} :: ${head}`);
376
388
  }
377
389
  }
378
390
 
package/bin/lib/spawn.js CHANGED
@@ -22,13 +22,22 @@ import fs from "node:fs";
22
22
  import path from "node:path";
23
23
  import crypto from "node:crypto";
24
24
  import os from "node:os";
25
+ import { createRequire } from "node:module";
25
26
  import { execFileSync, spawn } from "node:child_process";
26
- import { fail, readJson, writeJson, cleanBranchPrefix } from "./store.js";
27
- import { touchAgent } from "./identity.js";
27
+ import { fail, readJson, writeJson, writeExclusiveJson, cleanBranchPrefix } from "./store.js";
28
+ import { touchAgent, readAgent, writeAgentFile, assertMintWon, mintToken, newSalt, hashToken } from "./identity.js";
28
29
  import { readVisible, isCheckpoint } from "./mail.js";
30
+ import { saveTokenFile, loadTokenFile } from "./tokenfile.js";
29
31
 
30
32
  // cleanBranchPrefix lives in store.js (canonical home).
31
33
 
34
+ // Auto-provisioned lead lane: the board's standing coordinator lane name.
35
+ // `spawn --lead board` (the default) mints it once (first claim wins) and
36
+ // boots a detached persistent coordinator that owns the crew for the burst;
37
+ // the interactive spawner steps back to boss (reads the dashboard, gets
38
+ // milestone DMs). All worker prompts pin replies to this name.
39
+ export const LEAD_NAME = "lead";
40
+
32
41
  export function sandboxPresent() { // line 1017
33
42
  return !!(process.env.CONTAINER || process.env.DOCKER || process.env.CI || process.env.CREWBUS_SANDBOX);
34
43
  }
@@ -161,9 +170,14 @@ export function parseAllowEnv(raw) { // line 4078
161
170
  return String(raw).split(",").map((s) => String(s).trim()).filter(Boolean).join(",");
162
171
  }
163
172
 
164
- export function buildSpawnPrompt({ name, from, subject, body, replyId, rev, cwd, root, crew }) { // line 4090
173
+ export function buildSpawnPrompt({ name, from, lead, subject, body, replyId, rev, cwd, root, crew, workbench }) { // line 4090
165
174
  const me = String(name || "you");
175
+ // Reply target: the sender by default (legacy star topology), or the
176
+ // board's lead lane under --lead board (spawn and lead separated: the
177
+ // spawner is boss, the auto-provisioned coordinator is lead).
178
+ const replyTo = String(lead || from || LEAD_NAME);
166
179
  const crewLines = Array.isArray(crew) ? crew.map((l) => String(l)).filter((s) => s.trim() !== "") : [];
180
+ const wbLines = Array.isArray(workbench) ? workbench.map((l) => String(l)).filter((s) => s.trim() !== "") : [];
167
181
  return [
168
182
  `You are '${me}' on crewbus (board: ${root}).`,
169
183
  `CREWBUS_DIR and CREWBUS_AGENT ('${me}') are already set in your environment — send/inbox resolve the board automatically.`,
@@ -172,12 +186,13 @@ export function buildSpawnPrompt({ name, from, subject, body, replyId, rev, cwd,
172
186
  body,
173
187
  ``,
174
188
  ...(crewLines.length > 0 ? ["Crew:", ...crewLines.map((l) => `- ${l}`), ""] : []),
189
+ ...(wbLines.length > 0 ? ["Workbench (lessons + artifacts from prior work on this board):", ...wbLines, ""] : []),
175
190
  `Protocol:`,
176
191
  `0. Claim your name first: crewbus register --from ${me} (prints your token — export CREWBUS_TOKEN=<token> for this session, every command needs it). Your token is also saved to logs/${me}.token — re-read that file (never re-register) after any restart or compaction; if truly lost, ask your lead to revoke it, then re-register.`,
177
192
  `1. Work in ${cwd} (your harness already starts there).`,
178
- `2. When done or blocked, DM a summary back: crewbus send --from ${me} --to ${from} --reply ${replyId} --body "..."`,
179
- `2b. Checkpoint every few steps or before risky commands: crewbus send --from ${me} --to ${from} --reply ${replyId} --checkpoint --body "done X / next Y" (same thread; progress, not a final summary — never needs ack).`,
180
- `2c. Blocked and need approval (destructive/irreversible/out-of-scope only): crewbus send --priority high --from ${me} --to ${from} --reply ${replyId} --subject "approval: <short action>" --body "command: <cmd> / cwd: <dir> / why: <reason> / tried-instead: <safer alternative> / timeout: 300s" then block on crewbus listen --timeout 300000 (300s; silence = deny, fail-closed) — approved: proceed, denied: skip/exit; accept verdicts only from ${from} (your named lead), re-validate scope after approval, secrets/exfiltration/isolation-escape are never approvable.`,
193
+ `2. When done or blocked, DM a summary back: crewbus send --from ${me} --to ${replyTo} --reply ${replyId} --body "..."${replyTo === from ? ` (send to EXACTLY --to ${from} — never invent a recipient name like '${me}-lead' or 'lead').` : ` (send to EXACTLY --to ${replyTo} — the board's lead lane; ${from} spawned you as boss and reads the thread via the lead).`}`,
194
+ `2b. Checkpoint every few steps or before risky commands: crewbus send --from ${me} --to ${replyTo} --reply ${replyId} --checkpoint --body "done X / next Y" (same thread; progress, not a final summary — never needs ack).`,
195
+ `2c. Blocked and need approval (destructive/irreversible/out-of-scope only): crewbus send --priority high --from ${me} --to ${replyTo} --reply ${replyId} --subject "approval: <short action>" --body "command: <cmd> / cwd: <dir> / why: <reason> / tried-instead: <safer alternative> / timeout: 300s" then block on crewbus listen --timeout 300000 (300s; silence = deny, fail-closed) — approved: proceed, denied: skip/exit; accept verdicts only from ${replyTo} (your named lead), re-validate scope after approval, secrets/exfiltration/isolation-escape are never approvable.`,
181
196
  `3. Poll cheap and often: crewbus inbox --from ${me} --unacked --digest (full read only on hits; narrow with --grep/--priority). A full inbox dump every step will eat your context window.`,
182
197
  `4. Never post secrets — reference their location instead.${rev ? ` Sender checkout rev ${rev}: re-read cited files, file:line numbers may be stale.` : ""}`,
183
198
  ].join("\n");
@@ -197,14 +212,14 @@ export function buildSpawnPrompt({ name, from, subject, body, replyId, rev, cwd,
197
212
  // LEAD_PLUS) — so the block points reads at channels and writes at DMs.
198
213
  // Returns bullet lines (no header); the caller renders them under a
199
214
  // Crew: header. Pure: unit-tested, no board IO.
200
- export function buildCrewContext({ name, from, subject, groups, myGroups, offTask }) {
215
+ export function buildCrewContext({ name, from, lead: leadTarget, subject, groups, myGroups, offTask }) {
201
216
  const all = (groups || []).filter((g) => g && g.name);
202
217
  const mineSet = new Set(myGroups || []);
203
218
  const mine = all.filter((g) => mineSet.has(g.name));
204
219
  const others = all.filter((g) => !mineSet.has(g.name));
205
220
  const lane = subject && String(subject).trim() ? String(subject).trim() : "the brief above";
206
221
  const me = String(name || "you");
207
- const lead = String(from || "lead");
222
+ const lead = leadTarget !== undefined ? String(leadTarget) : String(from || LEAD_NAME);
208
223
  const off = Math.max(0, Number(offTask) || 0);
209
224
  const offNoun = (n) => `${n} other group${n === 1 ? "" : "s"}`;
210
225
  const showMembers = (members) => {
@@ -227,10 +242,218 @@ export function buildCrewContext({ name, from, subject, groups, myGroups, offTas
227
242
  }
228
243
  out.push(`ask the group that owns a thing before asking ${lead}: crewbus send --from ${me} --to-group <group> --subject "..." --body "..." (reaches every member; ${lead} is for approvals and scope disputes only).`);
229
244
  out.push(`read context freely: crewbus channel tail grp-<group> (a group's briefs + progress, reads need no token). Posting to channels needs lead rights — write everything via DMs.`);
230
- out.push(`boot habit: crewbus agents + crewbus group list once, so you know who else is here.`);
245
+ out.push(`boot habit: crewbus agents + crewbus group list once, so you know who else is here. Broadcast one hello once you claimed your name (--to-group <group> when you have one, else a DM per sibling) so peers know you exist before they need you.`);
231
246
  return out;
232
247
  }
233
248
 
249
+ // Coordinator prompt for the auto-provisioned lead (--lead board, the spawn
250
+ // default): a detached persistent worker that OWNS the crew for the burst so
251
+ // the interactive spawner can step back to boss. This is infrastructure
252
+ // steering (the one place a brief may orchestrate): the coordinator TRIAGES
253
+ // and KEEPS THE BOARD ALIVE — it never rewrites the mission or does workers'
254
+ // jobs. Split of authority (docs/CREWS.md §auto-lead):
255
+ // peers — all strategy, peer DMs, self-coordination
256
+ // lead — env repair, respawn/health chasing, TTL pacing, verdicts on
257
+ // worker approval asks (fail-closed), verification of claims
258
+ // boss — the spawner identity (may poll at its own cadence): mission
259
+ // changes, scope disputes; receives milestone + final DMs
260
+ // `workers` is [{ name, id, harness }]; `batch` the batch id ("" when solo).
261
+ // Pure: unit-tested, no board IO — the caller boots it via bootWorker
262
+ // (prompt override) and stamps the log file itself.
263
+ export function buildCoordinatorPrompt({ name, from, subject, body, batch, workers, crew, cwd, root, auto, workbench }) {
264
+ const boss = String(from || "boss");
265
+ const me = String(name || LEAD_NAME);
266
+ const ws = Array.isArray(workers) ? workers.filter((w) => w && w.name) : [];
267
+ const crewLines = Array.isArray(crew) ? crew.map((l) => String(l)).filter((s) => s.trim() !== "") : [];
268
+ const wbLines = Array.isArray(workbench) ? workbench.map((l) => String(l)).filter((s) => s.trim() !== "") : [];
269
+ const today = new Date().toISOString().slice(0, 10);
270
+ return [
271
+ `You are '${me}' on crewbus (board: ${root}) — the auto-provisioned lead for this crew${batch ? ` (batch ${batch})` : ""}.`,
272
+ `${boss} spawned the crew as boss and left the acting lead seat to you from here on. This is an infrastructure/coordinator role, not a do-the-task role.`,
273
+ ``,
274
+ `Mission the crew is working (from ${boss}${subject ? ` — ${subject}` : ""}):`,
275
+ String(body || "(original brief; see each worker's prompt file under logs/)"),
276
+ ``,
277
+ ...(crewLines.length > 0 ? ["Crew:", ...crewLines.map((l) => `- ${l}`), ""] : []),
278
+ ...(batch ? ["Worker briefs (thread ids, for --reply):", ...ws.map((w) => `- ${w.name}: brief ${w.id}${w.harness ? ` [${w.harness}]` : ""}`), ""] : []),
279
+ ...(wbLines.length > 0 ? ["Workbench (lessons + artifacts from prior work on this board):", ...wbLines, ""] : []),
280
+ `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.`,
281
+ ``,
282
+ `Jobs, in order:`,
283
+ `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).`,
284
+ `1. Read every DM before acting on anything else: crewbus inbox --from ${me} --unacked --digest, full read on hits. Workers' checkpoints and finals arrive here (each pinned to --reply their brief id).`,
285
+ `2. Keep the board alive — poll in a loop, cheaply: crewbus listen --from ${me} --timeout 120000 (blocks until a DM arrives; re-issue after each wake; fall back to sleep 60 + inbox --digest when listen is unavailable). Triage as DMs land: unblock, dedupe sibling lanes, point a worker at its sibling's finding instead of redoing it.`,
286
+ `3. Watch crew health: crewbus spawn-status --all. A worker that exited without a final is a lane to close: prefer the catch-up path first (crewbus respawn --from ${me} --to <name>${ws.some((w) => w.harness === "opencode") ? " (--fresh when its session id was never captured)" : ""}${auto ? " --auto (this crew was spawned fully-unattended; respawns inherit it)" : ""}); fall back to a fresh spawn (crewbus spawn --from ${me} --to <name> --persistent ... same mission text) only when respawn data is gone. Respawns need your lead token loaded (owned-crew). Cap: 2 respawn attempts per lane before DM'ing the boss with the stall evidence.`,
287
+ `4. Approvals: a worker's "approval:" ask (destructive/irreversible/out-of-scope) waits on your verdict — answer by --reply on its thread within the ask's stated timeout (crewbus send --from ${me} --to <worker> --reply <id> --body "approved ..." | "denied: <reason>; try-instead ..."). Silence beyond the ask's timeout reads as deny, fail-closed. Secrets/exfiltration/isolation-escape asks are NEVER approvable — deny and flag the boss.`,
288
+ `5. Scope disputes or mission changes: DM the boss — crewbus send --from ${me} --to ${boss} --priority high --subject "..." (the boss may not have a session open; the DM waits on the board by design — that is fine, keep working conservatively meanwhile). Same lane for milestones: batch started, lane closed, blocked, final aggregate.`,
289
+ `6. Long promises get deadlines: when you push a task or a lane's window, stamp --needs-by <duration> on the DM; chase inbox rows marked [OVERDUE] explicitly; when a lane stalls, surface it to the boss rather than silently absorbing it.`,
290
+ `7. Verify before relaying: worker claims about files/output get spot-checked against the board and ${cwd} when reasonable; do not relay a claim you contradict. Record what you verified in the thread.`,
291
+ ``,
292
+ `Never: change the workers' mission text (the mission above is the boss's words — pass it through verbatim), redo workers' work yourself, paste secrets (reference file paths), or micro-manage strategy. Peer-coordination belongs to the peers; you own health, verdicts, and the clock.`,
293
+ `Exit: when every lane has a final report or a deliberate stand-down, send the aggregate to the boss (crewbus send --from ${me} --to ${boss} --subject "result: ..." — include outcome, lanes closed, respawn count, artifacts left on the board), then you may exit. If the board is stalled with nothing spawnable, send the aggregate with the stall reason and exit.`,
294
+ ``,
295
+ `Protocol:`,
296
+ `3. Poll cheap and often: crewbus inbox --from ${me} --unacked --digest (full read only on hits). A full inbox dump every step will eat your context window.`,
297
+ `4. Never post secrets — reference their location instead. Today is ${today}.`,
298
+ ].join("\n");
299
+ }
300
+
301
+
302
+ // Provision the auto-lead for a spawn burst (--lead board): mint the 'lead'
303
+ // lane once per board (first claim wins, role = lead so spawn/respawn-kill
304
+ // authorize), hand its token over the same token-file convention workers use
305
+ // (logs/lead.token), boot a detached persistent coordinator with the prompt
306
+ // from buildCoordinatorPrompt, then TRANSFER crew ownership (workers'
307
+ // spawnedBy -> lead) so respawn/spawn-kill own-crew checks pass.
308
+ // opts: { d, spawnOpts, from, subject, body, rev, logDir, workers, batch,
309
+ // crew, workbench, auto }
310
+ // Returns { action: "minted"|"reused"|"skipped", reason?, leadName, pid?,
311
+ // logPath?, promptPath?, token?, transferred? } — token only on
312
+ // mint (printed once, then saved to the token file); a standing live
313
+ // coordinator is reused, never duplicated.
314
+ export function provisionAutoLead(d, opts) {
315
+ const leadName = LEAD_NAME;
316
+ const o = opts || {};
317
+ const boss = String(o.from || "boss");
318
+ if (boss === leadName) {
319
+ return { action: "skipped", reason: "the sender IS the lead lane — it bootstraps its own coordinator", leadName };
320
+ }
321
+ // Offboarded lead lane = deliberate teardown: refuse to skeleton it back
322
+ // up behind the caller's back (re-onboard it or pass --lead none).
323
+ const existing = readAgent(d, leadName);
324
+ if (existing && existing.offboarded) {
325
+ return { action: "skipped", reason: `lane "${leadName}" is offboarded (re-onboard it, or pass --lead none to pin replies to the spawner)`, leadName };
326
+ }
327
+ let token = null;
328
+ let minted = false;
329
+ if (existing && (existing.tokenHash || existing.token)) {
330
+ // Identity reuse — a lead lane already claims this name.
331
+ } else {
332
+ token = mintToken();
333
+ const salt = newSalt();
334
+ const tokenHash = hashToken(token, salt);
335
+ const doc = {
336
+ name: leadName, firstSeen: new Date().toISOString(), lastSeen: new Date().toISOString(),
337
+ lastDir: (o.spawnOpts && o.spawnOpts.cwd) || process.cwd(),
338
+ tokenHash, salt, role: "lead",
339
+ provisionedBy: boss, provisionedAt: new Date().toISOString(),
340
+ };
341
+ const claimed = writeExclusiveJson(path.join(d.agents, `${leadName}.json`), doc);
342
+ if (claimed) {
343
+ minted = true;
344
+ } else {
345
+ // Lost the exclusive create: either a concurrent minter won, or a
346
+ // legacy tokenless record holds the name (mint into it — this is the
347
+ // ensureSender post-revoke path, minus the token authentication step
348
+ // the caller already performed as board admin).
349
+ const again = readAgent(d, leadName);
350
+ if (again && !again.offboarded && !again.tokenHash && !again.token) {
351
+ const salt2 = newSalt();
352
+ again.tokenHash = hashToken(token, salt2);
353
+ again.salt = salt2;
354
+ if (!again.role) again.role = "lead";
355
+ writeAgentFile(d, leadName, again);
356
+ assertMintWon(d, leadName, again.tokenHash);
357
+ minted = true;
358
+ } else if (again && (again.tokenHash || again.token)) {
359
+ token = null; // absorbed by a concurrent minter — reuse its identity
360
+ } else {
361
+ return { action: "skipped", reason: `lane "${leadName}" raced a parallel claim that hid the name — retry or pass --lead none`, leadName };
362
+ }
363
+ }
364
+ }
365
+ // Always (re)record how this coordinator came to be, without clobbering
366
+ // an existing live one.
367
+ const rec = readAgent(d, leadName);
368
+ const standing = rec && typeof rec.spawnedPid === "number" && pidAlive(rec.spawnedPid);
369
+ if (minted) saveTokenFile(d.root, leadName, token);
370
+ let booted = null;
371
+ if (standing) {
372
+ // A live coordinator is already running on the lead lane (previous
373
+ // burst): never duplicate it — DMs land in the same lane by name.
374
+ } else {
375
+ try {
376
+ booted = bootWorker(d, o.spawnOpts, {
377
+ to: leadName, id: o.batch || "coord", from: boss, subject: o.subject,
378
+ body: o.body, rev: o.rev, logDir: o.logDir, crew: o.crew,
379
+ workbench: o.workbench, prompt: buildCoordinatorPrompt({
380
+ name: leadName, from: boss, subject: o.subject, body: o.body,
381
+ batch: o.batch, workers: o.workers, crew: o.crew,
382
+ cwd: (o.spawnOpts && o.spawnOpts.cwd) || process.cwd(),
383
+ root: d.root, auto: !!o.auto, workbench: o.workbench,
384
+ }),
385
+ spawnedLifetime: "persistent",
386
+ budgetTokens: undefined, budgetMinutes: undefined, deadlineAt: undefined,
387
+ });
388
+ } catch (e) {
389
+ const detail = String((e && (e.message || e)) || e).slice(0, 160);
390
+ if (minted) {
391
+ saveTokenFile(d.root, leadName, token);
392
+ try { touchAgent(d, leadName, { lastDir: (o.spawnOpts && o.spawnOpts.cwd) || undefined, provisionedBy: boss }); } catch {}
393
+ return { action: "minted", reason: `coordinator boot failed (${detail}) — lane minted; token at logs/${leadName}.token; boot it by hand (spawn --from ${boss} --to ${leadName} --persistent) or DM workers manually`, leadName, token, boss };
394
+ }
395
+ return { action: "skipped", reason: `coordinator boot failed (${detail}) — workers stay owned by ${boss}`, leadName };
396
+ }
397
+ }
398
+ try { touchAgent(d, leadName, { lastDir: (o.spawnOpts && o.spawnOpts.cwd) || undefined, spawnedBy: boss ?? undefined }); } catch {}
399
+ // Crew ownership transfer: workers now belong to the lead lane, which is
400
+ // what respawn/spawn-kill own-crew checks demand (docs/RBAC.md). The
401
+ // spawner's audit trail (appendChainRecord) preserves the original actor.
402
+ let transferred = 0;
403
+ for (const w of (Array.isArray(o.workers) ? o.workers : [])) {
404
+ if (!w || !w.name) continue;
405
+ try { touchAgent(d, w.name, { spawnedBy: leadName }); transferred += 1; } catch {}
406
+ }
407
+ return {
408
+ action: minted ? "minted" : "reused",
409
+ leadName,
410
+ pid: booted ? booted.pid : (rec && rec.spawnedPid),
411
+ logPath: booted ? booted.logPath : undefined,
412
+ promptPath: booted ? booted.promptPath : undefined,
413
+ token,
414
+ transferred,
415
+ boss,
416
+ stoodDown: !!standing,
417
+ };
418
+ }
419
+
420
+ // Resolve the real opencode binary from PATH, once per process. npm installs
421
+ // expose cmd/bash/ps1 shims that shell out to the real exe; running through
422
+ // them (the old `shell: true` path) drags the Bun binary into cmd.exe, which
423
+ // block-buffers stdout in console-less detached contexts — a hard kill then
424
+ // drops the buffer and the worker log never reveals the session id. Each shim
425
+ // embeds its target path (cmd: "%dp0%\node_modules\...\opencode.exe", shims:
426
+ // "$basedir/node_modules/...\opencode.exe"), so extract it, verify the file
427
+ // exists, and memoize. Returns the exe path, or null when resolution fails —
428
+ // callers fall back to the shim invocation they always used.
429
+ let _opencodeExe = undefined; // undefined = unresolved; string | null after
430
+ export function resolveOpencodeExe() {
431
+ if (_opencodeExe !== undefined) return _opencodeExe;
432
+ _opencodeExe = null;
433
+ try {
434
+ const out = execFileSync(process.platform === "win32" ? "where" : "which", ["opencode"], { encoding: "utf8", shell: true, stdio: ["ignore", "pipe", "ignore"], timeout: 10000 });
435
+ for (const raw of String(out || "").split(/\r?\n/)) {
436
+ const shim = raw.trim();
437
+ if (!shim) continue;
438
+ let target = shim;
439
+ if (process.platform === "win32" && !/\.exe$/i.test(shim)) {
440
+ try {
441
+ const m = /"([^"\n]*?\.exe)"/i.exec(fs.readFileSync(shim, "utf8"));
442
+ if (m) target = m[1].replace(/%~?dp0%/gi, path.dirname(shim)).replace(/\$basedir/gi, path.dirname(shim));
443
+ } catch {}
444
+ }
445
+ target = path.resolve(target);
446
+ try {
447
+ if (fs.existsSync(target) && fs.statSync(target).isFile() && (process.platform === "win32" ? /\.exe$/i.test(target) : true)) {
448
+ _opencodeExe = target;
449
+ break;
450
+ }
451
+ } catch {}
452
+ }
453
+ } catch {}
454
+ return _opencodeExe;
455
+ }
456
+
234
457
  export function buildSpawnTarget({ harness, cmd, model, auto, maxTurns, allowTools, cwd, name, promptPath, prompt, sessionId }) { // line 4114
235
458
  const quoteFree = (s) => String(s).replace(/"/g, "'");
236
459
  if (harness === "opencode") {
@@ -243,6 +466,11 @@ export function buildSpawnTarget({ harness, cmd, model, auto, maxTurns, allowToo
243
466
  // extractHarnessSessionId / syncWorkerSession).
244
467
  oargs.push("--format", "json");
245
468
  oargs.push(shortMsg);
469
+ // The resolved real exe runs with shell:false: output streams to the log
470
+ // from byte one (no cmd.exe buffering) and the spawned pid IS the
471
+ // harness process, so kill semantics stay honest too.
472
+ const exe = resolveOpencodeExe();
473
+ if (exe) return { exe, args: oargs, shell: false };
246
474
  return { exe: "opencode", args: oargs, shell: true };
247
475
  }
248
476
  if (harness === "claude") {
@@ -315,6 +543,12 @@ export function buildSpawnTarget({ harness, cmd, model, auto, maxTurns, allowToo
315
543
  // cursor `--resume`. Generic has no session continuity: plain fresh boot of
316
544
  // the catch-up brief. File/stdin plumbing mirrors buildSpawnTarget.
317
545
  export function buildRespawnTarget({ harness, cmd, model, auto, maxTurns, allowTools, cwd, name, promptPath, prompt, sessionId }) { // respawn slice 2
546
+ // No captured session id (generic, or the respawn --fresh fallback when a
547
+ // harness id could not be captured): plain fresh boot of the catch-up
548
+ // brief — the same command shape as spawn.
549
+ if (!sessionId) {
550
+ return buildSpawnTarget({ harness, cmd, model, auto, maxTurns, allowTools, cwd, name, promptPath, prompt });
551
+ }
318
552
  if (harness === "codex") {
319
553
  const base = buildSpawnTarget({ harness, cmd, model, auto, maxTurns, allowTools, cwd, name, promptPath, prompt });
320
554
  // Point at the catch-up file (which links the original brief), not the
@@ -337,9 +571,13 @@ export function buildRespawnTarget({ harness, cmd, model, auto, maxTurns, allowT
337
571
  }
338
572
 
339
573
  export function buildRespawnBrief({ name, attempt, origPromptPath, briefId, harnessSessionId, harness, lead, extraBody }) { // respawn slice 2
574
+ // --fresh respawn (opencode with no captured id) boots fresh like generic;
575
+ // the wording must not claim "prior context intact" when there is none.
340
576
  const resumed = harness === "generic"
341
577
  ? "fresh session (generic harness has no session continuity — no prior context)"
342
- : `resumed ${harness} session ${harnessSessionId} (prior context intact)`;
578
+ : !harnessSessionId && harness === "opencode"
579
+ ? "fresh session (opencode session id could not be captured; brief + checkpoints are your memory)"
580
+ : `resumed ${harness} session ${harnessSessionId} (prior context intact)`;
343
581
  const lines = [
344
582
  `You are '${name}': this is restart attempt ${attempt} (${resumed}). Your previous process died.`,
345
583
  ``,
@@ -452,6 +690,115 @@ export function extractHarnessSessionId(harness, text) { // respawn slice 1
452
690
  return null;
453
691
  }
454
692
 
693
+ // ----------------------------------------------------------- opencode storage fallback
694
+ // opencode.exe (a Bun binary) buffers ALL stdout when run through the cmd.exe
695
+ // wrapper in a console-less detached context; a hard kill drops the buffer, so
696
+ // the worker log can stay empty and the log-scan capture above never fires.
697
+ // opencode 1.19+ independently persists every session to its SQLite database
698
+ // (WAL) the moment a run starts — on every platform at
699
+ // `%USERPROFILE%\.local\share\opencode\opencode.db` — with the exact spawn
700
+ // marker `--title crewbus:<name>` (buildSpawnTarget) and the worker cwd in
701
+ // `directory` (forward slashes). node:sqlite ships with Node >= 22 (repo floor
702
+ // is 18): every failure below degrades SILENTLY to log-scan-only.
703
+ let _sqliteModule = undefined; // undefined = not probed; object or null after
704
+ function opencodeSqliteModule() {
705
+ if (_sqliteModule !== undefined) return _sqliteModule;
706
+ _sqliteModule = null;
707
+ // Dynamic import caches the module when its promise settles; the sync
708
+ // require keeps the first (possibly pre-microtask) probe working. Missing
709
+ // builtin (Node < 22) rejects — caught, cached as unavailable.
710
+ import("node:sqlite").then(
711
+ (m) => { _sqliteModule = m && typeof m.DatabaseSync === "function" ? m : null; },
712
+ () => {}
713
+ );
714
+ try {
715
+ const req = createRequire(import.meta.url);
716
+ const m = req("node:sqlite");
717
+ if (m && typeof m.DatabaseSync === "function") _sqliteModule = m;
718
+ } catch {}
719
+ return _sqliteModule;
720
+ }
721
+
722
+ // opencode normalizes paths to forward slashes and (verified) stores
723
+ // 'C:/Users/...'; compare lowercased on every platform. Both sides derive
724
+ // from the same spawn cwd wearing two spellings (Windows drive/segment
725
+ // casing disagrees freely; opencode's own normalization may recase
726
+ // elsewhere) — the matcher is about spellings, not filesystem identity.
727
+ function normalizeWorkspaceDir(dir) {
728
+ return String(dir || "").trim().replace(/\\/g, "/").replace(/\/+$/, "").toLowerCase();
729
+ }
730
+
731
+ // Best-effort opencode.db probe: { name, cwd, spawnedAt, dbPath? } -> id|null.
732
+ // Read-only (the WAL DB is opened readOnly whenever the Node build supports
733
+ // it); every failure returns null so the caller just stays log-scan-only. The
734
+ // 5-minute lookback from the spawn time guards title reuse across runs;
735
+ // ordering picks the newest row inside the window.
736
+ export function probeOpencodeStorage({ name, cwd, spawnedAt, dbPath } = {}) {
737
+ try {
738
+ const clean = String(name || "").trim();
739
+ if (!clean || !cwd) return null;
740
+ const birth = spawnedAt ? Date.parse(spawnedAt) : NaN;
741
+ if (Number.isNaN(birth)) return null; // no window -> refuse to guess
742
+ const mod = opencodeSqliteModule();
743
+ if (!mod || typeof mod.DatabaseSync !== "function") return null;
744
+ const dbFile = String(dbPath || process.env.OPENCODE_DB_PATH || path.join(os.homedir(), ".local", "share", "opencode", "opencode.db"));
745
+ if (!fs.existsSync(dbFile)) return null;
746
+ let db = null;
747
+ try {
748
+ db = new mod.DatabaseSync(dbFile, { readOnly: true });
749
+ } catch {
750
+ db = null;
751
+ }
752
+ if (!db) {
753
+ try { db = new mod.DatabaseSync(dbFile); } catch { return null; }
754
+ }
755
+ // Directory is filtered in JS, not SQL: sqlite `=` is case-sensitive and
756
+ // the row spelling may disagree in case with the recorded cwd.
757
+ let rows = [];
758
+ try {
759
+ rows = db.prepare("SELECT id, time_created, directory FROM session WHERE title = ? ORDER BY time_created DESC").all(`crewbus:${clean}`);
760
+ } catch {
761
+ // schema drift (doctor guards this with PRAGMA table_info(session))
762
+ try { db.close(); } catch {}
763
+ return null;
764
+ }
765
+ try { db.close(); } catch {}
766
+ const want = normalizeWorkspaceDir(cwd);
767
+ const floor = birth - 5 * 60 * 1000;
768
+ for (const row of rows) {
769
+ if (!row || typeof row.id !== "string" || !row.id.trim()) continue;
770
+ if (normalizeWorkspaceDir(row.directory) !== want) continue;
771
+ const t = typeof row.time_created === "bigint" ? Number(row.time_created) : row.time_created;
772
+ const at = typeof t === "number" ? (t > 1e12 ? t : t * 1000) : Date.parse(String(t));
773
+ if (Number.isNaN(at) || at < floor) continue;
774
+ return row.id.trim().slice(0, 200);
775
+ }
776
+ } catch {}
777
+ return null;
778
+ }
779
+
780
+ // Doctor support: can the storage fallback read session rows at all?
781
+ // One of "ok" | "drift" | "no-db" | "no-sqlite" | "error". Never throws.
782
+ export function opencodeStorageStatus(dbPath) {
783
+ const p = String(dbPath || process.env.OPENCODE_DB_PATH || path.join(os.homedir(), ".local", "share", "opencode", "opencode.db"));
784
+ const mod = opencodeSqliteModule();
785
+ const sqliteAvailable = !!mod && typeof mod.DatabaseSync === "function";
786
+ if (!fs.existsSync(p)) return { status: "no-db", dbPath: p, sqliteAvailable };
787
+ if (!sqliteAvailable) return { status: "no-sqlite", dbPath: p, sqliteAvailable: false };
788
+ try {
789
+ const db = new mod.DatabaseSync(p, { readOnly: true });
790
+ try {
791
+ const cols = db.prepare("PRAGMA table_info(session)").all().map((c) => c && c.name);
792
+ const ok = cols.includes("id") && cols.includes("title") && cols.includes("directory") && cols.includes("time_created");
793
+ return { status: ok ? "ok" : "drift", dbPath: p, sqliteAvailable: true };
794
+ } finally {
795
+ try { db.close(); } catch {}
796
+ }
797
+ } catch (e) {
798
+ return { status: "error", dbPath: p, sqliteAvailable: true, detail: String((e && (e.message || e)) || "").slice(0, 120) };
799
+ }
800
+ }
801
+
455
802
  // Worker-session binding lives beside the agent doc (not inside it) so
456
803
  // presence heartbeats — which rewrite agent docs field-by-field in five
457
804
  // places — can never wipe a captured id. Machine-local like pids: never
@@ -516,16 +863,27 @@ function readLogTail(p, maxBytes) {
516
863
  // "preassigned-confirmed" (log corroborates the minted id),
517
864
  // "log" (found in the log, nothing minted), "log-override" (log disagrees
518
865
  // with the minted id — trust the log: the installed harness may have
519
- // ignored the pre-assign flag). Workers that never emit an id cost one
520
- // small stat per status call, never a scan. Never throws.
521
- export function syncWorkerSession(d, name, logPath) { // respawn slice 1
866
+ // ignored the pre-assign flag), and "storage" (read from the harness's own
867
+ // on-disk store when the log reveals nothing — see probeOpencodeStorage).
868
+ // Workers that never emit an id cost one small stat per status call, never
869
+ // a scan. Never throws.
870
+ const STORAGE_PROBE_INTERVAL_MS = 30000;
871
+
872
+ export function syncWorkerSession(d, name, logPath, storageProbe) { // respawn slice 1
522
873
  const prev = readWorkerSession(d, name);
523
874
  const lp = logPath || (prev && prev.logPath) || newestWorkerLog(d, name);
524
875
  if (!lp) return prev;
525
876
  const tail = readLogTail(lp, WORKER_SESSION_TAIL_BYTES);
526
877
  if (!tail) return prev;
527
- if (prev && prev.logPath === lp && prev.checkedSize === tail.size && prev.checkedMtime === tail.mtime) return prev;
528
- const now = new Date().toISOString();
878
+ // Storage fallback applies only for opencode with no id captured yet, and
879
+ // at most every ~30s (storageCheckedAt guard). Log-scan keeps precedence:
880
+ // a log-confirmed id always wins over a DB-read one, regardless of order.
881
+ const storagePending = ((prev && prev.harness) === "opencode") && !(prev && prev.harnessSessionId);
882
+ const lastStorageAt = prev && prev.storageCheckedAt ? Date.parse(prev.storageCheckedAt) : NaN;
883
+ const storageDue = Number.isNaN(lastStorageAt) || Date.now() - lastStorageAt >= STORAGE_PROBE_INTERVAL_MS;
884
+ if (prev && prev.logPath === lp && prev.checkedSize === tail.size && prev.checkedMtime === tail.mtime && !(storagePending && storageDue)) return prev;
885
+ const nowMs = Date.now();
886
+ const now = new Date(nowMs).toISOString();
529
887
  const extracted = extractHarnessSessionId(prev && prev.harness, tail.text);
530
888
  const prevId = prev && prev.harnessSessionId;
531
889
  let id = prevId || undefined;
@@ -538,6 +896,22 @@ export function syncWorkerSession(d, name, logPath) { // respawn slice 1
538
896
  } else if (extracted && prevId && source === "preassigned") {
539
897
  source = "preassigned-confirmed";
540
898
  }
899
+ let storageCheckedAt = (prev && prev.storageCheckedAt) || undefined;
900
+ if (storagePending && storageDue && !id) { // log scan wins when both fire
901
+ storageCheckedAt = now;
902
+ const probe = storageProbe === undefined ? probeOpencodeStorage : storageProbe; // tests inject a fake
903
+ let probed = null;
904
+ try {
905
+ probed = probe({ name, cwd: prev && prev.cwd, spawnedAt: prev && prev.spawnedAt });
906
+ } catch {
907
+ probed = null;
908
+ }
909
+ if (probed) {
910
+ id = String(probed);
911
+ source = "storage";
912
+ capturedAt = now;
913
+ }
914
+ }
541
915
  const doc = {
542
916
  ...((prev && typeof prev === "object") ? prev : {}),
543
917
  name,
@@ -549,6 +923,7 @@ export function syncWorkerSession(d, name, logPath) { // respawn slice 1
549
923
  harnessSessionId: id,
550
924
  idSource: source,
551
925
  capturedAt,
926
+ storageCheckedAt,
552
927
  checkedSize: tail.size,
553
928
  checkedMtime: tail.mtime,
554
929
  checkedAt: now,
@@ -699,12 +1074,14 @@ export function installCliToUserPath() {
699
1074
  return { ok: true, binDir: userBinDir, addedToPath, detail };
700
1075
  }
701
1076
 
702
- export function bootWorker(d, spawnOpts, { to, id, from, subject, body, rev, logDir, budgetTokens, budgetMinutes, deadlineAt, spawnedWorktree, spawnedBranch, spawnedLifetime, sessionId, crew }) { // line 4338
1077
+ export function bootWorker(d, spawnOpts, { to, id, from, lead, subject, body, rev, logDir, budgetTokens, budgetMinutes, deadlineAt, spawnedWorktree, spawnedBranch, spawnedLifetime, sessionId, crew, workbench, prompt }) { // line 4338
703
1078
  const { cwd, root } = spawnOpts;
704
1079
  const stamp = new Date().toISOString().replace(/[:.]/g, "-");
705
- const prompt = buildSpawnPrompt({ name: to, from, subject, body, replyId: id, rev, cwd, root, crew });
706
- const promptPath = path.join(logDir, `${to}-${stamp}.prompt.md`);
707
- fs.writeFileSync(promptPath, prompt + "\n");
1080
+ // Prompt override: the auto-lead coordinator passes its own brief
1081
+ // (buildCoordinatorPrompt); everything else uses the worker template.
1082
+ const finalPrompt = (typeof prompt === "string" && prompt.trim()) ? prompt : buildSpawnPrompt({ name: to, from, lead, subject, body, replyId: id, rev, cwd, root, crew, workbench });
1083
+ const promptPath = path.join(logDir, `${to}-${stamp}.prompt.md`);
1084
+ fs.writeFileSync(promptPath, finalPrompt + "\n");
708
1085
  const logPath = path.join(logDir, `${to}-${stamp}.log`);
709
1086
  // Boot metadata first: child stdout is often block-buffered (or never
710
1087
  // flushed at all on some harnesses), so the log must carry proof of
@@ -742,7 +1119,7 @@ export function bootWorker(d, spawnOpts, { to, id, from, subject, body, rev, log
742
1119
  preId = undefined;
743
1120
  }
744
1121
  }
745
- const target = buildSpawnTarget({ ...spawnOpts, name: to, promptPath, prompt, sessionId: preId });
1122
+ const target = buildSpawnTarget({ ...spawnOpts, name: to, promptPath, prompt: finalPrompt, sessionId: preId });
746
1123
  const { pid } = launchWorkerProcess({ exe: target.exe, args: target.args, shell: target.shell, cwd, env: childEnv, logPath, stdinPath: target.stdinPath });
747
1124
  touchAgent(d, to, { spawnedPid: pid, spawnedAt: new Date().toISOString(), spawnedBy: from, briefId: id, lastDir: cwd, budgetTokens, budgetMinutes, budgetSince: (budgetTokens !== undefined || budgetMinutes !== undefined) ? new Date().toISOString() : undefined, deadlineAt, spawnedWorktree, spawnedBranch, spawnedLifetime, spawnedHarness: cleanHarnessDriver(spawnOpts.harness), spawnedEnvScrubbed: scrub.kept ? 0 : scrub.scrubbed.length });
748
1125
  // Binding doc for the later respawn step (preassigned id lands here