Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

scale: judge each import tenth by its cheapest slice so one stall cannot fail import_rate (#7939)

garrytan731dc9ebecca
diff --git a/scripts/scale/README.md b/scripts/scale/README.md
index ed26c48222..8e1493ba43 100644
--- a/scripts/scale/README.md
+++ b/scripts/scale/README.md
@@ -43,8 +43,13 @@ its EXPLAIN; the JSON report and a `.explain.txt` land next to `--out`);
2 on a usage error; 3 when the harness itself crashed (not a verdict).
The import-rate gate compares the last 10% of pages' per-page cost with the
first 10% (<= 1.5x) and the total import with the time at the halfway mark
-(<= 2.5x). The report and its JSON `import` section carry both bases for every
-engine: wall time (`elapsed_ms`) and the import process's main-thread CPU time
+(<= 2.5x). A tenth's per-page cost is the lowest mean over ten equal slices of
+it, and total/half sums those costs over the tenths: a stall shorter than a
+slice (a WAL checkpoint, a burst from another tenant of a CI host) once failed
+three unrelated pull requests on its own (#7939), while a cost that grows with
+the brain raises every slice. `total_ms` and `ms_at_half` stay plain sums.
+The report and its JSON `import` section carry both bases for every engine:
+wall time (`elapsed_ms`) and the import process's main-thread CPU time
(`cpu_ms`, user + system, from each progress tick; in-process timing under
`--import-mode content`). PGLite is judged on CPU time: the database runs
synchronously on the import's main thread, so that CPU time holds all of its
diff --git a/scripts/scale/gates.ts b/scripts/scale/gates.ts
index 1c505515dc..dc3783df58 100644
--- a/scripts/scale/gates.ts
+++ b/scripts/scale/gates.ts
@@ -63,16 +63,30 @@ export function importProgressPerFile(stderr: string): { wallMs: number[]; cpuMs
return { wallMs: perFile(wall), cpuMs: perFile(cpu) };
}
+/**
+ * #7939: the per-page cost of each tenth of the import is the lowest mean over ten equal slices of it, and both ratios
+ * use that cost (`total_vs_half` sums it over the tenths). A stall shorter than a slice of the window (a WAL checkpoint,
+ * a burst from another tenant of a shared CI host) used to carry a tenth's mean past the gate on its own (1.74x, 2.1x,
+ * and 2.57x total/half on three unrelated PRs); a per-page cost that grows with the brain raises every slice.
+ * `total_ms` and `ms_at_half` stay the plain sums.
+ */
export function rateMeasure(perPageMs: number[]): RateMeasure {
const sum = (xs: number[]) => xs.reduce((a, b) => a + b, 0);
- const tenth = Math.max(1, Math.floor(perPageMs.length / 10));
- const first = sum(perPageMs.slice(0, tenth)) / tenth;
- const last = sum(perPageMs.slice(-tenth)) / tenth;
- const half = sum(perPageMs.slice(0, Math.floor(perPageMs.length / 2)));
- const total = sum(perPageMs);
+ const sliceCost = (xs: number[]) => {
+ const size = Math.max(1, Math.floor(xs.length / 10));
+ let lowest = Infinity;
+ for (let at = 0; at + size <= xs.length; at += size) lowest = Math.min(lowest, sum(xs.slice(at, at + size)) / size);
+ return lowest;
+ };
+ const tenths = Array.from({ length: 10 }, (_, d) => perPageMs.slice(Math.floor(perPageMs.length * d / 10), Math.floor(perPageMs.length * (d + 1) / 10)))
+ .filter(xs => xs.length > 0).map(xs => ({ pages: xs.length, cost: sliceCost(xs) }));
+ const first = tenths[0]!.cost;
+ const last = tenths.at(-1)!.cost;
+ const work = (ts: typeof tenths) => sum(ts.map(t => t.cost * t.pages));
const round = (n: number, places: number) => Math.round(n * 10 ** places) / 10 ** places;
return { per_page_ms_first10: round(first, 1), per_page_ms_last10: round(last, 1), rate_ratio: round(last / first, 2),
- total_ms: Math.round(total), ms_at_half: Math.round(half), total_vs_half: round(total / half, 2) };
+ total_ms: Math.round(sum(perPageMs)), ms_at_half: Math.round(sum(perPageMs.slice(0, Math.floor(perPageMs.length / 2)))),
+ total_vs_half: round(work(tenths) / work(tenths.slice(0, Math.ceil(tenths.length / 2))), 2) };
}
/**
diff --git a/test/scripts/scale-gates.test.ts b/test/scripts/scale-gates.test.ts
index cb36e7afdb..1fda7477aa 100644
--- a/test/scripts/scale-gates.test.ts
+++ b/test/scripts/scale-gates.test.ts
@@ -161,6 +161,34 @@ describe('scale gates under --enforce', () => {
expect(importRateGate('postgres', growing)).toMatchObject({ status: 'fail', enforced: true });
});
+ // #7939: one stall (a WAL checkpoint, a burst from another tenant) inside the last tenth carried its mean past the
+ // gate on three unrelated PRs. Each tenth is judged by its cheapest tenth-of-a-tenth, which a stall that short
+ // cannot reach and a cost that grows with the brain raises everywhere.
+ const stall = (from: number, pages: number, ms: number) => (i: number) => (i >= from && i < from + pages ? ms / pages : 0);
+
+ test('an 8 s stall in the last tenth passes on Postgres wall time; the same 8 s spread over every page of it fails', () => {
+ const stalled = stall(PAGES * 0.95, 50, 8000);
+ const gate = importRateGate('postgres', progressStream(i => ({ wall: 5 + stalled(i), cpu: 5 })));
+ expect(gate.status).toBe('pass');
+ expect(gate.message).toContain('import rate (wall time): last 10% per-page cost 5 ms is 1x the first 10%');
+ expect(importRateGate('postgres', progressStream(i => ({ wall: lastTenth(i) ? 13 : 5, cpu: 5 })))).toMatchObject({ status: 'fail', enforced: true });
+ });
+
+ test('a stall in the second half passes total/half; a cost that rises over the second half fails it', () => {
+ const stalled = stall(PAGES * 0.7, 80, 30_000);
+ expect(importRateGate('postgres', progressStream(i => ({ wall: 5 + stalled(i), cpu: 5 }))).status).toBe('pass');
+ const report = { ...passingReport(), engine: 'postgres' as const };
+ report.import.wall = rateMeasure(Array.from({ length: PAGES }, (_, i) => (i >= PAGES / 2 && !lastTenth(i) ? 12 : 5)));
+ expect(report.import.wall).toMatchObject({ rate_ratio: 1, total_vs_half: 3.12 });
+ expect(evaluateScaleGates(report, ENFORCE).failures.map(f => f.gate)).toEqual(['import_rate']);
+ });
+
+ test('a slow page in every slice still counts: the cost of a tenth is a slice mean, not its fastest page', () => {
+ const everyTenthSlow = progressStream(i => ({ wall: lastTenth(i) && i % 10 === 0 ? 85 : 5, cpu: 5 }));
+ expect(importRateGate('postgres', everyTenthSlow)).toMatchObject({ status: 'fail' });
+ expect(importRateGate('postgres', everyTenthSlow).message).toContain('last 10% per-page cost 13 ms is 2.6x');
+ });
+
test('a tick without cpu_ms, or a missing tick, leaves NaN for the harness to reject', () => {
const stream = progressStream(() => ({ wall: 5, cpu: 5 })).split('\n');
stream[5] = stream[5]!.replace(/,"cpu_ms":\d+/, '');