Refactor context selection logic
>>> [!note] Migrated issue
<!-- Drupal.org comment -->
<!-- Migrated from issue #3574359. -->
Reported by: [kristen pol](https://www.drupal.org/user/8389)
Related to !76
>>>
<p>[Tracker]<br>
<strong>Update Summary: </strong>[One-line status update for stakeholders]<br>
<strong>Check-in Date: </strong>MM/DD/YYYY<br>
<strong>Blocked by: </strong>[#XXXXXX] (New issues on new lines)<br>
<strong>Additional Collaborators: </strong> @username1, @username2<br>
<em>Metadata is used by the <a href="https://www.drupalstarforge.ai/" title="AI Tracker">AI Tracker.</a> Docs and additional fields <a href="https://www.drupalstarforge.ai/ai-dashboard/docs" title="AI Issue Tracker Documentation">here</a>.</em><br>
[/Tracker]</p>
<h3>Testing Steps </h3>
<p><a href="https://docs.google.com/document/d/1NXEffwQxgkIaDVT6kJ8LXN-mYNn7pmRHm55HnKaHfts/edit?tab=t.0">https://docs.google.com/document/d/1NXEffwQxgkIaDVT6kJ8LXN-mYNn7pmRHm55HnKaHfts/edit?tab=t.0</a></p>
<h3 id="summary-problem-motivation">Problem/Motivation</h3>
<p>This will be split up into multiple issues.</p>
<p>The existing context selection logic has been updated many times as different features were added or refactored, and it's gotten confusing, suboptimal, and probably buggy.</p>
<p>This issue is to rebuild this logic from scratch.</p>
<h3 id="summary-proposed-resolution">Proposed resolution</h3>
<p><strong>MUST</strong> 0. Decouple context selection from agents</p>
<p>We should try to make the logic/interfaces not tied to agents specifically so that we can use the same logic for non-agent context selection:</p>
<p><span class="drupalorg-gitlab-issue-link project-issue-status-info project-issue-status-7"><a href="https://www.drupal.org/project/ai_context/issues/3556909" title="Status: Closed (fixed)">#3556909: [Discuss] Decouple AI Context from AI Agents</a></span></p>
<p><strong>MUST except smart selection </strong>1. Things to keep in mind for agent context selection:</p>
<ul>
<li>By default, the agent does not have any context included/injected to avoid unwanted behavior</li>
<li>Context items can be included based on agent context subscriptions (expected default behavior) [may require AI]</li>
<li>Context items can be set to always be included (override) [does not require AI]</li>
<li>Context items can be set to always be excluded (override) [does not require AI]</li>
<li>Context items can be included based on smart selection (override) [requires AI] (split out and do in future issue)</li>
</ul>
<p><strong>MUST (non AI)</strong> 2. For agent context subscriptions:</p>
<ul>
<li>(now without AI) The simple way to select the agent context without AI is just by comparing the scope values</li>
<li>(later with AI) The dynamic way is to use context purpose, agent purpose, scope values, agent subscriptions, etc</li>
</ul>
<p><strong>FUTURE</strong> 3. There will also be "boundaries" of things that cause the context not to be included:</p>
<ul>
<li>Do not use context on these specific paths</li>
<li>Do not use context on these specific pages/content</li>
<li>Do not use context on these specific entity bundles (e.g., blog posts)</li>
</ul>
<p>Note that the boundaries feature needs follow-up issues because:</p>
<ul>
<li>There is currently no way to specify these boundaries in the context scope</li>
<li>Entity bundles are not currently in the context scope features</li>
</ul>
<p>But we should keep these in mind when doing this refactor.</p>
<p><strong>MUST except cron and smart selection</strong> 4. We need a caching strategy for performance, which may or may not be part of this issue.</p>
<p>Things to keep in mind when caching agent context selections:</p>
<ul>
<li>Update when context is added, updated, or removed (via cron as it has to go through all agents)</li>
<li>Update when agent context subscription settings are changed (this should be real-time ideally, but there are scaling considerations)</li>
<li>There will be real-time settings for context selection (e.g., smart selection) that should only be used for a specific agent prompt</li>
<li>We want an easy way to see these cached/saved agent context selections in the UI</li>
</ul>
<h3 id="summary-remaining-tasks">Target date or deadline</h3>
<p>March 2026</p>
<h3 id="summary-remaining-tasks">Remaining tasks</h3>
<ul>
<li>Plan</li>
<li>Update code</li>
<li>Review and test (UX and functionality)</li>
<li>Create follow-up issues as needed</li>
</ul>
<h3 id="summary-ai-usage">AI usage (if applicable)</h3>
<p>[ ] AI Assisted Issue<br>
This issue was generated with AI assistance, but was reviewed and refined by the creator.</p>
<p>[ ] AI Assisted Code<br>
This code was mainly generated by a human, with AI autocompleting or parts AI generated, but under full human supervision.</p>
<p>[x] AI Generated Code<br>
This code was mainly generated by an AI with human guidance, and reviewed, tested, and refined by a human.</p>
<p>[ ] Vibe Coded<br>
This code was generated by an AI and has only been functionally tested.</p>
> Related issue: [Issue #3574910](https://www.drupal.org/node/3574910)
issue