Embed attachment text and rank it in semantic search #79

Closed
opened 2026-11-01 01:06:15 +00:00 by svc-agent-writer · 2 comments

Parent: #9 (Phase 4). Decision: ADR 0007 §8, which amends ADR 0005 §3.

Embed extracted attachment text, and rank it in the semantic and hybrid modes of mail_search.

Scope

  • [attachments] embed_chunks (default 4) in config.py.
  • attachment_embeddings(message_id, part, chunk, model, vec) in a new schema step, v8. #73 shipped attachment_text as v7 in 0.6.0. A deleted message loses its vectors in the same transaction. A moved message keeps or copies them, as the attachment text rows do: by the #59 key, in any account (#86).
  • embed.py: cut each extracted attachment into chunks of 512 tokens with the pinned tokenizer, at most embed_chunks, from the start. Embed the file name and the chunk text. New messages come first, attachment chunks second.
  • store.py and server.py: semantic mode scores each message by its best cosine over its message vector and its chunk vectors. Hybrid mode fuses that ranking with the keyword ranking, as today. When an attachment chunk gives the best match, the snippet line starts with [attachment <part>] and shows the start of the chunk. similar_to compares the given message vector with every vector.
  • mail_status shows the count of embedded attachment chunks.
  • ADR 0005 §3 reads as ADR 0007 §8 amends it.
  • README: the mail_search row and the [attachments] row.

Acceptance

  • Unit tests with a fake embedder:
    • the chunking, and the embed_chunks bound;
    • the order: new messages before attachment chunks;
    • the best-of score, and the [attachment <part>] snippet;
    • delete and move;
    • with [embed] absent, or [attachments] absent, no chunk is embedded.
  • A model test, which skips until make model: a query that matches only words in an attachment ranks that message first in semantic mode.
  • make check passes.

Blocked by

Filed under: operator word "amend 77. I want semantic attachment search" sha256:b1a874f89ad1

Parent: #9 (Phase 4). Decision: ADR 0007 §8, which amends ADR 0005 §3. Embed extracted attachment text, and rank it in the semantic and hybrid modes of `mail_search`. ## Scope - `[attachments] embed_chunks` (default 4) in `config.py`. - `attachment_embeddings(message_id, part, chunk, model, vec)` in a new schema step, v8. #73 shipped `attachment_text` as v7 in 0.6.0. A deleted message loses its vectors in the same transaction. A moved message keeps or copies them, as the attachment text rows do: by the #59 key, in any account (#86). - `embed.py`: cut each extracted attachment into chunks of 512 tokens with the pinned tokenizer, at most `embed_chunks`, from the start. Embed the file name and the chunk text. New messages come first, attachment chunks second. - `store.py` and `server.py`: semantic mode scores each message by its best cosine over its message vector and its chunk vectors. Hybrid mode fuses that ranking with the keyword ranking, as today. When an attachment chunk gives the best match, the snippet line starts with `[attachment <part>]` and shows the start of the chunk. `similar_to` compares the given message vector with every vector. - `mail_status` shows the count of embedded attachment chunks. - ADR 0005 §3 reads as ADR 0007 §8 amends it. - README: the `mail_search` row and the `[attachments]` row. ## Acceptance - Unit tests with a fake embedder: - the chunking, and the `embed_chunks` bound; - the order: new messages before attachment chunks; - the best-of score, and the `[attachment <part>]` snippet; - delete and move; - with `[embed]` absent, or `[attachments]` absent, no chunk is embedded. - A model test, which skips until `make model`: a query that matches only words in an attachment ranks that message first in semantic mode. - `make check` passes. ## Blocked by - #73 Filed under: operator word "amend 77. I want semantic attachment search" sha256:b1a874f89ad1
Collaborator

Status: CLEAN after 1 rework round(s) · 0 blocking defect(s) · 0 lens(es) INCONCLUSIVE (could not run) · 0 lens(es) DEGRADED (deterministic denial) · complexity substrate · dispatch effort high

Run: 20261101T084957Z-7f9da49a

Held because: CLEAN but 4 open risk(s) (2 verification · 2 decision) — HOLD-ALL parks everything. The risks, verbatim:

Open risks:

  1. Chunk length vs the model's 512-token window: ADR 0007 §8 says 512-token chunks embedded with the file name in front, but the model truncates at 512 including CLS/SEP (embed.py:69), so a full chunk loses its last ~3-20 tokens. Lean: keep CHUNK_TOKENS = 512, as the ADR says. Question: keep 512, or cut at 512 - 2 - len(filename tokens), which may need an ADR note? [decision]
  2. Upper bound of embed_chunks: the ADR fixes only the default of 4. Lean: le=16 (about 6,000 words per attachment; keeps the ADR 0005 §8 200k-vector budget meaningful). Question: is 16 right, or should it match per_message (le=50)? [decision]
  3. The model test and make check were not run at spec time (the sandbox denies .venv/bin/python, and system python has no numpy). Only a pure-Python model of best_k's selection was checked: 20,000 random trials matched brute force. In the dispatch worktree, run make model && uv run python3 -m pytest -q tests/test_semantic.py -k attachment_only and confirm it passes (not skipped), then run make check against the re-measured base failure set. [verification]
  4. Backfill cost on the live instance (ADR 0007 Consequences: 1-2 core-hours per 5,000 attachments). After deploying, check that mail_status attachment_chunks=N rises while embedded= keeps pace with new mail, and that the sync container stays within its memory limit (deploy/k8s/base/deployment.yaml:115). [verification]

Change: The spec adds [attachments] embed_chunks (default 4, ge=1, le=16) to AttachmentsConfig (config.py:203-219). Schema v8 creates the attachment_embeddings table and its delete trigger in the shape of embeddings (store.py:209-218). Three new methods mirror unembedded/copy_embeddings/set_embeddings (store.py:821-857). _copy_attachment_text (store.py:1834-1844) also copies vectors. attachment_vector_chunks shares a WHERE helper with vector_chunks (store.py:1383-1412). In embed.py, a split_tokens helper, a second tokenizer with no truncation, and Embedder.chunks do the chunking, and EmbedWorker._account (embed.py:133-154) embeds new messages before attachment chunks. semantic.py gets best_k. It does NOT keep the argsort(-scores)[: k + len(exclude)] prefilter (semantic.py:32). Each page is walked in np.lexsort((ids, -scores)) order, keeping each id's first (best) vector and skipping excluded ids, until k distinct ids are taken. Those are then merged into a min-heap of (score, -id) as at semantic.py:27,36-40, with a members dict holding scores and sources. The result is the exact top k by per-id maximum, even when one id repeats up to 800 times in a page. top_k becomes a wrapper. server.py _ranked (270-302) chains the chunk streams when [attachments] is set; _chunk_snippet adds the [attachment N] prefix, and mail_status gains attachment_chunks=. README, config.example.toml and CLAUDE.md doc rows are updated.

Files: src/imap_mailmind_mcp/config.py · src/imap_mailmind_mcp/store.py · src/imap_mailmind_mcp/embed.py · src/imap_mailmind_mcp/semantic.py · src/imap_mailmind_mcp/server.py · README.md · config.example.toml · CLAUDE.md · tests/test_embed.py · tests/test_semantic.py · tests/test_store.py · tests/test_attachments.py · tests/test_attachment_vectors.py

Decision: accept the 4 open risk(s) above and dispatch this spec (svd:go), or park (svd:skip) / re-spec (svd:respec)?

Your call (when this issue is held at the gate via svd:hold):

  • add label svd:go — approve; Half-B dispatch accepts exactly this spec (open risks accepted)
  • add label svd:fire beside svd:go — approve AND authorise the Half-B fire from the phone; never on its own
  • add label svd:skip — park it; dispatch refuses it and the issue stays for a human
  • add label svd:respec — discard this spec; the next Half-A run re-produces it with this record as prior-attempt context

Spec: /Users/piers/.local/state/exhaust/groupthink/specs/2026-11-01-embed-attachment-text-and-rank-it-in-semantic-search.md — next: codex-svd dispatch --args '<same-json>' --approved-spec-path /Users/piers/.local/state/exhaust/groupthink/specs/2026-11-01-embed-attachment-text-and-rank-it-in-semantic-search.md

### ⏸ SVD architect gate: dev/imap-mailmind-mcp#79 embed-attachment-text-and-rank-it-in-semantic-search **Status:** CLEAN after 1 rework round(s) · 0 blocking defect(s) · 0 lens(es) INCONCLUSIVE (could not run) · 0 lens(es) DEGRADED (deterministic denial) · complexity `substrate` · dispatch effort `high` **Run:** `20261101T084957Z-7f9da49a` **Held because:** CLEAN but 4 open risk(s) (2 verification · 2 decision) — HOLD-ALL parks everything. The risks, verbatim: **Open risks:** 1. Chunk length vs the model's 512-token window: ADR 0007 §8 says 512-token chunks embedded with the file name in front, but the model truncates at 512 including CLS/SEP (embed.py:69), so a full chunk loses its last ~3-20 tokens. Lean: keep CHUNK_TOKENS = 512, as the ADR says. Question: keep 512, or cut at 512 - 2 - len(filename tokens), which may need an ADR note? [decision] 2. Upper bound of embed_chunks: the ADR fixes only the default of 4. Lean: le=16 (about 6,000 words per attachment; keeps the ADR 0005 §8 200k-vector budget meaningful). Question: is 16 right, or should it match per_message (le=50)? [decision] 3. The model test and make check were not run at spec time (the sandbox denies .venv/bin/python, and system python has no numpy). Only a pure-Python model of best_k's selection was checked: 20,000 random trials matched brute force. In the dispatch worktree, run `make model && uv run python3 -m pytest -q tests/test_semantic.py -k attachment_only` and confirm it passes (not skipped), then run `make check` against the re-measured base failure set. [verification] 4. Backfill cost on the live instance (ADR 0007 Consequences: 1-2 core-hours per 5,000 attachments). After deploying, check that mail_status attachment_chunks=N rises while embedded= keeps pace with new mail, and that the sync container stays within its memory limit (deploy/k8s/base/deployment.yaml:115). [verification] **Change:** The spec adds `[attachments] embed_chunks` (default 4, ge=1, le=16) to `AttachmentsConfig` (config.py:203-219). Schema v8 creates the `attachment_embeddings` table and its delete trigger in the shape of `embeddings` (store.py:209-218). Three new methods mirror `unembedded`/`copy_embeddings`/`set_embeddings` (store.py:821-857). `_copy_attachment_text` (store.py:1834-1844) also copies vectors. `attachment_vector_chunks` shares a WHERE helper with `vector_chunks` (store.py:1383-1412). In embed.py, a `split_tokens` helper, a second tokenizer with no truncation, and `Embedder.chunks` do the chunking, and `EmbedWorker._account` (embed.py:133-154) embeds new messages before attachment chunks. semantic.py gets `best_k`. It does NOT keep the `argsort(-scores)[: k + len(exclude)]` prefilter (semantic.py:32). Each page is walked in `np.lexsort((ids, -scores))` order, keeping each id's first (best) vector and skipping excluded ids, until k distinct ids are taken. Those are then merged into a min-heap of (score, -id) as at semantic.py:27,36-40, with a `members` dict holding scores and sources. The result is the exact top k by per-id maximum, even when one id repeats up to 800 times in a page. `top_k` becomes a wrapper. server.py `_ranked` (270-302) chains the chunk streams when `[attachments]` is set; `_chunk_snippet` adds the `[attachment N]` prefix, and `mail_status` gains `attachment_chunks=`. README, config.example.toml and CLAUDE.md doc rows are updated. **Files:** `src/imap_mailmind_mcp/config.py` · `src/imap_mailmind_mcp/store.py` · `src/imap_mailmind_mcp/embed.py` · `src/imap_mailmind_mcp/semantic.py` · `src/imap_mailmind_mcp/server.py` · `README.md` · `config.example.toml` · `CLAUDE.md` · `tests/test_embed.py` · `tests/test_semantic.py` · `tests/test_store.py` · `tests/test_attachments.py` · `tests/test_attachment_vectors.py` **Decision:** accept the 4 open risk(s) above and dispatch this spec (`svd:go`), or park (`svd:skip`) / re-spec (`svd:respec`)? **Your call** (when this issue is held at the gate via `svd:hold`): - add label `svd:go` — approve; Half-B dispatch accepts exactly this spec (open risks accepted) - add label `svd:fire` **beside** `svd:go` — approve AND authorise the Half-B fire from the phone; never on its own - add label `svd:skip` — park it; dispatch refuses it and the issue stays for a human - add label `svd:respec` — discard this spec; the next Half-A run re-produces it with this record as prior-attempt context <sub>Spec: `/Users/piers/.local/state/exhaust/groupthink/specs/2026-11-01-embed-attachment-text-and-rank-it-in-semantic-search.md` — next: `codex-svd dispatch --args '<same-json>' --approved-spec-path /Users/piers/.local/state/exhaust/groupthink/specs/2026-11-01-embed-attachment-text-and-rank-it-in-semantic-search.md`</sub>
Collaborator

spec-verify-dispatch Half B for dev/imap-mailmind-mcp#79 embed-attachment-text-and-rank-it-in-semantic-search completed integration verify.

  • PR: dev/imap-mailmind-mcp#99
  • Branch: feat/svd-dev-imap-mailmind-mcp-79-embed-attachment-text-and-r-ff75e4d3
  • Commit: 5a82ba837f
  • Build: none
  • Test: green
  • Reader audit clean: True
  • Spec fidelity: True
  • Ready for merge: True
  • Checks: No remote checks: Forgejo statuses on PR #99 head 5a82ba8 are empty and the repo has no CI config. Local: 386 unit tests passed, model test passed (4, none skipped), ruff clean.
  • Warning: No remote CI exists for this repo: the Forgejo commit statuses on 5a82ba8 are empty and the repo has no .woodpecker directory (CI files are added by the architect).
  • Warning: The independent run did not repeat make check. It ran the full unit suite, ruff and the model test. The subagent reported make check green with a temporary UV_TOOL_DIR.
  • Warning: Open Risk 4 and ruling R5: the architect checks attachment_chunks= in mail_status and the sync container memory after deploy.
  • Decision: review the PR and merge? (codex-svd ship --run-id 20261101T093929Z-ff75e4d3 --confirm-merge)
spec-verify-dispatch Half B for dev/imap-mailmind-mcp#79 embed-attachment-text-and-rank-it-in-semantic-search completed integration verify. - PR: https://git.groupthink.asia/dev/imap-mailmind-mcp/pulls/99 - Branch: feat/svd-dev-imap-mailmind-mcp-79-embed-attachment-text-and-r-ff75e4d3 - Commit: 5a82ba837fb304c597bcf40b659ab91e864c0016 - Build: none - Test: green - Reader audit clean: True - Spec fidelity: True - Ready for merge: True - Checks: No remote checks: Forgejo statuses on PR #99 head 5a82ba8 are empty and the repo has no CI config. Local: 386 unit tests passed, model test passed (4, none skipped), ruff clean. - Warning: No remote CI exists for this repo: the Forgejo commit statuses on 5a82ba8 are empty and the repo has no .woodpecker directory (CI files are added by the architect). - Warning: The independent run did not repeat make check. It ran the full unit suite, ruff and the model test. The subagent reported make check green with a temporary UV_TOOL_DIR. - Warning: Open Risk 4 and ruling R5: the architect checks attachment_chunks= in mail_status and the sync container memory after deploy. - Decision: review the PR and merge? (`codex-svd ship --run-id 20261101T093929Z-ff75e4d3 --confirm-merge`)
Sign in to join this conversation.
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
dev/mail-mind-mcp#79
No description provided.