Detect allowPromptInjection and optimize auto-recall handling#1285
Merged
syzsunshine219 merged 2 commits intoMemTensor:mainfrom Mar 19, 2026
Merged
Detect allowPromptInjection and optimize auto-recall handling#1285syzsunshine219 merged 2 commits intoMemTensor:mainfrom
syzsunshine219 merged 2 commits intoMemTensor:mainfrom
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
Contributor
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.
Comment on lines
+921
to
922
| if (!allowPromptInjection) return {}; | ||
| if (!event.prompt || event.prompt.length < 3) return; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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