hermes-agent/hermes_cli
Teknium b61d9b297a refactor: consolidate symlink-safe atomic replace into shared helper
Extract the islink/realpath guard from the 16743 fix into a single
atomic_replace() helper in utils.py, then migrate every os.replace()
call site in the codebase to use it.

The original PR #16777 correctly identified and fixed the bug, but
only patched 9 of ~24 call sites. The same bug class (managed
deployments that symlink state files silently losing the link on
every write) still existed at auth.json, sessions file, gateway
config, env_loader, webhook subscriptions, debug store, model
catalog, pairing, google OAuth, nous rate guard, and more.

Rather than add another 10+ copies of the same three-line guard,
consolidate into atomic_replace(tmp, target) which:
- resolves symlinks via os.path.realpath before os.replace
- returns the resolved real path so callers can re-apply permissions
- is a drop-in replacement for os.replace at the use sites

Changes:
- utils.py: new atomic_replace() helper + atomic_json_write /
  atomic_yaml_write now call it instead of inlining the guard
- 16 files: all os.replace() call sites migrated to atomic_replace()
  - agent/{google_oauth, nous_rate_guard, shell_hooks}.py
  - cron/jobs.py
  - gateway/{pairing, session, platforms/telegram}.py
  - hermes_cli/{auth, config, debug, env_loader, model_catalog, webhook}.py
  - tools/{memory_tool, skill_manager_tool, skills_sync}.py

Tests: tests/test_atomic_replace_symlinks.py pins the invariant for
atomic_replace + atomic_json_write + atomic_yaml_write, covers plain
files, first-time creates, broken symlinks, and permission preservation.

Refs #16743
Builds on #16777 by @vominh1919.
2026-04-28 04:58:22 -07:00
..
__init__.py
auth.py refactor: consolidate symlink-safe atomic replace into shared helper 2026-04-28 04:58:22 -07:00
auth_commands.py
azure_detect.py
backup.py
banner.py
callbacks.py
claw.py
cli_output.py
clipboard.py
codex_models.py
colors.py
commands.py
completion.py
config.py refactor: consolidate symlink-safe atomic replace into shared helper 2026-04-28 04:58:22 -07:00
copilot_auth.py
cron.py
curses_ui.py
debug.py refactor: consolidate symlink-safe atomic replace into shared helper 2026-04-28 04:58:22 -07:00
default_soul.py
dingtalk_auth.py
doctor.py
dump.py
env_loader.py refactor: consolidate symlink-safe atomic replace into shared helper 2026-04-28 04:58:22 -07:00
fallback_cmd.py
gateway.py
hooks.py
logs.py
main.py
mcp_config.py
memory_setup.py
model_catalog.py refactor: consolidate symlink-safe atomic replace into shared helper 2026-04-28 04:58:22 -07:00
model_normalize.py
model_switch.py
models.py
nous_subscription.py
oneshot.py
pairing.py
platforms.py
plugins.py
plugins_cmd.py
profiles.py
providers.py
pty_bridge.py
runtime_provider.py
setup.py
skills_config.py
skills_hub.py
skin_engine.py
slack_cli.py
status.py
timeouts.py
tips.py
tools_config.py
uninstall.py
voice.py
web_server.py
webhook.py refactor: consolidate symlink-safe atomic replace into shared helper 2026-04-28 04:58:22 -07:00