Skip to content

fix(agent): guard against nil user message in knowledgeBaseLookup (nil-pointer panic)#485

Open
Osamaali313 wants to merge 1 commit into
mudler:mainfrom
Osamaali313:fix/kb-lookup-nil-user-message
Open

fix(agent): guard against nil user message in knowledgeBaseLookup (nil-pointer panic)#485
Osamaali313 wants to merge 1 commit into
mudler:mainfrom
Osamaali313:fix/kb-lookup-nil-user-message

Conversation

@Osamaali313

Copy link
Copy Markdown

Problem

knowledgeBaseLookup (core/agent/knowledgebase.go) dereferences the result of GetLatestUserMessage() before checking it for nil:

userMessage := conv.GetLatestUserMessage().Content   // line 44 — panics when nil

xlog.Info("[Knowledge Base Lookup] Last user message", ..., "lastMessage", conv.GetLatestUserMessage())

if userMessage == "" {                                // intended "no user message" handler
    ...
    return conv
}

Messages.GetLatestUserMessage() (core/agent/actions.go) returns nil when the conversation has no user-role message:

func (m Messages) GetLatestUserMessage() *openai.ChatCompletionMessage {
    for i := len(m) - 1; i >= 0; i-- {
        if m[i].Role == UserRole { msg := m[i]; return &msg }
    }
    return nil
}

So line 44 panics with a nil pointer dereference. The function's own if userMessage == "" branch is meant to handle exactly this "no user message" case — but it's unreachable, because the crash happens first. A non-empty conversation with no user message is a real, anticipated state (e.g. previous_response_id continuations that end with an assistant message, or system/assistant-only history). knowledgeBaseLookup runs for every job when KB auto-search is enabled (agent.go:986).

Reproduction

conv = [{system,...}, {assistant,...}] (no user message):

  • Before: runtime error: invalid memory address or nil pointer dereference
  • After: reaches the existing "No user message found in conversation" branch and returns cleanly.

(Verified with go1.26 on a standalone reproduction of GetLatestUserMessage + the line-44 pattern.)

Fix

Nil-check before dereferencing so the existing guard runs (and drop the redundant second GetLatestUserMessage() call in the log):

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{Error: "No user message found in conversation"}
        a.observer.Update(*obs)
    }
    return conv
}
userMessage := lastUser.Content

go build ./core/agent/ passes. (The core/agent suite expects a live LLM endpoint, so I verified the crash/fix via the standalone repro rather than the full suite.)

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.
Copilot AI review requested due to automatic review settings July 15, 2026 20:28

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants