fix(state-sync): mirror WebUI API call counts
This commit is contained in:
parent
059a06b70d
commit
0560f2615b
3 changed files with 7 additions and 2 deletions
|
|
@ -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,
|
def sync_session_usage(session_id: str, input_tokens: int=0, output_tokens: int=0,
|
||||||
estimated_cost=None, model=None, title: Optional[str] = None,
|
estimated_cost=None, model=None, title: Optional[str] = None,
|
||||||
message_count: Optional[int] = None, profile: 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.
|
"""Update token usage and title for a WebUI session in state.db.
|
||||||
Called after each turn completes. Uses absolute=True to set totals
|
Called after each turn completes. Uses absolute=True to set totals
|
||||||
(the WebUI Session already accumulates across turns).
|
(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,
|
output_tokens=output_tokens,
|
||||||
cache_read_tokens=cache_read_tokens,
|
cache_read_tokens=cache_read_tokens,
|
||||||
cache_write_tokens=cache_write_tokens,
|
cache_write_tokens=cache_write_tokens,
|
||||||
|
api_call_count=api_call_count,
|
||||||
estimated_cost_usd=estimated_cost,
|
estimated_cost_usd=estimated_cost,
|
||||||
model=model,
|
model=model,
|
||||||
absolute=True,
|
absolute=True,
|
||||||
|
|
|
||||||
|
|
@ -9289,6 +9289,7 @@ def _run_agent_streaming(
|
||||||
message_count=len(s.messages),
|
message_count=len(s.messages),
|
||||||
cache_read_tokens=s.cache_read_tokens or 0,
|
cache_read_tokens=s.cache_read_tokens or 0,
|
||||||
cache_write_tokens=s.cache_write_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
|
# #2762: pass the session's profile explicitly so the
|
||||||
# background-thread state.db lookup doesn't fall
|
# background-thread state.db lookup doesn't fall
|
||||||
# through to the process-global active profile and
|
# through to the process-global active profile and
|
||||||
|
|
|
||||||
|
|
@ -89,7 +89,7 @@ def _read_session(db_path: Path, session_id: str):
|
||||||
# column names so the test queries the actual schema.
|
# column names so the test queries the actual schema.
|
||||||
cur = conn.execute(
|
cur = conn.execute(
|
||||||
"SELECT id AS session_id, title, input_tokens, output_tokens, "
|
"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 = ?",
|
"FROM sessions WHERE id = ?",
|
||||||
(session_id,),
|
(session_id,),
|
||||||
)
|
)
|
||||||
|
|
@ -217,6 +217,7 @@ def test_sync_session_usage_writes_only_to_named_profile(two_profile_homes):
|
||||||
profile='maiko',
|
profile='maiko',
|
||||||
cache_read_tokens=1200,
|
cache_read_tokens=1200,
|
||||||
cache_write_tokens=300,
|
cache_write_tokens=300,
|
||||||
|
api_call_count=7,
|
||||||
)
|
)
|
||||||
|
|
||||||
maiko_row = _read_session(two_profile_homes['maiko'] / 'state.db', '2762-regression')
|
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"
|
"sync_session_usage(profile='maiko') did not write to maiko's state.db"
|
||||||
assert maiko_row["cache_read_tokens"] == 1200
|
assert maiko_row["cache_read_tokens"] == 1200
|
||||||
assert maiko_row["cache_write_tokens"] == 300
|
assert maiko_row["cache_write_tokens"] == 300
|
||||||
|
assert maiko_row["api_call_count"] == 7
|
||||||
assert hiyuki_row is None, \
|
assert hiyuki_row is None, \
|
||||||
"sync_session_usage(profile='maiko') leaked into hiyuki's state.db — #2762 regression"
|
"sync_session_usage(profile='maiko') leaked into hiyuki's state.db — #2762 regression"
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue