Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(doctor): run the bootstrap entry's git probes through localProbe and cap probe output at 1 MiB (#8990)
garrytan0ce958d649d0
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 921e55e888..2d03994232 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -10,6 +10,16 @@ credits are retained; no result has been reassigned to another provider. Originaidentifiers and attribution are available in the pre-removal Git revision`6040075c6cb95be5881cc2e1b76ef7d71f4e5d29` (retained on 2026-09-23).+## [Unreleased]++**`gbrain doctor` now ends on its `--budget-ms` deadline when `git` hangs in an OpenClaw workspace.** The bootstrap entry checks push health by running `git status`, `git branch` and `git rev-list` on the install receipt's workspace, and on a workspace whose last push failed. Those calls ran synchronously with a 10-second limit each. While one hung, nothing else in the process could run, so the run overran its budget and every later check, the cheap incident checks included, started with no time left. They now run asynchronously, like the home-dir probes since #8979. Each call is killed with SIGKILL, together with its process group, at 10 seconds or at the entry's deadline, whichever comes first, and a call that fails or is cut short reads as unverified, as before. Every local probe's output is also capped at 1 MiB again: a probe that prints more than that is killed and reads as absent, which is what `execFile`'s buffer limit did before #8979.++### Itemized changes++- `src/commands/doctor/local-probe.ts`: `localProbe` moved here from `checks/local-audits.ts`. It takes `ownTimeoutMs` (default 5 s) and `windowsShell` (git runs without cmd.exe on Windows), and it kills a probe and returns null once its stdout passes `LOCAL_PROBE_MAX_OUTPUT_BYTES` (1 MiB).+- `src/commands/doctor/bootstrap-checks.ts`: `bootstrapDoctorChecks(engine, entryDeadlineAt)` runs every git call through `localProbe` with a 10 s bound. `commitsAheadOfOrigin`, `treeMatchesOrigin` and `recordedPushFailureCheck` are async.+- Tests: `test/doctor-bootstrap-git-deadline-8990.test.ts`. A `git` stand-in that ignores SIGTERM and waits on a 6 s child is killed within a 1000 ms budget, on both the workspace probe and the recorded-failure probe, and neither it nor its child survives (each fails on the previous code, at 18 s and 12 s). A probe that floods stdout is killed within 1.5 s and reads as absent, one that prints 2 MiB and exits reads as absent, and one under the cap returns its whole output (the first two fail without the cap).+## [0.60.624.0] - 2026-11-09**`gbrain doctor` now ends on its `--budget-ms` deadline when a local probe hangs, and the cheap incident checks keep their share of the budget.** The `home_dir_in_worktree` entry, which runs before the connection checks, ran `npm root -g`, `which -a gbrain` and `crontab -l` synchronously, with 5-second limits on the first and last and none on `which`. While one of them hung, nothing else in the process could run, the deadline included. So a 2500 ms run took about 5100 ms, and every later check, `links_extraction_lag` and `persistence_capacity` included, came back unknown with `elapsed_ms: 0`. Those probes now run asynchronously and are killed with SIGKILL at the entry's deadline (5 s at most, less the 250 ms report reserve), so none of them outlives the run. The entries before the connection checks may use at most half of the time left when the check run starts, so the ops sections and the cheap incident checks keep the other half. `--only`, `--fast` and a run with no database connection still give those entries the whole budget. This was the flaky `#6803 doctor deadlines (postgres)` E2E case (#8979). It failed on every backend-matrix job that ran on the 20261026.1 runner image, where a probe ran into its 5 s limit.diff --git a/docs/architecture/key-files/doctor-budgets.md b/docs/architecture/key-files/doctor-budgets.mdindex 0323ea923b..3029c37e93 100644--- a/docs/architecture/key-files/doctor-budgets.md+++ b/docs/architecture/key-files/doctor-budgets.md@@ -3,4 +3,4 @@[Subsystem index](../KEY_FILES.md) · [Doctor commands](commands-2.md) · [continued](commands-2-continued.md). Read only the entries relevant to your change.Current behavior and load-bearing invariants; history belongs in Git and CHANGELOG.-- `src/commands/doctor/budget.ts` + `checks/ops-sections.ts` + `checks/orphan-estimate.ts` — doctor tiers and budgets (#6696 B1). `parseDoctorTier` (`--tier ops|quick|standard|deep`, a bad value is `invalid_params`), `parseBudgetMs` (`--budget-ms`, default 4000 under ops, 10000 under quick, none otherwise) and `startDoctorDeadline` build the `DoctorDeadline` the CLI starts before connecting. `runDoctorRegistry` runs the entries tagged `tier: 'ops'` (persistence capacity, terminal failures, managed sync movement, persistence compaction, serve build drift, schema-pack consistency, embedding state) right after the connection lane; under `--tier ops` it runs only those (each `executeRaw` gets the absolute `deadlineAt`, so `SET LOCAL statement_timeout` is the time left after the pool wait: `postgres-engine/bounded-statement.ts`), reports an entry marked `opsPglite: 'not_applicable'` as an `ok` info row on PGLite, and stays complete. Every other tier bounds each entry by `doctor.entry_budget_ms` (default 15000). An entry past its budget, an unknown reported after the overall deadline, or a connect the deadline ended becomes `budgetUnknownCheck` (warn, `details.health: 'unknown'`, `reason: 'budget'`, `exact: false`, a rerun-alone fix), never a clear result; every check carries `elapsed_ms`, and the report adds `tier`/`complete`/`unknown_sections`/`elapsed_ms`/`budget_ms` only when `--tier` or `--budget-ms` was given (`doctorRunSummary`). `doctorHealth(ctx)` memoizes `getHealth` for one run. `orphan_ratio` estimates from a `TABLESAMPLE SYSTEM` sample above `doctor.orphan_ratio.full_scan_max_pages` (default 50000) unless `--tier deep`. Capacity and compaction freshness live in `checks/ops-sections.ts`. On every tier each entry runs inside its own `StatementDeadline` (`src/core/statement-deadline.ts`: an AsyncLocalStorage scope plus `deadlineBoundEngine`, one stable Proxy per real engine that refuses any engine call past the deadline, carries the deadline to the server on Postgres `executeRaw`, and marks the scope expired on a cancel even when the check catches it; `engineCacheKey` maps the wrapper to the real engine for engine-keyed caches; PGLite gets only the pre-statement refusal). The budget-config read is one bounded statement (2 s). `CHEAP_INCIDENT_CHECKS` (by check name: persistence capacity, managed sync movement, `text_projection_readiness`, `partial_embedding`, `links_extraction_lag`) run right after the ops sections, before any full scan. #8979: with an engine and without `--fast` or `--only`, the entries before the connection lane stop at half the time left when the check run starts, so the ops sections and the cheap incident checks keep the other half. `local-audits.ts` `localProbe` runs the home-dir entry's `npm root -g`, `which -a gbrain` and `crontab -l` asynchronously and kills each with SIGKILL at the entry deadline (5 s at most), so a hung probe never holds the run past `--budget-ms`. `scanDeadline` clips the frontmatter walk and the fence census to the entry deadline less 250 ms, so they report partial results. `--only` with an explicit `--budget-ms` gives the selected entries the whole overall budget; `budgetUnknownCheck` names the limiting budget (`details.limit`: `entry` or `overall`) and fixes with `--only <check> --budget-ms N` (N ten times that budget; actor agent while N fits `AGENT_TOOL_LIMIT_MS` less 10 s, else actor user). `--budget-ms 0` is no overall budget. `default-budget.ts`: a plain non-interactive CLI `doctor --json` (stderr not a TTY; no `--budget-ms`, `--tier`, `--fix`, `--remediate` or `--remediation-plan`) caps its check run at `doctor.default_budget_ms` (file plane, default 90000, 0 off), started by `startDeferredDeadline` when the registry starts (connect and migrations are outside it), with one stderr line; the report then carries the tier fields. `capacity-memo.ts` `doctorCapacityForecast`: one forecast per run (shared by `persistence_capacity` and `persistence_request_growth`) read under the run's overall deadline and dropped from the memo on a deadline failure, which makes the waiting check unknown instead of a counter-only verdict. `persistence_session_timeouts` reads `SHOW statement_timeout` outside the entry deadline. Tests: `test/doctor-tier-6696.test.ts`, `test/doctor-day-zero.test.ts` (`--tier ops`), `test/e2e/doctor-ops-tier-postgres.test.ts`, `test/doctor-entry-deadline-6803.test.ts` (+ `test/e2e/doctor-entry-deadline-postgres.test.ts`), `test/doctor-default-budget-6803.test.ts`; bench `scripts/bench/doctor-scale.ts`. #7276: `scoredChecks` keeps over-budget unknowns out of every score (`unknown_count`); `derived_visibility` is its own entry (`test/doctor-unknown-score-7276.test.ts`).+- `src/commands/doctor/budget.ts` + `checks/ops-sections.ts` + `checks/orphan-estimate.ts` — doctor tiers and budgets (#6696 B1). `parseDoctorTier` (`--tier ops|quick|standard|deep`, a bad value is `invalid_params`), `parseBudgetMs` (`--budget-ms`, default 4000 under ops, 10000 under quick, none otherwise) and `startDoctorDeadline` build the `DoctorDeadline` the CLI starts before connecting. `runDoctorRegistry` runs the entries tagged `tier: 'ops'` (persistence capacity, terminal failures, managed sync movement, persistence compaction, serve build drift, schema-pack consistency, embedding state) right after the connection lane; under `--tier ops` it runs only those (each `executeRaw` gets the absolute `deadlineAt`, so `SET LOCAL statement_timeout` is the time left after the pool wait: `postgres-engine/bounded-statement.ts`), reports an entry marked `opsPglite: 'not_applicable'` as an `ok` info row on PGLite, and stays complete. Every other tier bounds each entry by `doctor.entry_budget_ms` (default 15000). An entry past its budget, an unknown reported after the overall deadline, or a connect the deadline ended becomes `budgetUnknownCheck` (warn, `details.health: 'unknown'`, `reason: 'budget'`, `exact: false`, a rerun-alone fix), never a clear result; every check carries `elapsed_ms`, and the report adds `tier`/`complete`/`unknown_sections`/`elapsed_ms`/`budget_ms` only when `--tier` or `--budget-ms` was given (`doctorRunSummary`). `doctorHealth(ctx)` memoizes `getHealth` for one run. `orphan_ratio` estimates from a `TABLESAMPLE SYSTEM` sample above `doctor.orphan_ratio.full_scan_max_pages` (default 50000) unless `--tier deep`. Capacity and compaction freshness live in `checks/ops-sections.ts`. On every tier each entry runs inside its own `StatementDeadline` (`src/core/statement-deadline.ts`: an AsyncLocalStorage scope plus `deadlineBoundEngine`, one stable Proxy per real engine that refuses any engine call past the deadline, carries the deadline to the server on Postgres `executeRaw`, and marks the scope expired on a cancel even when the check catches it; `engineCacheKey` maps the wrapper to the real engine for engine-keyed caches; PGLite gets only the pre-statement refusal). The budget-config read is one bounded statement (2 s). `CHEAP_INCIDENT_CHECKS` (by check name: persistence capacity, managed sync movement, `text_projection_readiness`, `partial_embedding`, `links_extraction_lag`) run right after the ops sections, before any full scan. #8979: with an engine and without `--fast` or `--only`, the entries before the connection lane stop at half the time left when the check run starts, so the ops sections and the cheap incident checks keep the other half. `local-probe.ts` `localProbe` runs the home-dir entry's `npm root -g`, `which -a gbrain` and `crontab -l`, and the bootstrap entry's `git` push-health probes (#8990), asynchronously and kills each, with its process group, at its own bound (5 s, 10 s for git) or the entry deadline, whichever comes first, so a hung probe never holds the run past `--budget-ms`; output past 1 MiB reads as absent. `scanDeadline` clips the frontmatter walk and the fence census to the entry deadline less 250 ms, so they report partial results. `--only` with an explicit `--budget-ms` gives the selected entries the whole overall budget; `budgetUnknownCheck` names the limiting budget (`details.limit`: `entry` or `overall`) and fixes with `--only <check> --budget-ms N` (N ten times that budget; actor agent while N fits `AGENT_TOOL_LIMIT_MS` less 10 s, else actor user). `--budget-ms 0` is no overall budget. `default-budget.ts`: a plain non-interactive CLI `doctor --json` (stderr not a TTY; no `--budget-ms`, `--tier`, `--fix`, `--remediate` or `--remediation-plan`) caps its check run at `doctor.default_budget_ms` (file plane, default 90000, 0 off), started by `startDeferredDeadline` when the registry starts (connect and migrations are outside it), with one stderr line; the report then carries the tier fields. `capacity-memo.ts` `doctorCapacityForecast`: one forecast per run (shared by `persistence_capacity` and `persistence_request_growth`) read under the run's overall deadline and dropped from the memo on a deadline failure, which makes the waiting check unknown instead of a counter-only verdict. `persistence_session_timeouts` reads `SHOW statement_timeout` outside the entry deadline. Tests: `test/doctor-tier-6696.test.ts`, `test/doctor-day-zero.test.ts` (`--tier ops`), `test/e2e/doctor-ops-tier-postgres.test.ts`, `test/doctor-entry-deadline-6803.test.ts` (+ `test/e2e/doctor-entry-deadline-postgres.test.ts`), `test/doctor-default-budget-6803.test.ts`; bench `scripts/bench/doctor-scale.ts`. #7276: `scoredChecks` keeps over-budget unknowns out of every score (`unknown_count`); `derived_visibility` is its own entry (`test/doctor-unknown-score-7276.test.ts`).diff --git a/src/commands/doctor/bootstrap-checks.ts b/src/commands/doctor/bootstrap-checks.tsindex ca2b4583c7..53420b0e4d 100644--- a/src/commands/doctor/bootstrap-checks.ts+++ b/src/commands/doctor/bootstrap-checks.ts@@ -5,7 +5,7 @@*/import { join } from 'path';import { existsSync } from 'fs';-import { execFileSync } from '../../core/spawn.ts';+import { localProbe } from './local-probe.ts';import type { BrainEngine } from '../../core/engine.ts';import { LATEST_VERSION } from '../../core/migrate.ts';// Agent-bootstrap doctor group (plan B2/B4/ENG-4 + one-live-serve note).@@ -44,14 +44,17 @@ async function pushSupersededEnrollmentCheck(checks: Check[], engine: BrainEnginreturn true;}+/** #8990: `git -C <cwd> ...` stdout, or null on failure or past its 10 s bound (clipped to the entry deadline). */+type GitProbe = (cwd: string, ...args: string[]) => Promise<string | null>;++function gitProbe(entryDeadlineAt: number | undefined): GitProbe {+ return (cwd, ...args) => localProbe(entryDeadlineAt, 'git', ['-C', cwd, ...args], { ownTimeoutMs: 10_000, windowsShell: false });+}+/** #5063: commits on the workspace's named branch that origin/<branch> lacks, counted whatever the push age (0 when unknown). */-function commitsAheadOfOrigin(ws: string, branch: string): number {+async function commitsAheadOfOrigin(git: GitProbe, ws: string, branch: string): Promise<number> {if (!branch) return 0;- try {- return parseInt(execFileSync('git', ['-C', ws, 'rev-list', '--count', `origin/${branch}..HEAD`], {- stdio: ['ignore', 'pipe', 'ignore'], timeout: 10_000,- }).toString().trim(), 10) || 0;- } catch { return 0; }+ return parseInt((await git(ws, 'rev-list', '--count', `origin/${branch}..HEAD`))?.trim() ?? '', 10) || 0;}/**@@ -72,14 +75,13 @@ function pushHealthCheck(root: string | null | undefined, status: 'warn' | 'fail}/** #6083: a tree with nothing uncommitted (ownership metadata aside) on a named branch, 0 commits ahead of a configured origin. */-function treeMatchesOrigin(root: string): boolean {- const g = (...args: string[]) => execFileSync('git', ['-C', root, ...args], { stdio: ['ignore', 'pipe', 'ignore'], timeout: 10_000 }).toString().trim();- try {- g('remote', 'get-url', 'origin');- const branch = g('branch', '--show-current');- return branch !== '' && withoutPhysicalRootMetadata(g('status', '--porcelain')) === ''- && g('rev-list', '--count', `origin/${branch}..HEAD`) === '0';- } catch { return false; }+async function treeMatchesOrigin(git: GitProbe, root: string): Promise<boolean> {+ if ((await git(root, 'remote', 'get-url', 'origin')) === null) return false;+ const branch = (await git(root, 'branch', '--show-current'))?.trim();+ if (!branch) return false;+ const status = await git(root, 'status', '--porcelain');+ if (status === null || withoutPhysicalRootMetadata(status.trim()) !== '') return false;+ return (await git(root, 'rev-list', '--count', `origin/${branch}..HEAD`))?.trim() === '0';}/**@@ -87,8 +89,9 @@ function treeMatchesOrigin(root: string): boolean {* its origin is superseded (nothing is unpushed, and on a managed worktree* nothing ever clears a refusal record); the first live failure decides.*/-function recordedPushFailureCheck(failing: PushStatusEntry[], ws: string | null): Check {- const live = failing.filter((s) => !s.repoRoot || !treeMatchesOrigin(s.repoRoot));+async function recordedPushFailureCheck(git: GitProbe, failing: PushStatusEntry[], ws: string | null): Promise<Check> {+ const live: PushStatusEntry[] = [];+ for (const s of failing) if (!s.repoRoot || !(await treeMatchesOrigin(git, s.repoRoot))) live.push(s);if (live.length === 0) {return { name: 'bootstrap_push_health', status: 'ok',message: `last recorded push failed for ${failing.map((s) => s.repoRoot).join(', ')} (${failing[0]!.ts ?? 'unknown'}), but the tree matches its origin branch with nothing uncommitted; nothing to push` };@@ -100,8 +103,9 @@ function recordedPushFailureCheck(failing: PushStatusEntry[], ws: string | null)` — run \`gbrain sources push${target ? ` --path ${target}` : ''}\``);}-export async function bootstrapDoctorChecks(engine: BrainEngine | null): Promise<Check[]> {+export async function bootstrapDoctorChecks(engine: BrainEngine | null, entryDeadlineAt?: number): Promise<Check[]> {const checks: Check[] = [];+ const git = gitProbe(entryDeadlineAt);let home: string;try {home = resolveGbrainHome();@@ -294,7 +298,7 @@ export async function bootstrapDoctorChecks(engine: BrainEngine | null): Promiseconst { PUSH_STALE_MS } = await import('../hook.ts'); // hook.ts owns the threshold (single source)const failing = pushStatuses.filter((s) => s.ok === false);if (failing.length > 0) {- checks.push(recordedPushFailureCheck(failing, ws));+ checks.push(await recordedPushFailureCheck(git, failing, ws));} else {const stamps = pushStatuses.map((s) => Date.parse(s.ts ?? '')).filter((t) => Number.isFinite(t));const stalest = stamps.length > 0 ? Math.min(...stamps) : NaN;@@ -318,61 +322,46 @@ export async function bootstrapDoctorChecks(engine: BrainEngine | null): Promise// itself failed): unverified must NOT be treated as clean below.let dirty = false, known = false, ahead = 0;if (ws) {- try {- const statusOut = withoutPhysicalRootMetadata(execFileSync('git', ['-C', ws, 'status', '--porcelain'], {- stdio: ['ignore', 'pipe', 'ignore'], timeout: 10_000,- }).toString());- const branch = execFileSync('git', ['-C', ws, 'branch', '--show-current'], { stdio: ['ignore', 'pipe', 'ignore'], timeout: 10_000 }).toString().trim();- ahead = commitsAheadOfOrigin(ws, branch);+ const statusRaw = await git(ws, 'status', '--porcelain');+ const branch = statusRaw === null ? null : (await git(ws, 'branch', '--show-current'))?.trim() ?? null;+ if (statusRaw !== null && branch !== null) {+ const statusOut = withoutPhysicalRootMetadata(statusRaw);+ ahead = await commitsAheadOfOrigin(git, ws, branch);if (statusOut.trim() !== '') {dirty = true;known = true;+ } else if (branch) {+ // origin/<branch> doesn't resolve — e.g. never pushed. Any+ // local commit on top of an empty tree still counts as+ // needing a push (this fallback only applies to a NAMED+ // branch with no upstream; see the detached-HEAD branch+ // below for why the same trick is unsafe there). Mirrors+ // treeNeedsPush's [hook.ts] existing "never pushed" fallback+ // as-is: a topic branch already fully contained in another+ // remote branch (e.g. origin/main) but never itself fetched+ // as origin/<branch> can over-report as dirty here — an+ // inherited, pre-existing edge case, left as a false FAIL+ // rather than a false OK, which is the safer direction for+ // this specific check.+ const countOut = (await git(ws, 'rev-list', '--count', `origin/${branch}..HEAD`)) ?? (await git(ws, 'rev-list', '--count', 'HEAD'));+ if (countOut !== null) {+ dirty = (parseInt(countOut.trim(), 10) || 0) > 0;+ known = true;+ }} else {- const branchOut = branch;- if (branchOut) {- try {- const aheadOut = execFileSync(- 'git', ['-C', ws, 'rev-list', '--count', `origin/${branchOut}..HEAD`],- { stdio: ['ignore', 'pipe', 'ignore'], timeout: 10_000 },- ).toString().trim();- dirty = (parseInt(aheadOut, 10) || 0) > 0;- known = true;- } catch {- // origin/<branch> doesn't resolve — e.g. never pushed. Any- // local commit on top of an empty tree still counts as- // needing a push (this fallback only applies to a NAMED- // branch with no upstream; see the detached-HEAD branch- // below for why the same trick is unsafe there). Mirrors- // treeNeedsPush's [hook.ts] existing "never pushed" fallback- // as-is: a topic branch already fully contained in another- // remote branch (e.g. origin/main) but never itself fetched- // as origin/<branch> can over-report as dirty here — an- // inherited, pre-existing edge case, left as a false FAIL- // rather than a false OK, which is the safer direction for- // this specific check.- const haveOut = execFileSync('git', ['-C', ws, 'rev-list', '--count', 'HEAD'], {- stdio: ['ignore', 'pipe', 'ignore'], timeout: 10_000,- }).toString().trim();- dirty = (parseInt(haveOut, 10) || 0) > 0;- known = true;- }- } else {- // Detached HEAD: `rev-list --count HEAD` counts ALL history- // reachable from HEAD, not just unpushed commits, so it is- // NOT a safe "never pushed" fallback here (unlike the named-- // branch case above) — it would flag any non-trivial repo as- // dirty even when HEAD is already contained by origin. If- // `@{u}` doesn't resolve, tree state is simply unverifiable.- try {- const aheadOut = execFileSync('git', ['-C', ws, 'rev-list', '--count', '@{u}..HEAD'], {- stdio: ['ignore', 'pipe', 'ignore'], timeout: 10_000,- }).toString().trim();- dirty = (parseInt(aheadOut, 10) || 0) > 0;- known = true;- } catch { known = false; }+ // Detached HEAD: `rev-list --count HEAD` counts ALL history+ // reachable from HEAD, not just unpushed commits, so it is+ // NOT a safe "never pushed" fallback here (unlike the named-+ // branch case above) — it would flag any non-trivial repo as+ // dirty even when HEAD is already contained by origin. If+ // `@{u}` doesn't resolve, tree state is simply unverifiable.+ const aheadOut = await git(ws, 'rev-list', '--count', '@{u}..HEAD');+ if (aheadOut !== null) {+ dirty = (parseInt(aheadOut.trim(), 10) || 0) > 0;+ known = true;}}- } catch { dirty = false; known = false; }+ }}if (dirty && ws && Date.now() - Date.parse(pushStatusForWorkspace(pushStatuses, ws)?.ts ?? '') > PUSH_STALE_MS) { // #5432: this root's own receiptchecks.push(pushHealthCheck(ws, 'fail', `last successful push ${pushStatusForWorkspace(pushStatuses, ws)?.ts} (>48h) with a DIRTY workspace tree — recent agent memory is unpushed [B4]`,diff --git a/src/commands/doctor/checks/local-audits.ts b/src/commands/doctor/checks/local-audits.tsindex 8fe5666a14..5a5b528935 100644--- a/src/commands/doctor/checks/local-audits.ts+++ b/src/commands/doctor/checks/local-audits.ts@@ -29,7 +29,7 @@ import type { Check } from '../../doctor.ts';import { withSkippedSegmented } from '../check-fix.ts';import type { DoctorContext, DoctorEntry } from '../context.ts';import { scanDeadline } from '../budget.ts';-import { currentStatementDeadline } from '../../../core/statement-deadline.ts';+import { localProbe } from '../local-probe.ts';async function runStubGuard(ctx: DoctorContext): Promise<Check[]> {const { engine, orphanRatioSourceId } = ctx;@@ -380,42 +380,6 @@ export const extractionBacklogsEntry: DoctorEntry = {run: runExtractionBacklogs,};-/**- * #8979: a local probe's stdout, or null when it fails or outlives its bound (5 s, clipped to the entry deadline less- * the report reserve; `entryDeadlineAt` is read when the entry starts). It runs asynchronously and is killed with- * SIGKILL at its bound (on POSIX its whole process group, so a wrapper's children die too), so a probe that hangs- * never blocks the event loop or outlives the entry's deadline. A probe- * the entry deadline cut short marks the entry's scope expired, so its checks report unknown instead of absent.- */-async function localProbe(entryDeadlineAt: number | undefined, file: string, args: string[]): Promise<string | null> {- const scope = currentStatementDeadline();- const ownEnd = Date.now() + 5_000;- const end = scanDeadline({ entryDeadlineAt }, 5_000);- const cutShort = () => { if (scope && end < ownEnd) scope.expired = true; return null; };- const timeout = end - Date.now();- if (timeout <= 0) return cutShort();- const { spawn } = await import('../../../core/spawn.ts');- const posix = process.platform !== 'win32';- return new Promise((resolve) => {- // nosemgrep: javascript.lang.security.audit.spawn-shell-true.spawn-shell-true -- file and args are constants from the three call sites (npm root -g, which -a gbrain, crontab -l); the shell is Windows-only, where npm resolves to npm.cmd, as with the execFile it replaced; trusted local CLI only- const child = spawn(file, args, { shell: !posix, detached: posix, stdio: ['ignore', 'pipe', 'ignore'] });- let out = '';- let done = false;- const finish = (value: string | null) => { if (done) return; done = true; clearTimeout(timer); resolve(value); };- const timer = setTimeout(() => {- if (done) return;- try { if (posix && child.pid) process.kill(-child.pid, 'SIGKILL'); else child.kill('SIGKILL'); } catch { child.kill('SIGKILL'); }- child.stdout?.destroy();- child.unref();- finish(cutShort());- }, timeout);- child.stdout?.setEncoding('utf8');- child.stdout?.on('data', (chunk: string) => { out += chunk; });- child.on('error', () => finish(null));- child.on('close', (code) => finish(code === 0 ? out : null));- });-}-async function runHomeDirInWorktree(ctx: DoctorContext): Promise<Check[]> {const checks: Check[] = [];const { entryDeadlineAt } = ctx;diff --git a/src/commands/doctor/checks/local-runtime.ts b/src/commands/doctor/checks/local-runtime.tsindex e44914d7d5..bf17af3f83 100644--- a/src/commands/doctor/checks/local-runtime.ts+++ b/src/commands/doctor/checks/local-runtime.ts@@ -24,14 +24,14 @@ import type { DoctorContext, DoctorEntry } from '../context.ts';import { checkError } from '../check-fix.ts';async function runBootstrapChecks(ctx: DoctorContext): Promise<Check[]> {- const { engine } = ctx;+ const { engine, entryDeadlineAt } = ctx;const checks: Check[] = [];// 2d. Agent-bootstrap health (plan B2/B4/ENG-4). Filesystem-first; the// one engine-dependent pairing check degrades gracefully when engine is// null. Emits NOTHING on machines with no bootstrap state, so ordinary// brains keep a clean doctor.- checks.push(...(await bootstrapDoctorChecks(engine)));+ checks.push(...(await bootstrapDoctorChecks(engine, entryDeadlineAt)));return checks;}diff --git a/src/commands/doctor/local-probe.ts b/src/commands/doctor/local-probe.tsnew file mode 100644index 0000000000..8e20ac660e--- /dev/null+++ b/src/commands/doctor/local-probe.ts@@ -0,0 +1,53 @@+import { scanDeadline } from './budget.ts';+import { currentStatementDeadline } from '../../core/statement-deadline.ts';++/** #8990: past this many bytes of stdout a local probe is killed and reads as absent (execFile's old maxBuffer). */+export const LOCAL_PROBE_MAX_OUTPUT_BYTES = 1024 * 1024;++/**+ * #8979: a local probe's stdout, or null when it fails, outlives its bound or prints more than+ * LOCAL_PROBE_MAX_OUTPUT_BYTES. The bound is `ownTimeoutMs` (5 s by default), clipped to the entry deadline less the+ * report reserve; `entryDeadlineAt` is read when the entry starts. It runs asynchronously and is killed with SIGKILL at+ * its bound (on POSIX its whole process group, so a wrapper's children die too), so a probe that hangs never blocks the+ * event loop or outlives the entry's deadline. A probe the entry deadline cut short marks the entry's scope expired, so+ * its checks report unknown instead of absent. `windowsShell: false` runs the file without cmd.exe on Windows (git takes+ * paths that may hold spaces).+ */+export async function localProbe(+ entryDeadlineAt: number | undefined, file: string, args: string[], { ownTimeoutMs = 5_000, windowsShell = true } = {},+): Promise<string | null> {+ const scope = currentStatementDeadline();+ const ownEnd = Date.now() + ownTimeoutMs;+ const end = scanDeadline({ entryDeadlineAt }, ownTimeoutMs);+ const cutShort = () => { if (scope && end < ownEnd) scope.expired = true; return null; };+ const timeout = end - Date.now();+ if (timeout <= 0) return cutShort();+ const { spawn } = await import('../../core/spawn.ts');+ const posix = process.platform !== 'win32';+ return new Promise((resolve) => {+ // nosemgrep: javascript.lang.security.audit.spawn-shell-true.spawn-shell-true -- file and args come from doctor's own call sites (npm root -g, which -a gbrain, crontab -l, git -C <receipt workspace>); the shell is Windows-only and off for git (windowsShell: false), where npm resolves to npm.cmd, as with the execFile it replaced; trusted local CLI only+ const child = spawn(file, args, { shell: !posix && windowsShell, detached: posix, stdio: ['ignore', 'pipe', 'ignore'] });+ const chunks: Buffer[] = [];+ let bytes = 0;+ let done = false;+ const finish = (value: string | null) => { if (done) return; done = true; clearTimeout(timer); resolve(value); };+ const kill = () => {+ try { if (posix && child.pid) process.kill(-child.pid, 'SIGKILL'); else child.kill('SIGKILL'); } catch { child.kill('SIGKILL'); }+ child.stdout?.destroy();+ child.unref();+ };+ const timer = setTimeout(() => {+ if (done) return;+ kill();+ finish(cutShort());+ }, timeout);+ child.stdout?.on('data', (chunk: Buffer) => {+ bytes += chunk.length;+ if (bytes <= LOCAL_PROBE_MAX_OUTPUT_BYTES) { chunks.push(chunk); return; }+ kill();+ finish(null);+ });+ child.on('error', () => finish(null));+ child.on('close', (code) => finish(code === 0 ? Buffer.concat(chunks).toString('utf8') : null));+ });+}diff --git a/test/doctor-bootstrap-git-deadline-8990.test.ts b/test/doctor-bootstrap-git-deadline-8990.test.tsnew file mode 100644index 0000000000..b872764912--- /dev/null+++ b/test/doctor-bootstrap-git-deadline-8990.test.ts@@ -0,0 +1,105 @@+/**+ * #8990: the bootstrap entry's git probes (push health on the receipt's workspace, and the recorded-failure+ * superseded check) run asynchronously and are killed at the entry's deadline, so a git call that hangs never holds+ * doctor past --budget-ms and leaves no git child running. localProbe also caps a probe's output at 1 MiB.+ */+import { afterAll, expect, test } from 'bun:test';+import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';+import { tmpdir } from 'node:os';+import { join } from 'node:path';+import { runDoctorRegistry } from '../src/commands/doctor/registry.ts';+import { bootstrapChecksEntry } from '../src/commands/doctor/checks/local-runtime.ts';+import { LOCAL_PROBE_MAX_OUTPUT_BYTES, localProbe } from '../src/commands/doctor/local-probe.ts';+import { startDoctorDeadline } from '../src/commands/doctor/budget.ts';+import type { DoctorContext } from '../src/commands/doctor/context.ts';+import { withEnv } from './helpers/with-env.ts';++const noProgress = new Proxy({}, { get: () => () => undefined }) as DoctorContext['progress'];+const tmpDirs: string[] = [];+afterAll(() => { for (const d of tmpDirs) rmSync(d, { recursive: true, force: true }); });++function tmp(prefix: string): string {+ const d = mkdtempSync(join(tmpdir(), prefix));+ tmpDirs.push(d);+ return d;+}++function ctxFor(args: string[]): DoctorContext {+ return {+ engine: null, args, jsonOutput: true, fastMode: false, doFix: false, dryRun: false, scope: 'brain', orphanRatioSourceId: undefined,+ progress: noProgress, skillsDirResolution: { dir: null, source: 'none' }, skillsDir: null, autoFixReport: null, schemaVersion: 0,+ connectionFailed: false, only: null, tier: 'standard', deadline: startDoctorDeadline(args),+ };+}++/** A GBRAIN_HOME parent whose install receipt names `ws`, with one push-status record. */+function bootstrapHome(ws: string, pushStatus: object): string {+ const parent = tmp('gbrain-8990-home-');+ const dir = join(parent, '.gbrain', 'bootstrap');+ mkdirSync(dir, { recursive: true });+ writeFileSync(join(dir, 'receipt.json'), JSON.stringify({+ receipt_version: 1, workspace_dir: ws, source_id: 'workspace', agent_name: 'Testy', created_at: new Date().toISOString(),+ created_by: 'test', brain_created_by_bootstrap: false, created_paths: [], registrations: [],+ }));+ writeFileSync(join(dir, 'push-status.json'), JSON.stringify(pushStatus));+ return parent;+}++/** A `git` on PATH that ignores SIGTERM and waits on a 6 s child of its own, recording both pids. */+function hangingGit(): { bin: string; pids: string } {+ const bin = tmp('gbrain-8990-bin-');+ const pids = join(bin, 'pids');+ writeFileSync(join(bin, 'git'), `#!/bin/sh\ntrap '' TERM\necho $$ >> '${pids}'\nsleep 6 &\necho $! >> '${pids}'\nwait\n`, { mode: 0o755 });+ return { bin, pids };+}++const STALE_TS = new Date(Date.now() - 72 * 3_600_000).toISOString();++for (const [label, status] of [+ ['stale push, workspace probe', (ws: string) => ({ ts: STALE_TS, ok: true })],+ ['recorded push failure, superseded probe', (ws: string) => ({ ts: STALE_TS, ok: false, reason: 'push_failed', repoRoot: ws })],+] as const) {+ test(`#8990: a git call that hangs (${label}) is killed at the entry deadline and leaves no child running`, async () => {+ const ws = tmp('gbrain-8990-ws-');+ const parent = bootstrapHome(ws, status(ws));+ const { bin, pids } = hangingGit();+ await withEnv({ PATH: `${bin}:${process.env.PATH}`, GBRAIN_HOME: parent }, async () => {+ const started = Date.now();+ const checks = await runDoctorRegistry(ctxFor(['--json', '--budget-ms', '1000']), [bootstrapChecksEntry]);+ expect(Date.now() - started).toBeLessThan(1_000 + 400);+ expect(checks.find((c) => c.name === 'bootstrap_push_health')?.status).not.toBe('ok');+ });+ await new Promise((r) => setTimeout(r, 1_500));+ const launched = readFileSync(pids, 'utf8').split('\n').filter(Boolean).map(Number);+ expect(launched.length).toBeGreaterThan(0);+ const alive = launched.filter((pid) => { try { process.kill(pid, 0); return true; } catch { return false; } });+ expect(alive).toEqual([]);+ }, 30_000);+}++test('#8990: a probe whose output passes 1 MiB reads as absent and is killed', async () => {+ const bin = tmp('gbrain-8990-flood-');+ const pids = join(bin, 'pids');+ writeFileSync(join(bin, 'flood'), `#!/bin/sh\necho $$ >> '${pids}'\nexec yes '0 * * * * gbrain sync --all'\n`, { mode: 0o755 });+ const started = Date.now();+ const out = await localProbe(Date.now() + 4_000, join(bin, 'flood'), []);+ expect(out).toBeNull();+ expect(Date.now() - started).toBeLessThan(1_500);+ await new Promise((r) => setTimeout(r, 300));+ const launched = readFileSync(pids, 'utf8').split('\n').filter(Boolean).map(Number);+ expect(launched.filter((pid) => { try { process.kill(pid, 0); return true; } catch { return false; } })).toEqual([]);+}, 30_000);++test('#8990: a probe that prints more than 1 MiB and exits 0 still reads as absent', async () => {+ const bin = tmp('gbrain-8990-over-');+ writeFileSync(join(bin, 'over'), `#!/bin/sh\nyes 'abcdefg' | head -c ${2 * 1024 * 1024}\n`, { mode: 0o755 });+ expect(await localProbe(Date.now() + 4_000, join(bin, 'over'), [])).toBeNull();+}, 30_000);++test('#8990: a probe under the cap returns its whole output', async () => {+ const bin = tmp('gbrain-8990-under-');+ const lines = Math.floor((LOCAL_PROBE_MAX_OUTPUT_BYTES - 1) / 8);+ writeFileSync(join(bin, 'under'), `#!/bin/sh\nyes 'abcdefg' | head -n ${lines}\n`, { mode: 0o755 });+ const out = await localProbe(Date.now() + 4_000, join(bin, 'under'), []);+ expect(out?.length).toBe(lines * 8);+}, 30_000);