helixent icon indicating copy to clipboard operation
helixent copied to clipboard

[Feature] Introduce Human-in-the-Loop (HITL) approval mechanism for high-risk tools and support middleware interception

Open ag9920 opened this issue 7 months ago • 0 comments

1. The Problem

Currently, the lead-agent executes tools like bash, write_file, and str_replace silently. If the LLM hallucinates or produces a destructive command (e.g., rm -rf), it will be executed without any human intervention. This makes the agent unsafe for use in real-world or production repositories.

Furthermore, the current AgentMiddleware design lacks a graceful way to intercept and skip a tool execution. Throwing an error inside beforeToolUse will cause the Promise.all in _act to reject entirely, crashing the agent loop instead of gracefully returning a mocked "denied" result to the LLM.

2. Proposed Solution

I propose implementing a robust Human-in-the-Loop (HITL) mechanism:

  1. Extend Middleware Interface: Allow beforeToolUse to return an instruction object like { __skip: true, result: unknown }. This enables middleware to safely bypass the actual tool.invoke and feed a custom result (e.g., "User denied execution") back to the LLM.
  2. Approval Manager (Event Bus): Implement a pure JS queue-based ApprovalManager to handle concurrent tool requests from the LLM, avoiding race conditions and preventing React state anti-patterns (like storing functions in useState).
  3. TUI Integration: Add an <ApprovalPrompt /> in the Ink UI that intercepts stdin focus and displays truncated tool arguments for user review ([Y/n]).

3. Benefits

  • Safety: Users have the final say before any destructive operation occurs.
  • Stability: Resolves the Promise.all crash issue by providing a first-class middleware skip mechanism.
  • Robustness: Queue-based approval handles LLM parallel tool calls correctly without deadlocks or UI state races.

I'm willing to submit a PR for this. Let me know if the architectural direction sounds good!

ag9920 avatar Apr 08 '26 18:04 ag9920