diff --git a/automation/patcher.py b/automation/patcher.py index b3acdb0..543fe21 100644 --- a/automation/patcher.py +++ b/automation/patcher.py @@ -68,8 +68,13 @@ def generate_output( raw_path.write_text(raw_patch, encoding="utf-8") - extracted = extract_patch_with_markers(raw_path.read_text(encoding="utf-8")) - clean_path.write_text(extracted, encoding="utf-8") + try: + extracted = extract_patch_with_markers(raw_path.read_text(encoding="utf-8")) + clean_path.write_text(extracted, encoding="utf-8") + except PatchGenerationError as e: + # Save raw output even if extraction fails for debugging + save_debug_artifacts(repo_root, output_rel, raw_path, None, None, None) + raise sanitized = sanitize_unified_patch(clean_path.read_text(encoding="utf-8")) if "--- /dev/null" in sanitized and "new file mode" not in sanitized: @@ -290,18 +295,21 @@ def rewrite_patch_for_p0(patch: str) -> str: def save_debug_artifacts( repo_root: Path, output_rel: Path, - raw_path: Path, - clean_path: Path, - sanitized_path: Path, - final_path: Path, + raw_path: Path | None, + clean_path: Path | None, + sanitized_path: Path | None, + final_path: Path | None, ) -> None: debug_dir = repo_root / ".git" / "ai-rules-debug" debug_dir.mkdir(parents=True, exist_ok=True) identifier = f"{output_rel.as_posix().replace('/', '_')}-{os.getpid()}" - shutil.copy(raw_path, debug_dir / f"{identifier}.raw.out") - shutil.copy(clean_path, debug_dir / f"{identifier}.clean.diff") - shutil.copy(sanitized_path, debug_dir / f"{identifier}.sanitized.diff") - if final_path.exists(): + if raw_path and raw_path.exists(): + shutil.copy(raw_path, debug_dir / f"{identifier}.raw.out") + if clean_path and clean_path.exists(): + shutil.copy(clean_path, debug_dir / f"{identifier}.clean.diff") + if sanitized_path and sanitized_path.exists(): + shutil.copy(sanitized_path, debug_dir / f"{identifier}.sanitized.diff") + if final_path and final_path.exists(): shutil.copy(final_path, debug_dir / f"{identifier}.final.diff")