Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix: rollout read errors render for the caller's grant and the rollout's tools
garrytan52a20f34c40d
diff --git a/src/core/skillopt/rollout.ts b/src/core/skillopt/rollout.tsindex a8eef241d9..44d99bbc3e 100644--- a/src/core/skillopt/rollout.ts+++ b/src/core/skillopt/rollout.ts@@ -213,10 +213,9 @@ interface ToolRegistry {function buildReadOnlyToolRegistry(ctx: OperationContext, offered: ReadonlySet<string>, allowed?: ReadonlySet<string>, original?: OperationContext): ToolRegistry {const defs: ChatToolDef[] = [];const handlers = new Map<string, ToolHandler>();- for (const op of operations) {- if (!offered.has(op.name)) continue;- if (op.mutating || op.localOnly) continue;- if (allowed && !allowed.has(op.name)) continue;+ const ops = operations.filter(op => offered.has(op.name) && !op.mutating && !op.localOnly && (!allowed || allowed.has(op.name)));+ const opNames = new Set(ops.map(op => op.name));+ for (const op of ops) {const toolName = `brain_${op.name}`;defs.push({name: toolName,@@ -234,7 +233,7 @@ function buildReadOnlyToolRegistry(ctx: OperationContext, offered: ReadonlySet<sif (failure) throw schemaInvalidParams(op, failure, { remote: true }, params);return await op.handler({ ...active, remote: true }, params);} catch (e) {- throw asAgentToolError(e, op.name, active.sourceId);+ throw asAgentToolError(e, op.name, active, opNames);}},});diff --git a/src/core/skillopt/write-capture.ts b/src/core/skillopt/write-capture.tsindex c1d6c12d1e..9321d4f2b9 100644--- a/src/core/skillopt/write-capture.ts+++ b/src/core/skillopt/write-capture.ts@@ -74,11 +74,10 @@ export function buildWriteCaptureRegistry(engine: BrainEngine): WriteCaptureRegiconst baseDefs: ChatToolDef[] = [];const baseHandlers = new Map<string, ToolHandler>();- // Build the read-only base (same shape as rollout.ts uses).- for (const op of operations) {- if (!BRAIN_TOOL_ALLOWLIST.has(op.name)) continue;- if (op.mutating || op.localOnly) continue;- if (WRITE_OPS.has(op.name)) continue; // skip write ops; virtual versions land below+ // Build the read-only base (same shape as rollout.ts uses); write ops get virtual versions below.+ const readOps = operations.filter(op => BRAIN_TOOL_ALLOWLIST.has(op.name) && !op.mutating && !op.localOnly && !WRITE_OPS.has(op.name));+ const readOpNames = new Set(readOps.map(op => op.name));+ for (const op of readOps) {const toolName = `brain_${op.name}`;baseDefs.push({name: toolName,@@ -94,7 +93,7 @@ export function buildWriteCaptureRegistry(engine: BrainEngine): WriteCaptureRegiif (failure) throw schemaInvalidParams(op, failure, { remote: true }, params);return await op.handler(ctx, params);} catch (e) {- throw asAgentToolError(e, op.name, ctx.sourceId);+ throw asAgentToolError(e, op.name, ctx, readOpNames);}},});diff --git a/src/mcp/dispatch.ts b/src/mcp/dispatch.tsindex ec057932fd..03e63d8fc3 100644--- a/src/mcp/dispatch.ts+++ b/src/mcp/dispatch.ts@@ -815,11 +815,14 @@ export function errorResult(e: unknown, opts: DispatchOpts, extra: { op?: string/*** GST-1054: SkillOpt's tool loops hand the model a thrown error's message, so their read handlers throw this: the* error text an MCP caller reads for the same failed read (code, message, suggestion, fix), with `code` kept for- * callers that branch on it. An AI invocation policy stop passes through unchanged, because the loop rethrows it.+ * callers that branch on it. It renders for the caller's grant (a read-scope reader when the loop has none) and the+ * loop's own tools, so a fix names a tool call the model can make. An AI invocation policy stop passes through+ * unchanged, because the loop rethrows it.*/-export function asAgentToolError(e: unknown, op: string, sourceId?: string): unknown {+export function asAgentToolError(e: unknown, op: string, ctx: Pick<OperationContext, 'sourceId' | 'auth'>, allowedOps: ReadonlySet<string>): unknown {if (isAIInvocationPolicyError(e)) return e;- const text = errorResult(e, { remote: true, transport: 'http', sourceId }, { op, mutating: false, idempotent: true }).content[0]!.text;+ const auth: AuthInfo = ctx.auth ?? { token: '', clientId: '', scopes: ['read'] };+ const text = errorResult(e, { remote: true, transport: 'http', sourceId: ctx.sourceId, auth, allowedOps }, { op, mutating: false, idempotent: true }).content[0]!.text;return Object.assign(new Error(text), { code: (JSON.parse(text) as { code?: string }).code });}diff --git a/test/skillopt-rollout-agent-errors.test.ts b/test/skillopt-rollout-agent-errors.test.tsindex 904b8f73d2..272302b482 100644--- a/test/skillopt-rollout-agent-errors.test.ts+++ b/test/skillopt-rollout-agent-errors.test.ts@@ -32,7 +32,7 @@ async function rolloutHandlers(operationContext?: OperationContext): Promise<Map}async function mcpText(args: Record<string, unknown>): Promise<string> {- const r = await dispatchToolCall(engine, 'get_page', args, { remote: true, transport: 'http', sourceId: 'default' });+ const r = await dispatchToolCall(engine, 'get_page', args, { remote: true, transport: 'http', sourceId: 'default', auth: { token: '', clientId: '', scopes: ['read'] } });expect(r.isError).toBe(true);return (r.content as Array<{ text: string }>)[0]!.text;}@@ -68,10 +68,12 @@ describe('GST-1054: SkillOpt rollout tool errors carry the MCP error text', () =test(name + ': a refusal reaches the model with its code, suggestion and runnable fix, as MCP returns it', async () => {const message = await thrownMessage(handlers(), twoSelectors);expect(message).toBe(await mcpText(twoSelectors));- const body = JSON.parse(message) as { code: string; suggestion?: string; fix?: { argv?: string[] } };+ const body = JSON.parse(message) as { code: string; suggestion?: string; fix?: { argv?: string[]; next?: string; mcp?: { tool: string } } };expect(body.code).toBe('invalid_params');expect(body.suggestion).toContain('Drop `bytes`');expect(body.fix?.argv).toEqual(['gbrain', 'get', '--lines', '1-1', '--body', 'window', '--source', 'default', '--', SLUG]);+ expect(body.fix?.next).toBe('run');+ expect(body.fix?.mcp?.tool).toBe('get_page');});test(name + ': a handler error reads the same as the MCP error', async () => {@@ -84,7 +86,7 @@ describe('GST-1054: SkillOpt rollout tool errors carry the MCP error text', () =await expect(withAIInvocationGuard(async () => { throw stop; }, () =>invokeAI({ operation: 'synthetic.expand', kind: 'embedding', model: 'synthetic:model', maxInputTokens: 8 }, async () => 'unreached', () => null))).rejects.toBe(stop);expect(isAIInvocationPolicyError(stop)).toBe(true);- expect(asAgentToolError(stop, 'get_page', 'default')).toBe(stop);+ expect(asAgentToolError(stop, 'get_page', { sourceId: 'default' }, new Set(['get_page']))).toBe(stop);});test('a remote rollout whose grant was revoked reads the permission_denied error with its fix', async () => {