Hmbown/CodeWhale · critical

Failed to persist turn

Error message

Failed to persist turn: {persistence_error}

What it means

When a turn fails to persist, the runtime rolls back the active-turn state and attempts cleanup. If persistence failed and cleanup succeeded (or found nothing to clean), it returns 'Failed to persist turn: {persistence_error}'; if cleanup also failed, the message appends '; cleanup also failed: {cleanup_error}'. The turn did not start and the store may be left without the turn record.

Solutions

  1. Read {persistence_error} for the concrete storage failure (permissions, disk, lock)
  2. Free disk space or fix filesystem permissions on the store directory
  3. Ensure no other Runtime process holds the store's file lock; close stale processes
  4. Repair or recreate the turn store (backup first) if it is corrupted
  5. If cleanup also failed, address {cleanup_error} as well — active-turn state may need manual reset

Example fix

// before
start_turn(...).await?; // opaque persistence error at runtime
// after
if store.writable().is_err() {
    bail!("turn store directory is not writable: {}", store.path().display());
}
start_turn(...).await?;
Defensive patterns

Strategy: try-catch

Validate before calling

let path = store.path();
let writable = std::fs::metadata(path).map(|m| !m.permissions().readonly());
if !matches!(writable, Ok(true)) { bail!("turn store not writable: {}", path.display()); }

Try / catch

match start_turn(req).await {
    Err(e) if e.to_string().starts_with("Failed to persist turn") => {
        // check disk space, permissions, and competing Runtime file locks before retry
        bail!("turn store unavailable; fix storage and retry: {e:#}");
    }
    other => other,
}

Prevention

When it happens

Trigger: start_turn reached the point of writing the turn record to the store and the write failed (disk full, permission denied, store locked by another Runtime process, corrupted store file), and no cleanup error compounded it.

Common situations: Full disk or read-only filesystem; a second Runtime process holding the OS-backed file lock on the shared store; store corruption or an incompatible store schema after an upgrade; permission changes on the data directory.

Related errors


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

Appendix: source

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

                current_thread.latest_turn_id = Some(turn_id.clone());
                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(

View on GitHub (pinned to 73e0f67d83)