plans: strict whole-seconds cutoff, and a stale rule stops the start (Astra R3)
Astra round 3: READY AFTER FIXES, two P2s.
- Cutoff precision (third time in this family, closed structurally):
sponsor_accounts_before must match
^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(Z|[+-]\d{2}:\d{2})$ on the raw
string before it is parsed, so no '.' or ',' fraction, no fraction past
nine digits and no other shape gets in. Tests add comma fractions, a
comma fraction past nine digits, a lower-case t, an offset without a
colon, and a valid +01:00 offset.
- Rule consistency: a failure to remove the recorded rule is marked
console.ErrLaunchRuleNotCleared; sponsorAtStart returns it and serve()
stops with log.Fatalf, so systemd restarts the console and no stale rule
outlives a start. Tested with a database that refuses the delete (plans
off, no cutoff, a refused cutoff), and serve()'s log.Fatalf on it is
pinned by TestServeSettlesTheSponsorshipBeforeItServes.
Census: internal/plans envelope re-pinned (catalog.go); overlay
re-proposed, census clean.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
7 files changed,
+144
−23
cmd/console/main.go+6 −1cmd/console/plansponsor.go+11 −7cmd/console/plansponsor_test.go+79 −4internal/console/plansponsor.go+20 −3internal/plans/catalog.go+18 −7internal/plans/catalog_test.go+9 −0internal/provision/testdata/plan14-root-reads-sponsor-existing-accounts.tsv+1 −1
CI failed — run details
diff --git a/cmd/console/main.go b/cmd/console/main.go| index 86586044d..df170f1c0 100644 |
| --- a/cmd/console/main.go |
| +++ b/cmd/console/main.go |
| @@ -375,7 +375,12 @@ func serve() { |
| // any worker or the listener. It records the rule every account insert |
| // applies from then on. One that does not settle runs this start with |
| // plans and billing off, never with plans on and the rule unrecorded. |
| - catalog, plansOff := sponsorAtStart(st, catalog, time.Now()) |
| + // An old rule that could not be removed stops the start: systemd |
| + // restarts the console, and no stale rule outlives a start. |
| + catalog, plansOff, err := sponsorAtStart(st, catalog, time.Now()) |
| + if err != nil { |
| + log.Fatalf("console: launch sponsorship: %v", err) |
| + } |
| // Billing (plans 6a): dormant unless CARLOS_BILLING is test or live. |
| // Every failure names the variable, never its value. |
| billingCfg, err := billingConfigFor(os.Getenv, catalog, plansOff) |
diff --git a/cmd/console/plansponsor.go b/cmd/console/plansponsor.go| index f0d88de27..e4b1d66d8 100644 |
| --- a/cmd/console/plansponsor.go |
| +++ b/cmd/console/plansponsor.go |
| @@ -1,6 +1,7 @@ |
| package main |
| import ( |
| + "errors" |
| "log" |
| "strings" |
| "time" |
| @@ -16,24 +17,27 @@ import ( |
| // for this start: no catalog, as if none were configured, said loudly, and |
| // plansOff is true so billing is left off rather than refusing the start. |
| // The next start tries again. |
| -func sponsorAtStart(st *console.Store, cat *plans.Catalog, now time.Time) (_ *plans.Catalog, plansOff bool) { |
| +// |
| +// When an old rule could not be removed (console.ErrLaunchRuleNotCleared) |
| +// it returns the error and the console must not start: the stale rule |
| +// would go on sponsoring every account inserted under a catalog that no |
| +// longer asks for it. systemd restarts the console. |
| +func sponsorAtStart(st *console.Store, cat *plans.Catalog, now time.Time) (_ *plans.Catalog, plansOff bool, _ error) { |
| n, err := console.SettleLaunchSponsorship(st, cat, now) |
| - if err != nil && (cat == nil || cat.SponsorCutoff().IsZero()) { |
| - // Only the clearing of an old rule failed; plans are as configured. |
| - log.Printf("console: clearing the launch sponsorship rule: %v", err) |
| - return cat, false |
| + if errors.Is(err, console.ErrLaunchRuleNotCleared) { |
| + return nil, true, err |
| } |
| if err != nil { |
| log.Printf("console: PLANS ARE OFF: CARLOS_PLAN_CATALOG is set, but its launch sponsorship "+ |
| "(sponsor_accounts_before) did not settle: %v. This console runs with no plans and no billing; "+ |
| "fix it and restart, and the next start tries again", err) |
| - return nil, true |
| + return nil, true, nil |
| } |
| if n > 0 { |
| log.Printf("console: sponsored %d existing accounts with no limits (created before %s)", n, |
| cat.SponsorCutoff().UTC().Format(time.RFC3339)) |
| } |
| - return cat, false |
| + return cat, false, nil |
| } |
| // billingConfigFor is billingConfig, except that billing stays off, |
diff --git a/cmd/console/plansponsor_test.go b/cmd/console/plansponsor_test.go| index bc5038fd2..e7f58abf8 100644 |
| --- a/cmd/console/plansponsor_test.go |
| +++ b/cmd/console/plansponsor_test.go |
| @@ -1,6 +1,8 @@ |
| package main |
| import ( |
| + "database/sql" |
| + "errors" |
| "go/ast" |
| "go/parser" |
| "go/token" |
| @@ -50,8 +52,8 @@ func TestSponsorAtStart(t *testing.T) { |
| return "" |
| } |
| - cat, off := sponsorAtStart(st, sponsorTestCatalog(t, now.Add(time.Hour)), now) |
| - if cat != nil || !off { |
| + cat, off, err := sponsorAtStart(st, sponsorTestCatalog(t, now.Add(time.Hour)), now) |
| + if cat != nil || !off || err != nil { |
| t.Fatalf("future cutoff: catalog %v, plans off %v; want none, off", cat != nil, off) |
| } |
| if b, err := billingConfigFor(env, cat, off); b != nil || err != nil { |
| @@ -62,12 +64,52 @@ func TestSponsorAtStart(t *testing.T) { |
| } |
| want := sponsorTestCatalog(t, now.Add(-time.Hour)) |
| - cat, off = sponsorAtStart(st, want, now) |
| - if cat != want || off { |
| + cat, off, err = sponsorAtStart(st, want, now) |
| + if cat != want || off || err != nil { |
| t.Fatalf("past cutoff: catalog kept %v, plans off %v", cat == want, off) |
| } |
| } |
| +// An old rule that cannot be removed stops the start (Astra R3): left in |
| +// place, it would go on sponsoring every account inserted, whatever this |
| +// start's catalog says. |
| +func TestSponsorAtStartStopsWhenTheOldRuleStays(t *testing.T) { |
| + dbPath := filepath.Join(t.TempDir(), "console.db") |
| + st, err := console.Open(dbPath) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + t.Cleanup(func() { st.Close() }) |
| + now := time.Unix(1_800_000_000, 0) |
| + if _, _, err := sponsorAtStart(st, sponsorTestCatalog(t, now.Add(-time.Hour)), now); err != nil { |
| + t.Fatal(err) |
| + } |
| + // The database refuses the delete (a failing disk, say). |
| + raw, err := sql.Open("sqlite", dbPath) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + t.Cleanup(func() { raw.Close() }) |
| + if _, err := raw.Exec(`CREATE TRIGGER keep_rule BEFORE DELETE ON plan_launch_sponsorship |
| + BEGIN SELECT RAISE(ABORT, 'disk I/O error'); END`); err != nil { |
| + t.Fatal(err) |
| + } |
| + noCutoff, err := plans.LoadCatalog("../../internal/plans/testdata/example-catalog.json") |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + for name, cat := range map[string]*plans.Catalog{ |
| + "plans off": nil, |
| + "no cutoff": noCutoff, |
| + "a refused cutoff": sponsorTestCatalog(t, now.Add(time.Hour)), |
| + } { |
| + got, off, err := sponsorAtStart(st, cat, now) |
| + if !errors.Is(err, console.ErrLaunchRuleNotCleared) || got != nil || !off { |
| + t.Errorf("%s: catalog %v, plans off %v, err %v; want the start stopped", name, got != nil, off, err) |
| + } |
| + } |
| +} |
| + |
| // serve() is not callable from a test, so this reads main.go: serve() |
| // settles the sponsorship (sponsorAtStart) once, on the catalog |
| // planCatalog loaded, and before billing, console.NewServer, every |
| @@ -131,7 +173,40 @@ func TestServeSettlesTheSponsorshipBeforeItServes(t *testing.T) { |
| t.Errorf("%s runs before the sponsorship settles", later) |
| } |
| } |
| + if !fatalsOnSponsorError(serve.Body) { |
| + t.Error("serve() does not stop with log.Fatal when sponsorAtStart returns an error") |
| + } |
| if count["billingConfig"] != 0 { |
| t.Error("serve() calls billingConfig directly; billing must go through billingConfigFor so a start with plans off keeps billing off") |
| } |
| } |
| + |
| +// fatalsOnSponsorError reports whether body has `..., err := sponsorAtStart(...)` |
| +// followed at once by `if err != nil { log.Fatalf(...) }`. |
| +func fatalsOnSponsorError(body *ast.BlockStmt) bool { |
| + for i, st := range body.List { |
| + as, ok := st.(*ast.AssignStmt) |
| + if !ok || len(as.Rhs) != 1 || len(as.Lhs) != 3 || i+1 >= len(body.List) { |
| + continue |
| + } |
| + call, ok := as.Rhs[0].(*ast.CallExpr) |
| + if !ok || !isIdent(call.Fun, "sponsorAtStart") || !isIdent(as.Lhs[2], "err") { |
| + continue |
| + } |
| + ifs, ok := body.List[i+1].(*ast.IfStmt) |
| + if !ok || len(ifs.Body.List) == 0 { |
| + return false |
| + } |
| + es, ok := ifs.Body.List[0].(*ast.ExprStmt) |
| + if !ok { |
| + return false |
| + } |
| + fatal, ok := es.X.(*ast.CallExpr) |
| + if !ok { |
| + return false |
| + } |
| + sel, ok := fatal.Fun.(*ast.SelectorExpr) |
| + return ok && isIdent(sel.X, "log") && sel.Sel.Name == "Fatalf" |
| + } |
| + return false |
| +} |
diff --git a/internal/console/plansponsor.go b/internal/console/plansponsor.go| index 34e9b49a1..7439dcd89 100644 |
| --- a/internal/console/plansponsor.go |
| +++ b/internal/console/plansponsor.go |
| @@ -107,22 +107,39 @@ func sponsorUnlimited() plans.Limits { |
| // sponsors nothing. A cutoff after now is refused. On any error the rule |
| // is cleared and the caller must run with no catalog: plans off, and the |
| // next start retries. |
| +// |
| +// An error that wraps ErrLaunchRuleNotCleared means the clearing itself |
| +// failed, so an old rule may still be recorded and would go on sponsoring |
| +// every account inserted. The console must not start then (Astra R3). |
| func SettleLaunchSponsorship(st *Store, cat *plans.Catalog, now time.Time) (int, error) { |
| if cat == nil || cat.SponsorCutoff().IsZero() { |
| - return 0, st.ClearLaunchSponsorship() |
| + return 0, clearLaunchRule(st) |
| } |
| cutoff := cat.SponsorCutoff() |
| if cutoff.After(now) { |
| return 0, errors.Join(fmt.Errorf("sponsor_accounts_before %s is after the console's start (%s): set it to when plans go on, not later", |
| - cutoff.UTC().Format(time.RFC3339), now.UTC().Format(time.RFC3339)), st.ClearLaunchSponsorship()) |
| + cutoff.UTC().Format(time.RFC3339), now.UTC().Format(time.RFC3339)), clearLaunchRule(st)) |
| } |
| n, err := st.StartLaunchSponsorship(cat, cutoff, sponsorUnlimited(), planSponsorReason, planDefaultActor, now) |
| if err != nil { |
| - return 0, errors.Join(fmt.Errorf("writing the sponsored plans: %w", err), st.ClearLaunchSponsorship()) |
| + return 0, errors.Join(fmt.Errorf("writing the sponsored plans: %w", err), clearLaunchRule(st)) |
| } |
| return n, nil |
| } |
| +// ErrLaunchRuleNotCleared marks a SettleLaunchSponsorship error in which |
| +// the recorded rule could not be removed. |
| +var ErrLaunchRuleNotCleared = errors.New("the recorded launch sponsorship rule could not be removed") |
| + |
| +// clearLaunchRule is ClearLaunchSponsorship, its failure marked |
| +// ErrLaunchRuleNotCleared. |
| +func clearLaunchRule(st *Store) error { |
| + if err := st.ClearLaunchSponsorship(); err != nil { |
| + return fmt.Errorf("%w: %w", ErrLaunchRuleNotCleared, err) |
| + } |
| + return nil |
| +} |
| + |
| // launchRule is the recorded launch sponsorship rule. |
| type launchRule struct { |
| cutoff int64 // unix seconds; accounts created before it are sponsored |
diff --git a/internal/plans/catalog.go b/internal/plans/catalog.go| index 6ea1fd546..f863d9f85 100644 |
| --- a/internal/plans/catalog.go |
| +++ b/internal/plans/catalog.go |
| @@ -16,8 +16,8 @@ import ( |
| "fmt" |
| "math" |
| "os" |
| + "regexp" |
| "slices" |
| - "strings" |
| "time" |
| "github.com/carlosframework/platform/internal/ratelimit" |
| @@ -150,6 +150,12 @@ func ParseCatalog(b []byte) (*Catalog, error) { |
| // can never claim a version whose content it does not carry. |
| func (c *Catalog) ID() string { return c.id } |
| +// sponsorCutoffShape is the only shape sponsor_accounts_before may take: |
| +// RFC 3339 in whole seconds, upper-case T, Z or a +hh:mm/-hh:mm offset. |
| +var sponsorCutoffShape = regexp.MustCompile(`^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(Z|[+-]\d{2}:\d{2})$`) |
| + |
| +var errSponsorCutoffShape = errors.New("not whole seconds") |
| + |
| // SponsorCutoff is SponsorAccountsBefore as a time, or the zero time when |
| // it is unset. |
| func (c *Catalog) SponsorCutoff() time.Time { return c.sponsorCutoff } |
| @@ -168,12 +174,17 @@ func (c *Catalog) validate() error { |
| return errors.New("free_accounts_per_person is negative") |
| } |
| if c.SponsorAccountsBefore != "" { |
| - // Whole seconds only, refused on the syntax before parsing: accounts |
| - // record their creation to the second, so a fraction would be |
| - // compared truncated, and Go's parser silently drops digits past |
| - // the ninth. RFC 3339 has a '.' only before a fraction. |
| - t, err := time.Parse(time.RFC3339, c.SponsorAccountsBefore) |
| - if err != nil || strings.Contains(c.SponsorAccountsBefore, ".") { |
| + // Whole seconds only, checked on the raw string against one strict |
| + // shape before parsing: accounts record their creation to the |
| + // second, so any fraction would be compared truncated, and Go's |
| + // parser takes '.' and ',' fractions and drops digits past the |
| + // ninth. Anything the pattern lets through is a whole second. |
| + var t time.Time |
| + err := errSponsorCutoffShape |
| + if sponsorCutoffShape.MatchString(c.SponsorAccountsBefore) { |
| + t, err = time.Parse(time.RFC3339, c.SponsorAccountsBefore) |
| + } |
| + if err != nil { |
| return fmt.Errorf("sponsor_accounts_before %q is not an RFC 3339 time in whole seconds, like 2026-11-01T09:00:00Z", c.SponsorAccountsBefore) |
| } |
| c.sponsorCutoff = t |
diff --git a/internal/plans/catalog_test.go b/internal/plans/catalog_test.go| index cb9031cee..eb1127ecf 100644 |
| --- a/internal/plans/catalog_test.go |
| +++ b/internal/plans/catalog_test.go |
| @@ -250,6 +250,10 @@ func TestCatalogSponsorAccountsBefore(t *testing.T) { |
| if c.ID() == plain.ID() { |
| t.Fatal("adding the cutoff did not change the catalog ID") |
| } |
| + offset, err := ParseCatalog([]byte(strings.Replace(base, `"default_plan": "free",`, `"default_plan": "free", "sponsor_accounts_before": "2026-11-01T10:00:00+01:00",`, 1))) |
| + if err != nil || !offset.SponsorCutoff().Equal(c.SponsorCutoff()) { |
| + t.Fatalf("an offset cutoff = %v, %v", offset, err) |
| + } |
| for name, v := range map[string]string{ |
| "not a time": `"tomorrow"`, |
| "no zone": `"2026-11-01T09:00:00"`, |
| @@ -262,6 +266,11 @@ func TestCatalogSponsorAccountsBefore(t *testing.T) { |
| // nanoseconds; the syntax itself is refused (Astra R2 finding 2). |
| "a fraction past nine digits": `"2026-11-01T09:00:00.0000000001Z"`, |
| "a zero fraction": `"2026-11-01T09:00:00.000Z"`, |
| + // Go's RFC 3339 parser takes a comma fraction too (Astra R3). |
| + "a comma fraction": `"2026-11-01T09:00:00,5Z"`, |
| + "a comma fraction past nine": `"2026-11-01T09:00:00,0000000001Z"`, |
| + "a lower-case t": `"2026-11-01t09:00:00Z"`, |
| + "an offset without a colon": `"2026-11-01T09:00:00+0100"`, |
| } { |
| body := strings.Replace(base, `"default_plan": "free",`, `"default_plan": "free", "sponsor_accounts_before": `+v+`,`, 1) |
| if _, err := ParseCatalog([]byte(body)); err == nil { |
diff --git a/internal/provision/testdata/plan14-root-reads-sponsor-existing-accounts.tsv b/internal/provision/testdata/plan14-root-reads-sponsor-existing-accounts.tsv| index 2d7e3c9f0..8aacc34e9 100644 |
| --- a/internal/provision/testdata/plan14-root-reads-sponsor-existing-accounts.tsv |
| +++ b/internal/provision/testdata/plan14-root-reads-sponsor-existing-accounts.tsv |
| @@ -1,3 +1,3 @@ |
| # Census overlay sponsor-existing-accounts, written by census-propose (#369). census-ruling-sponsor-existing-accounts, approved on #280 before the merge; the approval card is docs/superpowers/notes/census-proposal-sponsor-existing-accounts.md. |
| # 0 body, 0 sites, 1 envelope: each changed unit re-frozen at its existing disposition with its claim appended. |
| -@coverage internal/plans 93d0b45241a247a6f6d3c7c67e12c8f5e4f230d0335f0fb4ab31ef58ca683f37 |
| +@coverage internal/plans 15dfe16c49f24d0dbfaf835aea07eb98e198a89b4dc103868967ad2ddf756800 |