{"record":{"id":"d8cffb2e902cae8d","repo":"BoundaryML/baml","slug":"invalid-profiling-usage-ledger","errorCode":null,"errorMessage":"invalid profiling usage ledger","messagePattern":"invalid profiling usage ledger","errorType":"validation","errorClass":"io::Error","httpStatus":null,"severity":"error","filePath":"baml_language/crates/bex_prof_store/src/prof/backend/store.rs","lineNumber":1657,"sourceCode":"                    io::ErrorKind::InvalidData,\n                    \"profiling store contains an unsupported file type\",\n                ));\n            }\n        }\n        Ok(())\n    }\n\n    let mut total = USAGE_STATE_BYTES;\n    scan(root, root, &mut total)?;\n    Ok(total)\n}\n\nfn read_usage_state(root: &Path) -> io::Result<u64> {\n    let usage_state_len = usize::try_from(USAGE_STATE_BYTES).expect(\"fixed usage state fits usize\");\n    let mut bytes = Vec::with_capacity(usage_state_len);\n    File::open(root.join(\"usage.state\"))?.read_to_end(&mut bytes)?;\n    if bytes.len() != usage_state_len || &bytes[..8] != USAGE_MAGIC {\n        return Err(io::Error::new(\n            io::ErrorKind::InvalidData,\n            \"invalid profiling usage ledger\",\n        ));\n    }\n    let expected: [u8; 32] = Sha256::digest(&bytes[..16]).into();\n    if bytes[16..] != expected {\n        return Err(io::Error::new(\n            io::ErrorKind::InvalidData,\n            \"profiling usage ledger checksum mismatch\",\n        ));\n    }\n    Ok(u64::from_be_bytes(\n        bytes[8..16].try_into().expect(\"fixed-width usage\"),\n    ))\n}\n\nfn write_usage_state(root: &Path, usage: u64, platform: &dyn StorePlatform) -> io::Result<()> {\n    let usage_state_len = usize::try_from(USAGE_STATE_BYTES).expect(\"fixed usage state fits usize\");","sourceCodeStart":1639,"sourceCodeEnd":1675,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/baml_language/crates/bex_prof_store/src/prof/backend/store.rs#L1639-L1675","documentation":"read_usage_state loads the store's `usage.state` ledger and requires it to be exactly USAGE_STATE_BYTES long and to begin with the 8-byte USAGE_MAGIC. If the length or magic check fails, it raises InvalidData 'invalid profiling usage ledger' because the file is not a ledger this store version wrote.","triggerScenarios":"Opening/reading usage accounting when usage.state is missing bytes (truncated/empty), longer than expected, was created by a different tool/version, or starts with bytes other than USAGE_MAGIC.","commonSituations":"Manual edits to usage.state; crash during the first write leaving a zero-length file; store created by an older/newer version with a different layout; copying a partial file; a user placing an unrelated file named usage.state in the store root.","solutions":["Delete usage.state and let the store rebuild/reinitialize it (usage will be re-estimated from the segment scan).","Restore usage.state from a backup of the profiling store taken while the process was quiescent.","Verify you are opening the store with the same library version that created it (ledger layout/magic may differ across versions).","Recreate the profiling store directory if its contents are expendable."],"exampleFix":"// shell\n// before: usage.state truncated/corrupt\n// after\nrm <store-root>/usage.state  # store reinitializes the ledger on next open","handlingStrategy":"validation","validationCode":"let path = root.join(\"usage.state\");\nif !path.exists() || fs::metadata(&path)?.len() != USAGE_STATE_BYTES as u64 {\n    // ledger missing or wrong size: delete it and let the store reinitialize\n    let _ = fs::remove_file(&path);\n}","typeGuard":null,"tryCatchPattern":"match read_usage_state(&root) {\n    Ok(usage) => usage,\n    Err(e) if e.kind() == io::ErrorKind::InvalidData && e.to_string().contains(\"invalid profiling usage ledger\") => {\n        let _ = fs::remove_file(root.join(\"usage.state\"));\n        rebuild_usage_from_segments(&root)\n    }\n    Err(e) => return Err(e.into()),\n}","preventionTips":["Never hand-edit usage.state","Open the store only with the library version that created it","Avoid crashes during first-time initialization; let the store create its ledger"],"tags":["rust","io","corruption","ledger","profiling"],"backgroundTag":"schema-validation-failed","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}