{"record":{"id":"5dbc5e1ff05c9df6","repo":"Hmbown/CodeWhale","slug":"patch-missing-begin-patch-header","errorCode":null,"errorMessage":"patch missing *** Begin Patch header","messagePattern":"patch missing \\*\\*\\* Begin Patch header","errorType":"validation","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/tui/src/eval.rs","lineNumber":682,"sourceCode":"    Ok(SearchResult { matches })\n}\n\nfn append_workspace_file(path: &Path, line: &str) -> Result<()> {\n    let mut content = read_workspace_file(path)?;\n    if !content.ends_with('\\n') {\n        content.push('\\n');\n    }\n    content.push_str(line);\n    content.push('\\n');\n    fs::write(path, content).with_context(|| format!(\"failed to write {}\", path.display()))\n}\n\nfn apply_patch(root: &Path, patch: &str) -> Result<()> {\n    let mut lines = patch.lines();\n\n    let begin = lines.next().unwrap_or_default();\n    if begin != \"*** Begin Patch\" {\n        return Err(anyhow!(\"patch missing *** Begin Patch header\"));\n    }\n\n    let header = lines.next().unwrap_or_default();\n    let file_rel = header\n        .strip_prefix(\"*** Update File: \")\n        .ok_or_else(|| anyhow!(\"only *** Update File patches are supported\"))?;\n    if file_rel.contains(\"..\") {\n        return Err(anyhow!(\"patch path must be workspace-relative\"));\n    }\n\n    let file_path = root.join(file_rel);\n    let original = read_workspace_file(&file_path)?;\n    let had_trailing_newline = original.ends_with('\\n');\n    let mut file_lines: Vec<String> = original.lines().map(|l| l.to_string()).collect();\n\n    let mut cursor = 0usize;\n    for raw_line in lines {\n        if raw_line == \"*** End Patch\" {","sourceCodeStart":664,"sourceCodeEnd":700,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/73e0f67d83c59909b571efdfc88c4bc28c309cb1/crates/tui/src/eval.rs#L664-L700","documentation":"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.","triggerScenarios":"Model-produced patch text in an eval run does not start with `*** Begin Patch` — empty string, prose preamble, or a different patch dialect.","commonSituations":"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.","solutions":["Ensure the patch text begins on its first line with exactly `*** Begin Patch`.","Strip surrounding markdown fences, prose, and leading whitespace before passing the patch to apply_patch.","Convert unified diffs to the apply-patch format the eval harness supports."],"exampleFix":"// before\nlet patch = \"```\\n*** Begin Patch\\n...\"; // fenced\n// after\nlet patch = \"*** Begin Patch\\n*** Update File: src/main.rs\\n...\";","handlingStrategy":"validation","validationCode":"if !patch.trim_start().starts_with(\"*** Begin Patch\") {\n    eprintln!(\"patch is not in apply-patch format\");\n    return;\n}","typeGuard":"fn is_apply_patch(s: &str) -> bool {\n    s.lines().next() == Some(\"*** Begin Patch\")\n}","tryCatchPattern":"match apply_patch(&root, patch) {\n    Err(e) if e.to_string().contains(\"Begin Patch header\") => {\n        log::warn!(\"model emitted non-apply-patch format; strip fences/prose and retry\");\n    }\n    other => other?,\n}","preventionTips":["Strip markdown code fences and prose before handing model output to apply_patch.","Prompt the model explicitly for the apply-patch format.","Trim leading whitespace from the patch string."],"tags":["patch","parsing","eval"],"backgroundTag":"invalid-argument-format","analyzedSha":"73e0f67d83c59909b571efdfc88c4bc28c309cb1","analyzedAt":"2026-09-22T01:30:00.501Z","contentChangedAt":"2026-09-22T01:30:00.501Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}