fix: save placeholder message when task is cancelled

When user cancels a task with /stop, the user message was saved but
the assistant response was not, causing orphaned user messages in
session history. Now saves '[Task cancelled by user]' placeholder to
keep conversation history complete.
This commit is contained in:
swordandart 2026-03-10 00:01:13 +08:00
parent af2862b561
commit 270814c3c9

View file

@ -846,6 +846,12 @@ func (al *AgentLoop) runAgentLoop(
// 3. Run LLM iteration loop // 3. Run LLM iteration loop
finalContent, iteration, err := al.runLLMIteration(ctx, agent, messages, opts) finalContent, iteration, err := al.runLLMIteration(ctx, agent, messages, opts)
if err != nil { if err != nil {
// If task was cancelled, save a placeholder message to session
// This prevents the user message from being orphaned without a response
if errors.Is(err, context.Canceled) {
agent.Sessions.AddMessage(opts.SessionKey, "assistant", "[Task cancelled by user]")
agent.Sessions.Save(opts.SessionKey)
}
return "", err return "", err
} }