Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(security): readiness reruns an existing setup install with its recorded decisions, never a doctor self-loop; Codex stays with the user (review pass 2, #8426)
garrytan40b86cfd85b2
diff --git a/src/core/readiness.ts b/src/core/readiness.tsindex fdd937b852..56c5a207e9 100644--- a/src/core/readiness.ts+++ b/src/core/readiness.ts@@ -29,6 +29,9 @@ import { REGISTRATION_SURFACE, registrationAdvertise, stdioServeArgv } from './mimport { gbrainPath } from './config.ts';import { hookLauncherPath } from './setup/hook-launcher.ts';import { localSetupRecords } from './setup/local-receipts.ts';+import { setupRerunArgv } from './setup/launcher-lanes.ts';+import { readSetupReceipt } from './setup/claude-code.ts';+import { readCodexSetupReceipt } from './setup/codex.ts';import type { BrainEngine } from './engine.ts';import { redactForTransport, type Action, type ActionInput, type Effect, type Transport } from './agent-output.ts';import { detectCapabilities } from './capability.ts';@@ -416,11 +419,17 @@ const SURFACE_GLOSS: Record<McpSurface, string> = {function stdioRegistration(h: ReadinessHarness, bin: string, chosen?: McpSurface): Action {const surface = chosen ?? REGISTRATION_SURFACE;const advertise = registrationAdvertise({ userChoseSurface: chosen !== undefined });- // A setup install's hooks decision lives in its receipt; a --no-hooks rerun would turn its hooks off, so doctor names the rerun instead.- if (h !== 'opencode' && localSetupRecords().some((r) => r.harness === h && r.status === 'installed' && r.scope !== 'project')) {- return { argv: ['gbrain', 'doctor', '--only', 'harness_wiring', '--json'], consent: [], actor: 'agent', requires_exclusive: false,- why: `gbrain setup already registered gbrain for ${HARNESS_LABEL[h]}; doctor checks that entry and names the setup rerun that keeps its recorded hooks and source decisions.`,- verify: VERIFY('harness_wiring'), docs: MEMORY_VERBS_INSTALL };+ // A setup install's hooks decision lives in its receipt, so a --no-hooks rerun would turn its hooks off: rerun with what it recorded.+ const record = h === 'opencode' ? undefined : localSetupRecords().find((r) => r.harness === h && r.name === 'gbrain' && r.status === 'installed' && r.scope !== 'project');+ const receipt = record && (() => { try { return (h === 'claude-code' ? readSetupReceipt(record.path) : readCodexSetupReceipt(record.path)).setup; } catch { return null; } })();+ if (receipt && h !== 'opencode') {+ const rerun = setupRerunArgv(h, receipt);+ const why = `gbrain setup already registered gbrain for ${HARNESS_LABEL[h]}; rerunning it with the hooks, source and binary it recorded rewrites the entry in ${HARNESS_DESTINATION[h]}.`;+ // gbrain setup codex rewrites the shared hooks.json and recomputes its trust, so only the user runs it.+ if (h === 'codex') return { consent: [], actor: 'user', requires_exclusive: false, why, verify: VERIFY('harness_wiring'), docs: MEMORY_VERBS_INSTALL,+ user_message: `gbrain setup codex rewrites the Codex hooks.json and its trust entries, so run it yourself: ${rerun.join(' ')}` };+ return { argv: rerun, consent: ['persistent_install'], actor: 'agent', requires_exclusive: false, why, verify: VERIFY('harness_wiring'), docs: MEMORY_VERBS_INSTALL,+ user_message: `I'd like to rerun gbrain setup for ${HARNESS_LABEL[h]} with the decisions it recorded, which rewrites its MCP entry in ${HARNESS_DESTINATION[h]}. OK?` };}// Each harness starts a user-scope stdio server in the session's directory, so its writer registers serve behind gbrain's launcher (#8426, #8435).const serve = [hookLauncherPath(), ...stdioServeArgv(bin, surface, advertise)];diff --git a/test/readiness.test.ts b/test/readiness.test.tsindex 2cd4510812..4873cb3b3e 100644--- a/test/readiness.test.ts+++ b/test/readiness.test.ts@@ -19,9 +19,6 @@ import { renderAction, cliRenderContext, type Action } from '../src/core/agent-oimport { listRecipes } from '../src/core/ai/recipes/index.ts';import type { GBrainConfig } from '../src/core/config.ts';import { withEnv } from './helpers/with-env.ts';-import { mkdtempSync, rmSync, writeFileSync } from 'node:fs';-import { tmpdir } from 'node:os';-import { join } from 'node:path';const PROVIDER_ENVS = [...new Set(listRecipes().flatMap(r => r.auth_env?.required ?? []))];@@ -171,19 +168,6 @@ describe('harness_wiring by state', () => {const BIN = '/opt/gbrain/bin/gbrain';const owner = (transport: 'stdio' | 'http', is_self = false): LockOwner => ({ pid: 4242, transport, is_self });- test('an existing gbrain setup install is never rerun with --no-hooks: readiness points at doctor, which names the rerun that keeps its decisions', async () => {- const home = mkdtempSync(join(tmpdir(), 'gbrain-readiness-setup-'));- try {- await withEnv({ HOME: home, GBRAIN_HOME: home, CLAUDE_CONFIG_DIR: undefined, CODEX_HOME: undefined }, async () => {- writeFileSync(join(home, '.gbrain-connection-claude-code-gbrain.json'), JSON.stringify({ connection: 'local-stdio', setup_receipt_version: 1, gbrain_home: join(home, '.gbrain'), harness: 'claude-code', name: 'gbrain', status: 'installed', scope: 'user' }));- const claude = harnessWiringEntry({ transport: 'cli', harnesses: ['claude-code'], lockOwner: null, gbrainBin: BIN });- expect(claude.fix).toMatchObject({ argv: ['gbrain', 'doctor', '--only', 'harness_wiring', '--json'], consent: [], actor: 'agent' });- expect(harnessWiringEntry({ transport: 'cli', harnesses: ['codex'], lockOwner: null, gbrainBin: BIN }).fix?.argv)- .toEqual(['gbrain', 'setup', 'codex', '--no-hooks', '--gbrain-bin', BIN]);- });- } finally { rmSync(home, { recursive: true, force: true }); }- });-test('one harness, no serve: gbrain setup registers the absolute binary behind the hook launcher, --surface starter and --advertise verbs', () => {const claude = harnessWiringEntry({ transport: 'cli', harnesses: ['claude-code'], lockOwner: null, gbrainBin: BIN });expect(claude.fix?.argv).toEqual(['gbrain', 'setup', 'claude-code', '--no-hooks', '--gbrain-bin', BIN]);diff --git a/test/setup.test.ts b/test/setup.test.tsindex 1208c5106a..680d25377f 100644--- a/test/setup.test.ts+++ b/test/setup.test.ts@@ -36,6 +36,7 @@ import { refreshHookLaunchers } from '../src/core/setup/hook-launcher-refresh.tsimport { syncTurnRuleHook, isTurnRuleEntry } from '../src/core/setup/turn-hook.ts';import { hookLauncherFix, hookLauncherProblems } from '../src/commands/doctor/checks/harness-wiring.ts';import { launcherLaneFindings, setupRerunArgv } from '../src/core/setup/launcher-lanes.ts';+import { harnessWiringEntry } from '../src/core/readiness.ts';import { applyClaudeSetup, readSetupReceipt } from '../src/core/setup/claude-code.ts';const CLI = resolve(import.meta.dir, '..', 'src', 'cli.ts');@@ -1121,6 +1122,20 @@ describe('hooks written before the hook launcher (#8424): the next setup and posexpect(after.gbrain.env.GBRAIN_HOME).toBe(brainA);});+ test('readiness reruns an existing setup install with what it recorded: never --no-hooks, never a doctor self-loop, Codex left to the user', async () => {+ const b = box();+ expect((await setup(b, ['claude-code', '--json'])).exitCode).toBe(0);+ expect((await setup(b, ['codex', '--no-hooks', '--json'])).exitCode).toBe(0);+ await withEnv(env(b), async () => {+ expect(harnessWiringEntry({ transport: 'cli', harnesses: ['claude-code'], lockOwner: null, gbrainBin: b.bin }).fix)+ .toMatchObject({ actor: 'agent', consent: ['persistent_install'], argv: ['gbrain', 'setup', 'claude-code', '--gbrain-bin', b.bin] });+ const codex = harnessWiringEntry({ transport: 'cli', harnesses: ['codex'], lockOwner: null, gbrainBin: b.bin }).fix!;+ expect(codex).toMatchObject({ actor: 'user' });+ expect(codex.argv).toBeUndefined();+ expect(codex.user_message).toContain(`run it yourself: gbrain setup codex --no-hooks --gbrain-bin ${b.bin}`);+ });+ });+test('claude-code MCP (#8426): doctor\'s rerun keeps a recorded --no-hooks, so moving the entry turns no hooks on', async () => {const b = box();expect((await setup(b, ['claude-code', '--no-hooks', '--json'])).exitCode).toBe(0);