plans 6b: revision 9 after Astra's round 8. The restore-check stall deadline is provider time (PeriodEnd + 7 d against providerNow); the billing lock test is scoped to billing files' Locked functions and any function taking billingHold, naming both migrated 6a helpers; every budget branch carries its conditional probe cost, with tests for headerless reads, stale observations, failed probes and the pass's single probe
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 file changed,
+61
−32
docs/superpowers/plans/2026-10-25-plans-6b-checkout-flow.md+61 −32
CI cancelled — superseded by a newer push — run details
diff --git a/docs/superpowers/plans/2026-10-25-plans-6b-checkout-flow.md b/docs/superpowers/plans/2026-10-25-plans-6b-checkout-flow.md| index 0f2cb9ecf..cd8fbb026 100644 |
| --- a/docs/superpowers/plans/2026-10-25-plans-6b-checkout-flow.md |
| +++ b/docs/superpowers/plans/2026-10-25-plans-6b-checkout-flow.md |
| @@ -12,13 +12,12 @@ |
| A static pricing page links to one stable console URL, which takes a signed-out visitor through sign-in or sign-up first. |
| -**Revision 8 (2026-10-26)** answers Astra's round 7 (`plans6b-plan-review-r7.md`, at `a0654af6`). **The console's wall clock is taken out of every billing safety decision.** |
| -- Every deadline, first-send stamp, restore margin, `expires_at` and `proration_date` is **provider time**: the `Date` of a provider response seen in the same request no more than 60 s earlier, plus the monotonic time since. |
| -- A path with no such read makes **one clock probe**: a GET of one configured price. |
| -- The skew record, its refusal rule and the `clock_skew` problem are deleted. |
| -- The lock-taking close wrapper is renamed so that no `*Locked` function takes a lock. |
| +**Revision 9 (2026-10-26)** answers Astra's round 8 (`plans6b-plan-review-r8.md`, at `0d2b5e05`), which confirmed the provider-time model. |
| +- The 7-day restore-check stall deadline moves to provider time (`PeriodEnd + 7 d` against `providerNow`). |
| +- The lock test is scoped to billing code, with both migrated 6a helpers named. |
| +- Every budget branch carries its conditional probe cost. |
| -Revision 7 made flagging transactional and corrected 6a's names against main `7f79385e`. Revision 6 added durable restore obligations and crash recovery of `sending` rows. Revision 5 made every write an opaque prepared request. This file is **self-contained**: no task points to an earlier revision. |
| +Revision 8 took the console's wall clock out of every billing safety decision: provider time is a response `Date` at most 60 s old plus monotonic elapsed, with one price-GET clock probe where a path has no read. Revision 7 made flagging transactional and corrected 6a's names against main `7f79385e`. Revision 6 added durable restore obligations and crash recovery of `sending` rows. Revision 5 made every write an opaque prepared request. This file is **self-contained**: no task points to an earlier revision. |
| "Review record" maps every finding. Paul's seventeen calls stand as ruled. |
| @@ -93,7 +92,8 @@ Read with `stripe docs` on 2026-10-25. API v1, which is what the 6a client calls |
| - A recovery's `providerNow` underestimates true provider time by at most the observation's one-way latency (seconds). That is the only direction that lengthens our window. |
| - The total error is a few seconds against a 2-hour margin. A wrong console wall clock, a corrected clock or a restart cannot move either endpoint. |
| - **No wall clock** appears in these decisions. The skew record, `clockTrusted`, the skew refusal and the `clock_skew` problem are deleted. The adapter may log the observed skew at debug level as a diagnostic only. |
| - - **Console-local timers stay on console time**, because they are not compared with any Stripe time: the retirement row's 1 h staleness, the 7-day restore-check stall alert, the daily `seats_unpaid` re-check and the in-handler backoff. |
| + - **Also provider time:** the restore-check stall deadline, `obligation.PeriodEnd + 7 d` (a provider timestamp), compared with `providerNow` from the recorder or a probe. |
| + - **Console-local timers stay on console time**, because they are measured from console-recorded starts and never compared with a Stripe time: the retirement row's 1 h staleness (from `started_at`, written by the console), the daily `seats_unpaid` re-check (from `seats_checked_at`) and the in-handler backoff. |
| - The observation pass escalates exactly the rows that are outstanding, not `pending`, and **permanently ineligible**: indeterminate, or past their window. |
| - **A `sending` row seen by anyone holding the billing hold is orphaned.** The handler that wrote it holds the hold until it records an outcome, so a crash is the only way such a row survives. It is treated exactly as `unresolved`, and converted in the same transaction as the read that finds it. |
| @@ -111,7 +111,7 @@ Read with `stripe docs` on 2026-10-25. API v1, which is what the 6a client calls |
| - reads the renewal invoice for the period starting at the restore's period end, **once**, and stores its quantities as the obligation's baseline, because a later proration invoice can replace `latest_invoice`; |
| - compares the item's current quantity with the baseline. Above it, `Store.FlagRestoreCheck` sets the obligation `flagged` **and** opens `restore_after_renewal` ("item %s is at %d, the renewal billed %d") **in one transaction**. If either write fails, neither commits, and the obligation stays `open` for the next observation. At or below it, while the operation is still outstanding, the obligation stays `open`. Once the operation is terminal, it is `clear`. |
| - **The obligation survives failures.** An invoice-read failure or `ErrIncomplete` leaves it `open`, with `attempts` and `last_error` updated. A crash between saving the renewal and checking it leaves it `open`, and the pass finds it, because the trigger is the open row, not the period change. |
| - - An obligation still `open` 7 days after the renewal opens `restore_check_stalled` for an operator. The obligation stays `open` (still retried), so a lost problem write is retried too. |
| + - An obligation still `open` when provider time passes `PeriodEnd + 7 d` (the renewal plus seven days, both on Stripe's clock) opens `restore_check_stalled` for an operator. The obligation stays `open` (still retried), so a lost problem write is retried too. |
| - Detection writes nothing to the provider. The operator reduces the quantity or bills it in the Dashboard, then closes the problem. |
| - It needs Invoices read on the restricted key: granted by Paul (Call 18, 2026-10-25). |
| - **R2. A late Checkout session** after its create was abandoned (Invariant 1's exception). |
| @@ -257,7 +257,14 @@ Also binding: |
| - **Locks: member → billing → lapse → store.** |
| - `s.memberLocks` is keyed by sqid. |
| - The billing lock is taken only through `holdBilling`. |
| - - **Every** function named `*Locked` takes a `billingHold` first and never takes a lock. `TestBillingLockedHelpersNeverLock` enforces that globally, by name, across `internal/console`, including 6a's renamed `closeBillingProblemLocked` and `endMissingSubscriptionLocked`. |
| + - **The billing lock test is scoped to billing code.** `TestBillingLockedHelpersNeverLock` parses `internal/console` and selects: |
| + - (a) every function declared in a billing file (`billing*.go`, `apibilling*.go`) whose name ends in `Locked`; |
| + - (b) every function anywhere in the package with a `billingHold` parameter. |
| + |
| + Each selected function must take `billingHold` as its first parameter, and must not call `holdBilling`, `syncSubscription`, `closeBillingProblem` or `s.billing.locks.lock`. |
| + - The test also asserts by explicit name that both migrated 6a helpers, `closeBillingProblemLocked` (in `apibilling.go`) and `endMissingSubscriptionLocked` (in `billingsync.go`), are selected and pass. |
| + - It **fails** if a function in a billing file ends in `Locked` without taking `billingHold`. |
| + - Other packages' `*Locked` helpers (`sesConsoleMailer.ensureLocked`, `planGraceAccountLocked`, the lifetimes helpers) are out of scope and untouched. |
| - Timeout tests use 15 s. |
| - **Granted once paid.** Charges are `ProrateNow` (`always_invoice` + `pending_if_incomplete`). Reductions and restores are `ProrateNone`. |
| - **Idempotency keys**, each stored inside its `Prepared`: |
| @@ -283,8 +290,14 @@ Also binding: |
| - **Checkout create:** each retry is `L(c)`, the customer's sessions since the intent paged exhaustively, + 1 resend, so `1 + N × (L(c) + 1)`; |
| - **customer create:** no read, so `1 + N`; |
| - **expiry:** each retry is 1 session read + 1 resend, so `1 + 2N`. |
| - - `F`: one recovery inside a user's request. For an operation or expiry, 1 read + at most 1 resend. For a Checkout create, `L(c)` + at most 1. For a customer create, at most 1. |
| - - `P`: one clock probe (a price GET), **only** on a path with no provider response in the last 60 s: customer create on a first purchase, customer-create recovery, and a pass round before its first read. Every other branch already reads. |
| + - `P`: one clock probe (a price GET). |
| + - `P?`: **0 or 1 probe at each decision that needs provider time**. It costs 1 when, at that moment, no observation at most 60 s old exists, because the branch's own read returned no `Date`, or its observation went stale (handler backoff, slow pages), or the branch made no read at all. A **failed** probe ends the branch with no send, so it adds nothing after it. |
| + - Decisions needing provider time: a first send's stamp, every resend, the restore margin, `expires_at`, the continue check, a pass escalation and the stall deadline. |
| + - `F`: one recovery inside a user's request: |
| + - an operation or expiry: 1 read + `P?` + at most 1 resend; |
| + - a Checkout create: `L(c)` + `P?` + at most 1; |
| + - a customer create: `P?` + at most 1, where `P?` is in practice `P`, unless another read in this request is under 60 s old. |
| + - Retry costs with probes: each in-handler retry adds its own `P?`. A subscription operation is then `1 + N × (2 + P?)`, a Checkout create `1 + N × (L(c) + 1 + P?)`, a customer create `1 + N × (1 + P?)`, and an expiry `1 + N × (2 + P?)`. The un-probed forms above are the common case. |
| - `I(v, l)`: a renewal-invoice lookup, `L(v)` invoice pages (filtered to the subscription and `billing_reason=subscription_cycle`) + `L(l)` line pages. It is paid once per obligation on success; a failure repeats it at the next observation. |
| | Operation | Branch | Stripe requests | |
| @@ -292,29 +305,29 @@ Also binding: |
| | Readiness | per attempt | 34 (35 with a house coupon); stops at the first failure. Retries back off 1, 2, 4, 8, 16, then every 30 min. | |
| | Billing page view | any | 0 | |
| | Confirm now | unresolved op | `F` + `S` | |
| - | Confirm now | unresolved customer create (no customer id) | `P` + at most 1 resend | |
| - | Checkout start | first purchase | `S` (recorded subscription, if any) + `L(all subs)` (only with a bound customer) + `P` when neither read happened + customer (1 + N) + create (1 + N × (L(c) + 1)) | |
| - | | open intent, same request | `S` + `L` + 1 session read | |
| - | | open intent, Start over | `S` + `L` + 1 session read + expiry (1 + 2N; +1 read if refused) + create (1 + N × (L(c) + 1)) | |
| + | Confirm now | unresolved customer create (no customer id) | `P?` + at most 1 resend | |
| + | Checkout start | first purchase | `S` (recorded subscription, if any) + `L(all subs)` (only with a bound customer) + `P?` before the customer stamp + customer (1 + N) + `P?` before the create stamp and `expires_at` + create (1 + N × (L(c) + 1)) | |
| + | | open intent, same request | `S` + `L` + 1 session read + `P?` (continue check) | |
| + | | open intent, Start over | `S` + `L` + 1 session read + `P?` + expiry (1 + 2N; +1 read if refused) + `P?` + create (1 + N × (L(c) + 1)) | |
| | | creating intent | `S` + `L` + `F` on the create, then the branch it resolves to | |
| | | live subscription found | `S` + `L` + `S` per live id, then a refusal | |
| - | Continue (GET) | open intent | 1 session read | |
| + | Continue (GET) | open intent | 1 session read + `P?` | |
| | Checkout return | any | 1 session read + `S` | |
| | Pack or plan change | prior op outstanding | `F`; still outstanding means a refusal | |
| | | no step | `S` | |
| - | | one step | `S` + (1 + 2N) + `S` | |
| - | | restore, then charge | `S` + 2 × (1 + 2N) + `S` | |
| - | Renewal | any | `S` + (1 + 2N) + `S` | |
| + | | one step | `S` + `P?` + (1 + 2N) + `S` | |
| + | | restore, then charge | `S` + `P?` (margin and stamp) + (1 + 2N) + `P?` + (1 + 2N) + `S` | |
| + | Renewal | any | `S` + `P?` + (1 + 2N) + `S` | |
| | Portal | any | 1 | |
| | Join, billing team | free seat | `S` | |
| - | | restore or charge | `S` + (1 + 2N) + `S` | |
| - | | both | `S` + 2 × (1 + 2N) + `S` | |
| - | Match seats | drift | `S` + (1 + 2N) + `S` | |
| - | Deletion gate, per run | `u` outstanding ops, `j` non-terminal intents, `k` live of `n` examined | `u` × `F` + per intent (1 session read + expiry 1 + 2N, or the create's `F`) + `L(n)` + `k` × `S` | |
| - | Restore check | per open obligation, per observation | `S` (shared with the observation) + `I(v, l)` until the baseline is stored, then 0 | |
| - | Hourly pass, per account | any | 6a's sync; discovery `L(n)` per recently bound customer; 1 session read per open intent (creating past `ExpiresAt`: `L(c)`); 1 read per outstanding op; per open obligation as above; **0 writes** | |
| - |
| - - **Time costs a request only through `P`.** Everywhere else, provider time rides on responses already counted. |
| + | | restore or charge | `S` + `P?` + (1 + 2N) + `S` | |
| + | | both | `S` + `P?` + (1 + 2N) + `P?` + (1 + 2N) + `S` | |
| + | Match seats | drift | `S` + `P?` + (1 + 2N) + `S` | |
| + | Deletion gate, per run | `u` outstanding ops, `j` non-terminal intents, `k` live of `n` examined | `u` × `F` + per intent (1 session read + `P?` + expiry 1 + 2N, or the create's `F`) + `L(n)` + `k` × `S` | |
| + | Restore check | per open obligation, per observation | `S` (shared with the observation) + `I(v, l)` until the baseline is stored, then 0; + `P?` for the stall deadline | |
| + | Hourly pass, per account | any | 6a's sync; discovery `L(n)` per recently bound customer; 1 session read per open intent (creating past `ExpiresAt`: `L(c)`); 1 read per outstanding op; per open obligation as above; + `P?` per escalation or stall decision (at most 1 per account per round, since one fresh observation serves the rest of that account's decisions for 60 s); **0 writes** | |
| + |
| + - **Time costs a request only through `P` and `P?`.** Everywhere else, provider time rides on responses already counted. |
| - S3: nothing polled. A limits publication is a GET plus a conditional PUT, with conflict retries. `transitionAccountPlan` adds 4d's inventory reads and convergence writes per change that moves limits. |
| - **Call-count assertions:** recovery, Checkout-branch, deletion, restore-check and pass tests assert `Fake.CallsBy` (each list page counted), reads included, against a fake that **pages like Stripe**: `limit` up to 100, `has_more`, `starting_after`, for subscriptions, sessions, invoices and invoice lines. Pass tests assert zero writes. |
| - **No product name** in code, tests, copy or example data. **No `Record` in helper or type names.** |
| @@ -808,7 +821,7 @@ Also binding: |
| // FlagRestoreCheck sets the obligation flagged and opens restore_after_renewal through openBillingProblemTx, in one |
| // transaction (finding 1). StallRestoreCheck records stalled_at and opens restore_check_stalled the same way, leaving it open. |
| func (s *Store) FlagRestoreCheck(opID, detail string, now time.Time) error |
| - func (s *Store) StallRestoreCheck(opID string, now time.Time) error |
| + func (s *Store) StallRestoreCheck(opID string, providerNow time.Time) error // called only when providerNow > PeriodEnd + 7 d |
| func openBillingProblemTx(e sqlExecer, accountID int64, subscription, reason, detail string, now time.Time) (bool, error) |
| func (s *Server) restoreCheckLocked(h billingHold, ctx context.Context, acct *Account, sub *billing.Subscription, now time.Time) error // every open obligation of sub |
| func (s *Server) handleBillingConfirm(w http.ResponseWriter, r *http.Request) |
| @@ -838,7 +851,7 @@ Also binding: |
| 2. If the read's `PeriodEnd` still equals the obligation's: nothing to do yet. |
| 3. Otherwise, with no baseline stored: `RenewalInvoice(sub, obligation.PeriodEnd)`. On success, store the baseline. On failure (`ErrIncomplete`, any error), `attempts++` and `last_error`, and stay `open`. |
| 4. With a baseline: an item quantity above it, not explained by an `applied` op sent in the new period, is `FlagRestoreCheck`, one transaction for the flag and the problem. Otherwise, if the operation is terminal, `clear`; if not, stay `open`. |
| - 5. Open longer than 7 days after the renewal: `StallRestoreCheck`. |
| + 5. `providerNow` (the recorder or one probe) past `obligation.PeriodEnd + 7 d`: `StallRestoreCheck`. With no provider time, the stall is decided at the next observation; nothing else waits on it. |
| It writes nothing to the provider. |
| - **The observation pass:** |
| @@ -856,7 +869,8 @@ Also binding: |
| - [ ] **Step 1: Write the failing tests** |
| ```go |
| - func TestBillingLockedHelpersNeverLock(t *testing.T) |
| + func TestBillingLockedHelpersNeverLock(t *testing.T) // scope: billing files' *Locked + any func taking billingHold; names closeBillingProblemLocked and |
| + // endMissingSubscriptionLocked; a planted billing-file fooLocked without billingHold fails; ensureLocked ignored |
| func TestConsoleBillingHasNoProviderWords(t *testing.T) |
| func TestOpOutstandingMatchesIndex(t *testing.T) // the partial index's state list == opOutstanding's set |
| func TestSyncSubscriptionLockedSharesTheLock(t *testing.T) |
| @@ -891,7 +905,14 @@ Also binding: |
| func TestRestoreCheckSurvivesInvoiceReadFailure(t *testing.T) // InvoiceReadFails(2): open, attempts 2; third observation reads, flags |
| func TestRestoreCheckSurvivesCrashAfterSync(t *testing.T) // crash after applyBillingRead saved P2, before the check; restart; the pass finds the open obligation and checks it |
| func TestRestoreCheckBaselineIgnoresLaterInvoices(t *testing.T) // a proration invoice after the renewal: the stored baseline is used |
| - func TestRestoreCheckStalls(t *testing.T) // failures for 7 days: restore_check_stalled; obligation still open |
| + func TestRestoreCheckStalls(t *testing.T) // invoice reads fail; at provider PeriodEnd + 7 d + 1 m: restore_check_stalled; obligation still open |
| + func TestRestoreStallUsesProviderTime(t *testing.T) // invoice reads keep failing; console wall clock 14 days behind: stall opens at provider PeriodEnd + 7 d, not later; |
| + // a second account with the console clock 14 days ahead: no stall before provider PeriodEnd + 7 d; |
| + // clock corrected and console restarted mid-way: the deadline does not move |
| + func TestHeaderlessReadCostsOneProbe(t *testing.T) // op recovery: the subscription read has no Date; providerNow probes; resend: CallsBy == {Subscription: 1, ClockProbe: 1, Send: 1} |
| + func TestStaleObservationCostsOneProbe(t *testing.T) // in-handler retry after AdvanceMonotonic(61s): each retry's read then probe; counts per the budget's probed retry form |
| + func TestFailedProbeSendsNothing(t *testing.T) // the read is headerless and the probe fails: CallsBy == {Subscription: 1, ClockProbe: 1}, no Send; op stays unresolved |
| + func TestPassProbesAtMostOncePerAccount(t *testing.T) // a pass round with headerless reads and three decisions on one account: CallsBy[ClockProbe] == 1 |
| func TestFlagAndProblemCommitTogether(t *testing.T) // a SQLite trigger fails the problem insert: obligation still open, no problem; restart; next observation: |
| // flagged and restore_after_renewal together |
| func TestFirstSendUnderSkewThenLateRecoveryRefused(t *testing.T) // console wall clock 3 h ahead; renewal op first sent (FirstSentAt from the provider Date, correct); |
| @@ -909,7 +930,7 @@ Also binding: |
| func TestPassesNeverWrite(t *testing.T) |
| func TestReadinessBackoff(t *testing.T) |
| ``` |
| -- [ ] **Step 2: Run** `go test ./internal/console/ -run 'BillingLockedHelpers|ConsoleBillingHasNo|OpOutstanding|RecoveryEligible|FlagAndProblem|FirstSendUnderSkew|QuietCustomerRecovery|HeaderlessTimeoutRecovered|ObservationOlder|RestoreNeedsProviderTime|ExpiresAtAndProration|NoProviderTime|Crash(Before|After)Send|OrphanedSending|RestoreCheck|SyncSubscriptionLocked|Refresh|Allowance|QuantityPlan|PreparedPersisted|HandlerRetry|TimeoutStillProcessing|InFlight|RateLimit|Cached500|WindowCloses|NoResend|Pending(Never|Expiry)|OperatorClose|OneOutstanding|ReorderedWebhooks|LateRestore|PassesNeverWrite|ReadinessBackoff'`. Expected: FAIL. |
| +- [ ] **Step 2: Run** `go test ./internal/console/ -run 'BillingLockedHelpers|ConsoleBillingHasNo|OpOutstanding|RecoveryEligible|FlagAndProblem|RestoreStallUses|HeaderlessReadCosts|StaleObservationCosts|FailedProbe|PassProbes|FirstSendUnderSkew|QuietCustomerRecovery|HeaderlessTimeoutRecovered|ObservationOlder|RestoreNeedsProviderTime|ExpiresAtAndProration|NoProviderTime|Crash(Before|After)Send|OrphanedSending|RestoreCheck|SyncSubscriptionLocked|Refresh|Allowance|QuantityPlan|PreparedPersisted|HandlerRetry|TimeoutStillProcessing|InFlight|RateLimit|Cached500|WindowCloses|NoResend|Pending(Never|Expiry)|OperatorClose|OneOutstanding|ReorderedWebhooks|LateRestore|PassesNeverWrite|ReadinessBackoff'`. Expected: FAIL. |
| - [ ] **Step 3: Implement.** |
| - [ ] **Step 4: Run** again, plus the sync, webhook, pass, missing and claim suites. Expected: PASS. |
| - [ ] **Step 5: Commit** `console: operations as prepared requests resolved by Stripe's idempotent result or a positive read; one outstanding predicate and one recovery gate; orphaned sends recovered; durable late-restore obligations; passes only read (plans 6b)`. |
| @@ -1443,6 +1464,14 @@ CLI: `carlos billing house -account <sqid> -on|-off -note "..."`, console mode o |
| | 1. first-send stamps trust the wall clock | Invariant 9: every `FirstSentAt`, deadline, margin, `expires_at` and `proration_date` is provider time, a response `Date` at most 60 s old plus monotonic elapsed, so neither endpoint uses the console's wall clock. The skew record, its refusal rule and `clock_skew` are deleted (`TestFirstSendUnderSkewThenLateRecoveryRefused`: 3 h skew, correction, restart, recovery at provider + 24.5 h refused). | |
| | 2. the lock-taking wrapper | `closeBillingProblem` takes `holdBilling` and calls `closeBillingProblemLocked(h, ...)`, which no longer locks. `TestBillingLockedHelpersNeverLock` enforces "every `*Locked` takes a hold and never locks" globally by name, unchanged. | |
| +**Round 8** (`0d2b5e05`): |
| + |
| +| Finding | Revision 9 | |
| +| --- | --- | |
| +| 1. the 7-day stall crosses clock domains | The deadline is `obligation.PeriodEnd + 7 d` against `providerNow` (the recorder or one probe), and is removed from Invariant 9's console-time exceptions. The remaining console timers are measured from console-recorded starts (`TestRestoreStallUsesProviderTime`: 14 days behind, 14 days ahead, correction and restart). | |
| +| 2. the lock test catches unrelated helpers | Scoped to billing files' `*Locked` functions plus any function taking `billingHold`. Both migrated 6a helpers are named. A billing-file `*Locked` without `billingHold` fails; `ensureLocked` and the lifetimes helpers are out of scope. | |
| +| 3. fallback probes missing from budgets | `P?` (0 or 1 at each provider-time decision) in every affected row: first-send stamps, resends, the restore margin, `expires_at`, continue, pass escalations and the stall deadline, with probed retry forms. The pass probes at most once per account per round. Tests for a headerless read, a stale observation, a failed probe and the pass's single probe. | |
| + |
| --- |
| ## Plan 6c: Stripe setup runbook (outline) |