{"record":{"id":"5586a6720d9172ff","repo":"Hmbown/CodeWhale","slug":"fork-at-user-message-depth-exceeds-user-tur","errorCode":null,"errorMessage":"fork_at_user_message: depth {} exceeds {} user turn(s)","messagePattern":"fork_at_user_message: depth (.+?) exceeds (.+?) user turn\\(s\\)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/tui/src/runtime_threads.rs","lineNumber":8423,"sourceCode":"        depth_from_tail: usize,\n    ) -> Result<PreparedThreadFork> {\n        let source = self.get_thread(id).await?;\n        let source_turns = self.store.list_turns_for_thread(&source.id)?;\n\n        // Walk turns from newest to oldest. For each turn, ask: does it\n        // contain a UserMessage item? If yes, it counts toward the depth.\n        let mut user_turn_indices: Vec<usize> = Vec::new();\n        for (idx, turn) in source_turns.iter().enumerate().rev() {\n            let items = self.store.list_items_for_turn(&turn.id)?;\n            if items\n                .iter()\n                .any(|item| item.kind == TurnItemKind::UserMessage)\n            {\n                user_turn_indices.push(idx);\n            }\n        }\n        if depth_from_tail >= user_turn_indices.len() {\n            bail!(\n                \"fork_at_user_message: depth {} exceeds {} user turn(s)\",\n                depth_from_tail,\n                user_turn_indices.len()\n            );\n        }\n        // `user_turn_indices` is newest-first because we iterated in\n        // reverse, so the Nth element is exactly the Nth-from-tail user\n        // turn in the original chronological list.\n        let target_turn_idx = user_turn_indices[depth_from_tail];\n        let target_turn_id = source_turns[target_turn_idx].id.clone();\n\n        // Pull the original user-message text out of the dropped turn so\n        // the caller can drop it back into the composer.\n        let target_items = self.store.list_items_for_turn(&target_turn_id)?;\n        let original_user_text = target_items\n            .iter()\n            .find(|item| item.kind == TurnItemKind::UserMessage)\n            .and_then(|item| item.detail.clone());","sourceCodeStart":8405,"sourceCodeEnd":8441,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/73e0f67d83c59909b571efdfc88c4bc28c309cb1/crates/tui/src/runtime_threads.rs#L8405-L8441","documentation":"fork_at_user_message walked the source thread's turns and found fewer user turns than the requested depth, so the fork point does not exist; the fork is refused rather than silently forking at an unintended position.","triggerScenarios":"Thrown at crates/tui/src/runtime_threads.rs:8423 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Use a smaller depth_from_tail within the thread's user-turn count.","Choose the target user message by inspecting the thread's turns and pass the correct depth."],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"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"}