runAsAgent: Stable IDs Across Conversation Turns
>>> [!note] Migrated issue
<!-- Drupal.org comment -->
<!-- Migrated from issue #3582030. -->
Reported by: [ahmad khader](https://www.drupal.org/user/3727855)
Related to !1370
>>>
<p>[Tracker]<br>
<strong>Update Summary: </strong>[One-line status update for stakeholders]<br>
<strong>Short Description: </strong>[One-line issue summary for stakeholders]<br>
<strong>Check-in Date: </strong>MM/DD/YYYY<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 id="summary-problem-motivation">Problem/Motivation</h3>
<p><code>AgentRunner::runAsAgent()</code> creates a new agent instance on every request but never sets its runner ID. Since <code>AiAgentBase::getRunnerId()</code> lazily generates a random ID (<code>{pluginId}_{microtime}</code>) when none is set, the agent gets a <strong>different identity on every turn</strong> of the same conversation. Any event subscriber or service that uses <code>getRunnerId()</code> for cross-turn identification — scoping, persistence, status tracking — gets an inconsistent key and cannot correlate data across turns.</p>
<p>Also doesn't call <code>$agent->setProgressThreadId($job_id)</code>, so<code> AgentFinishedExecutionEvent::getThreadId() returns null </code>for any subscriber that needs to correlate the event back to the original conversation thread (e.g. persisting messages to a custom entity).</p>
<h3 id="summary-proposed-resolution">Proposed resolution</h3>
<p>Set the runner ID and Thread ID (to the conversation's <code>$job_id</code> (thread key) before the agent executes:<br>
<code>$agent->setRunnerId($job_id);</code><br>
<code>$agent->setProgressThreadId($job_id)</code></p>
<p>This gives the agent a <strong>stable identity</strong> that matches across all turns of the same conversation. </p>
<h3 id="summary-remaining-tasks">Remaining tasks</h3>
<h3>Optional: Other details as applicable (e.g., User interface changes, API changes, Data model changes)</h3>
<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>[ ] 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 #3576571](https://www.drupal.org/node/3576571)
issue