use std::path::Path;
pub const OVERSIZED_INDEX_BYTES: u64 = 5 * 1024 * 1024 * 1024;
#[must_use]
pub fn scope_warnings(project_path: &Path, home: Option<&Path>) -> Vec<String> {
let mut out = Vec::new();
let home_indexed = home.filter(|home| crate::tokensave::TokenSave::is_initialized(home));
if let Some(home) = home_indexed {
out.push(format!(
"Home directory is initialized as a project: {}/.tokensave/ ({}) \
— every `serve` started here indexes your whole home tree. \
Remove it with `rm -rf {}/.tokensave` unless you meant it.",
home.display(),
crate::display::format_bytes(index_size_bytes(home)),
home.display()
));
}
let is_home = home_indexed.is_some_and(|home| same_directory(home, project_path));
if !is_home && crate::tokensave::TokenSave::is_initialized(project_path) {
let size = index_size_bytes(project_path);
if size >= OVERSIZED_INDEX_BYTES {
out.push(format!(
"Index is unusually large: {} ({}) — check `exclude` globs in \
.tokensave/config.json; a `serve` maps this whole file.",
crate::display::format_bytes(size),
project_path.display()
));
}
}
out
}
pub fn warn_on_serve(project_path: &Path) {
if crate::config::load_config(project_path).is_ok_and(|c| c.suppress_scope_warning) {
return;
}
for warning in scope_warnings(project_path, crate::agents::home_dir().as_deref()) {
eprintln!("\x1b[33mwarning:\x1b[0m {warning}");
}
}
#[must_use]
pub fn index_size_bytes(project_path: &Path) -> u64 {
let dir = crate::config::get_tokensave_dir(project_path);
["tokensave.db", "tokensave.db-wal"]
.iter()
.map(|name| std::fs::metadata(dir.join(name)).map_or(0, |m| m.len()))
.sum()
}
#[must_use]
pub fn same_directory(a: &Path, b: &Path) -> bool {
match (a.canonicalize(), b.canonicalize()) {
(Ok(a), Ok(b)) => a == b,
_ => a == b,
}
}