refactor(tools): group H pass 1 — dead code, shared read_pane bridge, zip/xml helpers, collapsed defensive layers
This commit is contained in:
+92
-128
@@ -34,26 +34,23 @@ class OperationType(Enum):
|
||||
|
||||
@dataclass
|
||||
class HunkLine:
|
||||
"""A single line in a patch hunk."""
|
||||
prefix: str # ' ', '-', or '+'
|
||||
content: str
|
||||
|
||||
|
||||
@dataclass
|
||||
class Hunk:
|
||||
"""A group of changes within a file."""
|
||||
context_hint: Optional[str] = None
|
||||
lines: List[HunkLine] = field(default_factory=list)
|
||||
|
||||
|
||||
@dataclass
|
||||
class PatchOperation:
|
||||
"""A single operation in a V4A patch."""
|
||||
operation: OperationType
|
||||
file_path: str
|
||||
new_path: Optional[str] = None # For move operations
|
||||
new_path: Optional[str] = None # MOVE only
|
||||
hunks: List[Hunk] = field(default_factory=list)
|
||||
content: Optional[str] = None # For add file operations
|
||||
content: Optional[str] = None # ADD only
|
||||
|
||||
|
||||
# Markers must occupy the whole line at column 0 so content lines that merely
|
||||
@@ -92,16 +89,17 @@ def parse_v4a_patch(patch_content: str) -> Tuple[List[PatchOperation], Optional[
|
||||
current_op: Optional[PatchOperation] = None
|
||||
current_hunk: Optional[Hunk] = None
|
||||
|
||||
def _flush_hunk() -> None:
|
||||
if current_op and current_hunk and current_hunk.lines:
|
||||
current_op.hunks.append(current_hunk)
|
||||
|
||||
def _flush() -> None:
|
||||
if current_op:
|
||||
if current_hunk and current_hunk.lines:
|
||||
current_op.hunks.append(current_hunk)
|
||||
_flush_hunk()
|
||||
operations.append(current_op)
|
||||
|
||||
for line in lines[start_idx + 1:end_idx]:
|
||||
op_match = next(
|
||||
((kind, m) for kind, rx in _OP_MARKERS if (m := rx.match(line))), None,
|
||||
)
|
||||
op_match = next(((kind, m) for kind, rx in _OP_MARKERS if (m := rx.match(line))), None)
|
||||
if op_match:
|
||||
kind, m = op_match
|
||||
_flush()
|
||||
@@ -118,8 +116,7 @@ def parse_v4a_patch(patch_content: str) -> Tuple[List[PatchOperation], Optional[
|
||||
current_op = None
|
||||
elif line.startswith('@@'):
|
||||
if current_op:
|
||||
if current_hunk and current_hunk.lines:
|
||||
current_op.hunks.append(current_hunk)
|
||||
_flush_hunk()
|
||||
hint_match = _HINT_RE.match(line)
|
||||
current_hunk = Hunk(context_hint=hint_match.group(1) if hint_match else None)
|
||||
elif current_op and line:
|
||||
@@ -146,15 +143,7 @@ def parse_v4a_patch(patch_content: str) -> Tuple[List[PatchOperation], Optional[
|
||||
|
||||
def _count_occurrences(text: str, pattern: str) -> int:
|
||||
"""Count occurrences of *pattern* in *text*, advancing one char per hit (overlaps count)."""
|
||||
count = 0
|
||||
start = 0
|
||||
while True:
|
||||
pos = text.find(pattern, start)
|
||||
if pos == -1:
|
||||
break
|
||||
count += 1
|
||||
start = pos + 1
|
||||
return count
|
||||
return sum(1 for i in range(len(text) + 1) if text.startswith(pattern, i))
|
||||
|
||||
|
||||
def _split_hunk(hunk: Hunk) -> Tuple[List[str], List[str]]:
|
||||
@@ -173,10 +162,15 @@ def _no_match_hint(error: Optional[str], search_pattern: str, content: str) -> s
|
||||
return ""
|
||||
|
||||
|
||||
def _validate_operations(
|
||||
operations: List[PatchOperation],
|
||||
file_ops: Any,
|
||||
) -> List[str]:
|
||||
def _hint_ambiguity(content: str, hint: str, tail: str = "") -> Tuple[int, str]:
|
||||
"""(occurrences, error) for an addition-only hunk's context hint; error is '' when unique."""
|
||||
occurrences = _count_occurrences(content, hint)
|
||||
if occurrences > 1:
|
||||
return occurrences, f"context hint '{hint}' is ambiguous ({occurrences} occurrences){tail}"
|
||||
return occurrences, ""
|
||||
|
||||
|
||||
def _validate_operations(operations: List[PatchOperation], file_ops: Any) -> List[str]:
|
||||
"""Dry-run every operation; return error strings (empty list = safe to apply).
|
||||
|
||||
UPDATE hunks are simulated in order so later hunks validate against
|
||||
@@ -198,78 +192,65 @@ def _validate_operations(
|
||||
if path in pending_content:
|
||||
return pending_content[path], None
|
||||
r = file_ops.read_file_raw(path)
|
||||
if r.error:
|
||||
return None, r.error
|
||||
return r.content, None
|
||||
return (None, r.error) if r.error else (r.content, None)
|
||||
|
||||
def _validate_update(op: PatchOperation) -> None:
|
||||
nonlocal real_change_count
|
||||
content, read_err = _read(op.file_path)
|
||||
if read_err:
|
||||
errors.append(f"{op.file_path}: {read_err}")
|
||||
return
|
||||
simulated = content
|
||||
for hunk_index, hunk in enumerate(op.hunks, start=1):
|
||||
search_lines, replace_lines = _split_hunk(hunk)
|
||||
if not any(l.prefix in '-+' for l in hunk.lines):
|
||||
# Inert anchor hunk (context only) — models emit these
|
||||
# between real changes; ignore without failing the patch.
|
||||
continue
|
||||
real_change_count += 1
|
||||
if not search_lines:
|
||||
# Addition-only hunk: the context hint must be unique.
|
||||
if hunk.context_hint:
|
||||
occurrences, ambiguous = _hint_ambiguity(simulated, hunk.context_hint)
|
||||
if occurrences == 0:
|
||||
errors.append(f"{op.file_path}: addition-only hunk context hint '{hunk.context_hint}' not found")
|
||||
elif ambiguous:
|
||||
errors.append(f"{op.file_path}: addition-only hunk {ambiguous}")
|
||||
continue
|
||||
search_pattern = '\n'.join(search_lines)
|
||||
replacement = '\n'.join(replace_lines)
|
||||
if search_lines == replace_lines:
|
||||
# Identical -/+ lines: apply skips it as a no-op, so
|
||||
# validation must not reject it with the identical-strings error.
|
||||
continue
|
||||
new_simulated, count, _strategy, match_error = fuzzy_find_and_replace(
|
||||
simulated, search_pattern, replacement, replace_all=False
|
||||
)
|
||||
if count:
|
||||
simulated = new_simulated
|
||||
elif not is_already_applied(simulated or "", search_pattern, replacement):
|
||||
# Already-applied hunks (edit landed in a prior call) are no-ops so
|
||||
# multi-hunk patches don't fail wholesale; apply performs the same skip.
|
||||
label = f"'{hunk.context_hint}'" if hunk.context_hint else "(no hint)"
|
||||
errors.append(
|
||||
f"{op.file_path}: hunk {hunk_index} {label} not found"
|
||||
+ (f" — {match_error}" if match_error else "")
|
||||
+ _no_match_hint(match_error, search_pattern, simulated)
|
||||
)
|
||||
pending_content[op.file_path] = simulated
|
||||
|
||||
for op in operations:
|
||||
if op.operation != OperationType.UPDATE:
|
||||
real_change_count += 1
|
||||
if op.operation == OperationType.UPDATE:
|
||||
content, read_err = _read(op.file_path)
|
||||
if read_err:
|
||||
errors.append(f"{op.file_path}: {read_err}")
|
||||
continue
|
||||
|
||||
simulated = content
|
||||
for hunk_index, hunk in enumerate(op.hunks, start=1):
|
||||
search_lines, replace_lines = _split_hunk(hunk)
|
||||
if not any(l.prefix in '-+' for l in hunk.lines):
|
||||
# Inert anchor hunk (context only) — models emit these
|
||||
# between real changes; ignore without failing the patch.
|
||||
continue
|
||||
real_change_count += 1
|
||||
if not search_lines:
|
||||
# Addition-only hunk: the context hint must be unique.
|
||||
if hunk.context_hint:
|
||||
occurrences = _count_occurrences(simulated, hunk.context_hint)
|
||||
if occurrences == 0:
|
||||
errors.append(
|
||||
f"{op.file_path}: addition-only hunk context hint "
|
||||
f"'{hunk.context_hint}' not found"
|
||||
)
|
||||
elif occurrences > 1:
|
||||
errors.append(
|
||||
f"{op.file_path}: addition-only hunk context hint "
|
||||
f"'{hunk.context_hint}' is ambiguous "
|
||||
f"({occurrences} occurrences)"
|
||||
)
|
||||
continue
|
||||
|
||||
search_pattern = '\n'.join(search_lines)
|
||||
replacement = '\n'.join(replace_lines)
|
||||
if search_lines == replace_lines:
|
||||
# Identical -/+ lines: apply skips it as a no-op, so
|
||||
# validation must not reject it with the identical-strings error.
|
||||
continue
|
||||
|
||||
new_simulated, count, _strategy, match_error = fuzzy_find_and_replace(
|
||||
simulated, search_pattern, replacement, replace_all=False
|
||||
)
|
||||
if count == 0:
|
||||
# Already-applied hunk (edit landed in a prior call): treat
|
||||
# as a no-op so multi-hunk patches don't fail wholesale.
|
||||
# The apply phase performs the same skip.
|
||||
if is_already_applied(simulated or "", search_pattern, replacement):
|
||||
continue
|
||||
label = f"'{hunk.context_hint}'" if hunk.context_hint else "(no hint)"
|
||||
errors.append(
|
||||
f"{op.file_path}: hunk {hunk_index} {label} not found"
|
||||
+ (f" — {match_error}" if match_error else "")
|
||||
+ _no_match_hint(match_error, search_pattern, simulated)
|
||||
)
|
||||
else:
|
||||
simulated = new_simulated
|
||||
pending_content[op.file_path] = simulated
|
||||
|
||||
elif op.operation == OperationType.DELETE:
|
||||
_validate_update(op)
|
||||
continue
|
||||
real_change_count += 1
|
||||
if op.operation == OperationType.DELETE:
|
||||
_content, read_err = _read(op.file_path)
|
||||
if read_err:
|
||||
errors.append(f"{op.file_path}: file not found for deletion")
|
||||
else:
|
||||
removed_paths.add(op.file_path)
|
||||
pending_content.pop(op.file_path, None)
|
||||
|
||||
elif op.operation == OperationType.MOVE:
|
||||
if not op.new_path:
|
||||
errors.append(f"{op.file_path}: MOVE operation missing destination path")
|
||||
@@ -279,15 +260,12 @@ def _validate_operations(
|
||||
errors.append(f"{op.file_path}: source file not found for move")
|
||||
_dst, dst_err = _read(op.new_path)
|
||||
if not dst_err:
|
||||
errors.append(
|
||||
f"{op.new_path}: destination already exists — move would overwrite"
|
||||
)
|
||||
errors.append(f"{op.new_path}: destination already exists — move would overwrite")
|
||||
# Only a cleanly-validated move updates the overlay.
|
||||
if not src_err and dst_err:
|
||||
pending_content[op.new_path] = src_content if src_content is not None else ""
|
||||
pending_content.pop(op.file_path, None)
|
||||
removed_paths.add(op.file_path)
|
||||
|
||||
# ADD: parent directory creation handled by write_file; no pre-check needed.
|
||||
|
||||
if not errors and real_change_count == 0:
|
||||
@@ -299,8 +277,7 @@ def _validate_operations(
|
||||
ApplyResult = Tuple[bool, str, Optional[str], Optional[dict]]
|
||||
|
||||
|
||||
def apply_v4a_operations(operations: List[PatchOperation],
|
||||
file_ops: Any) -> 'PatchResult':
|
||||
def apply_v4a_operations(operations: List[PatchOperation], file_ops: Any) -> 'PatchResult':
|
||||
"""Validate all operations, then apply them (two-phase, atomic on validation failure).
|
||||
|
||||
A phase-2 failure (e.g. a race between validation and apply) is reported
|
||||
@@ -341,10 +318,7 @@ def apply_v4a_operations(operations: List[PatchOperation],
|
||||
if not ok:
|
||||
errors.append(f"Failed to {verb} {op.file_path}: {payload}")
|
||||
continue
|
||||
label = op.file_path
|
||||
if op.operation is OperationType.MOVE:
|
||||
label = f"{op.file_path} -> {op.new_path}"
|
||||
bucket.append(label)
|
||||
bucket.append(f"{op.file_path} -> {op.new_path}" if op.operation is OperationType.MOVE else op.file_path)
|
||||
all_diffs.append(payload)
|
||||
if lsp:
|
||||
lsp_blocks.append(lsp)
|
||||
@@ -376,18 +350,15 @@ def apply_v4a_operations(operations: List[PatchOperation],
|
||||
def _write_file_accepts_pre_content(file_ops: Any) -> bool:
|
||||
"""True when ``file_ops.write_file`` accepts a ``pre_content`` kwarg.
|
||||
|
||||
Decided from the signature rather than catching TypeError around the call,
|
||||
so a TypeError raised *inside* a capable write_file propagates instead of
|
||||
triggering a second, duplicate write. Unintrospectable callables get the
|
||||
basic two-argument form.
|
||||
Decided from the signature rather than catching TypeError around the call, so a
|
||||
TypeError raised *inside* a capable write_file propagates instead of triggering a
|
||||
second, duplicate write. Unintrospectable callables get the two-argument form.
|
||||
"""
|
||||
try:
|
||||
params = inspect.signature(file_ops.write_file).parameters
|
||||
except (TypeError, ValueError):
|
||||
return False
|
||||
return "pre_content" in params or any(
|
||||
p.kind is inspect.Parameter.VAR_KEYWORD for p in params.values()
|
||||
)
|
||||
return "pre_content" in params or any(p.kind is inspect.Parameter.VAR_KEYWORD for p in params.values())
|
||||
|
||||
|
||||
def _apply_add(op: PatchOperation, file_ops: Any) -> ApplyResult:
|
||||
@@ -426,15 +397,11 @@ def _apply_move(op: PatchOperation, file_ops: Any) -> ApplyResult:
|
||||
def _insert_addition_only(new_content: str, hunk: Hunk, insert_text: str) -> Tuple[Optional[str], Optional[str]]:
|
||||
"""Place an addition-only hunk after its context hint (or at EOF). Returns (content, error)."""
|
||||
if hunk.context_hint:
|
||||
occurrences = _count_occurrences(new_content, hunk.context_hint)
|
||||
if occurrences > 1:
|
||||
return None, (
|
||||
f"Addition-only hunk: context hint '{hunk.context_hint}' is ambiguous "
|
||||
f"({occurrences} occurrences) — provide a more unique hint"
|
||||
)
|
||||
occurrences, ambiguous = _hint_ambiguity(new_content, hunk.context_hint, " — provide a more unique hint")
|
||||
if ambiguous:
|
||||
return None, f"Addition-only hunk: {ambiguous}"
|
||||
if occurrences == 1:
|
||||
hint_pos = new_content.find(hunk.context_hint)
|
||||
eol = new_content.find('\n', hint_pos)
|
||||
eol = new_content.find('\n', new_content.find(hunk.context_hint))
|
||||
if eol != -1:
|
||||
return new_content[:eol + 1] + insert_text + '\n' + new_content[eol + 1:], None
|
||||
return new_content + '\n' + insert_text, None
|
||||
@@ -472,17 +439,16 @@ def _apply_update(op: PatchOperation, file_ops: Any) -> ApplyResult:
|
||||
continue
|
||||
|
||||
# Retry inside a window around the context hint, if any.
|
||||
if hunk.context_hint:
|
||||
hint_pos = new_content.find(hunk.context_hint)
|
||||
if hint_pos != -1:
|
||||
window_start = max(0, hint_pos - 500)
|
||||
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
|
||||
)
|
||||
if count > 0:
|
||||
new_content = new_content[:window_start] + window_new + new_content[window_end:]
|
||||
error = None
|
||||
hint_pos = new_content.find(hunk.context_hint) if hunk.context_hint else -1
|
||||
if hint_pos != -1:
|
||||
window_start = max(0, hint_pos - 500)
|
||||
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
|
||||
)
|
||||
if count > 0:
|
||||
new_content = new_content[:window_start] + window_new + new_content[window_end:]
|
||||
error = None
|
||||
if error:
|
||||
# Mirror the validation-phase already-applied skip, or the two
|
||||
# phases disagree and the whole patch fails here.
|
||||
@@ -499,9 +465,7 @@ def _apply_update(op: PatchOperation, file_ops: Any) -> ApplyResult:
|
||||
return False, write_result.error, None, None
|
||||
|
||||
diff = ''.join(difflib.unified_diff(
|
||||
current_content.splitlines(keepends=True),
|
||||
new_content.splitlines(keepends=True),
|
||||
fromfile=f"a/{op.file_path}",
|
||||
tofile=f"b/{op.file_path}",
|
||||
current_content.splitlines(keepends=True), new_content.splitlines(keepends=True),
|
||||
fromfile=f"a/{op.file_path}", tofile=f"b/{op.file_path}",
|
||||
))
|
||||
return True, diff, getattr(write_result, "lsp_diagnostics", None), getattr(write_result, "lint", None)
|
||||
|
||||
Reference in New Issue
Block a user