Skip to content

The agent that grows with you

fix: scope one-shot aux usage to the branch and keep it on failed turns

teknium11a08c487bebb
diff --git a/hermes_cli/oneshot.py b/hermes_cli/oneshot.py
index 6cfca651252..c329d7c4b80 100644
--- a/hermes_cli/oneshot.py
+++ b/hermes_cli/oneshot.py
@@ -51,14 +51,16 @@ def _auxiliary_usage(session_db, session_id: Optional[str]) -> dict[str, dict]:
return {}
-def _attach_auxiliary_usage(result: dict, session_db, before: dict[str, dict]) -> None:
+def _attach_auxiliary_usage(result: dict, session_db, before: dict[str, dict],
+ fallback_session_id: Optional[str] = None) -> None:
"""Store this run's auxiliary usage on *result* as the delta against the pre-turn snapshot
(a resumed session already carries earlier runs' aux rows). Waits (bounded) for the auto-title
- thread first: it bills from a daemon thread and can still be in flight when the turn returns."""
+ thread first: it bills from a daemon thread and can still be in flight when the turn returns.
+ Failed-turn dicts carry no ``session_id``; *fallback_session_id* keeps the delta readable then."""
from agent.title_generator import wait_for_title_upgrades
wait_for_title_upgrades()
- after = _auxiliary_usage(session_db, result.get("session_id"))
+ after = _auxiliary_usage(session_db, result.get("session_id") or fallback_session_id)
by_task: dict[str, dict] = {}
for task, counters in after.items():
prior = before.get(task, {})
@@ -557,7 +559,8 @@ def _run_agent(
aux_before = _auxiliary_usage(session_db, resume_sid) if ledger else {}
result = agent.run_conversation(prompt, conversation_history=conversation_history or None)
if ledger:
- _attach_auxiliary_usage(result, session_db, aux_before)
+ _attach_auxiliary_usage(result, session_db, aux_before,
+ fallback_session_id=agent.session_id or resume_sid)
return (result.get("final_response") or "", result)
finally:
_close_agent(agent, session_db)
diff --git a/hermes_state_usage.py b/hermes_state_usage.py
index 77c85ecb44e..06ea514c554 100644
--- a/hermes_state_usage.py
+++ b/hermes_state_usage.py
@@ -397,6 +397,12 @@ class SessionUsageMixin:
if not session_id:
return {}
chain = self._session_lineage_root_to_tip(session_id)
+ # An explicit ``/branch`` copy owns its spend: cut the walk at the nearest branch node so a
+ # resumed branch never absorbs aux rows billed to its source (hermes_state_messages does the same).
+ for i in range(len(chain) - 1, -1, -1):
+ if self._is_explicit_branch_session(chain[i]):
+ chain = chain[i:]
+ break
rows = self._read_all(
f"""SELECT task,
COALESCE(SUM(api_call_count), 0) AS api_calls,