fix(memory): scope tool calls to current gateway user in shared threads - #46
Closed
cursor[bot] wants to merge 1 commit into
Closed
fix(memory): scope tool calls to current gateway user in shared threads#46cursor[bot] wants to merge 1 commit into
cursor[bot] wants to merge 1 commit into
Conversation
Cached agents refresh agent._user_id per turn but memory providers kept the first speaker's _user_id, so mem0/memgw tool calls in shared-thread sessions could read or write another user's memory. Sync provider identity on cache reuse and pass user_id through handle_tool_call. Co-authored-by: dizhaky <dizhaky@gmail.com>
🔎 Lint report:
|
| Rule | Count |
|---|---|
invalid-assignment |
1 |
First entries
agent/memory_manager.py:438: [invalid-assignment] invalid-assignment: Object of type `str & ~AlwaysFalsy` is not assignable to attribute `_user_id` on type `MemoryProvider & <Protocol with members '_user_id'>`
✅ Fixed issues: none
Unchanged: 4570 pre-existing issues carried over.
Diagnostics are surfaced as warnings — this check never fails the build.
3 tasks
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.
Bug and impact
With
thread_sessions_per_user=False(default), multiple users in a Slack/Discord thread share one cachedAIAgent. Recent fixes scoped automatic prefetch/sync toagent._user_id, but memory tool calls (mem0_search,memgw_recall, etc.) still used the provider's stale_user_idfrom the first speaker — a cross-user memory read/write leak.Root cause
Gateway refreshes
agent._user_idon cached-agent reuse but never updates memory providers.handle_tool_calldid not accept a per-turnuser_idoverride.Fix
MemoryManager.sync_user_id()updates provider_user_idwhen gateway reuses a cached agent for a new calleruser_idthroughhandle_tool_callfrom tool dispatchkwargs['user_id']Validation
pytest tests/agent/test_memory_user_id.py— 16 passed (incl. new shared-thread scoping tests)