Skip to content

fix(mem0): scope prefetch search and result cache by user_id - #44

Merged
dizhaky merged 2 commits into
mainfrom
fix/mem0-prefetch-user-scoping
Jun 28, 2026
Merged

fix(mem0): scope prefetch search and result cache by user_id#44
dizhaky merged 2 commits into
mainfrom
fix/mem0-prefetch-user-scoping

Conversation

@dizhaky

@dizhaky dizhaky commented Jun 28, 2026

Copy link
Copy Markdown
Owner

Summary

  • queue_prefetch() was ignoring its user_id argument and searching under self._user_id (set once at initialize() time). In shared gateway sessions where the cached AIAgent is reused across participants, this caused cross-user memory leakage.
  • prefetch() returned cached results without checking which user they were fetched for, so a result queued for User A could be injected into User B's next prompt.

Changes

  • queue_prefetch(): capture effective_user_id = user_id or self._user_id before spawning the background thread; pass {"user_id": effective_user_id} directly to client.search() instead of self._read_filters(); store result as (user_id, text) tuple.
  • prefetch(): discard the cached result when its owner user_id does not match the current caller — prevents stale cross-user results from leaking into the wrong session.
  • __init__: initialize _prefetch_result to None instead of "" to match the new tuple-or-None shape.
  • Bugbot fix: empty-text tuples ((user_id, "")) now return "" rather than injecting a bare ## Mem0 Memory header with no bullets.

Addresses Codex PR#33 P1 review comment: Scope prefetched memory by current user.

All CI checks passed on the original draft PR #43.


Generated by Claude Code


Note

Medium Risk
Touches per-turn memory injection in multi-user gateway paths; behavior change is intentional isolation with limited surface area in one provider.

Overview
Fixes cross-user memory leakage when a shared Mem0MemoryProvider serves multiple gateway participants: background prefetch now searches and caches under the current turn’s user_id, not only self._user_id from initialize().

queue_prefetch() resolves effective_user_id = user_id or self._user_id, passes {"user_id": effective_user_id} to client.search() (instead of self._read_filters()), and stores the cache as (user_id, bullet text).

prefetch() returns cached bullets only when the tuple’s user matches the caller; mismatches or empty text yield "" so User A’s prefetch cannot appear in User B’s prompt and empty results do not inject a bare ## Mem0 Memory header.

__init__ sets _prefetch_result to None for the tuple-or-None cache shape.

Reviewed by Cursor Bugbot for commit 3a3d3ce. Configure here.

dizhaky added 2 commits June 28, 2026 01:12
queue_prefetch() was ignoring its user_id argument and always searching
under self._user_id (set at initialize() time). In shared gateway
sessions where the cached AIAgent is reused across participants, this
caused cross-user memory leakage: one user could receive prefetched
memories originally fetched for a different user.

Fixes:
- queue_prefetch(): capture effective_user_id before spawning thread,
  pass {"user_id": effective_user_id} directly instead of self._read_filters()
- Store prefetch result as (user_id, text) tuple so the consumer can
  verify ownership
- prefetch(): discard the cached result when its user_id does not match
  the current request — prevents stale cross-user results from leaking

Addresses Codex PR#33 P1 review comment.
…njection

When queue_prefetch gets results but all memory fields are empty, cached
becomes (user_id, ""). prefetch was treating any truthy tuple as valid,
returning "## Mem0 Memory
" with no bullets. Add not cached[1] guard.

Fixes Cursor Bugbot finding on PR #43.
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

@dizhaky
dizhaky merged commit b0c73df into main Jun 28, 2026
55 checks passed
@dizhaky
dizhaky deleted the fix/mem0-prefetch-user-scoping branch June 28, 2026 08:10
@github-actions

Copy link
Copy Markdown

🔎 Lint report: fix/mem0-prefetch-user-scoping 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: 8649 on HEAD, 8649 on base (➖ 0)

🆕 New issues: none

✅ Fixed issues: none

Unchanged: 4570 pre-existing issues carried over.

Diagnostics are surfaced as warnings — this check never fails the build.

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.

1 participant