Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
fix(upgrade): render hooks with replacement binary
AkitaOnRails314c646662f5
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex b378d137..bae66335 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -14,8 +14,9 @@ version in [`docs/changelog/`](docs/changelog/):### Fixed- Refreshed existing ai-memory-owned OpenCode, OpenCode 2, OMP, Pi, and- OpenClaw generated TypeScript integrations during `ai-memory upgrade`, while- leaving absent and user-owned installs untouched (#1869).+ OpenClaw generated TypeScript integrations during `ai-memory upgrade` using+ the newly installed binary's templates, while leaving absent and user-owned+ installs untouched (#1869).- The web UI's root operator now sees every user's sessions in a project'sSessions, Agents and Token usage tabs, on each agent's page and on eachsession's page, and so do the `/api/v1` session list and observationsdiff --git a/crates/ai-memory-cli/src/cli.rs b/crates/ai-memory-cli/src/cli.rsindex 34d2c8b5..2dc6e341 100644--- a/crates/ai-memory-cli/src/cli.rs+++ b/crates/ai-memory-cli/src/cli.rs@@ -377,6 +377,9 @@ pub enum Command {/// installs keep using the shell wrapper's `upgrade` (image pull);/// package-managed installs (Homebrew, AUR, …) are refused.Upgrade(UpgradeArgs),+ /// Re-render installed hooks from the replacement binary during upgrade.+ #[command(hide = true, name = "upgrade-refresh-hooks")]+ UpgradeRefreshHooks,/// Manage optional upstream LLM provider authentication.Auth(AuthArgs),/// Manage human users and their project grants. All subcommandsdiff --git a/crates/ai-memory-cli/src/commands/upgrade.rs b/crates/ai-memory-cli/src/commands/upgrade.rsindex 562667a5..24cf103d 100644--- a/crates/ai-memory-cli/src/commands/upgrade.rs+++ b/crates/ai-memory-cli/src/commands/upgrade.rs@@ -30,6 +30,7 @@use std::fs;use std::io::Cursor;use std::path::{Component, Path, PathBuf};+use std::process::{Command, Stdio};use std::time::Duration;use anyhow::{Context, Result, bail};@@ -64,7 +65,7 @@ const UNIX_EXECUTABLE_MODE: u32 = 0o755;type StagedAgentList = Vec<(String, AgentChoice)>;/// Run the `upgrade` subcommand.-pub async fn run(config: &Config, args: UpgradeArgs) -> Result<()> {+pub async fn run(config: &Config, config_path: Option<&Path>, args: UpgradeArgs) -> Result<()> {let exe = resolve_current_exe()?;ensure_supported_install(&exe)?;@@ -79,9 +80,10 @@ pub async fn run(config: &Config, args: UpgradeArgs) -> Result<()> {let extract_root = download_and_extract_release(&fetcher, &base, &tag).await?;apply_extracted_release(extract_root.path(), &exe)?;- // Sync FS after await is intentional for this CLI one-shot path.- refresh_staged_hooks(config)?;- refresh_generated_integrations(config);+ // The running process still contains the old release's embedded templates.+ // Re-enter through the replacement executable so every generated hook is+ // rendered by the version that was just installed.+ run_replacement_refresh(&exe, &config.data_dir, config_path)?;refresh_plugins(config, &super::user_service::SystemRunner);warn_remote_server(config);println!("✓ upgraded to {tag}");@@ -89,6 +91,40 @@ pub async fn run(config: &Config, args: UpgradeArgs) -> Result<()> {Ok(())}+/// Refresh staged and generated integrations from the replacement process.+/// This is reached only through the hidden `upgrade-refresh-hooks` command.+pub(crate) fn refresh_after_replace(config: &Config) -> Result<()> {+ refresh_staged_hooks(config)?;+ refresh_generated_integrations(config);+ Ok(())+}++fn run_replacement_refresh(exe: &Path, data_dir: &Path, config_path: Option<&Path>) -> Result<()> {+ let mut command = replacement_refresh_command(exe, data_dir, config_path);+ let status = command+ .status()+ .with_context(|| format!("starting replacement executable {}", exe.display()))?;+ if !status.success() {+ bail!(+ "the new binary was installed, but its hook refresh exited with {status}; \+ run `{} --data-dir {} upgrade-refresh-hooks`",+ exe.display(),+ data_dir.display()+ );+ }+ Ok(())+}++fn replacement_refresh_command(exe: &Path, data_dir: &Path, config_path: Option<&Path>) -> Command {+ let mut command = Command::new(exe);+ command.arg("--data-dir").arg(data_dir);+ if let Some(path) = config_path {+ command.arg("--config").arg(path);+ }+ command.arg("upgrade-refresh-hooks").stdin(Stdio::null());+ command+}+fn resolve_current_exe() -> Result<PathBuf> {let exe = std::env::current_exe().context("resolving current executable path")?;Ok(fs::canonicalize(&exe).unwrap_or(exe))@@ -1141,6 +1177,36 @@ mod tests {use zip::ZipWriter;use zip::write::SimpleFileOptions;+ #[test]+ fn replacement_binary_refresh_receives_the_loaded_config_coordinates() {+ let exe = Path::new("/opt/ai memory/bin/ai-memory");+ let data = Path::new("/srv/memory data");+ let config = Path::new("/etc/ai memory/config.toml");+ let command = replacement_refresh_command(exe, data, Some(config));++ assert_eq!(command.get_program(), exe.as_os_str());+ assert_eq!(+ command.get_args().collect::<Vec<_>>(),+ [+ "--data-dir".as_ref(),+ data.as_os_str(),+ "--config".as_ref(),+ config.as_os_str(),+ "upgrade-refresh-hooks".as_ref(),+ ]+ );++ let command = replacement_refresh_command(exe, data, None);+ assert_eq!(+ command.get_args().collect::<Vec<_>>(),+ [+ "--data-dir".as_ref(),+ data.as_os_str(),+ "upgrade-refresh-hooks".as_ref(),+ ]+ );+ }+/// `upgrade` keeps an installed Claude Code plugin in step with the/// binary: marketplace update, then plugin update. Without the plugin,/// and for the Codex plugin, it runs nothing.diff --git a/crates/ai-memory-cli/src/lib.rs b/crates/ai-memory-cli/src/lib.rsindex c18fdfec..a650be79 100644--- a/crates/ai-memory-cli/src/lib.rs+++ b/crates/ai-memory-cli/src/lib.rs@@ -198,7 +198,10 @@ pub async fn run() -> Result<()> {Command::MergeProject(args) => commands::merge_project::run(&config, args).await,Command::MoveSession(args) => commands::move_session::run(&config, args).await,Command::Uninstall(args) => commands::uninstall::run(&config, args),- Command::Upgrade(args) => commands::upgrade::run(&config, args).await,+ Command::Upgrade(args) => {+ commands::upgrade::run(&config, config_path.as_deref(), args).await+ }+ Command::UpgradeRefreshHooks => commands::upgrade::refresh_after_replace(&config),Command::Auth(args) => commands::auth::run(&config, args, config_path.as_deref()).await,Command::User(args) => commands::user::run(&config, args).await,Command::ApiKey(args) => commands::api_key::run(&config, args).await,diff --git a/docs/windows.md b/docs/windows.mdindex 035ac0c8..a8dd7a59 100644--- a/docs/windows.md+++ b/docs/windows.md@@ -299,8 +299,9 @@ checksum, replaces `ai-memory.exe` via rename-aside (Windows cannot overwritea running image), refreshes a sibling `hooks/` tree when present, thenre-stages hooks for agents already under the data-dir hooks tree. It alsore-renders existing ai-memory-owned OpenCode, OpenCode 2, OMP, Pi, and OpenClaw-TypeScript integrations at their default or active-profile paths; absent and-user-owned files are left untouched. Keep the manual zip download above as a+TypeScript integrations from the newly installed binary's templates at their+default or active-profile paths; absent and user-owned files are left untouched.+Keep the manual zip download above as afallback if the install directory is not writable (for example under ProgramFiles).