Skip to content

fix(billing): hold free plans to their allowance mid-run - #9462

Merged
waleedlatif1 merged 3 commits into
stagingfrom
fix/free-tier-cap-integrity
Oct 30, 2026
Merged

waleedlatif1 merged 3 commits into
stagingfrom
fix/free-tier-cap-integrity

Conversation

@waleedlatif1

Copy link
Copy Markdown
Collaborator

Summary

  • Free-plan spend past the one-time allowance is never billed, but runs could overshoot it: the verdict returned after each Mothership cost callback came from a cached admission (free payers are always "near limit", so up to a 10 s window), and an unreadable or slow standing read answered not-exceeded
  • After a cost callback, a free payer's standing is now read fresh from the ledger (checkAttributedUsageLimits, the function the cache wraps; the direct-v1 path skips its verdict cache and shared in-flight read), so the callback that crosses the limit pauses the run
  • Free payers fail closed when standing can't be read — update-cost (unknown or the 1 s budget), the provider mid-run check, the copilot continuation re-check, and Mothership continuation admission all stop/refuse a free run with the usage card. Paid plans and orgs keep today's fail-open behaviour so a database blip never ends a paying run
  • isFreeRunPayer decides free from what the run was admitted with (attributed payerSubscription; a direct-v1 decision without payerSubscriptionId), so no new query; errors land on the paid (status-quo) side
  • Default free-plan concurrency 10 → 2 (docs table updated); admission caching for workflow executions is unchanged

Type of Change

  • Bug fix

Testing

  • app/api/billing/update-cost/route.integration.ts (real Postgres): free direct-v1 and free attributed payers are paused when a second callback crosses the limit inside the cache window; a pro payer in the same scenario keeps the cached answer. Red with the fresh-read bypass disabled, green restored
  • Unit: update-cost/route.test.ts (fresh read, unreadable ledger, failed gate read, timeout → exceeded for free; blocked → not exceeded; paid unchanged), providers/run-usage-limit.test.ts, copilot/api-keys/validate/route.test.ts, mothership/request/lifecycle/admission.test.ts — each new free-payer guard shown red with the guard reverted
  • lib/billing + touched route/provider/lifecycle suites green; biome, check:test-patterns, check:comment-hygiene, check:unused-exports, check:file-names, check:boundaries
  • Full type-check / audits / suites left to CI

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing (new tests pass the test-audit authoring gate)
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

@vercel

vercel Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated
docs Ignored Ignored Preview Oct 30, 2026 1:34am UTC

Request Review

@greptile-apps

greptile-apps Bot commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no new actionable issues were found.

Summary

Free runs now read their latest spend after cost callbacks and stop when their allowance is spent or cannot be checked. Paid runs keep their existing behavior.

  • Free runs pause when the allowance is reached or cannot be checked.
  • Free accounts now have a default cap of two concurrent workflow executions.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Cost recorded] --> B{Free payer?}
  B -->|Yes| C[Read latest spend]
  B -->|No| D[Use existing cached check]
  C --> E{Standing}
  E -->|Within allowance| F[Continue]
  E -->|Exceeded or unreadable| G[Pause with usage card]
  D --> H{Standing}
  H -->|Exceeded| G
  H -->|Within or unreadable| F
Loading

Reviews (3) · Last reviewed commit: "fix(billing): bound free payers' fresh u..." · Reviewed by Greptile

Comment thread apps/sim/lib/billing/concurrency-defaults.ts

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 12 files

This PR changes payment, billing, or quota logic. Ultrareviews find 2.4x more serious bugs than standard reviews. Comment @cubic-dev-ai ultrareview to run one.

Turn on auto-fix | Re-trigger cubic

Comment thread apps/sim/lib/billing/core/mid-run-usage.ts Outdated
Comment thread apps/sim/providers/run-usage-limit.ts
Comment thread apps/sim/app/api/copilot/api-keys/validate/route.ts Outdated
Comment thread apps/sim/lib/billing/concurrency-defaults.ts
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@waleedlatif1 I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 13 files

Reply with feedback, questions, or to request a fix.

Turn on auto-fix | Re-trigger cubic

Comment thread apps/sim/lib/billing/core/mid-run-usage.ts Outdated
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Oct 30, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@waleedlatif1 I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 14 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Turn on auto-fix | Re-trigger cubic

@waleedlatif1
waleedlatif1 merged commit 9e2afe0 into staging Oct 30, 2026
69 checks passed
@waleedlatif1
waleedlatif1 deleted the fix/free-tier-cap-integrity branch October 30, 2026 01:45
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