Skip to content

fix(billing): bound every ledger aggregate at the database and size the gate deadline for two - #8148

Merged
waleedlatif1 merged 2 commits into
stagingfrom
fix/ledger-reads-bounded
Sep 22, 2026
Merged

waleedlatif1 merged 2 commits into
stagingfrom
fix/ledger-reads-bounded

Conversation

@waleedlatif1

Copy link
Copy Markdown
Collaborator

Summary

Type of Change

  • Bug fix

Testing

  • New helper test pins the bound is set inside one transaction on the given client before the read, and that read failures surface
  • Every ledger aggregate has a test asserting it reads through the bounded transaction; removing the bound fails 7 tests
  • Real-Postgres suites for the ledger and member limits pass against a local database (24 tests), exercising the transaction and SET LOCAL for real
  • bun run lint, check:audits (47 audits), docs-manifest:check, type-check pass; 1,031 billing and admission tests pass

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

vercel Bot commented Sep 22, 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 Skipped Skipped Sep 22, 2026 7:15pm UTC

Request Review

@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 10 files

Confidence score: 5/5

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

Re-trigger cubic

@greptile-apps

greptile-apps Bot commented Sep 22, 2026

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

The PR appears safe to merge; the previously reported unbounded stamped-period aggregate is fixed and no new actionable issues remain.

Summary

This follow-up consistently applies a transaction-local statement timeout to payer ledger aggregates and allows the usage gate enough time for its worst-case sequence.

  • Extracts readLedgerBounded to establish SET LOCAL statement_timeout before each aggregate.
  • Routes period, workflow-count, source-subset, per-user, weekly-refresh, member-cap, and stamped-range aggregates through the helper.
  • Expands the gate settlement deadline to cover two sequential bounded reads.
  • Adds focused coverage for timeout ordering, error propagation, and each migrated aggregate.
Diagram
sequenceDiagram
  participant Caller
  participant Gate as Usage Gate
  participant DB as PostgreSQL
  Caller->>Gate: Request usage decision
  Gate->>DB: BEGIN
  Gate->>DB: SET LOCAL statement_timeout
  Gate->>DB: Aggregate payer usage
  DB-->>Gate: Usage result
  Gate->>DB: COMMIT
  opt Organization member cap applies
    Gate->>DB: BEGIN
    Gate->>DB: SET LOCAL statement_timeout
    Gate->>DB: Aggregate member usage
    DB-->>Gate: Member result
    Gate->>DB: COMMIT
  end
  Gate-->>Caller: Admission decision
Loading

Reviews (2) · Last reviewed commit: "fix(billing): bound the cycle-close per-..."

Comment thread apps/sim/lib/billing/core/ledger-read.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 Sep 22, 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 10 files

Confidence score: 5/5

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

Re-trigger cubic

@waleedlatif1
waleedlatif1 merged commit c28366c into staging Sep 22, 2026
34 checks passed
@waleedlatif1
waleedlatif1 deleted the fix/ledger-reads-bounded branch September 22, 2026 19:23

This branch was previously deployed

1 inactive deployment
Preview ed269c66 Deployed Sep 22, 2026 by vercel[bot]
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