"""Fail-closed helpers shared by Hermes source patches.""" def replace_once(source: str, before: str, after: str, label: str) -> str: """Replace one anchored upstream fragment and fail closed on source drift.""" count = source.count(before) if count != 1: raise SystemExit( f"Hermes {label} patch context changed: expected 1, found {count}" ) return source.replace(before, after, 1)