{"record":{"id":"9a9977f4d527e81f","repo":"gitbutlerapp/gitbutler","slug":"failed-to-assure-that-rn-is-in-the-workspace-as","errorCode":null,"errorMessage":"Failed to assure that {rn} is in the workspace as dependent branch after removing it","messagePattern":"Failed to assure that (.+?) is in the workspace as dependent branch after removing it","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/but-workspace/src/branch/apply.rs","lineNumber":694,"sourceCode":"                match ws_mut.insert_new_segment_above_anchor_if_not_present(\n                    rn.as_ref(),\n                    segment_to_insert_above,\n                ) {\n                    None => {\n                        // For now bail, until we know it's worth fixing this case automatically.\n                        bail!(\n                            \"Missing reference {segment_to_insert_above} which should be known to workspace metadata to serve as insertion position for {rn}\"\n                        );\n                    }\n                    Some(false) => {\n                        // The branch already existed, probably as stack, but it didn't come through. Remove it and use the anchor.\n                        ws_mut.remove_segment(rn.as_ref());\n                        if ws_mut.insert_new_segment_above_anchor_if_not_present(\n                            rn.as_ref(),\n                            segment_to_insert_above,\n                        ) != Some(true)\n                        {\n                            bail!(\n                                \"Failed to assure that {rn} is in the workspace as dependent branch after removing it\"\n                            );\n                        }\n                    }\n                    Some(true) => {}\n                }\n            } else {\n                bail!(\"Unexpectedly failed to find anchor for {rn} to make it a dependent branch\")\n            }\n        }\n\n        // Redo the merge, with the different stack configuration.\n        // Note that this is the exception, typically using stacks will be fine.\n        let existing_stacks_superseded_by_branch =\n            find_superseded_stacks(branch.as_ref(), &ws, &mut ws_md);\n        merge_result = WorkspaceCommit::from_new_merge_with_metadata(\n            filter_superseded_metadata_stacks(\n                ws_md.stacks.iter(),","sourceCodeStart":676,"sourceCodeEnd":712,"githubUrl":"https://github.com/gitbutlerapp/gitbutler/blob/58e5313667b857ef39a730e380af31816a7b1768/crates/but-workspace/src/branch/apply.rs#L676-L712","documentation":"After removing a conflicting segment, apply re-inserts the branch as a dependent branch above an anchor and requires `insert_new_segment_above_anchor_if_not_present` to return `Some(true)`. Any other result means the branch could not be established as a dependent branch, so apply fails to leave the workspace in a defined state.","triggerScenarios":"Apply flow on an adhoc workspace where, after `remove_segment`, the anchor for re-insertion is not found or the insert reports the branch still not added — a race between the removal and insertion or missing anchor in metadata.","commonSituations":"Same class as error 352: stale/incorrect workspace metadata during adhoc workspace apply, or a bug in the segment bookkeeping.","solutions":["Rebuild workspace metadata from the repository state and retry the apply","Retry the apply operation once metadata is consistent (no concurrent writers)","If reproducible, file a bug — this indicates the insert helper failed unexpectedly after a successful remove"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"match apply(...) {\n    Err(e) if e.to_string().contains(\"as dependent branch after removing\") => { rebuild_workspace(); retry(); }\n    Err(e) => return Err(e.into()),\n    Ok(r) => r,\n}","preventionTips":["Avoid concurrent operations on the same workspace","Refresh workspace state before applying into adhoc workspaces","Report reproducible failures as upstream bugs"],"tags":["git","workspace","invariant"],"backgroundTag":"internal-invariant-violation","analyzedSha":"58e5313667b857ef39a730e380af31816a7b1768","analyzedAt":"2026-09-18T06:50:32.052Z","contentChangedAt":"2026-09-18T06:50:32.052Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}