Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

perf(desktop): keep macOS Runtime trees out of Spotlight (#2304)

Call White8be72be88869
diff --git a/desktop/runtime-host/src/private_runtime.rs b/desktop/runtime-host/src/private_runtime.rs
index 894f9edb7..9fe2ba428 100644
--- a/desktop/runtime-host/src/private_runtime.rs
+++ b/desktop/runtime-host/src/private_runtime.rs
@@ -6,6 +6,7 @@
//! user-data directory. The application bundle is never mutated, and an update
//! can install a successor without replacing files used by a running daemon.
+use std::ffi::OsStr;
use std::fs::{self, File, OpenOptions};
use std::io::{self, Read, Write};
use std::path::{Component, Path, PathBuf};
@@ -23,6 +24,25 @@ const MAX_UNPACKED_BYTES: u64 = 2 * 1024 * 1024 * 1024;
const MAX_ARCHIVE_ENTRIES: u64 = 200_000;
const TREE_HASH_DOMAIN: &[u8] = b"avibe-runtime-tree-v1\0";
const REPAIR_SLOT_SUFFIX: &str = "-repair";
+/// The directory inside the install root that holds every Runtime tree, and
+/// the staging and discarded copies on their way in and out.
+///
+/// On macOS its name ends in `.noindex`, which keeps Spotlight out of the
+/// whole subtree. Spotlight otherwise imports each of a new tree's ~31k files
+/// for minutes after an install or update, and its importer pushes the tree
+/// out of the page cache as it goes. Every launch hashes the whole tree before
+/// it runs any of it, so a launch in those minutes reads the tree back from
+/// disk instead of memory (#2283). macOS 26 still honours the suffix; a
+/// `.metadata_never_index` file inside the directory is ignored.
+///
+/// Windows keeps its trees directly in the install root. The suffix means
+/// nothing there, and the extra level would cost path length the tree does
+/// not have: its deepest entry is 168 characters, which already brings a
+/// typical profile's absolute path within about ten characters of `MAX_PATH`.
+#[cfg(target_os = "macos")]
+const TREES_DIR_NAME: Option<&str> = Some("trees.noindex");
+#[cfg(not(target_os = "macos"))]
+const TREES_DIR_NAME: Option<&str> = None;
static INSTALL_SEQUENCE: AtomicU64 = AtomicU64::new(0);
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
@@ -77,6 +97,11 @@ pub enum PrivateRuntimeError {
ArchiveInvalid,
#[error("the private Runtime could not be installed")]
Install(#[source] io::Error),
+ /// A link or a file where the installer needs its own directory. Replacing
+ /// it with a real directory is enough: the installer re-creates what the
+ /// directory holds.
+ #[error("{0} is a link or a file, not a directory the private Runtime owns")]
+ NotARealDirectory(PathBuf),
}
#[derive(Debug, Clone)]
@@ -99,13 +124,18 @@ pub struct InstalledPrivateRuntime {
pub struct PrivateRuntimeBundle {
bundle_dir: PathBuf,
install_root: PathBuf,
+ /// Where trees are installed: `install_root` itself, or its
+ /// [`TREES_DIR_NAME`] directory.
+ trees_root: PathBuf,
}
impl PrivateRuntimeBundle {
pub fn new(bundle_dir: PathBuf, install_root: PathBuf) -> Self {
+ let trees_root = TREES_DIR_NAME.map_or_else(|| install_root.clone(), |name| install_root.join(name));
Self {
bundle_dir,
install_root,
+ trees_root,
}
}
@@ -117,7 +147,12 @@ impl PrivateRuntimeBundle {
.archive_sha256
.get(..16)
.ok_or(PrivateRuntimeError::ManifestInvalid)?;
- let version_dir = self.install_root.join(&manifest.runtime_version);
+ let version_dir = self.trees_root.join(&manifest.runtime_version);
+ // Everything below reads, writes and deletes inside these three, so
+ // none of them may be a link to somewhere else.
+ for directory in [&self.install_root, &self.trees_root, &version_dir] {
+ own_directory(directory)?;
+ }
let [primary_dir, repair_dir] = install_slots(&version_dir, digest_prefix);
for candidate in [&primary_dir, &repair_dir] {
if path_present(candidate) {
@@ -132,7 +167,6 @@ impl PrivateRuntimeBundle {
let archive_path = self.bundle_dir.join(&manifest.archive);
verify_archive(&archive_path, &manifest)?;
- fs::create_dir_all(&self.install_root).map_err(PrivateRuntimeError::Install)?;
// Both independently installed copies failing integrity validation is
// not a dead end: the verified archive is reinstalled over the primary
// name. A daemon may still be running from that copy, since a tree that
@@ -147,7 +181,7 @@ impl PrivateRuntimeBundle {
let sequence = INSTALL_SEQUENCE.fetch_add(1, Ordering::SeqCst);
let staging = self
- .install_root
+ .trees_root
.join(format!(".install-{}-{sequence}", std::process::id()));
if staging.exists() {
fs::remove_dir_all(&staging).map_err(PrivateRuntimeError::Install)?;
@@ -159,11 +193,8 @@ impl PrivateRuntimeBundle {
validate_runtime_files(&staging, &manifest)?;
verify_installed_tree(&staging, &manifest)?;
write_marker(&staging, &manifest)?;
- if let Some(parent) = install_dir.parent() {
- fs::create_dir_all(parent).map_err(PrivateRuntimeError::Install)?;
- }
if replaces_primary {
- discard_install_path(&self.install_root, &install_dir)?;
+ discard_install_path(&self.trees_root, &install_dir)?;
}
match fs::rename(&staging, &install_dir) {
Ok(()) => {}
@@ -200,6 +231,10 @@ impl PrivateRuntimeBundle {
/// so a daemon started from the primary can still be serving after this
/// launch resolved the repair slot. Both slots of the served id are
/// therefore kept; every other version and digest is removed.
+ ///
+ /// Releases before [`TREES_DIR_NAME`] installed trees directly in the
+ /// install root. Those are superseded by the same rule: whatever is not a
+ /// slot of the served id goes.
pub fn prune_superseded(&self, active_root: &Path) -> Result<(), PrivateRuntimeError> {
let outside_root = || {
PrivateRuntimeError::Install(io::Error::new(
@@ -209,32 +244,28 @@ impl PrivateRuntimeBundle {
};
let active_version = active_root
.parent()
- .filter(|parent| parent.parent() == Some(self.install_root.as_path()))
+ .filter(|parent| parent.parent() == Some(self.trees_root.as_path()))
+ .and_then(Path::file_name)
.ok_or_else(outside_root)?;
let active_slot = active_root
.file_name()
.and_then(|name| name.to_str())
.ok_or_else(outside_root)?;
let digest_prefix = active_slot.strip_suffix(REPAIR_SLOT_SUFFIX).unwrap_or(active_slot);
- let served_slots = install_slots(active_version, digest_prefix);
- let entries = match fs::read_dir(&self.install_root) {
- Ok(entries) => entries,
- Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(()),
- Err(error) => return Err(PrivateRuntimeError::Install(error)),
- };
- for entry in entries {
- let path = entry.map_err(PrivateRuntimeError::Install)?.path();
- if path == active_version {
- for candidate in fs::read_dir(&path).map_err(PrivateRuntimeError::Install)? {
- let candidate = candidate.map_err(PrivateRuntimeError::Install)?.path();
- if !served_slots.contains(&candidate) {
- remove_install_path(&candidate)?;
- }
- }
- } else {
- remove_install_path(&path)?;
- }
+ // The trees root is reached through the install root, and a link there
+ // would point the deletions below at whatever it names.
+ if !real_directory(&self.install_root)? {
+ return Ok(());
+ }
+ prune_trees(&self.trees_root, active_version, digest_prefix, None)?;
+ if self.trees_root != self.install_root {
+ prune_trees(
+ &self.install_root,
+ active_version,
+ digest_prefix,
+ Some(&self.trees_root),
+ )?;
}
Ok(())
}
@@ -242,16 +273,8 @@ impl PrivateRuntimeBundle {
/// Removes every app-private Runtime install while leaving Avibe user state
/// untouched. The caller must first complete a graceful `vibe stop`.
pub fn remove_all(&self) -> Result<(), PrivateRuntimeError> {
- let metadata = match fs::symlink_metadata(&self.install_root) {
- Ok(metadata) => metadata,
- Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(()),
- Err(error) => return Err(PrivateRuntimeError::Install(error)),
- };
- if !metadata.file_type().is_dir() || metadata.file_type().is_symlink() {
- return Err(PrivateRuntimeError::Install(io::Error::new(
- io::ErrorKind::InvalidInput,
- "private Runtime install root is not a directory",
- )));
+ if !real_directory(&self.install_root)? {
+ return Ok(());
}
fs::remove_dir_all(&self.install_root).map_err(PrivateRuntimeError::Install)
}
@@ -365,9 +388,9 @@ fn install_slots(version_dir: &Path, digest_prefix: &str) -> [PathBuf; 2] {
/// The discarded copy is then removed on a best-effort basis: it is already
/// unreachable by name, and `prune_superseded` sweeps whatever is left after
/// the next successful launch.
-fn discard_install_path(install_root: &Path, path: &Path) -> Result<(), PrivateRuntimeError> {
+fn discard_install_path(trees_root: &Path, path: &Path) -> Result<(), PrivateRuntimeError> {
let sequence = INSTALL_SEQUENCE.fetch_add(1, Ordering::SeqCst);
- let discarded = install_root.join(format!(".discard-{}-{sequence}", std::process::id()));
+ let discarded = trees_root.join(format!(".discard-{}-{sequence}", std::process::id()));
// A machine that cannot even rename the directory reports what it reported
// before this became recoverable, rather than pretending it installed.
fs::rename(path, &discarded).map_err(|_| PrivateRuntimeError::ArchiveVerification)?;
@@ -375,6 +398,67 @@ fn discard_install_path(install_root: &Path, path: &Path) -> Result<(), PrivateR
Ok(())
}
+/// Whether `path` is a directory itself, not a link to one. Absent is `false`;
+/// anything else there is an error, so a caller about to write or delete inside
+/// a directory never follows a link out of the private install.
+fn real_directory(path: &Path) -> Result<bool, PrivateRuntimeError> {
+ match fs::symlink_metadata(path) {
+ Ok(metadata) if metadata.file_type().is_dir() => Ok(true),
+ Ok(_) => Err(PrivateRuntimeError::NotARealDirectory(path.to_owned())),
+ Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(false),
+ Err(error) => Err(PrivateRuntimeError::Install(error)),
+ }
+}
+
+/// Creates `path` if it is absent and requires it to be a real directory.
+///
+/// Its ancestors are not checked: above the install root they belong to the
+/// user and the system, and below it each level is owned on the way down.
+fn own_directory(path: &Path) -> Result<(), PrivateRuntimeError> {
+ if !real_directory(path)? {
+ fs::create_dir_all(path).map_err(PrivateRuntimeError::Install)?;
+ if !real_directory(path)? {
+ return Err(PrivateRuntimeError::NotARealDirectory(path.to_owned()));
+ }
+ }
+ Ok(())
+}
+
+/// Removes every entry of `root` except the two slots of the served id and
+/// `keep`.
+fn prune_trees(
+ root: &Path,
+ active_version: &OsStr,
+ digest_prefix: &str,
+ keep: Option<&Path>,
+) -> Result<(), PrivateRuntimeError> {
+ if !real_directory(root)? {
+ return Ok(());
+ }
+ let active_version = root.join(active_version);
+ let served_slots = install_slots(&active_version, digest_prefix);
+ for entry in fs::read_dir(root).map_err(PrivateRuntimeError::Install)? {
+ let path = entry.map_err(PrivateRuntimeError::Install)?.path();
+ if keep == Some(path.as_path()) {
+ continue;
+ }
+ if path == active_version {
+ if !real_directory(&path)? {
+ continue;
+ }
+ for candidate in fs::read_dir(&path).map_err(PrivateRuntimeError::Install)? {
+ let candidate = candidate.map_err(PrivateRuntimeError::Install)?.path();
+ if !served_slots.contains(&candidate) {
+ remove_install_path(&candidate)?;
+ }
+ }
+ } else {
+ remove_install_path(&path)?;
+ }
+ }
+ Ok(())
+}
+
fn remove_install_path(path: &Path) -> Result<(), PrivateRuntimeError> {
let metadata = fs::symlink_metadata(path).map_err(PrivateRuntimeError::Install)?;
if metadata.file_type().is_dir() && !metadata.file_type().is_symlink() {
@@ -726,12 +810,9 @@ mod tests {
assert!(first.python.is_file());
assert!(first.node.is_file());
assert!(first.npm_cli.is_file());
- assert_eq!(
- fs::read_dir(root.join("installs").join(&manifest.runtime_version))
- .expect("version directory")
- .count(),
- 1
- );
+ let version_dir = first.root.parent().expect("version directory");
+ assert!(version_dir.ends_with(&manifest.runtime_version));
+ assert_eq!(fs::read_dir(version_dir).expect("version directory").count(), 1);
fs::remove_dir_all(root).ok();
}
@@ -845,16 +926,16 @@ mod tests {
fn superseded_installs_are_pruned_only_after_an_active_runtime_is_selected() {
let root = scratch("prune");
write_bundle(&root, None);
- let install_root = root.join("installs");
- let bundle = PrivateRuntimeBundle::new(root.join("bundle"), install_root.clone());
+ let bundle = PrivateRuntimeBundle::new(root.join("bundle"), root.join("installs"));
let active = bundle.prepare().expect("active install");
+ let trees_root = active.root.parent().and_then(Path::parent).expect("trees root");
let other_digest = active.root.with_file_name("0123456789abcdef");
fs::create_dir_all(&other_digest).expect("other digest install");
fs::write(other_digest.join("unused"), b"unused").expect("other digest file");
- let old = install_root.join("2.9.0").join("old-digest");
+ let old = trees_root.join("2.9.0").join("old-digest");
fs::create_dir_all(&old).expect("old install");
fs::write(old.join("unused"), b"unused").expect("old file");
- let staging = install_root.join(".install-abandoned");
+ let staging = trees_root.join(".install-abandoned");
fs::create_dir_all(&staging).expect("staging install");
bundle.prune_superseded(&active.root).expect("prune succeeds");
@@ -874,10 +955,15 @@ mod tests {
fn pruning_keeps_both_slots_of_the_served_runtime() {
let root = scratch("prune-served-slots");
write_bundle(&root, None);
- let install_root = root.join("installs");
- let bundle = PrivateRuntimeBundle::new(root.join("bundle"), install_root.clone());
+ let bundle = PrivateRuntimeBundle::new(root.join("bundle"), root.join("installs"));
let primary = bundle.prepare().expect("primary install");
- let old = install_root.join("2.9.0").join("old-digest");
+ let old = primary
+ .root
+ .parent()
+ .and_then(Path::parent)
+ .expect("trees root")
+ .join("2.9.0")
+ .join("old-digest");
fs::create_dir_all(&old).expect("old install");
// What a daemon running from the primary leaves behind: a file written
// into its own tree, which makes the next launch repair.
@@ -902,6 +988,147 @@ mod tests {
fs::remove_dir_all(root).ok();
}
+ /// Spotlight skips a directory whose name ends in `.noindex`, subtree
+ /// included. Trees installed anywhere else get imported file by file after
+ /// every install, and the importer evicts them from the page cache that the
+ /// next launch's verification reads them from.
+ #[cfg(target_os = "macos")]
+ #[test]
+ fn macos_installs_trees_only_inside_a_noindex_directory() {
+ let root = scratch("noindex");
+ write_bundle(&root, None);
+ let install_root = root.join("installs");
+ let bundle = PrivateRuntimeBundle::new(root.join("bundle"), install_root.clone());
+
+ let installed = bundle.prepare().expect("install");
+
+ let entries: Vec<PathBuf> = fs::read_dir(&install_root)
+ .expect("install root")
+ .map(|entry| entry.expect("entry").path())
+ .collect();
+ assert_eq!(entries.len(), 1, "the install root holds one directory: {entries:?}");
+ assert!(entries[0].extension().is_some_and(|extension| extension == "noindex"));
+ assert!(installed.root.starts_with(&entries[0]));
+ fs::remove_dir_all(root).ok();
+ }
+
+ /// Releases before the `.noindex` directory left their trees directly in
+ /// the install root. Once this bundle's id is served they are superseded by
+ /// the same rule as any other tree: both slots of the served id are kept,
+ /// wherever a daemon might have started from them, and nothing else is.
+ #[cfg(target_os = "macos")]
+ #[test]
+ fn trees_an_earlier_release_left_in_the_install_root_are_pruned() {
+ let root = scratch("prune-install-root");
+ let manifest = write_bundle(&root, None);
+ let install_root = root.join("installs");
+ let bundle = PrivateRuntimeBundle::new(root.join("bundle"), install_root.clone());
+ let active = bundle.prepare().expect("active install");
+ let [served_primary, served_repair] = install_slots(
+ &install_root.join(&manifest.runtime_version),
+ &manifest.archive_sha256[..16],
+ );
+ let other_digest = install_root.join(&manifest.runtime_version).join("0123456789abcdef");
+ let old = install_root.join("2.9.0").join("old-digest");
+ let staging = install_root.join(".install-1-0");
+ for tree in [&served_primary, &served_repair, &other_digest, &old, &staging] {
+ fs::create_dir_all(tree).expect("earlier tree");
+ fs::write(tree.join("python3"), b"earlier").expect("earlier file");
+ }
+
+ bundle.prune_superseded(&active.root).expect("prune succeeds");
+
+ assert!(active.python.is_file());
+ assert!(served_primary.join("python3").is_file());
+ assert!(served_repair.join("python3").is_file());
+ assert!(!other_digest.exists());
+ assert!(!old.exists());
+ assert!(!staging.exists());
+ fs::remove_dir_all(root).ok();
+ }
+
+ /// The installer owns every directory from the install root down to a
+ /// version, and lists, writes and deletes inside each. A link in place of
+ /// any of them is refused by prepare and by prune, and whatever it names is
+ /// left as it was; a missing one is created as on a first install.
+ #[cfg(unix)]
+ #[test]
+ fn a_link_in_place_of_an_install_directory_is_refused() {
+ use std::os::unix::fs::symlink;
+
+ fn names(directory: &Path) -> Vec<String> {
+ let mut names: Vec<String> = fs::read_dir(directory)
+ .expect("directory")
+ .map(|entry| entry.expect("entry").file_name().to_string_lossy().into_owned())
+ .collect();
+ names.sort();
+ names
+ }
+ // An installed bundle and one of its directories, by level.
+ fn installed(
+ label: &str,
+ level: &str,
+ ) -> Option<(PathBuf, PrivateRuntimeBundle, InstalledPrivateRuntime, PathBuf)> {
+ let root = scratch(label);
+ write_bundle(&root, None);
+ let install_root = root.join("installs");
+ let bundle = PrivateRuntimeBundle::new(root.join("bundle"), install_root.clone());
+ let active = bundle.prepare().expect("install");
+ let version_dir = active.root.parent().expect("version directory").to_owned();
+ let directory = match level {
+ "install root" => install_root.clone(),
+ "trees directory" => version_dir.parent().expect("trees directory").to_owned(),
+ _ => version_dir,
+ };
+ if level == "trees directory" && directory == install_root {
+ // Outside macOS the trees sit directly in the install root.
+ fs::remove_dir_all(root).ok();
+ return None;
+ }
+ Some((root, bundle, active, directory))
+ }
+
+ for level in ["install root", "trees directory", "version directory"] {
+ for operation in ["prepare", "prune"] {
+ let Some((root, bundle, active, directory)) = installed("install-link", level) else {
+ continue;
+ };
+ let outside = root.join("outside");
+ fs::create_dir_all(outside.join("documents")).expect("outside directory");
+ fs::write(outside.join("keep-me"), b"user file").expect("outside file");
+ fs::rename(&directory, root.join("moved-aside")).expect("move the directory aside");
+ symlink(&outside, &directory).expect("link in its place");
+
+ let result = match operation {
+ "prepare" => bundle.prepare().map(|_| ()),
+ _ => bundle.prune_superseded(&active.root),
+ };
+
+ assert!(
+ matches!(&result, Err(PrivateRuntimeError::NotARealDirectory(path)) if *path == directory),
+ "{level} {operation}: {result:?}"
+ );
+ assert_eq!(names(&outside), ["documents", "keep-me"], "{level} {operation}");
+ assert!(names(&outside.join("documents")).is_empty(), "{level} {operation}");
+ assert_eq!(fs::read(outside.join("keep-me")).expect("outside file"), b"user file");
+ fs::remove_dir_all(root).ok();
+ }
+
+ let Some((root, bundle, _, directory)) = installed("install-missing", level) else {
+ continue;
+ };
+ fs::remove_dir_all(&directory).expect("remove the directory");
+ let reinstalled = bundle.prepare().expect("a missing directory is created");
+ assert!(!reinstalled.reused, "{level}");
+ assert!(reinstalled.python.is_file(), "{level}");
+ assert!(
+ fs::symlink_metadata(&directory).expect("created").file_type().is_dir(),
+ "{level}"
+ );
+ fs::remove_dir_all(root).ok();
+ }
+ }
+
#[test]
fn explicit_removal_deletes_only_the_private_runtime_root() {
let root = scratch("remove-all");
@@ -931,11 +1158,11 @@ mod tests {
fn two_tampered_install_slots_are_reinstalled_rather_than_stranded() {
let root = scratch("installed-double-tamper");
let manifest = write_bundle(&root, None);
- let install_root = root.join("installs");
- let version_dir = install_root.join(&manifest.runtime_version);
- let bundle = PrivateRuntimeBundle::new(root.join("bundle"), install_root.clone());
+ let bundle = PrivateRuntimeBundle::new(root.join("bundle"), root.join("installs"));
let primary = bundle.prepare().expect("primary install");
+ let version_dir = primary.root.parent().expect("version directory").to_owned();
+ let trees_root = version_dir.parent().expect("trees root").to_owned();
fs::write(&primary.python, b"tampered").expect("tamper primary");
let repair = bundle.prepare().expect("repair install");
fs::write(&repair.node, b"tampered").expect("tamper repair");
@@ -975,8 +1202,8 @@ mod tests {
"round {round} left more than the two install slots"
);
assert_eq!(
- fs::read_dir(&install_root)
- .expect("install root")
+ fs::read_dir(&trees_root)
+ .expect("trees root")
.filter(|entry| {
let name = entry.as_ref().expect("entry").file_name();
name.to_string_lossy().starts_with(".discard-")
diff --git a/docs/plans/tauri-desktop-vertical-slice.md b/docs/plans/tauri-desktop-vertical-slice.md
index e4b64f57e..92052e09b 100644
--- a/docs/plans/tauri-desktop-vertical-slice.md
+++ b/docs/plans/tauri-desktop-vertical-slice.md
@@ -495,9 +495,15 @@ staging directory. Only a complete install with the Python, Node, and npm
entrypoints and a durable marker is atomically renamed to:
```text
-<OS application data>/runtime/<avibe-version>/<archive-sha-prefix>/
+<OS application data>/runtime/<avibe-version>/<archive-sha-prefix>/ (Windows)
+<OS application data>/runtime/trees.noindex/<avibe-version>/<archive-sha-prefix>/ (macOS)
```
+On macOS the `.noindex` directory keeps Spotlight from importing a freshly
+written tree, which otherwise evicts it from the page cache just before the
+next launch hashes it (#2283). Trees that earlier releases installed directly
+under `runtime/` are pruned like any other superseded tree.
+
Every later launch recomputes the extracted file-tree digest before executing
the Runtime, rejects symlinks and extra/missing files, and may install one
separate repair slot from the immutable archive when the primary slot is