Skip to content

The agent that grows with you

fix(cli): /resume and -c restore the session's todo list

kshitijk4poorb342119d770b
diff --git a/hermes_cli/gateway_agent.py b/hermes_cli/gateway_agent.py
index 87f3b9dd2fb..9a1a086702f 100644
--- a/hermes_cli/gateway_agent.py
+++ b/hermes_cli/gateway_agent.py
@@ -351,6 +351,20 @@ class GatewayAgent:
return True
# -- session identity ----------------------------------------------------------------------
+ @property
+ def _todo_store(self):
+ return self._todos
+
+ @_todo_store.setter
+ def _todo_store(self, store):
+ """Main swaps in a fresh ``TodoStore`` after /new, /resume and /branch move ``session_id`` (and the
+ constructor builds one): it starts with the gateway's snapshot of that session, so a resumed
+ plan is there before its next ``todo.updated``."""
+ self._todos = store
+ snapshot = self._state().todo_state
+ if snapshot:
+ self._set_todos(snapshot)
+
@property
def session_id(self):
return self._session_id
diff --git a/hermes_cli/gateway_connection.py b/hermes_cli/gateway_connection.py
index 3b397fff11c..f012b34b763 100644
--- a/hermes_cli/gateway_connection.py
+++ b/hermes_cli/gateway_connection.py
@@ -68,7 +68,7 @@ class SessionState:
self.session_id = session_id
self.info: dict = {}
self.title: str | None = None
- self.todos: list = []
+ self.todo_state: dict | None = None # the session's {todos, revision}: resume snapshot, then todo.updated
self.revision = None
self.subscription_id = None
self.execution_generation = None
@@ -110,6 +110,9 @@ class SessionState:
self.subscription_id = snapshot.get("subscription_id", self.subscription_id)
self.execution_generation = snapshot.get("execution_generation", self.execution_generation)
self.messages = list(snapshot.get("messages") or [])
+ if "todo_state" in snapshot: # null clears an earlier plan; a missing key (older gateway) keeps it
+ todo_state = snapshot["todo_state"]
+ self.todo_state = dict(todo_state) if isinstance(todo_state, dict) else None
if isinstance(snapshot.get("pending"), list):
self.pending = list(snapshot["pending"])
self.pending_seq += 1 # a (re)attach replays no frames: the idle tick checks this viewer's /queue rows
@@ -131,7 +134,7 @@ class SessionState:
if payload.get("title"):
self.title = payload["title"]
elif kind == "todo.updated" and isinstance(payload, dict):
- self.todos = list(payload.get("todos") or [])
+ self.todo_state = {"todos": list(payload.get("todos") or []), "revision": payload.get("revision")}
elif kind == "session.usage" and isinstance(payload, dict):
# The gateway's frame carries the flat counters next to a nested TUI ``usage`` view this
# reader can't map (``session_usage.usage_frame``): the counters win when present.
diff --git a/tests/gateway/test_resume_todo_state.py b/tests/gateway/test_resume_todo_state.py
index 6f61f0daa23..d3b999888f6 100644
--- a/tests/gateway/test_resume_todo_state.py
+++ b/tests/gateway/test_resume_todo_state.py
@@ -60,3 +60,16 @@ async def test_resume_leaves_the_gateway_processs_stdout_alone(tmp_path, monkeyp
assert sys.stdout is stdout
assert 'tui_gateway.server' not in sys.modules
+
+def test_a_later_null_resume_clears_the_clients_todo_seed():
+ """The client keeps the reply's snapshot until the next one: a later resume of a session with no plan
+ (``todo_state: null``) clears it, so a fresh ``TodoStore`` is not seeded with a plan that is gone. A
+ reply without the field (an older gateway) keeps it."""
+ from hermes_cli.gateway_connection import SessionState
+ state = SessionState("local-abc")
+ state.adopt_snapshot({"todo_state": {"todos": TODOS, "revision": 2}})
+ assert state.todo_state == {"todos": TODOS, "revision": 2}
+ state.adopt_snapshot({})
+ assert state.todo_state == {"todos": TODOS, "revision": 2}
+ state.adopt_snapshot({"todo_state": None})
+ assert state.todo_state is None
diff --git a/tests/hermes_cli/test_gateway_agent.py b/tests/hermes_cli/test_gateway_agent.py
index eb3fca0d233..76d08218ea0 100644
--- a/tests/hermes_cli/test_gateway_agent.py
+++ b/tests/hermes_cli/test_gateway_agent.py
@@ -1018,3 +1018,50 @@ def test_todo_updates_after_a_session_switch_reach_the_cli_todo_store(make_agent
_sync_agent_to_session(cli, SID, parent_session_id="", reason="resume")
agent.run_conversation("plan")
assert agent._todo_store.read() == todos
+
+
+def _resume_with_todos(client, todos, revision):
+ """``client``'s ``session.resume`` answers as the gateway does for a session with a plan."""
+ original = client.rpc
+
+ async def rpc(method, *, _timeout=None, **params):
+ reply = await original(method, _timeout=_timeout, **params)
+ if method == "session.resume":
+ reply = dict(reply, stored_session_id=params["session_id"],
+ todo_state={"todos": todos, "revision": revision})
+ return reply
+ client.rpc = rpc
+
+
+def test_resume_restores_the_sessions_todos_into_the_cli_todo_store(make_agent):
+ """/resume and /branch attach through ``session.resume``; main then swaps in a fresh ``TodoStore``,
+ which starts with the gateway's ``todo_state`` (revision kept), not empty until the next ``todo.updated``."""
+ from types import SimpleNamespace
+
+ from hermes_cli.cli_commands_mixin import _sync_agent_to_session
+ from hermes_cli.gateway_session_store import GatewaySessionStore
+ todos = [{"id": "1", "content": "ship it", "status": "in_progress"}]
+ agent, client = make_agent([])
+ _resume_with_todos(client, todos, 4)
+ agent._session_db = GatewaySessionStore(agent.connection)
+ _sync_agent_to_session(SimpleNamespace(agent=agent, conversation_history=[]), "local-other",
+ parent_session_id="", reason="resume")
+ assert agent._todo_store.snapshot() == {"todos": todos, "revision": 4}
+
+
+def test_startup_resume_builds_the_agent_with_the_sessions_todos(make_agent):
+ """``chat -c`` attaches (``open_session``) before it builds the agent, whose store starts with the
+ snapshot; a later ``todo.updated`` of that session is what the next swapped-in store starts with."""
+ from hermes_cli.gateway_session_store import GatewaySessionStore
+ from tools.todo_tool import TodoStore
+ todos = [{"id": "1", "content": "ship it", "status": "completed"}]
+ agent, client = make_agent([])
+ _resume_with_todos(client, todos, 2)
+ store = GatewaySessionStore(agent.connection)
+ store.attach("local-resumed")
+ resumed = GatewayAgent(connection=agent.connection, session_db=store, session_id="local-resumed")
+ assert resumed._todo_store.snapshot() == {"todos": todos, "revision": 2}
+ later = [dict(todos[0], status="in_progress")]
+ agent.connection.state("local-resumed").apply_event("todo.updated", {"todos": later, "revision": 3})
+ resumed._todo_store = TodoStore()
+ assert resumed._todo_store.snapshot() == {"todos": later, "revision": 3}