From d37c9b4e15ce4e37e162159271475acd5aa39322 Mon Sep 17 00:00:00 2001 From: "lusendong.6789" Date: Fri, 4 Dec 2026 02:55:50 +0800 Subject: [PATCH] Find Node installed in the Python tool environment Signed-off-by: lusendong.6789 --- loopx/control_plane/runtime/node_probe.py | 32 +++++++++++++++---- loopx/entrypoint.py | 5 ++- .../test_effect_runtime_node_probe.py | 22 +++++++++++++ .../test_turn_host_decision_capture.py | 5 ++- tests/test_cli_entrypoint.py | 18 +++++++---- 5 files changed, 66 insertions(+), 16 deletions(-) diff --git a/loopx/control_plane/runtime/node_probe.py b/loopx/control_plane/runtime/node_probe.py index d9c485af69..ed35970ac8 100644 --- a/loopx/control_plane/runtime/node_probe.py +++ b/loopx/control_plane/runtime/node_probe.py @@ -6,6 +6,7 @@ """ from __future__ import annotations +import os import platform import re import shutil @@ -46,7 +47,9 @@ class NodeProbeOutcome(StrEnum): _FAILURES = { NodeProbeOutcome.MISSING: ( - "node_unavailable", f"LoopX Effect runtime requires Node.js {MINIMUM_NODE_VERSION_TEXT} or newer on PATH", + "node_unavailable", + f"LoopX Effect runtime requires Node.js {MINIMUM_NODE_VERSION_TEXT} or newer on PATH " + "or in the current Python environment's scripts directory", ), NodeProbeOutcome.UNSUPPORTED: ( "node_unavailable", f"LoopX Effect runtime requires Node.js {MINIMUM_NODE_VERSION_TEXT} or newer", @@ -69,10 +72,10 @@ class NodeProbeOutcome(StrEnum): } _REMEDIATION = { "node_unavailable": f"Install or activate Node.js {MINIMUM_NODE_VERSION_TEXT} or newer on PATH, then run `loopx doctor --deep`.", - "node_probe_timeout": "Check host load and the Node.js launcher on PATH, then rerun `loopx doctor --deep`. A timed-out probe does not establish version incompatibility.", - "node_probe_launch_failed": "Repair the Node.js launcher on PATH, then rerun `loopx doctor --deep`.", - "node_probe_exit_failed": "Repair the Node.js launcher on PATH, then rerun `loopx doctor --deep`.", - "node_probe_invalid_version": "Verify that the Node.js launcher on PATH returns a valid version, then rerun `loopx doctor --deep`.", + "node_probe_timeout": "Check host load and the selected Node.js launcher, then rerun `loopx doctor --deep`. A timed-out probe does not establish version incompatibility.", + "node_probe_launch_failed": "Repair the Node.js launcher, then rerun `loopx doctor --deep`.", + "node_probe_exit_failed": "Repair the Node.js launcher, then rerun `loopx doctor --deep`.", + "node_probe_invalid_version": "Verify that the selected Node.js launcher returns a valid version, then rerun `loopx doctor --deep`.", "runtime_host_permission_denied": HOST_PERMISSION_RECOMMENDATION, } @@ -111,13 +114,30 @@ def recommended_action(self) -> str | None: return node_probe_remediation(self.diagnostic_code or "") +def resolve_node_executable() -> str | None: + """Find Node on PATH or among scripts installed for this Python interpreter.""" + executable = shutil.which("node") + if executable is not None: + return executable + + # Exported tool commands can use a private interpreter without putting + # that interpreter's other installed scripts on the caller's PATH. + from sysconfig import get_path + + name = "node.exe" if sys.platform == "win32" else "node" + executable = os.path.join(get_path("scripts"), name) + if os.path.isfile(executable) and os.access(executable, os.X_OK): + return executable + return None + + def probe_node(*, timeout: float = STARTUP_READY_TIMEOUT_SECONDS) -> NodeProbe: # Pure callers import this module cold; the process transport stays # unselected until a probe actually launches a child (see # tests/test_process_runtime_loading.py). from ...extensions.process_runtime import no_console_creationflags - executable = shutil.which("node") + executable = resolve_node_executable() if executable is None: return NodeProbe(NodeProbeOutcome.MISSING) try: diff --git a/loopx/entrypoint.py b/loopx/entrypoint.py index 6007537443..e65e223acc 100644 --- a/loopx/entrypoint.py +++ b/loopx/entrypoint.py @@ -1,12 +1,11 @@ from __future__ import annotations import os -import shutil import sys from pathlib import Path from . import __version__ -from .control_plane.runtime.node_probe import node_typescript_flags +from .control_plane.runtime.node_probe import node_typescript_flags, resolve_node_executable def pin_utf8_stdio() -> None: @@ -94,7 +93,7 @@ def _native_scheduler_followup_argv(raw_argv: list[str]) -> list[str] | None: return None if not _bound_option_present(raw_argv, "--turn-instance-id"): return None - node = shutil.which("node") + node = resolve_node_executable() if node is None: raise RuntimeError("native scheduler follow-up requires Node.js 22.22.3 or newer") entry = ( diff --git a/tests/control_plane/test_effect_runtime_node_probe.py b/tests/control_plane/test_effect_runtime_node_probe.py index 058d979b08..065f60d7e6 100644 --- a/tests/control_plane/test_effect_runtime_node_probe.py +++ b/tests/control_plane/test_effect_runtime_node_probe.py @@ -5,6 +5,7 @@ import shutil import subprocess import sys +import sysconfig from pathlib import Path from types import SimpleNamespace @@ -98,6 +99,27 @@ def fail(*_args, **_kwargs): assert "reinstall LoopX" in result["recommended_action"] +def test_node_resolution_prefers_path(monkeypatch): + monkeypatch.setattr(shutil, "which", lambda name: "/external/node" if name == "node" else None) + monkeypatch.setattr(sysconfig, "get_path", lambda _: pytest.fail("PATH already provided Node")) + + assert node_probe.resolve_node_executable() == "/external/node" + + +@pytest.mark.parametrize(("system", "name"), [("darwin", "node"), ("win32", "node.exe")]) +def test_node_resolution_checks_python_environment_scripts(tmp_path, monkeypatch, system, name): + monkeypatch.setattr(shutil, "which", lambda _: None) + monkeypatch.setattr(sysconfig, "get_path", lambda key: {"scripts": str(tmp_path)}[key]) + monkeypatch.setattr(node_probe, "sys", SimpleNamespace(platform=system)) + script = tmp_path / name + script.mkdir() + assert node_probe.resolve_node_executable() is None + + script.rmdir() + script.touch(mode=0o755) + assert node_probe.resolve_node_executable() == str(script) + + def test_node_probe_uses_the_existing_bounded_startup_budget(monkeypatch): observed = [] monkeypatch.setattr(node_probe.shutil, "which", lambda _: "node") diff --git a/tests/control_plane/test_turn_host_decision_capture.py b/tests/control_plane/test_turn_host_decision_capture.py index 6234cbf806..6318526ca3 100644 --- a/tests/control_plane/test_turn_host_decision_capture.py +++ b/tests/control_plane/test_turn_host_decision_capture.py @@ -423,7 +423,10 @@ def unexpected_effect(*_args, **_kwargs): spend=unexpected_effect, scheduler=unexpected_effect, admit_start=unexpected_effect, decision_capture_payload=original, require_decision_capture=True) assert refused.value.diagnostic_code == "node_unavailable" - assert str(refused.value) == "LoopX Effect runtime requires Node.js 22.22.3 or newer on PATH" + assert str(refused.value) == ( + "LoopX Effect runtime requires Node.js 22.22.3 or newer on PATH " + "or in the current Python environment's scripts directory" + ) assert len(probes) == (2 if optional_context else 1) assert len(operations) == len(probes) if optional_context: diff --git a/tests/test_cli_entrypoint.py b/tests/test_cli_entrypoint.py index 1670473d08..1730402591 100644 --- a/tests/test_cli_entrypoint.py +++ b/tests/test_cli_entrypoint.py @@ -1933,13 +1933,16 @@ def test_release_launchers_use_lightweight_entrypoint() -> None: def test_console_entrypoint_selects_only_receipt_bound_native_scheduler_followup( - monkeypatch, + monkeypatch, tmp_path, ) -> None: + import sysconfig + from loopx import entrypoint - monkeypatch.setattr( - shutil, "which", lambda value: "/fixture/node" if value == "node" else None - ) + monkeypatch.setattr(shutil, "which", lambda _: None) + monkeypatch.setattr(sysconfig, "get_path", lambda key: {"scripts": str(tmp_path)}[key]) + node = tmp_path / ("node.exe" if sys.platform == "win32" else "node") + node.touch(mode=0o755) args = [ "--format", "json", @@ -1962,7 +1965,7 @@ def test_console_entrypoint_selects_only_receipt_bound_native_scheduler_followup assert native is not None assert native[:3] == [ - "/fixture/node", + str(node), "--no-warnings", "--experimental-strip-types", ] @@ -2159,7 +2162,10 @@ def test_history_reader_retains_required_native_runtime_failure(tmp_path): assert selected.returncode == full.returncode == 1 assert selected.stdout == full.stdout assert selected.stderr == full.stderr == "" - assert json.loads(selected.stdout)["error"] == "LoopX Effect runtime requires Node.js 22.22.3 or newer on PATH" + assert json.loads(selected.stdout)["error"] == ( + "LoopX Effect runtime requires Node.js 22.22.3 or newer on PATH " + "or in the current Python environment's scripts directory" + ) @pytest.mark.parametrize("case", ["current", "other-agent", "stale", "no-node"])