Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pkg/runtime/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ func (r *LocalRuntime) dispatchHook(
return nil
}

// Auto-fill AgentName for events whose Input builder omits it (tool
// events via toolexec.NewHooksInput), mirroring Cwd auto-fill in
// Executor.Dispatch. Caller-set values win.
if input != nil && input.AgentName == "" {
input.AgentName = a.Name()
}

started := time.Now()
if events != nil {
events.Emit(HookStarted(event, input.SessionID, a.Name()))
Expand Down
38 changes: 38 additions & 0 deletions pkg/runtime/pre_tool_use_approval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,41 @@ func TestPreToolUseHook_PermissionsAllowShortCircuitsHook(t *testing.T) {
assert.True(t, *executed,
"deterministic Allow must short-circuit the hook (cost / latency win)")
}

// TestPreToolUseHook_ReceivesAgentName verifies dispatchHook auto-fills
// Input.AgentName for tool events, whose toolexec.NewHooksInput builder
// can't set it — matching every other event type.
func TestPreToolUseHook_ReceivesAgentName(t *testing.T) {
t.Parallel()

executed := new(bool)
agentTools := recordingTool("the_tool", executed)
hookName := "test_agentname_" + t.Name()

prov := &mockProvider{id: "test/mock-model", stream: &mockStream{}}
root := agent.New("root", "instr",
agent.WithModel(prov),
agent.WithToolSets(newStubToolSet(nil, agentTools, nil)),
agent.WithHooks(preToolUseHooksConfig(hookName)),
)
rt, err := NewLocalRuntime(team.New(team.WithAgents(root)),
WithSessionCompaction(false), WithModelStore(mockModelStore{}))
require.NoError(t, err)

var gotAgentName string
require.NoError(t, rt.hooksRegistry.RegisterBuiltin(hookName, func(_ context.Context, in *hooks.Input, _ []string) (*hooks.Output, error) {
if in != nil && in.HookEventName == hooks.EventPreToolUse {
gotAgentName = in.AgentName
}
return &hooks.Output{HookSpecificOutput: &hooks.HookSpecificOutput{
HookEventName: hooks.EventPreToolUse,
PermissionDecision: hooks.DecisionAllow,
}}, nil
}))

sess := session.New(session.WithUserMessage("test"))
_ = runJudgedToolCall(t, rt, sess, agentTools)

assert.Equal(t, "root", gotAgentName,
"pre_tool_use hook Input must carry the dispatching agent's name")
}