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): share canonical phone summaries [A2,H17]
Corey Olsondf7f9afda9e3
diff --git a/relay/browser-history.js b/relay/browser-history.jsindex 4d473055..68246372 100644--- a/relay/browser-history.js+++ b/relay/browser-history.js@@ -65,7 +65,10 @@ export function createPhoneHistory({ active, current, install, changed, failed }/** Supersede only a waiting job; the pinned current worker can finish without starving under live moves. */function queue(page, sequence) {if (closed) return;- if (complete?.id === page.id) { report({ page, sequence }, page.total, true); return; }+ if (complete?.id === page.id) {+ if (!progress.complete || progress.sequence !== sequence) report({ page, sequence }, page.total, true);+ return;+ }if (running?.page.id === page.id || pending?.page.id === page.id) return;pending = { page, sequence };progress = { loaded: 0, total: page.total, complete: false, sequence };diff --git a/src/api.js b/src/api.jsindex b89c028d..50b943db 100644--- a/src/api.js+++ b/src/api.js@@ -43,6 +43,12 @@ import {} from './projects.js';import { milestoneRoadmap } from './roadmap.js';import { proofStats } from './stats.js';+import {+ SUMMARY_KEYS,+ itemSummary,+ milestoneNamesByItem,+ withListFields,+} from './item-list.js';import {prepareFlowHistory,projectFlowStats,@@ -508,123 +514,6 @@ function dependencyIds(item) {return item.item_after ? item.item_after.split(',').map(Number) : [];}-/** Match the first local milestone name exactly as the state list projection does. */-function milestoneNamesByItem(milestones) {- const names = new Map();- for (const milestone of milestones ?? []) {- for (const entry of milestone.items ?? []) {- const id = typeof entry === 'number' ? entry : entry?.id;- if (typeof id === 'number' && !names.has(id)) names.set(id, milestone.name);- }- }- return names;-}--/** What a board-state item summary keeps: what the lists and the roadmap read (A2, N26). */-const SUMMARY_FIELDS = Object.freeze(['id', 'title', 'lane', 'status', 'route', 'milestone', 'specs', 'claim', 'builtBy',- 'blockedBy', 'updatedAt', 'stage', 'since', 'counts', 'verdict', 'verdictState']);-const SUMMARY_KEYS = new Set(SUMMARY_FIELDS);-const CLOCK_MOVES = MACHINE.moves.filter((move) => move.by.includes('clock'));--/**- * Replay an item's recorded events through the declared lifecycle, as the view's timeline does- * (M1). The board logs a claim on a claimed item as renew, and never the clock's lapse, so a move- * that cannot start where the replay stands, or a status the history does not reach, puts the- * lapse in first.- *- * @param {{ kind: string, at: string }[]} history- * @param {string} status- * @returns {{ from: string, move: object | null, at: string | null }[]}- */-function lifecycleSteps(history, status) {- const steps = [];- let at = MACHINE.initial;- /** Record where the replay stands and the move it makes, if any, then follow that move. */- const step = (move, event) => {- steps.push({ from: at, move, at: event ? event.at : null });- if (move) at = move.to;- };- for (const event of history) {- const verb = event.kind === 'renew' ? 'claim' : event.kind;- /** The declared move this event makes from a state; a renew is the claim that stays claimed. */- const fits = (state) => MACHINE.moves.find((move) => move.verb === verb && move.from.includes(state)- && (verb !== 'claim' || (event.kind === 'renew') === (move.to === state)));- const lapse = fits(at) ? null : CLOCK_MOVES.find((move) => move.from.includes(at) && fits(move.to));- if (lapse) step(lapse, null);- step(fits(at) || null, event);- }- const lapse = at === status ? null : CLOCK_MOVES.find((move) => move.from.includes(at) && move.to === status);- if (lapse) step(lapse, null);- return steps;-}--/** When an item arrived in its list stage; edits, review reservations and renewals do not restart it. */-function stageSince(item, steps) {- if (item.merged) return item.history.findLast((event) => event.kind === 'merged')?.at || item.updatedAt;- const arrival = steps.findLast((step) => step.move && step.from !== step.move.to);- // A lapse is inferred, but the claim's own receipt records the deadline that caused it.- if (arrival?.move?.verb === 'lapse') {- const claim = item.thread.findLast((entry) => entry.type === 'move' && ['claim', 'renew'].includes(entry.kind));- if (claim?.detail?.leaseUntil) return claim.detail.leaseUntil;- }- return arrival ? arrival.at || item.updatedAt : item.history.find((event) => event.kind === 'add')?.at || item.updatedAt;-}--/**- * Give every item what its list rows read without its history: its list stage and when it entered- * it, its roadmap milestone, the live lease on it, and its verdict and fact counts. A recorded merge,- * not an accept, ends the pipeline. The board's moves are tallied once, keyed from>to:verb as the- * lifecycle drawing sums them.- *- * @param {any} state- * @param {Map<number, string | null>} leases Each item's recorded claim deadline.- * @returns {any}- */-function withListFields(state, leases, { previous = null, dirtyIds = new Set(state.items.map((item) => item.id)) } = {}) {- const milestones = milestoneNamesByItem(state.milestones);- const moveCounts = {};- const itemMoveCounts = new Map();- const items = state.items.map((item) => {- let contribution = previous?.itemMoveCounts.get(item.id);- let projected = item;- if (!previous || dirtyIds.has(item.id) || !previous.items.has(item.id)) {- const steps = lifecycleSteps(item.history, item.status);- contribution = {};- for (const step of steps) {- const key = step.move && step.from + '>' + step.move.to + ':' + step.move.verb;- if (key) contribution[key] = (contribution[key] ?? 0) + 1;- }- // Who holds the item under a live lease: its builder while claimed, its reviewer while reserved.- const claim = item.status === 'claimed' && item.owner ? { by: item.owner, until: leases.get(item.id) ?? null }- : item.status === 'submitted' && item.reviewer ? { by: item.reviewer, until: item.reviewUntil } : null;- projected = { ...item,- milestone: milestones.get(item.id) ?? null,- claim,- stage: item.merged ? 'merged' : item.status === 'verified' ? 'accepted' : item.status,- since: stageSince(item, steps),- counts: { verdicts: item.verdicts.length, facts: item.thread.filter((entry) => entry.type === 'fact').length },- };- }- contribution ??= {};- itemMoveCounts.set(item.id, contribution);- for (const [key, count] of Object.entries(contribution)) moveCounts[key] = (moveCounts[key] ?? 0) + count;- return projected;- });- return { state: { ...state, items, moveCounts }, itemMoveCounts };-}--/**- * Keep the summary fields. The latest verdict keeps its decision and reason, and, for an item sent- * back and not verified, the first line of its note, which the Items list shows beside the reason.- */-function itemSummary(item) {- const summary = Object.fromEntries(SUMMARY_FIELDS.map((key) => [key, item[key]]));- const sentBack = item.verdict?.decision === 'REJECT' && item.status !== 'verified';- summary.verdict = item.verdict && { decision: item.verdict.decision, reason: item.verdict.reason,- ...(sentBack ? { note: String(item.verdict.note ?? '').split('\n').map((line) => line.trim()).find(Boolean) ?? '' } : {}) };- return summary;-}-/** Everything a summary leaves out, with the latest verdict whole: laid over its summary, it is the full item. */function itemDetail(item) {return Object.fromEntries(Object.entries(item).filter(([key]) => key === 'id' || key === 'verdict' || key === 'verdictState' || !SUMMARY_KEYS.has(key)));diff --git a/src/item-list.js b/src/item-list.jsnew file mode 100644index 00000000..4dad6a7a--- /dev/null+++ b/src/item-list.js@@ -0,0 +1,119 @@+/** Shared local/relay item-list fields, derived once from the declared lifecycle [A2,N26,H17]. */+import { MACHINE } from './machine.js';++/** Match the first local milestone name exactly as the state list projection does. */+export function milestoneNamesByItem(milestones) {+ const names = new Map();+ for (const milestone of milestones ?? []) {+ for (const entry of milestone.items ?? []) {+ const id = typeof entry === 'number' ? entry : entry?.id;+ if (typeof id === 'number' && !names.has(id)) names.set(id, milestone.name);+ }+ }+ return names;+}++/** What a board-state item summary keeps: what the lists and the roadmap read (A2, N26). */+const SUMMARY_FIELDS = Object.freeze(['id', 'title', 'lane', 'status', 'route', 'milestone', 'specs', 'claim', 'builtBy',+ 'blockedBy', 'updatedAt', 'stage', 'since', 'counts', 'verdict', 'verdictState']);+export const SUMMARY_KEYS = new Set(SUMMARY_FIELDS);+const CLOCK_MOVES = MACHINE.moves.filter((move) => move.by.includes('clock'));++/**+ * Replay an item's recorded events through the declared lifecycle, as the view's timeline does+ * (M1). The board logs a claim on a claimed item as renew, and never the clock's lapse, so a move+ * that cannot start where the replay stands, or a status the history does not reach, puts the+ * lapse in first.+ *+ * @param {{ kind: string, at: string }[]} history+ * @param {string} status+ * @returns {{ from: string, move: object | null, at: string | null }[]}+ */+function lifecycleSteps(history, status) {+ const steps = [];+ let at = MACHINE.initial;+ /** Record where the replay stands and the move it makes, if any, then follow that move. */+ const step = (move, event) => {+ steps.push({ from: at, move, at: event ? event.at : null });+ if (move) at = move.to;+ };+ for (const event of history) {+ const verb = event.kind === 'renew' ? 'claim' : event.kind;+ /** The declared move this event makes from a state; a renew is the claim that stays claimed. */+ const fits = (state) => MACHINE.moves.find((move) => move.verb === verb && move.from.includes(state)+ && (verb !== 'claim' || (event.kind === 'renew') === (move.to === state)));+ const lapse = fits(at) ? null : CLOCK_MOVES.find((move) => move.from.includes(at) && fits(move.to));+ if (lapse) step(lapse, null);+ step(fits(at) || null, event);+ }+ const lapse = at === status ? null : CLOCK_MOVES.find((move) => move.from.includes(at) && move.to === status);+ if (lapse) step(lapse, null);+ return steps;+}++/** When an item arrived in its list stage; edits, review reservations and renewals do not restart it. */+function stageSince(item, steps) {+ if (item.merged) return item.history.findLast((event) => event.kind === 'merged')?.at || item.updatedAt;+ const arrival = steps.findLast((step) => step.move && step.from !== step.move.to);+ // A lapse is inferred, but the claim's own receipt records the deadline that caused it.+ if (arrival?.move?.verb === 'lapse') {+ const claim = item.thread.findLast((entry) => entry.type === 'move' && ['claim', 'renew'].includes(entry.kind));+ if (claim?.detail?.leaseUntil) return claim.detail.leaseUntil;+ }+ return arrival ? arrival.at || item.updatedAt : item.history.find((event) => event.kind === 'add')?.at || item.updatedAt;+}++/**+ * Give every item what its list rows read without its history: its list stage and when it entered+ * it, its roadmap milestone, the live lease on it, and its verdict and fact counts. A recorded merge,+ * not an accept, ends the pipeline. The board's moves are tallied once, keyed from>to:verb as the+ * lifecycle drawing sums them.+ *+ * @param {any} state+ * @param {Map<number, string | null>} leases Each item's recorded claim deadline.+ * @returns {any}+ */+export function withListFields(state, leases, { previous = null, dirtyIds = new Set(state.items.map((item) => item.id)) } = {}) {+ const milestones = milestoneNamesByItem(state.milestones);+ const moveCounts = {};+ const itemMoveCounts = new Map();+ const items = state.items.map((item) => {+ let contribution = previous?.itemMoveCounts.get(item.id);+ let projected = item;+ if (!previous || dirtyIds.has(item.id) || !previous.items.has(item.id)) {+ const steps = lifecycleSteps(item.history, item.status);+ contribution = {};+ for (const step of steps) {+ const key = step.move && step.from + '>' + step.move.to + ':' + step.move.verb;+ if (key) contribution[key] = (contribution[key] ?? 0) + 1;+ }+ // Who holds the item under a live lease: its builder while claimed, its reviewer while reserved.+ const claim = item.status === 'claimed' && item.owner ? { by: item.owner, until: leases.get(item.id) ?? null }+ : item.status === 'submitted' && item.reviewer ? { by: item.reviewer, until: item.reviewUntil } : null;+ projected = { ...item,+ milestone: milestones.get(item.id) ?? null,+ claim,+ stage: item.merged ? 'merged' : item.status === 'verified' ? 'accepted' : item.status,+ since: stageSince(item, steps),+ counts: { verdicts: item.verdicts.length, facts: item.thread.filter((entry) => entry.type === 'fact').length },+ };+ }+ contribution ??= {};+ itemMoveCounts.set(item.id, contribution);+ for (const [key, count] of Object.entries(contribution)) moveCounts[key] = (moveCounts[key] ?? 0) + count;+ return projected;+ });+ return { state: { ...state, items, moveCounts }, itemMoveCounts };+}++/**+ * Keep the summary fields. The latest verdict keeps its decision and reason, and, for an item sent+ * back and not verified, the first line of its note, which the Items list shows beside the reason.+ */+export function itemSummary(item) {+ const summary = Object.fromEntries(SUMMARY_FIELDS.map((key) => [key, item[key]]));+ const sentBack = item.verdict?.decision === 'REJECT' && item.status !== 'verified';+ summary.verdict = item.verdict && { decision: item.verdict.decision, reason: item.verdict.reason,+ ...(sentBack ? { note: String(item.verdict.note ?? '').split('\n').map((line) => line.trim()).find(Boolean) ?? '' } : {}) };+ return summary;+}diff --git a/src/relay-page-client.js b/src/relay-page-client.jsindex 19ecc7e7..31999406 100644--- a/src/relay-page-client.js+++ b/src/relay-page-client.js@@ -1,7 +1,8 @@/** Native bounded snapshot upload/resume and complete restore; no private cache fingerprint crosses HTTP [H15,H17]. */import { Refused } from './refused.js';import { createRelayPageCache, snapshotSourceId } from './relay-page-cache.js';-import { openPagedSnapshot, writePagedSnapshot } from './relay-page-layout.js';+import { openPagedSnapshot } from './relay-page-layout.js';+import { writePagedSnapshot } from './relay-page-writer.js';import { PAGE_CIPHER_BYTES, pageReference } from './sealed-pages.js';const PAGE_CAPABILITIES = new WeakMap();diff --git a/src/relay-page-layout.js b/src/relay-page-layout.jsindex 7efa9794..a6744336 100644--- a/src/relay-page-layout.js+++ b/src/relay-page-layout.js@@ -18,6 +18,9 @@ function summary(item) { return Object.fromEntries(Object.entries(item).filter((export function currentSnapshotPresentation(presentation) {const state = presentation?.state;if (!state || !Array.isArray(state.items)) throw invalid();+ const full = { ...presentation, state: { ...state, items: state.items.map(summary) } };+ delete full.shouts;+ if (encoder.encode(JSON.stringify(full)).byteLength <= CURRENT_SNAPSHOT_BYTES) return full;for (let step = 0; step < 10; step++) {const count = Math.max(1, 128 >> step), text = Math.max(32, 4096 >> step);/** Keep schema names while limiting first-screen arrays, nesting and visible text, never the retained source. */@@ -140,7 +143,7 @@ async function* readRows(reference, field, context, target = null, depth = 0, re}/** Seal native tables once, current summaries separately, and detail/search indexes for on-demand phone reads. */-export async function writePagedSnapshot(document, context) {+export async function encodePagedSnapshot(document, context, projectList) {const tables = document?.tables;const state = document?.presentation?.state;const meta = new Map((tables?.board_meta ?? []).map(row => [row.meta_key, row.meta_value]));@@ -148,11 +151,12 @@ export async function writePagedSnapshot(document, context) {if (document?.version !== 1 || meta.get('board_id') !== context.boardId || !Number.isSafeInteger(engine) || engine < 1 ||String(context.sequence) !== meta.get('relay_applied_sequence') || !Array.isArray(state?.items) || !Array.isArray(tables.shout)) throw invalid();const names = Object.keys(tables);- if (!names.length || names.length > PAGE_FANOUT - 5 || names.some(name => !/^[a-z][a-z0-9_]*$/u.test(name))) throw invalid();+ if (!names.length || names.length > PAGE_FANOUT - 6 || names.some(name => !/^[a-z][a-z0-9_]*$/u.test(name))) throw invalid();const full = { ...document.presentation, state: { ...state, items: state.items.map(summary) } };delete full.shouts;- const boot = currentSnapshotPresentation(full);- const references = [await writeSealedValue(boot, context), await writeSealedValue(full, context)];+ const currentFull = { ...full, state: projectList(state, tables) };+ const boot = currentSnapshotPresentation(currentFull);+ const references = [await writeSealedValue(boot, context), await writeSealedValue(full, context), await writeSealedValue(currentFull, context)];for (const name of names) references.push(await writeRows(tables[name], name === 'shout' ? 'shout_id' : null, context));const items = [...state.items].sort((a, b) => a.id - b.id);references.push(await writeRows(items, 'id', context));@@ -170,7 +174,7 @@ export async function openPagedSnapshot(rawReference, context, { maxEngine } = {const { value, links } = await readSealedDescriptor(reference, context);if (value?.format !== FORMAT || value.version !== 1 || value.boardId !== context.boardId || value.sequence !== context.sequence ||!Number.isSafeInteger(value.engine) || value.engine < 1 || !Number.isSafeInteger(maxEngine) || !Array.isArray(value.tables) ||- value.tables.length + 5 !== links.length || new Set(value.tables).size !== value.tables.length ||+ value.tables.length + 6 !== links.length || new Set(value.tables).size !== value.tables.length ||value.tables.some(name => typeof name !== 'string' || !/^[a-z][a-z0-9_]*$/u.test(name))) throw invalid();if (value.engine > maxEngine) throw new Refused('ENGINE_VERSION', `This snapshot needs engine ${value.engine}; this device reads ${maxEngine}. Upgrade Pullboard before reading the board.`);const counts = value.counts;@@ -179,8 +183,8 @@ export async function openPagedSnapshot(rawReference, context, { maxEngine } = {const historyTotal = counts.items + Object.values(counts.tables).reduce((sum, count) => sum + count, 0);if (!Number.isSafeInteger(historyTotal)) throw invalid();const childContext = { ...context, parents: [...(context.parents ?? []), reference.id] };- const tableReferences = new Map(value.tables.map((name, index) => [name, links[index + 2]]));- const [details, search, answers] = links.slice(value.tables.length + 2);+ const tableReferences = new Map(value.tables.map((name, index) => [name, links[index + 3]]));+ const [details, search, answers] = links.slice(value.tables.length + 3);if (!tableReferences.has('board_meta') || !tableReferences.has('shout')) throw invalid();let current;/** Cache only the current presentation, not arbitrary historical pages. */@@ -236,7 +240,7 @@ export async function openPagedSnapshot(rawReference, context, { maxEngine } = {}/** Stream every retained table and full item detail newest first after current state has already rendered. */async function* history() {- yield { kind: 'presentation', value: await readSealedValue(links[1], childContext) };+ yield { kind: 'presentation', value: await readSealedValue(links[2], childContext) };const tables = ['shout', 'event', ...value.tables.filter(name => !['shout', 'event'].includes(name))];for (const name of tables) {const ref = tableReferences.get(name);diff --git a/src/relay-page-writer.js b/src/relay-page-writer.jsnew file mode 100644index 00000000..896fbb08--- /dev/null+++ b/src/relay-page-writer.js@@ -0,0 +1,15 @@+/** Native snapshot projection uses the same lifecycle fields as the local API [A2,H17]. */+import { itemSummary, withListFields } from './item-list.js';+import { encodePagedSnapshot } from './relay-page-layout.js';++/** Project current list fields without pulling the native lifecycle implementation into phone readers. */+function projectList(state, tables) {+ const leases = new Map((tables.item ?? []).map(row => [row.item_id, row.item_lease_until ?? null]));+ const listed = withListFields(state, leases).state;+ return { ...listed, items: listed.items.map(itemSummary) };+}++/** Seal an importable native snapshot and canonical bounded phone summaries. */+export async function writePagedSnapshot(document, context) {+ return encodePagedSnapshot(document, context, projectList);+}diff --git a/test/relay-browser-history.test.js b/test/relay-browser-history.test.jsindex 9e1b2b56..5e995df9 100644--- a/test/relay-browser-history.test.js+++ b/test/relay-browser-history.test.js@@ -39,10 +39,11 @@ test('history finishes a pinned predecessor while newer moves keep their own curconst a = page('a', aGate), b = page('b', bGate);let current = { id: 'a', sequence: 7 }, alive = true;const installed = [], failures = [], done = deferred();+ let changes = 0;let history;history = createPhoneHistory({ active: () => alive, current: () => current,install(value, details) { installed.push({ marker: value.state.marker, current: current.id, sequence: current.sequence, full: Boolean(details) }); },- changed() { if (history.status().complete && history.status().sequence === 8) done.resolve(); },+ changed() { changes++; if (history.status().complete && history.status().sequence === 8) done.resolve(); },failed(error) { failures.push(error.message); },});t.after(() => { alive = false; history.close(); aGate.resolve(); bGate.resolve(); });@@ -60,8 +61,10 @@ test('history finishes a pinned predecessor while newer moves keep their own curassert.deepEqual(failures, []);assert.deepEqual(a.counts(), { runs: 1, pruned: 0 });assert.deepEqual(b.counts(), { runs: 1, pruned: 1 });+ const before = changes;history.queue(b, 8); history.start();assert.equal(b.counts().runs, 1, 'a completed checkpoint is not streamed repeatedly by foreground reads');+ assert.equal(changes, before, 'a foreground read of completed history cannot emit another refresh and create an RPC feedback loop');});test('access loss discards held details and late background results cannot install or advertise completion [H15,H17]', async () => {diff --git a/test/relay-client-fixture.js b/test/relay-client-fixture.jsindex f8f28893..dee8a76b 100644--- a/test/relay-client-fixture.js+++ b/test/relay-client-fixture.js@@ -209,6 +209,7 @@ globalThis.fetch = function tracedFetch(input, init) {let refuseSnapshotWrites = false;let rejectSnapshotWrites = null;let rejectedStateReads = null;+ let legacySnapshotApi = false;let beforeSnapshotWrite = null;let refuseRequestWrites = false;let refuseBoardLinks = false;@@ -331,6 +332,11 @@ globalThis.fetch = function tracedFetch(input, init) {request: Buffer.concat(requestChunks), response: Buffer.concat(responseChunks) }));calls.push({ method: req.method, path: req.url, accept: req.headers.accept ?? '', engine: req.headers['x-pullboard-engine'],browser: String(req.headers.cookie ?? '').includes('pb_session=') });+ if (legacySnapshotApi && req.method === 'GET' && req.url === '/api/v2') {+ res.writeHead(200, { 'content-type': 'application/json' });+ res.end(JSON.stringify({ version: 1, apiVersion: 1, snapshot: 'legacy' }));+ request.resume(); return;+ }if (req.method === 'POST' && req.url === '/auth/device/confirmation/status' && nextConfirmationStatusHold) {const hold = nextConfirmationStatusHold;nextConfirmationStatusHold = null;@@ -698,6 +704,8 @@ globalThis.fetch = function tracedFetch(input, init) {refuseSnapshotWrites(value) { refuseSnapshotWrites = value; },/** Return a chosen HTTP refusal for snapshot writes, without inspecting the sealed body. */rejectSnapshots(value) { rejectSnapshotWrites = value ? { ...value } : null; },+ /** Model an older relay's capability response without changing the real API1 request and journal bounds. */+ legacySnapshotApi(value = true) { legacySnapshotApi = value; },/** Order a competing real HTTP upload before the next native snapshot reaches the journal. */beforeNextSnapshot(value) { beforeSnapshotWrite = value; },/** Return a chosen real HTTP state refusal to exercise uncertain-baseline safety. */diff --git a/test/relay-page-layout.test.js b/test/relay-page-layout.test.jsindex 001c5878..5760d192 100644--- a/test/relay-page-layout.test.js+++ b/test/relay-page-layout.test.js@@ -3,7 +3,8 @@ import assert from 'node:assert/strict';import { randomBytes } from 'node:crypto';import { test } from 'node:test';import { generateBoardKey } from '../src/seal.js';-import { CURRENT_SNAPSHOT_BYTES, currentSnapshotPresentation, openPagedSnapshot, writePagedSnapshot } from '../src/relay-page-layout.js';+import { CURRENT_SNAPSHOT_BYTES, currentSnapshotPresentation, openPagedSnapshot } from '../src/relay-page-layout.js';+import { writePagedSnapshot } from '../src/relay-page-writer.js';import { readSealedDescriptor, writeSealedDescriptor } from '../src/sealed-pages.js';/** Keep synthetic cipher objects and prove every requested history path belongs to the selected root. */@@ -52,6 +53,11 @@ test('current summaries read before history and a selected old shout follows onlconst boot = await page.presentation();assert.deepEqual(boot.state.items.map(item => item.id), [90, 2, 61]);for (const item of boot.state.items) for (const field of ['criterion', 'brief', 'history', 'thread', 'verdicts']) assert.ok(!Object.hasOwn(item, field));+ for (const item of boot.state.items) {+ assert.equal(item.stage, 'open', 'summary rows carry the canonical lifecycle stage before their history arrives');+ assert.deepEqual(item.counts, { verdicts: 0, facts: 0 });+ assert.equal(item.claim, null);+ }assert.ok(!Object.hasOwn(boot, 'shouts'), 'the complete shout history is not duplicated in phone boot');assert.ok(box.reads.length < 5 && total > 70, 'current state reads without downloading the historical page graph');box.reads.length = 0;diff --git a/test/relay-snapshot-size.test.js b/test/relay-snapshot-size.test.jsindex 2fd96d1c..8c11ba1b 100644--- a/test/relay-snapshot-size.test.js+++ b/test/relay-snapshot-size.test.js@@ -1,7 +1,7 @@/** Compression stays inside the device seal while both clients retain legacy snapshot reads [H5,H15,H17]. */import assert from 'node:assert/strict';import { createHash, randomBytes } from 'node:crypto';-import { readFileSync, writeFileSync } from 'node:fs';+import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';import { join } from 'node:path';import { gzipSync, gunzipSync } from 'node:zlib';import { test } from 'node:test';@@ -11,6 +11,7 @@ import { ENGINE_VERSION } from '../src/machine.js';import { presentationDigest, relaySnapshot } from '../src/relay-presentation.js';import { relayClientFixture } from './relay-client-fixture.js';import { findChromeExecutable, startChrome } from './relay-browser-fixture.js';+import { readSealedDescriptor } from '../src/sealed-pages.js';const RELAY_LIMIT = 10_000_000;const BODY_LIMIT = 14_000_000;@@ -169,7 +170,8 @@ test('a large board syncs to the relay compressed [H5,H15,H17]', {})()`, 'hash browser projection and native snapshot');assert.deepEqual(browserRead, { projectionHash, tablesHash }, 'the production browser client reads the exact projection and browser gzip retains every native table byte');- // A valid but still-too-large gzip checkpoint remains pending and is named by both CLI readers.+ // An older relay that does not advertise paged snapshots keeps its explicit per-request refusal.+ box.legacySnapshotApi();const oversizedDocument = relaySnapshot(box.root);const randomTitle = 'OVERSIZE_SNAPSHOT_340_' + randomBytes(5_050_000).toString('base64');const itemId = oversizedDocument.tables.item[0].item_id;@@ -195,3 +197,163 @@ test('a large board syncs to the relay compressed [H5,H15,H17]', {assert.ok(doctorJSON.document.problems.some(problem => problem.code === 'RELAY_SNAPSHOT_LIMIT'), 'doctor JSON names the stable oversize rule');assert.match(doctor.stdout, new RegExp(`relay snapshot for ${sourceState.board}: sealed snapshot \\d+\\.\\d MB over the relay limit 10\\.0 MB`));});++/** Authenticate one private fixture object's graph path without exposing credentials in diagnostics. */+function pagedReadContext(box, link, head, key) {+ return { boardId: link.board, key, sequence: head.sequence,+ /** Public HTTP still checks the principal's pinned root; only this device interprets the sealed directory. */+ async get(id, parents) {+ const response = await fetch(box.origin + '/api/v2/boards/' + link.board + '/objects/' + id+ + '?pin=' + head.read.id + '&via=' + parents.join(','), {+ headers: { authorization: 'Bearer ' + link.token, 'x-pullboard-engine': String(ENGINE_VERSION) },+ });+ const document = await response.json();+ assert.equal(response.status, 200, document.error?.message);+ return Buffer.from(document.page.sealed, 'base64url');+ },+ };+}++/** Select an old event leaf, so a controlled background hold cannot accidentally block the current-state boot. */+async function oldestEventPage(head, context) {+ const root = await readSealedDescriptor(head.root, context);+ const event = root.value.tables.indexOf('event');+ assert.ok(event >= 0);+ let reference = root.links[event + 3], parents = [head.root.id];+ for (;;) {+ const node = await readSealedDescriptor(reference, { ...context, parents });+ const range = node.value.entries[0], child = node.links[0];+ assert.ok(range && child);+ if (range.leaf) return child.id;+ parents = [...parents, reference.id]; reference = child;+ }+}++test('thirty thousand events open on the phone within three seconds and stream all sealed history beside live moves [H5,H15,H17]', {+ skip: !findChromeExecutable() && 'Chrome is not installed',+}, async t => {+ const box = await relayClientFixture(t);+ await box.link();+ const pairing = await box.cli('relay', 'pair');+ const second = await box.otherDeviceJoin(pairing.document.code);+ const currentMarker = 'PAGED_CURRENT_STATE_903';+ const oldestMarker = 'PAGED_OLDEST_SHOUT_903';+ const liveMarker = 'LIVE_MOVE_DURING_HISTORY_903';+ const native = store.openBoard(join(box.root, '.git/pullboard/board.sqlite'));+ let oldest;+ try {+ // These are synthetic native fixture moves in one normal board transaction, never owner-board edits.+ store.atomic(native, () => {+ for (let index = 0; index < 900; index++) store.addItem(native, {+ by: 'coordinator', lane: box.lane, title: index === 899 ? currentMarker : 'Retained current work ' + index, route: 'strong',+ });+ oldest = store.shout(native, { from: 'coordinator', to: 'all', text: oldestMarker, lanes: [box.lane] });+ // Incompressible retained history proves several times the old cap in actual uploaded cipher bytes.+ for (let index = 0; index < 32; index++) store.shout(native, {+ from: 'coordinator', to: 'all', text: 'Private historical data ' + index + ':' + randomBytes(1_000_000).toString('base64'), lanes: [box.lane],+ });+ for (let index = 0; index < 30_017; index++) store.shout(native, {+ from: 'coordinator', to: 'all', text: 'Recent and retained work ' + index, lanes: [box.lane],+ });+ });+ assert.equal(store.allShouts(native)[0].shout_id, oldest, 'the addressed marker really is this board\'s oldest shout');+ } finally { store.closeBoard(native); }+ const before = relaySnapshot(box.root);+ assert.ok(before.tables.event.length >= 30_000 && before.tables.event.length < 31_000);+ assert.ok(gzipSync(Buffer.from(JSON.stringify(before))).byteLength > RELAY_LIMIT * 3, 'even compressed whole-board state is several times the old limit');+ assert.equal((await box.cli('status')).code, 0, 'the actual opted-in CLI publishes through bounded pages');+ const link = JSON.parse(readFileSync(box.linkFile, 'utf8'));+ assert.equal(link.snapshotApiVersion, 2);+ assert.equal(link.checkpoint, undefined);+ const writes = box.transit.filter(row => row.method === 'PUT' && /\/api\/v2\/boards\/[^/]+\/uploads\/[^/]+\/objects\//u.test(row.path));+ const uploaded = new Map(writes.map(row => [row.path.split('/').at(-1), Buffer.from(JSON.parse(row.request).sealed, 'base64url').byteLength]));+ const cipherBytes = [...uploaded.values()].reduce((sum, bytes) => sum + bytes, 0);+ assert.ok(cipherBytes > RELAY_LIMIT * 3, 'real unique encrypted upload objects exceed thirty million bytes');+ assert.ok(writes.every(row => row.request.byteLength < 600_000), 'no request carries the whole board');+ assert.ok(writes.every(row => !row.request.includes(Buffer.from(oldestMarker))), 'the relay stores no historical plaintext');+ const headers = { authorization: 'Bearer ' + link.token, 'x-pullboard-engine': String(ENGINE_VERSION) };+ const headResponse = await fetch(box.origin + '/api/v2/boards/' + link.board + '/state', { headers });+ const head = (await headResponse.json()).state;+ const key = decodeBoardKey(readFileSync(box.keyFile, 'utf8').trim());+ const context = pagedReadContext(box, link, head, key);+ const root = await readSealedDescriptor(head.root, context);+ assert.ok(root.links[0].bytes < 1_000_000, 'the authenticated current-state plaintext budget is under one megabyte');+ const heldPage = await oldestEventPage(head, context);+ const chrome = await startChrome();+ t.after(() => chrome.close());+ await chrome.send('Emulation.setDeviceMetricsOverride', { width: 390, height: 844, deviceScaleFactor: 1, mobile: true });+ await chrome.send('Emulation.setTouchEmulationEnabled', { enabled: true, maxTouchPoints: 5 });+ await chrome.send('Page.addScriptToEvaluateOnNewDocument', { source: String.raw`+ window.__phoneTiming = { firstBoardPaint: null, objectRequests: 0, loopbackReads: 0, historyHeld: false };+ window.__releaseOlderHistory = null;+ const originalFetch = window.fetch;+ window.fetch = async (input, options) => {+ const url = new URL(typeof input === 'string' ? input : input.url, location.href);+ if (['http://127.0.0.1:4890', 'http://localhost:4890'].includes(url.origin)) {+ __phoneTiming.loopbackReads++; throw new TypeError('No local view in this private phone fixture');+ }+ if (/\/api\/v2\/boards\/[^/]+\/objects\//u.test(url.pathname)) __phoneTiming.objectRequests++;+ if (url.pathname.endsWith('/objects/${heldPage}') && !window.__olderHistoryAllowed) {+ __phoneTiming.historyHeld = true;+ await new Promise(resolveHistory => { window.__releaseOlderHistory = resolveHistory; });+ }+ return originalFetch(input, options);+ };+ let queued = false;+ new MutationObserver(() => {+ const node = document.querySelector('#chain');+ if (queued || !node?.textContent.includes('${currentMarker}') || !node.getBoundingClientRect().height) return;+ queued = true;+ requestAnimationFrame(() => { __phoneTiming.firstBoardPaint = performance.now(); });+ }).observe(document, { childList: true, subtree: true, attributes: true, characterData: true });+ ` });+ await chrome.send('Network.setCookie', {+ name: 'pb_session', value: (await box.phoneSession()).token, url: box.origin, httpOnly: true, sameSite: 'Lax',+ });+ await chrome.navigate(box.origin + '/#board=' + link.board + '&key=' + readFileSync(box.keyFile, 'utf8').trim());+ // Budget: the owner's explicit first-readable-board bar is three seconds, independent of historical streaming.+ await chrome.waitFor('Number.isFinite(window.__phoneTiming?.firstBoardPaint) && typeof transport !== "undefined" && !!transport?.historyProgress', 3000, 'bounded current board paints before all history');+ const first = await chrome.evaluate(`({ ...__phoneTiming, phone: document.documentElement.dataset.relayDevice, progress: transport.historyProgress('${link.board}') })`);+ assert.equal(first.phone, 'phone'); assert.ok(first.firstBoardPaint < 3000);+ assert.equal(first.loopbackReads, 0); assert.equal(first.progress.complete, false);+ // Budget: this fixture streams thirty thousand historical records before reaching its deliberately held oldest event leaf.+ await chrome.waitFor('window.__phoneTiming.historyHeld', 30_000, 'old history is streaming after the first readable board');+ try {+ assert.equal((await box.cli('add', box.lane, liveMarker)).code, 0, 'ordinary native moves continue while old history is pinned');+ await chrome.waitFor(`document.querySelector('#chain')?.textContent.includes('${liveMarker}')`, 15_000, 'new current state arrives while an older history page is held');+ } finally {+ await chrome.evaluate('window.__olderHistoryAllowed = true; window.__releaseOlderHistory?.(); true');+ }+ const expected = relaySnapshot(box.root);+ const finalSequence = Number(expected.tables.board_meta.find(row => row.meta_key === 'relay_applied_sequence').meta_value);+ // Budget: a complete loopback replay includes both the pinned predecessor and its live successor, never the three-second boot bar.+ await chrome.waitFor(`transport.historyProgress('${link.board}').complete && transport.historyProgress('${link.board}').sequence === ${finalSequence}`, 45_000, 'all sealed history is held at the live checkpoint');+ const oldestHash = createHash('sha256').update(oldestMarker).digest('hex');+ const read = await chrome.evaluate(`(async () => {+ const before = __phoneTiming.objectRequests;+ const reply = await transport.request('/api/v1/boards/${link.board}/shouts/${oldest}');+ const digest = new Uint8Array(await crypto.subtle.digest('SHA-256', new TextEncoder().encode(reply.shout.shout_text)));+ return { hash: [...digest].map(byte => byte.toString(16).padStart(2, '0')).join(''), extraObjectReads: __phoneTiming.objectRequests - before,+ progress: transport.historyProgress('${link.board}'), loopbackReads: __phoneTiming.loopbackReads };+ })()`);+ assert.equal(read.hash, oldestHash); assert.equal(read.extraObjectReads, 0, 'the oldest shout is already held after the background stream');+ assert.equal(read.progress.loaded, read.progress.total); assert.equal(read.loopbackReads, 0);+ await chrome.evaluate(`location.hash = '#shout-${oldest}'; true`);+ await chrome.waitFor(`document.body.innerText.includes('${oldestMarker}')`, 3000, 'the actual phone view opens the streamed oldest shout');+ assert.equal((await second.cli('status')).code, 0, 'an independent paired CLI restores the same paged checkpoint');+ const restored = await second.cli('export');+ assert.equal(restored.code, 0);+ const nativeDigest = value => createHash('sha256').update(JSON.stringify(plainTables(value))).digest('hex');+ assert.equal(nativeDigest(restored.document.tables), nativeDigest(expected.tables), 'every native row, counter and receipt is retained');+ const metrics = { firstPaintMs: first.firstBoardPaint, events: expected.tables.event.length, uniqueCipherBytes: cipherBytes,+ largestUploadBody: Math.max(...writes.map(row => row.request.byteLength)), currentSnapshotBytes: root.links[0].bytes,+ progress: read.progress, oldestHash, nativeTablesHash: nativeDigest(expected.tables) };+ t.diagnostic(JSON.stringify(metrics));+ if (process.env.PULLBOARD_RELAY_PAGE_PROOF) {+ const output = process.env.PULLBOARD_RELAY_PAGE_PROOF;+ mkdirSync(output, { recursive: true, mode: 0o700 });+ writeFileSync(join(output, 'metrics.json'), JSON.stringify(metrics, null, 2) + '\n', { mode: 0o600 });+ const screenshot = await chrome.send('Page.captureScreenshot', { format: 'png' });+ writeFileSync(join(output, 'oldest-shout-phone.png'), Buffer.from(screenshot.data, 'base64'), { mode: 0o600 });+ }+});