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.
JavaScript agentic-codingai-agentsai-governanceautonomous-agentsclaude-codeclicode-reviewcodexcoding-agentsdeveloper-toolsgit-hooksllmlocal-firstmulti-agentsoftware-factoryspec-driven-developmentverificationvibe-coding
fix(relay): clear a recovered checkpoint timeout [H16]
Corey Olson7847a7661c21
diff --git a/relay/browser-client.js b/relay/browser-client.jsindex 468d4758..857114bd 100644--- a/relay/browser-client.js+++ b/relay/browser-client.js@@ -90,6 +90,7 @@ export async function createTransport({ onUpdate = () => {} } = {}) {let waitingBoards = new Map();let warnings = [];let failure = '';+ let failureBoard = null;let accessLost = false;let pair = rememberPairing() ?? stored(sessionStorage, PENDING, null);if (pair && (!BOARD.test(pair.board ?? '') || typeof pair.key !== 'string')) pair = null;@@ -264,6 +265,7 @@ export async function createTransport({ onUpdate = () => {} } = {}) {}if (['AUTH_REQUIRED', 'NO_REPO_ACCESS', 'TOKEN_BOARD'].includes(error?.code)) await documentAt('/api/v1/devices/session', denied);failure = error ? '[' + error.code + '] ' + error.message : 'The relay stream ended. Refresh this board.';+ failureBoard = entry.id;notice();}return;@@ -271,6 +273,7 @@ export async function createTransport({ onUpdate = () => {} } = {}) {if (document.version !== 1) throw new Error('Upgrade this browser client to read the relay API.');await receive(entry, document.event);failure = '';+ failureBoard = null;notice();onUpdate();}@@ -308,6 +311,7 @@ export async function createTransport({ onUpdate = () => {} } = {}) {if (controller.signal.aborted || accessLost) return;if (['NO_BOARD', 'NO_SNAPSHOT'].includes(error?.code)) { waitForFirstSnapshot(entry); return; }failure = error.message;+ failureBoard = entry.id;notice();if (fatal) stream.close();},@@ -337,7 +341,7 @@ export async function createTransport({ onUpdate = () => {} } = {}) {paired.set(board.id, entry);waitingBoards.delete(board.id);keys[board.id] = encoded;- try { localStorage.setItem(KEYS, JSON.stringify({ ...stored(localStorage, KEYS, {}), [board.id]: encoded })); } catch { failure = 'This browser cannot save pairing. Use the pairing link again on your next visit.'; }+ try { localStorage.setItem(KEYS, JSON.stringify({ ...stored(localStorage, KEYS, {}), [board.id]: encoded })); } catch { failure = 'This browser cannot save pairing. Use the pairing link again on your next visit.'; failureBoard = null; }if (pair?.board === board.id) {pair = null;try { sessionStorage.removeItem(PENDING); } catch { /* Device storage can be unavailable. */ }@@ -351,6 +355,7 @@ export async function createTransport({ onUpdate = () => {} } = {}) {failure = ['ENGINE_VERSION', 'RELAY_ENGINE_VERSION'].includes(error.code) || /^\[(?:ENGINE_VERSION|RELAY_ENGINE_VERSION)\]/u.test(error.message)? error.message: 'Could not open this board with its saved key. Pair this browser again from a linked machine.';+ failureBoard = board.id;}}notice();@@ -452,7 +457,7 @@ export async function createTransport({ onUpdate = () => {} } = {}) {/** Pair a link opened in this already-loaded page as well as one opened on a fresh visit. */async function pairingChanged() {try { pair = rememberPairing() ?? pair; await enrollPhone((path, options) => documentAt(path, denied, options)); await listing(); onUpdate(); }- catch { failure = 'The pairing link is invalid. Get a new link from a linked machine.'; notice(); }+ catch { failure = 'The pairing link is invalid. Get a new link from a linked machine.'; failureBoard = null; notice(); }}addEventListener('hashchange', pairingChanged);addEventListener('pagehide', () => { for (const entry of paired.values()) entry.stream?.close(); });@@ -484,6 +489,8 @@ export async function createTransport({ onUpdate = () => {} } = {}) {await flushOutbox(entry);await snapshot(entry);await catchUp(entry);+ // A read has authenticated the checkpoint and complete prefix before clearing its failure.+ if (failureBoard === entry.id) { failure = ''; failureBoard = null; }if (entry.stream?.closed) subscribe(entry);notice();if (match[2] === 'state') return { version: 1, state: entry.state };diff --git a/test/relay-snapshot-recovery.test.js b/test/relay-snapshot-recovery.test.jsnew file mode 100644index 00000000..cea10596--- /dev/null+++ b/test/relay-snapshot-recovery.test.js@@ -0,0 +1,116 @@+/** Recover a native checkpoint timeout through the actual relay page [H16]. */+import assert from 'node:assert/strict';+import { readFileSync } from 'node:fs';+import { join } from 'node:path';+import { test } from 'node:test';+import { openBoard, closeBoard } from '../src/board.js';+import { prepareEngineMove, applyRelayMove } from '../src/engine.js';+import { ENGINE_VERSION } from '../src/machine.js';+import { relaySnapshot } from '../src/relay-presentation.js';+import { decodeBoardKey, generateBoardKey, seal } from '../src/seal.js';+import { relayClientFixture } from './relay-client-fixture.js';+import { findChromeExecutable, startChrome } from './relay-browser-fixture.js';++/** Seal a real native document for an exact authenticated relay position. */+async function encoded(key, value, board, kind, sequence) {+ return Buffer.from(await seal(key, new TextEncoder().encode(JSON.stringify(value)),+ { boardId: board, kind, sequence })).toString('base64url');+}++/** Upload to the real private relay, checking its actual acknowledgement. */+async function upload(box, link, endpoint, sequence, sealed) {+ const response = await fetch(box.origin + '/api/v1/boards/' + link.board + '/' + endpoint, {+ method: endpoint === 'state' ? 'PUT' : 'POST',+ headers: { authorization: 'Bearer ' + link.token, 'x-pullboard-engine': String(ENGINE_VERSION), 'content-type': 'application/json' },+ body: JSON.stringify({ sequence, sealed }),+ });+ assert.equal(response.status, 200, 'the real authorized relay acknowledges the sealed position');+ return response.json();+}++/** Read the real page transport without holding a long CDP evaluation open. */+async function readState(chrome, board) {+ const task = await chrome.startTask('transport.request(' + JSON.stringify('/api/v1/boards/' + board + '/state') + ')',+ 'read recovered native state');+ return chrome.pollTask(task, 45_000, 'read recovered native state');+}++test('a delayed native checkpoint recovers the real page after its visible timeout [H16]', {+ skip: !findChromeExecutable() && 'Chrome is not installed',+}, async t => {+ const box = await relayClientFixture(t);+ const initial = 'RECOVERY_INITIAL_336';+ const recovered = 'RECOVERY_CHECKPOINT_336';+ const later = 'RECOVERY_LATER_NATIVE_336';+ assert.equal((await box.cli('add', box.lane, initial)).code, 0);+ await box.link();+ const link = JSON.parse(readFileSync(box.linkFile, 'utf8'));+ const encodedKey = readFileSync(box.keyFile, 'utf8').trim();+ const key = decodeBoardKey(encodedKey);+ const chrome = await startChrome();+ t.after(() => chrome.close());+ const cookie = await chrome.send('Network.setCookie', { name: 'pb_session', value: link.token,+ url: box.origin, httpOnly: true, sameSite: 'Lax' });+ assert.equal(cookie.success, true);+ await chrome.navigate(box.origin + '/#board=' + link.board + '&key=' + encodedKey);+ await chrome.waitFor('typeof transport !== "undefined" && !!transport && document.querySelector("#chain")?.textContent.includes(' + JSON.stringify(initial) + ')');+ /** Count real document loads to detect a hidden recovery reload. */+ const pageLoads = () => box.calls.filter(call => call.method === 'GET' && call.path === '/').length;+ /** Count real authorized SSE connections, independently of the page polling timer. */+ const streams = () => box.calls.filter(call => call.method === 'GET' && call.accept.includes('text/event-stream')).length;+ const originalLoads = pageLoads();++ // A real engine operation intentionally has no attached presentation: the native checkpoint is late.+ const board = openBoard(join(box.root, '.git', 'pullboard', 'board.sqlite'));+ let move;+ try { move = prepareEngineMove(board, 'addItem', [{ by: 'coordinator', lane: box.lane, title: recovered }]); }+ finally { closeBoard(board); }+ const sent = await upload(box, link, 'moves', 1, await encoded(key, move, link.board, 'move', 1));+ const started = Date.now();+ const timeoutText = 'The relay snapshot has not caught up to this move. Retry this board.';+ await chrome.waitFor('document.querySelector("#relay-notice")?.textContent.includes(' + JSON.stringify(timeoutText) + ')',+ 45_000, 'actual fifteen-second native checkpoint timeout');+ assert.ok(Date.now() - started >= 14_000, 'the production wait elapsed; no clock or fetch was replaced');+ assert.equal(await chrome.evaluate('document.querySelector("#chain").textContent.includes(' + JSON.stringify(recovered) + ')'), false);++ const native = openBoard(join(box.root, '.git', 'pullboard', 'board.sqlite'));+ try {+ const outcome = applyRelayMove(native, move, { sequence: 1, at: sent.event.event_at, sender: sent.event.sender, kind: 'move' });+ assert.equal(outcome.error, undefined, 'the CLI engine applies exactly the acknowledged native operation');+ } finally { closeBoard(native); }+ const checkpoint = relaySnapshot(box.root);+ assert.ok(checkpoint.presentation.state.items.some(item => item.title === recovered));++ // Neither an incorrectly keyed checkpoint nor truncated ciphertext is successful recovery.+ const wrongKey = await generateBoardKey();+ for (const [label, bytes, code] of [+ ['wrong key', await encoded(wrongKey, checkpoint, link.board, 'snapshot', 1), 'SEAL_AUTH_FAILED'],+ ['malformed ciphertext', Buffer.from([1, 2, 3]).toString('base64url'), 'SEAL_FORMAT'],+ ]) {+ await upload(box, link, 'state', 1, bytes);+ await assert.rejects(readState(chrome, link.board), error => error.code === code, label + ' remains refused');+ assert.equal(await chrome.evaluate('document.querySelector("#chain").textContent.includes(' + JSON.stringify(recovered) + ')'), false,+ label + ' cannot install the recovered projection');+ assert.equal(await chrome.evaluate('document.querySelector("#relay-notice").textContent.includes(' + JSON.stringify(timeoutText) + ')'), true,+ label + ' cannot clear the stale failure');+ }++ const streamsBeforeRecovery = streams();+ await upload(box, link, 'state', 1, await encoded(key, checkpoint, link.board, 'snapshot', 1));+ const result = await readState(chrome, link.board);+ assert.ok(result.state.items.some(item => item.title === recovered), 'the real page transport returns the authenticated native projection');+ // Inspect immediately, before another event can incidentally clear the failure.+ assert.equal(await chrome.evaluate('document.querySelector("#relay-notice").textContent.includes(' + JSON.stringify(timeoutText) + ')'), false,+ 'successful authenticated state recovery clears the timeout notice');+ await chrome.evaluate('refresh()');+ await chrome.waitFor('document.querySelector("#chain")?.textContent.includes(' + JSON.stringify(recovered) + ')');+ assert.ok(streams() > streamsBeforeRecovery, 'the fatal old stream is replaced by a real authorized SSE connection');+ assert.equal(pageLoads(), originalLoads, 'checkpoint recovery does not reload the page');++ // The next ordinary CLI command catches up from the same checkpoint and authors a real native event.+ assert.equal((await box.cli('add', box.lane, later)).code, 0);+ await chrome.waitFor('document.querySelector("#chain")?.textContent.includes(' + JSON.stringify(later) + ')');+ assert.equal(await chrome.evaluate('document.querySelector("#relay-notice").textContent.includes(' + JSON.stringify(timeoutText) + ')'), false);+ assert.equal(pageLoads(), originalLoads, 'later native updates keep the recovered document');+ assert.equal(box.keyInRequest(), false, 'all native and browser relay traffic keeps the board key on the device');+});