Fix: instanceDescription crash in applyToolUsageLimitsToContext function
>>> [!note] Migrated issue
<!-- Drupal.org comment -->
<!-- Migrated from issue #3579630. -->
Reported by: [ahmad khader](https://www.drupal.org/user/3727855)
Related to !246
>>>
<h3 id="summary-problem-motivation">Problem/Motivation</h3>
<h4>8. Fix: <code>instanceDescription</code> crash in <code>applyToolUsageLimitsToContext()</code></h4>
<p><code>ai_agents/src/PluginBase/AiAgentEntityWrapper.php</code> (line ~1115)</p>
<ul>
<li><strong>Problem:</strong> The <code>tool_usage_limits</code> config array can contain non-context keys like <code>instanceDescription</code> injected by the FlowDrop UI. <code>applyToolUsageLimitsToContext()</code> iterated all keys and called <code>$function_call->getContextDefinition($property_name)</code> for each, which threw an exception for unknown property names.</li>
<li><strong>Fix:</strong> Added a guard that fetches <code>$context_definitions = $function_call->getContextDefinitions()</code> once, then skips any <code>$property_name</code> not present in the definitions via <code>if (!array_key_exists($property_name, $context_definitions)) { continue; }</code>.</li>
<li><strong>Error message:</strong> <em>"The instanceDescription context is not a valid context."</em></li>
</ul>
<h4 id="summary-steps-reproduce">Steps to reproduce</h4>
<ol>
<li>Save the agent tool through a model like FlowDrop</li>
<li>Call the agent via the AI agent debugger</li>
</ol>
<h3 id="summary-proposed-resolution">Proposed resolution</h3>
<h3 id="summary-remaining-tasks">Remaining tasks</h3>
<h3 id="summary-ui-changes">User interface changes</h3>
<h3 id="summary-api-changes">API changes</h3>
<h3 id="summary-data-model-changes">Data model changes</h3>
issue