Hmbown/CodeWhale · error · anyhow::Error

patch missing *** Begin Patch header

Error message

patch missing *** Begin Patch header

What it means

apply_patch in eval.rs applies a simplified apply-patch-format patch to a workspace file. The patch's first line must be exactly `*** Begin Patch`; anything else (including an empty patch) fails this check before any file is touched.

Solutions

  1. Ensure the patch text begins on its first line with exactly `*** Begin Patch`.
  2. Strip surrounding markdown fences, prose, and leading whitespace before passing the patch to apply_patch.
  3. Convert unified diffs to the apply-patch format the eval harness supports.

Example fix

// before
let patch = "```\n*** Begin Patch\n..."; // fenced
// after
let patch = "*** Begin Patch\n*** Update File: src/main.rs\n...";
Defensive patterns

Strategy: validation

Validate before calling

if !patch.trim_start().starts_with("*** Begin Patch") {
    eprintln!("patch is not in apply-patch format");
    return;
}

Type guard

fn is_apply_patch(s: &str) -> bool {
    s.lines().next() == Some("*** Begin Patch")
}

Try / catch

match apply_patch(&root, patch) {
    Err(e) if e.to_string().contains("Begin Patch header") => {
        log::warn!("model emitted non-apply-patch format; strip fences/prose and retry");
    }
    other => other?,
}

Prevention

When it happens

Trigger: Model-produced patch text in an eval run does not start with `*** Begin Patch` — empty string, prose preamble, or a different patch dialect.

Common situations: The model emitted a unified diff or its own format instead of apply-patch format; leading whitespace or a code fence before the header; patch string truncated so the header is missing.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


AI-assisted analysis of Hmbown/CodeWhale@73e0f67d83 (2026-09-22). Data as JSON: /api/errors/5dbc5e1ff05c9df6. Report an issue: GitHub.

Appendix: source

Thrown at crates/tui/src/eval.rs:682

    Ok(SearchResult { matches })
}

fn append_workspace_file(path: &Path, line: &str) -> Result<()> {
    let mut content = read_workspace_file(path)?;
    if !content.ends_with('\n') {
        content.push('\n');
    }
    content.push_str(line);
    content.push('\n');
    fs::write(path, content).with_context(|| format!("failed to write {}", path.display()))
}

fn apply_patch(root: &Path, patch: &str) -> Result<()> {
    let mut lines = patch.lines();

    let begin = lines.next().unwrap_or_default();
    if begin != "*** Begin Patch" {
        return Err(anyhow!("patch missing *** Begin Patch header"));
    }

    let header = lines.next().unwrap_or_default();
    let file_rel = header
        .strip_prefix("*** Update File: ")
        .ok_or_else(|| anyhow!("only *** Update File patches are supported"))?;
    if file_rel.contains("..") {
        return Err(anyhow!("patch path must be workspace-relative"));
    }

    let file_path = root.join(file_rel);
    let original = read_workspace_file(&file_path)?;
    let had_trailing_newline = original.ends_with('\n');
    let mut file_lines: Vec<String> = original.lines().map(|l| l.to_string()).collect();

    let mut cursor = 0usize;
    for raw_line in lines {
        if raw_line == "*** End Patch" {

View on GitHub (pinned to 73e0f67d83)