{"record":{"id":"d12dc282fc032b43","repo":"Hmbown/CodeWhale","slug":"task-id-already-exists","errorCode":null,"errorMessage":"Task id already exists: {}","messagePattern":"Task id already exists: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/tui/src/task_manager.rs","lineNumber":1798,"sourceCode":"            // replay ignores an interrupted create until the queue write has\n            // succeeded and this file is atomically promoted.\n            let staged_task_path = self.tasks_dir.join(format!(\".{}.json.pending\", task.id));\n            if recover_stage {\n                if let Some(accepted) = self.read_bound_task(&task.id)? {\n                    validate_bound_task_request(&accepted, &NewTaskRequest::from_task(&task))?;\n                    return Ok(accepted);\n                }\n                let current = read_bound_task_file(&staged_task_path, &task.id)?\n                    .context(\"Unaccepted task stage disappeared during recovery\")?;\n                if serde_json::to_value(&current)? != serde_json::to_value(&task)? {\n                    bail!(\"Unaccepted task stage changed during recovery\");\n                }\n            }\n            if state.tasks.contains_key(&task.id)\n                || task_path.exists()\n                || (!recover_stage && staged_task_path.exists())\n            {\n                bail!(\"Task id already exists: {}\", task.id);\n            }\n            let mut next_queue = state.queue.clone();\n            if !next_queue.contains(&task.id) {\n                next_queue.push_back(task.id.clone());\n            }\n\n            // Stage the owner record, then persist its queue membership, then\n            // atomically promote it. A crash before promotion leaves either an\n            // ignored staged file or a queue entry with no task (which replay\n            // drops); a crash after promotion leaves the complete runnable\n            // pair. In-memory scheduling is published only after all three.\n            if !recover_stage {\n                write_json_atomic(&staged_task_path, &task)?;\n            }\n            if let Err(err) = self.persist_queue_locked(&next_queue) {\n                if !recover_stage\n                    && let Err(cleanup_err) = tokio::fs::remove_file(&staged_task_path).await\n                {","sourceCodeStart":1780,"sourceCodeEnd":1816,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/73e0f67d83c59909b571efdfc88c4bc28c309cb1/crates/tui/src/task_manager.rs#L1780-L1816","documentation":"When admitting a recovered (or staged) task, the manager checks that the task id is not already present in the in-memory map, that no durable task file exists for it, and (outside stage recovery) that no staged file exists. Any of these means the id would collide with an existing task, so admission is refused.","triggerScenarios":"Calling task admission/recovery with a task whose id is already in ManagerState.tasks, or whose task_<id>.json file (or staged file) already exists on disk.","commonSituations":"Replaying an admission log after the task was already admitted; re-running recovery after a partial earlier recovery; client-generated preallocated ids colliding with existing tasks; duplicated task creation from a retried request.","solutions":["Use a fresh preallocated id (task_ + 16 hex chars) or let the manager allocate one","Check state/tasks or the tasks directory before admitting a manually chosen id","If recovering, drop the duplicate admission record instead of re-admitting it"],"exampleFix":"// before\nlet task = manager.create_task_with_id(\"task_abcdef0123456789\", req).await?; // may collide\n// after\nif manager.get_task(\"task_abcdef0123456789\").is_some() { return Err(anyhow!(\"id in use\")); }\nlet task = manager.create_task_with_id(\"task_abcdef0123456789\", req).await?","handlingStrategy":"validation","validationCode":"let id = \"task_abcdef0123456789\";\nif manager.get_task(id).is_some() || std::path::Path::new(&format!(\"tasks/{id}.json\")).exists() {\n    return Err(anyhow!(\"id already in use: {id}\"));\n}","typeGuard":"fn id_is_free(id: &str, state: &ManagerState, dir: &Path) -> bool {\n    !state.tasks.contains_key(id) && !dir.join(format!(\"{id}.json\")).exists()\n}","tryCatchPattern":"match manager.admit_task(task).await {\n    Err(e) if e.to_string().starts_with(\"Task id already exists\") => {\n        task.id = new_preallocated_id();\n        manager.admit_task(task).await?;\n    }\n    other => other?,\n}","preventionTips":["Let the manager allocate ids instead of preallocating when possible","Check for id existence before replaying admissions","Make task creation idempotent via a client request key rather than reusing ids"],"tags":["task-manager","duplicate-key","persistence","admission"],"backgroundTag":"file-already-exists","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"}