From 93ec54005f6bf2b140374630290b03a23e0113e7 Mon Sep 17 00:00:00 2001 From: Osamaali313 Date: Wed, 15 Jul 2026 23:27:46 +0300 Subject: [PATCH] fix(agent): guard against nil user message in knowledgeBaseLookup knowledgeBaseLookup did `userMessage := conv.GetLatestUserMessage().Content`, but GetLatestUserMessage returns nil when the conversation has no user-role message, so this panics with a nil pointer dereference. The function even has an explicit "No user message found in conversation" branch just below, but it was unreachable because the crash happens first. A non-empty conversation with no user message is a real state the code anticipates (e.g. previous_response_id continuations that end with an assistant message, or system/assistant-only history). Nil-check the pointer before dereferencing so the existing guard runs; also drop the redundant second GetLatestUserMessage() call in the log. --- core/agent/knowledgebase.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/core/agent/knowledgebase.go b/core/agent/knowledgebase.go index 10d70326..37c2c513 100644 --- a/core/agent/knowledgebase.go +++ b/core/agent/knowledgebase.go @@ -41,11 +41,8 @@ func (a *Agent) knowledgeBaseLookup(job *types.Job, conv Messages) Messages { // Walk conversation from bottom to top, and find the first message of the user // to use it as a query to the KB - userMessage := conv.GetLatestUserMessage().Content - - xlog.Info("[Knowledge Base Lookup] Last user message", "agent", a.Character.Name, "message", userMessage, "lastMessage", conv.GetLatestUserMessage()) - - if userMessage == "" { + lastUser := conv.GetLatestUserMessage() + if lastUser == nil || lastUser.Content == "" { xlog.Info("[Knowledge Base Lookup] No user message found in conversation", "agent", a.Character.Name) if obs != nil { obs.Completion = &types.Completion{ @@ -55,6 +52,9 @@ func (a *Agent) knowledgeBaseLookup(job *types.Job, conv Messages) Messages { } return conv } + userMessage := lastUser.Content + + xlog.Info("[Knowledge Base Lookup] Last user message", "agent", a.Character.Name, "message", userMessage) results, err := a.options.ragdb.Search(userMessage, a.options.kbResults) if err != nil {