Skip to content
This repository was archived by the owner on Jul 30, 2026. It is now read-only.
Merged
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
584 changes: 584 additions & 0 deletions crates/tokscale-cli/src/tui/actions.rs

Large diffs are not rendered by default.

16 changes: 9 additions & 7 deletions crates/tokscale-cli/src/tui/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,6 @@ impl App {
fn graph_cell_for_date(&self, date: NaiveDate) -> Option<(usize, usize)> {
self.data
.graph
.as_ref()?
.weeks
.iter()
.enumerate()
Expand All @@ -844,7 +843,6 @@ impl App {
fn graph_date_for_cell(&self, (week_idx, day_idx): (usize, usize)) -> Option<NaiveDate> {
self.data
.graph
.as_ref()?
.weeks
.get(week_idx)?
.get(day_idx)?
Expand Down Expand Up @@ -1237,7 +1235,7 @@ impl App {
{
self.close_period_detail();
}
KeyCode::Esc if self.selected_graph_cell.is_some() => {
KeyCode::Esc | KeyCode::Backspace if self.selected_graph_cell.is_some() => {
self.selected_graph_cell = None;
self.stats_auto_select_today_pending = false;
self.reset_current_list_interaction();
Expand Down Expand Up @@ -2270,8 +2268,8 @@ impl App {
}
}

fn copy_selected_to_clipboard(&mut self) {
let text = match self.current_tab {
fn selected_copy_text(&self) -> Option<String> {
match self.current_tab {
Tab::Overview | Tab::Models => self
.get_sorted_models()
.get(self.selected_index)
Expand Down Expand Up @@ -2346,7 +2344,11 @@ impl App {
)
}),
Tab::Stats | Tab::Usage | Tab::Sessions => None,
};
}
}

fn copy_selected_to_clipboard(&mut self) {
let text = self.selected_copy_text();

if let Some(text) = text {
match arboard::Clipboard::new().and_then(|mut cb| cb.set_text(&text)) {
Expand Down Expand Up @@ -3319,7 +3321,7 @@ mod tests {
let graph = tokscale_core::build_contribution_graph_for_today(&daily, graph_today);
UsageData {
daily,
graph: Some(graph),
graph,
..Default::default()
}
}
Expand Down
57 changes: 47 additions & 10 deletions crates/tokscale-cli/src/tui/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,10 @@ mod bundle_tests {
assert_eq!(raw["health"]["inputDataBytes"], 4096);
assert_eq!(raw["canonicalDigest"].as_str().unwrap().len(), 64);
assert!(raw["projections"]["model"].get("health").is_none());
assert_eq!(
raw["projections"]["model"]["graph"],
serde_json::json!({ "weeks": [] })
);

let CacheResult::Fresh(loaded) = load_cache(&clients, &GroupBy::Model, &scope) else {
panic!("expected a fresh schema-44 bundle");
Expand All @@ -244,6 +248,44 @@ mod bundle_tests {
assert_eq!(loaded.data.health, health);
}

#[test]
#[serial]
fn missing_or_null_projection_graph_is_an_explicit_miss() {
let (_temp, _guard, clients, scope, sessions, client_space) = fixture();
let path = cache_file().unwrap();

for graph in [None, Some(serde_json::Value::Null)] {
save_tui_bundle_cache(
&TuiAcc::new(),
&sessions,
&client_space,
&Default::default(),
&clients,
&scope,
signature(),
)
.unwrap();
let mut value: serde_json::Value =
serde_json::from_reader(File::open(&path).unwrap()).unwrap();
match graph.clone() {
Some(graph) => value["projections"]["model"]["graph"] = graph,
None => {
value["projections"]["model"]
.as_object_mut()
.unwrap()
.remove("graph");
}
}
tokscale_core::fs_atomic::write_atomic(&path, &serde_json::to_vec(&value).unwrap())
.unwrap();

assert!(matches!(
load_cache(&clients, &GroupBy::Model, &scope),
CacheResult::Miss
));
}
}

#[test]
#[serial]
fn schema_44_nonempty_bundle_round_trips_all_four_public_groupings() {
Expand Down Expand Up @@ -284,10 +326,7 @@ mod bundle_tests {
assert!(model_projection.models.len() >= 3);
assert!(!model_projection.daily.is_empty());
assert!(model_projection.hourly.len() >= 3);
assert!(model_projection
.graph
.as_ref()
.is_some_and(|graph| !graph.weeks.is_empty()));
assert!(!model_projection.graph.weeks.is_empty());
let workspace_projection = accumulator.project(&GroupBy::WorkspaceModel);
let workspace_keys = workspace_projection
.models
Expand Down Expand Up @@ -650,7 +689,7 @@ struct CachedUsageData {
agents: Vec<CachedAgentUsage>,
daily: Vec<CachedDailyUsage>,
hourly: Vec<CachedHourlyUsage>,
graph: Option<CachedGraphData>,
graph: CachedGraphData,
total_tokens: u64,
total_cost: f64,
current_streak: u32,
Expand Down Expand Up @@ -1328,15 +1367,13 @@ impl TryFrom<CachedUsageData> for UsageData {
let daily: Result<Vec<DailyUsage>, _> = u.daily.into_iter().map(|d| d.try_into()).collect();
let hourly: Result<Vec<HourlyUsage>, _> =
u.hourly.into_iter().map(|h| h.try_into()).collect();
let graph: Option<Result<GraphData, _>> = u.graph.map(|g| g.try_into());

Ok(Self {
health: Default::default(),
models: u.models.into_iter().map(|m| m.into()).collect(),
agents: normalize_cached_agents(u.agents)?,
daily: daily?,
hourly: hourly?,
graph: graph.transpose()?,
graph: u.graph.try_into()?,
total_tokens: u.total_tokens,
total_cost: u.total_cost,
error: None,
Expand Down Expand Up @@ -1589,7 +1626,7 @@ struct CachedProjectionUsageDataRef<'a> {
agents: CachedAgentsRef<'a>,
daily: CachedDailyEntriesRef<'a>,
hourly: CachedHourlyEntriesRef<'a>,
graph: Option<CachedGraphDataRef<'a>>,
graph: CachedGraphDataRef<'a>,
total_tokens: u64,
total_cost: f64,
current_streak: u32,
Expand All @@ -1603,7 +1640,7 @@ impl<'a> From<&'a UsageData> for CachedProjectionUsageDataRef<'a> {
agents: CachedAgentsRef(&data.agents),
daily: CachedDailyEntriesRef(&data.daily),
hourly: CachedHourlyEntriesRef(&data.hourly),
graph: data.graph.as_ref().map(CachedGraphDataRef::from),
graph: CachedGraphDataRef::from(&data.graph),
total_tokens: data.total_tokens,
total_cost: data.total_cost,
current_streak: data.current_streak,
Expand Down
Loading
Loading