Skip to content

Autonomous development for real products. Governed agents build from specs you approve, follow the rules you set, and nothing merges until another agent proves it. Your decisions, judgement and taste are the bar that work must clear. Declare your vision and let agents build it.

fix(blueprint): first add files every row; retries keep waits [S1,I1]

Corey Olson4705042548f0
diff --git a/src/blueprints.js b/src/blueprints.js
index 4cd649f5..3a00258b 100644
--- a/src/blueprints.js
+++ b/src/blueprints.js
@@ -188,6 +188,32 @@ export function blueprintMarker(blueprint) {
return `<!-- pullboard-blueprint ${blueprint.name} ${blueprint.version} -->`;
}
+/**
+ * The marker line written with the rows while an add is under way: it records which row id each
+ * rule and question key got, so a retry after a failure part way files no row twice and finds the
+ * starter items already added by those ids. The finished add replaces it with blueprintMarker.
+ *
+ * @param {{ name: string, version: number }} blueprint
+ * @param {Record<string, string>} ids - Row id by rule or question key.
+ * @returns {string}
+ */
+export function blueprintAddingMarker(blueprint, ids) {
+ return `<!-- pullboard-blueprint ${blueprint.name} ${blueprint.version} adding ${Object.entries(ids).map(([key, id]) => `${key}=${id}`).join(' ')} -->`;
+}
+
+/**
+ * Read an under-way marker line for this blueprint version, or null.
+ *
+ * @param {string} line
+ * @param {{ name: string, version: number }} blueprint
+ * @returns {Record<string, string> | null}
+ */
+function addingIds(line, blueprint) {
+ const match = /^<!-- pullboard-blueprint (\S+) (\d+) adding ((?:[A-Za-z0-9]+=[A-Za-z0-9.]+ ?)+) -->$/u.exec(line.trim());
+ if (!match || match[1] !== blueprint.name || Number(match[2]) !== blueprint.version) return null;
+ return Object.fromEntries(match[3].trim().split(' ').map((pair) => pair.split('=')));
+}
+
/**
* One rule's row text: the rule, then its reason.
*
@@ -209,72 +235,80 @@ export function questionText(question) {
}
/**
- * Work out what adding a blueprint changes, without writing anything: the section (found or new),
- * the rows not already in the spec with their new ids, the new SPEC.md text, and the items not
- * already on the board, each citing real row ids. A repeat of the same version plans nothing.
+ * Work out what adding a blueprint changes, without writing anything. A first add files every rule
+ * as a new draft row and every question as a new pending row, whatever the spec already says, in
+ * the blueprint's section (found by title, or new), and plans every starter item. A retry after a
+ * failure part way (the under-way marker is there) files no rows and plans only the starters not
+ * yet on the board, found by title and the row ids they cite. A finished add (the marker is there)
+ * plans nothing.
*
* @param {Blueprint} blueprint
- * @param {{ source: string, spec: any, doctrine: any, itemTitles: Iterable<string>, gate: string }} board
- * source is SPEC.md's text and spec its parse; doctrine is loadDoctrine's result; itemTitles are
- * every item title on the board; gate is pullboard.json's gate now.
- * @returns {{ marked: boolean, section: { prefix: string, name: string, isNew: boolean }, rows: { key: string, id: string, status: string, text: string, line: string }[], specRows: string, specAfter: string, ids: Record<string, string>, items: { key: string, title: string, specIds: string[], after: string[], lane?: string, route: string, criterion: string, brief: string }[], gate: string | null }}
+ * @param {{ source: string, spec: any, doctrine: any, existingItems: { id: number, title: string, specIds: string[] }[], gate: string }} board
+ * source is SPEC.md's text and spec its parse; doctrine is loadDoctrine's result; existingItems
+ * are the items on the board; gate is pullboard.json's gate now.
+ * @returns {{ marked: boolean, resumed: boolean, section: { prefix: string, name: string, isNew: boolean }, rows: { key: string, id: string, status: string, text: string, line: string }[], specRows: string, specAfter: string, ids: Record<string, string>, items: { key: string, title: string, specIds: string[], after: string[], lane?: string, route: string, criterion: string, brief: string }[], existing: Record<string, number>, gate: string | null }}
+ * specRows is the text to write before the items (rows and the under-way marker), specAfter the
+ * text once they are all added (the finished marker); existing maps starter keys already on the
+ * board to their item ids.
*/
-export function planBlueprint(blueprint, { source, spec, doctrine, itemTitles, gate }) {
+export function planBlueprint(blueprint, { source, spec, doctrine, existingItems = [], gate }) {
const marker = blueprintMarker(blueprint);
const lines = source.split(/\r?\n/u);
const marked = lines.some((text) => text.trim() === marker);
+ const addingLine = lines.find((text) => addingIds(text, blueprint));
+ const resumed = !marked && Boolean(addingLine);
const allRows = [...spec.rows, ...(doctrine?.rows ?? []), ...(doctrine?.repo?.rows ?? [])];
const prefixOf = (name) => /^([A-Z][A-Z0-9]*)\s*[·.:-]/u.exec(name)?.[1];
- const existing = spec.sections.find((section) => section.name.replace(/^[A-Z][A-Z0-9]*\s*[·.:-]\s*/u, '') === blueprint.title.trim() && prefixOf(section.name));
+ const existingSection = spec.sections.find((section) => section.name.replace(/^[A-Z][A-Z0-9]*\s*[·.:-]\s*/u, '') === blueprint.title.trim() && prefixOf(section.name));
const taken = new Set([
...allRows.map((row) => /^[A-Za-z]+/u.exec(row.id)?.[0].toUpperCase()),
...spec.sections.map((section) => prefixOf(section.name)),
...(doctrine?.sections ?? []).map((section) => prefixOf(section.name)),
].filter(Boolean));
- const prefix = existing ? prefixOf(existing.name) : freePrefix(blueprint, taken);
- const name = existing ? existing.name : `${prefix} · ${blueprint.title.trim()}`;
- // Only the blueprint's own section counts as already filed: a row elsewhere with the same words,
- // approved or not, belongs to the person's own spec, and the blueprint still files its drafts.
- const own = existing ? spec.rows.filter((row) => row.section === existing.name) : [];
- const byText = new Map(own.map((row) => [row.text, row.id]));
- const askedIds = new Map(own.map((row) => [row.text.split(': ')[0], row.id]));
- // The same rows spec draft numbers against: SPEC.md and the repo's own doctrine.
- const numberedRows = [...spec.rows, ...(doctrine?.repo?.rows ?? [])];
- const ids = {};
+ const prefix = existingSection ? prefixOf(existingSection.name) : freePrefix(blueprint, taken);
+ const name = existingSection ? existingSection.name : `${prefix} · ${blueprint.title.trim()}`;
+ const section = { prefix, name, isNew: !existingSection };
+ const gateToSet = !gate?.trim() && blueprint.gate ? blueprint.gate : null;
+ if (marked) return { marked, resumed: false, section, rows: [], specRows: source, specAfter: source, ids: {}, items: [], existing: {}, gate: null };
const rows = [];
- const allocate = () => {
- const id = nextRowId(prefix, numberedRows);
- numberedRows.push({ id });
- return id;
- };
- for (const rule of blueprint.rules) {
- const text = ruleText(rule);
- if (byText.has(text)) { ids[rule.key] = byText.get(text); continue; }
- if (marked) continue;
- const id = allocate();
- ids[rule.key] = id;
- rows.push({ key: rule.key, id, status: 'draft', text, line: `- ${id} [draft, ${rule.tier}] ${text} | gate: ${rule.gate}` });
+ let ids;
+ let specRows = source;
+ if (resumed) ids = addingIds(addingLine, blueprint);
+ else {
+ // The same rows spec draft numbers against: SPEC.md and the repo's own doctrine.
+ const numberedRows = [...spec.rows, ...(doctrine?.repo?.rows ?? [])];
+ const allocate = () => {
+ const id = nextRowId(prefix, numberedRows);
+ numberedRows.push({ id });
+ return id;
+ };
+ ids = {};
+ for (const rule of blueprint.rules) {
+ const id = allocate();
+ const text = ruleText(rule);
+ ids[rule.key] = id;
+ rows.push({ key: rule.key, id, status: 'draft', text, line: `- ${id} [draft, ${rule.tier}] ${text} | gate: ${rule.gate}` });
+ }
+ for (const question of blueprint.questions) {
+ const id = allocate();
+ const text = questionText(question);
+ ids[question.key] = id;
+ rows.push({ key: question.key, id, status: 'pending', text, line: `- ${id} [pending, must] ${text} | gate: review` });
+ }
+ specRows = insertSection(source, spec, { existing: existingSection, name, lines: [...rows.map((row) => row.line), blueprintAddingMarker(blueprint, ids)] });
}
- for (const question of blueprint.questions) {
- const asked = askedIds.get(question.ask.trim());
- if (asked) { ids[question.key] = asked; continue; }
- if (marked) continue;
- const id = allocate();
- const text = questionText(question);
- ids[question.key] = id;
- rows.push({ key: question.key, id, status: 'pending', text, line: `- ${id} [pending, must] ${text} | gate: review` });
+ const underWay = blueprintAddingMarker(blueprint, ids);
+ const specAfter = specRows.split(/(\r?\n)/u).map((part) => (part.trim() === underWay || (resumed && part === addingLine) ? part.replace(part.trim(), marker) : part)).join('');
+ const existing = {};
+ const items = [];
+ for (const item of blueprint.items) {
+ const specIds = item.cites.map((cited) => ids[cited]).filter(Boolean);
+ const found = resumed && existingItems.find((entry) => entry.title === item.title.trim() && entry.specIds.join(',') === specIds.join(','));
+ if (found) { existing[item.key] = found.id; continue; }
+ items.push({ key: item.key, title: item.title.trim(), specIds, after: item.after,
+ ...(item.lane ? { lane: item.lane } : {}), route: item.route, criterion: item.criterion, brief: item.brief });
}
- const titles = new Set(itemTitles);
- const items = marked ? [] : blueprint.items.filter((item) => !titles.has(item.title.trim())).map((item) => ({
- key: item.key, title: item.title.trim(), specIds: item.cites.map((cited) => ids[cited]).filter(Boolean), after: item.after,
- ...(item.lane ? { lane: item.lane } : {}), route: item.route, criterion: item.criterion, brief: item.brief,
- }));
- const rowLines = rows.map((row) => row.line);
- // Rows first, marker last: the caller writes specRows, adds the items, then writes specAfter, so a
- // failure part way leaves no marker and a retry adds what is missing.
- const specRows = marked || !rows.length ? source : insertSection(source, spec, { existing, name, lines: rowLines });
- const specAfter = marked ? source : insertSection(source, spec, { existing, name, lines: [...rowLines, marker] });
- return { marked, section: { prefix, name, isNew: !existing }, rows, specRows, specAfter, ids, items, gate: !gate?.trim() && blueprint.gate ? blueprint.gate : null };
+ return { marked, resumed, section, rows, specRows, specAfter, ids, items, existing, gate: gateToSet };
}
/**
diff --git a/src/cli.js b/src/cli.js
index a3ef62b9..068d2790 100644
--- a/src/cli.js
+++ b/src/cli.js
@@ -2224,7 +2224,8 @@ async function addBlueprint(ctx, board, me, blueprint, { report = true } = {}) {
const configFile = join(ctx.info.root, CONFIG_FILE);
const configSource = readFileSync(configFile, 'utf8');
const plan = planBlueprint(blueprint, { source, spec, doctrine: ctx.doctrine,
- itemTitles: store.listItems(board, { all: true }).map((item) => item.item_title), gate: String(JSON.parse(configSource).gate ?? '') });
+ existingItems: store.listItems(board, { all: true }).map((item) => ({ id: item.item_id, title: item.item_title, specIds: item.item_spec_ids ? item.item_spec_ids.split(',') : [] })),
+ gate: String(JSON.parse(configSource).gate ?? '') });
// Every starter is checked before anything is written, so a refusal leaves the board as it was.
const starters = plan.items.map((entry) => ({ key: entry.key, afterKeys: entry.after, item: { by: me.id,
lane: entry.lane && entry.lane !== COORDINATOR && isLane(ctx.config, entry.lane) ? entry.lane : COORDINATOR,
@@ -2241,7 +2242,8 @@ async function addBlueprint(ctx, board, me, blueprint, { report = true } = {}) {
writeRowApply(ctx.info.root, write);
const added = [];
try {
- const byKey = {};
+ // A retry starts from the starters a failed add already put on the board.
+ const byKey = { ...plan.existing };
for (const { key, afterKeys, item } of starters) {
item.after = afterKeys.map((before) => byKey[before]).filter(Boolean);
const id = await ordered(ctx, board, 'addItem', [item]);
@@ -2262,7 +2264,7 @@ async function addBlueprint(ctx, board, me, blueprint, { report = true } = {}) {
const summary = { blueprint: { name: blueprint.name, title: blueprint.title, version: blueprint.version },
section: plan.section.name, rows, items: added, gate: plan.gate, marked: plan.marked };
io.result?.(summary);
- if (!rows.length && !added.length && !plan.gate) {
+ if (plan.marked) {
io.say(`${blueprint.title} (version ${blueprint.version}) is already on this board; nothing added`);
return summary;
}
diff --git a/test/blueprint.test.js b/test/blueprint.test.js
index d587b389..75d67cea 100644
--- a/test/blueprint.test.js
+++ b/test/blueprint.test.js
@@ -177,3 +177,41 @@ test('blueprint add files a blueprint as drafts or nothing at all [S1,I1]', () =
assert.equal(added.rows.length, 10);
assert.equal(added.items.length, 3);
});
+
+test('blueprint add files a blueprint as drafts beside approved rows in its own section [S1,I1]', () => {
+ // The person already wrote the blueprint's section, with one rule approved in the same words: a
+ // first add still files every rule as a draft and every question as a pending row.
+ const box = freshBoard();
+ const file = join(box.repo, 'SPEC.md');
+ const rule = `${PUBLISHED.rules[0].text} Why: ${PUBLISHED.rules[0].why}`;
+ writeFileSync(file, `${readFileSync(file, 'utf8').trimEnd()}\n\n## A · Web app\n\n- A1 [approved, must] ${rule} | gate: review\n- A2 [approved, must] ${PUBLISHED.questions[0].ask}: Rails. Choices: Rails, Next.js. | gate: review\n`);
+ box.git('commit', '-q', '-am', 'chore: approve rows in the blueprint section');
+ const added = json(box, 'blueprint', 'add', 'web-app');
+ assert.equal(added.section, 'A · Web app', 'the rows go in the existing section');
+ assert.deepEqual(added.rows.map((row) => [row.key, row.status]), [
+ ...PUBLISHED.rules.map((entry) => [entry.key, 'draft']), ...PUBLISHED.questions.map((entry) => [entry.key, 'pending'])]);
+ const spec = parseSpec(readFileSync(file, 'utf8'));
+ assert.deepEqual(spec.rows.filter((row) => row.status === 'approved').map((row) => row.id), ['A1', 'A2']);
+ assert.equal(spec.rows.filter((row) => row.section === 'A · Web app').length, 12);
+});
+
+test('blueprint add files a blueprint as drafts after an add stopped part way [S1,I1]', () => {
+ // A finished add on one board gives the exact rows; a second board is left where an add stops
+ // after its rows and first starter: the rows and the under-way marker written, item 1 added.
+ const reference = freshBoard();
+ const done = json(reference, 'blueprint', 'add', 'web-app');
+ const ids = Object.fromEntries(done.rows.map((row) => [row.key, row.id]));
+ const underWay = `<!-- pullboard-blueprint web-app 1 adding ${Object.entries(ids).map(([key, id]) => `${key}=${id}`).join(' ')} -->`;
+ const finished = readFileSync(join(reference.repo, 'SPEC.md'), 'utf8');
+ assert.ok(finished.includes('<!-- pullboard-blueprint web-app 1 -->'));
+ const box = freshBoard();
+ writeFileSync(join(box.repo, 'SPEC.md'), finished.replace('<!-- pullboard-blueprint web-app 1 -->', underWay));
+ box.git('commit', '-q', '-am', 'chore: leave an add part way');
+ const first = json(box, 'add', 'coordinator', PUBLISHED.items[0].title, '--specs', `${ids.W1},${ids.W2}`);
+ const retried = json(box, 'blueprint', 'add', 'web-app');
+ assert.deepEqual(retried.rows, [], 'no row is filed twice');
+ assert.deepEqual(retried.items.map((item) => item.key), ['2', '3'], 'only the missing starters are added');
+ assert.deepEqual(retried.items.find((item) => item.key === '3').after, [first.item.item_id], 'the third starter still waits on the first');
+ assert.equal(readFileSync(join(box.repo, 'SPEC.md'), 'utf8'), finished, 'the finished marker replaces the under-way one');
+ assert.equal(json(box, 'blueprint', 'add', 'web-app').items.length, 0, 'and then a repeat adds nothing');
+});