Self-improving AI Employees for everyone
Python ai-agentsai-employeeai-employeesassistantautonomous-agentsco-employeescompanygrok-botinstinctloop-agentsmuseown-your-datapersonalself-hosted
feat(machina): Phase B -- migrate `pnpm run clean` and `pnpm run build`
trohitgbfbc9f603c94
diff --git a/machina/cli.py b/machina/cli.pyindex c2e414cd..1d4968d3 100644--- a/machina/cli.py+++ b/machina/cli.py@@ -8,6 +8,8 @@ from __future__ import annotationsimport typer+from machina.commands.build import build_command+from machina.commands.clean import clean_commandfrom machina.commands.stop import stop_command@@ -21,17 +23,18 @@ app = typer.Typer(@app.callback()def _root() -> None:- """Marks the Typer app as a multi-command group.-- Without an ``@app.callback()`` Typer collapses a single registered- subcommand into the root, hiding the subcommand structure that's- about to grow as more JS scripts migrate.- """+ """Marks the Typer app as a multi-command group."""app.command("stop", help="Stop all MachinaOS services and free configured ports.")(stop_command)+app.command("clean", help="Stop services then remove build artefacts and venvs.")(+ clean_command+)+app.command("build", help="Install toolchain + build client + sync Python + verify deps.")(+ build_command+)if __name__ == "__main__":diff --git a/machina/commands/build.py b/machina/commands/build.pynew file mode 100644index 00000000..bc235b09--- /dev/null+++ b/machina/commands/build.py@@ -0,0 +1,195 @@+"""``machina build`` -- replaces ``scripts/build.js``.++Checks toolchain (node, npm, python, uv, temporal-server), then runs+the 5-step build: ``.env`` bootstrap -> ``pnpm install`` -> client+build -> ``uv sync`` -> Playwright -> verify edgymeow binary.++The ``MACHINAOS_BUILDING`` env var is set so ``scripts/postinstall.js``+skips its own ``install.js`` invocation when build is the orchestrator.+"""++from __future__ import annotations++import os+import re+import shutil+import subprocess+import sys+from pathlib import Path++import typer++from machina.colors import console+from machina.platform_ import project_root+++# ---------------------------------------------------------------- helpers++def _run(cmd: list[str], *, cwd: Path | None = None, check: bool = True) -> int:+ """Inherit-stdio run; raises ``typer.Exit`` on failure when ``check``."""+ proc = subprocess.run(cmd, cwd=str(cwd) if cwd else None)+ if check and proc.returncode != 0:+ console.print(f"[red]Command failed:[/] {' '.join(cmd)}")+ raise typer.Exit(code=proc.returncode)+ return proc.returncode+++def _capture(cmd: list[str]) -> str | None:+ """Capture stdout; return None if the command isn't on PATH or fails."""+ try:+ out = subprocess.run(+ cmd, capture_output=True, text=True, check=True, encoding="utf-8"+ )+ return out.stdout.strip() or out.stderr.strip()+ except (FileNotFoundError, subprocess.CalledProcessError):+ return None+++def _which_python() -> str | None:+ """Prefer ``python3`` so we don't pick up Python 2.x on POSIX distros."""+ for cmd in ("python3", "python"):+ if shutil.which(cmd):+ return cmd+ return None+++def _check_python(cmd: str) -> bool:+ out = _capture([cmd, "--version"])+ if not out:+ return False+ match = re.search(r"Python (\d+)\.(\d+)", out)+ if not match:+ return False+ major, minor = int(match.group(1)), int(match.group(2))+ if major >= 3 and minor >= 12:+ console.print(f" {out}")+ return True+ console.print(f" {out} [red](too old, need 3.12+)[/]")+ return False+++def _ensure_pip(python_cmd: str) -> None:+ if not _capture([python_cmd, "-m", "pip", "--version"]):+ console.print(" Installing pip via ensurepip...")+ _run([python_cmd, "-m", "ensurepip", "--upgrade"])+++def _ensure_uv(python_cmd: str) -> str:+ """Install ``uv`` via pip if missing; return the resolved version string."""+ version = _capture(["uv", "--version"])+ if version:+ console.print(f" uv: {version}")+ return version+ _ensure_pip(python_cmd)+ console.print(" Installing uv via pip...")+ _run([python_cmd, "-m", "pip", "install", "uv"])+ version = _capture(["uv", "--version"])+ if not version:+ console.print("[red]Error: failed to install uv. See https://docs.astral.sh/uv/[/]")+ raise typer.Exit(code=1)+ console.print(f" uv: {version}")+ return version+++def _ensure_temporal() -> None:+ """``temporal-server`` is a global npm CLI; install if missing (non-fatal)."""+ version = _capture(["temporal-server", "--version"])+ if version:+ console.print(f" temporal-server: {version}")+ return+ console.print(" temporal-server: not found, installing globally...")+ rc = _run(["npm", "install", "-g", "temporal-server"], check=False)+ if rc != 0:+ console.print(+ " [yellow]Warning: temporal-server install failed. "+ "Distributed execution unavailable.[/]"+ )+ return+ version = _capture(["temporal-server", "--version"])+ if version:+ console.print(f" temporal-server: {version}")+++# ---------------------------------------------------------------- build++def build_command() -> None:+ root = project_root()++ # Prevent the postinstall orchestrator from re-running install.js when+ # we're orchestrating ourselves (matches the existing JS contract).+ os.environ["MACHINAOS_BUILDING"] = "true"+ os.environ.setdefault("PYTHONUTF8", "1")++ is_postinstall = os.environ.get("npm_lifecycle_event") == "postinstall"+ is_ci = os.environ.get("CI") == "true" or os.environ.get("GITHUB_ACTIONS") == "true"+ if is_ci and is_postinstall:+ console.print("CI environment detected, skipping postinstall build.")+ return++ # ---- toolchain ---------------------------------------------------+ console.print("[bold]Checking dependencies...[/]\n")+ node_version = _capture(["node", "--version"])+ console.print(f" Node.js: {node_version or '[red]not found[/]'}")+ if not node_version:+ console.print("[red]Error: Node.js is required.[/]")+ raise typer.Exit(code=1)++ npm_version = _capture(["npm", "--version"])+ console.print(f" npm: {npm_version or '[red]not found[/]'}")++ python_cmd = _which_python()+ if not python_cmd or not _check_python(python_cmd):+ console.print(+ "[red]Error: Python 3.12+ is required.[/] "+ "Install from https://python.org/downloads/"+ )+ raise typer.Exit(code=1)++ _ensure_uv(python_cmd)+ _ensure_temporal()++ console.print("\n[green]All dependencies ready.[/]\n")++ # ---- build steps -------------------------------------------------+ server_dir = root / "server"+ env_path = root / ".env"+ template_path = root / ".env.template"++ if not env_path.exists() and template_path.exists():+ shutil.copy2(template_path, env_path)+ console.print("[0/5] Created .env from template")++ if not is_postinstall:+ console.print("[1/5] Installing dependencies...")+ _run(["pnpm", "install"], cwd=root)+ else:+ console.print("[1/5] Dependencies already installed by package manager")++ console.print("[2/5] Building client...")+ _run(["pnpm", "--filter", "react-flow-client", "run", "build"], cwd=root)++ console.print("[3/5] Installing Python dependencies...")+ if not (server_dir / ".venv").exists():+ _run(["uv", "venv"], cwd=server_dir)+ _run(["uv", "sync"], cwd=server_dir)++ console.print("[4/5] Installing Playwright browser...")+ rc = _run(["playwright", "install", "chromium"], cwd=server_dir, check=False)+ if rc != 0:+ console.print(+ " [yellow]Warning: Playwright browser install failed. "+ "JS-rendered scraping unavailable.[/]"+ )++ console.print("[5/5] Verifying edgymeow binary...")+ bin_name = "edgymeow-server.exe" if sys.platform == "win32" else "edgymeow-server"+ edgymeow_bin = root / "node_modules" / "edgymeow" / "bin" / bin_name+ if edgymeow_bin.exists():+ console.print(f" Binary present: {edgymeow_bin}")+ else:+ console.print(+ " [yellow]Warning: edgymeow binary not found. "+ "Set WHATSAPP_RUNTIME_ENABLED=false to disable.[/]"+ )++ console.print("\n[green]Build complete.[/]")diff --git a/machina/commands/clean.py b/machina/commands/clean.pynew file mode 100644index 00000000..3e056aba--- /dev/null+++ b/machina/commands/clean.py@@ -0,0 +1,80 @@+"""``machina clean`` -- replaces ``scripts/clean.js``.++Stops every process listening on the configured ports + orphaned project+processes, waits for file locks to release, then removes build artefacts+and venvs.+"""++from __future__ import annotations++import shutil+import time+from pathlib import Path++from machina.colors import console+from machina.config import load_config+from machina.platform_ import project_root+from machina.ports import kill_orphaned_machina_processes, kill_port+++# Removed each run -- order doesn't matter, ``shutil.rmtree`` is recursive.+_TARGETS = [+ "node_modules",+ "client/node_modules",+ "client/dist",+ "client/.vite",+ "server/data", # workflow.db, credentials.db, workspaces/+ "server/.venv",+ ".venv", # stale root venv (should not exist)+]+++def _rmtree_with_retry(path: Path, *, attempts: int = 3, delay: float = 0.1) -> bool:+ """``shutil.rmtree`` with Windows-friendly retry on file-lock errors."""+ for attempt in range(attempts):+ try:+ shutil.rmtree(path, ignore_errors=False)+ return True+ except OSError as exc:+ if attempt == attempts - 1:+ console.print(f" [yellow]Warning: Could not remove {path.name}: {exc}[/]")+ return False+ time.sleep(delay)+ return False+++def clean_command() -> None:+ cfg = load_config()+ root = project_root()++ console.print("[bold]Cleaning MachinaOS...[/]\n")++ # Step 1: kill anything on configured ports+ console.print("Stopping running processes...")+ killed_ports = 0+ for port in cfg.ports.all():+ result = kill_port(port)+ if result.killed_pids:+ console.print(f" Port {port}: Killed {len(result.killed_pids)} process(es)")+ killed_ports += len(result.killed_pids)++ # Step 2: kill orphaned project processes (may hold .venv file locks)+ orphaned = kill_orphaned_machina_processes(str(root), exclude_substring="machina")+ if orphaned:+ console.print(f" Orphaned: Killed {len(orphaned)} process(es)")++ if killed_ports or orphaned:+ console.print(" Waiting for processes to release file locks...")+ time.sleep(1.0)+ else:+ console.print(" No running processes found.")++ # Step 3: remove directories+ console.print("\nRemoving directories...")+ for target in _TARGETS:+ path = root / target+ if path.exists():+ console.print(f" Removing: {target}")+ _rmtree_with_retry(path)++ console.print("\n[green]Done.[/]")diff --git a/machina/tests/test_build.py b/machina/tests/test_build.pynew file mode 100644index 00000000..2da44c5b--- /dev/null+++ b/machina/tests/test_build.py@@ -0,0 +1,48 @@+"""Smoke tests for ``machina.commands.build``."""++from __future__ import annotations++from unittest.mock import MagicMock, patch++import pytest+import typer++from machina.commands import build+++def test_capture_returns_none_when_command_missing():+ with patch.object(build.subprocess, "run", side_effect=FileNotFoundError):+ assert build._capture(["does-not-exist"]) is None+++def test_capture_returns_stdout_when_present():+ fake = MagicMock(stdout="v1.2.3\n", stderr="")+ with patch.object(build.subprocess, "run", return_value=fake):+ assert build._capture(["tool", "--version"]) == "v1.2.3"+++def test_check_python_accepts_3_12_plus():+ with patch.object(build, "_capture", return_value="Python 3.12.5"):+ assert build._check_python("python") is True+++def test_check_python_rejects_3_11():+ with patch.object(build, "_capture", return_value="Python 3.11.9"):+ assert build._check_python("python") is False+++def test_check_python_rejects_unparseable():+ with patch.object(build, "_capture", return_value="garbage output"):+ assert build._check_python("python") is False+++def test_run_raises_typer_exit_on_nonzero_when_check():+ fake = MagicMock(returncode=1)+ with patch.object(build.subprocess, "run", return_value=fake), pytest.raises(typer.Exit):+ build._run(["false"])+++def test_run_returns_code_when_check_disabled():+ fake = MagicMock(returncode=42)+ with patch.object(build.subprocess, "run", return_value=fake):+ assert build._run(["x"], check=False) == 42diff --git a/machina/tests/test_clean.py b/machina/tests/test_clean.pynew file mode 100644index 00000000..0b9fe467--- /dev/null+++ b/machina/tests/test_clean.py@@ -0,0 +1,38 @@+"""Smoke tests for ``machina.commands.clean``."""++from __future__ import annotations++from pathlib import Path+from unittest.mock import patch++from machina.commands import clean+++def test_targets_list_is_stable():+ """Pin the cleanup target list so accidental edits surface in review."""+ assert clean._TARGETS == [+ "node_modules",+ "client/node_modules",+ "client/dist",+ "client/.vite",+ "server/data",+ "server/.venv",+ ".venv",+ ]+++def test_rmtree_with_retry_handles_oserror(tmp_path: Path):+ """If rmtree fails 3 times, the helper returns False without raising."""+ target = tmp_path / "doesnotmatter"+ target.mkdir()+ with patch.object(clean.shutil, "rmtree", side_effect=OSError("locked")):+ assert clean._rmtree_with_retry(target, attempts=3, delay=0) is False+++def test_rmtree_with_retry_succeeds_on_first_try(tmp_path: Path):+ """Happy path -- a present directory is removed cleanly."""+ target = tmp_path / "doomed"+ target.mkdir()+ (target / "file.txt").write_text("x")+ assert clean._rmtree_with_retry(target) is True+ assert not target.exists()diff --git a/package.json b/package.jsonindex b4e40804..4f9a51fb 100644--- a/package.json+++ b/package.json@@ -70,8 +70,8 @@"temporal:stop": "temporal stop","temporal:status": "temporal status","temporal:worker": "cd server && uv run python -m services.temporal.worker",- "build": "node scripts/build.js",- "clean": "node scripts/clean.js",+ "build": "python -m machina build",+ "clean": "python -m machina clean","stop": "python -m machina stop","test": "npm run test:backend && npm run test:frontend","test:backend": "cd server && uv run pytest tests/ -v",diff --git a/scripts/build.js b/scripts/build.jsdeleted file mode 100755index ed4a1f43..00000000--- a/scripts/build.js+++ /dev/null@@ -1,212 +0,0 @@-#!/usr/bin/env node-/**- * Cross-platform build script for MachinaOS.- * Works on: Windows, macOS, Linux, WSL, Git Bash- *- * Automatically installs missing dependencies:- * - Python 3.12+ (via pip)- * - uv (Python package manager)- */-import { execSync } from 'child_process';-import { existsSync, copyFileSync } from 'fs';-import { resolve, dirname } from 'path';-import { fileURLToPath } from 'url';--const __dirname = dirname(fileURLToPath(import.meta.url));-const ROOT = resolve(__dirname, '..');--// Environment detection-// - postinstall: npm already installed root deps, skip to avoid infinite loop-// - CI: GitHub Actions handles build separately, skip postinstall entirely-const isPostInstall = process.env.npm_lifecycle_event === 'postinstall';-const isCI = process.env.CI === 'true' || process.env.GITHUB_ACTIONS === 'true';--// Ensure Python UTF-8 encoding-process.env.PYTHONUTF8 = '1';--// Tell postinstall.js to skip install.js -- build.js handles everything-process.env.MACHINAOS_BUILDING = 'true';--// Skip build entirely in CI (workflows handle this)-if (isCI && isPostInstall) {- console.log('CI environment detected, skipping postinstall build.');- process.exit(0);-}--function run(cmd, cwd = ROOT) {- execSync(cmd, { cwd, stdio: 'inherit', shell: true });-}--function runSilent(cmd) {- try {- execSync(cmd, { stdio: 'pipe', shell: true });- return true;- } catch {- return false;- }-}--function getVersion(cmd) {- try {- return execSync(cmd, { encoding: 'utf-8', stdio: ['pipe', 'pipe', 'pipe'], shell: true }).trim();- } catch {- return null;- }-}--function pnpmInstall(cwd = ROOT) {- execSync('pnpm install', { cwd, stdio: 'inherit', shell: true });-}--// ============================================================================-// Install dependencies via pip-// ============================================================================--function ensurePip(pythonCmd) {- // Check if pip exists, install via ensurepip if missing- if (!runSilent(`${pythonCmd} -m pip --version`)) {- console.log(' Installing pip via ensurepip...');- run(`${pythonCmd} -m ensurepip --upgrade`);- }-}--function installUv(pythonCmd) {- ensurePip(pythonCmd);- console.log(' Installing uv via pip...');- run(`${pythonCmd} -m pip install uv`);-}--// ============================================================================-// Check and Install Dependencies-// ============================================================================--console.log('Checking dependencies...\n');--const nodeVersion = getVersion('node --version');-console.log(` Node.js: ${nodeVersion}`);--const npmVersion = getVersion('npm --version');-console.log(` npm: ${npmVersion}`);--// Python (required, user must install)-// Check python3 first to avoid picking up Python 2.x on Linux/macOS/WSL-let pyCmd = null;-if (runSilent('python3 --version')) {- pyCmd = 'python3';-} else if (runSilent('python --version')) {- pyCmd = 'python';-}--if (pyCmd) {- const pyVersion = getVersion(`${pyCmd} --version`);- const match = pyVersion?.match(/Python (\d+)\.(\d+)/);- if (match) {- const [, major, minor] = match.map(Number);- if (major >= 3 && minor >= 12) {- console.log(` ${pyVersion}`);- } else {- console.log(` ${pyVersion} (too old, need 3.12+)`);- console.log(' Error: Please install Python 3.12+ from https://python.org/');- process.exit(1);- }- }-} else {- console.log(' Error: Python 3.12+ is required.');- console.log(' Install from: https://python.org/downloads/');- process.exit(1);-}--// uv-let uvVersion = getVersion('uv --version');-if (uvVersion) {- console.log(` uv: ${uvVersion}`);-} else {- installUv(pyCmd);- uvVersion = getVersion('uv --version');- if (uvVersion) {- console.log(` uv: ${uvVersion}`);- } else {- console.log(' Error: Failed to install uv. Please install manually.');- console.log(' https://docs.astral.sh/uv/getting-started/installation/');- process.exit(1);- }-}--// temporal-server (global CLI)-let temporalVersion = getVersion('temporal-server --version');-if (temporalVersion) {- console.log(` temporal-server: ${temporalVersion}`);-} else {- console.log(' temporal-server: not found, installing...');- try {- run('npm install -g temporal-server');- temporalVersion = getVersion('temporal-server --version');- if (temporalVersion) {- console.log(` temporal-server: ${temporalVersion}`);- }- } catch {- console.log(' Warning: temporal-server install failed. Distributed execution unavailable.');- }-}--console.log('\nAll dependencies ready.\n');--// ============================================================================-// Build-// ============================================================================--try {- // Step 0: Create .env if not exists- const envPath = resolve(ROOT, '.env');- const templatePath = resolve(ROOT, '.env.template');- if (!existsSync(envPath) && existsSync(templatePath)) {- copyFileSync(templatePath, envPath);- console.log('[0/5] Created .env from template');- }-- // Step 1: Install root + client dependencies (workspaces handles both)- if (!isPostInstall) {- console.log('[1/5] Installing dependencies...');- pnpmInstall(ROOT);- } else {- console.log('[1/5] Dependencies already installed by package manager');- }-- // Step 2: Build client- console.log('[2/5] Building client...');- run('pnpm --filter react-flow-client run build');-- // Step 3: Install Python dependencies- console.log('[3/5] Installing Python dependencies...');- const serverDir = resolve(ROOT, 'server');- if (!existsSync(resolve(serverDir, '.venv'))) {- run('uv venv', serverDir);- }- run('uv sync', serverDir);-- // Step 4: Install Playwright browser for web scraping (non-fatal)- console.log('[4/5] Installing Playwright browser...');- try {- run('playwright install chromium', serverDir);- } catch {- console.log(' Warning: Playwright browser install failed. JS-rendered web scraping will be unavailable.');- }-- // Step 5: Verify edgymeow Go binary is installed (non-fatal). The Python- // backend supervises it lazily via nodes/whatsapp/_runtime.py, so we just- // confirm pnpm fetched the package.- console.log('[5/5] Verifying edgymeow binary...');- const edgymeowBin = resolve(ROOT, 'node_modules', 'edgymeow', 'bin',- process.platform === 'win32' ? 'edgymeow-server.exe' : 'edgymeow-server');- if (existsSync(edgymeowBin)) {- console.log(` Binary present: ${edgymeowBin}`);- } else {- console.log(' Warning: edgymeow binary not found. Set WHATSAPP_RUNTIME_ENABLED=false to disable.');- }-- console.log('\nBuild complete.');--} catch (err) {- console.log(`\nBuild failed: ${err.message}`);- process.exit(1);-}diff --git a/scripts/clean.js b/scripts/clean.jsdeleted file mode 100755index b87947c8..00000000--- a/scripts/clean.js+++ /dev/null@@ -1,77 +0,0 @@-#!/usr/bin/env node-/**- * Cross-platform clean script using Node.js native APIs.- * Works on: Windows, macOS, Linux, WSL, Git Bash- *- * Kills running processes on configured ports before cleaning to avoid- * locked files (especially .venv on Windows).- */-import { rmSync, existsSync } from 'fs';-import { resolve } from 'path';-import {- ROOT,- loadEnvConfig,- killPort,- killMachinaProcesses,- sleep,-} from './utils.js';--const targets = [- 'node_modules',- 'client/node_modules',- 'client/dist',- 'client/.vite',- 'server/data', // DBs (workflow.db, credentials.db) + workspaces- 'server/.venv',- '.venv', // Root venv (stale, should not exist)-];--async function main() {- console.log('Cleaning MachinaOS...\n');-- // Step 1: Kill processes on configured ports- const config = loadEnvConfig();- console.log('Stopping running processes...');-- let killedPorts = 0;- for (const port of config.allPorts) {- const result = await killPort(port);- if (result.killed.length > 0) {- console.log(` Port ${port}: Killed ${result.killed.length} process(es)`);- killedPorts += result.killed.length;- }- }-- // Step 2: Kill any remaining MachinaOs processes (orphans holding file locks)- const orphaned = await killMachinaProcesses('clean.js');- if (orphaned.length > 0) {- console.log(` Orphaned: Killed ${orphaned.length} process(es)`);- }-- if (killedPorts > 0 || orphaned.length > 0) {- // Wait for processes to fully terminate and release file handles- console.log(' Waiting for processes to release file locks...');- await sleep(1000);- } else {- console.log(' No running processes found.');- }-- // Step 3: Remove directories- console.log('\nRemoving directories...');-- for (const target of targets) {- const fullPath = resolve(ROOT, target);- if (existsSync(fullPath)) {- console.log(` Removing: ${target}`);- try {- rmSync(fullPath, { recursive: true, force: true, maxRetries: 3, retryDelay: 100 });- } catch (err) {- console.log(` Warning: Could not remove ${target}: ${err.message}`);- }- }- }-- console.log('\nDone.');-}--main().catch(console.error);diff --git a/scripts/postinstall.js b/scripts/postinstall.jsindex 74f0198e..e610b2b1 100755--- a/scripts/postinstall.js+++ b/scripts/postinstall.js@@ -21,8 +21,6 @@ function fixPermissions() {resolve(ROOT, 'bin/cli.js'),resolve(ROOT, 'scripts/start.js'),resolve(ROOT, 'scripts/dev.js'),- resolve(ROOT, 'scripts/build.js'),- resolve(ROOT, 'scripts/clean.js'),resolve(ROOT, 'scripts/install.js'),resolve(ROOT, 'install.sh'),];