Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
fix(doctor): see a Hermes per-turn hook pasted into an existing hooks: section
garrytan55b93302716c
diff --git a/src/core/setup/hermes-turn-hook.ts b/src/core/setup/hermes-turn-hook.tsindex a230c35045..492e0cd63e 100644--- a/src/core/setup/hermes-turn-hook.ts+++ b/src/core/setup/hermes-turn-hook.ts@@ -145,12 +145,16 @@ export function removeHermesTurnHook(opts: { hermesHome: string; dryRun: boolean}}-/** Read-only: is the marked hook in config.yaml, and does Hermes's allowlist approve its exact command? */-export function inspectHermesTurnHook(hermesHome: string): { state: 'absent' | 'unapproved' | 'approved'; path: string; command?: string } {+/** Read-only: is the marked hook in config.yaml (or, without one, the exact entry setup printed for `access`, added by hand to the user's hooks: section), and does Hermes's allowlist approve its exact command? */+export function inspectHermesTurnHook(hermesHome: string, access?: BrainFirstAccess): { state: 'absent' | 'unapproved' | 'approved'; path: string; command?: string } {const path = join(hermesHome, 'config.yaml');try {- const probe = probeBlock(MARKERS, existsSync(path) ? readFileSync(path, 'utf8') : '');- const command = probe.state === 'present' ? commandIn(probe.interior) : null;+ const text = existsSync(path) ? readFileSync(path, 'utf8') : '';+ const probe = probeBlock(MARKERS, text);+ const printed = access && probe.state === 'absent' ? hermesTurnHookCommand(access) : null;+ const pasted = printed ? (loadBoundedYaml(text) as { hooks?: Record<string, unknown> } | null)?.hooks?.[EVENT] : undefined;+ const command = probe.state === 'present' ? commandIn(probe.interior)+ : Array.isArray(pasted) && pasted.some((e) => (e as { command?: unknown } | null)?.command === printed) ? printed : null;if (!command) return { state: 'absent', path };return { state: readAllowlist(join(hermesHome, ALLOWLIST)).approvals.some((e) => approves(e, command)) ? 'approved' : 'unapproved', path, command };} catch {diff --git a/src/core/setup/turn-hook-check.ts b/src/core/setup/turn-hook-check.tsindex 6347b3c4b0..78fb69a578 100644--- a/src/core/setup/turn-hook-check.ts+++ b/src/core/setup/turn-hook-check.ts@@ -23,7 +23,7 @@ import { CLAUDE_SETTINGS_FILE_RELPATH, GBRAIN_HOOK_MARKER_KEY, GBRAIN_SETUP_MARKimport { hookLauncherInCommand, hookLauncherState, type HookLauncherState } from './hook-launcher.ts';import type { LauncherLane } from './launcher-lanes.ts';import { enabledPluginLanes } from '../bootstrap/plugin-lanes.ts';-import { brainFirstTarget, openclawConfigPath, parseJson5ish } from './brain-first.ts';+import { brainFirstTarget, openclawConfigPath, parseJson5ish, readBrainFirstLedger } from './brain-first.ts';import { inspectHermesTurnHook } from './hermes-turn-hook.ts';import { OPENCLAW_TURN_PLUGIN_ID, openclawTurnPluginDir } from './openclaw-turn-plugin.ts';import { isOtherGbrainEntry, isTurnRuleEntry, isTurnRuleHarness, removeTurnRuleHook, syncTurnRuleHook, type TurnHookOutcome, type TurnHookPaths } from './turn-hook.ts';@@ -106,8 +106,9 @@ export function inspectTurnHook(harness: string, opts: { projectDir?: string; ma}if (harness === 'hermes') {const target = brainFirstTarget('hermes');- const home = 'error' in target ? '' : dirname(target.path);- const h = inspectHermesTurnHook(home);+ const soul = 'error' in target ? '' : target.path;+ const recorded = readBrainFirstLedger().find((e) => e.harness === harness && e.path === soul && e.status === 'installed');+ const h = inspectHermesTurnHook(soul ? dirname(soul) : '', recorded?.access);if (h.state === 'absent') return { harness, state: 'missing', detail: `no gbrain pre_llm_call hook in ${h.path}`, native_inspect };if (h.state === 'unapproved') return { harness, state: 'untrusted', via: 'native', detail: `the gbrain pre_llm_call hook in ${h.path} is not in Hermes's shell-hooks-allowlist.json, so Hermes does not run it`, native_inspect };return { harness, state: 'wired', via: 'native', detail: `pre_llm_call hook in ${h.path}, approved`, native_inspect };diff --git a/test/brain-first-turn-rule.test.ts b/test/brain-first-turn-rule.test.tsindex 921f829865..4d05ea4624 100644--- a/test/brain-first-turn-rule.test.ts+++ b/test/brain-first-turn-rule.test.ts@@ -15,6 +15,7 @@ import { withEnv } from './helpers/with-env.ts';import { brainFirstTurnRule, renderBrainFirstTurnRule, type BrainFirstAccess } from '../src/core/setup/brain-first.ts';import { runHook } from '../src/commands/hook.ts';import { hermesTurnHookCommand, removeHermesTurnHook, syncHermesTurnHook } from '../src/core/setup/hermes-turn-hook.ts';+import { inspectTurnHook } from '../src/core/setup/turn-hook-check.ts';import { OPENCLAW_TURN_PLUGIN_ID, openclawTurnPluginDir, removeOpenclawTurnPlugin, syncOpenclawTurnPlugin } from '../src/core/setup/openclaw-turn-plugin.ts';const roots: string[] = [];@@ -241,6 +242,26 @@ describe('per-turn rule: Hermes pre_llm_call hook in config.yaml', () => {expect(cfg(damaged)).toBe('# gbrain:turn-rule:begin\nmodel: x\n');});+ test('doctor sees the printed entry once it is pasted into the user\'s hooks: section, and only that exact command', async () => {+ const theirs = 'hooks:\n pre_tool_call:\n - command: /usr/bin/true\n';+ const d = hermes(theirs);+ const snippet = syncHermesTurnHook({ hermesHome: d, access, dryRun: false }).snippet!;+ const pasted = `${theirs} pre_llm_call:\n${snippet.split('\n').slice(2).join('\n')}`;+ expect(load(pasted)).toEqual({ hooks: { pre_tool_call: [{ command: '/usr/bin/true' }], pre_llm_call: [{ command: cmd, timeout: 10 }] } });+ writeFileSync(join(d, 'config.yaml'), pasted);+ const root = home([{ harness: 'hermes', status: 'installed', access }]);+ const ledger = join(root, '.gbrain', 'agent-instructions.json');+ const entries = JSON.parse(readFileSync(ledger, 'utf8')).entries;+ writeFileSync(ledger, JSON.stringify({ version: 1, entries: entries.map((e: Record<string, unknown>) => ({ ...e, path: join(d, 'SOUL.md') })) }));+ await withEnv({ GBRAIN_HOME: root, HERMES_HOME: d }, async () => {+ expect(inspectTurnHook('hermes')).toMatchObject({ state: 'untrusted', via: 'native' });+ writeFileSync(join(d, 'shell-hooks-allowlist.json'), JSON.stringify({ approvals: [{ event: 'pre_llm_call', command: cmd }] }));+ expect(inspectTurnHook('hermes')).toMatchObject({ state: 'wired', via: 'native' });+ writeFileSync(join(d, 'config.yaml'), pasted.replace(JSON.stringify(cmd), JSON.stringify('/bin/echo hi')));+ expect(inspectTurnHook('hermes').state).toBe('missing');+ });+ });+test('dry-run writes nothing', () => {const d = hermes('model: x\n');expect(syncHermesTurnHook({ hermesHome: d, access, dryRun: true }).action).toBe('write');