{"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":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"src/hooks/init.rs","lineNumber":2573,"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 { verbose, dry_run } = ctx;\n    let mut content = if path.exists() {\n        fs::read_to_string(path)?\n    } else {\n        String::new()\n    };","sourceCodeStart":2555,"sourceCodeEnd":2591,"githubUrl":"https://github.com/rtk-ai/rtk/blob/d977e1c31621fe8704e6500ceeb9c7a0de2b6836/src/hooks/init.rs#L2555-L2591","documentation":"write_rtk_block upserts an RTK-owned marker block into agent instruction files (CLAUDE.md, AGENTS.md, Copilot instructions, ...). If the existing content contains the opening marker '<!-- rtk-instructions' without the matching closing marker '<!-- /rtk-instructions -->', upsert_rtk_block returns Malformed and rtk refuses to write, printing the offending line number and an exact recovery command. This is a data-safety guard: auto-repair could duplicate or eat user content.","triggerScenarios":"A previous init was interrupted mid-write, a git merge/revert kept the opening marker but dropped the closing one, or a teammate deleted part of the block; any subsequent `rtk init` for that agent bails at src/hooks/init.rs:2573.","commonSituations":"Merge conflicts in CLAUDE.md/AGENTS.md resolved by hand; partial reverts of an init commit; instruction files truncated by an editor or LLM run.","solutions":["Open the file at the 'Location: line N' printed just above the error and delete the entire incomplete block, from the '<!-- rtk-instructions' opening marker through where the closing marker should be","Re-run the exact recovery command printed after 'Action:' (or a plain `rtk init` for that agent) — it re-adds a complete balanced block","Verify your own content around the block is intact before re-running"],"exampleFix":"# CLAUDE.md before (malformed: start marker with no closing marker)\n<!-- rtk-instructions\n...leftover instructions...\n<EOF>\n\n# after cleanup + re-run\ndelete from '<!-- rtk-instructions' to the intended block end, then:\nrtk init -g   # re-adds a complete, balanced block","handlingStrategy":"validation","validationCode":"# bash: verify RTK block markers are balanced before init\nF=CLAUDE.md\nS=$(grep -cF '<!-- rtk-instructions' \"$F\" 2>/dev/null || echo 0)\nE=$(grep -cF '<!-- /rtk-instructions -->' \"$F\" 2>/dev/null || echo 0)\n[ \"$S\" -eq \"$E\" ] || { echo \"unbalanced RTK markers in $F ($S start / $E end)\" >&2; exit 2; }","typeGuard":null,"tryCatchPattern":"Treat this specific bail as a stop-and-fix signal in automation: capture the file path and 'Location: line N' from stderr, fail the init step, and require a human edit before retry — retrying unchanged always fails.","preventionTips":["Commit instruction files (CLAUDE.md/AGENTS.md) immediately after rtk init so a broken block is one git checkout away","Resolve merge conflicts on instruction files by dropping the whole RTK block, never half of it","Run `rtk init --dry-run` after merges touching these files; Malformed is detected before any write"],"tags":["init","patch","markdown","merge-conflict","data-safety"],"backgroundTag":null,"analyzedSha":"d977e1c31621fe8704e6500ceeb9c7a0de2b6836","analyzedAt":"2026-08-16T05:40:46.291Z","schemaVersion":2},"datasetVersion":"2026-08-16T08:17:34.114Z"}