Skip to content

fix: keep earlier messages when ConfirmationHook rewrites the chat history - #12736

Merged
sjrl merged 3 commits into
deepset-ai:mainfrom
alanhuangyoo:fix/confirmation-hook-keeps-earlier-messages
Sep 24, 2026
Merged

sjrl merged 3 commits into
deepset-ai:mainfrom
alanhuangyoo:fix/confirmation-hook-keeps-earlier-messages

Conversation

@alanhuangyoo

@alanhuangyoo alanhuangyoo commented Sep 13, 2026 •

Copy link
Copy Markdown
Contributor

Related Issues

  • No issue. Found while reading the HITL hook.

Proposed Changes:

ConfirmationHook drops every message that comes after the last user or tool message. _update_chat_history rebuilds the history from that index, which assumed the pending tool-call message came right after it. Since HITL became a before_tool hook (#11831), other hooks can add messages in between, and those messages vanished from what the Agent sends to the model. This happens even with NeverAskPolicy, where every call is confirmed unchanged.

Two cases where this bites, both with a mock chat generator:

  • A before_run hook appends a system message with user memory. The second LLM call gets [user, assistant(tool_calls), tool]; the system message is gone.
  • The on_exit + continue_run pattern from the @hook docstring. After the reminder, the model calls a tool; the next LLM call no longer sees the draft answer or the reminder.

The hook only acts when the last message has tool calls, and that is the only message it passes in, so the fix replaces just that message and keeps everything before it.

How did you test it?

  • New tests in test/hooks/human_in_the_loop/test_hooks.py: confirm, modify and reject keep the messages before the tool-call message, plus an async case. All four fail without the fix.
  • test/components/agents and test/hooks: 668 passed (the RichConsoleUI tests were deselected because rich isn't installed here).
  • ruff check and ruff format --check pass. I didn't run mypy locally.

Notes for the reviewer

The existing _update_chat_history tests only use [user, assistant(tool_calls)] histories, so they pass either way.

Checklist

This PR was fully generated with an AI assistant, including the fix, the tests and this description. The checks above are the ones that were actually run.

…story

The history was rebuilt from the last user or tool message, which assumed the
pending tool-call message came right after it. Since HITL became a before_tool
hook, messages added by other hooks sit in between, and they were dropped from
what the Agent sends to the model. Only the last message holds the pending
tool calls, so replace just that one.
@alanhuangyoo
alanhuangyoo requested a review from a team as a code owner September 13, 2026 19:57
@alanhuangyoo
alanhuangyoo requested review from davidsbatista and removed request for a team September 13, 2026 19:57
@vercel

vercel Bot commented Sep 13, 2026

Copy link
Copy Markdown
Contributor

@alanhuangyoo is attempting to deploy a commit to the deepset Team on Vercel.

A member of the Team first needs to authorize it.

@CLAassistant

CLAassistant commented Sep 13, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@HaystackBot

Copy link
Copy Markdown
Contributor

Hi @alanhuangyoo, thanks a lot for your contribution! 🙏

We noticed that the Contributor License Agreement (CLA) check (license/cla) hasn't passed yet, so we've temporarily moved this PR to draft and paused the review assignment.

To get your PR reviewed, please sign the CLA via the link in the license/cla check below (or in the CLA bot comment). As soon as the check turns green, this PR will automatically be marked ready for review again and a reviewer will be re-assigned.

@HaystackBot
HaystackBot removed the request for review from davidsbatista September 13, 2026 21:24
@HaystackBot HaystackBot added the cla-pending PR is in draft until the contributor signs the CLA label Sep 13, 2026
@HaystackBot
HaystackBot marked this pull request as draft September 13, 2026 21:24
@HaystackBot
HaystackBot marked this pull request as ready for review September 13, 2026 21:51
@HaystackBot

Copy link
Copy Markdown
Contributor

Thanks for signing the CLA, @alanhuangyoo! 🎉 This PR is now ready for review again and the reviewer has been re-assigned.

@sjrl

sjrl commented Sep 14, 2026

Copy link
Copy Markdown
Contributor

@davidsbatista I'll review this one

@sjrl sjrl self-assigned this Sep 14, 2026
@github-actions github-actions Bot added the type:documentation Improvements on the docs label Sep 14, 2026
@github-actions

github-actions Bot commented Sep 14, 2026 •

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  haystack/hooks/human_in_the_loop
  strategies.py
Project Total  

This report was generated by python-coverage-comment-action

@alanhuangyoo
alanhuangyoo force-pushed the fix/confirmation-hook-keeps-earlier-messages branch from 9029fb7 to 6438949 Compare September 21, 2026 17:21

@sjrl sjrl left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@sjrl
sjrl merged commit 7d4054d into deepset-ai:main Sep 24, 2026
22 of 23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic:tests type:documentation Improvements on the docs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants