Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(serve): the boot deadline never stops a boot that is still advancing
garrytan80ca07a669b3
diff --git a/src/commands/serve.ts b/src/commands/serve.tsindex ba353d22aa..9b57c90988 100644--- a/src/commands/serve.ts+++ b/src/commands/serve.ts@@ -5,6 +5,7 @@ import { startMcpServer, stdioRpcsInFlightCount, resolveMcpStdioSourceScope } frimport { VERB_NAMES } from '../core/verbs.ts';import { RESIDENT_POOL_FLOOR } from '../core/pg-access-classify.ts';import { redirectStdoutLoggingToStderr } from '../core/console-prefix.ts';+import { onForwardProgress } from '../core/forward-progress.ts';import {installLoopStallWatchdog,resolveServeStallWatchdogMs,@@ -36,10 +37,11 @@ const CLEANUP_DEADLINE_MS = 5_000;// upstream is unreachable) holds the PGLite write lock indefinitely: the// post-#2348 lock discipline never steals from a live holder, so every// CLI consumer times out until someone hunts down and kills the PID. If-// startMcpServer hasn't finished connecting the transport within this-// window, we release the engine (dropping the lock) and exit non-zero so-// a supervisor can restart with backoff. Env-tunable via-// GBRAIN_SERVE_BOOT_TIMEOUT_SECONDS; 0 disables.+// the boot makes no progress (no new boot phase, no forward-progress+// note) for this window, we release the engine (dropping the lock) and+// exit non-zero so a supervisor can restart with backoff. A slow boot that+// keeps advancing is never killed: a large brain legitimately boots past+// one window. Env-tunable via GBRAIN_SERVE_BOOT_TIMEOUT_SECONDS; 0 disables.const DEFAULT_BOOT_TIMEOUT_SECONDS = 60;// How often the parent-process watchdog polls the live kernel parent PID@@ -355,11 +357,14 @@ export async function runServe(const bootTimeoutMs = opts.bootTimeoutMs ?? resolveBootTimeoutMs();let bootDeadline: ReturnType<typeof setTimeout> | null = null;let bootPhase = 'starting';+ let bootProgressAt = Date.now();+ const bootStartedAt = bootProgressAt;+ const stopProgressWatch = bootTimeoutMs > 0 ? onForwardProgress(() => { bootProgressAt = Date.now(); }) : () => {};if (bootTimeoutMs > 0) {const log = opts.log ?? ((msg: string) => console.error(msg));const exit = opts.exit ?? ((code?: number) => { process.exit(code); });- bootDeadline = setTimeout(() => {- log(formatBootTimeout(bootTimeoutMs, bootPhase, engine));+ const fire = () => {+ log(formatBootTimeout(bootTimeoutMs, bootPhase, engine, Date.now() - bootStartedAt));const cleanup = setTimeout(() => { exit(1); }, CLEANUP_DEADLINE_MS);cleanup.unref?.();Promise.resolve()@@ -372,12 +377,22 @@ export async function runServe(clearTimeout(cleanup);exit(1);});- }, bootTimeoutMs);+ };+ // The window measures time WITHOUT progress: each check re-arms for+ // whatever remains of the window since the last phase change or+ // progress note, and fires only once a full window passed without one.+ const check = () => {+ const idle = Date.now() - bootProgressAt;+ if (idle >= bootTimeoutMs) { fire(); return; }+ bootDeadline = setTimeout(check, bootTimeoutMs - idle);+ bootDeadline.unref?.();+ };+ bootDeadline = setTimeout(check, bootTimeoutMs);bootDeadline.unref?.();}try {- await start(engine, { surface, ...(sourceGuard ? { sourceGuard } : {}), onBootPhase: phase => { bootPhase = phase; }, ...(access === 'read-only' ? { access } : {}) });+ await start(engine, { surface, ...(sourceGuard ? { sourceGuard } : {}), onBootPhase: phase => { bootPhase = phase; bootProgressAt = Date.now(); }, ...(access === 'read-only' ? { access } : {}) });// `--stdio-idle-timeout` arms its timer during lifecycle installation,// but its stdin activity listener must wait until startMcpServer has// attached the MCP SDK transport listener. Attaching any `data` listener@@ -386,6 +401,7 @@ export async function runServe(activateStdioIdleActivityTracking();} finally {if (bootDeadline) clearTimeout(bootDeadline);+ stopProgressWatch();}// startMcpServer returns after the SDK has wired up its stdin 'data'// listener (and completed its engine-dependent boot); that listener keeps@@ -420,7 +436,7 @@ function resolveEofDrainMs(): number {* the engine's pool pressure, so a pool below the resident floor reads as a* pool problem instead of a provider-endpoint one.*/-function formatBootTimeout(timeoutMs: number, phase: string, engine: BrainEngine): string {+function formatBootTimeout(timeoutMs: number, phase: string, engine: BrainEngine, elapsedMs: number): string {let pool: { tracked: Record<string, number>; poolMax: number | null } | null = null;try { pool = (engine as { getPoolDiagnostics?: () => typeof pool }).getPoolDiagnostics?.() ?? null; } catch { /* diagnostic only */ }const inFlight = pool ? Object.values(pool.tracked).reduce((sum, n) => sum + n, 0) : 0;@@ -430,7 +446,7 @@ function formatBootTimeout(timeoutMs: number, phase: string, engine: BrainEngineconst fix = starved? `export GBRAIN_POOL_SIZE=${RESIDENT_POOL_FLOOR} (and size the pooler for every resident process)`: 'check configured provider endpoints, or export GBRAIN_SERVE_BOOT_TIMEOUT_SECONDS=<seconds> (0 disables)';- return `GBrain MCP server: boot did not complete within ${timeoutMs}ms — releasing DB lock and exiting so other consumers unblock. `+ return `GBrain MCP server: boot did not complete — no boot progress for ${timeoutMs}ms (${Math.round(elapsedMs / 1000)}s since boot started); releasing DB lock and exiting so other consumers unblock. `+ `code=serve_boot_timeout phase=${phase}${poolText}; cause: ${starved ? 'the connection pool is too small or saturated for a resident serve' : `boot stalled in ${phase}`}; `+ `fix: ${fix}; docs: docs/ENGINES.md#serve-boot-timeout`;}diff --git a/test/serve-stdio-lifecycle.test.ts b/test/serve-stdio-lifecycle.test.tsindex 792a0f3812..ec774a5d98 100644--- a/test/serve-stdio-lifecycle.test.ts+++ b/test/serve-stdio-lifecycle.test.ts@@ -682,6 +682,55 @@ describe('boot-readiness deadline (#3273)', () => {expect(line).toContain('GBRAIN_SERVE_BOOT_TIMEOUT_SECONDS');});+ // Large-brain ceiling (F4d): a 52k-document brain took ~80 s to boot,+ // past the 60 s deadline, and serve exited mid-session. The deadline now+ // measures time without boot progress; a boot that keeps advancing is+ // never killed, however long it takes in total.+ test('a slow boot that keeps advancing phases outlives several windows', async () => {+ const h = makeHarness();+ h.opts.startMcpServer = async (_engine, startOpts) => {+ for (const phase of ['source_preflight', 'writeback_config', 'mcp_connect', 'source_scope', 'persistence_consumer', 'resolve_ipc_bind', 'startup_sweep']) {+ startOpts?.onBootPhase?.(phase);+ await new Promise(r => setTimeout(r, 25));+ }+ };+ h.opts.bootTimeoutMs = 60;+ await runServe(h.engine as unknown as BrainEngine, [], h.opts);+ await new Promise(r => setTimeout(r, 100));+ expect(h.engine.disconnectCalls).toBe(0);+ expect(h.logs.some(l => l.includes('boot did not complete'))).toBe(false);+ });++ test('forward-progress notes inside one long phase keep the boot alive', async () => {+ const { noteForwardProgress } = await import('../src/core/forward-progress.ts');+ const h = makeHarness();+ h.opts.startMcpServer = async (_engine, startOpts) => {+ startOpts?.onBootPhase?.('persistence_consumer');+ for (let i = 0; i < 12; i++) { noteForwardProgress(); await new Promise(r => setTimeout(r, 20)); }+ };+ h.opts.bootTimeoutMs = 60;+ await runServe(h.engine as unknown as BrainEngine, [], h.opts);+ await new Promise(r => setTimeout(r, 100));+ expect(h.engine.disconnectCalls).toBe(0);+ expect(h.logs.some(l => l.includes('boot did not complete'))).toBe(false);+ });++ test('a boot that stops advancing still exits one window after its last progress, naming the phase', async () => {+ const h = makeHarness();+ h.opts.startMcpServer = async (_engine, startOpts) => {+ startOpts?.onBootPhase?.('source_scope');+ await new Promise(r => setTimeout(r, 30));+ startOpts?.onBootPhase?.('persistence_consumer');+ return new Promise<void>(() => {});+ };+ h.opts.bootTimeoutMs = 50;+ void runServe(h.engine as unknown as BrainEngine, [], h.opts);+ expect(await h.exited).toBe(1);+ const line = h.logs.find(l => l.includes('boot did not complete'))!;+ expect(line).toContain('no boot progress for 50ms');+ expect(line).toContain('code=serve_boot_timeout phase=persistence_consumer');+ });+test('bootTimeoutMs = 0 disables the deadline', async () => {const h = makeHarness();let resolveBoot!: () => void;