Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix: Step 11 review fixes
garrytan07bb1f069651
diff --git a/docs/ci-event-parity.md b/docs/ci-event-parity.mdindex a2d941824c..a7b848c808 100644--- a/docs/ci-event-parity.md+++ b/docs/ci-event-parity.md@@ -117,9 +117,11 @@ since. A full runon the tree still in progress is waited for (90 minutes at most, a progressline every minute), also while the other workflow has no full run on the tree:full-ci's second dispatch can still appear, and a failure of the run in-progress refuses instead of starting VMs. A passed run whose executed-test receipt-is refused (an unreadable, stale or empty ledger) ends the wait at once, since no-later run makes reuse possible. The newest full run on the tree decides,+progress refuses instead of starting VMs. A passed run on the head whose+executed-test receipt is refused (an unreadable, stale or empty ledger) ends the+wait at once, since no later dispatch of the head is coming; the same refusal on+another head with the same tree keeps waiting for the head's own dispatch, and a+receipt download that failed (a 5xx or timeout) is read again on the next poll. The newest full run on the tree decides,across the head and every recent head with the same tree, so neither an emptycommit nor an older pass elsewhere skips a newer failure; a failed one refuseswith no receipt. A run that did not run the full job (a diagnostic dispatch, adiff --git a/scripts/ci-reuse.ts b/scripts/ci-reuse.tsindex ee7c9ef8c2..3c6fa3a8fc 100644--- a/scripts/ci-reuse.ts+++ b/scripts/ci-reuse.ts@@ -146,8 +146,9 @@ function accept(run: FullRun, tree: string, rejected: Rejection[], where: string* the identical tree reuses; a stamp-only difference goes through gstack-pregate's stamp rule, never through here.* The newest full run on the tree decides, across `head` and every other recent head with the same tree: in progress* waits, a failure refuses (no receipt, no VMs), cancelled or not a full run (its full job skipped or absent: a- * diagnostic dispatch, a push that reused) looks further back; a passed run whose executed-test receipt is refused is- * `rejected`, which no later run on the tree changes; anything else is `none` and the caller runs the VM gate.+ * diagnostic dispatch, a push that reused) looks further back; a passed run on `head` whose executed-test receipt is+ * refused is `rejected`, since no later dispatch of `head` is coming (on another same-tree head it is `none`: `head`'s+ * own dispatch can still appear); anything else is `none` and the caller runs the VM gate.*/export type GateState = 'reuse' | 'pending' | 'failed' | 'rejected' | 'none';/**@@ -162,7 +163,7 @@ export interface GateDeps extends ReuseDeps {/** The newest workflow_dispatch and push runs of `file`, whatever their status (a failed run counts against its tree too). */recentRuns(file: string): Promise<FullRun[]>;/** Executed (pass, fail) test identities in the run's executed-identities artifact, each job at its newest attempt (`ledgerAttempt` when an earlier attempt wrote it), or why they cannot be read. */- executed(run: FullRun): Promise<ExecutedCounts | { reason: string }>;+ executed(run: FullRun): Promise<ExecutedCounts | { reason: string; transient?: boolean }>;}export async function decideGate(deps: GateDeps, p: Profile, head: string, tree: string, repo: string): Promise<GateDecision> {@@ -186,7 +187,10 @@ export async function decideGate(deps: GateDeps, p: Profile, head: string, tree:continue;}const why = await reject(deps, p, run, repo, tree, tree);- if (!why) return await gateAccept(deps, p, run, tree, repo);+ if (!why) {+ const d = await gateAccept(deps, p, run, tree, repo);+ return d.state === 'rejected' && run.head_sha !== head ? { ...d, state: 'none' } : d;+ }if (why.reason === 'run_in_progress') return { state: 'pending', detail: `${p.workflow} ${why.detail}` };if (why.reason === 'run_failed' || why.reason === 'full_suite_not_success') return { state: 'failed', detail: `${p.workflow} ${why.detail}`, url: runUrl(repo, run) };rejected.push({ run_id: run.id, ...why });@@ -201,7 +205,7 @@ const runUrl = (repo: string, run: FullRun) => `https://github.com/${repo}/actioasync function gateAccept(deps: GateDeps, p: Profile, run: FullRun, tree: string, repo: string): Promise<GateDecision> {const got = await deps.executed(run);- if ('reason' in got) return { state: 'rejected', detail: `${p.workflow}: ${got.reason}` };+ if ('reason' in got) return { state: got.transient ? 'none' : 'rejected', detail: `${p.workflow}: ${got.reason}` };const { ledgerAttempt, ...counts } = got;// A re-run that leaves the ledger job alone carries it into the new attempt with its original start; a ledger job// that ran again but uploaded nothing leaves the older attempt's ledger behind, now stale.@@ -303,7 +307,7 @@ export function gateDeps(client: GitHubClient): GateDeps {},async executed(run) {const got = await fetchArtifactJson(client, run.id, 'executed-identities', 'executed-identities.json');- if (got.reason) return { reason: got.reason };+ if (got.reason) return got.transient ? { reason: got.reason, transient: true } : { reason: got.reason };const doc = got.value as { head?: { identities?: Array<{ status?: string }> }; issues?: Array<{ kind?: string }>; run?: { id?: unknown; attempt?: unknown } | null; job_latest?: { executed?: unknown; failed?: unknown } } | null;const ids = doc?.head?.identities;if (!Array.isArray(ids)) return { reason: `run ${run.id}'s executed-identities.json has no head.identities list` };diff --git a/scripts/lib/gh-issue.ts b/scripts/lib/gh-issue.tsindex fe6952c9a7..fd590089e0 100644--- a/scripts/lib/gh-issue.ts+++ b/scripts/lib/gh-issue.ts@@ -260,8 +260,8 @@ export function readZipEntry(zip: Uint8Array, name: string): Uint8Array | undefireturn undefined;}-/** One JSON file from a run's named artifact; `reason` says why it could not be read (missing, expired, unreadable). */-export async function fetchArtifactJson(client: GitHubClient, runId: number, artifact: string, file: string): Promise<{ value?: unknown; reason?: string }> {+/** One JSON file from a run's named artifact; `reason` says why it could not be read (missing, expired, unreadable), and `transient` marks a failed request that a later read may get past. */+export async function fetchArtifactJson(client: GitHubClient, runId: number, artifact: string, file: string): Promise<{ value?: unknown; reason?: string; transient?: boolean }> {if (!client.download) return { reason: 'this client cannot download artifacts' };try {const list = await client.request<{ artifacts: Array<{ id: number; name: string; expired: boolean }> }>('GET', `repos/{repo}/actions/runs/${runId}/artifacts?name=${encodeURIComponent(artifact)}&per_page=10`);@@ -272,6 +272,6 @@ export async function fetchArtifactJson(client: GitHubClient, runId: number, artif (!bytes) return { reason: `run ${runId}'s '${artifact}' artifact has no readable ${file}` };return { value: JSON.parse(new TextDecoder().decode(bytes)) };} catch (e) {- return { reason: `reading run ${runId}'s '${artifact}' artifact failed: ${e instanceof Error ? e.message : String(e)}` };+ return { reason: `reading run ${runId}'s '${artifact}' artifact failed: ${e instanceof Error ? e.message : String(e)}`, transient: true };}}diff --git a/scripts/ubicloud/ubi-runner.sh b/scripts/ubicloud/ubi-runner.shindex b23740bfd1..5b1d165644 100755--- a/scripts/ubicloud/ubi-runner.sh+++ b/scripts/ubicloud/ubi-runner.sh@@ -23,8 +23,9 @@# - UBI_REAP takes the usual false spellings (0/false/no/off) and refuses# anything it cannot read, instead of treating every value but 0 as on.# - down retries a destroy request that timed out or got a 5xx (also 408,-# 429 and 404) inside its poll loop until UBI_DOWN_TIMEOUT, and gives up-# early only on another 4xx; reap bounds each VM's wait with+# 429 and 404) inside its poll loop, backing off 1, 2, 4 ... 60 s, until+# UBI_DOWN_TIMEOUT, and gives up early only on another 4xx; reap bounds+# each VM's wait with# UBI_REAP_DOWN_TIMEOUT. Both are pending upstream (GSTA-36).## Scratch record: every VM this machine creates is appended to a VM-id file@@ -687,7 +688,7 @@ cmd_down() {fifi- local deadline=$(( $(date +%s) + DOWN_TIMEOUT )) seen=0 asked=0 out code now failed=""+ local deadline=$(( $(date +%s) + DOWN_TIMEOUT )) seen=0 asked=0 backoff=1 out code now failed=""out=$(mktemp)while :; donow=$(date +%s)@@ -704,6 +705,7 @@ cmd_down() {code=$(api "$out" vm "$loc/$name" destroy -f)if [ "$code" = 200 ]; thenasked=$now+ backoff=1failed=""log "destroy requested: $loc/$name"elif [[ "$code" =~ ^4[0-9][0-9]$ ]] && [ "$code" != 404 ] && [ "$code" != 408 ] && [ "$code" != 429 ]; then@@ -714,6 +716,8 @@ cmd_down() {else[ "$code" = "$failed" ] || log "destroy request for $loc/$name got HTTP $code; retrying until ${DOWN_TIMEOUT}s have passed"failed=$code+ asked=$(( now - 60 + backoff ))+ backoff=$(( backoff * 2 > 60 ? 60 : backoff * 2 ))fifielif [ "$code" = 404 ]; thendiff --git a/test/helpers/mock-ubicloud-api.ts b/test/helpers/mock-ubicloud-api.tsindex 31df109d6c..b62a5f63cb 100644--- a/test/helpers/mock-ubicloud-api.ts+++ b/test/helpers/mock-ubicloud-api.ts@@ -37,6 +37,8 @@ export interface MockUbicloud {events: string[];/** Flip to make every later `destroy` (500) or `list` (503) fail. */faults: { destroy: boolean; list: boolean };+ /** Every destroy request received, answered or not. */+ destroyRequests: number;stop(): void;}@@ -48,6 +50,7 @@ export function startMockUbicloud(opts: MockUbicloudOptions = {}): MockUbicloudvms.set(vm.name, { location: vm.location ?? "eu-central-h1", name: vm.name, size: vm.size ?? "standard-16", state: "creating" });}const text = (body: string, status = 200) => new Response(body, { status, headers: { "content-type": "text/plain" } });+ const api = { vms, events, faults, destroyRequests: 0 } as MockUbicloud;const server = Bun.serve({port: 0,hostname: "127.0.0.1",@@ -78,6 +81,7 @@ export function startMockUbicloud(opts: MockUbicloudOptions = {}): MockUbicloudreturn text(`id: vm-${name}\nname: ${name}\nstate: ${vm.state}\nlocation: ${vm.location}\nsize: ${vm.size}\n${vm.state === "running" ? "ip4: 192.0.2.1\n" : ""}`);}if (verb === "destroy") {+ api.destroyRequests++;const fault = opts.destroyFaults?.shift();if (fault === "hang") {await Bun.sleep(3000);@@ -98,7 +102,8 @@ export function startMockUbicloud(opts: MockUbicloudOptions = {}): MockUbicloudreturn text(`! unsupported: ${argv.join(" ")}`, 400);},});- return { url: `http://127.0.0.1:${server.port}`, vms, events, faults, stop: () => server.stop(true) };+ Object.assign(api, { url: `http://127.0.0.1:${server.port}`, stop: () => server.stop(true) });+ return api;}export async function waitForEvent(mock: MockUbicloud, pattern: RegExp, timeoutMs = 20_000): Promise<string> {diff --git a/test/scripts/ci-reuse.test.ts b/test/scripts/ci-reuse.test.tsindex 554fca5152..67cfbb16a8 100644--- a/test/scripts/ci-reuse.test.ts+++ b/test/scripts/ci-reuse.test.ts@@ -337,7 +337,7 @@ describe('ci-reuse remote-gate decision', () => {const run = (file: File, over: Partial<FullRun> = {}): FullRun & { file: File } => ({ ...prRun({ id: file === 'test.yml' ? 600 : 700, head_sha: A, ...over }), file });const tree = (r: FullRun & { file: File }, over: Record<string, unknown> = {}) => ({ ...treeJson(r, { workflow: files[r.file].workflow }), ...over });- function gate(o: { commits?: Record<string, Commit>; runs: Array<FullRun & { file: File }>; artifacts?: Record<number, unknown>; jobs?: Record<string, JobRow[]>; executed?: Record<number, ExecutedCounts | { reason: string }> }): GateDeps {+ function gate(o: { commits?: Record<string, Commit>; runs: Array<FullRun & { file: File }>; artifacts?: Record<number, unknown>; jobs?: Record<string, JobRow[]>; executed?: Record<number, ExecutedCounts | { reason: string; transient?: boolean }> }): GateDeps {const commits = o.commits ?? { [A]: { tree: T, parents: [BASE] } };const art = o.artifacts ?? Object.fromEntries(o.runs.map(r => [r.id, tree(r)]));return {@@ -534,6 +534,35 @@ describe('ci-reuse remote-gate decision', () => {}});+ test('a receipt that could not be read (a GitHub 5xx or timeout) is not a refusal: the wait polls again', async () => {+ const live = run('e2e.yml', { status: 'in_progress', conclusion: null });+ const deps = gate({ runs: [run('test.yml'), live], executed: { 600: { reason: "reading run 600's 'executed-identities' artifact failed: HTTP 503", transient: true } } });+ expect(await decideGate(deps, PROFILES.test, A, T, REPO)).toMatchObject({ state: 'none' });+ const r = await go(deps, A, T, 3 * 60_000);+ expect(r.code).toBe(4);+ expect(r.lines.filter(l => l.includes('waiting for')).length).toBeGreaterThanOrEqual(3);+ });++ test('a refused receipt on another same-tree head does not end the wait: the head\'s own dispatch can still appear', async () => {+ const C2 = '8'.repeat(40);+ const commits = { [A]: { tree: T, parents: [BASE] }, [C2]: { tree: T, parents: [BASE] } };+ const older = run('test.yml', { id: 560, head_sha: C2 });+ const own = run('test.yml', { status: 'in_progress', conclusion: null });+ const live = run('e2e.yml', { status: 'in_progress', conclusion: null });+ let polls = 0;+ const deps = gate({ commits, runs: [older, own, live], executed: { 560: { reason: 'no executed-identities artifact' } } });+ const lagging = { ...deps, async runsForHead(file: string, sha: string) {+ if (file === 'test.yml' && sha === A && ++polls >= 3) Object.assign(own, { status: 'completed', conclusion: 'success' }), Object.assign(live, { status: 'completed', conclusion: 'success' });+ const rows = await deps.runsForHead(file, sha);+ return file === 'test.yml' && sha === A && polls < 2 ? rows.filter(r => r !== own) : rows;+ } };+ expect(await decideGate(gate({ commits, runs: [older, live], executed: { 560: { reason: 'no executed-identities artifact' } } }), PROFILES.test, A, T, REPO)).toMatchObject({ state: 'none', detail: expect.stringContaining('no executed-identities artifact') });+ const r = await go(lagging, A, T, 90 * 60_000);+ expect(r.code).toBe(0);+ expect(r.lines.some(l => l.includes('waiting for'))).toBe(true);+ expect(r.last).toBe('GBRAIN_REUSE_RESULT tests=1300 failed=0');+ });+test('a same-tree other head whose newest run skipped the full job, or was cancelled, looks further back (#7927)', async () => {const C2 = '8'.repeat(40);const commits = { [A]: { tree: T, parents: [BASE] }, [C2]: { tree: T, parents: [A] } };diff --git a/test/scripts/ubi-runner-run.test.ts b/test/scripts/ubi-runner-run.test.tsindex 80aa9968b5..130fb3c217 100644--- a/test/scripts/ubi-runner-run.test.ts+++ b/test/scripts/ubi-runner-run.test.ts@@ -257,6 +257,16 @@ describe("down retries a failed destroy request", () => {expect(api.vms.has("vm-a")).toBe(true);}, 60_000);+ it("a destroy that keeps failing backs off instead of resending every poll", async () => {+ const api = serve({ seed: [{ name: "vm-a" }] });+ api.faults.destroy = true;+ const r = await run(["down", "vm-a"], { UBI_DOWN_TIMEOUT: "4" });+ expect(r.code).toBe(1);+ expect(r.stderr).toContain("could not confirm eu-central-h1/vm-a is gone after 4s (last destroy request: HTTP 500)");+ expect(api.destroyRequests).toBeGreaterThanOrEqual(2);+ expect(api.destroyRequests).toBeLessThanOrEqual(4);+ }, 60_000);+it("UBI_REAP, UBI_DOWN_TIMEOUT and UBI_REAP_DOWN_TIMEOUT are checked only by the commands that read them", async () => {const api = serve({ seed: [{ name: "vm-a" }] });const stale = { UBI_REAP: "maybe", UBI_DOWN_TIMEOUT: "0", UBI_REAP_DOWN_TIMEOUT: "x" };