feat(plans): three-plan entitlements, 402 plan_limit task gate, Starter checkout (app#2044 row 3)

Merged
SW
3mo ago
+1604-88

Implements row 3 of app#2044. Fulfils the contract in docs#324 (merge the docs PR first).

What changed

  • lib/plans/ (new): Plan = free | starter | pro; getPlanEntitlements (Free 1 task / weekly / $3.33, Starter 3 / daily / $20, Pro uncapped / hourly / $300); resolvePlan (Starter price id => starter, any other active account sub, org sub, or enterprise domain => pro, otherwise free); getMinCadenceMinutes + parseCronField (walk the calendar from a fixed epoch, shortest gap between consecutive fires, standard DOM/DOW OR semantics, no new dependency); findTaskPlanViolation; buildPlanLimitBody; PlanLimitError; assertTaskWithinPlan (the write-path gate); isTaskPlanBlocked + getTaskRunBlock (the run-path gate); No grandfather window — limits apply immediately.
  • getAccountSubscriptionState gains plan; isPro is now plan === "pro", so Starter keeps the free-tier gates on roster scrape, any-recipient emails, and API keys.
  • Credits derive the monthly total from the plan: checkAndResetCredits (refill floor), initializeAccountCredits (seed), buildAccountCreditsResponse (GET /api/accounts/{id}/credits gains plan, task_limit, min_cadence_minutes). PRO_CREDITS_USD 99.99 -> 300, new STARTER_CREDITS_USD 20.
  • Task gate: createTask calls assertTaskWithinPlan before the insert (covers REST and MCP create_task); updateTask calls it when a PATCH re-enables a disabled task (takes a slot) or changes schedule (cadence only); title/prompt/model edits never touch it. Both handlers map PlanLimitError to 402 with the documented body. The owner's plan is checked, not the caller's; admins are not exempt.
  • Run path: GET /api/tasks?id= (what the Trigger.dev runner reads before every run, tasks/src/recoup/fetchTask.ts) answers 402 plan_limit when the task is outside its owner's plan. The runner treats any non-200 as "skip this run" today, so no change to the tasks repo. Limits apply immediately to every enabled task (including ones that existed before the gate); a downgraded account loses its extra / denser tasks at once. Over the cap, the oldest tasks keep their slots (created_at, then id tie-break).
  • Starter checkout: POST /api/subscriptions/sessions accepts optional plan (default pro); Starter uses STRIPE_STARTER_PRICE_ID with no trial and returns 400 { error: "starter_unavailable" } while the env var is unset. plan is stamped on session and subscription metadata. GET /api/accounts/{id}/subscription reports plan: "starter" with isPro: false for the Starter price.
  • lib/stripe/config.ts gains STRIPE_STARTER_PRICE_ID (env, empty default). The Stripe-first checkout PR (row 8) appends the identical line so the two merge cleanly.

Assumptions (Sweets' "Go" on 2026-08-29; each is one constant)

DecisionValueWhere
Pro credit multiple3x price = $300 (5x is a one-line change)PRO_CREDITS_USD
Pro minimum cadencehourlygetPlanEntitlements
Grandfatheringnone — enforce immediately (Sweets 2026-08-30)n/a
Starter trialnone, $19 charged at checkoutcreateStripeSession
Starter price idSTRIPE_STARTER_PRICE_ID env, set by Sweets after creating the $19/mo price in the Stripe dashboardVercel env

Not included: a separate email warning when existing tasks are blocked. The app's upgrade prompt (rows 1 and 4) is the surface that tells owners; adding a second channel here was left out to keep the gate small.

Tests

Red-first per unit. Domain suite: 485 tests pass across lib/plans lib/tasks lib/credits lib/stripe app/api/{subscriptions,tasks,accounts} after the 2026-08-30 review fixes. tsc --noEmit clean for touched files (repo baseline noise unchanged), eslint + prettier clean.

Preview verification follows in a comment.

🤖 Generated with Claude Code

https://claude.ai/code/session_01GkSubbDhRVoNanCEoKgL6K


Summary by cubic

Implements row 3 of app#2044: three-plan entitlements (free/starter/pro), a 402 plan_limit gate on the task API, and Starter plan checkout.

Plan entitlements and task gate

  • Free is 1 weekly task, Starter is 3 daily, Pro is uncapped hourly; monthly credits follow the plan ($3.33/$20/$300).
  • POST /api/tasks and PATCH /api/tasks return 402 plan_limit before writing when a create, re-enable, or schedule change exceeds the owner's plan; admins are not exempt.
  • GET /api/tasks?id= answers 402 for a task the plan no longer allows, which makes the Trigger.dev runner skip the run; over the cap, the oldest tasks keep their slots.
  • Limits apply immediately to pre-existing tasks, so a downgrade loses extra tasks at once; plan_limit responses point billingUrl at /plan.
  • A Starter account with an active org Pro subscription resolves as Pro, and the org subscription drives credit refills.
  • Internal plan state flows as plan everywhere; is_pro/isPro is derived only at API response edges.

Starter checkout

  • POST /api/subscriptions/sessions accepts optional plan (default pro); Starter charges at checkout with no trial and stamps plan on session and subscription metadata.
  • Starter returns 400 starter_unavailable until STRIPE_STARTER_PRICE_ID is set; until then, every paid price resolves as pro.
  • Starter keeps the free-tier gates on roster scrape, recipient emails, and API keys.
  • GET /api/accounts/{id}/credits now reports plan, task_limit, and min_cadence_minutes.
  • Set STRIPE_STARTER_PRICE_ID in the environment to enable Starter checkout.

Summary by CodeRabbit

  • New Features

    • Added Free, Starter, and Pro plan support with plan-based credits, task limits, and scheduling cadence rules.
    • Added Starter plan checkout at $19/month, with plan selection during subscription setup.
    • Added clear upgrade guidance when task limits or scheduling frequency restrictions are reached.
    • Added plan details to account credit and subscription information.
  • Bug Fixes

    • Correctly identifies Starter subscriptions instead of treating all paid subscriptions as Pro.
    • Prevents task creation, updates, or runs that exceed the active plan’s allowances.