{"record":{"id":"dc48d457f7ad7e7b","repo":"affaan-m/ECC","slug":"candidate-alias-collision-with-physical-candidate","errorCode":null,"errorMessage":"candidate alias collision with physical candidate id","messagePattern":"candidate alias collision with physical candidate id","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"ecc2/src/session/store.rs","lineNumber":5307,"sourceCode":"    }\n}\n\nimpl StateStore {\n    fn register_harness_alias(\n        tx: &rusqlite::Transaction<'_>,\n        alias_id: &str,\n        candidate_id: &str,\n    ) -> Result<()> {\n        if tx\n            .query_row(\n                \"SELECT 1 FROM harness_candidates WHERE id = ?1\",\n                [alias_id],\n                |_| Ok(()),\n            )\n            .optional()?\n            .is_some()\n        {\n            anyhow::bail!(\"candidate alias collision with physical candidate id\");\n        }\n        let existing = tx\n            .query_row(\n                \"SELECT candidate_id FROM harness_candidate_aliases WHERE alias_id = ?1\",\n                [alias_id],\n                |row| row.get::<_, String>(0),\n            )\n            .optional()?;\n        if let Some(existing) = existing {\n            if existing != candidate_id {\n                anyhow::bail!(\"candidate alias collision with different immutable target\");\n            }\n            return Ok(());\n        }\n        tx.execute(\n            \"INSERT INTO harness_candidate_aliases (alias_id, candidate_id, id_version, created_at) VALUES (?1, ?2, 2, ?3)\",\n            rusqlite::params![alias_id, candidate_id, chrono::Utc::now().to_rfc3339()],\n        )?;","sourceCodeStart":5289,"sourceCodeEnd":5325,"githubUrl":"https://github.com/affaan-m/ECC/blob/01e15490f04e29cfefe3896951f43db46994d8ee/ecc2/src/session/store.rs#L5289-L5325","documentation":"Thrown by register_harness_alias when the proposed alias_id is already present as a physical candidate id in the harness_candidates table. The store forbids an alias from shadowing a real candidate id so the id namespace stays unambiguous: a lookup of alias_id must not silently resolve to two different things.","triggerScenarios":"Calling register_harness_alias(tx, alias_id, candidate_id) where SELECT 1 FROM harness_candidates WHERE id = alias_id returns a row. Typical when a legacy id being registered as an alias happens to equal an already-stored v2 content-addressed candidate id.","commonSituations":"Migrating a legacy id scheme where a legacy id was itself promoted to a v2 candidate id; reusing one 64-char hex value as both a physical candidate and an alias target; two subsystems deriving the same digest for different purposes.","solutions":["Pick an alias_id that is provably not already a physical candidate id (query harness_candidates first).","If the alias_id must equal that value, drop or rename the physical candidate row before registering the alias.","Namespace aliases distinctly from physical ids (e.g. a prefix) so the two sets cannot overlap by construction."],"exampleFix":"// before\nstore.register_harness_alias(&tx, &legacy_id, &candidate.id)?;\n\n// after\nlet taken: Option<i64> = tx.query_row(\n    \"SELECT 1 FROM harness_candidates WHERE id = ?1\",\n    [&legacy_id], |_| Ok(1)).optional()?;\nif taken.is_some() {\n    anyhow::bail!(\"refusing to alias legacy_id {legacy_id}: it is a physical candidate id\");\n}\nstore.register_harness_alias(&tx, &legacy_id, &candidate.id)?;","handlingStrategy":"validation","validationCode":"fn alias_is_physical_candidate(tx: &rusqlite::Transaction, alias_id: &str) -> Result<bool> {\n    let exists: Option<i64> = tx.query_row(\n        \"SELECT 1 FROM harness_candidates WHERE id = ?1\",\n        [alias_id], |_| Ok(1)).optional()?;\n    Ok(exists.is_some())\n}\n\n// before register_harness_alias:\nif alias_is_physical_candidate(&tx, alias_id)? {\n    return Err(anyhow::anyhow!(\"alias_id {alias_id} is a physical candidate id; choose a distinct alias\"));\n}","typeGuard":"fn safe_alias_id(tx: &rusqlite::Transaction, alias_id: &str) -> bool {\n    !tx.query_row(\"SELECT 1 FROM harness_candidates WHERE id = ?1\", [alias_id], |_| Ok(1))\n        .optional().ok().flatten().is_some()\n}","tryCatchPattern":"match store.register_harness_alias(&tx, alias_id, candidate_id) {\n    Ok(()) => { /* registered */ }\n    Err(e) if e.to_string().contains(\"alias collision with physical candidate id\") => {\n        // choose a different alias_id and retry, or surface to caller\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["Keep alias ids and physical candidate ids in disjoint namespaces (e.g. prefix aliases) so they can never collide by construction.","When migrating legacy ids, cross-check each legacy id against harness_candidates before registering it as an alias.","Add a unique constraint or trigger so the DB itself rejects an alias equal to a physical candidate id."],"tags":["rust","sqlite","harness","alias","integrity"],"backgroundTag":null,"analyzedSha":"01e15490f04e29cfefe3896951f43db46994d8ee","analyzedAt":"2026-08-13T00:31:08.655Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}