The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(tools): pass the Unicode edit plan explicitly; tighten JSON constant scan
kshitijk4poor5d579c514463
diff --git a/hermes_cli/subcommands/backup.py b/hermes_cli/subcommands/backup.pyindex ea317bd8d6e..00b94447405 100644--- a/hermes_cli/subcommands/backup.py+++ b/hermes_cli/subcommands/backup.py@@ -7,7 +7,7 @@ from typing import Callabledef _non_negative_keep(value: str) -> int:- # The one --keep validation: cmd_backup -> run_backup is the only caller.+ # A negative keep would slice away the NEWEST archives instead of the oldest.if not value.isdecimal():raise ArgumentTypeError("must be a non-negative integer (0 keeps everything)")return int(value)diff --git a/tools/file_operations.py b/tools/file_operations.pyindex c51cba24bf7..de21a872b92 100644--- a/tools/file_operations.py+++ b/tools/file_operations.py@@ -171,6 +171,8 @@ def _split_segments(output: str, sentinel: str) -> list[str]:def _json_nonstandard_constant(text: str) -> Optional[str]:"""First NaN/Infinity/-Infinity in ``text`` when it is otherwise valid JSON,else None. ``json.loads`` accepts these JavaScript extensions by default."""+ if "NaN" not in text and "Infinity" not in text:+ return Nonefound: list[str] = []def note_constant(value: str) -> float:@@ -178,8 +180,8 @@ def _json_nonstandard_constant(text: str) -> Optional[str]:return float("nan")try:- json.loads(text.lstrip(_UTF8_BOM), parse_constant=note_constant)- except Exception: # noqa: BLE001 — unparseable text has no constant to report+ json.loads(_strip_bom(text)[0], parse_constant=note_constant)+ except (ValueError, RecursionError): # unparseable text has no constant to reportreturn Nonereturn found[0] if found else Nonediff --git a/tools/fuzzy_match.py b/tools/fuzzy_match.pyindex d5f8e84f54f..0291a43ea1a 100644--- a/tools/fuzzy_match.py+++ b/tools/fuzzy_match.py@@ -392,7 +392,8 @@ def fuzzy_find_and_replace(content: str, old_string: str, new_string: str,new_content = _apply_replacements(content, matches, effective_new,old_string=old_string if strategy_name != "exact" else None,- preserve_unicode=strategy_name == "unicode_normalized")+ unicode_plan=(_unicode_edit_plan(old_string, effective_new)+ if strategy_name == "unicode_normalized" else None))_note_edit_match(strategy_name)return new_content, len(matches), strategy_name, None@@ -557,12 +558,12 @@ def _unicode_edit_plan(old_string: str, new_string: str) -> tuple[str, list]:return norm_old, SequenceMatcher(None, norm_old, new_string).get_opcodes()-def _preserve_unicode_in_replacement(file_region: str, old_string: str, new_string: str,- plan: Optional[tuple[str, list]] = None) -> str:+def _preserve_unicode_in_replacement(file_region: str, new_string: str,+ plan: tuple[str, list]) -> str:"""Apply only the old->new edits onto ``file_region``'s original (Unicode) text,so a unicode_normalized match doesn't flatten the file's em-dashes/smart quotes.- ``plan`` is a precomputed ``_unicode_edit_plan(old_string, new_string)``."""- norm_old, opcodes = plan or _unicode_edit_plan(old_string, new_string)+ ``plan`` is ``_unicode_edit_plan(old_string, new_string)``."""+ norm_old, opcodes = planif norm_old != _unicode_normalize(file_region):return new_string # strategy shouldn't have fired; fall back@@ -582,19 +583,19 @@ def _preserve_unicode_in_replacement(file_region: str, old_string: str, new_stridef _apply_replacements(content: str, matches: list[Span],new_string: str, old_string: Optional[str] = None,- preserve_unicode: bool = False) -> str:+ unicode_plan: Optional[tuple[str, list]] = None) -> str:"""Splice ``new_string`` over each span (end-to-start so offsets stay valid);- ``old_string`` non-None (non-exact match) re-indents it per region."""+ ``old_string`` non-None (non-exact match) re-indents it per region, and+ ``unicode_plan`` (unicode_normalized match) keeps each region's typography."""result = content- plan = _unicode_edit_plan(old_string, new_string) if preserve_unicode and old_string is not None else Nonefor start, end in sorted(matches, key=lambda x: x[0], reverse=True):adjusted = new_stringif old_string is not None:region = content[start:end]- if plan is not None:+ if unicode_plan is not None:# Each occurrence may use different typographic characters even# though all normalize to the same old_string.- adjusted = _preserve_unicode_in_replacement(region, old_string, adjusted, plan)+ adjusted = _preserve_unicode_in_replacement(region, adjusted, unicode_plan)adjusted = _reindent_replacement(region, old_string, adjusted)result = result[:start] + adjusted + result[end:]return result