Skip to content

Commit e3dd37a

Browse files
fix: migrate auto-recall from before_agent_start to before_prompt_build (#1312)
## Problem `before_agent_start` is a legacy hook that the OpenClaw framework invokes **twice** per agent run: 1. **Model resolve phase** ([`run.ts` L348](https://github.com/openclaw/openclaw/blob/main/src/agents/pi-embedded-runner/run.ts#L348)): passes only `{ prompt }` — no `messages` field 2. **Prompt build phase** ([`attempt.ts` L1438](https://github.com/openclaw/openclaw/blob/main/src/agents/pi-embedded-runner/run/attempt.ts#L1438)): passes `{ prompt, messages }` — messages available This causes the auto-recall handler to fire twice: the first invocation has no `messages` so topic-aware pre-filtering (if any) is bypassed, and a redundant embedding search + LLM filter call runs on every turn. ## Solution Use `before_prompt_build` instead ([docs](https://docs.openclaw.ai/tools/plugin#plugin-hooks)), which: - Fires once per turn, after session load - Always provides `{ prompt, messages }` — reliable access to conversation history - Is the framework-recommended hook for prompt injection (per OpenClaw docs: *"legacy compatibility hook... prefer the explicit hooks above"*) - Returns the same result fields: `prependContext`, `systemPrompt`, `prependSystemContext`, `appendSystemContext` ## Changes - `before_agent_start` → `before_prompt_build` (single line change in `index.ts` L931) - No other logic changes; return contract is identical ## References - OpenClaw agent loop: [`concepts/agent-loop.md`](https://github.com/openclaw/openclaw/blob/main/docs/concepts/agent-loop.md) - OpenClaw plugin docs: [`tools/plugin.md`](https://github.com/openclaw/openclaw/blob/main/docs/tools/plugin.md) - Related issue: [openclaw/openclaw#26914](openclaw/openclaw#26914) (ephemeralContext request — same root cause of prependContext accumulation)
2 parents 821dc1d + 57566b5 commit e3dd37a

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

apps/memos-local-openclaw/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1806,7 +1806,7 @@ Groups: ${groupNames.length > 0 ? groupNames.join(", ") : "(none)"}`,
18061806

18071807
// ─── Auto-recall: inject relevant memories before agent starts ───
18081808

1809-
api.on("before_agent_start", async (event: { prompt?: string; messages?: unknown[] }, hookCtx?: { agentId?: string; sessionKey?: string }) => {
1809+
api.on("before_prompt_build", async (event: { prompt?: string; messages?: unknown[] }, hookCtx?: { agentId?: string; sessionKey?: string }) => {
18101810
if (!allowPromptInjection) return {};
18111811
if (!event.prompt || event.prompt.length < 3) return;
18121812

0 commit comments

Comments
 (0)