Skip to content

feat!: Recast HITL as before tool hook - #11831

Merged
sjrl merged 12 commits into
v3from
recast-hitl-as-before-tool-hook
Jul 1, 2026
Merged

sjrl merged 12 commits into
v3from
recast-hitl-as-before-tool-hook

Conversation

@sjrl

@sjrl sjrl commented Jun 30, 2026 •

Copy link
Copy Markdown
Contributor

Related Issues

The goal of this is to reduce the need for dedicated parameters on the Agent's init method by utilizing the new Hooks feature as the preferred method for modifying the behavior of the Agent's loop.

Proposed Changes:

Added ConfirmationHook, a before_tool Agent hook that applies Human-in-the-Loop confirmation strategies to pending tool calls. It confirms, modifies, or rejects the tool calls the model requested before they run by rewriting the conversation in the Agent's State. Its confirmation_strategies mapping accepts a single tool name, a tuple of tool names, or the wildcard "*" that applies to any tool without a more specific entry, so you can set a default strategy for all tools and override individual ones.

Breaking Changes:
Human-in-the-Loop tool confirmation on Agent is now expressed as a before_tool hook instead of a dedicated concept. The confirmation_strategies and confirmation_strategy_context parameters have been
removed from Agent.__init__, Agent.run, and Agent.run_async. To migrate, wrap your confirmation
strategies in the new ConfirmationHook, register it under the before_tool hook point, and pass any request-scoped resources through the generic hook_context run argument instead of confirmation_strategy_context:

from haystack.components.agents import Agent
from haystack.human_in_the_loop import (
    AlwaysAskPolicy,
    BlockingConfirmationStrategy,
    ConfirmationHook,
    RichConsoleUI,
)

confirmation_hook = ConfirmationHook(
    confirmation_strategies={
        "my_tool": BlockingConfirmationStrategy(
            confirmation_policy=AlwaysAskPolicy(), confirmation_ui=RichConsoleUI()
        )
    }
)
agent = Agent(chat_generator=..., tools=[...], hooks={"before_tool": [confirmation_hook]})
agent.run(messages=[...], hook_context={"websocket": ws})

How did you test it?

Added new tests.

Notes for the reviewer

I did think about seeing if there was a way to reduce abstractions and verbosity such as replacing ConfirmationStrategy with a ConfirmationHook protocol, but I ruled this out since it did come with cons.

  1. A BlockingConfirmationHook would only be able to hold one policy+UI so if you wanted to set "always ask" for delete_file and "sometimes ask" for the remaining tools then you would need to set multiple hooks which increases verbosity. The current {tool: strategy} map (with tuple keys and "*" wildcard) handles per-tool differentiation, including mixing a blocking strategy for one tool and a custom non-blocking one for another, in a single hook.
  2. It downgrades the extension contract. Today a custom integration (e.g. the redis/openwebui non-blocking strategy) implements a small surface — run(tool_name, description, params, ...) -> ToolExecutionDecision — and Haystack handles the fiddly message surgery (rejection/modify message rewriting + history reconstruction). Making the hook the new protocol would force those authors to implement the full run(state) -> None making them redo that surgery themselves.

Checklist

  • I have read the contributors guidelines and the code of conduct.
  • I have updated the related issue with new insights and changes.
  • I have added unit tests and updated the docstrings.
  • I've used one of the conventional commit types for my PR title: fix:, feat:, build:, chore:, ci:, docs:, style:, refactor:, perf:, test: and added ! in case the PR includes breaking changes.
  • I have documented my code.
  • I have added a release note file, following the contributors guidelines.
  • I have run pre-commit hooks and fixed any issue.

@vercel

vercel Bot commented Jun 30, 2026 •

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
haystack-docs Ignored Ignored Preview Jul 1, 2026 11:21am

Request Review

@github-actions github-actions Bot added topic:tests type:documentation Improvements on the docs labels Jun 30, 2026
@github-actions

github-actions Bot commented Jun 30, 2026 •

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  haystack/components/agents
  agent.py 1237
  haystack/human_in_the_loop
  __init__.py
  hooks.py 110
  strategies.py 265, 314, 620
Project Total  

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

Base automatically changed from fix-hitl-tool-arg-prep to v3 June 30, 2026 13:34
@sjrl sjrl changed the title refactor!: Recast HITL as before tool hook feat!: Recast HITL as before tool hook Jul 1, 2026
@sjrl
sjrl marked this pull request as ready for review July 1, 2026 07:45
@sjrl
sjrl requested review from a team as code owners July 1, 2026 07:45
@sjrl
sjrl requested review from bogdankostic and removed request for a team July 1, 2026 07:45

@bogdankostic bogdankostic 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.

Looks already good, just some minor comments regarding validation on whether ConfirmationHook is used with the before_tool hook point and deepcopying hook contexts.

Comment thread haystack/human_in_the_loop/hooks.py Resolved
Comment thread haystack/human_in_the_loop/hooks.py Resolved
Comment thread haystack/human_in_the_loop/hooks.py Resolved
@sjrl
sjrl requested a review from bogdankostic July 1, 2026 11:22

@bogdankostic bogdankostic 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.

🚀

@sjrl
sjrl merged commit a8522a4 into v3 Jul 1, 2026
27 checks passed
@sjrl
sjrl deleted the recast-hitl-as-before-tool-hook branch July 1, 2026 12:28
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.

2 participants