tokensave 7.12.1

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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
//! Missed-opportunity analyzer over ingested Claude Code transcript turns.
//!
//! Scans the `turns` table and flags turns that consisted *only* of
//! file-navigation tools (`Read`, `Grep`, `Glob`) — work a tokensave graph
//! query (`search`, `context`, `callers`, `callees`, `impact`, `outline`,
//! `read`, `node`) could have served far more cheaply. Results are bucketed by
//! the navigation tool that drove the turn, with a deliberately CONSERVATIVE
//! estimate of recoverable input tokens.
//!
//! # Estimation method and assumptions
//!
//! Per turn, the `turns` table records the comma-joined `tool_names` and — as
//! of #474 — `tool_result_tokens`, the measured size of the tool results that
//! turn's tools injected into the conversation. That is the quantity this
//! analyzer reports as **addressable**: the text a graph query could have
//! served instead.
//!
//! It is deliberately not `input_tokens`, which is what this read until #474.
//! Under prompt caching `input_tokens` is only the *uncached remainder* of the
//! prompt — a double-digit figure per turn, which is why the reported totals
//! were implausibly small and looked like a placeholder. Nor is it the whole
//! prompt with cache included: that is hundreds of thousands of tokens per
//! turn, most of them conversation the navigation did not cause, and a graph
//! query cannot recover any of it.
//!
//! Two limits remain, and we stay strictly within what the data supports:
//!
//! 1. Bash-based navigation (`grep`/`find`/`cat`/`rg`) cannot be detected here,
//!    because command text is not in the table. Those turns are simply not
//!    counted — this makes the analyzer a lower bound, never an over-claim.
//! 2. Turns parsed before #474 carry `tool_result_tokens = 0` and so contribute
//!    nothing to the addressable total. The figure is not recomputed for them:
//!    it comes from transcript lines the database does not keep, and re-reading
//!    every historical session would cost more than the metric is worth. A
//!    range extending back before the upgrade therefore under-reports, which
//!    keeps it a lower bound rather than a wrong number.
//!
//! A turn is "replaceable" only when *every* tool it used is a navigation tool;
//! a turn that also edits, runs Bash, delegates, etc. is left out entirely.
//! This keeps the count conservative and avoids attributing edit-turn cost to
//! navigation.

/// Conservative fraction of a navigation turn's injected tool results treated
/// as recoverable by a graph query.
///
/// The addressable figure is now the payload itself rather than a whole
/// prompt (#474), so the old rationale — that most of `input_tokens` was fixed
/// conversational overhead a graph query cannot shrink — no longer applies.
/// What remains true is that a graph query is not free: it returns a compact
/// slice where a `Read` returned a whole file, so it replaces most of the
/// payload rather than all of it. Half is claimed. This is still a stated
/// lower bound rather than a measured value; changing it only rescales the
/// recoverable column, and the addressable figure stands on its own.
pub const RECOVERABLE_FRACTION: f64 = 0.5;

/// Which tokensave graph query would have replaced a navigation tool.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum NavBucket {
    /// `Read` of a file → `outline` / `read` / `node`.
    Read,
    /// `Grep` across files → `search` / `callers` / `callees` / `impact`.
    Grep,
    /// `Glob` file discovery → `files` / `search`.
    Glob,
}

impl NavBucket {
    /// Short stable identifier (used for ordering and machine output).
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Read => "read",
            Self::Grep => "grep",
            Self::Glob => "glob",
        }
    }

    /// The navigation tool name this bucket corresponds to.
    pub fn tool_name(&self) -> &'static str {
        match self {
            Self::Read => "Read",
            Self::Grep => "Grep",
            Self::Glob => "Glob",
        }
    }

    /// The tokensave query/queries that would have served the same intent.
    pub fn suggestion(&self) -> &'static str {
        match self {
            Self::Read => "outline / read / node",
            Self::Grep => "search / callers / callees / impact",
            Self::Glob => "files / search",
        }
    }
}

/// The navigation tool names this analyzer recognizes from `tool_names`.
const NAV_TOOLS: [&str; 3] = ["Read", "Grep", "Glob"];

/// Per-bucket tally of replaceable navigation turns.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BucketStat {
    pub bucket: NavBucket,
    /// Number of replaceable navigation turns attributed to this bucket.
    pub turns: u64,
    /// Sum of `tool_result_tokens` across those turns — the text those tools
    /// injected, which is what a graph query could have served instead. Read
    /// from `input_tokens` until #474, where under prompt caching it measured
    /// only the uncached remainder of the prompt.
    pub addressable_input_tokens: u64,
    /// How many of `turns` carry a recorded tool-result size.
    ///
    /// Turns ingested before #474 have no size — the column defaults to 0 —
    /// so `addressable_input_tokens == 0` has two readings that are the same
    /// bytes: measured and genuinely zero, or never measured. This counts the
    /// turns that actually carry a figure, which is what separates them, and
    /// which is the only way to describe a range straddling the upgrade
    /// rather than rounding it off (#523). A turn whose tool results were
    /// truly empty counts as unmeasured; for a `Read`, `Grep` or `Glob` turn
    /// that is vanishingly rare, and erring that way keeps this a lower bound
    /// rather than an overclaim.
    pub turns_with_measured_sizes: u64,
}

impl BucketStat {
    /// Conservative lower-bound recoverable input tokens for this bucket.
    ///
    /// Defined as `addressable_input_tokens * RECOVERABLE_FRACTION`, rounded
    /// down. See the module-level docs for the assumption behind the fraction.
    pub fn recoverable_input_tokens(&self) -> u64 {
        ((self.addressable_input_tokens as f64) * RECOVERABLE_FRACTION) as u64
    }
}

/// Result of analyzing a set of turns for replaceable navigation.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DiscoverReport {
    /// Total turns examined (navigation and non-navigation alike).
    pub total_turns: u64,
    /// Per-bucket stats, ranked by addressable input tokens descending.
    pub buckets: Vec<BucketStat>,
}

impl DiscoverReport {
    /// Total replaceable navigation turns across all buckets.
    pub fn total_replaceable_turns(&self) -> u64 {
        self.buckets.iter().map(|b| b.turns).sum()
    }

    /// Total replaceable turns carrying a recorded tool-result size.
    ///
    /// Equal to [`Self::total_replaceable_turns`] when every turn in range was
    /// ingested after #474, 0 when none were, and something between for a
    /// range straddling the upgrade.
    pub fn total_turns_with_measured_sizes(&self) -> u64 {
        self.buckets
            .iter()
            .map(|b| b.turns_with_measured_sizes)
            .sum()
    }

    /// Total addressable input tokens across all buckets.
    pub fn total_addressable_input_tokens(&self) -> u64 {
        self.buckets
            .iter()
            .map(|b| b.addressable_input_tokens)
            .sum()
    }

    /// Total conservative recoverable input tokens across all buckets.
    pub fn total_recoverable_input_tokens(&self) -> u64 {
        self.buckets
            .iter()
            .map(BucketStat::recoverable_input_tokens)
            .sum()
    }
}

/// Split a stored `tool_names` value (comma-joined) into trimmed names.
fn split_tools(tool_names: &str) -> Vec<&str> {
    tool_names
        .split(',')
        .map(str::trim)
        .filter(|s| !s.is_empty())
        .collect()
}

/// Decide whether a turn is "replaceable navigation" and, if so, which bucket.
///
/// A turn qualifies only when it is non-empty and *every* tool it used is a
/// recognized navigation tool. Mixed turns (navigation + edit/Bash/etc.) return
/// `None` so their cost is never attributed to navigation. When several
/// navigation tools appear, the bucket is chosen by a fixed priority
/// (`Grep` > `Glob` > `Read`): a cross-file search is the strongest signal of
/// an opportunity a graph query would have answered, so it wins attribution.
fn classify_nav(tools: &[&str]) -> Option<NavBucket> {
    if tools.is_empty() {
        return None;
    }
    if !tools.iter().all(|t| NAV_TOOLS.contains(t)) {
        return None;
    }
    if tools.contains(&"Grep") {
        Some(NavBucket::Grep)
    } else if tools.contains(&"Glob") {
        Some(NavBucket::Glob)
    } else {
        Some(NavBucket::Read)
    }
}

/// Analyze `(tool_names, input_tokens)` rows into a ranked [`DiscoverReport`].
///
/// Pure function over already-fetched rows: deterministic, no I/O, no LLM. The
/// caller supplies rows via [`crate::global_db::GlobalDb::nav_turns_since`].
pub fn analyze(turns: &[(String, u64)]) -> DiscoverReport {
    let mut read = BucketStat {
        bucket: NavBucket::Read,
        turns: 0,
        addressable_input_tokens: 0,
        turns_with_measured_sizes: 0,
    };
    let mut grep = BucketStat {
        bucket: NavBucket::Grep,
        turns: 0,
        addressable_input_tokens: 0,
        turns_with_measured_sizes: 0,
    };
    let mut glob = BucketStat {
        bucket: NavBucket::Glob,
        turns: 0,
        addressable_input_tokens: 0,
        turns_with_measured_sizes: 0,
    };

    for (tool_names, input_tokens) in turns {
        let tools = split_tools(tool_names);
        if let Some(bucket) = classify_nav(&tools) {
            let stat = match bucket {
                NavBucket::Read => &mut read,
                NavBucket::Grep => &mut grep,
                NavBucket::Glob => &mut glob,
            };
            stat.turns += 1;
            stat.addressable_input_tokens =
                stat.addressable_input_tokens.saturating_add(*input_tokens);
            if *input_tokens > 0 {
                stat.turns_with_measured_sizes += 1;
            }
        }
    }

    // Keep only buckets that actually fired, ranked by addressable tokens
    // descending (ties broken by the stable bucket identifier).
    let mut buckets: Vec<BucketStat> = [read, grep, glob]
        .into_iter()
        .filter(|b| b.turns > 0)
        .collect();
    buckets.sort_by(|a, b| {
        b.addressable_input_tokens
            .cmp(&a.addressable_input_tokens)
            .then_with(|| a.bucket.as_str().cmp(b.bucket.as_str()))
    });

    DiscoverReport {
        total_turns: turns.len() as u64,
        buckets,
    }
}

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

    fn t(tools: &str, input: u64) -> (String, u64) {
        (tools.to_string(), input)
    }

    #[test]
    fn empty_input_is_empty_report() {
        let report = analyze(&[]);
        assert_eq!(report.total_turns, 0);
        assert!(report.buckets.is_empty());
        assert_eq!(report.total_recoverable_input_tokens(), 0);
    }

    #[test]
    fn pure_read_turn_is_read_bucket() {
        let report = analyze(&[t("Read", 1000)]);
        assert_eq!(report.buckets.len(), 1);
        let b = &report.buckets[0];
        assert_eq!(b.bucket, NavBucket::Read);
        assert_eq!(b.turns, 1);
        assert_eq!(b.addressable_input_tokens, 1000);
        assert_eq!(b.recoverable_input_tokens(), 500);
    }

    #[test]
    fn grep_wins_over_glob_and_read_when_mixed_navigation() {
        // All-navigation turn with several nav tools attributes to Grep.
        let report = analyze(&[t("Read,Grep,Glob", 2000)]);
        assert_eq!(report.buckets.len(), 1);
        assert_eq!(report.buckets[0].bucket, NavBucket::Grep);
        assert_eq!(report.buckets[0].turns, 1);
    }

    #[test]
    fn glob_wins_over_read() {
        let report = analyze(&[t("Read,Glob", 800)]);
        assert_eq!(report.buckets.len(), 1);
        assert_eq!(report.buckets[0].bucket, NavBucket::Glob);
    }

    #[test]
    fn turn_with_edit_is_not_replaceable() {
        // Navigation mixed with a mutating tool is excluded entirely.
        let report = analyze(&[t("Read,Edit", 5000), t("Grep,Write", 5000)]);
        assert!(report.buckets.is_empty());
        assert_eq!(report.total_replaceable_turns(), 0);
        assert_eq!(report.total_addressable_input_tokens(), 0);
    }

    #[test]
    fn bash_only_turn_is_not_counted() {
        // Bash command content is not stored, so Bash turns are never nav.
        let report = analyze(&[t("Bash", 3000)]);
        assert!(report.buckets.is_empty());
    }

    #[test]
    fn empty_tool_names_conversation_turn_excluded() {
        let report = analyze(&[t("", 1234)]);
        assert_eq!(report.total_turns, 1);
        assert!(report.buckets.is_empty());
    }

    #[test]
    fn buckets_ranked_by_addressable_tokens_descending() {
        let report = analyze(&[
            t("Read", 100),
            t("Read", 100),
            t("Grep", 5000),
            t("Glob", 900),
        ]);
        assert_eq!(report.buckets.len(), 3);
        assert_eq!(report.buckets[0].bucket, NavBucket::Grep);
        assert_eq!(report.buckets[1].bucket, NavBucket::Glob);
        assert_eq!(report.buckets[2].bucket, NavBucket::Read);
        // Read bucket aggregates both read turns.
        assert_eq!(report.buckets[2].turns, 2);
        assert_eq!(report.buckets[2].addressable_input_tokens, 200);
    }

    #[test]
    fn estimate_is_non_negative_and_monotonic() {
        let small = analyze(&[t("Read", 1000)]);
        let large = analyze(&[t("Read", 1000), t("Grep", 4000)]);
        assert!(large.total_recoverable_input_tokens() >= small.total_recoverable_input_tokens());
        // Recoverable never exceeds addressable (fraction <= 1).
        assert!(large.total_recoverable_input_tokens() <= large.total_addressable_input_tokens());
        // And is never negative (u64) — trivially true, but assert the bound.
        assert!(small.total_recoverable_input_tokens() <= small.total_addressable_input_tokens());
    }

    #[test]
    fn whitespace_in_tool_names_is_tolerated() {
        let report = analyze(&[t(" Read , Grep ", 1200)]);
        assert_eq!(report.buckets.len(), 1);
        assert_eq!(report.buckets[0].bucket, NavBucket::Grep);
        assert_eq!(report.buckets[0].turns, 1);
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod measured_size_tests {
    use super::*;

    fn rows(pairs: &[(&str, u64)]) -> Vec<(String, u64)> {
        pairs.iter().map(|(t, n)| ((*t).to_string(), *n)).collect()
    }

    /// The case #523 reported: every turn predates #474, so nothing carries a
    /// size. The total is 0, and the count says why — a consumer can tell this
    /// apart from a measured zero without reading English.
    #[test]
    fn a_wholly_unmeasured_range_reports_no_measured_turns() {
        let report = analyze(&rows(&[("Read", 0), ("Grep", 0), ("Read", 0)]));
        assert_eq!(report.total_replaceable_turns(), 3);
        assert_eq!(report.total_addressable_input_tokens(), 0);
        assert_eq!(report.total_turns_with_measured_sizes(), 0);
    }

    /// The case that reads identically in the old payload and must not: the
    /// tokens really were measured. Same `0` total, different count.
    #[test]
    fn a_measured_range_is_distinguishable_from_an_unmeasured_one() {
        let measured = analyze(&rows(&[("Read", 800), ("Grep", 200)]));
        let unmeasured = analyze(&rows(&[("Read", 0), ("Grep", 0)]));

        assert_eq!(measured.total_turns_with_measured_sizes(), 2);
        assert_eq!(unmeasured.total_turns_with_measured_sizes(), 0);
        assert_eq!(
            measured.total_replaceable_turns(),
            unmeasured.total_replaceable_turns(),
            "the two differ only in whether the sizes were recorded"
        );
    }

    /// The range-spanning case the issue's closing note describes, which a
    /// single boolean would have to round off: the total is real but partial.
    #[test]
    fn a_straddling_range_counts_only_the_measured_turns() {
        let report = analyze(&rows(&[
            ("Read", 0),
            ("Read", 500),
            ("Grep", 0),
            ("Grep", 300),
            ("Glob", 100),
        ]));

        assert_eq!(report.total_replaceable_turns(), 5);
        assert_eq!(report.total_turns_with_measured_sizes(), 3);
        assert_eq!(report.total_addressable_input_tokens(), 900);
    }

    /// Per-bucket, because a range can straddle the upgrade unevenly and a
    /// single top-level figure would hide which bucket is under-reported.
    #[test]
    fn the_measured_count_is_tracked_per_bucket() {
        let report = analyze(&rows(&[("Read", 0), ("Read", 0), ("Grep", 700)]));

        let read = report
            .buckets
            .iter()
            .find(|b| b.bucket == NavBucket::Read)
            .expect("read bucket");
        let grep = report
            .buckets
            .iter()
            .find(|b| b.bucket == NavBucket::Grep)
            .expect("grep bucket");

        assert_eq!((read.turns, read.turns_with_measured_sizes), (2, 0));
        assert_eq!((grep.turns, grep.turns_with_measured_sizes), (1, 1));
    }

    /// A non-navigation turn is not replaceable, so it contributes to neither
    /// count — the measured count never exceeds the replaceable total.
    #[test]
    fn the_measured_count_never_exceeds_the_replaceable_total() {
        let report = analyze(&rows(&[("Read,Bash", 900), ("Read", 400), ("Edit", 800)]));
        assert!(
            report.total_turns_with_measured_sizes() <= report.total_replaceable_turns(),
            "measured {} exceeded replaceable {}",
            report.total_turns_with_measured_sizes(),
            report.total_replaceable_turns()
        );
        assert_eq!(report.total_turns_with_measured_sizes(), 1);
    }
}