Skip to content

fix(billing): cache enterprise reporting-window usage sums for soft usage gates - #8267

Merged
waleedlatif1 merged 2 commits into
stagingfrom
fix/enterprise-usage-sum-cache
Sep 24, 2026
Merged

waleedlatif1 merged 2 commits into
stagingfrom
fix/enterprise-usage-sum-cache

Conversation

@waleedlatif1

@waleedlatif1 waleedlatif1 commented Sep 24, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • Enterprise usage periods are year-long reporting windows, so every soft usage check re-summed the payer's entire year of usage_log — once per billable event (every run, Chat request, v1 API response, knowledge document). For a busy enterprise org that is a multi-second-per-call scan thousands of times an hour, and it dominates database CPU
  • New lib/billing/core/reporting-usage-cache.ts: one LRUCache (fetchMethod, max 1000, 30 s TTL) keyed by payer, period source, and window (not per actor); the cached reader is private and typed to reporting periods, reached only via readSoftGateUsageCost; concurrent misses coalesce, a failed sum is never cached
  • Soft readers routed through it: pooled org admission (computePooledOrgUsage → Chat, wand, speech, uploads, credit/usage-gate routes, v1 limits, preprocessing misses) and v1 getEffectiveCurrentPeriodCost
  • The execution logger's usage-threshold emails stay on the exact, edge-triggered path (an edge needs an exact baseline; a TTL bounds staleness in time, not in omitted usage). Its per-completion sum remains and is removed by the ledger running-total follow-up
  • Exact paths untouched: getBillingPeriodUsageCost itself (read-your-writes), threshold billing (exits for enterprise before summing), cycle close, invoicing, analytics, admin and display
  • Staleness is bounded and one-directional: the ledger only grows within a window, so a cached sum can only trail by ≤30 s of usage — never a wrongful refusal
  • Follow-up: a trigger-maintained running total so no reader ever sums the ledger

Type of Change

  • Bug fix

Testing

Cache coalescing, TTL expiry, key isolation (incl. by source), non-reporting bypass, failure-not-cached, executor bypass, admission routing (each revert-proven); lib/billing + lib/logs suites, type-check, lint, 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 24, 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 24, 2026 9:29pm 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/reporting-usage-cache.ts Outdated
Comment thread apps/sim/lib/billing/constants.ts Outdated
@greptile-apps

greptile-apps Bot commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

The PR appears safe to merge; no new actionable finding or outstanding previous finding remains.

Summary

The PR caches enterprise reporting-window usage sums for soft admission checks and the v1 usage report, while retaining exact reads for other periods and threshold emails.

  • The change since the previous review replaces two 2 ms waits in the cache-expiry test with 1 ms waits.
Diagram
%%{init: {'theme': 'neutral'}}%%
flowchart LR
  A[Soft usage reader] --> B{Reporting window on default executor?}
  B -- Yes --> C[Shared 30-second usage cache]
  B -- No --> D[Exact ledger sum]
  C --> E[Ledger sum on cache miss]
Loading

Reviews (5) · Last reviewed commit: "test(billing): use 1 ms waits in the rep..."

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/billing/core/reporting-usage-cache.test.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 Sep 24, 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 5 files

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

Fix all with cubic | Re-trigger cubic

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 24, 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.

Comment thread apps/sim/lib/logs/execution/logger.ts Outdated
Comment thread apps/sim/lib/billing/core/usage.ts Outdated
Comment thread apps/sim/lib/billing/core/limit-notifications.ts Outdated

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

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

Fix all with cubic | Re-trigger cubic

Comment thread apps/sim/lib/billing/core/limit-notifications.ts Outdated
Comment thread apps/sim/lib/billing/core/limit-notifications.ts Outdated
Comment thread apps/sim/lib/billing/core/usage.ts Outdated
Comment thread apps/sim/lib/billing/core/usage.ts Outdated
…sage gates

An enterprise usage period is a reporting window up to a year long, so every
soft usage check re-summed the payer's whole year of usage_log, once per
billable event (every run admission, Chat request, v1 API response, and
knowledge document). For a busy enterprise org that scan ran thousands of
times an hour and dominated database CPU.

readSoftGateUsageCost serves reporting-window sums from a per-process
LRUCache (fetchMethod, max 1000, 30 s TTL) keyed by payer, period source, and
window bounds; concurrent misses coalesce and a failed sum is never cached.
The raw cached reader is private and typed to reporting periods, so every
other period is summed exactly. Pooled org admission (computePooledOrgUsage)
and the v1 usage report (getEffectiveCurrentPeriodCost) read through it.

The ledger only grows within a window, so a cached sum can only trail the
true one by at most one TTL of usage: an admission gate may let a payer run
on briefly past its limit, never refuse it wrongly. Exact paths are
untouched: getBillingPeriodUsageCost itself (read-your-writes), threshold
billing, cycle close, invoicing, analytics, and the execution logger's
edge-triggered usage emails, which need an exact baseline.
@waleedlatif1
waleedlatif1 force-pushed the fix/enterprise-usage-sum-cache branch from 07bcbfb to 08c9641 Compare September 24, 2026 21:20
@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 24, 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 5 files

Confidence score: 5/5

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

Re-trigger cubic

Comment thread apps/sim/lib/billing/core/reporting-usage-cache.test.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 24, 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 5 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 b28837f into staging Sep 24, 2026
34 checks passed
@waleedlatif1
waleedlatif1 deleted the fix/enterprise-usage-sum-cache branch September 24, 2026 21:43

This branch was previously deployed

1 inactive deployment
Preview — c92e5cba Deployed Sep 24, 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