Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions loopx/control_plane/runtime/node_probe.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"""
from __future__ import annotations

import os
import platform
import re
import shutil
Expand Down Expand Up @@ -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",
Expand All @@ -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,
}

Expand Down Expand Up @@ -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:
Expand Down
5 changes: 2 additions & 3 deletions loopx/entrypoint.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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 = (
Expand Down
22 changes: 22 additions & 0 deletions tests/control_plane/test_effect_runtime_node_probe.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import shutil
import subprocess
import sys
import sysconfig
from pathlib import Path
from types import SimpleNamespace

Expand Down Expand Up @@ -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")
Expand Down
5 changes: 4 additions & 1 deletion tests/control_plane/test_turn_host_decision_capture.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
18 changes: 12 additions & 6 deletions tests/test_cli_entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
]
Expand Down Expand Up @@ -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"])
Expand Down