tokensave 7.16.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
//! Conservative fallback detection. Evidence levels are rules, not probabilities.
//! No parser probing, interpreter execution, or persistent side cache is used.
use std::collections::{HashMap, VecDeque};
use std::io::Read;
use std::path::{Path, PathBuf};
use std::sync::{LazyLock, Mutex};
use std::time::{Instant, SystemTime};

use serde::{Deserialize, Serialize};

use crate::extraction::{LanguageExtractor, LanguageRegistry};

const DEFAULT_MAX_BYTES: usize = 4096;
const CACHE_LIMIT: usize = 4096;

/// Only unknown files are eligible; existing resolution always takes precedence.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct DetectionConfig {
    pub enabled: bool,
    #[serde(deserialize_with = "read_max_bytes")]
    pub max_bytes: usize,
    pub cache_results: bool,
}

impl Default for DetectionConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            max_bytes: DEFAULT_MAX_BYTES,
            cache_results: true,
        }
    }
}

fn read_max_bytes<'de, D: serde::Deserializer<'de>>(d: D) -> Result<usize, D::Error> {
    let value = usize::deserialize(d)?;
    if !(128..=65536).contains(&value) {
        return Err(serde::de::Error::custom(
            "language_detection.max_bytes must be 128..=65536",
        ));
    }
    Ok(value)
}

impl DetectionConfig {
    fn valid(self) -> bool {
        (128..=65536).contains(&self.max_bytes)
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Method {
    Shebang,
    Content,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Detection {
    pub language: &'static str,
    pub method: Method,
}

/// Process-local statistics; snapshots can be subtracted around a scan.
#[derive(Debug, Default, Clone, Copy)]
pub struct Statistics {
    pub examined: u64,
    pub shebang: u64,
    pub content: u64,
    pub unsupported: u64,
    pub cache_hits: u64,
    pub micros: u64,
}

impl std::ops::Sub for Statistics {
    type Output = Self;
    fn sub(self, rhs: Self) -> Self {
        Self {
            examined: self.examined.saturating_sub(rhs.examined),
            shebang: self.shebang.saturating_sub(rhs.shebang),
            content: self.content.saturating_sub(rhs.content),
            unsupported: self.unsupported.saturating_sub(rhs.unsupported),
            cache_hits: self.cache_hits.saturating_sub(rhs.cache_hits),
            micros: self.micros.saturating_sub(rhs.micros),
        }
    }
}

#[derive(Hash, PartialEq, Eq)]
struct CacheKey {
    path: PathBuf,
    modified: Option<SystemTime>,
    changed: Option<SystemTime>,
    size: u64,
    config: DetectionConfig,
    // Feature availability is part of the key, including negative results.
    languages: std::sync::Arc<str>,
}

#[derive(Default)]
struct Cache {
    entries: HashMap<CacheKey, Option<Detection>>,
    order: VecDeque<CacheKey>,
    stats: Statistics,
}

// At most 4096 decisions, including negatives. FIFO eviction releases paths and
// language fingerprints; no source bytes are retained and no tasks are spawned.
static CACHE: LazyLock<Mutex<Cache>> = LazyLock::new(|| Mutex::new(Cache::default()));

pub fn statistics() -> Statistics {
    CACHE.lock().map(|c| c.stats).unwrap_or_default()
}

/// Reject sensitive formats before opening them, even if their text resembles code.
pub fn eligible(path: &Path) -> bool {
    let name = path
        .file_name()
        .and_then(|s| s.to_str())
        .unwrap_or_default()
        .to_ascii_lowercase();
    let ext = path
        .extension()
        .and_then(|s| s.to_str())
        .unwrap_or_default()
        .to_ascii_lowercase();
    !matches!(
        ext.as_str(),
        "pem"
            | "key"
            | "crt"
            | "cer"
            | "der"
            | "p12"
            | "pfx"
            | "pub"
            | "p7b"
            | "p7c"
            | "keystore"
            | "jks"
    ) && !matches!(
        name.as_str(),
        "id_rsa" | "id_dsa" | "id_ecdsa" | "id_ed25519" | ".env"
    )
}

/// Bounded scan read. Metadata cache follows the same timestamp assumption as
/// incremental sync, at nanosecond precision and with Unix ctime as extra evidence.
/// A changed stat, settings, extractor set, or worktree path invalidates the key.
pub fn detect_file(
    registry: &LanguageRegistry,
    path: &Path,
    config: DetectionConfig,
) -> Option<Detection> {
    if !config.enabled || !config.valid() || !eligible(path) {
        return None;
    }
    let start = Instant::now();
    let metadata = std::fs::metadata(path).ok()?;
    if !metadata.is_file() {
        return None;
    }
    #[cfg(unix)]
    let changed = {
        use std::os::unix::fs::MetadataExt;
        u64::try_from(metadata.ctime()).ok().and_then(|seconds| {
            SystemTime::UNIX_EPOCH.checked_add(std::time::Duration::new(
                seconds,
                metadata.ctime_nsec() as u32,
            ))
        })
    };
    #[cfg(not(unix))]
    let changed = metadata.created().ok();
    let key = CacheKey {
        path: path.to_path_buf(),
        modified: metadata.modified().ok(),
        changed,
        size: metadata.len(),
        config,
        languages: registry.detection_languages(),
    };
    if config.cache_results {
        if let Ok(mut cache) = CACHE.lock() {
            if let Some(result) = cache.entries.get(&key).copied() {
                cache.stats.cache_hits += 1;
                return result;
            }
        }
    }
    let mut bytes = Vec::with_capacity(config.max_bytes + 4);
    std::fs::File::open(path)
        .ok()?
        .take((config.max_bytes + 4) as u64)
        .read_to_end(&mut bytes)
        .ok()?;
    let result = detect(registry, &bytes, config);
    if let Ok(mut cache) = CACHE.lock() {
        cache.stats.examined += 1;
        cache.stats.micros += start.elapsed().as_micros() as u64;
        match result.map(|d| d.method) {
            Some(Method::Shebang) => cache.stats.shebang += 1,
            Some(Method::Content) => cache.stats.content += 1,
            None => cache.stats.unsupported += 1,
        }
        if config.cache_results && !cache.entries.contains_key(&key) {
            while cache.entries.len() >= CACHE_LIMIT {
                if let Some(old) = cache.order.pop_front() {
                    cache.entries.remove(&old);
                } else {
                    break;
                }
            }
            // The queue owns the key and the map owns an equivalent key; both
            // are bounded by CACHE_LIMIT. No source content is retained.
            let queued = CacheKey {
                path: key.path.clone(),
                languages: key.languages.clone(),
                ..key
            };
            cache.order.push_back(queued);
            cache.entries.insert(key, result);
        }
    }
    result
}

/// Detect from an already available source buffer, with no filesystem reads.
/// A partial final UTF-8 codepoint at the sampling boundary is ignored when
/// the caller supplies a longer buffer. Invalid samples fail conservatively.
pub fn detect(
    registry: &LanguageRegistry,
    bytes: &[u8],
    config: DetectionConfig,
) -> Option<Detection> {
    if !config.enabled || !config.valid() {
        return None;
    }
    let sample = &bytes[..bytes.len().min(config.max_bytes)];
    let text = match std::str::from_utf8(sample) {
        Ok(text) => text,
        Err(e) if e.error_len().is_none() && bytes.len() > config.max_bytes => {
            std::str::from_utf8(&sample[..e.valid_up_to()]).ok()?
        }
        Err(_) => return None,
    };
    let text = text.strip_prefix('\u{feff}').unwrap_or(text);
    if text
        .chars()
        .any(|c| c.is_control() && !matches!(c, '\n' | '\r' | '\t'))
        || text.contains("-----BEGIN ")
    {
        return None;
    }
    let (language, method) = if text.starts_with("#!") {
        // An unrecognized shebang is authoritative evidence against guessing
        // another language from the rest of the file.
        (shebang(text.lines().next()?)?, Method::Shebang)
    } else {
        (content_language(text)?, Method::Content)
    };
    extractor_for_detected_language(registry, language)?;
    Some(Detection { language, method })
}

fn shebang(line: &str) -> Option<&'static str> {
    let mut words = line.strip_prefix("#!")?.split_whitespace();
    let executable = words.next()?;
    if !executable.starts_with('/') {
        return None;
    }
    let mut name = executable.rsplit('/').next()?;
    if name == "env" {
        name = words.next()?;
        if name == "-S" {
            name = words.next()?;
        }
        // env flags, assignments and quoted commands are deliberately rejected.
    }
    match name {
        "python" | "python2" | "python3" => Some("Python"),
        n if n
            .strip_prefix("python3.")
            .is_some_and(|s| !s.is_empty() && s.bytes().all(|b| b.is_ascii_digit())) =>
        {
            Some("Python")
        }
        "bash" | "sh" | "dash" | "ksh" => Some("Bash"),
        "node" | "nodejs" => Some("JavaScript"),
        "ruby" => Some("Ruby"),
        _ => None,
    }
}

fn content_language(text: &str) -> Option<&'static str> {
    // Require two independent, characteristic signals. Generic words such as
    // `class`, `import`, or `function` alone cannot classify a file.
    let mut py_def = false;
    let mut py_body = false;
    let mut py_import = false;
    let mut rust_fn = false;
    let mut rust_marker = false;
    let mut js_fn = false;
    let mut js_module = false;
    let mut ts_type = false;
    let mut go_package = false;
    let mut go_fn = false;
    let mut ruby_def = false;
    let mut ruby_end = false;
    let mut c_include = false;
    let mut c_main = false;
    let mut cpp_marker = false;
    let mut block_comment = false;
    for line in text.lines() {
        let s = line.trim();
        if block_comment {
            block_comment = !s.contains("*/");
            continue;
        }
        if s.starts_with("/*") {
            block_comment = !s.contains("*/");
            continue;
        }
        if s.starts_with('#') && !s.starts_with("#include") {
            continue;
        }
        if s.starts_with("//") || s.starts_with("/*") || s.starts_with('*') {
            continue;
        }
        py_def |= (s.starts_with("def ") || s.starts_with("async def "))
            && s.contains('(')
            && s.ends_with(':');
        py_body |= line.starts_with("    ")
            && (s.starts_with("return ") || s == "pass" || s.starts_with("raise "));
        py_import |= s.starts_with("from ") && s.contains(" import ");
        rust_fn |=
            (s.starts_with("fn ") || s.starts_with("pub fn ") || s.starts_with("pub async fn "))
                && s.contains('(')
                && s.contains('{');
        rust_marker |= (s.starts_with("use ") && s.contains("::") && s.ends_with(';'))
            || s.starts_with("impl ") && s.ends_with('{')
            || s.starts_with("mod ") && s.ends_with(';')
            || s.contains("println!(");
        js_fn |= (s.starts_with("function ")
            || s.starts_with("export function ")
            || s.starts_with("async function "))
            && s.contains('(')
            && s.contains('{')
            || (s.starts_with("const ") && s.contains("=>"));
        js_module |= (s.starts_with("import ") || s.starts_with("export "))
            && (s.contains(" from ")
                || s.starts_with("export function ")
                || s.starts_with("export const "));
        ts_type |= (s.starts_with("interface ") || s.starts_with("export interface "))
            && s.contains('{')
            || (s.starts_with("type ") || s.starts_with("export type ")) && s.contains(" = ");
        go_package |=
            s.starts_with("package ") && !s.ends_with(';') && s.split_whitespace().count() == 2;
        go_fn |= s.starts_with("func ") && s.contains('(') && s.ends_with('{');
        ruby_def |= s.starts_with("def ") && !s.contains(':') && !s.contains('{');
        ruby_end |= s == "end";
        c_include |= s.starts_with("#include <") && s.ends_with('>');
        c_main |= (s.starts_with("int main(") || s.starts_with("int main (")) && s.contains('{');
        cpp_marker |= s.starts_with("using namespace ") && s.ends_with(';')
            || s.starts_with("std::")
            || s.starts_with("template<")
            || s.starts_with("template <");
    }
    let candidates = [
        ("Python", py_def && (py_body || py_import)),
        ("Rust", rust_fn && rust_marker),
        ("JavaScript", js_fn && js_module && !ts_type),
        ("TypeScript", ts_type && (js_fn || js_module)),
        ("Go", go_package && go_fn),
        ("Ruby", ruby_def && ruby_end),
        ("C", c_include && c_main && !cpp_marker),
        ("C++", c_include && cpp_marker),
    ];
    let mut matches = candidates.into_iter().filter(|(_, matched)| *matched);
    let (language, _) = matches.next()?;
    if matches.next().is_some() {
        None
    } else {
        Some(language)
    }
}

/// Shared extraction fallback for in-process and isolated worker paths.
pub fn extractor_for_source<'r>(
    registry: &'r LanguageRegistry,
    root: &Path,
    path: &str,
    source: &str,
) -> Option<&'r dyn LanguageExtractor> {
    if !eligible(Path::new(path)) {
        return None;
    }
    // This is reached only for unknown paths; recognized files incur no config I/O.
    let config = settings_for(root)?;
    let result = detect(registry, source.as_bytes(), config)?;
    extractor_for_detected_language(registry, result.language)
}

fn extractor_for_detected_language<'r>(
    registry: &'r LanguageRegistry,
    language: &str,
) -> Option<&'r dyn LanguageExtractor> {
    // JavaScript shares the registered TypeScript extractor.
    registry.extractor_for_language(if language == "JavaScript" {
        "js"
    } else {
        language
    })
}

// Settings are cached separately because workers dispatch multiple files. At
// most 32 project roots are retained. File I/O occurs outside the lock.
type SettingsEntry = (PathBuf, Option<SystemTime>, u64, DetectionConfig);
static SETTINGS: LazyLock<Mutex<VecDeque<SettingsEntry>>> =
    LazyLock::new(|| Mutex::new(VecDeque::new()));

fn settings_for(root: &Path) -> Option<DetectionConfig> {
    let metadata = std::fs::metadata(crate::config::get_config_path(root)).ok();
    let modified = metadata.as_ref().and_then(|m| m.modified().ok());
    let size = metadata.as_ref().map_or(0, std::fs::Metadata::len);
    if let Ok(cache) = SETTINGS.lock() {
        if let Some(entry) = cache
            .iter()
            .find(|e| e.0 == root && e.1 == modified && e.2 == size)
        {
            return Some(entry.3);
        }
    }
    let config = crate::config::load_config(root).ok()?.language_detection;
    if let Ok(mut cache) = SETTINGS.lock() {
        cache.retain(|e| e.0 != root);
        if cache.len() == 32 {
            cache.pop_front();
        }
        cache.push_back((root.to_path_buf(), modified, size, config));
    }
    Some(config)
}

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

    #[test]
    fn decision_cache_has_a_fixed_bound_and_releases_evicted_paths() {
        let dir = tempfile::tempdir().unwrap();
        let registry = LanguageRegistry::new();
        let config = DetectionConfig {
            enabled: true,
            ..DetectionConfig::default()
        };
        for i in 0..=CACHE_LIMIT {
            let path = dir.path().join(format!("{i}.custom"));
            std::fs::write(&path, "unclassified text").unwrap();
            assert!(detect_file(&registry, &path, config).is_none());
        }
        let cache = CACHE.lock().unwrap();
        assert!(cache.entries.len() <= CACHE_LIMIT);
        assert_eq!(cache.entries.len(), cache.order.len());
        assert!(!cache
            .entries
            .keys()
            .any(|k| k.path == dir.path().join("0.custom")));
    }
}