Sync to labels
What runs today, from a mailbox sync to a labelled email, and the state each step leaves in Postgres.
This is the path that is built today. Mail syncs and stores each email, then hands it to the worker through the email-processing queue, as Mail to processing describes. The worker’s listener extracts the email and converts its documents, then, while model calls are on, classifies it and links it to the enquiry and quotation it answers, and labels and reads it when it is an enquiry. Classification and reading send email text to Jev, and reading an enquiry’s categories also sends an excerpt of its documents, through TypeSafe’s own API with no zero-retention agreement yet, and reading sends the client’s contact block to openai/gpt-6-luna through the Vercel AI Gateway; pictures attached to an enquiry, and its scanned PDFs where they are allowed, go to the same model. pnpm flow runs the same steps by hand. The job carries only the email id; everything else the two sides share is Postgres rows and stored files.
The path
The inbox reads the same rows. It lists emails at PROCESSED, which show as ready with no status badge, and, to a caller who sees all mail, those at NEEDS_REVIEW. An email at EXTRACTION_PENDING is listed to no one. NEEDS_REVIEW shows as Needs review, and the Needs Review tab lists a conversation when any of its messages is at that status. An email at INGESTION_FAILED is listed in no view.
- The scheduler starts a sync of every active mailbox every 30 seconds in development and every 15 minutes in production, unless
MAIL_POLL_INTERVAL_SECONDSsets another interval. On start it returns any email stranded atINGESTION_PROCESSINGtoINGESTION_PENDING. - Enrolling a mailbox, or turning one back on, makes the API add a
sync-mailboxjob for it, so it syncs as soon as the sync queue is free rather than at the next scheduled scan. Adding it while one for that mailbox is still queued does nothing. When Redis is unreachable the API logs a warning, the enrolment still succeeds, and the scheduled scan picks the mailbox up. - A sync reads the mailbox’s Graph delta pages from its saved delta link. Each new email is staged in one transaction: its thread, its
email_messagerow atINGESTION_PENDINGand a delivery for the mailbox. The new delta link is saved once the pages are read, and each staged email gets an ingest job carrying its id, tried up to three times. - With no saved link, a sync starts a new delta round over the Inbox at the mailbox’s sync-from time, newest first, and reads it in pages of 50 to the end. That time is when the mailbox first synced, unless the mail debug page moved it back, so a new mailbox takes only mail that arrives from then on.
MAIL_SYNC_MAX_MESSAGES_PER_PASSmakes each sync read one page of that many items and save the link to the next, so a window is walked a page per sync. - Ingestion claims the email, downloads its raw message and stores it as
mail/raw/<sha256>.eml, and each file attachment asmail/attachments/<sha256>, in the local folder or S3. It then records the raw message and attachment rows and moves the email toEXTRACTION_PENDING. A raw message missing on every delivery, or a last attempt that fails, leaves itINGESTION_FAILED. - Once those rows are committed, mail adds the email’s
process-emailjob to theemail-processingqueue. The job carries only the email id. Adding a job for an email that already has one waiting does nothing, and adding one while it runs queues a single follow-up. A failed add is logged, not retried, and the worker’s sweeper finds the email.
Worker
The worker runs the same steps in two ways, and both save through the same writers.
- The listener starts with
pnpm dev, orpnpm --filter @flowos/worker devon its own. It takes jobs from theemail-processingqueue, two at a time unlessWORKER_CONCURRENCYsets another number. For each email it starts the pipeline run, extracts the email, saves its display body, records its origin and converts its documents.- The display body is what the inbox’s reading pane shows: the email’s HTML with its embedded images inlined, every other image removed along with its alt text, and anything that could run or submit removed, or its plain text when it has no HTML. Remote images are never loaded, and an embedded image over 512 KB, or past 4 MB for the whole email, is left out. It is stored beside the raw message as
mail/bodies/<sha256>.html, andemail_messagerecords its key inbody_refwith the sender’s name and the To and Cc addresses. Each stored attachment the body shows as an image is markedembedded; one too large to inline is not, so it stays an attachment. The references extraction found, such as our quotation numbers, purchase orders, the client’s enquiry or job numbers and reference-shaped subject tokens to confirm, replace the email’s rows inemail_referencein the same step. It is saved before the origin, so a copy has one too, and with model calls off. A retry keeps a body an earlier attempt saved, with its recipients and references. An email with no body at all keeps an emptybody_ref. A picture the body shows is not a document and is never converted. Each document gets 30 seconds to convert, and the job log names any document that took 5 s or more, or timed out, by its sha256 prefix. - The origin is the client behind any forward, or quoted below a reply of ours, addressed only to us, whose note says nothing. An email that adds nothing to one already recorded from the same client, received within 30 days of it and at the same office, is a copy, unless a forwarding colleague’s note adds words of its own. A copy is linked
SAME_ORIGINto the first recorded, which on a newest-first sync can be the later email, its run endsDONE, and nothing else runs, not even document conversion. Any other email is processed normally; one from the same client under the same subject within seven days is also linkedSUSPECTED. How the origin is found, and the whole copy rule, are on Enquiry reading. - It then classifies the email, reusing a classification saved earlier unless the job is marked
fresh. A reply of ours, addressed only to us, whose note says nothing is classified on the client’s words quoted below it. A forward of a whole conversation with no note of its own is typed by its newest message that moves the stage. An email whose kind is unsure waits in Needs Review (R7) unless the next step links it by a quotation. The job then anchors the email: it finds the enquiry, and the sent quotation version, the email answers (R97), saves the link, and hands a linked client email to theclient-changesandpurchase-ordersqueues its kind routes to, as Worker jobs describes. An enquiry email that answers a quotation is saved as a revision request. Any kind but an enquiry, and a revision request so made, is finished there: its classification is saved, again when it was reused, and it is neither labelled nor read. Labelling calls no model: it splits the email and its documents into addressable lines and rows, and tags signatures, disclaimers and firm references by rule. An email classified as an enquiry first has each attached picture it can read (JPEG, PNG, WebP or GIF, and not one the body shows) transcribed byopenai/gpt-6-lunathrough the Vercel AI Gateway, and the transcription is saved as a document marked as read from a picture; a picture already transcribed is reused by its sha256, one that fails or comes back empty is listed as not read and tried again when the email is processed again, andpnpm flownever transcribes. A scanned PDF attached to an enquiry is transcribed the same way, and marked as read from a scan, when every mailbox the email was delivered to is one requirement gathering covers. It is then read for its catalogue categories, what the client asks of the offer and by when, and the client’s contact, with a Jev call per catalogue category for the categories, one for the asks, and one call toopenai/gpt-6-luna, as Enquiry reading describes. Saving the reading, not the labels, finishes an enquiry’s run. The job then routes the email to an office and Office Head (Inbox), and links it to an enquiry, its conversation’s or a new one, when the reading confirmed a category; with none confirmed it links no enquiry and marks the email for review. For a linked enquiry it then starts requirement gathering: an enquiry that has requirements keeps them and the email is marked processed with nothing gathered, and any other is queued onenquiry-requirementswhen requirement gathering is switched on and covers the email’s mailboxes; one read while it is paused is queued by the sweep once it resumes. Model calls are off by default: the mail debug page’s switch turns them on, orWORKER_MODEL_CALLS=onwhile the switch is unset. Off, it stops after the documents, with no picture read, and the run staysRUNNINGuntilpnpm flow, or the job the sweeper re-adds once model calls are on, classifies and labels it, and reads it when it is an enquiry. When TypeSafe or the AI Gateway rate limits a call, the job holds the whole queue for a minute and goes back to wait without using one of its three tries. - A raw email it cannot read, or a classification call that fails, fails the job. A display body it cannot write is logged and the email processed on, with its recipients and references saved;
pnpm backfill:bodiessaves the body later. The queue tries each job three times, backing off between tries. A reading call that fails does not fail the job: its part falls back to its rules and is saved aspartial, and the job’s log marks the reading partial. TheREADstep names Jev only when neither the categories nor the asks fell back to their rules. Each part’s fallback is on Enquiry reading. - A job for an email that mail has deleted, or is ingesting again, does nothing.
- An enquiry entered by hand is read like an email, with these differences: it is never a copy, the customer the person gave stays as the enquiry’s client, its owner and office stay the person’s choice, and it starts gathering even when its reading confirms no category.
- The display body is what the inbox’s reading pane shows: the email’s HTML with its embedded images inlined, every other image removed along with its alt text, and anything that could run or submit removed, or its plain text when it has no HTML. Remote images are never loaded, and an embedded image over 512 KB, or past 4 MB for the whole email, is left out. It is stored beside the raw message as
- The sweeper runs every five minutes, once across all worker processes, and once more straight away when the mail debug page switches model calls on. It re-adds a job for each email at
EXTRACTION_PENDINGwith no pipeline run, which covers a hand-off mail lost and the backlog when the worker first starts. With model calls on, it also re-adds an email whose run is stillRUNNINGand has no job on the queue: at once when it was never classified, such as one that stopped after the documents while model calls were off, and otherwise once its run has not changed for 30 minutes, such as one whose classification failed all three tries. It does so at most three times per run. The same sweep also queues gathering for read enquiries still without requirements, linked client mail still waiting for its readers, and earlier mail whose fetch is complete, as Worker jobs describes. An email whose job is waiting, retrying or running is never re-added or counted, however long it waits, and a re-added job that stops after the documents because model calls were switched off does not use up one of the three. An email still being ingested is left to mail, which hands it off once it is stored.- On a later sweep, a run already swept three times that is idle again by the same rule, with no job on the queue, is retired. The sweeper sets the email to
NEEDS_REVIEWwith the errorCould not classify after 3 attemptsand marks the runFAILED. The email and its artifacts stay. Retirement runs whether or not model calls are on; a run only reaches three sweeps while stalled retries are on. A classification that misses softly, with no probabilities or below the threshold, finishes in Needs Review asunsure_kind(R7) and is not retired. An email whose run finished but that never linked to an enquiry, such as one whose link failed to save, is not the sweeper’s: when its job fails its last try, the job sets it toNEEDS_REVIEWwith the errorCould not link after 3 attempts. A rate limit the job waits out, earlier mail fetched for another email and an email quoted in a forward never go to review this way.
- On a later sweep, a run already swept three times that is idle again by the same rule, with no job on the queue, is retired. The sweeper sets the email to
pnpm flow, run from the repository root, is a terminal menu. Each step works on the emails picked and saves what it makes, unless a dry run is on.
| Step | Reads | Saves |
|---|---|---|
| Pick emails | Emails at EXTRACTION_PENDING, NEEDS_REVIEW or PROCESSED, with their saved classification |
Nothing |
| Extract | The raw message, through the blob reader | With writes on, the email’s display body in mail storage, its body_ref, sender name and recipients, then its origin on email_message, a CONFIRMED SAME_ORIGIN link for a copy and SUSPECTED links to the same client and subject within seven days; later steps skip a copy |
| Extract documents | Each attachment’s file, converted to Markdown | One PREPARE step per attachment set, one DOCUMENT_EXTRACTION per file |
| Classify | The extracted text, sent to Jev after asking | A CLASSIFY step, a CLASSIFICATION artifact, and the email’s current kind in email_classification |
| Label | Text and documents of a classified enquiry; rules only | A LABEL step and a LABELLED_CONTENT artifact |
| Read enquiries | Labels, classification and the item group master; Jev and the contact model after asking, or by rule alone: the category vocabulary, asks by their keywords, and the contact’s rule fields | A READ step and an ENQUIRY_READING artifact, which finishes an enquiry’s run |
- “Newest not finished yet” offers emails with no
DONEpipeline run. - A file converted before, for any email, is reused rather than converted again, and an outbound email’s files are not converted.
- An image is not converted, since the document reader reads no image format, so
pnpm flownever sends a picture to the reader, though it reuses a reading already saved, which is marked as read from a picture. Pictures are read only by the listener, and only for an enquiry. A picture that fails to read with a retryable error other than a timeout is asked once more straight away; one that still fails is listed as not read, and the job log names it by its sha256 prefix with the reason. It is tried again only when the email is processed again, which today means a developer reprocesses it. The step counts images apart from files the reader rejected:2 of 21 documents converted (image 19). - Every step reloads the picked emails first and drops any that mail has deleted or is ingesting again. Label rereads the saved classifications and labels only emails classified as an enquiry. Read rereads them too, and reads only emails classified as an enquiry, skipping one whose classification call failed; it counts any with a part read by rule alone because its model call failed, and See results shows a reading’s due date, when it has one, and marks it partial when its categories or asks fell back to their rules or its contact call failed. Read sends contact blocks to the contact model only alongside the classifier’s model, so without one the setup note says contacts are read by rule. Run everything left asks before reading only when an enquiry may be read, and skips Read when nothing was labelled.
- A dry run saves nothing. Before writes come back on, its results are either saved, in stage order, or discarded, so a saved label never rests on an unsaved classification or document. A dry run still lists body pictures as images until their body is saved.
pnpm backfill:bodies, run from the repository root, saves the display body, recipients and references of every stored email that has no body, such as those processed before bodies were saved; with--allit saves every stored email’s again, as after the body rendering changes. It reads and extracts each raw message again, 50 at a time, asks before writing unless given--yes, calls no model, and ends with how many it saved, how many had no body, and the ids of any that failed.pnpm status, run from the repository root, prints each mailbox’s sync, the stored emails by status and how many emails were set aside as copies, what the pipeline saved in the last hour, theemail-processingqueue’s counts and the latest ten emails with their kind. It only reads.
Email status and run state
Mail moves an email up to EXTRACTION_PENDING. After that, the email’s status mirrors its pipeline run:
| Pipeline run | Email status | When |
|---|---|---|
| None | EXTRACTION_PENDING |
Mail has just stored the email |
RUNNING |
EXTRACTION_PENDING |
The listener started it, a document or classification is saved, an enquiry’s new labels are saved, or the latest classification failed |
DONE |
PROCESSED |
A classification of anything but an enquiry is saved and no stage’s latest attempt failed, or requirements were gathered from an enquiry |
DONE |
EXTRACTION_PENDING |
An enquiry’s reading is saved; the email waits for its requirements to be gathered |
DONE |
NEEDS_REVIEW |
An enquiry’s reading confirmed no category, or gathering its requirements failed for good |
DONE |
PROCESSED |
A copy of an email already recorded |
FAILED, after three stalled sweeps |
NEEDS_REVIEW |
The sweeper took the email off the queue for review |
| Deleted, with its steps and artifacts | Email deleted | A sync reset purges the email |
- A run is finished once a classification of anything but an enquiry, or an enquiry’s reading, is saved after its latest document, or when its email is a copy. A new document, a new classification, or an enquiry’s new labels reopen it. A classification of another kind saved again, as when a new document reopened the run, finishes it again; an enquiry’s run is finished by reading it again.
- A classification whose model call failed is saved as a
FAILEDstep. The run stays open, and the email stays on the “not finished” list, until a later classification succeeds or the sweeper retires it. - The worker never changes an email that is still being ingested.
markPipelineRunmirrors a run onto an email atEXTRACTION_PENDINGorPROCESSED:DONEmoves it toPROCESSED, except an enquiry no requirements were gathered from, which waits atEXTRACTION_PENDING, andRUNNINGmoves a processed email back unless requirements were gathered from it. A run whose latest attempt at any stage failed staysRUNNING, unless its email is a copy. Saving a requirement set moves its email toPROCESSED, and so does keeping the requirements its enquiry already has.NEEDS_REVIEWis set by the sweeper, by a reading that confirms no category (unless its conversation already holds an enquiry, which it then joins as a follow-up,PROCESSED) and by a gathering job that fails for good; a job that picks the email up again returns it toEXTRACTION_PENDING.
Mail debug
/debug/mail is the operator view of this path, shown to callers holding debug:view (the Developer role).
- Waiting for worker is an email at
EXTRACTION_PENDINGwith no open pipeline run and no job the worker currently holds. Worker running is an email the worker holds, or one whose pipeline run is stillRUNNING. - A processed email shows its saved classification when one exists: Enquiry, Revision request, Quotation, Purchase order, Sales order, Follow-up, or Other. The reading pane leads with an enquiry’s reading, in sections: who forwarded it and their note; the categories it asks for, solid when present and dashed when unsure, each with its likeliest item group, with the client’s words that show them and the documents’ hints; the due date, marked as counted from when it was sent when the client wrote it relative, and whether the client calls it urgent; the terms the client asks for, grouped by type, with the attachments named as the client’s terms; and the client’s contact, marked “by rule only” when the model did not read its contact block and noting a PIN the model’s address disagrees with, or saying that no client was found, or, for a reading made before contacts were read, that the contact was not read. A reading whose categories were all named by the vocabulary is marked “vocabulary only”, and one where only some were “some categories by vocabulary”, such a category showing no probability. A category shows its likeliest item group only when Jev scored one at 0.2 or more. A reading whose asks were kept by their keywords is marked “asks by keyword only”, each such ask marked “keyword only”. The labelled content, with its sections and the lines that were tagged, is folded below the reading, and open when the email has no reading; until the worker has labelled the email, it says so. A finished run of anything but an enquiry shows its Read stage as Not an enquiry; an enquiry processed before reading was built shows it as not run, and an open run labelled again after its reading shows it as not read since. The message ids, deliveries and storage sit under Envelope and storage.
- The email type filter lists only emails currently of the kinds picked, and Not classified those the worker has not classified yet. It combines with the search, direction, mailbox and tab filters. An email’s kind is its current classification, so one classified again moves with it.
- A finished email of any kind but an enquiry shows its Label and Read stages as
Not an enquiry. - Emails that share a thread stay together. The thread is placed by its latest activity, and the messages inside it run oldest first. A long thread can still split across pages.
- A copy shows its Documents, Classify, Label and Read stages as done, each naming the first email by the first eight characters of its id:
Copy of 0190aaaa — not processed. This needs the first email inside the viewer’s mailbox scope; a copy of an email delivered to another mailbox shows those stages as not run. - A forwarded email shows its origin, the client behind the forward, beside the sender:
(Origin: client@example.com). - Open in inbox on the selected email opens its conversation in the Global Inbox with that email selected. It is disabled for an email with no delivery, which is in no conversation.
- The Pipeline Progression Tracker shows, under each stage that called a model, its tokens and estimated cost:
12,340 in · 410 out · $0.0012. Every attempt of a stage counts, retries included, and the requirement phases are counted from the steps that produced their output. Documents counts the transcription of an enquiry’s pictures, and Verify requirements includes fluid matching. A call that failed, or whose result was already saved, is not counted. The cost is an estimate fixed when the step was saved, and is left off when the model has no known rate. A total for the email’s newest run sits at the foot of the card, marked partial when some calls have no cost. An email processed before usage was recorded showsCost not recorded. - Needs review lists emails at
NEEDS_REVIEW. That tab is shown while any email is in that state, or while the tab is selected. - Process again on the selected email, for an email of any kind, calls
POST /debug/mail/messages/:id/reprocess. The email moves toEXTRACTION_PENDINGwith its processing error cleared, and aprocess-emailjob markedfreshis queued; one still waiting for the email is markedfreshin its place, and one running is followed by it. The fresh job classifies the email again, reads and links it again, and queues a requirements job for the new reading that discards review and replaces the enquiry’s current requirements, removing an earlier finished job for that reading. That job is queued while requirement gathering is paused too, and waits there; a job for the reading still waiting is changed to replace, and one running without replacing, or starting as it is changed, is left to finish, its log saying to process the email again. An email no longer classified as an enquiry finishes as its new kind, keeping its enquiry link and requirements. The action is disabled while model calls are paused. An email mail is still storing answers 409, an enquiry email delivered to a mailbox gathering does not cover answers 409 withREQUIREMENTS_MAILBOX_NOT_ALLOWED, an enquiry entered to have its lines built by hand answers 409 withBUILT_BY_HAND, and a caller withoutinbox:editon an enquiry whose current requirements hold review work answers 403. - Syncing From, in the sync drawer, shows each mailbox’s sync-from time, with a days box and Sync from here beside it. Confirming calls
POST /debug/mail/sync/mailboxes/:id/startwith{ days }, 0 to 365: the time moves to that many days before now and the cursor is cleared, so the next sync pulls the mail received since, newest first, and then only new mail. 0 means from now. Mail already held is not stored twice. It answers 409 while that mailbox is syncing, and needsmailbox:editat ALL scope. - Clean all mail data deletes every email, with its threads, deliveries, links and pipeline runs, and every enquiry, quotation, purchase order and enquiry sales order, in one transaction (
POST /debug/mail/cleanup). Mailboxes, parties, masters, ERP sales orders and the number counters stay, and every mailbox then syncs from now. It is held back until mail sync is paused, answers 409 while a mailbox is syncing, takes the typed phrasedelete all mail data, needsmailbox:editat ALL scope, and then clears completed and failed jobs from the mail and processing queues. It locks the mail tables first, so a worker write made meanwhile waits and then fails; a job still waiting for a deleted email fails when it runs. Stored mail files are left in storage. Take a database snapshot before using it on a shared environment.
Resets
pnpm --filter @flowos/mail sync:reset puts a mailbox back to an earlier state, and settles the worker’s runs to match. A pipeline run has no foreign key to its email, so the reset does this itself.
- Soft reset and complete wipe delete the emails they purge, together with their pipeline runs, steps and artifacts. A copy of a purged email that survives loses its link, so its run is reopened and a processed copy goes back to
EXTRACTION_PENDING, for the worker to read as an email of its own. - Enquiries entered by hand are left alone by every reset mode.
- Force re-ingest sends the mailbox’s emails back to
INGESTION_PENDINGand reopens their runs. Once each is stored again, mail hands it to the listener again. Their artifacts are kept, so documents converted before are reused.
Not built yet
- The child-process pool. The listener runs each job in its own process, not in the pool that Worker jobs describes.
- Scaling on backlog. The sweeper reports no backlog metric, and nothing starts more workers.
- A zero-retention agreement. The email text and document excerpts the listener sends to Jev are kept under TypeSafe’s default terms, and the contact blocks and pictures sent through the Vercel AI Gateway under its default terms and those of the provider it routes to, OpenAI or Amazon Bedrock today.
How each layer reads an email is on Processing layers.