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
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
// Rust guideline compliant 2025-10-17
//! Agent integration layer for CLI tools (Claude Code, `OpenCode`, Codex, etc.).
//!
//! Each supported agent implements the [`AgentIntegration`] trait which provides
//! `install`, `uninstall`, and `healthcheck` operations. The MCP server
//! itself is agent-agnostic; this module handles the per-agent config
//! plumbing (registering the MCP server, permissions, hooks, prompt rules).

/// Set while a non-interactive caller (the silent reinstall-on-upgrade in
/// `main`) drives `install`, so the per-agent integrations stay quiet instead
/// of printing their full setup banner on every `init`/`sync` (#255).
static QUIET_INSTALL: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);

/// Suppress (or re-enable) agent install progress output.
pub fn set_quiet_install(quiet: bool) {
    QUIET_INSTALL.store(quiet, std::sync::atomic::Ordering::Relaxed);
}

/// Whether agent install progress output is currently suppressed.
pub fn quiet_install() -> bool {
    QUIET_INSTALL.load(std::sync::atomic::Ordering::Relaxed)
}

/// Re-run install for every tracked agent so permissions, hooks, and MCP
/// config stay in sync after the binary changes version.
///
/// Two signals trigger a resync:
///   (a) `previous_version` (set by `tokensave upgrade` / `channel switch`
///       just before replacing the binary) differs from the running version
///       AND the transition is a minor/major bump. Patch bumps are no-ops:
///       we just advance `previous_version` and skip reinstall.
///   (b) Fallback for external upgrades (`brew upgrade`, `cargo install`):
///       the running version is newer than `last_installed_version`.
///
/// `install` is called once per tracked agent id and returns `false` when that
/// agent could not be updated. Version markers advance regardless of those
/// failures — see [`ResyncOutcome::failed`]. Returns the outcome; the caller is
/// responsible for persisting `config` and reporting failures.
pub fn resync_installed_agents<F>(
    config: &mut crate::user_config::UserConfig,
    running: &str,
    mut install: F,
) -> ResyncOutcome
where
    F: FnMut(&str) -> bool,
{
    let previous_version = if config.previous_version.is_empty() {
        "6.0.0".to_string()
    } else {
        config.previous_version.clone()
    };
    let upgrade_detected = previous_version != running;
    let transition_needs_reinstall = upgrade_detected
        && (crate::cloud::is_newer_minor_version(&previous_version, running)
            || crate::cloud::is_newer_minor_version(running, &previous_version));
    let external_upgrade_needs_reinstall = !upgrade_detected
        && (config.last_installed_version.is_empty()
            || crate::cloud::is_newer_version(&config.last_installed_version, running));
    let needs_reinstall = transition_needs_reinstall || external_upgrade_needs_reinstall;

    if config.installed_agents.is_empty() || running.is_empty() || !needs_reinstall {
        if upgrade_detected {
            // Patch-only bump (or nothing to reinstall) — advance the marker
            // so we don't keep re-checking on every subsequent startup.
            config.previous_version = running.to_string();
            return ResyncOutcome {
                changed: true,
                ran: false,
                failed: Vec::new(),
            };
        }
        return ResyncOutcome {
            changed: false,
            ran: false,
            failed: Vec::new(),
        };
    }

    let agents = config.installed_agents.clone();
    let failed: Vec<String> = agents.into_iter().filter(|id| !install(id)).collect();

    // Advance the markers even when some agents failed. A config path we can't
    // write (missing app, read-only location) fails identically on every run,
    // and gating the markers on full success re-ran this resync — banner output
    // included — on every single command (#255). Report the failures once
    // instead of retrying forever.
    config.last_installed_version = running.to_string();
    config.previous_version = running.to_string();
    ResyncOutcome {
        changed: true,
        ran: true,
        failed,
    }
}

/// Result of [`resync_installed_agents`].
#[derive(Debug, Default, PartialEq, Eq)]
pub struct ResyncOutcome {
    /// Whether `config` was mutated and needs saving.
    pub changed: bool,
    /// Whether the per-agent install loop actually ran.
    pub ran: bool,
    /// Ids of agents whose install failed. Non-fatal.
    pub failed: Vec<String>,
}

/// `eprintln!` for agent install progress: silent under [`set_quiet_install`].
#[macro_export]
macro_rules! agent_note {
    ($($arg:tt)*) => {
        if !$crate::agents::quiet_install() {
            eprintln!($($arg)*);
        }
    };
}

/// `eprint!` for agent install progress: silent under [`set_quiet_install`].
#[macro_export]
macro_rules! agent_note_inline {
    ($($arg:tt)*) => {
        if !$crate::agents::quiet_install() {
            eprint!($($arg)*);
        }
    };
}

pub mod fs;
pub mod hooks;
pub mod integrations;
pub mod registry;
pub mod rules;
pub mod traits;

use std::path::{Path, PathBuf};

use crate::mcp::tools::get_tool_definitions;

pub use fs::*;
pub use hooks::*;
pub use integrations::*;
pub use registry::*;
pub use rules::*;
pub use traits::*;

/// Finds the tokensave binary path.
///
/// On Windows the returned path uses forward slashes so it can be safely
/// embedded in JSON hook commands without backslash-escaping issues.
pub fn which_tokensave() -> Option<String> {
    // Check the current executable first
    if let Ok(exe) = std::env::current_exe() {
        if exe
            .file_name()
            .and_then(|n| n.to_str())
            .is_some_and(|n| n.starts_with("tokensave"))
        {
            let normalized = normalize_path_separators(&exe.to_string_lossy());
            // `current_exe()` resolves symlinks, so under Homebrew it points at the
            // version-pinned Cellar path (e.g. `.../Cellar/tokensave/6.4.2/bin/...`).
            // `brew upgrade`/`brew cleanup` later remove that path, breaking any hook
            // config that embedded it (#146). Prefer the version-stable `bin` symlink
            // when it exists.
            if let Some(stable) = homebrew_stable_path(&normalized) {
                if Path::new(&stable).exists() {
                    return Some(stable);
                }
            }
            return Some(normalized);
        }
    }
    // Fall back to PATH lookup
    let path_var = std::env::var("PATH").ok()?;
    let separator = if cfg!(windows) { ';' } else { ':' };
    let bin_name = if cfg!(windows) {
        "tokensave.exe"
    } else {
        "tokensave"
    };
    path_var.split(separator).find_map(|dir| {
        let candidate = PathBuf::from(dir).join(bin_name);
        candidate
            .exists()
            .then(|| normalize_path_separators(&candidate.to_string_lossy()))
    })
}

/// Warns when agent config will reference a disposable Cargo build artifact.
pub fn cargo_build_binary_warning(path: &str) -> Option<String> {
    let components: Vec<_> = Path::new(path)
        .components()
        .filter_map(|component| match component {
            std::path::Component::Normal(value) => value.to_str(),
            _ => None,
        })
        .collect();

    let is_profile = |value: &str| matches!(value, "debug" | "release");
    let cargo_build = components
        .windows(2)
        .any(|parts| parts[0] == "target" && is_profile(parts[1]))
        || components
            .windows(3)
            .any(|parts| parts[0] == "target" && is_profile(parts[2]));

    cargo_build.then(|| {
        format!(
            "\x1b[33mwarning:\x1b[0m agent config references Cargo build output:\n  \
             {path}\n  `cargo clean` or removing its worktree will break tokensave hooks and MCP \
             servers.\n  Re-run `tokensave install` from a stable `cargo install`, Homebrew, or \
             release binary."
        )
    })
}

/// Keeps the user's existing MCP command when it still resolves (issue #161).
///
/// Reinstalls used to overwrite whatever command the config held with this
/// install's absolute binary path, clobbering deliberate choices like a bare
/// `tokensave` resolved via `PATH` (portable across machines with different
/// install locations). If the previous command still resolves to a tokensave
/// binary, keep it verbatim; otherwise use `new_bin`.
pub fn preserve_mcp_command_str(previous: Option<&str>, new_bin: &str) -> String {
    match previous {
        Some(prev) if command_resolves_to_tokensave(prev) => prev.to_string(),
        _ => new_bin.to_string(),
    }
}

/// JSON variant of [`preserve_mcp_command_str`]: accepts the previous
/// command as either a string (`"command": "tokensave"`) or an array whose
/// first element is the binary (`"command": ["tokensave", "serve"]`).
pub fn preserve_mcp_command(previous: Option<&serde_json::Value>, new_bin: &str) -> String {
    let prev_str = previous.and_then(|v| match v {
        serde_json::Value::String(s) => Some(s.as_str()),
        serde_json::Value::Array(a) => a.first().and_then(serde_json::Value::as_str),
        _ => None,
    });
    preserve_mcp_command_str(prev_str, new_bin)
}

/// True when `cmd` names a tokensave binary that exists: an absolute or
/// relative path that is on disk, or a bare name found on `PATH`.
fn command_resolves_to_tokensave(cmd: &str) -> bool {
    command_resolves_to_tokensave_in(cmd, std::env::var("PATH").ok().as_deref())
}

/// [`command_resolves_to_tokensave`] with the `PATH` value injected so tests
/// don't have to mutate process-global environment.
fn command_resolves_to_tokensave_in(cmd: &str, path_var: Option<&str>) -> bool {
    let name_ok = Path::new(cmd)
        .file_stem()
        .and_then(|s| s.to_str())
        .is_some_and(|n| n.starts_with("tokensave"));
    if !name_ok {
        return false;
    }
    if cmd.contains('/') || cmd.contains('\\') {
        return Path::new(cmd).exists();
    }
    let Some(path_var) = path_var else {
        return false;
    };
    let separator = if cfg!(windows) { ';' } else { ':' };
    path_var.split(separator).any(|dir| {
        let base = PathBuf::from(dir).join(cmd);
        base.exists() || (cfg!(windows) && base.with_extension("exe").exists())
    })
}

/// Maps a Homebrew Cellar executable path to its version-stable `bin` symlink.
///
/// Homebrew installs the real binary under `<prefix>/Cellar/tokensave/<version>/bin/`
/// and exposes it on `PATH` via a stable `<prefix>/bin/tokensave` symlink. Embedding
/// the Cellar path in hook configs breaks on `brew upgrade`/`brew cleanup`; the `bin`
/// symlink always tracks the current version. Expects a forward-slash path. Returns
/// `None` for non-Cellar paths, leaving the caller to use the path as-is.
fn homebrew_stable_path(exe: &str) -> Option<String> {
    let (prefix, rest) = exe.split_once("/Cellar/tokensave/")?;
    let file = rest.rsplit('/').next()?;
    Some(format!("{prefix}/bin/{file}"))
}

#[cfg(test)]
mod which_tokensave_tests {
    use super::*;

    #[test]
    fn warns_for_cargo_profile_paths() {
        for path in [
            "/repo/target/debug/tokensave",
            "/repo/target/release/tokensave",
            "/repo/target/aarch64-apple-darwin/release/tokensave",
            "C:/repo/target/x86_64-pc-windows-msvc/debug/tokensave.exe",
        ] {
            let warning = cargo_build_binary_warning(path)
                .unwrap_or_else(|| panic!("expected warning for {path}"));
            assert!(warning.contains(path));
            assert!(warning.contains("cargo clean"));
            assert!(warning.contains("cargo install"));
        }
    }

    #[test]
    fn ignores_stable_and_near_miss_paths() {
        for path in [
            "/Users/me/.cargo/bin/tokensave",
            "/opt/homebrew/bin/tokensave",
            "/repo/mytarget/release/tokensave",
            "/repo/target/profile/tokensave",
            "/repo/target/foo/bar/release/tokensave",
        ] {
            assert_eq!(
                cargo_build_binary_warning(path),
                None,
                "unexpected warning for {path}"
            );
        }
    }

    // Regression for #146: hooks embedded a version-pinned Homebrew Cellar
    // path, which `brew upgrade`/`brew cleanup` later removes. The stable
    // `<prefix>/bin/tokensave` symlink survives upgrades and must be preferred.

    #[test]
    fn deversions_linuxbrew_cellar_path() {
        assert_eq!(
            homebrew_stable_path("/home/linuxbrew/.linuxbrew/Cellar/tokensave/6.4.2/bin/tokensave"),
            Some("/home/linuxbrew/.linuxbrew/bin/tokensave".to_string())
        );
    }

    #[test]
    fn deversions_macos_arm_cellar_path() {
        assert_eq!(
            homebrew_stable_path("/opt/homebrew/Cellar/tokensave/6.4.2/bin/tokensave"),
            Some("/opt/homebrew/bin/tokensave".to_string())
        );
    }

    #[test]
    fn ignores_non_cellar_cargo_path() {
        assert_eq!(homebrew_stable_path("/Users/me/.cargo/bin/tokensave"), None);
    }

    #[test]
    fn ignores_already_stable_bin_path() {
        assert_eq!(
            homebrew_stable_path("/home/linuxbrew/.linuxbrew/bin/tokensave"),
            None
        );
    }
}

pub fn tool_names() -> Vec<String> {
    get_tool_definitions()
        .iter()
        .map(|t| t.name.clone())
        .collect()
}

pub fn read_only_tool_names() -> Vec<String> {
    get_tool_definitions()
        .iter()
        .filter(|t| {
            t.annotations
                .as_ref()
                .and_then(|annotations| annotations.get("readOnlyHint"))
                .and_then(serde_json::Value::as_bool)
                .unwrap_or(false)
        })
        .map(|t| t.name.clone())
        .collect()
}

pub fn expected_tool_perms() -> Vec<String> {
    get_tool_definitions()
        .iter()
        .map(|t| format!("mcp__tokensave__{}", t.name))
        .collect()
}

/// The single compact permission entry that grants Claude Code all tokensave
/// tools at once, as an alternative to enumerating every tool individually.
/// Both this wildcard form and the bare `mcp__tokensave` form are fully
/// honored by Claude Code as allow rules; this is the one tokensave writes
/// when the compact style is requested.
pub const TOKENSAVE_WILDCARD_PERM: &str = "mcp__tokensave__*";

/// Tool permissions to install for Claude Code: either the single compact
/// wildcard entry, or the full explicit per-tool list, depending on
/// `wildcard`. See [`TOKENSAVE_WILDCARD_PERM`] and [`expected_tool_perms`].
pub fn install_tool_perms(wildcard: bool) -> Vec<String> {
    if wildcard {
        vec![TOKENSAVE_WILDCARD_PERM.to_string()]
    } else {
        expected_tool_perms()
    }
}

/// Regression tests for #255: the silent reinstall-on-upgrade printed every
/// agent's setup banner on every `init`/`sync`, forever.
#[cfg(test)]
mod resync_tests {
    use super::*;
    use crate::user_config::UserConfig;

    /// A config that looks like a fresh external upgrade (`brew upgrade`):
    /// two tracked agents and a stale `last_installed_version`.
    fn upgraded_config() -> UserConfig {
        UserConfig {
            installed_agents: vec!["claude".to_string(), "copilot".to_string()],
            last_installed_version: "7.3.0".to_string(),
            previous_version: "7.4.0".to_string(),
            ..Default::default()
        }
    }

    #[test]
    fn resync_runs_once_then_stops_when_every_agent_succeeds() {
        let mut config = upgraded_config();
        let mut calls = 0;
        let first = resync_installed_agents(&mut config, "7.4.0", |_| {
            calls += 1;
            true
        });
        assert!(first.ran);
        assert_eq!(calls, 2);
        assert!(first.failed.is_empty());

        // Second invocation at the same version must not reinstall again.
        let second = resync_installed_agents(&mut config, "7.4.0", |_| {
            calls += 1;
            true
        });
        assert!(!second.ran, "resync repeated at an unchanged version");
        assert_eq!(calls, 2, "install ran again on the second invocation");
    }

    /// The core of #255: one agent that can never be written (missing app,
    /// read-only path) must not pin the version markers and re-trigger the
    /// whole resync — banner output included — on every subsequent command.
    #[test]
    fn failing_agent_does_not_retrigger_resync_forever() {
        let mut config = upgraded_config();
        let calls = std::cell::Cell::new(0);
        // "copilot" always fails, exactly as an unwritable VS Code settings
        // path would.
        let mut installer = |id: &str| {
            calls.set(calls.get() + 1);
            id != "copilot"
        };

        let first = resync_installed_agents(&mut config, "7.4.0", &mut installer);
        assert!(first.ran);
        assert_eq!(first.failed, vec!["copilot".to_string()]);
        assert!(first.changed, "markers must advance despite the failure");
        assert_eq!(config.last_installed_version, "7.4.0");
        assert_eq!(config.previous_version, "7.4.0");
        assert_eq!(calls.get(), 2);

        // Every later run at the same version is a no-op.
        for _ in 0..3 {
            let again = resync_installed_agents(&mut config, "7.4.0", &mut installer);
            assert!(!again.ran, "a failing agent re-triggered the resync (#255)");
            assert!(again.failed.is_empty());
        }
        assert_eq!(
            calls.get(),
            2,
            "install re-ran after a permanent failure (#255)"
        );
    }

    #[test]
    fn patch_bump_advances_marker_without_reinstalling() {
        let mut config = upgraded_config();
        config.last_installed_version = "7.4.0".to_string();
        config.previous_version = "7.4.1".to_string();
        let mut calls = 0;
        let outcome = resync_installed_agents(&mut config, "7.4.2", |_| {
            calls += 1;
            true
        });
        assert!(!outcome.ran, "patch bump should not reinstall");
        assert_eq!(calls, 0);
        assert!(outcome.changed);
        assert_eq!(config.previous_version, "7.4.2");
    }

    #[test]
    fn no_tracked_agents_is_a_no_op() {
        let mut config = UserConfig {
            previous_version: "7.4.2".to_string(),
            ..Default::default()
        };
        let mut calls = 0;
        let outcome = resync_installed_agents(&mut config, "7.4.2", |_| {
            calls += 1;
            true
        });
        assert_eq!(outcome, ResyncOutcome::default());
        assert_eq!(calls, 0);
    }

    #[test]
    fn quiet_install_suppresses_agent_progress_output() {
        set_quiet_install(true);
        assert!(quiet_install());
        set_quiet_install(false);
        assert!(!quiet_install());
    }
}