https://sonarly.com/issue/38542?type=bug
A worker-side billing query timed out while updating the “has reached cap” flag, causing failures during post-execution billing for an affected workspace. The failure is in server billing logic, not the frontend change shipped in v2.6.1.
Fix: Implemented a production fix in the exact timeout path:
1) Reduced query cost in `setSubscriptionItemHasReachedCap`:
- Changed lookup to use `billingSubscriptionId` (already available from workspace cache in caller) instead of joining through `billingSubscription.workspaceId + status`.
- Switched from `find(...)` (array load) to `findOne(...)` selecting only needed fields (`id`, `hasReachedCurrentPeriodCap`).
- Added a no-op short-circuit when the cap flag already matches target state, avoiding unnecessary updates.
2) Reduced call frequency for cap-flag lookup:
- In `decrementAvailableCreditsInCache`, cap update now runs only on threshold crossing (`availableCredits > 0 && decrementedAvailableCredits <= 0`) rather than on every decrement while already negative.
- Passed `billingSubscriptionId` from workspace cache into cap service.
This directly addresses the expensive worker-side cap-flag subscription-item query that timed out.
Authored by Sonarly by autonomous analysis (run 43909).