carlos / platform Public

plans: grandfather object storage approved before a plan's cutoff

Summary

A plan's kept_storage names a cutoff and a figure. Once per console start and catalog, the limits pass records the figure on the plan row (new kept column) of every account on that plan whose object storage an operator approved before the cutoff, and the account's limits merge it in from then on (EffectiveLimits, the published record, the billing page and the operator's accounts plan reply). Under draft catalog 2026-11-15-v1 a Free account approved before 2026-11-15 keeps 1 GB where Free now gives 50 MB. Kept is not a sponsorship and exempts the account from nothing.
Agent notes
On the flagship (read 2026-11-15) 12 accounts are approved; 11 are launch-sponsored with unlimited limits, so bgc (approved 2026-11-13, Free, default) is the one the rule changes. Only ever raises; a catalog without kept_storage reads no entitlement.
claude agent, operated by paul@keymail.dev d247a34d70d5be8c6f18d45e8fa690dac37de09c parent e3e653e
9 files changed, +264 −8
  • internal/console/accountplans.go +36 −3
  • internal/console/apiaccountplan.go +3 −0
  • internal/console/apistore.go +1 −1
  • internal/console/billingpage.go +3 −2
  • internal/console/plankept.go +123 −0
  • internal/console/plankept_test.go +86 −0
  • internal/console/planlimits.go +5 −2
  • internal/console/server.go +3 −0
  • internal/console/store.go +4 −0
diff --git a/internal/console/accountplans.go b/internal/console/accountplans.go
index 852e94874..fcf78c4b5 100644
--- a/internal/console/accountplans.go
+++ b/internal/console/accountplans.go
@@ -49,21 +49,32 @@ type AccountPlanRow struct {
LapseSince time.Time
// Disabled lists the hosts a lapse turned off, sorted; nil for none.
Disabled []string
+ // Kept is what the account keeps from its plan's earlier offer
+ // (#1076, plankept.go), merged into its limits by Limits.WithKept;
+ // nil for none. Not a sponsorship: it exempts the account from
+ // nothing.
+ Kept *plans.Limits
}
const accountPlanColumns = `p.account_id, a.sqid, p.plan, p.packs, p.source, p.sponsored, p.reason,
p.revision, p.published_revision, p.published_catalog, p.updated_by, p.updated_at, p.throttle,
- p.lapse_since, p.disabled`
+ p.lapse_since, p.disabled, p.kept`
func scanAccountPlan(sc interface{ Scan(...any) error }) (*AccountPlanRow, error) {
var r AccountPlanRow
- var packs, sponsored, throttle, disabled string
+ var packs, sponsored, throttle, disabled, kept string
var updated, lapse int64
if err := sc.Scan(&r.AccountID, &r.Sqid, &r.Plan, &packs, &r.Source, &sponsored, &r.Reason,
&r.Revision, &r.PublishedRevision, &r.PublishedCatalog, &r.UpdatedBy, &updated, &throttle,
- &lapse, &disabled); err != nil {
+ &lapse, &disabled, &kept); err != nil {
return nil, err
}
+ if kept != "" {
+ r.Kept = &plans.Limits{}
+ if err := json.Unmarshal([]byte(kept), r.Kept); err != nil {
+ return nil, err
+ }
+ }
if lapse != 0 {
r.LapseSince = time.Unix(lapse, 0)
}
@@ -407,6 +418,28 @@ func (s *Store) SetAccountThrottle(accountID int64, t *plans.Throttle, now time.
return n > 0, err
}
+// SetAccountKept records what the account keeps from its plan's earlier
+// offer (#1076) and bumps the revision so the publish pass carries it. An
+// unchanged value writes nothing. An account with no plan row is
+// (false, nil).
+func (s *Store) SetAccountKept(accountID int64, kept *plans.Limits, now time.Time) (bool, error) {
+ val := ""
+ if kept != nil {
+ b, err := json.Marshal(kept)
+ if err != nil {
+ return false, err
+ }
+ val = string(b)
+ }
+ res, err := s.db.Exec(`UPDATE account_plans SET kept = ?, revision = revision + 1, updated_at = ?
+ WHERE account_id = ? AND kept <> ?`, val, now.Unix(), accountID, val)
+ if err != nil {
+ return false, err
+ }
+ n, err := res.RowsAffected()
+ return n > 0, err
+}
+
// MarkLapse records that the account's limits fell from a paid assignment
// at now (plans 4d). Each fall moves it.
func (s *Store) MarkLapse(accountID int64, now time.Time) error {
diff --git a/internal/console/apiaccountplan.go b/internal/console/apiaccountplan.go
index cebda5310..c0bd72875 100644
--- a/internal/console/apiaccountplan.go
+++ b/internal/console/apiaccountplan.go
@@ -176,7 +176,9 @@ func (s *Server) accountPlanReplyFor(acct *Account) (accountPlanReply, error) {
}
reply := accountPlanReply{Account: acct.Sqid, CatalogID: cat.ID(), Plan: cat.DefaultPlan,
Packs: map[string]int{}, Source: PlanSourceDefault}
+ var kept *plans.Limits
if row != nil {
+ kept = row.Kept
reply.Plan, reply.Packs, reply.Source = row.Plan, row.Packs, row.Source
reply.Sponsored, reply.Reason, reply.UpdatedBy = row.Sponsored, row.Reason, row.UpdatedBy
reply.Revision, reply.PublishedRevision = row.Revision, row.PublishedRevision
@@ -187,6 +189,7 @@ func (s *Server) accountPlanReplyFor(acct *Account) (accountPlanReply, error) {
if l, err := cat.Effective(reply.Plan, reply.Packs, reply.Sponsored); err != nil {
reply.LimitsError = err.Error()
} else {
+ l = l.WithKept(kept)
reply.Limits = &l
}
return reply, nil
diff --git a/internal/console/apistore.go b/internal/console/apistore.go
index 13a6c62d5..77f0d368c 100644
--- a/internal/console/apistore.go
+++ b/internal/console/apistore.go
@@ -250,7 +250,7 @@ func (s *Server) storeReply(ctx context.Context, acct *Account, app, env string,
// self_serve: the account's plan lets the member's own `carlos
// store create` provision this store with no approval (#1076). A
// CLI that predates it reads the entitlement alone, as before.
- "self_serve": selfServe,
+ "self_serve": selfServe,
"backend": s.cfg.StoreDeployment.Backend,
"declared": rec.Declared,
"grant": rec.Grant,
diff --git a/internal/console/billingpage.go b/internal/console/billingpage.go
index e2e354ff5..5d03883d6 100644
--- a/internal/console/billingpage.go
+++ b/internal/console/billingpage.go
@@ -420,9 +420,9 @@ func (s *Server) billingViewFor(r *http.Request, in billingPageInput, st billing
// The summary.
plan, packs := cat.DefaultPlan, map[string]int(nil)
- var sponsored *plans.Limits
+ var sponsored, kept *plans.Limits
if in.Row != nil {
- plan, packs, sponsored = in.Row.Plan, in.Row.Packs, in.Row.Sponsored
+ plan, packs, sponsored, kept = in.Row.Plan, in.Row.Packs, in.Row.Sponsored, in.Row.Kept
}
v.Plan = s.planLabel(plan)
if p, ok := cat.Plans[plan]; ok {
@@ -432,6 +432,7 @@ func (s *Server) billingViewFor(r *http.Request, in billingPageInput, st billing
}
// The limits the account has now.
if l, err := cat.Effective(plan, held, sponsored); err == nil {
+ l = l.WithKept(kept)
maxMembers := p.MaxMembers
if in.Row != nil && planRowSponsored(in.Row) {
maxMembers = 0 // checkMemberCap passes a sponsored row
diff --git a/internal/console/plankept.go b/internal/console/plankept.go
new file mode 100644
index 000000000..3048f8828
--- /dev/null
+++ b/internal/console/plankept.go
@@ -0,0 +1,123 @@
+package console
+
+import (
+ "context"
+ "fmt"
+ "log"
+ "slices"
+ "strings"
+ "time"
+
+ "github.com/carlosframework/platform/internal/objstore"
+ "github.com/carlosframework/platform/internal/plans"
+ "github.com/carlosframework/platform/internal/release"
+)
+
+// settleKeptStorage grandfathers object storage (#1076): every account on a
+// plan with kept_storage (plans.Plan.KeptStorage) whose object storage an
+// operator approved before the plan's cutoff keeps the plan's earlier
+// storage, recorded on its plan row (SetAccountKept) and merged into its
+// limits from then on. Free's catalog 2026-11-15-v1 is the first: 1 GB for
+// an account approved before 2026-11-15, where the plan now gives 50 MB.
+//
+// The cutoff is in the past and an approval is never back-dated, so the
+// set of accounts is closed: this runs once per console start and catalog,
+// from the first limits pass, and is done only once every account it found
+// was recorded. A failure leaves it not done, and the next pass retries.
+// It only ever raises what is kept: an account that moves to another plan
+// keeps the figure, and a later catalog without kept_storage leaves it as
+// it is. Writes are revision bumps, so the publish pass that follows in the
+// same run carries them to the record.
+func (s *Server) settleKeptStorage(ctx context.Context, now time.Time) {
+ cat := s.cfg.PlanCatalog
+ if cat == nil || s.cfg.Releases == nil {
+ return
+ }
+ id := cat.ID()
+ if done := s.keptSettled.Load(); done != nil && *done == id {
+ return
+ }
+ if err := s.keepApprovedStorage(ctx, cat, now); err != nil {
+ log.Printf("console: plan limits: kept storage: %v", err)
+ jobFailed(ctx, fmt.Errorf("plan limits: kept storage: %w", err))
+ return
+ }
+ s.keptSettled.Store(&id)
+}
+
+func (s *Server) keepApprovedStorage(ctx context.Context, cat *plans.Catalog, now time.Time) error {
+ if !slices.ContainsFunc(sortedPlanKeys(cat), func(name string) bool { return cat.Plans[name].KeptStorage != nil }) {
+ return nil
+ }
+ keys, err := s.cfg.Releases.List(ctx, "store/entitlements/")
+ if err != nil {
+ return fmt.Errorf("list entitlements: %w", err)
+ }
+ slices.Sort(keys)
+ for _, key := range keys {
+ const prefix, suffix = "store/entitlements/", ".json"
+ sqid := strings.TrimSuffix(strings.TrimPrefix(key, prefix), suffix)
+ if !strings.HasPrefix(key, prefix) || !strings.HasSuffix(key, suffix) ||
+ !release.ValidAccount(sqid) || objstore.EntitlementKey(sqid) != key {
+ continue
+ }
+ e, _, err := objstore.GetEntitlement(ctx, s.cfg.Releases, sqid)
+ if err != nil {
+ return fmt.Errorf("%s: %w", key, err)
+ }
+ if !e.Approved() {
+ continue
+ }
+ acct, err := s.store.AccountBySqid(sqid)
+ if err != nil {
+ return fmt.Errorf("account %s: %w", sqid, err)
+ }
+ if acct == nil || s.store.planExempt(acct.ID) {
+ continue
+ }
+ row, err := s.store.AccountPlan(acct.ID)
+ if err != nil {
+ return fmt.Errorf("account %s: plan: %w", sqid, err)
+ }
+ plan := cat.DefaultPlan
+ if row != nil {
+ plan = row.Plan
+ }
+ k := cat.Plans[plan].KeptStorage
+ if k == nil || !e.Decision.At.Before(k.Cutoff()) {
+ continue
+ }
+ if row != nil && row.Kept != nil && row.Kept.StorageBytes >= k.StorageBytes {
+ continue
+ }
+ if row == nil {
+ // Every account has a row once EnsureDefaultPlans ran this
+ // pass; one that has none yet is kept on the next.
+ return fmt.Errorf("account %s has no plan row yet", sqid)
+ }
+ kept := plans.Limits{}
+ if row.Kept != nil {
+ kept = *row.Kept
+ }
+ kept.StorageBytes = k.StorageBytes
+ changed, err := s.store.SetAccountKept(acct.ID, &kept, now)
+ if err != nil {
+ return fmt.Errorf("account %s: %w", sqid, err)
+ }
+ if changed {
+ s.audit(acct, "", "plans", "plan.kept",
+ fmt.Sprintf("kept %d bytes of object storage, approved before %s", k.StorageBytes, k.ApprovedBefore))
+ }
+ }
+ return nil
+}
+
+// sortedPlanKeys is the catalog's plan keys in order.
+func sortedPlanKeys(cat *plans.Catalog) []string {
+ keys := make([]string, 0, len(cat.Plans))
+ for k := range cat.Plans {
+ keys = append(keys, k)
+ }
+ slices.Sort(keys)
+ return keys
+}
diff --git a/internal/console/plankept_test.go b/internal/console/plankept_test.go
new file mode 100644
index 000000000..b12a9b5ff
--- /dev/null
+++ b/internal/console/plankept_test.go
@@ -0,0 +1,86 @@
+package console
+
+import (
+ "context"
+ "testing"
+ "time"
+
+ "github.com/carlosframework/platform/internal/objstore"
+)
+
+func approveAt(t *testing.T, fx *consoleFixture, sqid string, at time.Time) {
+ t.Helper()
+ if _, err := objstore.PutEntitlement(context.Background(), fx.rel, sqid, &objstore.Entitlement{
+ Applied: &objstore.Applied{By: "owner@example.com", At: at.Add(-time.Hour)},
+ Decision: &objstore.Decision{Approved: true, By: "op@keymail.dev", At: at},
+ }, ""); err != nil {
+ t.Fatal(err)
+ }
+}
+
+// Free accounts approved for object storage before the draft's cutoff keep
+// 1 GB (#1076); later approvals, pending applications and other plans keep
+// nothing, and the kept figure reaches the published record.
+func TestKeptStorageGrandfathersApprovedFreeAccounts(t *testing.T) {
+ fx := newConsoleFixture(t)
+ fx.srv.cfg.PlanCatalog = draftFreeStorageCatalog(t)
+ cutoff := time.Date(2026, 11, 15, 0, 0, 0, 0, time.UTC)
+
+ early := planAccount(t, fx, 601) // Free, approved before the cutoff: keeps 1 GB
+ late := planAccount(t, fx, 602) // Free, approved after it: 50 MB
+ pending := planAccount(t, fx, 603) // Free, applied, undecided: 50 MB
+ paid := planAccount(t, fx, 604) // Personal, approved early: its own 25 GB
+ approveAt(t, fx, early.Sqid, cutoff.Add(-48*time.Hour))
+ approveAt(t, fx, late.Sqid, cutoff.Add(time.Hour))
+ if _, err := objstore.PutEntitlement(context.Background(), fx.rel, pending.Sqid, &objstore.Entitlement{
+ Applied: &objstore.Applied{By: "x@example.com", At: cutoff.Add(-72 * time.Hour)},
+ }, ""); err != nil {
+ t.Fatal(err)
+ }
+ approveAt(t, fx, paid.Sqid, cutoff.Add(-48*time.Hour))
+ if _, err := fx.st.SetAccountPlan(paid.ID, "personal", nil, PlanSourceOperator, nil, "test", "op", time.Now()); err != nil {
+ t.Fatal(err)
+ }
+
+ now := time.Now()
+ fx.srv.planLimitsPass(context.Background(), now)
+ fx.srv.planLimitsPass(context.Background(), now) // idempotent
+
+ want := map[*Account]int64{early: 1 << 30, late: 50 << 20, pending: 50 << 20, paid: 25 << 30}
+ for acct, bytes := range want {
+ l, err := fx.srv.EffectiveLimits(acct)
+ if err != nil || l == nil || l.StorageBytes != bytes {
+ t.Errorf("%s: effective storage %v, %v; want %d", acct.Sqid, l, err, bytes)
+ }
+ rec, _ := readLimits(t, fx, acct.Sqid)
+ if rec == nil || rec.Limits.StorageBytes != bytes {
+ t.Errorf("%s: published record %+v, want storage %d", acct.Sqid, rec, bytes)
+ }
+ }
+ row, err := fx.st.AccountPlan(early.ID)
+ if err != nil || row.Kept == nil || row.Kept.StorageBytes != 1<<30 || planRowSponsored(row) {
+ t.Fatalf("early row = %+v, %v; want 1 GB kept and not sponsored", row, err)
+ }
+ // Kept is the earlier offer, not a sponsorship: the scan requirement
+ // and the hourly mail limit still hold.
+ if l, _ := fx.srv.EffectiveLimits(early); !l.StorageScanRequired || l.EmailPerHour != 5 {
+ t.Errorf("kept account lost the plan's other limits: %+v", l)
+ }
+ for _, acct := range []*Account{late, pending, paid} {
+ if row, _ := fx.st.AccountPlan(acct.ID); row.Kept != nil {
+ t.Errorf("%s kept %+v", acct.Sqid, row.Kept)
+ }
+ }
+}
+
+// A catalog without kept_storage settles nothing and reads no entitlement.
+func TestKeptStorageNeedsTheCatalogField(t *testing.T) {
+ fx := newConsoleFixture(t)
+ fx.srv.cfg.PlanCatalog = examplePlanCatalog(t)
+ acct := planAccount(t, fx, 611)
+ approveAt(t, fx, acct.Sqid, time.Date(2026, 11, 1, 0, 0, 0, 0, time.UTC))
+ fx.srv.planLimitsPass(context.Background(), time.Now())
+ if row, _ := fx.st.AccountPlan(acct.ID); row == nil || row.Kept != nil {
+ t.Fatalf("row = %+v, want a row with nothing kept", row)
+ }
+}
diff --git a/internal/console/planlimits.go b/internal/console/planlimits.go
index bd5b96246..3d67fae6b 100644
--- a/internal/console/planlimits.go
+++ b/internal/console/planlimits.go
@@ -50,6 +50,7 @@ func (s *Server) planLimitsPass(ctx context.Context, now time.Time) {
return
}
s.settlePlanExempt(ctx, now)
+ s.settleKeptStorage(ctx, now)
// Page by account id: a row that cannot be published (an orphaned
// assignment) stays due, but never holds a later account back.
var after int64
@@ -94,6 +95,7 @@ func (s *Server) publishAccountLimits(ctx context.Context, row AccountPlanRow, n
if err != nil {
return err
}
+ limits = limits.WithKept(row.Kept)
rec := &plans.Record{
Account: row.Sqid,
CatalogID: cat.ID(),
@@ -138,14 +140,15 @@ func (s *Server) EffectiveLimits(acct *Account) (*plans.Limits, error) {
return nil, err
}
plan, packs := cat.DefaultPlan, map[string]int(nil)
- var sponsored *plans.Limits
+ var sponsored, kept *plans.Limits
if row != nil {
- plan, packs, sponsored = row.Plan, row.Packs, row.Sponsored
+ plan, packs, sponsored, kept = row.Plan, row.Packs, row.Sponsored, row.Kept
}
l, err := cat.Effective(plan, packs, sponsored)
if err != nil {
return nil, err
}
+ l = l.WithKept(kept)
return &l, nil
}
diff --git a/internal/console/server.go b/internal/console/server.go
index 0d19a143c..453468969 100644
--- a/internal/console/server.go
+++ b/internal/console/server.go
@@ -1112,6 +1112,9 @@ type Server struct {
// planExemptSettled is set once settlePlanExempt has cleared what plans
// left on the exempt account in this process (planexempt.go).
planExemptSettled atomic.Bool
+ // keptSettled is the catalog ID settleKeptStorage last finished for
+ // (#1076): once per console start and catalog.
+ keptSettled atomic.Pointer[string]
claimLossPageMu sync.Mutex
claimLossPageDone chan struct{}
diff --git a/internal/console/store.go b/internal/console/store.go
index e6038ad3a..ebaf04747 100644
--- a/internal/console/store.go
+++ b/internal/console/store.go
@@ -563,6 +563,10 @@ func (s *Store) migrate() error {
// JSON, "" for none.
`ALTER TABLE account_plans ADD COLUMN lapse_since INTEGER NOT NULL DEFAULT 0;`,
`ALTER TABLE account_plans ADD COLUMN disabled TEXT NOT NULL DEFAULT '';`,
+ // #1076: what the account keeps from its plan's earlier offer
+ // (plans.Plan.KeptStorage, settled by plankept.go), JSON limits,
+ // "" for none.
+ `ALTER TABLE account_plans ADD COLUMN kept TEXT NOT NULL DEFAULT '';`,
// deliveries (tenant-email, ruling 2a): SES already publishes
// Delivery to the event destination and the ingest already
// verifies and parses it — this stops throwing the answer away.