Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

fix(setup): drop the reply sentence from OpenClaw's rule on internal turns

garrytan4420b7b9b11b
diff --git a/docs/mcp/OPENCLAW.md b/docs/mcp/OPENCLAW.md
index cc2da8f5e8..8e9e1701f4 100644
--- a/docs/mcp/OPENCLAW.md
+++ b/docs/mcp/OPENCLAW.md
@@ -91,7 +91,8 @@ block at the top of `GBRAIN-INSTRUCTIONS.md`.
A rule in `AGENTS.md` reaches the system prompt only, so setup also puts it next
to every user turn with a hook-only OpenClaw plugin, `gbrain-turn-rule`: its
-`before_prompt_build` handler returns the rule as `prependContext`. The plugin is
+`before_prompt_build` handler returns the rule as `prependContext` (without its
+closing "answer" sentence on heartbeat, cron and exec-completion turns). The plugin is
self-contained (the rule text is in its code; it reads and calls nothing) and
lives in `<OpenClaw state dir>/gbrain/gbrain-turn-rule`. Setup installs it with
OpenClaw's own CLI:
diff --git a/src/core/setup/brain-first.ts b/src/core/setup/brain-first.ts
index a6e066e715..efabf42502 100644
--- a/src/core/setup/brain-first.ts
+++ b/src/core/setup/brain-first.ts
@@ -85,12 +85,14 @@ export function renderBrainFirstSnippet(harness: string, access: BrainFirstAcces
* before_prompt_build). The block in the system prompt or identity file alone
* left chat bots answering without the brain (harness-evals: 0 of 80 on
* OpenClaw and Hermes); the same rule next to the turn was followed.
+ * `reply: false` drops the closing "answer" sentence, for turns nobody asked
+ * a reply to (OpenClaw heartbeat, cron and exec-completion turns).
*/
-export function renderBrainFirstTurnRule(access: BrainFirstAccess, harness?: string): string {
+export function renderBrainFirstTurnRule(access: BrainFirstAccess, harness?: string, opts: { reply?: boolean } = {}): string {
const how = access.kind === 'cli' ? `\`${access.launcher} search "<names>"\` or \`${access.launcher} query "<question>"\``
: access.surface === 'verbs' ? `\`recall\` or \`entity\` on the \`${access.server}\` MCP server`
: `\`search\` or \`query\` on the \`${access.server}\` MCP server`;
- return `GBrain: before answering this turn, check GBrain first (${how}) for the people, companies, projects, meetings and decisions it touches.${access.kind === 'mcp' ? turnRuleDiscovery(harness, access) : ''} Results span every source it holds: use only what the current requester may see. Then answer and finish the task.`;
+ return `GBrain: before answering this turn, check GBrain first (${how}) for the people, companies, projects, meetings and decisions it touches.${access.kind === 'mcp' ? turnRuleDiscovery(harness, access) : ''} Results span every source it holds: use only what the current requester may see.${opts.reply === false ? '' : ' Then answer and finish the task.'}`;
}
/**
diff --git a/src/core/setup/openclaw-turn-plugin.ts b/src/core/setup/openclaw-turn-plugin.ts
index 73f60a0802..66f7778b4f 100644
--- a/src/core/setup/openclaw-turn-plugin.ts
+++ b/src/core/setup/openclaw-turn-plugin.ts
@@ -2,8 +2,9 @@
* openclaw-turn-plugin.ts — OpenClaw's native per-turn hook for the
* brain-first rule: a hook-only OpenClaw plugin whose `before_prompt_build`
* handler returns the rule as `prependContext`, so it reaches the model next
- * to each user turn. The workspace AGENTS.md block reaches the system prompt
- * only, and bots read it as identity, not as an instruction to call a tool.
+ * to each user turn (without its reply sentence on `internal_system` turns).
+ * The workspace AGENTS.md block reaches the system prompt only, and bots read
+ * it as identity, not as an instruction to call a tool.
*
* The plugin is self-contained (the rule text is written into its code; it
* reads nothing and calls nothing) and lives in <OpenClaw state dir>/gbrain/.
@@ -37,12 +38,13 @@ export function openclawTurnPluginFiles(access: BrainFirstAccess): Record<string
'index.js': [
'// Managed by gbrain setup openclaw; a re-run rewrites this file. Remove: gbrain setup openclaw --remove',
`const RULE = ${JSON.stringify(rule)};`,
+ `const INTERNAL_RULE = ${JSON.stringify(renderBrainFirstTurnRule(access, 'openclaw', { reply: false }))};`,
'export default {',
` id: ${JSON.stringify(OPENCLAW_TURN_PLUGIN_ID)},`,
" name: 'GBrain turn rule',",
` description: ${JSON.stringify(DESCRIPTION)},`,
' register(api) {',
- " api.on('before_prompt_build', () => ({ prependContext: RULE }));",
+ " api.on('before_prompt_build', (_event, ctx) => ({ prependContext: ctx?.inputProvenance?.kind === 'internal_system' ? INTERNAL_RULE : RULE }));",
' },',
'};',
'',
diff --git a/test/brain-first-turn-rule.test.ts b/test/brain-first-turn-rule.test.ts
index e93c9ecf42..ac91d3b7b8 100644
--- a/test/brain-first-turn-rule.test.ts
+++ b/test/brain-first-turn-rule.test.ts
@@ -285,6 +285,22 @@ describe('per-turn rule: OpenClaw before_prompt_build plugin', () => {
});
});
+ test('internal turns (heartbeat, cron, exec completion) get the rule without the reply sentence; every other turn keeps it', async () => {
+ await withEnv({ OPENCLAW_STATE_DIR: state(), OPENCLAW_PROFILE: undefined }, async () => {
+ syncOpenclawTurnPlugin({ access, dryRun: false, openclawBin: '/bin/openclaw', run: recorder().run });
+ const mod = await import(join(openclawTurnPluginDir(), 'index.js'));
+ const handlers: Record<string, (event?: unknown, ctx?: unknown) => unknown> = {};
+ mod.default.register({ on: (event: string, fn: (event?: unknown, ctx?: unknown) => unknown) => { handlers[event] = fn; } });
+ const full = renderBrainFirstTurnRule(access, 'openclaw');
+ expect(full).toEndWith(' Then answer and finish the task.');
+ const internal = renderBrainFirstTurnRule(access, 'openclaw', { reply: false });
+ expect(internal).toBe(full.slice(0, -' Then answer and finish the task.'.length));
+ expect(handlers.before_prompt_build({}, { inputProvenance: { kind: 'internal_system' } })).toEqual({ prependContext: internal });
+ expect(handlers.before_prompt_build({}, { inputProvenance: { kind: 'external_user' } })).toEqual({ prependContext: full });
+ expect(handlers.before_prompt_build({}, {})).toEqual({ prependContext: full });
+ });
+ });
+
test('no openclaw on PATH or a failed command: the plugin is written and the remaining commands are printed', async () => {
await withEnv({ OPENCLAW_STATE_DIR: state(), OPENCLAW_PROFILE: undefined }, async () => {
const o = syncOpenclawTurnPlugin({ access, dryRun: false, openclawBin: null });