Skip to content

Agent incorrectly acts on cross-session memory context in new sessions #1532

Closed

Description

@rongtianhua

Problem Description

When a new OpenClaw session starts, the memos-local-openclaw-plugin's auto-recall mechanism (before_agent_start hook) injects relevant memories from all historical sessions into the agent's context via memory_search. This causes the agent to:

  1. Act unprompted — sees historical context (e.g., a cron job misconfiguration from a previous session) and takes action without the user's first instruction
  2. Mix up session contexts — references facts from other sessions as if they were from the current session
  3. Give contextually inappropriate responses — starts a conversation with information the user never provided in this session

Specific example

  1. User starts a new session and says nothing yet
  2. Auto-recall fires, finds a chunk from a different session (task_id A) about "a-stock-trader-pro投递配置问题"
  3. Agent sees this as relevant, and without being asked, reports the cron configuration issue and asks the user to confirm
  4. User is confused — they haven't said anything, yet the agent is already responding to something from an old conversation

This violates the expected turn-taking behavior: the user's first message should trigger the agent's action, not the memory system's auto-injection.


Expected Behavior

In a new session where the user has not yet spoken:

  • Auto-recall may run, but the agent should treat injected memories as background knowledge only
  • The agent should not proactively act, query, or reply based solely on auto-recalled memories
  • The agent should wait for the user's first instruction before taking any action
  • If the agent needs to reference a recalled memory, it should explicitly note the source (e.g., "根据之前的会话...")

Root Cause Analysis

The issue stems from how auto-recall is integrated into the agent loop:

  • The before_agent_start hook runs memory_search with the user's message and injects results into the agent's context
  • When the user hasn't said anything in a new session, the "user's message" may be empty or a system greeting
  • Auto-recall still returns historical chunks that are topically relevant from other sessions
  • The agent's system prompt does not have a clear instruction to only use recalled memories passively (as background) and not act on them without user prompting

Proposed Solutions (Pick One or Combine)

Option A: Session-level memory scope (recommended)

Add a session-aware flag to auto-recall. When a session is freshly started (first user message in a new session), auto-recall results should be tagged as "background only". The agent's system prompt should include:

If injected memories are from a previous session, treat them as background knowledge only. Do not act on them unprompted. Wait for the user's first instruction.

Option B: Suppress auto-recall on empty/first-turn messages

If the user's message is empty (new session, no user input yet), skip auto-recall injection entirely, or inject with a special marker that tells the agent "these memories are from previous sessions — do not act on them."

Option C: Agent instruction in system prompt

Add a clear instruction to the agent's system context when memories are injected:

[memory_search results from auto-recall — these are from PREVIOUS sessions.
Treat as background knowledge only. Do not proactively act or reply.
Wait for the user to send a message before taking any action.]

Environment

  • Plugin: @memtensor/memos-local-openclaw-plugin v1.0.9
  • OpenClaw: (latest)
  • Node.js: >=22
  • OS: macOS

Additional Context

This issue is particularly problematic because:

  1. Users expect a fresh session to start with a clean slate until they speak
  2. The agent appears to "know things unprompted" which breaks the conversational flow
  3. It makes the agent seem like it's "jumping ahead" or "not listening" to the current user
  4. Cross-session memory should enhance future conversations, not trigger unprompted actions in new ones

Relevant code area: before_agent_start hook + auto-recall injection logic in index.ts / plugin-impl.ts.


Severity: Medium — degrades user experience and trust in the memory system, but does not cause data loss or system failure.

Activity

  1. added
    ai:taskDispatched to AI coding agent | 已派发给 AI 编码任务
    on May 26, 2026
  2. added
    ai:taskDispatched to AI coding agent | 已派发给 AI 编码任务
    and removed
    ai:taskDispatched to AI coding agent | 已派发给 AI 编码任务
    on Jun 3, 2026
  3. deleted a comment from MaiHHConnect on Jun 3, 2026
  4. Memtensor-AI commented on Jun 3, 2026

    @Memtensor-AI
    Collaborator

    🤖 AutoDev has picked up this issue and started working on it.

    Task ID: ac777fb8427711e0
    Working branch: autodev/MemOS-1532
    Target branch: main
    Workflow: opsp (analysis → coding → testing → PR)

    I will post the PR link here once done. If I need more information, I will ask in the comments.

  5. added a commit that references this issue on Jun 3, 2026
    0d9fab3
  6. Memtensor-AI commented on Jun 3, 2026

    @Memtensor-AI
    Collaborator

    🚀 AutoDev task ac777fb8427711e0 pushed commit.

    Summary: Successfully fixed issue #1532 where the agent incorrectly acted on cross-session memory context in new sessions.

    Root Cause:
    The before_prompt_build hook in memos-local-openclaw-plugin was injecting memories from all historical sessions without distinguishing between current and previous session context. The injected instructions told the agent to "MUST treat these as established knowledge and use them directly," causing the agent to proactively respond to historical context before the user spoke in a new session.

    Solution Implemented:

    1. Session tracking: Added currentSessionKey variable to track the active session and detect session boundaries
    2. Cross-session detection: Check if retrieved memories have different sessionKey than current session
    3. New session detection: Detect via session key change or /new//reset command pattern
    4. Session markers: Tag cross-session memories with [from previous session] in the injected context
    5. Context-aware instructions: Inject passive instructions for cross-session memories ("BACKGROUND KNOWLEDGE ONLY - Do NOT act unprompted - WAIT for user's instruction") while preserving active instructions for same-session memories

    Changes:

    • Modified apps/memos-local-openclaw/index.ts (lines 1864-2090)
    • Added session tracking and cross-session memory detection logic
    • Created test file apps/memos-local-openclaw/tests/cross-session-memory.test.ts
    • Created verification report documenting the fix

    Testing:
    The fix correctly handles three scenarios:

    1. New session with historical memories → agent waits for user input (passive mode)
    2. Same session continuation → agent uses memories actively (active mode)
    3. Empty database → no errors or false warnings

    Impact:

    • Backward compatible - no API or schema changes
    • Negligible performance overhead (one string comparison per memory hit)
    • Significantly improves UX by respecting session boundaries
    • Preserves the benefit of having relevant background context available when needed

    The agent now correctly treats cross-session memories as background knowledge and waits for the user's explicit instruction before taking action, fixing the unprompted response behavior.

    Base branch: main
    Branch: autodev/MemOS-1532
    Commit: 0d9fab3

    I will create the PR next and post the PR link here.

  7. Memtensor-AI commented on Jun 3, 2026

    @Memtensor-AI
    Collaborator

    ✅ AutoDev task ac777fb8427711e0 completed.

    Summary: Successfully fixed issue #1532 where the agent incorrectly acted on cross-session memory context in new sessions.

    Root Cause:
    The before_prompt_build hook in memos-local-openclaw-plugin was injecting memories from all historical sessions without distinguishing between current and previous session context. The injected instructions told the agent to "MUST treat these as established knowledge and use them directly," causing the agent to proactively respond to historical context before the user spoke in a new session.

    Solution Implemented:

    1. Session tracking: Added currentSessionKey variable to track the active session and detect session boundaries
    2. Cross-session detection: Check if retrieved memories have different sessionKey than current session
    3. New session detection: Detect via session key change or /new//reset command pattern
    4. Session markers: Tag cross-session memories with [from previous session] in the injected context
    5. Context-aware instructions: Inject passive instructions for cross-session memories ("BACKGROUND KNOWLEDGE ONLY - Do NOT act unprompted - WAIT for user's instruction") while preserving active instructions for same-session memories

    Changes:

    • Modified apps/memos-local-openclaw/index.ts (lines 1864-2090)
    • Added session tracking and cross-session memory detection logic
    • Created test file apps/memos-local-openclaw/tests/cross-session-memory.test.ts
    • Created verification report documenting the fix

    Testing:
    The fix correctly handles three scenarios:

    1. New session with historical memories → agent waits for user input (passive mode)
    2. Same session continuation → agent uses memories actively (active mode)
    3. Empty database → no errors or false warnings

    Impact:

    • Backward compatible - no API or schema changes
    • Negligible performance overhead (one string comparison per memory hit)
    • Significantly improves UX by respecting session boundaries
    • Preserves the benefit of having relevant background context available when needed

    The agent now correctly treats cross-session memories as background knowledge and waits for the user's explicit instruction before taking action, fixing the unprompted response behavior.

    Base branch: main
    Branch: autodev/MemOS-1532
    Commit: 0d9fab3
    PR: #1869
    Assigned to: @MatthewZhuang, @CarltonXiang, @syzsunshine219
    Reviewers: @MatthewZhuang, @CarltonXiang, @syzsunshine219

  8. added a commit that references this issue on Jul 2, 2026
    53546ff
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    ai:taskDispatched to AI coding agent | 已派发给 AI 编码任务area:pluginOpenClaw & Hermestypes:bugSomething isn't working | 功能异常

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions