Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

fix(backends): match codex app-server through node shim cmdline

cyhe63ab2e29c5d
diff --git a/tests/test_codex_process_match.py b/tests/test_codex_process_match.py
new file mode 100644
index 000000000..040e0747d
--- /dev/null
+++ b/tests/test_codex_process_match.py
@@ -0,0 +1,95 @@
+from __future__ import annotations
+
+import os
+import sys
+from pathlib import Path
+
+sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
+
+from vibe import api
+
+
+def _make_paths(tmp_path: Path) -> tuple[str, str]:
+ """Create a node shim + native codex binary on disk under tmp_path.
+
+ Returns (node_path, codex_shim_path) so tests can pretend the kernel
+ exec'd them as a #!/usr/bin/env node shebang script.
+ """
+ bin_dir = tmp_path / "bin"
+ bin_dir.mkdir(parents=True, exist_ok=True)
+ node_path = bin_dir / "node"
+ codex_path = bin_dir / "codex"
+ node_path.write_text("#!/bin/sh\n")
+ codex_path.write_text("#!/usr/bin/env node\n")
+ return str(node_path), str(codex_path)
+
+
+def test_matches_native_codex_binary(tmp_path: Path) -> None:
+ """argv[0] is the codex binary itself (native install)."""
+ _, codex_path = _make_paths(tmp_path)
+ cmdline = [codex_path, "app-server", "--analytics-default-enabled"]
+ assert api._process_matches_codex_binary(cmdline, codex_path) is True
+
+
+def test_matches_node_shim_codex(tmp_path: Path) -> None:
+ """argv[0] is node, argv[1] is the codex shebang script (npm install)."""
+ node_path, codex_path = _make_paths(tmp_path)
+ cmdline = [node_path, codex_path, "app-server"]
+ assert api._process_matches_codex_binary(cmdline, codex_path) is True
+
+
+def test_matches_node_shim_with_extra_node_flags(tmp_path: Path) -> None:
+ """Some node shims insert flags between node and the script path.
+
+ psutil reports cmdline including the interpreter and its flags, so we
+ should still tolerate one such flag without sweeping in unrelated tools.
+ """
+ node_path, codex_path = _make_paths(tmp_path)
+ cmdline = [node_path, codex_path, "app-server", "--something"]
+ assert api._process_matches_codex_binary(cmdline, codex_path) is True
+
+
+def test_rejects_unrelated_node_process(tmp_path: Path) -> None:
+ """A node process that happens to mention codex deeper in args is not ours."""
+ node_path, _ = _make_paths(tmp_path)
+ other = tmp_path / "bin" / "other-tool"
+ other.write_text("#!/usr/bin/env node\n")
+ # The codex/app-server tokens sit far down the cmdline; should be rejected.
+ cmdline = [node_path, str(other), "--config=codex.json", "--mode=app-server"]
+ codex_path = str(tmp_path / "bin" / "codex")
+ assert api._process_matches_codex_binary(cmdline, codex_path) is False
+
+
+def test_rejects_different_codex_install(tmp_path: Path) -> None:
+ """Another codex install elsewhere on the system is not ours to kill."""
+ _, codex_path = _make_paths(tmp_path)
+ elsewhere = tmp_path / "other" / "codex"
+ elsewhere.parent.mkdir(parents=True, exist_ok=True)
+ elsewhere.write_text("#!/usr/bin/env node\n")
+ cmdline = [str(elsewhere), "app-server"]
+ assert api._process_matches_codex_binary(cmdline, codex_path) is False
+
+
+def test_basename_fallback_when_no_resolved_binary(tmp_path: Path) -> None:
+ """When the configured CLI isn't on PATH, basename starting with codex matches."""
+ _, codex_path = _make_paths(tmp_path)
+ cmdline = [codex_path, "app-server"]
+ assert api._process_matches_codex_binary(cmdline, None) is True
+
+
+def test_basename_fallback_via_node_shim(tmp_path: Path) -> None:
+ """Basename fallback also works for the node-shim case."""
+ node_path, codex_path = _make_paths(tmp_path)
+ cmdline = [node_path, codex_path, "app-server"]
+ assert api._process_matches_codex_binary(cmdline, None) is True
+
+
+def test_rejects_empty_cmdline() -> None:
+ assert api._process_matches_codex_binary([], "/usr/local/bin/codex") is False
+
+
+def test_requires_app_server_token(tmp_path: Path) -> None:
+ """A codex invocation without ``app-server`` (e.g. ``codex login``) is not the daemon."""
+ _, codex_path = _make_paths(tmp_path)
+ cmdline = [codex_path, "login", "--browser"]
+ assert api._process_matches_codex_binary(cmdline, codex_path) is False
diff --git a/vibe/api.py b/vibe/api.py
index 25445ba4e..818f6bf6f 100644
--- a/vibe/api.py
+++ b/vibe/api.py
@@ -1672,42 +1672,56 @@ def _process_matches_codex_binary(cmdline: list[str], resolved_binary: str | Non
The original cmdline-substring check matched any ``codex`` mention in any
argument (e.g. a user invoking ``codex app-server`` themselves from a
- shell, or another tool whose args happen to contain those tokens). We
- now require:
-
- 1. argv[0] resolves to the same absolute path as the configured
- codex binary, **or** its basename starts with ``codex``; and
+ shell, or another tool whose args happen to contain those tokens), and
+ a follow-up tightening only inspected ``argv[0]`` — which missed the
+ ``npm install -g @openai/codex`` shim, where the live process is
+ ``node /path/.../bin/codex app-server`` (``argv[0] == "node"``).
+
+ We now scan the first few argv tokens (``argv[0]`` and ``argv[1]``)
+ looking for the codex binary itself, since the kernel preserves the
+ script path as ``argv[1]`` whenever a ``#!/usr/bin/env node`` shim is
+ exec'd. The match requires:
+
+ 1. one of ``argv[0]``/``argv[1]`` resolves to the same absolute path
+ as the configured codex binary (or, when no resolved binary is
+ known, has basename starting with ``codex``); and
2. one of the early arguments is exactly ``app-server``.
-
- When ``resolved_binary`` is None we fall back to a basename match so the
- chip still works for users whose config points at a CLI that isn't on
- PATH right now.
"""
if not cmdline:
return False
- head = cmdline[0]
try:
- head_resolved = str(Path(head).expanduser().resolve())
+ target = (
+ str(Path(resolved_binary).expanduser().resolve())
+ if resolved_binary
+ else None
+ )
except Exception:
- head_resolved = head
- if resolved_binary:
- # When we know which binary the user configured, require an exact
- # match. A second codex install elsewhere on the system is *not* ours
- # to kill.
+ target = resolved_binary
+
+ def _matches(token: str) -> bool:
try:
- target = str(Path(resolved_binary).expanduser().resolve())
+ resolved = str(Path(token).expanduser().resolve())
except Exception:
- target = resolved_binary
- if head_resolved != target:
- return False
- else:
+ resolved = token
+ if target is not None:
+ # When we know the configured binary, require an exact match
+ # against either the raw or the resolved token. A second codex
+ # install elsewhere on the system is *not* ours to kill.
+ return resolved == target or token == target
# No resolved binary: best-effort basename match so the chip still
# works when the configured CLI isn't on PATH right now.
- if not os.path.basename(head_resolved).startswith("codex"):
- return False
+ return os.path.basename(resolved).startswith("codex")
+
+ # Check argv[0] and argv[1] — the latter is where ``node`` shebang shims
+ # land the codex script path. We deliberately stop at argv[1] so an
+ # unrelated tool with ``codex`` mentioned later in its args isn't swept up.
+ if not any(_matches(tok) for tok in cmdline[:2] if tok):
+ return False
# ``codex app-server`` always passes ``app-server`` as an argv token; we
- # intentionally do NOT match it inside an arbitrary substring.
- return "app-server" in cmdline[1:4]
+ # intentionally do NOT match it inside an arbitrary substring. Widen the
+ # window slightly so the node-shim layout (``node script app-server``)
+ # still hits.
+ return "app-server" in cmdline[1:5]
def _codex_processes(resolved_binary: str | None) -> list[int]: