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(cli): read canary queue counts without transport [C7,N19]
Corey Olson8b34c597a988
diff --git a/src/relay-canary-needs.js b/src/relay-canary-needs.jsindex 34d42b76..e601a1ae 100644--- a/src/relay-canary-needs.js+++ b/src/relay-canary-needs.js@@ -1,10 +1,12 @@/** Project this checkout's active relay-canary alert into its local Needs-you list [H5,H15,C7]. */import { readRelayHealth } from './relay-canary-health.js';-import { relayStatus } from './relay.js';+import { readOutboxCount } from './relay-outbox-summary.js';+import { relayLinkFile } from './local-state.js';+import { repoInfo } from './git.js';/** Count only this local link's queued moves; status is read-only and never contacts the relay. */function queuedMoves(root) {- try { return relayStatus(root).outbox?.length ?? 0; }+ try { return readOutboxCount(relayLinkFile(repoInfo(root).commonDir)); }catch { return 0; }}diff --git a/src/relay-link-read.js b/src/relay-link-read.jsnew file mode 100644index 00000000..01328210--- /dev/null+++ b/src/relay-link-read.js@@ -0,0 +1,23 @@+/** Read validated local link metadata without transport, token migration or a board write [C7,H5]. */+import {+ existsSync,+ lstatSync,+ readFileSync,+} from 'node:fs';+import { Refused } from './refused.js';+import { relayOrigin } from './relay-origin.js';++/** Preserve the transport's private-file and metadata checks for lightweight local readers. */+export function readRelayLink(file) {+ if (!existsSync(file)) return null;+ const stat = lstatSync(file);+ if (!stat.isFile() || stat.isSymbolicLink() || (stat.mode & 0o077)) throw new Refused('RELAY_STORAGE', 'restore relay.json as an owner-only regular file with mode 600');+ let state;+ try { state = JSON.parse(readFileSync(file, 'utf8')); } catch { throw new Refused('RELAY_STORAGE', 'restore valid relay link metadata before syncing this board'); }+ if (!state || typeof state !== 'object' || state.version !== 1 || !/^[0-9a-f]{32}$/.test(state.board) || !(state.token === undefined || state.personReauth && state.token === null || /^(ps_|pa_|pm_)[A-Za-z0-9_-]{43}$/.test(state.token)) ||+ !Number.isSafeInteger(state.sequence) || state.sequence < 0 || !Number.isSafeInteger(state.cursor) || state.cursor < 0) {+ throw new Refused('RELAY_STORAGE', 'restore supported relay link metadata before syncing this board');+ }+ relayOrigin(state.url);+ return state;+}diff --git a/src/relay-outbox-summary.js b/src/relay-outbox-summary.jsnew file mode 100644index 00000000..266d628d--- /dev/null+++ b/src/relay-outbox-summary.js@@ -0,0 +1,38 @@+/** Key-free local outbox reads shared by transport and offline status [C7,H5]. */+import {+ existsSync,+ readFileSync,+} from 'node:fs';+import {+ dirname,+ join,+} from 'node:path';+import { readRelayLink } from './relay-link-read.js';++/** The sealed outbox beside a link file. */+export function outboxFile(linkFile) {+ return join(dirname(linkFile), 'relay-outbox.sealed');+}++/** The key-free summary of the outbox beside a link file. */+export function outboxSummaryFile(linkFile) {+ return join(dirname(linkFile), 'relay-outbox.summary.json');+}++/** Read the independently saved summary, or null when it contains no queued moves. */+export function readOutboxSummary(linkFile) {+ try {+ const summary = JSON.parse(readFileSync(outboxSummaryFile(linkFile), 'utf8'));+ return Array.isArray(summary?.moves) && summary.moves.length ? summary : null;+ } catch { return null; }+}++/** Match link loading's summary precedence and legacy sealed-outbox fallback without opening the board. */+export function readOutboxCount(linkFile) {+ const state = readRelayLink(linkFile);+ if (!state || state.unlinking) return 0;+ const waiting = readOutboxSummary(linkFile);+ if (!waiting && !existsSync(outboxFile(linkFile))) return 0;+ const moves = (waiting ?? state.outbox)?.moves;+ return Array.isArray(moves) ? moves.length : 0;+}diff --git a/src/relay-outbox.js b/src/relay-outbox.jsindex 4e43eaba..81e23411 100644--- a/src/relay-outbox.js+++ b/src/relay-outbox.js@@ -50,6 +50,16 @@ import { captureBoardChanges } from './board-capture.js';import { undoQueued } from './relay-outbox-undo.js';import { stampCoordinatorHolder } from './coordinator-hold.js';import { Refused } from './refused.js';+import {+ outboxFile,+ outboxSummaryFile,+ readOutboxSummary,+} from './relay-outbox-summary.js';+export {+ outboxFile,+ outboxSummaryFile,+ readOutboxSummary,+};import {createdItemAt,createsItems,@@ -63,11 +73,6 @@ const KEPT_REFUSALS = 20;/** Commands that already said their move waits in the outbox, so a command says it once. */const OUTBOX_NOTICES = new WeakSet();-/** The sealed outbox beside a link file. */-export function outboxFile(linkFile) {- return join(dirname(linkFile), 'relay-outbox.sealed');-}-/** Whether a link holds moves the relay has not ordered yet, while the local board still shows them applied. */export function outboxAhead(state) {return Boolean(state?.outbox?.moves?.length) && state.outbox.restored !== true;@@ -337,23 +342,6 @@ export function outboxQueueable(state, error, operation, io, args = []) {&& !args.some(arg => arg?.landingFence !== undefined) && transientRelayFailure(error) && outboxEnabled(io);}-/** The key-free summary of the outbox beside a link file, which pullboard status reads (#1013). */-export function outboxSummaryFile(linkFile) {- return join(dirname(linkFile), 'relay-outbox.summary.json');-}--/**- * Read the key-free outbox summary, or null when no move waits.- * Why: a move queues itself without the relay lock, so it never writes relay.json, which the sender owns; the- * summary lives in its own file, and the link reads it from there (#1013).- */-export function readOutboxSummary(linkFile) {- try {- const summary = JSON.parse(readFileSync(outboxSummaryFile(linkFile), 'utf8'));- return Array.isArray(summary?.moves) && summary.moves.length ? summary : null;- } catch { return null; }-}-/** Keep the key-free outbox summary beside the link, or drop it once the outbox is empty. */function saveOutboxSummary({ file, state }, outbox) {const where = outboxSummaryFile(file);diff --git a/src/relay.js b/src/relay.jsindex 6c48e347..301cebd6 100644--- a/src/relay.js+++ b/src/relay.js@@ -164,6 +164,7 @@ import {import { Refused } from './refused.js';import { landingRelayCallbacks } from './landing-relay.js';import { relayOrigin } from './relay-origin.js';+import { readRelayLink } from './relay-link-read.js';import { refusalFromReceipt } from './recorded-refusal.js';import { relayNotices } from './relay-notices.js';import {@@ -342,16 +343,8 @@ function forgetDeletedCopy(root, file, state, io) {/** Validate private link metadata before trusting its destination or acknowledgement cursor. */function loadLink(file) {- if (!existsSync(file)) return null;- const stat = lstatSync(file);- if (!stat.isFile() || stat.isSymbolicLink() || (stat.mode & 0o077)) throw new Refused('RELAY_STORAGE', 'restore relay.json as an owner-only regular file with mode 600');- let state;- try { state = JSON.parse(readFileSync(file, 'utf8')); } catch { throw new Refused('RELAY_STORAGE', 'restore valid relay link metadata before syncing this board'); }- if (!state || typeof state !== 'object' || state.version !== 1 || !/^[0-9a-f]{32}$/.test(state.board) || !(state.token === undefined || state.personReauth && state.token === null || /^(ps_|pa_|pm_)[A-Za-z0-9_-]{43}$/.test(state.token)) ||- !Number.isSafeInteger(state.sequence) || state.sequence < 0 || !Number.isSafeInteger(state.cursor) || state.cursor < 0) {- throw new Refused('RELAY_STORAGE', 'restore supported relay link metadata before syncing this board');- }- relayOrigin(state.url);+ const state = readRelayLink(file);+ if (!state) return null;if (state.token === undefined || state.token?.startsWith('ps_')) {state.token = null;delete state.tokenId;diff --git a/test/relay-canary-needs.test.js b/test/relay-canary-needs.test.jsnew file mode 100644index 00000000..84480751--- /dev/null+++ b/test/relay-canary-needs.test.js@@ -0,0 +1,94 @@+/** Offline canary needs retain local queue evidence without loading the relay writer [C7,H5]. */+import assert from 'node:assert/strict';+import { chmodSync, existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import test from 'node:test';+import { relayCanaryNeeds } from '../src/relay-canary-needs.js';+import { observeRelayHealth } from '../src/relay-canary-health.js';+import { outboxFile, outboxSummaryFile, readOutboxCount } from '../src/relay-outbox-summary.js';+import { readRelayLink } from '../src/relay-link-read.js';+import { runFixtureGit } from './fixture-child.js';++/** A real private Git repository with a persisted two-probe alert and no transport or native board. */+function fixture(t) {+ const root = mkdtempSync(join(tmpdir(), 'pullboard-canary-needs-'));+ t.after(() => rmSync(root, { recursive: true, force: true }));+ const env = { ...process.env, GIT_CONFIG_GLOBAL: '/dev/null', GIT_CONFIG_NOSYSTEM: '1',+ GIT_AUTHOR_NAME: 'Canary fixture', GIT_AUTHOR_EMAIL: '[email protected]',+ GIT_COMMITTER_NAME: 'Canary fixture', GIT_COMMITTER_EMAIL: '[email protected]' };+ runFixtureGit(['init', '-q', '-b', 'main'], { cwd: root, env });+ runFixtureGit(['commit', '-q', '--allow-empty', '-m', 'chore: private canary fixture'], { cwd: root, env });+ const failed = { ok: false, step: 'pull', code: 'RELAY_READ', roundTripMs: 2 };+ observeRelayHealth(root, failed);+ assert.equal(observeRelayHealth(root, failed).transition, 'alert');+ const file = join(root, '.git', 'pullboard', 'relay.json');+ /** Save local metadata without a credential, so reading cannot silently migrate it. */+ function link(overrides = {}) {+ const state = { version: 1, board: 'a'.repeat(32), url: 'https://example.invalid', sequence: 0, cursor: 0,+ outbox: { moves: [{ id: 'legacy' }] }, ...overrides };+ writeFileSync(file, JSON.stringify(state), { mode: 0o600 });+ return state;+ }+ return { root, file, link };+}++/** Assert the actual Needs-you projection and preserve all supplied local evidence byte for byte. */+function assertQueued(box, count, label = 'the local queued-move count stays accurate') {+ const files = [box.file, outboxSummaryFile(box.file), outboxFile(box.file),+ join(box.root, '.git', 'pullboard', 'relay-health.sqlite')];+ const before = files.map(file => existsSync(file) ? readFileSync(file) : null);+ const needs = relayCanaryNeeds(box.root);+ assert.equal(needs.length, 1, 'the persisted local alert remains visible');+ assert.equal(needs[0].queued, count, label);+ assert.match(needs[0].text, new RegExp(`queued locally \\(${count}\\)$`, 'u'));+ for (const [index, file] of files.entries()) {+ assert.deepEqual(existsSync(file) ? readFileSync(file) : null, before[index], 'a count read preserves local evidence');+ }+}++test('offline canary needs retain alerts and do not create a relay link or board [C7,H5]', t => {+ const box = fixture(t);+ assertQueued(box, 0);+ assert.equal(existsSync(join(box.root, '.git', 'pullboard', 'board.sqlite')), false);+});++test('canary queue counts prefer the modern summary and retain the sealed legacy fallback [C7,H5]', t => {+ const box = fixture(t);+ box.link();+ writeFileSync(outboxFile(box.file), 'opaque legacy fixture; count reads never unseal it');+ assertQueued(box, 1);+ writeFileSync(outboxSummaryFile(box.file), JSON.stringify({ moves: [{ id: 'first' }, { id: 'second' }] }));+ assertQueued(box, 2);+ writeFileSync(outboxSummaryFile(box.file), '{broken');+ assertQueued(box, 1);+ writeFileSync(outboxSummaryFile(box.file), JSON.stringify({ moves: [] }));+ assertQueued(box, 1);+ rmSync(outboxFile(box.file));+ assertQueued(box, 0);+ writeFileSync(outboxSummaryFile(box.file), JSON.stringify({ moves: [{ id: 'independent-summary' }] }));+ assertQueued(box, 1, 'the independently saved summary does not require the sealed file');+ box.link({ unlinking: true });+ assertQueued(box, 0);+ rmSync(box.file);+ assertQueued(box, 0, 'an orphaned summary is not a linked queue');+});++test('canary count reads preserve private-file and link validation without migrating metadata [C7,H5]', t => {+ const box = fixture(t);+ const state = box.link();+ assert.deepEqual(readRelayLink(box.file), state);+ writeFileSync(outboxSummaryFile(box.file), JSON.stringify({ moves: [{ id: 'queued' }] }));+ assert.equal(readOutboxCount(box.file), 1);+ assertQueued(box, 1);+ chmodSync(box.file, 0o644);+ assert.throws(() => readRelayLink(box.file), /RELAY_STORAGE/u);+ assertQueued(box, 0);+ chmodSync(box.file, 0o600);+ box.link({ cursor: -1 });+ assert.throws(() => readRelayLink(box.file), /RELAY_STORAGE/u);+ assertQueued(box, 0);+ box.link({ url: 'https://example.invalid/not-an-origin' });+ assert.throws(() => readRelayLink(box.file), /RELAY_URL/u);+ assertQueued(box, 0);+});