Skip to content

improvement(billing): share the enterprise reporting usage read across processes - #8325

Merged
waleedlatif1 merged 4 commits into
stagingfrom
improvement/shared-reporting-usage-cache
Sep 26, 2026
Merged

waleedlatif1 merged 4 commits into
stagingfrom
improvement/shared-reporting-usage-cache

Conversation

@waleedlatif1

@waleedlatif1 waleedlatif1 commented Sep 26, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Enterprise reporting-window usage sums (a year-long ledger scan) are now shared across processes through Redis with a 30s TTL. Trigger.dev runs every task in a fresh process, so the existing in-process LRU was always cold there, and the scan ran on nearly every document-processing and execution admission check
  • The in-process LRU stays in front and still coalesces concurrent misses. The Redis GET has a 250ms deadline, and any Redis error, timeout, or unreadable value falls through to the exact sum. The write is a fire-and-forget SET … NX with a jittered TTL, so a slower, older sum never overwrites a fresher one or extends its life. There is no lock or lease
  • The usage threshold email is now level-triggered, instead of being edge-triggered off an exact before/after org sum on every workflow completion. A claim keyed on (billing period, limit) (claimCreditsThreshold) sends each threshold at most once. A new period or a changed limit re-arms it with no reset write, and concurrent completions can't both send
  • Recipients are resolved before claiming, so a period's email isn't used up when nobody can receive it. Zero-cost completions don't claim. The personal usage baseline and the claimed period come from the same billing context
  • Invoicing, cycle close, overage, and threshold billing still read the ledger exactly
  • Rollout note: accounts already at or above 80% this billing period get one threshold email after deploy; there is no backfill

Type of Change

  • Improvement

Testing

  • New integration tests against real Postgres and Redis (reporting-usage-cache.integration.ts, usage-threshold-email.integration.ts). Six of the seven fail on the pre-change code. The Redis-unreachable test goes red if the read deadline is removed
  • Covered: a Redis hit skips the sum; a miss sums and stores it with a TTL; a corrupt value is treated as a miss; one email per period across concurrent and repeated completions; 80% then 100%, never the reverse; re-arm next period
  • lib/billing and lib/logs/execution unit suites, type-check, lint, and check:audits 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 26, 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 26, 2026 8:18pm 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.

All reported issues were addressed across 8 files

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

Fix all with cubic | Re-trigger cubic

Comment thread apps/sim/lib/billing/core/usage.ts Outdated
Comment thread apps/sim/lib/billing/core/reporting-usage-cache.ts Outdated
Comment thread apps/sim/lib/billing/core/reporting-usage-cache.ts Outdated
Comment thread apps/sim/lib/logs/execution/logger.ts
@greptile-apps

greptile-apps Bot commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[High risk] Refactors billing usage cache to share reads across processes via Redis.

The PR appears safe to merge; no outstanding findings or new actionable issues were identified.

Summary

The PR shares enterprise reporting-window usage reads through Redis while retaining the local cache and exact ledger fallback. It also makes credits-threshold emails level-triggered and deduplicates them by billing period and limit.

  • The latest changes add an indexed claim pre-check and consolidate unsubscribe mocks.
  • The previous findings are resolved or were withdrawn.
Diagram
%%{init: {'theme': 'neutral'}}%%
flowchart LR
  A[Usage read] --> B{Local cache hit?}
  B -- Yes --> C[Return cached sum]
  B -- No --> D{Redis hit?}
  D -- Yes --> C
  D -- No --> E[Sum ledger]
  E --> F[Store with Redis NX and TTL]
  E --> C
  G[Completion above threshold] --> H{Claim unclaimed?}
  H -- No --> I[Stop]
  H -- Yes --> J[Resolve eligible recipients]
  J --> K{Atomic claim won?}
  K -- Yes --> L[Send email]
  K -- No --> I
Loading

Reviews (4) · Last reviewed commit: "improvement(billing): skip claimed usage..."

Comment thread apps/sim/lib/billing/core/usage.ts Outdated
Comment thread apps/sim/lib/billing/core/reporting-usage-cache.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 26, 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 8 files

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

Fix all with cubic | Re-trigger cubic

Comment thread apps/sim/lib/logs/execution/logger.ts
Comment thread apps/sim/lib/billing/core/usage.ts
Comment thread apps/sim/lib/billing/core/reporting-usage-cache.ts
Comment thread apps/sim/lib/billing/core/limit-notifications.ts Outdated
Comment thread apps/sim/lib/billing/core/reporting-usage-cache.ts
Comment thread apps/sim/lib/billing/core/limit-notifications.ts Outdated
Comment thread apps/sim/lib/billing/core/usage-threshold-email.integration.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 26, 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 8 files

Confidence score: 5/5

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

Re-trigger cubic

@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 26, 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 12 files

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

Fix all with cubic | Re-trigger cubic

Comment thread apps/sim/lib/billing/core/usage.ts
@waleedlatif1
waleedlatif1 merged commit 10282f2 into staging Sep 26, 2026
32 checks passed
@waleedlatif1
waleedlatif1 deleted the improvement/shared-reporting-usage-cache branch September 26, 2026 21:29

This branch was previously deployed

1 inactive deployment
Preview — 3f3716bb Deployed Sep 26, 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