Skip to content

The agent that grows with you

fix(claw): cleanup no longer mistakes any "openclaw" in argv for a running daemon

Drexuxux45dd97a6f7e1
diff --git a/hermes_cli/claw.py b/hermes_cli/claw.py
index fd229a75f1a..a05320f52e6 100644
--- a/hermes_cli/claw.py
+++ b/hermes_cli/claw.py
@@ -26,6 +26,8 @@ _OPENCLAW_SCRIPT_INSTALLED = get_hermes_home() / "skills" / _SCRIPT_REL
# Known OpenClaw directory names (current + legacy)
_OPENCLAW_DIR_NAMES = (".openclaw", ".clawdbot", ".moltbot")
+# pgrep -f ERE anchored on a node interpreter as argv[0] (``node /usr/local/bin/openclaw gateway``).
+_OPENCLAW_NODE_CMDLINE_RE = r"^(\S*/)?node(js)?\s.*(openclaw|clawd)"
# `hermes claw migrate` flags/defaults. Secrets are never included implicitly: --migrate-secrets
# is required even under --preset full (OpenClaw's two-phase posture); no silent API-key import.
@@ -124,9 +126,16 @@ def _detect_openclaw_processes() -> list[str]:
result = _posix_probe(["systemctl", "--user", "is-active", "openclaw-gateway.service"], 5)
if result is not None and result.stdout.strip() == "active":
found.append("systemd service: openclaw-gateway.service")
- result = _posix_probe(["pgrep", "-f", "openclaw"], 3)
- if result is not None and result.returncode == 0:
- found.append(f"openclaw process(es) (PIDs: {', '.join(result.stdout.strip().split())})")
+ # Never a bare ``pgrep -f openclaw``: it matches ANY argv containing the word (an editor on
+ # ~/.openclaw/config.json, ``tail -f openclaw.log``) and aborted cleanup on idle hosts (#12648).
+ # Mirror the Windows branch: exact binary names, plus node processes whose script mentions it.
+ pids: list[str] = []
+ for probe in (["pgrep", "-x", "openclaw"], ["pgrep", "-x", "clawd"], ["pgrep", "-f", _OPENCLAW_NODE_CMDLINE_RE]):
+ result = _posix_probe(probe, 3)
+ if result is not None and result.returncode == 0:
+ pids.extend(result.stdout.split())
+ if pids:
+ found.append(f"openclaw process(es) (PIDs: {', '.join(dict.fromkeys(pids))})")
return found
diff --git a/tests/hermes_cli/test_claw.py b/tests/hermes_cli/test_claw.py
index d9472cd8c85..5cf10586874 100644
--- a/tests/hermes_cli/test_claw.py
+++ b/tests/hermes_cli/test_claw.py
@@ -350,17 +350,43 @@ class TestPrintMigrationReport:
class TestDetectOpenclawProcesses:
- def test_returns_match_when_pgrep_finds_openclaw(self):
+ def test_reports_union_of_exact_and_node_matches(self):
with patch.object(claw_mod, "subprocess") as mock_subprocess:
- # systemd check misses, pgrep finds openclaw
mock_subprocess.run.side_effect = [
MagicMock(returncode=1, stdout=""), # systemctl
- MagicMock(returncode=0, stdout="1234\n"), # pgrep
+ MagicMock(returncode=0, stdout="1234\n"), # pgrep -x openclaw
+ MagicMock(returncode=1, stdout=""), # pgrep -x clawd
+ MagicMock(returncode=0, stdout="1234\n5678\n"), # node cmdline probe
]
mock_subprocess.TimeoutExpired = subprocess.TimeoutExpired
result = claw_mod._detect_openclaw_processes()
+ assert result == ["openclaw process(es) (PIDs: 1234, 5678)"]
+
+ @pytest.mark.linux_only
+ def test_live_pgrep_ignores_argv_mentions_but_finds_node_openclaw(self, tmp_path):
+ """A process that merely mentions "openclaw" in argv (the #12648 false positive) is not
+ OpenClaw; a node interpreter running an openclaw script is."""
+ import sys
+ import time
+
+ idle = f'{sys.executable} -c "import time; time.sleep(30)"'
+ # argv mentions openclaw but the binary is not one.
+ bystander = subprocess.Popen(["bash", "-c", f"exec {idle} {tmp_path}/openclaw-notes.txt"])
+ # argv[0] renamed to ``node`` running an openclaw script: the real launch shape.
+ node_like = subprocess.Popen(["bash", "-c", f"exec -a node {idle} {tmp_path}/openclaw/entry.js"])
+ try:
+ time.sleep(0.3)
+ with patch.object(claw_mod, "_posix_probe", wraps=claw_mod._posix_probe) as probe:
+ result = claw_mod._detect_openclaw_processes()
+ assert not any(a[0][:2] == ["pgrep", "-f"] and a[0][2] == "openclaw" for a, _ in probe.call_args_list)
assert len(result) == 1
- assert "1234" in result[0]
+ pids = result[0].split("PIDs: ")[1].rstrip(")").split(", ")
+ assert str(node_like.pid) in pids
+ assert str(bystander.pid) not in pids
+ finally:
+ for proc in (bystander, node_like):
+ proc.kill()
+ proc.wait()
@pytest.mark.windows_only