vtcode-core 0.185.1

Core library for VT Code - a Rust-based terminal coding agent
Documentation
//! Workspace tool-config snapshot shared with the pack registry.
//!
//! Builtins register through `pack_impls` -> `register_builtin_packs` ->
//! `BUILTIN_PACKS`. Packs receive [`ToolConfigSnapshot`] explicitly; the
//! `TOOL_CONFIG` global below is a compatibility slot for callers that install
//! a process-wide snapshot, and the active registry must not read it instead of
//! the snapshot it was given.
use std::sync::OnceLock;

use vtcode_config::{WebFetchConfig, WebSearchConfig};

/// Snapshot of the user-configurable bits of `ToolsConfig` that built-in
/// tools care about. A subset is intentional: most tools don't need the
/// whole `ToolsConfig`, and a narrow struct keeps the global slot small.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct ToolConfigSnapshot {
    /// Settings for the `web_search` tool (provider, cooldown, cache, cap).
    pub web_search: WebSearchConfig,
    /// Fail closed for web tools when the workspace config cannot be loaded.
    pub web_config_load_failed: bool,
    /// Settings for the `web_fetch` tool (mode, allow/block lists, https).
    pub web_fetch: WebFetchConfig,
}

static TOOL_CONFIG: OnceLock<ToolConfigSnapshot> = OnceLock::new();

/// Install the tool config snapshot for the legacy distributed registration
/// compatibility path. The active pack-based registry does not call this
/// function; tests that exercise legacy `register_*` functions in isolation
/// may still use it.
///
/// A second call with the SAME snapshot is a no-op (lets tests share
/// state across multiple `ToolRegistry::new` invocations in one
/// process). A second call with a DIFFERENT snapshot returns an error,
/// since silently dropping the new value would mask a real config-loading
/// bug (e.g. two vtcode.toml files racing during a hot reload).
#[allow(
    dead_code,
    reason = "Installer kept test-facing: tool config snapshots are installed by test setups today."
)]
pub fn install_tool_config(snapshot: ToolConfigSnapshot) -> anyhow::Result<()> {
    match TOOL_CONFIG.set(snapshot) {
        Ok(()) => Ok(()),
        Err(new) => {
            let existing = TOOL_CONFIG.get().expect("set failed; lock should be initialized");
            if existing != &new {
                Err(anyhow::anyhow!(
                    "install_tool_config called with a different snapshot; \
                     first install and second install disagree on the user config"
                ))
            } else {
                Ok(())
            }
        }
    }
}

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

    /// The second install of the SAME snapshot is a no-op. This is the
    /// common case in tests that build multiple `ToolRegistry` instances
    /// in one process.
    #[test]
    fn install_tool_config_re_install_with_same_snapshot_is_a_noop() {
        // The first install may have already happened in an earlier test
        // case in this process. We re-install the same default snapshot;
        // the function must return Ok, even if a prior install exists.
        install_tool_config(ToolConfigSnapshot::default()).ok();
    }

    /// A second install with a DIFFERENT snapshot returns an error. This guards
    /// against a real bug (two config files racing during a hot reload,
    /// or a test that forgot to reset the global) silently dropping the
    /// new value.
    #[test]
    fn install_tool_config_errors_on_diverging_snapshot() {
        // Force a divergent install by clearing whatever is already
        // there and re-installing with a non-default value.
        let divergent = ToolConfigSnapshot {
            web_search: WebSearchConfig { max_results: 999, ..WebSearchConfig::default() },
            ..ToolConfigSnapshot::default()
        };
        // If a prior test already installed a default snapshot, this
        // divergent install should return an error because the values disagree.
        // If no prior install has happened yet, this will succeed;
        // to make the test deterministic we first install a known default,
        // then the divergent one.
        install_tool_config(ToolConfigSnapshot::default()).ok();
        let result = install_tool_config(divergent);
        assert!(result.is_err(), "expected error on diverging snapshot, got: {result:?}");
    }
}