Repository navigation
Closed
Closed
Copy link
Labels
Milestone
Description
Summary
Agents keep writing metadata filters in shapes search_notes rejects, and a rejected filter leaves an orphaned count query running. The orphan raises its own unhandled exception (Logfire issue #3004 in basic-memory-cloud).
Production evidence (basic-memory-cloud, last 14 days)
Malformed-filter 400s on search.execute:
| Count | Error | Likely intent |
|---|---|---|
| 5 | Invalid metadata filter (two keys in one operator dict) |
range: {"$gte": a, "$lte": b} |
| 2 | Unsupported operator in |
$in |
| 1 | Unsupported operator gte |
$gte |
| 1 | Unsupported operator contains |
array contains |
| 1 | Unsupported operator $exists |
key present / absent |
Plus 6 orphaned search.count ValueErrors raised after the request had already returned 400.
Representative trace: Codex called search_notes(metadata_filters={"started": {"gte": ...}}). The API returned a correct 400, but search.count started ~25ms after its parent api.search.search.execute_query span closed, then raised the same ValueError.
Root causes
- Orphaned count task.
api/v2/routers/search_router.pyrunsasyncio.gather(search_service.search(...), search_service.count(...)). Whensearchraises first,gatherpropagates but does not cancelcount, which keeps running with no owner. - Narrow filter grammar. The parser accepts only
$-prefixed operators and exactly one operator per key. Bare names (gte,in) are common in other query languages (Elasticsearch ranges), and multi-operator ranges are standard Mongo syntax. A dict value is always read as an operator (nested keys use dot notation), so accepting these is unambiguous. - Weak guidance. The
metadata_filtersparameter isDict[str, Any]with no schema description; operators are only listed deep in the docstring. The "unsupported operator" error does not list valid operators, so agents cannot self-correct.
Plan
- Widen inputs: accept bare operator names as aliases (
gt,gte,lt,lte,in,between,contains,exists); accept multiple operators per key, ANDed ({"$gte": a, "$lte": b}); add$contains(array contains) and$exists(key present/absent). - TaskGroup: validate filters before scheduling work, and run search + count in an
asyncio.TaskGroupso one failure cancels its sibling. - Better validation: error messages list the supported operators;
metadata_filtersgets aFielddescription listing the grammar so it appears in the tool JSON schema. - Regression tests: parser coverage for aliases, multi-operator,
$contains,$exists; an API-level test that a malformed filter returns 400 and leaves no count work running after the response.
Activity
claude commentedon Oct 8, 2026
Claude finished @phernandez's task in 22s —— View job
Triage
search.counttask is a real defect. The wider filter grammar is an enhancement bundled into the same issue.search_notes) and the API search router. Labellednonefor the cloud component, because the issue is in core code even though the evidence comes from cloud traces.search_notes) and Feature: Index custom YAML arrays per-element for metadata filtering #701 (per-element array indexing) are related context, not duplicates.Notes on the plan
asyncio.gatherto aTaskGroupcancels the sibling task, but any exception then reaches the caller wrapped in anExceptionGroup. The router will needexcept*or unwrapping to keep returning a clean 400.TaskGroupis a backstop for other failures.$containsand$existsneed to work on both SQLite and Postgres, so the tests should run on both backends.$eqcombined with$gte.I stopped at triage and made no code changes.
fix(search): widen metadata filter grammar and own the count query
fix(search): widen metadata filter grammar and own the count query