BigPizzaV3/CodexPlusPlus · error

restore conflict: agent_job_items row already assigned

Error message

restore conflict: agent_job_items row already assigned

What it means

During row restore, update_existing_agent_job_item refuses to overwrite an existing assignment: it reads agent_job_items.assigned_thread_id for the row's id, and if the row already has a non-NULL assigned_thread_id it aborts with this conflict. The subsequent UPDATE is also guarded by 'WHERE assigned_thread_id IS NULL', so assignments are never silently stolen.

Solutions

  1. Accept the current assignment and exclude that agent_job_items row from the restore, or restore only rows whose assigned_thread_id is NULL.
  2. Reassign or clear assigned_thread_id on the conflicting live rows if the backup assignment is authoritative, then re-run.
  3. Restore into a fresh database where the row doesn't yet exist.
  4. If this aborts an entire restore, serialize the restore so no job scheduler mutates assignments concurrently.

Example fix

// before: blanket restore of all rows
restore_rows(db, backup_rows)?; // row 42 already assigned to thread B, backup says thread A
// after: skip already-assigned rows
let rows: Vec<_> = backup_rows.into_iter().filter(|r| r["assigned_thread_id"].is_null()).collect();
restore_rows(db, &rows)?;
Defensive patterns

Strategy: try-catch

Validate before calling

const assigned = db.prepare('SELECT COUNT(*) FROM agent_job_items WHERE id = ? AND assigned_thread_id IS NOT NULL').get(row.id);
if (assigned) throw new Error(`agent_job_items id ${row.id} already assigned; skipping`);

Try / catch

match restore_rows(db, rows) {
    Err(e) if e.to_string().contains("row already assigned") => log::warn!("skipping pre-assigned job items"),
    other => other?,
}

Prevention

When it happens

Trigger: Calling restore_rows (via restore_backups) where a backup row for an agent_job_items id maps to an assigned_thread_id, but the live database row with that id already has a different (non-NULL) assigned_thread_id.

Common situations: Restoring an older backup after jobs were reassigned in the live DB; merging backups from two machines where the same job item was assigned to different threads; concurrent job assignment happening while a restore runs.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of BigPizzaV3/CodexPlusPlus@b1ed92e5e4 (2026-09-19). Data as JSON: /api/errors/e970c1231dcfaf39. Report an issue: GitHub.

Appendix: source

Thrown at crates/codex-plus-data/src/storage.rs:1262

    let Some(id) = row.get("id") else {
        return Ok(false);
    };
    if !row.contains_key("assigned_thread_id") || !has_table(db, "agent_job_items")? {
        return Ok(false);
    }
    let id_value = OwnedSqlValue(json_to_sql_value(id));
    let current_assignment = db.query_row(
        "SELECT assigned_thread_id FROM agent_job_items WHERE id = ?1 LIMIT 1",
        [&id_value as &dyn ToSql],
        |row| row.get::<_, Option<String>>(0),
    );
    let current_assignment = match current_assignment {
        Ok(value) => value,
        Err(rusqlite::Error::QueryReturnedNoRows) => return Ok(false),
        Err(err) => return Err(err.into()),
    };
    if current_assignment.is_some() {
        anyhow::bail!("restore conflict: agent_job_items row already assigned");
    }
    let assigned = OwnedSqlValue(json_to_sql_value(&row["assigned_thread_id"]));
    db.execute(
        "UPDATE agent_job_items SET assigned_thread_id = ?1 WHERE id = ?2 AND assigned_thread_id IS NULL",
        [&assigned as &dyn ToSql, &id_value as &dyn ToSql],
    )?;
    Ok(true)
}

fn backup_related_rows(
    db: &Connection,
    tables: &mut Map<String, Value>,
    table: &str,
    where_clause: &str,
    params: &[&dyn ToSql],
) -> anyhow::Result<()> {
    if has_table(db, table)? {
        let rows = select_dicts(

View on GitHub (pinned to b1ed92e5e4)