@botbuddy/cli 1.64.13 → 1.64.14

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/src/stack.mjs CHANGED
@@ -28,6 +28,7 @@
28
28
  // `stop` itself and self-activates/finalizes via the fallback MCP tools.
29
29
 
30
30
  import { writeLaneStatus } from "./lane-status.mjs";
31
+ import { markFeedbackGuidanceShown } from "./feedback-reminder.mjs";
31
32
  import { spawn, spawnSync } from "child_process";
32
33
  import { existsSync, lstatSync, readFileSync, realpathSync } from "fs";
33
34
  import { open, unlink, readFile } from "fs/promises";
@@ -58,7 +59,7 @@ import { FORWARD_ATTESTATION_ENV, startForwardAttestation, readForwardAttestatio
58
59
  import { stackServicesForForwards } from "./stack-service-discovery.mjs";
59
60
  import { ENGINE_PUBLISHES_BEYOND_LOOPBACK, engineSocketIsLocal, formatPublishIp, isLoopbackIp, publishIpFix, resolvePublishIp } from "./docker-publish-ip.mjs";
60
61
  import { bold, dim, yellow } from "./utils.mjs";
61
- import { STACK_RELEASE_INTENT_ENV, observeStackRunSignal, ownerReleaseHeld, reportStackRunOutcome } from "./stack-run-outcome.mjs";
62
+ import { STACK_RELEASE_INTENT_ENV, classifyStackRunOutcome, observeStackRunSignal, ownerReleaseHeld, reportStackRunOutcome } from "./stack-run-outcome.mjs";
62
63
 
63
64
  export const STACK_SCHEMA_VERSION = 1;
64
65
 
@@ -149,6 +150,10 @@ const STACK_READINESS_HELP = `${bold("READINESS")}
149
150
 
150
151
  export const STACK_HELP = `${bold("botbuddy stack")} — one command for a batch-scoped local stack lease (BOT-1218)
151
152
 
153
+ If BotBuddy blocks your work, run bb feedback init --file .botbuddy/feedback-report.json,
154
+ fill in the failure and evidence, then run bb feedback submit --file .botbuddy/feedback-report.json.
155
+ Preserve the original lease and do not cancel or repeat it merely to report the problem.
156
+
152
157
  ${bold("USAGE")}
153
158
  botbuddy stack up [options] Request a lease; park if the host is full; hold it active
154
159
  botbuddy stack status --admission-request-id <uuid>
@@ -887,7 +892,7 @@ export function truncateReceipt(receipt, maxBytes = DEFAULT_RECEIPT_MAX_BYTES) {
887
892
  const r = { ...receipt };
888
893
  if (r.connection !== undefined) r.connection = { connection_truncated: true };
889
894
  if (enc(r) <= maxBytes) return r;
890
- return {
895
+ const minimal = {
891
896
  schema_version: r.schema_version, command: r.command, outcome: r.outcome,
892
897
  lease_id: r.lease_id ?? null, state: r.state ?? null, exit_code: r.exit_code,
893
898
  error: r.error ?? null, truncated: true,
@@ -896,6 +901,8 @@ export function truncateReceipt(receipt, maxBytes = DEFAULT_RECEIPT_MAX_BYTES) {
896
901
  ...(r.cleanup_code ? { cleanup_code: r.cleanup_code } : {}),
897
902
  ...(r.cleanup_error ? { cleanup_error: String(r.cleanup_error).slice(0, 160) } : {}),
898
903
  };
904
+ const withFeedback = r.feedback_recovery ? { ...minimal, feedback_recovery: r.feedback_recovery } : minimal;
905
+ return enc(withFeedback) <= maxBytes ? withFeedback : minimal;
899
906
  }
900
907
 
901
908
  /** Resolve the requested stack directory once, before it leaves the coding
@@ -1442,6 +1449,12 @@ const AGENT_DEEPLINK_REQUIRED_GUIDANCE = "the control plane refused this stack b
1442
1449
 
1443
1450
  /** The refusal text for a receipt: the server's message, else the CLI's guidance, else the code. */
1444
1451
  export function leaseRefusalError(data) {
1452
+ if (data?.code === "STALE_AGENT_CONTEXT") {
1453
+ // These identifiers are shown only in the local receipt; never copy an
1454
+ // arbitrary server string into it or into the separate telemetry envelope.
1455
+ const ticket = (value) => typeof value === "string" && /^[A-Z][A-Z0-9]{1,15}-[0-9]{1,12}$/.test(value) ? value : "unknown";
1456
+ return `STALE_AGENT_CONTEXT: registered ticket ${ticket(data.registered_ticket_id)}; requested ticket ${ticket(data.requested_ticket_id)}. No lease was created. Run \`bb help register_agent\` to inspect the supported session registration options before retrying.`;
1457
+ }
1445
1458
  if (data?.lease_id && ["STACK_CONTEXT_BIND_FAILED", "STACK_CONTEXT_ALREADY_BOUND"].includes(data?.code)) {
1446
1459
  return `${data.message || data.code}; lease ${data.lease_id} is ${data.state ?? "unknown"}. ` +
1447
1460
  `Inspect with \`bb stack status ${data.lease_id}\`; after the owning batch ends, release with \`bb stack done ${data.lease_id} --reap\`, ` +
@@ -5326,7 +5339,7 @@ export function runReceiptFields(result) {
5326
5339
  * or the returned exit code.
5327
5340
  */
5328
5341
  export async function cmdRun(opts, childArgv, {
5329
- lifecycle = runStackLifecycle, report = reportStackRunOutcome, now = Date.now, env = process.env,
5342
+ lifecycle = runStackLifecycle, report = reportStackRunOutcome, now = Date.now, env = process.env, feedbackLog = (message) => process.stderr.write(message), markFeedback = markFeedbackGuidanceShown,
5330
5343
  } = {}) {
5331
5344
  // A caller that needs a wait selects this immutable UUID before dispatch.
5332
5345
  // Ordinary foreground runs still receive a unique telemetry id, but it is not
@@ -5335,9 +5348,23 @@ export async function cmdRun(opts, childArgv, {
5335
5348
  const startedAt = now();
5336
5349
  const result = await lifecycle(opts, childArgv);
5337
5350
  const fields = runReceiptFields(result);
5338
- const code = emit(buildReceipt(fields), opts, result.exitCode);
5351
+ const receipt = buildReceipt(fields);
5352
+ const failureClass = classifyStackRunOutcome(stackRunOutcomeFields(result), result.exitCode);
5353
+ const feedbackEligible = failureClass.category === "platform" || (failureClass.category === "invocation" && failureClass.code !== "INVALID_ARGUMENTS");
5354
+ if (feedbackEligible) {
5355
+ receipt.feedback_recovery = { status: "not_yet_checked", available_after: "command_exits", recover_command: "bb feedback recover", list_command: "bb call list_feedback" };
5356
+ }
5357
+ const code = emit(receipt, opts, result.exitCode);
5339
5358
  try {
5340
- await report({ fields: stackRunOutcomeFields(result), exitCode: code, runId, opts, env, durationMs: now() - startedAt, channel: result?.[OUTCOME_CHANNEL] ?? null });
5359
+ const outcome = await report({ fields: stackRunOutcomeFields(result), exitCode: code, runId, opts, env, durationMs: now() - startedAt, channel: result?.[OUTCOME_CHANNEL] ?? null });
5360
+ const capture = outcome?.feedback_capture;
5361
+ if (!opts?.json && capture?.status === "captured" && /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(capture.feedback_id ?? "")) {
5362
+ feedbackLog(`BotBuddy retained a failure report. Check it: bb feedback status --feedback-id ${capture.feedback_id}\nCreate a private draft: bb feedback init --file bb-feedback.json\nEdit the draft, then add your explanation: bb feedback enrich --feedback-id ${capture.feedback_id} --file bb-feedback.json\n`);
5363
+ markFeedback();
5364
+ } else if (!opts?.json && (capture?.status === "pending" || (outcome?.sent === false && feedbackEligible))) {
5365
+ feedbackLog("Automatic feedback capture was not confirmed and may still complete. Recover using the same identity: bb feedback recover\nThen check existing reports: bb call list_feedback\nIf the source event was not recorded, create a report: bb feedback init --file bb-feedback.json\n");
5366
+ markFeedback();
5367
+ }
5341
5368
  } catch { /* best-effort: the receipt and exit code are already final */ }
5342
5369
  return code;
5343
5370
  }
@@ -68,13 +68,15 @@ export default class BotBuddyPlaywrightReporter {
68
68
 
69
69
  onBegin(_config, suite) {
70
70
  if (!this._path) return;
71
- const tests = typeof suite?.allTests === "function" ? suite.allTests() : [];
71
+ const tests = typeof suite?.allTests === "function" ? suite.allTests() : null;
72
+ if (!Array.isArray(tests)) return;
72
73
  tests.forEach((t, i) => {
73
74
  const key = caseKey(t);
74
75
  this._order.set(key, i);
75
76
  const title = (typeof t?.titlePath === "function" ? t.titlePath() : []).filter(Boolean).join(" › ") || key;
76
77
  this._append({ type: "begin", key, title, sort_order: i });
77
78
  });
79
+ this._append({ type: "catalog_complete", case_count: tests.length });
78
80
  }
79
81
 
80
82
  onTestEnd(test, result) {
@@ -14,12 +14,19 @@ export const SYNC_BATCH = 500;
14
14
  export function parseLaneEvents(text, { warn = (m) => process.stderr.write(m) } = {}) {
15
15
  const cases = new Map();
16
16
  let malformed = 0;
17
+ let catalogComplete = false;
17
18
  for (const raw of String(text ?? "").split("\n")) {
18
19
  const line = raw.trim();
19
20
  if (!line) continue;
20
21
  let ev;
21
22
  try { ev = JSON.parse(line); } catch { malformed += 1; warn("[botbuddy] skipping corrupt lane event line\n"); continue; }
23
+ if (ev?.type === "catalog_complete") {
24
+ catalogComplete = Number.isSafeInteger(ev.case_count) && ev.case_count >= 0 && ev.case_count === cases.size;
25
+ if (!catalogComplete) malformed += 1;
26
+ continue;
27
+ }
22
28
  if (!ev || typeof ev.key !== "string" || (ev.type !== "begin" && ev.type !== "verdict")) { malformed += 1; continue; }
29
+ if (!cases.has(ev.key)) catalogComplete = false;
23
30
  const c = cases.get(ev.key) ?? { key: ev.key, title: ev.key, sort_order: cases.size, verdict: "pending", fail_reason: null, duration_ms: null };
24
31
  if (typeof ev.title === "string" && ev.title) c.title = ev.title;
25
32
  if (Number.isInteger(ev.sort_order)) c.sort_order = ev.sort_order;
@@ -30,7 +37,7 @@ export function parseLaneEvents(text, { warn = (m) => process.stderr.write(m) }
30
37
  }
31
38
  cases.set(ev.key, c);
32
39
  }
33
- return { cases, malformed };
40
+ return { cases, malformed, catalogComplete };
34
41
  }
35
42
 
36
43
  // Case map → the sync_test_run_cases payload, ordered by sort_order.
@@ -0,0 +1,62 @@
1
+ // Retain only the fixed failure code, and only when this exact session token
2
+ // binds to the cached agent and tenant. Replay re-verifies both identities.
3
+ import { randomUUID } from "node:crypto";
4
+ import { mkdtemp, rm, writeFile } from "node:fs/promises";
5
+ import { tmpdir } from "node:os";
6
+ import { join } from "node:path";
7
+ import { cacheBelongsToHarness, findAgentStateRoot, harnessSessionIdFromEnv, readAgentState } from "./agent-state.mjs";
8
+ import { restrictWindowsAcl, restrictWindowsAclDir } from "./agent-state-fs.mjs";
9
+ import { queueFeedback } from "./feedback-outbox.mjs";
10
+
11
+ /** @param {any} options */
12
+ export async function captureWaitRegistrationFailure({ token, claim = false, cwd = process.cwd(),
13
+ loadState = readAgentState, rootFor = findAgentStateRoot, queue = queueFeedback,
14
+ harnessId = harnessSessionIdFromEnv(), uuid = randomUUID } = {}) {
15
+ try {
16
+ const state = await loadState(cwd);
17
+ if (!state || !cacheBelongsToHarness(state, harnessId) ||
18
+ typeof token !== "string" || token !== state.agent_session_token ||
19
+ !/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(state.agent_id ?? "") ||
20
+ !/^[a-z0-9][a-z0-9-]{0,63}$/.test(state.tenant ?? "")) return false;
21
+ const code = claim ? "claim_registration_failed" : "register_failed";
22
+ const result = await queue({ root: await rootFor(cwd), agentId: state.agent_id, tenant: state.tenant,
23
+ report: { idempotency_key: uuid(), kind: "unknown", surface: "wait", impact: "unknown",
24
+ title: claim ? "Wait claim registration failed" : "Wait registration failed", error_code: code,
25
+ actual: claim ? "The claim wait could not confirm a registered session." : "The wait could not confirm a registered session.",
26
+ evidence: [{ type: "text", value: `Wait receipt error: ${code}` }] } });
27
+ return result?.queued === true;
28
+ } catch {
29
+ return false;
30
+ }
31
+ }
32
+
33
+ export function captureClaimRegistrationFailure(options = {}) {
34
+ return captureWaitRegistrationFailure({ ...options, claim: true });
35
+ }
36
+
37
+ // With no proven reporter binding, save only fixed, non-identifying context.
38
+ // It is never replayed automatically: the agent must review it and choose its
39
+ // authenticated reporter and tenant before submitting.
40
+ export async function saveUnboundWaitDraft({ claim = false, tempRoot = tmpdir() } = {}) {
41
+ let directory;
42
+ try {
43
+ directory = await mkdtemp(join(tempRoot, "bb-feedback-wait-"));
44
+ if (process.platform === "win32") await restrictWindowsAclDir(directory);
45
+ const file = join(directory, "report.json");
46
+ const code = claim ? "claim_registration_failed" : "register_failed";
47
+ const report = {
48
+ idempotency_key: randomUUID(), kind: "unknown", surface: "wait", impact: "unknown",
49
+ title: claim ? "Wait claim registration failed" : "Wait registration failed",
50
+ error_code: code, goal: "", steps: "", expected: "",
51
+ actual: claim ? "The claim wait could not confirm a registered session." : "The wait could not confirm a registered session.",
52
+ blocked_reason: "", blocking_step: "", can_continue: null, safe_next_action: "", ticket_ref: "",
53
+ evidence: [{ type: "text", value: `Wait receipt error: ${code}` }],
54
+ };
55
+ await writeFile(file, `${JSON.stringify(report, null, 2)}\n`, { flag: "wx", mode: 0o600 });
56
+ if (process.platform === "win32") await restrictWindowsAcl(file);
57
+ return file;
58
+ } catch {
59
+ if (directory) await rm(directory, { recursive: true, force: true }).catch(() => {});
60
+ return null;
61
+ }
62
+ }
package/src/wait.mjs CHANGED
@@ -22,6 +22,8 @@ import { AGENT_KEY_RE, readAgentSessionTokenEnv, legacyAgentAliasHint } from "./
22
22
  import { clearAgentState, hasAgentSessionRuntime, isRejectedCachedSession, resolveAgentSessionCredential, selfHealAgentSession } from "./agent-session.mjs";
23
23
  import { repairFailureRecovery } from "./agent-doctor.mjs";
24
24
  import { touchAgentStateExpiry } from "./agent-state.mjs";
25
+ import { captureWaitRegistrationFailure, saveUnboundWaitDraft } from "./wait-feedback-capture.mjs";
26
+ import { markFeedbackGuidanceShown } from "./feedback-reminder.mjs";
25
27
  import { SETUP_BLOCK } from "./setup-block.mjs";
26
28
  import { createWaitCheckpointStore, WaitCheckpointError } from "./wait-checkpoint.mjs";
27
29
  import { realpath } from "node:fs/promises";
@@ -56,6 +58,10 @@ Park until a condition holds, then exit ONCE with one JSON receipt on stdout. Ze
56
58
  and zero tokens while parked. Every wait — time included — is a bb wait: never sleep loops,
57
59
  until/while polling, watch, or gh run watch. Full guide: docs/agent-wait.md.
58
60
 
61
+ If BotBuddy blocks your work, run bb feedback init --file .botbuddy/feedback-report.json,
62
+ fill in the failure and evidence, then run bb feedback submit --file .botbuddy/feedback-report.json.
63
+ Preserve the original wait and do not cancel or repeat it merely to report the problem.
64
+
59
65
  USAGE
60
66
  bb wait [--any] <condition>... [options] arm a wait (also: bb help wait)
61
67
  bb wait <subcommand> … inspect or recover a saved wait (SUBCOMMANDS)
@@ -902,6 +908,12 @@ async function finalizeWait(opts, waitSessionId, receipt, timeoutMs = 5000) {
902
908
  process.stderr.write(`bb-wait: finalize (non-fatal) returned ${res.status}\n`);
903
909
  return { delivered: false, error: body?.error || (res.ok ? "finalize_not_applied" : `finalize_http_${res.status}`) };
904
910
  }
911
+ const capture = body?.feedback_capture;
912
+ if (capture?.status === "captured" && typeof capture.feedback_id === "string" && /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(capture.feedback_id)) {
913
+ process.stderr.write(`bb-wait: failure context retained as feedback ${capture.feedback_id}. Create the file, then set kind, surface to wait, title, impact, goal, reproduction, expected and actual outcomes, blocking reason, whether you can continue, safe next action and evidence before enriching:\n bb feedback init --file .botbuddy/feedback-report.json\n bb feedback enrich --feedback-id ${capture.feedback_id} --file .botbuddy/feedback-report.json\n`);
914
+ } else if (capture?.status === "pending") {
915
+ process.stderr.write("bb-wait: feedback capture is pending. After reconnecting with the same agent and tenant, run: bb feedback recover\n");
916
+ }
905
917
  return { delivered: true };
906
918
  } catch (err) {
907
919
  process.stderr.write(`bb-wait: finalize (non-fatal) failed: ${err && err.message || err}\n`);
@@ -1619,10 +1631,32 @@ async function sameWorktree(first, second) {
1619
1631
  catch { return false; }
1620
1632
  }
1621
1633
 
1634
+ function writeWaitFeedbackGuidance() {
1635
+ markFeedbackGuidanceShown();
1636
+ process.stderr.write("bb-wait: If this error blocks your work, preserve the receipt and report your goal, expected and actual outcomes, why you are blocked and evidence:\n bb feedback init --file .botbuddy/feedback-report.json\n bb feedback submit --file .botbuddy/feedback-report.json\nEdit the report before submitting; these instructions do not record feedback. If a partial draft is saved or queued below, use it instead of creating a duplicate. If the file exists, choose a new filename in both commands.\n");
1637
+ }
1638
+
1639
+ async function preserveWaitRegistrationFeedback(token, claim) {
1640
+ if (await captureWaitRegistrationFailure({ token, claim })) {
1641
+ process.stderr.write("bb-wait: A partial draft was queued for this agent and tenant. Do not submit a duplicate. Reconnect and run:\n bb feedback flush\nUse the feedback_id from its receipt. Create and edit the details file with your goal, expected and actual outcomes and why you are blocked, then enrich the same report:\n bb feedback init --file .botbuddy/feedback-details.json\n bb feedback enrich --feedback-id <feedback_id> --file .botbuddy/feedback-details.json\n");
1642
+ return;
1643
+ }
1644
+ const file = await saveUnboundWaitDraft({ claim });
1645
+ if (!file) {
1646
+ process.stderr.write("bb-wait: Could not save a partial draft. Preserve the receipt and use the feedback init and submit commands above.\n");
1647
+ return;
1648
+ }
1649
+ const quoted = process.platform === "win32"
1650
+ ? `'${file.replace(/'/g, "''")}'`
1651
+ : `'${file.replace(/'/g, "'\\''")}'`;
1652
+ process.stderr.write(`bb-wait: A private partial draft was saved without a verified reporter or tenant. Review and complete ${quoted}; verify the reporter and tenant before submitting. The OS may clear temporary files. Run:\n bb call whoami\n bb feedback submit --file ${quoted}\n`);
1653
+ }
1654
+
1622
1655
  async function emitSavedWaitError(error) {
1623
1656
  const code = error?.code ?? "wait_checkpoint_invalid";
1624
1657
  const exitCode = code === "wait_checkpoint_write_failed" ? EXIT.INTERNAL : EXIT.INVALID;
1625
1658
  process.stderr.write(`bb-wait: ${error?.message ?? code}\n`);
1659
+ writeWaitFeedbackGuidance();
1626
1660
  await writeWaitReceipt({
1627
1661
  schema_version: 1,
1628
1662
  outcome: "error",
@@ -1835,6 +1869,7 @@ export async function runWait(argv, { recoveryLocalWaitId = null } = {}) {
1835
1869
  // replayed — carries the lease-lost stderr line when it is a lease_lost terminal.
1836
1870
  const emitReceipt = (receipt, options = {}) => {
1837
1871
  writeLeaseLostNotice(receipt);
1872
+ if (receipt?.outcome === "error") writeWaitFeedbackGuidance();
1838
1873
  return emit(receipt, { ...options, maxBytes: opts.receiptMaxBytes });
1839
1874
  };
1840
1875
  if (opts.help) return writeThenExit(HELP);
@@ -2302,6 +2337,7 @@ export async function runWait(argv, { recoveryLocalWaitId = null } = {}) {
2302
2337
  if (hasClaim && waitSessionId == null) {
2303
2338
  process.stderr.write("bb-wait: claim=true registration returned no wait_session_id; refusing to arm an unmatchable claim wait\n");
2304
2339
  await emitReceipt({ schema_version: 1, outcome: "error", error: "claim_registration_failed" });
2340
+ await preserveWaitRegistrationFeedback(opts.token, true);
2305
2341
  process.exit(EXIT.INTERNAL);
2306
2342
  }
2307
2343
  } catch (err) {
@@ -2456,6 +2492,7 @@ export async function runWait(argv, { recoveryLocalWaitId = null } = {}) {
2456
2492
  if (hasClaim) {
2457
2493
  process.stderr.write(`bb-wait: claim=true wait-session registration failed (failing closed, not arming untracked): ${err && err.message || err}\n`);
2458
2494
  await emitReceipt({ schema_version: 1, outcome: "error", error: "claim_registration_failed" });
2495
+ await preserveWaitRegistrationFeedback(opts.token, true);
2459
2496
  process.exit(EXIT.INTERNAL);
2460
2497
  }
2461
2498
  // BOT-1338/1608: every non-timer wait is a relay-tracked, session-token wait.
@@ -2477,6 +2514,7 @@ export async function runWait(argv, { recoveryLocalWaitId = null } = {}) {
2477
2514
  null,
2478
2515
  { sessionTenant: sessionTenant ?? null, agentId: null },
2479
2516
  ));
2517
+ await preserveWaitRegistrationFeedback(opts.token, false);
2480
2518
  process.exit(EXIT.INTERNAL);
2481
2519
  }
2482
2520
  }
@@ -2584,6 +2622,9 @@ export async function runWait(argv, { recoveryLocalWaitId = null } = {}) {
2584
2622
  // BOT-2360: likewise `abandoned` — the relay refused the stream because the row is ended.
2585
2623
  if (waitSessionId && !SERVER_ENDED_OUTCOMES.has(receipt.outcome)) {
2586
2624
  const finalized = await finalizeWait(opts, waitSessionId, terminalReceipt);
2625
+ if (!finalized.delivered && terminalReceipt.outcome === "error" && typeof checkpoint.local_wait_id === "string" && /^[A-Za-z0-9][A-Za-z0-9_-]{0,127}$/.test(checkpoint.local_wait_id)) {
2626
+ process.stderr.write(`bb-wait: the Wait receipt is saved locally, but cloud finalization was not confirmed. With the same agent and tenant, recover the saved Wait and then recover feedback:\n bb wait resume ${checkpoint.local_wait_id}\n bb feedback recover\n`);
2627
+ }
2587
2628
  try {
2588
2629
  checkpoint = await store.update(checkpoint.local_wait_id, finalized.delivered
2589
2630
  ? { cloud_finalize_pending: false, cloud_finalized_at: new Date().toISOString(), cloud_finalize_error: null }