Hmbown/CodeWhale · error

refusing to replace an existing xAI OAuth retirement path

Error message

refusing to replace an existing xAI OAuth retirement path

What it means

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.

Solutions

  1. Check for and remove the existing retirement file first (via the store's remove API) if it is safe to discard.
  2. Use a unique retirement name (e.g. include a timestamp/nonce) so the destination never collides.
  3. Make the operation idempotent: if the destination already exists with identical content, treat the rollback as done.
  4. Serialize retirement operations with a lock to avoid races.

Example fix

// before
store.rename_raw(name, retirement_name)?; // fails if retirement_name exists
// after
if store.open_at(retirement_name)?.is_some() {
    store.remove_raw(retirement_name)?;
}
store.rename_raw(name, retirement_name)?;
Defensive patterns

Strategy: try-catch

Validate before calling

fn retirement_free(store: &XaiOAuthCredentialStore, to: &str) -> bool {
    store.open_at(to).map(|o| o.is_none()).unwrap_or(false)
}

Try / catch

match store.rename_raw(from, to) {
    Err(e) if e.to_string().contains("refusing to replace") => {
        // destination exists: make idempotent or choose a unique name
        handle_already_retired(from, to)?;
    }
    r => r?,
}

Prevention

When it happens

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

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

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/bea83247d8394810. Report an issue: GitHub.

Appendix: source

Thrown at crates/config/src/xai_credentials.rs:869

        if unsafe { libc::unlinkat(self.directory_handle.as_raw_fd(), name.as_ptr(), 0) } != 0 {
            let error = std::io::Error::last_os_error();
            if error.kind() == std::io::ErrorKind::NotFound {
                return Ok(false);
            }
            return Err(error).context("removing Codewhale-owned xAI OAuth file");
        }
        Ok(true)
    }

    fn rename_raw(&self, from: &str, to: &str) -> Result<()> {
        use std::os::fd::AsRawFd as _;
        validate_private_basename(from)?;
        validate_private_basename(to)?;
        let source = self
            .open_owned_file_for_read(from)?
            .context("xAI OAuth source disappeared before retirement")?;
        validate_owned_file_handle(&source, &self.directory.join(from))?;
        anyhow::ensure!(
            self.open_owned_file_for_read(to)?.is_none(),
            "refusing to replace an existing xAI OAuth retirement path"
        );
        drop(source);
        let from = CString::new(from).context("xAI OAuth basename contains an interior NUL")?;
        let to = CString::new(to).context("xAI OAuth basename contains an interior NUL")?;
        // SAFETY: both names are one component relative to the same pinned
        // directory descriptor.
        if unsafe {
            libc::renameat(
                self.directory_handle.as_raw_fd(),
                from.as_ptr(),
                self.directory_handle.as_raw_fd(),
                to.as_ptr(),
            )
        } != 0
        {
            return Err(std::io::Error::last_os_error()).context("retiring xAI OAuth file");

View on GitHub (pinned to 73e0f67d83)