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(api): harden nvm version sort against non-version entries

cyh891e704cbae3
diff --git a/tests/test_ui_api.py b/tests/test_ui_api.py
index 752e35157..18406ac22 100644
--- a/tests/test_ui_api.py
+++ b/tests/test_ui_api.py
@@ -135,6 +135,58 @@ def test_detect_cli_finds_npm_in_nvm(monkeypatch, tmp_path):
assert result["path"] == str(npm_path)
+def test_detect_cli_skips_non_version_entries_in_nvm(monkeypatch, tmp_path):
+ # Real-world nvm dir on macOS can contain a .DS_Store file (Finder/iCloud)
+ # and a "system" alias dir, alongside real vX.Y.Z version directories.
+ # Mixed-type sort keys used to crash with TypeError; this regression test
+ # locks in that we filter and sort robustly.
+ nvm_node = tmp_path / ".nvm" / "versions" / "node"
+ nvm_node.mkdir(parents=True, exist_ok=True)
+ (nvm_node / ".DS_Store").write_bytes(b"\x00\x01")
+ (nvm_node / "system").mkdir()
+ (nvm_node / "v18.20.0" / "bin").mkdir(parents=True)
+ older_npm = nvm_node / "v18.20.0" / "bin" / "npm"
+ older_npm.write_text("#!/bin/sh\n")
+ older_npm.chmod(0o755)
+ newer_npm = nvm_node / "v22.18.0" / "bin" / "npm"
+ newer_npm.parent.mkdir(parents=True)
+ newer_npm.write_text("#!/bin/sh\n")
+ newer_npm.chmod(0o755)
+
+ monkeypatch.setattr(api.Path, "home", lambda: tmp_path)
+ monkeypatch.setattr(api.shutil, "which", lambda binary: None)
+
+ result = api.detect_cli("npm")
+
+ assert result["found"] is True
+ # Newer version must win over older one after filtering out junk entries.
+ assert result["path"] == str(newer_npm)
+
+
+def test_detect_cli_prefers_released_over_prerelease_in_nvm(monkeypatch, tmp_path):
+ # When both a released and a pre-release of the same major.minor.patch exist,
+ # the released version must win. Locks in the suffix-priority logic in
+ # _nvm_version_sort_key (empty suffix maps to "~" so released > "-rc.1").
+ nvm_node = tmp_path / ".nvm" / "versions" / "node"
+ nvm_node.mkdir(parents=True, exist_ok=True)
+ rc_npm = nvm_node / "v22.0.0-rc.1" / "bin" / "npm"
+ rc_npm.parent.mkdir(parents=True)
+ rc_npm.write_text("#!/bin/sh\n")
+ rc_npm.chmod(0o755)
+ released_npm = nvm_node / "v22.0.0" / "bin" / "npm"
+ released_npm.parent.mkdir(parents=True)
+ released_npm.write_text("#!/bin/sh\n")
+ released_npm.chmod(0o755)
+
+ monkeypatch.setattr(api.Path, "home", lambda: tmp_path)
+ monkeypatch.setattr(api.shutil, "which", lambda binary: None)
+
+ result = api.detect_cli("npm")
+
+ assert result["found"] is True
+ assert result["path"] == str(released_npm)
+
+
def test_detect_cli_finds_codex_in_npm_global_prefix(monkeypatch, tmp_path):
npm_path = tmp_path / "tools" / "npm"
npm_path.parent.mkdir(parents=True, exist_ok=True)
diff --git a/vibe/api.py b/vibe/api.py
index 9b5c10e99..9cb304bb6 100644
--- a/vibe/api.py
+++ b/vibe/api.py
@@ -102,20 +102,40 @@ def _is_executable_file(path: Path) -> bool:
return path.exists() and path.is_file() and os.access(path, os.X_OK)
+_NVM_VERSION_RE = re.compile(r"^v?(\d+)(?:\.(\d+))?(?:\.(\d+))?(.*)$")
+
+
+def _nvm_version_sort_key(entry: Path) -> tuple[int, int, int, str]:
+ m = _NVM_VERSION_RE.match(entry.name)
+ if not m:
+ return (-1, -1, -1, entry.name)
+ major = int(m.group(1))
+ minor = int(m.group(2)) if m.group(2) else 0
+ patch = int(m.group(3)) if m.group(3) else 0
+ # Released versions ("") must outrank pre-release tags like "-rc.1".
+ # "~" is high-ASCII, so empty suffix maps to it and sorts last lexically,
+ # which after reverse=True puts released versions ahead of pre-releases.
+ suffix = m.group(4) or "~"
+ return (major, minor, patch, suffix)
+
+
def _nvm_binary_candidates(binary: str) -> list[Path]:
versions_dir = Path.home() / ".nvm" / "versions" / "node"
if not versions_dir.exists():
return []
- def _version_sort_key(path: Path) -> tuple:
- parts = str(path.name).lstrip("v").split(".")
- key: list[int | str] = []
- for part in parts:
- key.append(int(part) if part.isdigit() else part)
- return tuple(key)
+ valid: list[Path] = []
+ for entry in versions_dir.iterdir():
+ # Skip non-directory entries (e.g. macOS .DS_Store) and non-version
+ # dirs (e.g. nvm's "system" alias) before sorting.
+ if not entry.is_dir():
+ continue
+ if not _NVM_VERSION_RE.match(entry.name):
+ continue
+ valid.append(entry)
candidates: list[Path] = []
- for version_dir in sorted(versions_dir.glob("*"), key=_version_sort_key, reverse=True):
+ for version_dir in sorted(valid, key=_nvm_version_sort_key, reverse=True):
candidate = version_dir / "bin" / binary
if candidate not in candidates:
candidates.append(candidate)