Repository navigation
feat(email): rules turn inbound mail into owned actions (EA-3) - #637
Open
privatejoel wants to merge 1 commit into
Open
privatejoel wants to merge 1 commit into
privatejoel wants to merge 1 commit into
Conversation
Migration 252 work_items (the WM-5 subset) and 268 email_actions, email_rules and email_action_ensure (one transaction, unique for life on tenant, message key and action type). Deterministic rules with facts and inferences apart, exclusions, stated deadlines and business-hour due times. Links only on a reliable match: exact contact or customer address, a business domain one customer uses, a PO number as a whole token. Follow- ups update the open action. Observe mode is the default and writes nothing. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This was referenced Oct 23, 2026
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
EA-3 of the email accountability plan (#634, sections 6.2 to 6.5). The sync engine turns business-critical inbound mail into owned actions. It uses deterministic rules and no LLM.
Stacked on #636 (EA-2) ← #635 (EA-1) ← #619. Merge them in that order.
work_items. This is theWORK_MANAGEMENT_SCOPE.md4.2 table, decision ED-1. The names and types are the same as in the design, withoutitem_key, the template columns andcustom. WM-5 adds those later withadd column if not exists. An email action istype 'task'withsubject_type 'email_action'. Thesource_keyis unique while the item is open. RLS is on with no policy for tenant users, because the items carry mail subjects.email_actions: the email side of one action. It holds IDs and short metadata only, with no body.email_rules: rules for the tenant, or rules for one user.email_action_ensure(): makes the work item and the email action in one transaction. The key(tenant_id, message_key, action_type)is unique for life. Thus these cases never make a second action: a replayed page, a reset checkpoint, and a second mailbox that got the same message. A second mailbox is recorded inseen_connection_ids. Only the service role can execute the function.tenant_settings.email_actions_mode. The default isobserve: the engine writes nothing and counts whatcreatewould make. The logistics monitor's first run made about 2,000 wrong rows, and this setting prevents that.tenant_settings.email_content_retention_days, for the EA-8 purge.email-accountability/rules.jsis a pure function that has no network and no database. It gives:Precedence: bulk, Auto-Submitted, out-of-office and bounce subjects, no-reply senders, internal senders, and exclusion rules. A trusted-sender rule lets an internal sender through.awaiting_review. It is never ignored and never goes straight into an owner's queue.due_source 'customer_commit'). The forms are "by 25 Oct", "by tomorrow", "within 3 working days" and "before 05/11/2026". Otherwise the policy gives critical 4, high 8 and normal 16 business hours ('sla'). A deadline rule can override this for one action type.link.js: a link is made only when the match is reliable. The cases are: the exact contact address (canonical hash), the exact customer address, a business domain that only one customer uses, and a PO number that appears as a whole token on that customer's open order. A free-mail domain or a shared domain gives a reason and no link.ilikepatterns are escaped. The sharedmatchInboundToCustomeris not used, because it linksgmail.com.store.jsis the only module that knows an action is awork_itemsrow plus anemail_actionsrow. If ED-1 changes, only this file changes.engine.jsis theonPageof the sync:detectedwith no owner.last_sync_stats: created, would_create, excluded, irrelevant, follow_ups and reassigned.inbound_emails, so it never reaches the tick's LLM classifier or the draft-order step.Deploy notes
notify pgrst, 'reload schema';.email_actions_modestaysobserve. Leave it there until EA-5 (verification) is merged, because without EA-5 no action closes by itself. Before you switch, readlast_sync_stats.would_createon the mailboxes. To switch:update tenant_settings set email_actions_mode = 'create' where tenant_id = '<id>';.Tests
src/v3-app/api-email-rules.test.js(33 tests, pure):nopasses), out of office, no-reply, internal, an exclusion rule by domain and by phrase, and a trusted sender that lets an internal sender through.senderMatchesmatches a domain and its subdomains.src/v3-app/api-email-engine.test.js(21 tests). It uses the memory fake and a stand-in foremail_action_ensurewith the same semantics:detectedcases are covered too._in a domain is not a wildcard.inbound_emails,communicationsandquote_approvalsstay empty.runEmailSyncand the engine, makes one action, and the mailbox stats count it.SQL on a real Postgres engine. No Postgres runs on this machine, and stacked PRs get no CI. So I applied
supabase/ci-bootstrap.sqland all 233 migrations to PGlite 0.5.8, a WASM Postgres, with pgvector 0.0.9. This is whatscripts/db/apply-migrations.shdoes in CI. I installed PGlite in a scratch folder, outside the repo. Then I ran these checks onemail_action_ensure:created = false,seen_connection_idshas 2 entries).source_key = 'email_action:' || idandsubject_id = id.source_keyraisesunique_violation.authenticatedcannot execute the function, andservice_rolecan.All checks passed.
Mutation check: I broke each rule, ran the two files and restored the code. 21 of 23 mutations failed the tests. The 2 that survived are equivalent: when a match is not reliable,
linkSenderreturns nocustomer_id, and a request from an unknown sender is irrelevant either way. The killed mutations:ilike;Two mutations first survived and showed real gaps, which I fixed:
Gates:
npm run check: pass.npm run lint: 0 errors. The new files add no warnings.npm run build: pass.npm run verify: pass. I restored the unrelated rewrites ofdocs/RBAC_AUDIT.mdanddocs/WRITE_PATH_AUDIT.md.npm run eval:golden: pass.NODE_OPTIONS=--no-experimental-webstorage npx vitest run: 629 files, 7,553 tests, all pass.Not covered
observeuntil then.holiday_calendar. Many Indian distributors work on Saturday.zonedToUtcuses one offset step, so a time inside a DST change can be one hour off. India has no DST.migrationsjob runs when this stack reachesmain.🤖 Generated with Claude Code