carlos / platform Public

console: one lapse-fall rule shared by plan and membership changes (plans 6b)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev 11958b6c7a25bfcc25e8815f2475a35e6478ee67 parent 0d8b37f
3 files changed, +151 −57
  • internal/console/planlapse.go +55 −30
  • internal/console/planmembers.go +16 −27
  • internal/console/planmembers_test.go +80 −0
diff --git a/internal/console/planlapse.go b/internal/console/planlapse.go
index 320326314..e586beecf 100644
--- a/internal/console/planlapse.go
+++ b/internal/console/planlapse.go
@@ -22,13 +22,12 @@ func planRowSponsored(row *AccountPlanRow) bool {
}
// lapseStarted reports whether moving from old to the new assignment starts
-// a lapse: plans are on, the new assignment is not sponsored, the old one
-// was paid (a paid plan or any pack, and not sponsored), and a limit the
-// lapse lifecycle governs fell. An old assignment the catalog can no longer
-// price starts nothing. Team plans are compared at their member counts
-// (plans 6b): the old row's seats, or the live count when it has none, and
-// seats, or the live count, for the new assignment; a count that cannot be
-// read starts nothing.
+// a lapse: plans are on, neither assignment is sponsored, and paidFall
+// holds. Team plans are compared at their member counts (plans 6b): the
+// old row's seats, or the live count when it has none, and seats, or the
+// live count, for the new assignment. The live count is read at most once,
+// so both sides see the same figure, and only when a side is a team plan;
+// a count that cannot be read starts nothing.
func (s *Server) lapseStarted(accountID int64, old *AccountPlanRow, plan string, packs map[string]int, seats int, sponsored *plans.Limits) bool {
cat := s.cfg.PlanCatalog
if cat == nil || sponsored != nil {
@@ -41,6 +40,38 @@ func (s *Server) lapseStarted(accountID int64, old *AccountPlanRow, plan string,
}
oldPlan, oldPacks, oldSeats = old.Plan, old.Packs, old.Seats
}
+ live, counted := 0, false
+ count := func() (int, error) {
+ if !counted {
+ n, err := s.store.MemberCount(accountID)
+ if err != nil {
+ return 0, err
+ }
+ live, counted = n, true
+ }
+ return live, nil
+ }
+ wasMembers, err := teamMembers(cat, oldPlan, oldSeats, count)
+ if err != nil {
+ log.Printf("console: plan lapse: member count for account %d: %v", accountID, err)
+ return false
+ }
+ nextMembers, err := teamMembers(cat, plan, seats, count)
+ if err != nil {
+ log.Printf("console: plan lapse: member count for account %d: %v", accountID, err)
+ return false
+ }
+ return paidFall(cat, oldPlan, oldPacks, wasMembers, plan, packs, nextMembers)
+}
+
+// paidFall is the one lapse-fall rule (plans 4d), shared by plan changes
+// (lapseStarted) and membership changes (membershipFell): the old
+// assignment was paid (a paid plan or any pack) and a limit the lapse
+// lifecycle governs fell from the old assignment at wasMembers to the new
+// one at nextMembers. An assignment the catalog cannot price starts
+// nothing. Callers rule out sponsored assignments first.
+func paidFall(cat *plans.Catalog, oldPlan string, oldPacks map[string]int, wasMembers int,
+ plan string, packs map[string]int, nextMembers int) bool {
p, ok := cat.Plans[oldPlan]
if !ok {
return false
@@ -54,29 +85,6 @@ func (s *Server) lapseStarted(accountID int64, old *AccountPlanRow, plan string,
if !paid {
return false
}
- live := -1
- members := func(seats int) (int, bool) {
- if seats > 0 {
- return seats, true
- }
- if live < 0 {
- n, err := s.store.MemberCount(accountID)
- if err != nil {
- log.Printf("console: plan lapse: member count for account %d: %v", accountID, err)
- return 0, false
- }
- live = n
- }
- return live, true
- }
- wasMembers, ok := members(oldSeats)
- if !ok {
- return false
- }
- nextMembers, ok := members(seats)
- if !ok {
- return false
- }
was, err := cat.Effective(oldPlan, oldPacks, nil, wasMembers)
if err != nil {
return false
@@ -88,6 +96,23 @@ func (s *Server) lapseStarted(accountID int64, old *AccountPlanRow, plan string,
return plans.LimitsFell(was, next)
}
+// teamMembers is the member count plan is limited at (plans 6b): seats
+// when billing wrote some, else count(). A plan that is not a team plan
+// (or that the catalog does not have) is 0 without counting: Effective
+// ignores the count there.
+func teamMembers(cat *plans.Catalog, plan string, seats int, count func() (int, error)) (int, error) {
+ if cat == nil {
+ return 0, nil
+ }
+ if p, ok := cat.Plans[plan]; !ok || p.Team == nil {
+ return 0, nil
+ }
+ if seats > 0 {
+ return seats, nil
+ }
+ return count()
+}
+
// applyAccountPlan is the one way an operator changes an account's plan.
// The operator door uses it; billing uses applyBillingPlan, which shares
// its transition (transitionAccountPlan), so a paid account whose limits
diff --git a/internal/console/planmembers.go b/internal/console/planmembers.go
index 0211e6aec..38d00d09a 100644
--- a/internal/console/planmembers.go
+++ b/internal/console/planmembers.go
@@ -35,14 +35,20 @@ func memberCountOn(q sqlQueryRower, accountID int64) (int, error) {
return n, err
}
-// planMembers is the member count a plan row is limited at: its seats
-// when billing wrote some, else the members counted now. A nil row (the
-// default plan) counts the members.
+// planMembers is the member count a plan row is limited at: on a team
+// plan, its seats when billing wrote some, else the members counted now. A
+// nil row is the default plan. Any other plan, or no catalog, is 0 without
+// a count (teamMembers): Effective ignores it there.
func (s *Server) planMembers(row *AccountPlanRow, accountID int64) (int, error) {
- if row != nil && row.Seats > 0 {
- return row.Seats, nil
+ cat := s.cfg.PlanCatalog
+ if cat == nil {
+ return 0, nil
+ }
+ plan, seats := cat.DefaultPlan, 0
+ if row != nil {
+ plan, seats = row.Plan, row.Seats
}
- return s.store.MemberCount(accountID)
+ return teamMembers(cat, plan, seats, func() (int, error) { return s.store.MemberCount(accountID) })
}
// lockMembers takes the account's member lock and returns its release.
@@ -75,31 +81,14 @@ func liveTeam(cat *plans.Catalog, row *AccountPlanRow) *plans.Team {
}
// membershipFell reports whether a membership change from before to after
-// members is a fall that starts a lapse (plans 4d): the row is a
-// live-counted team row, paid (a paid plan or any pack) and not
-// sponsored, and a limit the lapse lifecycle governs fell.
+// members is a fall that starts a lapse: the row is a live-counted team
+// row, not sponsored, and paidFall (the rule plan changes use too) holds
+// for its own assignment at the two counts.
func membershipFell(cat *plans.Catalog, row *AccountPlanRow, before, after int) bool {
if liveTeam(cat, row) == nil || planRowSponsored(row) {
return false
}
- paid := cat.Plans[row.Plan].Paid
- for _, q := range row.Packs {
- if q > 0 {
- paid = true
- }
- }
- if !paid {
- return false
- }
- was, err := cat.Effective(row.Plan, row.Packs, nil, before)
- if err != nil {
- return false
- }
- next, err := cat.Effective(row.Plan, row.Packs, nil, after)
- if err != nil {
- return false
- }
- return plans.LimitsFell(was, next)
+ return paidFall(cat, row.Plan, row.Packs, before, row.Plan, row.Packs, after)
}
// MembershipTransition runs write, a membership change, in one
diff --git a/internal/console/planmembers_test.go b/internal/console/planmembers_test.go
index 2127ff9bd..38899a1b7 100644
--- a/internal/console/planmembers_test.go
+++ b/internal/console/planmembers_test.go
@@ -496,3 +496,83 @@ func TestSaveBillingSyncComparesSeats(t *testing.T) {
t.Fatalf("row after a seat change = seats %d revision %d, want 11 and %d", row.Seats, row.Revision, rev+1)
}
}
+
+// The browser remove door waits on the member lock and goes through the
+// membership transition: on a live-counted community row the revision
+// moves once the removal lands.
+func TestMembershipRemoveDoorUnderLock(t *testing.T) {
+ fx := newConsoleFixture(t)
+ owner := newBrowser(t)
+ signUp(t, fx, owner, "code-owner")
+ fx.srv.cfg.PlanCatalog = examplePlanCatalog(t)
+ acct := opsAccount(t, fx)
+ fillMembers(t, fx, acct.ID, 11)
+ applyPlan(t, fx, acct, "community", nil, nil, planNow)
+ csrf := sessionToken(t, fx, owner)
+ if err := fx.st.RecordStepUp(csrf, "paul@keymail.dev", time.Now(), time.Now()); err != nil {
+ t.Fatal(err)
+ }
+ rev := planRow(t, fx, acct.ID).Revision
+ waits := memberLockWaits(t, acct.Sqid)
+ unlock := fx.srv.lockMembers(acct.Sqid)
+ code := make(chan int, 1)
+ go func() {
+ c, _, _ := postForm(t, owner, fx.u("/members/remove"), url.Values{"csrf": {csrf}, "address": {"m100@example.com"}})
+ code <- c
+ }()
+ waitMemberWaiters(t, waits, 2, "the remove door to queue on the member lock")
+ if role, _ := fx.st.Role(acct.ID, "m100@example.com"); role == "" {
+ t.Fatal("the remove door removed without the member lock")
+ }
+ unlock()
+ select {
+ case c := <-code:
+ if c != http.StatusSeeOther {
+ t.Fatalf("remove = %d, want 303", c)
+ }
+ case <-time.After(15 * time.Second):
+ t.Fatal("remove never answered")
+ }
+ if role, _ := fx.st.Role(acct.ID, "m100@example.com"); role != "" {
+ t.Fatalf("member still holds %q", role)
+ }
+ if got := planRow(t, fx, acct.ID).Revision; got != rev+1 {
+ t.Fatalf("revision %d -> %d, want +1 (11 -> 10 members on community)", rev, got)
+ }
+}
+
+// The browser revoke door waits on the member lock.
+func TestMembershipRevokeDoorUnderLock(t *testing.T) {
+ fx := newConsoleFixture(t)
+ owner := newBrowser(t)
+ signUp(t, fx, owner, "code-owner")
+ acct := opsAccount(t, fx)
+ tok, err := fx.st.CreateInvite(acct.ID, "kim@keymail.dev", "paul@keymail.dev", time.Now())
+ if err != nil {
+ t.Fatal(err)
+ }
+ csrf := sessionToken(t, fx, owner)
+ waits := memberLockWaits(t, acct.Sqid)
+ unlock := fx.srv.lockMembers(acct.Sqid)
+ code := make(chan int, 1)
+ go func() {
+ c, _, _ := postForm(t, owner, fx.u("/invite/revoke"), url.Values{"csrf": {csrf}, "token": {tok}})
+ code <- c
+ }()
+ waitMemberWaiters(t, waits, 2, "the revoke door to queue on the member lock")
+ if pending, _ := fx.st.PendingInvites(acct.ID); len(pending) != 1 {
+ t.Fatalf("the revoke door revoked without the member lock: %+v", pending)
+ }
+ unlock()
+ select {
+ case c := <-code:
+ if c != http.StatusSeeOther {
+ t.Fatalf("revoke = %d, want 303", c)
+ }
+ case <-time.After(15 * time.Second):
+ t.Fatal("revoke never answered")
+ }
+ if pending, _ := fx.st.PendingInvites(acct.ID); len(pending) != 0 {
+ t.Fatalf("invite not revoked: %+v", pending)
+ }
+}