Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(closeout): thin-client CLI relays MCP-only server fixes as tell_user_to_run instead of dropping them
garrytan40f931c4ec91
diff --git a/docs/protocol/AGENT_OPERATOR_v1.md b/docs/protocol/AGENT_OPERATOR_v1.mdindex 8b27a3302d..0a98e6737d 100644--- a/docs/protocol/AGENT_OPERATOR_v1.md+++ b/docs/protocol/AGENT_OPERATOR_v1.md@@ -603,7 +603,7 @@ and report it if it repeats.interface RenderedAction {argv?: string[]; // ['gbrain', …]: explicit --brain/--source; positionals after '--'command?: string; // shell-quoted from argv; never hand-built- mcp?: { tool: string; arguments: Record<string, unknown> }; // present only when callable on your connection+ mcp?: { tool: string; arguments: Record<string, unknown> }; // present when callable on your connection (or relayed: see below)consent: Effect[]; // [] means no consent neededactor: 'agent' | 'user' | 'host_admin' | 'provider';next: 'run' | 'ask_user' | 'tell_user_to_run' | 'wait' | 'report';@@ -619,6 +619,18 @@ interface RenderedAction {}```+**Explicit routing.** Every `gbrain` argv in a fix (`argv`, `preview_argv`,+`verify.argv`, `then`) names the brain and source the failing call acted on:+`--brain <id>` for commands that open a brain, `--source <id>` for commands that+resolve their source through the ambient chain (shared ops, and CLI commands the+command table marks `routes_source`). gbrain appends them when it renders the+fix, before any bare `--`, and keeps flags the fix already carries. So you can+run the fix later from another directory, or under a different `GBRAIN_BRAIN_ID`,+`GBRAIN_SOURCE`, `.gbrain-mount` or `.gbrain-source`, and it still acts on the+intended brain. Over HTTP the ids stay (they are not sensitive); paths are+stripped as everywhere else. A thin client pins nothing: it has no local mounts+and its remote scopes the source.+`next` is computed when gbrain renders the fix for your connection: from theeffects, the actor, the transport, which tools you can call and the user'spreapprovals. It is never stored, so the same fix can say `run` on the CLI and@@ -651,14 +663,18 @@ First matching row wins.| # | Condition | `next` ||---|---|---|-| 1 | no runnable step (no `argv`, no callable `mcp`) | `report`: relay `message`; run `gbrain doctor --json` where you can |+| 1 | no runnable step (no `argv`, no callable `mcp`, and not an MCP-only fix on the CLI) | `report`: relay `message`; run `gbrain doctor --json` where you can || 2 | `actor = provider` | `wait`: retry after the stated delay with the same request identity |-| 3 | `actor` is `user` or `host_admin`, or the fix is CLI-only and you are on MCP | `tell_user_to_run` |+| 3 | `actor` is `user` or `host_admin`, or the fix is CLI-only and you are on MCP, or MCP-only and you are on the CLI | `tell_user_to_run` || 4 | `consent` is non-empty and not covered by a matching preapproval (`destructive` never is) | `ask_user` || 5 | otherwise | `run` |A CLI-only fix rendered for an MCP caller gets actor `user` on stdio and-`host_admin` on HTTP, and `next: tell_user_to_run`.+`host_admin` on HTTP, and `next: tell_user_to_run`. The mirror case: an+MCP-only fix rendered for the CLI (a remote brain's fix on a thin client) keeps+its `mcp` call as data, gets actor `user`, a `user_message` saying to call that+tool over MCP, and `next: tell_user_to_run`; the human line reads+`Fix: call <tool> over MCP with {…}`.### Exclusive fixes and two-step plansdiff --git a/src/core/agent-output.ts b/src/core/agent-output.tsindex f5013f57fa..057eb110e2 100644--- a/src/core/agent-output.ts+++ b/src/core/agent-output.ts@@ -214,9 +214,10 @@ export function docsUrl(anchor: string): string {/** The published decision table (first matching row wins). */export function deriveNext(a: Action, ctx: RenderContext): Next {const callableMcp = a.mcp !== undefined && ctx.isCallable(a.mcp.tool);- if (!a.argv?.length && !callableMcp) return 'report';+ const mcpOnlyOnCli = ctx.transport === 'cli' && !a.argv?.length && a.mcp !== undefined && !callableMcp;+ if (!a.argv?.length && !callableMcp && !mcpOnlyOnCli) return 'report';if (a.actor === 'provider') return 'wait';- if (a.actor === 'user' || a.actor === 'host_admin') return 'tell_user_to_run';+ if (a.actor === 'user' || a.actor === 'host_admin' || mcpOnlyOnCli) return 'tell_user_to_run';if (ctx.transport !== 'cli' && !callableMcp) return 'tell_user_to_run';if (a.consent.length > 0 && (a.consent.includes('destructive') || !ctx.preapproved(a.consent))) return 'ask_user';return 'run';@@ -231,10 +232,15 @@ function renderVerify(v: Action['verify'], ctx: RenderContext): RenderedAction['/** Render an Action for the caller's surface. Key order is part of the v1 goldens. */export function renderAction(a: Action, ctx: RenderContext): RenderedAction {- const mcp = a.mcp && ctx.isCallable(a.mcp.tool) ? a.mcp : undefined;- const cliOnlyOnMcp = ctx.transport !== 'cli' && !mcp && a.actor === 'agent' && !!a.argv?.length;- const actor: Actor = cliOnlyOnMcp ? (ctx.transport === 'http' ? 'host_admin' : 'user') : a.actor;+ const callable = a.mcp && ctx.isCallable(a.mcp.tool) ? a.mcp : undefined;+ const cliOnlyOnMcp = ctx.transport !== 'cli' && !callable && a.actor === 'agent' && !!a.argv?.length;+ // The mirror case: an MCP-only fix on the CLI (a remote server's fix on a thin client) is kept as+ // relay data, not dropped: the user (or their MCP client) calls the tool; the CLI cannot.+ const relay = ctx.transport === 'cli' && !callable && !a.argv?.length && a.mcp ? a.mcp : undefined;+ const mcp = callable ?? relay;+ const actor: Actor = cliOnlyOnMcp ? (ctx.transport === 'http' ? 'host_admin' : 'user') : relay && a.actor === 'agent' ? 'user' : a.actor;const effective: Action = { ...a, mcp, actor };+ const userMessage = a.user_message ?? (relay ? `Call the ${relay.tool} tool over MCP with ${JSON.stringify(relay.arguments)}; this command line cannot call it.` : undefined);const verify = renderVerify(a.verify, ctx);const argv = a.argv ? pinRouting(a.argv, ctx.routing) : undefined;const out: RenderedAction = {@@ -244,7 +250,7 @@ export function renderAction(a: Action, ctx: RenderContext): RenderedAction {actor,next: deriveNext(effective, ctx),why: inertText(a.why),- ...(a.user_message !== undefined ? { user_message: inertText(a.user_message) } : {}),+ ...(userMessage !== undefined ? { user_message: inertText(userMessage) } : {}),...(verify ? { verify } : {}),...(a.docs ? { docs: docsUrl(a.docs) } : {}),requires_exclusive: a.requires_exclusive,@@ -773,7 +779,8 @@ export function renderCliError(e: unknown, opts: { json: boolean; command: strinconst exitCode = exitCodeForCode(env.code);if (opts.json) return { stdout: `${JSON.stringify(env, null, 2)}\n`, exitCode };const lines = [`Error [${env.code}]: ${env.message}`];- lines.push(`Fix: ${env.fix?.command ?? env.suggestion}`);+ const relay = env.fix && !env.fix.command && env.fix.mcp ? `call ${env.fix.mcp.tool} over MCP with ${JSON.stringify(env.fix.mcp.arguments)}` : undefined;+ lines.push(`Fix: ${env.fix?.command ?? relay ?? env.suggestion}`);if (env.why) lines.push(`Why: ${env.why}`);if (env.docs) lines.push(`Docs: ${env.docs}`);return { stderr: `${lines.join('\n')}\n`, exitCode };diff --git a/test/agent-error.test.ts b/test/agent-error.test.tsindex 924b3650cf..68f2556f45 100644--- a/test/agent-error.test.ts+++ b/test/agent-error.test.ts@@ -4,7 +4,7 @@* canonical `code`, a `fix` where one applies, and the safe-recovery rule.*/import { afterAll, beforeAll, describe, expect, test } from 'bun:test';-import { __setDocsRefForTests, toAgentError, type AgentErrorContext, type RenderContext } from '../src/core/agent-output.ts';+import { __setDocsRefForTests, renderCliError, toAgentError, type AgentErrorContext, type RenderContext } from '../src/core/agent-output.ts';import { OperationError, opError } from '../src/core/ops/contract.ts';import { catalogueError } from '../src/core/error-catalogue.ts';import { errorFor } from '../src/core/errors.ts';@@ -88,6 +88,26 @@ describe('normaliser rows', () => {expect(toAgentError(new RemoteMcpError('network', 'timed out', { kind: 'timeout' }), cx()).code).toBe('timeout');});+ test('thin client: a server fix with only an MCP form is relayed to the CLI user, not dropped', () => {+ const requestId = '11111111-2222-4333-8444-555555555555';+ const e = new RemoteMcpError('tool_error', 'pending', {+ code: 'write_pending', message: 'The write is still pending.', suggestion: 'Read the receipt before resubmitting.',+ fix: { mcp: { tool: 'get_write_request', arguments: { request_id: requestId } }, consent: [], actor: 'agent', next: 'run',+ why: 'The receipt says whether the write committed.', requires_exclusive: false },+ } as never);+ const cli = render({ transport: 'cli', isCallable: () => false });+ const env = toAgentError(e, cx({ transport: 'cli', render: cli }));+ expect(env.fix).toMatchObject({ mcp: { tool: 'get_write_request', arguments: { request_id: requestId } }, actor: 'user', next: 'tell_user_to_run' });+ expect(env.fix?.argv).toBeUndefined();+ expect(env.fix?.user_message).toContain('Call the get_write_request tool over MCP');+ expect(env.suggestion).toContain('get_write_request');+ const human = renderCliError(e, { json: false, command: 'put', tty: false });+ expect(human.stderr).toContain(`Fix: call get_write_request over MCP with {"request_id":"${requestId}"}`);+ // The same fix stays runnable where the tool is callable, and an MCP transport that cannot call it still reports.+ expect(toAgentError(e, cx()).fix).toMatchObject({ mcp: { tool: 'get_write_request' }, actor: 'agent', next: 'run' });+ expect(toAgentError(e, cx({ render: render({ isCallable: () => false }) })).fix?.next).toBe('report');+ });+test('DB access errors classify (GBRAIN_DB_ACCESS marker); verbs keep frozen codes', () => {const err = Object.assign(new Error('connect ECONNREFUSED 127.0.0.1:5432'), { code: 'ECONNREFUSED' });const env = toAgentError(err, cx({ op: 'list_pages' }));