Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(smoke): bound smoke checks on hosts without GNU timeout (#5248)

time-attack593a1ac24031
diff --git a/scripts/smoke-test.sh b/scripts/smoke-test.sh
index 51618eca15..fcfa5bf1aa 100755
--- a/scripts/smoke-test.sh
+++ b/scripts/smoke-test.sh
@@ -22,6 +22,47 @@ FIXES=0
TOTAL=0
SKIPPED=0
+# Bounded command runner. GNU coreutils ships `timeout`; Homebrew installs it
+# as `gtimeout`; stock macOS has neither. Without one, a background watchdog
+# polls once a second, sends TERM (then KILL) at the deadline, and exits as
+# soon as the command does. The command's exit status is returned as-is.
+# GBRAIN_TEST_SMOKE_TIMEOUT_BIN=none forces the watchdog; it exists only for
+# test/smoke-test-worker.test.ts.
+TIMEOUT_BIN=""
+if [ "${GBRAIN_TEST_SMOKE_TIMEOUT_BIN:-}" != "none" ]; then
+ if command -v gtimeout >/dev/null 2>&1; then TIMEOUT_BIN="gtimeout"
+ elif command -v timeout >/dev/null 2>&1; then TIMEOUT_BIN="timeout"
+ fi
+fi
+
+run_with_timeout() {
+ local seconds="$1"
+ shift
+ if [ -n "$TIMEOUT_BIN" ]; then
+ "$TIMEOUT_BIN" "$seconds" "$@"
+ return $?
+ fi
+ "$@" &
+ local command_pid=$!
+ (
+ elapsed=0
+ while [ "$elapsed" -lt "$seconds" ]; do
+ sleep 1
+ kill -0 "$command_pid" 2>/dev/null || exit 0
+ elapsed=$((elapsed + 1))
+ done
+ kill -TERM "$command_pid" 2>/dev/null || exit 0
+ sleep 1
+ kill -KILL "$command_pid" 2>/dev/null || true
+ ) >/dev/null 2>&1 &
+ local watchdog_pid=$!
+ wait "$command_pid"
+ local result=$?
+ kill "$watchdog_pid" 2>/dev/null || true
+ wait "$watchdog_pid" 2>/dev/null || true
+ return "$result"
+}
+
timestamp() { date -u '+%Y-%m-%d %H:%M:%S'; }
pass() { TOTAL=$((TOTAL + 1)); echo "✅ $1"; echo "$(timestamp) PASS: $1" >> "$LOG"; }
fail() { TOTAL=$((TOTAL + 1)); FAILURES=$((FAILURES + 1)); echo "❌ $1"; echo "$(timestamp) FAIL: $1" >> "$LOG"; }
@@ -75,12 +116,12 @@ fi
# ── 2. GBrain CLI loads ────────────────────────────────────
if [ -n "$GBRAIN_DIR" ] && [ -n "$BUN_PATH" ]; then
- if timeout 15 "$BUN_PATH" run "$GBRAIN_DIR/src/cli.ts" --help >/dev/null 2>&1; then
+ if run_with_timeout 15 "$BUN_PATH" run "$GBRAIN_DIR/src/cli.ts" --help >/dev/null 2>&1; then
pass "GBrain CLI ($GBRAIN_DIR)"
else
# Auto-fix: reinstall deps
cd "$GBRAIN_DIR" && "$BUN_PATH" install --frozen-lockfile 2>/dev/null
- if timeout 15 "$BUN_PATH" run "$GBRAIN_DIR/src/cli.ts" --help >/dev/null 2>&1; then
+ if run_with_timeout 15 "$BUN_PATH" run "$GBRAIN_DIR/src/cli.ts" --help >/dev/null 2>&1; then
fixed "GBrain deps reinstalled"
pass "GBrain CLI (after dep fix)"
else
@@ -99,7 +140,7 @@ fi
# works with the DB down), then branch on doctor --json's connection check,
# with `gbrain db-repair --yes` as the auto-fix arm.
if [ -n "$GBRAIN_DIR" ] && [ -n "$BUN_PATH" ]; then
- ENGINE_JSON=$(DATABASE_URL="$DB_URL" GBRAIN_DATABASE_URL="$DB_URL" timeout 15 "$BUN_PATH" run "$GBRAIN_DIR/src/cli.ts" engine status --json 2>/dev/null)
+ ENGINE_JSON=$(DATABASE_URL="$DB_URL" GBRAIN_DATABASE_URL="$DB_URL" run_with_timeout 15 "$BUN_PATH" run "$GBRAIN_DIR/src/cli.ts" engine status --json 2>/dev/null)
# #4182: BSD grep (macOS) has no -P; keep this shipped smoke test POSIX-portable.
ENGINE_KIND=$(printf '%s\n' "$ENGINE_JSON" | sed -n 's/.*"effective_engine": *"\([a-z]*\)".*/\1/p' | head -1)
ENGINE_SRC=$(printf '%s\n' "$ENGINE_JSON" | sed -n 's/.*"db_url_source": *"\([^"]*\)".*/\1/p' | head -1)
@@ -108,7 +149,7 @@ if [ -n "$GBRAIN_DIR" ] && [ -n "$BUN_PATH" ]; then
if [ -z "$DB_URL" ] && [ "$ENGINE_KIND" != "pglite" ] && [ -z "$ENGINE_SRC" ]; then
fail "GBrain database — no DATABASE_URL, GBRAIN_DATABASE_URL or configured database URL (and the engine is not pglite)"
else
- run_doctor_json() { DATABASE_URL="$DB_URL" GBRAIN_DATABASE_URL="$DB_URL" timeout 30 "$BUN_PATH" run "$GBRAIN_DIR/src/cli.ts" doctor --json 2>/dev/null; }
+ run_doctor_json() { DATABASE_URL="$DB_URL" GBRAIN_DATABASE_URL="$DB_URL" run_with_timeout 30 "$BUN_PATH" run "$GBRAIN_DIR/src/cli.ts" doctor --json 2>/dev/null; }
DOCTOR_JSON=$(run_doctor_json)
CONN_STATUS=$(printf '%s\n' "$DOCTOR_JSON" | sed -n 's/.*"name": *"connection", *"status": *"\([a-z]*\)".*/\1/p' | head -1)
if [ "$CONN_STATUS" = "ok" ]; then
@@ -120,7 +161,7 @@ if [ -n "$GBRAIN_DIR" ] && [ -n "$BUN_PATH" ]; then
# 240s: the docker conn_refused arm's readiness poll alone can run
# ~165s worst-case; a 60s cap would SIGTERM the repair mid-poll and
# report failure while the container it just started is still warming.
- DATABASE_URL="$DB_URL" GBRAIN_DATABASE_URL="$DB_URL" timeout 240 "$BUN_PATH" run "$GBRAIN_DIR/src/cli.ts" db-repair --yes >> "$LOG" 2>&1
+ DATABASE_URL="$DB_URL" GBRAIN_DATABASE_URL="$DB_URL" run_with_timeout 240 "$BUN_PATH" run "$GBRAIN_DIR/src/cli.ts" db-repair --yes >> "$LOG" 2>&1
DOCTOR_JSON=$(run_doctor_json)
CONN_STATUS=$(printf '%s\n' "$DOCTOR_JSON" | sed -n 's/.*"name": *"connection", *"status": *"\([a-z]*\)".*/\1/p' | head -1)
if [ "$CONN_STATUS" = "ok" ]; then
@@ -140,7 +181,7 @@ if [ -n "$GBRAIN_DIR" ] && [ -n "$BUN_PATH" ] && [ -n "$DB_URL" ]; then
SUPERVISOR_RUNNING=0
LEGACY_WORKER_RUNNING=0
if DATABASE_URL="$DB_URL" GBRAIN_DATABASE_URL="$DB_URL" \
- timeout 15 "$BUN_PATH" run "$GBRAIN_DIR/src/cli.ts" jobs supervisor status --json >/dev/null 2>&1; then
+ run_with_timeout 15 "$BUN_PATH" run "$GBRAIN_DIR/src/cli.ts" jobs supervisor status --json >/dev/null 2>&1; then
SUPERVISOR_RUNNING=1
fi
if [ -f "$WORKER_PID_FILE" ] && kill -0 "$(cat "$WORKER_PID_FILE")" 2>/dev/null; then
diff --git a/test/smoke-test-worker.test.ts b/test/smoke-test-worker.test.ts
index ae8632e510..065e8bb10b 100644
--- a/test/smoke-test-worker.test.ts
+++ b/test/smoke-test-worker.test.ts
@@ -1,5 +1,5 @@
import { afterEach, describe, expect, test } from 'bun:test';
-import { chmodSync, existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'fs';
+import { chmodSync, existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, symlinkSync, writeFileSync } from 'fs';
import { tmpdir } from 'os';
import { join, resolve } from 'path';
import { spawnSync } from 'child_process';
@@ -8,7 +8,7 @@ const REPO = resolve(import.meta.dir, '..');
const SCRIPT = join(REPO, 'scripts', 'smoke-test.sh');
const tempDirs: string[] = [];
-function runSmoke(opts: { supervisorRunning: boolean; legacyPid?: number; configUrlOnly?: boolean; embeddingStatus?: string }) {
+function runSmoke(opts: { supervisorRunning: boolean; legacyPid?: number; configUrlOnly?: boolean; embeddingStatus?: string; forceWatchdog?: boolean; withoutTimeoutBinary?: boolean }) {
const dir = mkdtempSync(join(tmpdir(), 'gbrain-smoke-worker-'));
tempDirs.push(dir);
const fakeBun = join(dir, 'bun');
@@ -39,6 +39,7 @@ exit 0
`);
chmodSync(fakeBun, 0o755);
if (opts.legacyPid) writeFileSync(workerPid, `${opts.legacyPid}\n`);
+ const path = opts.withoutTimeoutBinary ? pathWithout(dir, ['timeout', 'gtimeout']) : process.env.PATH;
const result = spawnSync('bash', [SCRIPT], {
cwd: REPO,
@@ -61,11 +62,43 @@ exit 0
SMOKE_BUN_CALLS: calls,
SMOKE_WORKER_STARTED: workerStarted,
SMOKE_SUPERVISOR_RUNNING: opts.supervisorRunning ? '1' : '0',
+ PATH: path,
+ GBRAIN_TEST_SMOKE_TIMEOUT_BIN: opts.forceWatchdog ? 'none' : '',
},
});
return { ...result, calls, workerStarted, workerPid };
}
+/** A PATH that resolves every current binary except `names`, like a stock macOS host without GNU coreutils. */
+function pathWithout(dir: string, names: string[]): string {
+ const bin = join(dir, 'path-bin');
+ mkdirSync(bin);
+ const seen = new Set(names);
+ for (const entry of (process.env.PATH ?? '').split(':')) {
+ if (!entry || !existsSync(entry)) continue;
+ for (const name of readdirSync(entry)) {
+ if (seen.has(name)) continue;
+ seen.add(name);
+ symlinkSync(join(entry, name), join(bin, name));
+ }
+ }
+ return bin;
+}
+
+/** The smoke script's own timeout prelude and run_with_timeout, run in bash with the watchdog forced. */
+function runWatchdog(body: string) {
+ const script = readFileSync(SCRIPT, 'utf8');
+ const start = script.indexOf('TIMEOUT_BIN=""');
+ const end = script.indexOf('\n}\n', script.indexOf('run_with_timeout() {')) + 3;
+ const started = Date.now();
+ const result = spawnSync('bash', ['-c', `${script.slice(start, end)}\n${body}`], {
+ encoding: 'utf8',
+ timeout: 20_000,
+ env: { ...process.env, GBRAIN_TEST_SMOKE_TIMEOUT_BIN: 'none' },
+ });
+ return { ...result, elapsedMs: Date.now() - started };
+}
+
afterEach(() => {
for (const dir of tempDirs.splice(0)) rmSync(dir, { recursive: true, force: true });
});
@@ -113,3 +146,33 @@ describe('smoke-test database and embedding sources (#5063)', () => {
expect(result.stdout).toContain('doctor embedding_provider failed');
}, 30_000);
});
+
+describe('smoke-test bounded checks without GNU timeout (#5248)', () => {
+ test('a host with neither timeout nor gtimeout still runs every bounded check', () => {
+ const result = runSmoke({ supervisorRunning: true, withoutTimeoutBinary: true });
+ expect(result.status, `${result.stdout}\n${result.stderr}`).toBe(0);
+ expect(result.stderr).not.toContain('command not found');
+ expect(result.stdout).toContain('GBrain CLI');
+ expect(result.stdout).toContain('health score: 97/100');
+ expect(result.stdout).toContain('GBrain worker (supervisor-managed)');
+ expect(readFileSync(result.calls, 'utf8')).toContain('jobs supervisor status --json');
+ }, 30_000);
+
+ test('the forced watchdog path passes the same healthy run', () => {
+ const result = runSmoke({ supervisorRunning: true, forceWatchdog: true });
+ expect(result.status, `${result.stdout}\n${result.stderr}`).toBe(0);
+ expect(result.stdout).toContain('GBrain worker (supervisor-managed)');
+ }, 30_000);
+
+ test('the watchdog stops a hanging command at its deadline', () => {
+ const result = runWatchdog('run_with_timeout 1 sleep 30; echo "rc=$?"');
+ expect(result.stdout.trim()).toBe('rc=143');
+ expect(result.elapsedMs).toBeLessThan(6_000);
+ }, 30_000);
+
+ test('the watchdog returns a quick command\'s own exit status without waiting for the deadline', () => {
+ const result = runWatchdog('run_with_timeout 30 sh -c "exit 3"; echo "rc=$?"; run_with_timeout 30 true; echo "rc=$?"');
+ expect(result.stdout.trim().split('\n')).toEqual(['rc=3', 'rc=0']);
+ expect(result.elapsedMs).toBeLessThan(6_000);
+ }, 30_000);
+});