// F2 (PRD 1.20 draft REQ-SETUP-2): the CLI side of the north-star approval the Projects page offers. // `mosaic project north-star ` shows the spec and the assistant's draft; with --approve and // --base it copies that exact draft commit into spec/north-star.md through the human save verb, the // same approveNorthStarDraft the Console's /api/north-star/approve calls. It runs as the human through // callFor, so it refuses inside an agent like every other human command. import { approveNorthStarDraft, interviewMessage, isTemplateNorthStar, loadInterviewTemplate, OnboardingError, SPEC_PATH } from '../../project/src/onboarding.mjs'; import { CliError } from './errors.mjs'; import { parseArgs } from './thread.mjs'; import { busExit } from './transport.mjs'; export const NORTH_STAR_USAGE = 'mosaic project north-star [--business ] [--approve --base ]'; // The Console's "Start onboarding interview" is two bus writes; the CLI does each: thread create with the // assistant instance, then talk with this message. It reads only the template and contacts no bus. export const INTERVIEW_USAGE = 'mosaic project interview prints the interview\'s first message for mosaic talk --thread'; const ID = /^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/; const COMMIT = /^[0-9a-f]{40}$/; // A bus refusal keeps the exit the human transport gave it; a check of approveNorthStarDraft's own is invalid input (2). const exitOf = error => error.cause?.exitCode ?? (error.code === 'onboarding-template-invalid' || error.code === 'not-configured' ? 3 : busExit(error.code)); // The test transport throws plain coded errors; the real one throws CliError already. const asCli = error => { if (error instanceof CliError || typeof error?.code !== 'string') return error; return Object.assign(new CliError(error.code, busExit(error.code)), { code: error.code }); }; export async function northStarCommand(args, io, callFor, { template = loadInterviewTemplate } = {}) { const parsed = parseArgs(args, NORTH_STAR_USAGE, { flags: [], values: ['--business', '--approve', '--base'] }); if (parsed.positional.length !== 1) throw new CliError(`project north-star takes one records project\n ${NORTH_STAR_USAGE}`, 4); const [project] = parsed.positional; if (!ID.test(project)) throw new CliError(`${JSON.stringify(project)} is not a records project id\n ${NORTH_STAR_USAGE}`, 4); const draft = parsed.values['--approve'], base = parsed.values['--base']; if ((draft === undefined) !== (base === undefined)) throw new CliError(`--approve and --base go together\n ${NORTH_STAR_USAGE}`, 4); if (draft !== undefined && (!COMMIT.test(draft) || !COMMIT.test(base))) throw new CliError(`--approve and --base each take a full 40-character commit\n ${NORTH_STAR_USAGE}`, 4); let drafts; try { ({ drafts } = template()); } catch (error) { throw new CliError(`the interview template is unusable: ${error.message}`, 3); } const call = callFor(parsed); if (draft !== undefined) { try { const done = await approveNorthStarDraft({ read: a => call('project.file', a), save: a => call('project.file.save', a), project, draft, base, drafts }); return io.stdout.write(`approved ${SPEC_PATH} commit ${done.commit} from ${done.draft.path} ${done.draft.commit}\n`); } catch (error) { if (!(error instanceof OnboardingError)) throw asCli(error); const exit = exitOf(error); const unknown = exit === 1 ? ' The north star may have been saved; read the project before you approve again.' : ''; throw Object.assign(new CliError(`${error.code}: ${error.message}${unknown}`, exit), { code: error.code }); } } let spec, found = null; try { spec = await call('project.file', { project, path: SPEC_PATH }); } catch (error) { throw asCli(error); } try { found = await call('project.file', { project, path: drafts.northStar }); } catch (error) { if (error?.code !== 'project-read-missing') throw asCli(error); } const isTemplate = isTemplateNorthStar(spec.content); const lines = [`${SPEC_PATH} commit ${spec.commit} ${spec.at} ${isTemplate ? 'still the template' : `by ${spec.author}`}`]; if (!found) lines.push(`${drafts.northStar}: no draft yet${isTemplate ? '; the onboarding interview on the project page asks the assistant for one' : ''}`); else { lines.push(`${drafts.northStar} commit ${found.commit} ${found.at} by ${found.author}`, '', found.content.replace(/\n?$/, '\n')); const business = parsed.values['--business'] ? ` --business ${parsed.values['--business']}` : ''; lines.push(found.content.trim() ? `approve this draft: mosaic project north-star ${project}${business} --approve ${found.commit} --base ${spec.commit}` : 'the draft is empty; nothing to approve'); } io.stdout.write(`${lines.join('\n')}\n`); } export function interviewCommand(args, io, { template = loadInterviewTemplate } = {}) { const parsed = parseArgs(args, INTERVIEW_USAGE); if (parsed.positional.length !== 1 || !ID.test(parsed.positional[0])) throw new CliError(`project interview takes one records project id\n ${INTERVIEW_USAGE}`, 4); let plan; try { plan = template(); } catch (error) { throw new CliError(`the interview template is unusable: ${error.message}`, 3); } io.stdout.write(`${interviewMessage(plan, { project: parsed.positional[0] })}\n`); }