Agent data stored when a tool uses 'Return directly'
>>> [!note] Migrated issue
<!-- Drupal.org comment -->
<!-- Migrated from issue #3529593. -->
Reported by: [akhil babu](https://www.drupal.org/user/3632866)
>>>
<h3 id="summary-problem-motivation">Problem/Motivation</h3>
<p>I have an AI agent that functions as an assistant. The assistant has history turned off. The agent uses two sub-agent tools, which are prompted to provide a JSON response. Both tools have "Return directly" enabled, as their output doesn’t need to be sent back to the main agent.</p>
<p>In the earlier release of AI Agents version 1.1, this setup worked fine. But now, when a tool returns something directly, the agent job is marked as "Not finished", and the chat history is saved in tempstore.</p>
<p>// Drupal\ai_assistant_api\Service\AgentRunner::runAsAgent</p>
<div class="codeblock">
<pre><span style="color: #000000"><span style="color: #0000BB"><?php<br> $agent</span><span style="color: #007700">-></span><span style="color: #0000BB">determineSolvability</span><span style="color: #007700">();<br> </span><span style="color: #FF8000">// If the agent is still running, we store it for the next run.<br> </span><span style="color: #007700">if (!</span><span style="color: #0000BB">$agent</span><span style="color: #007700">-></span><span style="color: #0000BB">isFinished</span><span style="color: #007700">()) {<br></span><span style="color: #FF8000">// This gets executed<br> </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">tempStore</span><span style="color: #007700">-></span><span style="color: #0000BB">get</span><span style="color: #007700">(</span><span style="color: #DD0000">'ai_assistant_threads'</span><span style="color: #007700">)-></span><span style="color: #0000BB">set</span><span style="color: #007700">(</span><span style="color: #0000BB">$job_id</span><span style="color: #007700">, </span><span style="color: #0000BB">$agent</span><span style="color: #007700">-></span><span style="color: #0000BB">toArray</span><span style="color: #007700">());<br> }<br> else {<br> </span><span style="color: #FF8000">// Cleanup when finished.<br> </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">tempStore</span><span style="color: #007700">-></span><span style="color: #0000BB">get</span><span style="color: #007700">(</span><span style="color: #DD0000">'ai_assistant_threads'</span><span style="color: #007700">)-></span><span style="color: #0000BB">delete</span><span style="color: #007700">(</span><span style="color: #0000BB">$job_id</span><span style="color: #007700">);<br> }<br></span><span style="color: #0000BB">?></span></span></pre></div>
<p>So when the main agent is called for the second time, this stored data gets added to the current request which breaks the agent flow.</p>
<p>I am not sure if this is a bug or this is how it is supposed to work. As a workaround, I am manually deleting the thread after every response</p>
<div class="codeblock">
<pre><span style="color: #000000"><span style="color: #0000BB"><?php<br> $response </span><span style="color: #007700">= </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">aiAssistantClient</span><span style="color: #007700">-></span><span style="color: #0000BB">process</span><span style="color: #007700">();<br> </span><span style="color: #FF8000">// Delete the thread from the temp store after processing.<br> </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">tempStore</span><span style="color: #007700">-></span><span style="color: #0000BB">get</span><span style="color: #007700">(</span><span style="color: #DD0000">'ai_assistant_threads'</span><span style="color: #007700">)-></span><span style="color: #0000BB">delete</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">aiAssistantClient</span><span style="color: #007700">-></span><span style="color: #0000BB">getThreadsKey</span><span style="color: #007700">());<br></span><span style="color: #0000BB">?></span></span></pre></div>
<h4 id="summary-steps-reproduce">Steps to reproduce</h4>
<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