Issue #3586378: Remove max_items context limit and rely on token budget.
Description
Removes the Max context items to inject (max_items) setting and all selection logic that depended on it. The token budget (max_tokens) is now the primary user-facing limit on how much context reaches an agent prompt. max_global_items is unchanged and still caps the Global group only.
The selector also applies an internal, non-configurable bound of 100 candidates per selection group (scored, match-all, and auto-included) as a performance safeguard on very large catalogs. It keeps the highest-scored (or most recently changed, for match all) candidates. Realistic token budgets are exhausted well before this bound is reached.
Code changes
- Remove
max_itemsfromai_context.settingsschema/defaults, General settings, and per-agent Advanced overrides. - Remove
maxItemsfromAiContextRequest,AiContextRequestParamsData, andAiContextRequestFactory. - Update
AiContextSelectorso merge/scoring/auto-include paths no longer slice by configurable item count; token truncation at render time remains the user-facing ceiling. - Add
MAX_CANDIDATES_PER_GROUP(100) to bound scored, match-all, and auto-included candidate accumulation during chunked catalog scans. - Remove
AiContextLimitsConstraint(global cap vs total item cap). - Remove
max_itemsfrom theGetRelevantAiContextItemsfunction-call plugin. - Remove
getMaxItems()fromAiContextSelectionItemsSelectedEvent. - Add
ai_context_update_10020()to strip obsoletemax_itemsfrom active config on existing sites. - Strip legacy
max_itemsduring config import viaSTORAGE_TRANSFORM_IMPORT(not validation-only) so older exports import cleanly.
Documentation
- Update limit and selection docs (
context_selection.md,agent_configuration.md,scopes.md, developer docs). - Document the internal candidate bound in
context_selection.md.
Breaking API changes (documented)
max_itemsremoved from settings and agent configuration.AiContextRequestno longer acceptsmaxItems.AiContextSelectionItemsSelectedEvent::getMaxItems()removed.GetRelevantAiContextItemsno longer accepts amax_itemsparameter.AiContextLimitsConstraintand related validation removed.
Closes #3586378 (closed)
Testing instructions
- Run
drush updatedband confirm update10020removesmax_itemsfromai_context.settingsand any agent overrides. - Open Configuration → AI → Context Control Center → General settings (
/admin/config/ai/context/settings/general) and confirm Max context items to inject is no longer shown. Save the form with no errors. - Open a per-agent edit form under Agent configuration and confirm the Max context items to inject override is gone; Max global context items and Max tokens still work.
- Configure an agent with subscriptions that would previously exceed
max_itemsbut stay withinmax_tokens. Confirm additional matching context is selected and rendered until the token budget applies. - Export config from a site that previously had
max_items, re-import on a checkout of this branch, and confirm import succeeds (legacy key stripped from active config). - From the module directory, run
./lint.sh. - Run targeted PHPUnit (example):
ddev exec bash -c 'SIMPLETEST_DB=sqlite://localhost/tmp.sqlite \
/var/www/html/vendor/bin/phpunit --configuration /var/www/html/phpunit.xml \
web/modules/contrib/ai_context/tests/src/Unit/AiContextLimitResolverTest.php \
web/modules/contrib/ai_context/tests/src/Kernel/AiContextSettingsConfigValidationTest.php \
web/modules/contrib/ai_context/tests/src/Kernel/AiContextAgentConfigValidationTest.php \
web/modules/contrib/ai_context/tests/src/Kernel/AiContextRequestFactoryTest.php \
web/modules/contrib/ai_context/tests/src/Kernel/AiContextSelectorAlgorithmTest.php \
web/modules/contrib/ai_context/tests/src/Kernel/AiContextSelectorEventsTest.php \
web/modules/contrib/ai_context/tests/src/Kernel/AiContextSettingsConfigImportValidationTest.php \
web/modules/contrib/ai_context/tests/src/Kernel/AiContextAgentsConfigImportValidationTest.php \
web/modules/contrib/ai_context/tests/src/Kernel/AiContextUpdate10020Test.php'Post-merge
- Existing sites: run
drush updatedbonce after deploy (update10020). - No manual config edits required beyond the update hook.
Checklist
- I have updated the MR title to use format:
Issue #1234: My issue title. - I have updated the MR description to include:
Closes #1234 - I have performed a self-review of my own code
- I have added or updated tests, or explained in the description why this change is not covered by tests
- I have updated documentation for any new or changed functionality
- I have written testing instructions and verified them locally
- I have noted any required post-merge steps (config imports, cache rebuilds, manual changes)
- This MR contains no breaking API or hook changes, or they are explicitly documented in the description
AI Compliance
Note
Check the one that best describes your usage, or leave all unchecked if AI was not significantly used.
- AI Assisted Code — Mainly written by a human; AI used for autocomplete or partial generation under full human supervision.
- AI Generated Code — Mainly generated by AI, reviewed and approved by a human before this MR was created.
- Vibe Coded — Generated by AI and only functionally reviewed before this MR was created.