Skip to content

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

Merged
syzsunshine219 merged 3 commits into
dev-v2.0.22from
autodev/MemOS-1532
Jul 2, 2026
Merged

Fix #1532: Agent incorrectly acts on cross-session memory context in new sessions#1869
syzsunshine219 merged 3 commits into
dev-v2.0.22from
autodev/MemOS-1532

Conversation

@Memtensor-AI

Copy link
Copy Markdown
Collaborator

Description

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.

Related Issue (Required): Fixes #1532

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Refactor (does not change functionality, e.g. code style improvements, linting)
  • Documentation update

How Has This Been Tested?

Executor did not report tests.

  • Unit Test
  • Test Script Or Test Steps (please provide)
  • Pipeline Automated API Test (please provide)

Checklist

  • I have performed a self-review of my own code
  • I have commented my code in hard-to-understand areas
  • I have added tests that prove my fix is effective or that my feature works
  • I have created related documentation issue/PR in MemOS-Docs (if applicable)
  • I have linked the issue to this PR (if applicable)
  • I have mentioned the person who will review this PR

@MatthewZhuang, @CarltonXiang, @syzsunshine219 please review this PR.

Reviewer Checklist

- Add session tracking to detect when memories are from previous sessions
- Tag cross-session memories with [from previous session] marker
- Inject passive instructions for cross-session memories (background knowledge only)
- Agent now waits for user's explicit instruction in new sessions
- Preserve active memory usage within same session

Fixes #1532
@Memtensor-AI

Copy link
Copy Markdown
Collaborator Author

Automated Test Results: PASSED\n\nCloud test-engine rerun after resolving the dev-v2.0.22 merge conflict.\n\nRun: tr-a3469260-15d\nScope: memos_local_openclaw\nResult: 13/13 tests passed\nCommand group: memos_local_openclaw/unit\nDuration: 3s\n\nLocal pre-push verification also passed: npm run build, plus focused vitest for cross-session-memory.\n\nStatus: merge conflict resolved; automated scope test passed. Manual code review is still required before merge.

@syzsunshine219
syzsunshine219 merged commit 53546ff into dev-v2.0.22 Jul 2, 2026
16 checks passed
@syzsunshine219
syzsunshine219 deleted the autodev/MemOS-1532 branch July 2, 2026 13:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants