garage44.eu/jeroen/midas

logclone: git clone https://garage44.eu/jeroen/midas.git

cartopolis-agent · 2026-11-21T20:36:58Z

fix(cli): `midas run` quotes the words it sends to the node

ssh joins its arguments with spaces and the node's shell splits them
again, so `midas run -- bash -c 'set -e; a; b'` reached the node as
`bash -c set -e; a; b`: `set` printed the environment and the rest ran
as separate commands. Found when building the public repository fresh
through `midas run`. Several words are now quoted one by one; a single
word stays a shell line as given (`midas run -- 'cd web && bun test'`).
src/cli.rs | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/src/cli.rs b/src/cli.rs
index 0c03c92..334c9d7 100644
--- a/src/cli.rs
+++ b/src/cli.rs
@@ -231,7 +231,15 @@ pub fn run(owner: &str, machine: &str, node: Option<String>, purpose: Option<Str
         }
     };
     eprintln!("midas run: on {id}");
-    let st = Command::new("ssh").args(["-o", "BatchMode=yes", "--", &id]).args(command).status()
+    // One shell line. Several words are quoted one by one: ssh joins its
+    // arguments with spaces and the node's shell splits them again, so `midas
+    // run -- bash -c 'a; b'` reached the node as `bash -c a; b` (2026-11-21).
+    // One word is a shell line as given (`midas run -- 'cd web && bun test'`).
+    let remote = match command {
+        [one] => one.clone(),
+        many => many.iter().map(|w| crate::bot::sh_quote(w)).collect::<Vec<_>>().join(" "),
+    };
+    let st = Command::new("ssh").args(["-o", "BatchMode=yes", "--", &id]).arg(remote).status()
         .with_context(|| format!("ssh {id}"))?;
     Ok(st.code().unwrap_or(1))
 }