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): compare prerelease components numerically in nvm sort (Codex P2)

cyh10f8ab1dd5ad
diff --git a/tests/test_ui_api.py b/tests/test_ui_api.py
index 18406ac22..4d00cc236 100644
--- a/tests/test_ui_api.py
+++ b/tests/test_ui_api.py
@@ -165,8 +165,8 @@ def test_detect_cli_skips_non_version_entries_in_nvm(monkeypatch, tmp_path):
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").
+ # the released version must win. Locks in the released > prerelease ranking
+ # in _nvm_version_sort_key (released maps to is_released=True).
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"
@@ -187,6 +187,31 @@ def test_detect_cli_prefers_released_over_prerelease_in_nvm(monkeypatch, tmp_pat
assert result["path"] == str(released_npm)
+def test_detect_cli_sorts_prerelease_numerically_in_nvm(monkeypatch, tmp_path):
+ # "-rc.10" > "-rc.2" numerically, but lexicographic string comparison
+ # would put "-rc.10" before "-rc.2" (because "1" < "2"). Locks in that
+ # suffix tokens are split and the numeric segment compares as int.
+ nvm_node = tmp_path / ".nvm" / "versions" / "node"
+ nvm_node.mkdir(parents=True, exist_ok=True)
+ rc2 = nvm_node / "v22.0.0-rc.2" / "bin" / "npm"
+ rc2.parent.mkdir(parents=True)
+ rc2.write_text("#!/bin/sh\n")
+ rc2.chmod(0o755)
+ rc10 = nvm_node / "v22.0.0-rc.10" / "bin" / "npm"
+ rc10.parent.mkdir(parents=True)
+ rc10.write_text("#!/bin/sh\n")
+ rc10.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
+ # rc.10 must beat rc.2 even though "10" < "2" lexicographically.
+ assert result["path"] == str(rc10)
+
+
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 9cb304bb6..2aa2e9375 100644
--- a/vibe/api.py
+++ b/vibe/api.py
@@ -103,20 +103,38 @@ def _is_executable_file(path: Path) -> bool:
_NVM_VERSION_RE = re.compile(r"^v?(\d+)(?:\.(\d+))?(?:\.(\d+))?(.*)$")
+_NVM_SUFFIX_TOKEN_RE = re.compile(r"\d+|\D+")
+
+
+def _nvm_suffix_tokens(suffix: str) -> tuple[tuple[int, int, str], ...]:
+ # Tokenize the prerelease suffix into (kind, num, text) triples so all
+ # tokens are structurally identical and comparable. kind=0 marks numeric
+ # tokens (compared by num) and kind=1 marks alphanumeric tokens (compared
+ # by text). Numeric tokens compare numerically, so "-rc.10" beats
+ # "-rc.2"; cross-kind tokens never compare int-vs-str, ruling out
+ # TypeError for arbitrary suffix shapes.
+ triples: list[tuple[int, int, str]] = []
+ for tok in _NVM_SUFFIX_TOKEN_RE.findall(suffix):
+ if tok.isdigit():
+ triples.append((0, int(tok), ""))
+ else:
+ triples.append((1, 0, tok))
+ return tuple(triples)
-def _nvm_version_sort_key(entry: Path) -> tuple[int, int, int, str]:
+def _nvm_version_sort_key(entry: Path) -> tuple:
+ # Returns (major, minor, patch, is_released, suffix_tokens). is_released
+ # is True for plain "vX.Y.Z" and False for any "-suffix"; with reverse=True
+ # released versions outrank pre-releases of the same triple. Within
+ # pre-releases, suffix_tokens compares numerically where digits appear.
m = _NVM_VERSION_RE.match(entry.name)
if not m:
- return (-1, -1, -1, entry.name)
+ return (-1, -1, -1, False, ())
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)
+ suffix = m.group(4) or ""
+ return (major, minor, patch, not suffix, _nvm_suffix_tokens(suffix))
def _nvm_binary_candidates(binary: str) -> list[Path]: