plans: ops exemption drops the mail-pause clean-up (rule 2 cut, #455); no billing problem or house flag for ops (ops-plan-exempt round 2)
Round 2 (NOT READY at 4b28c0b3), stopping rule 2 on the mail family; Paul chose the cut in discussion 455. 1. Cut: settlePlanExempt no longer resumes plan mail pauses (no ListSenders, no resume). None can exist on ops: the flagship has no catalog, and dev's ops was sponsored within a minute of plans going on, before any grace period could end. The exemption stops new ones (EffectiveLimits is nil for ops). The other steps stay: limits record, storage denies on live and trashed apps, cap rows, lapse items. docs/site/plans.md no longer implies everything is undone. 2. No billing problem for ops: openBillingProblemTx, the one statement every billing problem goes through, is now a Store method that opens nothing for the exempt account. Covers the webhook paths outside the hold (subscription missing, unknown customer). 3. The house-flag door answers 409 planCopyExempt for ops before SetBillingHouse.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
6 files changed,
+105
−78
docs/site/plans.md+1 −1internal/console/apibillinghouse.go+6 −2internal/console/billinglate.go+2 −2internal/console/billingstore.go+9 −3internal/console/planexempt.go+6 −41internal/console/planexempt_test.go+81 −29
diff --git a/docs/site/plans.md b/docs/site/plans.md| index d9f62526f..212271f8d 100644 |
| --- a/docs/site/plans.md |
| +++ b/docs/site/plans.md |
| @@ -46,7 +46,7 @@ The console writes each account's limits to `accounts/<sqid>/limits.json` in the |
| ## The ops account |
| -The deployment's built-in ops account (`bab`) is exempt from plans. It has no limits, caps or billing, and owning it doesn't count toward anyone's free account. `carlos accounts plan --account bab` says so and changes nothing. Turning plans on also undoes anything an earlier plan left on it. |
| +The deployment's built-in ops account (`bab`) is exempt from plans. It has no limits, caps or billing, and owning it doesn't count toward anyone's free account. `carlos accounts plan --account bab` says so and changes nothing. If it was under a plan before, the console removes the limits, storage blocks and warnings that plan left. |
| ## Sponsored accounts |
diff --git a/internal/console/apibillinghouse.go b/internal/console/apibillinghouse.go| index 2512b932a..606d95528 100644 |
| --- a/internal/console/apibillinghouse.go |
| +++ b/internal/console/apibillinghouse.go |
| @@ -14,8 +14,8 @@ import ( |
| // handleAPIBillingHouse is POST /api/cli/billing/house, body |
| // {"account":"<sqid>","on":true,"note":"..."}. It goes through billingDoor |
| -// (signed in, operator, billing configured). An empty note is 400 and an |
| -// unknown account 404. It writes an audit line. Clearing an account that is |
| +// (signed in, operator, billing configured). An empty note is 400, an |
| +// unknown account 404, and the exempt ops account 409 (planExempt). It writes an audit line. Clearing an account that is |
| // not marked is not an error. |
| func (s *Server) handleAPIBillingHouse(w http.ResponseWriter, r *http.Request) { |
| addr, ok := s.billingDoor(w, r) |
| @@ -45,6 +45,10 @@ func (s *Server) handleAPIBillingHouse(w http.ResponseWriter, r *http.Request) { |
| http.NotFound(w, r) |
| return |
| } |
| + if s.store.planExempt(acct.ID) { |
| + jsonError(w, http.StatusConflict, planCopyExempt) |
| + return |
| + } |
| if err := s.store.SetBillingHouse(acct.ID, req.On, addr, note, time.Now()); err != nil { |
| log.Printf("console: api billing house %s: %v", acct.Sqid, err) |
| jsonError(w, http.StatusInternalServerError, billingCopyFailed) |
diff --git a/internal/console/billinglate.go b/internal/console/billinglate.go| index adaef288a..5e05689f4 100644 |
| --- a/internal/console/billinglate.go |
| +++ b/internal/console/billinglate.go |
| @@ -156,7 +156,7 @@ func (s *Store) FlagRestoreCheck(opID, detail string, now time.Time) error { |
| if err != nil { |
| return err |
| } |
| - if _, err := openBillingProblemTx(tx, accountID, subscription, problemRestoreAfterRenewal, detail, now); err != nil { |
| + if _, err := s.openBillingProblemTx(tx, accountID, subscription, problemRestoreAfterRenewal, detail, now); err != nil { |
| return err |
| } |
| return tx.Commit() |
| @@ -187,7 +187,7 @@ func (s *Store) StallRestoreCheck(opID string, providerNow, now time.Time) error |
| } |
| detail := fmt.Sprintf("restore %s has not been verified %s after the renewal of %s", opID, |
| restoreStallAfter.String(), time.Unix(periodEnd, 0).UTC().Format(time.RFC3339)) |
| - if _, err := openBillingProblemTx(tx, accountID, subscription, problemRestoreCheckStalled, detail, now); err != nil { |
| + if _, err := s.openBillingProblemTx(tx, accountID, subscription, problemRestoreCheckStalled, detail, now); err != nil { |
| return err |
| } |
| return tx.Commit() |
diff --git a/internal/console/billingstore.go b/internal/console/billingstore.go| index 51fe29544..1d14d8c65 100644 |
| --- a/internal/console/billingstore.go |
| +++ b/internal/console/billingstore.go |
| @@ -493,13 +493,19 @@ func scanBillingProblem(sc interface{ Scan(...any) error }) (*billingProblem, er |
| // OpenBillingProblem opens a problem unless one is already open for the |
| // same account, subscription and reason, and reports whether it opened one. |
| func (s *Store) OpenBillingProblem(accountID int64, subscription, reason, detail string, now time.Time) (bool, error) { |
| - return openBillingProblemTx(s.db, accountID, subscription, reason, detail, now) |
| + return s.openBillingProblemTx(s.db, accountID, subscription, reason, detail, now) |
| } |
| // openBillingProblemTx is OpenBillingProblem's statement on a store handle or |
| // a transaction, so a problem opens in the same transaction as the state it |
| -// reports (a flagged restore check, plans 6b). |
| -func openBillingProblemTx(e sqlExecer, accountID int64, subscription, reason, detail string, now time.Time) (bool, error) { |
| +// reports (a flagged restore check, plans 6b). It is the one place a billing |
| +// problem opens, so the one place the exempt ops account (planExempt) is |
| +// refused one: for it nothing opens and the answer is (false, nil), from a |
| +// webhook path outside the billing hold as from anywhere else. |
| +func (s *Store) openBillingProblemTx(e sqlExecer, accountID int64, subscription, reason, detail string, now time.Time) (bool, error) { |
| + if s.planExempt(accountID) { |
| + return false, nil |
| + } |
| res, err := e.Exec(`INSERT INTO billing_problems (account_id, subscription, reason, detail, opened_at) |
| VALUES (?, ?, ?, ?, ?) ON CONFLICT(account_id, subscription, reason) WHERE closed_at = 0 DO NOTHING`, |
| accountID, subscription, reason, detail, now.Unix()) |
diff --git a/internal/console/planexempt.go b/internal/console/planexempt.go| index d1ee68175..497d10f44 100644 |
| --- a/internal/console/planexempt.go |
| +++ b/internal/console/planexempt.go |
| @@ -9,7 +9,6 @@ import ( |
| "slices" |
| "time" |
| - "github.com/carlosframework/platform/internal/mail" |
| "github.com/carlosframework/platform/internal/plans" |
| ) |
| @@ -54,8 +53,6 @@ var errBillingExempt = billingRefusal{Key: "notice.exempt"} |
| // nothing turned off; |
| // - storage denies (4b) on the stores of every app, live or trashed: |
| // lifted, so a restored app takes uploads again; |
| -// - plan mail pauses (4b, mail.PauseKindPlan) on the senders of every |
| -// app, live or trashed: resumed. Manual, cap and rate pauses stay; |
| // - cap rows (4a), behind warning emails, banners, CLI warning headers |
| // and the after-grace stages: deleted; |
| // - lapse items (4d), behind the turned-off list, its banners and the keep |
| @@ -63,6 +60,12 @@ var errBillingExempt = billingRefusal{Key: "notice.exempt"} |
| // |
| // The account's plan row (its throttle, lapse mark and turned-off column) |
| // is left as it is: nothing reads it for ops, and it is never published. |
| +// |
| +// Plan mail pauses (4b, mail.PauseKindPlan) are not cleared here (Paul's |
| +// cut, discussion 455). None can exist on ops: the flagship has no plan |
| +// catalog, and dev's ops was sponsored within a minute of plans going on, |
| +// before any grace period could end. The exemption stops new ones, since |
| +// enforceAccountMail reads EffectiveLimits, which is nil for ops. |
| func (s *Server) settlePlanExempt(ctx context.Context, now time.Time) { |
| if s.planExemptSettled.Load() { |
| return |
| @@ -107,9 +110,6 @@ func (s *Server) clearPlanState(ctx context.Context, acct *Account, now time.Tim |
| if err := s.convergeStoresOf(ctx, acct, apps, false, capState{}, now); err != nil { |
| errs = append(errs, fmt.Errorf("storage deny: %w", err)) |
| } |
| - if err := s.resumePlanPauses(ctx, acct, apps, now); err != nil { |
| - errs = append(errs, fmt.Errorf("mail: %w", err)) |
| - } |
| states, err := s.store.CapStates(acct.ID) |
| if err != nil { |
| errs = append(errs, fmt.Errorf("cap rows: %w", err)) |
| @@ -135,38 +135,3 @@ func (s *Server) clearPlanState(ctx context.Context, acct *Account, now time.Tim |
| } |
| return errors.Join(errs...) |
| } |
| - |
| -// resumePlanPauses resumes every sender of the apps that a plan paused |
| -// (mail.PauseKindPlan), whatever the day. The pause kind is asked again of |
| -// the sender as read inside the mutation, so a pause taken since (manual, |
| -// cap, rate) stays. |
| -func (s *Server) resumePlanPauses(ctx context.Context, acct *Account, apps []string, now time.Time) error { |
| - if s.cfg.MailProvisioner == nil { |
| - return nil |
| - } |
| - planPaused := func(sn *mail.Sender) bool { |
| - return sn != nil && sn.Provisioned != nil && !sn.Provisioned.Active && sn.Provisioned.PauseKind == mail.PauseKindPlan |
| - } |
| - var errs []error |
| - for _, app := range apps { |
| - senders, err := mail.ListSenders(ctx, s.cfg.Releases, acct.Sqid, app) |
| - if err != nil { |
| - errs = append(errs, fmt.Errorf("%s: %w", app, err)) |
| - continue |
| - } |
| - for _, sn := range senders { |
| - if !planPaused(sn) { |
| - continue |
| - } |
| - _, err := mail.SetSenderPausedIf(ctx, s.claimMailStore(), s.cfg.MailProvisioner, acct.Sqid, app, sn.ID, false, "", now, planPaused) |
| - switch { |
| - case errors.Is(err, mail.ErrNotEligible): |
| - case err != nil: |
| - errs = append(errs, fmt.Errorf("%s/%s: %w", app, sn.ID, err)) |
| - default: |
| - s.audit(acct, app, "ses", "mail.resumed", fmt.Sprintf("%s: plan pause lifted; this account is exempt from plans", mailAuditSubject(app, sn.ID))) |
| - } |
| - } |
| - } |
| - return errors.Join(errs...) |
| -} |
diff --git a/internal/console/planexempt_test.go b/internal/console/planexempt_test.go| index f123f8cb9..d72c0570a 100644 |
| --- a/internal/console/planexempt_test.go |
| +++ b/internal/console/planexempt_test.go |
| @@ -14,9 +14,7 @@ import ( |
| "testing" |
| "time" |
| - "github.com/carlosframework/platform/internal/mail" |
| "github.com/carlosframework/platform/internal/plans" |
| - "github.com/carlosframework/platform/internal/release" |
| ) |
| // The ops account is exempt from plans (Paul, 2026-10-28): no limits |
| @@ -306,6 +304,7 @@ var planExemptSites = []string{ |
| // Billing (req 4). |
| "billingPageInputFor", |
| "billingTeam", "handleUpgrade", "billingOwner", "holdBilling", |
| + "openBillingProblemTx", "handleAPIBillingHouse", |
| // The plan door (req 5). |
| "accountPlanDoor", |
| } |
| @@ -421,16 +420,14 @@ func (s *Server) EffectiveLimits(acct *Account) {} |
| // TestPlanExemptSettleClearsEveryPlanState: everything a plan stage put on |
| // ops before the exemption is gone after one limits pass: the limits record |
| // (with its throttle and turned-off hosts), a storage deny, even on an app |
| -// trashed since, a plan mail pause, cap rows behind warnings and banners, |
| -// and lapse items. Manual and cap mail pauses stay; the plan row stays. |
| +// trashed since, cap rows behind warnings and banners, and lapse items. |
| +// The plan row stays. Plan mail pauses are not cleared (discussion 455). |
| func TestPlanExemptSettleClearsEveryPlanState(t *testing.T) { |
| fx, fake, ops := deniedStoreFixture(t, "code-plan-exempt-settle") // ops governed |
| ctx := context.Background() |
| - fx.srv.cfg.MailProvisioner = mail.NewMemProvisioner("1") |
| - addMailApp(t, fx, "blog") |
| - addMailApp(t, fx, "shop") |
| + capsApp(t, fx, ops, "blog") |
| - // Under a plan: a storage deny on titogo, a plan pause on every sender. |
| + // Under a plan: a storage deny on titogo. |
| st := storageRow(ops, 3_000_000_000, 1_000_000_000, planNow) |
| if err := fx.srv.convergeStorageDeny(ctx, ops, st, true, planNow); err != nil { |
| t.Fatal(err) |
| @@ -438,18 +435,6 @@ func TestPlanExemptSettleClearsEveryPlanState(t *testing.T) { |
| if rec := titogoStore(t, fx); rec.WritesDenied == nil || !hasWriteDeny(t, fake.PolicyFor(rec.Provisioned.IAMUser)) { |
| t.Fatal("setup: no deny") |
| } |
| - sends(t, fx, "blog", 200, planMailNow) |
| - if paused, err := fx.srv.enforceAccountMail(ctx, release.OpsAccount, planMailNow); err != nil || !paused { |
| - t.Fatalf("setup: plan pause = %v, %v", paused, err) |
| - } |
| - // shop's pause is an operator's (no kind): settlement keeps it. |
| - if _, err := mail.SetSenderPausedIf(ctx, fx.srv.claimMailStore(), fx.srv.cfg.MailProvisioner, release.OpsAccount, "shop", |
| - mail.SenderAppID, true, "", planMailNow, func(*mail.Sender) bool { return true }); err != nil { |
| - t.Fatal(err) |
| - } |
| - if p := senderState(t, fx, "shop"); p.Active || p.PauseKind != "" { |
| - t.Fatalf("setup: shop = %+v, want a manual pause", p) |
| - } |
| if err := fx.st.PutCapState(capState{AccountID: ops.ID, Cap: capEmail, Level: levelOver, Used: 200, Limit: 50, |
| Period: capPeriod(capEmail, planNow), FirstOver: planNow, GraceEnds: planNow, UpdatedAt: planNow}); err != nil { |
| t.Fatal(err) |
| @@ -478,12 +463,6 @@ func TestPlanExemptSettleClearsEveryPlanState(t *testing.T) { |
| if r, _ := readLimits(t, fx, ops.Sqid); r != nil { |
| t.Errorf("limits record left: %+v", r) |
| } |
| - if p := senderState(t, fx, "blog"); !p.Active { |
| - t.Errorf("blog's plan pause left: %+v", p) |
| - } |
| - if p := senderState(t, fx, "shop"); p.Active { |
| - t.Errorf("shop's manual pause was lifted: %+v", p) |
| - } |
| if states, _ := fx.st.CapStates(ops.ID); len(states) != 0 { |
| t.Errorf("cap rows left: %+v", states) |
| } |
| @@ -506,9 +485,6 @@ func TestPlanExemptSettleClearsEveryPlanState(t *testing.T) { |
| if !fx.srv.planExemptSettled.Load() { |
| t.Fatal("a settlement with nothing to clear did not finish") |
| } |
| - if p := senderState(t, fx, "shop"); p.Active { |
| - t.Errorf("shop's manual pause was lifted on the second pass: %+v", p) |
| - } |
| } |
| // ---- round 1: the billing entry --------------------------------------------- |
| @@ -656,3 +632,79 @@ func TestBillingDoorsEnterThroughBillingOwner(t *testing.T) { |
| t.Error("billingOwner does not ask planExempt") |
| } |
| } |
| + |
| +// ---- round 2: no billing problem and no house flag for ops ------------------- |
| + |
| +// TestPlanExemptOpensNoBillingProblem: the webhook's two problem paths that |
| +// run outside the billing hold (a subscription the provider no longer has, |
| +// and a customer bound to no account) open nothing for ops, and the store's |
| +// one problem-opening statement refuses it; other accounts still get theirs. |
| +func TestPlanExemptOpensNoBillingProblem(t *testing.T) { |
| + bf := newBillingFixture(t) |
| + bf.ready(t) |
| + ctx := context.Background() |
| + ops, err := bf.st.AccountByID(OpsAccountID) |
| + if err != nil || ops == nil { |
| + t.Fatalf("ops: %v", err) |
| + } |
| + |
| + // Before the exemption: ops bought a plan, so its subscription is live. |
| + governOps(bf.consoleFixture) |
| + if err := bf.st.SetBillingCustomer(ops.ID, "cus_ops", billingNow); err != nil { |
| + t.Fatal(err) |
| + } |
| + live := testPersonalSub("sub_ops", "cus_ops", ops.Sqid, "active", nil) |
| + bf.put(live) |
| + if err := bf.srv.syncSubscription(ctx, "sub_ops", billingNow); err != nil { |
| + t.Fatal(err) |
| + } |
| + if b, _ := bf.st.BillingAccount(ops.ID); b == nil || b.Subscription != "sub_ops" || !liveState(b.State) { |
| + t.Fatalf("setup: ops billing row = %+v", b) |
| + } |
| + |
| + // The exemption ships; the provider loses the subscription, and an |
| + // unbound customer's subscription names ops. |
| + bf.st.planExemptID = OpsAccountID |
| + bf.fake.Lock() |
| + delete(bf.fake.Subs, "sub_ops") |
| + bf.fake.Unlock() |
| + if err := bf.srv.syncSubscription(ctx, "sub_ops", billingNow); err != nil { |
| + t.Fatal(err) |
| + } |
| + stranger := testPersonalSub("sub_stranger", "cus_stranger", ops.Sqid, "active", nil) |
| + bf.put(stranger) |
| + if err := bf.srv.syncSubscription(ctx, "sub_stranger", billingNow); err != nil { |
| + t.Fatal(err) |
| + } |
| + if opened, err := bf.st.OpenBillingProblem(ops.ID, "sub_x", problemOperationUnresolved, "x", billingNow); err != nil || opened { |
| + t.Fatalf("OpenBillingProblem(ops) = %v, %v", opened, err) |
| + } |
| + open, _ := bf.st.BillingProblems(false) |
| + for _, p := range open { |
| + if p.AccountID == ops.ID { |
| + t.Errorf("a problem was opened for ops: %+v", p) |
| + } |
| + } |
| + |
| + // Another account's problems still open. |
| + other := testPersonalSub("sub_other", "cus_other", bf.acct.Sqid, "active", nil) |
| + bf.put(other) |
| + if err := bf.srv.syncSubscription(ctx, "sub_other", billingNow); err != nil { |
| + t.Fatal(err) |
| + } |
| + if reasons, _ := bf.st.OpenBillingProblemReasons(bf.acct.ID); !slices.Contains(reasons, problemUnknownCustomer) { |
| + t.Fatalf("another account's unknown-customer problem = %v", reasons) |
| + } |
| +} |
| + |
| +func TestPlanExemptHouseDoorRefuses(t *testing.T) { |
| + bf := newBillingFixture(t) |
| + op := planOperatorToken(t, bf.consoleFixture, "ops@keymail.dev", "code-house-exempt") |
| + status, out := postJSON(t, billingHouseURL(bf.consoleFixture), map[string]any{"account": opsSqid, "on": true, "note": "ours"}, op) |
| + if status != http.StatusConflict || out["error"] != planCopyExempt { |
| + t.Fatalf("house for ops = %d %v, want 409 %q", status, out, planCopyExempt) |
| + } |
| + if house, err := bf.st.BillingHouse(OpsAccountID); err != nil || house { |
| + t.Fatalf("ops marked as a house account: %v %v", house, err) |
| + } |
| +} |