agent-file-tools 0.45.1

Agent File Tools — tree-sitter powered code analysis for AI agents
Documentation
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
//! Pure oxc-backed TS/JS module graph facts and export-liveness verdicts.
//!
//! H1-1 intentionally stops at an engine/API boundary: scanners wire this into
//! inspect contributions in H1-2. The engine is pure (files in, verdicts out)
//! and keeps only an in-memory facts cache keyed by file content hash + parser
//! source type + facts format. That is enough for the required warm-cache perf gate while
//! avoiding premature persistence coupling to InspectCache/AppContext.

mod facts;
mod graph;
mod resolver;
pub mod types;

use std::collections::{BTreeMap, BTreeSet};
use std::fs;
use std::path::{Path, PathBuf};
use std::time::SystemTime;

use oxc_span::SourceType;

use facts::parse_file_facts;
use graph::compute_verdicts;
use resolver::{normalize_path, ModuleResolver};
pub use types::{
    DecoratorFact, DynamicImportFact, ExportFact, ExportName, FileFacts, FileId, ImportFact,
    ImportKind, LivenessVerdict, OxcEngineError, OxcEngineResult, OxcEngineStats, OxcExportVerdict,
    OxcFileVerdicts, OxcReExportContext, OxcResolvedEdge, ReExportFact, ReExportKind,
    ResolverConfigInput, OXC_PROVENANCE,
};

pub(crate) const FACTS_FORMAT_VERSION: u32 = 4;

#[derive(Debug, Clone, Default)]
pub struct AnalyzeOptions {
    pub entry_points: Vec<PathBuf>,
    pub public_api_files: Vec<PathBuf>,
    pub executable_root_exports: BTreeMap<PathBuf, BTreeSet<String>>,
    /// Files already proven stale by the inspect freshness layer. These paths
    /// bypass the path metadata fast path so same-size/same-mtime edits are
    /// still re-read and content-hashed before facts are reused.
    pub force_reparse_files: Vec<PathBuf>,
    /// When true, imports/re-exports only make targets live after execution is
    /// reachable from entry/public files. Used by dead_code; unused_exports keeps
    /// the default import-usage semantics.
    pub entry_reachability: bool,
}

#[derive(Debug, Clone, Default)]
pub struct OxcFactsCache {
    entries_by_hash: BTreeMap<String, FileFacts>,
    entries_by_path: BTreeMap<PathBuf, OxcFactsPathEntry>,
}

#[derive(Debug, Clone)]
struct OxcFactsPathEntry {
    mtime: SystemTime,
    size: u64,
    cache_key: String,
}

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct OxcFactsCacheStats {
    pub hits: usize,
    pub misses: usize,
}

impl OxcFactsCache {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn len(&self) -> usize {
        self.entries_by_hash.len()
    }

    pub fn is_empty(&self) -> bool {
        self.entries_by_hash.is_empty()
    }

    fn facts_for_file(
        &mut self,
        file_id: FileId,
        path: &Path,
        force_reparse: bool,
        stats: &mut OxcFactsCacheStats,
    ) -> std::io::Result<FileFacts> {
        let source_type = SourceType::from_path(path).unwrap_or_default();
        let source_type_key = source_type_cache_key(source_type);
        let metadata = fs::metadata(path)?;
        let mtime = metadata.modified().unwrap_or(std::time::UNIX_EPOCH);
        let size = metadata.len();
        let path_key = path.to_path_buf();

        if !force_reparse {
            if let Some(entry) = self.entries_by_path.get(&path_key) {
                if entry.mtime == mtime && entry.size == size {
                    if let Some(cached) = self.entries_by_hash.get(&entry.cache_key) {
                        stats.hits += 1;
                        return Ok(rebind_facts(cached, file_id, path, &cached.content_hash));
                    }
                }
            }
        }

        let source = fs::read_to_string(path)?;
        Ok(self.facts_for_source_with_metadata(
            file_id,
            path,
            &source,
            source_type,
            source_type_key,
            Some((mtime, size)),
            stats,
        ))
    }

    fn facts_for_source_with_metadata(
        &mut self,
        file_id: FileId,
        path: &Path,
        source: &str,
        source_type: SourceType,
        source_type_key: String,
        metadata: Option<(SystemTime, u64)>,
        stats: &mut OxcFactsCacheStats,
    ) -> FileFacts {
        let content_hash = crate::cache_freshness::hash_bytes(source.as_bytes())
            .to_hex()
            .to_string();
        let cache_key = format!("v{FACTS_FORMAT_VERSION}:{source_type_key}:{content_hash}");
        if let Some(cached) = self.entries_by_hash.get(&cache_key) {
            stats.hits += 1;
            if let Some((mtime, size)) = metadata {
                self.entries_by_path.insert(
                    path.to_path_buf(),
                    OxcFactsPathEntry {
                        mtime,
                        size,
                        cache_key,
                    },
                );
            }
            return rebind_facts(cached, file_id, path, &content_hash);
        }

        stats.misses += 1;
        let facts = parse_file_facts(file_id, path, source, content_hash, source_type);
        self.entries_by_hash
            .insert(cache_key.clone(), facts.clone());
        if let Some((mtime, size)) = metadata {
            self.entries_by_path.insert(
                path.to_path_buf(),
                OxcFactsPathEntry {
                    mtime,
                    size,
                    cache_key,
                },
            );
        }
        facts
    }
}

fn rebind_facts(cached: &FileFacts, file_id: FileId, path: &Path, content_hash: &str) -> FileFacts {
    let mut facts = cached.clone();
    facts.file_id = file_id;
    facts.path = path.to_path_buf();
    facts.content_hash = content_hash.to_string();
    facts
}

fn source_type_cache_key(source_type: SourceType) -> String {
    let language = if source_type.is_typescript_definition() {
        "dts"
    } else if source_type.is_typescript() {
        "ts"
    } else {
        "js"
    };
    let module_kind = if source_type.is_commonjs() {
        "commonjs"
    } else if source_type.is_module() {
        "module"
    } else if source_type.is_script() {
        "script"
    } else {
        "unambiguous"
    };
    let variant = if source_type.is_jsx() {
        "jsx"
    } else {
        "standard"
    };

    format!("{language}:{module_kind}:{variant}")
}

pub fn analyze_files(
    project_root: &Path,
    files: &[PathBuf],
    options: AnalyzeOptions,
) -> Result<OxcEngineResult, String> {
    let mut cache = OxcFactsCache::new();
    analyze_files_with_cache(project_root, files, options, &mut cache)
}

pub fn analyze_files_with_cache(
    project_root: &Path,
    files: &[PathBuf],
    options: AnalyzeOptions,
    cache: &mut OxcFactsCache,
) -> Result<OxcEngineResult, String> {
    // De-verbatim the canonical root (Windows \\?\ form) so strip_prefix
    // against normalize_path-built module paths keeps working.
    let project_root = fs::canonicalize(project_root)
        .map(|canonical| normalize_path(&canonical))
        .unwrap_or_else(|_| normalize_path(project_root));
    let force_reparse_files = normalize_option_paths(&options.force_reparse_files);
    let normalized_files = normalize_file_set(&project_root, files);
    let files = normalized_files.files;
    let skipped_outside_root = normalized_files.skipped_outside_root;
    let mut cache_stats = OxcFactsCacheStats::default();
    let mut errors = Vec::new();
    let mut facts = Vec::with_capacity(files.len());

    for (idx, path) in files.iter().enumerate() {
        match cache.facts_for_file(
            FileId(idx),
            path,
            force_reparse_files.contains(path),
            &mut cache_stats,
        ) {
            Ok(file_facts) => facts.push(file_facts),
            Err(error) => errors.push(OxcEngineError {
                file: path.clone(),
                message: format!("read: {error}"),
            }),
        }
    }

    Ok(analyze_preparsed_facts(
        project_root,
        facts,
        options,
        cache_stats,
        errors,
        skipped_outside_root,
    ))
}

pub(crate) fn analyze_file_facts(
    project_root: &Path,
    facts: Vec<FileFacts>,
    options: AnalyzeOptions,
    skipped_outside_root: Vec<PathBuf>,
) -> OxcEngineResult {
    // Same de-verbatim rule as analyze_files_with_cache: FileFacts paths are
    // normalize_path-built, so the root they are relativized against must be too.
    let project_root = fs::canonicalize(project_root)
        .map(|canonical| normalize_path(&canonical))
        .unwrap_or_else(|_| normalize_path(project_root));
    analyze_preparsed_facts(
        project_root,
        facts,
        options,
        OxcFactsCacheStats::default(),
        Vec::new(),
        skipped_outside_root,
    )
}

fn analyze_preparsed_facts(
    project_root: PathBuf,
    mut facts: Vec<FileFacts>,
    options: AnalyzeOptions,
    cache_stats: OxcFactsCacheStats,
    mut errors: Vec<OxcEngineError>,
    skipped_outside_root: Vec<PathBuf>,
) -> OxcEngineResult {
    // Preserve dense FileId indexing when unreadable files were skipped or facts
    // were reconstructed from contribution records.
    for (idx, fact) in facts.iter_mut().enumerate() {
        fact.file_id = FileId(idx);
        if let Some(parse_error) = &fact.parse_error {
            errors.push(OxcEngineError {
                file: fact.path.clone(),
                message: format!("parse: {parse_error}"),
            });
        }
    }
    let resolved_files = facts
        .iter()
        .map(|fact| fact.path.clone())
        .collect::<Vec<_>>();
    let resolver = ModuleResolver::new(&project_root, &resolved_files);
    let (resolved_modules, tracker, edges) = resolver.resolve_modules(&facts);
    let entry_points = normalize_option_paths(&options.entry_points);
    let public_api_files = normalize_option_paths(&options.public_api_files);
    let executable_root_exports =
        normalize_executable_root_exports(&options.executable_root_exports);
    let file_verdicts = compute_verdicts(
        &project_root,
        &resolved_modules,
        &entry_points,
        &public_api_files,
        &executable_root_exports,
        options.entry_reachability,
    );
    let resolved_edges = edges
        .iter()
        .filter(|edge| edge.resolved_file.is_some())
        .count();
    let unresolved_edges = edges.len().saturating_sub(resolved_edges);
    let resolver_config_inputs = tracker.inputs();
    let resolver_config_fingerprint = tracker.fingerprint();

    OxcEngineResult {
        files: file_verdicts,
        facts,
        resolver_config_inputs,
        resolver_config_fingerprint,
        edges,
        stats: OxcEngineStats {
            files: resolved_files.len(),
            cache_hits: cache_stats.hits,
            cache_misses: cache_stats.misses,
            resolved_edges,
            unresolved_edges,
        },
        errors,
        skipped_outside_root,
    }
}

#[derive(Debug, Default)]
struct NormalizedFileSet {
    files: Vec<PathBuf>,
    skipped_outside_root: Vec<PathBuf>,
}

fn normalize_file_set(project_root: &Path, files: &[PathBuf]) -> NormalizedFileSet {
    let mut normalized = NormalizedFileSet::default();
    for path in files.iter().filter(|path| is_ts_js_file(path)) {
        let path = normalize_input_path(project_root, path);
        if path.strip_prefix(project_root).is_ok() {
            normalized.files.push(path);
        } else {
            normalized.skipped_outside_root.push(path);
        }
    }

    normalized.files.sort();
    normalized.files.dedup();
    normalized.skipped_outside_root.sort();
    normalized.skipped_outside_root.dedup();
    normalized
}

pub(crate) fn normalize_input_path(project_root: &Path, path: &Path) -> PathBuf {
    // Route the canonicalized form through normalize_path too: on Windows,
    // fs::canonicalize returns verbatim (\\?\C:\) paths, while every set we
    // compare module paths against (entry_points, public_api_files,
    // executable_root_exports) is built via normalize_path, which strips the
    // verbatim prefix. Returning the raw canonical form makes those membership
    // checks silently miss on Windows only.
    fs::canonicalize(path)
        .map(|canonical| normalize_path(&canonical))
        .unwrap_or_else(|_| {
            if path.is_absolute() {
                normalize_path(path)
            } else {
                normalize_path(&project_root.join(path))
            }
        })
}

fn normalize_executable_root_exports(
    roots: &BTreeMap<PathBuf, BTreeSet<String>>,
) -> BTreeMap<PathBuf, BTreeSet<String>> {
    let mut normalized = BTreeMap::<PathBuf, BTreeSet<String>>::new();
    for (path, exports) in roots {
        normalized
            .entry(normalize_path(path))
            .or_default()
            .extend(exports.iter().cloned());
        if let Ok(canonical) = fs::canonicalize(path) {
            normalized
                .entry(normalize_path(&canonical))
                .or_default()
                .extend(exports.iter().cloned());
        }
    }
    normalized
}

fn normalize_option_paths(paths: &[PathBuf]) -> BTreeSet<PathBuf> {
    // Both insertions go through normalize_path (which de-verbatims Windows
    // \\?\ canonical forms) so membership checks against module paths built by
    // normalize_input_path always compare like with like.
    let mut normalized = BTreeSet::new();
    for path in paths {
        normalized.insert(normalize_path(path));
        if let Ok(canonical) = fs::canonicalize(path) {
            normalized.insert(normalize_path(&canonical));
        }
    }
    normalized
}

fn is_ts_js_file(path: &Path) -> bool {
    path.extension()
        .and_then(|ext| ext.to_str())
        .is_some_and(|ext| {
            matches!(
                ext,
                "ts" | "tsx" | "js" | "jsx" | "mts" | "cts" | "mjs" | "cjs"
            )
        })
}