Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
ci(ubicloud): run the unit-lane PostgreSQL arms in the full gate (pg-arms lane)
garrytan1db06d064a13
diff --git a/.github/workflows/persistence-validation.yml b/.github/workflows/persistence-validation.ymlindex fc70a76453..30965c85ff 100644--- a/.github/workflows/persistence-validation.yml+++ b/.github/workflows/persistence-validation.yml@@ -173,9 +173,9 @@ jobs:if-no-files-found: error# Unit-lane suites whose PostgreSQL arm is gated on DATABASE_URL: the unit,- # serial and slow lanes unset it, so these arms run only here and in the- # scheduled race hunt (both arms per file; the PGLite arm also runs in the- # unit lane). The list is test/postgres-unit-arms.txt;+ # serial and slow lanes unset it, so these arms run only here, in+ # ci:ubicloud's pg-arms lane and in the scheduled race hunt (both arms per+ # file; the PGLite arm also runs in the unit lane). The list is test/postgres-unit-arms.txt;# scripts/check-postgres-lane-coverage.ts fails a new DATABASE_URL-gated arm# until it is listed there or wrapped in a test/e2e registerPostgresTests# file. The eight shards (ARM_SHARDS in scripts/postgres-unit-arms.ts) arediff --git a/docs/TESTING.md b/docs/TESTING.mdindex 2ad4beac69..bea95763a3 100644--- a/docs/TESTING.md+++ b/docs/TESTING.md@@ -278,12 +278,7 @@ imports it (`registerPostgresTests`), a `tests/heavy/` script that names it,or a row in `scripts/e2e-backend-matrix.txt`. Unit-lane files with no otherPostgres owner are listed in `test/postgres-unit-arms.txt`, read by`persistence-validation.yml`'s `unit-postgres-arms` job (one Bun process per-file), the [race hunt](#race-hunt) and the lane guard. The job runs-`ARM_SHARDS` (8) shards balanced by `scripts/postgres-arm-weights.json`,-medians mined from the job's own logs with-`bun scripts/postgres-unit-arms.ts mine <job logs>`; re-mine after adding heavy-files. `test/scripts/postgres-unit-arms.test.ts` fails when a shard's weighted-sum passes 80% of the step's 20-minute timeout.+file), the [race hunt](#race-hunt) and the lane guard.`bun run check:postgres-lanes` (in `verify`) fails on every arm with no laneand on a bad list row. An arm deliberately left out is an `ALLOWLIST` row in`scripts/check-postgres-lane-coverage.ts` naming its reason and TODO; a rowdiff --git a/scripts/ci-ubicloud.ts b/scripts/ci-ubicloud.tsindex 1027ba18f2..c18c535e8e 100755--- a/scripts/ci-ubicloud.ts+++ b/scripts/ci-ubicloud.ts@@ -3,7 +3,7 @@* scripts/ci-ubicloud.ts — the ci:local gate fanned out across ephemeral* Ubicloud VMs.*- * bun run ci:ubicloud # gitleaks + verify + serial + slow + unit + ALL E2E+ * bun run ci:ubicloud # gitleaks + verify + serial + slow + unit + pg-arms + ALL E2E* bun run ci:ubicloud:diff # doc-only diff: doc checks + gitleaks; any other diff: the full gate** Options:@@ -15,7 +15,7 @@* --image IMAGE boot image (default: UBI_CI_IMAGE, else stock Ubuntu 24.04);* gbrain-ci@latest is the prebaked machine image* scripts/ubicloud/build-ci-image.sh builds- * --lanes a,b subset of gitleaks,verify,serial,slow,unit,e2e (default all)+ * --lanes a,b subset of gitleaks,verify,serial,slow,unit,pg-arms,e2e (default all)* --diff doc-only diffs run only the doc checks and gitleaks (ci:local --diff)* --record-weights write measured durations to scripts/ubicloud/weights.json* --keep leave the VMs running (destroy with ubi-runner.sh down NAME)@@ -32,7 +32,10 @@* serial and slow lanes' machine-exclusive files) and then joins the pool. Every other* test file is an item in one global heaviest-first queue that idle slots pull* from (scripts/ubicloud/schedule.ts); items run through the same wrappers- * ci:local uses (scripts/ubicloud/ci-item.sh). Durations of every run are+ * ci:local uses (scripts/ubicloud/ci-item.sh). The pg-arms lane runs every+ * 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. Durations of every run are* merged into .context/ci-ubicloud/weights.json, which weights the next run.** VMs are named ubirun-<owner>-<epoch>-ciNN<hex> (owner: UBI_OWNER or the@@ -63,6 +66,7 @@ import {} from "./ubicloud/schedule.ts";import { runScript, type RunScriptOpts } from "./ubicloud/run-script.ts";import { buildTypecheckState, portableBuildInfo, typecheckBuildInfo } from "./ubicloud/typecheck-state.ts";+import { ARM_SHARD_BUDGET_MS, ARM_SHARDS, ARM_WEIGHTS, armShards, readArmsList } from "./postgres-unit-arms.ts";const ROOT = resolve(import.meta.dir, "..");const RUNNER = join(ROOT, "scripts/ubicloud/ubi-runner.sh");@@ -72,7 +76,7 @@ const UNPACKED_MARKER = "work/gbrain.unpacked";const SETUP_SCRIPT = join(ROOT, "scripts/ubicloud/setup-ci-vm.sh");const COMMITTED_WEIGHTS = join(ROOT, "scripts/ubicloud/weights.json");const LOCAL_WEIGHTS = join(ROOT, ".context/ci-ubicloud/weights.json");-const ALL_LANES = ["gitleaks", "verify", "serial", "slow", "unit", "e2e"] as const;+const ALL_LANES = ["gitleaks", "verify", "serial", "slow", "unit", "pg-arms", "e2e"] as const;const BATCH_TIMEOUT_MS = 20 * 60 * 1000;// Items this heavy are the run's long poles: spread them one per VM so two of// them never compete for the same VM's CPUs.@@ -213,10 +217,13 @@ async function main() {const exclusive = lines(sh("bash", ["scripts/run-serial-tests.sh", "--dry-run-list-exclusive"]));const serialPool = lines(sh("bash", ["scripts/run-serial-tests.sh", "--dry-run-list"])).filter((f) => !exclusive.includes(f));const slowExclusive = lines(sh("bash", ["scripts/run-slow-tests.sh", "--dry-run-list-exclusive"]));+ const arms = readArmsList(ROOT);+ if (arms.errors.length) throw new Error(`test/postgres-unit-arms.txt: ${arms.errors.join("; ")}`);const inventory: Record<Lane, string[]> = {unit: lines(sh("bash", ["scripts/run-unit-shard.sh", "--dry-run-list"])),serial: serialPool,slow: lines(sh("bash", ["scripts/run-slow-tests.sh", "--dry-run-list"])).filter((f) => !slowExclusive.includes(f)),+ "pg-arms": arms.files,e2e: e2eFiles,};@@ -224,12 +231,13 @@ async function main() {readWeightTable(LOCAL_WEIGHTS),readWeightTable(COMMITTED_WEIGHTS),readWeightTable(join(ROOT, "scripts/e2e-weights.json"), 1, "e2e:"),+ readWeightTable(join(ROOT, ARM_WEIGHTS), 1, "pg-arms:"),readWeightTable(join(ROOT, "scripts/serial-weights.json"), 1000, "serial:"),readWeightTable(join(ROOT, "scripts/test-weights.json"), 1, "unit:"),readWeightTable(join(ROOT, "scripts/test-weights.json"), 1, "slow:"),];const queue: Item[] = [];- for (const lane of ["unit", "serial", "slow", "e2e"] as Lane[]) {+ for (const lane of ["unit", "serial", "slow", "pg-arms", "e2e"] as Lane[]) {if (opts.lanes.has(lane)) queue.push(...buildItems(lane, inventory[lane], tables));}sortQueue(queue);@@ -520,6 +528,21 @@ async function main() {console.log(" slowest items:");for (const r of [...all].sort((a, b) => b.ms - a.ms).slice(0, 8)) console.log(` ${Math.round(r.ms / 1000)}s ${r.lane} ${r.file}`);+ // The pg-arms lane's proof that unit-postgres-arms fits: each shard of the+ // workflow's own partition, summed from this run's measured file durations.+ const armShardReport = opts.lanes.has("pg-arms")+ ? armShards(ROOT, arms.files, ARM_SHARDS).map((files, i) => ({+ shard: i + 1,+ files: files.length,+ ms: files.reduce((sum, file) => sum + (results.get(weightKey("pg-arms", file))?.ms ?? 0), 0),+ }))+ : [];+ if (armShardReport.length) {+ console.log(` pg-arms shards (unit-postgres-arms partition, ${ARM_SHARDS} shards, budget ${ARM_SHARD_BUDGET_MS / 1000}s each):`);+ 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_SHARD_BUDGET_MS);+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;const merged = { ...Object.fromEntries(readWeightTable(LOCAL_WEIGHTS)), ...timings };@@ -536,6 +559,7 @@ async function main() {sha, runId, wallMs, computeMs, opts: { ...opts, lanes: [...opts.lanes] },vms: vms.map(({ name, state, setupMs, infraErrors }) => ({ name, state, setupMs, infraErrors })),lanes: Object.fromEntries(byLane), failures: failures.map(({ lane, file, rc, ms, vm }) => ({ lane, file, rc, ms, vm })),+ armShards: armShardReport,}, null, 2) + "\n");if (failures.length) {@@ -549,6 +573,10 @@ async function main() {console.log(`\nFull per-item logs: ${join(runDir, "failures")}`);exitCode = 1;}+ if (overBudget.length) {+ console.log(`\npg-arms: ${overBudget.map((s) => `shard ${s.shard}/${ARM_SHARDS} measured ${Math.round(s.ms / 1000)}s`).join(", ")}, over the ${ARM_SHARD_BUDGET_MS / 1000}s step timeout of persistence-validation's unit-postgres-arms job, which would cut that shard off mid-list. Fix: raise ARM_SHARDS (and the workflow matrix) or speed up the heaviest files, then re-mine scripts/postgres-arm-weights.json. Docs: docs/TESTING.md#postgres-arm-lanes`);+ exitCode = 1;+ }const expected = totalItems;if (exitCode === 0 && results.size !== expected) {console.error(`ci-ubicloud: ${results.size} results for ${expected} items`);diff --git a/scripts/postgres-unit-arms.ts b/scripts/postgres-unit-arms.tsindex 0db8b99301..058ac76ea3 100644--- a/scripts/postgres-unit-arms.ts+++ b/scripts/postgres-unit-arms.ts@@ -10,7 +10,12 @@** Weights are milliseconds per file (both arms, one Bun process) mined from* the job's `::group::<file>` timestamps; a file without a weight gets the- * p75 (scripts/sharding.ts). Docs: docs/TESTING.md#postgres-arm-lanes.+ * p75 (scripts/sharding.ts). The job runs ARM_SHARDS shards inside a+ * ARM_SHARD_BUDGET_MS step; re-mine after adding heavy files.+ * test/scripts/postgres-unit-arms.test.ts fails when a shard's weighted sum+ * passes 80% of the budget, and ci:ubicloud's pg-arms lane fails when a+ * shard's measured sum passes it (scripts/ubicloud/README.md).+ * Docs: docs/TESTING.md#postgres-arm-lanes.*/import { existsSync, readFileSync, writeFileSync } from 'node:fs';import { join } from 'node:path';@@ -78,6 +83,11 @@ export function mineArmWeights(logs: string[]): Map<string, number> {}));}+/** Shard n of m (1-based), as persistence-validation's unit-postgres-arms matrix runs it and ci:ubicloud's pg-arms lane reports it. */+export function armShards(root: string, files: string[], m: number): string[][] {+ return partition(files, loadWeights(join(root, ARM_WEIGHTS)), m).map(shard => [...shard].sort());+}+function main(argv: string[]): number {const root = join(import.meta.dir, '..');const [command, ...rest] = argv;@@ -96,8 +106,7 @@ function main(argv: string[]): number {console.error('usage: bun scripts/postgres-unit-arms.ts shard <n> <m> (1 <= n <= m)');return 2;}- const shards = partition(list.files, loadWeights(join(root, ARM_WEIGHTS)), m!);- console.log([...shards[n! - 1]!].sort().join('\n'));+ console.log(armShards(root, list.files, m!)[n! - 1]!.join('\n'));return 0;}if (command === 'mine' && rest.length) {diff --git a/scripts/structural-suites.tsv b/scripts/structural-suites.tsvindex 97cb092930..72758bc871 100644--- a/scripts/structural-suites.tsv+++ b/scripts/structural-suites.tsv@@ -318,6 +318,7 @@ test/scripts/nightly-issue.test.ts nightly-watch incidents 9 readFileSynctest/scripts/nightly-issue.test.ts nightly-watch routing and guard 9 readFileSynctest/scripts/nightly-issue.test.ts operator messages carry docs anchors that resolve 1 readFileSynctest/scripts/postgres-lane-coverage.test.ts check-postgres-lane-coverage.ts 9 readFileSync+test/scripts/postgres-unit-arms.test.ts postgres-unit-arms list 9 readFileSynctest/scripts/regen-all.test.ts regen:all 5 readFileSynctest/scripts/run-verify-parallel.test.ts guard registration ⇒ execution coverage 1 readFileSynctest/scripts/run-verify-parallel.test.ts run-verify-parallel.sh — no-timeout-binary fallback rc capture (regression) 7 readFileSyncdiff --git a/scripts/ubicloud/README.md b/scripts/ubicloud/README.mdindex 7e894d43a0..3e7a05cd0a 100644--- a/scripts/ubicloud/README.md+++ b/scripts/ubicloud/README.md@@ -105,7 +105,13 @@ build's output is in `logs/local-typecheck.log`. Items run through the `ci:local(`scripts/ubicloud/ci-item.sh`): `run-unit-shard.sh`, `run-serial-tests.sh` and`run-slow-tests.sh` accept explicit file arguments for this purpose, and`run-e2e.sh` runs each E2E file against its slot's own server and pooler. The-unit, serial and slow lanes run with database URLs unset. Tests run natively as+unit, serial and slow lanes run with database URLs unset. The `pg-arms` lane+runs every file in `test/postgres-unit-arms.txt` the way persistence-validation's+`unit-postgres-arms` step does (`bun --no-env-file test --timeout=120000`,+`GBRAIN_TEST_ALLOW_DATABASE_URL=1`, no timeout multiplier) against the slot's+own empty `gbrain_arms_test` database. The summary sums its measured durations over+that job's `ARM_SHARDS` partition, and a shard over the step's 20-minute timeout+fails the run, since GitHub would cut that shard off mid-list. Tests run natively asa non-root user on Ubuntu 24.04, the same OS as the CI runners, instead of asroot in the `oven/bun` container.diff --git a/scripts/ubicloud/ci-item.sh b/scripts/ubicloud/ci-item.shindex 732d4b3713..8ce392c268 100755--- a/scripts/ubicloud/ci-item.sh+++ b/scripts/ubicloud/ci-item.sh@@ -8,6 +8,8 @@# unit scripts/run-unit-shard.sh FILE (DATABASE_URL unset)# serial scripts/run-serial-tests.sh FILE (DATABASE_URL unset)# slow scripts/run-slow-tests.sh FILE (DATABASE_URL unset)+# pg-arms FILE's PostgreSQL arm, exactly as persistence-validation's+# unit-postgres-arms step runs it, against this slot's gbrain_arms_test database# e2e scripts/run-e2e.sh FILE against this slot's own postgres + pgbouncer# (backend-matrix files also run through the slot's pgbouncer)# verify check-bun-test-timeout.sh + bun run verify (no FILE)@@ -44,6 +46,11 @@ run_item() {unit) bash scripts/run-unit-shard.sh "$1" ;;serial) bash scripts/run-serial-tests.sh "$1" ;;slow) bash scripts/run-slow-tests.sh "$1" ;;+ pg-arms)+ env -u GBRAIN_TEST_TIMEOUT_MULTIPLIER -u GBRAIN_TEST_EXPORT_SCALE_PAGES \+ DATABASE_URL="$pg/gbrain_arms_test" GBRAIN_TEST_ALLOW_DATABASE_URL=1 \+ bun --no-env-file test --timeout=120000 "$1"+ ;;e2e)DATABASE_URL="$pg/gbrain_test" \GBRAIN_PGBOUNCER_URL="$pgb/gbrain_pgbouncer_test" \diff --git a/scripts/ubicloud/schedule.ts b/scripts/ubicloud/schedule.tsindex 8f6af07b2d..f98413fa94 100644--- a/scripts/ubicloud/schedule.ts+++ b/scripts/ubicloud/schedule.ts@@ -12,7 +12,7 @@import { existsSync, readFileSync } from "node:fs";-export type Lane = "unit" | "serial" | "slow" | "e2e";+export type Lane = "unit" | "serial" | "slow" | "e2e" | "pg-arms";export interface Item {lane: Lane;diff --git a/scripts/ubicloud/setup-ci-vm.sh b/scripts/ubicloud/setup-ci-vm.shindex 5e7fe655e4..ede301a31c 100755--- a/scripts/ubicloud/setup-ci-vm.sh+++ b/scripts/ubicloud/setup-ci-vm.sh@@ -176,6 +176,13 @@ for i in $(seq 1 "$SLOTS"); dodonestep "$SLOTS postgres + pgbouncer slots ready"+# The pg-arms lane's database: empty like the unit-postgres-arms job's service+# database, and apart from the E2E schema bootstrapped below.+for i in $(seq 1 "$SLOTS"); do+ PGPASSWORD="$PG_PASSWORD" psql -h 127.0.0.1 -p "$((15432 + i))" -U postgres -d gbrain_test -qc 'CREATE DATABASE gbrain_arms_test' \+ || { echo "[setup-ci-vm] could not create gbrain_arms_test on slot $i" >&2; exit 1; }+done+# Initialize every slot's schema up front, exactly like e2e.yml's "Bootstrap# isolated E2E schema" step, so no file depends on whichever file happens to# reach a fresh database first.diff --git a/test/scripts/postgres-unit-arms.test.ts b/test/scripts/postgres-unit-arms.test.tsindex e647a03b3d..cc6b517d8e 100644--- a/test/scripts/postgres-unit-arms.test.ts+++ b/test/scripts/postgres-unit-arms.test.ts@@ -47,6 +47,17 @@ describe('postgres-unit-arms list', () => {expect(sums.filter(ms => ms > ARM_SHARD_BUDGET_MS * 0.8)).toEqual([]);});+ test('ci:ubicloud\'s pg-arms lane runs each file with the workflow step\'s own command', () => {+ const command = 'bun --no-env-file test --timeout=120000';+ const workflow = readFileSync(join(REPO, '.github', 'workflows', 'persistence-validation.yml'), 'utf8');+ const item = readFileSync(join(REPO, 'scripts', 'ubicloud', 'ci-item.sh'), 'utf8');+ const lane = item.slice(item.indexOf(' pg-arms)'), item.indexOf(';;', item.indexOf(' pg-arms)')));+ expect(workflow).toContain(command);+ expect(lane).toContain(`${command} "$1"`);+ expect(lane).toContain('GBRAIN_TEST_ALLOW_DATABASE_URL=1');+ expect(lane).toContain('-u GBRAIN_TEST_TIMEOUT_MULTIPLIER');+ });+test('comments and blank lines are ignored; a new path is a one-line edit', () => {expect(parseArmsList('# header\n\ntest/a.test.ts\ntest/b/c.serial.test.ts\n')).toEqual({ files: ['test/a.test.ts', 'test/b/c.serial.test.ts'], errors: [] });});