refactor(tools): group H pass 2 — inline xlsx/pdf helpers, layout compaction, docstring tightening

This commit is contained in:
Teknium
2026-09-02 22:42:07 -07:00
parent 3dabf9ebf1
commit 0a07fc0a22
11 changed files with 117 additions and 201 deletions
+34 -49
View File
@@ -61,17 +61,13 @@ _OP_MARKERS: List[Tuple[OperationType, re.Pattern]] = [
(OperationType.UPDATE, re.compile(r'\*\*\*\s*Update\s+File:\s*(.+)')),
(OperationType.ADD, re.compile(r'\*\*\*\s*Add\s+File:\s*(.+)')),
(OperationType.DELETE, re.compile(r'\*\*\*\s*Delete\s+File:\s*(.+)')),
(OperationType.MOVE, re.compile(r'\*\*\*\s*Move\s+File:\s*(.+?)\s*->\s*(.+)')),
]
(OperationType.MOVE, re.compile(r'\*\*\*\s*Move\s+File:\s*(.+?)\s*->\s*(.+)'))]
_HINT_RE = re.compile(r'@@\s*(.+?)\s*@@')
def parse_v4a_patch(patch_content: str) -> Tuple[List[PatchOperation], Optional[str]]:
"""Parse a V4A patch into operations.
Returns ``(operations, None)`` — ``[]`` for an empty patch is not an
error — or ``([], "Parse error: ...")`` for malformed operations.
"""
"""Parse a V4A patch -> ``(operations, None)`` (``[]`` for an empty patch is not an
error) or ``([], "Parse error: ...")`` for malformed operations."""
# Tolerate CRLF bodies: a stray ``\r`` would otherwise end up in every
# HunkLine.content and defeat the anchored Begin/End markers.
lines = [ln[:-1] if ln.endswith('\r') else ln for ln in patch_content.split('\n')]
@@ -99,15 +95,15 @@ def parse_v4a_patch(patch_content: str) -> Tuple[List[PatchOperation], Optional[
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()
current_op = PatchOperation(
operation=kind,
file_path=m.group(1).strip(),
new_path=m.group(2).strip() if kind is OperationType.MOVE else None,
)
new_path=m.group(2).strip() if kind is OperationType.MOVE else None)
# UPDATE hunks start lazily (at '@@' or the first hunk line); ADD
# collects all '+' lines into one hunk; DELETE/MOVE are complete.
current_hunk = Hunk() if kind is OperationType.ADD else None
@@ -166,16 +162,14 @@ 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, (f"context hint '{hint}' is ambiguous "
f"({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
post-earlier-hunk content, exactly as the apply phase will see it.
"""
"""Dry-run every operation; return error strings (empty list = safe to apply). UPDATE
hunks are simulated in order so later hunks see post-earlier-hunk content, as apply will."""
from tools.fuzzy_match import fuzzy_find_and_replace, is_already_applied
errors: List[str] = []
@@ -213,7 +207,8 @@ def _validate_operations(operations: List[PatchOperation], file_ops: Any) -> Lis
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")
errors.append(f"{op.file_path}: addition-only hunk context hint "
f"'{hunk.context_hint}' not found")
elif ambiguous:
errors.append(f"{op.file_path}: addition-only hunk {ambiguous}")
continue
@@ -224,8 +219,7 @@ def _validate_operations(operations: List[PatchOperation], file_ops: Any) -> Lis
# 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
)
simulated, search_pattern, replacement, replace_all=False)
if count:
simulated = new_simulated
elif not is_already_applied(simulated or "", search_pattern, replacement):
@@ -235,8 +229,7 @@ def _validate_operations(operations: List[PatchOperation], file_ops: Any) -> Lis
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)
)
+ _no_match_hint(match_error, search_pattern, simulated))
pending_content[op.file_path] = simulated
for op in operations:
@@ -279,11 +272,8 @@ ApplyResult = Tuple[bool, str, Optional[str], Optional[dict]]
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
with a note to run ``git diff`` since state may be inconsistent.
``file_ops`` needs ``read_file_raw``, ``write_file``, ``delete_file``, ``move_file``.
"""
A phase-2 failure (validate/apply race) is reported with a ``git diff`` note since state
may be inconsistent. ``file_ops`` needs read_file_raw/write_file/delete_file/move_file."""
from tools.file_operations import PatchResult # avoid circular import
validation_errors = _validate_operations(operations, file_ops)
@@ -291,8 +281,7 @@ def apply_v4a_operations(operations: List[PatchOperation], file_ops: Any) -> 'Pa
return PatchResult(
success=False,
error="Patch validation failed (no files were modified):\n"
+ "\n".join(f" • {e}" for e in validation_errors),
)
+ "\n".join(f" • {e}" for e in validation_errors))
files_modified: List[str] = []
files_created: List[str] = []
@@ -308,8 +297,7 @@ def apply_v4a_operations(operations: List[PatchOperation], file_ops: Any) -> 'Pa
OperationType.ADD: (_apply_add, files_created, "add"),
OperationType.DELETE: (_apply_delete, files_deleted, "delete"),
OperationType.MOVE: (_apply_move, files_modified, "move"),
OperationType.UPDATE: (_apply_update, files_modified, "update"),
}
OperationType.UPDATE: (_apply_update, files_modified, "update")}
for op in operations:
try:
@@ -318,7 +306,10 @@ def apply_v4a_operations(operations: List[PatchOperation], file_ops: Any) -> 'Pa
if not ok:
errors.append(f"Failed to {verb} {op.file_path}: {payload}")
continue
bucket.append(f"{op.file_path} -> {op.new_path}" if op.operation is OperationType.MOVE else op.file_path)
label = op.file_path
if op.operation is OperationType.MOVE:
label = f"{op.file_path} -> {op.new_path}"
bucket.append(label)
all_diffs.append(payload)
if lsp:
lsp_blocks.append(lsp)
@@ -335,30 +326,26 @@ def apply_v4a_operations(operations: List[PatchOperation], file_ops: Any) -> 'Pa
files_created=files_created,
files_deleted=files_deleted,
lint=lint_results if lint_results else None,
lsp_diagnostics="\n\n".join(lsp_blocks) if lsp_blocks else None,
)
lsp_diagnostics="\n\n".join(lsp_blocks) if lsp_blocks else None)
if errors:
return PatchResult(
success=False,
error="Apply phase failed (state may be inconsistent — run `git diff` to assess):\n"
+ "\n".join(f" • {e}" for e in errors),
**result_kwargs,
)
**result_kwargs)
return PatchResult(success=True, **result_kwargs)
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 two-argument form.
"""
"""True when ``file_ops.write_file`` accepts ``pre_content``. Decided from the signature,
not by catching TypeError around the call, so a TypeError raised *inside* a capable
write_file propagates instead of triggering a duplicate write."""
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:
@@ -382,8 +369,7 @@ def _apply_delete(op: PatchOperation, file_ops: Any) -> ApplyResult:
return False, result.error, None, None
diff = ''.join(difflib.unified_diff(
read_result.content.splitlines(keepends=True), [],
fromfile=f"a/{op.file_path}", tofile="/dev/null",
))
fromfile=f"a/{op.file_path}", tofile="/dev/null"))
return True, diff or f"# Deleted: {op.file_path}", None, None
@@ -397,7 +383,8 @@ 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, ambiguous = _hint_ambiguity(new_content, hunk.context_hint, " — 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:
@@ -433,8 +420,7 @@ def _apply_update(op: PatchOperation, file_ops: Any) -> ApplyResult:
search_pattern = '\n'.join(search_lines)
replacement = '\n'.join(replace_lines)
new_content, count, _strategy, error = fuzzy_find_and_replace(
new_content, search_pattern, replacement, replace_all=False
)
new_content, search_pattern, replacement, replace_all=False)
if not (error and count == 0):
continue
@@ -466,6 +452,5 @@ def _apply_update(op: PatchOperation, file_ops: Any) -> ApplyResult:
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}",
))
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)