| 1 | package agent |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | |
| 6 | "reasonix/internal/provider" |
| 7 | ) |
| 8 | |
| 9 | // rollbackPlannerTurn restores the snapshot unless compaction rewrote it. |
| 10 | // After a rewrite it keeps the compacted history but removes a failed tail: |
| 11 | // plain user nudges and empty or unpaired tool-call assistant turns. |
| 12 | func (c *Coordinator) rollbackPlannerTurn(before []provider.Message, rewriteBefore int) { |
| 13 | if c.plannerSess.RewriteVersion() == rewriteBefore { |
| 14 | c.plannerSess.Replace(before) |
| 15 | return |
| 16 | } |
| 17 | msgs := c.plannerSess.Snapshot() |
| 18 | for len(msgs) > 0 { |
| 19 | last := msgs[len(msgs)-1] |
| 20 | if last.Role == provider.RoleAssistant && |
| 21 | (len(last.ToolCalls) > 0 || strings.TrimSpace(last.Content) == "") { |
| 22 | msgs = msgs[:len(msgs)-1] |
| 23 | continue |
| 24 | } |
| 25 | if last.Role != provider.RoleUser || isCompactionSummary(last) { |
| 26 | break |
| 27 | } |
| 28 | msgs = msgs[:len(msgs)-1] |
| 29 | } |
| 30 | c.plannerSess.Replace(msgs) |
| 31 | } |
| 32 |