Hmbown/CodeWhale · error
refusing to replace an existing xAI OAuth generation
Error message
refusing to replace an existing xAI OAuth generation
What it means
write_owned_file implements create-if-absent semantics for credential generations: it first opens the target name for read, and if a generation already exists it requires the caller to explicitly pass allow_replace=true. This prevents an accidental second persist call (e.g. a concurrent login or a retry) from silently overwriting valid stored credentials.
Solutions
- Pass allow_replace=true if overwriting the existing generation is intended
- Skip the write if a generation already exists (check via the store's read API first)
- Use the remove/rotate API to delete the old generation, then write the new one
Example fix
// before
store.write_owned_file("auth.json", &bytes, false)?;
// after (replacement intended)
store.write_owned_file("auth.json", &bytes, true)?; Defensive patterns
Strategy: try-catch
Validate before calling
fn generation_exists(store: &Store, name: &str) -> bool {
store.open_owned_file_for_read(name).map(|o| o.is_some()).unwrap_or(false)
} Try / catch
match store.write_owned_file("auth.json", &bytes, false) {
Err(e) if e.to_string().contains("refusing to replace") => {
// already stored: treat as success or opt in to replace
store.write_owned_file("auth.json", &bytes, true)?
}
r => r?,
} Prevention
- Call persist once per login; make retries idempotent (check before writing)
- Pass allow_replace=true explicitly when rotation is intended
- Serialize writes across processes with the lifecycle lock
When it happens
Trigger: Calling the store's write/persist API for a generation name that already exists in the credentials directory while allow_replace is false — e.g. calling save twice, or two processes racing to write the same generation.
Common situations: Double-invoking the login flow; a retry after a timeout that actually succeeded; a second Codewhale instance writing tokens while one is already stored.
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
- atomically replacing xAI OAuth credentials
- installing a new xAI OAuth generation without replacement
- refusing to replace an existing xAI OAuth retirement path
- a CNB access token is not configured in the Codewhale…
- agy OAuth token JSON carries no access token member
AI-assisted analysis of Hmbown/CodeWhale@73e0f67d83 (2026-09-22).
Data as JSON: /api/errors/e9ba9b0307ce9830.
Report an issue: GitHub.
Appendix: source
Thrown at crates/config/src/xai_credentials.rs:1091
})();
if let Err(error) = secured {
let cleanup = mark_windows_file_handle_for_deletion(&file);
return match cleanup {
Ok(()) => Err(error),
Err(cleanup) => Err(error).context(format!(
"also failed to delete the empty lifecycle lock: {cleanup:#}"
)),
};
}
return Ok(file);
}
bail!("xAI OAuth lifecycle lock changed repeatedly while opening")
}
fn write_owned_file(&self, name: &str, bytes: &[u8], allow_replace: bool) -> Result<()> {
let path = self.directory.join(name);
if let Some(existing) = self.open_owned_file_for_read(name)? {
anyhow::ensure!(
allow_replace,
"refusing to replace an existing xAI OAuth generation"
);
drop(existing);
}
let mut temporary = tempfile::NamedTempFile::new_in(&self.directory)
.context("creating private xAI OAuth temporary file")?;
let temporary_path = temporary.path().to_path_buf();
let security_handle =
reopen_windows_file_for_owner_security(temporary.as_file(), &temporary_path)?;
secure_windows_owner_only_handle(&security_handle, false)
.context("securing a new xAI OAuth temporary file before writing credentials")?;
validate_owned_file_handle(&security_handle, &temporary_path)
.context("verifying a new xAI OAuth temporary file before writing credentials")?;
let write_result = (|| -> Result<()> {
temporary
.write_all(bytes)
.context("writing xAI OAuth temporary file")?;View on GitHub (pinned to 73e0f67d83)