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.

feat(relay): read addressed shouts on paired clients [H5,N38]

Corey Olsona5e7cf16bae4
diff --git a/relay/browser-client.js b/relay/browser-client.js
index cf663cc5..ba72d5ce 100644
--- a/relay/browser-client.js
+++ b/relay/browser-client.js
@@ -100,7 +100,7 @@ export async function createTransport({ onUpdate = () => {} } = {}) {
function denied() {
if (accessLost) return;
accessLost = true;
- for (const entry of paired.values()) { entry.stream?.close(); entry.state = null; }
+ for (const entry of paired.values()) { entry.stream?.close(); entry.state = null; entry.shouts = null; }
paired.clear();
document.querySelector('main')?.replaceChildren();
location.replace('/');
@@ -129,6 +129,7 @@ export async function createTransport({ onUpdate = () => {} } = {}) {
function waitForFirstSnapshot(entry) {
entry.stream?.close();
entry.state = null;
+ entry.shouts = null;
paired.delete(entry.id);
const board = available.find(value => value.id === entry.id);
waitingBoards.set(entry.id, board?.repository ?? entry.repository ?? 'This board');
@@ -167,6 +168,7 @@ export async function createTransport({ onUpdate = () => {} } = {}) {
entry.snapshotTag = response.headers.get('etag');
if (row.sequence >= entry.cursor) {
entry.state = state;
+ entry.shouts = Array.isArray(value.presentation?.shouts) ? value.presentation.shouts : null;
entry.cursor = row.sequence;
}
}
@@ -214,6 +216,7 @@ export async function createTransport({ onUpdate = () => {} } = {}) {
if (move.engine !== ENGINE_VERSION) throw new Refused('RELAY_ENGINE_VERSION', 'This board needs engine ' + move.engine + '; this browser reads engine ' + ENGINE_VERSION + '. Upgrade the browser client.');
if (move.presentation) {
entry.state = presentationState(move.presentation, entry.id);
+ entry.shouts = Array.isArray(move.presentation.shouts) ? move.presentation.shouts : null;
entry.cursor = row.event_id;
} else {
// A snapshot may be uploaded just after this ACK and compact the event immediately. Keep
@@ -333,7 +336,7 @@ export async function createTransport({ onUpdate = () => {} } = {}) {
if (paired.has(board.id) && pair?.board !== board.id && encodeBoardKey(paired.get(board.id).key) === encoded) continue;
if (!encoded) continue;
try {
- const entry = { id: board.id, repository: board.repository, key: decodeBoardKey(encoded), cursor: -1, state: null, outbox: null };
+ const entry = { id: board.id, repository: board.repository, key: decodeBoardKey(encoded), cursor: -1, state: null, shouts: null, outbox: null };
await snapshot(entry);
await catchUp(entry);
if (accessLost) throw new Error('Sign in again to read this board.');
@@ -411,6 +414,23 @@ export async function createTransport({ onUpdate = () => {} } = {}) {
return enqueueStream(entry, work);
}
+ /** Resolve one addressed shout from this paired device's complete sealed presentation. */
+ function addressedShout(entry, rawId) {
+ if (!/^[1-9]\d*$/.test(rawId) || !Number.isSafeInteger(Number(rawId))) {
+ throw new Refused('BAD_REQUEST', 'shout needs a positive integer id; use the id from the event or recent state');
+ }
+ if (!Array.isArray(entry.shouts)) {
+ throw new Refused('SHOUT_NOT_AVAILABLE', 'this snapshot does not include addressed shout history; refresh it with a current CLI');
+ }
+ const id = Number(rawId);
+ const found = entry.shouts.find(shout => shout.shout_id === id);
+ if (!found) throw new Refused('NO_SHOUT', `no shout #${id}`);
+ const answer = found.shout_decision ? entry.shouts.find(shout => shout.shout_answers === id) ?? null : null;
+ return { version: 1, shout: { ...found,
+ decision_state: found.shout_decision ? (answer ? 'answered' : 'open') : null,
+ decision_answer: answer } };
+ }
+
/** Seal an unchanged intent at its current public sequence after another client wins the order. */
async function requestEnvelope(entry, value, sequence) {
const bytes = await seal(entry.key, new TextEncoder().encode(JSON.stringify(value)), { boardId: entry.id, kind: 'request', sequence });
@@ -471,9 +491,12 @@ export async function createTransport({ onUpdate = () => {} } = {}) {
if (url.origin !== location.origin) throw new Error('Use this relay origin to read a board.');
if (url.pathname === '/api/v1/boards') { if (body !== undefined && body !== null) throw new Refused('BAD_REQUEST', 'Send person intent to a paired board.'); return listing(); }
const match = /^\/api\/v1\/boards\/([0-9a-f]{32})\/(state|events|moves|requests)$/.exec(url.pathname);
- const entry = match && paired.get(match[1]);
+ const shoutMatch = /^\/api\/v1\/boards\/([0-9a-f]{32})\/shouts\/([^/]+)$/.exec(url.pathname);
+ const route = match ?? shoutMatch;
+ const entry = route && paired.get(route[1]);
if (!entry) throw new Error('Pair this browser to read that board.');
if (body !== undefined && body !== null) {
+ if (shoutMatch) throw new Refused('BAD_REQUEST', 'read an addressed shout with GET; use a board move to create or answer shouts');
if (!['moves', 'requests'].includes(match[2])) throw new Refused('BAD_REQUEST', 'Send person intent to this board’s moves endpoint.');
const value = preparePersonRequest(body);
const send = enqueue(entry, async () => {
@@ -485,6 +508,14 @@ export async function createTransport({ onUpdate = () => {} } = {}) {
});
return send;
}
+ if (shoutMatch) {
+ return enqueue(entry, async () => {
+ await flushOutbox(entry);
+ await snapshot(entry);
+ await catchUp(entry);
+ return addressedShout(entry, shoutMatch[2]);
+ });
+ }
if (!['state', 'events'].includes(match[2])) throw new Refused('BAD_REQUEST', 'Read this board’s state or events endpoint.');
return enqueue(entry, async () => {
await flushOutbox(entry);
diff --git a/test/relay-browser.test.js b/test/relay-browser.test.js
index 72cbc172..dc005690 100644
--- a/test/relay-browser.test.js
+++ b/test/relay-browser.test.js
@@ -10,7 +10,7 @@ import { snapshotState, presentationState } from '../relay/browser-model.js';
import { decodeBoardKey, seal, unseal } from '../src/seal.js';
import { relaySnapshot } from '../src/relay-presentation.js';
import { ENGINE_VERSION } from '../src/machine.js';
-import { addItem, closeBoard, openBoard } from '../src/board.js';
+import { addItem, answerDecision, closeBoard, openBoard, shout } from '../src/board.js';
import { relayClientFixture } from './relay-client-fixture.js';
import { findChromeExecutable, startChrome } from './relay-browser-fixture.js';
@@ -202,6 +202,66 @@ test('real Chrome pairs, retains its device key, declares its engine on every re
await chrome.waitFor("document.body.textContent.includes('Sign in with GitHub') && !document.body.textContent.includes(" + JSON.stringify(liveTitle) + ')');
});
+test('the relay reads an addressed shout [H5,N38]', {
+ skip: !findChromeExecutable() && 'Chrome is not installed',
+}, async t => {
+ const box = await relayClientFixture(t);
+ const board = openBoard(join(box.root, '.git', 'pullboard', 'board.sqlite'));
+ let shoutId;
+ try {
+ shoutId = shout(board, { from: 'coordinator', to: 'person', text: 'OLD_ADDRESSED_DECISION_434', lanes: [], decision: true });
+ answerDecision(board, shoutId, { agentId: 'coordinator', text: 'Answered on the device', lanes: [], asPerson: true, channel: 'view' });
+ for (let index = 0; index < 40; index += 1) {
+ shout(board, { from: 'coordinator', to: 'person', text: `RECENT_ADDRESSED_${index}_434`, lanes: [] });
+ }
+ } finally { closeBoard(board); }
+ await box.link();
+ const link = JSON.parse(readFileSync(box.linkFile, 'utf8'));
+ const key = readFileSync(box.keyFile, 'utf8').trim();
+ const chrome = await startChrome();
+ t.after(() => chrome.close());
+ await signIn(chrome, box);
+ await chrome.navigate(box.origin);
+ await chrome.waitFor("document.querySelector('#relay-notice')?.textContent.includes('pair this browser')");
+ const unpaired = await chrome.evaluate(`api('/api/v1/boards/${link.board}/shouts/${shoutId}').then(() => 'unexpected', error => error.message)`);
+ assert.match(unpaired, /Pair this browser to read that board/u);
+
+ await chrome.navigate(box.origin + '/#board=' + link.board + '&key=' + key);
+ await chrome.waitFor("document.querySelector('#chain')?.textContent.includes('private cleanup fixture item')");
+ const recentState = await chrome.evaluate(`api('/api/v1/boards/${link.board}/state')`);
+ assert.equal(recentState.state.shouts.length, 40);
+ assert.equal(recentState.state.shouts.some(row => row.shout_id === shoutId), false,
+ 'the old decision is outside the ordinary recent-40 state window');
+ const addressed = await chrome.evaluate(`api('/api/v1/boards/${link.board}/shouts/${shoutId}').then(value => ({ value, error: null }), error => ({ value: null, error: { code: error.code ?? null, message: error.message } }))`);
+ assert.equal(addressed.error, null, 'the paired browser resolves the old shout from its local sealed history');
+ assert.equal(addressed.value.version, 1);
+ assert.equal(addressed.value.shout.shout_id, shoutId);
+ assert.equal(addressed.value.shout.shout_text, 'OLD_ADDRESSED_DECISION_434');
+ assert.equal(addressed.value.shout.decision_state, 'answered');
+ assert.equal(addressed.value.shout.decision_answer.shout_text, 'Answered on the device');
+ const invalid = await chrome.evaluate(`api('/api/v1/boards/${link.board}/shouts/0').then(() => 'unexpected', error => error.code)`);
+ assert.equal(invalid, 'BAD_REQUEST', 'addressed reads accept only positive integer ids');
+ const missing = await chrome.evaluate(`api('/api/v1/boards/${link.board}/shouts/999999').then(() => 'unexpected', error => error.code)`);
+ assert.equal(missing, 'NO_SHOUT', 'an absent addressed id has the same named refusal as the local API');
+ const write = await chrome.evaluate(`transport.request('/api/v1/boards/${link.board}/shouts/${shoutId}', { text: 'must not leave the device' }).then(() => 'unexpected', error => error.code)`);
+ assert.equal(write, 'BAD_REQUEST', 'addressed shout routes remain read-only');
+ const unsupported = await chrome.evaluate(`api('/api/v1/boards/${link.board}/code').then(() => 'unexpected', error => error.message)`);
+ assert.match(unsupported, /Pair this browser to read that board/u, 'unrelated relay routes remain refused');
+ assert.equal(box.calls.some(call => call.path.includes('/shouts/')), false,
+ 'the browser resolves the complete shout locally from its decrypted sealed presentation');
+ assert.equal(box.transit.some(entry => entry.request.includes(Buffer.from('OLD_ADDRESSED_DECISION_434'))), false,
+ 'the shout text is absent from every request sent to the relay');
+ assert.equal(box.keyInRequest(), false, 'the device key and shout plaintext never leave the browser');
+
+ await chrome.navigate(box.origin + '/#shout-' + shoutId);
+ const card = `#shout-${shoutId}.found`;
+ await chrome.waitFor(`typeof api === 'function' && transport !== null && document.querySelector(${JSON.stringify(card + ' h2')})?.textContent.includes(${JSON.stringify('Shout #' + shoutId)})`);
+ assert.equal(await chrome.evaluate(`document.querySelector(${JSON.stringify(card + ' .text')})?.textContent`),
+ 'OLD_ADDRESSED_DECISION_434', 'the addressed relay fallback opens the actual old shout card');
+ assert.equal(await chrome.evaluate("document.querySelector('#shout-address-status') === null"), true,
+ 'the old-shout fallback does not leave an unavailable-history message');
+});
+
test('a board without a snapshot waits without reloading the page [H5,H16]', {
skip: !findChromeExecutable() && 'Chrome is not installed',
}, async t => {