Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: sweep each pregate stage group after the stage exits (#12641)

[._.]/ Adam Eivya8577ab12641
diff --git a/docs/CONTRIBUTING.md b/docs/CONTRIBUTING.md
index fb890f7487..4e722778fe 100644
--- a/docs/CONTRIBUTING.md
+++ b/docs/CONTRIBUTING.md
@@ -124,7 +124,9 @@ Interrupting pregate (Ctrl-C or SIGTERM) does not leave tests running behind
it: the running stage — including the test runner's npm and Vitest processes,
which share that stage's process group — gets the signal and a 5-second grace,
then the group is killed until no member remains. Only then does pregate write
-an incomplete receipt (`passed: false`) and exit by the same signal.
+an incomplete receipt (`passed: false`) and exit by the same signal. After every stage, on
+POSIX, pregate also kills anything still left in that stage's group, with a
+warning, so no stage's processes run into the next one or past pregate.
After the push (and, when you open the pull request yourself, before
`gh pr create`), `node scripts/pregate-attest.js` posts that receipt as a
diff --git a/scripts/pregate.js b/scripts/pregate.js
index e57ee7d321..1fe49b38ea 100644
--- a/scripts/pregate.js
+++ b/scripts/pregate.js
@@ -51,6 +51,7 @@
import { spawn, spawnSync } from 'child_process';
import { writeSync } from 'fs';
import { constants } from 'os';
+import { setTimeout as delay } from 'timers/promises';
import { execFileSync } from 'child_process';
import { dirname, join } from 'path';
import { fileURLToPath } from 'url';
@@ -466,18 +467,28 @@ async function cancelStage(stage, child, closed, signal) {
signalStage(child, 'SIGKILL');
if (!await within(closed, STAGE_SETTLE_MS)) return { settled: false, reason: 'stage process did not exit after SIGKILL' };
}
- if (IS_WIN32) return { settled: true };
- // The leader is reaped, but a descendant that ignored the signal keeps the
- // group (and its open log descriptors) alive. ESRCH is the proof of empty.
+ return IS_WIN32 ? { settled: true } : sweepStageGroup(child.pid);
+}
+
+/**
+ * Kill whatever is still in a stage's process group once its leader exited:
+ * a descendant that ignored a signal or outlived its launcher keeps running
+ * (and writing to inherited descriptors) into the next stage or past pregate.
+ * ESRCH is the proof the group is empty. POSIX only. Resolves
+ * `{ settled, lingered?, reason? }`.
+ */
+async function sweepStageGroup(pgid) {
const deadline = Date.now() + STAGE_SETTLE_MS;
- for (;;) {
+ for (let attempt = 0; ; attempt += 1) {
try {
- process.kill(-child.pid, 'SIGKILL');
+ process.kill(-pgid, 'SIGKILL');
} catch (error) {
- return error.code === 'ESRCH' ? { settled: true } : { settled: false, reason: `group kill failed (${error.code ?? 'unknown'})` };
+ return error.code === 'ESRCH'
+ ? { settled: true, lingered: attempt > 0 }
+ : { settled: false, reason: `group kill failed (${error.code ?? 'unknown'})` };
}
if (Date.now() >= deadline) return { settled: false, reason: 'group members remained after SIGKILL' };
- await new Promise((resolve) => setTimeout(resolve, 25));
+ await delay(25);
}
}
@@ -504,8 +515,11 @@ async function runStage(stage, owner) {
};
const outcome = await closed;
owner.onSignal = null;
- if (!cancellation) return { ...outcome, pid: child.pid };
- return { ...outcome, pid: child.pid, interrupted, ...await cancellation };
+ if (cancellation) return { ...outcome, pid: child.pid, interrupted, ...await cancellation };
+ if (IS_WIN32 || !child.pid) return { ...outcome, pid: child.pid };
+ const sweep = await sweepStageGroup(child.pid);
+ if (sweep.lingered) writeSync(2, `⚠️ Stage "${stage.name}" exited with processes still running in its group; killed them.\n`);
+ return { ...outcome, pid: child.pid, ...(sweep.settled ? {} : { unsettled: sweep.reason }) };
}
const formatDuration = (ms) => (ms >= 60_000 ? `${(ms / 60_000).toFixed(1)}m` : `${(ms / 1000).toFixed(1)}s`);
@@ -700,6 +714,10 @@ async function main() {
? `❌ Pregate incomplete: stage="${result.name}" cancelled by ${outcome.interrupted}; its process group has exited.`
: `❌ Pregate incomplete: stage="${result.name}" cancelled by ${outcome.interrupted}; owned process group settlement unverified (${outcome.reason}).`;
exitCode = 128 + constants.signals[outcome.interrupted];
+ } else if (outcome.unsettled) {
+ result.status = 'failed';
+ failure = `❌ ${result.name} left processes in its group that could not be stopped (${outcome.unsettled}).`;
+ exitCode = 1;
} else if (outcome.error) {
result.status = 'failed';
failure = `❌ ${result.name} could not start: ${outcome.error.message}`;
diff --git a/scripts/pregate.test.js b/scripts/pregate.test.js
index 2ff21a5f30..610ac6e5a6 100644
--- a/scripts/pregate.test.js
+++ b/scripts/pregate.test.js
@@ -412,6 +412,28 @@ describe('pregate hidden-content invocation', () => {
}
});
+ // run-ci-tests.js leaves npm and Vitest in pregate's stage group, so pregate
+ // owns them on every exit path, not only on cancellation.
+ it.skipIf(process.platform === 'win32')('kills a descendant still running in the stage group after the stage exits', () => {
+ const marker = join(scratch, 'straggler-pid');
+ write('scripts/scan-diff-hidden-content.js', `
+ import { spawn } from 'child_process';
+ import { writeFileSync } from 'fs';
+ const straggler = spawn(process.execPath, ['-e', "process.on('SIGTERM', () => {}); setInterval(() => {}, 1000);"], { stdio: 'ignore' });
+ writeFileSync(${JSON.stringify(marker)}, String(straggler.pid));
+ setTimeout(() => process.exit(0), 200);
+ `);
+ const result = run('--skip-lint');
+ const pid = Number(readFileSync(marker, 'utf8'));
+ let alive = true;
+ try { process.kill(pid, 0); } catch (error) { alive = error.code !== 'ESRCH'; }
+ if (alive) process.kill(pid, 'SIGKILL');
+ expect(alive).toBe(false);
+ // Later stages are not fixtures here; only the scan stage's outcome matters.
+ expect(result.stdout).toContain('hidden-content scan passed');
+ expect(result.stderr).toContain('Stage "hidden-content scan" exited with processes still running in its group; killed them.');
+ });
+
it.skipIf(process.platform === 'win32')('reports a stage child signal as incomplete rather than an ordinary assertion failure', () => {
write('scripts/scan-diff-hidden-content.js', "process.kill(process.pid, 'SIGTERM');\n");
const result = run('--skip-lint');