From acbf667f494596f090f4dc53c2eefc70a2c1e414 Mon Sep 17 00:00:00 2001 From: David Mann Date: Thu, 12 Nov 2026 22:12:09 +0100 Subject: [PATCH] fix: gateway copy keeps its execute bit; packaging test ships a gateway fixture - install_gateway returned an identical copy before setting its mode, so a copy that had lost its execute bit was reused and clients couldn't start it. The mode is now checked on both paths (Unix). Test extended: a 0644 copy with the right bytes comes back executable (fails without the fix; run in Linux). - packaging/linux/test-package-metadata.sh builds packages from a fake release folder; it now has a craft-ai-mcp fixture too, since the packages install the gateway beside the launcher. Co-Authored-By: Claude Opus 5.5 --- crates/native/src/agents.rs | 41 ++++++++++++++++-------- crates/native/src/agents_tests.rs | 7 +++- packaging/linux/test-package-metadata.sh | 3 ++ 3 files changed, 36 insertions(+), 15 deletions(-) diff --git a/crates/native/src/agents.rs b/crates/native/src/agents.rs index 078723ab..31ecf34a 100644 --- a/crates/native/src/agents.rs +++ b/crates/native/src/agents.rs @@ -310,24 +310,37 @@ pub fn install_gateway(src: &Path, dir: &Path) -> Result { (Err(e), _) => return Err(format!("couldn't read the Craft gateway at {} ({e})", src.display())), _ => false, }; - if current { - return Ok(dest); - } - let bytes = std::fs::read(src).map_err(|e| format!("couldn't read the Craft gateway at {} ({e})", src.display()))?; - match crate::paths::write_atomic(&dest, &bytes) { - Ok(()) => {} - Err(_) if dest.is_file() => return Ok(dest), - Err(e) => return Err(e), - } - #[cfg(unix)] - { - use std::os::unix::fs::PermissionsExt; - std::fs::set_permissions(&dest, std::fs::Permissions::from_mode(0o755)) - .map_err(|e| format!("couldn't make {} executable ({e})", dest.display()))?; + if !current { + let bytes = std::fs::read(src).map_err(|e| format!("couldn't read the Craft gateway at {} ({e})", src.display()))?; + match crate::paths::write_atomic(&dest, &bytes) { + Ok(()) => {} + // A client still running the old copy keeps it busy on Windows: keep using it. + Err(_) if dest.is_file() => {} + Err(e) => return Err(e), + } } + // Also for a copy with the right bytes: one that lost its execute bit couldn't be started. + make_executable(&dest)?; Ok(dest) } +/// Make the gateway copy executable for everyone who may run it (`0755`) when it isn't already. +#[cfg(unix)] +fn make_executable(path: &Path) -> Result<(), String> { + use std::os::unix::fs::PermissionsExt; + let mode = std::fs::metadata(path).map_err(|e| format!("couldn't read {} ({e})", path.display()))?.permissions().mode(); + if mode & 0o111 == 0o111 { + return Ok(()); + } + std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o755)).map_err(|e| format!("couldn't make {} executable ({e})", path.display())) +} + +/// Windows has no execute bit. +#[cfg(not(unix))] +fn make_executable(_path: &Path) -> Result<(), String> { + Ok(()) +} + /// Serialises every change to the app list and the gateway copy. Detection refreshes them on a /// background thread while Settings may be turning agents off; without this, a refresh that /// read "on" just before the switch could write the list back after it was removed. diff --git a/crates/native/src/agents_tests.rs b/crates/native/src/agents_tests.rs index 4e40b6e7..171f4860 100644 --- a/crates/native/src/agents_tests.rs +++ b/crates/native/src/agents_tests.rs @@ -230,7 +230,12 @@ fn the_gateway_copy_is_executable() { let src = src_dir.path().join("craft-ai-mcp"); std::fs::write(&src, b"#!/bin/sh\n").unwrap(); let copy = install_gateway(&src, dir.path()).unwrap(); - assert_eq!(std::fs::metadata(copy).unwrap().permissions().mode() & 0o111, 0o111); + assert_eq!(std::fs::metadata(©).unwrap().permissions().mode() & 0o111, 0o111); + // A copy with the right bytes that lost its execute bit gets it back instead of being reused + // as it is: clients couldn't start it. + std::fs::set_permissions(©, std::fs::Permissions::from_mode(0o644)).unwrap(); + let again = install_gateway(&src, dir.path()).unwrap(); + assert_eq!(std::fs::metadata(again).unwrap().permissions().mode() & 0o111, 0o111); } #[test] diff --git a/packaging/linux/test-package-metadata.sh b/packaging/linux/test-package-metadata.sh index d24dc2a7..141f374e 100755 --- a/packaging/linux/test-package-metadata.sh +++ b/packaging/linux/test-package-metadata.sh @@ -16,6 +16,9 @@ check_packages() { mkdir -p "$work/target/$target/release" printf '#!/bin/sh\necho "ArtCraft Launcher packaging fixture"\n' >"$work/target/$target/release/artcraft-launcher" chmod +x "$work/target/$target/release/artcraft-launcher" + # The packages ship the craft-ai-mcp gateway beside the launcher (Settings › AI agents). + printf '#!/bin/sh\necho "craft-ai-mcp packaging fixture"\n' >"$work/target/$target/release/craft-ai-mcp" + chmod +x "$work/target/$target/release/craft-ai-mcp" # A set cross target skips the version smoke execution; no target binary is run. ( unset EMULATOR