A self-improving RLM agent for coding workflows and long-running autonomous tasks.
Rust
keep installer socket pins within the Unix byte limit (#3555)
Sebastian Müller21bdf28e94ac
diff --git a/.changes/fix-installer-socket-path-limit.md b/.changes/fix-installer-socket-path-limit.mdnew file mode 100644index 0000000000..d44771d5a7--- /dev/null+++ b/.changes/fix-installer-socket-path-limit.md@@ -0,0 +1 @@+- On macOS, a long `TMPDIR` no longer breaks the daemon socket for installer and npm installs: when the default socket path would exceed the 103-byte limit, it moves to `/tmp/prime-agent-rust-<uid>`.diff --git a/install-rust.sh b/install-rust.shindex 4884a0145d..edf6712e92 100755--- a/install-rust.sh+++ b/install-rust.sh@@ -1606,6 +1606,12 @@ ts_stop_rust_stopped=""windows_stop_summary=""ts_socket="${TMPDIR:-/tmp}/prime-agent-$(id -u)/daemon.sock"rust_socket="${TMPDIR:-/tmp}/prime-agent-rust-$(id -u)/daemon.sock"+# The launcher pins /tmp when the TMPDIR path exceeds the non-Linux+# 103-byte sun_path limit (pa-daemon socket_dir() owns the rule); the+# stop probe must look where the launcher actually pinned.+if [ "$OS" != "Linux" ] && [ "$(printf '%s' "$rust_socket" | wc -c)" -gt 103 ]; then+ rust_socket="/tmp/prime-agent-rust-$(id -u)/daemon.sock"+fienv_socket="${PRIME_AGENT_DAEMON_SOCKET:-}"# THE TILDE EXPANSION (the CLI's own spelling: the products' clients# expand a leading ~/ in PRIME_AGENT_DAEMON_SOCKET before connecting, so@@ -2847,7 +2853,15 @@ export PRIME_AGENT_CODING_AGENT_DIR="${PRIME_AGENT_CODING_AGENT_DIR:-$HOME/.primif [ -f "$(dirname "$0")/../share/.prime-agent-npm-bridge/legacy-daemon-socket" ]; thenexport PRIME_AGENT_DAEMON_SOCKET="${PRIME_AGENT_DAEMON_SOCKET:-${TMPDIR:-${TMP:-${TEMP:-/tmp}}}/prime-agent-$(id -u)/daemon.sock}"else- export PRIME_AGENT_DAEMON_SOCKET="${PRIME_AGENT_DAEMON_SOCKET:-${TMPDIR:-/tmp}/prime-agent-rust-$(id -u)/daemon.sock}"+ if [ -z "${PRIME_AGENT_DAEMON_SOCKET:-}" ]; then+ # Same rule as pa-daemon socket_dir(): non-Linux sun_path caps a unix+ # socket at 103 bytes, so a too-deep TMPDIR pins /tmp/prime-agent-rust-<uid>.+ PRIME_AGENT_DAEMON_SOCKET="${TMPDIR:-/tmp}/prime-agent-rust-$(id -u)/daemon.sock"+ if [ "$(printf '%s' "$PRIME_AGENT_DAEMON_SOCKET" | wc -c)" -gt 103 ] && [ "$(uname -s)" != "Linux" ]; then+ PRIME_AGENT_DAEMON_SOCKET="/tmp/prime-agent-rust-$(id -u)/daemon.sock"+ fi+ export PRIME_AGENT_DAEMON_SOCKET+ fifiexec "$(dirname "$0")/../share/prime-agent/prime-agent" "$@"EOFdiff --git a/scripts/release/npm_bridge_assets/cli.cjs b/scripts/release/npm_bridge_assets/cli.cjsindex 4c8708a930..a89471a703 100644--- a/scripts/release/npm_bridge_assets/cli.cjs+++ b/scripts/release/npm_bridge_assets/cli.cjs@@ -47,11 +47,15 @@ const receipt = path.join(receiptDir, metadata.version);const failedReceipt = `${receipt}.failed`;const legacySocketMarker = path.join(receiptDir, "legacy-daemon-socket");const legacySocket = path.join(os.tmpdir(), `prime-agent-${process.getuid()}`, "daemon.sock");+const rustSocket = path.join(process.env.TMPDIR || "/tmp", `prime-agent-rust-${process.getuid()}`, "daemon.sock");+// Same rule as pa-daemon socket_dir(): non-Linux sun_path caps a unix+// socket at 103 bytes, so a too-deep TMPDIR pins /tmp/prime-agent-rust-<uid>.+const pinnedRustSocket = process.platform !== "linux" && Buffer.byteLength(rustSocket) > 103+ ? path.join("/tmp", `prime-agent-rust-${process.getuid()}`, "daemon.sock") : rustSocket;const nativeEnvironment = { ...process.env,PRIME_AGENT_CODING_AGENT_DIR: process.env.PRIME_AGENT_CODING_AGENT_DIR || path.join(os.homedir(), ".prime/agent"),PRIME_AGENT_DAEMON_SOCKET: process.env.PRIME_AGENT_DAEMON_SOCKET ||- (coordinator || fs.existsSync(legacySocketMarker) ? legacySocket :- path.join(process.env.TMPDIR || "/tmp", `prime-agent-rust-${process.getuid()}`, "daemon.sock")),+ (coordinator || fs.existsSync(legacySocketMarker) ? legacySocket : pinnedRustSocket),};delete nativeEnvironment.PI_PACKAGE_DIR;const signals = ["SIGINT", "SIGTERM", "SIGHUP"];diff --git a/scripts/release/test_npm_bridge.py b/scripts/release/test_npm_bridge.pyindex efa2d524af..af47dfa6e3 100644--- a/scripts/release/test_npm_bridge.py+++ b/scripts/release/test_npm_bridge.py@@ -319,22 +319,35 @@ child.on("exit",code=>{process.exitCode=code;});custom = self.launch("socket", PRIME_AGENT_DAEMON_SOCKET=str(self.root / "custom.sock"))self.assertEqual(custom.stdout.strip(), str(self.root / "custom.sock"))- def test_real_installer_preserves_node_relaunch_only_for_its_npm_bridge(self):+ def install_real_launcher(self, **environment):+ # Reuse the real published launcher section, not the fixture+ # installer: an old TUI captures the public symlink path in Node's+ # argv before npm replaces the package, and the launcher this+ # section writes is the file users run after the takeover.installer = (Path(__file__).resolve().parents[2] / "install-rust.sh").read_text()section = installer.split("# --- the launcher (the takeover lives here)", 1)[1]section = section.split("# The cmd/PowerShell launcher twin", 1)[0]- # Reuse the real published launcher section, not the fixture installer:- # an old TUI captures the public symlink path in Node's argv before npm- # replaces the package. That path must still contain JavaScript later.section = "# --- the launcher (the takeover lives here)" + sectionself.env["PRIME_AGENT_RUST_PREFIX"] = str(self.npm_prefix)payload = self.npm_prefix / "share/prime-agent"- payload.mkdir(parents=True)+ payload.mkdir(parents=True, exist_ok=True)shutil.copyfile(self.fixture, payload / "prime-agent")(payload / "prime-agent").chmod(0o755)(payload / "version").write_text("1.0.0\n")+ return subprocess.run(["sh", "-eu"], input='''+PREFIX="$PRIME_AGENT_RUST_PREFIX"+bin_dir="$PREFIX/bin"+launcher="$bin_dir/prime-agent"+WINDOWS=no+say() { :; }; note() { :; }; die() { echo "$*" >&2; exit 1; }+trap '[ -z "${launcher_tmp:-}" ] || rm -f "$launcher_tmp"' EXIT+''' + section + '\n"$install_probe_launcher" --version\n',+ text=True, capture_output=True, timeout=10,+ env={**self.env, **environment})++ def test_real_installer_preserves_node_relaunch_only_for_its_npm_bridge(self):receipt_dir = self.npm_prefix / "share/.prime-agent-npm-bridge"- receipt_dir.mkdir()+ receipt_dir.mkdir(parents=True)(receipt_dir / "1.0.0").write_text("installed\n")command = self.npm_prefix / "bin/prime-agent"foreign_entry = self.root / "unrelated.js"@@ -345,18 +358,10 @@ child.on("exit",code=>{process.exitCode=code;});with self.subTest(flag=flag, target=target):command.unlink(missing_ok=True)command.symlink_to(target)- result = subprocess.run(["sh", "-eu"], input='''-PREFIX="$PRIME_AGENT_RUST_PREFIX"-bin_dir="$PREFIX/bin"-launcher="$bin_dir/prime-agent"-WINDOWS=no-say() { :; }; note() { :; }; die() { echo "$*" >&2; exit 1; }-trap '[ -z "${launcher_tmp:-}" ] || rm -f "$launcher_tmp"' EXIT-''' + section + '\n"$install_probe_launcher" --version\n',- text=True, capture_output=True, timeout=10, env={**self.env,- "PRIME_AGENT_PRESERVE_NPM_BRIDGE": flag,- "PRIME_AGENT_NPM_BRIDGE_ENTRYPOINT": str(self.entrypoint),- "PRIME_AGENT_USE_LEGACY_DAEMON_SOCKET": "1"})+ result = self.install_real_launcher(+ PRIME_AGENT_PRESERVE_NPM_BRIDGE=flag,+ PRIME_AGENT_NPM_BRIDGE_ENTRYPOINT=str(self.entrypoint),+ PRIME_AGENT_USE_LEGACY_DAEMON_SOCKET="1")self.assertEqual(result.returncode, 0, result.stderr)self.assertEqual(result.stdout.strip(), "1.0.0")self.assertEqual(command.is_symlink(), preserved)@@ -367,6 +372,64 @@ trap '[ -z "${launcher_tmp:-}" ] || rm -f "$launcher_tmp"' EXITself.assertEqual(relaunched.stdout.strip(), "1.0.0")self.assertEqual(list(command.parent.glob(".prime-agent.*")), [])+ def test_real_launcher_pin_respects_the_sun_path_limit(self):+ # The launcher pins the daemon socket env; a TMPDIR whose pin cannot+ # fit the non-Linux 103-byte sun_path limit must pin /tmp instead+ # (the same fallback pa-daemon socket_dir() gives the binary default).+ command = self.npm_prefix / "bin/prime-agent"+ result = self.install_real_launcher()+ self.assertEqual(result.returncode, 0, result.stderr)+ # The PATH shim makes both arms testable on every platform.+ shim_dir = self.root / "uname-shim"+ shim_dir.mkdir()+ long_tmp = self.root / ("t" * 80)+ for system, root in (("Darwin", "/tmp"), ("Linux", str(long_tmp))):+ with self.subTest(system=system):+ (shim_dir / "uname").write_text(f'#!/bin/sh\necho {system}\n')+ (shim_dir / "uname").chmod(0o755)+ probe_env = {**self.env, "TMPDIR": str(long_tmp),+ "PATH": str(shim_dir) + os.pathsep + self.env["PATH"]}+ probe_env.pop("PRIME_AGENT_DAEMON_SOCKET", None)+ probe = subprocess.run([str(command), "socket"], env=probe_env,+ text=True, capture_output=True, timeout=10)+ self.assertEqual(probe.returncode, 0, probe.stderr)+ self.assertEqual(probe.stdout.strip(),+ f"{root}/prime-agent-rust-{os.getuid()}/daemon.sock")++ def test_utf8_socket_limit_applies_to_launcher_and_stop_probe(self):+ result = self.install_real_launcher()+ self.assertEqual(result.returncode, 0, result.stderr)+ command = self.npm_prefix / "bin/prime-agent"+ installer = (Path(__file__).resolve().parents[2] / "install-rust.sh").read_text()+ stop_probe = installer.split('rust_socket="${TMPDIR:-/tmp}', 1)[1]+ stop_probe = 'rust_socket="${TMPDIR:-/tmp}' + stop_probe.split('env_socket=', 1)[0]+ shim_dir = self.root / "utf8-uname-shim"+ shim_dir.mkdir()+ tmp = "/tmp/" + "é" * 40+ pin = f"{tmp}/prime-agent-rust-{os.getuid()}/daemon.sock"+ self.assertLessEqual(len(pin), 103)+ self.assertGreater(len(pin.encode()), 103)+ for system in ("Darwin", "Linux"):+ with self.subTest(system=system):+ (shim_dir / "uname").write_text(f"#!/bin/sh\necho {system}\n")+ (shim_dir / "uname").chmod(0o755)+ env = {**self.env, "OS": system, "TMPDIR": tmp,+ "LC_ALL": "en_US.UTF-8",+ "PATH": str(shim_dir) + os.pathsep + self.env["PATH"]}+ env.pop("PRIME_AGENT_DAEMON_SOCKET", None)+ expected = f"/tmp/prime-agent-rust-{os.getuid()}/daemon.sock" if system == "Darwin" else pin+ for shell in ("sh", "bash"):+ with self.subTest(shell=shell):+ probe = subprocess.run([shell, str(command), "socket"], env=env,+ text=True, capture_output=True, timeout=10)+ self.assertEqual(probe.returncode, 0, probe.stderr)+ self.assertEqual(probe.stdout.strip(), expected)+ stop = subprocess.run([shell, "-eu"], env=env,+ input=stop_probe + '\nprintf "%s" "$rust_socket"\n',+ text=True, capture_output=True, timeout=10)+ self.assertEqual(stop.returncode, 0, stop.stderr)+ self.assertEqual(stop.stdout, expected)+def test_stale_bridge_does_not_downgrade_newer_native_without_receipt(self):payload = self.prefix / "share/prime-agent"payload.mkdir(parents=True)@@ -385,6 +448,14 @@ else console.log(JSON.stringify(process.argv.slice(2)));self.env["TMPDIR"] = str(self.root / "sockets")expected = str(self.root / "sockets" / f"prime-agent-rust-{os.getuid()}" / "daemon.sock")self.assertEqual(self.launch("socket").stdout.strip(), expected)+ # A TMPDIR deeper than the non-Linux 103-byte sun_path limit pins+ # /tmp instead (node's platform cannot be shimmed, so the expected+ # value follows the host OS).+ long_tmp = str(self.root / ("t" * 80))+ self.env["TMPDIR"] = long_tmp+ expected = (f"/tmp/prime-agent-rust-{os.getuid()}/daemon.sock" if sys.platform == "darwin"+ else f"{long_tmp}/prime-agent-rust-{os.getuid()}/daemon.sock")+ self.assertEqual(self.launch("socket").stdout.strip(), expected)def test_concurrent_first_launches_install_once(self):processes = [subprocess.Popen(["node", str(self.entrypoint), str(i)], env=self.env,diff --git a/scripts/review_socket_pin_parity.py b/scripts/review_socket_pin_parity.pynew file mode 100644index 0000000000..d948fb8fc8--- /dev/null+++ b/scripts/review_socket_pin_parity.py@@ -0,0 +1,147 @@+#!/usr/bin/env python3+"""Real daemon comparison of preserved legacy sockets and Rust-only byte fallback.++The TS default namespace remains the migration-marker baseline. The new Rust-only+fallback is deliberately distinct; a Darwin uname shim exercises the actual+published launcher on Linux with the exact CI-built Rust payload. Both daemons+run only in fixture-owned homes and socket paths. No installer downloads occur.+"""+import argparse+import json+import os+from pathlib import Path+import shutil+import socket+import subprocess+import tempfile+import threading+import time++from review_draft_disconnect_parity import digest, env_for, stop, TS_ARCHIVE_SHA256+++def launch_daemon(command, expected, root, env):+ log_path = root / 'daemon.log'+ with log_path.open('wb') as log:+ process = subprocess.Popen(command + ['--mode', 'daemon'], cwd=root, env=env,+ stdout=log, stderr=log, start_new_session=True)+ try:+ deadline = time.monotonic() + 35+ while time.monotonic() < deadline:+ if process.poll() is not None:+ raise RuntimeError('daemon exited before bind: ' + log_path.read_text(errors='replace'))+ try:+ with socket.socket(socket.AF_UNIX) as sock:+ sock.settimeout(2)+ sock.connect(str(expected))+ with sock.makefile('rb') as stream:+ hello = json.loads(stream.readline())+ assert hello.get('type') == 'daemon_hello' and hello.get('schemaId'), hello+ return {'bound_socket': str(expected), 'hello': hello,+ 'log': log_path.read_text(errors='replace')}+ except (FileNotFoundError, ConnectionRefusedError, socket.timeout):+ threading.Event().wait(0.05)+ raise TimeoutError('daemon did not bind expected launcher socket: ' + str(expected))+ finally:+ stop(process)+++def install_launcher(binary, root, env):+ prefix = root / 'prefix'+ payload = prefix / 'share/prime-agent'+ payload.mkdir(parents=True)+ (payload / 'prime-agent').symlink_to(binary)+ installer = Path(__file__).resolve().parents[1].joinpath('install-rust.sh').read_text()+ marker = '# --- the launcher (the takeover lives here)'+ section = marker + installer.split(marker, 1)[1]+ section = section.split('# The cmd/PowerShell launcher twin', 1)[0]+ script = '''PREFIX="$PRIME_AGENT_RUST_PREFIX"+bin_dir="$PREFIX/bin"+mkdir -p "$bin_dir"+launcher="$bin_dir/prime-agent"+WINDOWS=no+say() { :; }; note() { :; }; die() { echo "$*" >&2; exit 1; }+trap '[ -z "${launcher_tmp:-}" ] || rm -f "$launcher_tmp"' EXIT+''' + section+ installed = subprocess.run(['sh', '-eu'], input=script,+ env={**env, 'PRIME_AGENT_RUST_PREFIX': str(prefix)},+ cwd=root, text=True, capture_output=True, timeout=15)+ if installed.returncode:+ raise RuntimeError('launcher extraction failed: ' + installed.stderr)+ return prefix / 'bin/prime-agent'+++def main():+ parser = argparse.ArgumentParser()+ parser.add_argument('--receipt', type=Path, required=True)+ args = parser.parse_args()+ receipt = {'passed': False, 'scenario': 'socket_pin', 'runs': {},+ 'scope': 'Actual daemons: legacy namespace compatibility and explicit override; actual Rust launcher and installer stop probe: UTF-8 byte fallback under Darwin uname shim.',+ 'intentional_difference': 'Unmarked Rust launcher uses rust-only namespace; non-Linux oversized pins fall back to /tmp. TS namespace and migration marker remain unchanged.'}+ fallback_dir = None+ try:+ assert digest(Path(os.environ['PA_TS_ARCHIVE'])) == TS_ARCHIVE_SHA256+ ts, rust = (Path(os.environ[key]).resolve() for key in ('PA_TS_BINARY', 'PA_RUST_BINARY'))+ receipt['binary_sha256'] = {'ts': digest(ts), 'rust': digest(rust)}+ with tempfile.TemporaryDirectory(prefix='pa-pin-', dir='/tmp') as temp:+ root = Path(temp)+ for kind, binary in (('ts', ts), ('rust', rust)):+ owned = root / kind+ owned.mkdir()+ env = env_for(owned, kind)+ tmp = owned / 'tmp'+ legacy = tmp / f'prime-agent-{os.getuid()}/daemon.sock'+ command = binary+ if kind == 'rust':+ command = install_launcher(binary, owned, env)+ marker = owned / 'prefix/share/.prime-agent-npm-bridge/legacy-daemon-socket'+ marker.parent.mkdir()+ marker.write_text('fixture\n')+ receipt['runs'][kind + '_legacy'] = launch_daemon([str(command)], legacy, owned, env)+ explicit = tmp / 'explicit.sock'+ receipt['runs'][kind + '_override'] = launch_daemon([str(command), '--daemon-socket', str(explicit)], explicit, owned,+ {**env, 'PRIME_AGENT_DAEMON_SOCKET': str(explicit)})+ if kind == 'rust':+ marker.unlink()+ shim = owned / 'shim'+ shim.mkdir()+ fixture_uid = str(500000000 + os.getpid())+ for name, text in (('uname', 'Darwin'), ('id', fixture_uid)):+ path = shim / name+ path.write_text(f'#!/bin/sh\nprintf "%s\\n" "{text}"\n')+ path.chmod(0o755)+ long_tmp = str(owned / ('é' * 25))+ long_pin = f'{long_tmp}/prime-agent-rust-{fixture_uid}/daemon.sock'+ assert len(long_pin) <= 103 and len(long_pin.encode()) > 103, long_pin+ candidate = Path(f'/tmp/prime-agent-rust-{fixture_uid}')+ # Atomic reservation: a collision never authorizes cleanup.+ candidate.mkdir(mode=0o700, exist_ok=False)+ fallback_dir = candidate+ fallback = fallback_dir / 'daemon.sock'+ long_env = {**env, 'TMPDIR': long_tmp,+ 'PATH': str(shim) + os.pathsep + env['PATH'], 'OS': 'Darwin',+ 'LC_ALL': 'C.UTF-8'}+ receipt['runs']['rust_utf8_fallback'] = launch_daemon([str(command)], fallback,+ owned, long_env)+ source = Path(__file__).resolve().parents[1].joinpath('install-rust.sh').read_text()+ probe = 'rust_socket="${TMPDIR:-/tmp}' + source.split('rust_socket="${TMPDIR:-/tmp}', 1)[1].split('env_socket=', 1)[0]+ observed = subprocess.run(['sh', '-eu'], input=probe + '\nprintf "%s" "$rust_socket"',+ env=long_env, text=True, capture_output=True, timeout=10, check=True).stdout+ assert observed == str(fallback), (observed, fallback)+ receipt['stop_probe'] = observed+ receipt['utf8_pin'] = {'characters': len(long_pin), 'bytes': len(long_pin.encode()),+ 'original_pin': long_pin}+ receipt['passed'] = True+ except Exception as error:+ receipt['error'] = f'{type(error).__name__}: {error}'+ finally:+ if fallback_dir is not None and fallback_dir.exists():+ shutil.rmtree(fallback_dir)+ args.receipt.parent.mkdir(parents=True, exist_ok=True)+ args.receipt.write_text(json.dumps(receipt, indent=2) + '\n')+ print(json.dumps({'passed': receipt['passed'], 'error': receipt.get('error')}))+ return 0 if receipt['passed'] else 1+++if __name__ == '__main__':+ raise SystemExit(main())diff --git a/scripts/test_review_socket_pin_parity.py b/scripts/test_review_socket_pin_parity.pynew file mode 100644index 0000000000..8c0c2f7bf2--- /dev/null+++ b/scripts/test_review_socket_pin_parity.py@@ -0,0 +1,25 @@+#!/usr/bin/env python3+import subprocess+import tempfile+import unittest+from pathlib import Path+from review_socket_pin_parity import install_launcher+from review_draft_disconnect_parity import env_for+++class LauncherExtractionTests(unittest.TestCase):+ def test_real_installer_section_remains_valid_shell(self):+ with tempfile.TemporaryDirectory(prefix='socket-fixture-', dir='/tmp') as temp:+ root = Path(temp)+ fixture = root / 'payload'+ fixture.write_text('#!/bin/sh\nprintf "%s\n" "fixture-payload"\n')+ fixture.chmod(0o755)+ env = env_for(root, 'rust')+ launcher = install_launcher(fixture, root, env)+ result = subprocess.run([str(launcher), '--version'], env=env,+ capture_output=True, text=True, timeout=10, check=True)+ self.assertEqual(result.stdout, 'fixture-payload\n')+++if __name__ == '__main__':+ unittest.main()