| 1 | # Coordinator email through Keymail |
| 2 | |
| 3 | Status: blocked on upstream contract/access, 2026-11-13. This is an integration |
| 4 | proposal, not an available email feature. No mailbox, transport, credentials, |
| 5 | database migration, worker or UI is enabled by this change. |
| 6 | |
| 7 | ## Evidence and exact blocker |
| 8 | |
| 9 | Checked the reachable resources before choosing an implementation: |
| 10 | |
| 11 | - MCP resource and resource-template discovery both returned empty lists. |
| 12 | No Keymail backend or mail-service API is exposed by the tools in this step. |
| 13 | - Repository searches for Keymail find sign-in configuration in |
| 14 | `internal/equipo/setup/session_handlers.go` (`auth.New`, `KeymailServers`) |
| 15 | and `github.com/keymaildev/signin v0.1.1` in `go.mod`. Neither establishes a |
| 16 | mailbox or two-way delivery contract. |
| 17 | - `README.md:50-53` describes CARLOS SMTP credentials for **sign-in mail**. |
| 18 | These must not be reused for this feature. |
| 19 | - Existing “connector” contracts concern model providers and token scheduling, |
| 20 | not Keymail delivery. |
| 21 | - The coordinator's repository lookup found only Docs' OAuth client, not a |
| 22 | Keymail backend. That is supplied investigation evidence, not a fresh backend |
| 23 | inspection here. No Docs code was changed. |
| 24 | |
| 25 | We cannot verify Keymail's mailbox capabilities, authentication scopes, inbound |
| 26 | sender guarantees, delivery semantics or test access. This does **not** establish |
| 27 | that Keymail lacks those capabilities. No guessed endpoints or live probes were |
| 28 | used. The unblocker is a Keymail maintainer supplying a versioned service contract, |
| 29 | an identified backend/repository owner and authorized sandbox access, with fixtures |
| 30 | for the capabilities below. Mailbox provisioning and real sending require separate |
| 31 | explicit authorization even after that contract is available. |
| 32 | |
| 33 | ## Proposed upstream contract, not discovered API |
| 34 | |
| 35 | Ask Keymail to confirm or provide: |
| 36 | |
| 37 | 1. An explicitly provisioned coordinator identity with stable mailbox ID, verified |
| 38 | sending domain, inbound address and revocation. Display name must disclose |
| 39 | “Equipo coordinator”; never impersonate the person. Equipo stores the mapping |
| 40 | of mailbox, coordinator, team and person. Provisioning is an operator-approved |
| 41 | operation, not an agent tool. |
| 42 | 2. Submission restricted to that mailbox, to one verified recipient, with immutable |
| 43 | content and a client idempotency key. Reusing the key with different content |
| 44 | must conflict. Supply retention guarantees, provider message ID, RFC Message-ID, |
| 45 | correlation headers, structured temporary/permanent errors, Retry-After and |
| 46 | a lookup-by-key operation for ambiguous submission outcomes. |
| 47 | 3. Authenticated inbound and delivery events with stable IDs, mailbox and message |
| 48 | IDs, signed timestamp, key rotation, replay window, retry/ack semantics and |
| 49 | reconciliation cursor. Specify signature coverage of raw bytes and envelope, |
| 50 | not just user-controlled headers. Events include accepted, delivered, deferred, |
| 51 | permanent bounce and complaint. Acceptance must not be labelled delivery. |
| 52 | 4. An explicit sender-verification assertion binding a Keymail authenticated |
| 53 | subject to its currently verified address, audience, message and mailbox. |
| 54 | Signing a webhook or reporting SPF/DKIM/DMARC does not by itself bind an |
| 55 | arbitrary From address to an Equipo person. If this assertion is unavailable |
| 56 | for external senders, hold their messages for confirmation in an authenticated |
| 57 | Equipo session before importing them. Do not treat a reply token as identity. |
| 58 | 5. Separate least-privilege credentials for sending from one mailbox and reading |
| 59 | its events, plus rotation/revocation and quotas. No domain administration, |
| 60 | arbitrary-recipient sending or access to the person's other mail. Provide |
| 61 | bounded MIME fixtures and attachment retrieval semantics without arbitrary |
| 62 | URL fetching. |
| 63 | |
| 64 | **Required Keymail work is conditional:** if these facilities exist, provide |
| 65 | documentation and sandbox access; otherwise implement only the missing agreed |
| 66 | capabilities. There is no evidence here to claim a particular backend change is |
| 67 | necessary, or to estimate one as completed. |
| 68 | |
| 69 | ## Equipo ownership and conversation handoff |
| 70 | |
| 71 | The new `internal/coordinatormail` package is an isolated, pure policy prototype. |
| 72 | It refuses unconfigured/unapproved outbound, unverified or third-party recipients, |
| 73 | opt-outs, suppressed recipients, undisclosed identity, mismatched reply scopes and |
| 74 | unverified/automatic replies. Its inputs are trusted server-resolved facts, not |
| 75 | JSON request fields. It is **not** a verifier, persistence layer or runtime guard |
| 76 | until a reviewed adapter integrates it. Tests exercise those refusal boundaries. |
| 77 | |
| 78 | Leave ongoing conversation-thread work in `internal/equipo/conversation` alone. |
| 79 | Its `EnsureThread` already joins task creation's transaction and `RouteMessage` |
| 80 | has requester and mutation checks. Do not introduce another task/thread store or |
| 81 | call RouteMessage from email. Agree this interface with that work's owner before |
| 82 | wiring it: |
| 83 | |
| 84 | - Resolve a stored opaque reply-route hash to mailbox, team, person, verified |
| 85 | address, coordinator, project, task and existing conversation/thread ID. Bind |
| 86 | the original outbound Message-ID too. References/In-Reply-To must match that |
| 87 | stored route; subject text never selects a task. Ambiguity, expired routes, |
| 88 | deleted tasks and lost access go to quarantine, never automatic task creation. |
| 89 | - Append an **untrusted external discussion** entry to that thread, atomically |
| 90 | with the inbound event receipt, using a deterministic mutation ID. The thread |
| 91 | owner must supply a non-executing append boundary; do not assume SendMessage |
| 92 | is non-executing. Until agreed, no inbound adapter is enabled. |
| 93 | - Retain source provenance and provider/event IDs. From, quoted/forwarded text, |
| 94 | attachments and model interpretations never grant authority. Even a verified |
| 95 | sender does not approve spending, permissions, execution, deployment or other |
| 96 | sensitive actions by email. Existing authenticated approval gates remain. |
| 97 | |
| 98 | ## Durable delivery design required before enabling |
| 99 | |
| 100 | Use Equipo's SQL store, not an in-memory queue. Keep a separate mail delivery |
| 101 | ledger from device notification acknowledgments. Existing integration points are |
| 102 | `internal/equipo/notifications/outbox.go`, its person-scoped Event model and |
| 103 | migration `0031_notifications.sql`. Do not alter their device cursor semantics. |
| 104 | |
| 105 | In a transaction with selection of an eligible source event, insert: |
| 106 | |
| 107 | - delivery ID; unique `(team, person, source_event, channel)`; immutable scope, |
| 108 | content hash, preference revision and sender identity; provider idempotency key; |
| 109 | - state, attempt count, next-attempt time, lease owner/expiry, provider IDs, |
| 110 | timestamps and sanitized error code; |
| 111 | - an append-only audit entry containing actor, decision, scope and correlation ID, |
| 112 | never tokens, raw MIME or message bodies. |
| 113 | |
| 114 | States: queued -> submitting -> accepted -> delivered. Temporary failures become |
| 115 | retry_wait; permanent failures become failed; bounces become bounced; complaints |
| 116 | or opt-out become suppressed. Unknown submission outcome becomes uncertain, not |
| 117 | a fresh send. Lease claims use compare-and-swap and fencing. After a crash, query |
| 118 | by the **same** key before resubmitting identical content. If upstream cannot |
| 119 | resolve uncertainty or its idempotency retention has expired, stop for operator |
| 120 | reconciliation, never retry with a new key. Exactly-once mail cannot be promised |
| 121 | without upstream guarantees. |
| 122 | |
| 123 | Proposed initial limits: five submission attempts total within 24 hours, delays |
| 124 | of 1 minute, 5 minutes, 30 minutes and 2 hours plus bounded jitter. Honor a longer |
| 125 | Retry-After only within the deadline, otherwise stop. Every provider attempt must |
| 126 | be recorded before I/O. Recheck verified address, membership, recipient consent, |
| 127 | mailbox authorization and suppression before each attempt. Address changes cancel |
| 128 | old queued mail rather than rewriting its recipient under the same key. |
| 129 | |
| 130 | Deduplicate status events and inbound receipts with unique |
| 131 | `(provider, mailbox_id, event_id)` constraints, plus a mailbox-scoped stable |
| 132 | provider message ID for repeated inbound events. Persist receipt, audit and |
| 133 | thread append atomically before acknowledging. Reject conflicting payload hashes |
| 134 | for the same ID. Apply status changes monotonically: a late accepted/delivered |
| 135 | event must not erase a bounce or complaint. Persist hard-bounce/complaint |
| 136 | suppression before scheduling further mail; soft bounces consume the bounded |
| 137 | retry budget. Re-enablement requires authenticated confirmation. |
| 138 | |
| 139 | Default preference is off. Authenticated person settings explicitly opt in per |
| 140 | coordinator/project and notification category, support quiet hours and frequency |
| 141 | caps, and can disable all email. Device subscriptions are not email consent. |
| 142 | Include a clear sender, project/task context, why the mail was sent and an |
| 143 | authenticated preference link. Never send to addresses supplied in prompts, |
| 144 | headers, forwarded messages, Cc or Bcc. This initial slice covers existing task |
| 145 | threads only. |
| 146 | |
| 147 | ## Inbound containment and loops |
| 148 | |
| 149 | Do not reply automatically to inbound mail. Filter coordinator-originated mail, |
| 150 | delivery reports, Auto-Submitted messages and bulk/list traffic. Suppress email |
| 151 | notifications for the imported message's source event, and do not allow that |
| 152 | event to trigger an automatic coordinator email response. Rate-limit by mailbox |
| 153 | and bound pending/quarantine storage. Bounce handling uses authenticated provider |
| 154 | events, not text that claims to be a bounce. |
| 155 | |
| 156 | Initially quarantine attachments, do not feed them to tools or models. Bound raw |
| 157 | MIME to 10 MiB, decoded aggregate to 10 MiB, part count to 20 and nesting to 5; |
| 158 | reject over-limit messages before expansion. Do not unpack archives, execute |
| 159 | files, load remote images or follow attachment URLs. Render plain text safely; |
| 160 | keep quoted/forwarded content labelled as untrusted context. A later explicit |
| 161 | attachment import must use `internal/attach` byte sniffing, filename cleanup, |
| 162 | image metadata stripping and limits, with private storage, access checks and |
| 163 | authenticated downloads. Malware scanning and retention/deletion policies must |
| 164 | be agreed before enabling attachment import. Logs must omit mail bodies and |
| 165 | credentials; raw quarantined mail requires restricted access and bounded retention. |
| 166 | |
| 167 | ## Release acceptance, beyond the offline policy tests |
| 168 | |
| 169 | Before opt-in is offered, run sandbox contract tests for: |
| 170 | |
| 171 | - Restart after provider acceptance but before local commit; concurrent workers; |
| 172 | expired leases; repeated source events; key/content conflicts; uncertainty |
| 173 | without lookup. Each produces at most one provider submission identity. |
| 174 | - Duplicate/reordered webhook events, lost acknowledgments, replayed/invalid |
| 175 | signatures, rotation, forged From and valid DKIM without subject binding. |
| 176 | - Cross-person/team/project/task routes, stolen reply token, subject spoofing, |
| 177 | revoked membership, changed address and ambiguous References. No new task. |
| 178 | - Atomic thread append/receipt rollback; retries append once and never execute |
| 179 | instructions. Approval-required actions still require the existing UI flow. |
| 180 | - Opt-out racing dispatch, hard/soft bounces, complaints, retry exhaustion, |
| 181 | rate limits, self loops and automatic replies. |
| 182 | - Oversized/nested MIME, malicious HTML, archives, remote URLs and quoted |
| 183 | instructions. No authority or automatic tool access from any content. |
| 184 | |
| 185 | This change implements only offline policy tests. Persistence, webhook verification, |
| 186 | preference UI, conversation handoff and all above end-to-end tests remain release |
| 187 | blockers. No real sending, provisioning, paid service or broader permission was used. |
| 188 | |