Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
feat(cli): show the session prompt-cache hit rate
thinhlpgf417ef401f96
diff --git a/src-tauri/src/core/cli/tui.rs b/src-tauri/src/core/cli/tui.rsindex eef9c9a7d..2974b27ba 100644--- a/src-tauri/src/core/cli/tui.rs+++ b/src-tauri/src/core/cli/tui.rs@@ -2144,6 +2144,36 @@ struct App {/// provider-config lock, which a turn in flight can hold, and a row keyed/// on a momentary `None` would split one model's spend in two.model_provider: Option<(String, Option<String>)>,+ /// Whether the last request reported any cache field at all, an honest zero+ /// included. Without it a route that reports zero reads renders identically+ /// to one that reports nothing, and the zero-hit case is the expensive one:+ /// a prefix rewritten every turn and never read.+ turn_cache_reported: bool,+ /// Session-cumulative prompt tokens: what the provider billed across every+ /// request this process made, how much of it came from its prompt cache, and+ /// how much it wrote into that cache. Sums, not means -- the number that+ /// matters is the share of this session's prompt tokens served from cache,+ /// and a 10-token request does not weigh the same as a 100K one. Unlike the+ /// `turn_*` pair above these survive turn boundaries and a compaction, since+ /// the tokens really were spent; they are dropped only when the conversation+ /// they describe is gone ([`App::reset_session`]) or replaced wholesale+ /// ([`load_thread`]).+ session_prompt_tokens: u64,+ session_cached_tokens: u64,+ session_cache_write_tokens: u64,+ /// Latched the first time a route reports a cache field, so "this route+ /// reports zero reads" stays distinguishable from "this route reports+ /// nothing" on requests that omit the fields. Where the usage is read+ /// through the OpenAI-shaped client, a reported zero is normalized to+ /// absent before it reaches us, so a route that has never reported a+ /// *positive* read still reads as nothing here (see+ /// `genai_bridge::completion_json`).+ session_cache_reported: bool,+ /// Set when the loaded history was not produced by this process (`/resume`,+ /// a fork of a saved thread). The counters above then start empty, so every+ /// readout names that scope rather than reporting a rate that silently+ /// excludes the earlier turns.+ session_cache_partial: bool,/// Transcript viewport rect from the last draw, for mapping mouse clicks/// to rows.transcript_rect: Rect,@@ -2593,6 +2623,12 @@ impl App {model_provider: None,turn_cached_tokens: 0,turn_cache_write_tokens: 0,+ turn_cache_reported: false,+ session_prompt_tokens: 0,+ session_cached_tokens: 0,+ session_cache_write_tokens: 0,+ session_cache_reported: false,+ session_cache_partial: false,transcript_rect: Rect::default(),last_scroll: 0,row_index: Vec::new(),@@ -2700,11 +2736,17 @@ impl App {self.turn_cache_write_tokens = 0;// `/usage` reports the session, and this is a new one.self.session_usage.clear();+ self.turn_cache_reported = false;self.tokens_per_sec = None;self.turn = (0, 0);self.detail.clear();self.scrollback = 0;self.last_kind = Kind::None;+ // The conversation the session counters described is gone, so they go+ // with it -- and the session is once again one this process owns from+ // its first message.+ self.reset_cache_usage();+ self.session_cache_partial = false;// A fresh session drops the todo projection and reminder state; the// model re-declares work with a new `todo init`.self.todos = crate::core::agent::todo::TodoList::default();@@ -2716,6 +2758,17 @@ impl App {self.reminder_awaiting_progress = false;}+ /// Forget the session-cumulative cache counters, and what the route has+ /// reported about its cache at all. For the cases where those numbers would+ /// describe a conversation, or a route, that is no longer the one on screen:+ /// a session that was cleared, resumed, or forked.+ fn reset_cache_usage(&mut self) {+ self.session_prompt_tokens = 0;+ self.session_cached_tokens = 0;+ self.session_cache_write_tokens = 0;+ self.session_cache_reported = false;+ }+/// Drop the provider's token measurement, because the history it measured is/// no longer the history we hold.///@@ -2730,6 +2783,7 @@ impl App {self.turn_prompt_tokens = 0;self.turn_cached_tokens = 0;self.turn_cache_write_tokens = 0;+ self.turn_cache_reported = false;}/// Drop any selection, and with it a copy armed but not yet lifted out of a@@ -4796,6 +4850,12 @@ impl App {turn_cached_tokens: self.turn_cached_tokens,turn_cache_write_tokens: self.turn_cache_write_tokens,session_cost: session_cost(&self.session_usage),+ turn_cache_reported: self.turn_cache_reported,+ session_prompt_tokens: self.session_prompt_tokens,+ session_cached_tokens: self.session_cached_tokens,+ session_cache_write_tokens: self.session_cache_write_tokens,+ session_cache_reported: self.session_cache_reported,+ session_cache_partial: self.session_cache_partial,}}}@@ -4819,6 +4879,17 @@ struct ContextSnapshot {/// published price was left out of it. Priced here, on the key path, so the/// off-loop task does not re-read the model catalog.session_cost: Option<(f64, bool)>,+ /// Whether the most recent request reported a cache field at all: a reported+ /// zero counts, an omitted field does not.+ turn_cache_reported: bool,+ /// Session-cumulative read/write/prompt totals and whether the route has+ /// ever reported a cache field, plus whether the counters start after the+ /// history did (a resume or fork).+ session_prompt_tokens: u64,+ session_cached_tokens: u64,+ session_cache_write_tokens: u64,+ session_cache_reported: bool,+ session_cache_partial: bool,}/// The `/context` breakdown for the current session, computed from an owned@@ -4933,10 +5004,11 @@ async fn compute_context_report(snapshot: ContextSnapshot) -> ContextReport {});let reported = !snapshot.tokens_estimated && snapshot.turn_prompt_tokens > 0;- // The cache figures share the fill's provenance: they describe the same- // measured request, so a fall-back-to-estimate turn has no cache line.- let cache_reported =- reported && (snapshot.turn_cached_tokens > 0 || snapshot.turn_cache_write_tokens > 0);+ // The last-request figures share the fill's provenance: they describe the+ // same measured request, so a fall-back-to-estimate turn has no line. The+ // gate is "that request reported a cache field", not "it reported a non-zero+ // one": zero reads is the state worth showing.+ let turn_cache_reported = reported && snapshot.turn_cache_reported;ContextReport {model_id: snapshot.model,window: snapshot.context_window,@@ -4947,10 +5019,15 @@ async fn compute_context_report(snapshot: ContextSnapshot) -> ContextReport {},fill_reported: reported,segments,- cache_reported,+ turn_cache_reported,cached_tokens: snapshot.turn_cached_tokens,cache_write_tokens: snapshot.turn_cache_write_tokens,session_cost: snapshot.session_cost,+ session_cache_reported: snapshot.session_cache_reported,+ session_cache_partial: snapshot.session_cache_partial,+ session_prompt_tokens: snapshot.session_prompt_tokens,+ session_cached_tokens: snapshot.session_cached_tokens,+ session_cache_write_tokens: snapshot.session_cache_write_tokens,}}@@ -5377,6 +5454,18 @@ impl App {let key = self.usage_key();self.session_usage.entry(key).or_default().add(&usage);self.turn_output_tokens += usage.completion_tokens.unwrap_or(0);+ // Session totals, summed over every request this process sent:+ // the denominator for the hit rate. A field the provider omits+ // contributes nothing, but a *reported* zero creates the latch --+ // that is what keeps an honest zero-hit route distinguishable+ // from one that says nothing about caching at all.+ self.session_prompt_tokens += usage.prompt_tokens.unwrap_or(0);+ self.session_cached_tokens += usage.cached_tokens.unwrap_or(0);+ self.session_cache_write_tokens += usage.cache_write_tokens.unwrap_or(0);+ if usage.cached_tokens.is_some() || usage.cache_write_tokens.is_some() {+ self.session_cache_reported = true;+ self.turn_cache_reported = true;+ }// Latest request's context, not a sum: each request resends the// whole conversation, so adding them would be meaningless.if let Some(prompt) = usage.prompt_tokens {@@ -5537,6 +5626,7 @@ impl App {self.turn_prompt_tokens = 0;self.turn_cached_tokens = 0;self.turn_cache_write_tokens = 0;+ self.turn_cache_reported = false;self.scrollback = 0;self.todo_call_this_turn = false;self.todo_ok_this_turn = false;@@ -6258,16 +6348,26 @@ struct ContextReport {/// Content categories plus free space and the autocompact buffer. Always/// exactly the seven bars rendered by the context view.segments: Vec<ContextSegment>,- /// Prompt-cache read/write from the most recent request, and whether the- /// provider reported either. `false` when the provider surfaces no cache- /// fields (every plain OpenAI-compatible endpoint that caches implicitly- /// without reporting), which suppresses the cache line entirely.- cache_reported: bool,+ /// Prompt-cache read/write from the most recent request, and whether that+ /// request reported a cache field at all -- a reported zero counts, an+ /// omitted field does not. The last-request line is a sample of one request,+ /// so it is only meaningful when that request said something about caching.+ turn_cache_reported: bool,cached_tokens: u64,cache_write_tokens: u64,/// Session spend and whether it is partial, from [`ContextSnapshot`]./// `None` suppresses the cost line: no model in play publishes prices.session_cost: Option<(f64, bool)>,+ /// Session-cumulative read/write/prompt totals, and whether the route has+ /// ever reported a cache field. The session share is the number the epic is+ /// about; `partial` says the counters start after the history did (a resume+ /// or fork), so the readout names that scope instead of quietly excluding+ /// the turns it never saw.+ session_cache_reported: bool,+ session_cache_partial: bool,+ session_prompt_tokens: u64,+ session_cached_tokens: u64,+ session_cache_write_tokens: u64,}impl ContextReport {@@ -6298,6 +6398,16 @@ fn format_tokens(tokens: u64) -> String {if tokens < 1_000 {return tokens.to_string();}+ // Session-cumulative cache totals run into the millions, where `1200K` reads+ // as noise. Same half-up tenths rule as the K branch, so no zero decimal.+ if tokens >= 1_000_000 {+ let tenths = (tokens + 50_000) / 100_000;+ return if tenths % 10 == 0 {+ format!("{}M", tenths / 10)+ } else {+ format!("{}.{}M", tenths / 10, tenths % 10)+ };+ }// Tenths of a thousand, half-up. Exact for every u64 below ~1.8e15.let tenths = (tokens + 50) / 100;if tokens < 10_000 && tenths % 10 != 0 {@@ -6388,33 +6498,81 @@ fn context_bank_bar(percent: f64, width: usize) -> (String, String) {(filled, empty)}-/// One-line prompt-cache readout for `/context`, or `None` when the provider-/// reported no cache activity. `read` as a share of the prompt is the number-/// that matters: it says how much of the prefix the provider actually served-/// from cache on the last request, i.e. whether prefix caching is working.-fn cache_summary_line(report: &ContextReport) -> Option<String> {- if !report.cache_reported {- return None;+/// Prompt-cache readout for `/context`: the session's hit rate above the last+/// request's sample, or a line saying the route reports no cache usage at all.+///+/// The session figure is `cached / prompt` over every request this process sent+/// -- a share of the tokens, not a mean of per-turn percentages, since a+/// 10-token request and a 100K one do not describe the same prefix. A route that+/// reports a cache field renders even when the value is zero: `0%` is the+/// expensive state (a prefix rewritten every turn and never read), and the whole+/// point of the split is that it cannot be mistaken for a route that reports+/// nothing, which says so in as many words. The style rides along so the rate+/// can turn red on a zero hit -- the alarm should read before the number does.+fn cache_summary_lines(report: &ContextReport) -> Vec<(String, Style)> {+ let mut lines = Vec::new();+ if report.session_cache_reported && report.session_prompt_tokens > 0 {+ let scope = if report.session_cache_partial {+ ", this process"+ } else {+ ""+ };+ let read_pct =+ cache_hit_percent(report.session_cached_tokens, report.session_prompt_tokens);+ let mut line = format!(+ "Prompt cache (session{scope}): {} read ({read_pct:.0}% of prompt)",+ format_tokens(report.session_cached_tokens)+ );+ if report.session_cache_write_tokens > 0 {+ line.push_str(&format!(+ ", {} written",+ format_tokens(report.session_cache_write_tokens)+ ));+ }+ let style = if read_pct == 0.0 {+ Style::new().red().bold()+ } else {+ Style::new().cyan()+ };+ lines.push((line, style));+ }+ if report.turn_cache_reported {+ let read_pct = cache_hit_percent(report.cached_tokens, report.fill);+ let mut line = format!(+ "Prompt cache (last request): {} read ({read_pct:.0}% of prompt)",+ format_tokens(report.cached_tokens)+ );+ if report.cache_write_tokens > 0 {+ line.push_str(&format!(+ ", {} written",+ format_tokens(report.cache_write_tokens)+ ));+ }+ lines.push((line, Style::new().cyan()));+ }+ if lines.is_empty() && report.fill_reported {+ // A measured request that reported no cache field. Saying so beats+ // printing a 0% the provider never claimed. Deliberately not "not+ // reported *by this provider*": the OpenAI-shaped path reads usage+ // through a client that normalizes a reported `0` to absent (see+ // `genai_bridge::completion_json`), so what this line can honestly+ // report is that no cache field arrived, not who withheld it.+ lines.push((+ "Prompt cache: not reported".to_string(),+ Style::new().dim(),+ ));}- // Clamped: normally `cached_tokens` is a subset of `fill` (prompt_tokens),- // but a raw Anthropic-shaped usage reports a prompt that excludes the cache- // read, which would push the share past 100%.- let read_pct = if report.fill > 0 {- (report.cached_tokens as f64 / report.fill as f64 * 100.0).min(100.0)- } else {- 0.0- };- let read = format_tokens(report.cached_tokens);- if report.cache_write_tokens > 0 {- Some(format!(- "Prompt cache (last request): {read} read ({read_pct:.0}% of prompt), {} written",- format_tokens(report.cache_write_tokens)- ))- } else {- Some(format!(- "Prompt cache (last request): {read} read ({read_pct:.0}% of prompt)"- ))+ lines+}++/// Share of a prompt the cache served, clamped to 100%: normally a cache read is+/// a subset of the prompt, but a raw Anthropic-shaped usage reports a prompt that+/// excludes it, which would push the share past 100%.+fn cache_hit_percent(cached: u64, prompt: u64) -> f64 {+ if prompt == 0 {+ return 0.0;}+ (cached as f64 / prompt as f64 * 100.0).min(100.0)}/// One-line session-spend readout for `/context`, or `None` when no model in@@ -6469,8 +6627,8 @@ fn context_lines(report: &ContextReport, width: usize) -> Vec<Line<'static>> {Style::new().bold(),)],];- if let Some(cache) = cache_summary_line(report) {- rows.push(vec![Span::styled(cache, Style::new().cyan())]);+ for (text, style) in cache_summary_lines(report) {+ rows.push(vec![Span::styled(text, style)]);}if let Some(cost) = cost_summary_line(report) {rows.push(vec![Span::styled(cost, Style::new().yellow())]);@@ -6579,8 +6737,8 @@ fn context_lines(report: &ContextReport, width: usize) -> Vec<Line<'static>> {),Style::new().bold(),)]);- if let Some(cache) = cache_summary_line(report) {- rows.push(vec![Span::styled(cache, Style::new().cyan())]);+ for (text, style) in cache_summary_lines(report) {+ rows.push(vec![Span::styled(text, style)]);}if let Some(cost) = cost_summary_line(report) {rows.push(vec![Span::styled(cost, Style::new().yellow())]);@@ -15604,6 +15762,12 @@ async fn load_thread(app: &mut App, thread: &serde_json::Value, verb: &str) {// any count left over from the thread we were on describes a different// conversation entirely. Estimate until a fresh response lands.app.invalidate_token_provenance();+ // The session cache counters describe requests *this* process sent, and the+ // thread they described is gone: a loaded history's turns were sent by+ // another run, or by the thread a fork came from. Start them over and mark+ // the scope, so no readout reports a rate that silently excludes those turns.+ app.reset_cache_usage();+ app.session_cache_partial = true;app.tokens = estimate_token_count(&app.history);let count = app.history@@ -18049,6 +18213,29 @@ fn header_spans(app: &App) -> Vec<Span<'static>> {app.context_window / 1000,app.context_window_source.label())));+ // Live prompt-cache hit rate: the share of this session's prompt tokens the+ // provider served from its cache, so a session that has begun to thrash is+ // visible while it runs rather than only from `/context`. Drawn only once+ // the route has reported a cache field -- no badge means "reports nothing",+ // never a fabricated 0% -- and red on a zero hit, the expensive state where+ // the prefix is rewritten every turn and never read.+ if app.session_cache_reported && app.session_prompt_tokens > 0 {+ let pct = cache_hit_percent(app.session_cached_tokens, app.session_prompt_tokens);+ // The counters begin where this process did, so a resumed or forked+ // history says so instead of implying a figure over turns this process+ // never sent.+ let scope = if app.session_cache_partial {+ " (this process)"+ } else {+ ""+ };+ let style = if pct == 0.0 {+ Style::new().red().bold()+ } else {+ Style::new().cyan()+ };+ spans.push(Span::styled(format!("cache {pct:.0}%{scope}"), style));+ }spans.push(Span::styled(elapsed, Style::new().dim()));// Output rate segment: last completed turn's tokens/sec, cached so it holds// steady instead of flickering to 0 between turns.@@ -18777,7 +18964,7 @@ mod tests {};use super::{agent_detail_lines, agent_picker_items, agents_column, background_shell_picker_items,- collapse_runs, open_agents_picker, trailing_repeat, SubagentPanel,+ cache_summary_lines, collapse_runs, open_agents_picker, trailing_repeat, SubagentPanel,};use crate::core::agent::events::{StreamEvent, Usage};use crate::core::agent::r#loop::PermissionRegistry;@@ -28689,6 +28876,39 @@ mod tests {app.join_journal();}+ /// A resumed thread's turns were sent by another run, so the session cache+ /// counters -- which describe requests *this* process made -- start over, and+ /// every readout names that scope instead of reporting a rate that silently+ /// excludes them.+ #[tokio::test]+ async fn resume_scopes_the_session_cache_counters_to_this_process() {+ let mut app = test_app();+ record_full_turn(&mut app);+ app.apply(StreamEvent::TurnUsage {+ usage: Usage {+ prompt_tokens: Some(1_000),+ cached_tokens: Some(900),+ ..Default::default()+ },+ });+ assert_eq!(app.session_cached_tokens, 900);++ let mut fresh = test_app();+ fresh.agent_dir = app.agent_dir.clone();+ apply_resume(&mut fresh, &ResumeRequest::resume(ResumeTarget::Latest)).await;++ assert_eq!(fresh.session_cached_tokens, 0, "another run's cache reads");+ assert_eq!(fresh.session_prompt_tokens, 0);+ assert!(+ !fresh.session_cache_reported,+ "the resumed route has not reported yet, so no rate may be claimed"+ );+ assert!(+ fresh.session_cache_partial,+ "the readout has to name the scope of what it did count"+ );+ }+#[tokio::test]async fn resume_restores_reasoning_tool_rows_and_diffs() {let mut app = test_app();@@ -31062,10 +31282,15 @@ mod tests {fill: used,fill_reported: false,segments,- cache_reported: false,+ turn_cache_reported: false,cached_tokens: 0,cache_write_tokens: 0,session_cost: None,+ session_cache_reported: false,+ session_cache_partial: false,+ session_prompt_tokens: 0,+ session_cached_tokens: 0,+ session_cache_write_tokens: 0,}}@@ -31307,40 +31532,162 @@ mod tests {assert!(transcript_text(&app).contains("no usage yet this session"));}- #[test]- fn context_view_shows_prompt_cache_when_reported() {+ /// A report with a measured fill and no cache fields, the starting point for+ /// the prompt-cache cases below.+ fn context_cache_report() -> ContextReport {let mut report = context_report(234_000, 35_000, [6_049, 9_000, 2_149, 8_049, 95_253]);report.fill = 120_000;report.fill_reported = true;- report.cache_reported = true;- report.cached_tokens = 90_000;- report.cache_write_tokens = 12_000;+ report+ }- let text = context_lines(&report, 80)+ fn context_text(report: &ContextReport) -> String {+ context_lines(report, 80).iter().map(line_text).collect::<Vec<_>>()- .join("\n");+ .join("\n")+ }++ /// The session share is the number the epic is about: every request this+ /// process sent, against every prompt token it billed -- not the last+ /// request's sample, which is one request dressed up as a rate.+ #[test]+ fn context_view_shows_the_session_cache_hit_rate() {+ let mut report = context_cache_report();+ report.session_cache_reported = true;+ report.session_prompt_tokens = 1_300_000;+ report.session_cached_tokens = 1_183_000;+ report.session_cache_write_tokens = 140_000;+ report.turn_cache_reported = true;+ report.cached_tokens = 90_000;+ report.cache_write_tokens = 12_000;++ let text = context_text(&report);+ assert!(+ text.contains("Prompt cache (session): 1.2M read (91% of prompt), 140K written"),+ "{text}"+ );assert!(text.contains("Prompt cache (last request): 90K read (75% of prompt), 12K written"),"{text}");}+ /// A route that reports zero cache reads renders `0%`, in the alarm colour.+ /// A prefix written every turn and never read is the most expensive state+ /// the agent can be in, and it used to render as nothing at all.#[test]- fn context_view_hides_prompt_cache_when_not_reported() {- let mut report = context_report(234_000, 35_000, [6_049, 9_000, 2_149, 8_049, 95_253]);- report.fill = 120_000;- report.fill_reported = true;- // cache_reported stays false (a plain OpenAI-compatible provider).- let text = context_lines(&report, 80)- .iter()- .map(line_text)- .collect::<Vec<_>>()- .join("\n");+ fn context_view_renders_a_zero_hit_rate_as_zero_percent() {+ let mut report = context_cache_report();+ report.session_cache_reported = true;+ report.session_prompt_tokens = 240_000;+ report.session_cache_write_tokens = 30_000;+ report.turn_cache_reported = true;+ report.cache_write_tokens = 30_000;++ let summary = cache_summary_lines(&report);+ assert_eq!(+ summary[0].0,+ "Prompt cache (session): 0 read (0% of prompt), 30K written"+ );+ assert_eq!(+ summary[0].1,+ Style::new().red().bold(),+ "a zero hit rate is the state to alarm on"+ );++ let text = context_text(&report);+ assert!(+ text.contains("Prompt cache (last request): 0 read (0% of prompt), 30K written"),+ "{text}"+ );+ assert!(!text.contains("not reported"), "{text}");+ }++ /// A measured request that reported no cache field says so. Printing a `0%`+ /// there would be a number the provider never claimed, and the previous+ /// blank line left an honest zero indistinguishable from this case.+ #[test]+ fn context_view_says_not_reported_when_the_route_reports_no_cache_fields() {+ let report = context_cache_report();++ let text = context_text(&report);+ assert!(+ text.contains("Prompt cache: not reported"),+ "{text}"+ );+ assert!(+ !text.contains("0% of prompt"),+ "a non-reporting route must not show a fabricated zero: {text}"+ );+ }++ /// Nothing is measured yet -- a cold open, or a resumed history before its+ /// first request -- so there is nothing to claim: no cache line, rather than+ /// a `0%` or a statement about a provider that has not answered.+ #[test]+ fn context_view_omits_the_cache_line_before_any_measurement() {+ let mut report = context_cache_report();+ report.fill_reported = false;++ let text = context_text(&report);assert!(!text.contains("Prompt cache"), "{text}");}+ /// A history loaded from disk was not sent by this process, so its rate is+ /// labelled instead of presented as the whole session's.+ #[test]+ fn context_view_labels_a_cache_rate_scoped_to_this_process() {+ let mut report = context_cache_report();+ report.session_cache_reported = true;+ report.session_cache_partial = true;+ report.session_prompt_tokens = 100_000;+ report.session_cached_tokens = 50_000;++ let text = context_text(&report);+ assert!(+ text.contains("Prompt cache (session, this process): 50K read (50% of prompt)"),+ "{text}"+ );+ }++ /// The session counters accumulate across a turn boundary while the per-turn+ /// sample resets, and a reported zero counts as reported -- otherwise the+ /// zero-hit state would erase itself from the rate.+ #[test]+ fn session_cache_counters_survive_a_turn_boundary() {+ let mut app = test_app();+ app.apply(StreamEvent::TurnUsage {+ usage: Usage {+ prompt_tokens: Some(1_000),+ cached_tokens: Some(900),+ ..Default::default()+ },+ });+ app.begin_turn();+ assert_eq!(app.turn_cached_tokens, 0, "the per-turn sample resets");+ assert!(!app.turn_cache_reported, "so does its reported flag");+ assert_eq!(app.session_cached_tokens, 900, "the session total does not");++ app.apply(StreamEvent::TurnUsage {+ usage: Usage {+ prompt_tokens: Some(4_000),+ cached_tokens: Some(0),+ ..Default::default()+ },+ });+ assert_eq!(app.session_prompt_tokens, 5_000, "both requests billed");+ assert_eq!(app.session_cached_tokens, 900);+ assert_eq!(app.turn_cached_tokens, 0, "the latest request served none");+ assert!(app.session_cache_reported);+ assert!(+ app.turn_cache_reported,+ "a reported zero is still a report, and the last-request line must \+ show it rather than vanish"+ );+ }+#[test]fn context_view_uses_consistent_language_and_aligned_bars() {let mut report = context_report(234_000, 35_000, [6_049, 9_000, 2_149, 8_049, 95_253]);@@ -31696,6 +32043,21 @@ mod tests {assert_eq!(format_tokens(9_950), "10K");assert_eq!(format_tokens(999), "999");assert_eq!(format_tokens(2_100), "2.1K");+ // Session-cumulative cache totals reach the millions, where `1200K`+ // reads as noise. Same rule up there: no zero decimal either.+ for tokens in (1_000_000..=3_000_000u64).step_by(997) {+ let text = format_tokens(tokens);+ assert!(+ !text.contains(".0M"),+ "{tokens} rendered as {text}: a zero decimal at the M boundary too"+ );+ }+ assert_eq!(format_tokens(1_000_000), "1M");+ assert_eq!(format_tokens(1_050_000), "1.1M");+ assert_eq!(format_tokens(1_183_000), "1.2M");+ assert_eq!(format_tokens(1_949_999), "1.9M");+ assert_eq!(format_tokens(1_950_000), "2M");+ assert_eq!(format_tokens(12_345_678), "12.3M");}/// An over-full estimate clamps free space to zero; the category banks@@ -35508,6 +35870,70 @@ mod tests {);}+ fn header_text(app: &App) -> String {+ header_spans(app)+ .iter()+ .map(|s| s.content.as_ref())+ .collect::<String>()+ }++ /// The session hit rate is on the status line, so a session that starts+ /// thrashing is visible while it runs instead of only behind `/context`.+ #[test]+ fn header_shows_the_live_cache_hit_rate() {+ let mut app = test_app();+ app.session_cache_reported = true;+ app.session_prompt_tokens = 200_000;+ app.session_cached_tokens = 150_000;++ assert!(header_text(&app).contains("cache 75%"), "{}", header_text(&app));+ }++ /// No badge means the route reports no cache usage at all -- never a `0%`+ /// the provider did not claim.+ #[test]+ fn header_shows_no_cache_rate_when_the_route_reports_none() {+ let app = test_app();+ assert!(+ !header_text(&app).contains("cache"),+ "{}",+ header_text(&app)+ );+ }++ /// A zero hit rate is in the alarm colour: the prefix is being written every+ /// turn and never read, which is the state worth interrupting for.+ #[test]+ fn header_marks_a_zero_hit_rate_in_red() {+ let mut app = test_app();+ app.session_cache_reported = true;+ app.session_prompt_tokens = 100_000;++ let span = header_spans(&app)+ .into_iter()+ .find(|s| s.content.contains("cache"))+ .expect("the badge must render once the route reports");+ assert_eq!(span.content, "cache 0%");+ assert_eq!(span.style, Style::new().red().bold());+ }++ /// The counters begin where the process did, so a resumed history's badge+ /// says which turns it covers.+ #[test]+ fn header_scopes_the_cache_rate_on_a_resumed_history() {+ let mut app = test_app();+ app.session_cache_reported = true;+ app.session_cache_partial = true;+ app.session_prompt_tokens = 100_000;+ app.session_cached_tokens = 90_000;++ assert!(+ header_text(&app).contains("cache 90% (this process)"),+ "{}",+ header_text(&app)+ );+ }+#[test]fn body_omits_max_tokens_when_unset() {let app = test_app();