Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
ci: darwin heartbeat posts one context per cell, retries its terminal post, and ends an in-flight pending before it
garrytan513caa4b209c
diff --git a/.github/workflows/native-locks.yml b/.github/workflows/native-locks.ymlindex 10d239e5cd..b6dc24784f 100644--- a/.github/workflows/native-locks.yml+++ b/.github/workflows/native-locks.yml@@ -63,7 +63,7 @@ jobs:name: ${{ matrix.target }} / Bun ${{ matrix.bun }}runs-on: ${{ fromJSON('{"linux-x64-glibc":"ubicloud-standard-4-ubuntu-2404","linux-arm64-glibc":"ubicloud-standard-4-arm-ubuntu-2404","darwin-arm64":"macos-26","darwin-x64":"macos-26-intel","win32-x64":"windows-2022","win32-arm64":"windows-11-arm"}')[matrix.target] }}timeout-minutes: 15- # statuses: the darwin-x64 heartbeat below (one commit status context, diag/native-heartbeat).+ # statuses: the darwin-x64 heartbeat below (one commit status context per cell, diag/native-heartbeat/<target>/<bun>).permissions:contents: readstatuses: write@@ -84,7 +84,7 @@ jobs:- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262# A wedged darwin-x64 runner uploads no log at all (#7457's cell, #7419, #7303, #7238), so a background loop posts# the current step or suite, free memory, swap, load and the busiest process names as the commit status- # diag/native-heartbeat; GitHub keeps every status, so the last one before a wedge survives it. Nothing on a+ # diag/native-heartbeat/<cell>; GitHub keeps every status, so the last one before a wedge survives it. Nothing on a# fork's pull request, whose token cannot write.- name: Start the diagnostic heartbeatif: matrix.target == 'darwin-x64'diff --git a/.github/workflows/test.yml b/.github/workflows/test.ymlindex 9d0360e326..d3b34093fd 100644--- a/.github/workflows/test.yml+++ b/.github/workflows/test.yml@@ -1049,7 +1049,7 @@ jobs:needs: [changes]if: ${{ needs.changes.outputs.reuse != 'true' && ((github.event_name != 'pull_request' || needs.changes.outputs.native-scope != 'smoke') && (github.event_name != 'workflow_dispatch' || (inputs.race_hunt != true && !inputs.stress_files && !inputs.stress_base))) }}uses: ./.github/workflows/native-locks.yml- # statuses: the darwin-x64 cell's diag/native-heartbeat commit status (native-locks.yml).+ # statuses: the darwin-x64 cells' diag/native-heartbeat/<cell> commit statuses (native-locks.yml).permissions:contents: readstatuses: writediff --git a/scripts/native/heartbeat.sh b/scripts/native/heartbeat.shindex 6396327d46..db16b8e93a 100755--- a/scripts/native/heartbeat.sh+++ b/scripts/native/heartbeat.sh@@ -2,13 +2,15 @@# scripts/native/heartbeat.sh: the darwin-x64 native-locks heartbeat.## A wedged macOS runner uploads no log at all, not even for the steps that passed, so while the job runs a background-# loop posts its state as the commit status `diag/native-heartbeat`. GitHub keeps every status a commit receives, so the+# loop posts its state as the commit status `diag/native-heartbeat/<cell>`, one context per cell so two cells never+# overwrite each other. GitHub keeps every status a commit receives, so the# last one before a wedge shows what was running. A status carries only the cell, the current step or suite and its# elapsed seconds, free memory, swap in use, the load average and the busiest process names; never the environment.## heartbeat.sh start start the loop (it posts on every label change and every GBRAIN_HEARTBEAT_INTERVAL_S)# heartbeat.sh mark LABEL the current step or suite; needs no token, the loop posts it-# heartbeat.sh stop RESULT end the loop and post the terminal `success` status, so nothing is left pending+# heartbeat.sh stop RESULT end the loop, its in-flight post included, then post the terminal `success` status (retried+# on a failed request), so nothing is left pending# heartbeat.sh describe print the description the next status would carry## start and stop read GH_TOKEN, GITHUB_REPOSITORY, GBRAIN_HEARTBEAT_SHA and GBRAIN_HEARTBEAT_CELL. Every command does@@ -16,11 +18,12 @@# bash 3.2 compatible.set -u-CONTEXT='diag/native-heartbeat'dir="${GBRAIN_HEARTBEAT_DIR:-${RUNNER_TEMP:-/tmp}/gbrain-native-heartbeat}"interval="${GBRAIN_HEARTBEAT_INTERVAL_S:-30}"safe() { tr -cd 'A-Za-z0-9._/-' | cut -c1-"$1"; }+cell=$(printf '%s' "${GBRAIN_HEARTBEAT_CELL:-}" | safe 32)+CONTEXT="diag/native-heartbeat${cell:+/$cell}"mem_free_pct() {if command -v memory_pressure >/dev/null 2>&1; then@@ -50,21 +53,20 @@ top_names() {}describe() {- local label since now cell+ local label since nowlabel=$(cat "$dir/label" 2>/dev/null || echo setup)since=$(cat "$dir/since" 2>/dev/null || date +%s)now=$(date +%s)- cell=$(printf '%s' "${GBRAIN_HEARTBEAT_CELL:-}" | safe 32)printf '%s %s %ss | free %s%% | swap %s | load %s | top %s' "$cell" "$(printf '%s' "${label##*/}" | safe 48)" "$((now - since))" \"$(mem_free_pct)" "$(swap_used)" "$(load_1m)" "$(top_names)" | tr -cd 'A-Za-z0-9 ._/|%:,=+-' | cut -c1-140}post() {local url="${GITHUB_SERVER_URL:-https://github.com}/${GITHUB_REPOSITORY:-}/actions/runs/${GITHUB_RUN_ID:-}"- curl -sS --max-time 10 -o /dev/null -X POST \+ curl -fsS --max-time 10 -o /dev/null -X POST \-H "Authorization: Bearer ${GH_TOKEN:-}" -H 'Accept: application/vnd.github+json' \"${GITHUB_API_URL:-https://api.github.com}/repos/${GITHUB_REPOSITORY:-}/statuses/${GBRAIN_HEARTBEAT_SHA:-}" \- -d "{\"state\":\"$1\",\"context\":\"$CONTEXT\",\"description\":\"$2\",\"target_url\":\"$url\"}" >/dev/null 2>&1 || true+ -d "{\"state\":\"$1\",\"context\":\"$CONTEXT\",\"description\":\"$2\",\"target_url\":\"$url\"}" >/dev/null 2>&1}loop() {@@ -74,7 +76,7 @@ loop() {label=$(cat "$dir/label" 2>/dev/null)now=$(date +%s)if [ "$label" != "$last" ] || [ "$now" -ge "$next" ]; then- post pending "$(describe)"+ post pending "$(describe)" || truelast=$labelnext=$((now + interval))fi@@ -87,6 +89,8 @@ case "${1:-}" instart)mkdir -p "$dir" && rm -f "$dir/stopped"echo setup > "$dir/label"; date +%s > "$dir/since"+ # Its own process group (job control on), so stop can end the loop together with a post it has in flight.+ set -mloop </dev/null >/dev/null 2>&1 &echo $! > "$dir/pid";;@@ -96,8 +100,15 @@ case "${1:-}" in;;stop)touch "$dir/stopped"- pid=$(cat "$dir/pid" 2>/dev/null) && kill "$pid" 2>/dev/null- post success "$(printf '%s finished %s |%s' "$(printf '%s' "${GBRAIN_HEARTBEAT_CELL:-}" | safe 32)" "$(printf '%s' "${2:-}" | safe 16)" "$(describe | cut -d'|' -f2-)" | cut -c1-140)"+ if pid=$(cat "$dir/pid" 2>/dev/null) && [ -n "$pid" ]; then+ kill -TERM -- "-$pid" 2>/dev/null+ for _ in 1 2 3 4 5 6 7 8 9 10; do kill -0 -- "-$pid" 2>/dev/null || break; sleep 0.5; done+ fi+ final=$(printf '%s finished %s |%s' "$cell" "$(printf '%s' "${2:-}" | safe 16)" "$(describe | cut -d'|' -f2-)" | cut -c1-140)+ for attempt in 1 2 3; do+ post success "$final" && break+ [ "$attempt" = 3 ] || sleep 2+ done;;describe) describe; echo ;;*) echo "usage: heartbeat.sh start | mark LABEL | stop RESULT | describe" >&2; exit 2 ;;diff --git a/test/scripts/native-heartbeat.test.ts b/test/scripts/native-heartbeat.test.tsindex 62b761cbd4..a01e0dbe2b 100644--- a/test/scripts/native-heartbeat.test.ts+++ b/test/scripts/native-heartbeat.test.ts@@ -1,6 +1,6 @@/*** scripts/native/heartbeat.sh: the darwin-x64 native-locks heartbeat. A wedged macOS runner uploads no log, so the job- * posts its state as the commit status diag/native-heartbeat while it runs. A fake curl on PATH records every post.+ * posts its state as the commit status diag/native-heartbeat/<cell> while it runs. A fake curl on PATH records every post.*/import { afterAll, describe, expect, test } from 'bun:test';import { chmodSync, existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'fs';@@ -21,9 +21,14 @@ function sandbox() {const bin = join(root, 'bin');const log = join(root, 'posts.ndjson');Bun.spawnSync(['mkdir', '-p', bin]);+ // The fake records a post when it lands. CURL_PENDING_DELAY_S holds each pending post that long first (a slow API);+ // a number in <root>/fail-success makes that many success posts fail the way a timed-out curl does.writeFileSync(join(bin, 'curl'), `#!/usr/bin/env bashurl="" data=""while [ $# -gt 0 ]; do case "$1" in -d) data="$2"; shift 2 ;; -H|-o|-X|--max-time) shift 2 ;; -*) shift ;; *) url="$1"; shift ;; esac; done+case "$data" in *'"state":"pending"'*) sleep "\${CURL_PENDING_DELAY_S:-0}" ;; esac+fails=$(cat "${root}/fail-success" 2>/dev/null || echo 0)+case "$data" in *'"state":"success"'*) if [ "$fails" -gt 0 ]; then echo $((fails - 1)) > "${root}/fail-success"; exit 28; fi ;; esacprintf '{"url":"%s","body":%s}\\n' "$url" "$data" >> "${log}"`);chmodSync(join(bin, 'curl'), 0o755);@@ -43,7 +48,7 @@ printf '{"url":"%s","body":%s}\\n' "$url" "$data" >> "${log}"const alive = (pid: number) => { try { process.kill(pid, 0); return true; } catch { return false; } };describe('native heartbeat (diag/native-heartbeat)', () => {- test('posts pending heartbeats naming the current suite, then one terminal success, all on one context with no secret', async () => {+ test('posts pending heartbeats naming the current suite, then one terminal success, all on the context of its own cell with no secret', async () => {const box = sandbox();expect(box.run(['start']).exitCode).toBe(0);const pid = Number(readFileSync(join(box.root, 'state', 'pid'), 'utf8'));@@ -55,7 +60,7 @@ describe('native heartbeat (diag/native-heartbeat)', () => {const posts = box.posts();expect(posts.length).toBeGreaterThanOrEqual(3);for (const post of posts) {- expect(post.context).toBe('diag/native-heartbeat');+ expect(post.context).toBe('diag/native-heartbeat/darwin-x64/1.4.4');expect(post.url).toBe('https://api.github.com/repos/owner-example/repo-example/statuses/abc123');expect(post.target_url).toBe('https://github.com/owner-example/repo-example/actions/runs/42');expect(post.description.length).toBeLessThanOrEqual(140);@@ -70,6 +75,40 @@ describe('native heartbeat (diag/native-heartbeat)', () => {expect(posts.at(-1)!.description).toMatch(/^darwin-x64\/1\.4\.4 finished success \| /);}, 30_000);+ test('each cell posts on its own context, so one cell never overwrites another', async () => {+ const box = sandbox();+ for (const cell of ['darwin-x64/1.4.0', 'darwin-x64/1.4.4']) {+ expect(box.run(['start'], { GBRAIN_HEARTBEAT_CELL: cell, GBRAIN_HEARTBEAT_DIR: join(box.root, cell) }).exitCode).toBe(0);+ }+ await Bun.sleep(1500);+ expect(box.run(['stop', 'success'], { GBRAIN_HEARTBEAT_CELL: 'darwin-x64/1.4.4', GBRAIN_HEARTBEAT_DIR: join(box.root, 'darwin-x64/1.4.4') }).exitCode).toBe(0);+ await Bun.sleep(2500);+ expect(box.run(['stop', 'success'], { GBRAIN_HEARTBEAT_CELL: 'darwin-x64/1.4.0', GBRAIN_HEARTBEAT_DIR: join(box.root, 'darwin-x64/1.4.0') }).exitCode).toBe(0);+ const last = new Map(box.posts().map(post => [post.context, post]));+ expect([...last.keys()].sort()).toEqual(['diag/native-heartbeat/darwin-x64/1.4.0', 'diag/native-heartbeat/darwin-x64/1.4.4']);+ for (const post of last.values()) expect(post.state).toBe('success');+ }, 30_000);++ test('a terminal post that times out is retried, so the run still ends on success', async () => {+ const box = sandbox();+ expect(box.run(['start']).exitCode).toBe(0);+ await Bun.sleep(1500);+ writeFileSync(join(box.root, 'fail-success'), '2');+ expect(box.run(['stop', 'success']).exitCode).toBe(0);+ expect(box.posts().at(-1)!.state).toBe('success');+ }, 30_000);++ test('a pending post still in flight when the job stops never lands after the terminal success', async () => {+ const box = sandbox();+ expect(box.run(['start'], { CURL_PENDING_DELAY_S: '3' }).exitCode).toBe(0);+ await Bun.sleep(1000);+ expect(box.run(['stop', 'success'], { CURL_PENDING_DELAY_S: '3' }).exitCode).toBe(0);+ await Bun.sleep(4000);+ const posts = box.posts();+ expect(posts.at(-1)!.state).toBe('success');+ expect(posts.filter(post => post.state === 'success')).toHaveLength(1);+ }, 30_000);+test('does nothing when disabled (a fork pull request)', async () => {const box = sandbox();for (const args of [['start'], ['mark', 'test/x.test.ts'], ['stop', 'success']]) expect(box.run(args, { GBRAIN_HEARTBEAT_ENABLED: '0' }).exitCode).toBe(0);