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(spec): keep revision approvals atomic and single-use [P6,B23]

Corey Olsone920003f1c18
diff --git a/src/board.js b/src/board.js
index 41ed5392..9dd9bb63 100644
--- a/src/board.js
+++ b/src/board.js
@@ -187,7 +187,10 @@ import {
import { LOCAL_LANDING_ENGINE } from './land-engine.js';
import { SPEC_APPROVAL_ENGINE } from './spec-approval-engine.js';
import { SPEC_REVISION_ENGINE } from './spec-revision-engine.js';
-import { validRevisionApproval } from './spec-revisions.js';
+import {
+ markAppliedRevisions,
+ validRevisionApproval,
+} from './spec-revisions.js';
export { decideSpecRevisions } from './spec-revisions.js';
import * as landingEnvironment from './landing-environment-guards.js';
import { Refused } from './refused.js';
@@ -2241,6 +2244,7 @@ export function applyRowDecisions(board, { agentId, events }) {
if (!applied.length) return [];
const at = now(board);
writeRowDecisions(board, records.map((record) => events.includes(record.event) ? { ...record, applied: true, appliedAt: at } : record));
+ markAppliedRevisions(board, applied, at);
logEvent(board, agentId, 'row_apply', null, { events: applied.map((record) => record.event) });
return applied.map((record) => ({ ...record, applied: true, appliedAt: at }));
});
diff --git a/src/moves/applyRowDecisions.js b/src/moves/applyRowDecisions.js
index 59f2218d..ff942c95 100644
--- a/src/moves/applyRowDecisions.js
+++ b/src/moves/applyRowDecisions.js
@@ -2,9 +2,16 @@ import { RIGOR_AUTO_ENGINE } from '../rigor-auto-engine.js';
/** Routing metadata owned by applyRowDecisions, discovered without a shared registry edit [C7,V4,H16]. */
import { LANDING_LEASE_ENGINE } from '../landing-lease-engine.js';
import { BOARD_SENTINEL_ENGINE } from '../sentinel-engine.js';
+import { SPEC_REVISION_ENGINE } from '../spec-revision-engine.js';
+import { rowDecisions } from '../board-query.js';
/** Preserve this operation's historical first-matching engine conditions [H16]. */
function minimumEngine(args, board, at) {
+ if (board?.db && rowDecisions(board).some(record => record.revision && args[0]?.events?.includes(record.event))) {
+ return Math.max(SPEC_REVISION_ENGINE, args[0]?.landingFence === undefined ? 0 : LANDING_LEASE_ENGINE,
+ args[1]?.cause?.startsWith('conflict:') ? RIGOR_AUTO_ENGINE : 0,
+ args[1]?.cause?.startsWith('sentinel:') ? BOARD_SENTINEL_ENGINE : 0);
+ }
if (typeof args[1]?.cause === 'string' && args[1].cause.startsWith('conflict:')) return RIGOR_AUTO_ENGINE;
if (args[0]?.landingFence !== undefined) return LANDING_LEASE_ENGINE;
if (typeof args[1]?.cause === 'string' && args[1].cause.startsWith('sentinel:')) return BOARD_SENTINEL_ENGINE;
diff --git a/src/spec-revisions.js b/src/spec-revisions.js
index 8196f3be..93a7736f 100644
--- a/src/spec-revisions.js
+++ b/src/spec-revisions.js
@@ -7,7 +7,10 @@ import {
PERSON,
recordRowDecisions,
} from './board.js';
-import { rowDrafts } from './board-query.js';
+import {
+ rowDecisions,
+ rowDrafts,
+} from './board-query.js';
import { Refused } from './refused.js';
import {
defaultPrincipal,
@@ -71,10 +74,12 @@ export function validSpecRevision(row) {
/** Accept only the exact approved effect of a still-pending board proposal. */
export function validRevisionApproval(board, record) {
const draft = rowDrafts(board).find(entry => entry.id === record.revision?.proposal);
- return Boolean(draft && !draft.decision && validSpecRevision(draft) && record.decision === 'approve'
+ return Boolean(draft && !draft.decision && !draft.applied && validSpecRevision(draft) && record.decision === 'approve'
&& record.kind === 'spec' && record.id === draft.revises && record.file === draft.file
&& record.source === draft.source && record.replacement === draft.replacement && record.text === draft.text
- && JSON.stringify(record.revision) === JSON.stringify(revisionReference(draft)));
+ && JSON.stringify(record.revision) === JSON.stringify(revisionReference(draft))
+ && !rowDecisions(board).some(current => current.kind === 'spec' && current.file === draft.file
+ && current.id === draft.revises && !current.applied));
}
/** Build exact person decisions, retaining normal optional SSH signatures and current-source checks. */
@@ -82,7 +87,7 @@ export function prepareSpecRevisionDecisions(root, config, drafts, { ids, decisi
const signed = decision === 'approve' && hasSignerFile(root);
return ids.map(id => {
const draft = drafts.find(entry => entry.id === id && entry.revises);
- if (!draft || draft.decision) throw new Refused('SPEC_REVISION', `${id} is not an undecided revision proposal; read Needs you`);
+ if (!draft || draft.decision || draft.applied) throw new Refused('SPEC_REVISION', `${id} is not an undecided revision proposal; read Needs you`);
if (decision === 'approve') {
const current = approvedSource(root, config, draft.revises);
if (draft.file !== config.spec || current.source !== draft.source) throw new Refused('STALE_ROW_DECISION', `${draft.revises} changed after ${id} was proposed; draft a fresh revision`);
@@ -103,9 +108,11 @@ export function decideSpecRevisions(board, { agentId, channel, decisions }) {
return atomic(board, () => {
let drafts = rowDrafts(board);
const seen = new Set();
- const results = decisions.map(record => {
+ const targets = new Set();
+ const pending = rowDecisions(board).filter(record => !record.applied);
+ const planned = decisions.map(record => {
const draft = drafts.find(entry => entry.id === record?.revision?.proposal);
- if (!draft || !validSpecRevision(draft) || draft.decision || seen.has(draft.id)
+ if (!draft || !validSpecRevision(draft) || draft.decision || draft.applied || seen.has(draft.id)
|| record.kind !== 'spec' || record.version !== 1 || record.type !== 'row-decision'
|| JSON.stringify(record.revision) !== JSON.stringify(revisionReference(draft))
|| record.id !== draft.revises || record.file !== draft.file || record.source !== draft.source
@@ -113,6 +120,16 @@ export function decideSpecRevisions(board, { agentId, channel, decisions }) {
|| !['approve', 'decline'].includes(record.decision)
|| (record.decision === 'decline' && !String(record.reason ?? '').trim())) throw new Refused('SPEC_REVISION', 'the decision must match one undecided revision exactly; refresh Needs you');
seen.add(draft.id);
+ const target = JSON.stringify([draft.file, draft.revises]);
+ if (record.decision === 'approve') {
+ if (targets.has(target) || pending.some(current => current.kind === 'spec' && current.file === draft.file && current.id === draft.revises)) {
+ throw new Refused('SPEC_REVISION', `${draft.revises} already has a pending approval or is named by two proposals; apply the first decision before approving another`);
+ }
+ targets.add(target);
+ }
+ return { record, draft };
+ });
+ const results = planned.map(({ record, draft }) => {
const approved = record.decision === 'approve' ? recordRowDecisions(board, { agentId, channel, decisions: [record] })[0] : null;
logEvent(board, PERSON, 'spec_revision_decision', null, { proposal: draft.id, record, channel });
drafts = drafts.map(entry => entry.id === draft.id ? { ...entry, decision: record.decision,
@@ -124,11 +141,22 @@ export function decideSpecRevisions(board, { agentId, channel, decisions }) {
});
}
+/** Keep completed proposal history closed even after a newer target decision replaces its index entry. */
+export function markAppliedRevisions(board, records, at) {
+ const ids = new Set(records.map(record => record.revision?.proposal).filter(Boolean));
+ if (!ids.size) return;
+ const drafts = rowDrafts(board).map(draft => ids.has(draft.id) ? { ...draft, applied: true, appliedAt: at } : draft);
+ board.db.prepare('UPDATE board_meta SET meta_value=? WHERE meta_key=?').run(JSON.stringify(drafts), 'row_drafts');
+}
+
/** Expose both sides of each outstanding revision through the shared person projection. */
export function projectSpecRevisions(state, drafts, decisions) {
- const revisions = drafts.filter(draft => draft.revises && draft.decision !== 'decline'
- && !(draft.decision === 'approve' && decisions.some(record => record.event === draft.decisionEvent && record.applied)))
- .map(draft => ({ ...draft, old: parseSpec(`## Revision\n${draft.source}\n`).rows[0],
+ const approvals = new Map(decisions.filter(record => record.decision === 'approve' && record.revision)
+ .map(record => [record.revision.proposal, record]));
+ const revisions = drafts.filter(draft => draft.revises && !draft.applied && draft.decision !== 'decline'
+ && !approvals.get(draft.id)?.applied)
+ .map(draft => ({ ...draft, ...(approvals.has(draft.id) ? { decision: 'approve', decisionEvent: approvals.get(draft.id).event } : {}),
+ old: parseSpec(`## Revision\n${draft.source}\n`).rows[0],
proposed: parseSpec(`## Revision\n${draft.replacement}\n`).rows[0] }));
return { ...state, specRevisions: revisions };
}
diff --git a/test/spec-revise.test.js b/test/spec-revise.test.js
index 7433c3ff..b9feb243 100644
--- a/test/spec-revise.test.js
+++ b/test/spec-revise.test.js
@@ -79,6 +79,7 @@ test('a proposed change to an approved row waits in Needs you and applies only w
succeeds(box.run(box.repo, 'spec', 'approve', retired.id));
succeeds(box.run(box.repo, 'spec', 'apply'));
assert.equal(row(box, 'G1').status, 'retired', 'the person can approve an explicit status revision');
+ assert.ok(!personNeedsState(box.repo).needs.some(entry => [proposal.id, retired.id].includes(entry.id)), 'older applied revisions stay closed after the target gets a newer decision');
assert.notEqual(box.run(box.web, 'spec', 'draft', 'Change a retired row.', '--revises', 'G1', '--reason', 'Not approved.').code, 0);
});
@@ -93,7 +94,12 @@ test('[P6,B23,N39] revisions require a reason and stale proposals cannot change
succeeds(box.run(box.repo, 'spec', 'decline', statusOnly.id, '--reason', 'Keep it.'));
const first = JSON.parse(succeeds(box.run(box.web, 'spec', 'draft', 'An obsolete proposal.', '--revises', 'G1', '--reason', 'First idea.', '--json')).out);
const second = JSON.parse(succeeds(box.run(box.web, 'spec', 'draft', 'The replacement the person chooses.', '--revises', 'G1', '--reason', 'Better idea.', '--json')).out);
+ assert.notEqual(box.run(box.repo, 'spec', 'approve', first.id, second.id).code, 0, 'one batch cannot overwrite two proposals for the same target');
+ for (const id of [first.id, second.id]) {
+ assert.equal(personNeedsState(box.repo).needs.find(entry => entry.id === id).what, 'approve or decline revision', 'a refused batch records no partial approval');
+ }
succeeds(box.run(box.repo, 'spec', 'approve', second.id));
+ assert.notEqual(box.run(box.repo, 'spec', 'approve', first.id).code, 0, 'a later command cannot orphan an unapplied approval');
succeeds(box.run(box.repo, 'spec', 'apply'));
const approved = readFileSync(join(box.repo, 'SPEC.md'), 'utf8');
assert.notEqual(box.run(box.repo, 'spec', 'approve', first.id).code, 0, 'a stale source cannot be approved over a different current row');