Issue #3586376: Add context item priority field and selection tie-breakers.
Description
Adds a Priority field (High / Medium / Low) on context items and uses it for deterministic selection ordering instead of the changed timestamp.
What changed
-
AiContextItemPriority— constants for High (10), Medium (5), Low (0); Medium is the default. -
prioritybase field onai_context_item, withgetPriority()on the entity. -
Context item form — Priority control in Context Scope (above Global); hidden on the default form display config.
-
ai_context_update_10015()— installs field storage and removespriorityfrom the default form display on existing sites. -
AiContextSelector— sharedcompareSelectionPreference()ordering:- Priority descending
- Shorter content length
- Higher item ID (final tie-break)
Applied within each group:
- Global — bounded top-K via
insertTopPreferenceCandidate() - Always-include — sorted by priority; lower-priority items drop first when capped
- Auto-included — same bounded preference helper (replaces chunk scan order)
- Scored — priority breaks score ties
- Match-all — sorted by priority
Between-group order is unchanged: global → always-include → auto-included → scored.
-
Docs —
context_items.md,context_selection.md, andscopes.mdupdated for Priority and tie-breaking behavior. -
Tests — kernel selector tests updated/added; functional form test asserts the Priority field on add.
Machine name is priority (not ranking) to avoid confusion with subscription scope scoring.
Closes #3586376
Testing instructions
Automated
cd web/modules/contrib/ai_context
./lint.sh
ddev exec vendor/bin/phpunit web/modules/contrib/ai_context/tests/src/Kernel/AiContextSelectorAlgorithmTest.php
ddev exec vendor/bin/phpunit --filter testContextItemAddFormShowsPriorityField web/modules/contrib/ai_context/tests/src/Functional/AiContextScopeContextItemFormTest.phpManual
- Run
ddev exec drush updatedb -y && ddev exec drush cron an existing site (applies update10015). - Add or edit a context item at
/admin/config/ai/context/items/add— confirm Priority appears in Context Scope above Global. - Create several global items with different priorities and run selection with a low
max_global_items— higher-priority items should appear first; lowest should be dropped. - Add several items to an agent Always include list (mixed request order) with different priorities and a low
max_items— confirm highest-priority items are kept and ordered first. - With Dynamic Entity Reference enabled, create target-entity auto-included items with different priorities on the same entity context — confirm higher priority is ordered first and survives caps.
Post-merge steps
- Run database updates on existing sites:
drush updatedb - Cache rebuild:
drush cr
No config import required beyond what updatedb applies.
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.