Commit Graph
7 Commits
Author SHA1 Message Date
Malik Glossop 42feabe643 Merge pull request #35907 from overleaf/mg-ai-usage-event
Fire a 'ai-usage-limit-reached' event in backend

GitOrigin-RevId: b43babdd2a0146086cee5ebd2bde83f8daaf1bb5
2026-09-17 11:50:38 +00:00
Antoine ClausseandMathias Jakobsen 87fd22cefc [web] Fix negative AI quota countdown after reset period elapses (#35690)
* Fix negative AI quota countdown after reset period elapses

Backend (`FeatureUsageRateLimiter`, `TokenUsageRateLimiter`): once the
24h period had lapsed, `remainingUsage`/`remainingTokens` reset to the
full allowance but `resetDate` kept pointing at the old `periodStart +
24h` (i.e. in the past). Advance to `now + 24h` in that case.

Frontend (`NewPlanPaywall`, `ErrorGetPremium`): the "wait X hours and Y
minutes" copy computed `restoreTime - now` without clamping, so a past
`restoreTime` produced negative hours/minutes (users saw "wait -118
hours and -35 minutes"). Clamp to 0.

* Address PR review on quota reset fix

- Keep `resetDate` tied to the stored `periodStart + PERIOD` rather
  than jumping to `now + PERIOD` when the period has lapsed. The
  moving-target variant unnecessarily reinterpreted a stable field
  (existing consumers such as `use-ai-access-gate` check whether the
  reset date is in the past).
- Clamp `remainingUsage` / `remainingTokens` at 0 so a concurrent
  over-usage never leaks a negative count.
- Align the boundary check with `_checkRateLimit` / `checkUsage`
  (inclusive `<=`).
- Cover the period boundary and the clamp in unit tests for both
  `FeatureUsageRateLimiter` and `WorkbenchRateLimiter`.

* Truncate test fixtures to whole seconds

`resetDate` is serialized with `Date.toString()`, which has second
resolution, so fixtures with a millisecond component failed the
boundary assertions.

* Revert code changes; keep tests that document the bug

The fixes masked the symptom (negative countdown) without addressing
the underlying issue, and it is not fully understood yet. Revert the
rate limiters and the writefull paywall components to their original
behaviour, and instead pin the current behaviour in tests:

- `it.fails` tests document that `remainingUsage`/`remainingTokens`
  go negative when usage exceeds the allowance — these flip red when
  the bug is fixed, at which point they should become plain `it`.
- The lapsed-period tests assert (with explanatory comments) that
  `resetDate` keeps pointing at the end of the lapsed period, which is
  the server-side ingredient of the negative-countdown support ticket.

* Reapply quota fixes

Restores the post-review state of the rate limiters (clamp remaining
counts at 0, inclusive period boundary, resetDate tied to periodStart)
and the writefull paywall components (clamp countdown at 0).

Tests intentionally untouched in this commit; the two `it.fails` tests
now need flipping to plain `it` in a follow-up.

* Flip bug-documenting tests to plain assertions

The clamp fixes are reapplied, so the `it.fails` tests now assert the
fixed behaviour directly. Trim the explanatory comments.

* Set resetDate to `Date.now() + PERIOD_IN_MILLISECONDS` instead of the expired/stale date.

Co-authored-by: Mathias Jakobsen <mathias.jakobsen@overleaf.com>

* Update lapsed-period tests for the fresh resetDate

`resetDate` is now `Date.now() + PERIOD` once the period has lapsed, so
assert an approximately-fresh date instead of the stale period end.

---------

Co-authored-by: Mathias Jakobsen <mathias.jakobsen@overleaf.com>
GitOrigin-RevId: ad39627ab9e5ceccb2a61443b2b7d39c68600594
2026-09-17 11:49:47 +00:00
Andrew Rumble e0d91517e8 Merge pull request #35225 from overleaf/copilot/set-globals-false-vitest
Set `globals: false` in web vitest configuration

GitOrigin-RevId: 3b809d0b5c47910f56ce81fa229292065b4527f0
2026-09-17 11:19:16 +00:00
Jimmy Domagala-Tang 41b96ec8d6 feat: ai quotas should reset when a new plan purchase is made or upgraded (#33095)
GitOrigin-RevId: 9034800e067426fc22f8f86f9d7309699797d02e
2026-04-30 08:06:30 +00:00
Jimmy Domagala-TangandCopilot 3fa5ce8b16 Add Shared Usage Quota to Writefull Functions (#31867)
* feat: moved error handling to controller, added config for each route, added usage rate limiter, and removed uneeded functions from manager

* feat: allow for 0 usage cost features in Writefull

* feat: update decrement to also use cost, and make cost optional, defaulting to 1

* feat: adding WF quota tests and updating tests for quota rate limiter (#31982)

* feat: adding WF quota tests and updating tests for quota rate limiter

* Apply suggestions from code review

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
GitOrigin-RevId: 281cb6b50b54c02f54a85a6492ca322ef8f00429
2026-03-06 09:14:46 +00:00
Jimmy Domagala-Tang 501e11a42a Move feature rate limiters to shared web folder (#31855)
* feat: remove old assist split test

* feat: moving featue rate limiters to main shared directory for use in multiple modules

* feat: base workbench rate limiter on a token specific base class

* feat: rename aiErrorAssistRateLimiter to AiFeatureUsageRateLimiter to better reflect its for our shared ai usage quota

GitOrigin-RevId: 89464d115b5904f6274756a7169e2b35945e2fc9
2026-03-06 09:13:40 +00:00
Mathias Jakobsen ae2d28505b [web] Abstract AiErrorAssistantRateLimiter (#31176)
* [web] Abstract AiErrorAssistantRateLimiter

* [web] Remove AI error assist reference in FeatureUsageRateLimiter

GitOrigin-RevId: cb12ea8b7a5a06576087b937380652fbe1948e95
2026-02-10 09:06:25 +00:00