From 0560f2615b7571382f6c901012b3054272f91231 Mon Sep 17 00:00:00 2001 From: kosta Date: Thu, 2 Jul 2026 12:51:57 -0400 Subject: [PATCH] fix(state-sync): mirror WebUI API call counts --- api/state_sync.py | 4 +++- api/streaming.py | 1 + tests/test_issue2762_state_sync_profile_kwarg.py | 4 +++- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/api/state_sync.py b/api/state_sync.py index 07cc1ccb..9f1b9da5 100644 --- a/api/state_sync.py +++ b/api/state_sync.py @@ -135,7 +135,8 @@ def sync_session_start(session_id: str, model=None, profile: Optional[str] = Non def sync_session_usage(session_id: str, input_tokens: int=0, output_tokens: int=0, estimated_cost=None, model=None, title: Optional[str] = None, message_count: Optional[int] = None, profile: Optional[str] = None, - cache_read_tokens: int = 0, cache_write_tokens: int = 0) -> None: + cache_read_tokens: int = 0, cache_write_tokens: int = 0, + api_call_count: Optional[int] = None) -> None: """Update token usage and title for a WebUI session in state.db. Called after each turn completes. Uses absolute=True to set totals (the WebUI Session already accumulates across turns). @@ -165,6 +166,7 @@ def sync_session_usage(session_id: str, input_tokens: int=0, output_tokens: int= output_tokens=output_tokens, cache_read_tokens=cache_read_tokens, cache_write_tokens=cache_write_tokens, + api_call_count=api_call_count, estimated_cost_usd=estimated_cost, model=model, absolute=True, diff --git a/api/streaming.py b/api/streaming.py index 6b32c7f4..d8046bb6 100644 --- a/api/streaming.py +++ b/api/streaming.py @@ -9289,6 +9289,7 @@ def _run_agent_streaming( message_count=len(s.messages), cache_read_tokens=s.cache_read_tokens or 0, cache_write_tokens=s.cache_write_tokens or 0, + api_call_count=getattr(agent, 'session_api_calls', None), # #2762: pass the session's profile explicitly so the # background-thread state.db lookup doesn't fall # through to the process-global active profile and diff --git a/tests/test_issue2762_state_sync_profile_kwarg.py b/tests/test_issue2762_state_sync_profile_kwarg.py index a420e9dc..2c5f79a5 100644 --- a/tests/test_issue2762_state_sync_profile_kwarg.py +++ b/tests/test_issue2762_state_sync_profile_kwarg.py @@ -89,7 +89,7 @@ def _read_session(db_path: Path, session_id: str): # column names so the test queries the actual schema. cur = conn.execute( "SELECT id AS session_id, title, input_tokens, output_tokens, " - "cache_read_tokens, cache_write_tokens " + "cache_read_tokens, cache_write_tokens, api_call_count " "FROM sessions WHERE id = ?", (session_id,), ) @@ -217,6 +217,7 @@ def test_sync_session_usage_writes_only_to_named_profile(two_profile_homes): profile='maiko', cache_read_tokens=1200, cache_write_tokens=300, + api_call_count=7, ) maiko_row = _read_session(two_profile_homes['maiko'] / 'state.db', '2762-regression') @@ -226,6 +227,7 @@ def test_sync_session_usage_writes_only_to_named_profile(two_profile_homes): "sync_session_usage(profile='maiko') did not write to maiko's state.db" assert maiko_row["cache_read_tokens"] == 1200 assert maiko_row["cache_write_tokens"] == 300 + assert maiko_row["api_call_count"] == 7 assert hiyuki_row is None, \ "sync_session_usage(profile='maiko') leaked into hiyuki's state.db — #2762 regression"