carlos / platform Public

console: account-wide email pause at twice the plan (plans 4b)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev 9c76cd77ba7e2f9e48f49aebca8c908177007f0e parent e50223e
5 files changed, +318 −1
  • internal/console/mailcounts.go +9 −1
  • internal/console/plangracepass.go +5 −0
  • internal/console/planmail.go +103 −0
  • internal/console/planmail_test.go +198 −0
  • internal/mail/mail.go +3 −0
diff --git a/internal/console/mailcounts.go b/internal/console/mailcounts.go
index 25e9e6c47..6a8ee21fe 100644
--- a/internal/console/mailcounts.go
+++ b/internal/console/mailcounts.go
@@ -181,7 +181,7 @@ func (s *Server) resumeStaleCapPause(ctx context.Context, acct, app, sender stri
// untouched. The first two never auto-resume by design; the third must
// not, because resuming reactivates every key including the standalones
// and the app credential every instance still holds.
- if sn.Provisioned.PauseKind != mail.PauseKindCap || sn.PerInstance {
+ if (sn.Provisioned.PauseKind != mail.PauseKindCap && sn.Provisioned.PauseKind != mail.PauseKindPlan) || sn.PerInstance {
return sn, false, nil
}
pausedDay := sn.Provisioned.At.UTC().Format("2006-01-02")
@@ -220,6 +220,14 @@ func (s *Server) resumeStaleCapPause(ctx context.Context, acct, app, sender stri
// presents to the member as "email stopped working", which is the failure
// this whole mechanism is supposed to prevent someone else's tenant causing.
func (s *Server) enforceMailLimits(ctx context.Context, acct, app, sender string, now time.Time) (bool, string, error) {
+ // The account-wide plan pause (plans 4b) runs after the sender's own
+ // checks, whatever they decided: one sender's event can carry the
+ // account past twice its daily limit.
+ defer func() {
+ if _, err := s.enforceAccountMail(ctx, acct, now); err != nil {
+ log.Printf("console: plan mail %s: %v", acct, err)
+ }
+ }()
sn, _, err := mail.GetSender(ctx, s.cfg.Releases, acct, app, sender)
if err != nil {
return false, "", err
diff --git a/internal/console/plangracepass.go b/internal/console/plangracepass.go
index 9e126ab88..09704b34e 100644
--- a/internal/console/plangracepass.go
+++ b/internal/console/plangracepass.go
@@ -94,6 +94,11 @@ func (s *Server) planGraceAccount(ctx context.Context, acct *Account, now time.T
}
}
}
+ if s.cfg.MailProvisioner != nil {
+ if err := s.resumePlanMail(ctx, acct, now); err != nil {
+ log.Printf("console: plan grace %s: mail resume: %v", acct.Sqid, err)
+ }
+ }
return s.notifyPlanOwners(ctx, acct, states, now)
}
diff --git a/internal/console/planmail.go b/internal/console/planmail.go
new file mode 100644
index 000000000..0a6bc98fa
--- /dev/null
+++ b/internal/console/planmail.go
@@ -0,0 +1,103 @@
+package console
+
+import (
+ "context"
+ "fmt"
+ "log"
+ "time"
+
+ "github.com/carlosframework/platform/internal/mail"
+ "github.com/carlosframework/platform/internal/plans"
+ "github.com/carlosframework/platform/internal/usage"
+)
+
+// planMailPauseAt is the day's send count at which the account's mail is
+// paused (plans 4b, spec §5.6): twice the plan's daily limit, never above
+// the catalog's email ceiling. 0 means the plan sets no email limit.
+func planMailPauseAt(l *plans.Limits, ceilings plans.Limits) int64 {
+ if l == nil || l.EmailPerDay <= 0 {
+ return 0
+ }
+ at := 2 * int64(l.EmailPerDay)
+ if c := int64(ceilings.EmailPerDay); c > 0 && at > c {
+ at = c
+ }
+ return at
+}
+
+// enforceAccountMail pauses every sender of the account once its sends for
+// the UTC day reach planMailPauseAt. The pause does not wait for notice:
+// email's grace ends at 2x within the day, and sending reputation is
+// shared by every tenant (Paul, 2026-10-25). An app sender in per-instance
+// mode is left alone: it cannot be resumed automatically, and pausing it
+// would deactivate every instance's key.
+func (s *Server) enforceAccountMail(ctx context.Context, acct string, now time.Time) (bool, error) {
+ if s.cfg.PlanCatalog == nil {
+ return false, nil
+ }
+ a, err := s.store.AccountBySqid(acct)
+ if err != nil || a == nil {
+ return false, err
+ }
+ l, err := s.EffectiveLimits(a)
+ if err != nil || l == nil {
+ return false, err
+ }
+ at := planMailPauseAt(l, s.cfg.PlanCatalog.Ceilings)
+ if at == 0 {
+ return false, nil
+ }
+ sent, err := s.store.AccountSends(ctx, acct, usage.DayOf(now))
+ if err != nil || sent < at {
+ return false, err
+ }
+ apps, err := s.store.AppsIn(a.ID)
+ if err != nil {
+ return false, err
+ }
+ paused := false
+ for _, app := range apps {
+ senders, err := mail.ListSenders(ctx, s.cfg.Releases, acct, app)
+ if err != nil {
+ return paused, err
+ }
+ for _, sn := range senders {
+ if sn.Provisioned == nil || !sn.Provisioned.Active {
+ continue
+ }
+ if sn.PerInstance {
+ log.Printf("console: plan mail %s: %s/%s is per-instance; left to its own cap", acct, app, sn.ID)
+ continue
+ }
+ if _, err := mail.SetSenderPaused(ctx, s.claimMailStore(), s.cfg.MailProvisioner, acct, app, sn.ID, true, mail.PauseKindPlan, now); err != nil {
+ return paused, err
+ }
+ paused = true
+ s.audit(a, app, "ses", "mail.paused", fmt.Sprintf("%s: plan: %d of %d emails today, paused at twice the limit until the UTC day rolls",
+ mailAuditSubject(app, sn.ID), sent, l.EmailPerDay))
+ }
+ }
+ return paused, nil
+}
+
+// resumePlanMail lifts plan and cap pauses from an earlier UTC day on every
+// sender of the account. A paused sender sends nothing, so no SES event
+// would ever resume it; the hourly grace pass does.
+func (s *Server) resumePlanMail(ctx context.Context, acct *Account, now time.Time) error {
+ apps, err := s.store.AppsIn(acct.ID)
+ if err != nil {
+ return err
+ }
+ for _, app := range apps {
+ senders, err := mail.ListSenders(ctx, s.cfg.Releases, acct.Sqid, app)
+ if err != nil {
+ return err
+ }
+ for _, sn := range senders {
+ if _, _, err := s.resumeStaleCapPause(ctx, acct.Sqid, app, sn.ID, sn, now); err != nil {
+ return err
+ }
+ }
+ }
+ return nil
+}
diff --git a/internal/console/planmail_test.go b/internal/console/planmail_test.go
new file mode 100644
index 000000000..b12b8294f
--- /dev/null
+++ b/internal/console/planmail_test.go
@@ -0,0 +1,198 @@
+package console
+
+import (
+ "context"
+ "strings"
+ "testing"
+ "time"
+
+ "github.com/carlosframework/platform/internal/mail"
+ "github.com/carlosframework/platform/internal/plans"
+ "github.com/carlosframework/platform/internal/release"
+)
+
+var planMailNow = time.Date(2026, 8, 19, 12, 0, 0, 0, time.UTC)
+
+// addMailApp gives the ops account an app with a provisioned, active app
+// sender whose own daily cap (1000) is well above the plan's.
+func addMailApp(t *testing.T, fx *consoleFixture, app string) {
+ t.Helper()
+ ctx := context.Background()
+ if _, err := fx.st.CreateAppIn(OpsAccountID, app, "paul@keymail.dev", planMailNow); err != nil {
+ t.Fatal(err)
+ }
+ if _, err := mail.Declare(ctx, fx.rel, release.OpsAccount, app, strings.ToUpper(app)+"_SMTP", "noreply@x.com", "owner@example.test", planMailNow); err != nil {
+ t.Fatal(err)
+ }
+ if _, err := mail.ApplyGrant(ctx, fx.rel, release.OpsAccount, app, []string{"x.com"}, 1000, "tier:free", "tier", planMailNow); err != nil {
+ t.Fatal(err)
+ }
+ rec, etag, _ := mail.Get(ctx, fx.rel, release.OpsAccount, app)
+ rec.Provisioned = &mail.Provisioned{IAMUser: "u-" + app, KeyID: "AKIA" + app, Active: true, At: planMailNow}
+ if _, err := mail.Put(ctx, fx.rel, release.OpsAccount, app, rec, etag); err != nil {
+ t.Fatal(err)
+ }
+ p := fx.srv.cfg.MailProvisioner.(*mail.MemProvisioner)
+ p.EnsureUser(ctx, "u-"+app, mail.UserPath, []string{"arn:aws:ses:eu-west-1:1:identity/x.com"}, nil, []string{"x.com"})
+}
+
+// planMailFixture: plans on (free: 50 emails a day), two mail apps.
+func planMailFixture(t *testing.T) *consoleFixture {
+ t.Helper()
+ fx := newConsoleFixture(t)
+ fx.srv.cfg.MailProvisioner = mail.NewMemProvisioner("1")
+ fx.srv.cfg.PlanCatalog = examplePlanCatalog(t)
+ addMailApp(t, fx, "blog")
+ addMailApp(t, fx, "shop")
+ return fx
+}
+
+func sends(t *testing.T, fx *consoleFixture, app string, n int, day time.Time) {
+ t.Helper()
+ for i := 0; i < n; i++ {
+ if err := fx.st.RecordMailEvent(context.Background(), release.OpsAccount, app, mail.SenderAppID, day.UTC().Format("2006-01-02"), mail.EventSend); err != nil {
+ t.Fatal(err)
+ }
+ }
+}
+
+func senderState(t *testing.T, fx *consoleFixture, app string) *mail.Provisioned {
+ t.Helper()
+ rec, _, err := mail.Get(context.Background(), fx.rel, release.OpsAccount, app)
+ if err != nil || rec == nil || rec.Provisioned == nil {
+ t.Fatalf("mail record %s: %v", app, err)
+ }
+ return rec.Provisioned
+}
+
+func TestPlanMailPauseAt(t *testing.T) {
+ for _, c := range []struct {
+ limit, ceiling int
+ want int64
+ }{{50, 10000, 100}, {8000, 10000, 10000}, {50, 0, 100}, {0, 10000, 0}} {
+ if got := planMailPauseAt(&plans.Limits{EmailPerDay: c.limit}, plans.Limits{EmailPerDay: c.ceiling}); got != c.want {
+ t.Errorf("limit %d ceiling %d = %d, want %d", c.limit, c.ceiling, got, c.want)
+ }
+ }
+}
+
+func TestAccountMailPausesEverySenderAt2x(t *testing.T) {
+ fx := planMailFixture(t)
+ sends(t, fx, "blog", 60, planMailNow)
+ sends(t, fx, "shop", 40, planMailNow)
+ paused, err := fx.srv.enforceAccountMail(context.Background(), release.OpsAccount, planMailNow)
+ if err != nil || !paused {
+ t.Fatalf("enforceAccountMail = %v, %v, want a pause", paused, err)
+ }
+ for _, app := range []string{"blog", "shop"} {
+ if p := senderState(t, fx, app); p.Active || p.PauseKind != mail.PauseKindPlan {
+ t.Errorf("%s sender = active %v kind %q, want paused for the plan", app, p.Active, p.PauseKind)
+ }
+ }
+ acct, _ := fx.st.AccountBySqid(fx.sqid)
+ events, _ := fx.st.Events(acct.ID, "blog", 10)
+ found := false
+ for _, e := range events {
+ if e.Action == "mail.paused" && strings.Contains(e.Message+e.Detail, "100 of 50") {
+ found = true
+ }
+ }
+ if !found {
+ t.Errorf("no mail.paused audit naming the figures: %+v", events)
+ }
+}
+
+func TestAccountMailBelow2xLeavesSenders(t *testing.T) {
+ fx := planMailFixture(t)
+ sends(t, fx, "blog", 99, planMailNow)
+ if paused, err := fx.srv.enforceAccountMail(context.Background(), release.OpsAccount, planMailNow); err != nil || paused {
+ t.Fatalf("enforceAccountMail at 99 = %v, %v", paused, err)
+ }
+ if !senderState(t, fx, "blog").Active {
+ t.Fatal("paused below 2x")
+ }
+}
+
+func TestAccountMailWithoutACatalogUnchanged(t *testing.T) {
+ fx := planMailFixture(t)
+ fx.srv.cfg.PlanCatalog = nil
+ sends(t, fx, "blog", 500, planMailNow)
+ if paused, _ := fx.srv.enforceAccountMail(context.Background(), release.OpsAccount, planMailNow); paused {
+ t.Fatal("paused with plans off")
+ }
+ if !senderState(t, fx, "shop").Active {
+ t.Fatal("a sender paused with plans off")
+ }
+}
+
+func TestAccountMailSkipsPerInstanceSenders(t *testing.T) {
+ fx := planMailFixture(t)
+ ctx := context.Background()
+ sn, etag, err := mail.GetSender(ctx, fx.rel, release.OpsAccount, "shop", mail.SenderAppID)
+ if err != nil || sn == nil {
+ t.Fatalf("sender: %v", err)
+ }
+ sn.PerInstance = true
+ if _, err := mail.PutSender(ctx, fx.rel, release.OpsAccount, "shop", sn, etag); err != nil {
+ t.Fatal(err)
+ }
+ sends(t, fx, "blog", 100, planMailNow)
+ fx.srv.enforceAccountMail(ctx, release.OpsAccount, planMailNow)
+ if !senderState(t, fx, "shop").Active {
+ t.Fatal("a per-instance sender was paused")
+ }
+ if senderState(t, fx, "blog").Active {
+ t.Fatal("the app sender was not paused")
+ }
+}
+
+func TestPlanMailPauseResumesOnDayRoll(t *testing.T) {
+ fx := planMailFixture(t)
+ ctx := context.Background()
+ sends(t, fx, "blog", 100, planMailNow)
+ fx.srv.enforceAccountMail(ctx, release.OpsAccount, planMailNow)
+ acct, _ := fx.st.AccountBySqid(fx.sqid)
+ if err := fx.srv.resumePlanMail(ctx, acct, planMailNow.Add(time.Hour)); err != nil {
+ t.Fatal(err)
+ }
+ if senderState(t, fx, "blog").Active {
+ t.Fatal("resumed the same day")
+ }
+ if err := fx.srv.resumePlanMail(ctx, acct, planMailNow.Add(13*time.Hour)); err != nil {
+ t.Fatal(err)
+ }
+ if !senderState(t, fx, "blog").Active || !senderState(t, fx, "shop").Active {
+ t.Fatal("plan pause not lifted on the UTC day roll")
+ }
+}
+
+func TestPlanResumeLeavesOtherPausesAlone(t *testing.T) {
+ fx := planMailFixture(t)
+ ctx := context.Background()
+ if _, err := mail.SetSenderPaused(ctx, fx.rel, fx.srv.cfg.MailProvisioner, release.OpsAccount, "blog", mail.SenderAppID, true, mail.PauseKindRate, planMailNow); err != nil {
+ t.Fatal(err)
+ }
+ if _, err := mail.SetSenderPaused(ctx, fx.rel, fx.srv.cfg.MailProvisioner, release.OpsAccount, "shop", mail.SenderAppID, true, "", planMailNow); err != nil {
+ t.Fatal(err)
+ }
+ acct, _ := fx.st.AccountBySqid(fx.sqid)
+ if err := fx.srv.resumePlanMail(ctx, acct, planMailNow.Add(48*time.Hour)); err != nil {
+ t.Fatal(err)
+ }
+ if senderState(t, fx, "blog").Active || senderState(t, fx, "shop").Active {
+ t.Fatal("a rate or manual pause was lifted by the plan resume")
+ }
+}
+
+// The SES event path runs the account check: one sender's event pauses
+// every sender of the account.
+func TestSESEventTriggersAccountPause(t *testing.T) {
+ fx := planMailFixture(t)
+ sends(t, fx, "blog", 100, planMailNow)
+ if _, _, err := fx.srv.enforceMailLimits(context.Background(), release.OpsAccount, "blog", mail.SenderAppID, planMailNow); err != nil {
+ t.Fatal(err)
+ }
+ if senderState(t, fx, "shop").Active {
+ t.Fatal("an SES event at 2x left a sibling sender sending")
+ }
+}
diff --git a/internal/mail/mail.go b/internal/mail/mail.go
index b3919b835..b2453833e 100644
--- a/internal/mail/mail.go
+++ b/internal/mail/mail.go
@@ -83,6 +83,9 @@ type Provisioned struct {
const (
PauseKindCap = "cap"
PauseKindRate = "rate"
+ // PauseKindPlan is the account-wide pause at twice the plan's daily
+ // email limit (plans 4b). Like a cap pause it lifts on the UTC day roll.
+ PauseKindPlan = "plan"
)
// Standalone is a displayed credential for something not running on CARLOS.