carlos / platform Public

plans: PDF cap refusals and notices

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev c1c7ac0d827bfa69e77a78005ae09cde7dce7ce5 parent cc40dcc
9 files changed, +169 −5
  • internal/console/apipdf.go +7 −1
  • internal/console/apipdf_test.go +44 −0
  • internal/console/plancaps_copy.go +3 −0
  • internal/console/plangracepass.go +6 −3
  • internal/console/planpdf.go +3 −0
  • internal/console/planpdf_test.go +74 −0
  • internal/console/templates/billing.html +1 −1
  • internal/plans/catalog.go +1 −0
  • internal/plans/catalog_test.go +30 −0

CI failed — run details

diff --git a/internal/console/apipdf.go b/internal/console/apipdf.go
index 8845dc163..535b6b8ba 100644
--- a/internal/console/apipdf.go
+++ b/internal/console/apipdf.go
@@ -122,7 +122,13 @@ func (s *Server) handleAPIPDF(w http.ResponseWriter, r *http.Request) {
return
}
if err := s.pdfPlanCheck(r.Context(), acct, now); err != nil {
- jsonError(out, http.StatusPaymentRequired, err.Error())
+ if isCap(err) {
+ jsonError(out, http.StatusPaymentRequired, err.Error())
+ } else {
+ log.Printf("console: pdf plan check %s: %v", acct.Sqid, err)
+ row.Outcome = pdfFailed
+ jsonError(out, http.StatusBadGateway, planCheckFailedCopy)
+ }
return
}
release, refusal := s.pdfLimiter.acquire(acct.Sqid, now)
diff --git a/internal/console/apipdf_test.go b/internal/console/apipdf_test.go
index 8ddbbf721..b2b36675f 100644
--- a/internal/console/apipdf_test.go
+++ b/internal/console/apipdf_test.go
@@ -956,3 +956,47 @@ func TestPDFDoorRefusesAtCeiling(t *testing.T) {
}
fx.row(t, pdfRendered, 200)
}
+
+func TestPDFDoorPlanCheckFailure(t *testing.T) {
+ for _, failure := range []string{"catalog", "count"} {
+ t.Run(failure, func(t *testing.T) {
+ var calls atomic.Int32
+ fx := newPDFDoorFixture(t, func(w http.ResponseWriter, r *http.Request) { calls.Add(1) })
+ acct := planAccount(t, fx.consoleFixture, 501)
+ capsApp(t, fx.consoleFixture, acct, "shop")
+ fx.sqid = acct.Sqid
+ fx.token = serviceToken(t, fx.st, acct.ID, "pdf", "shop", svcrole.Service)
+ fx.srv.cfg.PlanCatalog = pdfPlanCatalog(t, 100)
+ if failure == "catalog" {
+ fx.srv.cfg.PlanCatalog.DefaultPlan = "missing"
+ } else if _, err := fx.st.db.Exec(`DROP TABLE pdf_renders`); err != nil {
+ t.Fatal(err)
+ }
+ resp := fx.request(t, pdfHTMLRoute, nil, nil)
+ var body map[string]string
+ err := json.NewDecoder(resp.Body).Decode(&body)
+ resp.Body.Close()
+ if err != nil || resp.StatusCode != http.StatusBadGateway || body["error"] != planCheckFailedCopy {
+ t.Errorf("plan failure = %d %v, %v", resp.StatusCode, body, err)
+ }
+ fx.row(t, pdfFailed, http.StatusBadGateway)
+ if calls.Load() != 0 {
+ t.Error("failed plan check reached engine")
+ }
+ fx.srv.pdfLimiter.mu.Lock()
+ total, accounts := fx.srv.pdfLimiter.total, len(fx.srv.pdfLimiter.accounts)
+ fx.srv.pdfLimiter.mu.Unlock()
+ if total != 0 || accounts != 0 {
+ t.Errorf("failed plan check touched limiter: total %d, accounts %d", total, accounts)
+ }
+ if failure == "catalog" {
+ if got := countWhere(t, fx.st, "pdf_renders", "account = ? AND outcome = 'failed' AND status = 502", acct.Sqid); got != 1 {
+ t.Errorf("failed rows = %d", got)
+ }
+ if n, err := fx.st.AccountPDFRenders(t.Context(), acct.Sqid, pdfMonthStart(time.Now())); err != nil || n != 0 {
+ t.Errorf("rendered count = %d, %v; want 0", n, err)
+ }
+ }
+ })
+ }
+}
diff --git a/internal/console/plancaps_copy.go b/internal/console/plancaps_copy.go
index 707ad171c..86ead83b9 100644
--- a/internal/console/plancaps_copy.go
+++ b/internal/console/plancaps_copy.go
@@ -36,6 +36,9 @@ const planCheckFailedCopy = "could not check this account's plan. Try again"
// approved by delegation: a Luna pass on the house rules (Paul, 2026-10-25;
// docs/superpowers/specs/LUNA-COPY-RESULT-PLANS-4A.md).
var planNoticeCopy = map[string]string{
+ "cli.pdf_refused": "this account has used twice its PDF allowance this month. new PDFs are refused until the 1st of the UTC month, or until it moves to a larger plan",
+ "cli.pdf_over": "this account is over its PDF allowance: %s. at twice the allowance, new PDFs are refused until %s. move to a larger plan for more",
+ "banner.pdf_over": "This account is over its PDF allowance: %s. At twice the allowance, new PDFs are refused until %s. Move to a larger plan for more.",
"usage.running_things": "%d of %d apps and instances",
"usage.storage": "%s of %s object storage",
"usage.email": "%d of %d emails today",
diff --git a/internal/console/plangracepass.go b/internal/console/plangracepass.go
index 683b95238..057019dd1 100644
--- a/internal/console/plangracepass.go
+++ b/internal/console/plangracepass.go
@@ -308,17 +308,20 @@ func planNoticesOf(states []capState, now time.Time, stage func(capState) string
// planNoticeText renders one notice for a surface: "banner", "cli" or
// "email".
func planNoticeText(n planNotice, surface string) string {
- if n.Stage == "pdf_refused" && surface == "banner" {
- return planNoticeCopy["banner.pdf_refused"]
- }
use := usagePhrase(n)
if n.Stage == "lapse_grace" {
return fmt.Sprintf(planNoticeCopy[surface+".lapse_grace"], use, n.GraceEnds.UTC().Format("2 January 2006"))
}
if f, ok := planNoticeCopy[surface+"."+n.Stage]; ok && n.Stage != "" {
+ if n.Stage == "pdf_refused" {
+ return f
+ }
return fmt.Sprintf(f, use)
}
if n.Level.over() {
+ if f, ok := planNoticeCopy[surface+".pdf_over"]; ok && n.Cap == capPDF {
+ return fmt.Sprintf(f, use, n.GraceEnds.UTC().Format("2 January 2006"))
+ }
return fmt.Sprintf(planNoticeCopy[surface+".over"], use, n.GraceEnds.UTC().Format("2 January 2006"))
}
return fmt.Sprintf(planNoticeCopy[surface+".warn"], use)
diff --git a/internal/console/planpdf.go b/internal/console/planpdf.go
index 46cefeefc..1142a5621 100644
--- a/internal/console/planpdf.go
+++ b/internal/console/planpdf.go
@@ -13,6 +13,9 @@ func (s *Server) pdfPlanCheck(ctx context.Context, acct *Account, now time.Time)
if err != nil || l == nil || l.PDFRendersMonth == 0 {
return err
}
+ if l.PDFRendersMonth >= sponsorMost {
+ return nil
+ }
used, err := s.store.AccountPDFRenders(ctx, acct.Sqid, pdfMonthStart(now))
if err != nil {
return err
diff --git a/internal/console/planpdf_test.go b/internal/console/planpdf_test.go
index 7ffbe0d0f..9d06e0499 100644
--- a/internal/console/planpdf_test.go
+++ b/internal/console/planpdf_test.go
@@ -2,7 +2,9 @@ package console
import (
"errors"
+ "fmt"
"math"
+ "net/http/httptest"
"strings"
"testing"
"time"
@@ -162,3 +164,75 @@ func TestPDFCapBanner(t *testing.T) {
t.Fatalf("larger plan still refuses: %q", lines)
}
}
+
+func TestPDFPlanCheckUnlimitedSkipsCount(t *testing.T) {
+ fx := newConsoleFixture(t)
+ acct := planAccount(t, fx, 501)
+ if _, err := fx.st.db.Exec(`DROP TABLE pdf_renders`); err != nil {
+ t.Fatal(err)
+ }
+ for _, limit := range []int{sponsorMost - 1, sponsorMost, sponsorMost + 1} {
+ fx.srv.cfg.PlanCatalog = pdfPlanCatalog(t, limit)
+ err := fx.srv.pdfPlanCheck(t.Context(), acct, time.Now())
+ if limit < sponsorMost {
+ if err == nil || isCap(err) {
+ t.Errorf("finite limit did not count: %v", err)
+ }
+ } else if err != nil {
+ t.Errorf("unlimited limit %d counted: %v", limit, err)
+ }
+ }
+}
+
+func TestPDFCapNotices(t *testing.T) {
+ fx := newConsoleFixture(t)
+ acct := planAccount(t, fx, 501)
+ fx.srv.cfg.PlanCatalog = pdfPlanCatalog(t, 100)
+ for _, tc := range []struct {
+ used int64
+ banner, cli string
+ }{
+ {101,
+ "This account is over its PDF allowance: 101 of 100 PDFs this month. At twice the allowance, new PDFs are refused until %s. Move to a larger plan for more.",
+ "this account is over its PDF allowance: 101 of 100 PDFs this month. at twice the allowance, new PDFs are refused until %s. move to a larger plan for more"},
+ {200, pdfCeilingBanner,
+ "this account has used twice its PDF allowance this month. new PDFs are refused until the 1st of the UTC month, or until it moves to a larger plan"},
+ } {
+ t.Run(fmt.Sprint(tc.used), func(t *testing.T) {
+ now := time.Now().UTC()
+ st := evaluateCap(nil, capPDF, tc.used, 100, now)
+ st.AccountID = acct.ID
+ if err := fx.st.PutCapState(st); err != nil {
+ t.Fatal(err)
+ }
+ banner, cli := tc.banner, tc.cli
+ if tc.used == 101 {
+ date := time.Date(now.Year(), now.Month()+1, 1, 0, 0, 0, 0, time.UTC).Format("2 January 2006")
+ banner, cli = fmt.Sprintf(banner, date), fmt.Sprintf(cli, date)
+ }
+ lines, _ := fx.srv.planBanner(acct, RoleOwner, now)
+ if len(lines) != 1 || lines[0] != banner {
+ t.Errorf("banner = %q, want %q", lines, banner)
+ }
+ req := httptest.NewRequest("GET", "/", nil)
+ req.Header.Set(planWarningsAccept, "1")
+ w := httptest.NewRecorder()
+ fx.srv.writePlanWarnings(w, req, Principal{}, acct)
+ if got := w.Header().Values(planWarningHeader); len(got) != 1 || got[0] != cli {
+ t.Errorf("CLI header = %q, want %q", got, cli)
+ }
+ })
+ }
+ // A local December clock is still November in UTC.
+ now := time.Date(2026, 12, 1, 0, 30, 0, 0, time.FixedZone("east", 3600))
+ n := planNotice{Cap: capPDF, Used: 101, Limit: 100, Level: levelOver, GraceEnds: graceEnd(capPDF, now)}
+ for _, surface := range []string{"banner", "cli"} {
+ if got := planNoticeText(n, surface); !strings.Contains(got, "until 1 December 2026") {
+ t.Errorf("%s reset date = %q", surface, got)
+ }
+ }
+ n.Used, n.Level, n.Stage = 200, levelCeiling, "pdf_refused"
+ if got := planNoticeText(n, "email"); got != "- 200 of 100 PDFs this month, over the limit. Nothing changes until 1 December 2026." {
+ t.Errorf("generic email changed: %q", got)
+ }
+}
diff --git a/internal/console/templates/billing.html b/internal/console/templates/billing.html
index 06a195640..52cadfa5d 100644
--- a/internal/console/templates/billing.html
+++ b/internal/console/templates/billing.html
@@ -22,7 +22,7 @@
{{range .Includes}}<li>{{icon "check"}}<span>{{.Text}}</span></li>
{{end}}
</ul>{{end}}
- {{/* The usage, here only when no pack row carries it (billingusage.go). */}}
+ {{/* Usage that no pack row carries, including PDFs (billingusage.go). */}}
{{with .Usage}}{{if .Rows}}<div class="billing-usage">
<dl>
{{range .Rows}}<div><dt>{{.Label}}</dt><dd class="use{{if .Use.Level}} use-{{.Use.Level}}{{end}}">{{.Use.Text}}</dd></div>
diff --git a/internal/plans/catalog.go b/internal/plans/catalog.go
index d8f5cfc0b..1f9f745ca 100644
--- a/internal/plans/catalog.go
+++ b/internal/plans/catalog.go
@@ -324,6 +324,7 @@ func (c *Catalog) Effective(plan string, packs map[string]int, sponsored *Limits
l.StorageBytes = clamp(l.StorageBytes, ce.StorageBytes)
l.EmailPerDay = clamp(l.EmailPerDay, ce.EmailPerDay)
l.BandwidthBytesMonth = clamp(l.BandwidthBytesMonth, ce.BandwidthBytesMonth)
+ l.PDFRendersMonth = clamp(l.PDFRendersMonth, ce.PDFRendersMonth)
l.IdleSeconds = clamp(l.IdleSeconds, ce.IdleSeconds)
l.RateClientShare = clampRate(l.RateClientShare, ce.RateClientShare)
l.RatePerSecond = clampRate(l.RatePerSecond, ce.RatePerSecond)
diff --git a/internal/plans/catalog_test.go b/internal/plans/catalog_test.go
index a8d5e7e48..cf2f82556 100644
--- a/internal/plans/catalog_test.go
+++ b/internal/plans/catalog_test.go
@@ -306,3 +306,33 @@ func TestLimitsPDFRendersMonth(t *testing.T) {
t.Fatalf("pooled PDF limit = %d, %v", team.PDFRendersMonth, err)
}
}
+
+func TestEffectivePDFCeiling(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ limit, ceiling int
+ plan string
+ members int
+ sponsored *Limits
+ want int
+ }{
+ {"clamped", 200, 100, "personal", 1, nil, 100},
+ {"below", 50, 100, "personal", 1, nil, 50},
+ {"no ceiling", 200, 0, "personal", 1, nil, 200},
+ {"unlimited", 0, 100, "personal", 1, nil, 0},
+ {"team", 200, 100, "community", 15, nil, 300},
+ {"sponsored", 200, 100, "personal", 1, &Limits{PDFRendersMonth: 500}, 500},
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ c := exampleCatalog(t)
+ p := c.Plans["personal"]
+ p.Limits.PDFRendersMonth = tc.limit
+ c.Plans["personal"] = p
+ c.Ceilings.PDFRendersMonth = tc.ceiling
+ got, err := c.Effective(tc.plan, nil, tc.sponsored, tc.members)
+ if err != nil || got.PDFRendersMonth != tc.want {
+ t.Fatalf("PDF limit = %d, %v; want %d", got.PDFRendersMonth, err, tc.want)
+ }
+ })
+ }
+}