patch_parser.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. """V4A patch parser/applier (codex, cline). ``*** Begin Patch``/``*** End Patch`` wrap ops:
  2. ``*** Update File: p`` + hunks (``@@ hint @@``, `` ctx``, ``-old``, ``+new``); ``*** Add File: n``
  3. + ``+`` lines; ``*** Delete File: o``; ``*** Move File: a -> b``. Entry points:
  4. ``parse_v4a_patch(text) -> (ops, error)`` and ``apply_v4a_operations(ops, file_ops)``."""
  5. import contextlib
  6. import difflib
  7. import inspect
  8. import ntpath
  9. import posixpath
  10. import re
  11. from dataclasses import dataclass, field
  12. from enum import Enum
  13. from typing import TYPE_CHECKING, Any, Callable, Dict, List, NamedTuple, Optional, Tuple
  14. if TYPE_CHECKING: # annotations only; the real import is per-call in apply_v4a_operations
  15. from tools.file_operations_common import PatchResult
  16. from tools.file_operations_common import PatchResult
  17. class OperationType(Enum):
  18. ADD = "add"
  19. UPDATE = "update"
  20. DELETE = "delete"
  21. MOVE = "move"
  22. @dataclass
  23. class HunkLine:
  24. prefix: str # ' ', '-', or '+'
  25. content: str
  26. @dataclass
  27. class Hunk:
  28. context_hint: Optional[str] = None
  29. lines: list[HunkLine] = field(default_factory=list)
  30. @dataclass
  31. class PatchOperation:
  32. operation: OperationType
  33. file_path: str
  34. new_path: Optional[str] = None # MOVE only
  35. hunks: list[Hunk] = field(default_factory=list)
  36. # Markers must occupy the whole line at column 0 so content lines that merely
  37. # mention the format ("+*** End Patch") can't truncate or reset the patch.
  38. _BEGIN_MARKER = re.compile(r'^\*\*\*\s*Begin\s+Patch\s*$')
  39. _END_MARKER = re.compile(r'^\*\*\*\s*End\s+Patch\s*$')
  40. _OP_MARKERS: list[tuple[OperationType, re.Pattern]] = [
  41. (OperationType.UPDATE, re.compile(r'\*\*\*\s*Update\s+File:\s*(.+)')),
  42. (OperationType.ADD, re.compile(r'\*\*\*\s*Add\s+File:\s*(.+)')),
  43. (OperationType.DELETE, re.compile(r'\*\*\*\s*Delete\s+File:\s*(.+)')),
  44. (OperationType.MOVE, re.compile(r'\*\*\*\s*Move\s+File:\s*(.+?)\s*->\s*(.+)'))]
  45. _HINT_RE = re.compile(r'@@\s*(.+?)\s*@@')
  46. def parse_v4a_patch(patch_content: str) -> tuple[list[PatchOperation], Optional[str]]:
  47. """-> ``(operations, None)`` (empty patch = ``[]``, no error) or ``([], "Parse error: …")``."""
  48. # Tolerate CRLF: a stray ``\r`` would land in every HunkLine.content and defeat the markers.
  49. lines = [ln.removesuffix('\r') for ln in patch_content.split('\n')]
  50. start_idx = -1 # parse from the top when no Begin marker is present
  51. end_idx = len(lines)
  52. for i, line in enumerate(lines):
  53. if _BEGIN_MARKER.match(line):
  54. start_idx = i
  55. elif _END_MARKER.match(line):
  56. end_idx = i
  57. break
  58. operations: list[PatchOperation] = []
  59. current_op: Optional[PatchOperation] = None
  60. current_hunk: Optional[Hunk] = None
  61. def _flush_hunk() -> None:
  62. if current_op and current_hunk and current_hunk.lines:
  63. current_op.hunks.append(current_hunk)
  64. def _flush() -> None:
  65. if current_op:
  66. _flush_hunk()
  67. operations.append(current_op)
  68. for line in lines[start_idx + 1:end_idx]:
  69. op_match = next(((kind, m) for kind, rx in _OP_MARKERS if (m := rx.match(line))), None)
  70. if op_match:
  71. kind, m = op_match
  72. _flush()
  73. current_op = PatchOperation(
  74. operation=kind,
  75. file_path=m.group(1).strip(),
  76. new_path=m.group(2).strip() if kind is OperationType.MOVE else None)
  77. # UPDATE hunks start lazily ('@@' or first hunk line); ADD collects all '+' lines
  78. # into one hunk; DELETE/MOVE are complete.
  79. current_hunk = Hunk() if kind is OperationType.ADD else None
  80. if kind in (OperationType.DELETE, OperationType.MOVE):
  81. operations.append(current_op)
  82. current_op = None
  83. elif line.startswith('@@'):
  84. if current_op:
  85. _flush_hunk()
  86. hint_match = _HINT_RE.match(line)
  87. current_hunk = Hunk(context_hint=hint_match.group(1) if hint_match else None)
  88. elif current_op and line:
  89. if current_hunk is None:
  90. current_hunk = Hunk()
  91. if line[0] in '+- ':
  92. current_hunk.lines.append(HunkLine(line[0], line[1:]))
  93. elif line[0] != '\\': # "\ No newline at end of file" marker is skipped
  94. current_hunk.lines.append(HunkLine(' ', line)) # implicit context line
  95. _flush()
  96. parse_errors: list[str] = []
  97. for op in operations:
  98. if not op.file_path:
  99. parse_errors.append("Operation with empty file path")
  100. if op.operation is OperationType.UPDATE and not op.hunks:
  101. parse_errors.append(f"UPDATE {op.file_path!r}: no hunks found")
  102. if op.operation is OperationType.MOVE and not op.new_path:
  103. parse_errors.append(
  104. f"MOVE {op.file_path!r}: missing destination path (expected 'src -> dst')")
  105. return ([], "Parse error: " + "; ".join(parse_errors)) if parse_errors else (operations, None)
  106. def _count_occurrences(text: str, pattern: str) -> int:
  107. """Count occurrences of *pattern* in *text*, advancing one char per hit (overlaps count)."""
  108. return sum(1 for i in range(len(text) + 1) if text.startswith(pattern, i))
  109. def _split_hunk(hunk: Hunk) -> tuple[list[str], list[str]]:
  110. """``(search_lines, replace_lines)``: context+removed vs context+added."""
  111. return ([l.content for l in hunk.lines if l.prefix != '+'],
  112. [l.content for l in hunk.lines if l.prefix != '-'])
  113. def _no_match_hint(error: Optional[str], search_pattern: str, content: str) -> str:
  114. """Best-effort 'Did you mean...' suffix; never lets a hint failure mask the real error."""
  115. with contextlib.suppress(Exception):
  116. from tools.fuzzy_match import format_no_match_hint
  117. return format_no_match_hint(error, 0, search_pattern, content)
  118. return ""
  119. def _hint_ambiguity(content: str, hint: str, tail: str = "") -> tuple[int, str]:
  120. """(occurrences, error) for an addition-only hunk's context hint; error is '' when unique."""
  121. n = _count_occurrences(content, hint)
  122. return n, f"context hint '{hint}' is ambiguous ({n} occurrences){tail}" if n > 1 else ""
  123. def _overlay_key(path: str, file_ops: Any) -> str:
  124. """One validation-overlay key per backend file, however the patch spells it.
  125. Lexical only (the parser cannot resolve paths on a remote backend): ``x``, ``./x``,
  126. ``a//x`` and ``<cwd>/x`` share a key, so a second op on an alias validates against the
  127. first op's simulated result instead of the stale on-disk file. Never merge two real files:
  128. on POSIX ``..`` is kept, because the kernel resolves it AFTER following symlinks
  129. (``sub/link/../f`` is ``real/f`` when ``link`` points into ``real/``); Windows collapses
  130. ``..`` lexically and is case-insensitive, so only a Windows backend gets ntpath folding.
  131. Keys the path the backend opens: the backend's own ``_expand_path`` (``~`` -> its HOME, a
  132. host path under a docker workspace mount -> the container path) runs first. A trailing
  133. ``/`` or ``/.`` needs a directory, so it keeps a marker and never shares the file's key.
  134. """
  135. env = getattr(file_ops, "env", None)
  136. cwd = getattr(env, "cwd", None) or getattr(file_ops, "cwd", None)
  137. cwd = cwd if isinstance(cwd, str) else ""
  138. path = path.strip()
  139. expand = getattr(file_ops, "_expand_path", None)
  140. expanded = expand(path) if callable(expand) else None
  141. path = expanded if isinstance(expanded, str) and expanded else path
  142. if _backend_is_windows(env, cwd):
  143. tail = "\\" if re.search(r"[\\/]\.?[\\/]*$", path) else ""
  144. return ntpath.normcase(ntpath.normpath(ntpath.join(cwd, path))) + tail
  145. raw = posixpath.join(cwd, path) if cwd.startswith("/") else path
  146. segments = [seg for seg in raw.split("/") if seg not in ("", ".")]
  147. tail = "/" if segments and raw.split("/")[-1] in ("", ".") else ""
  148. return ("/" if raw.startswith("/") else "") + "/".join(segments) + tail
  149. def _backend_is_windows(env: Any, cwd: str) -> bool:
  150. """A local env on a Windows host, or a backend whose cwd is a drive path. Remote
  151. sandboxes (docker, ssh, modal ...) are POSIX shells even when Hermes runs on Windows."""
  152. from tools.environments.local import _IS_WINDOWS
  153. return bool(_IS_WINDOWS and getattr(env, "is_local", False)) or bool(
  154. re.match(r"^[A-Za-z]:[\\/]", cwd))
  155. def _validate_operations(operations: list[PatchOperation], file_ops: Any) -> list[str]:
  156. """Dry-run every operation -> error strings (empty = safe). UPDATE hunks are simulated in
  157. order so later hunks see post-earlier-hunk content, exactly as apply will."""
  158. from tools.fuzzy_match import fuzzy_find_and_replace, is_already_applied
  159. errors: list[str] = []
  160. real_change_count = 0
  161. # Overlay so inter-op state validates (a MOVE creating the path a later UPDATE targets),
  162. # keyed by _overlay_key so path aliases share one entry.
  163. pending_content: dict = {}
  164. removed_paths: set = set()
  165. def _read(path: str) -> tuple[Optional[str], Optional[str]]:
  166. key = _overlay_key(path, file_ops)
  167. if key in pending_content:
  168. return pending_content[key], None
  169. if key in removed_paths:
  170. return None, "file not found"
  171. r = file_ops.read_file_raw(path)
  172. return (None, r.error) if r.error else (r.content, None)
  173. def _occupied(path: str) -> Optional[str]:
  174. """Why an Add target or Move destination is not free, or None. Only a read that reports
  175. the path absent (``not_found``) frees it: a read that FAILED (no byte transport, a
  176. directory, an unreadable file) says nothing about what is there, and taking it as free
  177. writes over the file the check exists to protect."""
  178. key = _overlay_key(path, file_ops)
  179. if key in pending_content:
  180. return "exists"
  181. if key in removed_paths:
  182. return None
  183. r = file_ops.read_file_raw(path)
  184. if not r.error:
  185. return "exists"
  186. return None if getattr(r, "not_found", False) else f"could not confirm the path is free — {r.error}"
  187. def _validate_update(op: PatchOperation) -> None:
  188. nonlocal real_change_count
  189. simulated, read_err = _read(op.file_path)
  190. if read_err:
  191. errors.append(f"{op.file_path}: {read_err}")
  192. return
  193. for hunk_index, hunk in enumerate(op.hunks, start=1):
  194. search_lines, replace_lines = _split_hunk(hunk)
  195. if search_lines == replace_lines:
  196. # Context-only anchor hunks (models emit these between changes) are inert; identical
  197. # -/+ lines are skipped by apply as a no-op — neither may fail validation.
  198. real_change_count += any(l.prefix in '-+' for l in hunk.lines)
  199. continue
  200. real_change_count += 1
  201. if not search_lines: # addition-only: the context hint must be unique
  202. if hunk.context_hint:
  203. occurrences, ambiguous = _hint_ambiguity(simulated, hunk.context_hint)
  204. if occurrences == 0:
  205. errors.append(f"{op.file_path}: addition-only hunk context hint "
  206. f"'{hunk.context_hint}' not found")
  207. elif ambiguous:
  208. errors.append(f"{op.file_path}: addition-only hunk {ambiguous}")
  209. continue
  210. search_pattern, replacement = '\n'.join(search_lines), '\n'.join(replace_lines)
  211. new_simulated, count, _strategy, match_error = fuzzy_find_and_replace(
  212. simulated, search_pattern, replacement, replace_all=False)
  213. if count:
  214. simulated = new_simulated
  215. elif not is_already_applied(simulated or "", search_pattern, replacement):
  216. # Already-applied hunks are no-ops (apply performs the same skip).
  217. label = f"'{hunk.context_hint}'" if hunk.context_hint else "(no hint)"
  218. errors.append(
  219. f"{op.file_path}: hunk {hunk_index} {label} not found"
  220. + (f" — {match_error}" if match_error else "")
  221. + _no_match_hint(match_error, search_pattern, simulated))
  222. pending_content[_overlay_key(op.file_path, file_ops)] = simulated
  223. def _remove(path: str) -> None:
  224. key = _overlay_key(path, file_ops)
  225. removed_paths.add(key)
  226. pending_content.pop(key, None)
  227. for op in operations:
  228. if op.operation == OperationType.UPDATE:
  229. _validate_update(op)
  230. continue
  231. real_change_count += 1
  232. if op.operation == OperationType.DELETE:
  233. if _read(op.file_path)[1]:
  234. errors.append(f"{op.file_path}: file not found for deletion")
  235. else:
  236. _remove(op.file_path)
  237. elif op.operation == OperationType.MOVE:
  238. if not op.new_path:
  239. errors.append(f"{op.file_path}: MOVE operation missing destination path")
  240. continue
  241. src_content, src_err = _read(op.file_path)
  242. if src_err:
  243. errors.append(f"{op.file_path}: source file not found for move")
  244. dst_taken = _occupied(op.new_path)
  245. if dst_taken == "exists":
  246. errors.append(f"{op.new_path}: destination already exists — move would overwrite")
  247. elif dst_taken:
  248. errors.append(f"{op.new_path}: {dst_taken}")
  249. elif not src_err: # only a cleanly-validated move updates the overlay
  250. pending_content[_overlay_key(op.new_path, file_ops)] = src_content if src_content is not None else ""
  251. _remove(op.file_path)
  252. elif op.operation == OperationType.ADD:
  253. # An Add must create a NEW file. If the target already exists, write_file
  254. # would clobber it with only the patch's '+' lines and report success,
  255. # silently destroying the original contents (models frequently confuse Add
  256. # with Update). Reject it here so the two-phase contract holds, mirroring
  257. # the MOVE destination guard. Overlay-aware: an Add after a Delete of the
  258. # same path in this patch stays legal, and the added content enters the
  259. # overlay so later hunks against it validate.
  260. add_taken = _occupied(op.file_path)
  261. if add_taken == "exists":
  262. errors.append(f"{op.file_path}: file already exists — use Update File, not Add File")
  263. elif add_taken:
  264. errors.append(f"{op.file_path}: {add_taken}")
  265. else:
  266. removed_paths.discard(_overlay_key(op.file_path, file_ops))
  267. pending_content[_overlay_key(op.file_path, file_ops)] = '\n'.join(
  268. line.content for hunk in op.hunks for line in hunk.lines if line.prefix == '+')
  269. if not errors and real_change_count == 0:
  270. errors.append("Patch contains no changes (only context lines were provided)")
  271. return errors
  272. class ApplyResult(NamedTuple):
  273. """Outcome of one ``_apply_*`` handler. ``write`` is the PatchResult._writes entry of a file
  274. write (None for Delete/Move and failures); ``notes`` tell the caller how the edit was matched
  275. (escape-drift corrections, hunks located approximately)."""
  276. ok: bool
  277. payload: str # the diff on success, the error text on failure
  278. lsp: Optional[str] = None
  279. lint: Optional[dict] = None
  280. write: Optional[tuple] = None
  281. notes: tuple[str, ...] = ()
  282. def _fail(error: str) -> ApplyResult:
  283. return ApplyResult(False, error)
  284. def _written(result: Any, diff: str, path: str, read_sha256: Optional[str]) -> ApplyResult:
  285. """Outcome of a write: its error, else success with LSP/lint propagated from the WriteResult
  286. and the ``(path, read_sha256, written_sha256)`` record of the bytes it replaced and wrote."""
  287. if result.error:
  288. return _fail(result.error)
  289. write = (path, read_sha256, getattr(result, "_content_sha256", None))
  290. return ApplyResult(True, diff, getattr(result, "lsp_diagnostics", None), getattr(result, "lint", None), write)
  291. def _unified_diff(path: str, old: str, new: Optional[str]) -> str:
  292. """Unified diff ``a/path`` -> ``b/path`` (``new=None`` = deletion, ``/dev/null``)."""
  293. return ''.join(difflib.unified_diff(
  294. old.splitlines(keepends=True), [] if new is None else new.splitlines(keepends=True),
  295. fromfile=f"a/{path}", tofile="/dev/null" if new is None else f"b/{path}"))
  296. def apply_v4a_operations(operations: list[PatchOperation], file_ops: Any) -> PatchResult:
  297. """Two-phase: validate everything, then apply (atomic on validation failure). A phase-2
  298. failure (validate/apply race) carries a ``git diff`` note since state may be inconsistent.
  299. ``file_ops`` needs read_file_raw/write_file/delete_file/move_file."""
  300. def _bullets(errs: list[str]) -> str:
  301. return "\n".join(f" • {e}" for e in errs)
  302. if errors := _validate_operations(operations, file_ops):
  303. return PatchResult(
  304. success=False,
  305. error="Patch validation failed (no files were modified):\n" + _bullets(errors))
  306. files: dict[str, list[str]] = {"created": [], "deleted": [], "modified": []}
  307. all_diffs: list[str] = []
  308. notes: list[str] = []
  309. # V4A bypasses write_file's WriteResult plumbing: LSP diagnostics and lint propagate per file.
  310. lsp_blocks: list[str] = []
  311. lint_results: dict[str, dict] = {}
  312. writes: list[tuple] = []
  313. for op in operations:
  314. handler, verb, bucket = _APPLY_DISPATCH[op.operation]
  315. try:
  316. ok, payload, lsp, lint, write, op_notes = handler(op, file_ops)
  317. except Exception as e:
  318. ok, payload = None, str(e)
  319. if not ok:
  320. prefix = f"Failed to {verb}" if ok is False else "Error processing"
  321. errors.append(f"{prefix} {op.file_path}: {payload}")
  322. continue
  323. is_move = op.operation is OperationType.MOVE
  324. files[bucket].append(f"{op.file_path} -> {op.new_path}" if is_move else op.file_path)
  325. all_diffs.append(payload)
  326. if write:
  327. writes.append(write)
  328. notes.extend(op_notes)
  329. if lsp:
  330. lsp_blocks.append(lsp)
  331. if lint:
  332. lint_results[op.file_path] = lint
  333. # Each LSP block carries its own <diagnostics file="..."> header; joining keeps attribution.
  334. return PatchResult(
  335. success=not errors,
  336. error=("Apply phase failed (state may be inconsistent — run `git diff` to assess):\n"
  337. + _bullets(errors)) if errors else None,
  338. diff='\n'.join(all_diffs),
  339. files_modified=files["modified"], files_created=files["created"], files_deleted=files["deleted"],
  340. lint=lint_results or None, lsp_diagnostics="\n\n".join(lsp_blocks) or None, _writes=writes,
  341. note="\n".join(dict.fromkeys(notes)) or None)
  342. def _write_file_accepts_pre_content(file_ops: Any) -> bool:
  343. """Whether ``file_ops.write_file`` accepts ``pre_content`` — read from the signature, not by
  344. catching TypeError around the call, so a TypeError raised *inside* it can't double-write."""
  345. try:
  346. params = inspect.signature(file_ops.write_file).parameters
  347. except (TypeError, ValueError):
  348. return False
  349. return "pre_content" in params or any(
  350. p.kind is inspect.Parameter.VAR_KEYWORD for p in params.values())
  351. def _apply_add(op: PatchOperation, file_ops: Any) -> ApplyResult:
  352. """Create a file from the hunks' '+' lines. Fails closed when the target already
  353. exists: validation confirmed the path was free (or freed by an earlier DELETE in
  354. this patch, which has already applied by now), so an existing file here is a
  355. validate/apply race — never clobber."""
  356. read_back = file_ops.read_file_raw(op.file_path)
  357. if not read_back.error:
  358. return _fail(f"{op.file_path}: file already exists — use Update File, not Add File")
  359. if not getattr(read_back, "not_found", False):
  360. # The read FAILED; it did not report an absent path. Treating that as "the path is free"
  361. # writes the Add payload over whatever is actually there.
  362. return _fail(f"{op.file_path}: could not confirm the path is free — {read_back.error}")
  363. content_lines = [line.content for hunk in op.hunks for line in hunk.lines if line.prefix == '+']
  364. result = file_ops.write_file(op.file_path, '\n'.join(content_lines))
  365. diff = f"--- /dev/null\n+++ b/{op.file_path}\n" + '\n'.join(f"+{line}" for line in content_lines)
  366. return _written(result, diff, op.file_path, "")
  367. def _apply_delete(op: PatchOperation, file_ops: Any) -> ApplyResult:
  368. """Delete a file, producing a real unified diff of the removed content."""
  369. read_result = file_ops.read_file_raw(op.file_path) # re-read guards validate/apply races
  370. if read_result.error:
  371. return _fail(f"Cannot delete {op.file_path}: file not found")
  372. result = file_ops.delete_file(op.file_path)
  373. diff = _unified_diff(op.file_path, read_result.content, None) or f"# Deleted: {op.file_path}"
  374. return _fail(result.error) if result.error else ApplyResult(True, diff)
  375. def _apply_move(op: PatchOperation, file_ops: Any) -> ApplyResult:
  376. """Move, re-checking the destination first: validation's answer is stale once earlier ops of
  377. this patch have applied, and ``mv`` replaces whatever is there."""
  378. dst = file_ops.read_file_raw(op.new_path)
  379. if not dst.error:
  380. return _fail(f"{op.new_path}: destination already exists — move would overwrite")
  381. if not getattr(dst, "not_found", False):
  382. return _fail(f"{op.new_path}: could not confirm the destination is free — {dst.error}")
  383. result = file_ops.move_file(op.file_path, op.new_path)
  384. return _fail(result.error) if result.error else ApplyResult(
  385. True, f"# Moved: {op.file_path} -> {op.new_path}")
  386. def _insert_addition_only(new_content: str, hunk: Hunk, insert_text: str) -> tuple[Optional[str], Optional[str]]:
  387. """Place an addition-only hunk after its context hint (or at EOF). Returns (content, error)."""
  388. if hunk.context_hint:
  389. occurrences, ambiguous = _hint_ambiguity(
  390. new_content, hunk.context_hint, " — provide a more unique hint")
  391. if ambiguous:
  392. return None, f"Addition-only hunk: {ambiguous}"
  393. if occurrences == 1:
  394. eol = new_content.find('\n', new_content.find(hunk.context_hint))
  395. if eol == -1:
  396. return new_content + '\n' + insert_text, None
  397. return new_content[:eol + 1] + insert_text + '\n' + new_content[eol + 1:], None
  398. # No hint / hint not found — append at end as a safe fallback.
  399. return new_content.rstrip('\n') + '\n' + insert_text + '\n', None
  400. def _apply_update(op: PatchOperation, file_ops: Any) -> ApplyResult:
  401. """Apply each hunk via fuzzy replace, then write once."""
  402. from tools.fuzzy_match import boundary_note, fuzzy_find_and_replace, is_already_applied
  403. read_result = file_ops.read_file_raw(op.file_path) # raw: no line numbers / truncation
  404. if read_result.error:
  405. return _fail(f"Cannot read file: {read_result.error}")
  406. current_content = new_content = read_result.content
  407. notes: list[str] = [] # escape-drift corrections, appended by fuzzy_find_and_replace
  408. strategies: list[str] = [] # the fuzzy strategies that located the hunks
  409. for hunk in op.hunks:
  410. search_lines, replace_lines = _split_hunk(hunk)
  411. if search_lines and search_lines == replace_lines:
  412. continue
  413. search_pattern, replacement = '\n'.join(search_lines), '\n'.join(replace_lines)
  414. if not search_lines:
  415. new_content, err = _insert_addition_only(new_content, hunk, replacement)
  416. if err:
  417. return _fail(err)
  418. continue
  419. new_content, count, strategy, error = fuzzy_find_and_replace(
  420. new_content, search_pattern, replacement, replace_all=False, correction_notes=notes)
  421. if not (error and count == 0):
  422. if count > 0:
  423. strategies.append(strategy)
  424. continue
  425. # Retry inside a window around the context hint, if any.
  426. hint_pos = new_content.find(hunk.context_hint) if hunk.context_hint else -1
  427. if hint_pos != -1:
  428. window_start = max(0, hint_pos - 500)
  429. window_end = min(len(new_content), hint_pos + 2000)
  430. window_new, count, strategy, error = fuzzy_find_and_replace(
  431. new_content[window_start:window_end], search_pattern, replacement, replace_all=False,
  432. correction_notes=notes)
  433. if count > 0:
  434. strategies.append(strategy)
  435. new_content = new_content[:window_start] + window_new + new_content[window_end:]
  436. error = None
  437. if error:
  438. # Mirror validation's already-applied skip, else the two phases disagree and fail here.
  439. if is_already_applied(new_content, search_pattern, replacement):
  440. continue
  441. hint = _no_match_hint(error, search_pattern, new_content)
  442. return _fail(f"Could not apply hunk: {error}" + hint)
  443. # Pass pre_content to skip a redundant re-read inside write_file when supported.
  444. extra = {"pre_content": current_content} if _write_file_accepts_pre_content(file_ops) else {}
  445. write_result = file_ops.write_file(op.file_path, new_content, **extra)
  446. result = _written(write_result, _unified_diff(op.file_path, current_content, new_content),
  447. op.file_path, getattr(read_result, "_content_sha256", None))
  448. if not result.ok:
  449. return result
  450. # A hunk found by a similarity strategy may have landed a line off; say so, per file.
  451. if note := boundary_note(strategies, "the hunk's", f" in {op.file_path}"):
  452. notes.append(note)
  453. return result._replace(notes=tuple(notes))
  454. # operation -> (handler, verb for error text, files_* bucket)
  455. _APPLY_DISPATCH: dict[OperationType, tuple[Callable[[PatchOperation, Any], ApplyResult], str, str]] = {
  456. OperationType.ADD: (_apply_add, "add", "created"),
  457. OperationType.DELETE: (_apply_delete, "delete", "deleted"),
  458. OperationType.MOVE: (_apply_move, "move", "modified"),
  459. OperationType.UPDATE: (_apply_update, "update", "modified"),
  460. }