Skip to content

Personal AI, On Personal Devices

Python

fix(cli): verify legacy daemon execution boundaries

Elliot Slusky37394c8f081b
diff --git a/src/openjarvis/cli/daemon_cmd.py b/src/openjarvis/cli/daemon_cmd.py
index 12d633f4..b1342c17 100644
--- a/src/openjarvis/cli/daemon_cmd.py
+++ b/src/openjarvis/cli/daemon_cmd.py
@@ -4,6 +4,7 @@ from __future__ import annotations
import json
import os
+import re
import secrets
import subprocess
import sys
@@ -73,31 +74,80 @@ def _read_pid_identity() -> dict:
return identity
+def _legacy_cli_args(argv: list[str]) -> list[str] | None:
+ """Find CLI arguments without treating script arguments as Python options."""
+ entry_points = {"jarvis", "jarvis.exe"}
+ if not argv:
+ return None
+ executable = Path(argv[0]).name
+ if executable in entry_points:
+ return argv[1:]
+ if not re.fullmatch(
+ r"(?:python|pypy)(?:\d+(?:\.\d+)*)?[tdm]?(?:\.exe)?", executable
+ ):
+ return None
+
+ index = 1
+ while index < len(argv):
+ arg = argv[index]
+ if arg == "--":
+ index += 1
+ break
+ if not arg.startswith("-") or arg == "-":
+ break
+ if arg == "--check-hash-based-pycs":
+ index += 2 # Its value is not a module or script argument.
+ continue
+ if arg.startswith("--check-hash-based-pycs="):
+ index += 1
+ continue
+ # Python accepts combined flags (-uO) and attached values (-Wignore,
+ # -Xdev, -mopenjarvis.cli). Value-taking flags consume the remainder.
+ options = arg[1:]
+ while options:
+ option, options = options[0], options[1:]
+ if option == "c":
+ return None
+ if option in "mWX":
+ if not options:
+ index += 1
+ if index >= len(argv):
+ return None
+ options = argv[index]
+ if option == "m":
+ return argv[index + 1 :] if options == "openjarvis.cli" else None
+ break
+ if option not in "bBdEiIOPqRsSuvx":
+ return None
+ index += 1
+ # The first positional argument is the script. Never look beyond it for
+ # -m: those tokens belong to that script, not the Python interpreter.
+ if index < len(argv) and Path(argv[index]).name in entry_points:
+ return argv[index + 1 :]
+ return None
+
+
def _legacy_linux_daemon(pid: int) -> bool | None:
"""Recognize an old registration before it has a saved process identity."""
try:
- argv = [
- os.fsdecode(arg)
- for arg in Path(f"/proc/{pid}/cmdline").read_bytes().split(b"\0")
- if arg
- ]
+ arguments = Path(f"/proc/{pid}/cmdline").read_bytes().split(b"\0")
except OSError:
return None
- # Console entry points are either argv[0] or the script passed to Python.
- for index in (0, 1):
- if len(argv) > index and Path(argv[index]).name in {"jarvis", "jarvis.exe"}:
- command = argv[index + 1 :]
- break
- else:
- # `start` uses this form; supervised servers may add interpreter flags.
- try:
- index = argv.index("-m")
- except ValueError:
- return False
- if "-c" in argv[:index] or argv[index + 1 : index + 2] != ["openjarvis.cli"]:
- return False
- command = argv[index + 2 :]
- return next((arg for arg in command if not arg.startswith("-")), None) == "serve"
+ if arguments and arguments[-1] == b"":
+ arguments.pop()
+ # Empty option values still occupy an argument; dropping them shifts the
+ # execution boundary and can make a script's own -m look like Python's.
+ argv = [os.fsdecode(arg) for arg in arguments]
+ command = _legacy_cli_args(argv)
+ if command is None:
+ return False
+ # These are the CLI's global boolean options. Help/version and unknown
+ # options do not launch a server, even when followed by the word serve.
+ while command and command[0] in {"--verbose", "--quiet", "--pick-model"}:
+ command = command[1:]
+ if command and command[0] == "--":
+ command = command[1:]
+ return bool(command) and command[0] == "serve"
def _registered_pid_alive(pid: int) -> bool:
@@ -125,7 +175,12 @@ def _registered_pid_alive(pid: int) -> bool:
if confirmed is not None and confirmed != current:
return False
if confirmed is not None:
- secure_write_json(_PID_FILE.with_suffix(".identity.json"), confirmed)
+ try:
+ secure_write_json(_PID_FILE.with_suffix(".identity.json"), confirmed)
+ except OSError:
+ # Migration is optional. A read-only/full filesystem must not
+ # make a verified live daemon inaccessible to status or stop.
+ pass
return True
diff --git a/tests/cli/test_daemon_identity.py b/tests/cli/test_daemon_identity.py
index 426ee7ed..419dd08f 100644
--- a/tests/cli/test_daemon_identity.py
+++ b/tests/cli/test_daemon_identity.py
@@ -105,8 +105,25 @@ def test_reboot_rejects_even_an_identical_pid_and_start_tick(linux_process):
"argv",
[
["/venv/bin/jarvis", "serve"],
+ ["/venv/bin/jarvis", "--quiet", "serve"],
+ ["/venv/bin/jarvis", "--verbose", "--pick-model", "--", "serve"],
["/venv/bin/python3", "/venv/bin/jarvis", "--quiet", "serve"],
+ ["/venv/bin/python3", "-u", "/venv/bin/jarvis", "serve"],
+ ["/venv/bin/python3", "-uO", "-Wignore", "-Xdev", "/venv/bin/jarvis", "serve"],
+ ["/venv/bin/python3", "--", "/venv/bin/jarvis", "serve"],
["/venv/bin/python3", "-u", "-m", "openjarvis.cli", "serve"],
+ ["/venv/bin/python3", "-mopenjarvis.cli", "serve"],
+ ["/venv/bin/python3", "-W", "", "-m", "openjarvis.cli", "serve"],
+ [
+ "/venv/bin/python3",
+ "-W",
+ "ignore",
+ "-X",
+ "dev",
+ "-m",
+ "openjarvis.cli",
+ "serve",
+ ],
],
)
def test_live_legacy_daemon_is_migrated_without_wall_clock(linux_process, argv):
@@ -124,7 +141,23 @@ def test_live_legacy_daemon_is_migrated_without_wall_clock(linux_process, argv):
"argv",
[
["/usr/bin/python3", "-c", "import time; time.sleep(30)"],
+ ["/usr/bin/python3", "/tmp/worker.py", "-m", "openjarvis.cli", "serve"],
+ ["/usr/bin/python3", "-u", "/tmp/worker.py", "-m", "openjarvis.cli", "serve"],
+ ["/usr/bin/python3", "-c", "pass", "-m", "openjarvis.cli", "serve"],
+ ["/usr/bin/python3", "--", "-m", "openjarvis.cli", "serve"],
+ ["/usr/bin/python3", "-W", "-m", "openjarvis.cli", "serve"],
+ [
+ "/usr/bin/python3",
+ "-W",
+ "",
+ "/tmp/worker.py",
+ "-m",
+ "openjarvis.cli",
+ "serve",
+ ],
+ ["/usr/bin/not-python", "-m", "openjarvis.cli", "serve"],
["/venv/bin/jarvis", "ask", "serve"],
+ ["/venv/bin/jarvis", "--help", "serve"],
["/usr/bin/python3", "-m", "other", "-m", "openjarvis.cli", "serve"],
],
)
@@ -174,6 +207,34 @@ def test_legacy_migration_rechecks_identity_after_reading_command(
assert daemon_cmd._read_pid_identity() == {}
+def test_legacy_migration_write_failure_keeps_verified_daemon(
+ linux_process, monkeypatch
+):
+ daemon_cmd._PID_FILE.write_text("4321")
+ daemon_cmd._STATE_FILE.write_text(
+ json.dumps({"pid": 4321, "host": "127.0.0.1", "port": 8899})
+ )
+ monkeypatch.setattr(
+ daemon_cmd,
+ "secure_write_json",
+ Mock(side_effect=PermissionError("state directory is read-only")),
+ )
+ terminate = Mock()
+ monkeypatch.setattr(daemon_cmd, "terminate_process", terminate)
+
+ status = CliRunner().invoke(daemon_cmd.status)
+ assert status.exit_code == 0, status.output
+ assert "Server is running" in status.output
+ assert daemon_cmd._PID_FILE.read_text() == "4321"
+ assert daemon_cmd._read_pid_identity() == {}
+ terminate.assert_not_called()
+
+ stopped = CliRunner().invoke(daemon_cmd.stop)
+ assert stopped.exit_code == 0, stopped.output
+ terminate.assert_called_once_with(4321, grace_seconds=10.0)
+ assert not daemon_cmd._PID_FILE.exists()
+
+
def test_child_promotion_preserves_the_child_identity(linux_process):
daemon_cmd._write_pid(4321, "127.0.0.1", 0, ready=False, launch_token="launch-a")
daemon_cmd._write_pid(9876, "127.0.0.1", 8899, launch_token="launch-a")