feat(hooks): populate agent_name on tool-use hook events#3110
Open
t-mizumoto1203 wants to merge 1 commit into
Open
feat(hooks): populate agent_name on tool-use hook events#3110t-mizumoto1203 wants to merge 1 commit into
t-mizumoto1203 wants to merge 1 commit into
Conversation
Tool events (pre/post_tool_use) build their hooks.Input via toolexec.NewHooksInput, which has no agent reference, so they shipped an empty agent_name while every other event sets it explicitly. Auto-fill AgentName in the shared dispatchHook path (which already receives the agent), mirroring how Executor.Dispatch auto-fills Cwd. Caller-set values still win, and the field is omitempty, so the change is backward compatible. This lets pre_tool_use hooks and external policy engines see which agent issued a tool call (per-agent governance / auditing). Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Signed-off-by: kiriha1203 <mizumoto.takefumi@gmail.com>
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.
What
Tool-use hook events (
pre_tool_use/post_tool_use) now carryagent_namein theirhooks.Input, matching every other event type.Fixes #3109.
Why
hooks.Input.AgentNameexists and is documented as "identifies the agentdispatching the event", and session / turn /
before|after_llm_call/on_agent_switchevents all set it. But tool events build their Input viatoolexec.NewHooksInput, which receives no agent, soagent_nameshippedempty — even though the shared
dispatchHookpath already has the agent.This blocks any
pre_tool_useconsumer (auditing, per-agent rate limits,external policy engines) from knowing which agent issued a tool call.
How
Auto-fill
AgentNameindispatchHook(the shared path that alreadyreceives the agent), only when it is empty so explicit caller values win,
mirroring how
Executor.Dispatchauto-fillsCwd. The field isomitempty,so this is backward compatible, and it covers both
pre_tool_useandpost_tool_use(both flow through the same path).Semantics:
agent_name= the agent that directly issues the tool call(
a.Name()); on transfer/handoff it follows the active sub-agent.Testing
go test ./pkg/runtime/...— addedTestPreToolUseHook_ReceivesAgentName;all pre_tool_use hook tests pass.
go build ./...,go vet ./pkg/runtime/...,gofmt— clean.pre_tool_usehook dumping its stdin:{"agent_name":"root","tool_name":"think"}transfer_task):agent_namefollows the active agent across delegation.