Skip to content

feat(people): sender signals and reach on people rows, signed program headers - #3334

Merged
leemour merged 1 commit into
mainfrom
feat/contact-signals
Nov 25, 2026
Merged

leemour merged 1 commit into
mainfrom
feat/contact-signals

Conversation

@leemour

@leemour leemour commented Nov 25, 2026

Copy link
Copy Markdown
Member

Task

Slice 1a of the contact-kinds plan (plan PR #3323, not merged). It builds plan steps 2 and 3 in a read-only form: nothing new is stored on persons. Part of #3314.

Why

Service mail shows up under Strangers. An example is a bank's statements from an address with no no-reply word. The mail itself says a program sent it (Auto-Submitted, an empty Return-Path) or that it went to a list (one-click unsubscribe, Feedback-ID). Until now the store did not keep these headers.

Coordinator decisions for this slice:

What

  • Mail headers (src/mail/headers.ts). mailHeadersOf now also keeps three headers in emails.headers: auto-submitted (unless no or auto-replied), list-unsubscribe-post (one-click only) and feedback-id.
    • Each is kept only when a DKIM signature that Gmail's mx.google.com stamp passed covers that header, and the signature's domain has the From's registrable domain (signedFieldsOf, reuses registrableOf).
    • An empty Return-Path is kept as null-return-path, unless the mail is an auto-reply: an out-of-office makes no bot.
    • No store version: these are JSON keys.
  • Gap, documented in MAIL.md. Only Gmail's own stamp is trusted. Mail from an IMAP server never keeps the three signed headers.
  • people.list rows get reach (direct, group, broadcast or null) and signals. Both come from src/store/sqlite/sender-signals.ts: a pure senderSignalsOf plus sentMailOf, a query for the page's people. They are worked out for the returned page only and never stored.
  • Kind. A signed Auto-Submitted or an empty Return-Path on most of a sender's mail makes them automated (new KindSignals), unless the owner wrote to them. KIND_RULES goes from 5 to 6, so stored kinds are worked out again. Everything else is unchanged.

Defaults chosen for the plan's open questions:

  • Q2, mixed sender. One sender. The reach most of their mail had wins, the wider on a tie. Mail the owner wrote them, or a private chat both wrote in, makes it direct.
  • Q3, discussion lists. A List-Id that 3 or more addresses send to (+tag aside) is group. One sender is a newsletter, broadcast.
  • Owner's position. Only "no To or Cc at all" counts as broadcast. The owner missing from a To/Cc that names others proves nothing, because an alias the store does not know looks the same. The position comes from email_recipients, which already holds every To/Cc, so old mail counts too.

Notes for the re-plan:

  • PERF. On a page with List-Id mail, sentMailOf reads every stored email's List-Id. There is no index, and adding one needs a migration. A follow-up for the stored version.
  • Inconsistency, existing behaviour. A discussion-list member can have reach: group and still be kind: automated through today's bulk-mail rule. It is left as it is on purpose.
  • Downstream. cli-memo only reads ListedPerson and kindSignals. The widened union is listed under "Changed — may break callers".

Testing

All with --maxWorkers=1:

  • New: src/mail/sender-signals.test.ts (sync through the fake mail source to the people list, 3), src/mail/headers.test.ts (5), src/store/sqlite/sender-signals.test.ts (3).
  • Existing: people-list, people-kinds-stored, person-doubts, people-matching-rules, mail/score, mail/own (86 passed); mail/sync, store/mail, mail/rules, sender-scores, probe, mail/cli (135 passed).
  • pnpm typecheck, pnpm ci:quick, pnpm schema:check, pnpm docs:check, Biome on the changed files.

🤖 Generated with Claude Code

… headers

Mail sync keeps Auto-Submitted, one-click List-Unsubscribe-Post and
Feedback-ID in emails.headers, each only when a DKIM signature of the
From's own registrable domain that Gmail passed covers it, plus an empty
Return-Path unless the mail is an auto-reply.

people.list rows get `reach` (direct, group, broadcast or null) and
`signals`, worked out for the page only and not stored. A List-Id three
addresses send to (+tag aside) is a discussion list (group); no To or Cc
is broadcast; the owner missing from a To/Cc that names others proves
nothing. A signed Auto-Submitted or an empty Return-Path on most of a
sender's mail makes them automated unless the owner wrote to them;
KIND_RULES 6. No store version.

Part of #3314.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@leemour
leemour merged commit 42f05eb into main Nov 25, 2026
43 checks passed
@leemour
leemour deleted the feat/contact-signals branch November 25, 2026 01:40
leemour added a commit that referenced this pull request Nov 25, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
leemour added a commit that referenced this pull request Nov 25, 2026
Rebased onto main after #3338 (store version 73, which replaced #3288).
Version 74 was regenerated from 73's snapshot. Its new tables use
`AUTOINCREMENT`, as 1.0 asks, and `minCompatible` stays 73.
`api/store.api.txt` and `docs/reasons.md` were regenerated.
`version-73.test.ts` now migrates only up to 73, so a later version does
not break it. The changelog entry is under "Changed — experimental":
`KindSignal` and the people list are `@experimental`, and the rest only
adds things, so no major is needed.

## Task

Plan steps 3 and 4 of the contact-kinds plan ([plan
#3323](#3323)): bots as
their own entity, and the owner's choice to move a sender between a
person and a bot. Owner's ruling NEED-109 A. Part of #3314.

## Why

A bot was a person with the kind `automated`. Every list, merge scan and
tie check had to remember to skip it, and each one that forgot was a bug
(#3320 was one). The owner also had no way to say "this is a bot" or
"this is a person" in a way that holds.

## What

**Schema chosen (store version 74).** Tables, an index and triggers
added only. `minCompatible` stays 73.
- `sender_bots` (id, `person_id` unique, name, `organization_id`, author
`rules` or `owner`, reason, times). `bots` was already taken by agents,
and `bot:<n>` refs mean agents, so the table is `sender_bots` and the
ref is `sender-bot:<id>`.
- **A bot keeps the person row it was sorted from.** Its identities stay
in `identity_links` on that row. I chose this over
`identity_links.bot_id` or a `bot_identities` table:
- `identity_links.person_id` is `NOT NULL`. Making it nullable needs a
table rebuild.
- About 60 modules join `identity_links`. Taking an identity out of it
means ingest creates a new person on the next sync (`ensurePerson`). The
emptied person also shows as "added by hand".
- Desktop accepts only `person:<uid>` refs (`person_ref` in
`people.rs`). `people show person:<uid>` keeps working.
- `sender_bot_moves`: one row per identity per move. It holds from
(person or bot), to, author `rules` or `owner`, reason, and time.
- **No foreign keys** on either table, as `document_changes` does, so an
older build can still erase a person or purge an account. Triggers drop
the rows with the person or identity, and clear the organization. Other
triggers clear the stored kind when a bot or a move is written.
- **PERF from #3334:** an expression index `emails_by_list_id` on the
exact List-Id expression `sentMailOf` uses. No new column and no
backfill: `CREATE INDEX` fills it. A test asserts that `EXPLAIN QUERY
PLAN` of the real statement (`LIST_SENDERS`) uses the index. The schema
codegen now passes an expression key through.

**Rule move** (`people-list.ts`, `KIND_RULES` 7). When kinds are kept, a
person moves to a bot when all of these hold:
- their kind is `automated` by a program's signal: the platform bot
flag, a no-reply or notification address, a signed Auto-Submitted, a
null return path, or `bulk_never_replied`;
- the owner wrote them nothing: no mail tie and no two-way private chat
(the platform flag holds anyway);
- no `ALWAYS_SHOWN` reason applies;
- every identity is the program's, or they have only one.

Other cases:
- `automated-name` or a bulk mailbox alone marks a person and moves
nothing. A newsletter a human writes stays a person.
- A rule's bot goes back to a person when the rules stop holding, for
example when the owner writes to it. This is logged.
- No person is emptied by a rule move.

**Owner's choice.** `store.people.moveToBot(subject, by, { bot?, name?,
reason? })` and `moveToPerson(subject, by, { person?, reason? })`.
Subject: `person:<uid>`, `sender-bot:<id>` or an `IdentityRef`. Owner
only (`identity.owner-only`).
- One identity of a person with several gets a person row of its own
first (an owner's unlink).
- A bot the owner made is `author = 'owner'`. An identity whose latest
move is the owner's, to a person, is never moved by a rule. Both survive
`KIND_RULES` bumps (tested).

**Bots are left out of:**
- the matcher (`identity.not-a-person` for email and phone joins too;
name scans already skip `automated`);
- `tasks.assign` (new reason `sender-bot.assignee`);
- `people.similar`;
- `people.ties` (empty).

**`kind:automated` compatibility.** A bot is still listed as `automated`
under its `person:<uid>` ref, with the same counts and paging. The row
gains `bot: "sender-bot:<id>" | null` and has empty `ties`. memo's
`personRow` and Desktop's `listed()` read named fields only, so both
keep working unchanged.

**People counts.** `people.list` counts are the only people tallies in
cli-core. Bots count under `automated` and never under `person`.

**Alias joining the owner later (item 4).** I found no stale kind.
`person_kind_email_au` clears the recipients' kinds when `markOwnMail`
flips `outgoing`. A new test in `src/mail/own.test.ts` proves it:
stranger first, person after the alias joins. No fix was needed.

**Not in this PR (memo needs):** `bots list` and a command for the two
moves (for example `people bot <ref>` and `people person <ref>`), and a
field for the bot ref on `people list` rows. Call that field
`sender_bot`, not `bot`: Desktop's `Listed.bot` already means "has the
messenger bot signal". Desktop needs a Bots tab (plan step 7). No
downstream implements `PeopleStore` (grep of cli-memo, wirecat-desktop,
cli-template).

**RISK.** The first list after the upgrade creates every bot under the
write lock. A synthetic store with 20,000 no-reply senders took 1.4 s
for that list, against 0.66 s with nothing to move (load average about
30). The lock part stays under the other writers' 5 s wait. Chunk it if
a real store shows more.

## Testing

All runs used `--maxWorkers=1`.
- New `src/store/sender-bots.test.ts` (8 tests):
  - a rule move, kept through a sync;
- the owner's moves both ways, through a `KIND_RULES` change, refused
for an agent and for the owner's own person;
  - one identity moved to a new bot and into an existing one;
  - no join by address, and no task assignee;
- an upgrade from 73 moves the programs, and `EXPLAIN` shows the index
is used;
  - a rule's bot goes back once the owner writes to it;
  - a purge and an erase take the bot's rows with them;
  - no proposal by name.
- Existing suites: people-list (19), people-kinds-stored (6),
people-matching-rules (23), person-match-scan (34), person-doubts,
person-merges, schema, sender-signals, mail/own (8), store, upgrade,
rollback-copy, transaction, doctor, store-tables, store-maintenance.
- Under load (average 30–45), one people-list test and one
people-kinds-stored test passed only when run alone. They were over the
time limit, not wrong.
- `pnpm typecheck`, `ci:quick`, `schema:check`, `docs:check`, and Biome
on the changed files.
- Line limits were raised for `spec.mjs` (+2), `schema.ts` (+5) and
`reasons.ts` (in the 1010 group), as version 72 did.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

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