feat: #3615774 Rate limit on-site questions per agent, per user or visitor IP, on the shared limiter
Stacked on !33 (merged) and #3615766 (closed) MR 1 (!35 (merged)) and MR 2; review only the last commit(s) until those merge. The branch starts at the tip of 3615766-rate-limiter (2831f5bb, #3615766 (closed) MR 2), which sits on 3615766-cors-embed-token (59845e27, !35 (merged)) and !33 (merged) (4896f2c2). This MR's own change is 07fc7d0.
Closes #3615774 (closed).
As agreed on the issue: #3615766 (closed) builds the shared limiter, this adds the per-agent limit for on-site use on top of it.
What this adds
RateLimiter::checkAgentQuestion(). A question that names anagent(the blocks) counts per agent, per user for an authenticated account and per visitor IP for an anonymous one. Limit: the agent's ownrate_limit, or the site defaultai_answers.settings:rate_limits.on_site(30 per 10 min) when it's empty.- Where.
AiAnswersController::question(), in the same place as the placement check, so a 429 comes before any stream opens, also on the SSE path. AnswerService stays free of HTTP. - 429. The same response as placements:
{error, retry_after},Retry-After, translatederror. - Front end.
ai_answers.answer.jsshows the server'serroron a 429 instead of "Could not reach the answer service.". No newDrupal.t()string. - Agent form. A "Rate limit" fieldset: questions and window in seconds. Both or neither; empty means the site default, which the description names.
- Config.
rate_limiton eachai_answers.agentsrow ({count, window}ornull),rate_limits.on_siteinai_answers.settings, both with schema, and a post update that adds them to existing sites. - Docs. The agent form step and the permissions note in
getting-started.md(including proxies),architecture.md, README. The known-gaps entry from #3615766 (closed) MR 2 is removed.
Decisions to review
- A follow-up counts against its conversation's agent, not the one the body names. AnswerService already runs a follow-up on its conversation's agent, so otherwise a client could name a looser agent and keep asking a stricter one. New
AnswerService::questionAgentId()does the lookup. - A question for an agent that isn't enabled for answers isn't counted. AnswerService refuses it with a 404 or 409 and no LLM call. Same rule as placements: refused requests don't count.
- Feedback stays unlimited on-site. No LLM call, and the issue is about the question endpoint. Placement feedback keeps its own limit from #3615766 (closed).
- Default on. Existing on-site setups get 30 questions per 10 min per user or IP. 1.1.x has no release yet, and the number is meant to be generous. Behind a CDN, anonymous visitors share 1 IP unless
reverse_proxyis set, as raised on the issue; documented. - Per user, not per session, for authenticated users. A user can't reset the count by logging in again.
- The MCP tools aren't limited. They call AnswerService directly, not the endpoint, and have their own permissions. Out of scope here.
Testing
RateLimiterTest(Kernel, 2 new): anonymous per IP, users per account whatever the IP, each agent its own count, agent limit vs default,null= default, disabled and unknown agents never counted; the post update on old settings and agent rows.OnSiteRateLimitTest(Kernel, 3), over HTTP as a user with the permission: 429 +Retry-Afteron JSON and SSE, another agent and another user have their own counts, a follow-up counts against its conversation's agent, unknown and disabled agents never get a 429.AiAnswersEndpointsTest::testOnSiteRateLimit(Functional): the real flow with a session andX-CSRF-Token, 429 withRetry-Afterand the message, another user unaffected.AiAnswersAgentFormRateLimitTest(Functional): set, re-read, half-set is an error, clear back tonull.- The JS change has no automated test: the FunctionalJavascript tests can't run on my site (no WebDriver).
- Locally on 11.4 / PHP 8.4, the full module suite with
ai_answers_log: 260 tests, 251 pass. The 9 that fail are the FunctionalJavascript tests, for the same reason, so the pipeline is the first run for those. PHPCS (Drupal, DrupalPractice) has 0 errors; its 2 warnings are already on 1.1.x, in a file this MR doesn't touch. PHPStan is clean. CSpell is clean on the changed files; ESLint has no errors, and its 18 warnings onai_answers.answer.jsare all on lines this MR doesn't change.
AI disclosure
Used AI to help with research, writing and code (tracing how follow-ups pick their agent, writing the per-agent limit, form fields and tests, drafting this description). The reasoning and positions are my own, and I reviewed everything before posting. (Drupal.org AI policy)