Hmbown/CodeWhale · error · anyhow::Error

Codewhale-owned xAI OAuth file {} is not valid UTF-8

Error message

Codewhale-owned xAI OAuth file {} is not valid UTF-8

What it means

XaiOAuthCredentialStore's reader loads a Codewhale-owned OAuth file (after the 1 MiB size check) and decodes it as UTF-8 text, since these files are JSON documents. This error means the bytes are not valid UTF-8: corruption or foreign binary content inside the credentials directory. The offending path is quoted in the message.

Source

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

        let mut bytes = Vec::with_capacity(metadata.len() as usize);
        (&mut file)
            .take(XAI_OAUTH_FILE_LIMIT + 1)
            .read_to_end(&mut bytes)
            .with_context(|| {
                format!(
                    "reading Codewhale-owned xAI OAuth file {}",
                    crate::quote_os_path(&self.directory.join(name))
                )
            })?;
        if bytes.len() as u64 > XAI_OAUTH_FILE_LIMIT {
            bail!(
                "Codewhale-owned xAI OAuth file {} exceeds the {} byte limit",
                crate::quote_os_path(&self.directory.join(name)),
                XAI_OAUTH_FILE_LIMIT
            );
        }
        String::from_utf8(bytes).map(Some).map_err(|_| {
            anyhow::anyhow!(
                "Codewhale-owned xAI OAuth file {} is not valid UTF-8",
                crate::quote_os_path(&self.directory.join(name))
            )
        })
    }

    pub fn write(&self, name: &str, bytes: &[u8], allow_replace: bool) -> Result<()> {
        validate_owned_auth_name(name)?;
        anyhow::ensure!(
            bytes.len() as u64 <= XAI_OAUTH_FILE_LIMIT,
            "refusing oversized xAI OAuth credential payload"
        );
        self.write_owned_file(name, bytes, allow_replace)
    }

    pub fn remove(&self, name: &str) -> Result<bool> {
        validate_owned_auth_name(name)?;
        self.remove_raw(name)

View on GitHub (pinned to 8880682c63)

Solutions

  1. Re-authenticate: remove the named file (quoted in the error) or run the xAI logout flow so the next login recreates it
  2. Identify what wrote non-UTF-8 data into the credentials directory and keep that directory Codewhale-only
  3. If the file matters, inspect it with a hex editor to confirm corruption before deleting

Example fix

# before: corrupted file blocks xAI auth
$ codewhale xai status   # ... is not valid UTF-8

# after: clear the file quoted in the error and re-login
$ rm "<path-from-error-message>" && codewhale xai login
Defensive patterns

Strategy: validation

Validate before calling

// Pre-check the credential file is UTF-8 before the store reads it:
fn credential_file_is_utf8(path: &std::path::Path) -> bool {
    match std::fs::read(path) {
        Ok(bytes) => std::str::from_utf8(&bytes).is_ok(),
        Err(_) => false,
    }
}

Try / catch

On Err, prompt re-authentication (delete the quoted file or log out) instead of retrying the read; corrupted bytes will not decode on a second attempt. Keep the quoted path in user-facing output so the fix is actionable.

Prevention

When it happens

Trigger: Reading a named file under the xAI OAuth credentials directory that contains non-UTF-8 bytes: disk corruption, a partial write after a crash, another program overwriting the file, or an editor saving as UTF-16.

Common situations: Power loss mid-write leaving a truncated or binary blob; sync/backup tools mangling the file; users hand-editing token JSON with a tool that writes a non-UTF-8 encoding.

Related errors


AI-assisted analysis of Hmbown/CodeWhale@8880682c63 (2026-08-16). Data as JSON: /api/errors/2f77ac2a8afba550. Report an issue: GitHub.