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): keep an early pairing until the transport takes it [H5,H15]
Corey Olsonbe770f682393
diff --git a/relay/browser-client.js b/relay/browser-client.jsindex ba72d5ce..aab8431e 100644--- a/relay/browser-client.js+++ b/relay/browser-client.js@@ -19,6 +19,9 @@ function stored(storage, name, fallback) {try { return JSON.parse(storage.getItem(name)) ?? fallback; } catch { return fallback; }}+/** The validated pairing this page read before its transport took it; module memory only, never a global or storage. */+let earlyPairing = null;+/** Keep an unauthenticated pairing fragment on the device through the GitHub redirect. */export function rememberPairing() {rememberDevicePairing();@@ -31,6 +34,14 @@ export function rememberPairing() {const pair = { board, key };try { sessionStorage.setItem(PENDING, JSON.stringify(pair)); } catch { /* Current visit can still pair in memory. */ }history.replaceState(null, '', location.pathname + location.search);+ earlyPairing = pair;+ return pair;+}++/** Hand the transport the pairing this page already read, once, so an early read survives refused storage. */+function takePairing() {+ const pair = earlyPairing;+ earlyPairing = null;return pair;}@@ -92,7 +103,8 @@ export async function createTransport({ onUpdate = () => {} } = {}) {let failure = '';let failureBoard = null;let accessLost = false;- let pair = rememberPairing() ?? stored(sessionStorage, PENDING, null);+ rememberPairing();+ let pair = takePairing() ?? stored(sessionStorage, PENDING, null);if (pair && (!BOARD.test(pair.board ?? '') || typeof pair.key !== 'string')) pair = null;try { if (pair) decodeBoardKey(pair.key); } catch { pair = null; }@@ -476,7 +488,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(); }+ try { rememberPairing(); pair = takePairing() ?? 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.'; failureBoard = null; notice(); }}addEventListener('hashchange', pairingChanged);diff --git a/test/relay-deploy.test.js b/test/relay-deploy.test.jsindex 4a6401a1..0debd675 100644--- a/test/relay-deploy.test.js+++ b/test/relay-deploy.test.js@@ -3,13 +3,16 @@ import assert from 'node:assert/strict';import { startFixtureChild as spawn, reportFixtureChildFailure, safeFixtureDiagnostic } from './fixture-child.js';import { performance } from 'node:perf_hooks';import { createServer } from 'node:http';-import { existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, statSync, copyFileSync } from 'node:fs';+import { existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, statSync, copyFileSync, writeFileSync } from 'node:fs';import { join, resolve } from 'node:path';import { tmpdir } from 'node:os';+import { pathToFileURL } from 'node:url';import { test } from 'node:test';import { ENGINE_VERSION } from '../src/machine.js';import { relayClientFixture } from './relay-client-fixture.js';import { githubFixture } from './relay-fixture.js';+import { createRelayBrowserHandler } from '../relay/browser-page.js';+import { encodeBoardKey, generateBoardKey, seal } from '../src/seal.js';const SMOKE = resolve(import.meta.dirname, '../relay/smoke.mjs');const SERVER = resolve(import.meta.dirname, '../relay/server.mjs');@@ -430,3 +433,128 @@ test('a timer that fires a millisecond early still meets the damaged-copy budgetassert.equal(damagedServerWaitMeetsBudget(budgetMs - 1, budgetMs), true, 'one millisecond early fits timer granularity');assert.equal(damagedServerWaitMeetsBudget(budgetMs - 2, budgetMs), false, 'two milliseconds early remains below budget');});++/** Read one relay browser asset through the production handler, without a network listener. */+async function servedAsset(handler, path) {+ let status = 0;+ let body = '';+ const handled = await handler({ method: 'GET', url: path }, { writeHead(code) { status = code; }, end(content) { body = String(content); } });+ assert.equal(handled, true, `the relay browser handler owns ${path}`);+ assert.equal(status, 200, `the relay browser handler serves ${path}`);+ return body;+}++/** Write the served relay client module graph to a private folder, so Node imports exactly what a browser loads. */+async function servedRelayClient(t) {+ const folder = mkdtempSync(join(tmpdir(), 'pullboard-relay-client-'));+ t.after(() => rmSync(folder, { recursive: true, force: true }));+ mkdirSync(join(folder, 'relay'));+ const handler = createRelayBrowserHandler({ authenticate: async () => { throw new Error('relay assets need no session'); } });+ const pending = ['client.js'];+ const written = new Set();+ while (pending.length > 0) {+ const name = pending.pop();+ if (written.has(name)) continue;+ written.add(name);+ const source = await servedAsset(handler, '/relay/' + name);+ writeFileSync(join(folder, 'relay', name), source);+ for (const match of source.matchAll(/from\s+['"]\.\/([\w.-]+\.js)['"]/gu)) pending.push(match[1]);+ }+ return import(pathToFileURL(join(folder, 'relay', 'client.js')).href);+}++/** Install a browser page whose storage refuses every write and whose relay serves one sealed board. */+function refusingStoragePage(t, { hash, board, sealed }) {+ const refused = [];+ const unexpected = [];+ const replaced = [];+ let snapshotReads = 0;+ /** Model storage that refuses writes, as a private window or a full quota does. */+ const storage = () => ({+ length: 0, key: () => null, getItem: () => null,+ setItem(name) { refused.push(name); throw new DOMException('Storage refused this write.', 'QuotaExceededError'); },+ removeItem(name) { refused.push(name); throw new DOMException('Storage refused this write.', 'QuotaExceededError'); },+ });+ const events = new EventTarget();+ const location = { origin: 'https://relay.example', pathname: '/', search: '', hash, replace(path) { replaced.push(path); } };+ /** Answer one JSON relay document. */+ const reply = (document, status = 200, headers = {}) => new Response(JSON.stringify(document), { status, headers: { 'content-type': 'application/json', ...headers } });+ /** Model an IndexedDB that holds no phone key, so only the pairing link can open the board. */+ const indexedDB = {+ open() {+ const db = {+ close() {},+ transaction() {+ const transaction = {+ objectStore: () => ({ get() {+ const request = {};+ queueMicrotask(() => { request.onsuccess?.(); transaction.oncomplete?.(); });+ return request;+ } }),+ };+ return transaction;+ },+ };+ const request = { result: db };+ queueMicrotask(() => request.onsuccess?.());+ return request;+ },+ };+ const globals = {+ location,+ history: { replaceState(state, title, path) { const url = new URL(path, location.origin); location.pathname = url.pathname; location.search = url.search; location.hash = url.hash; } },+ sessionStorage: storage(), localStorage: storage(), indexedDB,+ document: { getElementById: () => null, querySelector: () => null },+ addEventListener: (type, listener, options) => events.addEventListener(type, listener, options),+ /** Serve only the listing, one sealed snapshot, an empty prefix and a live stream that waits until the page hides. */+ fetch: async (input, init = {}) => {+ const url = new URL(String(input), location.origin);+ const accept = new Headers(init.headers).get('accept');+ if (url.pathname === '/api/v1/boards') return reply({ version: 1, boards: [{ id: board, repository: 'acme/widgets' }] });+ if (url.pathname === '/api/v1/boards/' + board + '/state') { snapshotReads += 1; return reply({ version: 1, state: { sequence: 0, sealed } }, 200, { etag: '"0"' }); }+ if (url.pathname === '/api/v1/boards/' + board + '/events' && accept === 'text/event-stream') {+ return new Promise((resolveStream, reject) => init.signal.addEventListener('abort', () => reject(init.signal.reason), { once: true }));+ }+ if (url.pathname === '/api/v1/boards/' + board + '/events') return reply({ version: 1, events: [] });+ unexpected.push(url.pathname);+ return reply({ version: 1, error: { code: 'NOT_FOUND', message: 'This fixture relay has no such route.' } }, 404);+ },+ };+ const saved = Object.keys(globals).map(name => [name, Object.getOwnPropertyDescriptor(globalThis, name)]);+ for (const [name, value] of Object.entries(globals)) Object.defineProperty(globalThis, name, { value, configurable: true, writable: true });+ t.after(() => {+ events.dispatchEvent(new Event('pagehide'));+ for (const [name, descriptor] of saved) {+ if (descriptor) Object.defineProperty(globalThis, name, descriptor);+ else delete globalThis[name];+ }+ });+ return { location, refused, unexpected, replaced, snapshotReads: () => snapshotReads };+}++test('[H5,H15] an early pairing survives until the transport takes it when storage refuses writes', async (t) => {+ const board = '0123456789abcdef0123456789abcdef';+ const key = await generateBoardKey();+ const snapshot = { version: 1, tables: { board_meta: [{ meta_key: 'board_id', meta_value: board }], item: [], agent: [], event: [], shout: [], verdict: [] } };+ const sealed = Buffer.from(await seal(key, new TextEncoder().encode(JSON.stringify(snapshot)), { boardId: board, kind: 'snapshot', sequence: 0 })).toString('base64url');+ const page = refusingStoragePage(t, { hash: '#board=' + board + '&key=' + encodeBoardKey(key), board, sealed });+ const { rememberPairing, createTransport } = await servedRelayClient(t);++ assert.equal(rememberPairing()?.board, board, 'the page reads its pairing link at start, before any transport exists');+ assert.equal(page.location.hash === '', true, 'reading the link strips its fragment from the address bar');+ assert.equal(page.refused.includes('pullboard.relay.pair.v1'), true, 'the browser refused to keep the pairing in session storage');++ const transport = await createTransport();+ assert.deepEqual((await transport.request('/api/v1/boards')).boards.map(row => row.id), [board], 'the transport takes the early pairing from module memory and pairs the board');+ assert.equal(page.snapshotReads(), 1, 'the board pairs once');++ const later = await createTransport();+ assert.deepEqual((await later.request('/api/v1/boards')).boards, [], 'a second take finds no pairing');+ assert.equal(page.snapshotReads(), 1, 'a second transport does not pair the board again');+ assert.equal(page.location.hash === '', true, 'the address bar still has no pairing fragment');++ page.location.hash = '#device=' + 'A'.repeat(22) + '.' + 'B'.repeat(43);+ assert.throws(() => rememberPairing(), error => error.code === 'DEVICE_STORAGE', 'device pairing still refuses storage that cannot keep its code');+ assert.deepEqual(page.unexpected, [], 'the transport used only the fixture relay routes');+ assert.deepEqual(page.replaced, [], 'the page never lost relay access');+});