Extract attachment text in the sync worker, in a limited child process #73

Closed
opened 2026-11-01 00:54:12 +00:00 by svc-agent-writer · 0 comments

Parent: #9 (Phase 4). Decision: ADR 0007.

Extract the text of each in-scope attachment in the sync worker, store it in the mirror, and index it in FTS5. The bytes never reach the disk.

Scope

  • AttachmentsConfig in config.py: an optional [attachments] section with max_bytes, per_message, max_chars and per_pass (ADR 0007 §3, §5). Absent means nothing is fetched.
  • src/imap_mailmind_mcp/extract.py (new):
    • the child entry point python -I -m imap_mailmind_mcp.extract <kind>, which reads bytes on stdin and writes text on stdout;
    • the parent helper that runs the child with RLIMIT_CPU 10 s (pypdf and stdlib) or 30 s (MarkItDown), RLIMIT_AS 512 MiB or 1 GiB, a wall timeout of twice the CPU limit, an environment of PATH only, and stderr discarded;
    • the extractors of ADR 0007 §2: pypdf for PDF; MarkItDown with only DocxConverter, PptxConverter, OutlookMsgConverter and HtmlConverter registered, built-ins and plugins off; a stdlib reader for XLSX that writes Markdown table rows; charset decoding for the text types;
    • the zip-member check of ADR 0007 §3 before any Office parser;
    • socket.socket disabled in the child before any parser import.
  • Pin pypdf to one minor release, and markitdown[docx,pptx,outlook] to one release, in pyproject.toml and uv.lock. Measure the amd64 image growth and the child address space, and set the RLIMIT_AS values from that measurement.
  • A new schema version in store.py: attachment_text(message_id, part, status, text) and one more FTS5 column for the joined attachment text. A deleted message loses its rows in the same transaction.
  • The byte sources of ADR 0007 §5, in order: the raw message just downloaded for the bucket; the bucket by blob_key for the backlog; then the IMAP section fetch or the JMAP blob download. The raw-bytes fallback uses the bytes it holds. A raw-message extraction matches each MIME attachment to the stored list by position, name, type and size.
  • The backlog runs after each sync pass, newest first, at most per_pass attachments, and the worker shortens its wait while a backlog remains.
  • A moved IMAP message copies the rows from the row with the same Message-ID and the same attachment list.
  • mail_status shows the extracted count and the backlog for each account.
  • Logs carry counts, status codes and type classes only, never a filename or text.

Acceptance

  • Unit tests:
    • each extractor on a fixture;
    • the child limits: a CPU loop gives timeout, and a zip bomb is refused from its declared size;
    • a garbage PDF gives failed and the parent goes on;
    • each bound gives its status: too_large, skipped, unsupported;
    • the MarkItDown allow-list refuses a .zip and a .pdf;
    • a connection attempt in the child fails;
    • with [blobs] set, new mail is extracted with no extra fetch, and the backlog reads the bucket;
    • a raw-message part that does not match the stored list gives failed;
    • the absent section fetches nothing;
    • the FTS index matches a word that is only in an attachment;
    • a captured-log test shows no filename and no text.
  • An integration test on GreenMail and one on Stalwart: a message with a PDF attachment and one with a DOCX attachment are each matched by mail_search on a word from the attachment only.
  • README, config.example.toml, SECURITY.md and CLAUDE.md name the section, the formats and the limits.
  • make check and make test-integration pass.

Blocked by

Human act

ruling: the operator approves parsing hostile attachments in a child process of the sync container (CONV-40)

Filed under: operator word "resume phase 4 development." sha256:b5be33c704e1

Parent: #9 (Phase 4). Decision: ADR 0007. Extract the text of each in-scope attachment in the sync worker, store it in the mirror, and index it in FTS5. The bytes never reach the disk. ## Scope - `AttachmentsConfig` in `config.py`: an optional `[attachments]` section with `max_bytes`, `per_message`, `max_chars` and `per_pass` (ADR 0007 §3, §5). Absent means nothing is fetched. - `src/imap_mailmind_mcp/extract.py` (new): - the child entry point `python -I -m imap_mailmind_mcp.extract <kind>`, which reads bytes on stdin and writes text on stdout; - the parent helper that runs the child with `RLIMIT_CPU` 10 s (pypdf and stdlib) or 30 s (MarkItDown), `RLIMIT_AS` 512 MiB or 1 GiB, a wall timeout of twice the CPU limit, an environment of `PATH` only, and stderr discarded; - the extractors of ADR 0007 §2: pypdf for PDF; MarkItDown with only `DocxConverter`, `PptxConverter`, `OutlookMsgConverter` and `HtmlConverter` registered, built-ins and plugins off; a stdlib reader for XLSX that writes Markdown table rows; charset decoding for the text types; - the zip-member check of ADR 0007 §3 before any Office parser; - `socket.socket` disabled in the child before any parser import. - Pin pypdf to one minor release, and `markitdown[docx,pptx,outlook]` to one release, in `pyproject.toml` and `uv.lock`. Measure the amd64 image growth and the child address space, and set the `RLIMIT_AS` values from that measurement. - A new schema version in `store.py`: `attachment_text(message_id, part, status, text)` and one more FTS5 column for the joined attachment text. A deleted message loses its rows in the same transaction. - The byte sources of ADR 0007 §5, in order: the raw message just downloaded for the bucket; the bucket by `blob_key` for the backlog; then the IMAP section fetch or the JMAP blob download. The raw-bytes fallback uses the bytes it holds. A raw-message extraction matches each MIME attachment to the stored list by position, name, type and size. - The backlog runs after each sync pass, newest first, at most `per_pass` attachments, and the worker shortens its wait while a backlog remains. - A moved IMAP message copies the rows from the row with the same Message-ID and the same attachment list. - `mail_status` shows the extracted count and the backlog for each account. - Logs carry counts, status codes and type classes only, never a filename or text. ## Acceptance - Unit tests: - each extractor on a fixture; - the child limits: a CPU loop gives `timeout`, and a zip bomb is refused from its declared size; - a garbage PDF gives `failed` and the parent goes on; - each bound gives its status: `too_large`, `skipped`, `unsupported`; - the MarkItDown allow-list refuses a `.zip` and a `.pdf`; - a connection attempt in the child fails; - with `[blobs]` set, new mail is extracted with no extra fetch, and the backlog reads the bucket; - a raw-message part that does not match the stored list gives `failed`; - the absent section fetches nothing; - the FTS index matches a word that is only in an attachment; - a captured-log test shows no filename and no text. - An integration test on GreenMail and one on Stalwart: a message with a PDF attachment and one with a DOCX attachment are each matched by `mail_search` on a word from the attachment only. - README, `config.example.toml`, SECURITY.md and CLAUDE.md name the section, the formats and the limits. - `make check` and `make test-integration` pass. ## Blocked by - #72 ## Human act ruling: the operator approves parsing hostile attachments in a child process of the sync container (CONV-40) Filed under: operator word "resume phase 4 development." sha256:b5be33c704e1
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
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#73
No description provided.