From 0c3893ee8b007a6cec76a38131bcf225fe075800 Mon Sep 17 00:00:00 2001 From: Bryan Frimin Date: Fri, 13 Mar 2026 19:31:09 +0100 Subject: [PATCH] Skip nil handoffs to prevent panic during run Signed-off-by: Bryan Frimin --- pkg/agent/agent.go | 10 +++- pkg/agent/agent_test.go | 110 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+), 2 deletions(-) diff --git a/pkg/agent/agent.go b/pkg/agent/agent.go index 569ea0050..dad6c8804 100644 --- a/pkg/agent/agent.go +++ b/pkg/agent/agent.go @@ -178,14 +178,20 @@ func WithTools(tools ...Tool) Option { func WithHandoffs(agents ...*Agent) Option { return func(a *Agent) { for _, ag := range agents { - a.handoffs = append(a.handoffs, &Handoff{Agent: ag}) + if ag != nil { + a.handoffs = append(a.handoffs, &Handoff{Agent: ag}) + } } } } func WithHandoffConfigs(handoffs ...*Handoff) Option { return func(a *Agent) { - a.handoffs = append(a.handoffs, handoffs...) + for _, h := range handoffs { + if h != nil && h.Agent != nil { + a.handoffs = append(a.handoffs, h) + } + } } } diff --git a/pkg/agent/agent_test.go b/pkg/agent/agent_test.go index 2842595b8..00b1e7db3 100644 --- a/pkg/agent/agent_test.go +++ b/pkg/agent/agent_test.go @@ -3103,3 +3103,113 @@ func TestResume_HandoffWithInputFilter(t *testing.T) { assert.Equal(t, "Filtered specialist here.", result.FinalMessage().Text()) assert.Equal(t, "specialist", result.LastAgent.Name()) } + +func TestRun_NilHandoffsAreSkipped(t *testing.T) { + t.Parallel() + + t.Run( + "nil agent in WithHandoffs", + func(t *testing.T) { + t.Parallel() + + provider := &mockProvider{ + responses: []*llm.ChatCompletionResponse{ + stopResponse("Hello."), + }, + } + + client := newTestClient(provider) + + billing := agent.New( + "billing", + client, + agent.WithModel("test-model"), + ) + + a := agent.New( + "triage", + client, + agent.WithModel("test-model"), + agent.WithHandoffs(nil, billing, nil), + ) + + result, err := a.Run( + context.Background(), + []llm.Message{userMessage("hi")}, + ) + + require.NoError(t, err) + assert.Equal(t, "Hello.", result.FinalMessage().Text()) + }, + ) + + t.Run( + "nil handoff in WithHandoffConfigs", + func(t *testing.T) { + t.Parallel() + + provider := &mockProvider{ + responses: []*llm.ChatCompletionResponse{ + stopResponse("Hello."), + }, + } + + client := newTestClient(provider) + + billing := agent.New( + "billing", + client, + agent.WithModel("test-model"), + ) + + a := agent.New( + "triage", + client, + agent.WithModel("test-model"), + agent.WithHandoffConfigs( + nil, + agent.HandoffTo(billing), + nil, + ), + ) + + result, err := a.Run( + context.Background(), + []llm.Message{userMessage("hi")}, + ) + + require.NoError(t, err) + assert.Equal(t, "Hello.", result.FinalMessage().Text()) + }, + ) + + t.Run( + "HandoffTo with nil agent in WithHandoffConfigs", + func(t *testing.T) { + t.Parallel() + + provider := &mockProvider{ + responses: []*llm.ChatCompletionResponse{ + stopResponse("Hello."), + }, + } + + client := newTestClient(provider) + + a := agent.New( + "triage", + client, + agent.WithModel("test-model"), + agent.WithHandoffConfigs(agent.HandoffTo(nil)), + ) + + result, err := a.Run( + context.Background(), + []llm.Message{userMessage("hi")}, + ) + + require.NoError(t, err) + assert.Equal(t, "Hello.", result.FinalMessage().Text()) + }, + ) +}