Skip to content

perf(mail): undo and removal of a 500-sender sorting rule, with a second writer - #413

Open
leemour wants to merge 1 commit into
mainfrom
perf/mail-rules-undo
Open

leemour wants to merge 1 commit into
mainfrom
perf/mail-rules-undo

Conversation

@leemour

@leemour leemour commented Nov 25, 2026

Copy link
Copy Markdown
Member

Task

Re-measure the undo of a sender sorting rule with 500 senders after the cli-core fixes in fix/mail-rules-owner (PR linked from there). Results go to the existing page performance/mail/rules/README.md, section "Round 2".

Why

cli-core store version 77 (#3366) put senders back with the owner's mark when a sender rule was removed or undone, and did it in one write. The fix puts them back with their earlier author, 100 per write. The question was what the undo costs now, and whether another writer gets the lock meanwhile.

What

  • lock-probe.mjs: a second writer in a worker thread. It writes through cli-core's own write every 5 ms, as another WireCat command would, and reports its longest wait and give-ups.
  • bench.mjs: step 5 now runs the 500-sender rule's add, undo and removal, each with the probe beside it, and counts the senders left with the owner's mark.
  • Round 2 results on the page, and a correction of round 1's claim that 100 per write kept other writers safe.

Headline numbers (100,000 synthetic emails, load 8.6 to 15.8):

main fix, 2 ms pause
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
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

The commit took 0.1 to 3.3 s per write on the shared disk, while each write's work was about 20 ms.

Testing

biome check, markdownlint-cli2, cspell, pnpm docs:check. The bench ran 5 times. The page reports only the 3 runs whose probe works like a WireCat writer.

🤖 Generated with Claude Code

…ond writer

Round 2 of performance/mail/rules for cli-core fix/mail-rules-owner.
lock-probe.mjs writes through cli-core's own write beside the bench and
reports its longest wait and give-ups. Three builds on 100,000 synthetic
emails: main (#3366), the fix without a pause between writes, and the fix
with a 2 ms pause.

- main left all 500 put-back senders with the owner's mark; the fix
  leaves none.
- Writes back to back starved the second writer (gave up after 5 s on
  main and without the pause); with the 2 ms pause none gave up.
- The commit (0.1 to 3.3 s on a busy disk) sets the lock time, not the
  100 senders a write moves (about 20 ms of work).

Corrects round 1's claim that 100 per write kept other writers safe.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
leemour added a commit to WireCatLabs/cli-core that referenced this pull request Nov 26, 2026
…ers back with their author, in batches (#3369)

## 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](WireCatLabs/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](https://claude.com/claude-code)

---------

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