Files
hermes-agent/hermes_cli/cli_undo_hint.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

39 lines
1.8 KiB
Python

"""Classic CLI ``/undo``: the read-only line naming the files the undone turns changed and pointing at ``/rollback``.
The in-process CLI asks its agent's checkpoint manager over the same cwd its ``/rollback`` lists; a
CLI rewired onto the shared gateway asks the owner (``rollback.list`` with ``undo_since``), whose
tools took the checkpoints. Either way the line is advisory: any failure prints nothing.
"""
from __future__ import annotations
import contextlib
import os
def _undone_span(before: list, turns_undone: int):
"""``(target stamp, kept prefix)`` of the oldest undone user turn in the pre-undo history."""
from hermes_cli.cli_session_mixin import _user_turn_indices
indices = _user_turn_indices(before)
if not indices or turns_undone < 1:
return None, []
cut = indices[max(len(indices) - turns_undone, 0)]
return before[cut].get("timestamp"), before[:cut]
def print_undo_rollback_hint(cli, before: list, turns_undone: int) -> None:
from hermes_cli.gateway_side import connection_for
from agent.checkpoints.undo_hint import undo_hint_payload, undo_span_start
target_ts, kept = _undone_span(before, turns_undone)
if target_ts is None:
return
hint = None
if (connection := connection_for(cli)) is not None:
with contextlib.suppress(Exception): # an older owner refuses the param: no line, as on main
hint = connection.call("rollback.list", session_id=cli.session_id,
undo_since=undo_span_start(target_ts, kept)).get("undo_hint")
else:
mgr = getattr(getattr(cli, "agent", None), "_checkpoint_mgr", None)
hint = undo_hint_payload(mgr, os.getenv("TERMINAL_CWD", os.getcwd()), undo_span_start(target_ts, kept))
if text := (hint or {}).get("text"):
print(f" {text}")