Skip to content

The agent that grows with you

fix(telemetry): CLI /undo N counts the user turns undo_last really removed

teknium15816a768e4f0
diff --git a/hermes_cli/cli_loops_mixin.py b/hermes_cli/cli_loops_mixin.py
index 92f6858c3c4..1821495bbe0 100644
--- a/hermes_cli/cli_loops_mixin.py
+++ b/hermes_cli/cli_loops_mixin.py
@@ -214,10 +214,9 @@ class CLILoopsMixin:
else f"This removes the last {_undo_n} user turns from history.")
if self._confirm_destructive_slash("undo", _undo_desc, cmd_original=cmd_original) is None:
return True # confirmation cancelled — command handled, keep REPL alive
- _history = self.conversation_history
- self.undo_last(_undo_n)
- if self.conversation_history is not _history: # only a successful rewind installs a new history
- self._record_model_friction("undo", min(_undo_n, sum(isinstance(m, dict) and m.get("role") == "user" for m in _history)))
+ turns_undone = self.undo_last(_undo_n)
+ if turns_undone: # None when nothing was rewound
+ self._record_model_friction("undo", turns_undone)
def _cmd_skills(self, cmd_original: str):
with self._busy_command(self._slow_command_status(cmd_original)):
diff --git a/hermes_cli/cli_session_mixin.py b/hermes_cli/cli_session_mixin.py
index f785283881b..4770af8650b 100644
--- a/hermes_cli/cli_session_mixin.py
+++ b/hermes_cli/cli_session_mixin.py
@@ -766,7 +766,8 @@ class CLISessionMixin:
turn). Rows are soft-deleted in SessionDB (``active=0``, kept for audit), memory
providers get ``on_session_switch(rewound=True)``, and the agent is patched like
/branch does. ``prefill=False`` is for programmatic callers (checkpoint rollback)
- that must not touch the input buffer.
+ that must not touch the input buffer. Returns the number of user turns undone (None when
+ nothing changed).
"""
from cli import logger
if not self.conversation_history:
@@ -820,6 +821,7 @@ class CLISessionMixin:
# Editable, not auto-sent (Claude-Code-style).
if prefill and removed_text:
self._prefill_input_buffer(removed_text)
+ return turns_undone
@staticmethod
def _undo_content_to_text(content) -> str:
diff --git a/tests/hermes_cli/test_shared_metrics_model_attribution.py b/tests/hermes_cli/test_shared_metrics_model_attribution.py
index 168f84546e6..a6c3dfe913e 100644
--- a/tests/hermes_cli/test_shared_metrics_model_attribution.py
+++ b/tests/hermes_cli/test_shared_metrics_model_attribution.py
@@ -314,6 +314,20 @@ def test_cli_undo_counts_friction_only_when_something_was_undone(direct_runtime,
({"model": PUBLIC, "provider": "openrouter", "signal": "undo"}, 1)]
+def test_cli_undo_n_counts_the_user_turns_actually_undone_not_compaction_handoffs(direct_runtime, tmp_path):
+ from agent.context_compressor import COMPRESSED_SUMMARY_METADATA_KEY, SUMMARY_PREFIX
+
+ cli = _cli(None)
+ cli.conversation_history = [
+ {"role": "user", "content": SUMMARY_PREFIX + "\nearlier work", COMPRESSED_SUMMARY_METADATA_KEY: True},
+ {"role": "assistant", "content": "ack"},
+ {"role": "user", "content": "q1"}, {"role": "assistant", "content": "a1"},
+ {"role": "user", "content": "q2"}, {"role": "assistant", "content": "a2"}]
+ cli._cmd_undo("/undo 5")
+ _flush()
+ assert sum(value for _, value in _stored_values(tmp_path, "hermes.wasted_tokens.count")) == 2
+
+
def test_context_peak_is_one_row_per_conversation_across_compression_rotation(direct_runtime, tmp_path):
"""Compression hands the session id off (s1 -> s1c): the
conversation reports its fullest segment once, whatever order the segments close in. A