Skip to main content

fallow_engine/
session.rs

1//! Engine-owned analysis session orchestration.
2
3use std::path::{Path, PathBuf};
4use std::sync::atomic::{AtomicBool, Ordering};
5use std::sync::{Arc, Mutex};
6use std::time::Instant;
7
8use fallow_config::{DuplicatesConfig, ResolvedConfig, WorkspaceInfo};
9use fallow_types::cache_rejection::CacheRejection;
10use fallow_types::discover::DiscoveredFile;
11use fallow_types::extract::{ModuleInfo, SourceParseDegradation, SourceReadFailure};
12#[cfg(test)]
13use fallow_types::results::AnalysisResults;
14use fallow_types::source_fingerprint::{SourceFingerprint, now_ns};
15use fallow_types::workspace::{WorkspaceDiagnostic, merge_workspace_diagnostics};
16use rustc_hash::{FxHashMap, FxHashSet};
17
18pub use crate::session_reuse::SessionParseCounts;
19use crate::session_reuse::{
20    MAX_INCREMENTAL_REPARSE_FILES, ParseCountCells, changed_file_indices, merge_reparsed_modules,
21};
22use crate::{
23    EngineResult, core_backend, duplicates,
24    project_analysis::{
25        ProjectAnalysisArtifactOptions, ProjectAnalysisArtifacts, ProjectAnalysisOutput,
26    },
27    project_config::{ProjectConfig, config_for_project, default_project_config},
28    results::{
29        DeadCodeAnalysis, DeadCodeAnalysisArtifacts, DeadCodeAnalysisOutput, DuplicationAnalysis,
30        SharedDeadCodeAnalysisArtifacts,
31    },
32    warm_parse::{WarmParse, WarmParseKey, WarmParseStore},
33};
34
35/// Reusable engine session for one resolved project.
36///
37/// The session owns the resolved config and discovered file set so future
38/// consumers can share graph-sensitive inputs without each surface recreating
39/// its own partial orchestration.
40#[derive(Debug)]
41pub struct AnalysisSession {
42    config: ResolvedConfig,
43    config_path: Option<PathBuf>,
44    config_inputs: fallow_config::ConfigInputs,
45    config_inputs_before_resolve: fallow_config::ConfigInputsSnapshot,
46    discovery: crate::discover::AnalysisDiscovery,
47    workspaces: Vec<WorkspaceInfo>,
48    workspace_diagnostics: Vec<WorkspaceDiagnostic>,
49    parsed_cache: Mutex<Option<ParsedModuleCache>>,
50    styling_cache: Mutex<Option<Arc<crate::health::StylingAnalysisArtifacts>>>,
51    cancellation: Option<Arc<AtomicBool>>,
52    warm_parse: Option<Arc<WarmParseStore>>,
53    /// Config load already resolved the workspaces, so a discovery refresh
54    /// keeps them.
55    preloaded_workspaces: bool,
56    parse_counts: ParseCountCells,
57    /// An incremental parse changed modules that the persisted parse cache
58    /// does not hold yet. [`AnalysisSession::flush_parse_cache`] writes them.
59    disk_cache_stale: AtomicBool,
60}
61
62#[derive(Debug)]
63struct ParsedModuleCache {
64    need_complexity: bool,
65    fingerprints: Vec<SourceFingerprint>,
66    /// The wall-clock time before the session read `fingerprints`. A
67    /// fingerprint that is not settled before this time can belong to a
68    /// later write in the same timestamp tick, so it does not prove that the
69    /// module is current.
70    read_started_ns: u64,
71    modules: Arc<[ModuleInfo]>,
72    /// The read failures of the parse, kept so that an incremental parse can
73    /// record the full set of the project again.
74    read_failures: Vec<SourceReadFailure>,
75    /// The parse degradations of the parse, kept for the same reason.
76    parse_degradations: Vec<SourceParseDegradation>,
77}
78
79/// Owned session parts for runners that need to continue an existing pipeline.
80#[derive(Debug)]
81pub struct AnalysisSessionParts {
82    /// Resolved project config the session was created with.
83    pub config: ResolvedConfig,
84    /// Path of the loaded config file; `None` when defaults were used.
85    pub config_path: Option<PathBuf>,
86    /// Files discovered under the session root.
87    pub files: Vec<DiscoveredFile>,
88    /// Workspace metadata discovered during config resolution.
89    pub workspaces: Vec<WorkspaceInfo>,
90    /// Diagnostics from workspace discovery (undeclared or invalid members).
91    pub workspace_diagnostics: Vec<WorkspaceDiagnostic>,
92}
93
94/// Owned session parts after parsing the discovered files.
95#[derive(Debug)]
96pub struct ParsedAnalysisSessionParts {
97    /// Resolved project config the session was created with.
98    pub config: ResolvedConfig,
99    /// Path of the loaded config file; `None` when defaults were used.
100    pub config_path: Option<PathBuf>,
101    /// Files discovered under the session root.
102    pub files: Vec<DiscoveredFile>,
103    /// Parsed modules, one per discovered file.
104    pub modules: Vec<ModuleInfo>,
105    /// Workspace metadata discovered during config resolution.
106    pub workspaces: Vec<WorkspaceInfo>,
107    /// Diagnostics from workspace discovery (undeclared or invalid members).
108    pub workspace_diagnostics: Vec<WorkspaceDiagnostic>,
109    /// Parse wall time in milliseconds.
110    pub parse_ms: f64,
111    /// Parse-cache write-back wall time in milliseconds.
112    pub cache_update_ms: f64,
113    /// Files served from the parse cache.
114    pub cache_hits: usize,
115    /// Files that had to be parsed fresh.
116    pub cache_misses: usize,
117    /// Summed parse CPU time across rayon workers in milliseconds.
118    pub parse_cpu_ms: f64,
119}
120
121#[derive(Debug)]
122pub(crate) struct SharedParsedAnalysisSessionParts {
123    pub(crate) config: ResolvedConfig,
124    pub(crate) files: Vec<DiscoveredFile>,
125    pub(crate) modules: Arc<[ModuleInfo]>,
126    pub workspaces: Vec<WorkspaceInfo>,
127    pub workspace_diagnostics: Vec<WorkspaceDiagnostic>,
128    pub parse_ms: f64,
129    pub parse_cpu_ms: f64,
130}
131
132/// Reusable artifacts produced by one session-owned dead-code run.
133#[derive(Debug)]
134pub struct AnalysisSessionArtifacts {
135    /// Retained dead-code analysis output (results, graph, timings).
136    pub analysis: DeadCodeAnalysisArtifacts,
137    /// Diff scope the run was limited to, when one was resolved.
138    pub changed_files: Option<FxHashSet<PathBuf>>,
139    /// Per-file source fingerprints for downstream cache invalidation.
140    pub source_fingerprints: FxHashMap<PathBuf, SourceFingerprint>,
141}
142
143impl AnalysisSession {
144    /// Load config and discover files for a project root.
145    ///
146    /// # Errors
147    ///
148    /// Returns an error when config loading fails.
149    pub fn load(root: &Path, config_path: Option<&Path>) -> EngineResult<Self> {
150        let project_config = config_for_project(root, config_path)?;
151        Ok(Self::from_config(project_config))
152    }
153
154    /// Load config, apply one caller-supplied config adjustment, then discover
155    /// files for a project root.
156    ///
157    /// # Errors
158    ///
159    /// Returns an error when config loading fails.
160    pub fn load_with_config(
161        root: &Path,
162        config_path: Option<&Path>,
163        configure: impl FnOnce(&mut ResolvedConfig),
164    ) -> EngineResult<Self> {
165        Self::load_with_config_options(
166            root,
167            config_path,
168            fallow_config::ConfigLoadOptions::default(),
169            configure,
170        )
171    }
172
173    /// Load config with an explicit inheritance trust policy, apply one
174    /// caller-supplied adjustment, then discover project files.
175    ///
176    /// # Errors
177    ///
178    /// Returns an error when config loading fails.
179    pub fn load_with_config_options(
180        root: &Path,
181        config_path: Option<&Path>,
182        load_options: fallow_config::ConfigLoadOptions,
183        configure: impl FnOnce(&mut ResolvedConfig),
184    ) -> EngineResult<Self> {
185        let mut project_config = crate::project_config::config_for_project_with_load_options(
186            root,
187            config_path,
188            load_options,
189        )?;
190        configure(&mut project_config.config);
191        project_config.workspaces.clear();
192        project_config.workspace_diagnostics.clear();
193        project_config.workspace_discovery_ms = None;
194        Ok(Self::from_config(project_config))
195    }
196
197    /// Build a session from built-in defaults, ignoring project config files.
198    ///
199    /// This is intended for editor fallback paths that have already reported a
200    /// config-load warning but should still surface best-effort diagnostics.
201    #[must_use]
202    pub fn load_default(root: &Path) -> Self {
203        Self::load_default_with_config(root, |_| {})
204    }
205
206    /// Build a session from built-in defaults, apply one caller-supplied
207    /// adjustment, then discover project files.
208    #[must_use]
209    pub fn load_default_with_config(
210        root: &Path,
211        configure: impl FnOnce(&mut ResolvedConfig),
212    ) -> Self {
213        let mut project_config = default_project_config(root);
214        configure(&mut project_config.config);
215        Self::from_config(project_config)
216    }
217
218    /// Build a session from a previously resolved config.
219    #[must_use]
220    pub fn from_config(project_config: ProjectConfig) -> Self {
221        let uses_preloaded_workspaces = project_config.workspace_discovery_ms.is_some();
222        let discovery = if let Some(workspace_discovery_ms) = project_config.workspace_discovery_ms
223        {
224            crate::discover::prepare_analysis_discovery_with_workspaces(
225                &project_config.config,
226                &project_config.workspaces,
227                workspace_discovery_ms,
228            )
229        } else {
230            crate::discover::prepare_analysis_discovery(&project_config.config)
231        };
232        let workspaces = if uses_preloaded_workspaces {
233            project_config.workspaces
234        } else {
235            discovery.workspaces().to_vec()
236        };
237        // Analysis-stage diagnostics are owned by the analyze pass, which
238        // refreshes the registry on every run; pinning them in the session
239        // snapshot would keep a stale entry alive after the cause is fixed
240        // (issue #2366). `current_workspace_diagnostics` reads them live.
241        //
242        // Source-discovery entries come from THIS walk's return value, not from
243        // the registry: combined mode runs the dead-code and duplication walks
244        // concurrently whenever a per-analysis `production` split stops them
245        // from sharing a file list, and each walk replaces the registry's
246        // source-discovery set for the root, so a registry read here would
247        // report whichever walk happened to write last (issue #2366).
248        let workspace_diagnostics = merge_workspace_diagnostics(
249            merge_workspace_diagnostics(
250                project_config.workspace_diagnostics,
251                fallow_config::workspace_diagnostics_for(&project_config.config.root)
252                    .into_iter()
253                    .filter(|diagnostic| {
254                        !diagnostic.kind.is_analysis_stage()
255                            && !diagnostic.kind.is_source_discovery()
256                    })
257                    .collect(),
258            ),
259            discovery.source_diagnostics().to_vec(),
260        );
261        Self {
262            config: project_config.config,
263            config_path: project_config.path,
264            config_inputs: project_config.inputs,
265            config_inputs_before_resolve: project_config.inputs_before_resolve,
266            discovery,
267            workspaces,
268            workspace_diagnostics,
269            parsed_cache: Mutex::new(None),
270            styling_cache: Mutex::new(None),
271            cancellation: None,
272            warm_parse: crate::warm_parse::installed(),
273            preloaded_workspaces: uses_preloaded_workspaces,
274            parse_counts: ParseCountCells::default(),
275            disk_cache_stale: AtomicBool::new(false),
276        }
277    }
278
279    /// Attach a caller-owned cancellation token to this session.
280    ///
281    /// Analyses that run through the session check the token at each pipeline
282    /// stage boundary and inside the per-file parse loop, and return
283    /// [`crate::EngineError::cancelled`] instead of a partial result once it is
284    /// set. A session without a token can never be cancelled, so existing
285    /// callers keep their current behavior.
286    ///
287    /// The stop is cooperative, and how long it takes is bounded by the
288    /// longest stage that holds no check, not by any promised latency. Only
289    /// the parse loop stops per file. Duplication detection (tokenization plus
290    /// suffix-array matching) and the dead-code detectors run to the end of the
291    /// stage once entered, and on a large repository either is seconds of work.
292    /// A caller that needs a bounded stop has to kill the process instead.
293    #[must_use]
294    pub fn with_cancellation(mut self, cancellation: Arc<AtomicBool>) -> Self {
295        self.cancellation = Some(cancellation);
296        self
297    }
298
299    /// Use `store` for parsed modules across sessions, in place of the store
300    /// that the process installed with [`crate::warm_parse::install`].
301    /// `None` makes the session parse through the persisted cache only.
302    #[must_use]
303    pub fn with_warm_parse(mut self, store: Option<Arc<WarmParseStore>>) -> Self {
304        self.warm_parse = store;
305        self
306    }
307
308    /// Replace the cancellation token of a session that serves several runs.
309    ///
310    /// A long-lived session gets the token of each new run, so a set token of
311    /// an earlier run does not stop the next one.
312    pub fn set_cancellation(&mut self, cancellation: Arc<AtomicBool>) {
313        self.cancellation = Some(cancellation);
314    }
315
316    /// The parse work of this session since it was created.
317    #[must_use]
318    pub fn parse_counts(&self) -> SessionParseCounts {
319        self.parse_counts.snapshot()
320    }
321
322    /// Walk the project again and keep the parsed modules when the file set
323    /// did not change.
324    ///
325    /// A session that serves several runs calls this before each run, so a
326    /// created or deleted file reaches the analysis without a config reload.
327    /// When the file set changed, the file ids move, so the session writes its
328    /// parsed modules to the persisted parse cache and drops them. The next
329    /// parse then reads that cache. Returns whether the file set changed.
330    ///
331    /// The session also drops its modules when a fingerprint of the cached
332    /// parse cannot stand in for the file content, as on a platform without
333    /// ctime. A same-size edit with a restored mtime keeps such a
334    /// fingerprint, so only the persisted cache, which then compares content
335    /// hashes, can tell whether the module is current.
336    pub fn refresh_discovery(&mut self) -> bool {
337        let discovery = if self.preloaded_workspaces {
338            crate::discover::prepare_analysis_discovery_with_workspaces(
339                &self.config,
340                &self.workspaces,
341                0.0,
342            )
343        } else {
344            crate::discover::prepare_analysis_discovery(&self.config)
345        };
346        let same_files = discovery.files().len() == self.files().len()
347            && discovery
348                .files()
349                .iter()
350                .zip(self.files())
351                .all(|(fresh, known)| fresh.path == known.path);
352        if !same_files || !self.cached_fingerprints_are_trustworthy() {
353            self.flush_parse_cache();
354            *self
355                .parsed_cache
356                .get_mut()
357                .unwrap_or_else(std::sync::PoisonError::into_inner) = None;
358            self.clear_styling_cache();
359        }
360        if !self.preloaded_workspaces {
361            self.workspaces = discovery.workspaces().to_vec();
362        }
363        self.discovery = discovery;
364        !same_files
365    }
366
367    /// Whether each fingerprint of the cached parse can stand in for the file
368    /// content. No cached parse counts as trustworthy.
369    fn cached_fingerprints_are_trustworthy(&mut self) -> bool {
370        self.parsed_cache
371            .get_mut()
372            .unwrap_or_else(std::sync::PoisonError::into_inner)
373            .as_ref()
374            .is_none_or(|cache| {
375                cache
376                    .fingerprints
377                    .iter()
378                    .all(|fingerprint| fingerprint.is_trustworthy_without_content())
379            })
380    }
381
382    /// Write the modules of incremental parses to the persisted parse cache.
383    ///
384    /// Each module is stored with the fingerprint that the session read before
385    /// it parsed the file. A file that changed after that parse then misses
386    /// the cache instead of serving the older module. Does nothing when no
387    /// incremental parse happened since the last write, or when the cache is
388    /// off.
389    ///
390    /// The session does not write these modules when it is dropped. A caller
391    /// that parses again after files changed, such as a session that lives
392    /// across editor runs, calls this method before it drops the session. A
393    /// session that parses once needs no call: the full parse writes the
394    /// persisted cache itself. The editor server is the only caller now.
395    pub fn flush_parse_cache(&self) {
396        if self.config.no_cache || !self.disk_cache_stale.swap(false, Ordering::SeqCst) {
397            return;
398        }
399        let Ok(cache) = self.parsed_cache.lock() else {
400            return;
401        };
402        let Some(cache) = cache.as_ref() else {
403            return;
404        };
405        let cache_max_size_bytes =
406            crate::project_config::resolve_cache_max_size_bytes(&self.config);
407        let mut store = fallow_extract::cache::CacheStore::load(
408            &self.config.cache_dir,
409            &self.config.root,
410            self.config.cache_config_hash,
411            cache_max_size_bytes,
412        )
413        .ok();
414        let files = self.files();
415        write_parse_cache(
416            &self.config,
417            &mut store,
418            &ParseCacheWrite {
419                modules: &cache.modules,
420                files,
421                need_complexity: cache.need_complexity,
422                fingerprint_of: &|file: &DiscoveredFile| {
423                    cache.fingerprints.get(file.id.0 as usize).map_or_else(
424                        || SourceFingerprint::new(0, 0),
425                        |fingerprint| fingerprint.for_content_read_at(cache.read_started_ns),
426                    )
427                },
428            },
429        );
430    }
431
432    /// Parse the discovered files into the module cache of the session,
433    /// without analysis. A later run of this session then starts from warm
434    /// modules.
435    ///
436    /// # Errors
437    ///
438    /// Returns [`crate::EngineError::cancelled`] when the token of the session
439    /// is set.
440    pub fn prewarm_parsed_modules(&self, need_complexity: bool) -> EngineResult<()> {
441        self.shared_parsed_modules_cancellable(need_complexity, "prewarm")
442            .map(drop)
443    }
444
445    fn clear_styling_cache(&self) {
446        if let Ok(mut cache) = self.styling_cache.lock() {
447            *cache = None;
448        }
449    }
450
451    /// Whether this session's caller has requested cancellation.
452    #[must_use]
453    pub fn is_cancelled(&self) -> bool {
454        self.cancellation
455            .as_ref()
456            .is_some_and(|cancelled| cancelled.load(Ordering::SeqCst))
457    }
458
459    fn ensure_not_cancelled(&self, stage: &str) -> EngineResult<()> {
460        if self.is_cancelled() {
461            return Err(crate::EngineError::cancelled(stage));
462        }
463        Ok(())
464    }
465
466    /// Build a session from a resolved config when the caller already owns
467    /// command-specific config loading.
468    ///
469    /// # Errors
470    ///
471    /// Returns an engine error when root manifest loading fails during
472    /// workspace discovery, matching `ProjectConfig::load`.
473    pub fn from_resolved_config(config: ResolvedConfig) -> EngineResult<Self> {
474        let (workspaces, workspace_diagnostics, workspace_discovery_ms) =
475            crate::project_config::collect_workspace_metadata(&config)?;
476        Ok(Self::from_config(ProjectConfig {
477            inputs: fallow_config::ConfigInputs::default(),
478            inputs_before_resolve: fallow_config::ConfigInputsSnapshot::default(),
479            config,
480            path: None,
481            workspaces,
482            workspace_diagnostics,
483            workspace_discovery_ms: Some(workspace_discovery_ms),
484        }))
485    }
486
487    /// Resolved project root.
488    #[must_use]
489    pub fn root(&self) -> &Path {
490        &self.config.root
491    }
492
493    /// Resolved project config.
494    #[must_use]
495    pub fn config(&self) -> &ResolvedConfig {
496        &self.config
497    }
498
499    /// Config file path when one was loaded.
500    #[must_use]
501    pub fn config_path(&self) -> Option<&Path> {
502        self.config_path.as_deref()
503    }
504
505    /// The plugin files, rule packs and `autoDiscover` directories that
506    /// config resolution read. A session built from a resolved config
507    /// lists none.
508    #[must_use]
509    pub const fn config_inputs(&self) -> &fallow_config::ConfigInputs {
510        &self.config_inputs
511    }
512
513    /// The content of [`Self::config_inputs`] just before config resolution
514    /// read them. A snapshot after the load that differs from this one tells
515    /// that an input changed during the load.
516    #[must_use]
517    pub const fn config_inputs_before_resolve(&self) -> &fallow_config::ConfigInputsSnapshot {
518        &self.config_inputs_before_resolve
519    }
520
521    /// The estimated heap memory of the parsed modules that the session
522    /// keeps between calls, with the estimate of
523    /// [`crate::warm_parse::estimated_retained_bytes`]. Zero before the first
524    /// parse.
525    #[must_use]
526    pub fn retained_bytes_estimate(&self) -> u64 {
527        self.parsed_cache
528            .lock()
529            .ok()
530            .and_then(|cache| {
531                cache
532                    .as_ref()
533                    .map(|cache| crate::warm_parse::estimated_retained_bytes(&cache.fingerprints))
534            })
535            .unwrap_or(0)
536    }
537
538    /// Discovered files for this session.
539    #[must_use]
540    pub fn files(&self) -> &[DiscoveredFile] {
541        self.discovery.files()
542    }
543
544    /// Workspace packages discovered during config/session setup.
545    #[must_use]
546    pub fn workspaces(&self) -> &[WorkspaceInfo] {
547        &self.workspaces
548    }
549
550    /// Source metadata fingerprints for every discovered source file.
551    #[must_use]
552    fn source_fingerprints(&self) -> FxHashMap<PathBuf, SourceFingerprint> {
553        self.discovery
554            .files()
555            .iter()
556            .map(|file| {
557                let fingerprint = std::fs::metadata(&file.path).map_or_else(
558                    |_| SourceFingerprint::new(0, file.size_bytes),
559                    |metadata| SourceFingerprint::from_metadata(&metadata),
560                );
561                (file.path.clone(), fingerprint)
562            })
563            .collect()
564    }
565
566    /// Resolve files changed since a git ref against this session root.
567    ///
568    /// # Errors
569    ///
570    /// Returns an error when the ref is invalid, git is unavailable, or the
571    /// root is not part of a repository.
572    pub(crate) fn changed_files_since(
573        &self,
574        git_ref: &str,
575    ) -> Result<FxHashSet<PathBuf>, crate::changed_files::ChangedFilesError> {
576        crate::changed_files::changed_files(&self.config.root, git_ref)
577    }
578
579    /// The discovery this session walked.
580    pub(crate) const fn discovery(&self) -> &crate::discover::AnalysisDiscovery {
581        &self.discovery
582    }
583
584    /// Workspace and source-discovery diagnostics captured for this session.
585    #[must_use]
586    pub fn workspace_diagnostics(&self) -> &[WorkspaceDiagnostic] {
587        &self.workspace_diagnostics
588    }
589
590    /// Current diagnostics, including the source read failures the parse stage
591    /// discovers and the analysis-stage entries the analyze pass records, both
592    /// of which land in the registry after the session was created.
593    ///
594    /// The live read goes through
595    /// [`fallow_config::registry_diagnostics_to_fold`], which drops
596    /// walk-recorded entries for the same reason the constructor does: a
597    /// concurrent walk on the same root replaces that set, so importing it here
598    /// would make this session's list depend on which walk wrote last, and the
599    /// combined root's union would come out in a different ORDER between runs
600    /// of the same command (issue #2366). This session's own walk-recorded
601    /// entries are already in the snapshot, by value, from its own walk.
602    #[must_use]
603    pub fn current_workspace_diagnostics(&self) -> Vec<WorkspaceDiagnostic> {
604        merge_workspace_diagnostics(
605            self.workspace_diagnostics.clone(),
606            fallow_config::registry_diagnostics_to_fold(&self.config.root),
607        )
608    }
609
610    pub(crate) fn styling_analysis_artifacts(
611        &self,
612    ) -> Arc<crate::health::StylingAnalysisArtifacts> {
613        if let Ok(cache) = self.styling_cache.lock()
614            && let Some(artifacts) = cache.as_ref()
615        {
616            return Arc::clone(artifacts);
617        }
618
619        let modules = self.shared_parsed_modules(false);
620        let artifacts = Arc::new(crate::health::build_styling_analysis_artifacts(
621            self.files(),
622            &modules,
623            self.config(),
624        ));
625        if let Ok(mut cache) = self.styling_cache.lock() {
626            *cache = Some(Arc::clone(&artifacts));
627        }
628        artifacts
629    }
630
631    /// Consume the session and return the resolved config plus discovery data.
632    #[must_use]
633    pub fn into_parts(self) -> AnalysisSessionParts {
634        let workspace_diagnostics = self.current_workspace_diagnostics();
635        AnalysisSessionParts {
636            config: self.config,
637            config_path: self.config_path,
638            files: self.discovery.into_files(),
639            workspaces: self.workspaces,
640            workspace_diagnostics,
641        }
642    }
643
644    /// Consume the session, load the parser cache, and parse discovered files.
645    #[must_use]
646    pub fn into_parsed_parts(self, need_complexity: bool) -> ParsedAnalysisSessionParts {
647        let AnalysisSessionParts {
648            config,
649            config_path,
650            files,
651            workspaces,
652            workspace_diagnostics,
653        } = self.into_parts();
654        let ParsedModules {
655            modules,
656            metrics,
657            source_diagnostics,
658            ..
659        } = parse_files_with_config(&config, &files, need_complexity, None);
660        ParsedAnalysisSessionParts {
661            config,
662            config_path,
663            files,
664            modules,
665            workspaces,
666            workspace_diagnostics: merge_workspace_diagnostics(
667                workspace_diagnostics,
668                source_diagnostics,
669            ),
670            parse_ms: metrics.parse_ms,
671            cache_update_ms: metrics.cache_ms,
672            cache_hits: metrics.cache_hits,
673            cache_misses: metrics.cache_misses,
674            parse_cpu_ms: metrics.parse_cpu_ms,
675        }
676    }
677
678    /// Parse discovered files without consuming the session.
679    #[must_use]
680    pub fn parsed_parts(&self, need_complexity: bool) -> ParsedAnalysisSessionParts {
681        let SharedParsedModules { modules, metrics } = self.parse_modules(need_complexity, None);
682        self.parsed_parts_from_modules(modules.to_vec(), metrics)
683    }
684
685    /// Parse discovered files while retaining shared immutable module storage.
686    #[must_use]
687    pub(crate) fn shared_parsed_parts(
688        &self,
689        need_complexity: bool,
690    ) -> SharedParsedAnalysisSessionParts {
691        let SharedParsedModules { modules, metrics } = self.parse_modules(need_complexity, None);
692        SharedParsedAnalysisSessionParts {
693            config: self.config.clone(),
694            files: self.discovery.files().to_vec(),
695            modules,
696            workspaces: self.workspaces.clone(),
697            workspace_diagnostics: self.current_workspace_diagnostics(),
698            parse_ms: metrics.parse_ms,
699            parse_cpu_ms: metrics.parse_cpu_ms,
700        }
701    }
702
703    /// Return immutable parsed modules backed by the reusable session cache.
704    ///
705    /// Workspace-owned consumers use this additive path when they only need
706    /// parsed modules and can borrow discovery and config directly from the
707    /// session. Stable owned callers can continue using [`Self::parsed_parts`].
708    #[doc(hidden)]
709    #[must_use]
710    pub(crate) fn shared_parsed_modules(&self, need_complexity: bool) -> Arc<[ModuleInfo]> {
711        self.parse_modules(need_complexity, None).modules
712    }
713
714    /// Parse the discovered files, stopping if the session's caller cancelled.
715    ///
716    /// The token is checked on both sides of the parse loop, so the truncated
717    /// module set a cancelled parse produces is never returned as a smaller
718    /// project. `stage` names the work that would have followed.
719    ///
720    /// # Errors
721    ///
722    /// Returns [`crate::EngineError::cancelled`] when the token is set. A
723    /// session without a token can never return it.
724    pub(crate) fn shared_parsed_modules_cancellable(
725        &self,
726        need_complexity: bool,
727        stage: &str,
728    ) -> EngineResult<Arc<[ModuleInfo]>> {
729        self.ensure_not_cancelled("parsing")?;
730        let modules = self
731            .parse_modules(need_complexity, self.cancellation.as_deref())
732            .modules;
733        self.ensure_not_cancelled(stage)?;
734        Ok(modules)
735    }
736
737    /// Parse discovered files without consuming the session or retaining parser
738    /// output in the session cache.
739    #[must_use]
740    pub fn parsed_parts_uncached(&self, need_complexity: bool) -> ParsedAnalysisSessionParts {
741        let read_started_ns = now_ns();
742        let fingerprints = self
743            .warm_parse
744            .as_ref()
745            .and_then(|_| source_fingerprints_for_files(self.files()));
746        if let Some(warm) = self.warm_parse(
747            need_complexity,
748            fingerprints.as_deref(),
749            read_started_ns,
750            None,
751        ) {
752            return self.parsed_parts_from_modules(warm.modules.to_vec(), warm.metrics);
753        }
754        let ParsedModules {
755            modules, metrics, ..
756        } = parse_files_with_config(&self.config, self.files(), need_complexity, None);
757        self.parsed_parts_from_modules(modules, metrics)
758    }
759
760    fn parsed_parts_from_modules(
761        &self,
762        modules: Vec<ModuleInfo>,
763        metrics: core_backend::ParseMetrics,
764    ) -> ParsedAnalysisSessionParts {
765        ParsedAnalysisSessionParts {
766            config: self.config.clone(),
767            config_path: self.config_path.clone(),
768            files: self.discovery.files().to_vec(),
769            modules,
770            workspaces: self.workspaces.clone(),
771            workspace_diagnostics: self.current_workspace_diagnostics(),
772            parse_ms: metrics.parse_ms,
773            cache_update_ms: metrics.cache_ms,
774            cache_hits: metrics.cache_hits,
775            cache_misses: metrics.cache_misses,
776            parse_cpu_ms: metrics.parse_cpu_ms,
777        }
778    }
779
780    /// Run dead-code analysis for this session.
781    ///
782    /// # Errors
783    ///
784    /// Returns an error if parsing or analysis fails.
785    pub fn analyze_dead_code(&self) -> EngineResult<DeadCodeAnalysis> {
786        self.analyze_dead_code_with_artifacts(false, false)
787            .map(|output| DeadCodeAnalysis {
788                results: output.results,
789            })
790    }
791
792    /// Run dead-code analysis with retained complexity artifacts.
793    ///
794    /// # Errors
795    ///
796    /// Returns an error if parsing or analysis fails.
797    pub fn analyze_dead_code_with_complexity(&self) -> EngineResult<DeadCodeAnalysisOutput> {
798        self.analyze_dead_code_with_artifacts(true, false)
799            .map(|output| DeadCodeAnalysisOutput {
800                results: output.results,
801                modules: output.modules,
802                files: output.files,
803            })
804    }
805
806    /// Run dead-code analysis with retained modules, discovered files and graph.
807    ///
808    /// # Errors
809    ///
810    /// Returns an error if parsing or analysis fails.
811    pub fn analyze_dead_code_with_artifacts(
812        &self,
813        need_complexity: bool,
814        retain_graph: bool,
815    ) -> EngineResult<DeadCodeAnalysisArtifacts> {
816        self.analyze_dead_code_with_shared_artifacts(need_complexity, retain_graph)
817            .map(SharedDeadCodeAnalysisArtifacts::into_owned)
818    }
819
820    /// Run dead-code analysis with shared immutable parser artifacts.
821    ///
822    /// Workspace-owned consumers use this additive path to retain warm parser
823    /// modules without deep-cloning the session cache. External callers can
824    /// continue using [`Self::analyze_dead_code_with_artifacts`].
825    ///
826    /// # Errors
827    ///
828    /// Returns an error if parsing or analysis fails.
829    #[doc(hidden)]
830    pub fn analyze_dead_code_with_shared_artifacts(
831        &self,
832        need_complexity: bool,
833        retain_graph: bool,
834    ) -> EngineResult<SharedDeadCodeAnalysisArtifacts> {
835        self.analyze_dead_code_with_reuse_artifacts(need_complexity, retain_graph, need_complexity)
836    }
837
838    /// Run dead-code analysis while retaining discovered files for downstream
839    /// command stages that reuse discovery but do not need parser modules.
840    ///
841    /// # Errors
842    ///
843    /// Returns an error if parsing or analysis fails.
844    pub fn analyze_dead_code_retaining_files(
845        &self,
846        need_complexity: bool,
847        retain_graph: bool,
848    ) -> EngineResult<DeadCodeAnalysisArtifacts> {
849        self.analyze_dead_code_with_reuse_artifacts(need_complexity, retain_graph, true)
850            .map(SharedDeadCodeAnalysisArtifacts::into_owned)
851    }
852
853    /// Run dead-code analysis from modules already parsed through this session.
854    ///
855    /// This preserves the session's resolved config and discovered file set for
856    /// follow-up analyses that reuse parser output without redoing discovery.
857    ///
858    /// # Errors
859    ///
860    /// Returns an error if graph construction or analysis fails.
861    pub fn analyze_dead_code_with_parsed_modules(
862        &self,
863        modules: &[ModuleInfo],
864    ) -> EngineResult<DeadCodeAnalysisArtifacts> {
865        self.analyze_dead_code_with_shared_modules(Arc::from(modules))
866    }
867
868    /// Run dead-code analysis from shared immutable parser modules.
869    ///
870    /// # Errors
871    ///
872    /// Returns an error if graph construction or analysis fails.
873    #[doc(hidden)]
874    pub(crate) fn analyze_dead_code_with_shared_modules(
875        &self,
876        modules: Arc<[ModuleInfo]>,
877    ) -> EngineResult<DeadCodeAnalysisArtifacts> {
878        run_engine_owned_dead_code_pipeline(EngineDeadCodePipelineInput {
879            config: &self.config,
880            discovery: &self.discovery,
881            modules,
882            metrics: reused_parse_metrics(),
883            collect_usages: true,
884            retain_graph: true,
885            retain_modules: false,
886            retain_files: false,
887            cancellation: self.cancellation.as_deref(),
888        })
889        .map(SharedDeadCodeAnalysisArtifacts::into_owned)
890    }
891
892    fn analyze_dead_code_with_reuse_artifacts(
893        &self,
894        need_complexity: bool,
895        retain_graph: bool,
896        retain_files: bool,
897    ) -> EngineResult<SharedDeadCodeAnalysisArtifacts> {
898        self.ensure_not_cancelled("parsing")?;
899        let SharedParsedModules { modules, metrics } =
900            self.parse_modules(need_complexity, self.cancellation.as_deref());
901        // The parse loop no-ops the files it has not reached yet, so a token
902        // set during parsing leaves `modules` truncated. It must never reach
903        // the graph as if it were the whole project.
904        self.ensure_not_cancelled("the dead-code pipeline")?;
905        run_engine_owned_dead_code_pipeline(EngineDeadCodePipelineInput {
906            config: &self.config,
907            discovery: &self.discovery,
908            modules,
909            metrics,
910            collect_usages: true,
911            retain_graph,
912            retain_modules: need_complexity,
913            retain_files,
914            cancellation: self.cancellation.as_deref(),
915        })
916    }
917
918    /// Run dead-code analysis and return the session-scoped reuse artifacts.
919    ///
920    /// Callers pass a changed-file set they have already resolved for the
921    /// command. The returned value keeps that set beside parser, graph, and
922    /// source-fingerprint data so downstream runners do not have to rebuild or
923    /// rediscover the same inputs.
924    ///
925    /// # Errors
926    ///
927    /// Returns an error if parsing or analysis fails.
928    pub fn analyze_dead_code_with_session_artifacts(
929        &self,
930        need_complexity: bool,
931        retain_graph: bool,
932        changed_files: Option<FxHashSet<PathBuf>>,
933    ) -> EngineResult<AnalysisSessionArtifacts> {
934        Ok(AnalysisSessionArtifacts {
935            analysis: self.analyze_dead_code_with_artifacts(need_complexity, retain_graph)?,
936            changed_files,
937            source_fingerprints: self.source_fingerprints(),
938        })
939    }
940
941    /// Run dead-code and duplication analysis for this session.
942    ///
943    /// When `retain_complexity_artifacts` is true, the dead-code result keeps
944    /// parser artifacts needed by editor overlays such as inline complexity.
945    ///
946    /// # Errors
947    ///
948    /// Returns an error if dead-code parsing or analysis fails.
949    pub fn analyze_project_with(
950        &self,
951        duplicates_config: &DuplicatesConfig,
952        retain_complexity_artifacts: bool,
953    ) -> EngineResult<ProjectAnalysisOutput> {
954        self.analyze_project_with_artifacts(
955            duplicates_config,
956            ProjectAnalysisArtifactOptions {
957                retain_complexity_artifacts,
958                ..ProjectAnalysisArtifactOptions::default()
959            },
960        )
961        .map(ProjectAnalysisArtifacts::into_output)
962    }
963
964    /// Run dead-code and duplication analysis with retained session reuse data.
965    ///
966    /// This is the engine-owned project artifact boundary for callers that need
967    /// to hand one analysis result across audit, decision, editor, or follow-up
968    /// analysis surfaces without rediscovering session metadata.
969    ///
970    /// # Errors
971    ///
972    /// Returns an error if dead-code parsing or analysis fails.
973    pub fn analyze_project_with_artifacts(
974        &self,
975        duplicates_config: &DuplicatesConfig,
976        options: ProjectAnalysisArtifactOptions,
977    ) -> EngineResult<ProjectAnalysisArtifacts> {
978        self.ensure_not_cancelled("duplication detection")?;
979        let cache_dir = (!self.config.no_cache).then_some(self.config.cache_dir.as_path());
980        let duplication = if let Some(changed_files) = options.changed_files.as_ref() {
981            let changed_files = changed_files.iter().cloned().collect::<Vec<_>>();
982            self.find_duplicates_touching_files_with_defaults(
983                duplicates_config,
984                &changed_files,
985                cache_dir,
986            )
987            .report
988        } else {
989            self.find_duplicates_with_defaults(duplicates_config, cache_dir)
990                .report
991        };
992        // Duplication detection is infallible, so a token set while it ran can
993        // only be reported here, before its report is handed on as a complete
994        // one.
995        self.ensure_not_cancelled("the dead-code half of project analysis")?;
996        let source_fingerprints = options
997            .collect_source_fingerprints
998            .then(|| self.source_fingerprints());
999        Ok(ProjectAnalysisArtifacts {
1000            dead_code: self.analyze_dead_code_with_artifacts(
1001                options.retain_complexity_artifacts,
1002                options.retain_graph,
1003            )?,
1004            duplication,
1005            changed_files: options.changed_files,
1006            source_fingerprints,
1007        })
1008    }
1009
1010    /// Run duplication detection and return report sidecar metadata.
1011    #[must_use]
1012    pub fn find_duplicates_with_defaults(
1013        &self,
1014        config: &DuplicatesConfig,
1015        cache_dir: Option<&Path>,
1016    ) -> DuplicationAnalysis {
1017        duplicates::find_duplicates_with_defaults(
1018            &self.config.root,
1019            self.files(),
1020            config,
1021            cache_dir,
1022        )
1023    }
1024
1025    /// Run focused duplication detection for a changed-file set.
1026    #[must_use]
1027    pub fn find_duplicates_touching_files_with_defaults(
1028        &self,
1029        config: &DuplicatesConfig,
1030        changed_files: &[PathBuf],
1031        cache_dir: Option<&Path>,
1032    ) -> DuplicationAnalysis {
1033        duplicates::find_duplicates_touching_files_with_defaults(
1034            &self.config.root,
1035            self.files(),
1036            config,
1037            changed_files,
1038            cache_dir,
1039        )
1040    }
1041
1042    /// Parse the discovered files, reusing the session's warm module cache.
1043    ///
1044    /// `cancellation` is threaded through to the per-file parse loop, so a set
1045    /// token truncates the returned modules. Only callers that convert a set
1046    /// token into an error may pass one.
1047    fn parse_modules(
1048        &self,
1049        need_complexity: bool,
1050        cancellation: Option<&AtomicBool>,
1051    ) -> SharedParsedModules {
1052        let read_started_ns = now_ns();
1053        let fingerprints = source_fingerprints_for_files(self.files());
1054        if let Some(fingerprints) = fingerprints.as_ref()
1055            && let Some(parsed) = self.reparse_changed_modules(
1056                need_complexity,
1057                fingerprints,
1058                read_started_ns,
1059                cancellation,
1060            )
1061        {
1062            return parsed;
1063        }
1064
1065        let (modules, metrics, has_complexity, modules_reused, problems) = if let Some(warm) = self
1066            .warm_parse(
1067                need_complexity,
1068                fingerprints.as_deref(),
1069                read_started_ns,
1070                cancellation,
1071            ) {
1072            let reused = if warm.reused { warm.modules.len() } else { 0 };
1073            (warm.modules, warm.metrics, true, reused, warm.problems)
1074        } else {
1075            let ParsedModules {
1076                modules,
1077                metrics,
1078                read_failures,
1079                parse_degradations,
1080                ..
1081            } = parse_files_with_config(&self.config, self.files(), need_complexity, cancellation);
1082            let problems = SourceProblems {
1083                read_failures,
1084                parse_degradations,
1085            };
1086            (modules.into(), metrics, need_complexity, 0, problems)
1087        };
1088        self.parse_counts.record(SessionParseCounts {
1089            modules_parsed: metrics.cache_misses,
1090            disk_cache_hits: metrics.cache_hits,
1091            modules_reused,
1092        });
1093        // A cancelled parse returns a truncated module set. Storing it would
1094        // serve that truncation to the next call as a warm cache hit, long
1095        // after the cancellation itself is forgotten.
1096        if !token_is_set(cancellation)
1097            && let Some(fingerprints) = fingerprints
1098            && let Ok(mut cache) = self.parsed_cache.lock()
1099        {
1100            *cache = Some(ParsedModuleCache {
1101                need_complexity: has_complexity,
1102                fingerprints,
1103                read_started_ns,
1104                modules: Arc::clone(&modules),
1105                read_failures: problems.read_failures,
1106                parse_degradations: problems.parse_degradations,
1107            });
1108            // The full parse wrote the persisted cache for these modules.
1109            self.disk_cache_stale.store(false, Ordering::SeqCst);
1110            self.clear_styling_cache();
1111        }
1112        SharedParsedModules { modules, metrics }
1113    }
1114
1115    /// Parse through the store of parsed modules across sessions.
1116    ///
1117    /// Returns `None` when the session has no store, when caching is off, or
1118    /// when a fingerprint cannot stand in for the file content. The caller
1119    /// then parses through the persisted cache only.
1120    ///
1121    /// The parse always computes complexity. The cost is
1122    /// about the same, and a later `health` session can then use the modules.
1123    /// The persisted cache and the in-session cache already serve modules with
1124    /// complexity to callers that need none. One case costs more: a persisted
1125    /// cache without complexity, from a CLI `dead-code` run, gives no hits, so
1126    /// the first parse through the store parses each file from source.
1127    fn warm_parse(
1128        &self,
1129        need_complexity: bool,
1130        fingerprints: Option<&[SourceFingerprint]>,
1131        read_started_ns: u64,
1132        cancellation: Option<&AtomicBool>,
1133    ) -> Option<WarmParsedModules> {
1134        let store = self.warm_parse.as_deref()?;
1135        if self.config.no_cache {
1136            return None;
1137        }
1138        let key = WarmParseKey {
1139            root: &self.config.root,
1140            cache_config_hash: self.config.cache_config_hash,
1141            files: self.files(),
1142            fingerprints: fingerprints?,
1143            read_started_ns,
1144        };
1145        if !key.is_reusable() {
1146            return None;
1147        }
1148        if let Some(parse) = store.get(&key, need_complexity) {
1149            // A parse records its read failures and parse degradations for the
1150            // project, and the session reads them back as workspace
1151            // diagnostics. Record the kept ones again, so the diagnostics of
1152            // this run are the same as after a parse.
1153            record_source_diagnostics(
1154                &self.config.root,
1155                &parse.read_failures,
1156                &parse.parse_degradations,
1157            );
1158            return Some(WarmParsedModules {
1159                modules: parse.modules,
1160                metrics: reused_parse_metrics(),
1161                reused: true,
1162                problems: SourceProblems {
1163                    read_failures: parse.read_failures.to_vec(),
1164                    parse_degradations: parse.parse_degradations.to_vec(),
1165                },
1166            });
1167        }
1168
1169        let parsed = parse_files_with_config(&self.config, self.files(), true, cancellation);
1170        store.record_parse(parsed.metrics.cache_misses, parsed.metrics.cache_hits);
1171        let modules: Arc<[ModuleInfo]> = parsed.modules.into();
1172        if !token_is_set(cancellation) {
1173            store.put(
1174                &key,
1175                true,
1176                WarmParse {
1177                    modules: Arc::clone(&modules),
1178                    read_failures: parsed.read_failures.clone().into(),
1179                    parse_degradations: parsed.parse_degradations.clone().into(),
1180                },
1181            );
1182        }
1183        Some(WarmParsedModules {
1184            modules,
1185            metrics: parsed.metrics,
1186            reused: false,
1187            problems: SourceProblems {
1188                read_failures: parsed.read_failures,
1189                parse_degradations: parsed.parse_degradations,
1190            },
1191        })
1192    }
1193
1194    /// The file positions that changed since the kept parse, with the read
1195    /// start and the modules of that parse.
1196    ///
1197    /// The fingerprints are compared under the lock. The unsettled files are
1198    /// read after it, so a concurrent caller does not wait on disk work.
1199    /// Returns `None` when there is no kept parse, when it lacks the requested
1200    /// complexity, or when the file set differs.
1201    fn changed_since_kept_parse(
1202        &self,
1203        need_complexity: bool,
1204        fingerprints: &[SourceFingerprint],
1205    ) -> Option<(Vec<usize>, u64, Arc<[ModuleInfo]>)> {
1206        let unsettled = std::cell::RefCell::new(Vec::new());
1207        let guard = self.parsed_cache.lock().ok()?;
1208        let cache = guard.as_ref()?;
1209        if need_complexity && !cache.need_complexity {
1210            return None;
1211        }
1212        let mut changed = changed_file_indices(
1213            &cache.fingerprints,
1214            cache.read_started_ns,
1215            fingerprints,
1216            &|index| {
1217                unsettled.borrow_mut().push(index);
1218                true
1219            },
1220        )?;
1221        let kept_read_started_ns = cache.read_started_ns;
1222        let kept_modules = Arc::clone(&cache.modules);
1223        drop(guard);
1224        changed.extend(unsettled.into_inner().into_iter().filter(|&index| {
1225            !self.files().get(index).is_some_and(|file| {
1226                crate::session_reuse::module_matches_source(&kept_modules, file.id, &file.path)
1227            })
1228        }));
1229        changed.sort_unstable();
1230        Some((changed, kept_read_started_ns, kept_modules))
1231    }
1232
1233    /// Parse only the files whose fingerprint changed since the cached parse,
1234    /// and keep the other cached modules. When no file changed, return the
1235    /// cached modules as they are.
1236    ///
1237    /// Returns `None` when the cache cannot serve the request: no cached
1238    /// parse, a cache without the requested complexity, a different file set,
1239    /// or more changed files than [`MAX_INCREMENTAL_REPARSE_FILES`]. The
1240    /// caller then parses every file through the persisted cache.
1241    fn reparse_changed_modules(
1242        &self,
1243        need_complexity: bool,
1244        fingerprints: &[SourceFingerprint],
1245        read_started_ns: u64,
1246        cancellation: Option<&AtomicBool>,
1247    ) -> Option<SharedParsedModules> {
1248        let (changed, kept_read_started_ns, kept_modules) =
1249            self.changed_since_kept_parse(need_complexity, fingerprints)?;
1250        let mut guard = self.parsed_cache.lock().ok()?;
1251        let cache = guard.as_mut()?;
1252        // A concurrent cache change during the reads falls back to a full
1253        // parse. This is rare and accepted.
1254        if !Arc::ptr_eq(&cache.modules, &kept_modules)
1255            || cache.read_started_ns != kept_read_started_ns
1256        {
1257            return None;
1258        }
1259        // Release the extra reference, so the merge below can update the
1260        // modules in place.
1261        drop(kept_modules);
1262        if changed.is_empty() {
1263            // Each recent file had the kept content, and the content was read
1264            // after `read_started_ns`, so the fingerprints now prove it from
1265            // that time on.
1266            cache.read_started_ns = read_started_ns;
1267            let modules = Arc::clone(&cache.modules);
1268            drop(guard);
1269            self.parse_counts.record(SessionParseCounts {
1270                modules_reused: modules.len(),
1271                ..SessionParseCounts::default()
1272            });
1273            return Some(SharedParsedModules {
1274                modules,
1275                metrics: reused_parse_metrics(),
1276            });
1277        }
1278        if changed.len() > MAX_INCREMENTAL_REPARSE_FILES {
1279            return None;
1280        }
1281        let files: Vec<DiscoveredFile> = changed
1282            .iter()
1283            .filter_map(|&index| self.files().get(index).cloned())
1284            .collect();
1285        let parse_start = Instant::now();
1286        let parsed = crate::source::parse_all_files(
1287            &files,
1288            None,
1289            cache.need_complexity,
1290            cancellation,
1291            &self.config.flags.patterns(),
1292        );
1293        // The caller turns a set token into an error. The cache keeps the
1294        // earlier complete modules, because the parse above may be truncated.
1295        if token_is_set(cancellation) {
1296            return Some(SharedParsedModules {
1297                modules: Arc::clone(&cache.modules),
1298                metrics: reused_parse_metrics(),
1299            });
1300        }
1301        let mut fresh = parsed.modules;
1302        for module in &mut fresh {
1303            module.prepare_analysis_facts();
1304        }
1305        let fresh_count = fresh.len();
1306        let reparsed: Vec<_> = files.iter().map(|file| file.id).collect();
1307        merge_reparsed_modules(&mut cache.modules, &reparsed, fresh);
1308        cache.fingerprints = fingerprints.to_vec();
1309        cache.read_started_ns = read_started_ns;
1310        cache
1311            .read_failures
1312            .retain(|failure| !reparsed.contains(&failure.file_id));
1313        cache.read_failures.extend(parsed.read_failures);
1314        cache
1315            .parse_degradations
1316            .retain(|degradation| !reparsed.contains(&degradation.file_id));
1317        cache.parse_degradations.extend(parsed.parse_degradations);
1318        record_source_diagnostics(
1319            &self.config.root,
1320            &cache.read_failures,
1321            &cache.parse_degradations,
1322        );
1323        self.parse_counts.record(SessionParseCounts {
1324            modules_parsed: parsed.cache_misses,
1325            disk_cache_hits: 0,
1326            modules_reused: cache.modules.len().saturating_sub(fresh_count),
1327        });
1328        let modules = Arc::clone(&cache.modules);
1329        drop(guard);
1330        self.disk_cache_stale.store(true, Ordering::SeqCst);
1331        self.clear_styling_cache();
1332        Some(SharedParsedModules {
1333            modules,
1334            metrics: core_backend::ParseMetrics {
1335                parse_ms: parse_start.elapsed().as_secs_f64() * 1000.0,
1336                cache_ms: 0.0,
1337                cache_hits: 0,
1338                cache_misses: parsed.cache_misses,
1339                parse_cpu_ms: parsed.parse_cpu_ms,
1340                cache_rejection: None,
1341                files_read: parsed.files_read,
1342                source_bytes_read: parsed.source_bytes_read,
1343                parse_cache_bytes_read: 0,
1344                css_masked_bytes: parsed.css_masked_bytes,
1345                parse_cache_load_ms: 0.0,
1346            },
1347        })
1348    }
1349}
1350
1351struct ParsedModules {
1352    modules: Vec<ModuleInfo>,
1353    metrics: core_backend::ParseMetrics,
1354    source_diagnostics: Vec<WorkspaceDiagnostic>,
1355    read_failures: Vec<SourceReadFailure>,
1356    parse_degradations: Vec<SourceParseDegradation>,
1357}
1358
1359struct SharedParsedModules {
1360    modules: Arc<[ModuleInfo]>,
1361    metrics: core_backend::ParseMetrics,
1362}
1363
1364/// Modules from [`AnalysisSession::warm_parse`]. They always have complexity.
1365struct WarmParsedModules {
1366    modules: Arc<[ModuleInfo]>,
1367    metrics: core_backend::ParseMetrics,
1368    /// The modules came from the store without parse work.
1369    reused: bool,
1370    problems: SourceProblems,
1371}
1372
1373/// The files of a parse that did not read or that parsed with errors.
1374struct SourceProblems {
1375    read_failures: Vec<SourceReadFailure>,
1376    parse_degradations: Vec<SourceParseDegradation>,
1377}
1378
1379fn token_is_set(cancellation: Option<&AtomicBool>) -> bool {
1380    cancellation.is_some_and(|cancelled| cancelled.load(Ordering::SeqCst))
1381}
1382
1383fn parse_files_with_config(
1384    config: &ResolvedConfig,
1385    files: &[DiscoveredFile],
1386    need_complexity: bool,
1387    cancellation: Option<&AtomicBool>,
1388) -> ParsedModules {
1389    let parse_start = Instant::now();
1390    let cache_max_size_bytes = crate::project_config::resolve_cache_max_size_bytes(config);
1391    let mut cache_rejection = None;
1392    let mut parse_cache_bytes_read = 0;
1393    let mut parse_cache_load_ms = 0.0;
1394    let mut cache = if config.no_cache {
1395        None
1396    } else {
1397        let load_start = Instant::now();
1398        let (loaded, bytes_read) = fallow_extract::cache::CacheStore::load_counting_bytes(
1399            &config.cache_dir,
1400            &config.root,
1401            config.cache_config_hash,
1402            cache_max_size_bytes,
1403        );
1404        parse_cache_bytes_read = bytes_read;
1405        parse_cache_load_ms = load_start.elapsed().as_secs_f64() * 1000.0;
1406        match loaded {
1407            Ok(store) => Some(store),
1408            Err(rejection) => {
1409                cache_rejection = Some(rejection);
1410                None
1411            }
1412        }
1413    };
1414    let read_started_ns = now_ns();
1415    let parse_result = crate::source::parse_all_files(
1416        files,
1417        cache.as_ref(),
1418        need_complexity,
1419        cancellation,
1420        &config.flags.patterns(),
1421    );
1422    let source_diagnostics = record_source_diagnostics(
1423        &config.root,
1424        &parse_result.read_failures,
1425        &parse_result.parse_degradations,
1426    );
1427    let mut modules = parse_result.modules;
1428    for module in &mut modules {
1429        module.prepare_analysis_facts();
1430    }
1431    let parse_ms = parse_start.elapsed().as_secs_f64() * 1000.0;
1432    let cache_ms = if token_is_set(cancellation) {
1433        0.0
1434    } else {
1435        update_parse_cache_if_enabled(
1436            config,
1437            &mut cache,
1438            &ParseCacheWrite {
1439                modules: &modules,
1440                files,
1441                need_complexity,
1442                fingerprint_of: &|file: &DiscoveredFile| {
1443                    source_fingerprint(&file.path).for_content_read_at(read_started_ns)
1444                },
1445            },
1446        )
1447    };
1448    let metrics = core_backend::ParseMetrics {
1449        parse_ms,
1450        cache_ms,
1451        cache_hits: parse_result.cache_hits,
1452        cache_misses: parse_result.cache_misses,
1453        parse_cpu_ms: parse_result.parse_cpu_ms,
1454        cache_rejection,
1455        files_read: parse_result.files_read,
1456        source_bytes_read: parse_result.source_bytes_read,
1457        parse_cache_bytes_read,
1458        css_masked_bytes: parse_result.css_masked_bytes,
1459        parse_cache_load_ms,
1460    };
1461    ParsedModules {
1462        modules,
1463        metrics,
1464        source_diagnostics,
1465        read_failures: parse_result.read_failures,
1466        parse_degradations: parse_result.parse_degradations,
1467    }
1468}
1469
1470/// Record the read failures and parse degradations of a parse for the
1471/// project, and return them as workspace diagnostics.
1472fn record_source_diagnostics(
1473    root: &Path,
1474    read_failures: &[SourceReadFailure],
1475    parse_degradations: &[SourceParseDegradation],
1476) -> Vec<WorkspaceDiagnostic> {
1477    let mut diagnostics = fallow_config::record_source_read_failures(root, read_failures);
1478    diagnostics.extend(fallow_config::record_source_parse_degradations(
1479        root,
1480        parse_degradations,
1481    ));
1482    diagnostics
1483}
1484
1485fn reused_parse_metrics() -> core_backend::ParseMetrics {
1486    core_backend::ParseMetrics {
1487        parse_ms: 0.0,
1488        cache_ms: 0.0,
1489        cache_hits: 0,
1490        cache_misses: 0,
1491        parse_cpu_ms: 0.0,
1492        cache_rejection: None,
1493        files_read: 0,
1494        source_bytes_read: 0,
1495        parse_cache_bytes_read: 0,
1496        css_masked_bytes: 0,
1497        parse_cache_load_ms: 0.0,
1498    }
1499}
1500
1501fn source_fingerprints_for_files(files: &[DiscoveredFile]) -> Option<Vec<SourceFingerprint>> {
1502    files
1503        .iter()
1504        .map(|file| {
1505            std::fs::metadata(&file.path)
1506                .ok()
1507                .map(|metadata| SourceFingerprint::from_metadata(&metadata))
1508                .filter(|fingerprint| fingerprint.has_known_mtime())
1509        })
1510        .collect()
1511}
1512
1513fn update_parse_cache_if_enabled(
1514    config: &ResolvedConfig,
1515    cache: &mut Option<fallow_extract::cache::CacheStore>,
1516    write: &ParseCacheWrite<'_>,
1517) -> f64 {
1518    let start = Instant::now();
1519    write_parse_cache(config, cache, write);
1520    start.elapsed().as_secs_f64() * 1000.0
1521}
1522
1523/// Modules to store in the persisted parse cache, with the fingerprint that
1524/// each file gets in the cache.
1525struct ParseCacheWrite<'a> {
1526    modules: &'a [ModuleInfo],
1527    files: &'a [DiscoveredFile],
1528    need_complexity: bool,
1529    fingerprint_of: &'a dyn Fn(&DiscoveredFile) -> SourceFingerprint,
1530}
1531
1532fn write_parse_cache(
1533    config: &ResolvedConfig,
1534    cache: &mut Option<fallow_extract::cache::CacheStore>,
1535    write: &ParseCacheWrite<'_>,
1536) {
1537    if config.no_cache {
1538        return;
1539    }
1540
1541    let cache_max_size_bytes = crate::project_config::resolve_cache_max_size_bytes(config);
1542    let store = cache.get_or_insert_with(|| fallow_extract::cache::CacheStore::new(&config.root));
1543    if update_parse_cache(store, write)
1544        && let Err(error) = store.save(
1545            &config.cache_dir,
1546            config.cache_config_hash,
1547            cache_max_size_bytes,
1548        )
1549    {
1550        tracing::warn!("Failed to save cache: {error}");
1551    }
1552}
1553
1554/// Mirror of `fallow_core`'s `update_cache` for session-owned parsing: rewrite
1555/// an unchanged entry only when its metadata moved or when this run can add
1556/// complexity the entry lacks, and never let a complexity-blind run strip the
1557/// complexity a `health` run stored.
1558fn update_parse_cache(
1559    store: &mut fallow_extract::cache::CacheStore,
1560    write: &ParseCacheWrite<'_>,
1561) -> bool {
1562    let ParseCacheWrite {
1563        modules,
1564        files,
1565        need_complexity,
1566        fingerprint_of,
1567    } = *write;
1568    let mut dirty = false;
1569    for module in modules {
1570        if let Some(file) = files.get(module.file_id.0 as usize) {
1571            let fingerprint = fingerprint_of(file);
1572            if let Some(cached) = store.get_by_path_only(&file.path)
1573                && cached.content_hash == module.content_hash
1574            {
1575                let stale_metadata = cached.source_fingerprint() != fingerprint;
1576                let adds_complexity = need_complexity && !cached.complexity_extracted;
1577                if stale_metadata || adds_complexity {
1578                    let preserved_last_access = cached.last_access_secs;
1579                    let preserved_complexity = (!need_complexity && cached.complexity_extracted)
1580                        .then(|| cached.complexity.clone());
1581                    let mut refreshed = fallow_extract::cache::module_to_cached(
1582                        module,
1583                        fingerprint,
1584                        need_complexity,
1585                    );
1586                    refreshed.last_access_secs = preserved_last_access;
1587                    if let Some(complexity) = preserved_complexity {
1588                        refreshed.complexity = complexity;
1589                        refreshed.complexity_extracted = true;
1590                    }
1591                    store.insert(&file.path, refreshed);
1592                    dirty = true;
1593                }
1594                continue;
1595            }
1596            store.insert(
1597                &file.path,
1598                fallow_extract::cache::module_to_cached(module, fingerprint, need_complexity),
1599            );
1600            dirty = true;
1601        }
1602    }
1603    store.retain_paths(files) || dirty
1604}
1605
1606fn source_fingerprint(path: &Path) -> SourceFingerprint {
1607    std::fs::metadata(path).map_or_else(
1608        |_| SourceFingerprint::new(0, 0),
1609        |metadata| SourceFingerprint::from_metadata(&metadata),
1610    )
1611}
1612
1613struct EngineDeadCodePipelineInput<'a> {
1614    config: &'a ResolvedConfig,
1615    discovery: &'a crate::discover::AnalysisDiscovery,
1616    modules: Arc<[ModuleInfo]>,
1617    metrics: core_backend::ParseMetrics,
1618    collect_usages: bool,
1619    retain_graph: bool,
1620    retain_modules: bool,
1621    retain_files: bool,
1622    cancellation: Option<&'a AtomicBool>,
1623}
1624
1625fn run_engine_owned_dead_code_pipeline(
1626    input: EngineDeadCodePipelineInput<'_>,
1627) -> EngineResult<SharedDeadCodeAnalysisArtifacts> {
1628    let EngineDeadCodePipelineInput {
1629        config,
1630        discovery,
1631        modules,
1632        metrics,
1633        collect_usages,
1634        retain_graph,
1635        retain_modules,
1636        retain_files,
1637        cancellation,
1638    } = input;
1639    let stopped = |stage: &str| -> EngineResult<()> {
1640        if token_is_set(cancellation) {
1641            return Err(crate::EngineError::cancelled(stage));
1642        }
1643        Ok(())
1644    };
1645    stopped("the dead-code prelude")?;
1646    let prelude = core_backend::prepare_dead_code_backend_prelude(config, discovery)?;
1647    let prelude_timings = prelude.timings();
1648    stopped("dead-code entry-point discovery")?;
1649    let entry_points = core_backend::discover_dead_code_entry_points(&prelude);
1650    stopped("import resolution and graph construction")?;
1651    let (resolved, graph, graph_cache_rejection) =
1652        resolve_or_build_dead_code_graph(&prelude, &entry_points, &modules);
1653    stopped("the dead-code detectors")?;
1654
1655    let mut detector = core_backend::run_dead_code_detectors(
1656        &prelude,
1657        &graph.graph,
1658        &resolved.project.modules,
1659        &modules,
1660        collect_usages,
1661        &entry_points,
1662    );
1663    // Stamp ids on the full set, before any filter removes a finding. The
1664    // tiebreak suffix depends on the other findings with the same identity.
1665    crate::dead_code::stamp_finding_ids(&mut detector.results, &config.root);
1666    // Security ids too, so every surface reads the id this pipeline set.
1667    crate::security::stamp_security_finding_ids(
1668        &mut detector.results.security_findings,
1669        &config.root,
1670    );
1671    crate::dead_code::filter_configured_ignored_findings(&mut detector.results, config);
1672    // The detectors are the longest uninterruptible stage. Without this a
1673    // token set inside them yields a complete report, so the same request
1674    // would be answered with results or with `cancelled` depending only on
1675    // which side of the stage the flip landed.
1676    stopped("assembling the dead-code report")?;
1677    let profile =
1678        core_backend::dead_code_pipeline_profile(core_backend::DeadCodePipelineProfileInput {
1679            retain_timings: retain_graph,
1680            prelude: &prelude,
1681            prelude_timings,
1682            parse_metrics: metrics,
1683            module_count: modules.len(),
1684            entry_points: &entry_points,
1685            resolved: &resolved,
1686            graph: &graph,
1687            detector: &detector,
1688            file_count: discovery.files().len(),
1689            workspace_count: discovery.workspaces().len(),
1690            graph_cache_rejection,
1691        });
1692    let script_used_packages = prelude.script_used_packages();
1693    let trace_provenance = prelude.trace_provenance(&modules);
1694    prelude.finish();
1695    let file_hashes = collect_file_hashes(&modules, discovery.files());
1696
1697    Ok(SharedDeadCodeAnalysisArtifacts {
1698        results: detector.results,
1699        timings: profile.timings,
1700        graph: retain_graph.then_some(graph.graph),
1701        modules: retain_modules.then_some(modules),
1702        files: retain_files.then(|| discovery.files().to_vec()),
1703        script_used_packages,
1704        trace_provenance,
1705        file_hashes,
1706    })
1707}
1708
1709/// Reuse the persisted module graph, or rebuild it and carry the reason the
1710/// persisted one was refused.
1711///
1712/// The reason is the third element rather than a discarded `Option`: a warm run
1713/// that paid for a multi-megabyte decode and reused none of it is the case the
1714/// perf table exists to explain, and every engine-backed command reaches the
1715/// pipeline through here.
1716fn resolve_or_build_dead_code_graph(
1717    prelude: &core_backend::DeadCodeBackendPrelude,
1718    entry_points: &core_backend::DeadCodeEntryPoints,
1719    modules: &[ModuleInfo],
1720) -> (
1721    core_backend::DeadCodeResolvedModules,
1722    core_backend::DeadCodeGraphRun,
1723    Option<CacheRejection>,
1724) {
1725    let rejection =
1726        match core_backend::try_load_dead_code_graph_cache(prelude, entry_points, modules) {
1727            Ok((resolved, graph)) => return (resolved, graph, None),
1728            Err(rejection) => rejection,
1729        };
1730
1731    let resolved = core_backend::resolve_dead_code_imports(prelude, modules);
1732    let graph =
1733        core_backend::build_dead_code_graph(prelude, &resolved.project, entry_points, modules);
1734    (resolved, graph, rejection)
1735}
1736
1737fn collect_file_hashes(
1738    modules: &[ModuleInfo],
1739    files: &[DiscoveredFile],
1740) -> FxHashMap<PathBuf, u64> {
1741    modules
1742        .iter()
1743        .filter_map(|module| {
1744            files
1745                .get(module.file_id.0 as usize)
1746                .map(|file| (file.path.clone(), module.content_hash))
1747        })
1748        .collect()
1749}
1750
1751pub(crate) fn analyze_dead_code_with_parse_result_from_config(
1752    config: &ResolvedConfig,
1753    modules: &[ModuleInfo],
1754) -> EngineResult<DeadCodeAnalysisArtifacts> {
1755    let (workspaces, _diagnostics, workspaces_ms) =
1756        crate::project_config::collect_workspace_metadata(config)?;
1757    let discovery = crate::discover::prepare_analysis_discovery_with_workspaces(
1758        config,
1759        &workspaces,
1760        workspaces_ms,
1761    );
1762    run_engine_owned_dead_code_pipeline(EngineDeadCodePipelineInput {
1763        config,
1764        discovery: &discovery,
1765        modules: Arc::from(modules),
1766        metrics: reused_parse_metrics(),
1767        collect_usages: true,
1768        retain_graph: true,
1769        retain_modules: false,
1770        retain_files: false,
1771        cancellation: None,
1772    })
1773    .map(SharedDeadCodeAnalysisArtifacts::into_owned)
1774}
1775
1776#[cfg(test)]
1777mod tests {
1778    use std::fmt::Write as _;
1779    use std::time::Duration;
1780
1781    use super::*;
1782
1783    fn session_with_source(source: &str) -> (tempfile::TempDir, AnalysisSession) {
1784        let project = tempfile::tempdir().expect("project");
1785        let root = project.path();
1786        std::fs::create_dir(root.join("src")).expect("create source directory");
1787        std::fs::write(root.join("src/index.ts"), source).expect("write source");
1788        let session = AnalysisSession::load_default(root);
1789        (project, session)
1790    }
1791
1792    /// A cancelled session reports the failure and names the boundary it
1793    /// stopped at, so a caller can tell a stopped run from a broken one.
1794    #[test]
1795    fn a_cancelled_session_returns_a_cancellation_error_not_an_empty_result() {
1796        let project = tempfile::tempdir().expect("project");
1797        let root = project.path();
1798        std::fs::create_dir(root.join("src")).expect("create source directory");
1799        std::fs::write(root.join("src/index.ts"), "export const entry = 1;\n").expect("entry");
1800        std::fs::write(root.join("src/orphan.ts"), "export const orphan = 1;\n").expect("orphan");
1801
1802        let baseline = AnalysisSession::load_default(root)
1803            .analyze_dead_code()
1804            .expect("an uncancelled session analyzes");
1805        assert!(
1806            !baseline.results.unused_files.is_empty(),
1807            "the fixture must have findings, so an empty result would be a plausible wrong answer"
1808        );
1809
1810        let error = AnalysisSession::load_default(root)
1811            .with_cancellation(Arc::new(AtomicBool::new(true)))
1812            .analyze_dead_code()
1813            .expect_err("a cancelled session must not return results");
1814        assert!(error.is_cancelled(), "unexpected error: {error}");
1815        assert!(error.message().contains("cancelled"));
1816    }
1817
1818    /// The engine pipeline is what every CLI command runs, and it reported no
1819    /// graph-cache reason at all: the loader produced one, the boundary threw
1820    /// it away, and the profile hardcoded `None`. A warm run that decoded a
1821    /// multi-megabyte graph and then rebuilt from scratch looked exactly like a
1822    /// first run, so the row that explains it could never print.
1823    #[test]
1824    fn a_refused_graph_cache_names_its_reason_in_the_engine_timings() {
1825        let project = tempfile::tempdir().expect("project");
1826        let root = project.path();
1827        std::fs::create_dir(root.join("src")).expect("create source directory");
1828        std::fs::write(root.join("src/index.ts"), "export const entry = 1;\n").expect("entry");
1829
1830        let cold = AnalysisSession::load_default(root)
1831            .analyze_dead_code_with_artifacts(false, true)
1832            .expect("cold run analyzes");
1833        assert_eq!(
1834            cold.timings
1835                .expect("cold timings retained")
1836                .graph_cache_rejection,
1837            Some(CacheRejection::Absent),
1838            "a first run has no persisted graph to refuse"
1839        );
1840
1841        std::fs::write(
1842            root.join("src/index.ts"),
1843            "export const entry = 1;\nexport const added = 2;\n",
1844        )
1845        .expect("edit the entry");
1846
1847        let warm = AnalysisSession::load_default(root)
1848            .analyze_dead_code_with_artifacts(false, true)
1849            .expect("warm run analyzes");
1850        assert_eq!(
1851            warm.timings
1852                .expect("warm timings retained")
1853                .graph_cache_rejection,
1854            Some(CacheRejection::FingerprintChanged),
1855            "the decoded graph was refused because a file changed, and the run must say so"
1856        );
1857    }
1858
1859    /// A session that is not given a token can never be cancelled, so every
1860    /// existing caller keeps its current behavior.
1861    #[test]
1862    fn a_session_without_a_token_is_never_cancelled() {
1863        let (_project, session) = session_with_source("export const unused = 1;\n");
1864        assert!(!session.is_cancelled());
1865        session
1866            .analyze_dead_code()
1867            .expect("a session without a token analyzes");
1868    }
1869
1870    /// Files per fixture for the parse-loop tests. Large enough that a parse
1871    /// takes long enough to be cancelled part way through, small enough to
1872    /// stay a unit test.
1873    const PARSE_LOOP_FILES: usize = 400;
1874
1875    fn parse_loop_project() -> tempfile::TempDir {
1876        let project = tempfile::tempdir().expect("project");
1877        let src = project.path().join("src");
1878        std::fs::create_dir_all(&src).expect("src dir");
1879        for module in 0..PARSE_LOOP_FILES {
1880            let mut source = String::new();
1881            for symbol in 0..20 {
1882                let _ = writeln!(
1883                    source,
1884                    "export const helper{symbol} = (input: number): number => {{\n  \
1885                     if (input > {symbol}) {{\n    return input * {symbol};\n  }}\n  \
1886                     return input - {symbol};\n}};"
1887                );
1888            }
1889            std::fs::write(src.join(format!("mod{module}.ts")), source).expect("module");
1890        }
1891        project
1892    }
1893
1894    /// A platform without ctime, such as Windows, gives fingerprints that
1895    /// can stand in for the content only after a content check. A kept
1896    /// session must not serve its modules to the next run on such a
1897    /// fingerprint, because a same-size edit with a restored mtime keeps it.
1898    // Unix only: other platforms expose no inode change time, so no
1899    // fingerprint is trustworthy and a refresh always drops the modules.
1900    #[cfg(unix)]
1901    #[test]
1902    fn a_refresh_drops_modules_whose_fingerprints_need_a_content_check() {
1903        let (_project, mut session) = session_with_source("export const kept = 1;\n");
1904        drop(session.parse_modules(false, None));
1905
1906        assert!(!session.refresh_discovery(), "the file set is the same");
1907        assert!(
1908            session.parsed_cache.lock().expect("parse cache").is_some(),
1909            "fingerprints with a known ctime keep the modules for the next run"
1910        );
1911
1912        if let Some(cache) = session
1913            .parsed_cache
1914            .get_mut()
1915            .expect("parse cache")
1916            .as_mut()
1917        {
1918            for fingerprint in &mut cache.fingerprints {
1919                fingerprint.ctime_ns = 0;
1920            }
1921        }
1922        assert!(!session.refresh_discovery(), "the file set is the same");
1923        assert!(
1924            session.parsed_cache.lock().expect("parse cache").is_none(),
1925            "the next run parses through the persisted cache, which checks the content"
1926        );
1927    }
1928
1929    /// A flush writes the kept modules to the persisted cache. A fingerprint
1930    /// that did not settle before the kept parse read the file must lose its
1931    /// ctime there, or the next run takes the metadata fast path on it.
1932    #[cfg(unix)]
1933    #[test]
1934    fn a_flush_stores_an_unsettled_fingerprint_without_its_ctime() {
1935        let (project, mut session) = session_with_source("export const kept = 1;\n");
1936        drop(session.parse_modules(false, None));
1937        let path = project.path().join("src/index.ts");
1938        let stored_ctime = |session: &AnalysisSession| {
1939            let _ = std::fs::remove_dir_all(&session.config.cache_dir);
1940            session.disk_cache_stale.store(true, Ordering::SeqCst);
1941            session.flush_parse_cache();
1942            let store = fallow_extract::cache::CacheStore::load(
1943                &session.config.cache_dir,
1944                &session.config.root,
1945                session.config.cache_config_hash,
1946                crate::project_config::resolve_cache_max_size_bytes(&session.config),
1947            )
1948            .expect("flush writes the parse cache");
1949            store
1950                .get_by_path_only(&path)
1951                .expect("flushed module")
1952                .ctime_ns
1953        };
1954
1955        // Set the read start relative to the file's own ctime, so the test
1956        // does not depend on how much time passed since the write.
1957        let set_read_started = |session: &mut AnalysisSession, offset: u64| {
1958            if let Some(cache) = session
1959                .parsed_cache
1960                .get_mut()
1961                .expect("parse cache")
1962                .as_mut()
1963            {
1964                let ctime = cache.fingerprints[0].ctime_ns;
1965                assert!(ctime > 0, "the platform reports a ctime");
1966                cache.read_started_ns = ctime.saturating_add(offset);
1967            }
1968        };
1969
1970        set_read_started(&mut session, 0);
1971        assert_eq!(
1972            stored_ctime(&session),
1973            0,
1974            "a fingerprint inside the window is not settled"
1975        );
1976
1977        set_read_started(
1978            &mut session,
1979            fallow_types::source_fingerprint::TIMESTAMP_SETTLE_WINDOW_NS,
1980        );
1981        assert!(
1982            stored_ctime(&session) > 0,
1983            "a settled fingerprint keeps its ctime"
1984        );
1985    }
1986
1987    /// A same-length write in the same timestamp tick as the previous write
1988    /// keeps mtime, ctime and size. The kept modules must not stand in for
1989    /// the new content when the fingerprint is that recent.
1990    #[cfg(unix)]
1991    #[test]
1992    fn a_same_tick_rewrite_does_not_reuse_the_kept_module() {
1993        let (project, mut session) = session_with_source("export const alpha = 1;\n");
1994        drop(session.parse_modules(false, None));
1995
1996        let path = project.path().join("src/index.ts");
1997        std::fs::write(&path, "export const bravo = 1;\n").expect("rewrite source");
1998        let same_tick = SourceFingerprint::from_metadata(
1999            &std::fs::metadata(&path).expect("metadata after rewrite"),
2000        );
2001        if let Some(cache) = session
2002            .parsed_cache
2003            .get_mut()
2004            .expect("parse cache")
2005            .as_mut()
2006        {
2007            cache.fingerprints = vec![same_tick];
2008        }
2009
2010        let modules = session.parse_modules(false, None).modules;
2011        let exports: Vec<String> = modules
2012            .iter()
2013            .flat_map(|module| module.exports.iter())
2014            .map(|export| export.name.to_string())
2015            .collect();
2016        assert_eq!(exports, vec!["bravo".to_string()]);
2017    }
2018
2019    /// A session over `root` that never reads or writes the on-disk parse
2020    /// cache, so repeated parses in one test all do the same work.
2021    fn uncached_session(root: &Path) -> AnalysisSession {
2022        let mut project_config = crate::project_config::default_project_config(root);
2023        project_config.config.no_cache = true;
2024        AnalysisSession::from_config(project_config)
2025    }
2026
2027    /// The parse loop is the one place cancellation stops work per item rather
2028    /// than at a stage boundary, so it is the one place the stop can be
2029    /// asserted from what was parsed instead of from how long the call took.
2030    ///
2031    /// A truncated parse is also the most dangerous thing cancellation
2032    /// produces: served from a cache it would read as a project with fewer
2033    /// modules long after the cancellation was forgotten. The second half
2034    /// asserts it is not retained.
2035    #[test]
2036    fn a_cancelled_parse_stops_partway_and_leaves_no_truncated_cache_behind() {
2037        let project = parse_loop_project();
2038        let root = project.path();
2039
2040        // Warm the page cache, so the timed run measures parsing.
2041        drop(uncached_session(root).parse_modules(false, None));
2042
2043        let started = Instant::now();
2044        let full = uncached_session(root).parse_modules(false, None);
2045        let full_parse = started.elapsed();
2046        let full_count = full.modules.len();
2047        assert_eq!(
2048            full_count, PARSE_LOOP_FILES,
2049            "the fixture must parse every generated module"
2050        );
2051        assert!(
2052            full_parse >= Duration::from_millis(20),
2053            "the fixture is too small to cancel part way through: {full_parse:?}"
2054        );
2055
2056        // Where the flip lands is a scheduling outcome, so this retries until
2057        // one attempt lands strictly inside the loop. Every attempt asserts the
2058        // property; the retry only chooses an attempt that measures the stop
2059        // part way through rather than at the entry guard.
2060        let mut partial = None;
2061        let mut cancelled_session = None;
2062        for attempt in 1..=6_u32 {
2063            let token = Arc::new(AtomicBool::new(false));
2064            // Build the session before arming the watchdog. Discovery runs in
2065            // the constructor, and a delay spent there would leave the token
2066            // already set when the loop starts.
2067            let session = uncached_session(root).with_cancellation(Arc::clone(&token));
2068            let watchdog = {
2069                let token = Arc::clone(&token);
2070                let delay = full_parse * attempt / 6;
2071                std::thread::spawn(move || {
2072                    std::thread::sleep(delay);
2073                    token.store(true, Ordering::SeqCst);
2074                })
2075            };
2076            let cancelled = session.parse_modules(false, Some(&token));
2077            watchdog.join().expect("watchdog thread");
2078
2079            let parsed = cancelled.modules.len();
2080            assert!(
2081                parsed < full_count,
2082                "the parse returned all {full_count} modules, so the loop never read the token"
2083            );
2084            cancelled_session = Some(session);
2085            if parsed > 0 {
2086                partial = Some(parsed);
2087                break;
2088            }
2089        }
2090        let parsed = partial.expect(
2091            "no attempt flipped the token while the loop was running, so this never measured a \
2092             stop part way through",
2093        );
2094        assert!(parsed < full_count);
2095
2096        assert!(
2097            cancelled_session
2098                .expect("a cancelled session")
2099                .parsed_cache
2100                .lock()
2101                .expect("parse cache")
2102                .is_none(),
2103            "a truncated parse must not be retained as this session's warm cache"
2104        );
2105        let recovered = uncached_session(root).parse_modules(false, None);
2106        assert_eq!(
2107            recovered.modules.len(),
2108            full_count,
2109            "a later uncancelled parse must still see the whole project"
2110        );
2111    }
2112
2113    #[test]
2114    fn session_retains_workspace_metadata_from_config_load() {
2115        let project = tempfile::tempdir().expect("project");
2116        let root = project.path();
2117        std::fs::write(
2118            root.join("package.json"),
2119            r#"{"name":"root","workspaces":["packages/*"]}"#,
2120        )
2121        .expect("write root package");
2122        std::fs::create_dir_all(root.join("packages/a")).expect("create workspace");
2123        std::fs::write(
2124            root.join("packages/a/package.json"),
2125            r#"{"name":"pkg-a","type":"module"}"#,
2126        )
2127        .expect("write workspace package");
2128
2129        let session = AnalysisSession::load(root, None).expect("session loads");
2130
2131        assert!(
2132            session
2133                .workspaces()
2134                .iter()
2135                .any(|workspace| workspace.name == "pkg-a"),
2136            "session must retain workspace metadata discovered during config load"
2137        );
2138    }
2139
2140    #[test]
2141    fn finding_ignore_filters_results_without_removing_graph_inputs() {
2142        let project = tempfile::tempdir().expect("project");
2143        let root = project.path();
2144        std::fs::create_dir(root.join("src")).expect("create source directory");
2145        std::fs::write(
2146            root.join("package.json"),
2147            r#"{"name":"finding-ignore","devDependencies":{"vitest":"latest"}}"#,
2148        )
2149        .expect("write package manifest");
2150        std::fs::write(
2151            root.join("vitest.config.ts"),
2152            "import './src/feature';\nexport default {};\n",
2153        )
2154        .expect("write vitest config");
2155        std::fs::write(
2156            root.join("src/feature.ts"),
2157            "export const feature = true;\n",
2158        )
2159        .expect("write reachable source");
2160        std::fs::write(root.join("src/hidden.ts"), "export const hidden = true;\n")
2161            .expect("write hidden source");
2162
2163        let unfiltered = AnalysisSession::load(root, None)
2164            .expect("unfiltered session loads")
2165            .analyze_dead_code()
2166            .expect("unfiltered analysis succeeds");
2167        assert!(
2168            unfiltered
2169                .results
2170                .unused_files
2171                .iter()
2172                .any(|finding| finding.file.path.ends_with("src/hidden.ts"))
2173        );
2174
2175        std::fs::write(
2176            root.join(".fallowrc.json"),
2177            r#"{"ignoreFindings":["src/hidden.ts"]}"#,
2178        )
2179        .expect("write fallow config");
2180        let session = AnalysisSession::load(root, None).expect("filtered session loads");
2181        let hidden_path = root.join("src/hidden.ts");
2182        assert!(session.files().iter().any(|file| file.path == hidden_path));
2183
2184        let filtered = session
2185            .analyze_dead_code_with_artifacts(false, true)
2186            .expect("filtered analysis succeeds");
2187        assert!(
2188            filtered
2189                .results
2190                .unused_files
2191                .iter()
2192                .all(|finding| finding.file.path != hidden_path)
2193        );
2194        assert!(
2195            filtered
2196                .graph
2197                .as_ref()
2198                .is_some_and(|graph| graph.module_count() == session.files().len())
2199        );
2200    }
2201
2202    #[test]
2203    fn finding_ignore_normalizes_separators_and_rejects_outside_paths() {
2204        use fallow_types::output_dead_code::UnusedFileFinding;
2205        use fallow_types::results::UnusedFile;
2206
2207        let project = tempfile::tempdir().expect("project");
2208        let config = serde_json::from_str::<fallow_config::FallowConfig>(
2209            r#"{"ignoreFindings":["**/*.ts"]}"#,
2210        )
2211        .expect("config parses")
2212        .resolve(
2213            project.path().to_path_buf(),
2214            fallow_config::OutputFormat::Human,
2215            1,
2216            true,
2217            true,
2218            None,
2219        );
2220        let outside = project
2221            .path()
2222            .parent()
2223            .expect("project has parent")
2224            .join("outside.ts");
2225        let mut results = AnalysisResults {
2226            unused_files: vec![
2227                UnusedFileFinding::with_actions(UnusedFile {
2228                    path: PathBuf::from(r"src\hidden.ts"),
2229                }),
2230                UnusedFileFinding::with_actions(UnusedFile {
2231                    path: outside.clone(),
2232                }),
2233            ],
2234            ..AnalysisResults::default()
2235        };
2236
2237        crate::dead_code::filter_configured_ignored_findings(&mut results, &config);
2238
2239        assert_eq!(results.unused_files.len(), 1);
2240        assert_eq!(results.unused_files[0].file.path, outside);
2241    }
2242
2243    /// Two files under `src/`, with the default config, so the persisted
2244    /// parse cache is on.
2245    #[cfg(unix)]
2246    fn warm_store_project() -> tempfile::TempDir {
2247        let project = tempfile::tempdir().expect("project");
2248        let root = project.path();
2249        std::fs::create_dir(root.join("src")).expect("create source directory");
2250        std::fs::write(root.join("src/index.ts"), "export const entry = 1;\n").expect("entry");
2251        std::fs::write(root.join("src/other.ts"), "export const other = 2;\n").expect("other");
2252        project
2253    }
2254
2255    #[cfg(unix)]
2256    fn warm_session(root: &Path, store: &Arc<WarmParseStore>) -> AnalysisSession {
2257        AnalysisSession::load_default(root).with_warm_parse(Some(Arc::clone(store)))
2258    }
2259
2260    #[cfg(unix)]
2261    fn exported_names(modules: &[ModuleInfo]) -> Vec<String> {
2262        let mut names: Vec<String> = modules
2263            .iter()
2264            .flat_map(|module| module.exports.iter().map(|export| export.name.to_string()))
2265            .collect();
2266        names.sort();
2267        names
2268    }
2269
2270    #[cfg(unix)]
2271    #[test]
2272    fn a_warm_store_serves_a_new_session_without_parse_work() {
2273        let project = warm_store_project();
2274        let store = Arc::new(WarmParseStore::new(
2275            crate::warm_parse::WarmParseLimits::default(),
2276        ));
2277
2278        let first = warm_session(project.path(), &store).parse_modules(false, None);
2279        let second = warm_session(project.path(), &store).parse_modules(false, None);
2280
2281        assert!(Arc::ptr_eq(&first.modules, &second.modules));
2282        assert_eq!(second.metrics.cache_hits + second.metrics.cache_misses, 0);
2283        let counts = store.counts();
2284        assert_eq!(counts.parse_runs, 1);
2285        assert_eq!(counts.modules_parsed, 2);
2286        assert_eq!(counts.modules_reused, 2);
2287    }
2288
2289    #[cfg(unix)]
2290    #[test]
2291    fn a_warm_store_serves_the_health_parse_of_a_new_session() {
2292        let project = warm_store_project();
2293        let store = Arc::new(WarmParseStore::new(
2294            crate::warm_parse::WarmParseLimits::default(),
2295        ));
2296
2297        drop(warm_session(project.path(), &store).parse_modules(false, None));
2298        let health = warm_session(project.path(), &store).parsed_parts_uncached(true);
2299
2300        assert_eq!(health.modules.len(), 2);
2301        assert_eq!(health.cache_hits + health.cache_misses, 0);
2302        assert_eq!(
2303            store.counts().parse_runs,
2304            1,
2305            "the first parse computed complexity"
2306        );
2307    }
2308
2309    #[cfg(unix)]
2310    #[test]
2311    fn an_edit_or_a_new_file_makes_the_next_session_parse_again() {
2312        let project = warm_store_project();
2313        let root = project.path();
2314        let store = Arc::new(WarmParseStore::new(
2315            crate::warm_parse::WarmParseLimits::default(),
2316        ));
2317        drop(warm_session(root, &store).parse_modules(false, None));
2318
2319        std::fs::write(root.join("src/other.ts"), "export const renamed = 22;\n").expect("edit");
2320        let edited = warm_session(root, &store).parse_modules(false, None);
2321        assert_eq!(exported_names(&edited.modules), ["entry", "renamed"]);
2322        assert_eq!(
2323            edited.metrics.cache_misses, 1,
2324            "only the edited file is parsed"
2325        );
2326
2327        std::fs::write(root.join("src/added.ts"), "export const added = 3;\n").expect("add");
2328        let added = warm_session(root, &store).parse_modules(false, None);
2329        assert_eq!(
2330            exported_names(&added.modules),
2331            ["added", "entry", "renamed"]
2332        );
2333
2334        let counts = store.counts();
2335        assert_eq!(counts.parse_runs, 3);
2336        assert_eq!(counts.modules_reused, 0);
2337        assert_eq!(store.len(), 2, "each file list keeps its latest parse only");
2338    }
2339
2340    #[cfg(unix)]
2341    #[test]
2342    fn a_session_without_the_persisted_cache_does_not_use_the_store() {
2343        let project = warm_store_project();
2344        let store = Arc::new(WarmParseStore::new(
2345            crate::warm_parse::WarmParseLimits::default(),
2346        ));
2347
2348        drop(
2349            uncached_session(project.path())
2350                .with_warm_parse(Some(Arc::clone(&store)))
2351                .parse_modules(false, None),
2352        );
2353
2354        assert_eq!(
2355            store.counts(),
2356            crate::warm_parse::WarmParseCounts::default()
2357        );
2358        assert!(store.is_empty());
2359    }
2360
2361    /// A parse records its read failures for the project. A session that
2362    /// takes kept modules must record them again, or a run between the two
2363    /// that cleared them would hide the failure.
2364    #[cfg(unix)]
2365    #[test]
2366    fn a_kept_parse_records_its_read_failures_again() {
2367        let project = warm_store_project();
2368        let root = project.path();
2369        std::fs::write(root.join("src/broken.ts"), [0xff, 0xfe, 0x00]).expect("invalid UTF-8");
2370        let store = Arc::new(WarmParseStore::new(
2371            crate::warm_parse::WarmParseLimits::default(),
2372        ));
2373        let read_failures = |session: &AnalysisSession| {
2374            session
2375                .current_workspace_diagnostics()
2376                .into_iter()
2377                .filter(|diagnostic| {
2378                    matches!(
2379                        diagnostic.kind,
2380                        fallow_types::workspace::WorkspaceDiagnosticKind::SourceReadFailure { .. }
2381                    )
2382                })
2383                .count()
2384        };
2385
2386        let first = warm_session(root, &store);
2387        drop(first.parse_modules(false, None));
2388        assert_eq!(read_failures(&first), 1);
2389
2390        drop(fallow_config::record_source_read_failures(root, &[]));
2391        let second = warm_session(root, &store);
2392        drop(second.parse_modules(false, None));
2393        assert_eq!(store.counts().modules_reused, 2);
2394        assert_eq!(read_failures(&second), 1);
2395    }
2396
2397    #[test]
2398    fn warm_parse_cache_reuses_module_storage() {
2399        let (_project, session) = session_with_source("export function value() { return 1; }\n");
2400        let first = session.parse_modules(true, None);
2401        let second = session.parse_modules(false, None);
2402
2403        assert!(
2404            Arc::ptr_eq(&first.modules, &second.modules),
2405            "warm session queries must share parsed module storage"
2406        );
2407    }
2408
2409    #[test]
2410    fn warm_styling_cache_reuses_artifact_allocation() {
2411        let project = tempfile::tempdir().expect("project");
2412        let root = project.path();
2413        std::fs::write(root.join("styles.css"), ".button { color: red; }\n")
2414            .expect("write stylesheet");
2415        let session = AnalysisSession::load_default(root);
2416
2417        let first = session.styling_analysis_artifacts();
2418        let second = session.styling_analysis_artifacts();
2419
2420        assert!(
2421            Arc::ptr_eq(&first, &second),
2422            "warm styling queries must share the cached artifact allocation"
2423        );
2424    }
2425
2426    #[test]
2427    fn shared_parsed_modules_reuse_public_session_storage() {
2428        let (_project, session) = session_with_source("export const value = 1;\n");
2429        let first = session.shared_parsed_modules(true);
2430        let second = session.shared_parsed_modules(false);
2431
2432        assert!(Arc::ptr_eq(&first, &second));
2433    }
2434
2435    #[test]
2436    fn shared_parsed_parts_reuse_public_session_storage() {
2437        let (_project, session) = session_with_source("export const value = 1;\n");
2438        let cached = session.shared_parsed_modules(true);
2439        let parts = session.shared_parsed_parts(false);
2440
2441        assert!(Arc::ptr_eq(&cached, &parts.modules));
2442    }
2443
2444    #[test]
2445    fn warm_complexity_artifacts_reuse_cached_module_storage() {
2446        let (_project, session) = session_with_source("export function value() { return 1; }\n");
2447        let cached = session.parse_modules(true, None);
2448        let artifacts = session
2449            .analyze_dead_code_with_reuse_artifacts(true, true, false)
2450            .expect("analysis succeeds");
2451        let retained = artifacts.modules.expect("complexity modules retained");
2452
2453        assert!(
2454            Arc::ptr_eq(&cached.modules, &retained),
2455            "warm complexity artifacts must share parsed module storage"
2456        );
2457    }
2458
2459    #[test]
2460    fn shared_and_owned_artifacts_preserve_output_bytes() {
2461        let (_project, session) = session_with_source(
2462            "export const used = 1;\nexport const unused = 2;\nconsole.log(used);\n",
2463        );
2464        let owned = session
2465            .analyze_dead_code_with_artifacts(true, true)
2466            .expect("owned analysis succeeds");
2467        let shared = session
2468            .analyze_dead_code_with_shared_artifacts(true, true)
2469            .expect("shared analysis succeeds");
2470
2471        assert_eq!(
2472            serde_json::to_vec(&owned.results).expect("serialize owned results"),
2473            serde_json::to_vec(&shared.results).expect("serialize shared results")
2474        );
2475        assert_eq!(owned.file_hashes, shared.file_hashes);
2476        assert_eq!(
2477            owned
2478                .modules
2479                .as_deref()
2480                .unwrap_or_default()
2481                .iter()
2482                .map(|module| module.content_hash)
2483                .collect::<Vec<_>>(),
2484            shared
2485                .modules
2486                .as_deref()
2487                .unwrap_or_default()
2488                .iter()
2489                .map(|module| module.content_hash)
2490                .collect::<Vec<_>>()
2491        );
2492    }
2493
2494    #[test]
2495    fn route_loader_whole_use_matches_across_cold_and_warm_sessions() {
2496        let project = tempfile::tempdir().expect("project");
2497        let root = project.path();
2498        std::fs::create_dir_all(root.join("app/routes")).expect("create route directory");
2499        std::fs::write(
2500            root.join("package.json"),
2501            r#"{"name":"route-cache-parity","dependencies":{"react-router":"latest"}}"#,
2502        )
2503        .expect("write package manifest");
2504        std::fs::write(
2505            root.join("app/routes/home.tsx"),
2506            r#"
2507import { useLoaderData } from "react-router";
2508export function loader() { return { opaque: "value" }; }
2509export default function Home() {
2510  const data = useLoaderData<typeof loader>();
2511  const copy = { ...data };
2512  return JSON.stringify(copy);
2513}
2514"#,
2515        )
2516        .expect("write route module");
2517
2518        let cold_session = AnalysisSession::load(root, None).expect("cold session loads");
2519        let cold_parse = cold_session.parsed_parts(false);
2520        assert_eq!(cold_parse.cache_hits, 0, "first parse must be cold");
2521        let cold = cold_session
2522            .analyze_dead_code()
2523            .expect("cold analysis succeeds");
2524
2525        let warm_session = AnalysisSession::load(root, None).expect("warm session loads");
2526        let warm_parse = warm_session.parsed_parts(false);
2527        assert!(
2528            warm_parse.cache_hits > 0,
2529            "second session must use disk cache"
2530        );
2531        let warm = warm_session
2532            .analyze_dead_code()
2533            .expect("warm analysis succeeds");
2534
2535        assert!(
2536            cold.results.unused_load_data_keys.is_empty(),
2537            "cold analysis must abstain for an opaque route-loader use"
2538        );
2539        assert_eq!(
2540            serde_json::to_vec(&cold.results).expect("serialize cold results"),
2541            serde_json::to_vec(&warm.results).expect("serialize warm results"),
2542            "warm route-loader analysis must match cold analysis"
2543        );
2544    }
2545
2546    #[test]
2547    fn replaced_module_coverage_matches_across_cold_and_warm_graph_cache() {
2548        let project = tempfile::tempdir().expect("project");
2549        let root = project.path();
2550        std::fs::create_dir(root.join("src")).expect("create source directory");
2551        std::fs::write(
2552            root.join("package.json"),
2553            r#"{"name":"mock-cache-parity","main":"src/index.ts","devDependencies":{"vitest":"latest"}}"#,
2554        )
2555        .expect("write package manifest");
2556        std::fs::write(
2557            root.join("src/dependency.ts"),
2558            "export function dependency() { return 'real'; }\n",
2559        )
2560        .expect("write dependency");
2561        std::fs::write(
2562            root.join("src/wrapper.ts"),
2563            "import { dependency } from './dependency';\nexport function wrapper() { return dependency(); }\n",
2564        )
2565        .expect("write wrapper");
2566        std::fs::write(
2567            root.join("src/index.ts"),
2568            "export { wrapper } from './wrapper';\n",
2569        )
2570        .expect("write entry point");
2571        std::fs::write(
2572            root.join("src/wrapper.test.ts"),
2573            r#"
2574import { vi } from "vitest";
2575vi.mock("./dependency", () => ({ dependency: () => "mock" }));
2576import { wrapper } from "./wrapper";
2577wrapper();
2578"#,
2579        )
2580        .expect("write test");
2581
2582        let cold_session = AnalysisSession::load(root, None).expect("cold session loads");
2583        let dependency_id = cold_session
2584            .files()
2585            .iter()
2586            .find(|file| file.path == root.join("src/dependency.ts"))
2587            .expect("dependency discovered")
2588            .id;
2589        let cold = cold_session
2590            .analyze_dead_code_with_artifacts(false, true)
2591            .expect("cold analysis succeeds");
2592        let cold_exports = crate::module_graph::module_value_exports(
2593            cold.graph.as_ref().expect("cold graph retained"),
2594        );
2595        assert!(
2596            fallow_graph::cache::GraphCacheStore::load(&cold_session.config().cache_dir).is_ok(),
2597            "cold analysis must persist the graph cache"
2598        );
2599
2600        let warm_session = AnalysisSession::load(root, None).expect("warm session loads");
2601        let warm = warm_session
2602            .analyze_dead_code_with_artifacts(false, true)
2603            .expect("warm analysis succeeds");
2604        let warm_exports = crate::module_graph::module_value_exports(
2605            warm.graph.as_ref().expect("warm graph retained"),
2606        );
2607
2608        let dependency = cold_exports
2609            .iter()
2610            .find(|export| export.file_id == dependency_id && export.name == "dependency")
2611            .expect("dependency export retained");
2612        assert!(!dependency.test_referenced);
2613        assert_eq!(warm_exports, cold_exports);
2614    }
2615
2616    #[test]
2617    fn session_parse_surfaces_removed_source_with_sparse_file_ids() {
2618        let project = tempfile::tempdir().expect("project");
2619        let root = project.path();
2620        std::fs::create_dir(root.join("src")).expect("create source directory");
2621        std::fs::write(root.join("package.json"), r#"{"name":"read-failure"}"#)
2622            .expect("write package manifest");
2623        for name in ["a.ts", "b.ts", "c.ts"] {
2624            std::fs::write(
2625                root.join("src").join(name),
2626                format!("export const {} = 1;\n", name.replace('.', "_")),
2627            )
2628            .expect("write source");
2629        }
2630        let session = AnalysisSession::load(root, None).expect("session loads");
2631        let removed_path = root.join("src/b.ts");
2632        let removed_id = session
2633            .files()
2634            .iter()
2635            .find(|file| file.path == removed_path)
2636            .expect("removed source discovered")
2637            .id;
2638        std::fs::remove_file(&removed_path).expect("remove source after discovery");
2639
2640        let parts = session.parsed_parts(false);
2641
2642        assert!(
2643            parts
2644                .modules
2645                .iter()
2646                .all(|module| module.file_id != removed_id),
2647            "unreadable file must not receive a placeholder module"
2648        );
2649        let diagnostic = parts
2650            .workspace_diagnostics
2651            .iter()
2652            .find(|diagnostic| diagnostic.kind.id() == "source-read-failure")
2653            .expect("parsed session parts carry source read failure");
2654        assert_eq!(diagnostic.path, removed_path);
2655        assert!(
2656            session
2657                .current_workspace_diagnostics()
2658                .iter()
2659                .any(|diagnostic| {
2660                    diagnostic.kind.id() == "source-read-failure" && diagnostic.path == removed_path
2661                }),
2662            "session output carries parse-time source diagnostics"
2663        );
2664    }
2665
2666    const MALFORMED_PNPM_WORKSPACE_YAML: &str =
2667        "catalog:\n  react: ^18.2.0\n{this is\nnot: valid: yaml: at: all\n";
2668    const VALID_PNPM_WORKSPACE_YAML: &str = "catalog:\n  react: ^18.2.0\n";
2669
2670    fn has_diagnostic_kind(diagnostics: &[WorkspaceDiagnostic], id: &str) -> bool {
2671        diagnostics
2672            .iter()
2673            .any(|diagnostic| diagnostic.kind.id() == id)
2674    }
2675
2676    fn write_single_source_project(root: &Path, manifest: &str) {
2677        std::fs::create_dir(root.join("src")).expect("create source directory");
2678        std::fs::write(root.join("package.json"), manifest).expect("write package manifest");
2679        std::fs::write(root.join("src/index.ts"), "export const value = 1;\n")
2680            .expect("write source");
2681    }
2682
2683    /// Issue #2366: engine sessions (the MCP and LSP path) never re-stash the
2684    /// registry, so a session created after an earlier analysis in the same
2685    /// process must not keep that analysis's analysis-stage diagnostic once
2686    /// the cause is fixed: the analyze pass refreshes the entry and the
2687    /// session snapshot must not pin it.
2688    #[test]
2689    fn later_session_drops_stale_analysis_stage_diagnostic_after_cause_is_fixed() {
2690        let project = tempfile::tempdir().expect("project");
2691        let root = project.path();
2692        write_single_source_project(
2693            root,
2694            r#"{"name":"issue-2366-engine-session","private":true}"#,
2695        );
2696        std::fs::write(
2697            root.join("pnpm-workspace.yaml"),
2698            MALFORMED_PNPM_WORKSPACE_YAML,
2699        )
2700        .expect("write malformed workspace yaml");
2701
2702        let broken = AnalysisSession::load(root, None).expect("session loads");
2703        broken
2704            .analyze_dead_code()
2705            .expect("analysis on the malformed yaml succeeds");
2706        assert!(
2707            has_diagnostic_kind(
2708                &broken.current_workspace_diagnostics(),
2709                "malformed-pnpm-workspace-yaml"
2710            ),
2711            "the first session surfaces the malformed yaml: {:?}",
2712            broken.current_workspace_diagnostics()
2713        );
2714
2715        std::fs::write(root.join("pnpm-workspace.yaml"), VALID_PNPM_WORKSPACE_YAML)
2716            .expect("fix workspace yaml");
2717
2718        let fixed = AnalysisSession::load(root, None).expect("session loads");
2719        fixed
2720            .analyze_dead_code()
2721            .expect("analysis on the fixed yaml succeeds");
2722        let current = fixed.current_workspace_diagnostics();
2723        assert!(
2724            !has_diagnostic_kind(&current, "malformed-pnpm-workspace-yaml"),
2725            "a later session must not keep the stale analysis-stage entry (#2366): {current:?}"
2726        );
2727    }
2728
2729    /// Watch-mode rerun shape (issue #2366): the CLI reloads config, which
2730    /// re-stashes the workspace-discovery set, and builds a fresh session from
2731    /// the resolved config before re-analyzing. Once a text `bun.lock` exists
2732    /// the rerun must drop the bun.lockb skip diagnostic. Regression pin: the
2733    /// old stash wiped analysis-stage entries instead of preserving them, so
2734    /// this passes before and after the fix.
2735    #[test]
2736    fn watch_style_rerun_drops_bun_lockb_skip_once_text_lockfile_exists() {
2737        let project = tempfile::tempdir().expect("project");
2738        let root = project.path();
2739        write_single_source_project(
2740            root,
2741            r#"{"name":"issue-2366-watch-rerun","private":true,"overrides":{"ws":"^8.21.0"}}"#,
2742        );
2743        std::fs::write(root.join("bun.lockb"), b"placeholder binary lockfile")
2744            .expect("write bun.lockb placeholder");
2745        let config = fallow_config::FallowConfig::default().resolve(
2746            root.to_path_buf(),
2747            fallow_config::OutputFormat::Json,
2748            1,
2749            true,
2750            true,
2751            None,
2752        );
2753        let reload_config = || {
2754            let (_, diagnostics) =
2755                fallow_config::discover_workspaces_with_diagnostics(root, &config.ignore_patterns)
2756                    .expect("workspace discovery succeeds");
2757            fallow_config::stash_workspace_diagnostics(root, diagnostics);
2758        };
2759
2760        reload_config();
2761        let first =
2762            AnalysisSession::from_resolved_config(config.clone()).expect("first session loads");
2763        first
2764            .analyze_dead_code()
2765            .expect("analysis with bun.lockb only succeeds");
2766        assert!(
2767            has_diagnostic_kind(
2768                &first.current_workspace_diagnostics(),
2769                "bun-lockb-override-resolution-skipped"
2770            ),
2771            "the first run surfaces the bun.lockb skip: {:?}",
2772            first.current_workspace_diagnostics()
2773        );
2774
2775        std::fs::write(
2776            root.join("bun.lock"),
2777            r#"{"lockfileVersion":1,"workspaces":{"":{"name":"issue-2366-watch-rerun"}},"packages":{"ws":["ws@8.21.3","",{},"sha512-20"]}}"#,
2778        )
2779        .expect("write text bun.lock");
2780
2781        reload_config();
2782        let rerun =
2783            AnalysisSession::from_resolved_config(config.clone()).expect("rerun session loads");
2784        rerun
2785            .analyze_dead_code()
2786            .expect("analysis with the text bun.lock succeeds");
2787        let current = rerun.current_workspace_diagnostics();
2788        assert!(
2789            !has_diagnostic_kind(&current, "bun-lockb-override-resolution-skipped"),
2790            "the rerun drops the skip once a text bun.lock exists (#2366): {current:?}"
2791        );
2792    }
2793
2794    /// Issue #2366: `current_workspace_diagnostics` reads the registry live so
2795    /// the parse-stage and analyze-stage entries that land after the session
2796    /// was created still reach the envelope, but it must not import another
2797    /// walk's skips along with them.
2798    ///
2799    /// Combined mode runs the dead-code and duplication walks on the same root
2800    /// under `rayon::join` whenever a per-analysis `production` split stops
2801    /// them from sharing a file list, and each walk replaces the registry's
2802    /// source-discovery set. A session that read that set back would answer
2803    /// "whichever walk wrote last", which decides where the other walk's skip
2804    /// lands in the combined root's union and made the array come out in a
2805    /// different ORDER between runs of the same command.
2806    #[test]
2807    fn session_keeps_its_own_walk_skips_and_ignores_another_walks_registry_write() {
2808        let project = tempfile::tempdir().expect("project");
2809        let root = project.path();
2810        write_single_source_project(
2811            root,
2812            r#"{"name":"issue-2366-parallel-walks","private":true}"#,
2813        );
2814        std::fs::write(root.join("src/huge.ts"), "// filler\n".repeat(400))
2815            .expect("write oversized source");
2816        let mut config = fallow_config::FallowConfig::default().resolve(
2817            root.to_path_buf(),
2818            fallow_config::OutputFormat::Json,
2819            1,
2820            true,
2821            true,
2822            None,
2823        );
2824        config.max_file_size_bytes = Some(1024);
2825
2826        let session = AnalysisSession::from_resolved_config(config).expect("session loads");
2827
2828        // The state a concurrent walk leaves behind: its own skip in this
2829        // root's registry entry. It writes that through the registry's
2830        // replace-in-one-operation call, which an architecture guard reserves
2831        // for the walk itself, so the append is the stand-in here.
2832        fallow_config::append_workspace_diagnostics(
2833            root,
2834            vec![WorkspaceDiagnostic::new(
2835                root,
2836                root.join("src/other-walk-only.ts"),
2837                fallow_types::workspace::WorkspaceDiagnosticKind::SkippedLargeFile {
2838                    size_bytes: 4096,
2839                },
2840            )],
2841        );
2842
2843        let current = session.current_workspace_diagnostics();
2844        let skipped: Vec<&Path> = current
2845            .iter()
2846            .filter(|diagnostic| diagnostic.kind.id() == "skipped-large-file")
2847            .map(|diagnostic| diagnostic.path.as_path())
2848            .collect();
2849        assert_eq!(
2850            skipped.len(),
2851            1,
2852            "the session reports its own walk's skips only: {skipped:?}"
2853        );
2854        assert!(
2855            skipped[0].ends_with("src/huge.ts"),
2856            "the surviving skip is this walk's own: {skipped:?}"
2857        );
2858    }
2859
2860    /// Issue #2366: a config reload that happens AFTER the analyze pass, with
2861    /// no further pass to re-record, must not wipe the analysis-stage entry
2862    /// from the process registry. This is the long-lived-server shape: an MCP
2863    /// or LSP process analyzes once, a later request reloads config for a
2864    /// different analysis family, and a session built after that reload still
2865    /// reads the registry live. Pins the analysis-stage preserve in
2866    /// `stash_workspace_diagnostics`; without it this session reports nothing.
2867    #[test]
2868    fn config_reload_after_the_analyze_pass_keeps_the_bun_lockb_skip_readable() {
2869        let project = tempfile::tempdir().expect("project");
2870        let root = project.path();
2871        write_single_source_project(
2872            root,
2873            r#"{"name":"issue-2366-reload-preserve","private":true,"overrides":{"ws":"^8.21.0"}}"#,
2874        );
2875        std::fs::write(root.join("bun.lockb"), b"placeholder binary lockfile")
2876            .expect("write bun.lockb placeholder");
2877        let config = fallow_config::FallowConfig::default().resolve(
2878            root.to_path_buf(),
2879            fallow_config::OutputFormat::Json,
2880            1,
2881            true,
2882            true,
2883            None,
2884        );
2885        let reload_config = || {
2886            let (_, diagnostics) =
2887                fallow_config::discover_workspaces_with_diagnostics(root, &config.ignore_patterns)
2888                    .expect("workspace discovery succeeds");
2889            fallow_config::stash_workspace_diagnostics(root, diagnostics);
2890        };
2891
2892        reload_config();
2893        let analyzing =
2894            AnalysisSession::from_resolved_config(config.clone()).expect("session loads");
2895        analyzing
2896            .analyze_dead_code()
2897            .expect("analysis with bun.lockb only succeeds");
2898
2899        // A later request reloads config for another analysis family and never
2900        // runs a second dead-code pass.
2901        reload_config();
2902
2903        let later = AnalysisSession::from_resolved_config(config.clone()).expect("session loads");
2904        let current = later.current_workspace_diagnostics();
2905        assert!(
2906            has_diagnostic_kind(&current, "bun-lockb-override-resolution-skipped"),
2907            "the reload must preserve the analysis-stage entry the pass recorded (#2366): \
2908             {current:?}"
2909        );
2910    }
2911}