{"record":{"id":"4bd6fe6d0924ffee","repo":"Hmbown/CodeWhale","slug":"failed-to-persist-turn-persistence-error","errorCode":null,"errorMessage":"Failed to persist turn: {persistence_error}","messagePattern":"Failed to persist turn: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"crates/tui/src/runtime_threads.rs","lineNumber":10287,"sourceCode":"                current_thread.latest_turn_id = Some(turn_id.clone());\n                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(","sourceCodeStart":10269,"sourceCodeEnd":10305,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/73e0f67d83c59909b571efdfc88c4bc28c309cb1/crates/tui/src/runtime_threads.rs#L10269-L10305","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Read {persistence_error} for the concrete storage failure (permissions, disk, lock)","Free disk space or fix filesystem permissions on the store directory","Ensure no other Runtime process holds the store's file lock; close stale processes","Repair or recreate the turn store (backup first) if it is corrupted","If cleanup also failed, address {cleanup_error} as well — active-turn state may need manual reset"],"exampleFix":"// before\nstart_turn(...).await?; // opaque persistence error at runtime\n// after\nif store.writable().is_err() {\n    bail!(\"turn store directory is not writable: {}\", store.path().display());\n}\nstart_turn(...).await?;","handlingStrategy":"try-catch","validationCode":"let path = store.path();\nlet writable = std::fs::metadata(path).map(|m| !m.permissions().readonly());\nif !matches!(writable, Ok(true)) { bail!(\"turn store not writable: {}\", path.display()); }","typeGuard":null,"tryCatchPattern":"match start_turn(req).await {\n    Err(e) if e.to_string().starts_with(\"Failed to persist turn\") => {\n        // check disk space, permissions, and competing Runtime file locks before retry\n        bail!(\"turn store unavailable; fix storage and retry: {e:#}\");\n    }\n    other => other,\n}","preventionTips":["Monitor disk space and permissions on the store directory","Run only one Runtime process per store, or handle the OS-backed file lock contention","Back up and schema-check the store after upgrades","If the message includes 'cleanup also failed', manually verify active-turn state was rolled back"],"tags":["persistence","storage","turn-lifecycle"],"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"}