bug(cli): search --filter with a top-level AND/OR drops the -u/--agent-id/--app-id/--run-id scope
Component
Other
Description
Summary
CLI (Python mem0-cli and Node @mem0/cli): mem0 search with a --filter that has a top-level AND/OR drops the -u/--user-id, --agent-id, --app-id and --run-id scope (and the configured defaults), so the search is not scoped to the requested entity and nothing warns.
Root cause: PlatformBackend._build_filters (cli/python/src/mem0_cli/backend/platform.py:149) and _buildFilters (cli/node/src/backend/platform.ts:181) return any filter with a top-level AND/OR unchanged, before the entity conditions are added.
Steps to Reproduce
mem0 search "invoices" -u alice --filter '{"AND": [{"categories": {"in": ["work"]}}]}'(the example from mem0 search --help and docs/platform/cli.mdx, added in #6907)
Expected Behavior
The request to /v3/memories/search/ carries {"filters": {"AND": [{"user_id": "alice"}, {"AND": [{"categories": {"in": ["work"]}}]}]}}.
Actual Behavior
It carries {"filters": {"AND": [{"categories": {"in": ["work"]}}]}}, with no user_id.
Environment
- mem0 version: mem0-cli 0.2.13 / @mem0/cli 0.2.14 (
main@94c3fe9f238f3dbf29c9ce98643bd71eb13077cd) - Python/Node version: Python 3.12 / Node 22
- OS: macOS
How You Verified This
What I Ran
cli/python/tests/test_platform_backend_options.py::TestSearchFilterScope (captures the request body), on unmodified main @ 94c3fe9f238f3dbf29c9ce98643bd71eb13077cd:
cd cli/python && python -m pytest -q tests/test_platform_backend_options.py -k TestSearchFilterScopeWhat I Saw
E {'AND': [{'categories': {'in': ['work']}}]} != {'AND': [{'user_id': 'alice'}, {'AND': [{'categories': {'in': [...]}}]}]}
E AssertionError: assert {'OR': [{'cat...['travel']}}]} == {'AND': [{'us...': [...]}}]}]}
E Left contains 1 more item:
E {'OR': [{'categories': {'in': ['work']}}, {'categories': {'in': ['travel']}}]}(2 of the 3 tests fail; the Node test "search --filter keeps entity scope" in cli/node/tests/platform-backend.test.ts failed the same way in our earlier run on main @ 8d6c0019, 2026-09-25; both platform files are unchanged since.) An end-to-end run through the real Typer app over an httpx mock transport, in that same earlier run, showed the same request body.
Why This Is a Bug
The -u flag and the configured defaults are documented as scoping the search; the platform filter docs (v2-memory-filters.mdx) allow nesting AND/OR, so the scope can be kept.
What I Ruled Out
Filters without a top-level AND/OR keep the scope (existing tests). Not observed: the platform's actual response to the unscoped request (needs a real API key).
A fix with a regression test is ready: nest the user's filter as one condition next to the entity IDs, {"AND": [{"user_id": "alice"}, <filter>]}, in both CLIs (cli/python: 202 pass; cli/node: 153 pass). Branch: https://github.com/breken-ai/mem0/tree/fix/cli-filter-keeps-entity-scope. We'll open the PR only once this issue is accepted.
AI Assistance
AI found and wrote this, and I have not reproduced it myself
(To be precise: an AI coding agent, Claude Code running for the breken-ai account, found the bug, wrote the regression test and ran it against current main; the output above is from that run. No human has reproduced it separately.)
Source: mem0ai/mem0