#!/usr/bin/env node import { spawnSync } from 'node:child_process'; import { existsSync } from 'node:fs'; import { fileURLToPath } from 'node:url'; import { dirname, join } from 'node:path'; import { findCommandInPath, resolveBbPath } from './lib/driver-lib.mjs'; const __dirname = dirname(fileURLToPath(import.meta.url)); function configureMailDelegate(bdPath, shimPath) { if (!existsSync(shimPath)) { return { configured: false, reason: `shim not found at ${shimPath}`, }; } const delegateCmd = `node ${shimPath}`; const result = spawnSync(bdPath, ['config', 'set', 'mail.delegate', delegateCmd], { stdio: 'pipe', shell: false, }); if (result.status !== 0) { const stderr = result.stderr?.toString().trim() || ''; return { configured: false, reason: `bd config set failed: ${stderr || 'non-zero exit'}`, delegate: delegateCmd, }; } return { configured: true, delegate: delegateCmd, shim_path: shimPath, note: 'Set BB_AGENT to your agent name before using bd mail.', }; } function validateMemorySystem(bdPath) { try { const result = spawnSync(bdPath, ['query', 'label=mem-canonical AND status=closed', '--limit', '5'], { stdio: 'pipe', shell: false, }); if (result.status !== 0) { return { validated: false, reason: 'Failed to query memory system', memories_found: 0, }; } const output = result.stdout?.toString() || ''; const memoryCount = (output.match(/beadboard-/g) || []).length; return { validated: true, memories_found: memoryCount, note: 'Remember to read memory beads at session start: bd show beadboard-116 beadboard-60a beadboard-zas', }; } catch (error) { return { validated: false, reason: error instanceof Error ? error.message : String(error), memories_found: 0, }; } } async function main() { const shimPath = join(__dirname, 'bb-mail-shim.mjs'); try { const bdPath = await findCommandInPath('bd'); if (!bdPath) { process.stdout.write( `${JSON.stringify( { ok: false, error_code: 'BD_NOT_FOUND', reason: 'Could not find bd in PATH.', remediation: process.platform === 'win32' ? 'Primary: npm i -g beadboard. Fallback: powershell -ExecutionPolicy Bypass -File ./install/install.ps1. Then ensure bd is available in PATH.' : 'Primary: npm i -g beadboard. Fallback: bash ./install/install.sh. Then ensure bd is available in PATH.', tools: { bd: { available: false, path: null }, }, bb: null, mail: null, memory: null, }, null, 2, )}\n`, ); return; } const bb = await resolveBbPath(); if (!bb.ok) { process.stdout.write( `${JSON.stringify( { ok: false, error_code: 'BB_NOT_FOUND', reason: bb.reason, remediation: bb.remediation, tools: { bd: { available: true, path: bdPath }, }, bb, mail: { configured: false, reason: 'bb not available — mail delegate requires bb agent commands', }, memory: null, }, null, 2, )}\n`, ); return; } const mail = configureMailDelegate(bdPath, shimPath); const memory = validateMemorySystem(bdPath); process.stdout.write( `${JSON.stringify( { ok: true, timestamp: new Date().toISOString(), tools: { bd: { available: true, path: bdPath }, }, bb, mail, memory, }, null, 2, )}\n`, ); } catch (error) { process.stdout.write( `${JSON.stringify( { ok: false, error_code: 'PREFLIGHT_INTERNAL_ERROR', reason: error instanceof Error ? error.message : String(error), remediation: 'Inspect session-preflight.js and retry.', tools: { bd: { available: false, path: null }, }, bb: null, mail: null, memory: null, }, null, 2, )}\n`, ); } } void main();