···199199 // Remove taken operations
200200 m.operations = m.operations[n:]
201201202202+ // Adjust lastSavedLen to account for removed operations
203203+ if m.lastSavedLen > 0 {
204204+ if m.lastSavedLen > n {
205205+ m.lastSavedLen -= n // Some saved ops remain
206206+ } else {
207207+ m.lastSavedLen = 0 // All saved ops were taken
208208+ }
209209+ }
210210+211211+ // ✨ Mark dirty since state changed
212212+ m.dirty = true
213213+202214 return result, nil
203215}
204216···294306 // Validate before saving
295307 if err := m.validateLocked(); err != nil {
296308 return fmt.Errorf("mempool validation failed, refusing to save: %w", err)
309309+ }
310310+311311+ // Bounds check to prevent panic
312312+ if m.lastSavedLen > len(m.operations) {
313313+ // This shouldn't happen, but if it does, log and reset
314314+ if m.verbose {
315315+ m.logger.Printf("Warning: lastSavedLen (%d) > operations (%d), resetting to 0",
316316+ m.lastSavedLen, len(m.operations))
317317+ }
318318+ m.lastSavedLen = 0
297319 }
298320299321 // Get only new operations since last save