{"record":{"id":"41ab077ec2130576","repo":"sinelaw/fresh","slug":"buffer-not-found","errorCode":null,"errorMessage":"buffer not found","messagePattern":"buffer not found","errorType":"exception","errorClass":"io::Error (NotFound)","httpStatus":null,"severity":"error","filePath":"crates/fresh-editor-core/src/model/buffer/mod.rs","lineNumber":2157,"sourceCode":"            &**self.persistence.fs(),\n            pattern,\n            opts,\n            &regex,\n            max_matches,\n            query_len,\n        )\n    }\n\n    /// Count `\\n` bytes in a single leaf.\n    ///\n    /// Uses `count_line_feeds_in_range` for unloaded buffers, which remote\n    /// filesystem implementations can override to count server-side.\n    pub fn scan_leaf(&self, leaf: &crate::model::piece_tree::LeafData) -> std::io::Result<usize> {\n        let buffer_id = leaf.location.buffer_id();\n        let buffer = self\n            .buffers\n            .get(buffer_id)\n            .ok_or_else(|| std::io::Error::new(std::io::ErrorKind::NotFound, \"buffer not found\"))?;\n\n        let count = match &buffer.data {\n            crate::model::piece_tree::BufferData::Loaded { data, .. } => {\n                let end = (leaf.offset + leaf.bytes).min(data.len());\n                data[leaf.offset..end]\n                    .iter()\n                    .filter(|&&b| b == b'\\n')\n                    .count()\n            }\n            crate::model::piece_tree::BufferData::Unloaded {\n                file_path,\n                file_offset,\n                ..\n            } => {\n                let read_offset = *file_offset as u64 + leaf.offset as u64;\n                self.persistence.fs().count_line_feeds_in_range(\n                    file_path,\n                    read_offset,","sourceCodeStart":2139,"sourceCodeEnd":2175,"githubUrl":"https://github.com/sinelaw/fresh/blob/67894ca5463dbd7a89bb31add4627c27d6b79d83/crates/fresh-editor-core/src/model/buffer/mod.rs#L2139-L2175","documentation":"BufferMap::scan_leaf fails when a piece-tree leaf references a buffer_id that is absent from the buffers map. This is an internal consistency failure: leaves should never point at buffers that have been removed. It surfaces during byte-counting/scanning of leaf data.","triggerScenarios":"Calling scan_leaf on a LeafData whose location.buffer_id() was never registered or was already removed from self.buffers (e.g. scanning after buffer close/eviction).","commonSituations":"Holding stale leaf references after closing a buffer; background scanning tasks racing with buffer removal.","solutions":["Ensure leaves are invalidated/removed when their buffer is deleted from the map.","Check the buffer_id is registered before scanning.","Treat as an invariant violation and fix the ownership/lifecycle code that drops buffers with live leaves."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// guard before scanning\nif !buffers.contains_key(leaf.location.buffer_id()) {\n    // leaf is stale; skip or rebuild\n}","typeGuard":null,"tryCatchPattern":"match map.scan_leaf(&leaf) {\n    Ok(count) => count,\n    Err(e) if e.to_string() == \"buffer not found\" => {\n        // stale leaf: drop it and rescan\n        0\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["Invalidate leaves when removing buffers","Verify buffer registration before scanning","Add a debug assertion that leaf buffer_ids exist in the map"],"tags":["internal","buffers","io"],"backgroundTag":"internal-invariant-violation","analyzedSha":"67894ca5463dbd7a89bb31add4627c27d6b79d83","analyzedAt":"2026-09-13T15:04:03.701Z","contentChangedAt":"2026-09-13T15:04:03.701Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}