Files
hermes-agent/tui_gateway/checkpoints.py
alt-glitch 458a0046ad refactor(agent): six checkpoint files move from tools/ to agent/checkpoints/
tools/ holds model tools. These six files register none: they snapshot and
restore a working directory for the agent loop, /rollback, /undo's
changed-files line and `hermes checkpoints`. They move next to the agent
loop that takes the checkpoints:

  tools/checkpoint_manager.py                -> agent/checkpoints/manager.py
  tools/checkpoint_maintenance.py            -> agent/checkpoints/maintenance.py
  tools/checkpoint_pruning.py                -> agent/checkpoints/pruning.py
  tools/checkpoint_refs.py                   -> agent/checkpoints/refs.py
  tools/checkpoint_undo_hint.py              -> agent/checkpoints/undo_hint.py
  tools/checkpoint_manager_profile_rename.py -> agent/checkpoints/manager_profile_rename.py

agent/checkpoints/__init__.py is a docstring only. Nine test files move to
tests/agent/checkpoints/. Every importer changes its import line in place,
inside the same function. Nothing stays at the old paths: none of the six
names is on the plugin API list, the shipped-release list or the
old-updater list.

Behaviour is unchanged. The four loggers keep their tools.checkpoint_*
names, so the lines stay under `hermes logs --component tools`. The folder
is not a root checkpoints/ package because the checkpoint store is
HERMES_HOME/checkpoints, which is the same directory as such a package on an
install whose checkout root is HERMES_HOME. agent.* is already packaged, so
pyproject.toml and the lists of root packages do not change.

The import and docs rewrites were made by scripts/move/rewrite_imports.py
and rewrite_docs.py.
2026-11-16 19:21:55 -06:00

79 lines
3.7 KiB
Python

"""Checkpoint-enablement resolution for TUI-created agents (issue #79625).
Desktop sessions (``hermes serve`` → TUI gateway) never set ``HERMES_TUI_CHECKPOINTS``
(that env var is only set by the ``hermes --tui --checkpoints`` CLI path), so agent
creation previously gated filesystem checkpointing on the env var alone and silently
ignored ``checkpoints.enabled`` in config.yaml. These helpers resolve enablement the
way the messaging gateway does (``gateway/run.py`` ``_checkpoint_agent_kwargs``):
the env var (explicit CLI opt-in) wins when set, otherwise the ``checkpoints``
config section applies with ``DEFAULT_CONFIG`` defaults.
"""
import os
from utils import is_truthy_value
def _load_checkpoints_enabled(cfg: dict | None = None) -> bool:
"""Whether filesystem checkpoints are enabled for TUI-created agents.
Keeps the legacy ``checkpoints: true`` bool form working and falls back to the
``DEFAULT_CONFIG`` default when the section is absent or malformed.
"""
cp_cfg = (cfg or {}).get("checkpoints", {})
if isinstance(cp_cfg, bool):
cp_cfg = {"enabled": cp_cfg}
elif not isinstance(cp_cfg, dict):
cp_cfg = {}
from hermes_cli.config import DEFAULT_CONFIG
return bool(cp_cfg.get("enabled", DEFAULT_CONFIG["checkpoints"]["enabled"]))
def resolve_checkpoints_enabled(cfg: dict | None = None) -> bool:
"""Resolve checkpoint enablement with the env-var override first.
``HERMES_TUI_CHECKPOINTS`` (set by ``hermes --tui --checkpoints``) is the
explicit opt-in and wins when present; otherwise fall back to the
``checkpoints`` config section (issue #79625 — the desktop backend never
sets the env var, so config was being silently ignored).
"""
env_val = os.environ.get("HERMES_TUI_CHECKPOINTS")
if env_val is not None:
return is_truthy_value(env_val)
return _load_checkpoints_enabled(cfg)
def _resolve_checkpoint_hash(mgr, cwd: str, ref: str) -> str:
"""``/rollback`` number or hash → the checkpoint's hash (``agent/checkpoints/refs.py``, every surface's rule)."""
from agent.checkpoints.refs import resolve_checkpoint_ref
return resolve_checkpoint_ref(mgr.list_checkpoints(cwd), ref)
def undo_rollback_hint_for(session: dict, target_message: dict, kept_history: list) -> dict | None:
"""``/undo``'s changed-files line for a sidecar session (``agent/checkpoints/undo_hint.py``): the same
manager, cwd and container refusal its ``rollback.*`` methods use. None: no line.
The store is the SESSION's profile's (``_resolve_checkpoint_base`` reads the bound HERMES_HOME), so the
whole read runs under that profile's runtime scope: an RPC thread otherwise holds the launch profile's."""
mgr = getattr(session.get("agent"), "_checkpoint_mgr", None)
if mgr is None or not mgr.enabled:
return None
from agent.checkpoints.undo_hint import undo_hint_payload, undo_span_start
from tui_gateway import server # method bodies are bound onto server.py's globals (method_ctx.py)
def hint(mgr, cwd): # under the same profile scope rollback.restore runs in (_with_checkpoints)
if server._container_checkpoint_refusal(session, mgr, cwd):
return None
since = undo_span_start((target_message or {}).get("timestamp"), kept_history)
return undo_hint_payload(mgr, cwd, since, task_id=None)
return server._with_checkpoints(session, hint)
def undo_hint_fields(session: dict, rewound, params: dict) -> dict:
"""``session.undo``'s ``undo_hint`` field for a rewind ``(kept, live_view, count)``; ``{}`` for none or /retry."""
if not rewound or not rewound[2] or params.get("intent") == "retry":
return {}
hint = undo_rollback_hint_for(session, rewound[1], rewound[0])
return {"undo_hint": hint} if hint else {}