tokensave 7.9.0

Code intelligence tool that builds a semantic knowledge graph from Rust, Go, Java, Scala, TypeScript, Python, C, C++, Kotlin, C#, Swift, and many more codebases
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
//! Aggregation and summary queries for token accounting.

use crate::accounting::parser::{CoverageState, SourceCoverage};
use crate::display::{format_token_count, CostRow};
use crate::global_db::{AgentCostSummary, GlobalDb};

/// Full cost summary with breakdowns.
pub struct CostSummary {
    pub total_cost: f64,
    pub total_input_tokens: u64,
    pub total_output_tokens: u64,
    pub total_cache_read_tokens: u64,
    pub by_model: Vec<(String, f64, u64)>, // (model, cost, total_tokens)
    pub by_category: Vec<(String, f64, u64)>, // (category, cost, turn_count)
    pub tokens_saved: u64,
    pub efficiency_ratio: f64,
    pub by_agent: Vec<AgentCostSummary>,
}

/// Quick cost summary for the `tokensave status` header row.
/// Returns `None` if no accounting data exists.
pub async fn quick_cost_summary(
    gdb: &GlobalDb,
    tokens_saved: u64,
    global_tokens_saved: u64,
) -> Option<CostRow> {
    quick_cost_summary_with_droid_presence(gdb, tokens_saved, global_tokens_saved, true).await
}

/// Build a quick summary while excluding stale Droid rows when no Droid source exists.
pub async fn quick_cost_summary_with_droid_presence(
    gdb: &GlobalDb,
    tokens_saved: u64,
    global_tokens_saved: u64,
    droid_present: bool,
) -> Option<CostRow> {
    let now = now_epoch();
    let today_start = today_start_epoch(now);
    let week_start = now.saturating_sub(7 * 86400);

    let today_cost = gdb.total_cost_since(today_start).await?;
    let week_cost = gdb.total_cost_since(week_start).await?;
    let by_agent = gdb.cost_by_agent_since(week_start).await;
    let week_consumed = consumed_tokens(&by_agent, droid_present);

    // Don't show the row if there's no meaningful data
    if today_cost < 0.001 && week_cost < 0.001 {
        return None;
    }

    let total_saved = tokens_saved + global_tokens_saved;
    let efficiency_pct = if total_saved + week_consumed > 0 {
        (total_saved as f64 / (total_saved + week_consumed) as f64) * 100.0
    } else {
        0.0
    };

    Some(CostRow {
        today_cost,
        week_cost,
        efficiency_pct,
    })
}

/// Build a full cost summary for a given time range.
pub async fn cost_summary(gdb: &GlobalDb, since: u64, tokens_saved: u64) -> Option<CostSummary> {
    cost_summary_with_droid_presence(gdb, since, tokens_saved, true).await
}

/// Build a full summary while excluding stale Droid rows when no Droid source exists.
pub async fn cost_summary_with_droid_presence(
    gdb: &GlobalDb,
    since: u64,
    tokens_saved: u64,
    droid_present: bool,
) -> Option<CostSummary> {
    let total_cost = gdb.total_cost_since(since).await?;
    let (total_input, total_output, total_cache_read) =
        gdb.token_breakdown_since(since).await.unwrap_or((0, 0, 0));
    let by_model = gdb.cost_by_model_since(since).await;
    let by_category = gdb.cost_by_category_since(since).await;
    let mut by_agent = gdb.cost_by_agent_since(since).await;
    if !droid_present {
        by_agent.retain(|summary| summary.agent != "droid");
    }

    let all_consumed = consumed_tokens(&by_agent, droid_present);
    let efficiency_ratio = if tokens_saved + all_consumed > 0 {
        tokens_saved as f64 / (tokens_saved + all_consumed) as f64
    } else {
        0.0
    };

    Some(CostSummary {
        total_cost,
        total_input_tokens: total_input,
        total_output_tokens: total_output,
        total_cache_read_tokens: total_cache_read,
        by_model,
        by_category,
        tokens_saved,
        efficiency_ratio,
        by_agent,
    })
}

pub(crate) fn consumed_tokens(by_agent: &[AgentCostSummary], droid_present: bool) -> u64 {
    by_agent
        .iter()
        .filter(|summary| droid_present || summary.agent != "droid")
        .fold(0, |total, summary| {
            total
                .saturating_add(summary.input_tokens)
                .saturating_add(summary.output_tokens)
        })
}

/// Format a human-readable coverage string when a Droid source exists.
///
/// Returns an empty string when Droid is absent so existing Claude-only output
/// remains unchanged.
pub fn format_coverage(coverage: &[SourceCoverage], by_agent: &[AgentCostSummary]) -> String {
    let claude_state = coverage
        .iter()
        .find(|c| c.agent == "claude")
        .map_or(CoverageState::Absent, |c| c.state);

    let droid_state = coverage
        .iter()
        .find(|c| c.agent == "droid")
        .map_or(CoverageState::Absent, |c| c.state);
    if droid_state == CoverageState::Absent {
        return String::new();
    }

    let claude_text = format!(
        "Claude {}",
        match claude_state {
            CoverageState::Complete => "complete",
            CoverageState::Partial => "partial",
            CoverageState::Absent => "absent",
        }
    );
    let usage = by_agent.iter().find(|a| a.agent == "droid");
    let raw = usage.map(|u| {
        u.input_tokens
            .saturating_add(u.output_tokens)
            .saturating_add(u.cache_write_tokens)
            .saturating_add(u.cache_read_tokens)
    });
    let droid_text = match droid_state {
        CoverageState::Absent => unreachable!("handled above"),
        CoverageState::Partial => match (usage, raw) {
            (Some(u), Some(r)) if r > 0 => match u.credits {
                Some(credits) => format!(
                    "Droid {} credits, {} raw tokens (partial, session-start buckets)",
                    format_token_count(credits),
                    format_token_count(r)
                ),
                None => format!(
                    "Droid credits n/a, {} raw tokens (partial, session-start buckets)",
                    format_token_count(r)
                ),
            },
            _ => "Droid partial; no usage in range".to_string(),
        },
        CoverageState::Complete => match (usage, raw) {
            (Some(u), Some(r)) if r > 0 => match u.credits {
                Some(credits) => format!(
                    "Droid {} credits, {} raw tokens (observed locally, session-start buckets)",
                    format_token_count(credits),
                    format_token_count(r)
                ),
                None => format!(
                    "Droid credits n/a, {} raw tokens (observed locally, session-start buckets)",
                    format_token_count(r)
                ),
            },
            _ => "Droid complete; no usage in range".to_string(),
        },
    };
    format!("Coverage: {claude_text}; {droid_text}")
}

/// Parse a range string into a unix timestamp for "since".
pub fn parse_range(range: &str) -> u64 {
    let now = now_epoch();
    match range {
        "today" => today_start_epoch(now),
        "30d" => now.saturating_sub(30 * 86400),
        "month" => month_start_epoch(now),
        "all" => 0,
        _ => now.saturating_sub(7 * 86400),
    }
}

fn now_epoch() -> u64 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs()
}

/// Start of today (midnight UTC).
fn today_start_epoch(now: u64) -> u64 {
    now - (now % 86400)
}

/// Start of the current calendar month (UTC).
/// Uses 30 days as an approximation to avoid pulling in chrono.
fn month_start_epoch(now: u64) -> u64 {
    now.saturating_sub(30 * 86400)
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use super::*;
    use crate::global_db::AgentCostSummary;

    fn make_agent(
        agent: &str,
        input: u64,
        output: u64,
        cw: u64,
        cr: u64,
        credits: Option<u64>,
    ) -> AgentCostSummary {
        AgentCostSummary {
            agent: agent.to_string(),
            cost_usd: 0.0,
            input_tokens: input,
            output_tokens: output,
            cache_write_tokens: cw,
            cache_read_tokens: cr,
            credits,
            turns: 1,
        }
    }

    #[test]
    fn format_coverage_complete_with_credits_and_usage() {
        let coverage = vec![
            SourceCoverage {
                agent: "claude",
                state: CoverageState::Complete,
                sessions: 5,
            },
            SourceCoverage {
                agent: "droid",
                state: CoverageState::Complete,
                sessions: 3,
            },
        ];
        // raw = 20000 + 3000 + 1000 + 600 = 24600 → "24.6k"; credits = 2900 → "2.9k"
        let by_agent = vec![
            make_agent("claude", 100, 20, 5, 7, None),
            make_agent("droid", 20000, 3000, 1000, 600, Some(2900)),
        ];
        let got = format_coverage(&coverage, &by_agent);
        let expected = "Coverage: Claude complete; Droid 2.9k credits, 24.6k raw tokens (observed locally, session-start buckets)";
        assert_eq!(got, expected);
    }

    #[test]
    fn format_coverage_complete_credits_none() {
        // Complete + usage but credits field is None → "Droid credits n/a, ..."
        let coverage = vec![
            SourceCoverage {
                agent: "claude",
                state: CoverageState::Complete,
                sessions: 5,
            },
            SourceCoverage {
                agent: "droid",
                state: CoverageState::Complete,
                sessions: 3,
            },
        ];
        // raw = 20000 + 3000 + 1000 + 600 = 24600 → "24.6k"; credits = None
        let by_agent = vec![make_agent("droid", 20000, 3000, 1000, 600, None)];
        let got = format_coverage(&coverage, &by_agent);
        let expected = "Coverage: Claude complete; Droid credits n/a, 24.6k raw tokens (observed locally, session-start buckets)";
        assert_eq!(got, expected);
    }

    #[test]
    fn format_coverage_partial_credits_na() {
        let coverage = vec![
            SourceCoverage {
                agent: "claude",
                state: CoverageState::Partial,
                sessions: 2,
            },
            SourceCoverage {
                agent: "droid",
                state: CoverageState::Partial,
                sessions: 1,
            },
        ];
        // raw = 400 + 50 + 10 + 20 = 480
        let by_agent = vec![make_agent("droid", 400, 50, 10, 20, None)];
        let got = format_coverage(&coverage, &by_agent);
        let expected = "Coverage: Claude partial; Droid credits n/a, 480 raw tokens (partial, session-start buckets)";
        assert_eq!(got, expected);
    }

    #[test]
    fn format_coverage_partial_preserves_known_range_credits() {
        let coverage = vec![
            SourceCoverage {
                agent: "claude",
                state: CoverageState::Complete,
                sessions: 1,
            },
            SourceCoverage {
                agent: "droid",
                state: CoverageState::Partial,
                sessions: 1,
            },
        ];
        let by_agent = vec![make_agent("droid", 24_383, 73, 0, 123, Some(2_945))];

        let got = format_coverage(&coverage, &by_agent);

        assert_eq!(
            got,
            "Coverage: Claude complete; Droid 2.9k credits, 24.6k raw tokens (partial, session-start buckets)"
        );
    }

    #[test]
    fn format_coverage_partial_no_usage() {
        // Partial coverage but no usage entries in range
        let coverage = vec![
            SourceCoverage {
                agent: "claude",
                state: CoverageState::Partial,
                sessions: 2,
            },
            SourceCoverage {
                agent: "droid",
                state: CoverageState::Partial,
                sessions: 1,
            },
        ];
        let got = format_coverage(&coverage, &[]);
        let expected = "Coverage: Claude partial; Droid partial; no usage in range";
        assert_eq!(got, expected);
    }

    #[test]
    fn format_coverage_droid_absent() {
        let coverage = vec![
            SourceCoverage {
                agent: "claude",
                state: CoverageState::Complete,
                sessions: 1,
            },
            SourceCoverage {
                agent: "droid",
                state: CoverageState::Absent,
                sessions: 0,
            },
        ];
        let got = format_coverage(&coverage, &[]);
        assert!(got.is_empty(), "{got}");
    }

    #[test]
    fn format_coverage_droid_no_usage_in_range() {
        let coverage = vec![
            SourceCoverage {
                agent: "claude",
                state: CoverageState::Complete,
                sessions: 1,
            },
            SourceCoverage {
                agent: "droid",
                state: CoverageState::Complete,
                sessions: 3,
            },
        ];
        let got = format_coverage(&coverage, &[]);
        assert_eq!(
            got,
            "Coverage: Claude complete; Droid complete; no usage in range"
        );
    }

    #[test]
    fn test_parse_range() {
        let now = now_epoch();
        let today = parse_range("today");
        assert!(today <= now);
        assert!(now - today < 86400);

        let week = parse_range("7d");
        assert!(now - week >= 7 * 86400 - 1);
        assert!(now - week <= 7 * 86400 + 1);

        assert_eq!(parse_range("all"), 0);
    }

    #[test]
    fn test_today_start() {
        // Use a value that's exactly at midnight UTC (divisible by 86400)
        let midnight = (1_713_100_800 / 86400) * 86400;
        assert_eq!(today_start_epoch(midnight), midnight);
        assert_eq!(today_start_epoch(midnight + 3600), midnight);
        assert_eq!(today_start_epoch(midnight + 86399), midnight);
    }
}