feat(adk): add standard tool policy and human approval middleware - #1260
Open
Dragonzz27 wants to merge 1 commit into
Open
Dragonzz27 wants to merge 1 commit into
Dragonzz27 wants to merge 1 commit into
Conversation
Add adk/middlewares/toolpolicy implementing the thin-hook direction discussed in cloudwego#996: a reusable ChatModelAgentMiddleware that gates every tool call through a small Policy interface and rides on ADK's existing interrupt/resume machinery instead of introducing a second checkpoint or approval-storage mechanism. Per tool call, the policy can: - allow execution, optionally with rewritten/sanitized arguments; - deny execution, returning a safe tool result to the model; - require human approval, interrupting the run with user-facing ApprovalInfo until the run is resumed with an *Approval decision. The middleware wraps all four tool-call kinds (invokable, streamable, enhanced invokable, enhanced streamable), works for both Message and AgenticMessage agents, keeps approval state in the checkpoint so unapproved sibling calls re-interrupt with the same info, and fails closed on policy errors. Decision auditing can be layered on via the existing callback handlers.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Implements #996
Motivation
Eino has powerful primitives (tool middleware, interrupt/resume), but application developers still hand-roll a policy layer for deciding whether a tool call should be allowed, denied, rewritten, audited, or interrupted for human approval. This PR adds the standard, reusable abstraction proposed in #996, following the thin-hook direction that converged in the issue discussion: the middleware owns only the invocation seam and lifecycle integration; policy storage, approval storage, and audit persistence stay deployment-specific (audit can plug into existing callback handlers).
Design
New
adk/middlewares/toolpolicypackage:Policyinterface —Decide(ctx, *Request) (*Result, error);Requestcarries tool name, call ID, and raw JSON arguments. APolicyFuncadapter is provided.allow: execute, optionally with rewritten/sanitized arguments;deny: the model receives a safe tool result (default or custom message) without executing the tool;require_approval: the run interrupts with the policy'sApprovalInfoand waits.*toolpolicy.Approvalas resume data (Approved, optional denialMessage, optionalRewrittenArguments). Unapproved sibling calls re-interrupt with the same user-facing info.MessageandAgenticMessageagents.Policyis rejected at construction.Example
Tests
18 tests, all green with
-race:*schema.AgenticMessageagents.