A self-improving RLM agent for coding workflows and long-running autonomous tasks.
Rust
preserve native launcher during npm takeover (#3531)
kt0b0e3b55a159
diff --git a/.changes/fix-shared-prefix-installer.md b/.changes/fix-shared-prefix-installer.mdnew file mode 100644index 0000000000..eed2a5fca0--- /dev/null+++ b/.changes/fix-shared-prefix-installer.md@@ -0,0 +1 @@+- Upgrading from the npm package no longer deletes the native `prime-agent` command when npm and the installer share a bin directory, even if the npm uninstall fails or is interrupted. The installer now fails when the installed command cannot answer `--version`, instead of reporting success with a broken command.diff --git a/crates/pa-cli/tests/installer_update_e2e.rs b/crates/pa-cli/tests/installer_update_e2e.rsindex 36e53e098e..ecafc0410d 100644--- a/crates/pa-cli/tests/installer_update_e2e.rs+++ b/crates/pa-cli/tests/installer_update_e2e.rs@@ -559,7 +559,7 @@ fn update_rollback_reads_the_unsuffixed_installer_slot() {"install-rust.sh channel beta\nversion 1.0.0\n",).expect("slot marker");- std::fs::write(slot.join("prime-agent"), b"payload\n").expect("slot payload");+ std::fs::write(slot.join("prime-agent"), b"#!/bin/sh\necho 1.0.0\n").expect("slot payload");make_executable(&slot.join("prime-agent"));let mut command = Command::new(&binary);diff --git a/install-rust.sh b/install-rust.shindex c1c40224c4..4884a0145d 100755--- a/install-rust.sh+++ b/install-rust.sh@@ -2531,6 +2531,7 @@ if [ "$WINDOWS" = "yes" ] \fifilauncher_tmp=""+npm_launcher_backup=""cmd_tmp=""preserved_cmd_file=""windows_installed=""@@ -2561,6 +2562,12 @@ on_exit() {# to publish into share_dir while this one still restores state — the# restore would delete that fresh payload (cross-installer data loss).publish_window_restore+ # npm may have unlinked the public command before an interrupted uninstall.+ # Restore the saved native launcher before releasing the publication lock.+ if [ -n "${npm_launcher_backup:-}" ]; then+ mv -f "$npm_launcher_backup" "$launcher" 2>/dev/null \+ || echo "warning: restore the native launcher from ${npm_launcher_backup} to ${launcher}" >&2+ fi[ -n "$launcher_tmp" ] && rm -f "$launcher_tmp" 2>/dev/null || true[ -n "${cmd_tmp:-}" ] && rm -f "$cmd_tmp" 2>/dev/null || true# The extraction stage is disposable on every failed path: a successful@@ -3003,6 +3010,13 @@ try:except Exception:print("")' "${npm_root}/prime-agent/package.json")"if [ -n "$ts_version" ]; then+ # npm unlinks its old bin entry even when our regular launcher has+ # replaced that symlink. Keep a sibling copy and publish it again after+ # uninstall (including a partial failure), without guessing npm's prefix.+ launcher_tmp="$(mktemp "${bin_dir}/.prime-agent.XXXXXX")"+ cp -p "$launcher" "$launcher_tmp"+ npm_launcher_backup="$launcher_tmp"+ launcher_tmp=""step_start "Removing the TypeScript npm package"if npm uninstall -g prime-agent >/dev/null 2>&1; thenstep_ok "Removed the TypeScript npm package" "restore: npm install -g prime-agent@${ts_version}"@@ -3011,6 +3025,8 @@ except Exception:note "! The TypeScript npm command can shadow ${launcher} on PATH. Remove it:"todo "npm uninstall -g prime-agent"fi+ mv -f "$npm_launcher_backup" "$launcher"+ npm_launcher_backup=""fififi@@ -3112,15 +3128,15 @@ elsefi# --- verify: the launcher must answer --version ------------------------------------# Tried once. The common failure on a fresh install is the first-run kernel-# venv bootstrap (the sidecar provisions itself on first launch), so the-# failure prints the output plus a re-run hint instead of failing the-# install over it.-version_ok="yes"+# Tried once. The probe prints the compiled-in version and does not run the+# first-run kernel venv bootstrap (the pre-warm above stays best-effort; the+# first session retries it), so a failure here is a broken payload and must+# fail installation: a published payload alone does not certify a working+# command.if version_out="$("$install_probe_launcher" --version 2>&1)"; thensay "installed: ${version_out}"else- version_ok="no"+ die "The installed prime-agent --version check failed: ${version_out}"fi# --- the summary ---------------------------------------------------------------------@@ -3170,15 +3186,7 @@ if [ "$ts_stop_rust_stopped" = "yes" ] && [ "$rust_leftover" != "yes" ]; thenfiprintf '\n' >&2-if [ "$version_ok" = yes ]; then- printf ' %sDone.%s Run %sprime-agent%s to start.\n' "$C_BOLD" "$C_RESET" "$C_BOLD" "$C_RESET" >&2-else- note "! Installed, but the first prime-agent --version run failed (it sets up"- note " the kernel on first launch). Its output:"- printf '%s\n' "$version_out" >&2- note " Run it again:"- todo "${launcher} --version"-fi+printf ' %sDone.%s Run %sprime-agent%s to start.\n' "$C_BOLD" "$C_RESET" "$C_BOLD" "$C_RESET" >&2dim "installed to $(tilde "$share_dir") · launcher $(tilde "$launcher")"if [ -n "$ts_takeover_from" ]; thendim "moved the TypeScript install to $(tilde "$ts_takeover_from") · undo:"diff --git a/scripts/release/test_npm_bridge.py b/scripts/release/test_npm_bridge.pyindex f6aca2debe..efa2d524af 100644--- a/scripts/release/test_npm_bridge.py+++ b/scripts/release/test_npm_bridge.py@@ -450,6 +450,10 @@ if [ "$1" = root ]; then echo "$TEST_NPM_ROOT"; else echo uninstall >> "$TEST_UN"primeAgentRustBridge": bridge}))result = subprocess.run(["sh", "-eu"], input='''WINDOWS=no+bin_dir="$TEST_NPM_ROOT"+launcher="$bin_dir/launcher"+printf '#!/bin/sh\\nexit 0\\n' > "$launcher"+chmod 0755 "$launcher"step_start() { :; }; step_ok() { :; }; step_fail() { :; }; note() { :; }; todo() { :; }''' + retirement, text=True, capture_output=True, env={**self.env,"PATH": str(fake_bin) + os.pathsep + self.env["PATH"],@@ -459,5 +463,77 @@ step_start() { :; }; step_ok() { :; }; step_fail() { :; }; note() { :; }; todo()self.assertEqual(uninstalls.exists(), expected)[email protected](sys.platform in ("darwin", "linux") and shutil.which("npm"),+ "requires npm on a historical TypeScript platform")+class InstallerTakeoverTests(unittest.TestCase):+ def test_npm_retirement_leaves_working_native_launcher(self):+ installer = (Path(__file__).resolve().parents[2] / "install-rust.sh").read_text()+ launcher_section = installer.split("# --- the launcher (the takeover lives here)", 1)[1].split(+ "# The cmd/PowerShell launcher twin", 1)[0]+ retirement = installer.split("# The TS npm package:", 1)[1].split(+ "# --- the kernel pre-warm:", 1)[0]+ recovery = installer.split("on_exit() {", 1)[1].split(' [ -n "$launcher_tmp" ]', 1)[0]+ for shared, uninstall_fails in ((True, 0), (False, 0), (True, 1), (True, 2)):+ with self.subTest(shared=shared, uninstall_fails=uninstall_fails), tempfile.TemporaryDirectory() as temp:+ root = Path(temp)+ prefix = root / "native prefix"+ npm_prefix = prefix if shared else root / "npm prefix"+ package = npm_prefix / "lib/node_modules/prime-agent"+ package.mkdir(parents=True)+ (package / "package.json").write_text(json.dumps({+ "name": "prime-agent", "version": "0.9.8", "bin": {"prime-agent": "cli.js"}}))+ (package / "cli.js").write_text("#!/usr/bin/env node\nconsole.log('0.9.8');\n")+ (package / "cli.js").chmod(0o755)+ (npm_prefix / "bin").mkdir()+ (npm_prefix / "bin/prime-agent").symlink_to("../lib/node_modules/prime-agent/cli.js")+ (prefix / "bin").mkdir(parents=True, exist_ok=True)+ payload = prefix / "share/prime-agent"+ payload.mkdir(parents=True)+ (payload / "prime-agent").write_text("#!/bin/sh\nprintf '0.10.0\\n'\n")+ (payload / "prime-agent").chmod(0o755)+ env = {"PATH": os.environ["PATH"], "HOME": temp, "PREFIX": str(prefix),+ "UVPY": sys.executable, "TEST_UNINSTALL_FAIL": str(uninstall_fails),+ "npm_config_prefix": str(npm_prefix),+ "npm_config_cache": str(root / "cache"),+ "npm_config_userconfig": str(root / "empty-user-npmrc"),+ "npm_config_globalconfig": str(root / "empty-global-npmrc")}+ result = subprocess.run(["sh", "-eu"], input='''+bin_dir="$PREFIX/bin"+launcher="$bin_dir/prime-agent"+WINDOWS=no+say() { :; }; note() { :; }; die() { echo "$*" >&2; exit 1; }+step_start() { :; }; step_ok() { :; }; step_fail() { :; }; todo() { :; }+ui_stop() { :; }; publish_window_restore() { :; }+''' + 'recover() {\n' + recovery + '\n}\n' + '''+trap 'recover; [ -z "${launcher_tmp:-}" ] || rm -f "$launcher_tmp"' EXIT+trap 'exit 143' TERM+npm() {+ command npm "$@" || return $?+ if [ "$1" = uninstall ]; then+ case "$TEST_UNINSTALL_FAIL" in+ 1) return 42 ;;+ 2) kill -TERM "$$" ;;+ esac+ fi+}+''' + '# --- the launcher (the takeover lives here)' + launcher_section + '# The TS npm package:' + retirement + '\n"$launcher" --version\n',+ env=env, text=True, capture_output=True, timeout=30)+ self.assertFalse(package.exists(), result.stderr)+ self.assertEqual(result.returncode, 143 if uninstall_fails == 2 else 0, result.stderr)+ probe = subprocess.run([str(prefix / "bin/prime-agent"), "--version"],+ env=env, text=True, capture_output=True, timeout=10)+ self.assertEqual((probe.returncode, probe.stdout.strip()), (0, "0.10.0"), probe.stderr)+ self.assertEqual(list((prefix / "bin").glob(".prime-agent.*")), [])++ def test_failed_version_probe_is_install_failure(self):+ installer = (Path(__file__).resolve().parents[2] / "install-rust.sh").read_text()+ verification = installer.split("# --- verify: the launcher must answer --version", 1)[1].split("\n", 1)[1].split("# --- the summary", 1)[0]+ result = subprocess.run(["sh", "-eu"], input='''+install_probe_launcher=/nonexistent/prime-agent+say() { :; }; die() { echo "$*" >&2; exit 1; }+''' + verification, text=True, capture_output=True)+ self.assertNotEqual(result.returncode, 0, "a broken launcher must not certify installation")++if __name__ == "__main__":unittest.main()