{"record":{"id":"afa9848cc0be914c","repo":"Hmbown/CodeWhale","slug":"segment-exists-or-was-inserted-above","errorCode":null,"errorMessage":"segment exists or was inserted above","messagePattern":"segment exists or was inserted above","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/config/src/config_document.rs","lineNumber":445,"sourceCode":"    root: &'a mut toml_edit::Table,\n    segments: &[&str],\n    lookup: PathLookup,\n) -> Result<Option<&'a mut dyn toml_edit::TableLike>> {\n    let mut current: &mut dyn toml_edit::TableLike = root;\n    for segment in segments {\n        if current.get(segment).is_none() {\n            match lookup {\n                PathLookup::Create => {\n                    let mut table = toml_edit::Table::new();\n                    table.set_implicit(true);\n                    current.insert(segment, toml_edit::Item::Table(table));\n                }\n                PathLookup::Existing => return Ok(None),\n            }\n        }\n        let item = current\n            .get_mut(segment)\n            .expect(\"segment exists or was inserted above\");\n        match item.as_table_like_mut() {\n            Some(table) => current = table,\n            None => match lookup {\n                PathLookup::Create => bail!(\"`{segment}` in config.toml must be a table\"),\n                PathLookup::Existing => return Ok(None),\n            },\n        }\n    }\n    Ok(Some(current))\n}\n\n#[cfg(test)]\nmod tests {\n    #[test]\n    fn healing_keeps_a_non_table_extras_key_it_cannot_lift() {\n        // `extras` is where the config structs flatten unknown keys, so a\n        // scalar or array under that exact name round-trips through the typed\n        // path as ordinary user data. Healing used to `remove()` it before","sourceCodeStart":427,"sourceCodeEnd":463,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/73e0f67d83c59909b571efdfc88c4bc28c309cb1/crates/config/src/config_document.rs#L427-L463","documentation":"While walking the config document, table_like_at_path_mut calls current.get_mut(segment) and expects the segment to be present, because earlier iterations either found it or inserted it (for PathLookup::Create). If traversal somehow reaches a missing segment, this expect fires — a structural invariant of the walk.","triggerScenarios":"table_like_at_path_mut (used by set_config_document_value and unset_config_document_value) reaching a get_mut(segment) that is None — possible only if the insert branch above it was skipped or a prior mutation removed the segment mid-walk.","commonSituations":"A regression in the insert logic for PathLookup::Create, or concurrent document mutation violating the lock discipline (the shared-lock test guards this). Not triggerable from config file contents.","solutions":["Inspect the Create/Existing handling just above: every segment must exist or be inserted before this line.","Fix the insertion branch so Create inserts missing intermediate tables.","Check for unsynchronized concurrent access to the same toml document."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"// internal invariant; surface as bug, not user error\nstd::panic::catch_unwind(|| doc.unset(&path))","preventionTips":["Test unset/set traversal over segments that do and do not exist.","Never mutate the toml document outside the documented lock discipline.","Preserve the insert-before-walk ordering when refactoring."],"tags":["internal","config","invariant"],"backgroundTag":"internal-invariant-violation","analyzedSha":"73e0f67d83c59909b571efdfc88c4bc28c309cb1","analyzedAt":"2026-09-22T01:30:00.501Z","contentChangedAt":"2026-09-22T01:30:00.501Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}