Repository navigation
feat(mail): sorting rules by mail, list, address and domain (store version 77) - #3366
Merged
Merged
Conversation
…rsion 77) One rules table over mail, applied in a fixed order before the header classifier of store version 76: one mail; a List-Id or a subject pattern; an address; a registrable domain (Public Suffix List). Within a scope the owner's rule beats the program's and the newest wins; a domain rule may beat the address rules (overAddresses). Addresses match whole and domains on a label boundary, so ann@ never matches joann@. A rule sets the mail type, moves the sender to a bot or a person through moveToBot and moveToPerson (for its whole history, owner only), or files the sender under an organization through holdAddress. Each correction names its scope and applyTo (all, or only mail dated from the rule on). An email a rule typed carries typeSignal rule:<id>. Free-mail and throwaway domains (FREE_MAIL, the data pack's free-mail lists, the disposable list): a domain rule there is the owner's only and is set only with the confirm of a dry run whose counts still hold. No call widens a correction to a domain on its own; scopes() lists the domain last and flags free mail. store.mail.sorting (experimental): rules, scopes, add (dryRun, confirm), remove, undo (newest first, walks back) and reset (a sender's address and one-mail rules). Removing a rule types its mail again and puts its senders back from mail_sort_effects. Store version 77 (reserved in #3360): mail_sort_rules, mail_sort_effects, emails.sort_checked with a partial index and a trigger that nulls it with mail_type, so a type a build before 77 kept is worked out again once a rule exists. Added only, forward-only, minCompatible stays 73. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Moving 500 senders in one write held the lock 4-6 s, past the 5 s another writer waits. The rule and its type clears stay one write; the moves follow in writes of 100, and a sender already settled is skipped. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
leemour
added a commit
to WireCatLabs/cli-testing
that referenced
this pull request
Nov 25, 2026
…ion 77) (#412) ## Why The owner corrects mail sorting by scope (one mail, a list, an address, a domain), for past mail too or only for new mail. cli-core store version 77 checks those rules in the lazy type pass of version 76. This page answers what the check adds to the first typed read, and how long a domain rule takes to apply to past mail: on a company domain, on `gmail.com`, and when it moves 500 senders to bots. ## What `performance/mail/rules/README.md` and `bench.mjs`. Synthetic data only: 100,000 emails from 20,000 senders. Linked from `performance/mail/README.md`. Headline: 50 rules add 0.1–0.35 s per 100,000 emails typed in memory. A `gmail.com` rule over 30,000 emails applies in about 1 s, plus 1.5–2.8 s on the next typed read. Moving 500 senders took 4–6 s in one write, past another writer's 5 s wait, so the code now moves 100 per write. Code: cli-core PR [#3366](WireCatLabs/cli-core#3366). 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
leemour
added a commit
that referenced
this pull request
Nov 25, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Task
Contact-kinds plan (#3323, section "Mail categories"; research note
2026-11-25-contact-kinds-mail-products.md, IDEA-3 and IDEA-4, the SaneBox and Inbox Zero notes). Owner's rulings, 2026-11-25: domain rules on free-mail domains are allowed but never made by accident, the domain scope is not offered first, preview before apply, every correction names its scope and whether it covers past mail, corrections can be undone and a sender's manual sorting reset.Why
#3358 gives each email a type from its headers. The owner needs to correct it: "this mail is a receipt", "everything from this address is a newsletter", "this whole domain is a bot". A correction must say how far it reaches, and must never sort strangers on
gmail.comtogether by accident.Not
mail_rules. That table decides how one account's sync stores mail (skip, envelope, tags) before an email exists, matches domains byendsWith, and has no levels or authors. Sorting rules are cross-account, decide what stored mail is, and need a fixed order. So they get their own table and API (store.mail.sorting, beside the storagestore.mail.rules).What
Order (
src/mail/sort-rules.ts, pure): the first scope that has a rule wins.maillistorsubjectlistIdOfkeeps it), or a subject pattern (any case; any number matches any number)addressann@never matchesjoann@domainshop.examplematchesmail.shop.example, nevermyshop.exampleoverAddressesbeats the address rules at its domain (SaneBox's "override training").applyTo: "new"is a cutoff on the rule, checked against the mail's own date, so a backfill of old mail counts as past and a later trigger clear never retypes past mail with it.Actions. A rule sets any of:
sender: "bot" | "person", throughmoveToBot/moveToPersononly, for the sender's whole history. Owner only, address or domain scope only. An address rule that moves a sender refusesapplyTo: "new". A domain rule withapplyTo: "new"logs the domain's known senders as left, and moves only senders first seen later. Mail sync settles the senders of each saved batch when such a rule exists;organization: "organization:<id>", throughholdAddress(methodsort-rule), owner only.What a rule did to each sender is logged in
mail_sort_effects(beforeandafter). A removal puts each sender back, then lets the rules still live settle it.Free-mail guard.
isSharedMailDomain=FREE_MAILor the data pack'sisFreeMailDomain/isPackFreeMailDomain(#3359) orisDisposableDomain.mail-sort.free-mail-program).confirmof a dry run. The confirm is a hash of the rule and its counts, so a dry run gone stale (new mail arrived) is refused (mail-sort.confirm).scopes(email)lists mail, list, address, domain in that order, the domain last and flaggedfreeMail.API (experimental),
store.mail.sorting:add(input, by, { dryRun?, confirm? })→{ applied, preview, rule? }.preview={ scope, value, label, applyTo, freeMail, addresses, emails, retyped, senders, confirm }.rules({ scope?, removed? }),scopes(emailId),remove(id, by)(a program removes only its own).undo(by): the owner's newest add or removal not yet undone. Repeated undo walks back; it does not toggle.reset(sender, by): removes one sender's address and one-mail rules (an address,person:orsender-bot:). Domain and list rules stay. A directpeople.moveToBotthe owner made outside rules is not reset by it.typeSignal: "rule:<id>".Storage, store version 77 (reserved in #3360):
mail_sort_rules,mail_sort_effects(an identity's rows dropped with it by a trigger),emails.sort_checkedwith the partial indexemails_sort_to_check, and a trigger that nulls it withmail_type.minCompatiblestays 73.Also:
listIdOfexported frommail/headers.ts(reused, not copied); 16mail-sort.*reasons; GLOSSARY "sorting rule"; biome line limits raised as v72–v76 did (spec.mjs1902→1908,schema.ts3265→3269, theemails.tsgroup 1024→1033).Timings
100,000 synthetic emails from 20,000 senders, load 6–17 ([cli-testing page and script](WireCatLabs/cli-testing#412,
performance/mail/rules)):gmail.comrule (6,000 addresses, 30,000 emails): dry run 0.14–0.19 s, add 1.0–1.2 s, next typed read 1.5–2.1 s. Undo costs the same.moveToBot), past another writer's 5 s wait. So the rule and its type clears are one write, and the senders then move 100 per write (about 1 s of lock each; 5.6 s in total). Moved back in 0.4–0.9 s.Testing
src/store/mail-sorting.test.ts(10, through the store),src/mail/sort-rules.test.ts(6: order, authors, override, anchoring, cutoff, values),src/store/version-77.test.ts(upgrade from 76).pnpm typecheck,biome check(no errors),schema:check, the docs checks,cli-dev api-report --check(stable API unchanged), full vitest suite before the chunking change: 996 files, 13,773 tests; after it, the sorting tests (17).Not in this PR
moveToPerson/moveToBot, which logauthor = 'owner'. So a sender a removed rule moved back counts as one the owner kept, and the kind pass will not make it a bot by its own rules again.sort_checked); senders it saves are settled only on the next save by a build that speaks 77.mail.shop.exampleis kept asshop.example, as the plan says. The preview'svalueandlabelshow it before apply.resetdoes not release a directpeople.moveToBot/moveToPerson. Those keep the owner's mark.🤖 Generated with Claude Code