use std::time::{Duration, SystemTime};
use crate::domain::LanguageId;
use crate::domain::index::{AdmissionTier, SkipReason};
use crate::watcher_state::{WatcherInfo, WatcherState};
use super::query::normalize_path_query;
use super::search::{NoiseClass, NoisePolicy};
use super::store::{IndexState, IndexedFile, LiveIndex, ParseStatus};
pub struct HealthStats {
pub file_count: usize,
pub symbol_count: usize,
pub parsed_count: usize,
pub partial_parse_count: usize,
pub unexpected_partial_parse_count: usize,
pub expected_vendor_partial_parse_count: usize,
pub expected_framework_partial_parse_count: usize,
pub expected_language_partial_parse_count: usize,
pub failed_count: usize,
pub load_duration: Duration,
pub watcher_state: WatcherState,
pub events_processed: u64,
pub last_event_at: Option<SystemTime>,
pub debounce_window_ms: u64,
pub overflow_count: u64,
pub last_overflow_at: Option<SystemTime>,
pub stale_files_found: u64,
pub last_reconcile_at: Option<SystemTime>,
pub partial_parse_files: Vec<String>,
pub unexpected_partial_parse_files: Vec<String>,
pub expected_vendor_partial_parse_files: Vec<String>,
pub expected_framework_partial_parse_files: Vec<String>,
pub expected_language_partial_parse_files: Vec<String>,
pub failed_files: Vec<(String, String)>,
pub tier_counts: (usize, usize, usize),
pub local_empty_reason: Option<String>,
pub untracked_indexed: usize,
}
pub const EXPECTED_VENDOR_PARTIAL_PARSE_REASON: &str =
"expected vendor: tree-sitter-scss C/header parser limitation";
pub const EXPECTED_FRAMEWORK_PARTIAL_PARSE_REASON: &str = "expected framework: Angular template control-flow not supported by tree-sitter-html; \
symbols extracted best-effort";
pub const EXPECTED_LANGUAGE_PARTIAL_PARSE_REASON: &str = "expected language: TypeScript import-type array (import('mod').Member[]) not supported by \
tree-sitter-typescript 0.23.2; symbols extracted best-effort";
const ANGULAR_CONTROL_FLOW_KEYWORDS: [&str; 4] = ["@if", "@for", "@switch", "@defer"];
pub(crate) fn is_expected_framework_partial_parse(file: &IndexedFile) -> bool {
use crate::domain::SymbolKind;
if !matches!(file.language, LanguageId::Html) {
return false;
}
if !matches!(file.parse_status, ParseStatus::PartialParse { .. }) {
return false;
}
let has_angular_control_flow_symbol = file.symbols.iter().any(|s| {
s.kind == SymbolKind::Module && ANGULAR_CONTROL_FLOW_KEYWORDS.contains(&s.name.as_str())
});
if !has_angular_control_flow_symbol {
return false;
}
crate::parsing::is_expected_angular_template_control_flow_limitation(
&file.language,
&file.content,
)
}
fn is_expected_vendor_partial_parse_noise(
path: &str,
file: &IndexedFile,
gitignore: Option<&ignore::gitignore::Gitignore>,
) -> bool {
let normalized = path.replace('\\', "/").to_ascii_lowercase();
let is_tree_sitter_scss_c_or_header = normalized.starts_with("vendor/tree-sitter-scss/src/")
&& (normalized.ends_with(".c") || normalized.ends_with(".h"));
is_tree_sitter_scss_c_or_header
&& file.classification.is_vendor
&& matches!(file.language, LanguageId::C | LanguageId::Cpp)
&& matches!(
NoisePolicy::classify_path(path, gitignore),
NoiseClass::Vendor
)
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AdmissionTierLookupView {
pub tier: AdmissionTier,
pub path: String,
pub size: Option<u64>,
pub extension: Option<String>,
pub language: Option<LanguageId>,
pub reason: Option<SkipReason>,
}
impl LiveIndex {
pub fn capture_admission_tier_lookup_view(
&self,
relative_path: &str,
) -> Option<AdmissionTierLookupView> {
let path = normalize_path_query(relative_path);
if let Some(file) = self.files.get(&path) {
return Some(AdmissionTierLookupView {
tier: AdmissionTier::Normal,
path: file.relative_path.clone(),
size: Some(file.byte_len),
extension: None,
language: Some(file.language.clone()),
reason: None,
});
}
self.skipped_files
.iter()
.find(|skipped| normalize_path_query(&skipped.path) == path)
.map(|skipped| AdmissionTierLookupView {
tier: skipped.tier(),
path: skipped.path.clone(),
size: Some(skipped.size),
extension: skipped.extension.clone(),
language: None,
reason: skipped.reason(),
})
}
pub fn file_count(&self) -> usize {
self.files.len()
}
pub fn symbol_count(&self) -> usize {
self.files.values().map(|f| f.symbols.len()).sum()
}
pub fn is_ready(&self) -> bool {
if self.is_empty {
return false;
}
!self.cb_state.is_tripped()
}
pub fn index_state(&self) -> IndexState {
if self.is_empty {
return IndexState::Empty;
}
if self.cb_state.is_tripped() {
IndexState::CircuitBreakerTripped {
summary: self.cb_state.summary(),
}
} else {
IndexState::Ready
}
}
pub fn loaded_at_system(&self) -> SystemTime {
self.loaded_at_system
}
fn count_untracked_indexed(&self) -> usize {
let Some(root) = self.indexed_root.as_ref() else {
return 0;
};
let Ok(git_repo) = crate::git::GitRepo::open(root) else {
return 0;
};
let Ok(tracked) = git_repo.tracked_paths() else {
return 0;
};
if tracked.is_empty() {
return 0;
}
let tracked_set: std::collections::HashSet<&str> =
tracked.iter().map(|p| p.as_str()).collect();
self.files
.keys()
.filter(|path| !tracked_set.contains(path.as_str()) && !self.is_path_gitignored(path))
.count()
}
pub fn health_stats(&self) -> HealthStats {
let mut parsed_count = 0usize;
let mut partial_parse_count = 0usize;
let mut failed_count = 0usize;
let mut symbol_count = 0usize;
for file in self.files.values() {
symbol_count += file.symbols.len();
match &file.parse_status {
ParseStatus::Parsed => parsed_count += 1,
ParseStatus::PartialParse { .. } => partial_parse_count += 1,
ParseStatus::Failed { .. } => failed_count += 1,
}
}
let mut partial_parse_files = Vec::new();
let mut unexpected_partial_parse_files = Vec::new();
let mut expected_vendor_partial_parse_files = Vec::new();
let mut expected_framework_partial_parse_files = Vec::new();
let mut expected_language_partial_parse_files = Vec::new();
for (path, file) in &self.files {
if matches!(file.parse_status, ParseStatus::PartialParse { .. }) {
partial_parse_files.push(path.clone());
if is_expected_vendor_partial_parse_noise(path, file, self.gitignore.as_ref()) {
expected_vendor_partial_parse_files.push(path.clone());
} else if is_expected_framework_partial_parse(file) {
expected_framework_partial_parse_files.push(path.clone());
} else if crate::parsing::is_expected_typescript_import_type_array_limitation(
&file.language,
&file.content,
crate::domain::LanguageId::is_tsx_path(path),
) {
expected_language_partial_parse_files.push(path.clone());
} else {
unexpected_partial_parse_files.push(path.clone());
}
}
}
partial_parse_files.sort();
partial_parse_files.dedup();
unexpected_partial_parse_files.sort();
unexpected_partial_parse_files.dedup();
expected_vendor_partial_parse_files.sort();
expected_vendor_partial_parse_files.dedup();
expected_framework_partial_parse_files.sort();
expected_framework_partial_parse_files.dedup();
expected_language_partial_parse_files.sort();
expected_language_partial_parse_files.dedup();
let mut failed_files: Vec<(String, String)> = self
.files
.iter()
.filter_map(|(path, f)| {
if let ParseStatus::Failed { error } = &f.parse_status {
Some((path.clone(), error.clone()))
} else {
None
}
})
.collect();
failed_files.sort_by(|a, b| a.0.cmp(&b.0));
HealthStats {
file_count: self.files.len(),
symbol_count,
parsed_count,
partial_parse_count,
unexpected_partial_parse_count: unexpected_partial_parse_files.len(),
expected_vendor_partial_parse_count: expected_vendor_partial_parse_files.len(),
expected_framework_partial_parse_count: expected_framework_partial_parse_files.len(),
expected_language_partial_parse_count: expected_language_partial_parse_files.len(),
failed_count,
load_duration: self.load_duration,
watcher_state: WatcherState::Off,
events_processed: 0,
last_event_at: None,
debounce_window_ms: 200,
overflow_count: 0,
last_overflow_at: None,
stale_files_found: 0,
last_reconcile_at: None,
partial_parse_files,
unexpected_partial_parse_files,
expected_vendor_partial_parse_files,
expected_framework_partial_parse_files,
expected_language_partial_parse_files,
failed_files,
tier_counts: self.tier_counts(),
local_empty_reason: self.local_empty_reason(),
untracked_indexed: self.count_untracked_indexed(),
}
}
pub fn health_stats_with_watcher(&self, watcher: &WatcherInfo) -> HealthStats {
let mut stats = self.health_stats();
stats.watcher_state = watcher.state.clone();
stats.events_processed = watcher.events_processed;
stats.last_event_at = watcher.last_event_at;
stats.debounce_window_ms = watcher.debounce_window_ms;
stats.overflow_count = watcher.overflow_count;
stats.last_overflow_at = watcher.last_overflow_at;
stats.stale_files_found = watcher.stale_files_found;
stats.last_reconcile_at = watcher.last_reconcile_at;
stats
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::domain::FileClassification;
use crate::live_index::store::{IndexedFile, ParseStatus};
use std::sync::Arc;
fn partial_ts_file(path: &str, content: &str) -> IndexedFile {
IndexedFile {
relative_path: path.to_string(),
language: LanguageId::TypeScript,
classification: FileClassification::for_code_path(path),
content: content.as_bytes().to_vec(),
symbols: Vec::new(),
parse_status: ParseStatus::PartialParse {
warning: "syntax error".to_string(),
},
parse_diagnostic: None,
byte_len: content.len() as u64,
content_hash: "test-hash".to_string(),
references: Vec::new(),
alias_map: std::collections::HashMap::new(),
mtime_secs: 0,
}
}
fn index_with_files(files: Vec<IndexedFile>) -> LiveIndex {
let mut index = LiveIndex::empty_live_index();
index.is_empty = false;
for file in files {
index
.files
.insert(file.relative_path.clone(), Arc::new(file));
}
index
}
#[test]
fn sf003_excused_partial_is_accounted_in_quarantine_registry() {
let excused = partial_ts_file("src/app.ts", "type S = import('rxjs').Subscription[];");
let broken = partial_ts_file("src/broken.ts", "function f( { return ;");
let stats = index_with_files(vec![excused, broken]).health_stats();
assert_eq!(
stats.partial_parse_count, 2,
"both files are ParseStatus::PartialParse"
);
let registry_total = stats.unexpected_partial_parse_count
+ stats.expected_vendor_partial_parse_count
+ stats.expected_framework_partial_parse_count
+ stats.expected_language_partial_parse_count
+ stats.failed_count;
assert_eq!(
registry_total,
stats.partial_parse_count + stats.failed_count,
"registry total must account for every partial parse (header={}, \
unexpected={}, vendor={}, framework={}, language={}, failed={})",
stats.partial_parse_count,
stats.unexpected_partial_parse_count,
stats.expected_vendor_partial_parse_count,
stats.expected_framework_partial_parse_count,
stats.expected_language_partial_parse_count,
stats.failed_count,
);
assert_eq!(
stats.expected_language_partial_parse_count, 1,
"the SF-003 import-type-array file is an expected language partial"
);
assert!(
stats
.expected_language_partial_parse_files
.contains(&"src/app.ts".to_string()),
"the SF-003 file must appear in the expected-language partial list"
);
assert_eq!(stats.unexpected_partial_parse_count, 1);
assert!(
stats
.unexpected_partial_parse_files
.contains(&"src/broken.ts".to_string()),
"the genuinely broken file stays an unexpected partial"
);
}
}