Skip to content

fix(memory): scope tool calls to current gateway user in shared threads - #46

Closed
cursor[bot] wants to merge 1 commit into
mainfrom
cursor/critical-bug-management-c535
Closed

fix(memory): scope tool calls to current gateway user in shared threads#46
cursor[bot] wants to merge 1 commit into
mainfrom
cursor/critical-bug-management-c535

Conversation

@cursor

@cursor cursor Bot commented Jun 28, 2026

Copy link
Copy Markdown

Bug and impact

With thread_sessions_per_user=False (default), multiple users in a Slack/Discord thread share one cached AIAgent. Recent fixes scoped automatic prefetch/sync to agent._user_id, but memory tool calls (mem0_search, memgw_recall, etc.) still used the provider's stale _user_id from the first speaker — a cross-user memory read/write leak.

Root cause

Gateway refreshes agent._user_id on cached-agent reuse but never updates memory providers. handle_tool_call did not accept a per-turn user_id override.

Fix

  • MemoryManager.sync_user_id() updates provider _user_id when gateway reuses a cached agent for a new caller
  • Pass user_id through handle_tool_call from tool dispatch
  • mem0/memgw tool handlers honor kwargs['user_id']

Validation

  • pytest tests/agent/test_memory_user_id.py — 16 passed (incl. new shared-thread scoping tests)
Open in Web View Automation 

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>
@github-actions

Copy link
Copy Markdown

🔎 Lint report: cursor/critical-bug-management-c535 vs origin/main

ruff

Total: 0 on HEAD, 0 on base (➖ 0)

🆕 New issues: none

✅ Fixed issues: none

Unchanged: 0 pre-existing issues carried over.

ty (type checker)

Total: 8650 on HEAD, 8649 on base (🆕 +1)

🆕 New issues (1):

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.

@dizhaky dizhaky closed this Jun 28, 2026
@dizhaky
dizhaky deleted the cursor/critical-bug-management-c535 branch June 28, 2026 15:00
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.

2 participants