Detect allowPromptInjection and optimize auto-recall handling#1285
Conversation
…ecall early. -When allowPromptInjection=false, the OpenClaw framework strips prompt mutation fields from before_agent_start results, leaving undefined. -The ?? fallback in attempt.ts then invokes the hook a second time. MemOS's auto-recall (LLM + embedding search) runs twice per message, and both results are discarded.
…agent_start_hook # Conflicts: # apps/memos-local-openclaw/index.ts
There was a problem hiding this comment.
Pull request overview
This PR adds a plugin-level workaround for an OpenClaw framework behavior where allowPromptInjection=false can lead to before_agent_start being invoked twice (due to prompt-mutation fields being stripped and the framework falling back via ??), causing MemOS auto-recall to run redundantly.
Changes:
- Read
hooks.allowPromptInjectionfrom the OpenClaw runtime config (api.config) during plugin registration. - Short-circuit the
before_agent_startauto-recall hook whenallowPromptInjectionis disabled to avoid unnecessary LLM/embedding work. - Add startup logging indicating whether auto-recall is enabled/disabled under the policy.
Comments suppressed due to low confidence (1)
apps/memos-local-openclaw/index.ts:925
- In allowPromptInjection=false mode, this early return prevents updating
currentAgentIdfromhookCtx.agentId. Since tools likememory_searchusecurrentAgentIdfor owner isolation, tool calls during the agent run may incorrectly use the defaultmainowner. SetcurrentAgentIdfromhookCtx?.agentIdbefore returning (or move the policy check after thecurrentAgentIdupdate) so isolation remains correct even when auto-recall is disabled.
api.on("before_agent_start", async (event: { prompt?: string; messages?: unknown[] }, hookCtx?: { agentId?: string; sessionKey?: string }) => {
if (!allowPromptInjection) return {};
if (!event.prompt || event.prompt.length < 3) return;
const recallAgentId = hookCtx?.agentId ?? "main";
currentAgentId = recallAgentId;
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
| if (!allowPromptInjection) return {}; | ||
| if (!event.prompt || event.prompt.length < 3) return; |
|
Hey @lcpdeb! Thanks for the contribution — already merged your PR, thanks for putting this together! 🚀 Btw if you enjoyed contributing and want to stay connected with the project, we run a Discord server for contributors, a chill space to discuss features, ask questions, or pick up new issues 👉🏻 MemOS Discord Server (Contributor Exclusive) Thanks again for shipping this, and happy MemOSing 🧠✨ |
Description
When allowPromptInjection=false, the OpenClaw framework strips prompt mutation fields from before_agent_start results, leaving undefined. The ?? fallback in attempt.ts then invokes the hook a second time. MemOS's auto-recall (LLM + embedding search) runs twice per message, and both results are discarded.
Related Issue (Required): Fixes #1276
Fix (workaround at plugin level): Detect allowPromptInjection in the plugin and skip auto-recall early.
Returning {} (truthy) prevents the ?? fallback from triggering a second call.
Note: This is a workaround for an openclaw framework issue. The framework-level fix would be in constrainLegacyPromptInjectionHook to return {} instead of undefined.
Type of change
Please delete options that are not relevant.
How Has This Been Tested?
Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration
Checklist
Reviewer Checklist