Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

fix(agentic-ide): let the installed Kimi binary decide its generation, not a stale folder

Ruben Lütkee5df3eba66e9
diff --git a/jarvis/workspace/agents.py b/jarvis/workspace/agents.py
index a217e9f738..a2dca9b58a 100644
--- a/jarvis/workspace/agents.py
+++ b/jarvis/workspace/agents.py
@@ -365,6 +365,11 @@ KIMI_CURRENT = "kimi-code"
KIMI_LEGACY = "kimi-cli"
+#: The npm package that IS the current generation. Its presence on the resolved
+#: binary path is the only positive, subprocess-free proof of which one runs.
+_KIMI_CURRENT_PACKAGE = "kimi-code"
+
+
def kimi_generation() -> str | None:
"""Which Kimi is installed: the current CLI, the wound-down one, or unknown.
@@ -376,14 +381,49 @@ def kimi_generation() -> str | None:
therefore not polish: it is the difference between resuming a conversation
and opening a stranger's.
- Decided on the data directory, which is the thing that actually differs and
- the one signal that costs no subprocess. ``None`` means neither has been run
- yet on this machine — a fresh install, where the honest answer is "whatever
- the installer just put there", and the caller falls back to the current
- generation's behaviour because that is what a fresh install gets.
+ **The BINARY decides, not the data directory**, and that order was learned
+ the hard way. Deciding on the data directory looks cheaper and is wrong for
+ the single most likely state a machine is in: someone upgrades, so the new
+ binary is on PATH — but its data root does not exist until the first run,
+ while the old generation's root is still lying there from before. A
+ directory-first probe reports the WOUND-DOWN generation on a machine that
+ can no longer run it, and every pane then reads the wrong history and offers
+ to resume conversations belonging to a CLI that is gone. Measured on the
+ maintainer's own box the moment the upgrade landed.
+
+ So: the resolved binary path first (an npm install carries the package name
+ in it, which is positive proof and costs no subprocess), then the data roots
+ as a fallback for an install laid out some other way. ``None`` means nothing
+ on this machine says either way, and the caller reads no history at all
+ rather than guessing at one.
"""
+ import shutil
from pathlib import Path
+ try:
+ from jarvis.core.path_augment import ensure_cli_paths
+
+ ensure_cli_paths()
+ except Exception: # noqa: BLE001, S110 - PATH augmentation is best-effort
+ pass
+ binary = shutil.which("kimi")
+ if binary:
+ try:
+ resolved = Path(binary).resolve()
+ except OSError:
+ resolved = Path(binary)
+ # The shim itself is just "kimi", so the package name shows up either in
+ # the resolved path (a POSIX symlink into the package) or in a sibling
+ # node_modules tree (the Windows .cmd shim, which stays where it is).
+ parts = {part.lower() for part in resolved.parts}
+ if _KIMI_CURRENT_PACKAGE in parts:
+ return KIMI_CURRENT
+ package = (
+ resolved.parent / "node_modules" / "@moonshot-ai" / _KIMI_CURRENT_PACKAGE
+ )
+ if package.is_dir():
+ return KIMI_CURRENT
+
home = Path.home()
if (home / ".kimi-code").is_dir():
return KIMI_CURRENT
@@ -524,6 +564,15 @@ _AGENTS: dict[str, WorkspaceAgent] = {
# to an installed binary reads as a broken install.
version_regex=_LOOSE_SEMVER_RE,
needs_trust=False,
+ # The npm install puts a .cmd shim on PATH that runs node against the
+ # package's own entrypoint; going straight there keeps the pane's own
+ # process the agent rather than a cmd wrapper around it.
+ win_shim=WinShim(
+ relative_path=(
+ "node_modules", "@moonshot-ai", "kimi-code", "dist", "main.mjs",
+ ),
+ kind="node",
+ ),
# Its update preflight can check, install and PROMPT mid-session.
spawn_env=(("KIMI_CODE_NO_AUTO_UPDATE", "1"),),
# DELIBERATELY no AccountSpec — several seats of this CLI are not
diff --git a/tests/unit/agentic_ide/test_new_cli_providers.py b/tests/unit/agentic_ide/test_new_cli_providers.py
index 0aa3fac165..1878a2e1f4 100644
--- a/tests/unit/agentic_ide/test_new_cli_providers.py
+++ b/tests/unit/agentic_ide/test_new_cli_providers.py
@@ -240,11 +240,12 @@ def test_kimi_folder_key_is_derived_from_the_native_path(tmp_path: Path) -> None
assert agent_sessions._kimi_folder_key(str(tmp_path)) == expected
-def test_the_kimi_generation_probe_reads_the_data_root(
+def test_the_kimi_generation_probe_falls_back_to_the_data_root(
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
) -> None:
"""Two generations, one binary name — and the wrong flags on the wrong one."""
monkeypatch.setattr(Path, "home", classmethod(lambda cls: tmp_path))
+ monkeypatch.setattr("shutil.which", lambda _name: None)
assert workspace_agents.kimi_generation() is None
(tmp_path / ".kimi").mkdir()
assert workspace_agents.kimi_generation() == workspace_agents.KIMI_LEGACY
@@ -252,6 +253,46 @@ def test_the_kimi_generation_probe_reads_the_data_root(
assert workspace_agents.kimi_generation() == workspace_agents.KIMI_CURRENT
+def test_the_installed_binary_outranks_a_leftover_data_directory(
+ monkeypatch: pytest.MonkeyPatch, tmp_path: Path
+) -> None:
+ """The state a machine is actually in right after someone upgrades.
+
+ The new binary is on PATH, its data root does not exist yet (nothing has
+ run), and the OLD generation's root is still lying there from before. A
+ probe that reads directories first answers "wound-down generation" on a
+ machine that can no longer run it — and every pane then reads the wrong
+ history and offers to resume conversations belonging to a CLI that is gone.
+ Reproduced on the maintainer's own box the moment the upgrade landed.
+ """
+ monkeypatch.setattr(Path, "home", classmethod(lambda cls: tmp_path))
+ (tmp_path / ".kimi").mkdir() # left behind by the previous generation
+ assert not (tmp_path / ".kimi-code").exists()
+
+ # The Windows shape: a .cmd shim beside the package it launches.
+ shim_dir = tmp_path / "npm"
+ package = shim_dir / "node_modules" / "@moonshot-ai" / "kimi-code"
+ package.mkdir(parents=True)
+ shim = shim_dir / "kimi.cmd"
+ shim.write_text("@echo off\n", encoding="utf-8")
+ monkeypatch.setattr("shutil.which", lambda _name: str(shim))
+ assert workspace_agents.kimi_generation() == workspace_agents.KIMI_CURRENT
+
+ # The POSIX shape: the package name is in the resolved path itself.
+ posix_bin = package / "dist" / "main.mjs"
+ posix_bin.parent.mkdir(parents=True)
+ posix_bin.write_text("#!/usr/bin/env node\n", encoding="utf-8")
+ monkeypatch.setattr("shutil.which", lambda _name: str(posix_bin))
+ assert workspace_agents.kimi_generation() == workspace_agents.KIMI_CURRENT
+
+ # And a binary that says nothing either way still lets the roots decide.
+ stray = tmp_path / "elsewhere" / "kimi"
+ stray.parent.mkdir()
+ stray.write_text("", encoding="utf-8")
+ monkeypatch.setattr("shutil.which", lambda _name: str(stray))
+ assert workspace_agents.kimi_generation() == workspace_agents.KIMI_LEGACY
+
+
# ------------------------------------------------------------------- GLM