fix(agent): guard against nil user message in knowledgeBaseLookup (nil-pointer panic)#485
Open
Osamaali313 wants to merge 1 commit into
Open
fix(agent): guard against nil user message in knowledgeBaseLookup (nil-pointer panic)#485Osamaali313 wants to merge 1 commit into
Osamaali313 wants to merge 1 commit into
Conversation
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.
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.
Problem
knowledgeBaseLookup(core/agent/knowledgebase.go) dereferences the result ofGetLatestUserMessage()before checking it for nil:Messages.GetLatestUserMessage()(core/agent/actions.go) returnsnilwhen the conversation has nouser-role message: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_idcontinuations that end with an assistant message, or system/assistant-only history).knowledgeBaseLookupruns for every job when KB auto-search is enabled (agent.go:986).Reproduction
conv = [{system,...}, {assistant,...}](no user message):runtime error: invalid memory address or nil pointer dereference(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):go build ./core/agent/passes. (Thecore/agentsuite expects a live LLM endpoint, so I verified the crash/fix via the standalone repro rather than the full suite.)