Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
M1-D: file watcher with reconciliation + reset with live-process guard
AkitaOnRails5e29909bd592
diff --git a/Cargo.lock b/Cargo.lockindex a7e893fd..749fa42a 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -36,6 +36,7 @@ dependencies = ["figment","serde","serde_json",+ "sysinfo","tempfile","tokio","tracing",@@ -110,6 +111,8 @@ dependencies = ["ai-memory-store","anyhow","jiff",+ "notify",+ "notify-debouncer-full","serde","serde_json","serde_yaml",@@ -290,6 +293,12 @@ version = "1.0.5"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570"+[[package]]+name = "core-foundation-sys"+version = "0.8.7"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b"+[[package]]name = "cpufeatures"version = "0.2.17"@@ -308,6 +317,25 @@ dependencies = ["crossbeam-utils",]+[[package]]+name = "crossbeam-deque"+version = "0.8.6"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "9dd111b7b7f7d55b72c0a6ae361660ee5853c9af73f70c3c2ef6858b950e2e51"+dependencies = [+ "crossbeam-epoch",+ "crossbeam-utils",+]++[[package]]+name = "crossbeam-epoch"+version = "0.9.18"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e"+dependencies = [+ "crossbeam-utils",+]+[[package]]name = "crossbeam-utils"version = "0.8.21"@@ -375,6 +403,12 @@ dependencies = ["syn",]+[[package]]+name = "either"+version = "1.16.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "91622ff5e7162018101f2fea40d6ebf4a78bbe5a49736a2020649edf9693679e"+[[package]]name = "equivalent"version = "1.0.2"@@ -423,6 +457,15 @@ dependencies = ["version_check",]+[[package]]+name = "file-id"+version = "0.2.3"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "e1fc6a637b6dc58414714eddd9170ff187ecb0933d4c7024d1abbd23a3cc26e9"+dependencies = [+ "windows-sys 0.60.2",+]+[[package]]name = "find-msvc-tools"version = "0.1.9"@@ -444,6 +487,15 @@ dependencies = ["percent-encoding",]+[[package]]+name = "fsevent-sys"+version = "4.1.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "76ee7a02da4d231650c7cea31349b889be2f45ddb3ef3032d2ec8185f6313fd2"+dependencies = [+ "libc",+]+[[package]]name = "futures-core"version = "0.3.32"@@ -668,6 +720,26 @@ version = "0.1.15"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "c8fae54786f62fb2918dcfae3d568594e50eb9b5c25bf04371af6fe7516452fb"+[[package]]+name = "inotify"+version = "0.11.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "bd5b3eaf1a28b758ac0faa5a4254e8ab2705605496f1b1f3fbbc3988ad73d199"+dependencies = [+ "bitflags",+ "inotify-sys",+ "libc",+]++[[package]]+name = "inotify-sys"+version = "0.1.5"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "e05c02b5e89bff3b946cedeca278abc628fe811e604f027c45a8aa3cf793d0eb"+dependencies = [+ "libc",+]+[[package]]name = "is_terminal_polyfill"version = "1.70.2"@@ -733,6 +805,26 @@ dependencies = ["wasm-bindgen",]+[[package]]+name = "kqueue"+version = "1.1.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "eac30106d7dce88daf4a3fcb4879ea939476d5074a9b7ddd0fb97fa4bed5596a"+dependencies = [+ "kqueue-sys",+ "libc",+]++[[package]]+name = "kqueue-sys"+version = "1.1.2"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "07293a4e297ac234359b510362495713f75ea345d5307140414f20c69ffeb087"+dependencies = [+ "bitflags",+ "libc",+]+[[package]]name = "lazy_static"version = "1.5.0"@@ -820,10 +912,60 @@ source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "50b7e5b27aa02a74bac8c3f23f448f8d87ff11f92d3aac1a6ed369ee08cc56c1"dependencies = ["libc",+ "log","wasi","windows-sys 0.61.2",]+[[package]]+name = "notify"+version = "8.2.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "4d3d07927151ff8575b7087f245456e549fea62edf0ec4e565a5ee50c8402bc3"+dependencies = [+ "bitflags",+ "fsevent-sys",+ "inotify",+ "kqueue",+ "libc",+ "log",+ "mio",+ "notify-types",+ "walkdir",+ "windows-sys 0.60.2",+]++[[package]]+name = "notify-debouncer-full"+version = "0.6.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "375bd3a138be7bfeff3480e4a623df4cbfb55b79df617c055cd810ba466fa078"+dependencies = [+ "file-id",+ "log",+ "notify",+ "notify-types",+ "walkdir",+]++[[package]]+name = "notify-types"+version = "2.1.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "42b8cfee0e339a0337359f3c88165702ac6e600dc01c0cc9579a92d62b08477a"+dependencies = [+ "bitflags",+]++[[package]]+name = "ntapi"+version = "0.4.3"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "c3b335231dfd352ffb0f8017f3b6027a4917f7df785ea2143d8af2adc66980ae"+dependencies = [+ "winapi",+]+[[package]]name = "nu-ansi-term"version = "0.50.3"@@ -998,6 +1140,26 @@ version = "6.0.0"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf"+[[package]]+name = "rayon"+version = "1.12.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "fb39b166781f92d482534ef4b4b1b2568f42613b53e5b6c160e24cfbfa30926d"+dependencies = [+ "either",+ "rayon-core",+]++[[package]]+name = "rayon-core"+version = "1.13.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "22e18b0f0062d30d4230b2e85ff77fdfe4326feb054b9783a3460d8435c8ab91"+dependencies = [+ "crossbeam-deque",+ "crossbeam-utils",+]+[[package]]name = "redox_syscall"version = "0.5.18"@@ -1320,6 +1482,20 @@ dependencies = ["syn",]+[[package]]+name = "sysinfo"+version = "0.32.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "4c33cd241af0f2e9e3b5c32163b873b29956890b5342e6745b917ce9d490f4af"+dependencies = [+ "core-foundation-sys",+ "libc",+ "memchr",+ "ntapi",+ "rayon",+ "windows",+]+[[package]]name = "tempfile"version = "3.27.0"@@ -1779,6 +1955,22 @@ dependencies = ["semver",]+[[package]]+name = "winapi"+version = "0.3.9"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"+dependencies = [+ "winapi-i686-pc-windows-gnu",+ "winapi-x86_64-pc-windows-gnu",+]++[[package]]+name = "winapi-i686-pc-windows-gnu"+version = "0.4.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"+[[package]]name = "winapi-util"version = "0.1.11"@@ -1788,19 +1980,87 @@ dependencies = ["windows-sys 0.61.2",]+[[package]]+name = "winapi-x86_64-pc-windows-gnu"+version = "0.4.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"++[[package]]+name = "windows"+version = "0.57.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "12342cb4d8e3b046f3d80effd474a7a02447231330ef77d71daa6fbc40681143"+dependencies = [+ "windows-core",+ "windows-targets 0.52.6",+]++[[package]]+name = "windows-core"+version = "0.57.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "d2ed2439a290666cd67ecce2b0ffaad89c2a56b976b736e6ece670297897832d"+dependencies = [+ "windows-implement",+ "windows-interface",+ "windows-result",+ "windows-targets 0.52.6",+]++[[package]]+name = "windows-implement"+version = "0.57.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "9107ddc059d5b6fbfbffdfa7a7fe3e22a226def0b2608f72e9d552763d3e1ad7"+dependencies = [+ "proc-macro2",+ "quote",+ "syn",+]++[[package]]+name = "windows-interface"+version = "0.57.0"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "29bee4b38ea3cde66011baa44dba677c432a78593e202392d1e9070cf2a7fca7"+dependencies = [+ "proc-macro2",+ "quote",+ "syn",+]+[[package]]name = "windows-link"version = "0.2.1"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"+[[package]]+name = "windows-result"+version = "0.1.2"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "5e383302e8ec8515204254685643de10811af0ed97ea37210dc26fb0032647f8"+dependencies = [+ "windows-targets 0.52.6",+]+[[package]]name = "windows-sys"version = "0.48.0"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"dependencies = [- "windows-targets",+ "windows-targets 0.48.5",+]++[[package]]+name = "windows-sys"+version = "0.60.2"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "f2f500e4d28234f72040990ec9d39e3a6b950f9f22d3dba18416c35882612bcb"+dependencies = [+ "windows-targets 0.53.5",][[package]]@@ -1818,13 +2078,46 @@ version = "0.48.5"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c"dependencies = [- "windows_aarch64_gnullvm",- "windows_aarch64_msvc",- "windows_i686_gnu",- "windows_i686_msvc",- "windows_x86_64_gnu",- "windows_x86_64_gnullvm",- "windows_x86_64_msvc",+ "windows_aarch64_gnullvm 0.48.5",+ "windows_aarch64_msvc 0.48.5",+ "windows_i686_gnu 0.48.5",+ "windows_i686_msvc 0.48.5",+ "windows_x86_64_gnu 0.48.5",+ "windows_x86_64_gnullvm 0.48.5",+ "windows_x86_64_msvc 0.48.5",+]++[[package]]+name = "windows-targets"+version = "0.52.6"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"+dependencies = [+ "windows_aarch64_gnullvm 0.52.6",+ "windows_aarch64_msvc 0.52.6",+ "windows_i686_gnu 0.52.6",+ "windows_i686_gnullvm 0.52.6",+ "windows_i686_msvc 0.52.6",+ "windows_x86_64_gnu 0.52.6",+ "windows_x86_64_gnullvm 0.52.6",+ "windows_x86_64_msvc 0.52.6",+]++[[package]]+name = "windows-targets"+version = "0.53.5"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "4945f9f551b88e0d65f3db0bc25c33b8acea4d9e41163edf90dcd0b19f9069f3"+dependencies = [+ "windows-link",+ "windows_aarch64_gnullvm 0.53.1",+ "windows_aarch64_msvc 0.53.1",+ "windows_i686_gnu 0.53.1",+ "windows_i686_gnullvm 0.53.1",+ "windows_i686_msvc 0.53.1",+ "windows_x86_64_gnu 0.53.1",+ "windows_x86_64_gnullvm 0.53.1",+ "windows_x86_64_msvc 0.53.1",][[package]]@@ -1833,42 +2126,138 @@ version = "0.48.5"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8"+[[package]]+name = "windows_aarch64_gnullvm"+version = "0.52.6"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"++[[package]]+name = "windows_aarch64_gnullvm"+version = "0.53.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "a9d8416fa8b42f5c947f8482c43e7d89e73a173cead56d044f6a56104a6d1b53"+[[package]]name = "windows_aarch64_msvc"version = "0.48.5"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"+[[package]]+name = "windows_aarch64_msvc"+version = "0.52.6"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"++[[package]]+name = "windows_aarch64_msvc"+version = "0.53.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "b9d782e804c2f632e395708e99a94275910eb9100b2114651e04744e9b125006"+[[package]]name = "windows_i686_gnu"version = "0.48.5"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"+[[package]]+name = "windows_i686_gnu"+version = "0.52.6"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"++[[package]]+name = "windows_i686_gnu"+version = "0.53.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "960e6da069d81e09becb0ca57a65220ddff016ff2d6af6a223cf372a506593a3"++[[package]]+name = "windows_i686_gnullvm"+version = "0.52.6"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"++[[package]]+name = "windows_i686_gnullvm"+version = "0.53.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "fa7359d10048f68ab8b09fa71c3daccfb0e9b559aed648a8f95469c27057180c"+[[package]]name = "windows_i686_msvc"version = "0.48.5"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"+[[package]]+name = "windows_i686_msvc"+version = "0.52.6"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"++[[package]]+name = "windows_i686_msvc"+version = "0.53.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "1e7ac75179f18232fe9c285163565a57ef8d3c89254a30685b57d83a38d326c2"+[[package]]name = "windows_x86_64_gnu"version = "0.48.5"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e"+[[package]]+name = "windows_x86_64_gnu"+version = "0.52.6"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"++[[package]]+name = "windows_x86_64_gnu"+version = "0.53.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "9c3842cdd74a865a8066ab39c8a7a473c0778a3f29370b5fd6b4b9aa7df4a499"+[[package]]name = "windows_x86_64_gnullvm"version = "0.48.5"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc"+[[package]]+name = "windows_x86_64_gnullvm"+version = "0.52.6"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"++[[package]]+name = "windows_x86_64_gnullvm"+version = "0.53.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "0ffa179e2d07eee8ad8f57493436566c7cc30ac536a3379fdf008f47f6bb7ae1"+[[package]]name = "windows_x86_64_msvc"version = "0.48.5"source = "registry+https://github.com/rust-lang/crates.io-index"checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"+[[package]]+name = "windows_x86_64_msvc"+version = "0.52.6"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"++[[package]]+name = "windows_x86_64_msvc"+version = "0.53.1"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "d6bbff5f0aada427a1e5a6da5f1f98158182f26556f345ac9e04d36d0ebed650"+[[package]]name = "winnow"version = "0.7.15"diff --git a/Cargo.toml b/Cargo.tomlindex 1bfcb4b1..e77ac497 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -64,6 +64,11 @@ refinery = { version = "0.8", features = ["rusqlite"] }sha2 = "0.10"parking_lot = "0.12"+# Filesystem watcher + process inspection.+notify = "8"+notify-debouncer-full = "0.6"+sysinfo = "0.32"+# Testing.tempfile = "3"diff --git a/crates/ai-memory-cli/Cargo.toml b/crates/ai-memory-cli/Cargo.tomlindex a2b26443..f561003a 100644--- a/crates/ai-memory-cli/Cargo.toml+++ b/crates/ai-memory-cli/Cargo.toml@@ -22,6 +22,7 @@ dirs.workspace = truefigment.workspace = trueserde.workspace = trueserde_json.workspace = true+sysinfo.workspace = truetokio.workspace = truetracing.workspace = truetracing-appender.workspace = truediff --git a/crates/ai-memory-cli/src/cli.rs b/crates/ai-memory-cli/src/cli.rsindex 1f56cceb..c236971d 100644--- a/crates/ai-memory-cli/src/cli.rs+++ b/crates/ai-memory-cli/src/cli.rs@@ -35,6 +35,10 @@ pub enum Command {Search(SearchArgs),/// Write or update a wiki page atomically (also indexes it in the store).WritePage(WritePageArgs),+ /// Run the filesystem watcher (foreground; Ctrl-C to stop).+ Watch(WatchArgs),+ /// Wipe the data directory's wiki/, db/, raw/ contents.+ Reset(ResetArgs),}/// Arguments for `init`.@@ -66,6 +70,25 @@ pub struct SearchArgs {pub json: bool,}+/// Arguments for `watch`.+#[derive(Debug, Args)]+pub struct WatchArgs {+ /// Workspace name to attribute discovered files to (auto-created).+ #[arg(long, default_value = "default")]+ pub workspace: String,+ /// Project name within the workspace (auto-created).+ #[arg(long, default_value = "scratch")]+ pub project: String,+}++/// Arguments for `reset`.+#[derive(Debug, Args)]+pub struct ResetArgs {+ /// Required to actually wipe data. Without this we just dry-run.+ #[arg(long)]+ pub confirm: bool,+}+/// Arguments for `write-page`.#[derive(Debug, Args)]pub struct WritePageArgs {diff --git a/crates/ai-memory-cli/src/commands/mod.rs b/crates/ai-memory-cli/src/commands/mod.rsindex c22a60c0..9690d3f9 100644--- a/crates/ai-memory-cli/src/commands/mod.rs+++ b/crates/ai-memory-cli/src/commands/mod.rs@@ -1,6 +1,8 @@//! Subcommand implementations.pub mod init;+pub mod reset;pub mod search;pub mod status;+pub mod watch;pub mod write_page;diff --git a/crates/ai-memory-cli/src/commands/reset.rs b/crates/ai-memory-cli/src/commands/reset.rsnew file mode 100644index 00000000..b17286ae--- /dev/null+++ b/crates/ai-memory-cli/src/commands/reset.rs@@ -0,0 +1,71 @@+//! `ai-memory reset --confirm` — wipe wiki/, db/, raw/ contents.+//!+//! Refuses to run while another `ai-memory` process is alive (lesson from+//! basic-memory #765, where a zombie process holding the old SQLite+//! inode caused phantom search results after a reset).++use anyhow::{Result, bail};+use std::ffi::OsStr;+use sysinfo::System;++use crate::cli::ResetArgs;+use crate::config::Config;++const SUBDIRS: &[&str] = &["wiki", "db", "raw"];+const BIN_NAME: &str = "ai-memory";++/// Run the `reset` subcommand.+///+/// # Errors+/// Returns an error if another `ai-memory` process is running, if+/// `--confirm` was not provided, or if a directory cannot be removed.+pub fn run(config: &Config, args: ResetArgs) -> Result<()> {+ let siblings = sibling_processes();+ if !siblings.is_empty() {+ let pids: Vec<u32> = siblings.iter().map(|p| p.as_u32()).collect();+ bail!(+ "refusing to reset: {} other ai-memory process(es) running (pids: {:?}). \+ Stop them first, then re-run.",+ pids.len(),+ pids,+ );+ }++ if !args.confirm {+ for sub in SUBDIRS {+ let path = config.data_dir.join(sub);+ if path.exists() {+ println!("would remove {}", path.display());+ }+ }+ println!("(dry-run; pass --confirm to wipe)");+ return Ok(());+ }++ for sub in SUBDIRS {+ let path = config.data_dir.join(sub);+ if !path.exists() {+ continue;+ }+ std::fs::remove_dir_all(&path)?;+ std::fs::create_dir_all(&path)?;+ tracing::info!(path = %path.display(), "reset");+ }+ tracing::info!("reset complete");+ Ok(())+}++fn sibling_processes() -> Vec<sysinfo::Pid> {+ let mut sys = System::new();+ sys.refresh_processes(sysinfo::ProcessesToUpdate::All, true);+ let me = sysinfo::Pid::from_u32(std::process::id());+ let bin_os: &OsStr = OsStr::new(BIN_NAME);+ sys.processes_by_exact_name(bin_os)+ // On Linux, sysinfo lists tokio worker threads alongside the main+ // process under the same comm name. thread_kind() == None means+ // we're looking at the process leader, not one of its threads.+ .filter(|p| p.thread_kind().is_none())+ .map(sysinfo::Process::pid)+ .filter(|pid| *pid != me)+ .collect()+}diff --git a/crates/ai-memory-cli/src/commands/watch.rs b/crates/ai-memory-cli/src/commands/watch.rsnew file mode 100644index 00000000..40507fd3--- /dev/null+++ b/crates/ai-memory-cli/src/commands/watch.rs@@ -0,0 +1,43 @@+//! `ai-memory watch` — foreground watcher loop.+//!+//! In v1 this is a standalone command; in M2 the watcher folds into+//! `ai-memory serve` alongside the MCP transport.++use ai_memory_store::Store;+use ai_memory_wiki::{WatcherHandle, Wiki};+use anyhow::{Context, Result};++use crate::cli::WatchArgs;+use crate::config::Config;++/// Run the `watch` subcommand.+///+/// # Errors+/// Returns an error if the store cannot be opened or the watcher+/// cannot install its filesystem hooks.+pub async fn run(config: &Config, args: WatchArgs) -> Result<()> {+ let store = Store::open(&config.data_dir)+ .with_context(|| format!("opening store at {}", config.data_dir.display()))?;+ let ws = store+ .writer+ .get_or_create_workspace(args.workspace.clone())+ .await?;+ let proj = store+ .writer+ .get_or_create_project(ws, args.project.clone(), None)+ .await?;++ let wiki = Wiki::new(&config.data_dir, store.writer.clone())?;+ let handle = WatcherHandle::start(wiki.clone(), ws, proj)?;+ tracing::info!(+ root = %wiki.root().display(),+ workspace = %args.workspace,+ project = %args.project,+ "watching wiki (Ctrl-C to stop)"+ );++ tokio::signal::ctrl_c().await?;+ tracing::info!("shutting down watcher");+ handle.shutdown().await;+ Ok(())+}diff --git a/crates/ai-memory-cli/src/main.rs b/crates/ai-memory-cli/src/main.rsindex 734bf0e4..e25c2023 100644--- a/crates/ai-memory-cli/src/main.rs+++ b/crates/ai-memory-cli/src/main.rs@@ -40,5 +40,7 @@ async fn main() -> Result<()> {Command::Status(args) => commands::status::run(&config, args).await,Command::Search(args) => commands::search::run(&config, args).await,Command::WritePage(args) => commands::write_page::run(&config, args).await,+ Command::Watch(args) => commands::watch::run(&config, args).await,+ Command::Reset(args) => commands::reset::run(&config, args),}}diff --git a/crates/ai-memory-wiki/Cargo.toml b/crates/ai-memory-wiki/Cargo.tomlindex 38e36a5c..1fb1d5db 100644--- a/crates/ai-memory-wiki/Cargo.toml+++ b/crates/ai-memory-wiki/Cargo.toml@@ -20,6 +20,8 @@ tempfile.workspace = truejiff.workspace = truetracing.workspace = truetokio.workspace = true+notify.workspace = true+notify-debouncer-full.workspace = true[dev-dependencies]diff --git a/crates/ai-memory-wiki/src/lib.rs b/crates/ai-memory-wiki/src/lib.rsindex 691ea2e7..9e28618a 100644--- a/crates/ai-memory-wiki/src/lib.rs+++ b/crates/ai-memory-wiki/src/lib.rs@@ -8,8 +8,10 @@mod atomic;mod error;mod markdown;+mod watcher;mod wiki;pub use error::{WikiError, WikiResult};pub use markdown::{Markdown, derive_title, emit, parse};+pub use watcher::{DEBOUNCE_WINDOW, RECONCILE_INTERVAL, WatcherHandle};pub use wiki::{Wiki, WritePageRequest};diff --git a/crates/ai-memory-wiki/src/watcher.rs b/crates/ai-memory-wiki/src/watcher.rsnew file mode 100644index 00000000..7d3fd578--- /dev/null+++ b/crates/ai-memory-wiki/src/watcher.rs@@ -0,0 +1,317 @@+//! Filesystem watcher with debouncing and a periodic reconciliation pass.+//!+//! Two parts work together:+//!+//! 1. **Debounced events** via [`notify_debouncer_full`]. When a markdown+//! file under the wiki root is created or modified, we read it from+//! disk, parse the frontmatter, and `reindex_page` against the store.+//! Own-writes are absorbed by the store's sha256 short-circuit, so+//! the loop terminates after one no-op reindex.+//! 2. **Reconciliation tick** every 30s walks the entire wiki tree and+//! reindexes every markdown file. Catches any events the OS dropped+//! (basic-memory #580 — file watchers go stale under FSEvents buffer+//! overflow, hidden-dir globs, etc.). Hidden-directory paths are+//! explicitly NOT skipped (#798 lesson).+//!+//! The watcher never *writes* to disk — that loop would be unbounded.+//! External writes drive store updates; internal writes drive disk ++//! store updates via [`Wiki::write_page`].++use std::path::Path;+use std::time::Duration;++use ai_memory_core::{PagePath, ProjectId, WorkspaceId};+use notify::{EventKind, RecursiveMode};+use notify_debouncer_full::{DebounceEventResult, Debouncer, RecommendedCache, new_debouncer};+use tokio::sync::mpsc;+use tracing::{debug, info, warn};++use crate::error::{WikiError, WikiResult};+use crate::wiki::Wiki;++/// Reconciliation tick interval.+pub const RECONCILE_INTERVAL: Duration = Duration::from_secs(30);++/// Debounce window for filesystem events.+pub const DEBOUNCE_WINDOW: Duration = Duration::from_millis(300);++/// Handle representing an active watcher; drop to stop.+pub struct WatcherHandle {+ _debouncer: Debouncer<notify::RecommendedWatcher, RecommendedCache>,+ shutdown: Option<tokio::sync::oneshot::Sender<()>>,+ task: Option<tokio::task::JoinHandle<()>>,+}++impl WatcherHandle {+ /// Start watching `wiki.root()` recursively. Spawns one tokio task+ /// that consumes debounced events and runs the reconciliation timer.+ ///+ /// # Errors+ /// Propagates any notify error encountered when installing the OS+ /// watcher.+ pub fn start(wiki: Wiki, workspace_id: WorkspaceId, project_id: ProjectId) -> WikiResult<Self> {+ let (event_tx, event_rx) = mpsc::unbounded_channel();++ let mut debouncer = new_debouncer(+ DEBOUNCE_WINDOW,+ None,+ move |result: DebounceEventResult| match result {+ Ok(events) => {+ for event in events {+ let _ = event_tx.send(event);+ }+ }+ Err(errors) => {+ for e in errors {+ warn!(error = %e, "notify error");+ }+ }+ },+ )+ .map_err(|e| WikiError::Io(std::io::Error::other(e.to_string())))?;++ debouncer+ .watch(wiki.root(), RecursiveMode::Recursive)+ .map_err(|e| WikiError::Io(std::io::Error::other(e.to_string())))?;++ let (shutdown_tx, shutdown_rx) = tokio::sync::oneshot::channel::<()>();+ let task = tokio::spawn(run_loop(+ wiki,+ workspace_id,+ project_id,+ event_rx,+ shutdown_rx,+ ));++ Ok(Self {+ _debouncer: debouncer,+ shutdown: Some(shutdown_tx),+ task: Some(task),+ })+ }++ /// Stop the watcher and wait for the event loop to drain.+ pub async fn shutdown(mut self) {+ if let Some(tx) = self.shutdown.take() {+ let _ = tx.send(());+ }+ if let Some(handle) = self.task.take() {+ let _ = handle.await;+ }+ }+}++impl Drop for WatcherHandle {+ fn drop(&mut self) {+ if let Some(tx) = self.shutdown.take() {+ let _ = tx.send(());+ }+ }+}++async fn run_loop(+ wiki: Wiki,+ ws: WorkspaceId,+ proj: ProjectId,+ mut rx: mpsc::UnboundedReceiver<notify_debouncer_full::DebouncedEvent>,+ mut shutdown: tokio::sync::oneshot::Receiver<()>,+) {+ let mut tick = tokio::time::interval(RECONCILE_INTERVAL);+ tick.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);+ // First tick fires immediately; consume it so we don't reconcile at boot.+ tick.tick().await;++ loop {+ tokio::select! {+ biased;+ _ = &mut shutdown => {+ debug!("watcher shutting down");+ return;+ }+ Some(event) = rx.recv() => {+ handle_event(&wiki, ws, proj, event).await;+ }+ _ = tick.tick() => {+ if let Err(e) = reconcile(&wiki, ws, proj).await {+ warn!(error = %e, "reconciliation failed");+ }+ }+ else => return,+ }+ }+}++async fn handle_event(+ wiki: &Wiki,+ ws: WorkspaceId,+ proj: ProjectId,+ event: notify_debouncer_full::DebouncedEvent,+) {+ if !matches!(+ event.kind,+ EventKind::Create(_) | EventKind::Modify(_) | EventKind::Other+ ) {+ return;+ }+ for raw_path in &event.paths {+ if !is_markdown(raw_path) {+ continue;+ }+ if is_tempfile(raw_path) {+ continue;+ }+ let Some(page_path) = page_path_relative_to(wiki.root(), raw_path) else {+ continue;+ };+ if !raw_path.is_file() {+ // Likely a transient state (mv, atomic rename in flight).+ continue;+ }+ match wiki.reindex_page(ws, proj, page_path.clone()).await {+ Ok(_) => debug!(path = %page_path, "reindexed via watcher"),+ Err(e) => warn!(path = %page_path, error = %e, "watcher reindex failed"),+ }+ }+}++async fn reconcile(wiki: &Wiki, ws: WorkspaceId, proj: ProjectId) -> WikiResult<()> {+ let root = wiki.root().to_path_buf();+ let paths = tokio::task::spawn_blocking(move || walk_markdown(&root))+ .await+ .map_err(|e| WikiError::Io(std::io::Error::other(e.to_string())))??;++ let count = paths.len();+ for path in paths {+ if let Err(e) = wiki.reindex_page(ws, proj, path.clone()).await {+ warn!(path = %path, error = %e, "reconcile reindex failed");+ }+ }+ info!(count, "reconciliation pass complete");+ Ok(())+}++fn walk_markdown(root: &Path) -> WikiResult<Vec<PagePath>> {+ let mut out = Vec::new();+ let mut stack = vec![root.to_path_buf()];+ while let Some(dir) = stack.pop() {+ let read = match std::fs::read_dir(&dir) {+ Ok(r) => r,+ Err(e) if e.kind() == std::io::ErrorKind::NotFound => continue,+ Err(e) => return Err(WikiError::Io(e)),+ };+ for entry in read {+ let entry = entry?;+ let path = entry.path();+ let ft = entry.file_type()?;+ if ft.is_dir() {+ stack.push(path);+ } else if ft.is_file()+ && is_markdown(&path)+ && !is_tempfile(&path)+ && let Some(pp) = page_path_relative_to(root, &path)+ {+ out.push(pp);+ }+ }+ }+ Ok(out)+}++fn is_markdown(path: &Path) -> bool {+ path.extension().is_some_and(|e| e == "md")+}++fn is_tempfile(path: &Path) -> bool {+ path.file_name()+ .and_then(|n| n.to_str())+ .is_some_and(|n| n.starts_with(".ai-memory-tmp."))+}++fn page_path_relative_to(root: &Path, abs: &Path) -> Option<PagePath> {+ let rel: &Path = abs.strip_prefix(root).ok()?;+ let s = rel.to_string_lossy().replace('\\', "/");+ PagePath::new(s).ok()+}++#[cfg(test)]+mod tests {+ use super::*;+ use ai_memory_store::Store;+ use tempfile::TempDir;++ async fn setup() -> (TempDir, Store, Wiki, WorkspaceId, ProjectId) {+ let tmp = TempDir::new().unwrap();+ let store = Store::open(tmp.path()).unwrap();+ let ws = store+ .writer+ .get_or_create_workspace("default")+ .await+ .unwrap();+ let proj = store+ .writer+ .get_or_create_project(ws, "scratch", None)+ .await+ .unwrap();+ let wiki = Wiki::new(tmp.path(), store.writer.clone()).unwrap();+ (tmp, store, wiki, ws, proj)+ }++ #[tokio::test(flavor = "multi_thread", worker_threads = 2)]+ async fn picks_up_externally_created_file() {+ let (tmp, store, wiki, ws, proj) = setup().await;+ let handle = WatcherHandle::start(wiki.clone(), ws, proj).unwrap();++ // Drop a file *bypassing* the wiki write API (simulating an+ // external editor).+ let target = tmp.path().join("wiki/external.md");+ std::fs::write(&target, "Hello from outside the wiki API.\n").unwrap();++ // Poll for the row to land. Watcher debounces at 300ms.+ let deadline = std::time::Instant::now() + Duration::from_secs(3);+ let mut hits = Vec::new();+ while std::time::Instant::now() < deadline {+ hits = store+ .reader+ .search_pages("outside".into(), 5)+ .await+ .unwrap();+ if !hits.is_empty() {+ break;+ }+ tokio::time::sleep(Duration::from_millis(100)).await;+ }+ assert!(!hits.is_empty(), "watcher did not pick up external write");+ assert_eq!(hits[0].path.as_str(), "external.md");+ handle.shutdown().await;+ }++ #[tokio::test(flavor = "multi_thread", worker_threads = 2)]+ async fn reconcile_picks_up_file_added_while_watcher_offline() {+ let (tmp, store, wiki, ws, proj) = setup().await;+ // Write a file BEFORE starting the watcher.+ let target = tmp.path().join("wiki/preexisting.md");+ std::fs::write(&target, "I existed first.\n").unwrap();++ let handle = WatcherHandle::start(wiki.clone(), ws, proj).unwrap();+ // Hit reconcile manually instead of waiting 30s.+ reconcile(&wiki, ws, proj).await.unwrap();++ let hits = store+ .reader+ .search_pages("existed".into(), 5)+ .await+ .unwrap();+ assert_eq!(hits.len(), 1);+ assert_eq!(hits[0].path.as_str(), "preexisting.md");+ handle.shutdown().await;+ }++ #[tokio::test(flavor = "multi_thread", worker_threads = 2)]+ async fn ignores_own_atomic_tempfiles() {+ // Quick unit test: tempfile prefix detection.+ let p = Path::new("/some/dir/.ai-memory-tmp.abc.md");+ assert!(is_tempfile(p));+ let q = Path::new("/some/dir/normal.md");+ assert!(!is_tempfile(q));+ }+}diff --git a/crates/ai-memory-wiki/src/wiki.rs b/crates/ai-memory-wiki/src/wiki.rsindex 2c59df80..beca4022 100644--- a/crates/ai-memory-wiki/src/wiki.rs+++ b/crates/ai-memory-wiki/src/wiki.rs@@ -57,6 +57,46 @@ impl Wiki {parse(&raw)}+ /// Cloneable handle to the underlying store writer.+ #[must_use]+ pub fn writer(&self) -> &WriterHandle {+ &self.writer+ }++ /// Re-index the page on disk at `path` into the store *without*+ /// rewriting the file.+ ///+ /// Called by the watcher when an external editor (Obsidian, vim) has+ /// changed a file we did not write. The store-side sha256 short-circuit+ /// makes this idempotent: if the on-disk content already matches the+ /// latest version, no supersession happens.+ ///+ /// # Errors+ /// Returns [`WikiError`] for any filesystem, parsing, or store error.+ pub async fn reindex_page(+ &self,+ workspace_id: WorkspaceId,+ project_id: ProjectId,+ path: PagePath,+ ) -> WikiResult<PageId> {+ let md = self.read_page(&path)?;+ let title = derive_title(&md.frontmatter, &md.body, &path);+ let id = self+ .writer+ .upsert_page(NewPage {+ workspace_id,+ project_id,+ path,+ title,+ body: md.body,+ tier: Tier::Semantic,+ frontmatter_json: md.frontmatter,+ pinned: false,+ })+ .await?;+ Ok(id)+ }+/// Write `body` (with optional `frontmatter`) atomically to/// `<wiki_root>/<path>` and upsert the matching page row in the store.///