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.

fix(relay): refuse revoked seats before local queuing [H2,H9,H21]

Corey Olson6ba31daac3cb
diff --git a/src/relay-outbox.js b/src/relay-outbox.js
index 4e43eaba..b2174aaa 100644
--- a/src/relay-outbox.js
+++ b/src/relay-outbox.js
@@ -34,6 +34,7 @@ import { leaveConflictNotice } from './relay-conflicts.js';
import { withOutboxGuard } from './relay-outbox-guard.js';
import { transientRelayFailure } from './relay-move-fallback.js';
import {
+ refuseRevokedSeat,
seatReceipt,
withSeatName,
} from './relay-seats.js';
@@ -416,6 +417,7 @@ export async function queueMove(link, { operation, args, command, move, error, a
move = stampCoordinatorHolder(localRecords(root, (board) => prepareEngineMove(board, operation, sent)), state.machine, state.computerName);
move.actor = relayMoveActor(move) ?? credentialActor(move);
}
+ refuseRevokedSeat(state, credentialActor(move));
const stray = strayItemRefs(outbox, move);
if (stray.length) {
throw new Refused('RELAY_OUTBOX_UNBOUND', `this move names #${stray[0]}, which this machine added while the relay was down, in a field the outbox cannot follow if the relay renumbers it; run it again once the relay answers`);
diff --git a/src/relay-seats.js b/src/relay-seats.js
index afa73c44..0cdec8f1 100644
--- a/src/relay-seats.js
+++ b/src/relay-seats.js
@@ -71,6 +71,11 @@ function renewalDue(cached) {
? cached.expires - (cached.expires - cached.created) / 2 : cached.expires - 60_000;
}
+/** Refuse a seat whose locally recorded revocation also forbids provisional offline moves [H2,H9,H21]. */
+export function refuseRevokedSeat(state, actor) {
+ if (state.agentTokens?.[actor]?.revoked) throw new Refused('TOKEN_REVOKED', `the relay token of ${actor} was revoked on this board; work from a seat with its own token, or ask the coordinator for a fresh worktree`);
+}
+
/**
* The current token of one registered seat (#579). A live token renews itself with itself; a lost
* or expired one is reissued by the seat that enrolled it, walking up to the coordinator, whose
@@ -80,7 +85,7 @@ function renewalDue(cached) {
async function seatToken(root, file, state, actor, io, depth = 0) {
if (depth > 64) throw new Refused('RELAY_ACTOR', 'this seat\'s line of creators loops; ask the coordinator to make a fresh seat');
const cached = state.agentTokens?.[actor];
- if (cached?.revoked) throw new Refused('TOKEN_REVOKED', `the relay token of ${actor} was revoked on this board; work from a seat with its own token, or ask the coordinator for a fresh worktree`);
+ refuseRevokedSeat(state, actor);
if (cached?.token && cached.expires > Date.now()) {
if (Date.now() < renewalDue(cached)) return cached.token;
const renewed = await request({ ...state, token: cached.token, agent: actor }, '/auth/tokens', { method: 'POST', body: { board: state.board, agent: actor } }, io);
diff --git a/src/relay.js b/src/relay.js
index 6c48e347..21352320 100644
--- a/src/relay.js
+++ b/src/relay.js
@@ -1051,14 +1051,18 @@ function localFirstLink(file, operation, args, io) {
* Make a move on this machine at once and queue it for the relay (#1013): no network call and no relay lock, only
* the short outbox guard. The background sender sends it and rebuilds the board once the relay has ordered it.
*/
-async function queueLocally(root, file, state, operation, args, io, command, applyLocally, onRecorded) {
+async function queueLocally(root, file, operation, args, io, command, applyLocally, onRecorded) {
const guarded = landingRelayCallbacks(operation, args, {
apply: () => typeof applyLocally === 'function' ? applyLocally() : localRecords(root, (board) => store[operation](board, ...args)),
queue: () => { throw new Refused('RELAY_OUTBOX', 'a local-first move queues itself; it is never queued twice'); },
});
// An unlink that finished while this move waited for the guard leaves an ordinary local board.
- const result = await withOutboxGuard(file, () => !existsSync(file) ? guarded.apply() : queueMove(outboxLink(root, file, state, io),
- { operation, args, command, applyLocally: guarded.apply, quiet: true }));
+ const result = await withOutboxGuard(file, () => {
+ // A revoke can finish while this move waits: admission uses the cache protected by this guard, never its earlier snapshot.
+ const current = loadLink(file);
+ return !current ? guarded.apply() : queueMove(outboxLink(root, file, current, io),
+ { operation, args, command, applyLocally: guarded.apply, quiet: true });
+ });
onRecorded?.();
nudgeRelaySender(root, file);
return result;
@@ -1069,7 +1073,7 @@ export async function relayOperation(root, operation, args, io, command, applyLo
const roundTripStarted = Date.now();
const file = linkFile(root);
const local = localFirstLink(file, operation, args, io);
- if (local) return queueLocally(root, file, local, operation, args, io, command, applyLocally, onRecorded);
+ if (local) return queueLocally(root, file, operation, args, io, command, applyLocally, onRecorded);
return locked(file, async () => {
const state = loadLink(file);
if (operation === 'rehomeItem') await requireAuthoritativeRehomeItem(file, state, args?.[0]);
@@ -1311,8 +1315,12 @@ export async function relayRevoke(root, id, io) {
}
const result = await nativePhoneAction(state, 'revoke-token', id, io, request);
if (result.id !== id || result.revoked !== true) throw new Refused('RELAY_RESPONSE', 'the relay did not acknowledge this token revocation; retry its opaque id');
- for (const credential of Object.values(state.agentTokens ?? {})) if (credential.id === id) credential.revoked = true;
- saveLink(file, state);
+ // Queue admission and the local revocation marker share one short guard; the phone and network never hold it.
+ await withOutboxGuard(file, () => {
+ const current = loadLink(file);
+ for (const credential of Object.values(current.agentTokens ?? {})) if (credential.id === id) credential.revoked = true;
+ saveLink(file, current);
+ });
return { ...tokenContext(state), id: result.id, revoked: result.revoked };
});
}
diff --git a/test/relay-local-first.test.js b/test/relay-local-first.test.js
index b93b3fd7..4d322461 100644
--- a/test/relay-local-first.test.js
+++ b/test/relay-local-first.test.js
@@ -185,6 +185,37 @@ test('a move and a read return within a second with no network call while the se
await eventually('the sender sends the queued move once the relay lock is free', () => outboxEmpty(box) && box.moveAcks.length > acks, () => senderLog(env));
});
+test('a locally known revoked seat cannot queue or apply a move and retains earlier valid outbox history [H2,H9,H21]', async t => {
+ const { box, env } = await linkedBoard(t);
+ const lock = box.linkFile + '.lock';
+ mkdirSync(lock, { mode: 0o700 });
+ writeFileSync(join(lock, 'pid'), String(process.pid), { mode: 0o600 });
+ t.after(() => rmSync(lock, { recursive: true, force: true }));
+ const text = 'A valid move queued before this seat was revoked';
+ await local(box, t, box.root, env, 'before-local-revocation', ['shout', 'all', text]);
+ const exported = await local(box, t, box.root, env, 'before-revocation-export', ['export']);
+ const actor = exported.document.tables.shout.find(row => row.shout_text === text).shout_from;
+ const state = JSON.parse(readFileSync(box.linkFile, 'utf8'));
+ // The real phone-approved revocation flow is covered in relay-agent; here its persisted marker isolates offline admission.
+ state.agentTokens ??= {};
+ state.agentTokens[actor] = { ...state.agentTokens[actor], revoked: true };
+ writeFileSync(box.linkFile, JSON.stringify(state) + '\n', { mode: 0o600 });
+ const sealed = join(dirname(box.linkFile), 'relay-outbox.sealed');
+ const generation = join(dirname(box.linkFile), 'relay-outbox.generation');
+ const beforeOutbox = readFileSync(sealed);
+ const beforeGeneration = readFileSync(generation, 'utf8');
+ const calls = box.calls.length;
+ const refused = await runFixtureChildAsync(process.execPath,
+ [CLI, 'shout', 'all', 'A revoked seat must not create this move', '--json'], { cwd: box.root, env });
+ assert.equal(refused.status, 1, refused.context);
+ assert.equal(JSON.parse(refused.stdout).error.code, 'TOKEN_REVOKED');
+ assert.equal(box.calls.length, calls, 'known revocation is refused without network or token minting');
+ assert.equal(readFileSync(sealed).equals(beforeOutbox), true, 'earlier valid queued moves remain byte-identical');
+ assert.equal(readFileSync(generation, 'utf8'), beforeGeneration, 'a refused move never advances the outbox generation');
+ const after = await local(box, t, box.root, env, 'after-revocation-export', ['export']);
+ assert.deepEqual(after.document.tables, exported.document.tables, 'the refused move changes no native board row');
+});
+
test('a seat registration waits for relay authority while ordinary moves and reads stay local [H21,H5,H16]', async t => {
const { box, env } = await linkedBoard(t);
const agentPath = join(dirname(box.root), 'local-first-registration-agent');