Add semantic and hybrid modes and similar_to to mail_search #48

Merged
svc-agent-writer merged 1 commit from feat/semantic-search into main 2026-10-31 13:31:35 +00:00

Closes #44. Part of #9. Decision: ADR 0005 §6 and §8.

Summary

  • mail_search gains two parameters:
    • mode: keyword (the default, unchanged), semantic or hybrid.
    • similar_to: a message id.
    • Rows and footer keep their format. In the new modes, rows come best match first and total counts the ranked candidates. Every filter applies in every mode.
  • Semantic. ServerState.embedder() loads the pinned model on the first semantic call, under a lock, and keeps it. The query is embedded in memory, then never stored or logged. semantic.top_k scans the vectors of the filtered rows in chunks of 2048 (about 3 MB each) and keeps the best k in a heap.
  • Hybrid. Reciprocal rank fusion (k = 60) of the semantic top 100 and the FTS5 top 100 by rank. Meaning comes first, so it breaks ties.
  • similar_to uses the stored vector of that message. It leaves out the message and every copy with its Message-ID. It refuses a query and mode="hybrid", and answers not_found when the message has no vector yet.
  • Off. Without [embed], the new modes and similar_to answer Error: code=invalid_argument, and the message names [embed].
  • Store.
    • _filters() is factored out of search(), so every mode shares one set of filter terms.
    • New methods: keyword_ranked, vector_chunks (keyset pages, the lock held per chunk), vector, copies, messages_by_ids.
  • Deploy. The base sync and api memory limits rise from 512Mi to 768Mi.
  • Docs. The README tool row and a Semantic search section. SECURITY.md says the query stays in memory on the host.

Test plan

  • make check: 130 passed, mypy strict clean, pip-audit clean.
  • Fake-vector tests cover:
    • the semantic order, paging and the mailbox filter;
    • hybrid returning both lists;
    • similar_to leaving out its own message;
    • the four refusals;
    • keyword mode unchanged;
    • no query text in the imap_mailmind_mcp logs.
  • top_k keeps the best across chunks and honours the exclusions.
  • fuse order.
  • The store scan honours the filters and the model name.
  • Real model (make model): the query "when must I pay the utility invoice" ranks the water-rates mail ("water bill … Payment is due") first. It ranks above three mails whose subject contains the word "Invoice".
  • Without [embed], both new paths are refused.

🤖 Generated with Claude Code

Closes #44. Part of #9. Decision: ADR 0005 §6 and §8. ## Summary - **`mail_search`** gains two parameters: - `mode`: `keyword` (the default, unchanged), `semantic` or `hybrid`. - `similar_to`: a message id. - Rows and footer keep their format. In the new modes, rows come best match first and `total` counts the ranked candidates. Every filter applies in every mode. - **Semantic.** `ServerState.embedder()` loads the pinned model on the first semantic call, under a lock, and keeps it. The query is embedded in memory, then never stored or logged. `semantic.top_k` scans the vectors of the filtered rows in chunks of 2048 (about 3 MB each) and keeps the best *k* in a heap. - **Hybrid.** Reciprocal rank fusion (k = 60) of the semantic top 100 and the FTS5 top 100 by `rank`. Meaning comes first, so it breaks ties. - **`similar_to`** uses the stored vector of that message. It leaves out the message and every copy with its Message-ID. It refuses a `query` and `mode="hybrid"`, and answers `not_found` when the message has no vector yet. - **Off.** Without `[embed]`, the new modes and `similar_to` answer `Error: code=invalid_argument`, and the message names `[embed]`. - **Store.** - `_filters()` is factored out of `search()`, so every mode shares one set of filter terms. - New methods: `keyword_ranked`, `vector_chunks` (keyset pages, the lock held per chunk), `vector`, `copies`, `messages_by_ids`. - **Deploy.** The base `sync` and `api` memory limits rise from 512Mi to 768Mi. - **Docs.** The README tool row and a Semantic search section. SECURITY.md says the query stays in memory on the host. ## Test plan - `make check`: 130 passed, mypy strict clean, pip-audit clean. - Fake-vector tests cover: - the semantic order, paging and the mailbox filter; - hybrid returning both lists; - `similar_to` leaving out its own message; - the four refusals; - keyword mode unchanged; - no query text in the `imap_mailmind_mcp` logs. - `top_k` keeps the best across chunks and honours the exclusions. - `fuse` order. - The store scan honours the filters and the model name. - Real model (`make model`): the query "when must I pay the utility invoice" ranks the water-rates mail ("water bill … Payment is due") first. It ranks above three mails whose subject contains the word "Invoice". - Without `[embed]`, both new paths are refused. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
The api loads the local model on the first semantic call, embeds the query in memory, and ranks the filtered rows by cosine over the stored vectors in chunks. Hybrid fuses that ranking with the FTS5 rank by reciprocal rank fusion. The sync and api memory limits rise by 256Mi for the model (ADR 0005).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign in to join this conversation.
No description provided.