Hmbown/CodeWhale · error

Task id already exists

Error message

Task id already exists: {}

What it means

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.

Solutions

  1. Use a fresh preallocated id (task_ + 16 hex chars) or let the manager allocate one
  2. Check state/tasks or the tasks directory before admitting a manually chosen id
  3. If recovering, drop the duplicate admission record instead of re-admitting it

Example fix

// before
let task = manager.create_task_with_id("task_abcdef0123456789", req).await?; // may collide
// after
if manager.get_task("task_abcdef0123456789").is_some() { return Err(anyhow!("id in use")); }
let task = manager.create_task_with_id("task_abcdef0123456789", req).await?
Defensive patterns

Strategy: validation

Validate before calling

let id = "task_abcdef0123456789";
if manager.get_task(id).is_some() || std::path::Path::new(&format!("tasks/{id}.json")).exists() {
    return Err(anyhow!("id already in use: {id}"));
}

Type guard

fn id_is_free(id: &str, state: &ManagerState, dir: &Path) -> bool {
    !state.tasks.contains_key(id) && !dir.join(format!("{id}.json")).exists()
}

Try / catch

match manager.admit_task(task).await {
    Err(e) if e.to_string().starts_with("Task id already exists") => {
        task.id = new_preallocated_id();
        manager.admit_task(task).await?;
    }
    other => other?,
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "already exists" / EEXIST / FileAlreadyExistsException: what the 'file already exists' error means and how to fix it — this error's family across 37 libraries.

Related errors


AI-assisted analysis of Hmbown/CodeWhale@73e0f67d83 (2026-09-22). Data as JSON: /api/errors/d12dc282fc032b43. Report an issue: GitHub.

Appendix: source

Thrown at crates/tui/src/task_manager.rs:1798

            // replay ignores an interrupted create until the queue write has
            // succeeded and this file is atomically promoted.
            let staged_task_path = self.tasks_dir.join(format!(".{}.json.pending", task.id));
            if recover_stage {
                if let Some(accepted) = self.read_bound_task(&task.id)? {
                    validate_bound_task_request(&accepted, &NewTaskRequest::from_task(&task))?;
                    return Ok(accepted);
                }
                let current = read_bound_task_file(&staged_task_path, &task.id)?
                    .context("Unaccepted task stage disappeared during recovery")?;
                if serde_json::to_value(&current)? != serde_json::to_value(&task)? {
                    bail!("Unaccepted task stage changed during recovery");
                }
            }
            if state.tasks.contains_key(&task.id)
                || task_path.exists()
                || (!recover_stage && staged_task_path.exists())
            {
                bail!("Task id already exists: {}", task.id);
            }
            let mut next_queue = state.queue.clone();
            if !next_queue.contains(&task.id) {
                next_queue.push_back(task.id.clone());
            }

            // Stage the owner record, then persist its queue membership, then
            // atomically promote it. A crash before promotion leaves either an
            // ignored staged file or a queue entry with no task (which replay
            // drops); a crash after promotion leaves the complete runnable
            // pair. In-memory scheduling is published only after all three.
            if !recover_stage {
                write_json_atomic(&staged_task_path, &task)?;
            }
            if let Err(err) = self.persist_queue_locked(&next_queue) {
                if !recover_stage
                    && let Err(cleanup_err) = tokio::fs::remove_file(&staged_task_path).await
                {

View on GitHub (pinned to 73e0f67d83)