{"record":{"id":"c082538ecce1b65e","repo":"gitbutlerapp/gitbutler","slug":"found-gitbutler-managed-block-markers-in-the-wrong-order-end","errorCode":null,"errorMessage":"Found GitButler managed block markers in the wrong order (end before start). Refusing to edit a malformed managed block.","messagePattern":"Found GitButler managed block markers in the wrong order \\(end before start\\)\\. Refusing to edit a malformed managed block\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/but/src/command/agent/files.rs","lineNumber":109,"sourceCode":"    } else {\n        block.to_string()\n    }\n}\n\npub(super) fn upsert_managed_block(existing: &str, block: &str) -> Result<String> {\n    let start = find_line_anchored(existing, MANAGED_BLOCK_START, 0);\n    let end = find_line_anchored(existing, MANAGED_BLOCK_END, 0);\n\n    match (start, end) {\n        // No managed block yet: append a fresh one.\n        (None, None) => return Ok(append_managed_block(existing, block)),\n        (Some(_), None) => anyhow::bail!(\n            \"Found only the GitButler managed block start marker. Refusing to edit a partial managed block.\"\n        ),\n        (None, Some(_)) => anyhow::bail!(\n            \"Found only the GitButler managed block end marker. Refusing to edit a partial managed block.\"\n        ),\n        (Some(start), Some(end)) if end < start => anyhow::bail!(\n            \"Found GitButler managed block markers in the wrong order (end before start). Refusing to edit a malformed managed block.\"\n        ),\n        // Well-formed: a start marker with an end after it. Fall through to replace.\n        (Some(_), Some(_)) => {}\n    }\n\n    // Replace the first block with the fresh one and drop any extra blocks\n    // (e.g. left over from an earlier buggy run), so the file converges to\n    // exactly one block.\n    let mut spans = Vec::new();\n    for span in managed_block_spans(existing)? {\n        let mut block_end = span.end;\n        if existing[block_end..].starts_with(\"\\r\\n\") {\n            block_end += 2;\n        } else if existing[block_end..].starts_with('\\n') {\n            block_end += 1;\n        }\n        spans.push((span.start, block_end));","sourceCodeStart":91,"sourceCodeEnd":127,"githubUrl":"https://github.com/gitbutlerapp/gitbutler/blob/58e5313667b857ef39a730e380af31816a7b1768/crates/but/src/command/agent/files.rs#L91-L127","documentation":"Both managed block markers exist but the end marker appears before the start marker, so the block is malformed. The tool bails out instead of replacing what it cannot safely interpret as a contiguous block.","triggerScenarios":"upsert_managed_block finds MANAGED_BLOCK_END at a line index earlier than MANAGED_BLOCK_START — e.g. duplicated blocks after manual editing, reordered lines, or a botched merge that swapped marker order.","commonSituations":"Users rearranging their instructions file; copy-pasting blocks out of order; merge conflicts resolved by keeping marker lines in the wrong sequence.","solutions":["Open the file and reorder markers so the start marker precedes the end marker.","Remove all markers and let the upsert append one fresh, well-ordered block.","Verify only one start/end pair exists, then re-run the upsert."],"exampleFix":"// before\n<!-- GITBUTLER-MANAGED-BLOCK:END -->\n...content...\n<!-- GITBUTLER-MANAGED-BLOCK:START -->\n// after\n<!-- GITBUTLER-MANAGED-BLOCK:START -->\n...content...\n<!-- GITBUTLER-MANAGED-BLOCK:END -->","handlingStrategy":"validation","validationCode":"const content = fs.readFileSync(file, 'utf8');\nconst startIdx = content.indexOf('GITBUTLER-MANAGED-BLOCK:START');\nconst endIdx = content.indexOf('GITBUTLER-MANAGED-BLOCK:END');\nif (startIdx !== -1 && endIdx !== -1 && endIdx < startIdx) throw new Error(`Managed block markers out of order in ${file}`);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never reorder or move marker lines when reorganizing the file.","If restructuring is needed, delete both markers and let the upsert recreate the block.","Check for duplicated blocks after merges."],"tags":["gitbutler","managed-block","malformed-file","cli"],"backgroundTag":"invalid-state-transition","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"}