Files
stack/packages/bus/tests/north-star-approval.test.mjs
jason.woltje 00ae2c718c feat(project,cli): onboarding interview template and north-star approval (F2)
REQ-SETUP-2 (PRD 1.20 draft), unified workspace direction ruling 14 and
phase F2.

- packages/project/templates/onboarding-interview.json: the interview's
  conversation name, four questions, draft paths and first message.
  loadInterviewTemplate(url) reads it and validateInterviewTemplate checks
  it, so an organization file or a plug-in can pass a replacement later.
- approveNorthStarDraft: agents never write spec/, so the assistant
  proposes docs/north-star-draft.md with its existing record.write grant
  and the human approves. The approval copies the exact reviewed draft
  commit into spec/north-star.md through the human save verb, pinned to
  the reviewed spec commit. A rewritten draft or a moved spec writes
  nothing. No new verb, grant or role change.
- mosaic project north-star <project> [--approve <draft> --base <spec>]
  shows the spec and the draft and approves as the human; mosaic project
  interview <project> prints the first message; mosaic thread create
  takes --instance.
2026-11-09 12:58:29 -06:00

77 lines
5.6 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { readFileSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
import { fixture, git } from './spec-fixtures.mjs';
import { readSpec } from '../../project/src/index.mjs';
import { approveNorthStarDraft, isTemplateNorthStar, loadInterviewTemplate, SPEC_PATH } from '../../project/src/onboarding.mjs';
// F2 (REQ-SETUP-2): the north star through the bus. The assistant proposes with the record.write grant
// the shipped roles/assistant.json already gives it; only the human's project.file.save writes the spec.
// The assistant runs under the shipped authority and messaging, so this is the grant the stack runs.
const shipped = name => JSON.parse(readFileSync(fileURLToPath(new URL(`../../../roles/${name}.json`, import.meta.url)), 'utf8'));
const within = { authority: { withinRole: ['message.send'], crossRole: [] } };
const { drafts } = loadInterviewTemplate();
async function setup(t) {
const f = await fixture(t, { roles: {
pm: { definition: 'pm', ...within }, cto: { definition: 'cto', ...within },
assistant: { definition: 'assistant', authority: shipped('assistant').authority },
} });
f.business.definitions = { assistant: { messaging: shipped('assistant').messaging } };
const project = f.call(f.human, 'project.create', { name: 'stack work', repo: 'stack' }).id;
const thread = f.call(f.human, 'thread.create', { project, name: 'Onboarding interview', instance: 'assistant' }).id;
f.assistant = f.broker.bindLaunch({ business: 'demo', role: 'assistant', run: 'assistant-run', harness: 'pi', thread });
f.broker.request(f.assistant, { verb: 'role.claim' });
f.notices = () => f.store.all("SELECT to_role, body FROM messages WHERE from_role='_bus' ORDER BY seq");
f.approver = {
read: args => f.call(f.human, 'project.file', args),
save: args => f.call(f.human, 'project.file.save', args),
};
return f;
}
const refusals = f => f.store.all("SELECT body FROM events WHERE kind='action.refused'").map(r => JSON.parse(r.body).code);
test('the assistant cannot write the north star by any verb; it proposes a draft doc instead', async t => {
const f = await setup(t);
await assert.rejects(f.call(f.assistant, 'record.write', { project: 'stack', path: SPEC_PATH, record: '# Mine\n' }), { code: 'project-spec-human-only' });
await assert.rejects(f.call(f.assistant, 'project.file.save', { project: 'stack', path: SPEC_PATH, base: f.commit, content: '# Mine\n' }), { code: 'human-required' });
assert.equal(git(f.repo, 'rev-parse', 'HEAD'), f.commit);
assert.ok(refusals(f).includes('project-spec-human-only') && refusals(f).includes('human-required'));
const draft = await f.call(f.assistant, 'record.write', { project: 'stack', path: drafts.northStar, record: '# North star\n\nOnboard new organizations.\n' });
assert.equal(git(f.repo, 'show', '-s', '--format=%an', draft.commit), 'assistant.assistant');
// The draft alone changes nothing: the spec is still the template, unconfirmed, and no notice went out.
const spec = await f.call(f.human, 'project.file', { project: 'stack', path: SPEC_PATH });
assert.equal(spec.commit, f.commit); assert.equal(isTemplateNorthStar(spec.content), true);
assert.deepEqual(f.notices(), []);
});
test('the human approval copies the reviewed draft into the spec as the human, confirms it and notifies pm and cto', async t => {
const f = await setup(t);
const text = '# North star\n\nOnboard new organizations in one sitting.\n';
const draft = await f.call(f.assistant, 'record.write', { project: 'stack', path: drafts.northStar, record: text });
const spec = await f.call(f.human, 'project.file', { project: 'stack', path: SPEC_PATH });
const done = await approveNorthStarDraft({ ...f.approver, project: 'stack', draft: draft.commit, base: spec.commit, drafts });
assert.equal(git(f.repo, 'rev-parse', 'HEAD'), done.commit);
assert.equal(git(f.repo, 'show', '-s', '--format=%an|%s', done.commit), 'jason|Edit spec/north-star.md');
const after = await readSpec({ business: f.business, project: 'stack', dataRoot: f.root });
assert.equal(after.content, text); assert.equal(after.confirmed, true); assert.equal(after.commit, done.commit);
const sent = f.notices();
assert.deepEqual(sent.map(n => n.to_role).sort(), ['cto', 'pm']);
for (const n of sent) assert.ok(n.body.includes(`commit ${done.commit}, from the editor's base ${spec.commit}`));
});
test('an approval of a draft the assistant rewrote, or over a spec that moved, writes nothing and sends no notice', async t => {
const f = await setup(t);
const first = await f.call(f.assistant, 'record.write', { project: 'stack', path: drafts.northStar, record: '# First\n' });
const spec = await f.call(f.human, 'project.file', { project: 'stack', path: SPEC_PATH });
const second = await f.call(f.assistant, 'record.write', { project: 'stack', path: drafts.northStar, record: '# Second\n' });
await assert.rejects(approveNorthStarDraft({ ...f.approver, project: 'stack', draft: first.commit, base: spec.commit, drafts }), { code: 'draft-stale' });
const moved = await f.call(f.human, 'project.file.save', { project: 'stack', path: SPEC_PATH, base: spec.commit, content: '# Typed by hand\n' });
const head = git(f.repo, 'rev-parse', 'HEAD'), notices = f.notices().length;
await assert.rejects(approveNorthStarDraft({ ...f.approver, project: 'stack', draft: second.commit, base: spec.commit, drafts }), { code: 'stale-base', stage: 'save' });
assert.equal(git(f.repo, 'rev-parse', 'HEAD'), head);
assert.equal(f.notices().length, notices);
assert.equal(git(f.repo, 'show', `HEAD:${SPEC_PATH}`), '# Typed by hand');
assert.equal(moved.commit, head);
});