{"record":{"id":"bea83247d8394810","repo":"Hmbown/CodeWhale","slug":"refusing-to-replace-an-existing-xai-oauth-retireme","errorCode":null,"errorMessage":"refusing to replace an existing xAI OAuth retirement path","messagePattern":"refusing to replace an existing xAI OAuth retirement path","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/config/src/xai_credentials.rs","lineNumber":869,"sourceCode":"        if unsafe { libc::unlinkat(self.directory_handle.as_raw_fd(), name.as_ptr(), 0) } != 0 {\n            let error = std::io::Error::last_os_error();\n            if error.kind() == std::io::ErrorKind::NotFound {\n                return Ok(false);\n            }\n            return Err(error).context(\"removing Codewhale-owned xAI OAuth file\");\n        }\n        Ok(true)\n    }\n\n    fn rename_raw(&self, from: &str, to: &str) -> Result<()> {\n        use std::os::fd::AsRawFd as _;\n        validate_private_basename(from)?;\n        validate_private_basename(to)?;\n        let source = self\n            .open_owned_file_for_read(from)?\n            .context(\"xAI OAuth source disappeared before retirement\")?;\n        validate_owned_file_handle(&source, &self.directory.join(from))?;\n        anyhow::ensure!(\n            self.open_owned_file_for_read(to)?.is_none(),\n            \"refusing to replace an existing xAI OAuth retirement path\"\n        );\n        drop(source);\n        let from = CString::new(from).context(\"xAI OAuth basename contains an interior NUL\")?;\n        let to = CString::new(to).context(\"xAI OAuth basename contains an interior NUL\")?;\n        // SAFETY: both names are one component relative to the same pinned\n        // directory descriptor.\n        if unsafe {\n            libc::renameat(\n                self.directory_handle.as_raw_fd(),\n                from.as_ptr(),\n                self.directory_handle.as_raw_fd(),\n                to.as_ptr(),\n            )\n        } != 0\n        {\n            return Err(std::io::Error::last_os_error()).context(\"retiring xAI OAuth file\");","sourceCodeStart":851,"sourceCodeEnd":887,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/73e0f67d83c59909b571efdfc88c4bc28c309cb1/crates/config/src/xai_credentials.rs#L851-L887","documentation":"rename_raw refuses to overwrite: before renameat2-ing `from` to `to` (the retirement/rollback path), it verifies the destination does not already exist. This prevents silently destroying an existing retired credential generation. Rename is only a move, never a replace.","triggerScenarios":"Calling stage_revocation or rollback when the destination retirement basename already exists in the store — e.g. rolling back twice, or staging revocation for a generation that was already retired.","commonSituations":"A retry after a partial rollback left the retirement file in place; two processes racing to retire the same generation; stale state from a previous version using the same retirement names.","solutions":["Check for and remove the existing retirement file first (via the store's remove API) if it is safe to discard.","Use a unique retirement name (e.g. include a timestamp/nonce) so the destination never collides.","Make the operation idempotent: if the destination already exists with identical content, treat the rollback as done.","Serialize retirement operations with a lock to avoid races."],"exampleFix":"// before\nstore.rename_raw(name, retirement_name)?; // fails if retirement_name exists\n// after\nif store.open_at(retirement_name)?.is_some() {\n    store.remove_raw(retirement_name)?;\n}\nstore.rename_raw(name, retirement_name)?;","handlingStrategy":"try-catch","validationCode":"fn retirement_free(store: &XaiOAuthCredentialStore, to: &str) -> bool {\n    store.open_at(to).map(|o| o.is_none()).unwrap_or(false)\n}","typeGuard":null,"tryCatchPattern":"match store.rename_raw(from, to) {\n    Err(e) if e.to_string().contains(\"refusing to replace\") => {\n        // destination exists: make idempotent or choose a unique name\n        handle_already_retired(from, to)?;\n    }\n    r => r?,\n}","preventionTips":["Include a timestamp/nonce in retirement names","Make rollback idempotent when the retirement file already matches","Lock retirement operations against concurrent runs"],"tags":["file-already-exists","rename","credentials","conflict"],"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"}