Skip to content

Billing audit (sc-9146): automatic packs and credit emails restored, opt-in; builds stop when credits run out; Free never owes; dead subscriptions move to Free - #199

Merged
ndbroadbent merged 4 commits into
mainfrom
billing-audit
Dec 4, 2026

Conversation

@ndbroadbent

@ndbroadbent ndbroadbent commented Dec 4, 2026 •

Copy link
Copy Markdown
Member

The must-fix half of the billing audit before sign-ups (sc-9146). The audit notes, with the should-fix items that come next, are in the story.

What was wrong

  1. Automatic packs and every credit email were lost in the Laravel Rust port. Remove the Loco server (server/): app/ is the only server #132 removed the Loco server's auto_reload, its out-of-credits email and the 80%/95% warnings, and nothing replaced them.
    • app/ dispatches CreditsSpent, CreditsRanOut and BuildStoppedForCredits, but nothing listened.
    • A Team customer with automatic packs on ran out, and every build failed: no pack bought, no email.
  2. A subscription whose card died stayed on Team. unpaid only set a notice, and canceled and incomplete_expired did nothing. The 1,000,000 allowance renewed every month.
  3. Running out didn't stop a build. A screenshot build with 1 credit left compared everything, and on Free the overrun became a hidden negative balance. That debt came out of the first $15 pack bought after upgrading.
  4. Automatic packs were on by default (4 a month, $60), while the terms say "if you turn on automatic reloads".

What changes

  • billing/reload.rs ports the reload:

    • claimed on the organization's row, so two builds buy one pack;
    • an invoice paid off-session with idempotency keys;
    • credited once, together with invoice.paid;
    • a declined card voids the invoice, pauses packs and sends an email;
    • the monthly limit holds, with its own email.
  • billing/listeners.rs:

    • after a build spends: the 80% and 95% emails (once each a month) and an automatic pack when credits are low;
    • before a build is stopped: an automatic pack;
    • after it's stopped: the out-of-credits email (once a month).

    A listener failure is logged and never fails the build.

  • One-click buy: the out-of-credits email's button is "Buy a credit pack", linking to billing?buy=pack. The billing page then goes straight on to Stripe's Checkout for someone who can pay. On Free, the button is "Upgrade to Team".

  • Dead subscriptions:

    • unpaid moves to Free and keeps the subscription, with a payment notice; the billing page shows "Pay the open invoice" (Stripe's portal), and paying brings Team back. Checkout refuses a second subscription with a message saying so;
    • canceled and incomplete_expired move to Free and clear the subscription, so Upgrade starts a new one;
    • past_due (Stripe still retrying) keeps Team with the notice.
  • Overrun (Nathan's decision):

    • screenshot builds charge and check credits every 200 comparisons, and stop there;
    • on Free, use past the allowance is forgiven, taking only pack credits left from an earlier plan;
    • on Team, the next pack pays it.
  • Opt-in (Nathan's decision). A migration sets the default to 0 and turns every organization's automatic packs off.

    • Production has 4 organizations with the old default, all Nathan's, none with a Stripe customer, so nobody was charged (checked with talira infra).
    • It also clears the negative balances Free organizations carried.
  • Docs and design: docs/reference/credits.md and docs/design.md describe the new behaviour. Golden files change only auto_reload_packs from 4 to 0.

Tests (failing first)

tests/automatic_packs.rs (8), plus one each in tests/billing.rs and tests/processing.rs. Against main, all fail except off_by_default_nothing_is_charged, a guard that passes because main never charged at all:

  • automatic packs off by default; nothing charged while off;
  • running out buys one pack, charged and credited once, emailed;
  • a declined card voids the invoice, pauses packs and sends an email;
  • the monthly limit holds;
  • the out-of-credits email goes out once a month with the buy-a-pack link;
  • the 80% and 95% emails go out once each;
  • Free never owes credits;
  • a subscription that stops paying moves to Free, and paying brings Team back;
  • a 205-snapshot build that runs out stops part-way and Free owes nothing.

The whole app suite, fmt, clippy and the frontend's tsc -b pass.

Second round (independent review, and the test harness gap)

  • Each render and diff is charged to the build that made it (made_by_build_id, a nullable column on renders and diffs), replacing "made after the build was created". That comparison mixed the app's clock with Postgres's now(). Inside a test's transaction every row was stamped with the test's start, so no test ever charged for a diff or a page. In production, a render that one build queued and another build finished could be charged twice or not at all.
  • New tests, each failing on main for the charge itself:
    • a build pays for the image diffs it makes, and another project reusing one doesn't;
    • the build that queued a render pays for its pages once, even when another build shares it;
    • an automatic pack lets a build that ran out go on;
    • Stripe failing never fails more than the credits do;
    • a pay call cut off after the charge still credits the pack.
  • Review fixes:
    • a pay call that errors with something other than 402 asks Stripe for the invoice before calling it declined;
    • the reload checks the credits, limit and pause again under its claim;
    • a finished build sends its check and live update before billing's listeners run;
    • the migration writes a ledger row for each Free balance it forgives.
  • Not done here (should-fix series next): the reload still runs inline in finalize's request when credits run out, and a document snapshot with many pages can overrun by more than 200 comparisons.

@visualci-com

visualci-com Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

VisualCI

docspring/visualci · build #722 · 515 unchanged · no changes

This PR vs base: 25 changed, 101 added since #541, across 4 builds

docspring/visualci · journeys · build #721 · 50 unchanged · no changes

This PR vs base: 1 changed, 8 added since #538, across 5 builds

Updated as builds change. Agents can review with VisualCI's MCP tools.

…gain (lost in the Laravel Rust port), off until an admin turns them on; builds stop between batches when credits run out; Free never owes credits; a subscription Stripe gives up on moves to Free
…e it (no clock comparison: shared renders paid once, tests charge for diffs and pages); unpaid keeps the subscription with Pay the open invoice, incomplete_expired clears it; a pay call cut off after the charge credits the pack; checks again under the reload's claim; the build's check before billing's listeners; ledger row for forgiven balances
…en its build stops, is charged to the build that waits for it; the reload keeps its claim after an error (an unknown charge isn't tried again) and checks the plan under it; test callers fixed
@ndbroadbent
ndbroadbent merged commit 4d69b05 into main Dec 4, 2026
16 checks passed
@ndbroadbent
ndbroadbent deleted the billing-audit branch December 4, 2026 14:15
ndbroadbent added a commit that referenced this pull request Dec 4, 2026
… billing hears a build finish even when its check fails (#199)

- A document's base drawn again with the head's settings is a render the build made that none of its snapshots points
  at; the charge counted only renders its snapshots point at, so those pages were never charged. It now counts every
  render the build made.
- The part-way credit check ran every 200 snapshots, so a build of long documents could compare thousands of pages
  past its credits. It now counts pages (a document's from its render).
- finished() returned early when BuildChanged's dispatch failed (a forge that's down), so CreditsSpent (low-credit
  emails, automatic packs) never ran. Both run now, and the first error is returned.
ndbroadbent added a commit that referenced this pull request Dec 4, 2026
… billing hears a build finish even when its check fails (#199)

- A document's base drawn again with the head's settings is a render the build made that none of its snapshots points
  at; the charge counted only renders its snapshots point at, so those pages were never charged. It now counts every
  render the build made.
- The part-way credit check ran every 200 snapshots, so a build of long documents could compare thousands of pages
  past its credits. It now counts pages (a document's from its render).
- finished() returned early when BuildChanged's dispatch failed (a forge that's down), so CreditsSpent (low-credit
  emails, automatic packs) never ran. Both run now, and the first error is returned.
ndbroadbent added a commit that referenced this pull request Dec 4, 2026
… hears every finish (#199 leftovers) (#218)

* Processing: redrawn bases are charged, the credit check counts pages, billing hears a build finish even when its check fails (#199)

- A document's base drawn again with the head's settings is a render the build made that none of its snapshots points
  at; the charge counted only renders its snapshots point at, so those pages were never charged. It now counts every
  render the build made.
- The part-way credit check ran every 200 snapshots, so a build of long documents could compare thousands of pages
  past its credits. It now counts pages (a document's from its render).
- finished() returned early when BuildChanged's dispatch failed (a forge that's down), so CreditsSpent (low-credit
  emails, automatic packs) never ran. Both run now, and the first error is returned.

* Tests: each test's pool holds at most 4 connections (sc-9385)

The suite is one binary whose tests boot at once, each with its own pool of up to 10: a CI runner's threads asked
Postgres for more than its 100 connections, and tests failed at boot with "pool timed out".
ndbroadbent added a commit that referenced this pull request Dec 4, 2026
ndbroadbent added a commit that referenced this pull request Dec 4, 2026
…check fails; a reused image diff shares its row (#199)
ndbroadbent added a commit that referenced this pull request Dec 4, 2026
ndbroadbent added a commit that referenced this pull request Dec 4, 2026
…check fails; a reused image diff shares its row (#199)
ndbroadbent added a commit that referenced this pull request Dec 4, 2026
…under its claim (#199's weak and untested spots) (#226)

* Tests: the shared render really is shared, and the reload's re-check under its claim (#199's weak and untested spots)

* Tests: billing hears a finished build after its check, even when the check fails; a reused image diff shares its row (#199)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant