Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions gateway/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -4068,6 +4068,7 @@ async def _handle_message_with_agent(self, event, source, _quick_key: str):
self.session_store.update_session(
session_entry.session_key,
last_prompt_tokens=agent_result.get("last_prompt_tokens", 0),
total_tokens=agent_result.get("session_total_tokens", 0),
Copy link

Copilot AI Apr 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agent_result.get("session_total_tokens", 0) will write 0 into the session store whenever the key is missing (e.g., proxy-mode results and early error returns that don't include session_total_tokens). That can clobber a previously accumulated SessionEntry.total_tokens and reintroduce the “Tokens: 0” issue after any non-token-reporting run. Prefer passing None when the key is absent (or conditionally include the kwarg only when present) so update_session() leaves the stored value unchanged.

Suggested change
total_tokens=agent_result.get("session_total_tokens", 0),
total_tokens=agent_result.get("session_total_tokens"),

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Critical: wrong key name. agent.run_conversation() returns total_tokens (run_agent.py:10721), not session_total_tokens. This will always resolve to 0. Change to agent_result.get("total_tokens", 0).

)

# Auto voice reply: send TTS audio before the text response
Expand Down Expand Up @@ -8712,11 +8713,14 @@ def _approval_notify_sync(approval_data: dict) -> None:
_last_prompt_toks = 0
_input_toks = 0
_output_toks = 0
_session_total_toks = 0
_agent = agent_holder[0]
if _agent and hasattr(_agent, "context_compressor"):
_last_prompt_toks = getattr(_agent.context_compressor, "last_prompt_tokens", 0)
_input_toks = getattr(_agent, "session_prompt_tokens", 0)
_output_toks = getattr(_agent, "session_completion_tokens", 0)
if _agent:
_session_total_toks = getattr(_agent, "session_total_tokens", 0) or 0
_resolved_model = getattr(_agent, "model", None) if _agent else None

if not final_response:
Expand All @@ -8732,6 +8736,7 @@ def _approval_notify_sync(approval_data: dict) -> None:
"last_prompt_tokens": _last_prompt_toks,
"input_tokens": _input_toks,
"output_tokens": _output_toks,
"session_total_tokens": _session_total_toks,
"model": _resolved_model,
}

Expand Down Expand Up @@ -8821,6 +8826,7 @@ def _approval_notify_sync(approval_data: dict) -> None:
"last_prompt_tokens": _last_prompt_toks,
"input_tokens": _input_toks,
"output_tokens": _output_toks,
"session_total_tokens": _session_total_toks,
"model": _resolved_model,
"session_id": effective_session_id,
"response_previewed": result.get("response_previewed", False),
Expand Down
3 changes: 3 additions & 0 deletions gateway/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,7 @@ def update_session(
self,
session_key: str,
last_prompt_tokens: int = None,
total_tokens: int = None,
) -> None:
"""Update lightweight session metadata after an interaction."""
with self._lock:
Expand All @@ -780,6 +781,8 @@ def update_session(
entry.updated_at = _now()
if last_prompt_tokens is not None:
entry.last_prompt_tokens = last_prompt_tokens
if total_tokens is not None:
entry.total_tokens = total_tokens
self._save()

def suspend_session(self, session_key: str) -> bool:
Expand Down
1 change: 1 addition & 0 deletions tests/gateway/test_status_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ async def test_handle_message_persists_agent_token_counts(monkeypatch):
runner.session_store.update_session.assert_called_once_with(
session_entry.session_key,
last_prompt_tokens=80,
total_tokens=0,
)
Comment on lines 150 to 154
Copy link

Copilot AI Apr 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test currently expects total_tokens=0 even though the mocked _run_agent() result does not include session_total_tokens. That assertion will still pass if the implementation mistakenly defaults missing totals to 0 (which can overwrite real stored totals). Update the mock result to include a non-zero session_total_tokens and assert it’s passed through (or, if missing is valid, assert update_session() is called without total_tokens).

Copilot uses AI. Check for mistakes.


Expand Down
Loading