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(api): discard captured events on rollback [A2,R2]

Corey Olsonf370eaf7fbe1
diff --git a/src/board.js b/src/board.js
index bb5bbc1d..a7593c7f 100644
--- a/src/board.js
+++ b/src/board.js
@@ -271,12 +271,16 @@ const now = (board) => board.clock.now().toISOString();
*/
export function atomic(board, work) {
board.db.exec('BEGIN IMMEDIATE');
+ const lastEvent = board.lastEvent;
+ const emitted = board.emittedEvents?.length ?? 0;
try {
const result = work();
board.db.exec('COMMIT');
return result;
} catch (error) {
board.db.exec('ROLLBACK');
+ board.lastEvent = lastEvent;
+ board.emittedEvents?.splice(emitted);
throw error;
}
}
diff --git a/test/api-events.test.js b/test/api-events.test.js
index 0c1123f8..bf6d228b 100644
--- a/test/api-events.test.js
+++ b/test/api-events.test.js
@@ -112,3 +112,27 @@ test('[A2, R2] a refused command emits no move event', async (t) => {
const after = store.openBoard(join(repo, '.git', 'pullboard', 'board.sqlite'));
try { assert.equal(store.events(after).length, beforeCount); } finally { store.closeBoard(after); }
});
+
+
+test('[A2, R2] rolling back a transaction also discards its captured events', async (t) => {
+ const { repo } = await project(t);
+ const board = store.openBoard(join(repo, '.git', 'pullboard', 'board.sqlite'));
+ try {
+ const item = store.addItem(board, { by: 'coordinator', lane: 'app', title: 'Committed move' });
+ const rows = store.events(board);
+ const emitted = [...board.emittedEvents];
+ const last = board.lastEvent;
+ assert.throws(() => store.atomic(board, () => {
+ store.recordAttempt(board, item, { agentId: 'coordinator', n: 1, seconds: 1, result: 'rolled back' });
+ throw new Error('abort the real SQLite transaction');
+ }), /abort the real SQLite transaction/);
+ assert.deepEqual(store.events(board), rows, 'the database contains only committed events');
+ assert.deepEqual(board.emittedEvents, emitted, 'delivery cannot include rolled-back rows');
+ assert.deepEqual(board.lastEvent, last, 'the retained row still belongs to a committed move');
+ store.recordAttempt(board, item, { agentId: 'coordinator', n: 2, seconds: 1, result: 'committed' });
+ assert.deepEqual(board.emittedEvents, [...emitted, board.lastEvent]);
+ assert.equal(JSON.parse(board.lastEvent.event_detail).result, 'committed');
+ } finally {
+ store.closeBoard(board);
+ }
+});