Skip to content

perf(mail): what typing every email costs cli-core's store (store version 76) - #411

Merged
leemour merged 2 commits into
mainfrom
perf/mail-types
Nov 25, 2026
Merged

leemour merged 2 commits into
mainfrom
perf/mail-types

Conversation

@leemour

@leemour leemour commented Nov 25, 2026

Copy link
Copy Markdown
Member

Task

Measure cli-core store version 76 (cli-core#3358): every email typed lazily on the first read that filters by type.

What

performance/mail/types/ with the script and its results on 100,000 synthetic emails, linked from performance/mail/README.md.

Testing

markdownlint, cspell and Biome on the new files. Two runs plus one with the earlier batch size.

🤖 Generated with Claude Code

leemour and others added 2 commits November 25, 2026 21:23
…sion 76)

100,000 synthetic emails: the first filtered read types them all in
1.7-11.6 s, one batch of 5,000 holds the write lock 0.1-0.6 s, warm
filtered pages stay under 25 ms. The cost is the commit's sync to disk.
cli-core#3358.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
leemour added a commit to WireCatLabs/cli-core that referenced this pull request Nov 25, 2026
…#3358)

## Task

Contact-kinds plan (#3323, section "Mail categories"; research notes
`2026-11-25-contact-kinds-mail-products.md` §2.1, IDEA-1, IDEA-2, IDEA-7
and the taxonomy in `2026-11-25-contact-kinds-domain-sources.md`).
Owner's rulings, 2026-11-25: every mail gets its own type, apart from
its sender; a tab is a filtered view; `reach` is per mail; a sender's
view is a summary of its mails.

## Why

A sender's single kind puts a shop's receipts and its sales in the same
place. With a type per email, Subscriptions can list any sender with
broadcast mail, and Bots any sender with receipts, from the same store.

## What

**Classifier** (`src/mail/mail-types.ts`, pure, no network). One type
per email, first match wins:

| # | Type | Signal (`typeSignal`) |
|---|---|---|
| 1 | `auto_reply` | a `message/delivery-status` or
`message/disposition-notification` part; `mailer-daemon@`/`postmaster@`;
an out-of-office or undeliverable subject on mail with no list headers |
| 2 | `security` | `security@`, `verify@`, `2fa@`… or a
sign-in/code/password-reset subject (EN/RU/ES); not when Gmail stamped
DKIM or DMARC `fail` |
| 3 | `calendar` | a `text/calendar` or `application/ics` part |
| 4 | `travel` | schema.org `*Reservation` (flight, train, bus, lodging,
car, taxi, boat); a boarding-pass/itinerary subject; sector `airline` on
mail with no list headers |
| 5 | `event_ticket` | schema.org `EventReservation`; a tickets subject
|
| 6 | `order` | schema.org `Order`; `orders@`/`receipts@`; an order or
receipt subject (not when it is also a shipping subject) |
| 7 | `shipping` | schema.org `ParcelDelivery`; `shipping@`/`tracking@`;
a shipped/on-its-way subject; sector `delivery` |
| 8 | `bill` | schema.org `Invoice`; `billing@`/`invoices@`; an
invoice/statement/payment subject |
| 9 | `social_notification` | sector `social` (the sender's
organization) |
| 10 | `discussion_list` | a List-Id three or more addresses send to
(`DISCUSSION_SENDERS`); `Precedence: list` with no List-Id |
| — | `account_notice` | Zero's exception: List-Unsubscribe with no
one-click or Feedback-ID, from an automated address |
| 11 | `newsletter` | List-Id; `news@`/`digest@`; a newsletter subject;
sector `news`; or any list mail from a person, not a bot |
| 12 | `promotion` | a sale/discount subject;
`promo@`/`deals@`/`info@`…; otherwise one-click unsubscribe, Feedback-ID
or `Precedence: bulk` |
| 13 | `system_alert` | `alerts@`/`monitoring@`/`ci@`/`cron@`…; a
`[FIRING]`/build-failed subject |
| 14 | `account_notice` | a signed Auto-Submitted; a no-reply address;
an automated address (Zero's list) |
| 15 | `personal` | the owner wrote to the sender, or a sender that is
no bot and no null return path |
| 16 | `other` | a bot's mail no rule names |

- **Order differs from the plan in two places** (for the owner to
confirm): `system_alert` runs before the generic `account_notice`
signals, since an alert carries Auto-Submitted too and would never be
reached; and Zero's "automated sender with plain List-Unsubscribe" is an
`account_notice` before the list types, which keeps receipts out of
promotions.
- **Zero's gate (rule 1)**: mail from someone the owner wrote to (and
who is no bot) is never typed by an address word, a subject or a sector,
only by what the mail carries (parts, markup, list headers). Subject and
address words are also never used on List-Id mail.
- **Header trust as today**: Auto-Submitted, one-click unsubscribe and
Feedback-ID are read only as `mailHeadersOf` keeps them, under the From
domain's DKIM. List-Id, List-Unsubscribe and Precedence are kept
unsigned today and read as such; they only push mail towards bulk types.
- **Reach per mail** is `reachOfOne` from `sender-signals.ts`, now
exported, the same rule the people list uses.
- Ported from Zero (MIT, commit 4b642dfd): the check order and the
AUTOMATED_SENDER / MARKETING_SENDER patterns. The notice is at the top
of the file and in `THIRD_PARTY_NOTICES`. No Inbox Zero or Mailspring
code.

**Storage (store version 76, reserved in #3357).** `emails.mail_type`,
`reach`, `type_signal`; indexes `emails_by_mail_type` and
`emails_type_to_work_out` (`id WHERE mail_type IS NULL`). Columns,
indexes and 14 triggers added only, forward-only, `minCompatible` stays
73. Triggers null an email's type when what decides it changes:
- its own From, subject, headers, metadata, outgoing flag or HTML body;
its recipients; a calendar or report attachment;
- a new sender on its List-Id (the discussion threshold);
- the owner's first mail to its sender (an outgoing insert, or
`markOwnMail` flipping `outgoing`);
- its sender joining or leaving `sender_bot_identities` or
`organization_identities`, or that organization's sector changing.
A read that filters by `types`/`reaches`, and `mailSenders`, first fill
what is missing, 5,000 per write. A query-only open fills nothing and
works the missing ones out in memory, so a filtered list is never short.
Unfiltered reads type only their page's untyped rows, in memory. Plain
reads and syncs never fill.

**API (experimental).**
- `Email.mailType`, `Email.reach`, `Email.typeSignal`.
- `store.mail.emails({ types, reaches, … })`.
- `store.mail.mailSenders({ types?, reaches?, accountId?, since?,
until?, limit?, offset? })` → `{ items, total }`. Each item is `{ ref
(person:<id> | sender-bot:<id> | null), addresses, name, emails, types,
reaches, lastAt }`. A sender is listed when any of its mail passes the
filter, and its counts cover all its mail.
- No memo command: that comes after a release.

**Glossary**: "mail type", "reach" and a "Mail types" table in
`docs/dev/GLOSSARY.md`.

**Signals not in the store today**: what mail sync would need to keep.
mail-cli is not changed.
- **schema.org markup.** Sync keeps no HTML
(`src/mail/email-input.ts:29` stores `bodyText` only). `htmlToText`
drops `<script>` (JSON-LD) and every tag attribute (microdata)
(`src/mail/parse.ts:39,65`). Sync would need to keep the `@type` values
`schemaTypesOf` reads, for example as `headers."schema-types"`, or keep
`body_html`. The classifier already reads `body_html` when an importer
keeps it (tested).
- **`Auto-Submitted: auto-replied`** is not kept: `programSignalsOf`
keeps only generated values (`src/mail/headers.ts:111`). Auto-replies
are found by their subject only. Sync would need an `auto-replied` key
under the same DKIM rule.
- **`X-Auto-Response-Suppress`, `X-Campaign`** (Zero reads both),
**`List-Post`** (a discussion list's own marker), the **Feedback-ID
value** (only its presence is kept) and the calendar part's `METHOD` (an
invitation or a cancellation).
- **Envelope mail** (kept by a rule, `metadata.rule`) has no recipients
and no body: its reach is `direct` and its type comes from headers and
the subject only.
- Verified as stored: `text/calendar` and `message/delivery-status`
parts keep their media type in `attachments.mime`
(`src/store/mail-types.test.ts`).

**Line limits** raised as v72 and v74 did: `spec.mjs` 1893→1902,
`schema.ts` 3260→3265, the `emails.ts` group 1012→1024. `rollBackTo` in
`src/testing/older-store.ts` now undoes versions above 73 before version
73's table copies: those copies re-create every index, including one on
a later column.

## Testing

- New: `src/store/mail-types.test.ts` (7, through `syncMail`),
`src/mail/mail-types.test.ts` (32), `src/store/version-76.test.ts`
(4,500 emails typed in batches after the upgrade from 75, a 1.0 build's
write typed again, the index used).
- `pnpm typecheck`, `pnpm lint` (no errors), `schema:check`,
`docs:check`, `api-report --check`. The full vitest suite before the
rebase passed: 991 files, 13,717 tests. After the rebase onto #3356:
`src/store` and `src/mail`, 309 files, 2,481 tests.
- **Timings** on 100,000 synthetic emails (load 14–20, other agents
writing to the disk): the first filtered read types all of them in
1.7–11.6 s. One batch of 5,000 holds the write lock 91–628 ms. Warm
filtered pages take 10–21 ms, `mailSenders` 190–280 ms, and a query-only
read of an untyped store 0.7–1.8 s. The cost is the commit's sync to
disk, not the classification (about 15 ms per 2,000). Page and script:
[cli-testing#411](WireCatLabs/cli-testing#411),
`performance/mail/types`.

Not in this PR: rules by address and domain, and scoped corrections (the
plan's next step); memo commands; the Desktop tabs.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
@leemour
leemour merged commit e01e730 into main Nov 25, 2026
1 check passed
@leemour
leemour deleted the perf/mail-types branch November 25, 2026 20:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant