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): a checkpoint upload holds no lock, so moves never wait [H5]
Corey Olson013609a3d551
diff --git a/src/relay.js b/src/relay.jsindex f18458c7..aeeb7e13 100644--- a/src/relay.js+++ b/src/relay.js@@ -884,11 +884,25 @@ async function prepareCheckpoint(root, state) {* a newer saved checkpoint.*/async function publishCheckpoint(root, file, state, io, prepared = null) {- if (!state.token?.startsWith('pm_') || process.env.PULLBOARD_RELAY_TOKEN) return;+ const staged = await stageCheckpoint(root, file, state, io, prepared);+ if (!staged) return;+ const sent = state.checkpoint;+ let outcome;+ try { outcome = { reply: await request(state, '/api/v1/boards/' + state.board + '/state', { method: 'PUT', body: sent }, io) }; }+ catch (error) { outcome = { error }; }+ recordCheckpointUpload(file, state, io, staged, sent, outcome);+}++/**+ * Decide what checkpoint to send and save its exact sealed record first; null when nothing is to be sent (no machine+ * credential, an upload pause not yet over, or a definitive rejection of this same image).+ */+async function stageCheckpoint(root, file, state, io, prepared) {+ if (!state.token?.startsWith('pm_') || process.env.PULLBOARD_RELAY_TOKEN) return null;const resumed = Boolean(state.checkpointRefusal);if (state.checkpointRefusal && uploadPauseType(state.checkpointRefusal)) {if (!await uploadRetryReady(state, state.checkpointRefusal, path => request(state, path, {}, io))) {- saveLink(file, state); reportCheckpointRefusal(state, io); return;+ saveLink(file, state); reportCheckpointRefusal(state, io); return null;}delete state.checkpointRefusal;saveLink(file, state);@@ -901,29 +915,39 @@ async function publishCheckpoint(root, file, state, io, prepared = null) {saveLink(file, state);}// A definitive rejection retries only a fresh image after the board changes.- if (state.checkpointRefusal) { reportCheckpointRefusal(state, io); return; }+ if (state.checkpointRefusal) { reportCheckpointRefusal(state, io); return null; }}- let reply;- try { reply = await request(state, '/api/v1/boards/' + state.board + '/state', { method: 'PUT', body: state.checkpoint }, io); }- catch (error) {+ return { resumed };+}++/**+ * Record how the upload of `sent` ended. A newer checkpoint saved meanwhile is left for the next upload, so an older+ * acknowledgement never marks it sent.+ */+function recordCheckpointUpload(file, state, io, staged, sent, { reply, error }) {+ if (error) {if (!(error instanceof Refused) || !permanentUploadRefusal(error)) throw error;- state.checkpointRefusal = { code: safePauseCode(error), message: safePauseMessage(error), status: error.httpStatus };- saveLink(file, state);- reportCheckpointRefusal(state, io);+ if (state.checkpoint?.sealed === sent.sealed) {+ state.checkpointRefusal = { code: safePauseCode(error), message: safePauseMessage(error), status: error.httpStatus };+ saveLink(file, state);+ reportCheckpointRefusal(state, io);+ }return;}- if (reply.state?.sequence !== state.checkpoint.sequence || reply.state.sealed !== state.checkpoint.sealed) throw new Refused('RELAY_RESPONSE', 'the relay did not acknowledge this checkpoint; retry the configured relay');- state.presentationDigest = state.checkpointPresentationDigest;- delete state.checkpoint;- delete state.checkpointPresentationDigest;+ if (reply.state?.sequence !== sent.sequence || reply.state.sealed !== sent.sealed) throw new Refused('RELAY_RESPONSE', 'the relay did not acknowledge this checkpoint; retry the configured relay');+ if (state.checkpoint?.sealed === sent.sealed) {+ state.presentationDigest = state.checkpointPresentationDigest;+ delete state.checkpoint;+ delete state.checkpointPresentationDigest;+ }saveLink(file, state);- if (resumed) clearUploadPauseNotices(state);+ if (staged.resumed) clearUploadPauseNotices(state);}/*** Publish this board's checkpoint once: the background publisher's one step (#1014). The board is read and sealed- * before the relay lock; only saving and uploading the sealed record hold it, since the upload may record the relay's- * API version and storage in the link. A board whose link is gone or being removed publishes nothing.+ * before the relay lock, the lock is held only to save the sealed record and later to record the reply, and the+ * upload itself holds no lock. A board whose link is gone or being removed publishes nothing.** @param {string} root* @param {{ err: (line: string) => void, say: (line: string) => void }} io@@ -935,12 +959,26 @@ export async function publishLatestCheckpoint(root, io) {if (!linked || linked.unlinking) return false;// Reading and sealing a large board takes about a second; it happens before the lock, so moves never wait on it.const prepared = await prepareCheckpoint(root, linked);- return locked(file, async () => {+ const staged = await locked(file, async () => {const state = loadLink(file);- if (!state || state.unlinking || state.board !== linked.board) return false;- await publishCheckpoint(root, file, state, io, prepared);- return true;+ if (!state || state.unlinking || state.board !== linked.board) return null;+ const result = await stageCheckpoint(root, file, state, io, prepared);+ return result && { ...result, sent: state.checkpoint, copy: structuredClone(state) };+ }, io);+ if (!staged) return false;+ // The upload holds no lock, so a move during it returns at once and the next checkpoint covers it. It runs on a+ // copy of the link: the copy saves nothing, and what the upload learns is carried back under the lock below.+ let outcome;+ try { outcome = { reply: await request(staged.copy, '/api/v1/boards/' + staged.copy.board + '/state', { method: 'PUT', body: staged.sent }, io) }; }+ catch (error) { outcome = { error }; }+ await locked(file, async () => {+ const state = loadLink(file);+ if (!state || state.board !== linked.board) return;+ if (staged.copy.snapshotApiVersion !== undefined) state.snapshotApiVersion = staged.copy.snapshotApiVersion;+ if (staged.copy.storage !== undefined) state.storage = staged.copy.storage;+ recordCheckpointUpload(file, state, io, staged, staged.sent, outcome);}, io);+ return true;}/** Drain a legacy source once, refusing foreign history before it can be silently checkpointed. */diff --git a/test/relay-checkpoint.test.js b/test/relay-checkpoint.test.jsindex c862e55b..985adf39 100644--- a/test/relay-checkpoint.test.js+++ b/test/relay-checkpoint.test.js@@ -5,7 +5,7 @@ import { DatabaseSync } from 'node:sqlite';import { dirname, join } from 'node:path';import { test } from 'node:test';import { relayClientFixture } from './relay-client-fixture.js';-import { CHECKPOINT_BACKGROUND_ENV } from '../src/relay-checkpoint.js';+import { CHECKPOINT_BACKGROUND_ENV, CHECKPOINT_QUIET_ENV } from '../src/relay-checkpoint.js';/** Whether a relay call uploads a checkpoint: an API1 state write or an API2 page publish. */function checkpointUpload(call) {@@ -29,7 +29,12 @@ test('a move on a large board sends only its event and never the checkpoint [H5]// The runner keeps checkpoints inside commands, like the view; this case opts in to the background publisher.const inline = process.env[CHECKPOINT_BACKGROUND_ENV];delete process.env[CHECKPOINT_BACKGROUND_ENV];- t.after(() => { if (inline === undefined) delete process.env[CHECKPOINT_BACKGROUND_ENV]; else process.env[CHECKPOINT_BACKGROUND_ENV] = inline; });+ // A CLI child takes about a second to start, so a burst of them is modelled as moves under three seconds apart.+ process.env[CHECKPOINT_QUIET_ENV] = '3000';+ t.after(() => {+ if (inline === undefined) delete process.env[CHECKPOINT_BACKGROUND_ENV]; else process.env[CHECKPOINT_BACKGROUND_ENV] = inline;+ delete process.env[CHECKPOINT_QUIET_ENV];+ });const box = await relayClientFixture(t);// Seed the presentation's heaviest part, as on the 22 Oct board: 20,000 shouts.@@ -60,6 +65,34 @@ test('a move on a large board sends only its event and never the checkpoint [H5]assert.ok(elapsed < 1000, `move ${index} returned in ${Math.round(elapsed)} ms, under one second`);}await publisherDone(box, before, 1);- await new Promise(next => setTimeout(next, 2_000)); // budget: a second quiet period, so a second upload would show.+ await new Promise(next => setTimeout(next, 4_000)); // budget: a second quiet period, so a second upload would show.assert.equal(box.calls.slice(before).filter(checkpointUpload).length, 1, 'ten moves in a burst produced one checkpoint upload');});++test('a move during a held checkpoint upload returns before the upload ends [H5]', async (t) => {+ const inline = process.env[CHECKPOINT_BACKGROUND_ENV];+ delete process.env[CHECKPOINT_BACKGROUND_ENV];+ t.after(() => { if (inline === undefined) delete process.env[CHECKPOINT_BACKGROUND_ENV]; else process.env[CHECKPOINT_BACKGROUND_ENV] = inline; });+ const box = await relayClientFixture(t);+ await box.link();+ const warm = box.calls.length;+ assert.equal((await box.cli('shout', 'all', 'warm up')).code, 0);+ await publisherDone(box, warm, 1);++ // The relay holds the next checkpoint upload; a move meanwhile must not wait for it.+ box.holdPublishes(true);+ t.after(() => box.holdPublishes(false));+ const from = box.calls.length;+ assert.equal((await box.cli('shout', 'all', 'starts the held upload')).code, 0);+ const deadline = Date.now() + 15_000;+ while (box.heldPublishes() === 0 && Date.now() < deadline) await new Promise(next => setTimeout(next, 50));+ assert.equal(box.heldPublishes(), 1, 'the publisher\'s upload is in flight and held');+ const started = performance.now();+ const moved = await box.cli('shout', 'all', 'moves during the held upload');+ const elapsed = performance.now() - started;+ assert.equal(moved.code, 0, JSON.stringify(moved.document));+ assert.equal(box.heldPublishes(), 1, 'the move returned while the upload was still held');+ assert.ok(elapsed < 2000, `the move took ${Math.round(elapsed)} ms; it must not wait for the upload`);+ box.holdPublishes(false);+ await publisherDone(box, from, 2);+});diff --git a/test/relay-client-fixture.js b/test/relay-client-fixture.jsindex 83d77f56..7a23cef0 100644--- a/test/relay-client-fixture.js+++ b/test/relay-client-fixture.js@@ -238,6 +238,8 @@ globalThis.fetch = function tracedFetch(input, init) {let stateReadStarted = 0;let refuseSnapshotWrites = false;let stallApi = false;+ let holdPublishes = false;+ const heldPublishes = [];let loseSnapshotReply = null;const lostSnapshotReplies = [];let rejectSnapshotWrites = null;@@ -383,6 +385,10 @@ globalThis.fetch = function tracedFetch(input, init) {});calls.push(call);if (stallApi && (req.url ?? '').startsWith('/api/')) { request.resume(); await new Promise(() => {}); }+ // A held API2 checkpoint publish waits until released, so a test can act while an upload is in flight (#1014).+ if (holdPublishes && req.method === 'POST' && /^\/api\/v2\/boards\/[0-9a-f]{32}\/uploads\/[^/]+\/publish$/u.test(req.url ?? '')) {+ await new Promise(release => heldPublishes.push(release));+ }if (legacySnapshotApi && req.method === 'GET' && req.url === '/api/v2') {res.writeHead(legacySnapshotApi === 'bad-route' ? 400 : 200, { 'content-type': 'application/json' });res.end(JSON.stringify(legacySnapshotApi === 'bad-route'@@ -763,6 +769,13 @@ globalThis.fetch = function tracedFetch(input, init) {refuseSnapshotWrites(value) { refuseSnapshotWrites = value; },/** Leave every board API request unanswered until its client times out, like an overloaded relay. */stallApi(value = true) { stallApi = value; },+ /** Hold every API2 checkpoint publish until called with false, which releases the held ones. */+ holdPublishes(value = true) {+ holdPublishes = value;+ if (!value) for (const release of heldPublishes.splice(0)) release();+ },+ /** How many API2 checkpoint publishes are being held. */+ heldPublishes() { return heldPublishes.length; },/*** Let the real relay accept the next API1 snapshot PUT, then drop its reply by closing the socket,* optionally answering later state reads with a chosen refusal, as the #919 lost-ACK review did.