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(core): bound hung tests by name [C7,V10]

Corey Olson4ce6bcedf984
diff --git a/bin/run-tests.js b/bin/run-tests.js
index dbe5482d..86b10e1e 100644
--- a/bin/run-tests.js
+++ b/bin/run-tests.js
@@ -7,9 +7,79 @@ import { delimiter, join, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { execFileSync, spawnSync } from 'node:child_process';
import { tmpdir } from 'node:os';
+import { after, afterEach, beforeEach } from 'node:test';
+import { Worker } from 'node:worker_threads';
import { AGENT_SHELL_MARKERS, SSH_SHELL_MARKERS } from '../src/person.js';
const runner = fileURLToPath(import.meta.url);
+export const DEFAULT_TEST_TIMEOUT_MS = 660_000;
+
+const watchdogSource = `
+const { parentPort } = require('node:worker_threads');
+const { writeSync } = require('node:fs');
+const timers = new Map();
+parentPort.on('message', (message) => {
+ if (message.type === 'stop') {
+ const active = timers.get(message.id);
+ if (active) clearTimeout(active.timer);
+ timers.delete(message.id);
+ return;
+ }
+ if (message.type !== 'start') return;
+ const startedAt = Date.now();
+ const timer = setTimeout(() => {
+ const elapsedMs = Date.now() - startedAt;
+ writeSync(2, 'run-tests: test "' + message.name + '" in ' + message.file +
+ ' failed: it ran ' + (elapsedMs / 1000).toFixed(1) + 's, past the ' +
+ message.timeoutMs + 'ms per-test timeout\\n');
+ try { process.kill(message.pid, 'SIGKILL'); } catch { /* The timed-out test process already exited. */ }
+ }, message.timeoutMs);
+ timers.set(message.id, { timer });
+});
+`;
+
+/**
+ * Read the per-test timeout and reject malformed overrides before tests start.
+ *
+ * @param {string | undefined} value
+ * @returns {number}
+ */
+function testTimeoutMs(value = process.env.PULLBOARD_TEST_TIMEOUT_MS) {
+ if (value === undefined) return DEFAULT_TEST_TIMEOUT_MS;
+ if (!/^[1-9][0-9]*$/u.test(value) || !Number.isSafeInteger(Number(value)) || Number(value) > 2_147_483_647) {
+ throw new Error('PULLBOARD_TEST_TIMEOUT_MS must be a positive integer no greater than 2147483647 milliseconds');
+ }
+ return Number(value);
+}
+
+/**
+ * Keep an independent watchdog for every active test in this test-file process.
+ *
+ * @returns {void}
+ */
+function installTestWatchdog() {
+ const timeoutMs = testTimeoutMs();
+ const file = process.env.PULLBOARD_TEST_FILE ?? process.argv[1] ?? '<unknown test file>';
+ const watchdog = new Worker(watchdogSource, { eval: true });
+ const activeTests = new WeakMap();
+ let nextTestId = 0;
+
+ beforeEach((context) => {
+ const id = `${process.pid}:${++nextTestId}`;
+ const name = String(context.name).replace(/[\r\n\t\u0000-\u001f]/gu, ' ').slice(0, 200);
+ activeTests.set(context, id);
+ watchdog.postMessage({ type: 'start', id, name, file, timeoutMs, pid: process.pid });
+ });
+
+ afterEach((context) => {
+ const id = activeTests.get(context);
+ if (id !== undefined) watchdog.postMessage({ type: 'stop', id });
+ });
+
+ after(async () => {
+ await watchdog.terminate();
+ });
+}
/** Quote a path for the private Git shim without interpreting shell metacharacters. */
function shellWord(value) {
@@ -83,6 +153,13 @@ function runTests(args, env) {
* @returns {void}
*/
function main(args) {
+ try {
+ testTimeoutMs();
+ } catch (error) {
+ process.stderr.write(`${error.message}\n`);
+ process.exitCode = 1;
+ return;
+ }
const sandbox = mkdtempSync(join(tmpdir(), 'pullboard-test-run-'));
let signal = null;
try {
@@ -100,4 +177,5 @@ if (process.argv[1] && resolve(process.argv[1]) === runner) {
main(process.argv.slice(2));
} else if (process.argv[1]) {
process.env.PULLBOARD_TEST_FILE = process.argv[1];
+ installTestWatchdog();
}
diff --git a/test/run-tests-timeout.test.js b/test/run-tests-timeout.test.js
new file mode 100644
index 00000000..c4ec1018
--- /dev/null
+++ b/test/run-tests-timeout.test.js
@@ -0,0 +1,99 @@
+/** Prove each test has a named bounded timeout and the run continues [C7, V10]. */
+import assert from 'node:assert/strict';
+import { spawnSync } from 'node:child_process';
+import { existsSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import { fileURLToPath } from 'node:url';
+import { test } from 'node:test';
+import { DEFAULT_TEST_TIMEOUT_MS } from '../bin/run-tests.js';
+
+const runner = fileURLToPath(new URL('../bin/run-tests.js', import.meta.url));
+
+/**
+ * Create an isolated fixture and run its tests through the production runner.
+ *
+ * @param {import('node:test').TestContext} t
+ * @param {string} stalledSource
+ * @param {string} timeoutOverride
+ * @returns {import('node:child_process').SpawnSyncReturns<string>}
+ */
+function runFixture(t, stalledSource, timeoutOverride = '1200') {
+ const root = mkdtempSync(join(tmpdir(), 'pullboard-test-timeout-'));
+ t.after(() => rmSync(root, { recursive: true, force: true }));
+ writeFileSync(join(root, 'package.json'), JSON.stringify({ type: 'module' }));
+ const stalled = join(root, 'stalled.test.js');
+ const later = join(root, 'later.test.js');
+ writeFileSync(stalled, stalledSource);
+ writeFileSync(later, "import { test } from 'node:test';\ntest('later file still reports [C7,V10]', () => {});\n");
+
+ const result = spawnSync(process.execPath, [runner, stalled, later], {
+ cwd: root,
+ env: { ...process.env, PULLBOARD_TEST_TIMEOUT_MS: timeoutOverride },
+ encoding: 'utf8',
+ detached: true,
+ timeout: 15_000,
+ });
+ if (result.error?.code === 'ETIMEDOUT' && result.pid) {
+ try { process.kill(-result.pid, 'SIGKILL'); } catch { /* The owned fixture process group already exited. */ }
+ }
+ return result;
+}
+
+/**
+ * Combine child output without hiding the runner's diagnostic channel.
+ *
+ * @param {import('node:child_process').SpawnSyncReturns<string>} result
+ * @returns {string}
+ */
+function outputOf(result) {
+ return `${result.stdout ?? ''}${result.stderr ?? ''}`;
+}
+
+test('a never-settling test fails by name and later files still report [C7, V10]', (t) => {
+ assert.equal(DEFAULT_TEST_TIMEOUT_MS, 660_000, '660 seconds exceeds three times the measured 213.2-second slowest test');
+ const result = runFixture(t, `import { test } from 'node:test';
+test('before timeout target passes [C7,V10]', () => {});
+test('awaited timeout target [C7,V10]', async (context) => {
+ const keepAlive = setTimeout(() => {}, 30000);
+ context.after(() => clearTimeout(keepAlive));
+ await new Promise(() => {});
+});
+`);
+ const output = outputOf(result);
+ assert.equal(result.status, 1, output);
+ assert.match(output, /test "awaited timeout target \[C7,V10\]" in .*stalled\.test\.js failed: it ran .*past the 1200ms per-test timeout/u);
+ assert.match(output, /ok \d+ - before timeout target passes \[C7,V10\]/u);
+ assert.match(output, /ok \d+ - later file still reports \[C7,V10\]/u);
+});
+
+test('a synchronous test hang is named while another file reports [C7, V10]', (t) => {
+ const result = runFixture(t, `import { test } from 'node:test';
+test('synchronous timeout target [C7,V10]', () => { Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 30000); });
+`);
+ const output = outputOf(result);
+ assert.equal(result.status, 1, output);
+ assert.match(output, /test "synchronous timeout target \[C7,V10\]" in .*stalled\.test\.js failed: it ran .*past the 1200ms per-test timeout/u);
+ assert.match(output, /ok \d+ - later file still reports \[C7,V10\]/u);
+});
+
+test('an invalid test-timeout override refuses before running a test [C7, V10]', (t) => {
+ const root = mkdtempSync(join(tmpdir(), 'pullboard-test-timeout-invalid-'));
+ t.after(() => rmSync(root, { recursive: true, force: true }));
+ const marker = join(root, 'ran');
+ const probe = join(root, 'probe.test.js');
+ writeFileSync(join(root, 'package.json'), JSON.stringify({ type: 'module' }));
+ writeFileSync(probe, `import { test } from 'node:test';
+import { writeFileSync } from 'node:fs';
+test('must not run [C7,V10]', () => writeFileSync(${JSON.stringify(marker)}, 'ran'));
+`);
+ const result = spawnSync(process.execPath, [runner, probe], {
+ cwd: root,
+ env: { ...process.env, PULLBOARD_TEST_TIMEOUT_MS: '0' },
+ encoding: 'utf8',
+ timeout: 15_000,
+ });
+ assert.equal(existsSync(marker), false, 'invalid timeout was refused before the fixture ran');
+ assert.equal(result.status, 1, outputOf(result));
+ assert.match(outputOf(result), /PULLBOARD_TEST_TIMEOUT_MS must be a positive integer no greater than 2147483647/u);
+});