agent ctx-badge: seed Bus::last_usage from latest turn_stats row on startup

This commit is contained in:
müde 2026-05-18 18:00:48 +02:00
commit f827187341
4 changed files with 47 additions and 0 deletions

View file

@ -156,6 +156,34 @@ impl TurnStats {
tracing::warn!(error = ?e, "turn_stats: insert failed");
}
}
/// Token counts from the most recently inserted row, if any. Lets
/// the harness seed `Bus::last_usage` on startup so the per-agent
/// web UI's `ctx-badge` paints with real numbers on cold load
/// instead of waiting for the next `TokenUsageChanged` SSE event.
/// Best-effort: any sqlite error returns `None` and the caller
/// falls back to the empty state.
#[must_use]
pub fn last_usage(&self) -> Option<crate::events::TokenUsage> {
let conn = self.inner.lock().unwrap();
conn.query_row(
"SELECT input_tokens, output_tokens,
cache_read_input_tokens, cache_creation_input_tokens
FROM turn_stats
ORDER BY started_at DESC
LIMIT 1",
[],
|row| {
Ok(crate::events::TokenUsage {
input_tokens: u64::try_from(row.get::<_, i64>(0)?).unwrap_or(0),
output_tokens: u64::try_from(row.get::<_, i64>(1)?).unwrap_or(0),
cache_read_input_tokens: u64::try_from(row.get::<_, i64>(2)?).unwrap_or(0),
cache_creation_input_tokens: u64::try_from(row.get::<_, i64>(3)?).unwrap_or(0),
})
},
)
.ok()
}
}
fn default_path() -> PathBuf {