Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
chore(pregate): remote_gate delegates the test tier to the full Ubicloud gate on small machines
garrytan3c971af161fa
diff --git a/.gstack/pregate.json b/.gstack/pregate.jsonindex 8abfc4eba7..d95434a6e0 100644--- a/.gstack/pregate.json+++ b/.gstack/pregate.json@@ -14,6 +14,9 @@],"src/core/operations*.ts": ["test/cli-goldens.test.ts"+ ],+ "scripts/pregate-remote-gate.sh": [+ "test/scripts/pregate-remote-gate.test.ts"]},"guards": [@@ -72,5 +75,6 @@"**/usage.jsonl.gz"],"visibility": "public"- }+ },+ "remote_gate": "bash scripts/pregate-remote-gate.sh"}diff --git a/CONTRIBUTING.md b/CONTRIBUTING.mdindex 4350adda3e..3f4c999126 100644--- a/CONTRIBUTING.md+++ b/CONTRIBUTING.md@@ -715,7 +715,14 @@ gstack reads both from master, so a branch's edits take effect after it merges.failing guard prints its fix line. `sealed` lists eval output that must neverreach this public repository (run reports, sealed-confirmation and countedcell outputs, per-cell `results.jsonl`/`usage.jsonl.gz`);- `gstack-pregate sealed` refuses a push that adds one.+ `gstack-pregate sealed` refuses a push that adds one. `remote_gate` names+ `scripts/pregate-remote-gate.sh`: on a machine under 8 cores pregate records+ the selected test files as a requires-remote obligation instead of running+ them locally (a hub file such as `src/core/config.ts` selects nearly the+ whole suite). `UBI_OWNER=<thread code> gstack-pregate --stage tests --remote+ --spend-cap <usd> --allow-repo-commands` then runs the full Ubicloud gate on+ the working tree and clears the obligation from its+ `PREGATE_REMOTE: tree=… tests=… failed=…` line.- `.gstack/ship-policy.json`: the stamp rule (the release stamp files andversion patterns) and the migration numbering `gstack-pregate regate` and`gstack-tree-receipt` use to decide whether a restamped head is the same treediff --git a/scripts/pregate-remote-gate.sh b/scripts/pregate-remote-gate.shnew file mode 100755index 0000000000..9ff8a4a11e--- /dev/null+++ b/scripts/pregate-remote-gate.sh@@ -0,0 +1,75 @@+#!/usr/bin/env bash+# scripts/pregate-remote-gate.sh — gbrain's remote test gate for gstack-pregate.+#+# .gstack/pregate.json names this script as `remote_gate`. Under 8 cores+# gstack-pregate records the local test tier as a requires-remote obligation+# instead of running thousands of files on the machine;+# `gstack-pregate --stage tests --remote --spend-cap <usd> --allow-repo-commands`+# runs this script, and its last line clears the obligation:+#+# PREGATE_REMOTE: tree=<tree> tests=<n> failed=<n>+#+# It runs the full Ubicloud gate (`bun run ci:ubicloud`, every lane), a+# superset of whatever pregate selected, so PREGATE_FILES_FILE is not read.+# <tree> is the working-tree content fingerprint this script computes itself+# (tracked plus untracked-unignored files, the set ci:ubicloud packs), taken+# before and after the gate; the line is printed only when both agree and,+# when gstack passed PREGATE_TREE, match it. <tests> and <failed> are summed+# from the gate's summary. No line is printed when the gate printed no+# summary. The exit code is the gate's.+#+# Needs UBI_OWNER (your thread code, lowercase) so the VMs are tagged and torn+# down as yours, plus what ci:ubicloud needs (UBICLOUD_API_KEY or+# UBICLOUD_API_TOKEN). Provider API keys are removed from the gate's+# environment. Extra arguments go to ci:ubicloud (for example --vms 8).+# Spend is not reported (no spent=), so gstack counts it at the cap.+#+# Seam: GBRAIN_PREGATE_GATE_CMD replaces `bun run ci:ubicloud`+# (test/scripts/pregate-remote-gate.test.ts).+set -uo pipefail++fail() { echo "pregate-remote-gate: $1" >&2; exit "${2:-1}"; }++[ -n "${UBI_OWNER:-}" ] || fail "UBI_OWNER is not set; set it to your thread code in lowercase (e.g. UBI_OWNER=gbra106) so the gate's VMs are yours to tear down" 2++TOP=$(git rev-parse --show-toplevel 2>/dev/null) || fail "not inside a git checkout" 2+cd "$TOP" || exit 2++content_tree() {+ local idx real+ idx=$(mktemp "${TMPDIR:-/tmp}/pregate-remote-gate-XXXXXX") || return 1+ real=$(git rev-parse --git-path index)+ if [ -f "$real" ]; then cp "$real" "$idx"; else GIT_INDEX_FILE="$idx" git read-tree HEAD || { rm -f "$idx"; return 1; }; fi+ GIT_INDEX_FILE="$idx" git add -A . && GIT_INDEX_FILE="$idx" git write-tree+ local rc=$?+ rm -f "$idx"+ return $rc+}++before=$(content_tree) || fail "cannot fingerprint the working tree"+if [ -n "${PREGATE_TREE:-}" ] && [ "$before" != "$PREGATE_TREE" ]; then+ fail "the working tree is $before, not the tree pregate selected for ($PREGATE_TREE); rerun gstack-pregate on the current tree"+fi++log=$(mktemp "${TMPDIR:-/tmp}/pregate-remote-gate-log-XXXXXX") || fail "cannot create a log file"+trap 'rm -f "$log"' EXIT++gate_cmd=${GBRAIN_PREGATE_GATE_CMD:-bun run ci:ubicloud}+# shellcheck disable=SC2086+env -u OPENAI_API_KEY -u ANTHROPIC_API_KEY -u VOYAGE_API_KEY -u GEMINI_API_KEY -u JEV_TYPESAFE_API_KEY -u DATABASE_URL \+ bash -c "$gate_cmd \"\$@\"" pregate-remote-gate "$@" 2>&1 | tee "$log"+rc=${PIPESTATUS[0]}++after=$(content_tree) || fail "cannot fingerprint the working tree after the gate"+[ "$after" = "$before" ] || fail "the working tree changed during the gate ($before -> $after); no receipt"++totals=$(awk '+ /^=== ci-ubicloud summary ===$/ { seen = 1; next }+ seen && /^ +[a-z0-9-]+ +[0-9]+ passed, [0-9]+ failed/ { passed += $2; failed += $4; rows++ }+ END { if (rows) print passed + failed, failed }+' "$log")+[ -n "$totals" ] || fail "the gate printed no ci-ubicloud summary (exit $rc); no receipt" "$([ "$rc" -ne 0 ] && echo "$rc" || echo 1)"++read -r tests failed <<<"$totals"+echo "PREGATE_REMOTE: tree=$before tests=$tests failed=$failed"+exit "$rc"diff --git a/test/scripts/pregate-remote-gate.test.ts b/test/scripts/pregate-remote-gate.test.tsnew file mode 100644index 0000000000..c31950c66c--- /dev/null+++ b/test/scripts/pregate-remote-gate.test.ts@@ -0,0 +1,112 @@+/**+ * scripts/pregate-remote-gate.sh — gbrain's gstack-pregate remote gate.+ *+ * Protects: the receipt gstack-pregate --remote reads to clear the+ * requires-remote obligation on a small machine (PREGATE_REMOTE: tree=…+ * tests=… failed=…).+ * Fails when: the tree reported is not the working tree's content fingerprint+ * (untracked files included, ignored files excluded), the totals stop summing+ * every summary lane, a gate with no summary or a moved tree still prints a+ * receipt, the gate's exit code is lost, or a missing UBI_OWNER or a+ * PREGATE_TREE mismatch reaches the gate.+ * Seam: GBRAIN_PREGATE_GATE_CMD replaces `bun run ci:ubicloud`.+ */+import { afterEach, beforeEach, describe, expect, it } from 'bun:test';+import { spawnSync } from 'node:child_process';+import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join, resolve } from 'node:path';++const SCRIPT = resolve(import.meta.dir, '..', '..', 'scripts/pregate-remote-gate.sh');+const SUMMARY = [+ '=== ci-ubicloud summary ===',+ ' gitleaks 1 passed, 0 failed, 8s compute',+ ' unit 30 passed, 2 failed, 80s compute',+ ' e2e 5 passed, 0 failed, 60s compute',+ ' VMs: 8/8 ready, setup 62-72s',+].join('\n');++let repo: string;+const git = (...args: string[]) => spawnSync('git', args, { cwd: repo, encoding: 'utf8', env: { ...process.env, GIT_CONFIG_NOSYSTEM: '1' } }).stdout.trim();++beforeEach(() => {+ repo = mkdtempSync(join(tmpdir(), 'pregate-remote-gate-'));+ git('init', '-q', '-b', 'main');+ git('config', 'user.email', '[email protected]');+ git('config', 'user.name', 'Test');+ writeFileSync(join(repo, '.gitignore'), 'ignored.txt\n');+ writeFileSync(join(repo, 'a.txt'), 'a\n');+ git('add', '.');+ git('commit', '-q', '-m', 'init');+});+afterEach(() => rmSync(repo, { recursive: true, force: true }));++function run(gate: string, env: Record<string, string | undefined> = {}) {+ const r = spawnSync('bash', [SCRIPT], {+ cwd: repo, encoding: 'utf8',+ env: { ...process.env, GIT_CONFIG_NOSYSTEM: '1', UBI_OWNER: 'gbra0test', PREGATE_TREE: undefined, GBRAIN_PREGATE_GATE_CMD: gate, ...env },+ });+ return { code: r.status, out: r.stdout, err: r.stderr };+}++/** The content tree of tracked plus untracked-unignored files, as gstack-wtree computes it. */+function contentTree(): string {+ const idx = join(repo, '.git', 'test-index');+ const env = { ...process.env, GIT_INDEX_FILE: idx, GIT_CONFIG_NOSYSTEM: '1' };+ spawnSync('git', ['read-tree', 'HEAD'], { cwd: repo, env });+ spawnSync('git', ['add', '-A', '.'], { cwd: repo, env });+ const tree = spawnSync('git', ['write-tree'], { cwd: repo, env, encoding: 'utf8' }).stdout.trim();+ rmSync(idx, { force: true });+ return tree;+}++describe('pregate-remote-gate.sh', () => {+ it('reports the working tree with untracked files and sums every summary lane', () => {+ writeFileSync(join(repo, 'new.txt'), 'untracked\n');+ writeFileSync(join(repo, 'ignored.txt'), 'scratch\n');+ const expected = contentTree();+ expect(expected).not.toBe(git('rev-parse', 'HEAD^{tree}'));+ const gate = join(repo, '.git', 'gate.sh');+ writeFileSync(gate, 'printf \'%s\\n\' "$SUMMARY_TEXT"; exit 1\n');+ const r = run(`bash ${gate}`, { SUMMARY_TEXT: SUMMARY });+ expect(r.out.trim().split('\n').at(-1)).toBe(`PREGATE_REMOTE: tree=${expected} tests=38 failed=2`);+ expect(r.code).toBe(1);+ });++ it('a green gate exits 0 with failed=0 and receives the extra arguments', () => {+ const gate = join(repo, '.git', 'gate.sh');+ writeFileSync(gate, `echo "args: $*"; printf '%s\\n' '=== ci-ubicloud summary ===' ' unit 4 passed, 0 failed, 1s compute'\n`);+ const r = spawnSync('bash', [SCRIPT, '--vms', '8'], {+ cwd: repo, encoding: 'utf8',+ env: { ...process.env, GIT_CONFIG_NOSYSTEM: '1', UBI_OWNER: 'gbra0test', GBRAIN_PREGATE_GATE_CMD: `bash ${gate}` } as Record<string, string>,+ });+ expect(r.stdout).toContain('args: --vms 8');+ expect(r.stdout.trim().split('\n').at(-1)).toBe(`PREGATE_REMOTE: tree=${contentTree()} tests=4 failed=0`);+ expect(r.status).toBe(0);+ });++ it('prints no receipt when the gate printed no summary', () => {+ const r = run('echo provisioning failed; exit 3');+ expect(r.out).not.toContain('PREGATE_REMOTE:');+ expect(r.err).toContain('no ci-ubicloud summary');+ expect(r.code).toBe(3);+ });++ it('prints no receipt when the working tree changes during the gate', () => {+ const r = run(`echo changed > moved.txt; printf '%s\\n' '=== ci-ubicloud summary ===' ' unit 4 passed, 0 failed, 1s compute'`);+ expect(r.out).not.toContain('PREGATE_REMOTE:');+ expect(r.err).toContain('changed during the gate');+ expect(r.code).toBe(1);+ });++ it('refuses before the gate without UBI_OWNER or when PREGATE_TREE is another tree', () => {+ const marker = join(repo, '.git', 'ran');+ const noOwner = run(`touch ${marker}`, { UBI_OWNER: '' });+ expect(noOwner.code).toBe(2);+ expect(noOwner.err).toContain('UBI_OWNER is not set');+ const stale = run(`touch ${marker}`, { PREGATE_TREE: '0'.repeat(40) });+ expect(stale.code).toBe(1);+ expect(stale.err).toContain('not the tree pregate selected');+ expect(spawnSync('test', ['-e', marker]).status).not.toBe(0);+ });+});