Files
Ettore Di Giacinto f0df15cdfe fix(agent): guard the tool-call lookup that ends a job
consumeJob read Messages[len-2].ToolCalls[0] behind a guard that only
checked that the message list was non-empty. Neither index was safe.

When the model returns no tool selection, the fragment still ends in a
tool role but the message before it carries an empty ToolCalls slice.
On a LocalAI backend serving a 50176-token context, a 50603-token
request produced exactly that, and the read panicked with "index out of
range [0] with length 0". consumeJob runs on its own goroutine with no
recover, so the panic ended the process instead of the job, and the
agent crash-looped on every restart.

The lookup moves to lastToolCallName, which reports failure instead of
indexing when the conversation is too short or carries no tool call.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KZaoEXfGsjmkhXtVxtvPp4
2026-08-24 18:49:54 +00:00

29 lines
990 B
Go

package agent
// lastToolCallName returns the name of the tool call that produced the
// conversation's closing tool result, reporting false when the conversation
// does not end in one or when the call that produced it cannot be recovered.
//
// Both guards are load-bearing. A tool result needs a message before it to read
// the call from, and that message can carry an empty ToolCalls slice: when the
// model returns no tool selection — after a context-window overflow, say — the
// fragment still ends in a tool role. Reading ToolCalls[0] there panics, and
// because consumeJob runs on its own goroutine with no recover, that panic ends
// the process rather than the job.
func lastToolCallName(messages Messages) (string, bool) {
if len(messages) < 2 {
return "", false
}
if messages[len(messages)-1].Role != "tool" {
return "", false
}
calls := messages[len(messages)-2].ToolCalls
if len(calls) == 0 {
return "", false
}
return calls[0].Function.Name, true
}