Skip to content

Isolate provisioned teams that share an organization ESP #220

Done

Description

@s3rot0n1n

Why

Provisioned teams (for example, each school on a course platform) send through the organization's shared ESP and its single From domain. Bounces, complaints, suppressions, quotas and automatic pauses are already per team. Two things are still shared:

  1. Mailbox-provider reputation. Gmail and Microsoft score mail by its DKIM signing domain, so every team on the shared From domain shares one reputation.
  2. ESP account standing. ESPs enforce bounce and complaint limits on the whole account (SES reviews at about 5% bounces / 0.1% complaints). One team with a bad list can get the shared account paused, which stops every team.

Team-owned ESPs stay the full-isolation option. This issue adds the middle tier (shared ESP, per-team domain), guards the shared account, and shows the per-team numbers to the organization. It stays provider-neutral, and IP pools stay out of scope (ADR 0005).

Subtasks

1. Guard the shared ESP account

  • Rolling 7-day bounce and complaint rate for each organization-owned ESP, summed across its teams, from the outbound ledger.
  • Thresholds from config, set below what ESPs enforce (for example, warn at 2% / 0.05%, act at 4% / 0.08%).
  • When over the line, pause marketing for the teams causing it, ranked by their share of bounces and complaints. Pause marketing on the whole shared ESP only if the rate is still over after that.
  • Small teams on a shared ESP: catch them with a lower floor or absolute counts, not only rates after 500 accepted sends. Transactional (API) sends count too.
  • Let self-hosted operators turn the team and ESP checks on for organization ESPs (today the team check only runs in cloud).

Done when: one team mailing a bad list ends with that team paused, the other teams still sending, and the shared ESP's rate held under the ESP's own limit.

2. Show per-team health to the organization

  • Provisioned-team and usage responses include the reputation status (normal / warned / marketing_paused / all_paused, with the reason) and 7-day sent, bounce and complaint figures.
  • Org mail activity reads the outbound ledger, so it covers campaigns and complaints, not only transactional mail.
  • MCP tools, openapi.json and the headless provisioning docs updated.

Done when: one provisioning call shows each team's health and quota.

3. Hold only the team that made a flagged import

  • assertNoImportUnderReview is scoped to the team, so a flagged import holds only that team's campaigns. The org-wide new-account ramp stays as it is.

Done when: a flagged import at team A leaves team B's broadcasts sending.

4. Per-team sending domains on the shared ESP

  • Data: sending_domains.team_id, plus from_email and sending_domain_id on esp_config_team_grants. Additive only, so the previous release keeps working.
  • Provider adapters (register / records / verify / remove) using a separately stored, encrypted management credential on the org ESP. SES first, then SendGrid, then Resend and Postmark, then Mailgun (which needs an SMTP login per domain). Plain SMTP stays manual. SES is built (Isolate teams on a shared organization ESP (#220) #226); SendGrid, Resend, Postmark and Mailgun move to Team sending domains for SendGrid, Resend, Postmark and Mailgun shared mailboxes #227.
  • Provisioning API: PUT / GET / DELETE /provisioning/teams/:teamId/sending-domain, returning the DNS records to add and the status.
  • Sending: use the team's from_email only while its domain is verified and DKIM and DMARC pass. If it later fails, give a grace period, then pause that team's marketing mail. Never quietly fall back to the shared domain.
  • Org policy requireTeamSendingDomain (off / marketing / all), default off, plus an optional monthly cap on sending through the shared domain.
  • Team sending domains added to the Postmaster Tools sync.
  • Dashboard: a team sending-domain dialog following the domain authentication dialog, and its status in the org's team list.

Done when: a provisioned team enters a domain, adds the records, and its next broadcast is signed d= its own domain, while other teams are unaffected.

Also

  • ADR: "Isolating teams on a shared organization ESP" (items 1 and 4). docs/decisions/0013-isolate-teams-on-a-shared-organization-esp.md

Out of scope: IP pools or dedicated IPs, changes to team-owned ESPs, one-click DNS setup (Domain Connect / Entri). Revisit one-click setup if teams drop off at the DNS step.

🤖 Generated with Claude Code

Activity

  1. s3rot0n1n commented on Nov 25, 2026

    @s3rot0n1n
    ContributorAuthor

    Progress on branch t3/school-team-provisioning-isolation (local commits, not pushed yet):

    • 1. Guard the shared ESP account: 3beb013. Also fixes a deadlock in shared-mailbox quota locking: reserving and committing a send locked the usage buckets in different orders. A stress test against real Postgres went from hundreds of deadlocks to none.
    • 2. Per-team health: ef9cd5b. GET /provisioning/teams/:id and /usage return health (status, reason, scope, 7-day sent, bounced and complained counts). GET /organizations/:id/mail-activity now reads the outbound ledger (campaigns plus transactional, with complaints) and carries each team's health and quota, so one call covers every team. These routes stay REST-only by design (MCP parity lists them as restOnly), so no MCP tool changed.

    Both pass a local browser smoke. The QA smoke runs once for the whole issue, after the release is deployed to QA.

  2. s3rot0n1n commented on Nov 25, 2026

    @s3rot0n1n
    ContributorAuthor

    Subtasks 1–3 are deployed to QA (codelit/sendlit:qa-issue-220-04b323a, codelit/sendlit-web:qa-issue-220-04b323a, arm64, built from 04b323a) and pass an e2e browser smoke there. Nothing is merged or pushed yet.

    The QA smoke used the existing "Pro Team 1" organization (Pro plan) and its "Mailpit QA" shared mailbox, with three teams granted to it:

    • 1. Guard: "prola 1" sent 20 transactional emails and 6 were marked bounced. The guard paused only that team on that mailbox (shared_esp_team_breach), and no other organization or mailbox on QA was affected. Its broadcast was refused with the shared-mailbox message, its transactional email still went out, and the owner got "Marketing email paused for prola 1". After an operator release (billing CLI), the owner got "Marketing email resumed for prola 1" and its broadcast delivered.
    • 2. Health: Organizations → Activity shows each team's health, its 7-day sent, bounced and spam counts, and its quota for today.
    • 3. Import hold: an import of 1,100 contacts was flagged against "prola 2" only (scope: team). Its broadcast was refused with import_under_review, while "QA smoke 220" in the same organization sent its broadcast (20 emails, all in Mailpit). The organization is 86 days old, so its real flag was medium. To exercise the hold I raised that flag to high for the test and set it back to medium afterwards.

    Bug found and fixed during the QA smoke (04b323a). This was a deadlock that predates this branch. Each send's outbound insert takes a foreign-key share lock on the team's grant, and the quota reservation then locked that same grant FOR UPDATE, so every broadcast through a shared mailbox deadlocked and paused itself. The fix locks the grant FOR NO KEY UPDATE instead. On real Postgres, 199 of 200 concurrent sends deadlocked before the fix and none of 4,000 after.

    Subtask 4 is next.

  3. moved this from Backlog to In progress in SendLit Roadmapon Nov 25, 2026
  4. s3rot0n1n commented on Nov 26, 2026

    @s3rot0n1n
    ContributorAuthor

    Subtask 4 (teams' own sending domains on a shared mailbox) is on QA as pr-226-2345618 and passed an e2e smoke with a real Amazon SES account (ap-southeast-1):

    • The org owner saved an SES API key on the shared mailbox from the dashboard; SendLit checked it with GetEmailIdentity and stores it encrypted.
    • Registering news@schoola.clqa.site created the SES identity and showed 3 DKIM CNAMEs plus DMARC. Once DNS was published, the dialog showed 2 of 2 done.
    • Transactional email and a 20-recipient broadcast went out From news@schoola.clqa.site; the "Send via" label showed the team address.
    • With policy "Broadcasts and sequences", a team without its own domain had its broadcast refused with the right message, and its transactional email still sent.
    • Found and fixed on QA: team domains showed in the org's "Sending domains" list with Verify and Revoke (2345618). They are now left out, and org verify and revoke can't reach them.
    • "Stop using this domain" deleted the SES identity; the team then sent From the shared news@district.clqa.site again. The policy is back to "Nothing".

    The SES mailbox's SMTP host is QA Mailpit, so no email went through SES. The SES API calls were a handful of identity calls.

    Still open from this subtask: domain adapters for SendGrid, Resend, Postmark and Mailgun (SES only so far).

  5. s3rot0n1n commented on Nov 26, 2026

    @s3rot0n1n
    ContributorAuthor

    Review pass on the whole branch: removed duplicated and unused code and docs, and fixed what the review found (60268f0, on QA as pr-226-60268f0).

    Fixes:

    • SendLit never takes over or deletes an SES identity it didn't create. A domain that already exists in the SES account gets domain_exists_at_provider (409), and a team can't use the shared mailbox's own domain (domain_in_use).
    • When a team switches or removes its domain, the old SES identity is deleted only after the change is saved.
    • With more than one API replica, each due domain is claimed first, so it's checked once.
    • Provider errors return esp_provider_error (422) instead of an undeclared 502.
    • An operator's mailbox-wide release now applies to a team that was released earlier.

    QA smoke with real SES (ap-southeast-1):

    • A team using the shared mailbox's domain was refused.
    • news@schoolb.clqa.site was registered and verified (2 of 2 done).
    • A transactional email went out From news@schoolb.clqa.site. Email went to QA Mailpit, not through SES.
    • Removing the domain deleted the SES identity, with no errors logged.

    Migration 0036 was edited before release to drop two unused columns, so QA still has them, unused.

  6. s3rot0n1n commented on Nov 26, 2026

    @s3rot0n1n
    ContributorAuthor

    Code review findings fixed in 41513fb, deployed to QA as pr-226-41513fb.

    • Cancelling a team's grant (which also happens when the team is archived or the organization closed) or retiring the shared mailbox now removes the team's own domain from SES. Retiring a mailbox and deleting an organization also wipe the SES API key.
    • A team moved to another mailbox registers its domain there. A domain registered through the old mailbox is never used to send.
    • The SES API key can't be removed while team domains use it: 409 esp_management_credentials_in_use.
    • The shared-domain monthly cap counts only broadcast and sequence sends from the shared domain, queued ones included. Transactional email doesn't count.
    • A sequence paused because the organization requires a team domain now says so.
    • Import flags raised before flags were per team keep holding the whole organization.
    • Reading a team domain re-checks SES at most every 30 seconds.
    • The organization can add a domain a team used before. One a team still uses returns domain_in_use.

    QA smoke with real SES, with no DNS needed:

    • Registered news@schoolc.clqa.site for a team.
    • Removing the SES key was refused with 409, and the organization adding that domain got domain_in_use.
    • Cancelling the team's grant deleted the SES identity, with no errors logged.
    • The organization then added the domain (and revoked it again to clean up).
    • After re-granting the mailbox, the team's transactional email went out From the shared domain.
  7. s3rot0n1n commented on Nov 28, 2026

    @s3rot0n1n
    ContributorAuthor

    Done in #226 (merged as 4bb0b23). v0.0.0 is deployed to QA and the QA smoke test passed. The prod deploy of the same tag is still to come.

  8. moved this from In progress to Done in SendLit Roadmapon Nov 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions