{"record":{"id":"28dd09aac7975139","repo":"Hmbown/CodeWhale","slug":"flat-cache-populated-above","errorCode":null,"errorMessage":"flat cache populated above","messagePattern":"flat cache populated above","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/tui/src/tui/live_transcript.rs","lineNumber":283,"sourceCode":"    /// render options — all of which are in `FlatKey`.\n    fn flattened(&self, width: u16) -> std::cell::Ref<'_, FlattenedTranscript> {\n        let key = FlatKey {\n            generation: self.snapshots_generation.get(),\n            width: width.max(1),\n            mode: self.mode,\n            options: self.options,\n        };\n        {\n            let mut cache = self.flat_cache.borrow_mut();\n            let stale = cache.as_ref().is_none_or(|cached| cached.key != key);\n            if stale {\n                let flat = self.flatten(key.width);\n                self.flattens.set(self.flattens.get() + 1);\n                *cache = Some(FlatCache { key, flat });\n            }\n        }\n        std::cell::Ref::map(self.flat_cache.borrow(), |cache| {\n            &cache.as_ref().expect(\"flat cache populated above\").flat\n        })\n    }\n\n    fn flatten(&self, width: u16) -> FlattenedTranscript {\n        let width = width.max(1);\n        let mut out: Vec<Line<'static>> = Vec::new();\n        let mut out_links: Vec<Vec<crate::tui::osc8::LineLink>> = Vec::new();\n        let mut highlighted_range = None;\n\n        // Pre-compute which cell index (in `self.snapshots`) is the one\n        // the user has selected via Esc-Esc. We walk snapshots backwards\n        // counting User cells; the snapshot index whose count matches\n        // `selected_idx + 1` is the highlighted one.\n        let highlighted_cell_idx: Option<usize> = match self.mode {\n            Mode::BacktrackPreview { selected_idx } => {\n                let mut count = 0usize;\n                let mut hit = None;\n                for (idx, snap) in self.snapshots.iter().enumerate().rev() {","sourceCodeStart":265,"sourceCodeEnd":301,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/0c42157ee52f9d55af2b506d71b46249910f77d3/crates/tui/src/tui/live_transcript.rs#L265-L301","documentation":"flattened() recomputes the flattened transcript when the FlatKey (generation/width/mode/options) is stale, stores it in flat_cache, and then borrows it. The expect fires only if the cache was not populated by the preceding stale-check block — i.e. a refactor moved or skipped the store step — since the very code above guarantees a fresh entry exists.","triggerScenarios":"Thrown at crates/tui/src/tui/live_transcript.rs:283 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Keep the borrow_mut store and the Ref borrow inside flattened() so no caller can observe an empty cache","Invalidate by bumping snapshots_generation rather than clearing flat_cache directly","If RefCell borrow conflicts arise, restructure rather than leaving the cache empty"],"exampleFix":null,"handlingStrategy":"type-guard","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"0c42157ee52f9d55af2b506d71b46249910f77d3","analyzedAt":"2026-08-20T21:50:45.477Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}