| 1 | #!/usr/bin/env python3 |
| 2 | from __future__ import annotations |
| 3 | |
| 4 | import argparse |
| 5 | import re |
| 6 | from pathlib import Path |
| 7 | |
| 8 | META_LINE_PATTERNS = [ |
| 9 | re.compile(r"^✅\s+.+\bis done\..*$"), |
| 10 | re.compile(r"^Quality score:\s+.*$", re.IGNORECASE), |
| 11 | re.compile(r"^Editorial thesis:\s+.*$", re.IGNORECASE), |
| 12 | re.compile(r"^data/analyzed/.+\.md is written.*$", re.IGNORECASE), |
| 13 | re.compile(r"^\.squad/reskill/.+\.md is written.*$", re.IGNORECASE), |
| 14 | ] |
| 15 | |
| 16 | |
| 17 | def parse_args(argv: list[str] | None = None) -> argparse.Namespace: |
| 18 | parser = argparse.ArgumentParser( |
| 19 | description="Strip leaked Copilot/Farnsworth meta lines from markdown outputs." |
| 20 | ) |
| 21 | parser.add_argument("--path", required=True, type=Path, help="File to sanitize in place.") |
| 22 | return parser.parse_args(argv) |
| 23 | |
| 24 | |
| 25 | def sanitize_text(text: str) -> str: |
| 26 | sanitized_lines: list[str] = [] |
| 27 | changed = False |
| 28 | for line in text.splitlines(): |
| 29 | stripped = line.strip() |
| 30 | if stripped and any(pattern.match(stripped) for pattern in META_LINE_PATTERNS): |
| 31 | changed = True |
| 32 | continue |
| 33 | sanitized_lines.append(line) |
| 34 | sanitized = "\n".join(sanitized_lines) |
| 35 | if text.endswith("\n"): |
| 36 | sanitized += "\n" |
| 37 | if changed: |
| 38 | sanitized = re.sub(r"\n{3,}", "\n\n", sanitized) |
| 39 | return sanitized |
| 40 | |
| 41 | |
| 42 | def sanitize_file(path: Path) -> bool: |
| 43 | if not path.exists() or not path.is_file(): |
| 44 | return False |
| 45 | original = path.read_text(encoding="utf-8") |
| 46 | sanitized = sanitize_text(original) |
| 47 | if sanitized == original: |
| 48 | return False |
| 49 | path.write_text(sanitized, encoding="utf-8") |
| 50 | return True |
| 51 | |
| 52 | |
| 53 | def main(argv: list[str] | None = None) -> int: |
| 54 | args = parse_args(argv) |
| 55 | sanitize_file(args.path) |
| 56 | return 0 |
| 57 | |
| 58 | |
| 59 | if __name__ == "__main__": |
| 60 | raise SystemExit(main()) |