Skip to content

The agent that grows with you

refactor(tools): return V4A edit notes through ApplyResult

emozilla54b07ec7f16e
diff --git a/tools/patch_parser.py b/tools/patch_parser.py
index 55aa5cb37e6..b4001d38b63 100644
--- a/tools/patch_parser.py
+++ b/tools/patch_parser.py
@@ -9,7 +9,7 @@ import inspect
import re
from dataclasses import dataclass, field
from enum import Enum
-from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Tuple
+from typing import TYPE_CHECKING, Any, Callable, Dict, List, NamedTuple, Optional, Tuple
if TYPE_CHECKING: # annotations only; the real import is per-call in apply_v4a_operations
from tools.file_operations_common import PatchResult
@@ -42,8 +42,6 @@ class PatchOperation:
file_path: str
new_path: Optional[str] = None # MOVE only
hunks: list[Hunk] = field(default_factory=list)
- # Fuzzy strategies that located this operation's hunks (filled while applying an UPDATE).
- strategies_used: list[str] = field(default_factory=list)
# Markers must occupy the whole line at column 0 so content lines that merely
@@ -268,13 +266,20 @@ def _validate_operations(operations: list[PatchOperation], file_ops: Any) -> lis
return errors
-# Every _apply_* returns (success, diff_or_error, lsp_diagnostics, lint_result, write): ``write``
-# is the PatchResult._writes entry of a file write, None for Delete/Move and failures.
-ApplyResult = tuple[bool, str, Optional[str], Optional[dict], Optional[tuple]]
+class ApplyResult(NamedTuple):
+ """Outcome of one ``_apply_*`` handler. ``write`` is the PatchResult._writes entry of a file
+ write (None for Delete/Move and failures); ``notes`` tell the caller how the edit was matched
+ (escape-drift corrections, hunks located approximately)."""
+ ok: bool
+ payload: str # the diff on success, the error text on failure
+ lsp: Optional[str] = None
+ lint: Optional[dict] = None
+ write: Optional[tuple] = None
+ notes: tuple[str, ...] = ()
def _fail(error: str) -> ApplyResult:
- return False, error, None, None, None
+ return ApplyResult(False, error)
def _written(result: Any, diff: str, path: str, read_sha256: Optional[str]) -> ApplyResult:
@@ -283,7 +288,7 @@ def _written(result: Any, diff: str, path: str, read_sha256: Optional[str]) -> A
if result.error:
return _fail(result.error)
write = (path, read_sha256, getattr(result, "_content_sha256", None))
- return True, diff, getattr(result, "lsp_diagnostics", None), getattr(result, "lint", None), write
+ return ApplyResult(True, diff, getattr(result, "lsp_diagnostics", None), getattr(result, "lint", None), write)
def _unified_diff(path: str, old: str, new: Optional[str]) -> str:
@@ -297,7 +302,6 @@ def apply_v4a_operations(operations: list[PatchOperation], file_ops: Any) -> Pat
"""Two-phase: validate everything, then apply (atomic on validation failure). A phase-2
failure (validate/apply race) carries a ``git diff`` note since state may be inconsistent.
``file_ops`` needs read_file_raw/write_file/delete_file/move_file."""
- from tools.fuzzy_match import boundary_note
def _bullets(errs: list[str]) -> str:
return "\n".join(f" • {e}" for e in errs)
@@ -308,20 +312,15 @@ def apply_v4a_operations(operations: list[PatchOperation], file_ops: Any) -> Pat
error="Patch validation failed (no files were modified):\n" + _bullets(errors))
files: dict[str, list[str]] = {"created": [], "deleted": [], "modified": []}
all_diffs: list[str] = []
- correction_notes: list[str] = []
+ notes: list[str] = []
# V4A bypasses write_file's WriteResult plumbing: LSP diagnostics and lint propagate per file.
lsp_blocks: list[str] = []
lint_results: dict[str, dict] = {}
writes: list[tuple] = []
- # A hunk found by a similarity strategy may have landed a line off; say so, per file.
- boundary_notes: list[str] = []
for op in operations:
handler, verb, bucket = _APPLY_DISPATCH[op.operation]
try:
- if op.operation is OperationType.UPDATE:
- ok, payload, lsp, lint, write = handler(op, file_ops, correction_notes=correction_notes)
- else:
- ok, payload, lsp, lint, write = handler(op, file_ops)
+ ok, payload, lsp, lint, write, op_notes = handler(op, file_ops)
except Exception as e:
ok, payload = None, str(e)
if not ok:
@@ -333,8 +332,7 @@ def apply_v4a_operations(operations: list[PatchOperation], file_ops: Any) -> Pat
all_diffs.append(payload)
if write:
writes.append(write)
- if note := boundary_note(op.strategies_used, "the hunk's", f" in {op.file_path}"):
- boundary_notes.append(note)
+ notes.extend(op_notes)
if lsp:
lsp_blocks.append(lsp)
if lint:
@@ -347,7 +345,7 @@ def apply_v4a_operations(operations: list[PatchOperation], file_ops: Any) -> Pat
diff='\n'.join(all_diffs),
files_modified=files["modified"], files_created=files["created"], files_deleted=files["deleted"],
lint=lint_results or None, lsp_diagnostics="\n\n".join(lsp_blocks) or None, _writes=writes,
- note="\n".join([*dict.fromkeys(correction_notes), *boundary_notes]) or None)
+ note="\n".join(dict.fromkeys(notes)) or None)
def _write_file_accepts_pre_content(file_ops: Any) -> bool:
@@ -386,7 +384,7 @@ def _apply_delete(op: PatchOperation, file_ops: Any) -> ApplyResult:
return _fail(f"Cannot delete {op.file_path}: file not found")
result = file_ops.delete_file(op.file_path)
diff = _unified_diff(op.file_path, read_result.content, None) or f"# Deleted: {op.file_path}"
- return _fail(result.error) if result.error else (True, diff, None, None, None)
+ return _fail(result.error) if result.error else ApplyResult(True, diff)
def _apply_move(op: PatchOperation, file_ops: Any) -> ApplyResult:
@@ -398,8 +396,8 @@ def _apply_move(op: PatchOperation, file_ops: Any) -> ApplyResult:
if not getattr(dst, "not_found", False):
return _fail(f"{op.new_path}: could not confirm the destination is free — {dst.error}")
result = file_ops.move_file(op.file_path, op.new_path)
- return _fail(result.error) if result.error else (
- True, f"# Moved: {op.file_path} -> {op.new_path}", None, None, None)
+ return _fail(result.error) if result.error else ApplyResult(
+ True, f"# Moved: {op.file_path} -> {op.new_path}")
def _insert_addition_only(new_content: str, hunk: Hunk, insert_text: str) -> tuple[Optional[str], Optional[str]]:
@@ -418,15 +416,15 @@ def _insert_addition_only(new_content: str, hunk: Hunk, insert_text: str) -> tup
return new_content.rstrip('\n') + '\n' + insert_text + '\n', None
-def _apply_update(op: PatchOperation, file_ops: Any, *,
- correction_notes: Optional[list[str]] = None) -> ApplyResult:
+def _apply_update(op: PatchOperation, file_ops: Any) -> ApplyResult:
"""Apply each hunk via fuzzy replace, then write once."""
- from tools.fuzzy_match import fuzzy_find_and_replace, is_already_applied
+ from tools.fuzzy_match import boundary_note, fuzzy_find_and_replace, is_already_applied
read_result = file_ops.read_file_raw(op.file_path) # raw: no line numbers / truncation
if read_result.error:
return _fail(f"Cannot read file: {read_result.error}")
current_content = new_content = read_result.content
- hunk_notes: list[str] = []
+ notes: list[str] = [] # escape-drift corrections, appended by fuzzy_find_and_replace
+ strategies: list[str] = [] # the fuzzy strategies that located the hunks
for hunk in op.hunks:
search_lines, replace_lines = _split_hunk(hunk)
if search_lines and search_lines == replace_lines:
@@ -438,10 +436,10 @@ def _apply_update(op: PatchOperation, file_ops: Any, *,
return _fail(err)
continue
new_content, count, strategy, error = fuzzy_find_and_replace(
- new_content, search_pattern, replacement, replace_all=False, correction_notes=hunk_notes)
+ new_content, search_pattern, replacement, replace_all=False, correction_notes=notes)
if not (error and count == 0):
if count > 0:
- op.strategies_used.append(strategy)
+ strategies.append(strategy)
continue
# Retry inside a window around the context hint, if any.
hint_pos = new_content.find(hunk.context_hint) if hunk.context_hint else -1
@@ -450,9 +448,9 @@ def _apply_update(op: PatchOperation, file_ops: Any, *,
window_end = min(len(new_content), hint_pos + 2000)
window_new, count, strategy, error = fuzzy_find_and_replace(
new_content[window_start:window_end], search_pattern, replacement, replace_all=False,
- correction_notes=hunk_notes)
+ correction_notes=notes)
if count > 0:
- op.strategies_used.append(strategy)
+ strategies.append(strategy)
new_content = new_content[:window_start] + window_new + new_content[window_end:]
error = None
if error:
@@ -466,9 +464,12 @@ def _apply_update(op: PatchOperation, file_ops: Any, *,
write_result = file_ops.write_file(op.file_path, new_content, **extra)
result = _written(write_result, _unified_diff(op.file_path, current_content, new_content),
op.file_path, getattr(read_result, "_content_sha256", None))
- if result[0] and correction_notes is not None:
- correction_notes.extend(hunk_notes)
- return result
+ if not result.ok:
+ return result
+ # A hunk found by a similarity strategy may have landed a line off; say so, per file.
+ if note := boundary_note(strategies, "the hunk's", f" in {op.file_path}"):
+ notes.append(note)
+ return result._replace(notes=tuple(notes))
# operation -> (handler, verb for error text, files_* bucket)