{"record":{"id":"4a238171353dd9bc","repo":"rtk-ai/rtk","slug":"refusing-to-modify-malformed-at","errorCode":null,"errorMessage":"Refusing to modify malformed {} at {}","messagePattern":"Refusing to modify malformed (.+?) at (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/hooks/init.rs","lineNumber":3070,"sourceCode":"                println!(\"[ok] {} already up to date in {}\", label, path.display());\n            }\n        }\n        RtkBlockUpsert::Malformed => {\n            eprintln!(\n                \"[warn] Found '{}' without closing marker in {}\",\n                RTK_BLOCK_START,\n                path.display()\n            );\n            if let Some((line_num, _)) = existing\n                .lines()\n                .enumerate()\n                .find(|(_, line)| line.contains(RTK_BLOCK_START))\n            {\n                eprintln!(\"    Location: line {}\", line_num + 1);\n            }\n            eprintln!(\"    Action: Manually remove the incomplete block, then re-run:\");\n            eprintln!(\"            {recovery_cmd}\");\n            anyhow::bail!(\n                \"Refusing to modify malformed {} at {}\",\n                label,\n                path.display()\n            );\n        }\n    }\n\n    Ok(action)\n}\n\n/// Patch CLAUDE.md: add @RTK.md, migrate if old block exists\nfn patch_claude_md(path: &Path, ctx: InitContext) -> Result<bool> {\n    let InitContext {\n        verbose, dry_run, ..\n    } = ctx;\n    let mut content = if path.exists() {\n        fs::read_to_string(path)?\n    } else {","sourceCodeStart":3052,"sourceCodeEnd":3088,"githubUrl":"https://github.com/rtk-ai/rtk/blob/0924356b4caba4989607227b7c8824d3d8098719/src/hooks/init.rs#L3052-L3088","documentation":"Thrown by write_rtk_block when the target file (CLAUDE.md, or another label-specific doc) contains a malformed RTK block — e.g. a block start marker without a matching end marker or an otherwise corrupted block. rtk refuses to edit such files to avoid duplicating or further corrupting the block, and prints the offending line plus a manual recovery command before bailing.","triggerScenarios":"Running any init mode that writes the RTK block (claude-md, kimi, copilot local/global) when the destination file already contains RTK_BLOCK_START but the block is incomplete/unterminated. The bail fires in write_rtk_block at src/hooks/init.rs:3070 after printing the location and recovery hint.","commonSituations":"A previous `rtk init` run was interrupted mid-write; a user or merge conflict edited CLAUDE.md and broke the markers; manual copy/paste of a partial RTK block; re-running init after a failed run without cleanup.","solutions":["Open the file named in the error and remove the incomplete/corrupt RTK block (from the start marker through the end of the partial block).","Re-run the original rtk init command afterwards (the error output prints the exact recovery command).","Use `rtk uninstall -g` where applicable to clean up generated state, then re-init.","Check git history (`git diff CLAUDE.md` / `git log -p CLAUDE.md`) to find which edit broke the block markers."],"exampleFix":"// before (CLAUDE.md, malformed block)\n<!-- rtk:start -->\nrtk proxy ...\n// after (CLAUDE.md, complete block restored by re-running init)\n<!-- rtk:start -->\nrtk proxy ...\n<!-- rtk:end -->","handlingStrategy":"validation","validationCode":"# verify the RTK block is well-formed before re-running init\nFILE=CLAUDE.md\nSTARTS=$(grep -c 'rtk:start' \"$FILE\" || true)\nENDS=$(grep -c 'rtk:end' \"$FILE\" || true)\n[ \"$STARTS\" -eq \"$ENDS\" ] && echo OK || echo \"malformed block: $STARTS start vs $ENDS end; fix $FILE first\"","typeGuard":null,"tryCatchPattern":"if ! rtk init --claude-md 2>err.txt; then\n  grep -q 'Refusing to modify malformed' err.txt && { $EDITOR CLAUDE.md; rtk init --claude-md; }\nfi","preventionTips":["Never hand-edit inside RTK block markers; edit only outside them.","Resolve merge conflicts on CLAUDE.md by re-running `rtk init` after conflict resolution instead of splicing blocks.","If an init run is interrupted, remove the partial block before retrying.","Validate marker pairing (equal start/end counts) in CI for repos that commit CLAUDE.md."],"tags":["cli","claude-md","corrupt-state","write-guard"],"backgroundTag":"internal-invariant-violation","analyzedSha":"0924356b4caba4989607227b7c8824d3d8098719","analyzedAt":"2026-09-19T19:25:29.612Z","contentChangedAt":"2026-09-19T19:25:29.612Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}