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): catch-up applies the since=N delta, not a re-read [N26,A2]

Corey Olsonfc3de8905d2d
diff --git a/src/cockpit.js b/src/cockpit.js
index 1241690e..023599ad 100644
--- a/src/cockpit.js
+++ b/src/cockpit.js
@@ -6,6 +6,7 @@ import {
import { commandPhrases } from './help.js';
import { personNeeds } from './needs.js';
import { staleChip } from './view/stale.js';
+import { applyStateDelta } from './api-http.js';
/** The CLI's default phrases, ordered by depth for the existing prose inventory guard. */
export const PULLBOARD_COMMANDS = Object.freeze(commandPhrases().sort((left, right) => right.split(' ').length - left.split(' ').length));
@@ -1257,10 +1258,9 @@ async function boardState(root, mark, fresh = null) {
return kept.row;
}
try {
- const query = board.root === root && cursor !== null ? '?seen=' + encodeURIComponent(cursor) : '';
- const reply = await api('/api/v1/boards/' + encodeURIComponent(board.id) + '/state' + query);
- states.set(board.root, reply.state);
- return boardSummary(board, reply.state);
+ const state = await boardRead(board, board.root === root ? cursor : null);
+ states.set(board.root, state);
+ return boardSummary(board, state);
} catch (error) {
return { ...board, ok: false, error: String(error.message || error) };
}
@@ -1274,6 +1274,32 @@ async function boardState(root, mark, fresh = null) {
};
}
+// What each local board last sent this page, {state, sequence, version}: the base its next read asks to extend.
+const sentStates = new Map();
+// #552's delta applier, embedded as the server's own source, so the page applies a delta and recomputes nothing.
+const applyStateDelta = (${applyStateDelta.toString()});
+
+/**
+ * Read one board's summary state. A local board the page read before asks only what changed since the state it holds
+ * (#552) and applies that delta as sent; it loads a fresh snapshot only when the answer resets, as when the server no
+ * longer keeps that base or the format changed (#555). Relay boards and snapshots have no deltas.
+ */
+async function boardRead(board, seen) {
+ const path = '/api/v1/boards/' + encodeURIComponent(board.id) + '/state', mark = seen === null ? '' : 'seen=' + encodeURIComponent(seen);
+ const local = !snapshot && localBoardIds.has(board.id), held = local ? sentStates.get(board.id) : null;
+ if (held && Number.isSafeInteger(held.sequence)) {
+ const reply = await api(path + '?since=' + held.sequence + (mark && '&' + mark) + (held.version === undefined ? '' : '&eventLogVersion=' + encodeURIComponent(held.version)));
+ if (reply.delta) {
+ const state = applyStateDelta(held.state, reply.delta);
+ sentStates.set(board.id, { state, sequence: reply.sequence, version: reply.eventLogVersion });
+ return state;
+ }
+ }
+ const reply = await api(path + (mark && '?' + mark));
+ if (local) sentStates.set(board.id, { state: reply.state, sequence: reply.state.sequence, version: reply.eventLogVersion });
+ return reply.state;
+}
+
/** Whether a board state already carries every item's detail, as exports and event replays do. */
const fullItems = (project) => project.items.every((item) => Array.isArray(item.history));
@@ -1620,8 +1646,8 @@ async function listen(entry) {
/**
* The one catch-up function: bring the page up to date after the events that arrived, one read at a time, by reading
- * the state of each board they changed again. A hidden page catches up when it is shown. #555 switches this read to
- * #552's since=N delta.
+ * what changed in each board they named since the state it holds (#555, through boardRead). A hidden page catches up
+ * when it is shown.
*/
function catchUp() {
if (document.hidden) { catchWhenShown = true; return; }
diff --git a/test/cockpit/live.test.js b/test/cockpit/live.test.js
index 0427f59f..66ac7bce 100644
--- a/test/cockpit/live.test.js
+++ b/test/cockpit/live.test.js
@@ -151,6 +151,33 @@ test('the view shows a move as it happens [N26,A2]', { timeout: 120_000 }, async
}
});
+// #555: the catch-up asks the server what changed since the state the page holds (#552) and applies that delta.
+test('the view catches up with a delta after a move [N26,A2]', { timeout: 120_000 }, async () => {
+ const { box, alpha, view, chrome, close } = await liveBoard('live-delta');
+ try {
+ const held = (await boardOf(view, alpha.repo)).events[0].event_id;
+ const mark = chrome.requests.length;
+ box.run(alpha.web, 'claim', '1');
+ await chrome.waitFor('!!document.querySelector(\'#chain [data-item="1"] .chip.busy\')', 15_000); // budget: hang ceiling for the claim to arrive.
+ await liveReady(chrome);
+ /** The since cursor of each state read the page made from a request mark on, null for a full read. */
+ const reads = (from) => chrome.requests.slice(from).filter((request) => /^https?:/.test(request.url) && /^\/api\/v1\/boards\/[^/]+\/state$/.test(new URL(request.url).pathname))
+ .map((request) => new URL(request.url).searchParams.get('since'));
+ assert.deepEqual(reads(mark), [String(held)], `the page asked once what changed since the event it held, and read no full state: ${JSON.stringify(reads(mark))}`);
+ assert.equal(await chrome.stable('data.project.items.find((item) => item.id === 1).status'), 'claimed', 'the item shows the move');
+ // The delta's result is the page's next base: the move after it asks from the claim on.
+ const claimed = (await boardOf(view, alpha.repo)).events[0].event_id;
+ const again = chrome.requests.length;
+ box.run(alpha.repo, 'add', 'web', 'After the delta', '--specs', 'G1', '--criterion', 'renders');
+ await chrome.waitFor('!!document.querySelector(\'#chain [data-item="2"]\')', 15_000); // budget: hang ceiling for the new item to arrive.
+ await liveReady(chrome);
+ assert.deepEqual(reads(again), [String(claimed)], `the next catch-up asks from the state the delta made: ${JSON.stringify(reads(again))}`);
+ assert.deepEqual(chrome.exceptions, [], 'the page raises no uncaught exception');
+ } finally {
+ await close();
+ }
+});
+
test('an idle view makes no requests [N26,A2]', { timeout: 150_000 }, async () => {
const { box, alpha, chrome, close } = await liveBoard('live-idle');
try {
@@ -272,6 +299,30 @@ test('a catch-up that cannot read the shown board keeps it shown [N26]', async (
}
});
+// #555's fallback: when the server cannot extend the state the page holds, as after it forgot that base, the
+// answer resets and the page loads a fresh snapshot instead.
+test('a catch-up whose base the server does not keep loads a fresh snapshot [N26,A2]', async () => {
+ const box = machine();
+ const alpha = project(box, 'reset-alpha');
+ box.run(alpha.repo, 'add', 'web', 'Before', '--specs', 'G1', '--criterion', 'renders');
+ const view = await startView(box);
+ try {
+ const page = await openPage(view);
+ // Sequence 0 is a base this server never sent, so its answer resets with BASE_NOT_KEPT.
+ page.run('[...sentStates.values()].forEach((held) => { held.sequence = 0; })');
+ box.run(alpha.repo, 'add', 'web', 'After', '--specs', 'G1', '--criterion', 'renders');
+ const mark = page.requests.length;
+ await page.run(`refresh(new Set([${JSON.stringify(alpha.repo)}]))`);
+ const reads = page.requests.slice(mark).map((request) => request.path).filter((path) => /\/state(?:\?|$)/.test(path))
+ .map((path) => new URL(path, 'http://page').searchParams.get('since'));
+ assert.deepEqual(reads, ['0', null], 'the page asked for a delta, was told to reset, and read one fresh snapshot');
+ assert.deepEqual(JSON.parse(page.run('JSON.stringify(data.project.items.map((item) => item.title).sort())')), ['After', 'Before'], 'the fresh snapshot shows the change');
+ assert.equal(page.run(`[...sentStates.values()][0].sequence`), page.run('data.project.sequence'), 'the fresh snapshot is the next base');
+ } finally {
+ await view.stop();
+ }
+});
+
/**
* A project group of `count` real repos, opened in real Chrome as the group, where a claim on `mover`'s item must
* show within two seconds while the page follows every repo on one stream.