rtk-ai/rtk · error

Refusing to modify malformed

Error message

Refusing to modify malformed {} at {}

What it means

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.

Solutions

  1. 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).
  2. Re-run the original rtk init command afterwards (the error output prints the exact recovery command).
  3. Use `rtk uninstall -g` where applicable to clean up generated state, then re-init.
  4. Check git history (`git diff CLAUDE.md` / `git log -p CLAUDE.md`) to find which edit broke the block markers.

Example fix

// before (CLAUDE.md, malformed block)
<!-- rtk:start -->
rtk proxy ...
// after (CLAUDE.md, complete block restored by re-running init)
<!-- rtk:start -->
rtk proxy ...
<!-- rtk:end -->
Defensive patterns

Strategy: validation

Validate before calling

# verify the RTK block is well-formed before re-running init
FILE=CLAUDE.md
STARTS=$(grep -c 'rtk:start' "$FILE" || true)
ENDS=$(grep -c 'rtk:end' "$FILE" || true)
[ "$STARTS" -eq "$ENDS" ] && echo OK || echo "malformed block: $STARTS start vs $ENDS end; fix $FILE first"

Try / catch

if ! rtk init --claude-md 2>err.txt; then
  grep -q 'Refusing to modify malformed' err.txt && { $EDITOR CLAUDE.md; rtk init --claude-md; }
fi

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


AI-assisted analysis of rtk-ai/rtk@0924356b4c (2026-09-19). Data as JSON: /api/errors/4a238171353dd9bc. Report an issue: GitHub.

Appendix: source

Thrown at src/hooks/init.rs:3070

                println!("[ok] {} already up to date in {}", label, path.display());
            }
        }
        RtkBlockUpsert::Malformed => {
            eprintln!(
                "[warn] Found '{}' without closing marker in {}",
                RTK_BLOCK_START,
                path.display()
            );
            if let Some((line_num, _)) = existing
                .lines()
                .enumerate()
                .find(|(_, line)| line.contains(RTK_BLOCK_START))
            {
                eprintln!("    Location: line {}", line_num + 1);
            }
            eprintln!("    Action: Manually remove the incomplete block, then re-run:");
            eprintln!("            {recovery_cmd}");
            anyhow::bail!(
                "Refusing to modify malformed {} at {}",
                label,
                path.display()
            );
        }
    }

    Ok(action)
}

/// Patch CLAUDE.md: add @RTK.md, migrate if old block exists
fn patch_claude_md(path: &Path, ctx: InitContext) -> Result<bool> {
    let InitContext {
        verbose, dry_run, ..
    } = ctx;
    let mut content = if path.exists() {
        fs::read_to_string(path)?
    } else {

View on GitHub (pinned to 0924356b4c)