Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

ci(ubicloud): hold the gate's unit shards to the GitHub budget, measured time report-only

garrytan8379dd5e146f
diff --git a/scripts/ci-ubicloud.ts b/scripts/ci-ubicloud.ts
index 25ea877627..1ba2621a7e 100755
--- a/scripts/ci-ubicloud.ts
+++ b/scripts/ci-ubicloud.ts
@@ -40,7 +40,8 @@
* file in test/postgres-unit-arms.txt with DATABASE_URL set, the way
* persistence-validation's unit-postgres-arms job does, and fails the run when
* a shard of that job's partition measures over its step timeout; with the unit
- * and slow lanes it does the same for test.yml's unit shards. Durations of every run are
+ * and slow lanes it fails a test.yml unit shard whose mined weight is over
+ * UNIT_SHARD_BUDGET_MS (unitShardCheck) and reports each shard's measured time. Durations of every run are
* merged into .context/ci-ubicloud/weights.json, which weights the next run.
*
* VMs are named ubirun-<owner>-<epoch>-x<expiry epoch>-ciNN<hex> (owner:
@@ -74,7 +75,7 @@ import { runScript, type RunScriptOpts } from "./ubicloud/run-script.ts";
import { expiringVmName } from "./ubicloud/reap-expired.ts";
import { buildTypecheckState, portableBuildInfo, typecheckBuildInfo } from "./ubicloud/typecheck-state.ts";
import { ARM_SHARDS, ARM_STEP_TIMEOUT_MS, ARM_WEIGHTS, ARM_WORKERS, armMakespan, armShards, readArmsList } from "./postgres-unit-arms.ts";
-import { UNIT_JOB_TIMEOUT_MS, UNIT_SETUP_ALLOWANCE_MS, UNIT_SHARDS, unitShards } from "./sharding.ts";
+import { UNIT_SHARD_BUDGET_MS, UNIT_SHARDS, unitShardPlan } from "./sharding.ts";
const ROOT = resolve(import.meta.dir, "..");
const RUNNER = join(ROOT, "scripts/ubicloud/ubi-runner.sh");
@@ -556,20 +557,16 @@ async function main() {
for (const s of armShardReport) console.log(` ${s.shard}/${ARM_SHARDS}: ${Math.round(s.ms / 1000)}s across ${s.files} files`);
}
const overBudget = armShardReport.filter((s) => s.ms > ARM_STEP_TIMEOUT_MS);
- // The same proof for test.yml's unit shards: test-shard.sh's own partition,
- // summed from this run's measured unit and slow file durations.
- const unitShardRoomMs = UNIT_JOB_TIMEOUT_MS - UNIT_SETUP_ALLOWANCE_MS;
- const unitShardReport = opts.lanes.has("unit") && opts.lanes.has("slow")
- ? unitShards(ROOT).map((files, i) => {
- const measured = files.map((file) => results.get(weightKey("unit", file))?.ms ?? results.get(weightKey("slow", file))?.ms);
- return { shard: i + 1, files: files.length, unmeasured: measured.filter((ms) => ms === undefined).length, ms: measured.reduce<number>((sum, ms) => sum + (ms ?? 0), 0) };
- })
- : [];
+ // test.yml's unit shards: the plan test/scripts/unit-shard-budget.test.ts holds (unitShardCheck).
+ const unitCheck = opts.lanes.has("unit") && opts.lanes.has("slow")
+ ? unitShardCheck(ROOT, (file) => results.get(weightKey("unit", file))?.ms ?? results.get(weightKey("slow", file))?.ms)
+ : null;
+ const unitShardReport = unitCheck?.shards ?? [];
+ const unitOverBudget = unitCheck?.overBudget ?? [];
if (unitShardReport.length) {
- console.log(` unit shards (test.yml partition, ${UNIT_SHARDS} shards, ${unitShardRoomMs / 1000}s each after setup):`);
- for (const s of unitShardReport) console.log(` ${s.shard}/${UNIT_SHARDS}: ${Math.round(s.ms / 1000)}s across ${s.files} files${s.unmeasured ? ` (${s.unmeasured} not run by this gate)` : ""}`);
+ console.log(` unit shards (test.yml partition, ${UNIT_SHARDS} shards, mined weight at most ${UNIT_SHARD_BUDGET_MS / 1000}s each; measured here for drift only):`);
+ for (const s of unitShardReport) console.log(` ${s.shard}/${UNIT_SHARDS}: mined ${Math.round(s.mined / 1000)}s, measured ${Math.round(s.ms / 1000)}s across ${s.files} files${s.unmeasured ? ` (${s.unmeasured} not run by this gate)` : ""}`);
}
- const unitOverBudget = unitShardReport.filter((s) => s.ms > unitShardRoomMs);
const timings: Record<string, number> = {};
for (const r of all) if (r.rc === 0 && r.file !== "-") timings[weightKey(r.lane as Lane, r.file)] = r.ms;
@@ -607,7 +604,7 @@ async function main() {
exitCode = 1;
}
if (unitOverBudget.length) {
- console.log(`\nunit: ${unitOverBudget.map((s) => `shard ${s.shard}/${UNIT_SHARDS} measured ${Math.round(s.ms / 1000)}s`).join(", ")}, over the ${unitShardRoomMs / 1000}s a test.yml unit shard has inside its job timeout after setup, which would cancel that shard mid-list. Fix: raise UNIT_SHARDS (and both workflow matrices) or speed up the heaviest files, then re-mine scripts/test-weights.json. Docs: docs/TESTING.md#keeping-ci-partitions-balanced`);
+ console.log(`\nunit: ${unitOverBudget.map((s) => `shard ${s.shard}/${UNIT_SHARDS} mined ${Math.round(s.mined / 1000)}s`).join(", ")}, over UNIT_SHARD_BUDGET_MS (${UNIT_SHARD_BUDGET_MS / 1000}s), which test/scripts/unit-shard-budget.test.ts also enforces. Fix: re-mine scripts/test-weights.json, then raise UNIT_SHARDS (and both workflow matrices) or speed up the heaviest files. Docs: docs/TESTING.md#keeping-ci-partitions-balanced`);
exitCode = 1;
}
const expected = totalItems;
@@ -619,6 +616,21 @@ async function main() {
process.exit(exitCode);
}
+/**
+ * test.yml's unit shards as this gate checks them: the partition and mined weights of unitShardPlan, failing a
+ * shard over UNIT_SHARD_BUDGET_MS exactly as test/scripts/unit-shard-budget.test.ts does. `measured` (this run's
+ * per-file durations) is reported beside the plan and never gates: each item here is its own wrapper and Bun
+ * process (~0.6 s of startup per file) on a shared 16-vCPU VM, not one Bun process per shard on a 4-vCPU runner,
+ * so recent gates measured the same partition at 1.2-2.5x its mined weight.
+ */
+export function unitShardCheck(root: string, measured: (file: string) => number | undefined) {
+ const shards = unitShardPlan(root).map(({ shard, files, ms }) => {
+ const times = files.map(measured);
+ return { shard, files: files.length, mined: ms, unmeasured: times.filter((t) => t === undefined).length, ms: times.reduce<number>((sum, t) => sum + (t ?? 0), 0) };
+ });
+ return { budgetMs: UNIT_SHARD_BUDGET_MS, shards, overBudget: shards.filter((s) => s.mined > UNIT_SHARD_BUDGET_MS) };
+}
+
function sortedObject(obj: Record<string, number>): Record<string, number> {
return Object.fromEntries(Object.entries(obj).sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)));
}
diff --git a/scripts/sharding.ts b/scripts/sharding.ts
index 804ca7b7be..3965efd6bd 100644
--- a/scripts/sharding.ts
+++ b/scripts/sharding.ts
@@ -80,6 +80,15 @@ export function unitShards(root: string, n: number = UNIT_SHARDS): string[][] {
export type WeightMap = Map<string, number>;
+/**
+ * test.yml's unit partition with each shard's mined weight (`root`'s scripts/test-weights.json): the plan
+ * test/scripts/unit-shard-budget.test.ts and the ci:ubicloud gate both hold to UNIT_SHARD_BUDGET_MS.
+ */
+export function unitShardPlan(root: string): { shard: number; files: string[]; ms: number }[] {
+ const weights = loadWeights(join(root, "scripts/test-weights.json"));
+ return unitShards(root).map((files, i) => ({ shard: i + 1, files, ms: Math.round(predictedWeight(files, weights).totalMs) }));
+}
+
export class WeightsLoadError extends Error {
constructor(public readonly path: string, public readonly cause: unknown) {
super(`failed to load weights from ${path}: ${cause}`);
diff --git a/scripts/ubicloud/README.md b/scripts/ubicloud/README.md
index 57de8efec4..739ebb32c5 100644
--- a/scripts/ubicloud/README.md
+++ b/scripts/ubicloud/README.md
@@ -148,10 +148,15 @@ files at a time), and a shard over the step's 20-minute timeout
fails the run, since GitHub would cut that shard off mid-list. The committed
weights must also fit each shard twice over with a minute to spare
(`ARM_SHARD_BUDGET_MS`, 570 s), because a slow runner ran the files 1.3-2x
-their mined weights. With the unit and slow lanes, the summary does the same
-for test.yml's unit shards: it sums measured durations over `test-shard.sh`'s
-`UNIT_SHARDS` partition and fails a shard over the 21 minutes left in the
-22-minute job after setup (`docs/TESTING.md#keeping-ci-partitions-balanced`);
+their mined weights. With the unit and slow lanes, the summary checks
+test.yml's unit shards exactly as `test/scripts/unit-shard-budget.test.ts`
+does: `test-shard.sh`'s `UNIT_SHARDS` partition, each shard's weight from
+`scripts/test-weights.json`, failing a shard over `UNIT_SHARD_BUDGET_MS`
+(`unitShardCheck`, `docs/TESTING.md#keeping-ci-partitions-balanced`). It
+prints each shard's measured time beside its mined weight but never fails on
+it: every item is its own Bun process on a shared VM, so the same partition
+measured 1.2-2.5x its mined weight across gates (one shard 1,274 s, about
+503 s mined), with every test passing;
`evals/` files, which no ci:ubicloud lane runs, count as unmeasured. Tests run natively as
a non-root user on Ubuntu 24.04, the same OS as the CI runners, instead of as
root in the `oven/bun` container.
diff --git a/test/scripts/ci-ubicloud-unit-budget.test.ts b/test/scripts/ci-ubicloud-unit-budget.test.ts
new file mode 100644
index 0000000000..ba3f114975
--- /dev/null
+++ b/test/scripts/ci-ubicloud-unit-budget.test.ts
@@ -0,0 +1,35 @@
+/**
+ * The ci:ubicloud gate's unit-shard check and test.yml's budget guard are one check: the gate holds the same
+ * partition (scripts/test-shard.sh via unitShardPlan), the same mined weights (scripts/test-weights.json) and
+ * the same UNIT_SHARD_BUDGET_MS as test/scripts/unit-shard-budget.test.ts.
+ * Fails when: the gate partitions, weighs or budgets unit shards on its own again (its own 1,260 s limit over
+ * measured time failed GBRA-95's gate at 1,274 s for a shard every GitHub guard passed), or measured gate time
+ * gates the run.
+ */
+import { describe, expect, test } from 'bun:test';
+import { join } from 'node:path';
+import { unitShardCheck } from '../../scripts/ci-ubicloud.ts';
+import { UNIT_SHARD_BUDGET_MS, unitShardPlan } from '../../scripts/sharding.ts';
+
+const REPO = join(import.meta.dir, '..', '..');
+
+describe('ci:ubicloud unit shard check', () => {
+ const plan = unitShardPlan(REPO);
+ const guarded = plan.filter(({ ms }) => ms > UNIT_SHARD_BUDGET_MS).map(({ shard }) => shard);
+
+ test('checks the partition, mined weights and budget the GitHub guard checks', () => {
+ const gate = unitShardCheck(REPO, () => undefined);
+ expect(gate.budgetMs).toBe(UNIT_SHARD_BUDGET_MS);
+ expect(gate.shards.map(({ shard, files, mined }) => ({ shard, files, mined })))
+ .toEqual(plan.map(({ shard, files, ms }) => ({ shard, files: files.length, mined: ms })));
+ expect(gate.shards.every(({ unmeasured, files }) => unmeasured === files)).toBe(true);
+ expect(gate.overBudget.map(({ shard }) => shard)).toEqual(guarded);
+ });
+
+ test('measured gate time is reported, never gating: a 1,274 s file leaves the verdict to the mined plan', () => {
+ const gate = unitShardCheck(REPO, () => 1_274_000);
+ expect(gate.shards.map(({ shard, ms, files }) => ({ shard, ms: ms / files })))
+ .toEqual(plan.map(({ shard }) => ({ shard, ms: 1_274_000 })));
+ expect(gate.overBudget.map(({ shard }) => shard)).toEqual(guarded);
+ });
+});
diff --git a/test/scripts/unit-shard-budget.test.ts b/test/scripts/unit-shard-budget.test.ts
index 91c1d7ec35..4d52f75193 100644
--- a/test/scripts/unit-shard-budget.test.ts
+++ b/test/scripts/unit-shard-budget.test.ts
@@ -10,13 +10,11 @@ import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import { load } from 'js-yaml';
import {
- loadWeights,
- predictedWeight,
UNIT_JOB_TIMEOUT_MS,
UNIT_SETUP_ALLOWANCE_MS,
UNIT_SHARD_BUDGET_MS,
UNIT_SHARDS,
- unitShards,
+ unitShardPlan,
} from '../../scripts/sharding.ts';
const REPO = join(import.meta.dir, '..', '..');
@@ -31,11 +29,10 @@ describe('unit shard budget', () => {
test('every unit shard\'s mined weight fits the job twice over, after setup, with a minute to spare', () => {
expect(2 * UNIT_SHARD_BUDGET_MS + UNIT_SETUP_ALLOWANCE_MS + 60_000).toBe(UNIT_JOB_TIMEOUT_MS);
- const weights = loadWeights(join(REPO, 'scripts', 'test-weights.json'));
- const shards = unitShards(REPO);
- expect(shards.every(files => files.length > 0)).toBe(true);
- const over = shards
- .map((files, i) => ({ shard: `${i + 1}/${UNIT_SHARDS}`, ms: Math.round(predictedWeight(files, weights).totalMs) }))
+ const plan = unitShardPlan(REPO);
+ expect(plan.every(({ files }) => files.length > 0)).toBe(true);
+ const over = plan
+ .map(({ shard, ms }) => ({ shard: `${shard}/${UNIT_SHARDS}`, ms }))
.filter(s => s.ms > UNIT_SHARD_BUDGET_MS);
expect(over).toEqual([]);
});