From 9c87a7c79e9b14366f5dd9aa5b46cebde868cfd4 Mon Sep 17 00:00:00 2001 From: kshitijk4poor <82637225+kshitijk4poor@users.noreply.github.com> Date: Fri, 28 Aug 2026 09:00:26 +0530 Subject: [PATCH] refactor(macos): use shared atomic_write_text for the anchor marker /simplify-code reuse finding: _write_marker reimplemented the mkstemp->write->os.replace pattern that utils.atomic_write_text already provides as the repo's shared atomic-text-write helper (and the shared version adds fsync + cross-device/busy-file fallbacks). --- hermes_cli/macos_tcc_anchor.py | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/hermes_cli/macos_tcc_anchor.py b/hermes_cli/macos_tcc_anchor.py index 99b0d76c16..3bceddb7ec 100644 --- a/hermes_cli/macos_tcc_anchor.py +++ b/hermes_cli/macos_tcc_anchor.py @@ -51,6 +51,7 @@ from pathlib import Path from hermes_constants import venv_python_path from hermes_cli.managed_uv import _RUNTIME_DIR_NAME +from utils import atomic_write_text logger = logging.getLogger(__name__) @@ -164,23 +165,17 @@ def _anchor_marker(venv_bin: Path) -> Path: def _write_marker(venv_bin: Path, source_file: Path) -> None: - """Write the anchor marker atomically (write-then-rename). + """Write the anchor marker atomically via the shared helper. A concurrent ensure (update + doctor --fix) must never observe a partially-written marker: a torn read would compare unequal and trigger a spurious reinstall, and ``write_text`` alone is not atomic. """ - fd, tmp_name = tempfile.mkstemp(prefix=f"{_MARKER_NAME}.", dir=str(venv_bin)) - try: - with os.fdopen(fd, "w", encoding="utf-8") as fh: - fh.write(_marker_value(source_file)) - os.replace(tmp_name, _anchor_marker(venv_bin)) - except OSError: - try: - os.unlink(tmp_name) - except OSError: - pass - raise + atomic_write_text( + _anchor_marker(venv_bin), + _marker_value(source_file), + tmp_prefix=f"{_MARKER_NAME}.", + ) def _store_root(source_file: Path) -> Path: