Embed every mirrored message locally with a pinned model #46

Merged
svc-agent-writer merged 1 commit from feat/embedder into main 2026-10-31 13:27:56 +00:00

Closes #43. Part of #9. Decision: ADR 0005 (#45).

Summary

  • Model in the image. scripts/fetch_model.py downloads bge-small-en-v1.5 int8 ONNX and its tokenizer from Xenova/bge-small-en-v1.5 at the pinned revision.
    • It checks both sha256 values from ADR 0005 §2 and fails on a mismatch.
    • The builder stage runs it, and the runtime stage copies /opt/mailmind/model owned by root.
    • make model fetches the same files into local/model for the tests.
  • [embed] config: model_dir, batch, pause_seconds, poll_seconds. With the section absent, nothing loads the model.
  • Schema v4. An embeddings(message_id, model, vec) table holds one L2-normalised float32 vector per message, 1.5 KB each. An AFTER DELETE trigger on messages drops the vector in the same transaction, whichever path deletes the row.
  • embed.py.
    • OnnxEmbedder: one ONNX thread, truncation at 512 tokens, CLS output, one message per run.
    • EmbedWorker, a thread in sync next to the assessor. Each round it copies vectors by Message-ID, then embeds the newest rows that have no vector from the current model. A stop takes effect between rounds, and the next tick resumes.
    • A model error leaves the rows for the next tick.
  • Reporting.
    • mail_status shows embed: off or embed: model=…, and embedded=N per account.
    • mailmind check loads the model and embeds one string.
    • sync --once runs one embed tick.
    • Logs carry counts, the model name and seconds.
  • Docs: README, SECURITY.md (a local-embeddings row), CLAUDE.md, and config.example.toml.

The search side (mail_search modes, api memory limits) is #44.

Image

The amd64 image grows from 264 MB to 477 MB:

  • onnxruntime: 67 MB
  • numpy with its bundled libraries: 70 MB
  • the model: 34 MB
  • tokenizers: 12 MB
  • huggingface_hub and hf_xet: about 15 MB. They are dependencies of tokenizers that the service never calls.

ADR 0005 estimated 145 MB. I'll correct the figure in #45.

Inside the amd64 image, the model embeds three test strings. The invoice query scores 0.689 against the payment text and 0.412 against the unrelated text.

Test plan

  • make check: 122 passed, mypy strict clean, pip-audit clean.

  • With make model, three tests run against the real model:

    • a meaning-ranking check;
    • from_config loads the model;
    • check embeds one string.

    Without the model, those three skip.

  • Fake-model tests cover:

    • the backfill, with no message text in the logs;
    • the copy by Message-ID, with no model run;
    • resume after a stop;
    • re-embedding on a model change;
    • a deleted row or a dropped mailbox losing its vectors;
    • a deleted-meanwhile row being skipped;
    • a failing model leaving the rows.
  • The store upgrade test now starts from a file without the v4 table and trigger.

🤖 Generated with Claude Code

Closes #43. Part of #9. Decision: ADR 0005 (#45). ## Summary - **Model in the image.** `scripts/fetch_model.py` downloads `bge-small-en-v1.5` int8 ONNX and its tokenizer from `Xenova/bge-small-en-v1.5` at the pinned revision. - It checks both sha256 values from ADR 0005 §2 and fails on a mismatch. - The builder stage runs it, and the runtime stage copies `/opt/mailmind/model` owned by root. - `make model` fetches the same files into `local/model` for the tests. - **`[embed]` config:** `model_dir`, `batch`, `pause_seconds`, `poll_seconds`. With the section absent, nothing loads the model. - **Schema v4.** An `embeddings(message_id, model, vec)` table holds one L2-normalised float32 vector per message, 1.5 KB each. An `AFTER DELETE` trigger on `messages` drops the vector in the same transaction, whichever path deletes the row. - **`embed.py`.** - `OnnxEmbedder`: one ONNX thread, truncation at 512 tokens, CLS output, one message per run. - `EmbedWorker`, a thread in `sync` next to the assessor. Each round it copies vectors by Message-ID, then embeds the newest rows that have no vector from the current model. A stop takes effect between rounds, and the next tick resumes. - A model error leaves the rows for the next tick. - **Reporting.** - `mail_status` shows `embed: off` or `embed: model=…`, and `embedded=N` per account. - `mailmind check` loads the model and embeds one string. - `sync --once` runs one embed tick. - Logs carry counts, the model name and seconds. - **Docs:** README, SECURITY.md (a local-embeddings row), CLAUDE.md, and `config.example.toml`. The search side (`mail_search` modes, api memory limits) is #44. ## Image The amd64 image grows from 264 MB to 477 MB: - onnxruntime: 67 MB - numpy with its bundled libraries: 70 MB - the model: 34 MB - tokenizers: 12 MB - `huggingface_hub` and `hf_xet`: about 15 MB. They are dependencies of `tokenizers` that the service never calls. ADR 0005 estimated 145 MB. I'll correct the figure in #45. Inside the amd64 image, the model embeds three test strings. The invoice query scores 0.689 against the payment text and 0.412 against the unrelated text. ## Test plan - `make check`: 122 passed, mypy strict clean, pip-audit clean. - With `make model`, three tests run against the real model: - a meaning-ranking check; - `from_config` loads the model; - `check` embeds one string. Without the model, those three skip. - Fake-model tests cover: - the backfill, with no message text in the logs; - the copy by Message-ID, with no model run; - resume after a stop; - re-embedding on a model change; - a deleted row or a dropped mailbox losing its vectors; - a deleted-meanwhile row being skipped; - a failing model leaving the rows. - The store upgrade test now starts from a file without the v4 table and trigger. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
An embedder thread in the sync process stores one bge-small-en-v1.5 int8 vector per message (ADR 0005). It copies vectors across moved copies, resumes a backfill, re-embeds on a model change, and uses one core. The image carries the model, fetched and hash-checked at build.

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