Guardrail plugins only evaluate the last message
>>> [!note] Migrated issue <!-- Drupal.org comment --> <!-- Migrated from issue #3579088. --> Reported by: [abhisekmazumdar](https://www.drupal.org/user/3557964) Related to !1467 >>> <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> <h2>Problem/Motivation</h2> <p>Both <code>RegexpGuardrail</code> and <code>RestrictToTopic</code> only inspect the last message in a conversation. The full chat history is passed in via <code>ChatInput</code>, but both plugins call <code>end($messages)</code> and evaluate that single message only.</p> <p>In a multi-turn chat session, earlier messages are silently skipped. This means guardrail rules are not applied consistently across the full conversation, only to whatever the user most recently sent.</p> <p>Example:</p> <ul> <li>Turn 1 (user): Asks something that would normally be caught by a guardrail.</li> <li>Turn 2 (user): Sends a follow-up that is benign on its own.</li> </ul> <p>The guardrail evaluates Turn 2 only and passes, even though the conversation as a whole may violate the configured rules.</p> <h2>Proposed resolution</h2> <p>Extend the guardrail plugin system to support evaluating all user messages in the conversation, not only the last one. Possible approaches:</p> <ul> <li>A new plugin variant that scans each <code>ChatMessage</code> where <code>getRole() === 'user'</code> individually before passing.</li> <li>A configuration option on the existing plugins (e.g. "Scan all user messages") so site builders can opt in to broader coverage without changing the default behaviour.</li> </ul> <h2>Remaining tasks</h2> <ul> <li>Agree on approach: new plugin vs. configuration option on existing plugins.</li> <li>Implement and write tests covering multi-turn conversations.</li> </ul> <h3 id="summary-ai-usage">AI usage (if applicable)</h3> <p>[ x ] 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>
issue