Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(eval): retire a reported stale-run lease once its group exits (#6776)

garrytanb101d6a6a4ee
diff --git a/.wave18-fragments/lane5.md b/.wave18-fragments/lane5.md
index 11d19a36e0..7a78ff3209 100644
--- a/.wave18-fragments/lane5.md
+++ b/.wave18-fragments/lane5.md
@@ -79,7 +79,7 @@ nothing is signalled across runs.
{ since: '<v>', text: 'A graduation intent marker that recorded a boot id or PID namespace this process cannot read now reads unknown, not alive, so a marker from another host or container is never taken as a live run because a local process shares its pid (#6774).' },
{ since: '<v>', text: '`scripts/eval-spend-guard.sh` runs the guarded command in its own process group and stops the whole group (helpers and grandchildren included) on an interrupt, after the kill grace, and when the command exits leaving processes behind. A terminal stdin is now EOF (the command cannot read the terminal from its own group); pipe or redirect input if it needs any (#6776).' },
- { since: '<v>', text: 'While its command runs, `scripts/eval-spend-guard.sh` keeps a run lease in `<ledger>.leases/<run_id>.lease` (run id, guard pid, process group id, host, boot id) and removes it once the run is reconciled. When a guard was SIGKILLed and its process group may still be running, the next guard on that host prints a STALE RUN report on stderr with the lease path, the pgid and the commands to inspect it (ps -o pid,pgid,cmd -g <pgid>) and stop it (kill -TERM -- -<pgid>), appends a cost-0 status "unknown" ledger row (stale_run_id, pgid, lease) and renames the lease to .reported. Nothing is killed automatically, and the stale run\'s reservation stays counted at its estimate (#6776).' },
+ { since: '<v>', text: 'While its command runs, `scripts/eval-spend-guard.sh` keeps a run lease in `<ledger>.leases/<run_id>.lease` (run id, guard pid, process group id, host, boot id) and removes it once the run is reconciled. When a guard was SIGKILLed and its process group may still be running, the next guard on that host prints a STALE RUN report on stderr with the lease path, the pgid and the commands to inspect it (ps -o pid,pgid,cmd -g <pgid>) and stop it (kill -TERM -- -<pgid>), appends a cost-0 status "unknown" ledger row (stale_run_id, pgid, lease) and renames the lease to .reported; a later start retires a .reported lease once its group has exited or the host rebooted (or remove it by hand). Nothing is killed automatically, and the stale run\'s reservation stays counted at its estimate (#6776).' },
{ since: '<v>', text: '`gbrain claw-test` runs each phase in its own process group: a phase timeout, SIGINT and SIGTERM stop everything the phase started, and SIGINT stops the running phase even when it was not sent from the terminal (a second SIGINT kills it) (#6775).' },
#6719's "checkpoint receipts become historical" row from plan-v2 is not
diff --git a/docs/eval-bench.md b/docs/eval-bench.md
index 552c9cac77..9a198b6cc1 100644
--- a/docs/eval-bench.md
+++ b/docs/eval-bench.md
@@ -779,8 +779,10 @@ reader pins and the judge pins all land on one receipt.
guard on that host reports it on stderr with the lease path, the pgid and
the commands to inspect (`ps -o pid,pgid,cmd -g <pgid>`) and stop it
(`kill -TERM -- -<pgid>`), and records a cost-0 `status: "unknown"`
- ledger row. It stops nothing itself, and that run's reservation stays
- counted at the estimate.
+ ledger row, once. It stops nothing itself, and that run's reservation
+ stays counted at the estimate. The lease is kept as `<lease>.reported`
+ and retired by a later guard once the group has exited (or by
+ `rm <lease>.reported`).
Row fields with `--judge`: exactly one of `judge_correct` / `judge_error`
(+ `judge_error_detail`) / `judge_skipped`, plus `judge_model`,
diff --git a/scripts/eval-spend-guard.sh b/scripts/eval-spend-guard.sh
index 5b5913057c..e2e51bf904 100755
--- a/scripts/eval-spend-guard.sh
+++ b/scripts/eval-spend-guard.sh
@@ -203,7 +203,8 @@ group_alive() { # <pgid>; falls back to kill -0 where ps cannot list processes
# boot id that cannot be read, and the group still has members), it reports
# the lease path, the pgid and the exact inspect/stop commands, appends a
# cost-0 status "unknown" row (no run_id, so the stale reservation stays
-# counted at its estimate) and renames the lease to .reported. It never
+# counted at its estimate) and renames the lease to .reported, which a later
+# start removes once the group is gone (or the host rebooted). It never
# signals anything: whether those processes are still that run's is the
# operator's call. A lease whose group is gone (or whose host rebooted) is
# removed; another host's lease is left alone.
@@ -215,7 +216,7 @@ BOOT_ID="$(cat /proc/sys/kernel/random/boot_id 2>/dev/null || sysctl -n kern.boo
report_stale_leases() {
local f k v run pid pgid host boot
[ -d "$LEASES" ] || return 0
- for f in "$LEASES"/*.lease; do
+ for f in "$LEASES"/*.lease "$LEASES"/*.lease.reported; do
[ -f "$f" ] || continue
run=""; pid=""; pgid=""; host=""; boot=""
while IFS='=' read -r k v; do
@@ -226,12 +227,14 @@ report_stale_leases() {
case "$pid" in ''|*[!0-9]*) continue ;; esac
case "$pgid" in ''|*[!0-9]*) continue ;; esac
[ "$host" = "$GUARD_HOST" ] || continue
- kill -0 "$pid" 2>/dev/null && continue
+ case "$f" in *.reported) ;; *) kill -0 "$pid" 2>/dev/null && continue ;; esac
if [ "$boot" != "$BOOT_ID" ] && [ "$boot" != unknown ] && [ "$BOOT_ID" != unknown ]; then rm -f "$f"; continue; fi
if ! group_alive "$pgid"; then rm -f "$f"; continue; fi
+ case "$f" in *.reported) continue ;; esac
echo "eval-spend-guard: STALE RUN $run: its guard (pid $pid) is gone but its process group $pgid may still be running; nothing was stopped. Lease: $f" >&2
echo "eval-spend-guard: inspect: ps -o pid,pgid,cmd -g $pgid" >&2
echo "eval-spend-guard: stop it, only if those processes are that run's: kill -TERM -- -$pgid" >&2
+ echo "eval-spend-guard: the lease is kept as $f.reported and retired automatically once the group exits (or now: rm $f.reported)" >&2
printf '{"ts":"%s","status":"unknown","stale_run_id":"%s","pgid":%s,"lease":"%s","reason":"guard_gone_group_may_run","cost_usd":0}\n' \
"$(date -u +%Y-%m-%dT%H:%M:%SZ)" "$(json_escape "$run")" "$pgid" "$(json_escape "$f")" >> "$LEDGER" \
|| echo "eval-spend-guard: could not record the stale run in $LEDGER" >&2
diff --git a/test/eval-spend-guard.test.ts b/test/eval-spend-guard.test.ts
index 7a8586a55e..a37d5d6324 100644
--- a/test/eval-spend-guard.test.ts
+++ b/test/eval-spend-guard.test.ts
@@ -23,6 +23,7 @@ import {
chmodSync,
existsSync,
readdirSync,
+ renameSync,
} from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
@@ -765,6 +766,7 @@ describe('eval-spend-guard.sh stale run leases (r3-d1)', () => {
expect(next.stderr).toContain('ps -o pid,pgid,cmd -g ' + leader);
expect(next.stderr).toContain('kill -TERM -- -' + leader);
expect(next.stderr).toContain('nothing was stopped');
+ expect(next.stderr).toContain('retired automatically once the group exits (or now: rm ' + lease + '.reported)');
expect(unknownRows()).toEqual([expect.objectContaining({ stale_run_id: runId, pgid: leader, lease, cost_usd: 0, reason: 'guard_gone_group_may_run' })]);
expect(unknownRows()[0]).not.toHaveProperty('run_id');
expect(alive(leader)).toBe(true);
@@ -792,4 +794,27 @@ describe('eval-spend-guard.sh stale run leases (r3-d1)', () => {
expect(existsSync(foreign)).toBe(true);
expect(unknownRows()).toEqual([]);
});
+ test('a reported lease is retired on the next start once its group has exited, with no new ledger row; one whose group still runs is kept', async () => {
+ const host = spawnSync('hostname', { encoding: 'utf-8' }).stdout.trim();
+ const deadGuard = spawnSync('true').pid;
+ const deadGroup = spawnSync('true').pid;
+ const live = spawn('sleep', ['30'], { detached: true, stdio: 'ignore' });
+ try {
+ await waitFor(() => alive(live.pid ?? 0), 'the live group');
+ const exited = writeLease('exited-run', { run_id: 'exited-run', guard_pid: deadGuard, pgid: deadGroup, host, boot_id: 'unknown' }) + '.reported';
+ renameSync(exited.replace(/\.reported$/, ''), exited);
+ const running = writeLease('running-run', { run_id: 'running-run', guard_pid: deadGuard, pgid: live.pid!, host, boot_id: 'unknown' }) + '.reported';
+ renameSync(running.replace(/\.reported$/, ''), running);
+ const r = run(['10', '1', '--', 'true']);
+ expect(r.status).toBe(0);
+ expect(r.stderr).not.toContain('STALE RUN');
+ expect(existsSync(exited)).toBe(false);
+ expect(existsSync(running)).toBe(true);
+ expect(alive(live.pid!)).toBe(true);
+ expect(unknownRows()).toEqual([]);
+ } finally {
+ killQuietly(live.pid ?? 0);
+ }
+ }, 20_000);
});
+