[Feature] Introduce Human-in-the-Loop (HITL) approval mechanism for high-risk tools and support middleware interception
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:
- Extend Middleware Interface: Allow
beforeToolUseto return an instruction object like{ __skip: true, result: unknown }. This enables middleware to safely bypass the actualtool.invokeand feed a custom result (e.g., "User denied execution") back to the LLM. - Approval Manager (Event Bus): Implement a pure JS queue-based
ApprovalManagerto handle concurrent tool requests from the LLM, avoiding race conditions and preventing React state anti-patterns (like storing functions inuseState). - TUI Integration: Add an
<ApprovalPrompt />in the Ink UI that interceptsstdinfocus 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.allcrash 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!