Skip to content

fix(mail): owner's sorting rules win across levels; removal puts senders back with their author, in batches - #3369

Open
leemour wants to merge 3 commits into
mainfrom
fix/mail-rules-owner
Open

leemour wants to merge 3 commits into
mainfrom
fix/mail-rules-owner

Conversation

@leemour

@leemour leemour commented Nov 25, 2026

Copy link
Copy Markdown
Member

Task

Three gaps #3366 (mail sorting rules, store version 77) left open. The contact-kinds plan (#3323, "Moving between entities" and "Mail categories") says the owner's choice always wins, and names this follow-up: "the owner's rules win across levels, and a removed rule puts senders back with their earlier author."

Why

  1. Owner-over-program held only within one level, so a program's one-mail rule beat the owner's domain rule.
  2. Removing or undoing a sender rule moved senders back through moveToPerson / moveToBot, which log the owner as author. The kind pass then treated them as the owner's choice and never sorted them again. Measured: all 500 senders of an undone rule kept the owner's mark.
  3. Removal and undo put senders back in one write. The add already moves 100 per write.

What

Order (src/mail/sort-rules.ts): SortIndex is split by author before the level walk. sortRuleFor checks every owner's rule in level order, then every program's, then the header classifier. Within one author: the first level that has a rule wins, then the newest. overAddresses beats the same author's address rules. A program's rule on mail.shop.example no longer hides the owner's rule on shop.example.

Put back with the earlier author (src/store/sqlite/sender-bots.ts, mail-sorting.ts):

  • An effect's before now keeps who put the sender there: sender-bot:4 rules, person:9 owner. For a bot that is the bot's author. For a person it is the author of the identity's last move, or rules when nothing moved it.
  • putBack moves the sender back through intoBot / outOfBot under that author, with the side effects the public moves have: renaming after leaving, domain grouping, releasing domain holds. A rules bot that was deleted with its last identity comes back as a new rules bot.
  • A sender that is no longer where the rule put it (the owner moved it since) stays where it is.

Batches: remove, undo and reset mark the rule removed and clear its types in one write. Then they put senders back 100 per write. Pending put-backs are found by state (effects of removed rules not yet undone), so a stopped run resumes on the next remove, undo or reset.

A waiting writer gets in (transaction.ts): a new export, letWaitersIn, is the 2 ms pause writeBatch already takes after a commit. The sorting loops (add, and the put-back) take it between two writes. Without it a second writer that goes through write still gave up after 5 s during the add, on main too.

No schema change. Version 77 is not on npm yet (latest is 1.1.0), so before changes form without a migration. The schema note and GLOSSARY "sorting rule" are updated.

Timings

100,000 synthetic emails, 500 senders on one domain, load 8.6 to 15.8, with a second writer that goes through cli-core's write (cli-testing#413, performance/mail/rules, "Round 2"):

main (#3366) this PR
undo: 500 senders back 0.5 s, 1 write 3.7 s, 6 writes
second writer during the undo: longest wait, gave up 0.2 s, 0 1.9 s, 0
add: 500 senders to bots 9.5 s 6.6 s
second writer during the add: longest wait, gave up 5.0 s, 1 2.0 s, 0
senders left with the owner's mark after undo or removal 500 0

On a busy disk, the commit sets the lock time, not the senders. Each write of 100 is about 20 ms of work plus 55 ms of queued work, and the COMMIT took 0.1 to 3.3 s. So writes of 100 did not keep each write under 1 s here. They also make an undo slower in total than main's single write. Whether to keep 100 is an open decision for the owner.

Testing

  • New in src/store/mail-sorting.test.ts:
    • a rules bot, a plain person and an owner-kept person, each through a rule's undo or removal. The first two come back without the owner's mark, and the kind pass sorts them again. The third keeps the owner's mark. This test fails on main.
    • a 120-sender removal (two writes), and a put-back a stopped run left.
    • a program's one-mail rule losing to the owner's domain rule.
  • src/mail/sort-rules.test.ts: the authors case now asserts the owner wins across levels, and a subdomain case.
  • pnpm typecheck, biome check . (no errors), schema:check, the docs checks, cli-dev api-report --check, and the full vitest suite (996 files, 13,776 tests).

Not in this PR

  • The rule's own forward move of a sender out of a bot (moveToPerson) writes an owner-authored identity_link_events row. It stays after a removal. The CRM cleanup reads it (it never deletes a CRM contact the owner moved). Automatic sorting does not read it.
  • If the owner merged person X into Y after the rule, a sender is put back only when it is still on X or on X's current person.
  • A free-mail sender rule with 6,000 senders is still not measured.

🤖 Generated with Claude Code

leemour and others added 3 commits November 25, 2026 22:47
…ers back with their author, in batches

Three gaps left by #3366 (store version 77):

- The owner's choice always wins (contact-kinds plan, "Moving between
  entities" and "Mail categories"). sortRuleFor now walks every owner's
  rule in level order first, then every program's, then the header
  classifier. Within one author the first level that has a rule wins and
  within a level the newest. The index is split by author before the
  domain-suffix walk, so a program's rule on mail.shop.example no longer
  hides the owner's rule on shop.example.
- Removing or undoing a sender rule moved senders back through
  moveToPerson and moveToBot, which log the owner as author, so the kind
  pass never sorted them again. An effect's `before` now keeps who put the
  sender there (`sender-bot:4 rules`, `person:9 owner`), and the new
  putBack in sender-bots.ts moves it back with that author through
  intoBot / outOfBot. A rules bot gone with its last identity comes back as
  a new rules bot. A sender the owner moved since the rule stays.
- remove, undo and reset now mark the rule removed in one write, then put
  senders back 100 per write, as add already settles them. Pending
  put-backs are picked up by state, so a stopped run resumes on the next
  remove, undo or reset.

No schema change: version 77 is not on npm yet, so `before` changes form
without a migration. Docs: GLOSSARY "sorting rule", schema notes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…pped put-back

Removes a 120-sender rule, two writes of 100, and finishes a put-back
that a stopped run left behind. A chunk answers whether more may be left,
so the last one does not run one empty write more.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Measured with a second writer that goes through the store's own write
(100,000 synthetic emails, load 10 to 16): a sender rule's add, 100 per
write, still made that writer wait the full 5 s and give up, on the
build before this branch too; the chunked undo made it wait 4.1 s. Each
loop began its next write at once, and write pauses only when it waited
itself, so the waiter never saw the lock free.

transaction.ts now exports letWaitersIn, the 2 ms pause writeBatch
already takes after a commit, and the sorting loops take it between two
writes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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