Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
fix(cli): quote destructive confirmation commands safely
AkitaOnRails9c4b6a2a23b2
diff --git a/crates/ai-memory-cli/src/commands/move_project.rs b/crates/ai-memory-cli/src/commands/move_project.rsindex 4ecbbd2c..32a75e6c 100644--- a/crates/ai-memory-cli/src/commands/move_project.rs+++ b/crates/ai-memory-cli/src/commands/move_project.rs@@ -38,15 +38,19 @@ struct MoveProjectRequest {/// conflicting paths instead of merging them, and a dropped `--force` leaves/// the active-project guard in place.fn confirm_command(from_workspace: &str, target_flag: &str, args: &MoveProjectArgs) -> String {+ let from_workspace = super::render_shared::native_shell_quote(from_workspace);+ let to_workspace = super::render_shared::native_shell_quote(&args.to_workspace);let mut cmd = format!(- "ai-memory move-project --from-workspace {from_workspace} {target_flag} --to-workspace {}",- args.to_workspace+ "ai-memory move-project --from-workspace {from_workspace} {target_flag} --to-workspace {to_workspace}");if args.force {cmd.push_str(" --force");}if args.on_conflict != "block" {- cmd.push_str(&format!(" --on-conflict {}", args.on_conflict));+ cmd.push_str(&format!(+ " --on-conflict {}",+ super::render_shared::native_shell_quote(&args.on_conflict)+ ));}cmd.push_str(" --confirm");cmd@@ -81,8 +85,20 @@ pub async fn run(config: &Config, args: MoveProjectArgs) -> Result<()> {}};let (target_flag, requested) = match (&project, &project_id) {- (Some(project), _) => (format!("--project {project}"), project.clone()),- (None, Some(id)) => (format!("--project-id {id}"), id.clone()),+ (Some(project), _) => (+ format!(+ "--project {}",+ super::render_shared::native_shell_quote(project)+ ),+ project.clone(),+ ),+ (None, Some(id)) => (+ format!(+ "--project-id {}",+ super::render_shared::native_shell_quote(id)+ ),+ id.clone(),+ ),(None, None) => bail!("one of --project or --project-id is required"),};@@ -252,6 +268,17 @@ mod tests {);}+ #[cfg(not(windows))]+ #[test]+ fn confirm_command_quotes_free_form_scope_names() {+ let mut a = args();+ a.to_workspace = "new team".into();+ assert_eq!(+ confirm_command("old team", "--project 'api; echo nope'", &a),+ "ai-memory move-project --from-workspace 'old team' --project 'api; echo nope' --to-workspace 'new team' --confirm"+ );+ }+#[test]fn a_move_by_id_rekeys_the_link_under_the_resolved_name() {let report = serde_json::json!({ "from": "default/api", "to": "team/api" });diff --git a/crates/ai-memory-cli/src/commands/move_session.rs b/crates/ai-memory-cli/src/commands/move_session.rsindex ff2b1d08..04bc1db0 100644--- a/crates/ai-memory-cli/src/commands/move_session.rs+++ b/crates/ai-memory-cli/src/commands/move_session.rs@@ -264,14 +264,26 @@ fn apply_command(args: &MoveSessionArgs) -> String {cmd.push_str(&id.to_string());}if let Some(project) = &args.from_project {- cmd.push_str(&format!(" --from-project {project}"));+ cmd.push_str(&format!(+ " --from-project {}",+ super::render_shared::native_shell_quote(project)+ ));}if let Some(ws) = &args.from_workspace {- cmd.push_str(&format!(" --from-workspace {ws}"));+ cmd.push_str(&format!(+ " --from-workspace {}",+ super::render_shared::native_shell_quote(ws)+ ));}- cmd.push_str(&format!(" --to {}", args.to));+ cmd.push_str(&format!(+ " --to {}",+ super::render_shared::native_shell_quote(&args.to)+ ));if let Some(ws) = &args.to_workspace {- cmd.push_str(&format!(" --to-workspace {ws}"));+ cmd.push_str(&format!(+ " --to-workspace {}",+ super::render_shared::native_shell_quote(ws)+ ));}if args.pages != "move" {cmd.push_str(&format!(" --pages {}", args.pages));diff --git a/crates/ai-memory-cli/src/commands/purge_project.rs b/crates/ai-memory-cli/src/commands/purge_project.rsindex 854cad0c..98b7a6f4 100644--- a/crates/ai-memory-cli/src/commands/purge_project.rs+++ b/crates/ai-memory-cli/src/commands/purge_project.rs@@ -92,6 +92,7 @@ fn purge_summary_line(verb: &str, label: &str, report: &serde_json::Value) -> St/// run lease is active`, which is the exact condition `--force` exists to/// override.fn confirm_command(workspace: &str, target_flag: &str, args: &PurgeProjectArgs) -> String {+ let workspace = super::render_shared::native_shell_quote(workspace);let mut cmd = format!("ai-memory purge-project --workspace {workspace} {target_flag}");if args.force {cmd.push_str(" --force");@@ -166,8 +167,20 @@ pub async fn run(config: &Config, args: PurgeProjectArgs) -> Result<()> {// the preview's fallback label. A `--project-id` run has no resolved name// to print, so the id stands in.let (target_flag, target_label) = match (&project, &project_id) {- (Some(project), _) => (format!("--project {project}"), project.clone()),- (None, Some(id)) => (format!("--project-id {id}"), id.clone()),+ (Some(project), _) => (+ format!(+ "--project {}",+ super::render_shared::native_shell_quote(project)+ ),+ project.clone(),+ ),+ (None, Some(id)) => (+ format!(+ "--project-id {}",+ super::render_shared::native_shell_quote(id)+ ),+ id.clone(),+ ),(None, None) => bail!("one of --project or --project-id is required"),};@@ -316,6 +329,15 @@ mod tests {);}+ #[cfg(not(windows))]+ #[test]+ fn confirm_command_quotes_free_form_scope_names() {+ assert_eq!(+ confirm_command("team space", "--project 'api; echo nope'", &args()),+ "ai-memory purge-project --workspace 'team space' --project 'api; echo nope' --confirm"+ );+ }+/// Pins the exact wording and field order a script would grep for./// `purge_summary_line` is the single source for both the confirmed/// "Purged" line and the preview's "Would purge" line, so this alsodiff --git a/crates/ai-memory-cli/src/commands/render_shared.rs b/crates/ai-memory-cli/src/commands/render_shared.rsindex 4c3bbc2f..e9ce1f35 100644--- a/crates/ai-memory-cli/src/commands/render_shared.rs+++ b/crates/ai-memory-cli/src/commands/render_shared.rs@@ -2602,6 +2602,22 @@ pub(crate) fn shell_quote(s: &str) -> String {format!("'{escaped}'")}+/// Quote one argument for the native shell used by copy-pasteable commands.+pub(crate) fn native_shell_quote(s: &str) -> String {+ if cfg!(windows) {+ if s.chars().all(|c| {+ c.is_ascii_alphanumeric()+ || matches!(c, '-' | '_' | '.' | '/' | ':' | '%' | '+' | '=' | ',')+ }) {+ s.to_owned()+ } else {+ powershell_quote(s)+ }+ } else {+ shell_quote(s)+ }+}+fn powershell_quote(s: &str) -> String {format!("'{}'", s.replace('\'', "''"))}diff --git a/crates/ai-memory-hooks/src/router.rs b/crates/ai-memory-hooks/src/router.rsindex a4949c23..76d76bb7 100644--- a/crates/ai-memory-hooks/src/router.rs+++ b/crates/ai-memory-hooks/src/router.rs@@ -5324,6 +5324,7 @@ pub(crate) mod tests {use ai_memory_store::{FinishWorkstreamRun, PrepareWorkstreamRun, Store, WorkstreamSelection};use ai_memory_wiki::Wiki;use tempfile::TempDir;+ use tower::ServiceExt as _;use ai_memory_core::ObservationKind;@@ -11910,6 +11911,20 @@ pub(crate) mod tests {assert_eq!(ack["accepted"], 0);}+ #[tokio::test]+ async fn hook_batch_route_rejects_a_body_over_the_machine_limit() {+ let tmp = TempDir::new().unwrap();+ let app = hook_router(make_state(&tmp).await);+ let request = Request::builder()+ .method("POST")+ .uri("/hook/batch")+ .header("content-type", "application/json")+ .body(Body::from(vec![b' '; MAX_HOOK_BODY_BYTES + 1]))+ .unwrap();+ let response = app.oneshot(request).await.unwrap();+ assert_eq!(response.status(), StatusCode::PAYLOAD_TOO_LARGE);+ }+#[tokio::test]async fn handle_hook_batch_processes_sequentially_with_one_permit() {let tmp = TempDir::new().unwrap();