Skip to content

The agent that grows with you

fix(cli): the first turn after /personality keeps main's exact context figure (no `~`)

Tekniumd5c40dcdb491
diff --git a/hermes_cli/gateway_agent.py b/hermes_cli/gateway_agent.py
index 6eeeb7518a4..268a838fc11 100644
--- a/hermes_cli/gateway_agent.py
+++ b/hermes_cli/gateway_agent.py
@@ -131,6 +131,7 @@ class GatewayAgent:
self._context_tokens = 0
self._turn_calls_base = 0
self._turn_ask_index = None
+ self._turn_api_calls = None
self._usage_stale = False
self._current_task_id = self._current_turn_id = self._current_api_request_id = ""
self._memory_manager = None
@@ -396,6 +397,7 @@ class GatewayAgent:
task_id=None, persist_user_message=None, turn_author=None, **_kw):
history = list(conversation_history or [])
self._turn_ask_index = len(history) # the staged ask's row in this turn's transcript
+ self._turn_api_calls = None
self._turn_calls_base = self.session_api_calls
self._usage_stale = True
self._interrupt_requested = False
@@ -714,6 +716,8 @@ class GatewayAgent:
out["messages"].append({"role": "assistant", "content": out["final_response"]})
out["session_id"] = self.session_id
self._session_messages = out["messages"]
+ calls = stored.get("api_calls")
+ self._turn_api_calls = calls if isinstance(calls, int) and not isinstance(calls, bool) else None
self._apply_usage(stored, usage)
return out
@@ -746,7 +750,12 @@ class GatewayAgent:
last_user = ask if isinstance(ask, int) and 0 <= ask < len(messages) and messages[ask].get("role") == "user" \
else next((i for i in range(len(messages) - 1, -1, -1) if messages[i].get("role") == "user"),
len(messages) - 1)
- base = last_user + 1 if self.session_api_calls > self._turn_calls_base else last_user
+ # Did this turn complete an API call? The turn result's own count when the gateway sent one: a
+ # rebuilt gateway agent (/personality, /model) restarts its session counters, so the cumulative
+ # delta can read "no call" for a turn that answered.
+ turn_calls = self._turn_api_calls
+ made_call = turn_calls > 0 if isinstance(turn_calls, int) else self.session_api_calls > self._turn_calls_base
+ base = last_user + 1 if made_call else last_user
if base <= 0:
self._turn_base_usage_anchor = None
return
diff --git a/tests/hermes_cli/test_gateway_agent.py b/tests/hermes_cli/test_gateway_agent.py
index b7aa62c671e..4a38016271e 100644
--- a/tests/hermes_cli/test_gateway_agent.py
+++ b/tests/hermes_cli/test_gateway_agent.py
@@ -497,3 +497,23 @@ def test_a_mid_turn_steer_row_is_estimated_delta_like_main(make_agent):
agent.run_conversation("slow", conversation_history=[{"role": "user", "content": "a"},
{"role": "assistant", "content": "b"}])
assert agent._turn_base_usage_anchor["base_count"] == 3 # history + the ask, not the steer row
+
+
+def test_first_turn_on_a_rebuilt_gateway_agent_keeps_the_exact_figure(make_agent):
+ """/personality or /model rebuilds the gateway's agent, whose session counters restart: the turn
+ result's own api_calls says the turn answered, so its reply is anchored (no `~`), as on main."""
+ usage = dict(SESSION_USAGE, context_tokens=120, api_calls=1) # restarted counters
+ agent, client = make_agent([frame("message.complete", {"text": "arr", "outcome": "completed"})],
+ result={"final_response": "arr", "api_calls": 1,
+ "messages": [{"role": "assistant", "content": "arr"}]})
+ original = client.rpc
+
+ async def rpc(method, *, _timeout=None, **params):
+ if method == "session.stats":
+ return usage
+ return await original(method, _timeout=_timeout, **params)
+ client.rpc = rpc
+ agent.session_api_calls = 9 # what the retired agent's counters had reached
+ agent.run_conversation("who are you", conversation_history=[{"role": "user", "content": "a"},
+ {"role": "assistant", "content": "b"}])
+ assert agent._turn_base_usage_anchor["base_count"] == 3 # anchored after the ask: no estimated delta