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.

test(links): require sealed durable relay delivery [E32,H17]

Corey Olsonb4203725fc9e
diff --git a/test/board-link.test.js b/test/board-link.test.js
index b5a446da..c818eba9 100644
--- a/test/board-link.test.js
+++ b/test/board-link.test.js
@@ -80,3 +80,106 @@ test('a signed two-sided link carries only its disclosure mode and refuses a cyc
await assert.rejects(api.applyBoardLink(parent.board, record, authority), error => error.code === 'BOARD_LINK_CONFLICT',
'replaying old consent cannot undo a signed revocation');
});
+
+/** Start the existing authenticated relay with real stand-in OAuth and private native snapshots. */
+async function linkRelay(t, boards, authority) {
+ const { githubFixture } = await import('./relay-fixture.js');
+ const { createGitHubClient } = await import('../relay/github.js');
+ const { createRelayAuth } = await import('../relay/auth.js');
+ const { serveRelay } = await import('../relay/service.js');
+ const { request } = await import('../src/relay.js');
+ const { generateBoardKey, seal } = await import('../src/seal.js');
+ const { exportBoard } = await import('../src/exchange.js');
+ const provider = await githubFixture(t);
+ const directory = mkdtempSync(join(tmpdir(), 'pullboard-link-relay-'));
+ let time = Date.now();
+ const auth = createRelayAuth({ database: join(directory, 'auth.sqlite'),
+ github: createGitHubClient(provider.config), now: () => time });
+ const grant = await auth.beginDevice();
+ provider.state.deviceAuthorized = true;
+ time += grant.interval * 1000;
+ const person = await auth.pollDevice(grant.ticket);
+ for (const box of boards) await auth.linkBoard(person.token, box.reference.boardId, 'fixture/repository');
+ let service = await serveRelay({ directory: join(directory, 'relay'), auth, maintenanceMs: 0,
+ now: () => time, boardLinkAuthority: authority });
+ let origin = 'http://127.0.0.1:' + service.port;
+ const states = new Map();
+ for (const box of boards) {
+ const board = box.reference.boardId;
+ const key = await generateBoardKey();
+ const snapshot = await seal(key, Buffer.from(JSON.stringify(exportBoard(box.board))), { boardId: board, kind: 'snapshot', sequence: 0 });
+ await request({ url: origin, token: person.token, board }, '/api/v1/boards/' + board + '/state',
+ { method: 'PUT', body: { sequence: 0, sealed: Buffer.from(snapshot).toString('base64url') } }, { ignoreRelayEnvironmentToken: true });
+ const issued = await auth.issueToken(person.token, { board, agent: 'link-agent' });
+ states.set(board, { url: origin, token: issued.token, board, agent: 'link-agent' });
+ }
+ t.after(async () => { await service.close(); auth.close(); rmSync(directory, { recursive: true, force: true }); });
+ return { states, request,
+ async restart() {
+ await service.close();
+ service = await serveRelay({ directory: join(directory, 'relay'), auth, maintenanceMs: 0,
+ now: () => time, boardLinkAuthority: authority });
+ origin = 'http://127.0.0.1:' + service.port;
+ for (const state of states.values()) state.url = origin;
+ },
+ };
+}
+
+test('sealed board-link delivery authenticates both boards and retains exact retries after restart [E32,H16,H17]', async t => {
+ const loaded = await import('../src/board-link-relay.js').then(api => ({ api }), error => ({ error }));
+ assert.equal(loaded.error, undefined, 'the common authenticated delivery dispatcher must exist');
+ const transportApi = loaded.api;
+ const api = await import('../src/board-link.js');
+ const { createSshBoardLinkAuthority } = await import('../src/board-link-ssh.js');
+ const { createBoardLinkCapability } = await import('../src/board-link-envelope.js');
+ const { generateBoardKey, seal, unseal } = await import('../src/seal.js');
+ const parent = signedBoard(t, 'parent');
+ const child = signedBoard(t, 'child');
+ const authority = createSshBoardLinkAuthority([parent, child]);
+ const record = await consent(api, authority, parent, child, 'sealed-link');
+ await api.applyBoardLink(parent.board, record, authority);
+ await api.applyBoardLink(child.board, record, authority);
+ const box = await linkRelay(t, [parent, child], authority);
+ const sender = transportApi.createBoardLinkRelayTransport(box.states.get(child.reference.boardId));
+ const receiver = transportApi.createBoardLinkRelayTransport(box.states.get(parent.reference.boardId));
+ await sender.publish(record);
+ const linkKey = await generateBoardKey();
+ const capability = createBoardLinkCapability(record, linkKey);
+ const parentKey = await generateBoardKey();
+ const privateParent = await seal(parentKey, Buffer.from('unrelated private parent content'),
+ { boardId: parent.reference.boardId, kind: 'snapshot', sequence: 0 });
+ assert.equal(Buffer.from(await unseal(parentKey, privateParent,
+ { boardId: parent.reference.boardId, kind: 'snapshot', sequence: 0 })).toString(), 'unrelated private parent content');
+ await assert.rejects(unseal(linkKey, privateParent,
+ { boardId: parent.reference.boardId, kind: 'snapshot', sequence: 0 }), error => error.code === 'SEAL_AUTH_FAILED',
+ 'a child’s per-link key cannot open other parent content');
+ const context = { linkId: record.linkId, generation: 1, sourceBoardId: child.reference.boardId,
+ destinationBoardId: parent.reference.boardId, position: 10, correlationId: 'state-10',
+ rows: [{ board: child.reference.boardId, id: 1, state: 'claimed', title: 'must stay private' }] };
+ const ports = { authority, capability, transport: sender };
+ const receipt = await transportApi.deliverBoardState(child.board, context, ports);
+ assert.deepEqual(await transportApi.deliverBoardState(child.board, context, ports), receipt, 'a retry returns the same durable outcome');
+ await box.restart();
+ assert.deepEqual(await transportApi.deliverBoardState(child.board, context, ports), receipt, 'a restart does not duplicate delivery');
+ const messages = await receiver.read();
+ assert.equal(messages.deliveries.length, 1);
+ const delivered = messages.deliveries[0];
+ assert.deepEqual(await sender.send(delivered.envelope), receipt, 'the restarted HTTP dispatcher returns its durable retry receipt');
+ assert.equal((await receiver.read()).deliveries.length, 1, 'an actual HTTP retry does not append another envelope');
+ const plain = await capability.open(delivered.envelope);
+ assert.deepEqual(plain.rows, [{ board: child.reference.boardId, id: 1, state: 'claimed' }]);
+ assert.equal(JSON.stringify(plain).includes('must stay private'), false);
+ const received = await transportApi.receiveBoardState(parent.board, delivered, { authority, capability });
+ const eventCount = parent.board.db.prepare('SELECT COUNT(*) AS n FROM event').get().n;
+ assert.deepEqual(await transportApi.receiveBoardState(parent.board, delivered, { authority, capability }), received);
+ assert.equal(parent.board.db.prepare('SELECT COUNT(*) AS n FROM event').get().n, eventCount, 'consumer retry makes no second event');
+ await assert.rejects(transportApi.deliverBoardState(child.board, { ...context,
+ rows: [{ board: child.reference.boardId, id: 1, state: 'submitted' }] }, ports), error => error.code === 'BOARD_LINK_CONFLICT');
+ await assert.rejects(box.request(box.states.get(child.reference.boardId),
+ '/api/v1/boards/' + parent.reference.boardId + '/link-deliveries', {}, { ignoreRelayEnvironmentToken: true }),
+ error => error.code === 'TOKEN_BOARD', 'a child token cannot read a parent inbox');
+ const revoked = await api.revokeBoardLink(record, parent.reference.boardId, authority, { by: parent.by, key: parent.key });
+ await receiver.publish(revoked);
+ await assert.rejects(transportApi.deliverBoardState(child.board, { ...context, correlationId: 'state-11', position: 11 }, ports),
+ error => error.code === 'BOARD_LINK_INACTIVE', 'authoritative revocation blocks a source whose local catalog is stale');
+});