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
test(relay): keep one sender through a slow multi-board pass [H21,H5]
Corey Olsoned28b9bc5c7e
diff --git a/src/relay-local-first.js b/src/relay-local-first.jsindex 6ad64739..4ffc2104 100644--- a/src/relay-local-first.js+++ b/src/relay-local-first.js@@ -50,7 +50,7 @@ export function senderFiles() {// Budget: a sender refreshes its claim every few seconds while it runs; a claim this old belongs to a process that// died, whose number may since have been reused.-const CLAIM_STALE_MS = 120_000;+export const SENDER_CLAIM_STALE_MS = 120_000;/** Whether a process is alive; one another user owns still is. */export function alive(pid) {@@ -67,7 +67,7 @@ export function alive(pid) {export function senderHolding() {const { pid } = senderFiles();try {- if (Date.now() - statSync(pid).mtimeMs > CLAIM_STALE_MS) return 0;+ if (Date.now() - statSync(pid).mtimeMs > SENDER_CLAIM_STALE_MS) return 0;const holder = Number(readFileSync(pid, 'utf8'));return alive(holder) ? holder : 0;} catch { return 0; }diff --git a/test/relay-sender-heartbeat.test.js b/test/relay-sender-heartbeat.test.jsnew file mode 100644index 00000000..405deadf--- /dev/null+++ b/test/relay-sender-heartbeat.test.js@@ -0,0 +1,212 @@+// @pullboard-test-loads src/relay.js src/cli.js src/relay-sender-worker.js+/** A slow real multi-board pass must not make a live sender look abandoned [H21,H5,H16]. */+import assert from 'node:assert/strict';+import { execFileSync } from 'node:child_process';+import { createHash } from 'node:crypto';+import { existsSync, mkdirSync, readFileSync, readlinkSync, statSync, writeFileSync } from 'node:fs';+import { hostname } from 'node:os';+import { join } from 'node:path';+import { performance } from 'node:perf_hooks';+import { test } from 'node:test';+import { fileURLToPath } from 'node:url';+import { SENDER_CLAIM_STALE_MS, SENDER_WORKER } from '../src/relay-local-first.js';+import { outboxFile } from '../src/relay-outbox.js';+import { runFixtureChildAsync, runFixtureGit } from './fixture-child.js';+import { relayClientFixture } from './relay-client-fixture.js';++const CLI = fileURLToPath(new URL('../bin/pullboard.js', import.meta.url));+const LOCAL_FIRST = new URL('../src/relay-local-first.js', import.meta.url).href;+const RELAY = new URL('../src/relay.js', import.meta.url).href;+const POLICY = new URL('../src/cli.js', import.meta.url).href;+// Budget: each real reply waits below the unchanged production ten-second per-request deadline.+const RESPONSE_HOLD_MS = 5000;+const MOVES = 26; // Twenty-six genuine queued moves keep one flush awaiting I/O beyond the real two-minute claim window.++/** Wait for an actual lifecycle barrier, with a finite failure ceiling rather than a latency assertion. */+async function until(description, ready, ceilingMs = 10_000) {+ const deadline = performance.now() + ceilingMs;+ while (performance.now() < deadline) {+ if (ready()) return;+ await new Promise(resolve => setTimeout(resolve, 20));+ }+ assert.fail(description);+}++/** Preserve the committed absent-setting default before linking either genuine private fixture board. */+function commitDefault(root, env) {+ const file = join(root, 'pullboard.json');+ const config = JSON.parse(readFileSync(file, 'utf8'));+ if (config.relay) delete config.relay.outbox;+ writeFileSync(file, JSON.stringify(config, null, 2) + '\n');+ writeFileSync(join(env.PATH, 'pullboard'), `#!/bin/sh\nexec node "${CLI}" "$@"\n`, { mode: 0o700 });+ runFixtureGit(['add', 'pullboard.json'], { cwd: root, env });+ runFixtureGit(['commit', '-q', '-m', 'chore(config): keep the production sender default'], { cwd: root, env });+}++/** Run the real CLI during unlinked setup without enabling the sender before both boards are ready. */+async function cli(root, env, args) {+ const result = await runFixtureChildAsync(process.execPath, [CLI, ...args, '--json'], { cwd: root, env });+ assert.equal(result.status, 0, result.context);+ return JSON.parse(result.stdout);+}++/** Initialize a second genuine repository against the same account and relay, sharing exactly one machine home. */+async function secondBoard(box) {+ const root = join(box.root, '..', 'second-board');+ mkdirSync(root);+ runFixtureGit(['init', '-q', '-b', 'main'], { cwd: root, env: box.env });+ writeFileSync(join(root, 'README.md'), 'private second sender fixture\n');+ runFixtureGit(['add', 'README.md'], { cwd: root, env: box.env });+ runFixtureGit(['commit', '-q', '-m', 'fixture base'], { cwd: root, env: box.env });+ await cli(root, box.env, ['init']);+ commitDefault(root, box.env);+ const repository = 'sender-second-repository';+ box.providerState.repositories.push(repository);+ runFixtureGit(['remote', 'add', 'origin', `[email protected]:fixture/${repository}.git`], { cwd: root, env: box.env });+ await cli(root, box.env, ['relay', 'on', '--url', box.origin]);+ const file = join(root, '.git/pullboard/relay.json');+ const state = JSON.parse(readFileSync(file, 'utf8'));+ assert.equal(state.baselineAccepted, true);+ return { root, file, id: state.board, env: box.env };+}++/** Capture only a freshly spawned fixture worker's start, command and cwd; never enumerate another process. */+function workerIdentity(pid, root) {+ let metadata;+ try { metadata = execFileSync('ps', ['-p', String(pid), '-o', 'lstart=,command='], { encoding: 'utf8', stdio: ['ignore', 'pipe', 'ignore'] }).trim(); }+ catch { return null; }+ assert.ok(metadata.includes(SENDER_WORKER), 'the recorded fixture PID still runs the expected sender entry');+ const cwd = process.platform === 'linux' ? readlinkSync(`/proc/${pid}/cwd`)+ : execFileSync('lsof', ['-a', '-p', String(pid), '-d', 'cwd', '-Fn'], { encoding: 'utf8' }).split('\n').find(line => line.startsWith('n'))?.slice(1);+ assert.equal(cwd, root, 'only this freshly spawned private worker belongs to the test');+ return { pid, metadata, cwd };+}++/** Recheck exact PID/start/cwd immediately before terminating only this test's recorded sender. */+async function stopWorker(record) {+ const current = workerIdentity(record.pid, record.cwd);+ if (!current) return;+ assert.ok(current.metadata === record.metadata, 'a reused PID is never signalled');+ const checked = workerIdentity(record.pid, record.cwd);+ assert.ok(checked?.metadata === record.metadata, 'the exact identity is unchanged immediately before signalling');+ process.kill(record.pid, 'SIGTERM');+ await until('the exact fixture sender did not terminate', () => !workerIdentity(record.pid, record.cwd));+}++/** Nudge once through the public helper, passively recording only the exact real child handles it creates. */+async function nudge(root, file, env, workers) {+ const source = `+ import childProcess from 'node:child_process';+ import { syncBuiltinESMExports } from 'node:module';+ const original = childProcess.spawn, spawned = [];+ childProcess.spawn = function (...args) {+ const child = original.apply(this, args);+ if (args[0] === process.execPath && args[1]?.[0] === ${JSON.stringify(SENDER_WORKER)}) spawned.push(child.pid);+ return child;+ };+ syncBuiltinESMExports();+ const { nudgeRelaySender } = await import(${JSON.stringify(LOCAL_FIRST)});+ nudgeRelaySender(process.cwd(), ${JSON.stringify(file)});+ process.stdout.write(JSON.stringify({ spawned, home: process.env.PULLBOARD_HOME }));+ `;+ const result = await runFixtureChildAsync(process.execPath, ['--input-type=module', '-e', source], { cwd: root, env });+ assert.equal(result.status, 0, result.context);+ const output = JSON.parse(result.stdout);+ assert.equal(output.home, env.PULLBOARD_HOME);+ for (const pid of output.spawned) {+ const record = workerIdentity(pid, root);+ assert.ok(record, 'the real spawned worker is alive for exact cleanup registration');+ workers.push(record);+ }+ return output.spawned;+}++/** Queue genuine ordinary native moves with saved credentials, before any background sender is started. */+async function queueMoves(box) {+ const source = `+ const { relayOperation } = await import(${JSON.stringify(RELAY)});+ const { relayOutboxSetting } = await import(${JSON.stringify(POLICY)});+ const io = { err() {}, relayOutbox: () => relayOutboxSetting(process.cwd()) };+ for (let index = 0; index < ${MOVES}; index++) await relayOperation(process.cwd(), 'shout',+ [{ from: 'coordinator', to: 'all', lanes: [], text: 'Slow real sender move ' + index }], io);+ process.stdout.write(JSON.stringify({ queued: ${MOVES} }));+ `;+ const result = await runFixtureChildAsync(process.execPath, ['--input-type=module', '-e', source], { cwd: box.root, env: box.env });+ assert.equal(result.status, 0, result.context);+ assert.equal(JSON.parse(result.stdout).queued, MOVES);+ assert.equal(existsSync(outboxFile(box.linkFile)), true);+}++test('one sender keeps its claim through a slow multi-board pass [H21,H5,H16]', { timeout: 660_000 }, async t => { // budget: one real two-minute stale window, under-deadline replies, and bounded fixture cleanup.+ t.diagnostic(JSON.stringify({ node: process.version, host: hostname(), execPath: process.execPath,+ binarySha256: createHash('sha256').update(readFileSync(process.execPath)).digest('hex') }));+ const workers = [], pending = new Set();+ let slowing = true;+ /** Release all real response barriers before any owned worker or fixture is cleaned up. */+ const releaseAll = () => { slowing = false; for (const release of [...pending]) release(); };+ t.after(async () => {+ releaseAll();+ const stopped = await Promise.allSettled(workers.map(stopWorker));+ const errors = stopped.filter(result => result.status === 'rejected').map(result => result.reason);+ if (errors.length) throw new AggregateError(errors, 'scoped fixture sender cleanup failed');+ });+ const box = await relayClientFixture(t);+ commitDefault(box.root, box.env);+ await box.link();+ const other = await secondBoard(box);+ assert.notEqual(JSON.parse(readFileSync(box.linkFile, 'utf8')).board, other.id);+ assert.equal(other.env.PULLBOARD_HOME, box.env.PULLBOARD_HOME);+ assert.equal(other.env.PULLBOARD_MACHINE_HOME, box.env.PULLBOARD_MACHINE_HOME);+ await queueMoves(box);+ let firstResponse;+ const reached = new Promise(resolve => { firstResponse = resolve; });+ let responseIndex = 0, firstAt = 0;+ /** Hold each genuine move response below its unchanged ten-second transport deadline. */+ async function slowResponse() {+ if (!slowing) return;+ box.beforeNextMove(slowResponse);+ responseIndex++;+ if (!firstAt) { firstAt = performance.now(); firstResponse(); }+ await new Promise(resolve => {+ let timer;+ const release = () => { clearTimeout(timer); pending.delete(release); resolve(); };+ pending.add(release);+ timer = setTimeout(release, RESPONSE_HOLD_MS);+ });+ }+ box.beforeNextMove(slowResponse);+ const env = { ...box.env };+ delete env.PULLBOARD_RELAY_SENDER_BACKGROUND;+ assert.equal(env.PULLBOARD_RELAY_TOKEN, undefined);+ const claim = join(env.PULLBOARD_HOME, 'relay-sender/sender.pid');+ try {+ assert.equal((await nudge(box.root, box.linkFile, env, workers)).length, 1);+ await until('the sender never reached its first real relay response', () => firstAt > 0, 20_000);+ await reached;+ await until('the first sender did not claim its machine home', () => existsSync(claim));+ const first = workers[0];+ assert.equal(Number(readFileSync(claim, 'utf8')), first.pid);+ const wait = firstAt + SENDER_CLAIM_STALE_MS + 500 - performance.now();+ if (wait > 0) await new Promise(resolve => setTimeout(resolve, wait)); // Real elapsed time is the tested stale window, never a fake timestamp.+ assert.ok(performance.now() - firstAt > SENDER_CLAIM_STALE_MS);+ assert.ok(pending.size > 0 && responseIndex <= MOVES, 'the first worker is still awaiting a held real relay response in this same flush');+ assert.equal(existsSync(outboxFile(box.linkFile)), true, 'the same genuine first-board flush still has queued work');+ assert.doesNotMatch(readFileSync(join(env.PULLBOARD_HOME, 'relay-sender/sender.log'), 'utf8'), /served in/u,+ 'the original serve has not completed or timed out and restarted');+ assert.ok(workerIdentity(first.pid, first.cwd)?.metadata === first.metadata, 'the original worker is alive with its original start and cwd');+ const claimAgeMs = Date.now() - statSync(claim).mtimeMs;+ const secondReads = () => box.calls.filter(call => call.method === 'GET' && call.path.includes('/boards/' + other.id + '/')).length;+ const readsBefore = secondReads();+ const created = await nudge(other.root, other.file, env, workers);+ if (created.length) await until('the replacement worker did not expose its claim', () => Number(readFileSync(claim, 'utf8')) === created[0]);+ assert.ok(workerIdentity(first.pid, first.cwd)?.metadata === first.metadata, 'a replacement cannot be justified by the first worker exiting');+ t.diagnostic(JSON.stringify({ realHeldMs: performance.now() - firstAt, claimAgeMs, responseIndex,+ workers: workers.map(({ pid, metadata, cwd }) => ({ pid, startAndCommand: metadata, cwd })) }));+ assert.equal(created.length, 0, 'a slow pass cannot spawn another live sender for the same machine home');+ assert.equal(Number(readFileSync(claim, 'utf8')), first.pid, 'the original sender retains the claim');+ releaseAll();+ await until('the first board outbox did not drain after releasing real responses', () => !existsSync(outboxFile(box.linkFile)), 30_000);+ await until('the same sender did not serve the second board', () => secondReads() > readsBefore, 30_000);+ assert.ok(workerIdentity(first.pid, first.cwd)?.metadata === first.metadata, 'one original sender finishes both boards');+ } finally { releaseAll(); }+});