Skip to content

fix: honor project scope in smart search#806

Open
mturac wants to merge 2 commits into
rohitg00:mainfrom
mturac:fix/issue-787-project-scoped-smart-search
Open

fix: honor project scope in smart search#806
mturac wants to merge 2 commits into
rohitg00:mainfrom
mturac:fix/issue-787-project-scoped-smart-search

Conversation

@mturac
Copy link
Copy Markdown

@mturac mturac commented Jun 3, 2026

Summary

  • add project filters to the MCP memory_recall and memory_smart_search schemas
  • forward project through MCP server and standalone proxy calls
  • filter smart-search observation and memory hits by project while preserving legacy unscoped results
  • add regression coverage for project-scoped compact, expanded, proxy, and schema paths

Tests

  • git diff --check
  • npm test -- test/smart-search.test.ts test/mcp-standalone-proxy.test.ts test/mcp-surface-default.test.ts
  • npm run build

Closes #787

Summary by CodeRabbit

  • New Features

    • Memory search (recall and smart search) now accepts an optional project field to scope results; local and proxied modes honor this scope and saved memories can be project-tagged.
    • Smart-search result expansion respects project scope and increases temporary over-fetching to preserve result quality after filtering.
  • Tests

    • Added tests verifying project-based filtering for compact and expanded smart-search and proxy behavior.

Review Change Stack

@vercel
Copy link
Copy Markdown

vercel Bot commented Jun 3, 2026

@mturac is attempting to deploy a commit to the rohitg00's projects Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jun 3, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 30346ff1-e2e5-42ca-a220-9c40cfc7619c

📥 Commits

Reviewing files that changed from the base of the PR and between 5745e89 and e02e175.

📒 Files selected for processing (1)
  • src/functions/smart-search.ts

📝 Walkthrough

Walkthrough

Adds an optional project parameter to MCP memory tools and threads project-based filtering into smart-search, MCP server proxying, local persistence, and tests so searches can be scoped to a project.

Changes

Project-based scoping for MCP search tools

Layer / File(s) Summary
MCP tool schemas and server forwarding
src/mcp/tools-registry.ts, src/mcp/server.ts, test/mcp-surface-default.test.ts
memory_recall and memory_smart_search input schemas now include an optional project string. MCP server forwards the trimmed project into mem::search and mem::smart-search payloads when provided.
Standalone MCP server implementation
src/mcp/standalone.ts, test/mcp-standalone-proxy.test.ts
Standalone validation accepts optional project for memory_save, memory_recall, and memory_smart_search. In proxy mode, POST bodies to /agentmemory/remember, /agentmemory/search, and /agentmemory/smart-search include project. In local mode, memory_save persists project and local recall/smart-search filter results by project.
Smart-search core project filtering
src/functions/smart-search.ts, test/smart-search.test.ts
Smart-search extracts a trimmed project and filters expandIds and hybrid hits by project via new helpers that check Memory.project then fallback to Session.project. Hybrid search increases over-fetch (limit × 3, capped) and applies agent-scope filtering, async project filtering, then final truncation.

Sequence Diagram

sequenceDiagram
  participant MCP as MCP Client
  participant Server as MCP Server
  participant SmartSearch as smart-search fn
  participant MemoryKV as MemoryKV
  participant SessionKV as SessionKV
  MCP->>Server: memory_smart_search(query, project)
  Server->>SmartSearch: call with project in payload
  SmartSearch->>SmartSearch: extract & trim project
  alt Hybrid search branch
    SmartSearch->>MemoryKV: fetch overFetch results
    SmartSearch->>SmartSearch: apply agent-scope filtering
    SmartSearch->>MemoryKV: read Memory/Session for project matching (async)
    SmartSearch->>SmartSearch: filter by project and truncate to limit
  else expandIds branch
    SmartSearch->>MemoryKV: fetch expanded observation ids
    SmartSearch->>SessionKV: read Session/Memory for project matching (async)
    SmartSearch->>SmartSearch: filter expanded results by project
  end
  SmartSearch->>Server: filtered observations
  Server->>MCP: scoped results
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Suggested reviewers

  • rohitg00

Poem

🐰 The rabbit trims each project's thread,
Hops through memories, filters ahead.
Queries now whisper "this project's mine",
Results return tidy, neat, and fine.
Hop—smart-search served, scoped every time.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix: honor project scope in smart search' clearly summarizes the main change: applying project-scope filtering to smart search operations.
Linked Issues check ✅ Passed All coding objectives from issue #787 are met: project parameter added to memory_smart_search and memory_recall schemas; project forwarded through MCP server and standalone proxy; project filtering implemented in smart-search hybrid search with wildcard-compatible guard; comprehensive test coverage added.
Out of Scope Changes check ✅ Passed All changes are directly aligned with issue #787 requirements: schema updates, project forwarding, filtering logic, and test coverage—no out-of-scope modifications detected.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

Review ran into problems

🔥 Problems

Git: Failed to clone repository. Please run the @coderabbitai full review command to re-trigger a full review. If the issue persists, set path_filters to include or exclude specific files.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
test/mcp-standalone-proxy.test.ts (1)

56-87: ⚡ Quick win

Add a local-fallback project-scoping regression here too.

These assertions only prove the proxy request bodies carry project. src/mcp/standalone.ts now has separate local filtering logic in handleLocal(), so a fallback regression would still leave this suite green. Please add an ECONNREFUSED case that seeds two projects and verifies memory_recall/memory_smart_search only return the requested one.

Also applies to: 109-127

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@test/mcp-standalone-proxy.test.ts` around lines 56 - 87, The test currently
only asserts the proxied request body carries project but misses the local
fallback branch in handleLocal; modify the test (both the memory_smart_search
case and the memory_recall case at lines 109-127) to simulate an ECONNREFUSED
upstream (e.g., make installFetch throw or return a connection-refused error) so
the code falls back to handleLocal, seed two memories in two different projects,
then call handleToolCall("memory_smart_search", ...) and
handleToolCall("memory_recall", ...) with project set to one project and assert
the returned results include only items from that project; locate references to
handleToolCall, installFetch, memory_smart_search, memory_recall and handleLocal
to add the error simulation, seeding, and project-scoped assertions.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/functions/smart-search.ts`:
- Around line 315-353: filterExpandedByProject and filterHybridByProject perform
observationMatchesProject serially inside a loop, causing many sequential
kv.get() calls; change them to run the per-item checks in parallel (use
Promise.all over mapped observationMatchesProject promises) and then filter the
original arrays by the resolved boolean results so the functions
(filterExpandedByProject, filterHybridByProject) return the same scoped arrays
but with kv reads performed concurrently to reduce latency.

---

Nitpick comments:
In `@test/mcp-standalone-proxy.test.ts`:
- Around line 56-87: The test currently only asserts the proxied request body
carries project but misses the local fallback branch in handleLocal; modify the
test (both the memory_smart_search case and the memory_recall case at lines
109-127) to simulate an ECONNREFUSED upstream (e.g., make installFetch throw or
return a connection-refused error) so the code falls back to handleLocal, seed
two memories in two different projects, then call
handleToolCall("memory_smart_search", ...) and handleToolCall("memory_recall",
...) with project set to one project and assert the returned results include
only items from that project; locate references to handleToolCall, installFetch,
memory_smart_search, memory_recall and handleLocal to add the error simulation,
seeding, and project-scoped assertions.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 43c4f65c-6811-4896-ba0e-9a7cd104b907

📥 Commits

Reviewing files that changed from the base of the PR and between e5c9dc6 and 5745e89.

📒 Files selected for processing (7)
  • src/functions/smart-search.ts
  • src/mcp/server.ts
  • src/mcp/standalone.ts
  • src/mcp/tools-registry.ts
  • test/mcp-standalone-proxy.test.ts
  • test/mcp-surface-default.test.ts
  • test/smart-search.test.ts

Comment thread src/functions/smart-search.ts
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.

Project scoping not honored by MCP search tools (memory_smart_search / memory_recall) or /smart-search

1 participant