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(view): export read-only replay snapshots [A10,A3]

Corey Olsonfaabc876d440
diff --git a/src/cockpit.js b/src/cockpit.js
index aa1a515b..81139740 100644
--- a/src/cockpit.js
+++ b/src/cockpit.js
@@ -30,18 +30,20 @@ function lifecycle() {
* the page itself holds none, so its policy can refuse inline styles.
*
* @param {string} [key] - The session's secret.
+ * @param {{snapshot?: boolean}} [options] - A static, read-only page with event replay.
* @returns {string}
*/
-export function cockpitPage(key = '') {
+export function cockpitPage(key = '', { snapshot = false } = {}) {
return `<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Pullboard</title>
-<link rel="stylesheet" href="/view.css?k=${encodeURIComponent(key)}">
+<link rel="stylesheet" href="${snapshot ? 'view.css' : '/view.css?k=' + encodeURIComponent(key)}">
+${snapshot ? '<link rel="icon" href="data:,">' : ''}
</head>
-<body class="loading">
+<body class="loading${snapshot ? ' snapshot' : ''}">
<div class="shell">
<aside class="side" id="side" aria-label="Projects">
<div class="side-top">
@@ -67,6 +69,13 @@ export function cockpitPage(key = '') {
<span class="live" id="live"></span>
</header>
<main>
+ <section class="card-panel snapshot-controls" id="snapshot-controls" ${snapshot ? '' : 'hidden'} aria-label="Snapshot replay">
+ <span>Read-only snapshot</span>
+ <button class="ghost" id="replay-play" type="button">Play from first event</button>
+ <button class="ghost" id="replay-pause" type="button" disabled>Pause</button>
+ <label>Speed <select id="replay-speed"><option value="1">1×</option><option value="4">4×</option><option value="16">16×</option></select></label>
+ <output id="replay-progress" aria-live="polite">Loading events…</output>
+ </section>
<section class="card-panel first">
<h2>No boards yet</h2>
<p>Run <code>pullboard init</code> in a git repo, or ask an agent to. Its board shows up here by itself.</p>
@@ -124,9 +133,12 @@ export function cockpitPage(key = '') {
</div>
<div class="console" id="console" title="Click to close" hidden></div>
<script>
-const key = new URLSearchParams(location.search).get('k') || '';
-const keep = (name, value) => { try { if (value === undefined) return localStorage.getItem(name); localStorage.setItem(name, value); } catch { return null; } return value; };
+const snapshot = ${JSON.stringify(snapshot)};
+const key = snapshot ? '' : new URLSearchParams(location.search).get('k') || '';
+const snapshotReplay = { final: null, events: [], index: 0, playing: false, timer: null };
+const keep = (name, value) => { name = snapshot ? 'snapshot.' + name : name; try { if (value === undefined) return localStorage.getItem(name); localStorage.setItem(name, value); } catch { return null; } return value; };
const view = { root: keep('pb.project'), tab: keep('pb.tab') || 'items', seen: {}, code: {}, item: null, adding: false, state: 'active', rows: { spec: 'decide', doctrine: 'all' }, row: { spec: null, doctrine: null } };
+if (snapshot) view.state = 'all';
let data = null;
let seen = '';
const $ = (id) => document.getElementById(id);
@@ -410,7 +422,9 @@ function flowSvg(p) {
/** Read or move through API v1, retaining the rule and repair guidance in a refusal. */
async function api(path, body) {
- const res = await fetch(path, { method: body ? 'POST' : 'GET', headers: { 'x-pullboard-key': key, ...(body ? { 'content-type': 'application/json' } : {}) }, body: body ? JSON.stringify(body) : undefined });
+ if (snapshot && body) throw new Error('This is a read-only snapshot.');
+ const address = snapshot ? path.split('?')[0].slice(1) + '.json' : path;
+ const res = await fetch(address, { method: body ? 'POST' : 'GET', headers: snapshot ? {} : { 'x-pullboard-key': key, ...(body ? { 'content-type': 'application/json' } : {}) }, body: body ? JSON.stringify(body) : undefined });
const json = await res.json();
if (!res.ok) {
const refusal = json.error;
@@ -496,7 +510,116 @@ async function refresh() {
data = next;
render();
}
- $('live').textContent = 'live · ' + new Date().toLocaleTimeString([], { hour: '2-digit', minute: '2-digit', second: '2-digit', hourCycle: 'h23' });
+ if (snapshot && !snapshotReplay.final && data.project) {
+ snapshotReplay.final = structuredClone(data.project);
+ snapshotReplay.events = (await api(boardPath(view.root) + '/events')).events;
+ snapshotReplay.index = snapshotReplay.events.length;
+ replayControls();
+ }
+ $('live').textContent = snapshot ? 'read-only snapshot' : 'live · ' + new Date().toLocaleTimeString([], { hour: '2-digit', minute: '2-digit', second: '2-digit', hourCycle: 'h23' });
+}
+
+/** Display the current event and make the replay's play, pause and end states explicit. */
+function replayControls() {
+ const r = snapshotReplay;
+ const event = r.events[r.index - 1];
+ $('replay-progress').textContent = 'Event ' + r.index + ' / ' + r.events.length + (event ? ' · ' + event.event_kind + (event.item_id ? ' #' + event.item_id : '') : '') + (r.index === r.events.length ? ' · End' : '');
+ $('replay-play').disabled = r.playing || !r.final || !r.events.length;
+ $('replay-play').textContent = r.index === r.events.length ? 'Play from first event' : 'Play';
+ $('replay-pause').disabled = !r.playing;
+}
+
+/** Project only the events already played, using the same declared lifecycle as the live page. */
+function replayProject(index) {
+ const r = snapshotReplay;
+ if (index === r.events.length) return structuredClone(r.final);
+ const p = structuredClone(r.final);
+ const log = r.events.slice(0, index);
+ p.items = p.items.flatMap((item) => {
+ const history = log.filter((event) => event.item_id === item.id);
+ if (!history.some((event) => event.event_kind === 'add')) return [];
+ let state = FLOW.initial;
+ item.owner = null;
+ item.builtBy = null;
+ item.verifiedBy = null;
+ item.reviewer = null;
+ item.reviewUntil = null;
+ item.commit = null;
+ item.merged = null;
+ let verdicts = 0;
+ for (const event of history) {
+ const verb = event.event_kind === 'renew' ? 'claim' : event.event_kind;
+ /** Find a declared move, distinguishing a renewed claim from a new one. */
+ const fits = (at) => FLOW.moves.find((move) => move.verb === verb && move.from.includes(at) && (verb !== 'claim' || (event.event_kind === 'renew') === (move.to === at)));
+ const lapse = fits(state) ? null : FLOW.moves.find((move) => move.by.includes('clock') && move.from.includes(state) && fits(move.to));
+ if (lapse) state = lapse.to;
+ const move = fits(state);
+ if (move) state = move.to;
+ const detail = JSON.parse(event.event_detail);
+ if (verb === 'claim') item.owner = event.event_by;
+ if (verb === 'submit') { item.commit = detail.commit; item.builtBy = event.event_by; }
+ if (verb === 'accept') { item.verifiedBy = event.event_by; verdicts += 1; }
+ if (verb === 'reject') { item.owner = null; verdicts += 1; }
+ if (verb === 'release' || verb === 'withdraw') item.owner = null;
+ if (verb === 'merged') item.merged = detail.commit;
+ }
+ item.status = state;
+ item.history = history.map((event) => ({ kind: event.event_kind, by: event.event_by, at: event.event_at }));
+ item.updatedAt = history.at(-1).event_at;
+ item.verdicts = item.verdicts.slice(0, verdicts);
+ item.verdict = item.verdicts.at(-1) ?? null;
+ return [item];
+ });
+ const statuses = new Map(p.items.map((item) => [item.id, item.status]));
+ for (const item of p.items) {
+ const added = log.find((event) => event.item_id === item.id && event.event_kind === 'add');
+ item.blockedBy = (JSON.parse(added.event_detail).after || []).filter((id) => statuses.get(id) !== 'verified');
+ }
+ const shouted = new Set(log.map((event) => JSON.parse(event.event_detail).shout).filter(Boolean));
+ p.shouts = p.shouts.filter((shout) => shouted.has(shout.shout_id));
+ p.decisions = p.decisions.filter((shout) => shouted.has(shout.shout_id));
+ p.asked = p.asked.filter((shout) => shouted.has(shout.shout_id));
+ p.events = log.slice(-80).reverse();
+ return p;
+}
+
+/** Draw a replay position without refreshing or changing the exported board. */
+function replayPosition(index) {
+ const r = snapshotReplay;
+ r.index = index;
+ view.root = r.final.root;
+ view.state = 'all';
+ data.root = view.root;
+ data.group = null;
+ data.project = replayProject(index);
+ data.projects = data.projects.map((board) => board.root === view.root ? boardSummary(board, data.project) : board);
+ render();
+ replayControls();
+}
+
+/** Pause before clearing the next callback, so no event advances after the control returns. */
+function pauseReplay() {
+ snapshotReplay.playing = false;
+ clearTimeout(snapshotReplay.timer);
+ replayControls();
+}
+
+/** Play one event per speed-adjusted interval, stopping exactly at the exported final state. */
+function advanceReplay() {
+ const r = snapshotReplay;
+ if (!r.playing) return;
+ replayPosition(r.index + 1);
+ if (r.index === r.events.length) return pauseReplay();
+ r.timer = setTimeout(advanceReplay, 1000 / Number($('replay-speed').value));
+}
+
+/** Resume a paused replay, or restart at the first event when the snapshot is at the end. */
+function playReplay() {
+ const r = snapshotReplay;
+ if (!r.final || !r.events.length || r.playing) return;
+ if (r.index === r.events.length) replayPosition(0);
+ r.playing = true;
+ advanceReplay();
}
/**
@@ -852,6 +975,7 @@ function moveMessage(move, result) {
/** Run a public API move and let only the latest action own the console and its close timer. */
async function act(command, args) {
const out = $('console');
+ if (snapshot) { out.hidden = false; out.className = 'console no'; out.textContent = 'This is a read-only snapshot.'; return false; }
const run = (view.acting = (view.acting || 0) + 1);
const latest = () => run === view.acting;
clearTimeout(view.closing);
@@ -964,8 +1088,15 @@ $('hold-form').addEventListener('submit', async (event) => {
if (await act('hold', { lane: $('hold-lane').value, reason: $('hold-reason').value })) $('hold-reason').value = '';
});
showTab();
+if (snapshot) {
+ $('replay-play').addEventListener('click', playReplay);
+ $('replay-pause').addEventListener('click', pauseReplay);
+ $('replay-speed').addEventListener('change', () => {
+ if (snapshotReplay.playing) { clearTimeout(snapshotReplay.timer); snapshotReplay.timer = setTimeout(advanceReplay, 1000 / Number($('replay-speed').value)); }
+ });
+}
refresh().catch((error) => { document.body.classList.remove('loading'); $('live').textContent = 'cannot reach the view: ' + error.message; });
-setInterval(() => { if (!document.hidden) refresh().catch(() => { $('live').textContent = 'offline: is pullboard view still running?'; }); }, 3000);
+if (!snapshot) setInterval(() => { if (!document.hidden) refresh().catch(() => { $('live').textContent = 'offline: is pullboard view still running?'; }); }, 3000);
setInterval(tickAges, 60000);
</script>
</body>
diff --git a/src/serve.js b/src/serve.js
index b3d57d62..505032e5 100644
--- a/src/serve.js
+++ b/src/serve.js
@@ -9,16 +9,16 @@
*/
import { spawnSync } from 'node:child_process';
import { randomBytes, timingSafeEqual } from 'node:crypto';
-import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';
+import { existsSync, mkdirSync, readdirSync, readFileSync, statSync, writeFileSync } from 'node:fs';
import { createServer } from 'node:http';
-import { dirname, join } from 'node:path';
+import { basename, dirname, join, resolve } from 'node:path';
import * as store from './board.js';
import { cockpitPage } from './cockpit.js';
import { COORDINATOR, loadConfig } from './config.js';
import { loadDoctrine, standardDoctrine } from './doctrine.js';
import { repoInfo, resolveCommit } from './git.js';
import { productSummaries } from './products.js';
-import { registryFile } from './projects.js';
+import { listApiProjects, registryFile } from './projects.js';
import { Refused } from './refused.js';
import { loadSpec } from './spec.js';
@@ -26,6 +26,60 @@ import { loadSpec } from './spec.js';
const VIEW_CSS = readFileSync(new URL('./view.css', import.meta.url), 'utf8');
export const LOOPBACK = '127.0.0.1';
+/**
+ * Export one board through the same API v1 used by the live page, closing its temporary local
+ * transport before returning. The static page needs neither that transport nor its session key.
+ *
+ * @param {string} root
+ * @param {string} directory
+ * @returns {Promise<{path: string}>}
+ */
+export async function exportView(root, directory) {
+ const output = resolve(directory);
+ if (existsSync(output) && (!statSync(output).isDirectory() || readdirSync(output).length)) {
+ throw new Refused('EXPORT_EXISTS', `snapshot folder ${output} is not empty; use an empty folder`);
+ }
+ const info = repoInfo(root);
+ const config = loadConfig(info.root);
+ const registered = listApiProjects().find((entry) => {
+ try { return repoInfo(entry.root).commonDir === info.commonDir; } catch { return false; }
+ }) ?? { root: info.root, name: config.name || basename(info.root), project: config.project || '', added: new Date().toISOString() };
+ const { serveApi } = await import('./api.js');
+ const { main } = await import('./cli.js');
+ const api = await serveApi({ port: 0, projects: () => [registered], runCommand: main });
+ let listing;
+ let state;
+ let events;
+ try {
+ const address = new URL(api.url);
+ /** Read an authenticated API document without putting its private key in the snapshot. */
+ const read = async (path) => {
+ const reply = await fetch(address.origin + path, { headers: { 'x-pullboard-key': address.searchParams.get('k') } });
+ const document = await reply.json();
+ if (!reply.ok) throw new Refused(document.error.code, document.error.message);
+ return document;
+ };
+ listing = await read('/api/v1/boards');
+ const board = listing.boards[0];
+ if (!board) throw new Refused('NO_BOARD', 'no readable board to export; run pullboard init in this repo');
+ const path = '/api/v1/boards/' + encodeURIComponent(board.id);
+ state = await read(path + '/state');
+ events = await read(path + '/events');
+ const last = state.state.events[0]?.event_id ?? 0;
+ events.events = events.events.filter((event) => event.event_id <= last);
+ } finally {
+ await api.close();
+ }
+ const boardPath = join(output, 'api', 'v1', 'boards', listing.boards[0].id);
+ mkdirSync(boardPath, { recursive: true });
+ writeFileSync(join(output, 'index.html'), cockpitPage('', { snapshot: true }));
+ writeFileSync(join(output, 'view.css'), VIEW_CSS);
+ writeFileSync(join(output, 'api', 'v1', 'boards.json'), JSON.stringify(listing) + '\n');
+ writeFileSync(join(boardPath, 'state.json'), JSON.stringify(state) + '\n');
+ writeFileSync(join(boardPath, 'events.json'), JSON.stringify(events) + '\n');
+ return { path: output };
+}
+
/** Where the view keeps the port it last served from, beside the machine's list of projects. */
const lastPortFile = () => join(dirname(registryFile()), 'view.json');
diff --git a/src/view.css b/src/view.css
index fc6c72fb..951353ec 100644
--- a/src/view.css
+++ b/src/view.css
@@ -277,3 +277,8 @@ input:disabled { background: var(--surface-2); color: var(--ink-muted); }
.flow g:hover > .box, .flow g:hover > .edge { stroke-width: 2.2; }
.console { position: fixed; right: 16px; bottom: 16px; width: min(560px, calc(100vw - 32px)); background: var(--surface); border: 1px solid var(--line-strong); border-radius: 12px; box-shadow: var(--shadow); padding: 10px 12px; font: 12px/1.5 var(--mono); white-space: pre-wrap; overflow-wrap: anywhere; max-height: 38vh; overflow: auto; z-index: 20; cursor: pointer; }
.console.ok { border-color: var(--accent); } .console.no { border-color: var(--reject); }
+.snapshot-controls:not([hidden]) { display: flex; align-items: center; flex-wrap: wrap; gap: 10px; margin-bottom: 16px; padding: 12px 16px; }
+.snapshot-controls > span { font-weight: 600; }
+.snapshot-controls label { display: flex; align-items: center; gap: 6px; }
+.snapshot-controls output { color: var(--ink-muted); font: 12px/1.5 var(--mono); }
+.snapshot #new-item, .snapshot form, .snapshot [data-new], .snapshot [data-release], .snapshot [data-shout], .snapshot [data-go^="decide:"] { display: none !important; }