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.

perf(agentic-ide): stop re-probing every coding CLI on each state read

Ruben Lütkeabee02594803
diff --git a/jarvis/workspace/agents.py b/jarvis/workspace/agents.py
index a2dca9b58a..3cfb48319c 100644
--- a/jarvis/workspace/agents.py
+++ b/jarvis/workspace/agents.py
@@ -28,7 +28,9 @@ rather than handing the PTY an argv that cannot start.
"""
from __future__ import annotations
+import asyncio
import logging
+import time
from collections.abc import Callable
from dataclasses import dataclass
from typing import Any, Literal
@@ -828,7 +830,43 @@ class AgentInfo:
description: str = ""
-async def detect_agents(prober: CliStatusProber | None = None) -> list[AgentInfo]:
+#: How long a completed detection sweep answers for.
+#:
+#: A sweep is not a lookup — it starts ONE subprocess per registered CLI, and on
+#: Windows every one of those is an npm shim, so `claude --version` is really
+#: cmd.exe -> conhost -> node. Measured on the maintainer's machine: 1.1-1.4 s
+#: wall for five CLIs, and while it runs the shared event loop stalls for up to
+#: 1.7 s (probed with /api/health, which is answered on that same loop).
+#:
+#: That loop is where the wake microphone is delivered, three hops per 100 ms
+#: block with two seconds of buffer behind it, so an uncached sweep is heard as
+#: "the wake word takes longer in the Agentic IDE" — the view re-reads its whole
+#: state on every workspace change, and each of those reads paid for a fresh
+#: sweep. Nothing a sweep answers changes on that timescale: a CLI is installed
+#: or it is not.
+_DETECTION_TTL_S = 30.0
+
+#: Last completed sweep: (monotonic timestamp, result).
+_detection_cache: tuple[float, list[AgentInfo]] | None = None
+#: The sweep currently running, so N callers arriving together (a grid of panes
+#: all asking at once) share ONE burst of subprocesses instead of N.
+_detection_task: asyncio.Task[list[AgentInfo]] | None = None
+
+
+def invalidate_agent_detection() -> None:
+ """Forget the cached sweep — the next read probes the machine again.
+
+ For callers that KNOW the answer just changed (a CLI was installed or
+ removed from inside the app). A user watching an install does not want to
+ wait out the TTL to see it appear.
+ """
+ global _detection_cache
+ _detection_cache = None
+
+
+async def detect_agents(
+ prober: CliStatusProber | None = None, *, force: bool = False
+) -> list[AgentInfo]:
"""Probe every registered entry and report what this machine can run.
CLI entries go through the shared prober; the plain terminal answers from
@@ -836,8 +874,50 @@ async def detect_agents(prober: CliStatusProber | None = None) -> list[AgentInfo
installed" is not a question a ``--version`` probe can ask. Its reported
version is the shell that would actually open ("PowerShell 7", "zsh"), which
is the one thing a user picking it wants to know.
+
+ Answered from a short-lived cache (``_DETECTION_TTL_S``) unless ``force`` is
+ set, because the sweep is expensive enough to be felt elsewhere in the app —
+ see the constant. An explicit ``prober`` (tests, and any caller supplying
+ its own probing strategy) always runs a real sweep and never touches the
+ cache: a caller that hands over the prober is asking for THAT prober's
+ answer.
"""
- prober = prober or CliStatusProber()
+ if prober is not None:
+ return await _sweep_agents(prober)
+
+ global _detection_cache, _detection_task
+ cached = _detection_cache
+ if (
+ not force
+ and cached is not None
+ and time.monotonic() - cached[0] < _DETECTION_TTL_S
+ ):
+ return list(cached[1])
+
+ running = _detection_task
+ if running is not None and not running.done():
+ try:
+ same_loop = running.get_loop() is asyncio.get_running_loop()
+ except RuntimeError: # pragma: no cover - no loop is not reachable here
+ same_loop = False
+ if same_loop:
+ # Shielded: a browser that navigates away mid-sweep must not cancel
+ # the sweep the other five panes are waiting on.
+ return list(await asyncio.shield(running))
+
+ task = asyncio.ensure_future(_sweep_agents(CliStatusProber()))
+ _detection_task = task
+ try:
+ infos = await asyncio.shield(task)
+ finally:
+ if _detection_task is task:
+ _detection_task = None
+ _detection_cache = (time.monotonic(), infos)
+ return list(infos)
+
+
+async def _sweep_agents(prober: CliStatusProber) -> list[AgentInfo]:
+ """One real detection pass — a subprocess per registered CLI."""
specs = [a.spec for a in _AGENTS.values() if a.spec is not None]
statuses = await prober.probe_all(specs) if specs else {}
shell = default_shell()
@@ -959,6 +1039,7 @@ __all__ = [
"get_agent",
"glm_spawn_env",
"install_command",
+ "invalidate_agent_detection",
"kimi_generation",
"list_agents",
"make_cli_agent",
diff --git a/tests/unit/workspace/test_agents.py b/tests/unit/workspace/test_agents.py
index eb5cca5475..31058b9b59 100644
--- a/tests/unit/workspace/test_agents.py
+++ b/tests/unit/workspace/test_agents.py
@@ -4,6 +4,8 @@ Detection runs against a fake prober — no real CLI is ever invoked.
"""
from __future__ import annotations
+import asyncio
+
import pytest
from jarvis.clis.spec import CliSpec, CliStatus
@@ -227,3 +229,93 @@ def test_registering_a_taken_name_is_refused() -> None:
"""Two things answering to one name is a pane running the wrong tool."""
with pytest.raises(ValueError):
register_agent(make_cli_agent("codex", "Impostor", binary="nope"))
+
+
+class CountingProber:
+ """Counts how many detection sweeps actually reach the machine."""
+
+ def __init__(self) -> None:
+ self.sweeps = 0
+
+ async def probe_all(self, specs) -> dict[str, CliStatus]: # noqa: ANN001
+ self.sweeps += 1
+ # Yield once, so concurrent callers really do overlap in the test.
+ await asyncio.sleep(0)
+ return {s.name: CliStatus(installed=True, version="1.0.0") for s in specs}
+
+
+def _no_cached_detection():
+ """Run against a cold cache and leave one behind."""
+ from jarvis.workspace import agents as registry
+
+ registry.invalidate_agent_detection()
+ yield
+ registry.invalidate_agent_detection()
+
+
+async def test_detection_is_cached_between_reads(monkeypatch, _no_cached_detection) -> None:
+ """A repeated read must not restart a subprocess per CLI.
+
+ The sweep spawns one process per registered CLI — on Windows an npm shim,
+ so cmd -> conhost -> node — and the shared event loop stalls while it runs.
+ The Agentic-IDE view re-reads its state on every workspace change, and each
+ of those used to pay for a fresh sweep, which the wake microphone (delivered
+ on that same loop) paid for as added latency.
+ """
+ from jarvis.workspace import agents as registry
+
+ prober = CountingProber()
+ monkeypatch.setattr(registry, "CliStatusProber", lambda: prober)
+
+ first = await detect_agents()
+ second = await detect_agents()
+
+ assert prober.sweeps == 1
+ assert [i.name for i in first] == [i.name for i in second]
+
+
+async def test_concurrent_reads_share_one_sweep(monkeypatch, _no_cached_detection) -> None:
+ """Six panes asking at once are six answers, not six subprocess bursts."""
+ from jarvis.workspace import agents as registry
+
+ prober = CountingProber()
+ monkeypatch.setattr(registry, "CliStatusProber", lambda: prober)
+
+ answers = await asyncio.gather(*(detect_agents() for _ in range(6)))
+
+ assert prober.sweeps == 1
+ assert all(a for a in answers)
+
+
+async def test_force_and_invalidate_reach_the_machine_again(
+ monkeypatch, _no_cached_detection
+) -> None:
+ """A CLI installed while the app runs must not wait out the TTL."""
+ from jarvis.workspace import agents as registry
+
+ prober = CountingProber()
+ monkeypatch.setattr(registry, "CliStatusProber", lambda: prober)
+
+ await detect_agents()
+ await detect_agents(force=True)
+ registry.invalidate_agent_detection()
+ await detect_agents()
+
+ assert prober.sweeps == 3
+
+
+async def test_an_explicit_prober_never_reads_or_fills_the_cache(
+ _no_cached_detection,
+) -> None:
+ """Handing over a prober is asking for THAT prober's answer."""
+ from jarvis.workspace import agents as registry
+
+ mine = FakeProber({"claude": CliStatus(installed=True, version="9.9.9")})
+ infos = {i.name: i for i in await detect_agents(mine)}
+ assert infos["claude"].version == "9.9.9"
+ assert registry._detection_cache is None