feat(credits): /research/web → 1 credit + auto top-up opt-out via Stripe customer metadata
Summary
Implements the api slice of recoupable/chat#1861, fulfilling the contract in docs#270 (merge order: docs#270 → this → chat).
1. Web search repriced 5 → 1 credit
lib/research/ensureWebResearchCredits.ts— dedicated 1-credit gate forPOST /api/research/web(the sharedRESEARCH_CREDIT_COST = 5is untouched: Songstats-backed endpoints cost us ~$0.09/call and stay at 5; Perplexity web search costs a flat $0.005/request).postResearchWebHandlerdeducts 1 on success.
2. Auto top-up opt-out (consent on the Stripe Customer)
lib/stripe/getAutoRechargeOptOut.ts— freshcustomers.retrieve(never the ~60s-stale search-index copy), presence semantics: any non-emptyauto_recharge_opt_outmetadata value = opted out.lib/stripe/setAutoRechargeOptOut.ts— opt-out stamps"true"; opt-in writes""(Stripe deletes the key). Never writes"false".autoRechargeOrFailchecks consent beforechargeCustomerOffSession; opted-out accounts get the existing402 + checkoutUrlCheckout fallback — manual top-ups (and card saving via Checkout) keep working, but the saved card is never charged silently.- New
GET/PATCH /api/accounts/{id}/auto-recharge(mirrors thepayment-methodroute: same param validation, auth viavalidateAuthContext, flat{ account_id, enabled }response). GET is read-only (no Customer provisioning); PATCH resolves viaresolveStripeCustomerForAccount— the samemetadata.accountIdlookup the charge path uses — so the flag always lands on the Customer the gate reads.
TDD evidence
Every unit was RED before GREEN (new modules failed collection; modified suites failed on the new assertions — e.g. creditsToDeduct: 5 vs expected 1). Final: 87 test files / 385 tests pass across lib/stripe, lib/billing, lib/credits, lib/research; tsc --noEmit reports zero errors in touched files (remaining tsc noise is pre-existing on origin/main, verified by stash-compare); pnpm lint clean.
Preview verification results will be posted as a comment once the deployment is Ready.
🤖 Generated with Claude Code
Summary by cubic
Repriced web search to 1 credit and added a Stripe-based auto top-up opt-out so off-session charges only happen with consent. This makes web research cheaper and gives users a clear way to avoid silent charges.
-
New Features
POST /api/research/webnow costs 1 credit viaensureWebResearchCredits; other research endpoints stay at 5.- Auto top-up opt-out stored on the Stripe Customer (
auto_recharge_opt_outmetadata).autoRechargeOrFaildoes a freshcustomers.retrieve, never charges off-session when opted out, and falls back to 402 with acheckoutUrl. GET/PATCH /api/accounts/{id}/auto-rechargeto read/update the setting. Default is enabled;GETis read-only;PATCHprovisions the Customer and uses presence-based metadata (delete on opt-in; never write "false").
-
Refactors
- Consolidated PATCH validation:
validateUpdateAutoRechargeBodynow handles path/auth checks and safe JSON parsing; response contract unchanged.
- Consolidated PATCH validation: