Self-improving AI Employees for everyone
Python ai-agentsai-employeeai-employeesassistantautonomous-agentsco-employeescompanygrok-botinstinctloop-agentsmuseown-your-datapersonalself-hosted
fix(cli): keep company stop to this checkout's Temporal and processes
trohitg946773f73f51
diff --git a/cli/commands/stop.py b/cli/commands/stop.pyindex 9441dbd7..2b540148 100644--- a/cli/commands/stop.py+++ b/cli/commands/stop.py@@ -14,7 +14,7 @@ import typerfrom cli._common import backend_shutdown_grace_seconds, free_all_ports, preflightfrom cli.colors import consolefrom cli.config import load_dev_overrides-from cli.platform_ import platform_name+from cli.platform_ import platform_name, user_data_dirfrom cli.ports import (kill_by_pattern,kill_orphaned_opencompany_processes,@@ -49,7 +49,10 @@ def stop_command() -> None:if result.killed_pids:console.print(f" PIDs: {', '.join(str(p) for p in result.killed_pids)}")- temporal_pids = kill_by_pattern("temporal")+ # Only this installation's Temporal server: its binary and database live+ # in the data directory, which keeps another checkout's server (and any+ # process whose arguments merely mention "temporal") out of the kill.+ temporal_pids = kill_by_pattern("temporal", within=user_data_dir())if temporal_pids:console.print(f"[green]\\[OK][/] Temporal: Killed {len(temporal_pids)} process(es)"diff --git a/cli/platform_.py b/cli/platform_.pyindex 3771dded..29ee675e 100644--- a/cli/platform_.py+++ b/cli/platform_.py@@ -178,11 +178,16 @@ def user_data_dir() -> Path:Honours the project's ``DATA_DIR`` env override (see``.env.template``) -- the convention shared with the server's- ``core.paths.opencompany_root``. Otherwise delegates to platformdirs.+ ``core.paths.opencompany_root``, including its rule that a relative+ value (dev mode's ``.opencompany``) is resolved under the app root, not+ under the directory the command was run from. Otherwise delegates to+ platformdirs."""override = os.environ.get("DATA_DIR")if override:configured = Path(override).expanduser()+ if not configured.is_absolute():+ configured = project_root() / configured# Released versions commonly configured ``~/.machina`` or# ``<repo>/.machina``. The new defaults use the sibling# ``.opencompany`` directory; discover the old sibling until realdiff --git a/cli/ports.py b/cli/ports.pyindex 46c44b30..c87fe5a0 100644--- a/cli/ports.py+++ b/cli/ports.py@@ -7,6 +7,7 @@ same battle-tested ``psutil`` paths that are already in production.from __future__ import annotationsimport os+import shutilimport sysimport subprocessimport time@@ -177,25 +178,128 @@ def kill_port(port: int, *, backend_graceful_timeout: float | None = None) -> Kireturn KillResult(port=port, killed_pids=killed, port_free=port_free)-def kill_by_pattern(pattern: str, *, root_dir: str | None = None) -> list[int]:- """Kill processes whose name OR command line matches ``pattern``.+def _normalized(path: str | os.PathLike[str]) -> str:+ """A path as command lines are compared here: lower case, forward slashes,+ no trailing slash."""+ return os.fspath(path).lower().replace("\\", "/").rstrip("/")- When ``root_dir`` is supplied, only processes whose command line also- references that path are killed (so unrelated tools that happen to- share a substring are left alone).++def _command_line(proc: psutil.Process) -> str:+ """``proc``'s command line from ``process_iter``'s cache, normalized like a path."""+ return " ".join(proc.info.get("cmdline") or []).lower().replace("\\", "/")+++def _within(path: str, root: str) -> bool:+ return path == root or path.startswith(root + "/")+++def _path_ends_at(text: str, end: int) -> bool:+ """Whether a path in ``text`` can end at ``end``: at a separator, at the+ end of an argument, or at the end of the text."""+ return text[end : end + 1] in ("", "/", " ", '"', "'")+++@dataclass(frozen=True)+class _Tree:+ """A directory and, when it is a git working tree, the repository's others.++ Paths are :func:`_normalized`. ``others`` comes from git (see+ :func:`_working_tree`): a worktree nested inside ``root``, or beside it,+ is a separate checkout even when its files share ``root``'s prefix.+ """++ root: str+ others: tuple[str, ...] = ()++ def named_in(self, cmd: str) -> bool:+ """Whether a normalized command line names a path in this tree.++ The root has to end at a path boundary, so a sibling folder that only+ shares the prefix (``opencompany-worktrees/...``) does not count, and+ a path inside a worktree nested here belongs to that worktree.+ """+ if not self.root:+ return False+ nested = [tree for tree in self.others if _within(tree, self.root)]+ start = 0+ while (index := cmd.find(self.root, start)) >= 0:+ start = index + 1+ if not _path_ends_at(cmd, index + len(self.root)):+ continue+ if not any(cmd.startswith(tree, index) and _path_ends_at(cmd, index + len(tree)) for tree in nested):+ return True+ return False++ def belongs_elsewhere(self, path: str) -> bool:+ """Whether a normalized ``path`` lies in another of the repository's+ working trees. The deepest working tree containing a path owns it."""+ owners = [tree for tree in (self.root, *self.others) if _within(path, tree)]+ return bool(owners) and max(owners, key=len) != self.root+++def _working_tree(root_dir: str | os.PathLike[str]) -> _Tree:+ """``root_dir`` with the repository's other working trees, as git lists them.++ Git, not a folder-naming convention, says which directories are separate+ checkouts: ``git worktree add`` can put one anywhere. There are none+ without git, outside a repository, or when ``root_dir`` is not itself a+ working tree (an installed package unpacked inside some other repository).+ """+ root_path = Path(root_dir).resolve()+ root = _normalized(root_path)+ git = shutil.which("git")+ if git is None:+ return _Tree(root)+ try:+ listing = subprocess.run(+ [git, "-C", str(root_path), "worktree", "list", "--porcelain", "-z"],+ capture_output=True,+ encoding="utf-8",+ check=True,+ ).stdout+ except (OSError, subprocess.SubprocessError):+ return _Tree(root)+ trees = [_normalized(field.removeprefix("worktree ")) for field in listing.split("\0") if field.startswith("worktree ")]+ if root not in trees:+ return _Tree(root)+ return _Tree(root, tuple(tree for tree in trees if tree != root))+++def _works_in_another_tree(proc: psutil.Process, checkout: _Tree) -> bool:+ """Whether ``proc``'s working directory lies in another working tree.++ A test run in a worktree that borrows this checkout's Python names the+ interpreter's path here; its working directory says whose run it is. An+ unreadable working directory leaves the command line to decide.+ """+ try:+ cwd = proc.cwd()+ except psutil.Error:+ return False+ return checkout.belongs_elsewhere(_normalized(cwd))+++def kill_by_pattern(pattern: str, *, within: str | os.PathLike[str]) -> list[int]:+ """Kill processes whose name or command line contains ``pattern`` and+ whose command line names a path inside the directory ``within``.++ ``within`` is what keeps the kill to this installation. ``company stop``+ passes its data directory, where the Temporal binary and database live,+ so another checkout's Temporal server, and a test run whose arguments+ merely mention ``temporal``, are left alone."""pattern_lower = pattern.lower()- root_norm = root_dir.lower().replace("\\", "/") if root_dir else None+ scope = _Tree(_normalized(Path(within).resolve()))safe_pids = _ancestor_pids()killed: list[int] = []for proc in psutil.process_iter(["pid", "name", "cmdline"]):try:name = (proc.info["name"] or "").lower()- cmd = " ".join(proc.info.get("cmdline") or []).lower().replace("\\", "/")+ cmd = _command_line(proc)if pattern_lower not in name and pattern_lower not in cmd:continue- if root_norm and root_norm not in cmd:+ if not scope.named_in(cmd):continueif proc.pid in safe_pids:continue@@ -206,40 +310,17 @@ def kill_by_pattern(pattern: str, *, root_dir: str | None = None) -> list[int]:return killed-def _names_this_checkout(cmd: str, root_norm: str) -> bool:- """Whether a normalized cmdline names a path inside the checkout at ``root_norm``.-- A plain substring match also caught other checkouts: a sibling folder- that only shares the prefix (``opencompany-worktrees/...``) and the- worktrees nested in the checkout (``<root>/.claude/worktrees/...``), so a- ``company stop`` in one checkout killed a dev server or test run in another.- The root must be followed by a separator or the end of an argument, and a- path under the nested worktrees does not count.- """- root = root_norm.rstrip("/")- if not root:- return False- nested_worktrees = root + "/.claude/worktrees/"- start = 0- while True:- index = cmd.find(root, start)- if index < 0:- return False- following = cmd[index + len(root) : index + len(root) + 1]- if following in ("", "/", " ", '"', "'") and not cmd.startswith(nested_worktrees, index):- return True- start = index + 1--def kill_orphaned_opencompany_processes(root_dir: str, *, exclude_substring: str | None = None,backend_graceful_timeout: float | None = None,) -> list[int]:- """Kill stray python/bun processes whose cmdline references the project root.+ """Kill stray python/bun/node processes that belong to the checkout at ``root_dir``.- Only this checkout's processes: see :func:`_names_this_checkout`.+ A process belongs to it when its command line names a path in the+ checkout (:meth:`_Tree.named_in`) and it is not working in another of+ the repository's working trees (:func:`_works_in_another_tree`)."""- root_norm = root_dir.lower().replace("\\", "/")+ checkout = _working_tree(root_dir)# bun runs the JS executor sidecar and the company shim; node is kept# so a sidecar left over from a pre-bun install is still reaped.target_names = {"python", "python3", "python.exe", "bun", "bun.exe", "node", "node.exe"}@@ -251,13 +332,15 @@ def kill_orphaned_opencompany_processes(name = (proc.info["name"] or "").lower()if name not in target_names:continue- cmd = " ".join(proc.info.get("cmdline") or []).lower().replace("\\", "/")- if not _names_this_checkout(cmd, root_norm):+ cmd = _command_line(proc)+ if not checkout.named_in(cmd):continueif exclude_substring and exclude_substring.lower() in cmd:continueif proc.pid in safe_pids:continue+ if _works_in_another_tree(proc, checkout):+ continuegrace = 2.0if backend_graceful_timeout is not None and _is_backend_process(proc.pid, root_dir):grace = backend_graceful_timeoutdiff --git a/cli/tests/test_backend_shutdown.py b/cli/tests/test_backend_shutdown.pyindex 16fe2405..5b06a1e9 100644--- a/cli/tests/test_backend_shutdown.py+++ b/cli/tests/test_backend_shutdown.py@@ -88,3 +88,17 @@ def test_stop_orphan_cleanup_preserves_backend_budget(tmp_path: Path, monkeypatc):stop.stop_command()kill_orphans.assert_called_once_with(str(tmp_path), backend_graceful_timeout=backend_shutdown_grace_seconds(cfg))+++def test_stop_kills_only_this_installations_temporal(tmp_path: Path, monkeypatch: pytest.MonkeyPatch):+ cfg = replace(load_config(), temporal_enabled=False)+ monkeypatch.setenv("DATA_DIR", str(tmp_path / "state"))+ with (+ patch.object(stop, "preflight", return_value=(cfg, tmp_path)),+ patch.object(stop, "load_dev_overrides"),+ patch.object(stop, "free_all_ports", return_value=[KillResult(p, [], True) for p in cfg.all_ports]),+ patch.object(stop, "kill_by_pattern", return_value=[]) as kill_by_pattern,+ patch.object(stop, "kill_orphaned_opencompany_processes", return_value=[]),+ ):+ stop.stop_command()+ kill_by_pattern.assert_called_once_with("temporal", within=tmp_path / "state")diff --git a/cli/tests/test_platform_paths.py b/cli/tests/test_platform_paths.pyindex 967c9ce0..cd101644 100644--- a/cli/tests/test_platform_paths.py+++ b/cli/tests/test_platform_paths.py@@ -45,3 +45,17 @@ def test_custom_data_dir_is_never_rewritten(tmp_path: Path, monkeypatch) -> Nonemonkeypatch.setenv("DATA_DIR", str(configured))assert platform_.user_data_dir() == configured+++def test_relative_data_dir_resolves_under_the_app_root(tmp_path: Path, monkeypatch) -> None:+ """The server's ``core.paths`` rule: dev mode's relative ``.opencompany``+ is ``<app root>/.opencompany`` wherever the command runs from."""+ app_root = tmp_path / "app"+ elsewhere = tmp_path / "elsewhere"+ app_root.mkdir()+ elsewhere.mkdir()+ monkeypatch.setattr(platform_, "project_root", lambda: app_root)+ monkeypatch.chdir(elsewhere)+ monkeypatch.setenv("DATA_DIR", "state")++ assert platform_.user_data_dir() == app_root / "state"diff --git a/cli/tests/test_ports.py b/cli/tests/test_ports.pyindex db0e7693..1bc3d90c 100644--- a/cli/tests/test_ports.py+++ b/cli/tests/test_ports.py@@ -116,31 +116,35 @@ def test_kill_port_excludes_self():assert result.port_free is True-def test_orphan_reaper_kills_only_this_checkouts_processes():- """A substring match on the root also caught sibling folders sharing the- prefix and the worktrees nested under ``.claude/worktrees/`` -- so a- ``company stop`` in the main checkout killed dev servers and test runs in- every worktree."""+def _process(pid, name, cmdline, cwd):from types import SimpleNamespace as NS+ return NS(pid=pid, info={"name": name, "cmdline": cmdline}, cwd=lambda: cwd, kill=MagicMock())+++def test_orphan_reaper_kills_only_this_checkouts_processes():+ """A substring match on the root also caught sibling folders sharing the+ prefix and the worktrees nested in the checkout, so a ``company stop`` in+ the main checkout killed dev servers and test runs in every worktree. A+ worktree's test run that borrows the main checkout's Python names a path+ here too; its working directory is what says whose run it is."""root = "D:\\startup\\projects\\opencompany"+ worktree = f"{root}\\.claude\\worktrees\\native-browser"+ main_python = f"{root}\\server\\.venv\\Scripts\\python.exe"procs = [- NS(pid=11, info={"name": "python.exe", "cmdline": [f"{root}\\server\\.venv\\Scripts\\python.exe", "-m", "uvicorn"]}),- NS(pid=12, info={"name": "bun.exe", "cmdline": ["bun", f"{root}/server/nodejs/dist/index.js"]}),- NS(pid=21, info={"name": "python.exe", "cmdline": [f"{root}-worktrees\\feature\\server\\.venv\\Scripts\\python.exe"]}),- NS(- pid=22,- info={- "name": "python.exe",- "cmdline": [f"{root}\\.claude\\worktrees\\native-browser\\server\\.venv\\Scripts\\python.exe", "-m", "pytest"],- },- ),- NS(pid=23, info={"name": "python.exe", "cmdline": ["python", "-c", "print('opencompanyx')"]}),- NS(pid=24, info={"name": "chrome.exe", "cmdline": [f"{root}\\server\\whatever"]}),+ _process(11, "python.exe", [main_python, "-m", "uvicorn"], f"{root}\\server"),+ _process(12, "bun.exe", ["bun", f"{root}/server/nodejs/dist/index.js"], f"{root}\\server\\nodejs"),+ _process(21, "python.exe", [f"{root}-worktrees\\feature\\server\\.venv\\Scripts\\python.exe"], f"{root}-worktrees\\feature"),+ _process(22, "python.exe", [f"{worktree}\\server\\.venv\\Scripts\\python.exe", "-m", "pytest"], f"{worktree}\\server"),+ _process(23, "python.exe", ["python", "-c", "print('opencompanyx')"], root),+ _process(24, "chrome.exe", [f"{root}\\server\\whatever"], root),+ _process(25, "python.exe", [main_python, "-m", "pytest", "tests/"], f"{worktree}\\server"),]+ checkout = ports._Tree(ports._normalized(root), (ports._normalized(worktree),))killed = []with (patch.object(psutil, "process_iter", return_value=procs),+ patch.object(ports, "_working_tree", return_value=checkout),patch.object(ports, "_ancestor_pids", return_value=set()),patch.object(ports, "kill_pid", side_effect=lambda pid, **_kw: killed.append(pid) or True),):@@ -148,11 +152,81 @@ def test_orphan_reaper_kills_only_this_checkouts_processes():assert killed == [11, 12]-def test_checkout_match_needs_a_path_boundary():+def test_a_checkout_is_named_only_at_a_path_boundary_outside_its_worktrees():root = "d:/startup/projects/opencompany"- assert ports._names_this_checkout(f"python {root}/server/main.py", root)- assert ports._names_this_checkout(f"python -m cli dev --root {root}", root)- assert not ports._names_this_checkout(f"python {root}-worktrees/a/main.py", root)- assert not ports._names_this_checkout(f"python {root}/.claude/worktrees/a/server/main.py", root)- # Both a worktree path and a main-checkout path: the main one counts.- assert ports._names_this_checkout(f"python {root}/.claude/worktrees/a/x.py {root}/server/y.py", root)+ checkout = ports._Tree(root, (f"{root}/.claude/worktrees/a", "d:/elsewhere/b"))+ assert checkout.named_in(f"python {root}/server/main.py")+ assert checkout.named_in(f"python -m cli dev --root {root}")+ assert not checkout.named_in(f"python {root}-worktrees/a/main.py")+ assert not checkout.named_in(f"python {root}/.claude/worktrees/a/server/main.py")+ # A worktree path and a checkout path: the checkout one counts.+ assert checkout.named_in(f"python {root}/.claude/worktrees/a/x.py {root}/server/y.py")+ # Git decides what is a separate checkout, not the folder it sits in.+ assert checkout.named_in(f"python {root}/.claude/worktrees/not-a-worktree/x.py")+++def test_the_deepest_working_tree_owns_a_directory():+ root = "d:/repo"+ nested = f"{root}/.claude/worktrees/a"+ checkout = ports._Tree(root, (nested, "d:/beside"))+ assert not checkout.belongs_elsewhere(f"{root}/server")+ assert checkout.belongs_elsewhere(f"{nested}/server")+ assert checkout.belongs_elsewhere("d:/beside/client")+ assert not checkout.belongs_elsewhere("c:/users/me")+ # Seen from the nested worktree, the enclosing checkout is the other tree.+ worktree = ports._Tree(nested, (root,))+ assert worktree.belongs_elsewhere(f"{root}/server")+ assert not worktree.belongs_elsewhere(f"{nested}/server")+++def test_working_trees_come_from_git(tmp_path):+ import shutil+ import subprocess++ git = shutil.which("git")+ if git is None:+ pytest.skip("git is not installed")+ repo = tmp_path.resolve() / "repo"+ repo.mkdir()++ def run(*args):+ subprocess.run([git, "-C", str(repo), *args], check=True, capture_output=True)++ run("init", "-q")+ run("-c", "user.name=test", "-c", "[email protected]", "commit", "-q", "--allow-empty", "-m", "start")+ nested = repo / ".claude" / "worktrees" / "a"+ beside = tmp_path.resolve() / "beside"+ run("worktree", "add", "-q", "--detach", str(nested))+ run("worktree", "add", "-q", "--detach", str(beside))++ checkout = ports._working_tree(repo)+ assert checkout.root == ports._normalized(repo)+ assert set(checkout.others) == {ports._normalized(nested), ports._normalized(beside)}+ assert ports._normalized(repo) in ports._working_tree(nested).others+ # A directory inside a working tree, but not one itself, has no others.+ (repo / "sub").mkdir()+ assert ports._working_tree(repo / "sub").others == ()+++def test_working_trees_without_git_or_a_repository(tmp_path):+ with patch.object(ports.shutil, "which", return_value=None):+ assert ports._working_tree(tmp_path).others == ()+ assert ports._working_tree(tmp_path).others == ()+++def test_temporal_kill_stays_inside_the_data_directory(tmp_path):+ """``company stop`` killed every process on the machine with "temporal"+ in its name or command line: other checkouts' Temporal servers and test+ runs of ``tests/temporal``."""+ data = tmp_path.resolve() / ".opencompany"+ worktree_data = tmp_path.resolve() / ".claude" / "worktrees" / "a" / ".opencompany"+ temporal = ["server", "start-dev", "--db-filename"]+ procs = [+ _process(31, "temporal.exe", [str(data / "packages" / "temporal" / "temporal.exe"), *temporal, str(data / "temporal.db")], str(data)),+ _process(32, "temporal.exe", [str(worktree_data / "packages" / "temporal" / "temporal.exe"), *temporal], str(worktree_data)),+ _process(33, "python.exe", ["python", "-m", "pytest", "tests/temporal"], str(tmp_path)),+ _process(34, "temporal.exe", [str(tmp_path.resolve() / ".opencompany-old" / "temporal.exe")], str(tmp_path)),+ ]+ with patch.object(psutil, "process_iter", return_value=procs), patch.object(ports, "_ancestor_pids", return_value=set()):+ assert ports.kill_by_pattern("temporal", within=data) == [31]+ procs[0].kill.assert_called_once()diff --git a/docs-internal/cli_services_integration.md b/docs-internal/cli_services_integration.mdindex 4a6e8615..97a2a9b0 100644--- a/docs-internal/cli_services_integration.md+++ b/docs-internal/cli_services_integration.md@@ -17,7 +17,7 @@ Wraps the official `temporal` CLI's `server start-dev` mode (per [docs.temporal.**Install:** Automated. `company build` step [6/6] runs `python -m services.temporal._install`, which uses `pooch` to download the official CLI archive from `https://temporal.download/cli/archive/latest?platform=<os>&arch=<arch>` into `<DATA_DIR>/packages/temporal/` (= `~/.opencompany/packages/temporal/` by default, on every OS — via `core.paths.package_dir("temporal")`). No npm package, no system install required.-**Lifecycle:** Backend-owned (July 2026): the FastAPI lifespan starts the dev server via `TemporalServerRuntime.ensure_started()` ([server/services/temporal/_runtime.py](../server/services/temporal/_runtime.py)) when `TEMPORAL_ENABLED` and the configured address is loopback; `shutdown_all_supervisors()` stops it at shutdown. The CLI no longer supervises Temporal (the `_supervised_runtime.py` shim and the `_temporal_specs.py` command helpers were deleted); `company stop` still frees the Temporal ports.+**Lifecycle:** Backend-owned (July 2026): the FastAPI lifespan starts the dev server via `TemporalServerRuntime.ensure_started()` ([server/services/temporal/_runtime.py](../server/services/temporal/_runtime.py)) when `TEMPORAL_ENABLED` and the configured address is loopback; `shutdown_all_supervisors()` stops it at shutdown. The CLI no longer supervises Temporal (the `_supervised_runtime.py` shim and the `_temporal_specs.py` command helpers were deleted); `company stop` still frees the Temporal ports, then kills any other Temporal process whose command line names this installation's data directory, where the binary and its SQLite database live (`cli/ports.py::kill_by_pattern`). Another checkout's or worktree's Temporal server, and a test run that merely mentions `temporal`, are left running. Its orphan cleanup works the same way: it takes the repository's worktrees from `git worktree list`, and a process working in another worktree is left alone even when it runs this checkout's Python.**Ports (declared in `.env.template`, freed by `company stop`'s port-kill pre-flight):**| Service | Env var (value in `.env.template`) | Note |