Skip to content

Cherry pick #114885 to 26.7: AI functions: Make quotas per query not per block - #115864

Merged
robot-ch-test-poll4 merged 13 commits into
backport/26.7/114885from
cherrypick/26.7/114885
Aug 24, 2026
Merged

robot-ch-test-poll4 merged 13 commits into
backport/26.7/114885from
cherrypick/26.7/114885

Conversation

@robot-ch-test-poll

Copy link
Copy Markdown
Contributor

Original pull-request #114885

Do not merge this PR manually

This pull-request is a first step of an automated backporting.
It contains changes similar to calling git cherry-pick locally.
If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.

The check results does not matter at this step - you can safely ignore them.

Troubleshooting

If the conflicts were resolved in a wrong way

If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

george-larionov and others added 12 commits August 13, 2026 22:48
`AIQuotaTracker` was a stack local in `executeImpl`, so every block and
every pipeline stream got a fresh allowance and the `ai_function_max_*_per_query`
limits scaled with the data instead of bounding the query. Own one tracker on
the query `Context` (shared by every AI function call in the query) and make its
counters atomic for concurrent pipeline threads.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Move the AI call-count tests into the mock suite and add multi-part
(multi-block) tests that a per-block tracker cannot pass:
`test_api_call_quota_is_per_query`, `test_api_call_quota_throws_per_query`,
`test_input_token_quota_is_per_query`. Merges are stopped so the parts
survive to the scan.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Co-Authored-By: Claude Opus 4.8 <[email protected]>
Reserve each request slot with an atomic bounded increment
(`tryReserveApiCall`) so concurrent pipeline threads cannot overshoot
`ai_function_max_api_calls_per_query`. Token limits stay best-effort:
usage is known only after the response. Adds a multi-threaded test and
updates the setting docs.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
- Fix the garbled `ai_function_max_api_calls_per_query` doc string.
- Print the observed call count (not the max twice) in the limit-reached
  exception.
- Make `test_api_call_quota_holds_under_concurrency` prove concurrency:
  assert `peak_threads_usage` exceeds 1 (from `system.query_log`) over a
  table with enough marks to split the scan across threads. The mock
  becomes a threaded server with a larger backlog so it serves the
  concurrent AI calls.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
…uery

The tracker lives on the per-query-execution `Context`, and distributed
execution creates one context per shard/fragment (`makeQueryContext`), so
each enforces the limit independently. Narrow the setting docs and the
`AIQuotaTracker` comment to say so, instead of claiming a global cap.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Drop the `AIQuotaTracker & quota = *quota_tracker` reference bindings and
call through the `shared_ptr` instead. No behavior change.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Per review: "reserve an API call" reads oddly; the method counts a call
against the request quota. Behavior unchanged.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Per review: the counters aren't performance-critical (each is touched
once per provider request), so a plain mutex with TSA annotations is
simpler than atomics and drops the memory-order noise. Behavior
unchanged.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
The quota settings are read from the query context, so a sub-query
SETTINGS override of them is ignored. Note this in the four settings'
docs and add a regression test asserting a sub-query cap does not change
the query budget (the top-level value governs).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
recordApiCall now re-checks the token/sticky quotas under the same lock as
the API-call reservation (shared helper quotasExceededLocked), so a call is
never started after another thread's response pushed the token budget over -
closing the check-then-reserve gap for the token limits. The now-redundant
per-attempt checkQuotas before recordApiCall is removed; the top-of-row and
top-of-batch checks stay.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
AI functions: Make quotas per query not per block
@robot-ch-test-poll robot-ch-test-poll added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default labels Aug 21, 2026
…ypick/26.7/114885

# Conflicts:
#	src/Core/Settings.cpp
#	src/Functions/FunctionBaseAI.cpp
#	src/Functions/aiEmbed.cpp
#	src/Functions/aiSimilarity.cpp
#	tests/integration/test_ai_functions/test.py
@robot-ch-test-poll4
robot-ch-test-poll4 merged commit 9a32e98 into backport/26.7/114885 Aug 24, 2026
1 check passed
@robot-ch-test-poll4
robot-ch-test-poll4 deleted the cherrypick/26.7/114885 branch August 24, 2026 21:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants