Skip to content

fix(mcp): SSE search_memories was leaking rejected/superseded memories - #7763

Merged
kodjima33 merged 1 commit into
mainfrom
fix/mcp-sse-stale-memory-filter
Jun 10, 2026
Merged

kodjima33 merged 1 commit into
mainfrom
fix/mcp-sse-stale-memory-filter

Conversation

@kodjima33

Copy link
Copy Markdown
Collaborator

Problem

The SSE MCP search_memories tool (/v1/mcp/sse, used by external clients like personal agents) only filtered is_locked when building results. The REST MCP path (routers/mcp.py) additionally skips:

  • user_review is False — memories the user explicitly rejected
  • invalid_at is not None — memories that were superseded by a newer fact

So semantic search over SSE could return stale or user-rejected facts that the REST path correctly hides.

Fix

Mirror the REST filter in the SSE results loop — skip rejected/invalidated memories before returning. Locked-content truncation behavior is unchanged.

Verification

Reproduced on dev: created an active memory ("favorite language = Python") and a rejected one (user_review=False, "= COBOL"), both vectorized. Vector search returns both; the old loop returned both (leaking COBOL), the fixed loop returns only Python.

🤖 Generated with Claude Code

The SSE MCP search_memories tool only filtered is_locked, while the REST MCP path
(routers/mcp.py) also skips user_review=False (user-rejected) and invalid_at
(superseded) memories. So agents hitting /v1/mcp/sse (e.g. external clients) could
get stale or explicitly-rejected facts back from semantic search. Mirror the REST
filter so both paths return only active, non-rejected memories.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@greptile-apps

greptile-apps Bot commented Jun 10, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a data-leak in the SSE MCP search_memories tool where rejected (user_review=False) and superseded (invalid_at set) memories were being returned to callers. The fix adds two guard conditions matching the REST MCP path in routers/mcp.py.

  • Adds user_review is False and invalid_at is not None skip conditions to the SSE result loop, closing the parity gap with the REST MCP search_memories implementation.
  • Pre-existing is_locked truncation behavior (include with 70-char clip) is deliberately preserved, leaving a minor divergence with the REST path which excludes locked memories entirely.
  • No changes to authentication, vector search, or score ranking logic.

Confidence Score: 4/5

The fix is correct and targeted — it closes the rejected/invalidated memory leak in the SSE path with minimal risk of regression.

The two new filter conditions directly match the REST MCP path's logic and are safe to merge. Two non-blocking observations remain: the fetch size isn't inflated before filtering (so heavily-filtered queries return fewer results than requested), and locked memories are still included with truncated content via SSE while the REST path excludes them entirely.

backend/routers/mcp_sse.py — specifically the is_locked handling and the fetch-limit strategy relative to the REST MCP path.

Important Files Changed

Filename Overview
backend/routers/mcp_sse.py Adds two filter conditions (user_review is False and invalid_at is not None) to the SSE search_memories loop, correctly mirroring the REST MCP path. Pre-existing is_locked truncation behavior is unchanged, leaving a minor divergence with the REST path which excludes locked memories entirely.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[SSE search_memories request] --> B[vector_db.find_similar_memories]
    B --> C[get_memories_by_ids]
    C --> D{user_review is False?}
    D -- Yes --> E[skip NEW]
    D -- No --> F{invalid_at is not None?}
    F -- Yes --> G[skip NEW]
    F -- No --> H{is_locked?}
    H -- Yes --> I[truncate content to 70 chars]
    I --> J[append to results]
    H -- No --> J
    J --> K[sort by relevance_score]
    K --> L[return memories]
Loading

Comments Outside Diff (1)

  1. backend/routers/mcp_sse.py, line 545 (link)

    P2 Filtered results may be fewer than limit

    The vector search fetches exactly limit candidates, then the new filter discards rejected/invalidated memories. If many of those match the query, the caller receives a silently short list. The REST MCP path mitigates this by fetching min(limit * 3, 60) candidates before filtering, ensuring the result set is still approximately the size the caller requested.

    Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Reviews (1): Last reviewed commit: "fix(mcp): SSE search_memories was leakin..." | Re-trigger Greptile

Comment on lines 560 to 562
if mem.get('is_locked', False):
content = mem.get('content', '')
mem['content'] = (content[:70] + '...') if len(content) > 70 else content

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 is_locked treatment diverges from REST MCP path

In routers/mcp.py line 160, locked memories are excluded entirely from search_memories results (is_locked is in the same continue guard as user_review and invalid_at). The SSE path instead includes them with content truncated to 70 characters. This is a pre-existing SSE behavior left intentionally unchanged by this PR, but it means a locked fact is visible (in truncated form) via SSE while invisible via REST. Worth reconciling if both paths are meant to behave identically.

@kodjima33
kodjima33 merged commit 40db1d3 into main Jun 10, 2026
3 checks passed
@kodjima33
kodjima33 deleted the fix/mcp-sse-stale-memory-filter branch June 10, 2026 06:40
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