{"record":{"id":"2f77ac2a8afba550","repo":"Hmbown/CodeWhale","slug":"codewhale-owned-xai-oauth-file-is-not-valid-utf","errorCode":null,"errorMessage":"Codewhale-owned xAI OAuth file {} is not valid UTF-8","messagePattern":"Codewhale-owned xAI OAuth file (.+?) is not valid UTF-8","errorType":"validation","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/config/src/xai_credentials.rs","lineNumber":217,"sourceCode":"        let mut bytes = Vec::with_capacity(metadata.len() as usize);\n        (&mut file)\n            .take(XAI_OAUTH_FILE_LIMIT + 1)\n            .read_to_end(&mut bytes)\n            .with_context(|| {\n                format!(\n                    \"reading Codewhale-owned xAI OAuth file {}\",\n                    crate::quote_os_path(&self.directory.join(name))\n                )\n            })?;\n        if bytes.len() as u64 > XAI_OAUTH_FILE_LIMIT {\n            bail!(\n                \"Codewhale-owned xAI OAuth file {} exceeds the {} byte limit\",\n                crate::quote_os_path(&self.directory.join(name)),\n                XAI_OAUTH_FILE_LIMIT\n            );\n        }\n        String::from_utf8(bytes).map(Some).map_err(|_| {\n            anyhow::anyhow!(\n                \"Codewhale-owned xAI OAuth file {} is not valid UTF-8\",\n                crate::quote_os_path(&self.directory.join(name))\n            )\n        })\n    }\n\n    pub fn write(&self, name: &str, bytes: &[u8], allow_replace: bool) -> Result<()> {\n        validate_owned_auth_name(name)?;\n        anyhow::ensure!(\n            bytes.len() as u64 <= XAI_OAUTH_FILE_LIMIT,\n            \"refusing oversized xAI OAuth credential payload\"\n        );\n        self.write_owned_file(name, bytes, allow_replace)\n    }\n\n    pub fn remove(&self, name: &str) -> Result<bool> {\n        validate_owned_auth_name(name)?;\n        self.remove_raw(name)","sourceCodeStart":199,"sourceCodeEnd":235,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/8880682c63083a91624de936797efa3ce9e498fd/crates/config/src/xai_credentials.rs#L199-L235","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Re-authenticate: remove the named file (quoted in the error) or run the xAI logout flow so the next login recreates it","Identify what wrote non-UTF-8 data into the credentials directory and keep that directory Codewhale-only","If the file matters, inspect it with a hex editor to confirm corruption before deleting"],"exampleFix":"# before: corrupted file blocks xAI auth\n$ codewhale xai status   # ... is not valid UTF-8\n\n# after: clear the file quoted in the error and re-login\n$ rm \"<path-from-error-message>\" && codewhale xai login","handlingStrategy":"validation","validationCode":"// Pre-check the credential file is UTF-8 before the store reads it:\nfn credential_file_is_utf8(path: &std::path::Path) -> bool {\n    match std::fs::read(path) {\n        Ok(bytes) => std::str::from_utf8(&bytes).is_ok(),\n        Err(_) => false,\n    }\n}","typeGuard":null,"tryCatchPattern":"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.","preventionTips":["Keep the credentials directory exclusive to Codewhale; other writers are the usual culprit","Never edit token JSON with editors that save UTF-16 or add binary headers"],"tags":["xai","oauth","credentials","utf-8","corruption"],"backgroundTag":null,"analyzedSha":"8880682c63083a91624de936797efa3ce9e498fd","analyzedAt":"2026-08-16T11:31:27.956Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}