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(voice): start watchdog via PID file instead of PowerShell scan

rubenluetke10-beepa118c5721853
diff --git a/jarvis/speech/watchdog.py b/jarvis/speech/watchdog.py
index 3146a4b1f7..b3d5d2f0ce 100644
--- a/jarvis/speech/watchdog.py
+++ b/jarvis/speech/watchdog.py
@@ -51,6 +51,91 @@ def _load_env() -> None:
os.environ[k] = v
+def _pid_file(project_root: Path) -> Path:
+ """PID file for the fast voice.bat single-instance check.
+
+ The launcher used to enumerate every process via PowerShell
+ (Get-CimInstance, ~1s startup cost) on each ``voice`` start.
+ A plain PID file plus a native ``tasklist`` liveness probe in the
+ batch file is instant and needs no PowerShell at all.
+ """
+ return project_root / "data" / "jarvis_watchdog.pid"
+
+
+def _write_pid_file(path: Path) -> None:
+ try:
+ path.parent.mkdir(parents=True, exist_ok=True)
+ path.write_text(str(os.getpid()), encoding="utf-8")
+ except OSError:
+ pass
+
+
+def _read_pid_file(path: Path) -> int | None:
+ try:
+ return int(path.read_text(encoding="utf-8").strip())
+ except (OSError, ValueError):
+ return None
+
+
+def _pid_is_live_watchdog(pid: int) -> bool:
+ """True only when ``pid`` is a running jarvis.speech.watchdog process.
+
+ Guards against PID reuse: a stale PID file must never block a fresh
+ start or kill an unrelated process (e.g. a browser that inherited the
+ number). Uses psutil's command line when available; without psutil it
+ fails closed (assumes running) so we never double-start blindly.
+ """
+ if pid <= 0:
+ return False
+ try:
+ import psutil # noqa: PLC0415 - optional at type-check time, required at runtime
+ except ImportError:
+ return True
+ try:
+ proc = psutil.Process(pid)
+ if not proc.is_running():
+ return False
+ try:
+ parts = list(proc.cmdline())
+ except Exception: # noqa: BLE001 - unreadable cmdline fails closed
+ return True
+ joined = " ".join(parts)
+ if "jarvis.speech.watchdog" not in joined:
+ return False
+ # Strict: a real watchdog runs as ``python -m jarvis.speech.watchdog``
+ # (or pythonw). A bare substring also matches our own lightweight
+ # ``python -c "from jarvis.speech.watchdog import ..."`` verifier,
+ # which must never count as a running watchdog.
+ return (
+ ("-m" in parts) or ("speech/watchdog.py" in joined) or ("speech\\watchdog.py" in joined)
+ )
+ except Exception: # noqa: BLE001 - gone/denied means not our watchdog
+ return False
+
+
+def recorded_watchdog_alive(project_root: Path | None = None) -> bool:
+ """Whether the PID file points at a live watchdog process."""
+ root = project_root or Path(__file__).resolve().parents[2]
+ pid = _read_pid_file(_pid_file(root))
+ return pid is not None and _pid_is_live_watchdog(pid)
+
+
+def recorded_watchdog_pid(project_root: Path | None = None) -> int | None:
+ """The PID recorded in the PID file, or ``None`` when unreadable."""
+ root = project_root or Path(__file__).resolve().parents[2]
+ return _read_pid_file(_pid_file(root))
+
+
+def _clear_pid_file(path: Path) -> None:
+ try:
+ if path.is_file():
+ if _read_pid_file(path) != os.getpid():
+ return
+ path.unlink()
+ except OSError:
+ pass
+
+
async def _main() -> None:
if sys.platform == "win32":
try:
@@ -69,6 +154,9 @@ async def _main() -> None:
_setup_logging(log_file)
_load_env()
+ pid_file = _pid_file(project_root)
+ _write_pid_file(pid_file)
+
log = logging.getLogger("jarvis.watchdog")
log.info("=" * 60)
log.info("WATCHDOG-START — Pipeline mit voller Diagnostik")
@@ -172,7 +260,10 @@ async def _main() -> None:
output_device=output_device,
)
log.info("Starting pipeline …")
- await pipeline.run()
+ try:
+ await pipeline.run()
+ finally:
+ _clear_pid_file(pid_file)
if __name__ == "__main__":
diff --git a/voice.bat b/voice.bat
index faca62a4ac..54ee80c5cc 100644
--- a/voice.bat
+++ b/voice.bat
@@ -7,22 +7,67 @@ REM voice → Default: Console sichtbar, volle Logs in data/jarvis_wa
REM voice --quiet → im Hintergrund (pythonw), kein Console-Fenster
REM voice --stop → alle laufenden Voice-Watchdogs beenden
-setlocal
+setlocal EnableDelayedExpansion
cd /d "%~dp0"
+set PIDFILE=data\jarvis_watchdog.pid
if exist ".venv\Scripts\activate.bat" (
call .venv\Scripts\activate.bat
)
-if "%1"=="--stop" (
- REM Beende alle python-Prozesse die jarvis.speech.watchdog ausfuehren.
- powershell -NoProfile -Command "Get-CimInstance Win32_Process -Filter \"Name LIKE 'python%%'\" | Where-Object { $_.CommandLine -match 'jarvis.speech.watchdog' } | ForEach-Object { Write-Host ('Stoppe PID ' + $_.ProcessId); Stop-Process -Id $_.ProcessId -Force }"
- goto :end
-)
+if "%1"=="--stop" goto :do_stop
+if not exist "%PIDFILE%" goto :do_start
+goto :check_running
+
+:do_stop
+REM Fast stop via PID file — no PowerShell, no process scan.
+if not exist "%PIDFILE%" goto :stop_none
+set /p WATCHDOG_PID=<"%PIDFILE%"
+if "!WATCHDOG_PID!"=="" goto :stop_empty
+tasklist /FI "PID eq !WATCHDOG_PID!" 2>NUL | find "!WATCHDOG_PID!" >NUL
+if errorlevel 1 goto :stop_stale
+REM PID is alive — verify it is really our watchdog (PID reuse guard).
+python -c "import sys; from jarvis.speech.watchdog import recorded_watchdog_alive; sys.exit(0 if recorded_watchdog_alive() else 1)" >NUL 2>&1
+if errorlevel 1 goto :stop_foreign
+echo Stoppe PID !WATCHDOG_PID!
+taskkill /PID !WATCHDOG_PID! /F
+del "%PIDFILE%" 2>NUL
+goto :end
+:stop_none
+echo Voice-Watchdog laeuft nicht (keine PID-Datei).
+goto :end
+:stop_empty
+del "%PIDFILE%" 2>NUL
+echo Voice-Watchdog laeuft nicht (leere PID-Datei aufgeraeumt).
+goto :end
+:stop_stale
+echo Kein laufender Prozess zu PID !WATCHDOG_PID! — raeume stale PID-Datei auf.
+del "%PIDFILE%" 2>NUL
+goto :end
+:stop_foreign
+echo PID !WATCHDOG_PID! ist kein Voice-Watchdog — raeume stale PID-Datei auf (Prozess wird NICHT beendet).
+del "%PIDFILE%" 2>NUL
+goto :end
+
+:check_running
+REM Fast single-instance check: no PID file = instant start, straight to STT.
+REM Only when a PID file exists do we spend one tasklist probe (~50ms) plus
+REM one light python verify (~250ms) — still far below the old PowerShell
+REM Get-CimInstance scan (~1s+). No permission hop anywhere on this path.
+set /p WATCHDOG_PID=<"%PIDFILE%"
+if "!WATCHDOG_PID!"=="" goto :stale_pid
+tasklist /FI "PID eq !WATCHDOG_PID!" 2>NUL | find "!WATCHDOG_PID!" >NUL
+if errorlevel 1 goto :stale_pid
+python -c "import sys; from jarvis.speech.watchdog import recorded_watchdog_alive; sys.exit(0 if recorded_watchdog_alive() else 1)" >NUL 2>&1
+if not errorlevel 1 goto :already_running
+:stale_pid
+del "%PIDFILE%" 2>NUL
+goto :do_start
+:already_running
+echo Voice-Watchdog laeuft bereits (PID !WATCHDOG_PID!). Beende mit: voice --stop
+goto :end
-REM Check: ist bereits einer am Laufen?
-powershell -NoProfile -Command "$p = Get-CimInstance Win32_Process -Filter \"Name LIKE 'python%%'\" | Where-Object { $_.CommandLine -match 'jarvis.speech.watchdog' }; if ($p) { Write-Host ('Voice-Watchdog laeuft bereits (PID ' + $p.ProcessId + '). Beende mit: voice --stop'); exit 1 } else { exit 0 }"
-if errorlevel 1 goto :end
+:do_start
if "%1"=="--quiet" (
REM Headless: pythonw laesst keine Console, Logs gehen in data/jarvis_watchdog.log