Self-improving AI Employees for everyone
Python ai-agentsai-employeeai-employeesassistantautonomous-agentsco-employeescompanygrok-botinstinctloop-agentsmuseown-your-datapersonalself-hosted
refactor(machina): simplify `daemon` to pure-Python detached process (~80 LOC)
trohitgd8f2407de3f1
diff --git a/machina/commands/daemon.py b/machina/commands/daemon.pyindex a09b4e54..7bdbf754 100644--- a/machina/commands/daemon.py+++ b/machina/commands/daemon.py@@ -1,39 +1,32 @@-"""``machina daemon`` -- replaces ``scripts/daemon.js``.+"""``machina daemon`` -- run the backend as a detached process.-Cross-platform service installer:-- Windows -> NSSM-- Linux -> systemd-- macOS -> launchd+Pure-Python, all-platforms. No NSSM / systemd / launchd integration.+Spawns ``uvicorn main:app`` in a new session (POSIX) or detached+process group (Windows), writes the PID under ``~/.machina/`` so the+other verbs can find it, and uses ``psutil`` for tree-kill on stop.-Each verb (install / uninstall / status / start / stop / restart)-dispatches to the appropriate platform implementation. The daemon-runs ``uvicorn main:app --host 0.0.0.0 --port 3010 --log-level warning``-out of ``server/.venv/`` regardless of platform; the only thing that-varies is HOW the OS supervises the resulting process.+For boot-time auto-start, configure your OS service manager+separately (`systemctl`, `launchctl`, Task Scheduler) -- this CLI does+not register itself with the system."""from __future__ import annotationsimport os-import shutil+import signalimport subprocessimport sysfrom pathlib import Path+import psutilimport typer-from dotenv import dotenv_valuesfrom machina.colors import console-from machina.platform_ import IS_MACOS, IS_WINDOWS, project_root---SERVICE_NAME = "MachinaOs"-PLIST_LABEL = "com.machina.backend"-+from machina.platform_ import IS_WINDOWS, project_rootapp = typer.Typer(name="daemon",- help="Install/manage MachinaOS as a system service.",+ help="Run the MachinaOs backend as a detached process.",no_args_is_help=True,add_completion=False,)@@ -41,8 +34,8 @@ app = typer.Typer(# ---------------------------------------------------------------- helpers-def _install_dir(override: str | None) -> Path:- return Path(override).resolve() if override else project_root()+_PID_DIR = Path.home() / ".machina"+_PID_FILE = _PID_DIR / "machina-backend.pid"def _venv_python(install_dir: Path) -> Path:@@ -51,385 +44,112 @@ def _venv_python(install_dir: Path) -> Path:return install_dir / "server" / ".venv" / "bin" / "python"-def _server_dir(install_dir: Path) -> Path:- return install_dir / "server"---def _logs_dir(install_dir: Path) -> Path:- return install_dir / "logs"+def _detached_kwargs() -> dict:+ """Cross-platform "spawn detached, survive parent exit" kwargs."""+ if IS_WINDOWS:+ # CREATE_NEW_PROCESS_GROUP lets us send CTRL_BREAK_EVENT later;+ # DETACHED_PROCESS releases the console handle.+ return {+ "creationflags": (+ subprocess.DETACHED_PROCESS | subprocess.CREATE_NEW_PROCESS_GROUP+ ),+ }+ # POSIX: setsid puts the child in its own process group / session,+ # so killing the parent (this CLI) doesn't take the daemon down.+ return {"start_new_session": True}+++def _read_pid() -> int | None:+ if not _PID_FILE.exists():+ return None+ try:+ pid = int(_PID_FILE.read_text().strip())+ except (ValueError, OSError):+ return None+ return pid if psutil.pid_exists(pid) else None+++def _kill_tree(pid: int) -> None:+ try:+ proc = psutil.Process(pid)+ except psutil.NoSuchProcess:+ return+ for child in proc.children(recursive=True):+ try:+ child.kill()+ except psutil.NoSuchProcess:+ pass+ try:+ proc.terminate()+ proc.wait(timeout=5)+ except psutil.NoSuchProcess:+ return+ except psutil.TimeoutExpired:+ try:+ proc.kill()+ except psutil.NoSuchProcess:+ pass+++# ---------------------------------------------------------------- verbs-def _ensure_built(install_dir: Path) -> None:- py = _venv_python(install_dir)[email protected]("start")+def start_command() -> None:+ """Start the backend in the background; write PID to ~/.machina/."""+ if (existing := _read_pid()) is not None:+ console.print(f"[yellow]Already running (pid={existing}).[/]")+ return++ root = project_root()+ server = root / "server"+ py = _venv_python(root)if not py.exists():- console.print(f"[red]Python venv not found at {py}[/]")- console.print('Run "machina build" first.')+ console.print(f'[red]Python venv not found at {py}.[/] Run "machina build" first.')raise typer.Exit(code=1)+ _PID_DIR.mkdir(parents=True, exist_ok=True)+ log_file = _PID_DIR / "backend.log"+ log = log_file.open("ab")-def _run(argv: list[str], *, ignore_error: bool = False) -> int:- """Inherit-stdio run; honours ``ignore_error`` like the JS version."""- proc = subprocess.run(argv)- if proc.returncode != 0 and not ignore_error:- console.print(f"[red]Command failed:[/] {' '.join(argv)}")- raise typer.Exit(code=proc.returncode)- return proc.returncode---def _has(cmd: str) -> bool:- return shutil.which(cmd) is not None---# ============================================================== Windows--def _nssm_check() -> None:- if not _has("nssm"):- console.print("[red]NSSM not found. Install from https://nssm.cc/[/]")- console.print("Or use: winget install nssm")- raise typer.Exit(code=1)---def _windows_install(install_dir: Path) -> None:- _nssm_check()- _ensure_built(install_dir)-- py = _venv_python(install_dir)- server = _server_dir(install_dir)- logs = _logs_dir(install_dir)- logs.mkdir(parents=True, exist_ok=True)-- console.print(f"Installing {SERVICE_NAME} service...")- console.print(f" Directory: {install_dir}")- console.print(f" Python: {py}")-- # Remove any pre-existing service quietly first.- _run(["nssm", "stop", SERVICE_NAME], ignore_error=True)- _run(["nssm", "remove", SERVICE_NAME, "confirm"], ignore_error=True)-- _run([- "nssm", "install", SERVICE_NAME, str(py),- "-m", "uvicorn", "main:app",- "--host", "0.0.0.0", "--port", "3010", "--log-level", "warning",- ])- _run(["nssm", "set", SERVICE_NAME, "AppDirectory", str(server)])-- # Load .env vars + the standard PYTHON* knobs.- env_pairs = [f"PYTHONPATH={server}", "PYTHONUTF8=1"]- env_file = install_dir / ".env"- if env_file.exists():- for key, value in dotenv_values(env_file).items():- if key and value:- env_pairs.append(f"{key}={value}")- _run(["nssm", "set", SERVICE_NAME, "AppEnvironmentExtra", " ".join(env_pairs)])- _run(["nssm", "set", SERVICE_NAME, "Start", "SERVICE_AUTO_START"])- log_file = str(logs / "service.log")- _run(["nssm", "set", SERVICE_NAME, "AppStdout", log_file])- _run(["nssm", "set", SERVICE_NAME, "AppStderr", log_file])- _run(["nssm", "set", SERVICE_NAME, "AppRotateFiles", "1"])- _run(["nssm", "set", SERVICE_NAME, "AppRotateBytes", "10485760"]) # 10MB-- console.print("[green]Service installed successfully.[/]")- console.print(f"Logs: {log_file}")---def _windows_uninstall(_: Path) -> None:- _nssm_check()- _run(["nssm", "stop", SERVICE_NAME], ignore_error=True)- _run(["nssm", "remove", SERVICE_NAME, "confirm"], ignore_error=True)- console.print("Service uninstalled.")---def _windows_status(_: Path) -> None:- _nssm_check()- _run(["nssm", "status", SERVICE_NAME], ignore_error=True)---def _windows_start(_: Path) -> None:- _nssm_check()- _run(["nssm", "start", SERVICE_NAME])---def _windows_stop(_: Path) -> None:- _nssm_check()- _run(["nssm", "stop", SERVICE_NAME])---def _windows_restart(_: Path) -> None:- _nssm_check()- _run(["nssm", "restart", SERVICE_NAME])---# ============================================================== Linux (systemd)--_LINUX_UNIT = "/etc/systemd/system/machina.service"---def _linux_unit_text(install_dir: Path, *, service_user: str, memory: str) -> str:- venv = install_dir / "server" / ".venv"- server = install_dir / "server"- return f"""[Unit]-Description=MachinaOs Backend-After=network.target--[Service]-Type=simple-User={service_user}-WorkingDirectory={server}-EnvironmentFile=-{install_dir}/.env-Environment=PYTHONPATH={server}-Environment=PYTHONUTF8=1-Environment=PATH={venv}/bin:/usr/local/bin:/usr/bin:/bin-ExecStart={venv}/bin/uvicorn main:app --host 0.0.0.0 --port 3010 --log-level warning-Restart=always-RestartSec=5-StartLimitIntervalSec=60-StartLimitBurst=5--# Resource limits-MemoryMax={memory}--[Install]-WantedBy=multi-user.target-"""---def _linux_install(install_dir: Path, *, service_user: str, memory: str) -> None:- _ensure_built(install_dir)- console.print(f"Installing {SERVICE_NAME} service...")- console.print(f" Directory: {install_dir}")- console.print(f" User: {service_user}")- console.print(f" Memory limit: {memory}")-- tmp = Path("/tmp/machina.service")- tmp.write_text(_linux_unit_text(install_dir, service_user=service_user, memory=memory))-- _run(["sudo", "cp", str(tmp), _LINUX_UNIT])- _run(["sudo", "systemctl", "daemon-reload"])- _run(["sudo", "systemctl", "enable", "machina"])-- console.print("[green]Service installed successfully.[/]")- console.print("Start with: sudo systemctl start machina")- console.print("View logs: journalctl -u machina -f")---def _linux_uninstall(_: Path) -> None:- _run(["sudo", "systemctl", "stop", "machina"], ignore_error=True)- _run(["sudo", "systemctl", "disable", "machina"], ignore_error=True)- if Path(_LINUX_UNIT).exists():- _run(["sudo", "rm", _LINUX_UNIT])- _run(["sudo", "systemctl", "daemon-reload"])- console.print("Service uninstalled.")---def _linux_status(_: Path) -> None:- _run(["systemctl", "status", "machina"], ignore_error=True)-+ proc = subprocess.Popen(+ [str(py), "-m", "uvicorn", "main:app",+ "--host", "0.0.0.0", "--port", "3010", "--log-level", "warning"],+ cwd=str(server),+ stdin=subprocess.DEVNULL,+ stdout=log,+ stderr=log,+ **_detached_kwargs(),+ )+ _PID_FILE.write_text(str(proc.pid))+ console.print(f"[green]Started pid={proc.pid}[/] (logs: {log_file})")-def _linux_start(_: Path) -> None:- _run(["sudo", "systemctl", "start", "machina"])--def _linux_stop(_: Path) -> None:- _run(["sudo", "systemctl", "stop", "machina"])---def _linux_restart(_: Path) -> None:- _run(["sudo", "systemctl", "restart", "machina"])---# ============================================================== macOS (launchd)--def _mac_plist_path() -> Path:- return Path.home() / "Library" / "LaunchAgents" / f"{PLIST_LABEL}.plist"---def _mac_plist_text(install_dir: Path) -> str:- venv = install_dir / "server" / ".venv"- server = install_dir / "server"- logs = _logs_dir(install_dir)- return f"""<?xml version="1.0" encoding="UTF-8"?>-<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">-<plist version="1.0">-<dict>- <key>Label</key>- <string>{PLIST_LABEL}</string>- <key>ProgramArguments</key>- <array>- <string>{venv}/bin/uvicorn</string>- <string>main:app</string>- <string>--host</string>- <string>0.0.0.0</string>- <string>--port</string>- <string>3010</string>- <string>--log-level</string>- <string>warning</string>- </array>- <key>WorkingDirectory</key>- <string>{server}</string>- <key>EnvironmentVariables</key>- <dict>- <key>PATH</key>- <string>{venv}/bin:/usr/local/bin:/usr/bin:/bin</string>- <key>PYTHONPATH</key>- <string>{server}</string>- <key>PYTHONUTF8</key>- <string>1</string>- </dict>- <key>RunAtLoad</key>- <true/>- <key>KeepAlive</key>- <true/>- <key>StandardOutPath</key>- <string>{logs}/service.log</string>- <key>StandardErrorPath</key>- <string>{logs}/service.log</string>-</dict>-</plist>-"""---def _mac_install(install_dir: Path) -> None:- _ensure_built(install_dir)- logs = _logs_dir(install_dir)- logs.mkdir(parents=True, exist_ok=True)-- console.print(f"Installing {SERVICE_NAME} service...")- console.print(f" Directory: {install_dir}")-- plist_path = _mac_plist_path()- plist_path.parent.mkdir(parents=True, exist_ok=True)- _run(["launchctl", "unload", str(plist_path)], ignore_error=True)- plist_path.write_text(_mac_plist_text(install_dir))-- console.print("[green]Service installed successfully.[/]")- console.print(f"Plist: {plist_path}")- console.print(f"Logs: {logs}/service.log")- console.print('Start with: machina daemon start')---def _mac_uninstall(_: Path) -> None:- plist_path = _mac_plist_path()- _run(["launchctl", "unload", str(plist_path)], ignore_error=True)- if plist_path.exists():- plist_path.unlink()- console.print("Service uninstalled.")---def _mac_status(_: Path) -> None:- proc = subprocess.run(["launchctl", "list"], capture_output=True, text=True)- for line in proc.stdout.splitlines():- if "machina" in line:- console.print(line)---def _mac_start(_: Path) -> None:- _run(["launchctl", "load", str(_mac_plist_path())])---def _mac_stop(_: Path) -> None:- _run(["launchctl", "unload", str(_mac_plist_path())])---def _mac_restart(install_dir: Path) -> None:- _mac_stop(install_dir)- _mac_start(install_dir)---# ---------------------------------------------------------------- dispatch--def _platform_label() -> str:- if IS_WINDOWS:- return "Windows (NSSM)"- if IS_MACOS:- return "macOS (launchd)"- return "Linux (systemd)"---# Common Typer options reused by every verb.-_dir_opt = typer.Option(None, "--dir", help="Installation directory (default: project root).")-_user_opt = typer.Option(- None, "--user",- help="Service user (Linux only; default: current user).",-)-_memory_opt = typer.Option(- "2G", "--memory",- help="Memory limit (Linux only; default: 2G).",-)--[email protected]("install")-def install_cmd(- dir: str | None = _dir_opt,- user: str | None = _user_opt,- memory: str = _memory_opt,-) -> None:- """Install MachinaOS as a system service for the current platform."""- install_dir = _install_dir(dir)- if IS_WINDOWS:- _windows_install(install_dir)- elif IS_MACOS:- _mac_install(install_dir)- else:- service_user = user or os.environ.get("USER") or os.environ.get("USERNAME") or "root"- _linux_install(install_dir, service_user=service_user, memory=memory)---def _dispatch_simple(verb: str, install_dir: Path) -> None:- handlers = {- ("windows", "uninstall"): _windows_uninstall,- ("windows", "status"): _windows_status,- ("windows", "start"): _windows_start,- ("windows", "stop"): _windows_stop,- ("windows", "restart"): _windows_restart,- ("mac", "uninstall"): _mac_uninstall,- ("mac", "status"): _mac_status,- ("mac", "start"): _mac_start,- ("mac", "stop"): _mac_stop,- ("mac", "restart"): _mac_restart,- ("linux", "uninstall"): _linux_uninstall,- ("linux", "status"): _linux_status,- ("linux", "start"): _linux_start,- ("linux", "stop"): _linux_stop,- ("linux", "restart"): _linux_restart,- }- platform = "windows" if IS_WINDOWS else "mac" if IS_MACOS else "linux"- handlers[(platform, verb)](install_dir)--[email protected]("uninstall")-def uninstall_cmd(dir: str | None = _dir_opt) -> None:- """Remove the system service."""- _dispatch_simple("uninstall", _install_dir(dir))[email protected]("stop")+def stop_command() -> None:+ """Stop the backend if running; clear PID file."""+ pid = _read_pid()+ if pid is None:+ console.print("Not running.")+ _PID_FILE.unlink(missing_ok=True)+ return+ _kill_tree(pid)+ _PID_FILE.unlink(missing_ok=True)+ console.print(f"[green]Stopped pid={pid}[/]")@app.command("status")-def status_cmd(dir: str | None = _dir_opt) -> None:- """Show service status (NSSM / systemctl / launchctl)."""- _dispatch_simple("status", _install_dir(dir))--[email protected]("start")-def start_cmd(dir: str | None = _dir_opt) -> None:- """Start the system service."""- _dispatch_simple("start", _install_dir(dir))--[email protected]("stop")-def stop_cmd(dir: str | None = _dir_opt) -> None:- """Stop the system service."""- _dispatch_simple("stop", _install_dir(dir))+def status_command() -> None:+ """Report whether the backend is running."""+ pid = _read_pid()+ if pid is None:+ console.print("Not running.")+ raise typer.Exit(code=1)+ console.print(f"[green]Running pid={pid}[/]")@app.command("restart")-def restart_cmd(dir: str | None = _dir_opt) -> None:- """Restart the system service."""- _dispatch_simple("restart", _install_dir(dir))---def _root() -> None:- """Cross-platform service installer.-- Platform: detected from sys.platform (Windows -> NSSM,- macOS -> launchd, Linux -> systemd).- """+def restart_command() -> None:+ """Stop then start; convenience wrapper."""+ stop_command()+ start_command()diff --git a/machina/tests/test_daemon.py b/machina/tests/test_daemon.pyindex c74e759b..60104a49 100644--- a/machina/tests/test_daemon.py+++ b/machina/tests/test_daemon.py@@ -2,20 +2,14 @@from __future__ import annotations-import sys+import subprocessfrom pathlib import Path-from unittest.mock import patch+from unittest.mock import MagicMock, patch-from machina.commands import daemon---def test_install_dir_uses_project_root_by_default():- with patch.object(daemon, "project_root", return_value=Path("/proj")):- assert daemon._install_dir(None) == Path("/proj")+import psutil+import pytest--def test_install_dir_resolves_explicit_override(tmp_path: Path):- assert daemon._install_dir(str(tmp_path)) == tmp_path.resolve()+from machina.commands import daemondef test_venv_python_chooses_layout_per_platform():@@ -26,43 +20,89 @@ def test_venv_python_chooses_layout_per_platform():assert daemon._venv_python(install_dir).name == "python"-def test_linux_unit_text_includes_user_memory_and_paths(tmp_path: Path):- text = daemon._linux_unit_text(- tmp_path, service_user="bob", memory="3G",- )- assert "User=bob" in text- assert "MemoryMax=3G" in text- assert f"WorkingDirectory={tmp_path / 'server'}" in text- assert "ExecStart=" in text- assert "uvicorn main:app" in text+def test_detached_kwargs_windows_uses_creationflags():+ with patch.object(daemon, "IS_WINDOWS", True):+ kw = daemon._detached_kwargs()+ assert "creationflags" in kw+ expected = subprocess.DETACHED_PROCESS | subprocess.CREATE_NEW_PROCESS_GROUP+ assert kw["creationflags"] == expected+ assert "start_new_session" not in kw-def test_mac_plist_text_uses_label_and_paths(tmp_path: Path):- text = daemon._mac_plist_text(tmp_path)- assert f"<string>{daemon.PLIST_LABEL}</string>" in text- assert "main:app" in text- assert "0.0.0.0" in text+def test_detached_kwargs_posix_uses_setsid():+ with patch.object(daemon, "IS_WINDOWS", False):+ kw = daemon._detached_kwargs()+ assert kw == {"start_new_session": True}-def test_mac_plist_path_under_launchagents():- path = daemon._mac_plist_path()- assert path.name == f"{daemon.PLIST_LABEL}.plist"- # Sanity: should resolve under user's home Library/LaunchAgents.- assert "LaunchAgents" in str(path)+def test_read_pid_returns_none_when_file_missing(tmp_path: Path):+ with patch.object(daemon, "_PID_FILE", tmp_path / "missing.pid"):+ assert daemon._read_pid() is None-def test_dispatch_simple_routes_per_platform():- """Each verb hits the right handler for the current platform."""- install_dir = Path("/x")- with patch.object(daemon, "_windows_status") as win, \- patch.object(daemon, "_linux_status") as lin, \- patch.object(daemon, "_mac_status") as mac:- if sys.platform == "win32":- daemon._dispatch_simple("status", install_dir)- win.assert_called_once_with(install_dir)- elif sys.platform == "darwin":- daemon._dispatch_simple("status", install_dir)- mac.assert_called_once_with(install_dir)- else:- daemon._dispatch_simple("status", install_dir)- lin.assert_called_once_with(install_dir)+def test_read_pid_returns_none_for_corrupt_pid_file(tmp_path: Path):+ pid_file = tmp_path / "machina.pid"+ pid_file.write_text("not-a-number")+ with patch.object(daemon, "_PID_FILE", pid_file):+ assert daemon._read_pid() is None+++def test_read_pid_returns_none_when_process_no_longer_exists(tmp_path: Path):+ pid_file = tmp_path / "machina.pid"+ pid_file.write_text("999999")+ with patch.object(daemon, "_PID_FILE", pid_file), \+ patch.object(daemon.psutil, "pid_exists", return_value=False):+ assert daemon._read_pid() is None+++def test_read_pid_returns_pid_when_alive(tmp_path: Path):+ pid_file = tmp_path / "machina.pid"+ pid_file.write_text("1234\n")+ with patch.object(daemon, "_PID_FILE", pid_file), \+ patch.object(daemon.psutil, "pid_exists", return_value=True):+ assert daemon._read_pid() == 1234+++def test_kill_tree_terminates_children_and_parent():+ parent = MagicMock()+ child1 = MagicMock()+ child2 = MagicMock()+ parent.children.return_value = [child1, child2]+ with patch.object(daemon.psutil, "Process", return_value=parent):+ daemon._kill_tree(123)+ child1.kill.assert_called_once()+ child2.kill.assert_called_once()+ parent.terminate.assert_called_once()+ parent.wait.assert_called_once_with(timeout=5)+++def test_kill_tree_force_kills_on_timeout():+ parent = MagicMock()+ parent.children.return_value = []+ parent.wait.side_effect = psutil.TimeoutExpired(seconds=5)+ with patch.object(daemon.psutil, "Process", return_value=parent):+ daemon._kill_tree(123)+ parent.terminate.assert_called_once()+ parent.kill.assert_called_once()+++def test_kill_tree_no_such_process_is_noop():+ with patch.object(daemon.psutil, "Process", side_effect=psutil.NoSuchProcess(123)):+ daemon._kill_tree(123)+++def test_stop_command_clears_pid_file_when_not_running(tmp_path: Path):+ pid_file = tmp_path / "machina.pid"+ pid_file.write_text("999999")+ with patch.object(daemon, "_PID_FILE", pid_file), \+ patch.object(daemon, "_read_pid", return_value=None):+ daemon.stop_command()+ assert not pid_file.exists()+++def test_status_command_exits_1_when_not_running():+ import typer+ with patch.object(daemon, "_read_pid", return_value=None), \+ pytest.raises(typer.Exit) as exc:+ daemon.status_command()+ assert exc.value.exit_code == 1diff --git a/package.json b/package.jsonindex a8f93057..fc4b329c 100644--- a/package.json+++ b/package.json@@ -81,11 +81,9 @@"docs:nodes:check": "python -m machina docs nodes --check","deploy": "bash deploy.sh","deploy:gcp": "bash deploy.sh",- "daemon:install": "python -m machina daemon install",- "daemon:uninstall": "python -m machina daemon uninstall",- "daemon:status": "python -m machina daemon status","daemon:start": "python -m machina daemon start","daemon:stop": "python -m machina daemon stop",+ "daemon:status": "python -m machina daemon status","daemon:restart": "python -m machina daemon restart","version:sync": "python -m machina version sync","prepublishOnly": "python -m machina version sync && node -e \"const p=require('./package.json'); if(!p.bin||!p.version){process.exit(1)}\"",