Skip to content

feat(people): organizations from mail domains - #3347

Merged
leemour merged 2 commits into
mainfrom
feat/organizations-from-domains
Nov 25, 2026
Merged

leemour merged 2 commits into
mainfrom
feat/organizations-from-domains

Conversation

@leemour

@leemour leemour commented Nov 25, 2026

Copy link
Copy Markdown
Member

Task

Plan step 5 of the contact-kinds plan (plan #3323, "Grouping under an organization"): organizations from mail domains, with the three exceptions. Part of #3314.

Why

After #3339 a program that writes to the owner is a sender bot, but nothing says whose bot it is. Ten no-reply and info@ addresses of one bank showed as ten unrelated senders. People at that bank had no link to it either.

What

The rule (src/store/sqlite/domain-organizations.ts, run from keep() in people-list.ts after the rule moves and before the kinds are written):

  • A sender bot, or a shared mailbox (kind organization by role-address), at a company domain joins the organization of its registrable domain (registrableOf / tldts getDomain: mail.bank.ru and bank.ru are one).
    • The organization is the one already holding an address at that domain. Otherwise a new company is made, named by the sender when the name is a company's (isCompanyName) or equals the domain's own word (Bank for bank.ru); otherwise it is named by the domain.
    • The addresses go to organization_identities with method domain, author rules, and the bot's sender_bots.organization_id is set.
    • This never goes to merge review and makes no proposal.
  • A human with an address at that domain gets a member-of link (author rule, metadata.derived = "mail-domain", confirmed). They are never merged. A new organization also links the humans already stored at its domain.
  • Exceptions: FREE_MAIL; throwaway mail from disposable-email-domains-js (CC0, already a dependency, so nothing was vendored); and SENDING_PLATFORMS, a hand table. A platform domain never groups anything.
  • The owner's word wins:
    • The owner removes a membership (knowledge.removeRelation). The existing rejected relationship proposal is checked, and the link is never added again.
    • The owner erases an organization (knowledge.eraseOrganization). This leaves a rejected proposed_actions row of kind organization-domain for each company domain it held, and the rule never makes an organization for that domain again. The row is written only from the owner-facing erase, not from a CRM purge's clean-up.
    • The owner moves a bot to a person (moveToPerson). The rule's holds on those identities go. A person the owner kept as a person holds nothing; they can still be a member.
  • KIND_RULES goes to 8, so the first list after the upgrade groups the senders already stored.

Decisions I made (owner asleep):

  • No migration. sender_bots.organization_id, organization_identities.method/author and links.author already exist, so I made no cli-core: merges into main and release runs — announce here first (continues #849) #3157 reservation.
  • A human alone makes no organization. Otherwise every correspondent's company becomes one. A human is linked only to an organization that already holds the domain.
  • New organizations get scope personal, the schema default. The other automatic creators use work, but these senders are mostly services the owner uses privately.
  • member-of role is null. The rule cannot tell an employee from a contact.
  • The owner and test people are left out.
  • A domain that two organizations hold is skipped, not guessed.
  • Link source owner, author rule follows the tasks.ts answered-by precedent. LINK_ORIGINS has no rule.
  • Only From is matched. The plan's DKIM-domain matching and grouping by List-Id or display name inside a platform are not built. The bounce and DKIM entries in the table guard mail whose From carries them.

Sending platforms, checked against vendor pages on 2026-11-25:

Status Domains
Verified on the vendor's own page amazonses.com (AWS), mandrillapp.com (Mailchimp), mtasv.net (Postmark), ghost.io (Ghost), mcsv.net, mcdlv.net, mailchimpapp.net, rsgsv.net (Mailchimp)
Partial sendgrid.net (Twilio; the page does not name the header), buttondown.email (Buttondown blog, not docs), mailgun.org (sandbox only)
Search excerpt of a vendor page only (the page refused the fetch or redirects) substack.com, sparkpostmail.com, brevosend.com, beehiiv.com
Unverified buttondown.com, list-manage.com, postmarkapp.com, mailgun.net, convertkit-mail.com, sendinblue.com, mlsend.com

PERF (found, not fixed here): isDisposableEmailDomain builds its whole set on every call, about 0.5 ms each. The new module builds the set once. src/mail/score.ts still calls the slow function per sender.

Gaps left:

  • The owner can take one bot or mailbox out of its organization only by erasing the organization or by moveToPerson. No store method detaches one address. memo needs one before it can offer "not this company".
  • No category from domain lists (out of scope).
  • memo and Desktop do not show organizations of bots yet.

Testing

All runs used --maxWorkers=1.

  • New src/store/domain-organizations.test.ts (7 integration tests):
    • a bot and an info@ at mail.bank.ru / bank.ru → one organization named Bank, and the bot's organization_id is set;
    • a name that is no company's → named by the domain;
    • gmail.com and mailinator.com → none;
    • two newsletters on substack.com plus one on ghost.io → no organization, no membership;
    • a human at a company domain → member-of, whether the human came first or later, and no proposal;
    • the owner's separations (a removed membership, an erased organization, a bot moved to a person) are not undone after every kind is worked out again;
    • a second full pass changes no row.
  • people-list.test.ts: the shared mailbox info@hotel.ru now also has the organization signal, because an organization holds it.
  • Existing suites passed: sender-bots, people-list, people-kinds-stored, people-matching-rules, person-match-scan, person-doubts, person-merges, person-separation, person-erase, organization-erase, agent-links, crm-email-join, identity-links, identity-splits, same-email-join, mail-findings, mail, records-matching, people-owner, mail/own, mail/sender-signals, crm/relations, messenger, proposals registry, private-people, owner-labels, people-owner-guard, rollback-copy, store, upgrade, version-61, version-66, mcp-doctor, store-maintenance, store-tables.
  • pnpm typecheck, pnpm lint, schema:check, docs:check, and command-lint.

Cost, on a synthetic store with 22,000 senders: 10,000 no-reply bots and 4,000 role mailboxes across 2,000 domains, 6,000 humans at those domains, and 2,000 at gmail. Load average was about 15.

Step Time
First list after the upgrade (rule moves, kinds, grouping) 1.6 s
The grouping pass alone, making 2,000 organizations, 14,000 holds, 6,000 links 0.64 s
The same pass with nothing to do 0.16 s
The pass for 50 people 30 ms
A list after a small sync 0.55 s

All of it runs under the write lock, well under the other writers' 5 s wait. Before the disposable-set fix, the pass with nothing to do took 13 s.

🤖 Generated with Claude Code

leemour and others added 2 commits November 25, 2026 05:12
Plan step 5 of the contact-kinds plan. When kinds are kept, a sender bot
or a shared mailbox at a company domain joins the organization of its
registrable domain: the one already holding an address there, else a
new company named by the sender (a company name or the domain's own
word) or by the domain. Its addresses go to organization_identities
(method domain, author rules) and a bot's sender_bots.organization_id is
set. A human writing from that domain gets a member-of link, never a
merge or a proposal; humans alone make no organization.

Free mail, throwaway mail (disposable-email-domains-js, already a
dependency) and a hand table of sending platforms are never company
domains. The owner's word holds: a removed member-of leaves a rejected
relationship proposal the rule checks, erasing an organization leaves a
rejected organization-domain proposal per domain, and moveToPerson drops
the rule's holds. No migration; KIND_RULES goes to 8.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@leemour
leemour merged commit 197f893 into main Nov 25, 2026
38 of 41 checks passed
@leemour
leemour deleted the feat/organizations-from-domains branch November 25, 2026 04:20
leemour added a commit that referenced this pull request Nov 25, 2026
## Task

Follow-up to #3347: its PR body noted that `isDisposableEmailDomain`
(`disposable-email-domains-js`) builds its whole list on every call, and
that `src/mail/score.ts` still called it once per sender.

## Why

The library's `isDisposableEmailDomain` turns its array of about 9,000
domains into a new `Set` on each call. `scoreGroup` calls it for every
sender group, so scoring a large mailbox spent most of its time
rebuilding the same set.

## What

- `src/mail/score.ts`: new `isDisposableDomain(domain)`, which builds
the set once (`disposableEmailBlocklistSet`) on first use. `scoreGroup`
uses it.
- `src/store/sqlite/domain-organizations.ts`: its private cache from
#3347 is gone; `companyDomainOf` imports `isDisposableDomain` from the
same module it already takes `FREE_MAIL` and `registrableOf` from.
- Changelog fragment under "Fixed".

**Decision (owner asleep):** the shared helper lives in `mail/score.ts`,
next to `FREE_MAIL`, rather than in a new module. Both callers already
import from there. The input is not lower-cased or trimmed, unlike the
library function: both callers pass `registrableOf` output, which is
lower case already.

## Testing

**Measured**, synthetic case: 20,000 senders, 3 emails each (every tenth
at `mailinator.com`), `scoreGroup` over all of them, 5 runs in one
process, vitest `--maxWorkers=1`, Node 24.19, 24 cores, load average
about 5. The timing file was a throwaway test, not committed.

| | Runs (ms) | Median |
|---|---|---|
| Before (`origin/main`) | 8026, 7116, 6948, 6987, 7255 | 7.0 s |
| After | 240, 209, 218, 208, 207 | 0.21 s |

About 0.35 ms per sender before, 0.01 ms after (that is the whole score,
not only the check).

- New test in `src/mail/score.test.ts`: `isDisposableDomain` is true for
`mailinator.com` and `guerrillamail.com`, false for `gmail.com` and a
company domain. The existing `disposable_domain` signal test still
passes.
- `src/mail/score.test.ts`, `src/store/domain-organizations.test.ts`: 22
passed.
- `pnpm typecheck`, `pnpm lint` (no new warnings), `pnpm docs:check`,
`pnpm schema:check`.

🤖 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
…3350)

## Task

Contact-kinds plan, "Domain lists" (plan step 5, second half): an
organization gets what it does (bank, airline, shop, news, government,
…) from a domain list built into the package. Open question 4 is
answered with the default: the list is built into the package at
release, with no network at run time. Part of #3314.

## Why

After #3347, a bank's no-reply bots and mailboxes are one organization,
but nothing says it is a bank. Desktop's Bots and Organizations tabs
need that for their filter. A lookup per sender (a category API, DNS)
would tell a third party who writes to the owner, so the table ships
with the package.

## What

- **Data:** `scripts/organization-sectors.ts` queries the Wikidata
SPARQL endpoint (https://query.wikidata.org/sparql), one class after
another, and writes `src/domain/organization-sectors.generated.ts`.
Header: CC0 1.0 and the export date (2026-11-25). The file is **358 KB,
25,639 domains**:

| sector | domains | Wikidata items | classes (with subclasses unless
noted) |
  |---|---|---|---|
  | airline | 1,695 | 1,867 | Q46970 |
  | bank | 3,571 | 3,796 | Q22687 |
| financial | 3,488 | 7,826 | insurance Q2143354, payment provider
Q1956140, crypto exchange Q25401607, stock exchange Q11691, financial
institution Q650241 |
  | telecom | 718 | 752 | Q2401749, Q1941618, ISP Q11371 |
  | social | 651 | 677 | Q3220391 |
| shop | 3,754 | 3,704 | Q4382945, Q3390477, Q507619, Q18043413,
Q216107; 2+ sitelinks |
  | delivery | 120 | 126 | postal service Q1529128 |
| news | 7,592 | 9,651 | Q1193236, Q11032, Q192283, Q17232649, Q1153191,
Q15265344, Q2001305; 3+ sitelinks |
| government | 4,050 | 6,039 | ministry Q192350, revenue service
Q573607, government agency Q327333 (direct instances only); 2+ sitelinks
|

- **Query rules:** the official website (P856) at its best rank, no end
date (P582), of an item not dissolved (P576). The registrable domain
comes from tldts, the same way `registrableOf` reads mail. The
User-Agent is `WireCat-build/1.0
(https://github.com/WireCatLabs/cli-core)`; the query sends class ids
only. Queries run one at a time with 1 s between them, inside the limits
[the WDQS
manual](https://www.mediawiki.org/wiki/Wikidata_Query_Service/User_Manual#Query_limits)
describes (60 s per query, 5 parallel). A 429 waits for `Retry-After`. A
502/503/504 is tried again, and a timeout splits the query into the
class's direct subclasses. A full run takes about 10 minutes.
- **Apply:** at the end of `groupByDomain`, every organization holding a
company domain gets `metadata.sector` (the JSON column already exists,
so there is no migration and no #3157 reservation). It is written only
when the value changes. Only domains that pass `companyDomainOf` are
looked up, so free mail, throwaway mail and sending platforms never give
a sector. None of them is in today's table: an earlier export without
the host rule had `mail.ru` as social. `KIND_RULES` goes to 9, so
existing stores get the pass on their next list.
- **Exposed:** `Organization.sector: Sector | null` on every
organization row the store returns. The `organization` entity
(`src/entities/knowledge-lists.ts`, also used by `organization_context`)
has `sector`, nullable. `Organization` moved next to its mapper in
`organizations.ts`; `knowledge.ts` re-exports it because that file is at
the 1,000-line lint limit.
- **Docs:** ARCHITECTURE (organizations from mail domains), the schema
spec and `schema.md` (the `organizations` doc names `metadata.sector`),
THIRD_PARTY_NOTICES (Wikidata, CC0), a changelog fragment.

**Left open:**
- A sector is never cleared. It stays when a later export drops the
domain or the organization stops holding it.
- A pass with no company mail skips the sector step: `groupByDomain`
returns early.
- The owner cannot set or clear a sector, and memo and Desktop do not
show or filter by it yet.
- DKIM-domain matching (plan) is not built.

**Decisions I made (owner asleep):**
- **Named `sector`, not `category`.** `Category` already exists
(`src/domain/categories.ts`): client / supplier / partner / employer,
derived from relationships, and it is `organizations({ category })`'s
filter. One word for two meanings would confuse every caller. Renaming
is cheap now if the owner prefers `category`.
- **Best rank, not preferred only.** Preferred-only statements keep 227
of 3,796 banks. Best rank is the preferred statement where one exists,
else the normal one.
- **Only home pages.** A URL with a path other than `/` or a language
folder is dropped, and so is a host other than the domain itself or
`www`. Without this rule, banks' Facebook pages made `facebook.com` a
bank, YouTube TV made `youtube.com` news, and blogs made `wordpress.com`
news.
- **A notability floor** (Wikipedia sitelinks) for shop, news and
government. Without it the file was 1.6 MB and about 90,000 domains,
mostly local newspapers. Government agency's subclass tree also pulls in
about 50,000 towns, so only its direct instances count.
- **Precedence** is the table order, most specific first. A domain
claimed by 3 or more sectors is dropped (a conglomerate or a host).
- **UT1 is not included** (CC BY-SA). That is left to the owner.

## Testing

All runs used `--maxWorkers=1`.
- New `src/domain/organization-sectors.test.ts`: every domain appears
once, in lower case, and is registrable.
- New integration test in `src/store/domain-organizations.test.ts`:
- a bot at `mail.hsbc.com` → organization `HSBC` with `sector: "bank"`;
  - a company outside the table → `null`;
- an organization holding a `mail.ru` address (free mail) → `null`. This
guards against a later export listing it; today's table does not;
  - a second full pass changes no `organizations` row.
- Passed: `domain-organizations`, knowledge, organization-erase,
owner-labels, mail-findings, people-list, sender-bots,
people-kinds-stored, crm, records-matching, business-review, areas,
`mail/score`.
- Some store tests timed out at 15 s on one run. That happened on
unchanged `main` too (crm.test.ts took 30 s on main against 1.4 s on
this branch, one after the other), so it was machine load. They passed
when run alone.
- The 40 test files that read organizations (398 tests) and
`entities.test.ts` passed; the entity samples have `sector`.
- `pnpm typecheck`, `pnpm lint` (no new warnings), `pnpm docs:check`,
`pnpm schema:check`.
- Cost: loading the table takes 4 ms, and building the lookup map on
first use takes 13 ms (from `dist`, Node 24).

🤖 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>
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