{"record":{"id":"97f6750a04716cbb","repo":"Hmbown/CodeWhale","slug":"failed-to-persist-turn-persistence-error-clean","errorCode":null,"errorMessage":"Failed to persist turn: {persistence_error}; cleanup also failed: {cleanup_error}","messagePattern":"Failed to persist turn: (.+?); cleanup also failed: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"crates/tui/src/runtime_threads.rs","lineNumber":10288,"sourceCode":"                current_thread.updated_at = now;\n                self.store.save_thread(&current_thread)\n            })();\n            if let Err(persistence_error) = persistence_result {\n                let cleanup_error = self\n                    .cleanup_unaccepted_turn_records(\n                        &turn_id,\n                        Some(&user_item_id),\n                        operation\n                            .as_ref()\n                            .map(|operation| operation.binding.operation_key_fingerprint.as_str()),\n                    )\n                    .err();\n                state.active_turn = None;\n                state.route_identity = previous_active_route.0;\n                state.route_model = previous_active_route.1;\n                return match cleanup_error {\n                    None => Err(anyhow!(\"Failed to persist turn: {persistence_error}\")),\n                    Some(cleanup_error) => Err(anyhow!(\n                        \"Failed to persist turn: {persistence_error}; cleanup also failed: {cleanup_error}\"\n                    )),\n                };\n            }\n\n            // The binding, item, turn, and thread pointer are now durable.\n            // Release the cross-process claim before handing the provider work\n            // to the engine; exact retries will observe and replay this turn.\n            drop(operation_claim_guard);\n            drop(operation_claim);\n\n            self.register_runtime_usage_sink(&turn_id);\n            // Sending through an owned permit cannot await or fail. From this\n            // point the engine owns the operation and the spawned task owns\n            // lifecycle events, monitoring, and terminal cleanup even if the\n            // HTTP/client future is dropped.\n            engine.publish_turn_authority(\n                mode,","sourceCodeStart":10270,"sourceCodeEnd":10306,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/73e0f67d83c59909b571efdfc88c4bc28c309cb1/crates/tui/src/runtime_threads.rs#L10270-L10306","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["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).","Free disk space or fix permissions on the codewhale data directory, then retry the turn.","If cleanup also failed, inspect the store for orphaned records from this turn_id and remove them manually or let startup recovery reconcile them.","If the store is corrupt, back up and remove/rebuild the thread store; exact retries replay the turn after recovery."],"exampleFix":"// Both failures chained into one anyhow error — no caller code change; fix the environment\n// before\nErr(anyhow!(\"Failed to persist turn: {persistence_error}\"))  // cleanup error lost\n// after (current code)\nErr(anyhow!(\"Failed to persist turn: {persistence_error}; cleanup also failed: {cleanup_error}\"))","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"match result {\n    Err(e) if e.to_string().contains(\"Failed to persist turn\") => {\n        // inspect chained cleanup error, free disk space / repair store, then retry the turn\n    }\n    _ => {}\n}","preventionTips":["Monitor free disk space on the codewhale data directory before long sessions.","Keep the store on reliable, non-read-only storage with correct permissions.","Avoid running multiple codewhale processes against the same data directory.","Back up the thread store periodically so corruption is recoverable."],"tags":["persistence","storage","cleanup","rust"],"backgroundTag":"database-write-failed","analyzedSha":"73e0f67d83c59909b571efdfc88c4bc28c309cb1","analyzedAt":"2026-09-22T01:30:00.501Z","contentChangedAt":"2026-09-22T01:30:00.501Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}