#!/usr/bin/env python3 """Apply fail-closed Hermes identity and PWA patches to pinned WebUI source.""" from __future__ import annotations import os from pathlib import Path ROOT = Path(os.environ.get("HERMES_WEBUI_PATCH_ROOT", "/opt/hermes-webui")) def replace_exact(path: Path, before: str, after: str, count: int = 1) -> None: """Replace one exact upstream fragment and reject pin drift.""" source = path.read_text(encoding="utf-8") if source.count(before) != count: raise SystemExit( f"Hermes brand patch context changed in {path}: {before[:80]!r}" ) path.write_text(source.replace(before, after, count), encoding="utf-8") def replace_between_exact(path: Path, start: str, end: str, after: str) -> None: """Replace one uniquely bounded upstream region and reject ambiguous input.""" source = path.read_text(encoding="utf-8") if source.count(start) != 1 or source.count(end) != 1: raise SystemExit( f"Hermes brand patch context changed in {path}: {start[:80]!r}" ) start_index = source.index(start) end_index = source.index(end, start_index) path.write_text(source[:start_index] + after + source[end_index:], encoding="utf-8") index = ROOT / "static/index.html" replace_exact(index, "Hermes", "Hermes Chat") replace_exact( index, """ """, """ """, ) replace_exact( index, '', '', ) replace_exact( index, '', '', ) replace_exact( index, '', '', ) replace_exact( index, '', '', ) replace_exact( index, '', '', ) replace_exact( index, "var c=t==='dark'?'#141425':'#FAF7F0';", "var c=t==='dark'?'#0D1420':'#E8F1F2';", ) replace_exact( index, '', '\n' '', ) replace_between_exact( index, '