Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(closeout): withRelationGuard suggestion is rendered from its diagnostic fix
garrytan3c5ae341ebb1
diff --git a/src/core/ops/contract.ts b/src/core/ops/contract.tsindex 4afba798e7..f1eabdd759 100644--- a/src/core/ops/contract.ts+++ b/src/core/ops/contract.ts@@ -189,10 +189,14 @@ export async function withRelationGuard<T>(fn: () => Promise<T>, what: string):} catch (err) {const msg = err instanceof Error ? err.message : String(err);if (/relation .* does not exist|no such table/i.test(msg)) {- throw new OperationError(+ // The cause is not knowable here (pending migrations, or a dropped table), so the fix is the+ // registry's diagnostic read (doctor on the brain host); the prose names no other command and the+ // envelope appends the rendered fix, so suggestion and fix always agree.+ throw opError('unavailable',`${what} is unavailable on this brain: a required table is missing.`,- 'Run gbrain apply-migrations on the brain host, then retry.',+ 'The brain is missing a table this gbrain expects (usually pending schema migrations). Doctor on the brain host reports what is missing and the command that repairs it.',+ { reason: 'schema_missing' },);}throw err;diff --git a/test/agent-error-surfaces.serial.test.ts b/test/agent-error-surfaces.serial.test.tsindex 514d333e37..2a10e55a22 100644--- a/test/agent-error-surfaces.serial.test.ts+++ b/test/agent-error-surfaces.serial.test.ts@@ -438,6 +438,9 @@ describe('withRelationGuard (REAL: the job table is gone)', () => {expect(call.exitCode).toBe(1);expectSurvives(call.json, want, 'gbrain call');expect(call.json.message).toContain('a required table is missing');+ // The suggestion is rendered from the fix: it quotes the fix command and names no other one.+ expect(call.json.suggestion).toContain(call.json.fix.command);+ expect(call.json.suggestion).not.toContain('apply-migrations');expect(call.json.docs_cmd).toEqual(['gbrain', 'errors', 'unavailable']);expect(call.stderr).toContain('Error [unavailable]');diff --git a/test/agent-output.test.ts b/test/agent-output.test.tsindex e220bbaf2c..7bc9199c25 100644--- a/test/agent-output.test.ts+++ b/test/agent-output.test.ts@@ -6,11 +6,11 @@import { afterAll, beforeAll, describe, expect, test } from 'bun:test';import {__setDocsRefForTests, argvFromCommand, deriveNext, docsUrl, inertText, noticeBlock, orderNotices, paramRef,- redactForTransport, renderAction, renderCliError, renderNotice, shellQuote,+ redactForTransport, renderAction, renderCliError, renderNotice, shellQuote, toAgentError,type Action, type Effect, type Notice, type RenderContext,} from '../src/core/agent-output.ts';import { agentBlock, renderCliNotices } from '../src/core/agent-markers.ts';-import { opError } from '../src/core/ops/contract.ts';+import { opError, withRelationGuard } from '../src/core/ops/contract.ts';import { VERSION } from '../src/version.ts';import { withEnv } from './helpers/with-env.ts';@@ -175,3 +175,25 @@ describe('renderCliError', () => {expect(r.exitCode).toBe(1);});});++describe('withRelationGuard: suggestion and fix agree', () => {+ const cli = ctx({ transport: 'cli', isCallable: () => false });+ const stdio = ctx();+ const missingTable = () => withRelationGuard(async () => { throw new Error('relation "minion_jobs" does not exist'); }, 'get_job_stats').catch((e: unknown) => e);++ test('CLI: the diagnostic doctor fix, and the suggestion quotes it and names no other command', async () => {+ const env = toAgentError(await missingTable(), { transport: 'cli', op: 'get_job_stats', render: cli });+ expect(env.code).toBe('unavailable');+ expect(env.reason).toBe('schema_missing');+ expect(env.fix?.argv?.slice(0, 3)).toEqual(['gbrain', 'doctor', '--json']);+ expect(env.suggestion).toContain(`Next: ${env.fix!.command}`);+ expect(env.suggestion).not.toContain('apply-migrations');+ });++ test('MCP: the suggestion quotes the rendered tool call', async () => {+ const env = toAgentError(await missingTable(), { transport: 'stdio', op: 'get_job_stats', render: stdio });+ expect(env.fix?.mcp?.tool).toBe('run_doctor');+ expect(env.suggestion).toContain('Next: run_doctor {}');+ expect(env.suggestion).not.toContain('apply-migrations');+ });+});