Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(serve): keep startMcpServer and installStdioLifecycle within their size limits; document the read-only serve's duties (#7599)
garrytaned0d347a44d8
diff --git a/docs/mcp/ADMIN.md b/docs/mcp/ADMIN.mdindex 71d442d1c2..738dada3ab 100644--- a/docs/mcp/ADMIN.md+++ b/docs/mcp/ADMIN.md@@ -582,6 +582,15 @@ drives `tools/list`, the `gbrain://capabilities` resource (`access:`unknown_tool` before any handler runs. It denies agent-requestedmutations; owner maintenance on the same process (startup migrations, hookIPC banking) is a separate control. The default is `--access full`.++A read-only serve is also not a resident owner, because harnesses start one per+session. It holds no session-lane (direct pool) connection, starts no+persistence consumer, takes no on-commit notifier lease, runs no startup or idle+sweep, and refuses delegated sync and sweep requests; resolve, turn_context and+context_pack still answer. A read-write `gbrain serve`, `gbrain jobs work` or+the autopilot owns those duties. `gbrain doctor` reports when no process on the+host holds the on-commit notifier (`commit_notifier`) or a persistence consumer+(`two_consumers_on_host`).`gbrain serve --http --access read-only` refuses: narrow HTTP access per tokenwith `gbrain auth rescope-token <name> --operations <op,...>` or`gbrain auth rescope-client <client_id> --allowed-operations <op,...>`diff --git a/src/commands/serve.ts b/src/commands/serve.tsindex dbbe1893a6..0803aeeeaf 100644--- a/src/commands/serve.ts+++ b/src/commands/serve.ts@@ -854,9 +854,7 @@ function installStdioLifecycle(// it can never hold the process open. Cleared in beginShutdown. Kill// switch: GBRAIN_SWEEP=0 (seam: opts.sweepEnabled). Chunk-level stdin// 'data' granularity is sufficient — same rationale as armIdle below.- // #7599: a read-only serve runs no resident write duty (no idle sweep, facts drain, movement watch or on-commit watcher).- const readOnly = isReadOnlyServeArgs(args);- const sweepEnabled = !readOnly && (opts.sweepEnabled ?? (process.env.GBRAIN_SWEEP !== '0'));+ const sweepEnabled = !isReadOnlyServeArgs(args) && (opts.sweepEnabled ?? (process.env.GBRAIN_SWEEP !== '0')); // #7599: no sweep in a read-only serveif (sweepEnabled) {const runIdleSweep = opts.sweep ?? (async (e: BrainEngine) => {// #4409: the runner loads lazily here too — the sweep fires after@@ -914,7 +912,7 @@ function installStdioLifecycle(}// Automatic facts drain (Lane D) and the #6317 movement watch: see installResidentTickers.- factsDrain = readOnly ? null : installResidentTickers(engine, opts, deps, () => shuttingDown);+ factsDrain = isReadOnlyServeArgs(args) ? null : installResidentTickers(engine, opts, deps, () => shuttingDown); // #7599: nor its resident tickers// Optional idle-timeout safety net. Default OFF; opt-in via// `--stdio-idle-timeout <seconds>`. The flag is for the rare case wherediff --git a/src/mcp/server.ts b/src/mcp/server.tsindex ff4a69a737..c2497631da 100644--- a/src/mcp/server.ts+++ b/src/mcp/server.ts@@ -318,6 +318,14 @@ function stdioSurfaceSession(return { session, finishResult, statusResult };}+/**+ * #7599: a read-only stdio serve runs no resident write duty: no persistence consumer, no delegated sync, nudge or sweep+ * kinds, no session-cursor GC and no startup sweep (serve.ts also skips the idle sweep and the resident tickers, the on-commit+ * watcher among them). The long-lived serve, worker or autopilot owns them; doctor's commit_notifier and+ * two_consumers_on_host say when none runs.+ */+const READ_ONLY_SERVE_NOTE = '[gbrain-serve] read-only: no persistence consumer, on-commit sync or sweep runs here; a read-write serve, jobs work or autopilot owns them.\n';+export async function startMcpServer(engine: BrainEngine, opts: { surface?: McpSurface; surfaceSource?: SurfaceSource; invalidSurfaceEnv?: string; sourceGuard?: boolean; onBootPhase?: (phase: string) => void; access?: McpAccess } = {}) {const config = loadConfig();const bootPhase = (phase: string) => { try { opts.onBootPhase?.(phase); } catch { /* diagnostic only */ } };@@ -529,21 +537,14 @@ export async function startMcpServer(engine: BrainEngine, opts: { surface?: McpSconst [{ createPersistenceIpcProvider, residentPersistenceConfig }, { bindResolveIpcForServe }] = await Promise.all([import('../core/persistence/provider.ts'), import('./resolve-ipc-binding.ts')]);bootPhase('source_scope');const { sourceId: ipcSourceId } = await resolveMcpStdioSourceScope(engine);- // #7599: a read-only serve runs no resident write duty: no persistence consumer, no delegated sync, nudge or sweep,- // no session-cursor GC and no sweep of its own (serve.ts also skips the idle sweep, facts drain and on-commit watcher).- // The long-lived serve, worker or autopilot owns them; doctor's commit_notifier and two_consumers_on_host say when none runs.bootPhase('persistence_consumer');const persistence = session.readOnly ? undefined : await createPersistenceIpcProvider(engine, residentPersistenceConfig(config) ?? { engine: engine.kind });bootPhase('resolve_ipc_bind');- ipcBinding = await bindResolveIpcForServe(engine, ipcSourceId, persistence, { rememberCallable: () => !session.allowedOps || session.allowedOps.has('remember'), ...(session.readOnly ? { readOnly: true } : {}) });- if (session.readOnly) {- process.stderr.write('[gbrain-serve] read-only: no persistence consumer, on-commit sync or sweep runs here; a read-write serve, jobs work or autopilot owns them.\n');- import('../core/onboard/mcp-onboarding.ts').then(m => m.startOnboardingRefresher(engine, { idle: () => _stdioRpcsInFlight === 0 })).catch(() => { /* coaching is best-effort */ });- return;- }+ ipcBinding = await bindResolveIpcForServe(engine, ipcSourceId, persistence, { rememberCallable: () => !session.allowedOps || session.allowedOps.has('remember'), readOnly: session.readOnly });+ import('../core/onboard/mcp-onboarding.ts').then(m => m.startOnboardingRefresher(engine, { idle: () => _stdioRpcsInFlight === 0 })).catch(() => { /* coaching is best-effort */ });+ if (session.readOnly) return void process.stderr.write(READ_ONLY_SERVE_NOTE);- // v0.45.7 ambient recall: age out stale session cursors once per serve boot- // (7-day TTL, indexed DELETE). Best-effort — GC failure never blocks serve.+ // v0.45.7 ambient recall: age out stale session cursors once per serve boot (7-day TTL, indexed DELETE). Best-effort — GC failure never blocks serve.gcSessionContextState(engine).catch(() => {});// Startup maintenance sweep [ENG-5][CX-P0.1+P0.3]: the serve process is@@ -553,7 +554,6 @@ export async function startMcpServer(engine: BrainEngine, opts: { surface?: McpS// connect, unref'd (can never hold the process open), all errors// swallowed inside armStartupSweep. Kill switch: GBRAIN_SWEEP=0 (checked// inside the helper). Lazy import keeps sweep code off the boot path.- import('../core/onboard/mcp-onboarding.ts').then(m => m.startOnboardingRefresher(engine, { idle: () => _stdioRpcsInFlight === 0 })).catch(() => { /* coaching is best-effort */ });bootPhase('startup_sweep');try {const { armStartupSweep } = await import('../core/sweep.ts');