{"record":{"id":"c4981557be20dfd9","repo":"Hmbown/CodeWhale","slug":"thread-already-has-an-active-turn","errorCode":null,"errorMessage":"Thread already has an active turn","messagePattern":"Thread already has an active turn","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/tui/src/runtime_threads.rs","lineNumber":9930,"sourceCode":"        }\n        if !image_blocks.is_empty() || req.max_output_tokens.is_some() {\n            let identity = self.provider_identity_for_thread(&cfg_snapshot, &thread)?;\n            let route = resolve_runtime_thread_route_for_identity(&cfg_snapshot, &identity, Some(&requested_model))?;\n            if !image_blocks.is_empty() && route.candidate.capabilities().image_input != codewhale_config::route::CapabilityState::Supported {\n                bail!(\"image inputs require a model with explicitly supported image input\");\n            }\n            if req.max_output_tokens.is_some() && !crate::route_budget::route_supports_output_token_limit(route.identity.provider, route.candidate.protocol()) {\n                bail!(\"maxOutputTokens is unsupported by the selected provider transport\");\n            }\n        }\n        let engine = self.ensure_engine_loaded(&thread).await?;\n\n        let client_preflight_required = {\n            let active = self.active.lock().await;\n            if let Some(active_thread) = active.engines.get(thread_id)\n                && active_thread.active_turn.is_some()\n            {\n                bail!(\"Thread already has an active turn\");\n            }\n            active\n                .engines\n                .get(thread_id)\n                .is_none_or(|state| state.client_preflight_required)\n        };\n\n        // Resolve the concrete provider/model before persisting a turn. Auto\n        // routing can fail, and such a failure must not leave a zombie\n        // in-progress record behind.\n        let identity = self.provider_identity_for_thread(&cfg_snapshot, &thread)?;\n        let mut thread_config = cfg_snapshot.clone();\n        thread_config.scope_to_provider_identity(&identity);\n        let verbosity = thread_config.verbosity.clone();\n        let (\n            route,\n            reasoning_effort,\n            auto_controls_reasoning,","sourceCodeStart":9912,"sourceCodeEnd":9948,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/73e0f67d83c59909b571efdfc88c4bc28c309cb1/crates/tui/src/runtime_threads.rs#L9912-L9948","documentation":"Before starting a turn, the runtime checks under the active-engines lock whether the thread's engine state already has an active_turn. Rejecting concurrent turns prevents interleaved turn state, goal mutations, and duplicated streaming output on one thread.","triggerScenarios":"Calling the start-turn API twice for the same thread_id before the first turn finishes; firing a turn while a previous turn is still streaming; a stuck active_turn left by a crashed/interrupted turn that was never cleared.","commonSituations":"Double-clicking a send button; an event handler re-entering on streaming updates; a previous turn hung on a provider call so active_turn was never released.","solutions":["Wait for the current turn to complete (or call interrupt_turn with the active turn_id) before starting a new one.","Track turn lifecycle events to disable the send path while a turn is active.","If a stale turn is stuck, restart/reload the thread engine to clear active_turn."],"exampleFix":"// before\nstart_turn(thread_id, req)?;\nstart_turn(thread_id, req2)?; // panics: active turn\n// after\nif !thread_has_active_turn(thread_id) {\n    start_turn(thread_id, req)?;\n}","handlingStrategy":"try-catch","validationCode":"fn can_start_turn(active: &ActiveEngines, thread_id: &str) -> bool {\n    active.engines.get(thread_id).map(|s| s.active_turn.is_none()).unwrap_or(false)\n}","typeGuard":null,"tryCatchPattern":"match submit_turn(thread_id, req).await {\n    Err(e) if e.to_string().contains(\"active turn\") => queue_for_retry(thread_id, req),\n    other => other?,\n}","preventionTips":["Disable the send control while a turn is active on the thread.","Subscribe to turn-completion events before allowing another submission.","Interrupt (with the known turn_id) instead of blindly resubmitting."],"tags":["concurrency","turn-lifecycle","state-conflict"],"backgroundTag":"invalid-state-transition","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"}