Repository navigation
feat(assistant): add per-user daily quota and anti-flood limits - #110
Merged
Merged
Conversation
Enforce Ask ApplyWell limits before any ChatModel call, independent of the configured provider (OpenRouter, Gemini or future ones): - daily quota per user with configurable reset timezone - sliding-window rate limit and one in-flight generation per user - global daily and per-window limits protecting the shared provider key - portable max output tokens and a per-request tool-call budget Check-and-reserve runs under a SELECT ... FOR UPDATE row lock in MariaDB, so parallel tabs and multiple instances cannot overshoot. Rejections return HTTP 429 with a stable code and Retry-After before the SSE stream opens. Provider failures before any output are refunded; disconnects and partial outputs stay charged. GET /api/v1/assistant/usage exposes the caller's usage. Limits are configured via environment variables. Closes #109 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GfjfbSZRwVyCUYMTGSLCAA
Qodana for JVM5 new problems were found
View the detailed Qodana reportTo be able to view the detailed Qodana report, you can either:
To get - name: 'Qodana Scan'
uses: JetBrains/qodana-action@v2025.3.2
with:
upload-result: trueContact Qodana teamContact us at qodana-support@jetbrains.com
|
hugoalves-dev
pushed a commit
that referenced
this pull request
Nov 23, 2026
The PostgreSQL migration (#107) branched before the assistant quota (#110) landed, so the Postgres schema lacked assistant_request_reservations and assistant_quota_locks, and the entity still declared BINARY(16) columns. With ddl-auto=validate the app would not start on PostgreSQL, and the data migrator would refuse the cutover because the MariaDB source has the tables. - add PostgreSQL V37 with both tables and the lock row seed - map the reservation UUIDs with the native type - migrator: expect Flyway V37 and replace the seeded lock row on import Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GfjfbSZRwVyCUYMTGSLCAA
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #109
Summary
Ask ApplyWell limits are now checked in
AssistantService.stream()before anyChatModelcall. Because the check sits above Spring AI'sChatModelabstraction, it applies to OpenRouter, Gemini and any future provider the same way.APP_ASSISTANT_QUOTA_RESET_ZONE, defaultAmerica/Sao_Paulo), sliding-window rate limit (5/min), and one in-flight generation across tabs and instances.maxTokenscap (ChatOptions, merged into each provider's defaults) and a tool-call budget per message (8). Each tool call is an extra provider round-trip.READ COMMITTEDtransaction holdingSELECT ... FOR UPDATEon a single row inassistant_quota_locks. Rows inassistant_request_reservations(migration V36) are the source of truth.IN_PROGRESSslot stops blocking the user afterstream-timeout + 30s, but stays charged.code(ASSISTANT_DAILY_QUOTA_EXCEEDED,ASSISTANT_RATE_LIMITED,ASSISTANT_REQUEST_IN_PROGRESS,ASSISTANT_GLOBAL_LIMIT_REACHED),retryAfterSeconds,retryAtand theRetry-Afterheader.errorevent with codeRATE_LIMITED.GET /api/v1/assistant/usagereturns only the caller's daily usage.APP_ASSISTANT_QUOTA_*,APP_ASSISTANT_MAX_OUTPUT_TOKENSandAPP_ASSISTANT_MAX_TOOL_CALLS.0disables a single limit. Seedocs/assistant-quota.mdand.env.example.Testing
mvn test: 502 tests, 0 failures.AssistantQuotaServiceITandAssistantServiceQuotaITcover daily limit and reset, rate window, concurrency (20 parallel threads against a limit of 5), global limits, per-user isolation, refund/charge policy, cancellation and usage.Accept: text/event-stream), the usage endpoint, the token cap and the tool budget.ddl-auto=validate. The FK was dropped in that throwaway container only, because the tests use random user IDs.Deployment notes
APP_ASSISTANT_QUOTA_GLOBAL_*below the OpenRouter plan's limits. Allow for up to1 + max-tool-callsprovider requests per message.Generated by Claude Code