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(tools): restore portable installed application inventory

rubenluetke10-beep0da8e8d06295
diff --git a/jarvis/plugins/tool/app_inventory.py b/jarvis/plugins/tool/app_inventory.py
new file mode 100644
index 0000000000..a447cb337d
--- /dev/null
+++ b/jarvis/plugins/tool/app_inventory.py
@@ -0,0 +1,142 @@
+"""Installed-application inventory — the sweep behind "where could context live?".
+
+``resolve_app_launch_target`` (app_resolver.py) answers "is THIS app here?" one
+name at a time. Context acquisition (C13, docs/plans/autonomous-missions.md)
+needs the opposite direction: "what IS here?", so an errand can notice that
+WhatsApp or Spotify exists before deciding where to look. This module walks the
+same roots the resolver already trusts — Start-Menu ``.lnk`` trees on Windows,
+``.app`` bundles on macOS, ``.desktop`` entries on Linux.
+
+Constraints, in order of importance:
+- NEVER on the boot critical path (anti-pattern register): callers reach this
+ lazily from an errand's gather phase, and results are cached for a day
+ because installed software changes rarely.
+- Headless-safe: a server with none of these roots yields an empty tuple,
+ quietly. An empty inventory is a useful fact, not an error.
+- Never raises: a broken registry walk costs the inventory, not the errand.
+"""
+
+from __future__ import annotations
+
+import logging
+import os
+import time
+from pathlib import Path
+from typing import Final
+
+from jarvis.platform import detect_platform
+
+# Same package, shared on purpose: the sweep must trust exactly the roots the
+# per-name resolver trusts, or the two would drift apart.
+from .app_resolver import _LINUX_DESKTOP_ENTRY_DIRS, _start_menu_roots
+
+log = logging.getLogger(__name__)
+
+#: Installed software changes rarely; a stale day costs nothing because the
+#: per-name resolver re-verifies before any launch.
+CACHE_TTL_SECONDS: Final[float] = 24 * 3600
+
+#: Cap on the returned inventory. Far beyond what a prompt should ever carry —
+#: the renderer trims further — this only guards against a pathological tree.
+_MAX_APPS: Final[int] = 200
+
+#: Start-Menu folders are full of "Uninstall Foo" companions that name no app.
+_NOISE_PREFIXES: Final[tuple[str, ...]] = ("uninstall", "readme", "help ", "website")
+
+_cache: list[tuple[float, tuple[str, ...]]] = []
+
+
+def installed_app_names(*, force_refresh: bool = False) -> tuple[str, ...]:
+ """Names of applications installed on this machine, cached, sorted, capped."""
+ now = time.monotonic()
+ if _cache and not force_refresh and now - _cache[0][0] < CACHE_TTL_SECONDS:
+ return _cache[0][1]
+ names = _sweep()
+ _cache.clear()
+ _cache.append((now, names))
+ return names
+
+
+def reset_cache() -> None:
+ """Drop the cached sweep. Tests only."""
+ _cache.clear()
+
+
+def _sweep() -> tuple[str, ...]:
+ plat = detect_platform()
+ try:
+ if plat == "win32":
+ raw = _sweep_windows()
+ elif plat == "darwin":
+ raw = _sweep_darwin()
+ else:
+ raw = _sweep_linux()
+ except Exception: # noqa: BLE001 — a broken walk costs the inventory, never a run
+ log.debug("app inventory sweep failed", exc_info=True)
+ return ()
+ cleaned = {
+ name.strip()
+ for name in raw
+ if name.strip() and not name.strip().lower().startswith(_NOISE_PREFIXES)
+ }
+ return tuple(sorted(cleaned, key=str.lower)[:_MAX_APPS])
+
+
+def _sweep_windows(roots: list[str] | None = None) -> list[str]:
+ """Stems of every Start-Menu ``.lnk`` — the tree installers actually fill.
+
+ The same reasoning as ``_resolve_via_start_menu``: per-user Electron
+ installs (Discord, Slack, WhatsApp) appear ONLY here, in neither App Paths
+ nor ``PATH``.
+ """
+ names: list[str] = []
+ for root in _start_menu_roots() if roots is None else roots:
+ try:
+ for _dirpath, _dirnames, filenames in os.walk(root):
+ names.extend(fn[:-4] for fn in filenames if fn.lower().endswith(".lnk"))
+ except OSError:
+ log.debug("Could not scan application root %s", root, exc_info=True)
+ continue
+ return names
+
+
+_DARWIN_APP_ROOTS: Final[tuple[str, ...]] = (
+ "/Applications",
+ "~/Applications",
+ "/System/Applications",
+)
+
+
+def _sweep_darwin(roots: tuple[str, ...] | None = None) -> list[str]:
+ """Top-level ``.app`` bundle names. Deliberately shallow: nested bundles
+ are helpers, not applications a user would name."""
+ names: list[str] = []
+ for root in _DARWIN_APP_ROOTS if roots is None else roots:
+ base = Path(root).expanduser()
+ try:
+ entries = list(base.iterdir())
+ except OSError:
+ log.debug("Could not scan application root %s", root, exc_info=True)
+ continue
+ names.extend(e.stem for e in entries if e.name.endswith(".app"))
+ return names
+
+
+def _sweep_linux(roots: tuple[str, ...] | None = None) -> list[str]:
+ """Stems of ``.desktop`` entries, with reverse-DNS ids shortened to their
+ last segment (``org.mozilla.firefox`` reads as ``firefox``)."""
+ names: list[str] = []
+ for root in _LINUX_DESKTOP_ENTRY_DIRS if roots is None else roots:
+ base = Path(root).expanduser()
+ try:
+ entries = list(base.glob("*.desktop"))
+ except OSError:
+ log.debug("Could not scan application root %s", root, exc_info=True)
+ continue
+ for entry in entries:
+ stem = entry.stem
+ names.append(stem.rsplit(".", 1)[-1] if stem.count(".") >= 2 else stem)
+ return names
+
+
+__all__ = ["CACHE_TTL_SECONDS", "installed_app_names", "reset_cache"]
diff --git a/tests/unit/plugins/tool/test_app_inventory.py b/tests/unit/plugins/tool/test_app_inventory.py
new file mode 100644
index 0000000000..65f5c02657
--- /dev/null
+++ b/tests/unit/plugins/tool/test_app_inventory.py
@@ -0,0 +1,99 @@
+"""Pins for the installed-apps sweep behind the C13 source inventory.
+
+The per-OS sweeps are tested directly against temp trees, so every branch runs
+on every CI platform; the cache and the never-raise promise are pinned on the
+public entry point.
+"""
+
+from __future__ import annotations
+
+from pathlib import Path
+
+import pytest
+
+from jarvis.plugins.tool import app_inventory
+from jarvis.plugins.tool.app_inventory import (
+ _sweep_darwin,
+ _sweep_linux,
+ _sweep_windows,
+ installed_app_names,
+ reset_cache,
+)
+
+
[email protected](autouse=True)
+def fresh_cache() -> None:
+ reset_cache()
+
+
+def test_windows_sweep_reads_start_menu_shortcuts(tmp_path: Path) -> None:
+ programs = tmp_path / "Programs"
+ (programs / "Chat").mkdir(parents=True)
+ (programs / "WhatsApp.lnk").write_bytes(b"")
+ (programs / "Chat" / "Discord.lnk").write_bytes(b"") # nested folders count
+ (programs / "notes.txt").write_bytes(b"") # non-shortcuts do not
+ names = _sweep_windows(roots=[str(programs)])
+ assert sorted(names) == ["Discord", "WhatsApp"]
+
+
+def test_darwin_sweep_reads_top_level_app_bundles(tmp_path: Path) -> None:
+ (tmp_path / "Spotify.app").mkdir()
+ (tmp_path / "Notes.app").mkdir()
+ (tmp_path / "loose-file").write_bytes(b"")
+ names = _sweep_darwin(roots=(str(tmp_path),))
+ assert sorted(names) == ["Notes", "Spotify"]
+
+
+def test_linux_sweep_shortens_reverse_dns_ids(tmp_path: Path) -> None:
+ (tmp_path / "org.mozilla.firefox.desktop").write_bytes(b"")
+ (tmp_path / "gimp.desktop").write_bytes(b"")
+ names = _sweep_linux(roots=(str(tmp_path),))
+ assert sorted(names) == ["firefox", "gimp"]
+
+
+def test_missing_roots_yield_an_empty_quiet_inventory(tmp_path: Path) -> None:
+ """The headless-server promise: no roots, no error, no output."""
+ gone = str(tmp_path / "does-not-exist")
+ assert _sweep_windows(roots=[gone]) == []
+ assert _sweep_darwin(roots=(gone,)) == []
+ assert _sweep_linux(roots=(gone,)) == []
+
+
+def test_uninstall_noise_is_filtered_and_result_is_sorted(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ monkeypatch.setattr(
+ app_inventory,
+ "_sweep_windows",
+ lambda roots=None: ["zoom", "Uninstall Zoom", "Anki", " "],
+ )
+ monkeypatch.setattr(app_inventory, "detect_platform", lambda: "win32")
+ assert installed_app_names(force_refresh=True) == ("Anki", "zoom")
+
+
+def test_the_sweep_runs_once_a_day_not_once_a_call(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ calls: list[int] = []
+
+ def counting(roots=None): # noqa: ANN001, ANN202
+ calls.append(1)
+ return ["OnlyApp"]
+
+ monkeypatch.setattr(app_inventory, "_sweep_windows", counting)
+ monkeypatch.setattr(app_inventory, "detect_platform", lambda: "win32")
+ first = installed_app_names()
+ second = installed_app_names()
+ assert first == second == ("OnlyApp",)
+ assert len(calls) == 1
+
+
+def test_a_broken_sweep_degrades_to_empty_never_raises(
+ monkeypatch: pytest.MonkeyPatch,
+) -> None:
+ def explode(roots=None): # noqa: ANN001, ANN202
+ raise OSError("registry on fire")
+
+ monkeypatch.setattr(app_inventory, "_sweep_windows", explode)
+ monkeypatch.setattr(app_inventory, "detect_platform", lambda: "win32")
+ assert installed_app_names(force_refresh=True) == ()