The AI-native screen recorder. Record your screen and talk; the people you share it with and your AI agents both get it.
Rust agent-readableai-agentsarecclaude-codecodexcursorlocal-firstmacosprivacyrustscreen-recorderscreen-recordingtauritranscriptiontypescriptwindows
//! Which folders Remove model deletes for Parakeet on the Neural Engine.
//! Pure std, no Tauri, so it is tested anywhere:
//! `rustc --edition 2021 --test fluidaudio_paths.rs`.
//!
//! FluidAudio keeps every model it downloads under
//! `~/Library/Application Support/FluidAudio/Models/<folder>`, and that
//! folder is shared with any other app built on FluidAudio. Remove model must
//! only ever delete the folders of the models Clipy itself downloads: the
//! Parakeet TDT 0.6B v2, v3 or Ultra folder, and for the English model the
//! Parakeet CTC 110M keyword spotter. Folder names are FluidAudio 0.18.5's
//! `Repo.folderName` (`Sources/FluidAudio/ModelNames.swift`): the repo name
//! with `-coreml` dropped, except the CTC 110M spotter, which keeps it. The
//! `Models` folder is what `AsrModels.defaultCacheDirectory` and
//! `CtcModels.defaultCacheDirectory` resolve to. Anything else in there,
//! another app's models included, is left alone, and a folder that is a
//! symbolic link or resolves outside `Models` is refused rather than
//! followed.
use std::path::{Path, PathBuf};
/// Parakeet TDT 0.6B v2 (English), FluidAudio's `Repo.parakeetV2`.
pub const TDT_V2: &str = "parakeet-tdt-0.6b-v2";
/// Parakeet TDT 0.6B v3 (25 languages), FluidAudio's `Repo.parakeetV3`.
pub const TDT_V3: &str = "parakeet-tdt-0.6b-v3";
/// Parakeet Ultra (25 languages), FluidAudio's `Repo.parakeetUltra`.
pub const TDT_ULTRA: &str = "parakeet-ultra";
/// Parakeet CTC 110M, the keyword spotter for vocabulary boosting,
/// FluidAudio's `Repo.parakeetCtc110m`.
pub const CTC_110M: &str = "parakeet-ctc-110m-coreml";
/// The only folder names Remove model may ever delete.
const ALLOWED: [&str; 4] = [TDT_V2, TDT_V3, TDT_ULTRA, CTC_110M];
/// The English model's folders: its TDT folder and the keyword spotter only
/// it uses.
pub const ENGLISH: &[&str] = &[TDT_V2, CTC_110M];
/// The 25-language model's folder.
pub const MULTILINGUAL: &[&str] = &[TDT_V3];
/// Parakeet Ultra's folder.
pub const ULTRA: &[&str] = &[TDT_ULTRA];
/// FluidAudio's models folder under the user's Application Support folder.
pub fn models_dir(application_support: &Path) -> PathBuf {
application_support.join("FluidAudio").join("Models")
}
/// The path to delete for `folder`, `Ok(None)` when it is not there, or an
/// error when deleting it would not be safe: a name outside the allowlist,
/// a symbolic link, something that is not a folder, or a folder that does
/// not resolve to a direct child of `models_dir`.
pub fn removable(models_dir: &Path, folder: &str) -> Result<Option<PathBuf>, String> {
if !ALLOWED.contains(&folder) {
return Err(format!("{folder} is not a Clipy model folder"));
}
let path = models_dir.join(folder);
let meta = match std::fs::symlink_metadata(&path) {
Ok(meta) => meta,
Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(None),
Err(error) => return Err(format!("{}: {error}", path.display())),
};
if meta.file_type().is_symlink() {
return Err(format!("{} is a link; not following it", path.display()));
}
if !meta.is_dir() {
return Err(format!("{} is not a folder", path.display()));
}
let resolved = path
.canonicalize()
.map_err(|error| format!("{}: {error}", path.display()))?;
let parent = models_dir
.canonicalize()
.map_err(|error| format!("{}: {error}", models_dir.display()))?;
if resolved.parent() != Some(parent.as_path())
|| resolved.file_name().and_then(|name| name.to_str()) != Some(folder)
{
return Err(format!(
"{} is not inside {}",
resolved.display(),
parent.display()
));
}
Ok(Some(resolved))
}
/// Deletes the folders of one model (`ENGLISH`, `MULTILINGUAL` or `ULTRA`)
/// from `models_dir` and nothing else.
/// Every folder is checked before any is deleted, so an unsafe one stops
/// the whole removal. Returns the folders deleted.
pub fn remove_model_folders(models_dir: &Path, folders: &[&str]) -> Result<Vec<PathBuf>, String> {
let mut targets = Vec::new();
for folder in folders {
if let Some(path) = removable(models_dir, folder)? {
targets.push(path);
}
}
for path in &targets {
std::fs::remove_dir_all(path)
.map_err(|error| format!("Could not remove {}: {error}", path.display()))?;
}
Ok(targets)
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
use std::sync::atomic::{AtomicU32, Ordering};
/// A fresh Application Support stand-in with FluidAudio's models folder
/// holding Clipy's folders and another app's.
fn fixture(name: &str) -> (PathBuf, PathBuf) {
static COUNTER: AtomicU32 = AtomicU32::new(0);
let root = std::env::temp_dir().join(format!(
"clipy-fluidaudio-{name}-{}-{}",
std::process::id(),
COUNTER.fetch_add(1, Ordering::SeqCst)
));
let _ = fs::remove_dir_all(&root);
let models = models_dir(&root);
for folder in [
TDT_V2,
TDT_V3,
TDT_ULTRA,
CTC_110M,
"kokoro-82m-coreml",
"silero-vad-coreml",
] {
fs::create_dir_all(models.join(folder)).unwrap();
fs::write(models.join(folder).join("model.bin"), b"x").unwrap();
}
fs::write(models.join("README"), b"other app").unwrap();
(root, models)
}
fn present(models: &Path) -> Vec<String> {
let mut names: Vec<String> = fs::read_dir(models)
.unwrap()
.map(|entry| entry.unwrap().file_name().to_string_lossy().into_owned())
.collect();
names.sort();
names
}
#[test]
fn the_models_folder_is_fluidaudios() {
assert_eq!(
models_dir(Path::new("/Users/a/Library/Application Support")),
PathBuf::from("/Users/a/Library/Application Support/FluidAudio/Models")
);
}
#[test]
fn each_model_owns_exactly_its_folders() {
assert_eq!(ENGLISH, [TDT_V2, CTC_110M]);
assert_eq!(MULTILINGUAL, [TDT_V3]);
assert_eq!(ULTRA, [TDT_ULTRA]);
for folders in [ENGLISH, MULTILINGUAL, ULTRA] {
for folder in folders {
assert!(ALLOWED.contains(folder));
}
}
}
#[test]
fn removing_english_deletes_v2_and_the_spotter_only() {
let (root, models) = fixture("english");
let removed = remove_model_folders(&models, ENGLISH).unwrap();
assert_eq!(removed.len(), 2);
assert_eq!(
present(&models),
[
"README",
"kokoro-82m-coreml",
"parakeet-tdt-0.6b-v3",
"parakeet-ultra",
"silero-vad-coreml"
]
);
fs::remove_dir_all(root).unwrap();
}
#[test]
fn removing_the_25_language_model_keeps_the_english_one_and_the_spotter() {
let (root, models) = fixture("v3");
remove_model_folders(&models, MULTILINGUAL).unwrap();
assert_eq!(
present(&models),
[
"README",
"kokoro-82m-coreml",
"parakeet-ctc-110m-coreml",
"parakeet-tdt-0.6b-v2",
"parakeet-ultra",
"silero-vad-coreml"
]
);
fs::remove_dir_all(root).unwrap();
}
#[test]
fn removing_ultra_keeps_v2_v3_and_the_spotter() {
let (root, models) = fixture("ultra");
assert_eq!(remove_model_folders(&models, ULTRA).unwrap().len(), 1);
assert_eq!(
present(&models),
[
"README",
"kokoro-82m-coreml",
"parakeet-ctc-110m-coreml",
"parakeet-tdt-0.6b-v2",
"parakeet-tdt-0.6b-v3",
"silero-vad-coreml"
]
);
fs::remove_dir_all(root).unwrap();
}
#[test]
fn a_missing_folder_is_not_an_error() {
let (root, models) = fixture("missing");
fs::remove_dir_all(models.join(CTC_110M)).unwrap();
assert_eq!(removable(&models, CTC_110M), Ok(None));
assert_eq!(remove_model_folders(&models, ENGLISH).unwrap().len(), 1);
assert!(!models.join(TDT_V2).exists());
// No models folder at all.
assert_eq!(
remove_model_folders(&root.join("nowhere"), ENGLISH),
Ok(vec![])
);
fs::remove_dir_all(root).unwrap();
}
#[test]
fn names_outside_the_allowlist_are_refused() {
let (root, models) = fixture("allowlist");
for name in [
"kokoro-82m-coreml",
"",
".",
"..",
"../Models",
"parakeet-tdt-0.6b-v2/..",
"parakeet-tdt-0.6b-v2-coreml",
] {
assert!(removable(&models, name).is_err(), "{name:?}");
}
assert!(models.join("kokoro-82m-coreml").exists());
fs::remove_dir_all(root).unwrap();
}
#[cfg(unix)]
#[test]
fn a_linked_folder_is_refused_and_its_target_kept() {
let (root, models) = fixture("link");
let elsewhere = root.join("elsewhere");
fs::create_dir_all(&elsewhere).unwrap();
fs::write(elsewhere.join("keep.bin"), b"x").unwrap();
fs::remove_dir_all(models.join(TDT_V2)).unwrap();
std::os::unix::fs::symlink(&elsewhere, models.join(TDT_V2)).unwrap();
assert!(remove_model_folders(&models, ENGLISH).is_err());
assert!(elsewhere.join("keep.bin").exists());
// Nothing is deleted when one folder is unsafe, the spotter included.
assert!(models.join(CTC_110M).exists());
fs::remove_dir_all(root).unwrap();
}
#[test]
fn a_file_where_a_folder_should_be_is_refused() {
let (root, models) = fixture("file");
fs::remove_dir_all(models.join(TDT_V3)).unwrap();
fs::write(models.join(TDT_V3), b"x").unwrap();
assert!(removable(&models, TDT_V3).is_err());
assert!(models.join(TDT_V3).exists());
fs::remove_dir_all(root).unwrap();
}
}