Hmbown/CodeWhale · critical

Failed to persist turn

Error message

Failed to persist turn: {persistence_error}; cleanup also failed: {cleanup_error}

What it means

Raised in the TUI runtime's turn-preparation path when the durable store fails to persist a new turn (its operation binding, user item, turn record, or thread pointer). Before returning the error, the runtime attempts to clean up any partially-written records for the unaccepted turn; if that cleanup also fails, the original persistence error is wrapped together with the cleanup error so neither failure is silently lost. The active-turn slot and route identity are rolled back to their previous values.

Solutions

  1. Read both `{persistence_error}` and `{cleanup_error}` in the message; fix the persistence root cause first (usually disk space or a locked/corrupt store file).
  2. Free disk space or fix permissions on the codewhale data directory, then retry the turn.
  3. If cleanup also failed, inspect the store for orphaned records from this turn_id and remove them manually or let startup recovery reconcile them.
  4. If the store is corrupt, back up and remove/rebuild the thread store; exact retries replay the turn after recovery.

Example fix

// Both failures chained into one anyhow error — no caller code change; fix the environment
// before
Err(anyhow!("Failed to persist turn: {persistence_error}"))  // cleanup error lost
// after (current code)
Err(anyhow!("Failed to persist turn: {persistence_error}; cleanup also failed: {cleanup_error}"))
Defensive patterns

Strategy: try-catch

Try / catch

match result {
    Err(e) if e.to_string().contains("Failed to persist turn") => {
        // inspect chained cleanup error, free disk space / repair store, then retry the turn
    }
    _ => {}
}

Prevention

When it happens

Trigger: Calling the send-message path (spawn of a new turn via `acceptance` preparation in runtime_threads.rs) when the persistence closure fails: `store.save_turn_operation_binding`, `store.save_item`, `store.save_turn`, or `store.save_thread` returns Err (e.g. disk full, corrupted SQLite/file store, lock contention), AND `cleanup_unaccepted_turn_records` also returns Err.

Common situations: Disk full or I/O errors on the machine running the TUI; a corrupted or locked thread store after a crash; concurrent processes holding the store's mutation locks; running on read-only storage or a failing disk.

Related errors


AI-assisted analysis of Hmbown/CodeWhale@73e0f67d83 (2026-09-22). Data as JSON: /api/errors/97f6750a04716cbb. Report an issue: GitHub.

Appendix: source

Thrown at crates/tui/src/runtime_threads.rs:10288

                current_thread.updated_at = now;
                self.store.save_thread(&current_thread)
            })();
            if let Err(persistence_error) = persistence_result {
                let cleanup_error = self
                    .cleanup_unaccepted_turn_records(
                        &turn_id,
                        Some(&user_item_id),
                        operation
                            .as_ref()
                            .map(|operation| operation.binding.operation_key_fingerprint.as_str()),
                    )
                    .err();
                state.active_turn = None;
                state.route_identity = previous_active_route.0;
                state.route_model = previous_active_route.1;
                return match cleanup_error {
                    None => Err(anyhow!("Failed to persist turn: {persistence_error}")),
                    Some(cleanup_error) => Err(anyhow!(
                        "Failed to persist turn: {persistence_error}; cleanup also failed: {cleanup_error}"
                    )),
                };
            }

            // The binding, item, turn, and thread pointer are now durable.
            // Release the cross-process claim before handing the provider work
            // to the engine; exact retries will observe and replay this turn.
            drop(operation_claim_guard);
            drop(operation_claim);

            self.register_runtime_usage_sink(&turn_id);
            // Sending through an owned permit cannot await or fail. From this
            // point the engine owns the operation and the spawned task owns
            // lifecycle events, monitoring, and terminal cleanup even if the
            // HTTP/client future is dropped.
            engine.publish_turn_authority(
                mode,

View on GitHub (pinned to 73e0f67d83)