Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,7 @@
"noExcessiveLinesPerFile": {
"level": "error",
"options": {
"maxLines": 1902
"maxLines": 1908
}
}
}
Expand All @@ -792,7 +792,7 @@
"noExcessiveLinesPerFile": {
"level": "error",
"options": {
"maxLines": 1024
"maxLines": 1033
}
}
}
Expand Down Expand Up @@ -822,7 +822,7 @@
"noExcessiveLinesPerFile": {
"level": "error",
"options": {
"maxLines": 3265
"maxLines": 3269
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions changes/feat-mail-rules.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
### Added

- **Mail sorting rules (store version 77).** `store.mail.sorting` keeps the owner's and the program's rules over mail:
`add` (with `dryRun`), `rules`, `remove`, `undo`, `reset` and `scopes`, all experimental. A rule names its scope,
one mail, a List-Id, a subject pattern, an address or a registrable domain, and says whether it sorts past mail too
(`applyTo: "all"`) or only mail dated from now on (`"new"`). The first scope that has a rule wins, in that order, then
the header classifier; within a scope the owner's rule beats the program's and the newest wins, and a domain rule may
beat the address rules (`overAddresses`). A rule sets the mail type, makes the sender a bot or a person through
`moveToBot` and `moveToPerson`, or files the sender under an organization. An email a rule typed has `typeSignal`
`rule:<id>`. A domain rule on a free-mail or throwaway domain is the owner's only and is set only with the `confirm`
of a dry run that shows how many addresses and emails it touches. Removing or undoing a rule types its mail again and
moves its senders back. Not the storage `store.mail.rules`.
2 changes: 1 addition & 1 deletion docs/dev/COORDINATION.md

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions docs/dev/GLOSSARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ Verbs used but not in cli-base's vocabulary, by CLI (2026-10-12). Each needs a r
| person | one human, across every messenger and mail (`person:<uid>`) | user, people | tg bot help: "user:<id> for a person", "the person's user id"; drive `people` |
| bot | a program that writes to the owner: a no-reply or alert address, a messenger's bot, a service's mail; an entity apart from people, with a name, identities and an optional organization, and no ties, tasks or merge review (`sender-bot:<id>`, the `sender_bots` table). Not an agent: `bot:<id>` and the `bots` table are agents that work here | automated person, sender (for a bot) | `people list kind:automated` still lists people a name or a bulk mailbox marks; memo's `{ bot }` author and `bot list` mean agents and a messenger's bot account |
| mail type | what one email is, apart from who sent it: one of the words in [Mail types](#mail-types), worked out per email from what the store keeps of it (`emails.mail_type`, key `mailType`). A sender's view is a summary of its mail, so a shop that sends receipts and sales counts under `order` and under `promotion` | category, kind (a person's `kind` is another thing), label | none |
| sorting rule | the owner's or the program's word on what mail is, by one of five scopes tried in this order: one mail, a List-Id or a subject pattern, an address, a registrable domain; then the header classifier. It sets a mail type, makes the sender a bot or a person for its whole history, or files the sender under an organization (`mail_sort_rules`, `store.mail.sorting`). An email it typed has `typeSignal` `rule:<id>`. Not a mail rule: `mail_rules` decide how one account's sync stores mail | filter, mail rule, label rule | none |
| reach | how a message reached the owner: `direct` (to the owner or a few people), `group` (a large group or a discussion list), `broadcast` (one sender to many readers who cannot answer in place). Worked out per email (`emails.reach`) and per sender (`people list`) | audience, scope | none |
| contact | one person as one source knows them: a mail correspondent, a Telegram user, a Drive owner, a calendar attendee; an `identities` row | user | none found beyond the above |
| entity | the thing the owner means — a person, an event, a document; it carries no provider id ([WC-DATA-2](https://github.com/WireCatLabs/cli-base/blob/main/docs/standards/data-model.md)) | record, object | none |
Expand Down Expand Up @@ -126,6 +127,11 @@ email itself carries (a part, schema.org markup, list headers), never by an addr
| `personal` | a person writing, to the owner or to a few people | primary, direct |
| `other` | a program's mail no rule above names | unknown |

A sorting rule decides before these: the first scope that has a rule wins (one mail; a List-Id or a subject pattern; an
address; a domain), and within a scope the owner's rule beats the program's and the newest wins. A domain rule may say
it beats the address rules. Addresses match whole and domains on a label boundary: `ann@` never matches `joann@`. A
domain rule on a free-mail or throwaway domain (`gmail.com`) is the owner's only and is set only after a dry run.

## Output keys

Read from source, not run.
Expand Down
16 changes: 16 additions & 0 deletions docs/reasons.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,22 @@ headings.
| `mail-findings.not-stored` | `not_found` | a mail finding cites an email the store does not hold | |
| `mail-findings.partial` | `validation_error` | a cited email is not stored whole yet; cite it once it is | |
| `mail-findings.stale` | `validation_error` | mail findings cite mail that changed since it was read; stage a new batch | |
| `mail-sort.apply-to` | `validation_error` | a sorting rule says whether it sorts past mail too: applyTo all or new | |
| `mail-sort.confirm` | `validation_error` | a free-mail domain rule needs the confirm of a dry run whose counts still hold; run the dry run again | |
| `mail-sort.email` | `not_found` | no incoming email has that id | |
| `mail-sort.free-mail-program` | `validation_error` | only the owner sets a sorting rule on a free-mail or throwaway domain | |
| `mail-sort.mail-type` | `validation_error` | the mail type is not one of the known types | |
| `mail-sort.no-action` | `validation_error` | a sorting rule sets a mail type, a sender or an organization | |
| `mail-sort.not-found` | `not_found` | no mail sorting rule has that id; the rules list names them | |
| `mail-sort.organization` | `validation_error` | the organization named is not in the store | |
| `mail-sort.over-addresses` | `validation_error` | only a domain rule can beat the address rules | |
| `mail-sort.owner-only` | `validation_error` | only the owner moves or files senders, undoes and resets; a program removes its own rules | |
| `mail-sort.scope` | `validation_error` | a sorting rule's scope is mail, list, subject, address or domain | |
| `mail-sort.sender` | `validation_error` | a sorting rule makes a sender a bot or a person | |
| `mail-sort.sender-new` | `validation_error` | a sender moves with its whole history, so an address rule that moves one applies to all | |
| `mail-sort.sender-ref` | `validation_error` | the sender is named by an address, a person: ref or a sender-bot: ref | |
| `mail-sort.sender-scope` | `validation_error` | a sorting rule moves or files a sender at address or domain scope only | |
| `mail-sort.value` | `validation_error` | the value names no mail, list, subject, address or domain of that scope | |
| `mail.attachments-unread` | `provider_unavailable` | the email could not be read again, so its skipped attachments have no text | |
| `mail.cursor-ahead` | `validation_error` | an acknowledge cursor is past the newest stored email | |
| `mail.holder-not-yours` | `permission_error` | a run moves only the mark of the holder its schedule names | |
Expand Down
46 changes: 44 additions & 2 deletions docs/storage/schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Every table of the shared store (tg, max, memo, zm) as its migrations create it.
`drizzle/` must agree with it. `pnpm schema:check` fails when they do not, and `src/store/sqlite/schema.test.ts`
checks every table and column of a new store against this page.

148 tables, 28 full-text indexes.
150 tables, 28 full-text indexes.

## Conventions

Expand Down Expand Up @@ -812,8 +812,9 @@ One email.
| `mail_type` | text | | | what the email is (`order`, `newsletter`, `personal`, …, docs/dev/GLOSSARY.md); null until worked out, and nulled by triggers on its inputs |
| `reach` | text | | | `direct`, `group` or `broadcast`: how it reached the owner; kept with `mail_type` |
| `type_signal` | text | | | what decided `mail_type`, such as `list-id`; kept with it |
| `sort_checked` | integer | | | 1: `mail_type` was worked out with the sorting rules; a build before version 77 leaves it null |

*Keys and indexes:* `UNIQUE (account_id, external_id)`, `INDEX (account_id, sent_at DESC)`, `INDEX (mail_rule_id) WHERE mail_rule_id IS NOT NULL`, `INDEX (mail_type)`, `INDEX (id) WHERE mail_type IS NULL`, `INDEX (coalesce(json_extract(headers, '$."list-id"'), json_extract(metadata, '$.listId')))`, `INDEX (email_thread_id)`, `INDEX (from_identity_id)`
*Keys and indexes:* `UNIQUE (account_id, external_id)`, `INDEX (account_id, sent_at DESC)`, `INDEX (mail_rule_id) WHERE mail_rule_id IS NOT NULL`, `INDEX (mail_type)`, `INDEX (id) WHERE mail_type IS NULL`, `INDEX (id) WHERE mail_type IS NOT NULL AND sort_checked IS NULL`, `INDEX (coalesce(json_extract(headers, '$."list-id"'), json_extract(metadata, '$.listId')))`, `INDEX (email_thread_id)`, `INDEX (from_identity_id)`

*Checks:* `CHECK ("references" IS NULL OR json_valid("references"))`, `CHECK ("headers" IS NULL OR json_valid("headers"))`, `CHECK ("metadata" IS NULL OR json_valid("metadata"))`

Expand All @@ -838,6 +839,47 @@ A mail rule of one account: conditions an email must match, and what to do with

*Checks:* `CHECK ("conditions" IS NULL OR json_valid("conditions"))`, `CHECK ("actions" IS NULL OR json_valid("actions"))`

### `mail_sort_rules`

A sorting rule over mail, the owner's or the program's: what one mail, a List-Id, a subject pattern, an address or a registrable domain is. The first scope that matches wins in that order, then the header classifier; within a scope the owner's rule beats the program's and the newest wins. Not `mail_rules`, which decides how one account's sync stores mail.

| Column | Type | Constraints | References | Meaning |
|---|---|---|---|---|
| `id` | integer | PK | | |
| `scope` | text | not null | | `mail`, `list`, `subject`, `address` or `domain` |
| `value` | text | not null | | the email's id, the List-Id, the folded subject pattern, the lower-case address or the registrable domain |
| `mail_type` | text | | | the type it gives each mail it matches; null leaves the type to the classifier |
| `sender` | text | | | `bot` or `person`: what the sender becomes, for its whole history; the owner's only |
| `organization_id` | integer | | | the organization the sender's addresses are filed under; no foreign key, a gone one files nothing |
| `over_addresses` | integer | not null | | 1: this domain rule beats the address rules at its domain |
| `past` | integer | not null | | 1: past mail too; 0: only mail dated from `created_at` on |
| `author` | text | not null | | `owner` or `program` |
| `decided_by` | text | | | the bot that made a program's rule |
| `reason` | text | | | why, in the owner's or the program's words |
| `created_at` | integer | not null | | when this row was saved here |
| `updated_at` | integer | not null | | when this row last changed here |
| `removed_at` | integer | | | removed; kept so the removal can be undone |
| `undone_at` | integer | | | its add was undone; the next undo goes further back |

*Keys and indexes:* `INDEX (scope, value)`

### `mail_sort_effects`

What a sorting rule did to one sender identity beyond its mail types: the sender moved to a bot or a person, or its address filed under an organization. A removal of the rule puts back `before`. A trigger drops an identity's rows with it.

| Column | Type | Constraints | References | Meaning |
|---|---|---|---|---|
| `id` | integer | PK | | |
| `rule_id` | integer | not null | | the `mail_sort_rules` row |
| `identity_id` | integer | not null | | no foreign key, as the bot tables: an older build purging an account does not know this table |
| `action` | text | not null | | `sender` or `organization` |
| `before` | text | | | `person:<id>` or `sender-bot:<id>` it was held by; for `organization`, `held` when it already was |
| `after` | text | | | where the rule put it; equal to `before` when the rule left it, as for past senders |
| `created_at` | integer | not null | | when this row was saved here |
| `undone_at` | integer | | | put back when the rule was removed |

*Keys and indexes:* `INDEX (rule_id)`, `INDEX (identity_id)`

### `email_recipients`

One address on an email.
Expand Down
34 changes: 34 additions & 0 deletions drizzle/20261125205013_version-77-mail-sort-rules/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
CREATE TABLE `mail_sort_effects` (
`id` integer PRIMARY KEY AUTOINCREMENT,
`rule_id` integer NOT NULL,
`identity_id` integer NOT NULL,
`action` text NOT NULL,
`before` text,
`after` text,
`created_at` integer NOT NULL,
`undone_at` integer
);
--> statement-breakpoint
CREATE TABLE `mail_sort_rules` (
`id` integer PRIMARY KEY AUTOINCREMENT,
`scope` text NOT NULL,
`value` text NOT NULL,
`mail_type` text,
`sender` text,
`organization_id` integer,
`over_addresses` integer NOT NULL,
`past` integer NOT NULL,
`author` text NOT NULL,
`decided_by` text,
`reason` text,
`created_at` integer NOT NULL,
`updated_at` integer NOT NULL,
`removed_at` integer,
`undone_at` integer
);
--> statement-breakpoint
ALTER TABLE `emails` ADD `sort_checked` integer;--> statement-breakpoint
CREATE INDEX `emails_sort_to_check` ON `emails` (`id`) WHERE mail_type IS NOT NULL AND sort_checked IS NULL;--> statement-breakpoint
CREATE INDEX `mail_sort_effects_by_rule_id` ON `mail_sort_effects` (`rule_id`);--> statement-breakpoint
CREATE INDEX `mail_sort_effects_by_identity_id` ON `mail_sort_effects` (`identity_id`);--> statement-breakpoint
CREATE INDEX `mail_sort_rules_by_scope_value` ON `mail_sort_rules` (`scope`,`value`);
Loading
Loading