From bbb4266060fd1dd4ab55ad46bd09aeb3cabb97f2 Mon Sep 17 00:00:00 2001 From: pkonowrocki <37312116+pkonowrocki@users.noreply.github.com> Date: Sun, 15 Feb 2026 17:19:56 +0100 Subject: [PATCH] fix: panic in atomic save when sanitizeHistory shrinks history historyOffset was computed from len(history) before sanitization, but BuildMessages runs sanitizeHistory which can remove messages. This caused a slice bounds out of range panic when the sanitized history was shorter than the original. Fix: compute offset from actual messages slice length instead. --- pkg/agent/loop.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pkg/agent/loop.go b/pkg/agent/loop.go index fa0db2e4d..8f213de71 100644 --- a/pkg/agent/loop.go +++ b/pkg/agent/loop.go @@ -374,7 +374,9 @@ func (al *AgentLoop) runAgentLoop(ctx context.Context, opts processOptions) (str ) // 3. Run LLM iteration loop (no session saves until success) - historyOffset := 1 + len(history) // skip system prompt + existing history + // Offset points to the user message (last element BuildMessages added). + // We save from here onward after success (user + any tool-call messages). + historyOffset := len(messages) - 1 finalContent, messages, iteration, err := al.runLLMIteration(ctx, messages, opts) if err != nil { return "", err