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(gate): lend a place only through a synchronous child call [C7]
Corey Olson4ad754316cf7
diff --git a/src/test-file-pool.js b/src/test-file-pool.jsindex 71fcc051..25178395 100644--- a/src/test-file-pool.js+++ b/src/test-file-pool.js@@ -5,12 +5,14 @@* Measured 22 Oct: 28 runs at --test-concurrency=10 kept 54 file processes live on 14 cores, and passing files* ran 18.5 times slower in landings than alone. A per-run limit cannot see the other runs; this pool can.*- * A test file that starts its own run-tests and waits on it lends its token to that nested run: while the nested- * run lives, its parent counts as waiting, not running, so the nested files take the parent's place and never a- * second core. The parent's place stays reserved while none of its nested files holds one, so it resumes into it.+ * A test file that starts its own run-tests with a synchronous call (so it is blocked until the run exits) lends its+ * token to that nested run: while the nested run lives, its parent counts as waiting, not running, so the nested+ * files take the parent's place and never a second core. The parent's place stays reserved while none of its nested+ * files holds one, so it resumes into it. A run started asynchronously gets no loan, since its parent may compute.* Two nested runs of one parent share its one place; neither can wait forever on the token its own parent holds.*/import { mkdirSync } from 'node:fs';+import { createRequire, syncBuiltinESMExports } from 'node:module';import { availableParallelism } from 'node:os';import { join } from 'node:path';import { machineHome } from './machine-home.js';@@ -80,10 +82,9 @@ async function openPool(home) {PRAGMA synchronous = NORMAL;CREATE TABLE IF NOT EXISTS file_holder(pid INTEGER PRIMARY KEY, since INTEGER NOT NULL, lender INTEGER);CREATE TABLE IF NOT EXISTS file_waiter(ticket INTEGER PRIMARY KEY AUTOINCREMENT, pid INTEGER NOT NULL UNIQUE);- CREATE TABLE IF NOT EXISTS file_loan(runner INTEGER PRIMARY KEY, lender INTEGER NOT NULL, active INTEGER NOT NULL DEFAULT 0);`);+ CREATE TABLE IF NOT EXISTS file_loan(runner INTEGER PRIMARY KEY, lender INTEGER NOT NULL);`);// A pool made before holders named their lender gains the column; a missing lender reads as none.if (!db.prepare('PRAGMA table_info(file_holder)').all().some(column => column.name === 'lender')) db.exec('ALTER TABLE file_holder ADD COLUMN lender INTEGER');- if (!db.prepare('PRAGMA table_info(file_loan)').all().some(column => column.name === 'active')) db.exec('ALTER TABLE file_loan ADD COLUMN active INTEGER NOT NULL DEFAULT 0');return db;}@@ -125,7 +126,7 @@ function prune(db) {* its run does, so no other file can take it in between and the lender resumes into its own place.*/function occupied(db) {- return db.prepare(`WITH lending AS (SELECT DISTINCT lender AS pid FROM file_loan WHERE active = 1 AND lender IN (SELECT pid FROM file_holder))+ return db.prepare(`WITH lending AS (SELECT DISTINCT lender AS pid FROM file_loan WHERE lender IN (SELECT pid FROM file_holder))SELECT (SELECT count(*) FROM file_holder WHERE pid NOT IN (SELECT pid FROM lending))+ (SELECT count(*) FROM lending WHERE NOT EXISTS (SELECT 1 FROM file_holder WHERE file_holder.lender = lending.pid)) AS n`).get().n;}@@ -153,7 +154,7 @@ function admit(db, cores, lender) {return immediate(db, () => {prune(db);// A nested file steps into its lender's reserved place when no sibling holds it; that leaves the count unchanged.- const reserved = lender && db.prepare(`SELECT 1 FROM file_loan WHERE lender = ? AND active = 1 AND lender IN (SELECT pid FROM file_holder)+ const reserved = lender && db.prepare(`SELECT 1 FROM file_loan WHERE lender = ? AND lender IN (SELECT pid FROM file_holder)AND NOT EXISTS (SELECT 1 FROM file_holder WHERE lender = ?)`).get(lender, lender);const { taken, ahead } = standing(db);if (!reserved && ahead >= cores - taken) return false;@@ -190,49 +191,37 @@ export async function takeTestFileToken(env = process.env) {try { db.prepare('DELETE FROM file_holder WHERE pid = ?').run(process.pid); db.close(); }catch { /* A row left behind is pruned once this process is gone. */ }});- env[TEST_FILE_POOL_ENV] = JSON.stringify({ home: pool.home, cores: pool.cores, lender: process.pid });+ env[TEST_FILE_POOL_ENV] = JSON.stringify({ home: pool.home, cores: pool.cores });+ lendThroughSyncCalls(pool, env);return Date.now() - asked;}-// Budget: the loan's state follows the parent within this long; a parent using less CPU than this per look waits.-const LOAN_WATCH_MS = 250;-const LOAN_IDLE_CPU_MS = 25;-/**- * Watches one parent: its loan is active only while the parent uses no CPU (blocked on its child, or idle awaiting- * it). A parent that keeps computing keeps its own token, so its nested files take separate ones (#1046).- * Linux reads CPU time from /proc in clock ticks (USER_HZ 100); other systems read ps, in hundredths.+ * Lend this file's place only to what it starts with a synchronous child call (spawnSync, execFileSync, execSync).+ * Such a call blocks this file's JavaScript until the child exits, so the file is waiting for as long as the loan+ * lasts. A child started asynchronously gets no lender: the file may keep computing, so it keeps its own token and+ * the nested files take theirs from the pool (#1046). A call whose environment no longer names this pool, as a test+ * that isolates its own pool does, is left exactly as written.*/-const LOAN_WATCH_SOURCE = `-const { workerData } = require('node:worker_threads');-const { execFileSync } = require('node:child_process');-const { readFileSync } = require('node:fs');-const emitWarning = process.emitWarning;-process.emitWarning = (warning, ...rest) => { if (!String(warning).includes('SQLite is an experimental feature')) emitWarning.call(process, warning, ...rest); };-const { DatabaseSync } = require('node:sqlite');-process.emitWarning = emitWarning;-const { file, runner, lender, everyMs, idleMs } = workerData;-const db = new DatabaseSync(file);-db.exec('PRAGMA busy_timeout = 10000');-const cpuMs = () => {- if (process.platform === 'linux') {- const stat = readFileSync('/proc/' + lender + '/stat', 'utf8');- const fields = stat.slice(stat.lastIndexOf(')') + 2).split(' ');- return (Number(fields[11]) + Number(fields[12])) * 10;+function lendThroughSyncCalls(pool, env) {+ const ours = env[TEST_FILE_POOL_ENV];+ const lent = JSON.stringify({ home: pool.home, cores: pool.cores, lender: process.pid });+ const childProcess = createRequire(import.meta.url)('node:child_process');+ for (const name of ['spawnSync', 'execFileSync', 'execSync']) {+ const original = childProcess[name];+ childProcess[name] = function lendingSyncCall(...args) {+ const at = args.findIndex((arg, index) => index > 0 && arg && typeof arg === 'object' && !Array.isArray(arg));+ const options = at === -1 ? {} : args[at];+ const childEnv = options.env ?? env;+ if (childEnv[TEST_FILE_POOL_ENV] === ours) {+ const lending = { ...options, env: { ...childEnv, [TEST_FILE_POOL_ENV]: lent } };+ if (at === -1) args.push(lending); else args[at] = lending;+ }+ return original.apply(this, args);+ };}- const time = execFileSync('ps', ['-o', 'time=', '-p', String(lender)], { encoding: 'utf8' }).trim();- const [days, clock] = time.includes('-') ? time.split('-') : ['0', time];- return clock.split(':').reverse().reduce((total, part, index) => total + Number(part) * [1, 60, 3600][index], Number(days) * 86400) * 1000;-};-let last = null;-setInterval(() => {- try {- const now = cpuMs();- if (last !== null) db.prepare('UPDATE file_loan SET active = ? WHERE runner = ?').run(now - last <= idleMs ? 1 : 0, runner);- last = now;- } catch { /* The parent is gone; the pool prunes this loan with its run. */ }-}, everyMs);-`;+ syncBuiltinESMExports();+}/*** Mark this run as nested in a test file, so that file counts as waiting while the run lives. A run started@@ -245,11 +234,7 @@ export async function lendParentToken(env = process.env) {const pool = testFilePool(env);if (!pool?.lender) return;const db = await openPool(pool.home);- immediate(db, () => db.prepare('INSERT OR REPLACE INTO file_loan(runner, lender, active) VALUES (?, ?, 0)').run(process.pid, pool.lender));- // A thread, because this runner's own loop blocks while its tests run; it never keeps the run alive.- const { Worker } = await import('node:worker_threads');- const watcher = new Worker(LOAN_WATCH_SOURCE, { eval: true, workerData: { file: join(pool.home, 'test-file-pool.sqlite'), runner: process.pid, lender: pool.lender, everyMs: LOAN_WATCH_MS, idleMs: LOAN_IDLE_CPU_MS } });- watcher.unref();+ immediate(db, () => db.prepare('INSERT OR REPLACE INTO file_loan(runner, lender) VALUES (?, ?)').run(process.pid, pool.lender));process.once('exit', () => {try { db.prepare('DELETE FROM file_loan WHERE runner = ?').run(process.pid); db.close(); }catch { /* A row left behind is pruned once this run is gone. */ }diff --git a/test/gate-admission.test.js b/test/gate-admission.test.jsindex 84f51ead..51d4ac9b 100644--- a/test/gate-admission.test.js+++ b/test/gate-admission.test.js@@ -108,8 +108,7 @@ test('a lender keeps its place between its last nested file and its nested run econst hold = (pid, lender = null) => db.prepare('INSERT INTO file_holder(pid, since, lender) VALUES (?, ?, ?)').run(pid, Date.now(), lender);hold(parent);hold(unrelated);- // Active: the parent is blocked on its nested run, as the loan watcher records it.- db.prepare('INSERT INTO file_loan(runner, lender, active) VALUES (?, ?, 1)').run(runner, parent);+ db.prepare('INSERT INTO file_loan(runner, lender) VALUES (?, ?)').run(runner, parent);assert.equal(await occupiedPlaces(home), 2, 'a lender with no nested file yet keeps its place');hold(child, parent);assert.equal(await occupiedPlaces(home), 2, 'its nested file runs in that place');@@ -119,10 +118,10 @@ test('a lender keeps its place between its last nested file and its nested run eassert.equal(await occupiedPlaces(home), 2, 'the nested run ended, the lender resumes in its own place');});-test('a parent that keeps computing beside its nested run keeps its own token, so its nested files take separate ones [C7]', async (t) => {+test('a parent that starts its nested run asynchronously keeps its own token, so its nested files take separate ones [C7]', async (t) => {const { root, env, log } = fixture(t, 2);for (const file of ['inner-a.test.js', 'inner-b.test.js']) writeFileSync(join(root, file), timedFile(file, 400));- // The parent starts its nested run without waiting on it and spins the CPU until the run ends.+ // The parent starts its nested run without blocking on it and spins the CPU until the run ends.writeFileSync(join(root, 'busy.test.js'), `import { spawn } from 'node:child_process';import { test } from 'node:test';test('busy parent', async () => {