agent: preserve session state on interrupt, discard only on real error

Interrupting a long turn previously cleared the session (first-turn case)
or rolled back messages (subsequent turns). An interrupt is intentional —
keep whatever state was built so the user can continue.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Levi Neely 2026-04-26 13:35:13 +02:00
parent c6d6f2f162
commit 554697e259
1 changed files with 9 additions and 6 deletions

View File

@ -854,14 +854,17 @@ func (s *agent) executeTurn(ctx context.Context, input string, handler EventHand
s.setState("idle")
if err != nil {
if snapSession == nil {
s.session = nil
} else {
s.session.messages = snapMessages
}
if !errors.Is(err, context.Canceled) && !errors.Is(err, ErrInterrupted) {
interrupted := errors.Is(err, context.Canceled) || errors.Is(err, ErrInterrupted)
if !interrupted {
// Real error: restore pre-turn state so the session is clean for retry.
if snapSession == nil {
s.session = nil
} else {
s.session.messages = snapMessages
}
handler(Event{Role: "error", Content: err.Error()})
}
// Interrupt: keep whatever session state was built during the turn.
return ""
}