{"record":{"id":"e970c1231dcfaf39","repo":"BigPizzaV3/CodexPlusPlus","slug":"restore-conflict-agent-job-items-row-already-assi","errorCode":null,"errorMessage":"restore conflict: agent_job_items row already assigned","messagePattern":"restore conflict: agent_job_items row already assigned","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/codex-plus-data/src/storage.rs","lineNumber":1262,"sourceCode":"    let Some(id) = row.get(\"id\") else {\n        return Ok(false);\n    };\n    if !row.contains_key(\"assigned_thread_id\") || !has_table(db, \"agent_job_items\")? {\n        return Ok(false);\n    }\n    let id_value = OwnedSqlValue(json_to_sql_value(id));\n    let current_assignment = db.query_row(\n        \"SELECT assigned_thread_id FROM agent_job_items WHERE id = ?1 LIMIT 1\",\n        [&id_value as &dyn ToSql],\n        |row| row.get::<_, Option<String>>(0),\n    );\n    let current_assignment = match current_assignment {\n        Ok(value) => value,\n        Err(rusqlite::Error::QueryReturnedNoRows) => return Ok(false),\n        Err(err) => return Err(err.into()),\n    };\n    if current_assignment.is_some() {\n        anyhow::bail!(\"restore conflict: agent_job_items row already assigned\");\n    }\n    let assigned = OwnedSqlValue(json_to_sql_value(&row[\"assigned_thread_id\"]));\n    db.execute(\n        \"UPDATE agent_job_items SET assigned_thread_id = ?1 WHERE id = ?2 AND assigned_thread_id IS NULL\",\n        [&assigned as &dyn ToSql, &id_value as &dyn ToSql],\n    )?;\n    Ok(true)\n}\n\nfn backup_related_rows(\n    db: &Connection,\n    tables: &mut Map<String, Value>,\n    table: &str,\n    where_clause: &str,\n    params: &[&dyn ToSql],\n) -> anyhow::Result<()> {\n    if has_table(db, table)? {\n        let rows = select_dicts(","sourceCodeStart":1244,"sourceCodeEnd":1280,"githubUrl":"https://github.com/BigPizzaV3/CodexPlusPlus/blob/b1ed92e5e4a2d74095d4b8db5af43cef7acba9c6/crates/codex-plus-data/src/storage.rs#L1244-L1280","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Accept the current assignment and exclude that agent_job_items row from the restore, or restore only rows whose assigned_thread_id is NULL.","Reassign or clear assigned_thread_id on the conflicting live rows if the backup assignment is authoritative, then re-run.","Restore into a fresh database where the row doesn't yet exist.","If this aborts an entire restore, serialize the restore so no job scheduler mutates assignments concurrently."],"exampleFix":"// before: blanket restore of all rows\nrestore_rows(db, backup_rows)?; // row 42 already assigned to thread B, backup says thread A\n// after: skip already-assigned rows\nlet rows: Vec<_> = backup_rows.into_iter().filter(|r| r[\"assigned_thread_id\"].is_null()).collect();\nrestore_rows(db, &rows)?;\n","handlingStrategy":"try-catch","validationCode":"const assigned = db.prepare('SELECT COUNT(*) FROM agent_job_items WHERE id = ? AND assigned_thread_id IS NOT NULL').get(row.id);\nif (assigned) throw new Error(`agent_job_items id ${row.id} already assigned; skipping`);","typeGuard":null,"tryCatchPattern":"match restore_rows(db, rows) {\n    Err(e) if e.to_string().contains(\"row already assigned\") => log::warn!(\"skipping pre-assigned job items\"),\n    other => other?,\n}","preventionTips":["Filter backup rows to unassigned (assigned_thread_id IS NULL) before restoring.","Pause job schedulers during restore to avoid concurrent assignment.","Restore into a fresh DB when the backup's assignments should win."],"tags":["restore","sqlite","conflict","data-integrity"],"backgroundTag":"invalid-state-transition","analyzedSha":"b1ed92e5e4a2d74095d4b8db5af43cef7acba9c6","analyzedAt":"2026-09-19T23:35:21.129Z","contentChangedAt":"2026-09-19T23:35:21.129Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}