Telling Technology · Learning in public
All episodes
EP.117 — MERGED DARK
Getting paid · shipped switched off

Billing that ships switched off

My AI assistant, the Pocket Operator, lives in Telegram, and my family and a few team workspaces already use it for free. Now it needs paying customers. Over three days I wired in Stripe Managed Payments, and the bot’s main file got seven marked insertions. None of them changes anything for anyone until one flag, BILLING_ENFORCE=1, is set. Today the billing route still answers 404, on purpose. No key, price id or customer appears on this page.

A dark shop counter with a closed cash register, a large master switch beside it in the off position glowing coral, and a thin teal wire running from a chat bubble to the register
⌁ 7 marked insertions · 1 flag, off by default · 416 tests pass on the trial merge · 2026-10-01..04
first, why a billing gate can lock out the people already using it
01 The risk

A billing gate can lock out the people who already use it for free

The assistant runs one sandboxed silo per person behind a shared Telegram bot, plus family bots and team workspaces. A billing gate is a door that says no, and if it is wired in wrong, the first deploy tells my own family their plan is paused. So the design starts from what Stripe must never be able to do. Stripe never talks to the bot. It talks to a Cloudflare Worker, which checks the signature, maps Stripe’s events to six neutral ones, and sends them on to POST /v1/billing on the gateway’s loopback listener. The ingest is idempotent on Stripe’s event id: every event id goes into a ledger, and a replay answers 200 with duplicate: true and changes nothing. So a webhook that is retried three times still grants one month, not three. Checkout and account links carry a signed uid.exp.mac token that is valid for 72 hours, and the Worker checks the same bytes (one shared test vector in both test suites). The plan file sits outside the user’s silo, in a ledger folder the model cannot see. Inside the silo, any user could ask their own operator to mark them as comped.

claude code — desktop
> POST /v1/billing  (the same paid event, sent twice)
first: 200 plan → active, event key written to _ledger/billing-events.jsonl
second: 200 {"ok": true, "duplicate": true}, plan unchanged
wrong bearer: 401 · unknown user: 404
A paraphrase of the named tests test_replayed_event_200_no_change, test_bad_bearer_401 and test_unknown_user_404. Not a recorded run.
A flow diagram: Stripe sends a signed webhook to a Cloudflare Worker, the Worker passes a neutral event to a loopback billing ingest with a ledger of seen event ids, the ingest writes a plan file outside the user's silo, and a may_talk gate reads that plan before any message reaches the model. A dashed switch on the gate is labelled BILLING_ENFORCE off
replay = 200, no change
Commits d4b7ece and 5e36cc9. Stripe never talks to the bot directly.
02 The seam

Seven marked insertions, and one flag that decides whether they act

The bot’s main file already serves people every day, so billing stays out of it. The logic lives in a new module, billing_commands.py (268 lines). main.py gets seven insertions, each tagged # W1 billing: the billing route and its ingest, the admin commands, /plan and the “cancel my subscription” phrase, the may_talk gate, placed before the usage limits, the schedule skip for a paused silo, and the daily trial and payment cards. The whole change to main.py is 35 lines. One grep for the tag finds every insertion, so they are easy to review and easy to rip out. Then the switch. BILLING_ENFORCE is off unless it is set. While it is off, the gate, the cards and the schedule skip do nothing. The ingest, /plan, /comp and /refund work either way, so plans get recorded and beta users get comped before anyone can be stopped. The gate is one pure function, may_talk(plan, now), over the states trial, active, past due (with 7 days’ grace), paused and canceled, plus a comped override. A paused user gets one message a day with an account button and never reaches the model.

claude code — desktop
> grep -n "# W1 billing" gateway/pocket_gateway/main.py
30, 31  import the billing module and the ingest
117  the ingest for POST /v1/billing
545  admin: /comp, /plan <uid>, /refund
746  /plan and the cancel phrase
780  the gate: paused or canceled never reaches the model
929  schedules do not fire for a paused silo
1011 trial and payment cards
1240 mount /v1/billing on the loopback listener
Today’s file: 9 tagged lines. The commit message counts them as seven insertions.
A dark diff view of the bot's main file, collapsed to the changed hunks only, each green insertion with an amber # W1 billing tag in the margin, the gate hunk highlighted, and a small switch icon reading BILLING_ENFORCE=0
grep "# W1 billing"
Commit e76c2f9: 268 lines in a new module, 35 in main.py.
03 Caught before go-live

Four bugs, and a route that still answers 404

Merging dark gives bugs a place to show up while they still cost nothing. Four did. One: the gateway’s own small Stripe client, used only for refunds, pinned API version 2025-03-31.basil, while the Worker and the account run 2026-08-26.dahlia. That is one account with two shapes of the same object. Both now pin dahlia. Two: /refund printed every amount with a hard-coded “kr”, after the prices had moved to USD. A refund preview in the wrong currency is exactly the kind of line you approve without reading. It now prints the charge’s own currency. Three: the billing tests passed on their own branch. Then a parallel branch, bring-your-own-AI, made the bot refuse to answer for any silo that has neither its own AI sign-in nor a comp. The tests now stub that credential check as comped, so they hold after the merge, and the trial merge ran 416 passed, 1 skipped. Four came from a review the next day. The billing ingest’s bearer fell back to the connect route’s secret, so one leaked secret could forge billing events. The ingest now needs its own secret. Without one, the route answers 503, not 404, because the Worker retries on a 5xx while a 404 would end Stripe’s retries.

claude code — desktop
> curl -X POST https://<tunnel host>/v1/billing  (no bearer)
404, re-checked 2026-10-04 06:00 UTC by the nightly triage
The tunnel’s ingress still maps only ^/v1/connect$. The billing code already runs in the gateway release, and BILLING_ENFORCE is not in its env, so it is off.
After step 1 of the live switch, the expected answer is 401.
From the triage note in the project summary. The host is left out.
A dark terminal: a POST to the billing route through the tunnel returns 404, the connect route next to it returns 401, and a grey note says the code is deployed but the route is not in the tunnel's ingress yet
deployed ≠ live
Commits 6eaf12e, f9c858d, 10184d4, b7c65b1 and c46b3a2.
⚠️

The honest part: it is not live, and nobody has paid yet

Everything on this page is merged, and the code runs in production, switched off. The tunnel route is closed, the live prices and the live webhook endpoint do not exist yet, and BILLING_ENFORCE is unset. The sandbox proved the loop end to end in Stripe test mode on 2026-10-01. Still to do before the first live charge: accept the Managed Payments terms on the live account, name Stripe and Link as processors on the privacy page, comp every beta silo, then buy one plan with my own card and refund it. Family bots and team workspaces stay outside billing; the gate covers only personal silos on the shared bot.

04 Receipts

The operator side, and the plan for undoing it

/plan <uid> shows one user’s state. /comp gives a beta tester free time. Since the merge it also takes a silo name, because family bots and team workspaces only run on the shared key when they are comped. /refund only shows a preview, and acts when the same line is sent again with --yes. All of these work in the admin chat only. Below that: the states the gate decides between, and the hand-over that was written before go-live. Tap any image to enlarge it and read the exact prompt that drew it.

MERGED DARK · 2026-10-01..04

Merge money code dark, and write the rollback first

The safest time to find a billing bug is while billing cannot stop anyone. The code is deployed, the gate is off and the route is closed. The hand-over already says how to undo each step on its own: restore the tunnel config, set the flag back to 0 and restart, roll back the gateway release with the one line its deploy script prints, roll back the Worker, and disable the webhook while Stripe holds the events for three days. Go-live is now flipping a switch I already know how to flip back.

A bold card on near-black with a large master switch in the off position, a small teal rollback arrow curling back from the on position, and the line merge it dark, write the rollback first
7 marked insertions · BILLING_ENFORCE off by default · idempotent on event id · rollback per step · route 404 until launch
06 Steal this

Ship your own billing dark

Take the pattern, not my product: a neutral billing ingest that is idempotent on the event id, a pure gate function, an enforcement flag that is off by default, and a hand-over with a rollback for every step. Everything in this episode is free and open — clone it, run it, make it yours.

skill may_talk(plan, now): one pure function, a named test for every state cmd grep -n "# W1 billing": every seam in one list template hand-over: deploy, live switch, rollback per step, merge note assets a ledger of seen event ids: a replay answers 200 and changes nothing
run it # repo soon. the idempotency rule in one line: if ledger.seen(event_id): return 200, {"ok": True, "duplicate": True}

No GitHub? Comment DARK on the post and the bot DMs you the link.

Next episode

The timeout that said it was a file lock

Next: a wrapper that runs my jobs reported a timeout as “file lock, exit 1”, and I went hunting for a lock that did not exist. Now it exits 124, says TIMED OUT, and a test pins it.

A darkened teaser image of a terminal with a blocked run
drops soon · follow so you don’t miss it