Skip to content

fix(misc): upgrade path change for new better-auth version, billing issue for workflow block agent usage#4803

Merged
icecrasher321 merged 2 commits into
stagingfrom
fix/better-auth-upgrade
May 30, 2026
Merged

fix(misc): upgrade path change for new better-auth version, billing issue for workflow block agent usage#4803
icecrasher321 merged 2 commits into
stagingfrom
fix/better-auth-upgrade

Conversation

@icecrasher321
Copy link
Copy Markdown
Collaborator

Summary

  • Better auth upgrade needs stripe sub id now instead of row id (Prod bug)
  • Billing discrepancy for agent blocks inside workflow in workflow block (only on Staging)

Type of Change

  • Bug fix

Testing

N/A

Checklist

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

@vercel
Copy link
Copy Markdown

vercel Bot commented May 30, 2026

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

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
docs Skipped Skipped May 30, 2026 12:23am

Request Review

@cursor
Copy link
Copy Markdown

cursor Bot commented May 30, 2026

PR Summary

Medium Risk
Touches production subscription upgrades and execution cost aggregation; both are billing-critical but changes are narrow with targeted regression tests for cost logic.

Overview
Fixes subscription upgrades after a better-auth change: betterAuthSubscription.upgrade now passes stripeSubscriptionId instead of the internal subscription row id, while org transfer after a team upgrade still uses the row id in subscriptionTransferContract.

Fixes execution billing for runs wrapped in synthetic workflow trace roots (including nested sub-workflows): calculateCostSummary treats type === 'workflow' spans as aggregate pass-throughs—it recurses into children but does not bill the root’s rolled-up cost.total, preventing double-charging agent/tool leaves. New tests cover the outer execution root and nested sub-workflow roots.

Reviewed by Cursor Bugbot for commit e09b32a. Configure here.

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented May 30, 2026

Greptile Summary

This PR fixes two independent bugs: a production issue where better-auth's subscription upgrade endpoint now requires the Stripe subscription ID instead of the database row ID, and a staging billing discrepancy where agent blocks nested inside a workflow block were double-charged because the synthetic workflow root span carries a cost equal to the sum of its descendants.

  • upgrade.ts: Splits the single currentSubscriptionId into currentSubscriptionRowId (used for the internal transfer API) and currentStripeSubscriptionId (passed to betterAuthSubscription.upgrade), fixing the production upgrade path.
  • logging-factory.ts: Introduces an isAggregateContainer guard that treats workflow-typed spans as pass-through containers, preventing their aggregate cost from being counted alongside the leaf spans that already account for it. Two regression tests cover both flat and nested sub-workflow scenarios.

Confidence Score: 4/5

Safe to merge — both fixes are narrowly scoped, well-commented, and backed by new regression tests.

The billing fix in upgrade.ts introduces a subtle assumption: stripeSubscriptionId must be present on every active subscription returned by client.subscription.list(). If an older subscription record lacks this field, the code silently omits the ID and creates a duplicate subscription rather than upgrading — the same failure mode the PR intends to prevent. The logging-factory.ts change is clean and the new tests directly cover the reported double-counting scenario.

apps/sim/lib/billing/client/upgrade.ts — the new stripeSubscriptionId lookup should be verified to be reliably populated on all active subscription records in production.

Important Files Changed

Filename Overview
apps/sim/lib/billing/client/upgrade.ts Splits the single subscription ID into currentSubscriptionRowId (DB row) and currentStripeSubscriptionId (Stripe); uses the Stripe ID for better-auth's upgrade call and the row ID for the internal transfer endpoint.
apps/sim/lib/logs/execution/logging-factory.ts Adds isAggregateContainer guard to skip workflow-typed spans (which carry a cost equal to the SUM of their children) when collecting billable spans, preventing double-counting of agent/tool costs inside workflow blocks.
apps/sim/lib/logs/execution/logging-factory.test.ts Adds two targeted regression tests — one for a flat workflow root with agent + tool children, one for nested sub-workflow roots — both verifying that aggregate container costs are not double-counted.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[handleUpgrade called] --> B[client.subscription.list]
    B --> C{activePersonalSub found?}
    C -- Yes --> D[currentSubscriptionRowId = sub.id\ncurrentStripeSubscriptionId = sub.stripeSubscriptionId]
    C -- No --> E[both IDs = undefined]
    D --> F{currentStripeSubscriptionId set?}
    E --> F
    F -- Yes --> G[finalParams includes\nsubscriptionId: stripeSubscriptionId]
    F -- No --> H[finalParams omits subscriptionId\nnew subscription created]
    G --> I[betterAuthSubscription.upgrade]
    H --> I
    I --> J{targetPlan === team\nAND currentSubscriptionRowId?}
    J -- Yes --> K[Transfer subscription\nusing currentSubscriptionRowId]
    J -- No --> L[Done]
    K --> L
Loading

Reviews (1): Last reviewed commit: "fix(misc): upgrade path change for new b..." | Re-trigger Greptile

Comment thread apps/sim/lib/billing/client/upgrade.ts
@icecrasher321 icecrasher321 merged commit c51c41f into staging May 30, 2026
13 checks passed
@waleedlatif1 waleedlatif1 deleted the fix/better-auth-upgrade branch May 30, 2026 03:00
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