mail: pause on the sender record, kinds kept
Platform mail submission plan, Task 1 (decision 8, test 8.13, gap G1). A sender's pause becomes its own field, mail.Sender.Pause, so it means the same once the app-level IAM user is reaped. The pause walk writes it, and while Provisioned exists (the overlap week) it also does everything it did before: the keys, standalones, owned environments and Provisioned's Active/PauseKind/At. A sender with neither Provisioned nor a switched app is still refused with ErrNotProvisioned. Every reader goes through mail.EffectivePause (the Pause field, else the legacy Provisioned reading): the day-roll resume, enforceMailLimits, the plan pause and its resume, the status reply, the settings card, the mint paths and the CLI printer. The kinds are kept, and per G1 a cap pause AND a plan pause lift at the day roll; rate, manual and per-instance do not. New package internal/mailwire holds the pure wire types the agent and the mailer will read (Pause, Submission, SenderView, key builders); the pause kind constants are defined there once. The status reply gains an optional "pause" block, top level and per sender; the CLI reads it and falls back to "active" and pause_kind from an older console. Census: no unit changed; the root (.) envelope is re-pinned for mail_cli.go. Overlay platform-mail-submission, claims in docs/superpowers/notes/census-claims-platform-mail-submission.tsv.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
20 files changed,
+949
−86
docs/superpowers/notes/census-claims-platform-mail-submission.tsv+9 −0docs/superpowers/notes/census-proposal-platform-mail-submission.md+11 −0internal/console/apimail.go+22 −6internal/console/apimail_test.go+52 −0internal/console/appsettings.go+9 −6internal/console/mailcounts.go+32 −34internal/console/mailcounts_test.go+74 −0internal/console/planmail.go+4 −2internal/mail/mail.go+21 −6internal/mail/pauseif_test.go+126 −0internal/mail/sender.go+45 −1internal/mail/sender_test.go+220 −0internal/mail/walk.go+47 −24internal/mail/walk_environment.go+3 −2internal/mailwire/mailwire.go+120 −0internal/mailwire/mailwire_test.go+73 −0internal/provision/testdata/plan14-root-reads-chain.tsv+1 −0internal/provision/testdata/plan14-root-reads-platform-mail-submission.tsv+3 −0mail_cli.go+33 −5mail_cli_test.go+44 −0
CI failed — run details
diff --git a/docs/superpowers/notes/census-claims-platform-mail-submission.tsv b/docs/superpowers/notes/census-claims-platform-mail-submission.tsv| new file mode 100644 |
| index 000000000..0e491fe48 |
| --- /dev/null |
| +++ b/docs/superpowers/notes/census-claims-platform-mail-submission.tsv |
| @@ -0,0 +1,9 @@ |
| +# CLAIMS for census-ruling-platform-mail-submission (plan |
| +# docs/superpowers/plans/2026-11-13-platform-mail-submission.md). One line per |
| +# changed root-read unit: <file>:<unit><TAB><what changed>. Cumulative: each |
| +# census-touching task adds its lines and reruns census-propose under the same NAME. |
| +# |
| +# Task 1 (pause on the sender record) changes no unit: `make census-card` names |
| +# none, and re-pins the root (.) envelope only, which census-propose takes |
| +# without a claim. The planned claim, kept here for the reviewer: |
| +# mail_cli.go:printEmailStatus: the `carlos email status` printer reads the pause from the reply's new optional `pause` block (kind, at), per sender too, and falls back to `active` and provisioned.pause_kind from an older console (emailPauseOf, a pure helper). apiEmailReply and apiEmailSender gain one optional field each. API mode only, run by a person against the console; no read of box state, no root read added |
diff --git a/docs/superpowers/notes/census-proposal-platform-mail-submission.md b/docs/superpowers/notes/census-proposal-platform-mail-submission.md| new file mode 100644 |
| index 000000000..aec7a48a2 |
| --- /dev/null |
| +++ b/docs/superpowers/notes/census-proposal-platform-mail-submission.md |
| @@ -0,0 +1,11 @@ |
| +# Census approval: platform-mail-submission |
| + |
| +Ruling `census-ruling-platform-mail-submission`. The independent reviewer approves or rejects every change below, 0 with a new disposition; nothing goes to the census owner (Paul, #280, 2026-10-26). |
| + |
| +Envelopes re-pinned: `.`. |
| + |
| +Review: a model from the other family than the author reads the branch's diff against main on the amadan.net remote (`origin` or `amadan`, whichever points at amadan.net) and gives a verdict for every unit and every re-pinned envelope above. For a unit, compare the change with its recorded disposition and basis below; for an envelope, read every changed function in the package and what it reaches. Reject if the change lets root open, run, query or follow anything it did not before from a path or input a tenant, an app or a box agent can create, write or name; if it weakens a check, refusal, validation, argument or environment the basis relies on; or if the diff cannot settle either. Otherwise approve. Record each verdict on #280 with the tip. A rejected unit goes back to the author to correct; nothing escalates to the census owner. |
| + |
| +Seal: `5e8a72175700a3e60c1e283b133c1ee25e36f292e2e319f02a19138f15186376` → `5e8a72175700a3e60c1e283b133c1ee25e36f292e2e319f02a19138f15186376`. |
| + |
| +Overlay: `internal/provision/testdata/plan14-root-reads-platform-mail-submission.tsv`, appended to `internal/provision/testdata/plan14-root-reads-chain.tsv`. |
diff --git a/internal/console/apimail.go b/internal/console/apimail.go| index 99bfd78d7..936796601 100644 |
| --- a/internal/console/apimail.go |
| +++ b/internal/console/apimail.go |
| @@ -433,11 +433,17 @@ func (s *Server) emailReply(ctx context.Context, acct *Account, app string, rec |
| if err != nil { |
| return nil, err |
| } |
| + // The pause is read through mail.EffectivePause (decision 8): a |
| + // record carrying it in its Pause field and a record from before |
| + // that field read the same. "active" keeps its old meaning (a |
| + // credential exists and is not paused) for an older CLI; "pause" is |
| + // the pause itself, omitted when there is none. |
| + pz := mail.EffectivePause(sn) |
| entry := map[string]any{ |
| "id": sn.ID, |
| "scope": sn.Scope, |
| "source": sn.Source, |
| - "active": sn.Provisioned != nil && sn.Provisioned.Active, |
| + "active": sn.Provisioned != nil && pz == nil, |
| "daily_cap": mailCapFor(sn.Grant), |
| "counts": map[string]any{ |
| "sends": sc.Sends, "bounces": sc.Bounces, |
| @@ -453,13 +459,17 @@ func (s *Server) emailReply(ctx context.Context, acct *Account, app string, rec |
| } |
| if sn.Provisioned != nil { |
| entry["key_id"] = sn.Provisioned.KeyID |
| - if sn.Provisioned.PauseKind != "" { |
| - entry["pause_kind"] = sn.Provisioned.PauseKind |
| + } |
| + if pz != nil { |
| + entry["pause"] = pz |
| + if pz.Kind != "" { |
| + entry["pause_kind"] = pz.Kind |
| } |
| } |
| senders = append(senders, entry) |
| } |
| - return map[string]any{ |
| + appPause := rec.EffectivePause() |
| + out := map[string]any{ |
| "app": app, |
| "declared": rec.Declared, |
| "grant": rec.Grant, |
| @@ -473,14 +483,20 @@ func (s *Server) emailReply(ctx context.Context, acct *Account, app string, rec |
| "pending_finish": rec.Provisioned != nil && rec.Provisioned.PrevKeyID != "", |
| "missing_vars": missing, |
| "daily_cap": mailEffectiveCap(rec), |
| - "active": rec.Provisioned != nil && rec.Provisioned.Active, |
| + "active": rec.Provisioned != nil && appPause == nil, |
| "counts": map[string]any{ |
| "sends": counts.Sends, "bounces": counts.Bounces, |
| "complaints": counts.Complaints, "rejects": counts.Rejects, |
| "deliveries": counts.Deliveries, |
| }, |
| "senders": senders, |
| - }, nil |
| + } |
| + // The app sender's pause (decision 8), omitted when it has none. Additive: |
| + // an older CLI reads "active" and provisioned.pause_kind as before. |
| + if appPause != nil { |
| + out["pause"] = appPause |
| + } |
| + return out, nil |
| } |
| // mailWalkReply is the shared tail of every walk door that hands back a |
diff --git a/internal/console/apimail_test.go b/internal/console/apimail_test.go| index 12b52d2e5..c21d43cda 100644 |
| --- a/internal/console/apimail_test.go |
| +++ b/internal/console/apimail_test.go |
| @@ -22,6 +22,7 @@ import ( |
| "github.com/carlosframework/platform/internal/appconfig" |
| "github.com/carlosframework/platform/internal/dnsrec" |
| "github.com/carlosframework/platform/internal/mail" |
| + "github.com/carlosframework/platform/internal/mailwire" |
| "github.com/carlosframework/platform/internal/release" |
| ) |
| @@ -1700,3 +1701,54 @@ func TestEmailReplyCarriesEverySenderAndKeepsItsTopLevelFields(t *testing.T) { |
| t.Fatalf("per-sender counts = %v / %v, want one send each", first["sends"], second["sends"]) |
| } |
| } |
| + |
| +// Decision 8: the status reply carries the pause as a block, top level for |
| +// the app sender and per sender, read through mail.EffectivePause, and |
| +// omits it for a sender that is not paused. "active" and pause_kind keep |
| +// their meaning for an older CLI. A Pause field alone (over a Provisioned |
| +// block that says active) is reported as paused: the reply reads the |
| +// field, not Provisioned.Active. |
| +func TestAPIEmailStatusCarriesThePause(t *testing.T) { |
| + fx, fake, _, token := emailArmed(t, "code-email-status-pause-block") |
| + app, domain := "titogo", "titogo.bab.oncarlos.com" |
| + provisionEmail(t, fx, fake, token, app, domain, "hello@"+domain) |
| + |
| + status, out := getJSON(t, emailURL(fx, app), token) |
| + if status != http.StatusOK { |
| + t.Fatalf("status = %d %v", status, out) |
| + } |
| + if _, ok := out["pause"]; ok { |
| + t.Fatalf("an unpaused app's reply carries a pause: %v", out["pause"]) |
| + } |
| + if _, ok := findSender(t, out, mail.SenderAppID)["pause"]; ok { |
| + t.Fatal("an unpaused sender's entry carries a pause") |
| + } |
| + |
| + ctx := context.Background() |
| + sn, etag, err := mail.GetSender(ctx, fx.rel, release.OpsAccount, app, mail.SenderAppID) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + sn.Pause = &mailwire.Pause{Kind: mail.PauseKindRate, At: time.Now()} |
| + if _, err := mail.PutSender(ctx, fx.rel, release.OpsAccount, app, sn, etag); err != nil { |
| + t.Fatal(err) |
| + } |
| + status, out = getJSON(t, emailURL(fx, app), token) |
| + if status != http.StatusOK { |
| + t.Fatalf("status = %d %v", status, out) |
| + } |
| + if out["active"] != false { |
| + t.Fatalf("active = %v, want false for a paused sender", out["active"]) |
| + } |
| + pause, _ := out["pause"].(map[string]any) |
| + if pause == nil || pause["kind"] != mail.PauseKindRate { |
| + t.Fatalf("pause = %#v, want a rate pause block", out["pause"]) |
| + } |
| + entry := findSender(t, out, mail.SenderAppID) |
| + if entry["active"] != false || entry["pause_kind"] != mail.PauseKindRate { |
| + t.Fatalf("sender entry = %#v, want inactive with pause_kind rate", entry) |
| + } |
| + if p, _ := entry["pause"].(map[string]any); p == nil || p["kind"] != mail.PauseKindRate { |
| + t.Fatalf("sender pause = %#v, want a rate pause block", entry["pause"]) |
| + } |
| +} |
diff --git a/internal/console/appsettings.go b/internal/console/appsettings.go| index 829ea82da..54902ae62 100644 |
| --- a/internal/console/appsettings.go |
| +++ b/internal/console/appsettings.go |
| @@ -742,12 +742,15 @@ func (s *Server) mailCard(ctx context.Context, acct *Account, app string) *MailC |
| card.KeyAge = humanAge(time.Since(pr.At)) + " ago" |
| } |
| card.PendingFinish = pr.PrevKeyID != "" |
| - // Active is the SENDING state, not the record's completeness — a |
| - // provisioned-but-paused app has a credential, just one that must |
| - // send nothing right now, which is exactly the sentence the page |
| - // has to make impossible to miss. |
| - card.Paused = !pr.Active |
| - card.PauseKind = pr.PauseKind |
| + } |
| + // The pause is the SENDING state, not the record's completeness — a |
| + // provisioned-but-paused app has a credential, just one that must send |
| + // nothing right now, which is exactly the sentence the page has to make |
| + // impossible to miss. Read through EffectivePause (decision 8), never |
| + // Provisioned.Active directly. |
| + if pz := rec.EffectivePause(); pz != nil { |
| + card.Paused = true |
| + card.PauseKind = pz.Kind |
| } |
| for _, sa := range rec.Standalone { |
| row := MailStandaloneView{Name: sa.Name, KeyID: sa.KeyID} |
diff --git a/internal/console/mailcounts.go b/internal/console/mailcounts.go| index 0f723ea2b..48c2cb0bf 100644 |
| --- a/internal/console/mailcounts.go |
| +++ b/internal/console/mailcounts.go |
| @@ -162,44 +162,42 @@ FROM mail_sender_counts WHERE account = ? AND app = ? AND day = ?` |
| // Both callers share this one function so they can never drift. |
| // |
| // Fail-safe by construction, matching "RATE AND MANUAL PAUSES NEVER |
| -// AUTO-RESUME" below: resumes ONLY when sn is currently paused (Active == |
| -// false), the pause is a CAP pause on a sender that is not PerInstance |
| -// (PauseKind == mail.PauseKindCap — a RATE pause, a PER-INSTANCE pause or a |
| -// kind-less manual/pre-existing pause, PauseKind == "", all fall through |
| -// untouched), and the pause was applied on a UTC day STRICTLY EARLIER than |
| -// now — same-day resumes would make the cap meaningless. |
| +// AUTO-RESUME" below: resumes ONLY when sn is currently paused, the pause |
| +// lifts at the day roll (a CAP or a PLAN pause, mailwire.Pause's |
| +// LiftsAtDayRoll — a RATE pause, a PER-INSTANCE pause or a kind-less |
| +// manual/pre-existing pause all fall through untouched) on a sender that is |
| +// not PerInstance, and the pause was applied on a UTC day STRICTLY EARLIER |
| +// than now — same-day resumes would make the cap meaningless. The pause is |
| +// read through mail.EffectivePause (platform mail submission decision 8), so |
| +// a record carrying it in its Pause field and a record from before that |
| +// field, carrying it in Provisioned, read the same. |
| // |
| // Returns the freshly re-read sender when it resumes (sn's SetSenderPaused |
| -// write moves Active, PauseKind and At, so callers must not patch the |
| -// in-memory copy by hand) and whether a resume happened; sn is passed |
| -// straight back, and resumed is false, in every other case, including |
| -// sn == nil or sn.Provisioned == nil. |
| +// write moves the pause, so callers must not patch the in-memory copy by |
| +// hand) and whether a resume happened; sn is passed straight back, and |
| +// resumed is false, in every other case, including sn == nil or a sender |
| +// that is not paused. |
| func (s *Server) resumeStaleCapPause(ctx context.Context, acct, app, sender string, sn *mail.Sender, now time.Time) (*mail.Sender, bool, error) { |
| - if sn == nil || sn.Provisioned == nil || sn.Provisioned.Active { |
| - return sn, false, nil |
| - } |
| + today := now.UTC().Format("2006-01-02") |
| + // stale is the whole condition, asked first of sn and then again of the |
| + // sender as read inside the mutation: sn may be a stale listing, and a |
| + // pause taken since (manual, rate) must stay (Astra plans-4b finding 2). |
| + // |
| // A RATE pause, a MANUAL pause and a PER-INSTANCE pause all fall through |
| // 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.Provisioned.PauseKind != mail.PauseKindPlan) || sn.PerInstance { |
| - return sn, false, nil |
| + stale := func(cur *mail.Sender) bool { |
| + pz := mail.EffectivePause(cur) |
| + // A pause dated earlier TODAY (or a clock oddity puts it in the |
| + // future) is not a day roll yet. |
| + return pz != nil && pz.LiftsAtDayRoll() && !cur.PerInstance && |
| + pz.At.UTC().Format("2006-01-02") < today |
| } |
| - pausedDay := sn.Provisioned.At.UTC().Format("2006-01-02") |
| - today := now.UTC().Format("2006-01-02") |
| - if pausedDay >= today { |
| - // Paused earlier TODAY (or a clock oddity puts the pause in the |
| - // future) — not a day roll yet. |
| + if sn == nil || !stale(sn) { |
| return sn, false, nil |
| } |
| - // Asked again of the sender as read inside the mutation: sn may be a |
| - // stale listing, and a pause taken since (manual, rate) must stay |
| - // (Astra plans-4b finding 2). |
| - stillStale := func(cur *mail.Sender) bool { |
| - return cur.Provisioned != nil && !cur.Provisioned.Active && !cur.PerInstance && |
| - (cur.Provisioned.PauseKind == mail.PauseKindCap || cur.Provisioned.PauseKind == mail.PauseKindPlan) && |
| - cur.Provisioned.At.UTC().Format("2006-01-02") < today |
| - } |
| + stillStale := stale |
| if _, err := mail.SetSenderPausedIf(ctx, s.claimMailStore(), s.cfg.MailProvisioner, acct, app, sender, false, "", now, stillStale); err != nil { |
| if errors.Is(err, mail.ErrNotEligible) { |
| return sn, false, nil |
| @@ -248,10 +246,11 @@ func (s *Server) enforceMailLimits(ctx context.Context, acct, app, sender string |
| if err != nil { |
| return false, "", err |
| } |
| - if sn == nil || sn.Provisioned == nil { |
| + // Nothing can send as this sender: no credential and no switched app. |
| + if !mail.CanSend(sn) { |
| return false, "", nil |
| } |
| - if !sn.Provisioned.Active { |
| + if mail.EffectivePause(sn) != nil { |
| fresh, resumed, err := s.resumeStaleCapPause(ctx, acct, app, sender, sn, now) |
| if err != nil { |
| return false, "", err |
| @@ -260,11 +259,10 @@ func (s *Server) enforceMailLimits(ctx context.Context, acct, app, sender string |
| return false, "", nil |
| } |
| // Re-read via resumeStaleCapPause: SetSenderPaused just wrote a |
| - // fresh sender (Active, PauseKind and At all moved), and everything |
| - // below reads sn fresh rather than patching the in-memory copy by |
| - // hand. |
| + // fresh sender (the pause moved), and everything below reads sn |
| + // fresh rather than patching the in-memory copy by hand. |
| sn = fresh |
| - if sn == nil || sn.Provisioned == nil || !sn.Provisioned.Active { |
| + if !mail.CanSend(sn) || mail.EffectivePause(sn) != nil { |
| return false, "", nil |
| } |
| } |
diff --git a/internal/console/mailcounts_test.go b/internal/console/mailcounts_test.go| index d36654fc0..ad369d19c 100644 |
| --- a/internal/console/mailcounts_test.go |
| +++ b/internal/console/mailcounts_test.go |
| @@ -8,6 +8,7 @@ import ( |
| "time" |
| "github.com/carlosframework/platform/internal/mail" |
| + "github.com/carlosframework/platform/internal/mailwire" |
| "github.com/carlosframework/platform/internal/release" |
| ) |
| @@ -827,3 +828,76 @@ func TestResumeAuditDetailIsByteIdenticalForTheAppSender(t *testing.T) { |
| } |
| t.Fatalf("no mail.resumed audit entry found: %+v", entries) |
| } |
| + |
| +// Test 8.13 as gap G1 reads it: with the pause on the sender record |
| +// (decision 8), a cap pause and a plan pause from yesterday lift on today's |
| +// enforceMailLimits; a rate, a manual and a per-instance pause from |
| +// yesterday do not. Each kind runs twice: written by the pause walk (Pause |
| +// and, in the overlap week, Provisioned too), and as a Pause field alone |
| +// over a Provisioned block that says active, which only a reader of the |
| +// Pause field gets right. |
| +func TestPauseKindsSurvive(t *testing.T) { |
| + for _, c := range []struct { |
| + kind string |
| + lifts bool |
| + }{ |
| + {mail.PauseKindCap, true}, |
| + {mail.PauseKindPlan, true}, |
| + {mail.PauseKindRate, false}, |
| + {"", false}, // manual |
| + {mail.PauseKindPerInstance, false}, |
| + } { |
| + for _, shape := range []string{"walk", "pause-field-only"} { |
| + name := c.kind |
| + if name == "" { |
| + name = "manual" |
| + } |
| + t.Run(name+"/"+shape, func(t *testing.T) { |
| + fx, now := mailFixture(t) |
| + ctx := context.Background() |
| + yesterday := now.AddDate(0, 0, -1) |
| + switch shape { |
| + case "walk": |
| + if _, err := mail.SetSenderPaused(ctx, fx.rel, fx.srv.cfg.MailProvisioner, release.OpsAccount, "harbourmail", |
| + mail.SenderAppID, true, c.kind, yesterday); err != nil { |
| + t.Fatal(err) |
| + } |
| + case "pause-field-only": |
| + sn, etag, err := mail.GetSender(ctx, fx.rel, release.OpsAccount, "harbourmail", mail.SenderAppID) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + sn.Pause = &mailwire.Pause{Kind: c.kind, At: yesterday} |
| + if _, err := mail.PutSender(ctx, fx.rel, release.OpsAccount, "harbourmail", sn, etag); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + |
| + paused, _, err := fx.srv.enforceMailLimits(ctx, release.OpsAccount, "harbourmail", mail.SenderAppID, now) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if paused { |
| + t.Fatal("enforce reports a new pause on an empty day") |
| + } |
| + sn, _, err := mail.GetSender(ctx, fx.rel, release.OpsAccount, "harbourmail", mail.SenderAppID) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + pz := mail.EffectivePause(sn) |
| + if c.lifts { |
| + if pz != nil { |
| + t.Fatalf("a %s pause from yesterday is still on today: %+v", name, pz) |
| + } |
| + if sn.Pause != nil || !sn.Provisioned.Active { |
| + t.Fatalf("lifted, but the record disagrees: Pause %+v, Provisioned %+v", sn.Pause, sn.Provisioned) |
| + } |
| + return |
| + } |
| + if pz == nil || pz.Kind != c.kind { |
| + t.Fatalf("a %s pause from yesterday read as %+v today, want it kept", name, pz) |
| + } |
| + }) |
| + } |
| + } |
| +} |
diff --git a/internal/console/planmail.go b/internal/console/planmail.go| index e412c812d..af0ce296e 100644 |
| --- a/internal/console/planmail.go |
| +++ b/internal/console/planmail.go |
| @@ -60,8 +60,10 @@ func (s *Server) enforceAccountMail(ctx context.Context, acct string, now time.T |
| // back (Astra plans-4b finding 4). |
| paused := false |
| var errs []error |
| + // A sender anything can send as, and that is not already paused: the |
| + // pause is read through mail.EffectivePause (decision 8). |
| eligible := func(cur *mail.Sender) bool { |
| - return cur.Provisioned != nil && cur.Provisioned.Active && !cur.PerInstance |
| + return mail.CanSend(cur) && mail.EffectivePause(cur) == nil && !cur.PerInstance |
| } |
| for _, app := range apps { |
| senders, err := mail.ListSenders(ctx, s.cfg.Releases, acct, app) |
| @@ -70,7 +72,7 @@ func (s *Server) enforceAccountMail(ctx context.Context, acct string, now time.T |
| continue |
| } |
| for _, sn := range senders { |
| - if sn.Provisioned == nil || !sn.Provisioned.Active { |
| + if !mail.CanSend(sn) || mail.EffectivePause(sn) != nil { |
| continue |
| } |
| if sn.PerInstance { |
diff --git a/internal/mail/mail.go b/internal/mail/mail.go| index 72e66e924..a3df97e5e 100644 |
| --- a/internal/mail/mail.go |
| +++ b/internal/mail/mail.go |
| @@ -24,6 +24,7 @@ import ( |
| "strings" |
| "time" |
| + "github.com/carlosframework/platform/internal/mailwire" |
| "github.com/carlosframework/platform/internal/release" |
| ) |
| @@ -77,15 +78,16 @@ type Provisioned struct { |
| At time.Time `json:"at"` |
| } |
| -// The two automatic pause kinds SetPaused records (spec §9, I3 review |
| -// follow-up). A manual (operator-driven) pause carries neither — see |
| -// Provisioned.PauseKind's own doc comment. |
| +// The automatic pause kinds SetPaused records (spec §9, I3 review |
| +// follow-up). A manual (operator-driven) pause carries none — see |
| +// Provisioned.PauseKind's own doc comment. They are defined once, in |
| +// internal/mailwire, which the mailer reads without importing this package. |
| const ( |
| - PauseKindCap = "cap" |
| - PauseKindRate = "rate" |
| + PauseKindCap = mailwire.PauseKindCap |
| + PauseKindRate = mailwire.PauseKindRate |
| // 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" |
| + PauseKindPlan = mailwire.PauseKindPlan |
| ) |
| // Standalone is a displayed credential for something not running on CARLOS. |
| @@ -152,6 +154,19 @@ func (s *Sender) record(app string) *Record { |
| Provisioned: s.Provisioned, Standalone: s.Standalone, Environments: s.Environments, from: s} |
| } |
| +// EffectivePause is the pause of the sender this record was read from, by |
| +// the same rule as the package-level EffectivePause. A record built from |
| +// nothing has only its Provisioned block to go on. |
| +func (r *Record) EffectivePause() *mailwire.Pause { |
| + if r == nil { |
| + return nil |
| + } |
| + if r.from != nil && r.from.Pause != nil { |
| + return r.from.Pause |
| + } |
| + return EffectivePause(&Sender{Provisioned: r.Provisioned}) |
| +} |
| + |
| // legacySender renders a Record decoded from LegacyKey as the app sender: |
| // the pre-senders object IS the app sender, with the identity fields it |
| // never carried filled in. |
diff --git a/internal/mail/pauseif_test.go b/internal/mail/pauseif_test.go| index 43c7be896..9fd0800d3 100644 |
| --- a/internal/mail/pauseif_test.go |
| +++ b/internal/mail/pauseif_test.go |
| @@ -6,6 +6,7 @@ import ( |
| "testing" |
| "time" |
| + "github.com/carlosframework/platform/internal/mailwire" |
| "github.com/carlosframework/platform/internal/release" |
| ) |
| @@ -38,3 +39,128 @@ func TestSetSenderPausedIfChecksTheRereadSender(t *testing.T) { |
| t.Fatalf("pause kind = %q, want the rate pause kept", sn.Provisioned.PauseKind) |
| } |
| } |
| + |
| +// Decision 8, the overlap week: a pause is written to the sender's own Pause |
| +// field AND, while the app-level user still exists, to every key and to |
| +// Provisioned exactly as before. A resume clears both. |
| +func TestSetSenderPausedWritesBoth(t *testing.T) { |
| + ctx := context.Background() |
| + s := release.NewDirStore(t.TempDir()) |
| + p := NewMemProvisioner("1") |
| + now := time.Date(2026, 11, 13, 12, 0, 0, 0, time.UTC) |
| + if _, err := Declare(ctx, s, "bab", "blog", "BLOG_SMTP", "noreply@x.com", "o@example.test", now); err != nil { |
| + t.Fatal(err) |
| + } |
| + arns := []string{"arn:aws:ses:eu-west-1:1:identity/x.com"} |
| + mint := func(user string, n int) []string { |
| + t.Helper() |
| + if _, err := p.EnsureUser(ctx, user, UserPath, arns, nil, []string{"x.com"}); err != nil { |
| + t.Fatal(err) |
| + } |
| + var ids []string |
| + for i := 0; i < n; i++ { |
| + id, _, err := p.MintKey(ctx, user) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + ids = append(ids, id) |
| + } |
| + return ids |
| + } |
| + app := mint("u-app", 2) // the delivered key and the previous one, mid-rotation |
| + sa := mint("u-laptop", 1) |
| + env := mint("u-env", 2) |
| + |
| + rec, etag, _ := Get(ctx, s, "bab", "blog") |
| + rec.Provisioned = &Provisioned{IAMUser: "u-app", KeyID: app[1], PrevKeyID: app[0], Active: true, At: now} |
| + rec.Standalone = []*Standalone{{Name: "laptop", IAMUser: "u-laptop", KeyID: sa[0], At: now}} |
| + rec.Environments = map[string]*Provisioned{"team": {IAMUser: "u-env", KeyID: env[1], PrevKeyID: env[0], Active: true, At: now}} |
| + if _, err := Put(ctx, s, "bab", "blog", rec, etag); err != nil { |
| + t.Fatal(err) |
| + } |
| + keys := map[string][]string{"u-app": app, "u-laptop": sa, "u-env": env} |
| + wantKeys := func(active bool) { |
| + t.Helper() |
| + for user, ids := range keys { |
| + for _, id := range ids { |
| + if got := p.Users[user].Keys[id].Active; got != active { |
| + t.Errorf("key %s of %s active = %v, want %v", id, user, got, active) |
| + } |
| + } |
| + } |
| + } |
| + |
| + if _, err := SetSenderPaused(ctx, s, p, "bab", "blog", SenderAppID, true, PauseKindCap, now); err != nil { |
| + t.Fatal(err) |
| + } |
| + sn, _, err := GetSender(ctx, s, "bab", "blog", SenderAppID) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if sn.Pause == nil || sn.Pause.Kind != PauseKindCap || !sn.Pause.At.Equal(now) { |
| + t.Fatalf("Pause = %+v, want a cap pause at %v", sn.Pause, now) |
| + } |
| + if sn.Provisioned.Active || sn.Provisioned.PauseKind != PauseKindCap || !sn.Provisioned.At.Equal(now) { |
| + t.Fatalf("Provisioned = %+v, want inactive with a cap pause kind (the overlap week)", sn.Provisioned) |
| + } |
| + if sn.Environments["team"].Active { |
| + t.Fatal("the owned environment's record is still active") |
| + } |
| + wantKeys(false) |
| + |
| + later := now.Add(time.Hour) |
| + if _, err := SetSenderPaused(ctx, s, p, "bab", "blog", SenderAppID, false, "", later); err != nil { |
| + t.Fatal(err) |
| + } |
| + sn, _, _ = GetSender(ctx, s, "bab", "blog", SenderAppID) |
| + if sn.Pause != nil { |
| + t.Fatalf("Pause = %+v after a resume, want nil", sn.Pause) |
| + } |
| + if !sn.Provisioned.Active || sn.Provisioned.PauseKind != "" { |
| + t.Fatalf("Provisioned = %+v after a resume, want active with no kind", sn.Provisioned) |
| + } |
| + if EffectivePause(sn) != nil { |
| + t.Fatal("a resumed sender still reads as paused") |
| + } |
| + wantKeys(true) |
| +} |
| + |
| +// The pause no longer needs an IAM user: a switched app's sender, whose |
| +// app-level user may be gone, can still be paused (decision 8). A sender |
| +// with neither a user nor a switched app has nothing that can send, and is |
| +// refused as before. |
| +func TestSetSenderPausedWithoutProvisioned(t *testing.T) { |
| + ctx := context.Background() |
| + s := release.NewDirStore(t.TempDir()) |
| + p := NewMemProvisioner("1") |
| + now := time.Date(2026, 11, 13, 12, 0, 0, 0, time.UTC) |
| + if _, err := Declare(ctx, s, "bab", "blog", "BLOG_SMTP", "noreply@x.com", "o@example.test", now); err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, err := SetSenderPaused(ctx, s, p, "bab", "blog", SenderAppID, true, "", now); !errors.Is(err, ErrNotProvisioned) { |
| + t.Fatalf("pause of a sender nothing can send as = %v, want ErrNotProvisioned", err) |
| + } |
| + |
| + sn, etag, _ := GetSender(ctx, s, "bab", "blog", SenderAppID) |
| + sn.Submission = &mailwire.Submission{On: true, At: now, By: "o@example.test"} |
| + if _, err := PutSender(ctx, s, "bab", "blog", sn, etag); err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, err := SetSenderPaused(ctx, s, p, "bab", "blog", SenderAppID, true, PauseKindRate, now); err != nil { |
| + t.Fatalf("pause of a switched sender with no user: %v", err) |
| + } |
| + sn, _, _ = GetSender(ctx, s, "bab", "blog", SenderAppID) |
| + if sn.Provisioned != nil { |
| + t.Fatalf("a pause invented a Provisioned block: %+v", sn.Provisioned) |
| + } |
| + if pz := EffectivePause(sn); pz == nil || pz.Kind != PauseKindRate { |
| + t.Fatalf("EffectivePause = %+v, want the rate pause", pz) |
| + } |
| + if _, err := SetSenderPaused(ctx, s, p, "bab", "blog", SenderAppID, false, "", now); err != nil { |
| + t.Fatal(err) |
| + } |
| + sn, _, _ = GetSender(ctx, s, "bab", "blog", SenderAppID) |
| + if sn.Pause != nil || !Switched(sn) { |
| + t.Fatalf("after a resume: Pause = %+v, switched = %v", sn.Pause, Switched(sn)) |
| + } |
| +} |
diff --git a/internal/mail/sender.go b/internal/mail/sender.go| index 7ef94ba4d..c5992eedd 100644 |
| --- a/internal/mail/sender.go |
| +++ b/internal/mail/sender.go |
| @@ -25,6 +25,7 @@ import ( |
| "strings" |
| "time" |
| + "github.com/carlosframework/platform/internal/mailwire" |
| "github.com/carlosframework/platform/internal/release" |
| ) |
| @@ -54,7 +55,7 @@ const SenderAppID = "app" |
| // exactly what per-instance mode exists to prevent. Nothing in this release |
| // sets it; the refusals that read it are in place so step 5 cannot land |
| // without them. |
| -const PauseKindPerInstance = "per-instance" |
| +const PauseKindPerInstance = mailwire.PauseKindPerInstance |
| // Sender is apps/<acct>/<app>/email/senders/<id>.json, one object per |
| // sender, each with its own CAS etag so two writers never contend over a |
| @@ -89,6 +90,17 @@ type Sender struct { |
| // app-sender enable, and the transition has to be one CAS write. |
| PerInstance bool `json:"per_instance,omitempty"` |
| + // Pause is the sender's pause, independent of Provisioned (platform mail |
| + // submission decision 8): a sender whose IAM user has been reaped can |
| + // still be paused. Nil means not paused. A record written before this |
| + // field existed carries its pause in Provisioned.Active and PauseKind, |
| + // which EffectivePause still reads. While Provisioned exists the pause |
| + // verb writes both. Additive: an older reader ignores it. |
| + Pause *mailwire.Pause `json:"pause,omitempty"` |
| + // Submission is the app sender's switch to the submit transport |
| + // (decision 10). Nothing writes it yet. Additive. |
| + Submission *mailwire.Submission `json:"submission,omitempty"` |
| + |
| // Salt disambiguates a slug collision. The slug is 40 bits of a hash, so |
| // two senders can in principle want the same IAM user name; the caller |
| // index detects that and ResolveSenderNames recomputes the slug with a |
| @@ -97,6 +109,38 @@ type Sender struct { |
| Salt string `json:"salt,omitempty"` |
| } |
| +// EffectivePause is a sender's pause: its Pause when it has one, else the |
| +// legacy reading of a record written before Pause existed (a Provisioned |
| +// block that is not active), else nil. Every reader of a sender's pause |
| +// state goes through this, never Provisioned.Active directly, so a record |
| +// of either shape reads the same (decision 8). mailwire.SenderView's |
| +// EffectivePause follows the same rule; a test pins the two together. |
| +func EffectivePause(sn *Sender) *mailwire.Pause { |
| + if sn == nil { |
| + return nil |
| + } |
| + if sn.Pause != nil { |
| + return sn.Pause |
| + } |
| + if sn.Provisioned != nil && !sn.Provisioned.Active { |
| + return &mailwire.Pause{Kind: sn.Provisioned.PauseKind, At: sn.Provisioned.At} |
| + } |
| + return nil |
| +} |
| + |
| +// Switched reports whether the sender's app has switched to the submit |
| +// transport (decision 10). Nothing sets it yet. |
| +func Switched(sn *Sender) bool { |
| + return sn != nil && sn.Submission != nil && sn.Submission.On |
| +} |
| + |
| +// CanSend reports whether anything can send as this sender at all: it has |
| +// an IAM credential (Provisioned), or its app is switched to the submit |
| +// transport. A sender that can send is one a pause means something for. |
| +func CanSend(sn *Sender) bool { |
| + return sn != nil && (sn.Provisioned != nil || Switched(sn)) |
| +} |
| + |
| // SendersPrefix is where an app's senders live. Inside apps/<acct>/<app>/ on |
| // purpose: release.PurgeApp's prefix sweep reaches it, so a purged app |
| // leaves no sender behind. |
diff --git a/internal/mail/sender_test.go b/internal/mail/sender_test.go| index f9c39ac1f..90c90c26d 100644 |
| --- a/internal/mail/sender_test.go |
| +++ b/internal/mail/sender_test.go |
| @@ -4,10 +4,12 @@ import ( |
| "context" |
| "encoding/json" |
| "errors" |
| + "reflect" |
| "strings" |
| "testing" |
| "time" |
| + "github.com/carlosframework/platform/internal/mailwire" |
| "github.com/carlosframework/platform/internal/release" |
| ) |
| @@ -292,3 +294,221 @@ func TestSenderOfCallerParsesBeforeItReads(t *testing.T) { |
| t.Fatalf("SenderOfCaller(slug) = %q %q %q %v", acct, app, sender, ok) |
| } |
| } |
| + |
| +// Decision 8: a record written before Pause existed carries its pause in |
| +// Provisioned, and reads as paused with that kind; an active one reads as |
| +// not paused. The record's own Pause wins over the legacy block. |
| +func TestEffectivePauseLegacyRecord(t *testing.T) { |
| + at := time.Date(2026, 11, 12, 9, 0, 0, 0, time.UTC) |
| + legacy := &Sender{ID: SenderAppID, Provisioned: &Provisioned{IAMUser: "u", KeyID: "K", Active: false, PauseKind: PauseKindRate, At: at}} |
| + got := EffectivePause(legacy) |
| + if got == nil || got.Kind != PauseKindRate || !got.At.Equal(at) { |
| + t.Fatalf("legacy paused record = %+v, want a rate pause at %v", got, at) |
| + } |
| + if got := EffectivePause(&Sender{ID: SenderAppID, Provisioned: &Provisioned{Active: true}}); got != nil { |
| + t.Fatalf("active legacy record = %+v, want nil", got) |
| + } |
| + if got := EffectivePause(&Sender{ID: SenderAppID}); got != nil { |
| + t.Fatalf("unprovisioned record = %+v, want nil", got) |
| + } |
| + if got := EffectivePause(nil); got != nil { |
| + t.Fatalf("nil sender = %+v, want nil", got) |
| + } |
| + withPause := &Sender{ID: SenderAppID, Pause: &mailwire.Pause{Kind: PauseKindCap, At: at}, |
| + Provisioned: &Provisioned{Active: false, PauseKind: PauseKindRate}} |
| + if got := EffectivePause(withPause); got == nil || got.Kind != PauseKindCap { |
| + t.Fatalf("record with a pause = %+v, want the cap pause", got) |
| + } |
| + |
| + // The Record view reads the sender it was decoded from, and falls back |
| + // to its own Provisioned block when it was built from nothing. |
| + if got := withPause.record("blog").EffectivePause(); got == nil || got.Kind != PauseKindCap { |
| + t.Fatalf("Record of a paused sender = %+v, want the cap pause", got) |
| + } |
| + if got := (&Record{Provisioned: legacy.Provisioned}).EffectivePause(); got == nil || got.Kind != PauseKindRate { |
| + t.Fatalf("bare Record with a paused block = %+v, want the rate pause", got) |
| + } |
| +} |
| + |
| +// The consumer pin: mailwire.SenderView is internal/mail's sender as the |
| +// agent and the mailer read it. Every field the view names decodes from a |
| +// marshalled mail.Sender to the same value, and the two EffectivePause |
| +// rules agree over every shape a pause can take. |
| +func TestSenderViewDecodesMailSender(t *testing.T) { |
| + at := time.Date(2026, 11, 13, 8, 30, 0, 0, time.UTC) |
| + reaped := at.Add(time.Hour) |
| + full := &Sender{ |
| + Schema: Schema, ID: SenderAppID, Slug: "s", Scope: ScopeApp, Source: SourceDomain, |
| + Declared: &Declared{EnvPrefix: "BLOG_SMTP", From: "noreply@x.com", At: at, By: "o@example.test"}, |
| + Grant: &GrantBlock{Domains: []string{"x.com", "y.com"}, DailyCap: 1000, Source: "tier:pro", At: at, By: "tier"}, |
| + Provisioned: &Provisioned{IAMUser: "u", UserARN: "arn:aws:iam::1:user/u", KeyID: "K", PrevKeyID: "P", |
| + Active: false, PauseKind: PauseKindRate, At: at}, |
| + Standalone: []*Standalone{{Name: "laptop", IAMUser: "u2", KeyID: "K2", At: at, By: "o"}}, |
| + Environments: map[string]*Provisioned{"team": {IAMUser: "u3", KeyID: "K3", Active: true, At: at}}, |
| + Identity: "arn:aws:ses:eu-west-1:1:identity/x.com", Address: "noreply@x.com", |
| + TenantARN: "arn:aws:ses:eu-west-1:1:tenant/carlos-bab/abc", |
| + PerInstance: true, |
| + Pause: &mailwire.Pause{Kind: PauseKindCap, At: at}, |
| + Submission: &mailwire.Submission{On: true, At: at, By: "paul@example.test", AppUserReapedAt: &reaped}, |
| + Salt: "beef", |
| + } |
| + body, err := json.Marshal(full) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + var v mailwire.SenderView |
| + if err := json.Unmarshal(body, &v); err != nil { |
| + t.Fatal(err) |
| + } |
| + if v.ID != full.ID { |
| + t.Errorf("ID = %q, want %q", v.ID, full.ID) |
| + } |
| + if v.Declared == nil || v.Declared.From != full.Declared.From { |
| + t.Errorf("Declared = %+v, want From %q", v.Declared, full.Declared.From) |
| + } |
| + if v.Grant == nil || !reflect.DeepEqual(v.Grant.Domains, full.Grant.Domains) || v.Grant.DailyCap != full.Grant.DailyCap { |
| + t.Errorf("Grant = %+v, want %+v", v.Grant, full.Grant) |
| + } |
| + if v.Provisioned == nil || v.Provisioned.Active != full.Provisioned.Active || |
| + v.Provisioned.PauseKind != full.Provisioned.PauseKind || !v.Provisioned.At.Equal(full.Provisioned.At) { |
| + t.Errorf("Provisioned = %+v, want %+v", v.Provisioned, full.Provisioned) |
| + } |
| + if !reflect.DeepEqual(v.Pause, full.Pause) { |
| + t.Errorf("Pause = %+v, want %+v", v.Pause, full.Pause) |
| + } |
| + if v.Submission == nil || v.Submission.On != full.Submission.On || !v.Submission.At.Equal(full.Submission.At) || |
| + v.Submission.By != full.Submission.By || v.Submission.AppUserReapedAt == nil || !v.Submission.AppUserReapedAt.Equal(reaped) { |
| + t.Errorf("Submission = %+v, want %+v", v.Submission, full.Submission) |
| + } |
| + if v.TenantARN != full.TenantARN { |
| + t.Errorf("TenantARN = %q, want %q", v.TenantARN, full.TenantARN) |
| + } |
| + if v.PerInstance != full.PerInstance { |
| + t.Errorf("PerInstance = %v, want %v", v.PerInstance, full.PerInstance) |
| + } |
| + |
| + // The two EffectivePause rules agree on every shape. |
| + for name, sn := range map[string]*Sender{ |
| + "pause and legacy": full, |
| + "legacy only": {ID: SenderAppID, Provisioned: &Provisioned{Active: false, PauseKind: PauseKindPlan, At: at}}, |
| + "active": {ID: SenderAppID, Provisioned: &Provisioned{Active: true, At: at}}, |
| + "pause only": {ID: SenderAppID, Pause: &mailwire.Pause{At: at}}, |
| + "nothing": {ID: SenderAppID}, |
| + } { |
| + body, err := json.Marshal(sn) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + var v mailwire.SenderView |
| + if err := json.Unmarshal(body, &v); err != nil { |
| + t.Fatal(err) |
| + } |
| + if got, want := v.EffectivePause(), EffectivePause(sn); !reflect.DeepEqual(got, want) { |
| + t.Errorf("%s: view EffectivePause = %+v, mail EffectivePause = %+v", name, got, want) |
| + } |
| + } |
| +} |
| + |
| +// mailwire builds the bucket keys the mailer and the agent read without |
| +// importing this package. They must be the same strings. |
| +func TestMailwireKeysMatchMail(t *testing.T) { |
| + for _, c := range []struct{ acct, app, id string }{ |
| + {"bab", "blog", SenderAppID}, |
| + {"bag", "harbour-mail", "book.example.com"}, |
| + {"zz", "a", "_0123456789abcdef"}, |
| + } { |
| + if got, want := mailwire.SenderKey(c.acct, c.app, c.id), SenderKey(c.acct, c.app, c.id); got != want { |
| + t.Errorf("mailwire.SenderKey(%q, %q, %q) = %q, want %q", c.acct, c.app, c.id, got, want) |
| + } |
| + } |
| + for _, c := range []struct{ acct, domain string }{ |
| + {"bab", "x.com"}, |
| + {"bag", "mail.example.co.uk"}, |
| + } { |
| + if got, want := mailwire.DomainKey(c.acct, c.domain), DomainKey(c.acct, c.domain); got != want { |
| + t.Errorf("mailwire.DomainKey(%q, %q) = %q, want %q", c.acct, c.domain, got, want) |
| + } |
| + } |
| + // One definition of the pause kinds. |
| + for got, want := range map[string]string{ |
| + PauseKindCap: mailwire.PauseKindCap, PauseKindRate: mailwire.PauseKindRate, |
| + PauseKindPlan: mailwire.PauseKindPlan, PauseKindPerInstance: mailwire.PauseKindPerInstance, |
| + } { |
| + if got != want { |
| + t.Errorf("pause kind %q != mailwire's %q", got, want) |
| + } |
| + } |
| +} |
| + |
| +// AGENTS.md: wire-shape changes are additive. A sender written before Pause |
| +// and Submission existed decodes into the new Sender and marshals back with |
| +// nothing lost or added, and still reads as the pause it always meant. A |
| +// sender carrying the new fields still decodes into the old shape. |
| +func TestAdditiveSenderShape(t *testing.T) { |
| + golden := `{"schema":1,"id":"app","slug":"","scope":"app","source":"domain",` + |
| + `"declared":{"env_prefix":"BLOG_SMTP","from":"noreply@x.com","at":"2026-09-15T12:00:00Z","by":"o@example.test"},` + |
| + `"grant":{"domains":["x.com"],"daily_cap":50,"source":"tier:free","at":"2026-09-15T12:00:00Z","by":"tier"},` + |
| + `"provisioned":{"iam_user":"carlos-mail-bab-blog","user_arn":"arn:aws:iam::1:user/carlos-mail/carlos-mail-bab-blog",` + |
| + `"key_id":"AKIA1","prev_key_id":"AKIA0","active":false,"pause_kind":"rate","at":"2026-09-16T08:00:00Z"},` + |
| + `"standalone":[{"name":"laptop","iam_user":"carlos-mail-bab-blog+s+laptop","key_id":"AKIA2","at":"2026-09-15T12:00:00Z","by":"o@example.test"}],` + |
| + `"environments":{"team":{"iam_user":"carlos-mail-bab-blog+e+team","user_arn":"","key_id":"AKIA3","active":false,"at":"2026-09-16T08:00:00Z"}},` + |
| + `"tenant_arn":"arn:aws:ses:eu-west-1:1:tenant/t","per_instance":true,"salt":"beef"}` |
| + var sn Sender |
| + if err := json.Unmarshal([]byte(golden), &sn); err != nil { |
| + t.Fatal(err) |
| + } |
| + if sn.Pause != nil || sn.Submission != nil { |
| + t.Fatalf("an old record decoded new fields: Pause %+v, Submission %+v", sn.Pause, sn.Submission) |
| + } |
| + back, err := json.Marshal(&sn) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + var want, got map[string]any |
| + if err := json.Unmarshal([]byte(golden), &want); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := json.Unmarshal(back, &got); err != nil { |
| + t.Fatal(err) |
| + } |
| + if !reflect.DeepEqual(got, want) { |
| + t.Fatalf("round trip changed the record:\n got %s\nwant %s", back, golden) |
| + } |
| + if pz := EffectivePause(&sn); pz == nil || pz.Kind != PauseKindRate { |
| + t.Fatalf("the old record's pause reads as %+v, want the rate pause it always meant", pz) |
| + } |
| + |
| + // The old shape, as a reader from before this change would decode it. |
| + type oldSender struct { |
| + Schema int `json:"schema"` |
| + ID string `json:"id"` |
| + Slug string `json:"slug"` |
| + Scope string `json:"scope"` |
| + Host string `json:"host,omitempty"` |
| + Source string `json:"source"` |
| + Declared *Declared `json:"declared,omitempty"` |
| + Grant *GrantBlock `json:"grant,omitempty"` |
| + Provisioned *Provisioned `json:"provisioned,omitempty"` |
| + Standalone []*Standalone `json:"standalone,omitempty"` |
| + Environments map[string]*Provisioned `json:"environments,omitempty"` |
| + Identity string `json:"identity,omitempty"` |
| + Address string `json:"address,omitempty"` |
| + TenantARN string `json:"tenant_arn,omitempty"` |
| + PerInstance bool `json:"per_instance,omitempty"` |
| + Salt string `json:"salt,omitempty"` |
| + } |
| + at := time.Date(2026, 11, 13, 9, 0, 0, 0, time.UTC) |
| + sn.Pause = &mailwire.Pause{Kind: PauseKindCap, At: at} |
| + sn.Submission = &mailwire.Submission{On: true, At: at} |
| + body, err := json.Marshal(&sn) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + var old oldSender |
| + if err := json.Unmarshal(body, &old); err != nil { |
| + t.Fatalf("a sender with a pause does not decode in the old shape: %v", err) |
| + } |
| + if old.Provisioned == nil || old.Provisioned.KeyID != "AKIA1" || old.ID != SenderAppID { |
| + t.Fatalf("old shape lost the record: %+v", old) |
| + } |
| +} |
diff --git a/internal/mail/walk.go b/internal/mail/walk.go| index 8ac4a0583..85d341004 100644 |
| --- a/internal/mail/walk.go |
| +++ b/internal/mail/walk.go |
| @@ -15,6 +15,7 @@ import ( |
| "github.com/carlosframework/platform/internal/appconfig" |
| "github.com/carlosframework/platform/internal/dnsrec" |
| + "github.com/carlosframework/platform/internal/mailwire" |
| "github.com/carlosframework/platform/internal/release" |
| ) |
| @@ -845,13 +846,17 @@ func claimUnserializedEnable(ctx context.Context, s release.Store, p Provisioner |
| // repair walk, which enforceMailLimits and the status-read auto-resume |
| // (R2) never resume, and which the CLI describes as needing an |
| // operator — resurrecting the exact defect PauseKind was added to fix, |
| - // through this door instead of SetPaused. A first-time enable (no |
| - // prior Provisioned block) starts active with no pause kind. |
| + // through this door instead of SetPaused. The pause is read through |
| + // EffectivePause (decision 8), so a sender paused while it had no |
| + // Provisioned block (a switched app whose user was reaped) is minted |
| + // paused too. A first-time enable of an unpaused sender starts active |
| + // with no pause kind. |
| active, prev, pauseKind := true, "", "" |
| if rec.Provisioned != nil { |
| prev = rec.Provisioned.PrevKeyID |
| - active = rec.Provisioned.Active |
| - pauseKind = rec.Provisioned.PauseKind |
| + } |
| + if pz := rec.EffectivePause(); pz != nil { |
| + active, pauseKind = false, pz.Kind |
| } |
| // Reap before minting: reaching here means delivery is missing, so any |
| @@ -1063,7 +1068,7 @@ func claimUnserializedRotate(ctx context.Context, s release.Store, p Provisioner |
| // C1: a rotate against a PAUSED app must not silently lift the pause at |
| // IAM. MintKey returns an active key regardless; this brings it back in |
| // line with what the record already says. |
| - if err := enforceKeyActive(ctx, p, user, keyID, rec.Provisioned.Active); err != nil { |
| + if err := enforceKeyActive(ctx, p, user, keyID, rec.EffectivePause() == nil); err != nil { |
| return nil, err |
| } |
| if err := writeSMTPEnv(ctx, s, seal, acct, app, rec.Declared.EnvPrefix, |
| @@ -1200,6 +1205,15 @@ func claimUnserializedSetPaused(ctx context.Context, s release.Store, p Provisio |
| // A resume is refused while PerInstance is set: the app's instances still |
| // hold this credential, and re-arming it is exactly what per-instance mode |
| // exists to prevent. A pause is always allowed. |
| +// |
| +// The pause itself is the sender's Pause field (platform mail submission |
| +// decision 8): set to {kind, now} on a pause and cleared on a resume, so it |
| +// means the same once the IAM user is gone. While Provisioned exists (the |
| +// overlap week before the app-level user is reaped) the walk also does |
| +// everything it did before Pause existed: the keys, and Provisioned's |
| +// Active, PauseKind and At, so an older console and the credential agree |
| +// with the record. A sender with neither Provisioned nor a switched app has |
| +// nothing that can send, and is refused with ErrNotProvisioned. |
| func claimUnserializedSetSenderPaused(ctx context.Context, s release.Store, p Provisioner, |
| acct, app, sender string, paused bool, kind string, now time.Time, eligible func(*Sender) bool) (*Sender, error) { |
| @@ -1207,7 +1221,7 @@ func claimUnserializedSetSenderPaused(ctx context.Context, s release.Store, p Pr |
| if err != nil { |
| return nil, err |
| } |
| - if sn == nil || sn.Provisioned == nil { |
| + if !CanSend(sn) { |
| return nil, ErrNotProvisioned |
| } |
| // The caller's condition is asked of the sender as read HERE, inside |
| @@ -1220,15 +1234,20 @@ func claimUnserializedSetSenderPaused(ctx context.Context, s release.Store, p Pr |
| if !paused && sn.PerInstance { |
| return nil, fmt.Errorf("mail: %s/%s: resume its instance senders instead: %w", acct, app, ErrPerInstance) |
| } |
| - user := sn.Provisioned.IAMUser |
| - for _, id := range []string{sn.Provisioned.KeyID, sn.Provisioned.PrevKeyID} { |
| - if id == "" { |
| - continue |
| - } |
| - if err := p.SetKeyActive(ctx, user, id, !paused); err != nil { |
| - return nil, err |
| + if sn.Provisioned != nil { |
| + user := sn.Provisioned.IAMUser |
| + for _, id := range []string{sn.Provisioned.KeyID, sn.Provisioned.PrevKeyID} { |
| + if id == "" { |
| + continue |
| + } |
| + if err := p.SetKeyActive(ctx, user, id, !paused); err != nil { |
| + return nil, err |
| + } |
| } |
| } |
| + // Standalones and owned environments are their own IAM users, so they |
| + // are toggled whenever they exist, Provisioned or not: an abuse brake |
| + // that left them sending would be no brake. |
| for _, sa := range sn.Standalone { |
| if err := p.SetKeyActive(ctx, sa.IAMUser, sa.KeyID, !paused); err != nil { |
| return nil, err |
| @@ -1249,13 +1268,20 @@ func claimUnserializedSetSenderPaused(ctx context.Context, s release.Store, p Pr |
| } |
| ep.Active = !paused |
| } |
| - sn.Provisioned.Active = !paused |
| + if sn.Provisioned != nil { |
| + sn.Provisioned.Active = !paused |
| + if paused { |
| + sn.Provisioned.PauseKind = kind |
| + } else { |
| + sn.Provisioned.PauseKind = "" |
| + } |
| + sn.Provisioned.At = now |
| + } |
| if paused { |
| - sn.Provisioned.PauseKind = kind |
| + sn.Pause = &mailwire.Pause{Kind: kind, At: now} |
| } else { |
| - sn.Provisioned.PauseKind = "" |
| + sn.Pause = nil |
| } |
| - sn.Provisioned.At = now |
| if _, err := PutSender(ctx, s, acct, app, sn, etag); err != nil { |
| return nil, err |
| } |
| @@ -1323,13 +1349,10 @@ func claimUnserializedCreateStandalone(ctx context.Context, s release.Store, p P |
| // A THIRD mint path, and enforceKeyActive's contract applies here just |
| // as it does to Enable and Rotate: minting a standalone credential for a |
| // PAUSED app must not hand back a live key just because the mint went |
| - // through a different member verb. An app with no Provisioned block yet |
| - // (declared and granted, never enabled) has nothing to be paused, so it |
| - // defaults active. |
| - active := true |
| - if rec.Provisioned != nil { |
| - active = rec.Provisioned.Active |
| - } |
| + // through a different member verb. The pause is read through |
| + // EffectivePause (decision 8). An app with no Provisioned block and no |
| + // Pause (declared and granted, never enabled) defaults active. |
| + active := rec.EffectivePause() == nil |
| if err := enforceKeyActive(ctx, p, user, keyID, active); err != nil { |
| return StandaloneCreds{}, err |
| } |
diff --git a/internal/mail/walk_environment.go b/internal/mail/walk_environment.go| index 42222cceb..8b93d0b5d 100644 |
| --- a/internal/mail/walk_environment.go |
| +++ b/internal/mail/walk_environment.go |
| @@ -94,8 +94,9 @@ func claimUnserializedEnableEnvironment(ctx context.Context, s release.Store, p |
| } |
| // The pause state follows the app's: a paused app's environments are |
| // paused with it (SetSenderPaused), so a fresh mint under a paused app |
| - // starts inactive. |
| - active, prev := rec.Provisioned.Active, "" |
| + // starts inactive. The app's pause is read through EffectivePause |
| + // (decision 8), never Provisioned.Active directly. |
| + active, prev := rec.EffectivePause() == nil, "" |
| if cur != nil { |
| prev, active = cur.PrevKeyID, cur.Active |
| } |
diff --git a/internal/mailwire/mailwire.go b/internal/mailwire/mailwire.go| new file mode 100644 |
| index 000000000..12b459e31 |
| --- /dev/null |
| +++ b/internal/mailwire/mailwire.go |
| @@ -0,0 +1,120 @@ |
| +// Package mailwire is the small, pure part of the tenant-email record that |
| +// the agent and the box-local mailer read (platform mail submission design, |
| +// docs/superpowers/specs/2026-11-12-platform-mail-submission-design.md). |
| +// |
| +// It holds wire types and string rules only: no AWS, no store writes, and no |
| +// import of internal/mail. internal/mail carries the IAM and SES walks, and |
| +// keeping it out of the root binary is what keeps those walks out of census |
| +// scope; the mailer-side packages import this package instead, and |
| +// internal/mail defines its constants from these so there is one definition. |
| +package mailwire |
| + |
| +import "time" |
| + |
| +// The pause kinds (decision 8, kept from internal/mail's Provisioned.PauseKind). |
| +// A manual (operator) pause carries the empty kind. |
| +const ( |
| + // PauseKindCap is the per-day send cap. It lifts on the UTC day roll. |
| + PauseKindCap = "cap" |
| + // PauseKindRate is a bounce or complaint threshold breach. It never |
| + // lifts on its own: SES's account-review thresholds are not a daily |
| + // allowance. |
| + 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 |
| + // (gap G1: the plan keeps today's behaviour rather than spec decision |
| + // 8's wording, which would have been a billing change). |
| + PauseKindPlan = "plan" |
| + // PauseKindPerInstance is the pause an app sender carries once its app |
| + // has moved to per-instance senders. It never lifts on its own. |
| + PauseKindPerInstance = "per-instance" |
| +) |
| + |
| +// Pause is a sender's pause, on the sender record itself (decision 8), so it |
| +// means the same whether or not the sender still has an IAM user. Kind is |
| +// one of the PauseKind constants, or "" for a manual pause. |
| +type Pause struct { |
| + Kind string `json:"kind,omitempty"` |
| + At time.Time `json:"at"` |
| +} |
| + |
| +// LiftsAtDayRoll reports whether the pause clears itself once the UTC day it |
| +// was taken on has passed: a cap pause and a plan pause do, and nothing else |
| +// does (gap G1). A nil pause lifts nothing. |
| +func (p *Pause) LiftsAtDayRoll() bool { |
| + if p == nil { |
| + return false |
| + } |
| + return p.Kind == PauseKindCap || p.Kind == PauseKindPlan |
| +} |
| + |
| +// Submission is the per-app switch to the submit transport (decision 10). |
| +// It lives on the app sender record. Nothing writes it until the switch's |
| +// door exists; it is defined here so the agent can read it. |
| +type Submission struct { |
| + On bool `json:"on"` |
| + At time.Time `json:"at"` |
| + By string `json:"by,omitempty"` |
| + AppUserReapedAt *time.Time `json:"app_user_reaped_at,omitempty"` |
| +} |
| + |
| +// DeclaredView is the part of the sender's declared block the mailer reads. |
| +type DeclaredView struct { |
| + From string `json:"from"` |
| +} |
| + |
| +// GrantView is the part of the sender's grant block the mailer reads. |
| +type GrantView struct { |
| + Domains []string `json:"domains"` |
| + DailyCap int `json:"daily_cap"` |
| +} |
| + |
| +// ProvisionedView is the part of the sender's provisioned block that still |
| +// carries a pause on a record written before Pause existed. |
| +type ProvisionedView struct { |
| + Active bool `json:"active"` |
| + PauseKind string `json:"pause_kind,omitempty"` |
| + At time.Time `json:"at"` |
| +} |
| + |
| +// SenderView is apps/<acct>/<app>/email/senders/<id>.json as the agent and |
| +// the mailer read it: only the fields they act on, under internal/mail's |
| +// JSON names. Every other field of the record is ignored on decode. |
| +type SenderView struct { |
| + ID string `json:"id"` |
| + Declared *DeclaredView `json:"declared,omitempty"` |
| + Grant *GrantView `json:"grant,omitempty"` |
| + Provisioned *ProvisionedView `json:"provisioned,omitempty"` |
| + Pause *Pause `json:"pause,omitempty"` |
| + Submission *Submission `json:"submission,omitempty"` |
| + TenantARN string `json:"tenant_arn,omitempty"` |
| + PerInstance bool `json:"per_instance,omitempty"` |
| +} |
| + |
| +// EffectivePause is the sender's pause: the record's Pause when it has one, |
| +// else the legacy reading of a record written before Pause existed (a |
| +// Provisioned block that is not active), else nil. |
| +func (v *SenderView) EffectivePause() *Pause { |
| + if v == nil { |
| + return nil |
| + } |
| + if v.Pause != nil { |
| + return v.Pause |
| + } |
| + if v.Provisioned != nil && !v.Provisioned.Active { |
| + return &Pause{Kind: v.Provisioned.PauseKind, At: v.Provisioned.At} |
| + } |
| + return nil |
| +} |
| + |
| +// SenderKey is the bucket key of one sender. It is the same string as |
| +// internal/mail's SenderKey, which a test pins. |
| +func SenderKey(acct, app, id string) string { |
| + return "apps/" + acct + "/" + app + "/email/senders/" + id + ".json" |
| +} |
| + |
| +// DomainKey is the bucket key of one sending domain. It is the same string |
| +// as internal/mail's DomainKey, which a test pins. |
| +func DomainKey(acct, domain string) string { |
| + return "accounts/" + acct + "/email/domains/" + domain + ".json" |
| +} |
diff --git a/internal/mailwire/mailwire_test.go b/internal/mailwire/mailwire_test.go| new file mode 100644 |
| index 000000000..8891d785f |
| --- /dev/null |
| +++ b/internal/mailwire/mailwire_test.go |
| @@ -0,0 +1,73 @@ |
| +package mailwire |
| + |
| +import ( |
| + "encoding/json" |
| + "testing" |
| + "time" |
| +) |
| + |
| +// Gap G1: a cap pause and a plan pause lift at the day roll; nothing else |
| +// does, a manual pause (the empty kind) least of all. |
| +func TestLiftsAtDayRoll(t *testing.T) { |
| + for kind, want := range map[string]bool{ |
| + PauseKindCap: true, |
| + PauseKindPlan: true, |
| + PauseKindRate: false, |
| + PauseKindPerInstance: false, |
| + "": false, |
| + "something-new": false, |
| + } { |
| + if got := (&Pause{Kind: kind}).LiftsAtDayRoll(); got != want { |
| + t.Errorf("Pause{Kind: %q}.LiftsAtDayRoll() = %v, want %v", kind, got, want) |
| + } |
| + } |
| + var none *Pause |
| + if none.LiftsAtDayRoll() { |
| + t.Error("a nil pause lifts at the day roll") |
| + } |
| +} |
| + |
| +// The view reads a pause the same way internal/mail does: the record's own |
| +// Pause first, then the legacy Provisioned block, then none. |
| +func TestEffectivePauseLegacyRecordView(t *testing.T) { |
| + at := time.Date(2026, 11, 12, 9, 0, 0, 0, time.UTC) |
| + decode := func(body string) *SenderView { |
| + t.Helper() |
| + var v SenderView |
| + if err := json.Unmarshal([]byte(body), &v); err != nil { |
| + t.Fatal(err) |
| + } |
| + return &v |
| + } |
| + |
| + legacy := decode(`{"id":"app","provisioned":{"active":false,"pause_kind":"rate","at":"2026-11-12T09:00:00Z"}}`) |
| + got := legacy.EffectivePause() |
| + if got == nil || got.Kind != PauseKindRate || !got.At.Equal(at) { |
| + t.Fatalf("legacy paused record = %+v, want a rate pause at %v", got, at) |
| + } |
| + |
| + if got := decode(`{"id":"app","provisioned":{"active":true}}`).EffectivePause(); got != nil { |
| + t.Fatalf("active legacy record = %+v, want no pause", got) |
| + } |
| + |
| + if got := decode(`{"id":"app"}`).EffectivePause(); got != nil { |
| + t.Fatalf("record with nothing provisioned = %+v, want no pause", got) |
| + } |
| + |
| + // The record's own Pause wins over whatever the legacy block says. |
| + both := decode(`{"id":"app","pause":{"kind":"cap","at":"2026-11-12T09:00:00Z"},"provisioned":{"active":false,"pause_kind":"rate"}}`) |
| + if got := both.EffectivePause(); got == nil || got.Kind != PauseKindCap { |
| + t.Fatalf("record with a pause = %+v, want the cap pause", got) |
| + } |
| + |
| + // A switched app's sender may have no Provisioned block at all and |
| + // still be paused. |
| + if got := decode(`{"id":"app","pause":{"at":"2026-11-12T09:00:00Z"}}`).EffectivePause(); got == nil || got.Kind != "" { |
| + t.Fatalf("manual pause without a provisioned block = %+v, want a kind-less pause", got) |
| + } |
| + |
| + var none *SenderView |
| + if none.EffectivePause() != nil { |
| + t.Fatal("a nil view is paused") |
| + } |
| +} |
diff --git a/internal/provision/testdata/plan14-root-reads-chain.tsv b/internal/provision/testdata/plan14-root-reads-chain.tsv| index 96e02662b..15a693f58 100644 |
| --- a/internal/provision/testdata/plan14-root-reads-chain.tsv |
| +++ b/internal/provision/testdata/plan14-root-reads-chain.tsv |
| @@ -201,3 +201,4 @@ plan14-root-reads-ha-unclean-marker-waits.tsv 821598970e60a776d4a62138d5adbfa393 |
| plan14-root-reads-ha-unclean-files-only.tsv bc138ce5435f3ab37f6adff46656eea1c20f48168c569550fe94e390c69ba309 |
| plan14-root-reads-edge-ppv2.tsv 44e5745a1d22ea2ddd6e73d775d87759afe7121666212b6ed2563c1518261bc4 |
| plan14-root-reads-static-site-geo-962.tsv 5e8a72175700a3e60c1e283b133c1ee25e36f292e2e319f02a19138f15186376 |
| +plan14-root-reads-platform-mail-submission.tsv 5e8a72175700a3e60c1e283b133c1ee25e36f292e2e319f02a19138f15186376 |
diff --git a/internal/provision/testdata/plan14-root-reads-platform-mail-submission.tsv b/internal/provision/testdata/plan14-root-reads-platform-mail-submission.tsv| new file mode 100644 |
| index 000000000..4c879f1cd |
| --- /dev/null |
| +++ b/internal/provision/testdata/plan14-root-reads-platform-mail-submission.tsv |
| @@ -0,0 +1,3 @@ |
| +# Census overlay platform-mail-submission, written by census-propose (#369). census-ruling-platform-mail-submission, approved on #280 before the merge; the approval card is docs/superpowers/notes/census-proposal-platform-mail-submission.md. |
| +# 0 body, 0 sites, 1 envelope: each changed unit re-frozen at its existing disposition with its claim appended. |
| +@coverage . 86f4a338d25bc4f54429d60af25f0008a2d0d22c4cd472e84bffdd8131f63fca |
diff --git a/mail_cli.go b/mail_cli.go| index 76d4281dd..c3e88c091 100644 |
| --- a/mail_cli.go |
| +++ b/mail_cli.go |
| @@ -249,6 +249,16 @@ type apiEmailProvisioned struct { |
| At time.Time `json:"at"` |
| } |
| +// apiEmailPause is a sender's pause in the status reply (platform mail |
| +// submission decision 8): the console's reading of the sender record's |
| +// pause, whichever shape the record carries it in. Absent when the sender |
| +// is not paused, and always absent from an older console, which says the |
| +// same through "active" and provisioned.pause_kind. |
| +type apiEmailPause struct { |
| + Kind string `json:"kind,omitempty"` |
| + At time.Time `json:"at"` |
| +} |
| + |
| type apiEmailStandalone struct { |
| Name string `json:"name"` |
| IAMUser string `json:"iam_user"` |
| @@ -295,6 +305,7 @@ type apiEmailSender struct { |
| Address string `json:"address,omitempty"` |
| Active bool `json:"active"` |
| PauseKind string `json:"pause_kind,omitempty"` |
| + Pause *apiEmailPause `json:"pause,omitempty"` |
| DailyCap int `json:"daily_cap"` |
| KeyID string `json:"key_id,omitempty"` |
| Counts apiEmailCounts `json:"counts"` |
| @@ -314,6 +325,7 @@ type apiEmailReply struct { |
| MissingVars []string `json:"missing_vars"` |
| DailyCap int `json:"daily_cap"` |
| Active bool `json:"active"` |
| + Pause *apiEmailPause `json:"pause,omitempty"` |
| Counts apiEmailCounts `json:"counts"` |
| Senders []apiEmailSender `json:"senders"` |
| // DefaultHost is this app's own host — <app>.<sqid>.<platform-suffix> — |
| @@ -1371,7 +1383,8 @@ func printEmailStatus(w io.Writer, res apiEmailReply) { |
| } |
| if res.Provisioned != nil { |
| - if res.Active { |
| + paused, kind := emailPauseOf(res.Pause, res.Active, res.Provisioned.PauseKind) |
| + if !paused { |
| fmt.Fprintf(w, " sending: %s (key %s)\n", c.ok("active"), res.Provisioned.KeyID) |
| } else { |
| warnf(w, " sending: PAUSED — SMTP auth is failing for this app right now") |
| @@ -1395,7 +1408,7 @@ func printEmailStatus(w io.Writer, res apiEmailReply) { |
| // RATE pause, or a manual pause this reply carries no kind |
| // for, does not clear on its own and genuinely needs an |
| // operator. |
| - switch res.Provisioned.PauseKind { |
| + switch kind { |
| case "cap": |
| mutedf(w, " daily cap reached — this clears at the next UTC day roll; running `carlos email status` after that will clear it") |
| case "rate": |
| @@ -1427,10 +1440,10 @@ func printEmailStatus(w io.Writer, res apiEmailReply) { |
| what = sn.Address |
| } |
| state := c.ok("active") |
| - if !sn.Active { |
| + if paused, kind := emailPauseOf(sn.Pause, sn.Active, sn.PauseKind); paused { |
| state = "paused" |
| - if sn.PauseKind != "" { |
| - state += " (" + sn.PauseKind + ")" |
| + if kind != "" { |
| + state += " (" + kind + ")" |
| } |
| } |
| fmt.Fprintf(w, " %s %s: %s, %d/%d sends, %d bounces, %d complaints\n", |
| @@ -1446,6 +1459,21 @@ func printEmailStatus(w io.Writer, res apiEmailReply) { |
| } |
| } |
| +// emailPauseOf reads a sender's pause from the status reply: the reply's |
| +// pause block when it has one, else the older fields, "active" and the |
| +// pause kind, which an older console sends alone. A newer console sends the |
| +// pause block whenever the sender is paused, and the older fields agree |
| +// with it. |
| +func emailPauseOf(p *apiEmailPause, active bool, legacyKind string) (paused bool, kind string) { |
| + if p != nil { |
| + return true, p.Kind |
| + } |
| + if !active { |
| + return true, legacyKind |
| + } |
| + return false, "" |
| +} |
| + |
| func standaloneNames(sa []apiEmailStandalone) string { |
| names := make([]string, 0, len(sa)) |
| for _, s := range sa { |
diff --git a/mail_cli_test.go b/mail_cli_test.go| index fa22ec58f..1a86ad604 100644 |
| --- a/mail_cli_test.go |
| +++ b/mail_cli_test.go |
| @@ -1408,3 +1408,47 @@ func TestPrintEmailStatusWithNoSendersArrayIsUnchanged(t *testing.T) { |
| t.Errorf("a reply with no senders array printed a senders heading:\n%s", buf.String()) |
| } |
| } |
| + |
| +// Decision 8: the status printer reads the pause from the reply's pause |
| +// block, and from "active" and provisioned.pause_kind when an older console |
| +// sends no block. The block wins: a reply whose pause block says cap is |
| +// described as a cap pause whatever the legacy kind says. |
| +func TestPrintEmailStatusReadsThePauseBlock(t *testing.T) { |
| + var buf bytes.Buffer |
| + printEmailStatus(&buf, apiEmailReply{ |
| + App: "harbourmail", DailyCap: 50, Active: false, |
| + Provisioned: &apiEmailProvisioned{KeyID: "AKIA1", PauseKind: "rate"}, |
| + Pause: &apiEmailPause{Kind: "cap"}, |
| + Counts: apiEmailCounts{Sends: 51}, |
| + Senders: []apiEmailSender{ |
| + {ID: "app", Scope: "app", Source: "domain", Active: false, PauseKind: "rate", Pause: &apiEmailPause{Kind: "cap"}, DailyCap: 50}, |
| + {ID: "book.example.com", Scope: "instance", Source: "domain", Host: "book.example.com", Active: true, DailyCap: 50}, |
| + }, |
| + }) |
| + out := buf.String() |
| + if !strings.Contains(out, "UTC day roll") { |
| + t.Fatalf("a cap pause block is not described as a cap pause:\n%s", out) |
| + } |
| + if !strings.Contains(out, "paused (cap)") { |
| + t.Fatalf("the sender line does not read the pause block:\n%s", out) |
| + } |
| + |
| + for _, c := range []struct { |
| + p *apiEmailPause |
| + active bool |
| + legacy string |
| + paused bool |
| + kind string |
| + whatIsMissed string |
| + }{ |
| + {nil, true, "", false, "", "an unpaused sender"}, |
| + {nil, false, "rate", true, "rate", "an older console's paused sender"}, |
| + {&apiEmailPause{Kind: "plan"}, false, "plan", true, "plan", "a newer console's paused sender"}, |
| + {&apiEmailPause{}, false, "", true, "", "a manual pause"}, |
| + } { |
| + paused, kind := emailPauseOf(c.p, c.active, c.legacy) |
| + if paused != c.paused || kind != c.kind { |
| + t.Errorf("%s: emailPauseOf = %v %q, want %v %q", c.whatIsMissed, paused, kind, c.paused, c.kind) |
| + } |
| + } |
| +} |