{"record":{"id":"36358693c09f680e","repo":"astrid-runtime/astrid","slug":"percentile-index-is-outside-the-distribution","errorCode":null,"errorMessage":"percentile index is outside the distribution","messagePattern":"percentile index is outside the distribution","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/astrid-storage-chunker-evidence/src/metrics.rs","lineNumber":270,"sourceCode":"    let total = sorted.iter().try_fold(0_u64, |count, (_, weight)| {\n        checked_add(count, *weight, \"chunk count\")\n    })?;\n    let last = total\n        .checked_sub(1)\n        .ok_or_else(|| anyhow::anyhow!(\"chunk distribution is empty\"))?;\n    let index = last\n        .checked_mul(percentile)\n        .and_then(|value| value.checked_add(99))\n        .ok_or_else(|| anyhow::anyhow!(\"percentile index overflow\"))?\n        / 100;\n    let mut cumulative = 0_u64;\n    for (length, weight) in sorted {\n        cumulative = checked_add(cumulative, *weight, \"chunk count\")?;\n        if cumulative > index {\n            return Ok(*length);\n        }\n    }\n    bail!(\"percentile index is outside the distribution\")\n}\n\nfn checked_add(left: u64, right: u64, label: &str) -> Result<u64> {\n    left.checked_add(right)\n        .ok_or_else(|| anyhow::anyhow!(\"{label} overflow\"))\n}\n\n#[cfg(test)]\nmod tests {\n    use super::*;\n\n    #[test]\n    fn dedup_ratios_are_exact_basis_points() {\n        assert_eq!(\n            deduplication(1_000, 471).unwrap(),\n            Deduplication {\n                retained_bytes: 471,\n                saved_bytes: 529,","sourceCodeStart":252,"sourceCodeEnd":288,"githubUrl":"https://github.com/astrid-runtime/astrid/blob/affd8760f44190dbdfbec23403f4c4b642c33112/crates/astrid-storage-chunker-evidence/src/metrics.rs#L252-L288","documentation":"percentile walks the sorted (length, weight) pairs accumulating weights until the target index is passed; if the cumulative weight never exceeds the index, the requested percentile lies outside the distribution, indicating internal inconsistency between the index computation and the distribution data.","triggerScenarios":"Requesting a percentile whose index >= total weighted chunk count — caused by weighted_count/total drift, empty weight sums, or corrupted distribution pairs.","commonSituations":"Integer truncation bugs in index computation; distribution data modified after count was computed; metrics built from a corpus that changed mid-measurement.","solutions":["Recompute the distribution and percentiles from the same immutable snapshot of data.","Verify the percentile index is derived from the same weighted_count used to build the distribution.","Report a bug if it occurs with the library's own percentile targets on unmodified data."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"fn index_in_range(index: u64, dist: &[(u64, u64)]) -> bool {\n    let total: u64 = dist.iter().map(|(_, w)| *w).sum();\n    index < total\n}","typeGuard":null,"tryCatchPattern":"match dist.percentile(p) {\n    Err(e) if e.to_string().contains(\"outside the distribution\") => recompute_distribution(),\n    other => other,\n}","preventionTips":["Compute percentile indices from the same weighted_count as the distribution","Never mutate distribution data after computing indices","Freeze the measurement snapshot before computing statistics"],"tags":["metrics","statistics","index-out-of-range"],"backgroundTag":"index-out-of-range","analyzedSha":"affd8760f44190dbdfbec23403f4c4b642c33112","analyzedAt":"2026-09-09T21:28:12.402Z","contentChangedAt":"2026-09-09T21:28:12.402Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}