[tools] CpuGuard: Full test runs wait for, then refuse, a machine above 80 % CPU

This commit is contained in:
2026-11-02 00:51:45 +01:00
parent 8b1c2eaee2
commit f9883e22ba
7 changed files with 195 additions and 4 deletions
+45
View File
@@ -0,0 +1,45 @@
/**
* `pnpm guard:cpu` — samples the machine for 2 s; starts if it is at or under the
* limit, waits and samples again while it is not, and exits 1 once the wait is used
* up. The full runs (`pnpm test`, `test:e2e`, `test:offline`) call it first.
*/
import { cpus } from 'node:os';
import { setTimeout as sleep } from 'node:timers/promises';
import { busyPercent, decide, limitsFrom } from './guard.js';
const SAMPLE_MS = 2_000;
const PAUSE_MS = 10_000;
async function sample(): Promise<number> {
const before = cpus();
await sleep(SAMPLE_MS);
return busyPercent(before, cpus());
}
let limits;
try {
limits = limitsFrom(process.env);
} catch (error) {
console.error(`guard:cpu: ${(error as Error).message}`);
process.exit(2);
}
const started = Date.now();
for (;;) {
const busy = await sample();
const waited = Math.round((Date.now() - started) / 1000);
const verdict = decide(busy, waited, limits);
if (verdict === 'start') {
console.log(`guard:cpu: ${busy} % busy, at or under ${limits.max} %. Starting.`);
break;
}
if (verdict === 'refuse') {
console.error(
`guard:cpu: still ${busy} % busy after ${waited} s, above ${limits.max} % (CLAUDE.md §5.7). Not starting.\n` +
' Close what is busy, or on a dedicated machine set ROSEA_CPU_MAX (100 never refuses).'
);
process.exit(1);
}
console.log(`guard:cpu: ${busy} % busy, above ${limits.max} %. Waiting (${waited} of ${limits.waitSeconds} s).`);
await sleep(PAUSE_MS);
}
+53
View File
@@ -0,0 +1,53 @@
/**
* CLAUDE.md §5.7: do not start a full run above ~80 % CPU. Read by a person, the rule
* was broken three times in one day (lessons/process.md), so a full run now asks
* this guard first. The logic is pure; `cli.ts` does the sampling and the waiting.
*/
import type { CpuInfo } from 'node:os';
/** The default limit, CLAUDE.md §5.7. */
export const DEFAULT_MAX = 80;
/** How long a busy machine is waited for before the run is refused. */
export const DEFAULT_WAIT_SECONDS = 180;
/** Whole-machine busy share between two `os.cpus()` readings, 0–100. */
export function busyPercent(before: readonly CpuInfo[], after: readonly CpuInfo[]): number {
let busy = 0;
let total = 0;
for (let i = 0; i < Math.min(before.length, after.length); i++) {
const a = before[i]!.times;
const b = after[i]!.times;
const idle = b.idle - a.idle;
const all = b.user - a.user + (b.nice - a.nice) + (b.sys - a.sys) + (b.irq - a.irq) + idle;
busy += all - idle;
total += all;
}
return total <= 0 ? 0 : Math.round((busy / total) * 100);
}
export interface Limits {
readonly max: number;
readonly waitSeconds: number;
}
/**
* The limits, from the environment. `ROSEA_CPU_MAX` is for a dedicated machine such as
* CI (100 never refuses); `ROSEA_CPU_WAIT_SECONDS` for how long to wait. A value that
* is not a whole number in range is an error, never quietly the default.
*/
export function limitsFrom(env: Readonly<Record<string, string | undefined>>): Limits {
const read = (name: string, fallback: number, min: number, max: number): number => {
const raw = env[name];
if (raw === undefined || raw.trim() === '') return fallback;
const n = Number(raw);
if (!Number.isInteger(n) || n < min || n > max) throw new RangeError(`${name} must be a whole number from ${min} to ${max}, got '${raw}'`);
return n;
};
return { max: read('ROSEA_CPU_MAX', DEFAULT_MAX, 1, 100), waitSeconds: read('ROSEA_CPU_WAIT_SECONDS', DEFAULT_WAIT_SECONDS, 0, 3600) };
}
/** Start once a sample is at or under the limit; refuse once the wait is used up. */
export function decide(busy: number, waitedSeconds: number, limits: Limits): 'start' | 'wait' | 'refuse' {
if (busy <= limits.max) return 'start';
return waitedSeconds >= limits.waitSeconds ? 'refuse' : 'wait';
}