src/mail/, exported as @wirecat/cli-core/mail and /mail/testing. A mail CLI (mail-cli) writes its sign-in and
one adapter, a MailSource; the sync, the scoring, the owner's rules and the contract kit are shared, as for
calendars. The mail tables and store.mail are described in the schema; this page
covers what runs over them. Who owns which command is in TOOL-BOUNDARIES.md.
The engine was written in memo and moved here as it was, so a mailbox memo imported goes on where it stopped: the same account, the same ids, the same sync state.
MailSource has the shape of Gmail over IMAP, the first mailbox it served. Where a source has no such thing it leaves
the member out.
| Member | What the adapter does |
|---|---|
list(since, folder, { headers?, searched? }) |
every message in folder received since since, all or nothing, since deletions are proved from it; headers also reads From, To, Cc and the list fields; searched limits it to those UIDs |
quick?(since, folder, { searched }), identify?(raw, item) |
for a server that gives no message id without reading the message (no OBJECTID, no Gmail extensions): search answers quick: true, quick lists the UIDs of searched with flags, date and size only, and identify gives the messageId and threadId that list would have given, from the bytes fetchRaw returned |
fetchRaw(items) |
each item's raw RFC 5322 bytes, in order; a message that cannot be read is an Error in its place, never a failed call |
search?(since, folder) |
the folder's UIDs in the window and its UIDVALIDITY, without reading a message; absent for a file, which has no UIDs |
stamp?() |
a file's size and modification time, so an unchanged file read to the end is not read again |
find?(messageIds, folder, { headers? }) |
the messages with these provider ids, listed as list lists them; a message no longer there is left out. Absent where messages have no provider id |
move?, present?, label?, folders? |
moving between folders, which UIDs a folder still holds, putting labels back, and the All Mail and Trash folders under the account's language, with on IMAP the sent folder: the one the server marks \Sent (RFC 6154), else one by a common name when the server marks no folder. Used by cleanup and restore, not by the sync, except folders |
An item is a Listed: uid, messageId (the provider's id, the same in every folder and across a UID reset),
threadId, receivedAt, and when the source knows them folder, labels, seen, flagged, size and headers.
A source with no provider id derives messageId from the Message-ID header, and from the folder, UIDVALIDITY and UID
when there is none.
Reads each folder of options.account (mailFolders gives them: the folders it names, INBOX and the source's
sent folder for mode: "imap", else All Mail by its role), saves each new message with store.mail.saveThread, and
returns a MailSyncResult
(listed, saved, alreadyStored, skipped, envelopes, deleted, complete, pass, coverage, and when there
are any unreadable, attachments, deletionsSkipped, tagsReused, tagsSkipped, indexed). Every message is the
account { provider: "email", account: <address, lower case> }.
- The owner's mail. An email is
outgoingwhen it is in the source'ssentfolder or under Gmail'sSentlabel, whatever its From, or when its From is one of the owner's addresses: the account's, theaddressesthe owner set onoptions.account(aliases), and those stored undermail_own_addresses. After the save the senders of the live mail in Sent join that list, the account's mail from any of them becomes outgoing, stored earlier or not (store.mail.markOwnMail), and the account is saved again, solinkOwnIdentitiesjoins their identities to the owner. Partial completion,readEmailWholeand a server search read the same list. A mail tie needs mail the owner wrote, so this is what gives the people an alias writes to their tie. Nothing is taken from a name. - Stored headers. An email stored in full keeps some header fields as JSON in
emails.headers(mailHeadersOf,src/mail/headers.ts):list-id,list-unsubscribe,precedence,reply-to, Gmail'sspf,dkimanddmarc, and what marks a program or a list. Those last areauto-submitted(its value, unlessnoorauto-replied, RFC 3834),list-unsubscribe-post(one-click, RFC 8058) andfeedback-id. The sender writes every header itself, so they are kept only when a DKIM signature Gmail passed covers them and the signature's domain has the From's registrable domain (signedFieldsOf). Gap: only Gmail's ownmx.google.comstamp is read, so mail of an IMAP server never keeps these three; another server's Authentication-Results may be the sender's own. An empty Return-Path, which the receiving server writes, is kept asnull-return-pathunless the mail is an auto-reply: a person's out-of-office comes that way too. Mail stored before these keys keeps none of them until read again. - Unsubscribe targets.
list-unsubscribestaystruewhenever the field is present, signed or not: the mail types and the bot rules read it as a mark of bulk mail. The targets themselves are kept apart, aslist-unsubscribe-httpsandlist-unsubscribe-mailto(the first of each scheme in RFC 2369's list), only when the same signature coversList-Unsubscribe: a target is where an unsubscribe goes, so a forged one is never offered.store.mail.subscriptionsreads them; withlist-unsubscribe-postand an https target the unsubscribe is one-click (RFC 8058, which requires both fields signed). Nothing in cli-core unsubscribes. Mail stored before cli-core kept the targets has none until read again. - Quick folders. A folder whose
searchsaysquickis not listed with headers: the sync lists the UID, flags, date and size of every message in the window, and reads the raw bytes of the newestmaxnot done and not located (a budgeted run:fetchBatcha round). It takes the id from the Message-ID, so stored mail keeps its id and the same message in two folders is one email. Rules and the bulk classification then run on the headers of those bytes; a skipped or envelope mail is read once and never stored in full, and is done afterwards. - Locations. For such a folder every stored email keeps where it is, in
email_locations(folder, UIDVALIDITY, UID; two folders, two rows), written in the same transaction as the email. The folder's resume point says from which date on that holds (located). A folder is quick when its point matches the UIDVALIDITY and the rules and itslocatedcovers the window, or when the store holds none of its mail. Otherwise (mail stored before locations existed, a new UIDVALIDITY, other rules,rescan, a wider window) it is listed once with headers, which recognises stored mail without reading it and records the locations, and the next run is quick. A new UIDVALIDITY drops the folder's old locations. A quick run refreshes the flags of every located message the listing shows, and proves deletions as a header listing does: located mail in the window (without its first two days) whose UIDsearchno longer lists leaves that folder, and is deleted when no other location of it is left and the run saw it nowhere. A run that read the window in parts (a budget, a retry pass) or whose too large a share would go proves nothing. - Rules. A skip rule keeps a new message out; an envelope rule records its sender, date, size and labels and never
its text; both come from
account.rules, decided bydecideover the facts of the listing (factsOf, which also works out a sender score when a rule asks). - Deletions. Stored mail the listing no longer holds is marked deleted, in the window without its first two days
(IMAP's SINCE counts whole days) and only when the run listed everything.
tooManyGonerefuses 5 or more and over a fifth of what is stored, and a snapshot source (mode: "mbox") never proves one. - Refused SEARCH.
search,queryandpresentthrow aCliErrorwith reasonmail.search-refused(SEARCH_REFUSED) when the server answers SEARCH with NO or BAD. The sync leaves that folder unread and keeps reading the others; the run is partial (refusedFolders), proves no deletion, changes no membership of the refused folder, moves none of its resume point and leavesmail_coverageas the last run set it. - Partial emails. A server search reads a candidate up to
SERVER_BOUNDS.textBytes(fetchRaw(items, { bytes })); one cut short is stored with its headers and text, no attachments, andmetadata.partial. After its rounds a sync (unlesscompletePartial: false) reads up to 100 of them whole (completePartial,src/mail/partial.ts): Gmail's by id withfind, elsewhere at a stored location, kept only whenidentifygives the stored id. It saves them as a sync would and clears the mark; the result haspartial: { completed, left }. The mark is a JSON key, not a column: no store version, and an older build simply leaves the email partial. - Limits.
maxnew messages a run, newest first (200; no limit when budgeted);maxBodyChars(MAX_BODY_CHARS); attachment text up toMAX_ATTACHMENT_CHARS(200,000); a run stops between batches of 50 onsignal, after saving its resume point. - Attachments.
attachments(MailAttachmentPolicy, defaultDEFAULT_ATTACHMENT_POLICY: on, 10 MB a file) says whether a sync reads attachments into the index and the largest file it reads. A file the policy leaves is stored with its name, type and size, no text, andextractionErrornot-read(policy off) ortoo-large; the run'sattachmentslog says the same. No file is kept either way. The email still arrives whole from the server: the limit keeps a large file out of the text readers and the index, not off the network. Sync, partial completion andretryAttachmentsfollow the policy. - One email read whole.
readEmailWhole({ store, source, address, externalId, folders, … })reads one stored email from its source again, as partial completion does, and every attachment with no size limit. It saves the email as a sync would (keeping its mailboxes, flags and rule) and answers each attachment with its bytes; it writes no file and runs noindexstep, so the new text gets vectors at the next sync.mailReadCommandstakes it aswhole. - Transport failures. A chunk in which every message failed stops the run, after saving its resume point; one unreadable message costs only itself and is tried again next run.
budget. ARunBudget(runBudgetOfin a command) makes the run budgeted (WC-LIFE-16): it reads the window in rounds, one request of the budget per call to the source (search,list,fetchRaw),source.listBatchandsource.fetchBatchmessages per call (200 and 50 when the source does not say), saves its resume point after every round and stops when the budget says so. The result hasstate(partialorcomplete),stopped,remaining(messages of the window not asked for) andrequests;maxdefaults to no limit. A message the source did not return fromlist, or answered with an Error fromfetchRaw, is asked for once more at the end of the run; if it still fails it is inunreadableand not in the resume point, so the next run asks again. A pass that read the window in parts proves no deletion (deletionsSkippedsays so), except when one round listed all of it. The source spends the budget on its own retries through thecanRetryit was given. A store another process has locked past its 5 s busy timeout (memo's scheduled sync does) is waited out: 2 s, 4 s, ... up to 5 times, each a request of the budget, and the round is read again; the slices earlier saved stay saved.retryAttachments. Reads again, by provider id throughfind, only the stored mail whose attachment a missing reader left unread (RETRYABLE_ATTACHMENTS). It lists nothing and moves no resume point.index(store, key). Runs after the save; what it answers isresult.indexed. The engine embeds nothing itself: memo passes its embedding here.ages. Domain registration dates known so far, for a rule that asks for a sender score.
searchMail(store, runAs, { text, mailAccount | accounts, … }) is search messages over mail: the shared query
language, with list: for a mailing list, and mail from a demoted sender after the rest unless plainOrder or
newest. showMail(store, locator, { before, after, full }) is one email and its thread neighbours with the text of
each attachment, cut at ATTACHMENT_CHARS unless full. memo's search mail and messages show and mail-cli's
commands call these two, so the answers match.
@wirecat/cli-core/mail/cli mounts the commands on a mail CLI: mailReadCommands({ app, read }) gives search messages, review, messages show and conversations list|show. read(command, work) opens the shared store for one run and
names the profile's mailbox; every command reads that mailbox alone and refuses another one's reference. A thread is a
conversation, chat:email/<address>/<thread>; conversations list filters on subject, emails_count and
last_email_at in SQL (THREAD_COLUMNS). The store keeps text, not the RFC 5322 source, so there is no --raw.
With whole(command, store, mailbox, emailId), messages show becomes a fetch: before it answers it reads again an
email whose attachment a sync's policy left (too-large, not-read). When that read fails it shows the stored email
with meta.complete: false and an issue mail.attachments-unread carrying the failure's code.
review is the messengers' review over the stored mail (reviewMail, src/mail/review.ts): a thread is a chat, an
email a message, and the answer has the same items, meta.complete and --new points (checkPoints, one file per
mailbox and reader: the owner, mcp-review, schedule-<name>-review under <PREFIX>_SCHEDULED). It reads the store
only and opens no task. Threads are picked by store.mail.threadActivity over email_threads.last_email_at, so no
email index is needed; it cuts at mail_coverage.until, the start of the last finished sync, so mail a running sync
has not stored yet is not passed. A complete --new run moves the mailbox's start to its end and drops the thread
points behind it.
Whose turn a thread is, owed or waiting, is mailTurn (src/mail/owed.ts), the one rule review --unanswered,
--waiting and tasks agenda over an email account share: owed when the newest email that is not a draft came in and
names one of the owner's addresses in To or Cc (the account's, or one under mail_own_addresses); waiting when the
owner wrote it. Mail sent to many counts too (the owner's ruling: look at all mail). Each review item's email carries
bulk, list_id, rule_id and the sender score instead, and the query term bulk:false leaves out threads whose
newest email is bulk, a list's or an envelope (isBulk).
A holder — an agent or a pipeline — keeps how far it processed an account's mail in the shared store, key
consumer:<holder> under the account in sync_cursors (src/mail/marks.ts); purging the account clears it with the
mail. The mark counts by emails.id, the store's insert order, so mail stored late with an old date is still after
it. review --holder <name> reads up to 200 emails past the mark, and the held ones now whole, and gives a cursor
(<last id>.<read time>); it moves nothing. Each email it gives also carries email.cursor and
email.findings_key, the cursor and key that end the batch at that email, so a reader that shows only part of the
batch acknowledges no email it did not show: it takes the pair of the shown email with the highest cursor id, and
the next read starts after it. Held emails end the batch at the mark with a key of their own,
<holder>:<mark>-h<highest held id>, since the run that set the mark holds the plain key. messages acknowledge --holder <name> --cursor <cursor> moves the mark
there, never back; it only adds to the holder's own bookkeeping, so a read-only scheduled run may run it
(destroysNothing). Only the owner moves a mark back: messages reset --holder <name> with --since-time <time>
(past every email received since then) or --last <n> (past the last n emails the holder was given), and
--dry-run to count first (rewindMark). It answers how many emails are read whole again, how many bulk ones come
again as excerpts and how many are skipped again. Every reset counts on the mark (resets), one that leaves the
mark where it is too, so the holder's next findings key is new. review --holder gives the mail-findings.add step
only when it gives emails. It is no proposal, so the read-only lock, a scheduled holder and an
agent are refused (mail.reset-owner-only). Envelopes and drafts are skipped and counted. Mail sent to many (the bulk mailbox or a List-Id)
goes through newsletterSorter (src/mail/newsletter.ts): words in the subject and the first lines, in Russian,
English and Spanish, the sender's address and an emoji or unsubscribe link score it as a newsletter, which is skipped
as newsletter; an order word in the subject, an order's or a request's number or a reply keeps it. What is kept
comes after the other mail as its subject and first lines (excerpt_only), at most 30 a read: the read ends before
the 31st, so the next read gives it. A host sets the words, the score and the limits from its config file through
MailReads.bulkTriage (MAIL_BULK_SETTINGS, bulkTriageOf), and passes the same triage to mailWaiting and
advancePastSkipped. Measured 2026-11-05 on the owner's last 90 days of bulk mail (10 252 emails, 0.05 ms each): 1 335
left out as newsletters, none of the 442 site lead forms among them; of 200 sampled newsletter verdicts 199 were
newsletters, and the one GitHub reply among them is kept by the reply rule now. A local embedding model (e5-small) found about 430 more newsletters
in 30 days at about 95% precision, for 10 ms an email, 1.2 s to load and 1 GB, and a labelled set to compare with, so
the sorter has no model. The mark itself counts what it passed by the plain verdicts: bulk mail, excerpt or not, adds
to skipped.bulk. A partial email is held until it is whole, or skipped after 7 days; one that changed after the read is held once more,
so the holder sees it at least once. A holder with no mark starts from the newest stored email. Reads stop at mail a
running sync stored, which files bulk mail only at its end. marksOf lists the marks, for status.
mailInboxCommand(mail, { modelsCommand, keyring, fetch }) gives a mail CLI inbox (src/mail/cli/inbox.ts, the
logic in src/mail/inbox.ts). It sends the threads that changed since their last sort to the shared AI model
(aiTextReader, the ai section the models ai commands write) and keeps one verdict per thread: answer (needs an
answer), read or later, with a one-line reason in the owner's first owner_languages (ownerLanguageOf), and
the name and address of the newest email's sender who is not the owner. No agent harness runs: an agent is refused (mail.inbox-agent)
and reads review itself. inboxDue is reviewMail with a point per sorted thread and noBulk, so mail sent to
many is never sent; a thread whose newest email is the owner's drops its verdict without a call. Each thread goes as
its subject and its 3 newest emails, each mailOwnText cut to 1500 characters, 10 threads a request. planInbox
keeps a run within --max-messages (40) and --max-tokens (60000, estimated at four characters a token before
anything is sent). A model off this computer is refused under local only and models chats denied, and asks under
ask (--yes, or chat-sending.ask when nobody can answer, whose details carry emails, threads, tokens, host,
model and estimated_cost_usd so a caller words the question itself); modelsCommand names the CLI that has those commands,
memo for mail. An answer is checked as stages suggest checks its own: refs from the batch only, a known bucket, a
reason; a thread without a usable verdict comes again next run. The mailbox's start moves only when nothing due was
left out. The answer's meta.model_run says what the run did: threads sent, sorted (done) and waiting for the next run
(left), the tokens the provider counted, cost_usd from the price list (costOf; 0 on this computer, null with no
price) and the model's endpoint. review adds each thread's verdict as email.inbox.
All under the account, in store.sync (syncState, setSyncState):
| Key | Holds |
|---|---|
mail_resume:<folder> |
a ResumePoint: the folder's UIDVALIDITY, the UID ranges done, from when its mail is located (located), the hash of the rules they were decided under, the widest full listing and its time, and the widest window ever listed. A later run lists only the UIDs not done, while the point's full listing is under a day old (FULL_PASS_MS) and the window is at least resumeFrom (5,000) UIDs; a new UIDVALIDITY, other rules or rescan list everything again |
mail_resume:mbox |
a MboxPoint: the file's size and modification time, and whether every message was read |
mail_folder_scope |
the mode and folders of the last run; a change skips deletions once |
mail_own_addresses |
the owner's other addresses on the account, lower case: the configured ones and the senders of mail in Sent |
mail_coverage |
the folders, window and whether the last run was complete |
mail_sender_scores |
when scoreSenders last ran, its version (SCORE_VERSION) and the senders it ranks lower |
consumer:<holder> |
a holder's processing mark: throughId, throughAt, held, read, skipped by reason |
mail_inbox |
inbox: where its next run starts and each sorted thread's bucket, reason, model, sender and newest email time; at most 500 threads of the last 30 days |
Mail sent to many is in the mailbox memo:bulk (BULK); mail an envelope rule recorded is in a mailbox of kind
envelope named by the rule (envelopeBox). Neither is a folder, so neither proves a deletion.
scoreSenders(store, address, ages, now, rules) scores each sender of the account from the mail the store holds, at
most once a day or when SCORE_VERSION changes, and keeps the scores with store.mail.setSenderScores, where search
reads them; demotedSenders(store) gives those whose mail ranks lower. ages is a DomainAges: known() and
lookUp(domains). How a domain's age is looked up and cached is the caller's.
import { CONTRACT_MAIL, fakeMailSource, mailSourceContract, mailSyncContract } from "@wirecat/cli-core/mail/testing"
for (const { name, run } of mailSourceContract(() => makeAdapterOver(CONTRACT_MAIL))) it(name, run)
for (const { name, run } of mailSyncContract(async () => ({ source: makeAdapterOver(CONTRACT_MAIL), store: await openStore(...) })))
it(name, run)mailSourceContract checks ids, the window, headers, search against list, fetchRaw in order with an Error for
a message it cannot read, find and present. mailSyncContract runs syncMail over the adapter and a real store:
each message once under the address's account, and nothing on a rerun. fakeMailSource(mailbox) is a Gmail-shaped
mailbox in memory that tests change between runs and make fail (searchFails, headersFail, moveFails,
unreadable); fakeArchiveSource is a snapshot source, as an mbox file is. cli-core runs the contract over both,
and the sync tests (src/mail/sync.test.ts) over the fake.