diff --git a/Cargo.lock b/Cargo.lock index e6d196d8f..a1bfa72f5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8370,7 +8370,6 @@ dependencies = [ "daachorse", "fst", "getrandom 0.3.4", - "lazy_static", "log", "regex", "reqwest 0.12.28", @@ -8615,7 +8614,6 @@ dependencies = [ "criterion", "futures", "genai", - "lazy_static", "log", "rand 0.9.2", "regex", @@ -8717,7 +8715,6 @@ dependencies = [ "criterion", "csv", "itertools 0.14.0", - "lazy_static", "log", "memoize", "regex", diff --git a/crates/terraphim_automata/Cargo.toml b/crates/terraphim_automata/Cargo.toml index 0143433ab..f76630d01 100644 --- a/crates/terraphim_automata/Cargo.toml +++ b/crates/terraphim_automata/Cargo.toml @@ -55,5 +55,4 @@ medical = ["daachorse", "zstd", "anyhow"] [dev-dependencies] criterion = "0.8" tempfile = "3.27" -lazy_static = "1.4.0" tokio = { version = "1", features = ["io-util", "time","macros","rt","rt-multi-thread"] } diff --git a/crates/terraphim_automata/benches/autocomplete_bench.rs b/crates/terraphim_automata/benches/autocomplete_bench.rs index 169d5ba1e..b78a7c12f 100644 --- a/crates/terraphim_automata/benches/autocomplete_bench.rs +++ b/crates/terraphim_automata/benches/autocomplete_bench.rs @@ -29,12 +29,12 @@ use terraphim_automata::{AutomataPath, load_thesaurus}; use tokio::runtime::Runtime; #[cfg(feature = "tokio-runtime")] -lazy_static::lazy_static! { - static ref TOKIO_RUNTIME: Runtime = tokio::runtime::Builder::new_current_thread() +static TOKIO_RUNTIME: std::sync::LazyLock = std::sync::LazyLock::new(|| { + tokio::runtime::Builder::new_current_thread() .enable_all() .build() - .unwrap(); -} + .unwrap() +}); // We can use this `block_on` function to run async code in the benchmarks when tokio is available #[cfg(feature = "tokio-runtime")] diff --git a/crates/terraphim_multi_agent/Cargo.toml b/crates/terraphim_multi_agent/Cargo.toml index 607031f50..aa2163308 100644 --- a/crates/terraphim_multi_agent/Cargo.toml +++ b/crates/terraphim_multi_agent/Cargo.toml @@ -35,7 +35,6 @@ rand = "0.9" # tiktoken-rs = "0.6" # Now handled by rust-genai tracing = "0.1" regex = "1.12" -lazy_static = "1.4" # Terraphim dependencies terraphim_types = { path = "../terraphim_types" } diff --git a/crates/terraphim_multi_agent/src/prompt_sanitizer.rs b/crates/terraphim_multi_agent/src/prompt_sanitizer.rs index 36f668bda..81a9ef955 100644 --- a/crates/terraphim_multi_agent/src/prompt_sanitizer.rs +++ b/crates/terraphim_multi_agent/src/prompt_sanitizer.rs @@ -1,14 +1,15 @@ -use lazy_static::lazy_static; use regex::Regex; use std::collections::HashSet; +use std::sync::LazyLock; use tracing::warn; const MAX_PROMPT_LENGTH: usize = 10_000; -lazy_static! { - static ref SUSPICIOUS_PATTERNS: Vec = vec![ +static SUSPICIOUS_PATTERNS: LazyLock> = LazyLock::new(|| { + vec![ Regex::new(r"(?i)ignore\s+\s*(previous|above|prior)\s+\s*(instructions|prompts?)").unwrap(), - Regex::new(r"(?i)disregard\s+\s*(previous|above|all)\s+\s*(instructions|prompts?)").unwrap(), + Regex::new(r"(?i)disregard\s+\s*(previous|above|all)\s+\s*(instructions|prompts?)") + .unwrap(), Regex::new(r"(?i)system\s*:\s*you\s+\s*are\s+\s*now").unwrap(), Regex::new(r"(?i)<\|?im_start\|?>").unwrap(), Regex::new(r"(?i)<\|?im_end\|?>").unwrap(), @@ -16,13 +17,16 @@ lazy_static! { Regex::new(r"(?i)forget\s+\s*(everything|all|previous)").unwrap(), Regex::new(r"\x00").unwrap(), Regex::new(r"[\x01-\x08\x0B-\x0C\x0E-\x1F\x7F]").unwrap(), - ]; - static ref CONTROL_CHAR_PATTERN: Regex = - Regex::new(r"[\x00-\x08\x0B-\x0C\x0E-\x1F\x7F]").unwrap(); + ] +}); - // Unicode special characters that can be used for obfuscation or attacks. - // HashSet for O(1) per-character lookup. - static ref UNICODE_SPECIAL_CHARS: HashSet = [ +static CONTROL_CHAR_PATTERN: LazyLock = + LazyLock::new(|| Regex::new(r"[\x00-\x08\x0B-\x0C\x0E-\x1F\x7F]").unwrap()); + +// Unicode special characters that can be used for obfuscation or attacks. +// HashSet for O(1) per-character lookup. +static UNICODE_SPECIAL_CHARS: LazyLock> = LazyLock::new(|| { + [ '\u{202E}', // RIGHT-TO-LEFT OVERRIDE '\u{202D}', // LEFT-TO-RIGHT OVERRIDE '\u{202C}', // POP DIRECTIONAL FORMATTING @@ -43,8 +47,11 @@ lazy_static! { '\u{206D}', // ACTIVATE ARABIC FORM SHAPING '\u{206E}', // NATIONAL DIGIT SHAPES '\u{206F}', // NOMINAL DIGIT SHAPES - ].iter().copied().collect(); -} + ] + .iter() + .copied() + .collect() +}); #[derive(Debug, Clone)] pub struct SanitizedPrompt { diff --git a/crates/terraphim_multi_agent/tests/concurrent_security_test.rs b/crates/terraphim_multi_agent/tests/concurrent_security_test.rs index 40550449e..91bdca8d9 100644 --- a/crates/terraphim_multi_agent/tests/concurrent_security_test.rs +++ b/crates/terraphim_multi_agent/tests/concurrent_security_test.rs @@ -129,8 +129,8 @@ async fn test_sanitizer_thread_safety() { } #[test] -fn test_lazy_static_thread_safety() { - // Verify lazy_static patterns are initialized safely +fn test_lazy_lock_thread_safety() { + // Verify LazyLock patterns are initialized safely // This tests the regex compilation in SUSPICIOUS_PATTERNS use std::thread; @@ -150,7 +150,7 @@ fn test_lazy_static_thread_safety() { #[tokio::test] async fn test_unicode_chars_vec_concurrent_access() { - // Test concurrent access to UNICODE_SPECIAL_CHARS lazy_static + // Test concurrent access to UNICODE_SPECIAL_CHARS LazyLock let mut handles = vec![]; for _ in 0..50 { diff --git a/crates/terraphim_rolegraph/Cargo.toml b/crates/terraphim_rolegraph/Cargo.toml index eeb3e2cce..a2e09f4d6 100644 --- a/crates/terraphim_rolegraph/Cargo.toml +++ b/crates/terraphim_rolegraph/Cargo.toml @@ -19,7 +19,6 @@ ahash = { version = "0.8.3", features = ["serde"] } aho-corasick = "1.0.2" anyhow = { version = "1.0", optional = true } itertools = "0.14.0" -lazy_static = "1.4.0" log = "0.4.29" memoize = "0.5.1" regex = "1.12.3" diff --git a/crates/terraphim_rolegraph/benches/throughput.rs b/crates/terraphim_rolegraph/benches/throughput.rs index b79a0eeb7..2fb3b5518 100644 --- a/crates/terraphim_rolegraph/benches/throughput.rs +++ b/crates/terraphim_rolegraph/benches/throughput.rs @@ -16,9 +16,8 @@ use terraphim_rolegraph::input::TEST_CORPUS; use terraphim_rolegraph::split_paragraphs; use terraphim_types::{Document, DocumentType, Thesaurus}; -lazy_static::lazy_static! { - static ref TOKIO_RUNTIME: Runtime = Runtime::new().unwrap(); -} +static TOKIO_RUNTIME: std::sync::LazyLock = + std::sync::LazyLock::new(|| Runtime::new().unwrap()); // We can use this `block_on` function to run async code in the benchmarks // without having to use `async fn`, which is not supported by the `criterion` library. diff --git a/crates/terraphim_rolegraph/src/lib.rs b/crates/terraphim_rolegraph/src/lib.rs index 31bff524e..84cde59cf 100644 --- a/crates/terraphim_rolegraph/src/lib.rs +++ b/crates/terraphim_rolegraph/src/lib.rs @@ -1249,11 +1249,8 @@ impl From for RoleGraphSync { } } -#[macro_use] -extern crate lazy_static; -lazy_static! { - static ref RE: Regex = Regex::new(r"[?!|]\s+").unwrap(); -} +static RE: std::sync::LazyLock = + std::sync::LazyLock::new(|| Regex::new(r"[?!|]\s+").unwrap()); pub fn split_paragraphs(paragraphs: &str) -> Vec<&str> { let sentences = UnicodeSegmentation::split_sentence_bounds(paragraphs);