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(launcher): every launch leaves a trace, and a stuck instance is asked about instead of bounced in silence

rubenluetke10-beepc79001f788dc
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 2efae00767..0fd152a919 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -30,6 +30,14 @@ versioning per [SemVer](https://semver.org/).
### Fixed
+- **A launch that never reaches a window is no longer mute.** The desktop
+ log now starts from the launcher's first millisecond, so an "already
+ running" bounce, a crash on the way to the window or a lock held by a stuck
+ earlier instance is written down instead of vanishing. When the running
+ instance has no window to bring forward, the app asks — one native Yes/No
+ box on Windows, macOS and Linux — whether to stop the stuck process and
+ start fresh; a restart that fails says so the same way. (BUG-180)
+
- **A dictated prompt reaches a Tauri/Electron coding-agent terminal.** On
Windows the transcript is now offered with delayed clipboard rendering, so
Jarvis sees who reads it: a proven paste restores the old clipboard at once,
diff --git a/docs/BUGS.md b/docs/BUGS.md
index ecc5de2f67..7bfbf226a9 100644
--- a/docs/BUGS.md
+++ b/docs/BUGS.md
@@ -12644,3 +12644,72 @@ window frame can change what the page is allowed to do, the page states its own
policy as well, so the app does not depend on every shell agreeing with it.
---
+
+## BUG-180: the app "takes minutes to start" — every launch that never reached a window was mute, and a stuck earlier instance turned every click into a silent exit (HIGH, FIXED 2026-08-25)
+
+**Symptom.** Click the Start-Menu shortcut, wait, nothing. Click again,
+nothing. Five to eleven minutes later a window appears after yet another
+click, and the machine was idle the whole time. `data/jarvis_desktop.log`
+shows the previous instance shutting down at 09:58:24 and the next line at
+10:09:16 — a hole with no explanation. Measured from the log, the boot itself
+takes ~2.5 s to a painted window and ~8 s to a usable voice, so the wait was
+never the boot.
+
+**Root cause.** Three things stacked, all invisible by construction:
+
+1. The file log sink was installed by `DesktopApp.__init__`, i.e. AFTER the
+ branded re-exec, the elevation check, config, the control key and the
+ single-instance lock. A launch that ended anywhere in that stretch ran
+ under `pythonw.exe` — no console, no stderr — and left no trace. The one
+ footprint of the 10:02:41 attempt is the autostart shortcut it rewrote
+ (that write sits just before the lock); the log never heard of it.
+2. `_run_desktop` answered a held lock with `print("already running")` to a
+ stderr that does not exist, then `focus_existing_instance_robust()`, then
+ exit 3. When the holder had no window (a previous instance whose teardown
+ wedged after its accept socket died — the `_start_serving ... self._sockets
+ is not None` AssertionError in the log — or a lock the zombie eviction
+ could not reclaim), focusing found nothing and the launch simply ended.
+ Every further click did the same until the stuck process died on its own.
+3. A restart that failed (`jarvis.ui.relauncher.main` giving up after three
+ spawns) returned 1 from a windowless helper — also nowhere.
+
+**Fix.**
+* `jarvis/ui/desktop_log.py` (new) — the sink + writer thread moved out of
+ `desktop_app` so the launcher can install them from its first millisecond
+ without the ~400 ms config import; `desktop_log_path()` mirrors
+ `config.DATA_DIR` exactly (parity test). `launcher._install_boot_trace`
+ writes `launcher: start pid=… argv=…`, the branded-exe hand-off, and every
+ already-running decision into the same `jarvis_desktop.log`.
+* `launcher.main` is now a crash-reporting wrapper around `_main`: an
+ exception before the window is logged with its traceback and shown in a
+ native box (`_report_startup_failure`).
+* `launcher._recover_from_already_running` — a healthy holder is focused as
+ before; a holder WITHOUT a window becomes one native Yes/No question ("It is
+ probably stuck. Stop that process and start fresh?"). Only an explicit Yes
+ terminates it and takes the lock; No, no display, no helper and any error
+ all count as No. `jarvis/ui/native_dialog.py` (new) carries `ask_yes_no` and
+ `show_error_dialog` for Windows (Win32), macOS (osascript) and Linux
+ (zenity/kdialog, nothing without a display).
+* `desktop_app._terminate_pid` logs each step and, on Windows, falls back to
+ `taskkill /F /T` for the whole process tree when psutil's kill did not take —
+ a wedged instance's child (inference server, ConPTY host) is often what
+ keeps it alive.
+* `relauncher.main` reports a failed restart (`_report_restart_failure`):
+ stderr when readable, a native box when not.
+* Two things the live test of the fix surfaced, fixed alongside: the log
+ writer thread is now drained at interpreter exit (`atexit`), so the last
+ line of a process that ends right after writing it — the bounce itself — is
+ no longer lost; and `_default_lock_holder_health` treats a port that
+ CONNECTS but answers late as a busy holder (return True) rather than a
+ zombie — only a refused connection (nothing bound) still counts as dead.
+ Before, a second click during the heavy part of a boot waited 6 s on four
+ 1-second probes and was one timeout away from terminating the very instance
+ it was supposed to bring forward.
+
+**Tests.** `tests/unit/ui/test_launcher_boot_trace.py`,
+`tests/unit/ui/test_native_dialog_and_eviction.py`.
+
+**Not changed, on purpose.** The boot path itself. The 0.5 s spent by the
+`pythonw` parent before it re-execs through the branded exe is the price of a
+branded taskbar button (`icon_utils.maybe_reexec_through_branded_launcher`);
+a shortcut that targets the in-venv branded exe directly skips it.
diff --git a/jarvis/ui/desktop_app.py b/jarvis/ui/desktop_app.py
index 2d4c538644..1dd5cddbc5 100644
--- a/jarvis/ui/desktop_app.py
+++ b/jarvis/ui/desktop_app.py
@@ -1,4 +1,4 @@
-"""Desktop app wrapper: pywebview window + FastAPI backend lifecycle.
+"""Desktop app wrapper: pywebview window + FastAPI backend lifecycle.
Coordinates:
1. Single-instance lock (filelock + PID sidecar + stale detection).
@@ -200,10 +200,7 @@ def _supported_call_kwargs(
# every kwarg through is the documented fallback of this helper.
return dict(kwargs)
parameters = tuple(signature.parameters.values())
- if any(
- parameter.kind is inspect.Parameter.VAR_KEYWORD
- for parameter in parameters
- ):
+ if any(parameter.kind is inspect.Parameter.VAR_KEYWORD for parameter in parameters):
return dict(kwargs)
keyword_names = {
parameter.name
@@ -231,273 +228,19 @@ class SingleInstanceError(RuntimeError):
# ---------------------------------------------------------------------------
-_DESKTOP_LOG_SINK_INSTALLED = False
-#: Rotate the desktop log at this size, keeping ``_LOG_RETENTION`` older files.
-_LOG_ROTATION_BYTES = 10 * 1024 * 1024
-_LOG_RETENTION = 3
-#: Bound on records waiting to reach disk. Deep enough to absorb a multi-second
-#: disk stall at the observed rate (a few hundred records per minute), shallow
-#: enough that a permanently wedged disk cannot grow the process without limit.
-_LOG_QUEUE_MAX = 20_000
-#: Records fused into one write+flush pair. Chatty subsystems (the wake
-#: heartbeat, Telegram polling) otherwise pay two syscalls per line.
-_LOG_BATCH_MAX = 256
-#: Sentinel telling the writer thread to drain and exit.
-_LOG_STOP = object()
-
-
-class _AsyncLogWriter:
- """Write loguru records to the log file from a dedicated thread.
-
- Why this exists instead of loguru's own file sink: a file sink runs
- INLINE on whichever thread emitted the record, and the backend asyncio
- loop emits constantly. One slow ``write()`` therefore blocks every
- WebSocket, HTTP route and brain turn behind it. Observed live on
- 2026-08-03 under heavy machine load: a 24.4 s event-loop stall whose
- stack ended in ``loguru/_file_sink.py:write``, immediately followed by
- ``listening socket ... is dead`` and an unhandled proactor error — the
- window lost its backend and never came back.
-
- loguru's ``enqueue=True`` would decouple the same way, but it builds a
- multiprocessing pipe that can fail with WinError 5 in restricted
- desktop/sandbox contexts before the window exists. A plain threading
- queue buys the decoupling without a pipe; rotation and retention, which
- the file sink would otherwise provide, are carried here instead.
-
- On overflow records are dropped rather than growing the queue without
- bound, and the dropped count is reported into the log as soon as the disk
- keeps up again — dropping silently would make the log lie about its own
- completeness (AP-30).
- """
-
- def __init__(
- self,
- path: Path,
- *,
- rotation_bytes: int = _LOG_ROTATION_BYTES,
- retention: int = _LOG_RETENTION,
- max_queue: int = _LOG_QUEUE_MAX,
- ) -> None:
- self._path = Path(path)
- self._rotation_bytes = rotation_bytes
- self._retention = retention
- self._queue: queue.Queue[Any] = queue.Queue(maxsize=max_queue)
- self._drop_lock = threading.Lock()
- self._dropped = 0
- self._handle: Any = None
- self._written = 0
- self._thread = threading.Thread(
- target=self._run,
- name="jarvis-log-writer",
- daemon=True,
- )
- self._thread.start()
-
- # -- loguru sink -------------------------------------------------------
- def __call__(self, message: Any) -> None:
- """Hand one formatted record to the writer thread. Never blocks."""
- try:
- self._queue.put_nowait(str(message))
- except queue.Full:
- # Silence is the point: a log sink that blocks or raises would stall
- # whichever thread emitted the record. The drop is not lost — the
- # count surfaces as a WARNING line in the next emitted batch.
- with self._drop_lock:
- self._dropped += 1
-
- def stop(self, timeout: float = 5.0) -> None:
- """Drain and close. Used by tests; the daemon thread needs no stop."""
- with suppress(queue.Full):
- self._queue.put_nowait(_LOG_STOP)
- self._thread.join(timeout=timeout)
-
- # -- writer thread -----------------------------------------------------
- def _run(self) -> None:
- while True:
- first = self._queue.get()
- if first is _LOG_STOP:
- break
- batch = [first]
- stopping = False
- while len(batch) < _LOG_BATCH_MAX:
- try:
- item = self._queue.get_nowait()
- except queue.Empty:
- # Not a failure: an empty queue simply ends this batch and
- # the outer loop blocks on the next record.
- break
- if item is _LOG_STOP:
- stopping = True
- break
- batch.append(item)
- self._emit(batch)
- if stopping:
- break
- self._close()
-
- def _emit(self, batch: list[str]) -> None:
- with self._drop_lock:
- dropped, self._dropped = self._dropped, 0
- if dropped:
- batch.insert(
- 0,
- f"{time.strftime('%Y-%m-%d %H:%M:%S')} | WARNING | "
- f"{__name__}:_AsyncLogWriter:0 | log writer dropped "
- f"{dropped} record(s) while the disk was not keeping up.\n",
- )
- try:
- handle = self._ensure_handle()
- handle.write("".join(batch))
- handle.flush()
- self._written = handle.tell()
- except (OSError, ValueError) as exc:
- # Cannot log this — we ARE the log. stderr is None under
- # pythonw.exe, so on the windowed build the only remaining signal
- # is the drop counter above, reported once writing recovers.
- self._handle = None
- stream = getattr(sys, "__stderr__", None)
- if stream is not None:
- with suppress(Exception):
- stream.write(f"jarvis log writer failed: {exc!r}\n")
- return
- if self._written >= self._rotation_bytes:
- self._rotate()
-
- def _ensure_handle(self) -> Any:
- if self._handle is None:
- self._path.parent.mkdir(parents=True, exist_ok=True)
- # newline="" keeps loguru's "\n" from becoming "\r\n" on Windows,
- # matching what the previous file sink wrote.
- self._handle = self._path.open("a", encoding="utf-8", errors="replace", newline="")
- self._written = self._handle.tell()
- return self._handle
-
- def _close(self) -> None:
- handle, self._handle = self._handle, None
- if handle is not None:
- with suppress(Exception):
- handle.close()
-
- def _rotate(self) -> None:
- self._close()
- # Microsecond suffix mirrors the naming the loguru file sink used, so
- # older rotated logs and new ones sort together — and two rotations in
- # the same second cannot collide on a name.
- now = time.time()
- stamp = time.strftime("%Y-%m-%d_%H-%M-%S", time.localtime(now))
- target = self._path.with_name(
- f"{self._path.stem}.{stamp}_{int(now % 1 * 1_000_000):06d}{self._path.suffix}"
- )
- try:
- self._path.rename(target)
- except OSError:
- # Another process holds the file open (Windows) or the rename
- # raced. Keep appending to the current file — an oversized log is
- # strictly better than losing records.
- self._written = 0
- return
- self._prune()
-
- def _prune(self) -> None:
- pattern = f"{self._path.stem}.*{self._path.suffix}"
- try:
- rotated = sorted(
- self._path.parent.glob(pattern),
- key=lambda item: item.stat().st_mtime,
- reverse=True,
- )
- except OSError:
- # Listing the log directory failed (removed, or locked mid-scan on
- # Windows). Pruning is housekeeping — skipping one round keeps a
- # few extra rotated files, which beats raising inside the writer.
- return
- for stale in rotated[self._retention :]:
- with suppress(OSError):
- stale.unlink()
-
-
-def _install_desktop_log_sink(log_path: Path) -> None:
- """Installs a loguru file sink for the desktop app.
-
- Why: ``pythonw.exe`` (windowed mode, via ``run.bat`` without args) has
- no stderr. Loguru writes to stderr by default → any crash in the
- backend thread stays invisible, and the process becomes a zombie (port
- not bound, window not open, user sees nothing).
-
- This sink writes every ``INFO+`` event to a rotating log file, and
- stdlib ``logging`` is redirected via ``InterceptHandler`` so that
- ``uvicorn`` / ``httpx`` / ``faster_whisper`` get captured too. Writing
- happens on :class:`_AsyncLogWriter`'s thread so that a slow disk can
- never stall the caller — see that class for the incident this prevents.
-
- Idempotent — calling it more than once is a no-op (important in case
- DesktopApp gets instantiated multiple times in tests).
- """
- global _DESKTOP_LOG_SINK_INSTALLED
- if _DESKTOP_LOG_SINK_INSTALLED:
- return
- _DESKTOP_LOG_SINK_INSTALLED = True
-
- from loguru import logger
-
- log_path.parent.mkdir(parents=True, exist_ok=True)
- logger.add(
- _AsyncLogWriter(log_path),
- level="INFO",
- # The writer thread IS the queue. loguru's own enqueue= would add a
- # multiprocessing pipe on top (WinError 5 in restricted contexts).
- enqueue=False,
- backtrace=True,
- diagnose=False, # don't dump locals (secrets!)
- format="{time:YYYY-MM-DD HH:mm:ss.SSS} | {level: <8} | {name}:{function}:{line} | {message}",
- )
-
- # Redirect stdlib logging -> loguru so uvicorn / httpx / faster_whisper
- # also end up in the file log. Don't remove prior handlers (the
- # watchdog run has its own handlers via _setup_logging).
- import logging as _logging
-
- from jarvis.core.redact import safe_preview as _safe_preview
-
- class _InterceptHandler(_logging.Handler):
- def emit(self, record: _logging.LogRecord) -> None:
- try:
- level: str | int = logger.level(record.levelname).name
- except ValueError:
- # A custom stdlib level loguru does not know by name; the
- # numeric level carries the same information.
- level = record.levelno
- frame, depth = _logging.currentframe(), 2
- while frame and frame.f_code.co_filename == _logging.__file__:
- frame = frame.f_back
- depth += 1
- message = _safe_preview(record.getMessage(), max_chars=16_384)
- logger.opt(depth=depth, exception=record.exc_info).log(level, message)
-
- root = _logging.getLogger()
- # Only add it if there isn't already an InterceptHandler present.
- if not any(isinstance(h, _InterceptHandler) for h in root.handlers):
- root.addHandler(_InterceptHandler())
- if root.level > _logging.INFO or root.level == 0:
- root.setLevel(_logging.INFO)
-
- logger.info("Desktop log sink active: {}", log_path)
- # Which interpreter is running the app is the first question a boot failure
- # raises, and the log never answered it. A machine carries several Python
- # installations, and a shortcut, scheduled task or launcher that resolves to
- # a different one than the app was installed into looks exactly like "it
- # suddenly stopped starting" (forensic 2026-08-09: a Start-menu shortcut
- # pointed at an interpreter without pywebview, so the window import died
- # 8 ms into boot). One line, every platform, no imports.
- logger.info("Interpreter: {} (Python {}.{}.{})", sys.executable, *sys.version_info[:3])
-
+# The file sink and its writer thread live in ``jarvis.ui.desktop_log`` so the
+# launcher can install them BEFORE this module (and the config graph it drags
+# in) is imported. Re-exported here: the names are part of this module's
+# surface for tests and for the crash hooks below.
+from jarvis.ui.desktop_log import ( # noqa: E402 — keep the section grouping
+ _AsyncLogWriter, # noqa: F401 — re-exported for tests
+ _install_desktop_log_sink,
+)
_CRASH_HOOKS_INSTALLED = False
-def _build_crash_hooks(
- previous_thread_hook: Any, previous_main_hook: Any
-) -> tuple[Any, Any]:
+def _build_crash_hooks(previous_thread_hook: Any, previous_main_hook: Any) -> tuple[Any, Any]:
"""Build the ``(threading.excepthook, sys.excepthook)`` pair.
Split out from the installer so the behaviour can be tested without
@@ -510,9 +253,7 @@ def _build_crash_hooks(
# A thread torn down during interpreter shutdown reports SystemExit
# with no traceback. That is an exit, not a crash.
if args.exc_type is not SystemExit:
- logger.opt(
- exception=(args.exc_type, args.exc_value, args.exc_traceback)
- ).critical(
+ logger.opt(exception=(args.exc_type, args.exc_value, args.exc_traceback)).critical(
"Thread '{}' died on an unhandled exception — whatever it was "
"serving is gone for the rest of this process.",
getattr(args.thread, "name", "<unnamed>"),
@@ -559,9 +300,7 @@ def _install_crash_hooks() -> None:
return
_CRASH_HOOKS_INSTALLED = True
- threading.excepthook, sys.excepthook = _build_crash_hooks(
- threading.excepthook, sys.excepthook
- )
+ threading.excepthook, sys.excepthook = _build_crash_hooks(threading.excepthook, sys.excepthook)
# ---------------------------------------------------------------------------
@@ -764,26 +503,20 @@ def _force_foreground_hwnd(hwnd: int, user32: Any, kernel32: Any) -> bool:
current_thread = kernel32.GetCurrentThreadId()
foreground = user32.GetForegroundWindow()
- foreground_thread = (
- user32.GetWindowThreadProcessId(foreground, None) if foreground else 0
- )
+ foreground_thread = user32.GetWindowThreadProcessId(foreground, None) if foreground else 0
target_thread = user32.GetWindowThreadProcessId(hwnd, None)
attached_foreground = False
attached_target = False
try:
if foreground_thread and foreground_thread != current_thread:
attached_foreground = bool(
- user32.AttachThreadInput(
- current_thread, foreground_thread, True
- )
+ user32.AttachThreadInput(current_thread, foreground_thread, True)
)
if target_thread and target_thread not in (
current_thread,
foreground_thread,
):
- attached_target = bool(
- user32.AttachThreadInput(current_thread, target_thread, True)
- )
+ attached_target = bool(user32.AttachThreadInput(current_thread, target_thread, True))
user32.BringWindowToTop(hwnd)
user32.ShowWindow(hwnd, 9) # SW_RESTORE
user32.SetForegroundWindow(hwnd)
@@ -1106,27 +839,82 @@ def _default_lock_holder_health(port: int) -> bool:
return True # cannot probe → never evict on uncertainty
url = f"http://127.0.0.1:{int(port)}/api/health"
for _ in range(4):
- with suppress(Exception):
+ try:
r = httpx.get(url, timeout=1.0)
if r.status_code == 200:
return True
+ except httpx.ConnectError:
+ # Nothing accepts on the port: the lock-zombie signature (its
+ # accept socket died while the process lived on). Probe again
+ # briefly — a fast-boot instance binds within the second.
+ pass
+ except Exception: # noqa: BLE001 — connected, but no answer in time
+ # The port IS bound and merely slow: an instance in its heavy
+ # initialisation (or under a GIL stall) can miss a 1 s deadline
+ # four times in a row. That is a busy holder, never a zombie —
+ # evicting it would kill a working app (live 2026-08-25: a second
+ # launch during boot waited 6 s here and would have been one probe
+ # away from terminating the instance it was meant to focus).
+ return True
time.sleep(0.5)
return False
-def _terminate_pid(pid: int) -> bool:
+def _kill_process_tree_win32(pid: int) -> bool:
+ """``taskkill /F /T`` — the OS-level fallback when psutil's kill did not take.
+
+ A wedged instance usually has children (a local inference server, a
+ terminal pane's ConPTY host, a browser helper) and it is often one of THOSE
+ holding the handle that keeps the parent from dying. ``taskkill /T`` ends
+ the whole tree at once. Windows only; elsewhere the caller's psutil path is
+ already the strongest tool available. Returns True when the command ran and
+ reported success — the caller still re-checks liveness.
+ """
+ if sys.platform != "win32":
+ return False
+ try:
+ import subprocess
+
+ from jarvis.core.process_utils import NO_WINDOW_CREATIONFLAGS
+
+ completed = subprocess.run( # noqa: S603 — fixed argv, pid is an int
+ ["taskkill", "/F", "/T", "/PID", str(int(pid))],
+ capture_output=True,
+ text=True,
+ encoding="utf-8",
+ errors="replace",
+ check=False,
+ timeout=15.0,
+ creationflags=NO_WINDOW_CREATIONFLAGS,
+ )
+ except Exception as exc: # noqa: BLE001
+ with suppress(Exception):
+ from loguru import logger
+
+ logger.warning("taskkill fallback for pid={} failed to run: {}", pid, exc)
+ return False
+ return completed.returncode == 0
+
+
+def _terminate_pid(pid: int, *, _tree_kill=_kill_process_tree_win32) -> bool:
"""Terminate a confirmed lock-zombie process. Returns True once it is gone.
- Graceful ``terminate()`` first, then ``kill()`` if it lingers. Returns False
- (→ the caller keeps the lock blocked, the SAFE outcome) when psutil is
- missing or the kill did not take, so we never falsely report a still-living
- process as evicted.
+ Graceful ``terminate()`` first, then ``kill()`` if it lingers, then — on
+ Windows — ``taskkill /F /T`` for the whole process tree (see
+ :func:`_kill_process_tree_win32`). Returns False (→ the caller keeps the
+ lock blocked, the SAFE outcome) when psutil is missing or nothing took, so
+ we never falsely report a still-living process as evicted. Every step is
+ logged: this runs before the window exists, and the log file is the only
+ place a user can later learn why a start took as long as it did.
"""
+ from loguru import logger
+
try:
import psutil # type: ignore[import-not-found]
except Exception: # noqa: BLE001
# Without psutil we cannot prove the zombie died, and the docstring's
# contract is to keep the lock blocked rather than claim an eviction.
+ logger.warning("cannot evict lock holder pid={}: psutil is unavailable", pid)
return False
try:
proc = psutil.Process(int(pid))
@@ -1135,14 +923,24 @@ def _terminate_pid(pid: int) -> bool:
# terminate may race the process self-exiting; the final _pid_alive check is
# the authority on whether it is really gone.
with suppress(Exception):
+ logger.warning("evicting lock holder pid={}: terminate()", pid)
proc.terminate()
try:
proc.wait(timeout=5.0)
except Exception: # noqa: BLE001 — graceful timed out → hard kill
+ logger.warning("lock holder pid={} survived terminate(); kill()", pid)
with suppress(Exception):
proc.kill()
proc.wait(timeout=5.0)
- return not _pid_alive(pid)
+ if _pid_alive(pid):
+ logger.warning("lock holder pid={} survived kill(); ending its process tree", pid)
+ if _tree_kill(pid):
+ deadline = time.monotonic() + 5.0
+ while _pid_alive(pid) and time.monotonic() < deadline:
+ time.sleep(0.2)
+ gone = not _pid_alive(pid)
+ logger.warning("lock holder pid={} {}", pid, "is gone" if gone else "is STILL alive")
+ return gone
def acquire_single_instance_lock(
@@ -1200,9 +998,7 @@ def acquire_single_instance_lock(
meta = None
pid = int(meta["pid"]) if meta and "pid" in meta else None
- port = (
- int(meta["port"]) if meta and "port" in meta and meta["port"] else None
- )
+ port = int(meta["port"]) if meta and "port" in meta and meta["port"] else None
if pid is not None and _pid_alive(pid):
# Holder is alive. Is it actually FUNCTIONAL (serving its port)? A
# healthy instance, the own pid, or a holder with no recorded port is
@@ -1366,9 +1162,7 @@ def _make_conductor_announcer(
except Exception as exc: # noqa: BLE001 — news is never worth a crash
from loguru import logger as _clog
- _clog.opt(exception=exc).warning(
- "Conductor announcement could not be published"
- )
+ _clog.opt(exception=exc).warning("Conductor announcement could not be published")
return _on_conductor_event
@@ -1378,7 +1172,6 @@ def _make_conductor_announcer(
# ---------------------------------------------------------------------------
-
class _HealthProbeLogFilter(logging.Filter):
"""Drop httpx's INFO line for the loopback ``/api/health`` probe.
@@ -1409,6 +1202,7 @@ def _silence_health_probe_log() -> None:
_health_probe_log_filter = _HealthProbeLogFilter()
logging.getLogger("httpx").addFilter(_health_probe_log_filter)
+
class DesktopApp:
"""Orchestrates the pywebview window + backend thread.
@@ -1640,9 +1434,7 @@ class DesktopApp:
vite_dev_url=(self.cfg.ui.vite_dev_url if self.cfg.ui.dev_mode else None),
)
try:
- loop.run_until_complete(
- bootstrap.serve("127.0.0.1", self.cfg.ui.admin_api_port)
- )
+ loop.run_until_complete(bootstrap.serve("127.0.0.1", self.cfg.ui.admin_api_port))
except Exception as exc:
self._backend_boot_error = exc
raise
@@ -1653,6 +1445,7 @@ class DesktopApp:
exc = context.get("exception")
msg = context.get("message", "<no message>")
from loguru import logger as _logger
+
if exc is not None:
_logger.opt(exception=exc).error("Unhandled asyncio exception: {}", msg)
else:
@@ -1676,9 +1469,7 @@ class DesktopApp:
from loguru import logger as _boot_logger
if shell_painted:
- _boot_logger.info(
- "Desktop boot shell painted; heavy initialization released."
- )
+ _boot_logger.info("Desktop boot shell painted; heavy initialization released.")
else:
_boot_logger.warning(
"Desktop boot shell paint was not acknowledged within 12s; "
@@ -1771,17 +1562,18 @@ class DesktopApp:
lat_cfg = getattr(self.cfg, "latency", None)
if lat_cfg is not None and getattr(lat_cfg, "log_jsonl", False):
from jarvis.telemetry.latency_log import LatencyLogWriter
- log_path = Path(
- getattr(lat_cfg, "log_path", "state/latency_log.jsonl")
- )
+
+ log_path = Path(getattr(lat_cfg, "log_path", "state/latency_log.jsonl"))
if not log_path.is_absolute():
log_path = Path.cwd() / log_path
self._latency_log_writer = LatencyLogWriter(log_path)
self._latency_log_writer.attach(server.bus)
from loguru import logger as _llog
+
_llog.info("Latency log JSONL writer attached: {}", log_path)
except Exception as exc: # noqa: BLE001 — telemetry never breaks boot
from loguru import logger as _llog
+
_llog.opt(exception=exc).warning(
"Latency log writer init failed — continuing without JSONL log.",
)
@@ -1806,6 +1598,7 @@ class DesktopApp:
apply_frontier_resolution(self.cfg, resolver, server.bus),
)
from loguru import logger as _flog
+
if switches:
_flog.info(
"Frontier autoswitch: {} model(s) raised to frontier.",
@@ -1815,6 +1608,7 @@ class DesktopApp:
_flog.info("Frontier autoswitch: TOML already frontier-compliant.")
except Exception as exc: # noqa: BLE001 — a resolver failure must not stop boot.
from loguru import logger as _flog
+
_flog.opt(exception=exc).warning(
"Frontier autoswitch failed — keeping TOML defaults.",
)
@@ -1826,8 +1620,8 @@ class DesktopApp:
# _focus_handler fetches the value dynamically.
server.app.state.shell = None
server.app.state.desktop_app = self
- server.app.state.realtime_transport_broker_token = (
- getattr(self, "realtime_transport_broker_token", "")
+ server.app.state.realtime_transport_broker_token = getattr(
+ self, "realtime_transport_broker_token", ""
)
# Local desktop run: the user IS at this machine, so reveal/open-with-
# default-app target their own desktop. Enable the native file actions.
@@ -1849,21 +1643,23 @@ class DesktopApp:
cancel_fn=mission_manager.jarvis_agent_cancel,
)
from loguru import logger as _bootlog
+
_bootlog.info(
"Wave-4 Y bootstrap: Brain.set_mission_command_handlers "
"wired up (status/cancel via MissionManager)."
)
except AttributeError:
from loguru import logger as _bootlog
+
_bootlog.warning(
"MissionManager is missing jarvis_agent_status/-_cancel — status/"
"cancel voice patterns fall back to the normal spawn path."
)
except Exception as exc: # noqa: BLE001
from loguru import logger as _bootlog
+
_bootlog.opt(exception=exc).warning(
- "Wave-4 Y bootstrap failed — "
- "status/cancel handlers stay unwired."
+ "Wave-4 Y bootstrap failed — status/cancel handlers stay unwired."
)
try:
@@ -1873,6 +1669,7 @@ class DesktopApp:
attach_brain(brain)
except Exception as exc: # noqa: BLE001
from loguru import logger as _bootlog
+
_bootlog.opt(exception=exc).warning(
"WorkflowRunner.attach_brain failed after the deferred build."
)
@@ -1887,18 +1684,18 @@ class DesktopApp:
task_runner._brain = brain
except Exception as exc: # noqa: BLE001
from loguru import logger as _bootlog
+
_bootlog.opt(exception=exc).warning(
"TaskRunner brain wiring failed after the deferred build."
)
async def _build_brain_bg() -> None:
try:
- built = await asyncio.to_thread(
- build_default_brain, bus=server.bus, tier="router"
- )
+ built = await asyncio.to_thread(build_default_brain, bus=server.bus, tier="router")
brain_holder["brain"] = built
server.app.state.brain = built
from loguru import logger
+
logger.info(
"Text-chat brain: {} active (shared with the voice pipeline).",
getattr(built, "active_provider", "unknown"),
@@ -1908,17 +1705,17 @@ class DesktopApp:
awareness_manager = getattr(built, "_awareness_manager", None)
if awareness_manager is not None:
from loguru import logger as _aw_logger
+
try:
await awareness_manager.start()
_aw_logger.info(
"AwarenessManager started — StoryTracker is now listening on bus."
)
except Exception as exc: # noqa: BLE001
- _aw_logger.opt(exception=exc).warning(
- "AwarenessManager.start() failed."
- )
+ _aw_logger.opt(exception=exc).warning("AwarenessManager.start() failed.")
except Exception as exc: # noqa: BLE001
from loguru import logger
+
brain_holder["error"] = f"{type(exc).__name__}: {exc}"
logger.opt(exception=exc).error(
"BrainManager build failed — chat replies with a setup hint."
@@ -1948,9 +1745,7 @@ class DesktopApp:
"""
def __init__(self) -> None:
- self._pending_skill_notes: list[
- tuple[tuple[Any, ...], dict[str, Any]]
- ] = []
+ self._pending_skill_notes: list[tuple[tuple[Any, ...], dict[str, Any]]] = []
@property
def active_provider(self) -> str:
@@ -1962,9 +1757,7 @@ class DesktopApp:
brain = brain_holder["brain"]
if brain is not None:
return str(getattr(brain, "reply_language", "auto"))
- return str(
- getattr(getattr(desktop_cfg, "brain", None), "reply_language", "auto")
- )
+ return str(getattr(getattr(desktop_cfg, "brain", None), "reply_language", "auto"))
@property
def conversation_language(self) -> str:
@@ -2087,6 +1880,7 @@ class DesktopApp:
publish_event=False,
)
from loguru import logger
+
brain = await _await_brain_ready()
# ------------------------------------------------------------------
@@ -2103,9 +1897,7 @@ class DesktopApp:
skill_ctx = try_get_skill_context()
if skill_ctx is not None and brain is not None:
matcher = TriggerMatcher(skill_ctx.registry)
- match_result = matcher.match_voice_with_match(
- evt.text, lang="auto"
- )
+ match_result = matcher.match_voice_with_match(evt.text, lang="auto")
if match_result is not None:
matched, regex_match = match_result
groups = regex_match.groups()
@@ -2119,8 +1911,8 @@ class DesktopApp:
if callable(note):
note(matched.name, content=content, source="chat")
logger.info(
- "Skill trigger matched (chat): '{}' — handed "
- "to the brain turn", matched.name,
+ "Skill trigger matched (chat): '{}' — handed to the brain turn",
+ matched.name,
)
except Exception as exc: # noqa: BLE001
# The pre-brain hook is defensive — a crash here must never
@@ -2225,9 +2017,7 @@ class DesktopApp:
loop,
)
else:
- reply = await _await_cancellable_chat_turn(
- brain(evt.text), loop
- )
+ reply = await _await_cancellable_chat_turn(brain(evt.text), loop)
except Exception as exc: # noqa: BLE001
detail = f"{type(exc).__name__}: {exc}"
message = f"Brain error: {detail}"
@@ -2275,11 +2065,7 @@ class DesktopApp:
await supervisor.set_state("SPEAKING")
if reply:
role = "system" if _is_brain_diagnostic(reply) else "assistant"
- trace = (
- turn_traces.snapshot(turn_started_ms)
- if role == "assistant"
- else None
- )
+ trace = turn_traces.snapshot(turn_started_ms) if role == "assistant" else None
await chat_store.add_message(
thread_id=thread_id, role=role, text=reply, trace=trace
)
@@ -2334,6 +2120,7 @@ class DesktopApp:
set_drop_handler(_on_overlay_drop)
except ModuleNotFoundError as exc:
from loguru import logger as _dlog
+
if exc.name == "overlay":
_dlog.debug(
"overlay drop handler wiring skipped: optional overlay package not installed"
@@ -2342,6 +2129,7 @@ class DesktopApp:
_dlog.opt(exception=exc).debug("overlay drop handler wiring skipped")
except Exception as exc: # noqa: BLE001 — drop wiring must never block boot.
from loguru import logger as _dlog
+
_dlog.opt(exception=exc).debug("overlay drop handler wiring skipped")
# Overlay right-click (bar OR mascot) → raise the main desktop window.
@@ -2366,8 +2154,8 @@ class DesktopApp:
workflow_runner = WorkflowRunner(
store=workflow_store,
bus=server.bus,
- brain=None, # set later via attach_brain
- tool_registry=None, # set later via attach_tools
+ brain=None, # set later via attach_brain
+ tool_registry=None, # set later via attach_tools
tool_executor=None,
)
# Harness + tools (audit 2026-08-17, AU-03): the runner shipped
@@ -2388,15 +2176,12 @@ class DesktopApp:
from jarvis.harness.manager import HarnessManager
from jarvis.workflows.runner import BrainToolSurface
- workflow_runner.attach_harness_manager(
- HarnessManager(bus=server.bus)
- )
- tool_surface = BrainToolSurface(
- lambda: getattr(server.app.state, "brain", None)
- )
+ workflow_runner.attach_harness_manager(HarnessManager(bus=server.bus))
+ tool_surface = BrainToolSurface(lambda: getattr(server.app.state, "brain", None))
workflow_runner.attach_tools(tool_surface, tool_surface)
except Exception as exc: # noqa: BLE001 — degrade, don't kill workflows.
from loguru import logger as _wf_logger
+
_wf_logger.opt(exception=exc).warning(
"Workflow runner has no harness/tools — harness_dispatch "
"and tool_call steps will fail; the rest still runs."
@@ -2413,6 +2198,7 @@ class DesktopApp:
self._workflow_scheduler = workflow_scheduler
except Exception as exc: # noqa: BLE001
from loguru import logger
+
logger.opt(exception=exc).warning(
"Workflow system could not start — the workflows view stays empty."
)
@@ -2502,7 +2288,7 @@ class DesktopApp:
from conductor import Runner as _CRunner
from conductor import Scheduler as _CSched
- conductor_store = _CStore() # ~/.conductor/conductor.sqlite
+ conductor_store = _CStore() # ~/.conductor/conductor.sqlite
# The bus reaches Conductor through its ``on_event`` seam, not the
# other way round (Conductor never imports ``jarvis``). Without
# this, every scheduled job ran into its own SQLite and the user
@@ -2523,6 +2309,7 @@ class DesktopApp:
self._conductor_scheduler = conductor_scheduler
except Exception as exc: # noqa: BLE001
from loguru import logger
+
logger.opt(exception=exc).warning(
"Conductor setup failed — the Conductor view stays empty."
)
@@ -2535,6 +2322,7 @@ class DesktopApp:
async def _bootstrap_conductor() -> None:
"""Conductor store init + seed jobs + scheduler start."""
from loguru import logger as _logger
+
store = server.app.state.conductor_store
scheduler = server.app.state.conductor_scheduler
if store is None:
@@ -2543,15 +2331,14 @@ class DesktopApp:
await store.init()
await store.cleanup_interrupted_runs()
from conductor import ensure_seed_jobs
+
added = await ensure_seed_jobs(store)
_logger.info("Conductor store ready ({} new seed job(s)).", added)
if scheduler is not None:
scheduler.start()
_logger.info("Conductor scheduler started.")
except Exception as exc: # noqa: BLE001
- _logger.opt(exception=exc).warning(
- "Conductor bootstrap failed"
- )
+ _logger.opt(exception=exc).warning("Conductor bootstrap failed")
async def _bootstrap_workflows() -> None:
"""Store init + seed workflows + scheduler start.
@@ -2571,9 +2358,9 @@ class DesktopApp:
await store.init()
await store.cleanup_interrupted_runs()
from jarvis.workflows import ensure_seed_workflows
+
added = await ensure_seed_workflows(store)
- _logger.info("Workflow store ready ({} new seed workflow(s)).",
- added)
+ _logger.info("Workflow store ready ({} new seed workflow(s)).", added)
# Harness manager + tool surface are attached above, at runner
# construction — ``tool_call`` and ``harness_dispatch`` steps
# run through the brain's own risk-tier-aware ToolExecutor.
@@ -2605,9 +2392,8 @@ class DesktopApp:
exc = task.exception()
if exc is not None:
from loguru import logger as _slog
- _slog.opt(exception=exc).error(
- "Voice/orb startup task crashed."
- )
+
+ _slog.opt(exception=exc).error("Voice/orb startup task crashed.")
# Wake-model GIL-priority gate: set by ``_start_speech_and_orb`` once
# the (light base/cpu) wake model has finished loading. The heavy
@@ -2670,11 +2456,10 @@ class DesktopApp:
# wiki) and background services land a few seconds later. Bounded
# so a stuck/absent wake load never blocks the backend forever.
try:
- await asyncio.wait_for(
- self._wake_model_loaded.wait(), timeout=12.0
- )
+ await asyncio.wait_for(self._wake_model_loaded.wait(), timeout=12.0)
except TimeoutError:
from loguru import logger as _slog
+
_slog.info(
"Heavy backend: wake-model gate timed out (12 s) — "
"starting the backend anyway."
@@ -2695,8 +2480,7 @@ class DesktopApp:
# Honest end-to-end anchor: the UI's data requests are now
# answered — spawn -> app usable, same clock as BOOT_READY.
print(
- "APP_INTERACTIVE_MS="
- f"{(time.perf_counter() - _bp_t0) * 1000.0:.1f}",
+ f"APP_INTERACTIVE_MS={(time.perf_counter() - _bp_t0) * 1000.0:.1f}",
flush=True,
)
# The bootstrap owns the bound port for the process lifetime;
@@ -2709,9 +2493,8 @@ class DesktopApp:
_db_mark("server_start")
except Exception as exc: # noqa: BLE001 — never kill the backend loop
from loguru import logger as _slog
- _slog.opt(exception=exc).error(
- "Heavy backend init (server.start) failed."
- )
+
+ _slog.opt(exception=exc).error("Heavy backend init (server.start) failed.")
loop.create_task(_build_brain_bg(), name="brain-build")
# MCP autostart as a fire-and-forget task — doesn't block backend-ready.
loop.create_task(_start_enabled_mcps())
@@ -2759,6 +2542,7 @@ class DesktopApp:
if plan.engine == "vosk_kws":
pipeline.set_wake_plan(plan)
from loguru import logger as _wmf_ok
+
_wmf_ok.info(
"Wake model for '{}' provisioned off-boot; "
"live-switched detector to vosk_kws.",
@@ -2766,9 +2550,8 @@ class DesktopApp:
)
except Exception: # noqa: BLE001 — a background probe never crashes boot
from loguru import logger as _wmf_log
- _wmf_log.opt(exception=True).debug(
- "Off-boot wake-model provision skipped."
- )
+
+ _wmf_log.opt(exception=True).debug("Off-boot wake-model provision skipped.")
loop.create_task(_provision_wake_model(), name="wake-model-provision")
@@ -2882,9 +2665,7 @@ class DesktopApp:
# Every read is a getattr: this runs on the way OUT, including out of a
# backend that never finished coming up, and a half-built instance must
# not turn a crash report into a second crash.
- if getattr(self, "_shutdown_done", False) or getattr(
- self, "_user_requested_quit", False
- ):
+ if getattr(self, "_shutdown_done", False) or getattr(self, "_user_requested_quit", False):
logger.debug("Backend loop ended as part of shutdown ({}).", reason)
return
@@ -2957,13 +2738,12 @@ class DesktopApp:
# Glide speed for ``glide_os_cursor`` (called by every click/move tool).
try:
from jarvis.control.cursor_motion import set_glide_ms
+
if cu is not None:
set_glide_ms(int(getattr(cu, "cursor_glide_ms", 0)))
except ModuleNotFoundError as exc:
if exc.name == "overlay":
- logger.debug(
- "set_glide_ms skipped: optional overlay package not installed"
- )
+ logger.debug("set_glide_ms skipped: optional overlay package not installed")
else:
logger.opt(exception=exc).debug("set_glide_ms failed")
except Exception as exc: # noqa: BLE001
@@ -2979,6 +2759,7 @@ class DesktopApp:
build_real_jarvis_cursor,
set_jarvis_system_cursor,
)
+
jcur = build_real_jarvis_cursor()
if jcur is not None:
set_jarvis_system_cursor(jcur)
@@ -2986,9 +2767,7 @@ class DesktopApp:
logger.info("Jarvis system cursor armed (swap on Computer-Use mission).")
except ModuleNotFoundError as exc:
if exc.name == "overlay":
- logger.debug(
- "Jarvis system cursor skipped: optional overlay package not installed"
- )
+ logger.debug("Jarvis system cursor skipped: optional overlay package not installed")
else:
logger.opt(exception=exc).warning("Jarvis system cursor not startable")
self._jarvis_cursor = None
@@ -3001,6 +2780,7 @@ class DesktopApp:
return
try:
from ui.orb.virtual_cursor_window import TkVirtualCursor
+
cursor = TkVirtualCursor()
if cursor.start():
self._virtual_cursor = cursor
@@ -3011,9 +2791,7 @@ class DesktopApp:
logger.opt(exception=exc).warning("Virtual mouse overlay not startable")
self._virtual_cursor = None
- def _build_overlay_surface(
- self, style: str, *, gate_until_voice_ready: bool = False
- ):
+ def _build_overlay_surface(self, style: str, *, gate_until_voice_ready: bool = False):
"""Construct (and start) the overlay surface for a display style.
Returns a ``NullOverlay`` for ``"none"`` (no Tk window, no-op surface),
@@ -3055,14 +2833,12 @@ class DesktopApp:
)
surface.start_in_thread()
logger.info(
- "JarvisBar hosted out-of-process on macOS "
- "(jarvis.ui.jarvisbar.host)."
+ "JarvisBar hosted out-of-process on macOS (jarvis.ui.jarvisbar.host)."
)
return surface
except Exception: # noqa: BLE001 — cosmetic; never block boot
logger.opt(exception=True).warning(
- "macOS JarvisBar host failed to start — falling back "
- "to the no-op surface."
+ "macOS JarvisBar host failed to start — falling back to the no-op surface."
)
elif style != "none":
try:
@@ -3083,8 +2859,7 @@ class DesktopApp:
return surface
except Exception: # noqa: BLE001 — cosmetic; never block boot
logger.opt(exception=True).warning(
- "macOS orb host failed to start — falling back "
- "to the no-op surface."
+ "macOS orb host failed to start — falling back to the no-op surface."
)
from jarvis.ui.jarvisbar import NullOverlay
@@ -3103,9 +2878,7 @@ class DesktopApp:
accent=self.cfg.ui.bar_accent,
startup_gated=gate_until_voice_ready,
size_scale=getattr(self.cfg.ui, "bar_size_scale", 1.0),
- follow_cursor_monitor=getattr(
- self.cfg.ui, "bar_follow_cursor_monitor", True
- ),
+ follow_cursor_monitor=getattr(self.cfg.ui, "bar_follow_cursor_monitor", True),
)
else: # an orb style ("mascot" / "voice_orb"), or any legacy value
from ui.orb.overlay import OrbOverlay
@@ -3431,9 +3204,7 @@ class DesktopApp:
destroy_window=self._destroy_all_windows,
)
- threading.Thread(
- target=_quit_soon, name="jarvis-restart-quit", daemon=True
- ).start()
+ threading.Thread(target=_quit_soon, name="jarvis-restart-quit", daemon=True).start()
logger.info(
"Self-restart scheduled (relauncher spawned{}; quitting in ~0.2 s, "
"independent hard-exit watchdog at ~0.9 s).",
@@ -3468,9 +3239,7 @@ class DesktopApp:
destroy_window=self._destroy_all_windows,
)
- threading.Thread(
- target=_quit_soon, name="jarvis-decline-quit", daemon=True
- ).start()
+ threading.Thread(target=_quit_soon, name="jarvis-decline-quit", daemon=True).start()
logger.info("Quit scheduled (Terms declined; hard-exit fallback armed).")
return True
@@ -3554,9 +3323,7 @@ class DesktopApp:
)
try:
- await asyncio.wait_for(
- voice_usable.wait(), timeout=_REALTIME_WARM_VOICE_GATE_S
- )
+ await asyncio.wait_for(voice_usable.wait(), timeout=_REALTIME_WARM_VOICE_GATE_S)
except TimeoutError:
# No microphone, a failed warm-up, or a headless host: the browser
# and bridge surfaces can still start a call, so warm anyway.
@@ -3592,8 +3359,7 @@ class DesktopApp:
raise
except Exception: # noqa: BLE001 — warming is advisory, never fatal
_warm_log_exc.warning(
- "Realtime transport warm failed; the next call pays the "
- "provider's cold start."
+ "Realtime transport warm failed; the next call pays the provider's cold start."
)
last_completed = time.monotonic()
@@ -3620,6 +3386,7 @@ class DesktopApp:
"""
if os.environ.get("JARVIS_VOICE", "").strip().lower() in ("0", "off", "false"):
from loguru import logger
+
logger.info("Voice stack disabled via JARVIS_VOICE=0.")
return
@@ -3668,7 +3435,8 @@ class DesktopApp:
logger.info(
"On-screen overlay stays with the default app — the {} instance "
"draws none (configured style={}).",
- current_instance().label, configured_style,
+ current_instance().label,
+ configured_style,
)
if not overlay_ok:
@@ -3693,9 +3461,7 @@ class DesktopApp:
gate_until_voice_ready=(orb_style == "jarvis_bar"),
)
hide_on_idle = (
- (not self.cfg.ui.bar_persistent)
- if orb_style == "jarvis_bar"
- else True
+ (not self.cfg.ui.bar_persistent) if orb_style == "jarvis_bar" else True
)
bridge = OrbBusBridge(bus=bus, orb=surface, hide_on_idle=hide_on_idle)
bridge.attach()
@@ -3706,10 +3472,13 @@ class DesktopApp:
self._surfaces = {orb_style: surface}
logger.info(
"On-screen overlay active: style={} (persistent={}, accent={}).",
- orb_style, self.cfg.ui.bar_persistent, self.cfg.ui.bar_accent,
+ orb_style,
+ self.cfg.ui.bar_persistent,
+ self.cfg.ui.bar_accent,
)
except Exception as exc: # noqa: BLE001
from loguru import logger
+
logger.opt(exception=exc).warning("On-screen overlay failed to start")
self._orb = None
self._bridge = None
@@ -3724,6 +3493,7 @@ class DesktopApp:
self._ducker.attach()
except Exception as exc: # noqa: BLE001
from loguru import logger
+
logger.opt(exception=exc).warning("Audio ducking not started")
self._ducker = None
@@ -3782,16 +3552,17 @@ class DesktopApp:
tool_registry=skill_tool_registry,
bus=bus,
)
- set_skill_context(
- SkillContext(registry=skill_registry, runner=skill_runner)
- )
+ set_skill_context(SkillContext(registry=skill_registry, runner=skill_runner))
from loguru import logger
+
logger.info(
"SkillContext active ({} skill(s) loaded from {}).",
- len(skill_registry.list()), skills_root,
+ len(skill_registry.list()),
+ skills_root,
)
except Exception as exc: # noqa: BLE001
from loguru import logger
+
logger.opt(exception=exc).warning(
"SkillContext setup failed — the pipeline runs without the skill hook."
)
@@ -3808,9 +3579,7 @@ class DesktopApp:
from jarvis.speech.pipeline import SpeechPipeline
stt_language = (
- self.cfg.stt.language
- if self.cfg.stt.language not in ("", "auto")
- else None
+ self.cfg.stt.language if self.cfg.stt.language not in ("", "auto") else None
)
# Resolve the user's custom wake word (jarvis.toml [trigger.wake_word])
# into a concrete plan. Whether a local Whisper engine is importable
@@ -3836,6 +3605,7 @@ class DesktopApp:
language=_wake_language,
)
from loguru import logger as _wlog
+
if not wake_plan.wake_available:
_wlog.info(
"Wake-word OFF: no local model for {!r} — the Call shortcut "
@@ -3884,6 +3654,7 @@ class DesktopApp:
# wake-model load first (no-op for headless / voice-off).
from jarvis.core import runtime_refs as _rr_wake
from jarvis.plugins.stt import build_wake_whisper
+
_rr_wake.signal_wake_model_expected()
# Seed the wake Whisper's prompt with the custom phrase ONLY on
@@ -3891,9 +3662,7 @@ class DesktopApp:
# heavy_local_whisper backstop for "Hey Jarvis" keeps its OWW
# model as the discriminator and passes no prompt, so the hot-
# path prompt-hallucination caveat never applies to it.
- wake_phrase = (
- wake_plan.phrase if wake_plan.needs_local_whisper else None
- )
+ wake_phrase = wake_plan.phrase if wake_plan.needs_local_whisper else None
# Build the local wake-Whisper OFF the event loop: it probes CUDA
# availability, whose first-call context init JIT-compiles for
# ~30-60 s on a Blackwell GPU (cold cache). Offloading keeps the
@@ -3932,6 +3701,7 @@ class DesktopApp:
from loguru import logger
from jarvis.brain.factory import build_default_brain as _bdb
+
logger.info(
"Shared brain is not directly callable — building a dedicated voice brain."
)
@@ -3950,6 +3720,7 @@ class DesktopApp:
# [ack_brain].enabled = true in jarvis.toml, otherwise returns
# None. Threaded into the pipeline below.
from jarvis.brain.factory import build_ack_brain as _bab
+
voice_ack_brain = _bab(self.cfg)
_t_ack = time.perf_counter()
# Join the wake-model build started above (it loaded in its worker
@@ -4049,14 +3820,10 @@ class DesktopApp:
enable_whisper_wake=(
self.cfg.trigger.wake_word_enabled
and wake_plan.wake_available
- and (
- wake_plan.engine == "stt_match"
- or self.cfg.trigger.heavy_local_whisper
- )
+ and (wake_plan.engine == "stt_match" or self.cfg.trigger.heavy_local_whisper)
),
enable_local_whisper=(
- self.cfg.trigger.heavy_local_whisper
- or wake_plan.needs_local_whisper
+ self.cfg.trigger.heavy_local_whisper or wake_plan.needs_local_whisper
),
# Strict "Hey"-prefix verification for OpenWakeWord hits. With
# this flag on (default in cfg.trigger.require_hey_prefix), an
@@ -4071,9 +3838,7 @@ class DesktopApp:
# Shipped default since 2026-07-18 is conversation mode
# (single_turn_mode = false); flipping the TOML entry opts
# back into one turn per wake (the retired 2026-05-18 mandate).
- continue_listening_after_response=(
- not self.cfg.trigger.single_turn_mode
- ),
+ continue_listening_after_response=(not self.cfg.trigger.single_turn_mode),
# Conversation-mode idle auto-hangup. ``session_idle_timeout_s``
# <= 0 keeps the session active until a manual hangup (user
# mandate). The constructor default (30 s) is the safe baseline.
@@ -4098,9 +3863,7 @@ class DesktopApp:
# adding work to the normal boot path.
latest_wake_plan = resolve_wake_plan(
self.cfg.trigger.wake_word,
- local_whisper_available=(
- _ilu.find_spec("faster_whisper") is not None
- ),
+ local_whisper_available=(_ilu.find_spec("faster_whisper") is not None),
language=resolve_wake_language(self.cfg),
)
plan_signature = (
@@ -4126,6 +3889,7 @@ class DesktopApp:
# build step costs what (the wake-Whisper CUDA probe was the hidden
# ~60 s "VOICE STARTING…" stall before the persisted probe cache).
from loguru import logger as _vlog
+
# Stamps now reflect the OVERLAPPED build: TTS + ack-brain run on the
# loop thread while the wake model loads in its worker thread, so
# ``wake_join`` is only the wake time NOT already hidden behind them
@@ -4157,9 +3921,7 @@ class DesktopApp:
runtime_refs.set_speech_pipeline(pipeline)
except Exception: # noqa: BLE001, S110 - best-effort, never block voice boot
pass
- self._pipeline_task = loop.create_task(
- pipeline.run(), name="speech-pipeline"
- )
+ self._pipeline_task = loop.create_task(pipeline.run(), name="speech-pipeline")
from loguru import logger
def _on_pipeline_done(task: asyncio.Task) -> None:
@@ -4206,8 +3968,7 @@ class DesktopApp:
if evt.voice_usable and not _usable_printed[0]:
_usable_printed[0] = True
print(
- "VOICE_USABLE_MS="
- f"{(time.perf_counter() - self._bp_t0) * 1000.0:.1f}",
+ f"VOICE_USABLE_MS={(time.perf_counter() - self._bp_t0) * 1000.0:.1f}",
flush=True,
)
@@ -4225,9 +3986,7 @@ class DesktopApp:
# those loads were still running and the heavy backend storm
# stretched a few-second load to 30+ s per model (live
# forensic 2026-07-17: en 34 s + de 24 s, voice ready at ~2 min).
- detector_warm = getattr(
- getattr(pipeline, "_wake", None), "is_warm", None
- )
+ detector_warm = getattr(getattr(pipeline, "_wake", None), "is_warm", None)
if detector_warm is not None:
return bool(detector_warm)
ww = getattr(pipeline, "_whisper_wake", None)
@@ -4244,10 +4003,12 @@ class DesktopApp:
return getattr(base_stt, "_model", None) is not None
from jarvis.core import runtime_refs as _rr_ready
+
if stt is None:
self._wake_model_loaded.set()
_rr_ready.signal_wake_model_ready()
else:
+
async def _signal_wake_model_loaded() -> None:
for _ in range(40): # ~20 s cap, then release the gate anyway
if _wake_model_is_loaded():
@@ -4257,9 +4018,7 @@ class DesktopApp:
# Release the boot-storm housekeeping gate too.
_rr_ready.signal_wake_model_ready()
- loop.create_task(
- _signal_wake_model_loaded(), name="wake-model-ready-signal"
- )
+ loop.create_task(_signal_wake_model_loaded(), name="wake-model-ready-signal")
# Progressive wake model: the pipeline is now live on the LIGHT
# base/cpu wake model (hear-ready fast, no CUDA JIT). Hot-swap the
@@ -4293,9 +4052,7 @@ class DesktopApp:
# measured 2026-06-27). Wake is already live on base while
# we wait, so this costs nothing user-visible.
try:
- await asyncio.wait_for(
- self._wake_model_loaded.wait(), timeout=120.0
- )
+ await asyncio.wait_for(self._wake_model_loaded.wait(), timeout=120.0)
except TimeoutError:
# The gate is an optimisation, not a precondition:
# after two minutes the upgrade proceeds anyway
@@ -4322,9 +4079,7 @@ class DesktopApp:
# for any STT without the prime hook.
_turbo_prime = getattr(turbo, "warm_up", None)
await asyncio.to_thread(
- _turbo_prime
- if callable(_turbo_prime)
- else turbo._ensure_model
+ _turbo_prime if callable(_turbo_prime) else turbo._ensure_model
)
if ww is not None:
# Runtime backstop: keep the proven base/cpu model
@@ -4353,6 +4108,7 @@ class DesktopApp:
)
except Exception as exc: # noqa: BLE001
from loguru import logger
+
# FAIL-LOUD (2026-05-28 "Hey Jarvis silently dead" incident): a
# fatal speech-pipeline init crash used to degrade to a SILENT
# warning, so voice went dead with no signal at all. Degrading is
@@ -4366,16 +4122,13 @@ class DesktopApp:
)
try:
from jarvis.audio.alerts import play_voice_offline_alert
+
loop.create_task(
- play_voice_offline_alert(
- self.cfg.audio.output_device or None
- ),
+ play_voice_offline_alert(self.cfg.audio.output_device or None),
name="voice-offline-alert",
)
except Exception: # noqa: BLE001 — the alert must never crash boot
- logger.debug(
- "could not schedule voice-offline alert", exc_info=True
- )
+ logger.debug("could not schedule voice-offline alert", exc_info=True)
# UI un-stick (permanent "Getting ready to listen" bug): the frontend's
# startup banner + top-left "STARTING…" status clear ONLY when a
# VoiceBootStatus(ready=True) is published. A crash in pipeline
@@ -4388,15 +4141,14 @@ class DesktopApp:
# WebServer's voice-ready watchdog for the warm-up-hang case.)
try:
from jarvis.core.events import VoiceBootStatus as _VBS
+
if bus is not None:
loop.create_task(
bus.publish(_VBS(ready=True, detail="voice_unavailable")),
name="voice-offline-ready-signal",
)
except Exception: # noqa: BLE001 — the un-stick must never crash boot
- logger.debug(
- "could not publish voice-unavailable ready signal", exc_info=True
- )
+ logger.debug("could not publish voice-unavailable ready signal", exc_info=True)
def _install_focus_route(self, server: WebServer) -> None:
"""Replaces the placeholder ``/api/window/focus`` with a real call.
@@ -4410,9 +4162,7 @@ class DesktopApp:
# FastAPI.routes is a property without a setter — filter in place
# instead of reassigning. app.router.routes is the underlying list.
app.router.routes[:] = [
- r
- for r in app.router.routes
- if not (getattr(r, "path", None) == "/api/window/focus")
+ r for r in app.router.routes if not (getattr(r, "path", None) == "/api/window/focus")
]
@app.post("/api/window/focus", include_in_schema=False)
@@ -4531,9 +4281,7 @@ class DesktopApp:
# frontend falls back to a real browser tab.
from loguru import logger
- logger.opt(exception=exc).warning(
- "Detached window for '{}' could not be created", view
- )
+ logger.opt(exception=exc).warning("Detached window for '{}' could not be created", view)
return {
"ok": False,
"reason": f"{type(exc).__name__}: {exc}",
@@ -4858,14 +4606,8 @@ class DesktopApp:
f" {python} -m pip install -r requirements.txt"
)
if exc is not None:
- return (
- f"{WINDOW_TITLE} cannot start: {type(exc).__name__}: {exc}\n\n"
- f"Python: {python}"
- )
- return (
- f"{WINDOW_TITLE} cannot start: the backend did not become ready.\n\n"
- f"Python: {python}"
- )
+ return f"{WINDOW_TITLE} cannot start: {type(exc).__name__}: {exc}\n\nPython: {python}"
+ return f"{WINDOW_TITLE} cannot start: the backend did not become ready.\n\nPython: {python}"
def _wait_for_backend(self, timeout_s: float = 45.0) -> bool:
"""Polls ``/api/health`` until it returns 200 or the timeout expires.
@@ -5086,7 +4828,7 @@ class DesktopApp:
remedy=(
" - Or install the desktop dependencies into the "
"interpreter that is running the app:\n"
- f" {sys.executable} -m pip install -e \".[desktop]\"\n"
+ f' {sys.executable} -m pip install -e ".[desktop]"\n'
),
)
@@ -5244,9 +4986,7 @@ class DesktopApp:
os._exit(code)
return code
- def _degrade_to_browser_ui(
- self, exc: BaseException, *, remedy: str | None = None
- ) -> int:
+ def _degrade_to_browser_ui(self, exc: BaseException, *, remedy: str | None = None) -> int:
"""Fallback when no native desktop-window backend is available.
Reached when pywebview cannot open a native window: a Linux host missing
@@ -5361,9 +5101,7 @@ class DesktopApp:
if ok:
if first_set_at is None:
first_set_at = time.monotonic()
- logger.debug(
- "Taskbar icon set; re-arming against WebView2 override."
- )
+ logger.debug("Taskbar icon set; re-arming against WebView2 override.")
# Late shortcut self-heal: the import-time ensure runs
# before anything else — if the Start-Menu shortcut got
# deleted after that (an uninstall/reinstall race, a
@@ -5391,9 +5129,7 @@ class DesktopApp:
WINDOW_TITLE,
)
- threading.Thread(
- target=_poll, name="jarvis-icon-setter", daemon=True
- ).start()
+ threading.Thread(target=_poll, name="jarvis-icon-setter", daemon=True).start()
# ---- Tray -----------------------------------------------------------------
@@ -5414,7 +5150,6 @@ class DesktopApp:
self._tray = tray
def _bridge_loop() -> None:
- import queue
cmd_queue = tray._command_queue # noqa: SLF001
while not self._shutdown_done:
@@ -5446,9 +5181,7 @@ class DesktopApp:
self._destroy_all_windows()
return
- threading.Thread(
- target=_bridge_loop, name="jarvis-tray-bridge", daemon=True
- ).start()
+ threading.Thread(target=_bridge_loop, name="jarvis-tray-bridge", daemon=True).start()
def _publish_kill_requested_threadsafe(self) -> None:
"""Publish ``KillRequested(source="tray")`` from a non-async thread.
@@ -5470,9 +5203,7 @@ class DesktopApp:
try:
from jarvis.core.events import KillRequested # noqa: PLC0415
- asyncio.run_coroutine_threadsafe(
- bus.publish(KillRequested(source="tray")), loop
- )
+ asyncio.run_coroutine_threadsafe(bus.publish(KillRequested(source="tray")), loop)
logger.info("Emergency stop: KillRequested(source=tray) published.")
except Exception: # noqa: BLE001 — the tray must survive a failed kill
logger.opt(exception=True).warning("Emergency stop publish failed.")
@@ -5613,9 +5344,7 @@ class DesktopApp:
except Exception: # noqa: BLE001
from loguru import logger
- logger.debug(
- "overlay suppress for hidden window failed", exc_info=True
- )
+ logger.debug("overlay suppress for hidden window failed", exc_info=True)
def _restore_overlay_for_visible_window(self) -> None:
"""Reverse of :meth:`_suppress_overlay_for_hidden_window`.
@@ -5645,9 +5374,7 @@ class DesktopApp:
except Exception: # noqa: BLE001
from loguru import logger
- logger.debug(
- "overlay restore for visible window failed", exc_info=True
- )
+ logger.debug("overlay restore for visible window failed", exc_info=True)
# ---- Shutdown ----------------------------------------------------------
@@ -5669,9 +5396,7 @@ class DesktopApp:
time.sleep(after_s)
os._exit(0)
- threading.Thread(
- target=_kill, name="jarvis-force-exit", daemon=True
- ).start()
+ threading.Thread(target=_kill, name="jarvis-force-exit", daemon=True).start()
def shutdown(self) -> int:
"""Idempotent. Stops the server + backend loop, cleans the meta file."""
@@ -5738,6 +5463,7 @@ class DesktopApp:
self._virtual_cursor.shutdown()
except Exception as exc: # noqa: BLE001
from loguru import logger as _logger
+
_logger.opt(exception=exc).warning(
"Virtual-cursor shutdown raised; overlay HWND may persist."
)
@@ -5752,11 +5478,13 @@ class DesktopApp:
self._jarvis_cursor.shutdown()
except Exception as exc: # noqa: BLE001
from loguru import logger as _logger
+
_logger.opt(exception=exc).warning(
"Jarvis system-cursor shutdown raised; cursor may stay swapped."
)
try:
from jarvis.overlay.system_cursor import set_jarvis_system_cursor
+
set_jarvis_system_cursor(None)
except Exception: # noqa: BLE001, S110
# The shutdown() above already logged if the cursor is stuck;
@@ -5814,6 +5542,7 @@ class DesktopApp:
cd_scheduler = getattr(self, "_conductor_scheduler", None)
cd_store = getattr(self, "_conductor_store", None)
if any(x is not None for x in (wf_scheduler, wf_store, cd_scheduler, cd_store)):
+
async def _workflow_cleanup() -> None:
for sched in (wf_scheduler, cd_scheduler):
try:
@@ -5831,14 +5560,17 @@ class DesktopApp:
# Same: the process is exiting, so an unclosed
# store handle is released by the OS anyway.
pass
+
try:
asyncio.run_coroutine_threadsafe(
- _workflow_cleanup(), loop,
+ _workflow_cleanup(),
+ loop,
).result(timeout=2.0)
except Exception: # noqa: BLE001, S110
# Bounded by the 2 s timeout on purpose: a wedged cleanup
# must not hold the whole quit open.
pass
+
# Cleanly close PTY sessions — otherwise zombies remain
async def _pty_cleanup() -> None:
try:
@@ -5849,7 +5581,9 @@ class DesktopApp:
pty.close_all()
except Exception as exc: # noqa: BLE001
from loguru import logger as _logger
+
_logger.warning("PTY-Cleanup failed: {}", exc)
+
try:
asyncio.run_coroutine_threadsafe(_pty_cleanup(), loop).result(timeout=2.0)
except Exception: # noqa: BLE001, S110
@@ -5859,9 +5593,9 @@ class DesktopApp:
# Stop the serve-first bootstrap (it owns the listening socket).
if self._bootstrap is not None:
try:
- asyncio.run_coroutine_threadsafe(
- self._bootstrap.stop(), loop
- ).result(timeout=3.0)
+ asyncio.run_coroutine_threadsafe(self._bootstrap.stop(), loop).result(
+ timeout=3.0
+ )
except Exception: # noqa: BLE001, S110
# Timed out or already down; the loop stop below is the
# backstop that frees the socket either way.
diff --git a/jarvis/ui/desktop_log.py b/jarvis/ui/desktop_log.py
new file mode 100644
index 0000000000..4f2cf73b9c
--- /dev/null
+++ b/jarvis/ui/desktop_log.py
@@ -0,0 +1,319 @@
+"""The desktop app's file log — installed before anything else can go wrong.
+
+Why a module of its own: ``jarvis.ui.desktop_app`` imports the full config
+graph (~400 ms) and is only reached AFTER the launcher has decided whether
+this process may start at all. Everything before that decision — the branded
+re-exec, the single-instance lock, an "already running" bounce, a crash on an
+import — used to happen under ``pythonw.exe`` with no console and no log file,
+so a launch that never produced a window left no trace anywhere (live
+incident 2026-08-25: a start at 10:02:41 rewrote the autostart entry and then
+vanished; the window came up on a later click at 10:09:16, and nothing in
+between was recorded). The launcher installs this sink from its first
+millisecond, and it must stay cheap: loguru + stdlib + the instance resolver.
+
+``desktop_log_path()`` mirrors ``jarvis.core.config.DATA_DIR`` exactly (env
+override, then the instance-named directory beside the checkout) — the two MUST
+agree, because the sink is installed once per process and every later
+``_install_desktop_log_sink(DATA_DIR / ...)`` call is a no-op against it.
+"""
+
+from __future__ import annotations
+
+import os
+import queue
+import sys
+import threading
+import time
+from contextlib import suppress
+from pathlib import Path
+from typing import Any
+
+#: The file the desktop app logs to, relative to its data directory.
+DESKTOP_LOG_FILE_NAME = "jarvis_desktop.log"
+
+
+def desktop_log_path() -> Path:
+ """``<data dir>/jarvis_desktop.log`` without importing ``jarvis.core.config``.
+
+ Same resolution as ``jarvis.core.config._resolve_data_dir``: ``JARVIS_DATA_DIR``
+ wins outright, otherwise ``<checkout>/<instance data dir name>``. A parity
+ test pins the two together.
+ """
+ env_dir = os.environ.get("JARVIS_DATA_DIR")
+ if env_dir and env_dir.strip():
+ return Path(env_dir.strip()) / DESKTOP_LOG_FILE_NAME
+ from jarvis.core.instance import current_instance
+
+ project_root = Path(__file__).resolve().parents[2]
+ return project_root / current_instance().data_dir_name / DESKTOP_LOG_FILE_NAME
+
+
+_DESKTOP_LOG_SINK_INSTALLED = False
+#: Rotate the desktop log at this size, keeping ``_LOG_RETENTION`` older files.
+_LOG_ROTATION_BYTES = 10 * 1024 * 1024
+_LOG_RETENTION = 3
+#: Bound on records waiting to reach disk. Deep enough to absorb a multi-second
+#: disk stall at the observed rate (a few hundred records per minute), shallow
+#: enough that a permanently wedged disk cannot grow the process without limit.
+_LOG_QUEUE_MAX = 20_000
+#: Records fused into one write+flush pair. Chatty subsystems (the wake
+#: heartbeat, Telegram polling) otherwise pay two syscalls per line.
+_LOG_BATCH_MAX = 256
+#: Sentinel telling the writer thread to drain and exit.
+_LOG_STOP = object()
+
+
+class _AsyncLogWriter:
+ """Write loguru records to the log file from a dedicated thread.
+
+ Why this exists instead of loguru's own file sink: a file sink runs
+ INLINE on whichever thread emitted the record, and the backend asyncio
+ loop emits constantly. One slow ``write()`` therefore blocks every
+ WebSocket, HTTP route and brain turn behind it. Observed live on
+ 2026-08-03 under heavy machine load: a 24.4 s event-loop stall whose
+ stack ended in ``loguru/_file_sink.py:write``, immediately followed by
+ ``listening socket ... is dead`` and an unhandled proactor error — the
+ window lost its backend and never came back.
+
+ loguru's ``enqueue=True`` would decouple the same way, but it builds a
+ multiprocessing pipe that can fail with WinError 5 in restricted
+ desktop/sandbox contexts before the window exists. A plain threading
+ queue buys the decoupling without a pipe; rotation and retention, which
+ the file sink would otherwise provide, are carried here instead.
+
+ On overflow records are dropped rather than growing the queue without
+ bound, and the dropped count is reported into the log as soon as the disk
+ keeps up again — dropping silently would make the log lie about its own
+ completeness (AP-30).
+ """
+
+ def __init__(
+ self,
+ path: Path,
+ *,
+ rotation_bytes: int = _LOG_ROTATION_BYTES,
+ retention: int = _LOG_RETENTION,
+ max_queue: int = _LOG_QUEUE_MAX,
+ ) -> None:
+ self._path = Path(path)
+ self._rotation_bytes = rotation_bytes
+ self._retention = retention
+ self._queue: queue.Queue[Any] = queue.Queue(maxsize=max_queue)
+ self._drop_lock = threading.Lock()
+ self._dropped = 0
+ self._handle: Any = None
+ self._written = 0
+ self._thread = threading.Thread(
+ target=self._run,
+ name="jarvis-log-writer",
+ daemon=True,
+ )
+ self._thread.start()
+
+ # -- loguru sink -------------------------------------------------------
+ def __call__(self, message: Any) -> None:
+ """Hand one formatted record to the writer thread. Never blocks."""
+ try:
+ self._queue.put_nowait(str(message))
+ except queue.Full:
+ # Silence is the point: a log sink that blocks or raises would stall
+ # whichever thread emitted the record. The drop is not lost — the
+ # count surfaces as a WARNING line in the next emitted batch.
+ with self._drop_lock:
+ self._dropped += 1
+
+ def stop(self, timeout: float = 5.0) -> None:
+ """Drain and close. Used by tests; the daemon thread needs no stop."""
+ with suppress(queue.Full):
+ self._queue.put_nowait(_LOG_STOP)
+ self._thread.join(timeout=timeout)
+
+ # -- writer thread -----------------------------------------------------
+ def _run(self) -> None:
+ while True:
+ first = self._queue.get()
+ if first is _LOG_STOP:
+ break
+ batch = [first]
+ stopping = False
+ while len(batch) < _LOG_BATCH_MAX:
+ try:
+ item = self._queue.get_nowait()
+ except queue.Empty:
+ # Not a failure: an empty queue simply ends this batch and
+ # the outer loop blocks on the next record.
+ break
+ if item is _LOG_STOP:
+ stopping = True
+ break
+ batch.append(item)
+ self._emit(batch)
+ if stopping:
+ break
+ self._close()
+
+ def _emit(self, batch: list[str]) -> None:
+ with self._drop_lock:
+ dropped, self._dropped = self._dropped, 0
+ if dropped:
+ batch.insert(
+ 0,
+ f"{time.strftime('%Y-%m-%d %H:%M:%S')} | WARNING | "
+ f"{__name__}:_AsyncLogWriter:0 | log writer dropped "
+ f"{dropped} record(s) while the disk was not keeping up.\n",
+ )
+ try:
+ handle = self._ensure_handle()
+ handle.write("".join(batch))
+ handle.flush()
+ self._written = handle.tell()
+ except (OSError, ValueError) as exc:
+ # Cannot log this — we ARE the log. stderr is None under
+ # pythonw.exe, so on the windowed build the only remaining signal
+ # is the drop counter above, reported once writing recovers.
+ self._handle = None
+ stream = getattr(sys, "__stderr__", None)
+ if stream is not None:
+ with suppress(Exception):
+ stream.write(f"jarvis log writer failed: {exc!r}\n")
+ return
+ if self._written >= self._rotation_bytes:
+ self._rotate()
+
+ def _ensure_handle(self) -> Any:
+ if self._handle is None:
+ self._path.parent.mkdir(parents=True, exist_ok=True)
+ # newline="" keeps loguru's "\n" from becoming "\r\n" on Windows,
+ # matching what the previous file sink wrote.
+ self._handle = self._path.open("a", encoding="utf-8", errors="replace", newline="")
+ self._written = self._handle.tell()
+ return self._handle
+
+ def _close(self) -> None:
+ handle, self._handle = self._handle, None
+ if handle is not None:
+ with suppress(Exception):
+ handle.close()
+
+ def _rotate(self) -> None:
+ self._close()
+ # Microsecond suffix mirrors the naming the loguru file sink used, so
+ # older rotated logs and new ones sort together — and two rotations in
+ # the same second cannot collide on a name.
+ now = time.time()
+ stamp = time.strftime("%Y-%m-%d_%H-%M-%S", time.localtime(now))
+ target = self._path.with_name(
+ f"{self._path.stem}.{stamp}_{int(now % 1 * 1_000_000):06d}{self._path.suffix}"
+ )
+ try:
+ self._path.rename(target)
+ except OSError:
+ # Another process holds the file open (Windows) or the rename
+ # raced. Keep appending to the current file — an oversized log is
+ # strictly better than losing records.
+ self._written = 0
+ return
+ self._prune()
+
+ def _prune(self) -> None:
+ pattern = f"{self._path.stem}.*{self._path.suffix}"
+ try:
+ rotated = sorted(
+ self._path.parent.glob(pattern),
+ key=lambda item: item.stat().st_mtime,
+ reverse=True,
+ )
+ except OSError:
+ # Listing the log directory failed (removed, or locked mid-scan on
+ # Windows). Pruning is housekeeping — skipping one round keeps a
+ # few extra rotated files, which beats raising inside the writer.
+ return
+ for stale in rotated[self._retention :]:
+ with suppress(OSError):
+ stale.unlink()
+
+
+def _install_desktop_log_sink(log_path: Path) -> None:
+ """Installs a loguru file sink for the desktop app.
+
+ Why: ``pythonw.exe`` (windowed mode, via ``run.bat`` without args) has
+ no stderr. Loguru writes to stderr by default → any crash in the
+ backend thread stays invisible, and the process becomes a zombie (port
+ not bound, window not open, user sees nothing).
+
+ This sink writes every ``INFO+`` event to a rotating log file, and
+ stdlib ``logging`` is redirected via ``InterceptHandler`` so that
+ ``uvicorn`` / ``httpx`` / ``faster_whisper`` get captured too. Writing
+ happens on :class:`_AsyncLogWriter`'s thread so that a slow disk can
+ never stall the caller — see that class for the incident this prevents.
+
+ Idempotent — calling it more than once is a no-op (important in case
+ DesktopApp gets instantiated multiple times in tests).
+ """
+ global _DESKTOP_LOG_SINK_INSTALLED
+ if _DESKTOP_LOG_SINK_INSTALLED:
+ return
+ _DESKTOP_LOG_SINK_INSTALLED = True
+
+ from loguru import logger
+
+ log_path.parent.mkdir(parents=True, exist_ok=True)
+ writer = _AsyncLogWriter(log_path)
+ # The writer is a daemon thread: a process that ends right after its last
+ # record — the launcher bouncing off a held lock is exactly that — would
+ # take the still-queued line with it. Drain on interpreter exit; bounded so
+ # a wedged disk cannot hold the exit hostage.
+ import atexit
+
+ atexit.register(writer.stop, 2.0)
+ logger.add(
+ writer,
+ level="INFO",
+ # The writer thread IS the queue. loguru's own enqueue= would add a
+ # multiprocessing pipe on top (WinError 5 in restricted contexts).
+ enqueue=False,
+ backtrace=True,
+ diagnose=False, # don't dump locals (secrets!)
+ format=(
+ "{time:YYYY-MM-DD HH:mm:ss.SSS} | {level: <8} | {name}:{function}:{line} | {message}"
+ ),
+ )
+
+ # Redirect stdlib logging -> loguru so uvicorn / httpx / faster_whisper
+ # also end up in the file log. Don't remove prior handlers (the
+ # watchdog run has its own handlers via _setup_logging).
+ import logging as _logging
+
+ from jarvis.core.redact import safe_preview as _safe_preview
+
+ class _InterceptHandler(_logging.Handler):
+ def emit(self, record: _logging.LogRecord) -> None:
+ try:
+ level: str | int = logger.level(record.levelname).name
+ except ValueError:
+ # A custom stdlib level loguru does not know by name; the
+ # numeric level carries the same information.
+ level = record.levelno
+ frame, depth = _logging.currentframe(), 2
+ while frame and frame.f_code.co_filename == _logging.__file__:
+ frame = frame.f_back
+ depth += 1
+ message = _safe_preview(record.getMessage(), max_chars=16_384)
+ logger.opt(depth=depth, exception=record.exc_info).log(level, message)
+
+ root = _logging.getLogger()
+ # Only add it if there isn't already an InterceptHandler present.
+ if not any(isinstance(h, _InterceptHandler) for h in root.handlers):
+ root.addHandler(_InterceptHandler())
+ if root.level > _logging.INFO or root.level == 0:
+ root.setLevel(_logging.INFO)
+
+ logger.info("Desktop log sink active: {}", log_path)
+ # Which interpreter is running the app is the first question a boot failure
+ # raises, and the log never answered it. A machine carries several Python
+ # installations, and a shortcut, scheduled task or launcher that resolves to
+ # a different one than the app was installed into looks exactly like "it
+ # suddenly stopped starting" (forensic 2026-08-09: a Start-menu shortcut
+ # pointed at an interpreter without pywebview, so the window import died
+ # 8 ms into boot). One line, every platform, no imports.
+ logger.info("Interpreter: {} (Python {}.{}.{})", sys.executable, *sys.version_info[:3])
diff --git a/jarvis/ui/native_dialog.py b/jarvis/ui/native_dialog.py
new file mode 100644
index 0000000000..1888d73caf
--- /dev/null
+++ b/jarvis/ui/native_dialog.py
@@ -0,0 +1,137 @@
+"""Native modal dialogs for the moments the app has no window of its own.
+
+A GUI launch is mute on all three desktop platforms: Windows ``pythonw`` has no
+standard streams at all, a macOS ``.app`` and a Linux ``.desktop`` with
+``Terminal=false`` bury stderr in Console.app and the journal. When the launcher
+or the relauncher must tell the user something BEFORE a window exists — the app
+refused to start, an earlier instance is stuck, a restart never came back — the
+only surface left is the operating system's own message box.
+
+Windows uses Win32 directly; macOS uses ``osascript``, which every install has;
+Linux tries the two desktop dialog helpers in turn and honestly gives up if the
+distro ships neither (stderr and the log still carry the reason). A headless
+host never shows anything: ``ask_yes_no`` answers ``False`` there, which every
+caller treats as "do nothing".
+
+Every function is best-effort and never raises — a dialog that cannot be shown
+must not turn a startup problem into a crash.
+"""
+
+from __future__ import annotations
+
+import contextlib
+import os
+import sys
+from collections.abc import Callable
+
+
+def _run_helper(cmd: list[str]) -> tuple[bool, int]:
+ """Run a dialog helper; ``(ran, returncode)``. ``ran=False`` → not installed."""
+ import subprocess
+
+ from jarvis.core.process_utils import NO_WINDOW_CREATIONFLAGS
+
+ try:
+ completed = subprocess.run( # noqa: S603 — fixed argv, no shell
+ cmd,
+ check=False,
+ encoding="utf-8",
+ errors="replace",
+ creationflags=NO_WINDOW_CREATIONFLAGS,
+ )
+ except (OSError, ValueError):
+ return False, -1
+ return True, int(getattr(completed, "returncode", 0) or 0)
+
+
+def _applescript_literal(text: str) -> str:
+ """Escape for an AppleScript string literal: backslash first, then the quote."""
+ return text.replace("\\", "\\\\").replace('"', '\\"')
+
+
+def _linux_has_display() -> bool:
+ return bool(os.environ.get("DISPLAY") or os.environ.get("WAYLAND_DISPLAY"))
+
+
+def show_error_dialog(title: str, message: str) -> None:
+ """Show a modal error box; silently does nothing where none can appear."""
+ if sys.platform == "win32":
+ with contextlib.suppress(Exception):
+ import ctypes
+
+ mb_iconerror = 0x10
+ ctypes.windll.user32.MessageBoxW(None, message, title, mb_iconerror)
+ return
+ if sys.platform == "darwin":
+ _run_helper(
+ [
+ "osascript",
+ "-e",
+ f'display dialog "{_applescript_literal(message)}" with title '
+ f'"{_applescript_literal(title)}" buttons {{"OK"}} default button "OK" '
+ "with icon stop",
+ ]
+ )
+ return
+ if not _linux_has_display():
+ return # no graphical session — a dialog has nowhere to appear
+ for cmd in (
+ ["zenity", "--error", f"--title={title}", f"--text={message}"],
+ ["kdialog", "--title", title, "--error", message],
+ ):
+ ran, _ = _run_helper(cmd)
+ if ran:
+ return
+
+
+def ask_yes_no(
+ title: str,
+ message: str,
+ *,
+ _run: Callable[[list[str]], tuple[bool, int]] = _run_helper,
+) -> bool:
+ """Modal Yes/No question; ``True`` only on an explicit Yes.
+
+ Anything else — No, the box closed, no dialog helper, no display, any
+ error — is ``False``, so callers can only ever take the destructive branch
+ on a real click. ``_run`` is injectable for tests (the helper is otherwise
+ a real process that blocks on a real click).
+ """
+ if sys.platform == "win32":
+ try:
+ import ctypes
+
+ mb_yesno, mb_iconquestion, mb_defbutton2, idyes = 0x4, 0x20, 0x100, 6
+ result = ctypes.windll.user32.MessageBoxW(
+ None, message, title, mb_yesno | mb_iconquestion | mb_defbutton2
+ )
+ return int(result) == idyes
+ except Exception: # noqa: BLE001 — no box → no consent
+ return False
+ if sys.platform == "darwin":
+ ran, code = _run(
+ [
+ "osascript",
+ "-e",
+ f'display dialog "{_applescript_literal(message)}" with title '
+ f'"{_applescript_literal(title)}" buttons {{"No", "Yes"}} '
+ 'default button "No" with icon caution',
+ "-e",
+ 'if button returned of result is "Yes" then return 0',
+ "-e",
+ "error number 1",
+ ]
+ )
+ # osascript exits 0 only when the script returned normally (Yes);
+ # "No" raises error 1 and a cancelled dialog raises -128.
+ return ran and code == 0
+ if not _linux_has_display():
+ return False
+ for cmd in (
+ ["zenity", "--question", f"--title={title}", f"--text={message}", "--default-cancel"],
+ ["kdialog", "--title", title, "--warningyesno", message],
+ ):
+ ran, code = _run(cmd)
+ if ran:
+ return code == 0 # both helpers exit 0 for Yes, 1 for No
+ return False
diff --git a/jarvis/ui/relauncher.py b/jarvis/ui/relauncher.py
index 4a36121925..ba6af8df9e 100644
--- a/jarvis/ui/relauncher.py
+++ b/jarvis/ui/relauncher.py
@@ -17,6 +17,7 @@ Invoked as::
It runs windowless + detached so it outlives its parent. Stdlib only — it must
start fast and never pull the heavy ``jarvis`` runtime into a throwaway process.
"""
+
from __future__ import annotations
import json
@@ -32,6 +33,7 @@ from pathlib import Path
from jarvis.core.branding import (
MACOS_APP_DIR_NAME,
MANAGED_INSTALL_MARKER,
+ PRODUCT_NAME,
WINDOWS_BRANDED_LAUNCH_ENV_VAR,
)
@@ -136,11 +138,7 @@ def fresh_user_env(
# marker would make that chain skip branding and fall back to pythonw.exe's
# Python icon. Match case-insensitively because Windows environment names are
# case-insensitive even though a supplied test/POSIX mapping may not be.
- for transient in [
- key
- for key in env
- if key.upper() == WINDOWS_BRANDED_LAUNCH_ENV_VAR.upper()
- ]:
+ for transient in [key for key in env if key.upper() == WINDOWS_BRANDED_LAUNCH_ENV_VAR.upper()]:
del env[transient]
persisted = _read_persisted()
if persisted is None:
@@ -148,9 +146,7 @@ def fresh_user_env(
# Drop every inherited JARVIS__* key first (Windows env names are
# case-insensitive, so spelling variants must not survive alongside the
# refreshed names), then lay down the persisted set verbatim.
- for stale in [
- k for k in env if k.upper().startswith("JARVIS__") and k not in persisted
- ]:
+ for stale in [k for k in env if k.upper().startswith("JARVIS__") and k not in persisted]:
del env[stale]
env.update(persisted)
return env
@@ -306,9 +302,7 @@ def _read_pending_update(root: Path) -> dict[str, object] | None:
"""Read and strictly validate a relaunch-time update transaction."""
try:
- payload = json.loads(
- (root / PENDING_UPDATE_FILENAME).read_text(encoding="utf-8")
- )
+ payload = json.loads((root / PENDING_UPDATE_FILENAME).read_text(encoding="utf-8"))
except (OSError, UnicodeDecodeError, json.JSONDecodeError, TypeError):
return None
if not isinstance(payload, dict) or payload.get("schema") != 1:
@@ -335,18 +329,12 @@ def _managed_python(root: Path) -> str:
return str(candidate if candidate.is_file() else Path(sys.executable))
-def _run_update_command(
- cmd: list[str], *, root: Path, timeout: float
-) -> int:
+def _run_update_command(cmd: list[str], *, root: Path, timeout: float) -> int:
"""Run a windowless update child and collapse every launch failure to -1."""
kwargs: dict[str, object] = {
"cwd": str(root),
- "env": {
- key: value
- for key, value in os.environ.items()
- if key != "JARVIS_INSTALL_NO_PIP"
- },
+ "env": {key: value for key, value in os.environ.items() if key != "JARVIS_INSTALL_NO_PIP"},
"stdin": subprocess.DEVNULL,
"stdout": subprocess.DEVNULL,
"stderr": subprocess.DEVNULL,
@@ -400,11 +388,14 @@ def _ui_bundle_ready(root: Path) -> bool:
required.add(f"jarvis/ui/web/dist/{ref}")
for rel in required:
- if _run_update_command(
- ["git", "ls-files", "--error-unmatch", "--", rel],
- root=root,
- timeout=30.0,
- ) != 0:
+ if (
+ _run_update_command(
+ ["git", "ls-files", "--error-unmatch", "--", rel],
+ root=root,
+ timeout=30.0,
+ )
+ != 0
+ ):
return False
return True
@@ -523,20 +514,13 @@ def finalize_pending_update(cwd: str | Path) -> bool:
profile = str(payload["profile"])
target_reset_ok = (
- _run_update_command(
- ["git", "reset", "--hard", target], root=root, timeout=120.0
- )
- == 0
+ _run_update_command(["git", "reset", "--hard", target], root=root, timeout=120.0) == 0
)
target_install_ok = False
if target_reset_ok:
- target_install_ok = (
- _run_update_command(
- _installer_command(root, profile), root=root, timeout=3600.0
- )
- == 0
- and _ui_bundle_ready(root)
- )
+ target_install_ok = _run_update_command(
+ _installer_command(root, profile), root=root, timeout=3600.0
+ ) == 0 and _ui_bundle_ready(root)
if target_install_ok:
pending_path.unlink(missing_ok=True)
_write_update_result(
@@ -549,20 +533,13 @@ def finalize_pending_update(cwd: str | Path) -> bool:
return True
rollback_reset_ok = (
- _run_update_command(
- ["git", "reset", "--hard", previous], root=root, timeout=120.0
- )
- == 0
+ _run_update_command(["git", "reset", "--hard", previous], root=root, timeout=120.0) == 0
)
rollback_install_ok = False
if rollback_reset_ok:
- rollback_install_ok = (
- _run_update_command(
- _installer_command(root, profile), root=root, timeout=3600.0
- )
- == 0
- and _ui_bundle_ready(root)
- )
+ rollback_install_ok = _run_update_command(
+ _installer_command(root, profile), root=root, timeout=3600.0
+ ) == 0 and _ui_bundle_ready(root)
pending_path.unlink(missing_ok=True)
rolled_back = rollback_reset_ok and rollback_install_ok
_write_update_result(
@@ -584,6 +561,7 @@ def main(
_alive=pid_alive,
_settled=_new_instance_settled,
_finalize_update=finalize_pending_update,
+ _report=None,
attempts: int = 3,
) -> int:
"""Wait for the old app to exit, then start a fresh launcher — verified.
@@ -604,6 +582,7 @@ def main(
except ValueError:
return 2
cwd = argv[1]
+ _report = _report or _report_restart_failure
kwargs: dict[str, object] = {"cwd": cwd, "close_fds": True}
# Fresh JARVIS__* overrides for the new instance — never the dying
@@ -637,8 +616,42 @@ def main(
new_pid = getattr(proc, "pid", None)
if _settled(new_pid, _alive=_alive, _sleep=_sleep):
return 0
+ _report(pid, still_alive=_alive(pid))
return 1
+def _report_restart_failure(old_pid: int, *, still_alive: bool) -> None:
+ """Say out loud that the restart did not bring the app back.
+
+ The relauncher is a windowless helper: its return code goes nowhere and the
+ app it was replacing is gone, so a failed restart used to look exactly like
+ "the app closed and never came back" — until the user found the Start menu
+ (live incident 2026-08-25). One native box, best-effort, never raises.
+ """
+ if still_alive:
+ message = (
+ f"{PRODUCT_NAME} could not restart: the previous instance (process "
+ f"{old_pid}) has not exited. End it in the task manager, then start "
+ f"{PRODUCT_NAME} from the Start menu."
+ )
+ else:
+ message = (
+ f"{PRODUCT_NAME} could not restart: the new instance did not stay up. "
+ f"Start {PRODUCT_NAME} from the Start menu; the log file has the details."
+ )
+ logging.getLogger(__name__).error(message)
+ try:
+ from jarvis.core.process_utils import standard_error_is_visible
+
+ if standard_error_is_visible():
+ print(message, file=sys.stderr, flush=True)
+ return # a readable stderr (terminal, pipe, test capture) is enough
+ from jarvis.ui.native_dialog import show_error_dialog
+
+ show_error_dialog(f"{PRODUCT_NAME} restart failed", message)
+ except Exception: # noqa: BLE001, S110 — a helper that cannot show a box stays quiet
+ pass
+
+
if __name__ == "__main__": # pragma: no cover
raise SystemExit(main())
diff --git a/jarvis/ui/web/launcher.py b/jarvis/ui/web/launcher.py
index 3046031555..707a51181c 100644
--- a/jarvis/ui/web/launcher.py
+++ b/jarvis/ui/web/launcher.py
@@ -49,6 +49,7 @@ ensure_standard_streams()
# Windows.
_ensure_dpi_awareness()
+
# Taskbar-Icon-Fix (Windows): the unique AUMID must be set BEFORE pywebview
# creates the window. Done in ``_run_desktop`` (the only path with a window) so
# the module import — which the headless fast-boot path pays on the
@@ -149,9 +150,7 @@ def _select_instance_early(argv: list[str]) -> str | None:
return None
-def _acquire_primary_lock_for_headless(
- *, lock_path=None, meta_path=None
-):
+def _acquire_primary_lock_for_headless(*, lock_path=None, meta_path=None):
"""Claim primary-instance status for a headless run and set the env flag.
Decides ``JARVIS_PRIMARY_INSTANCE`` the SAME way the desktop path does:
@@ -185,9 +184,7 @@ def _acquire_primary_lock_for_headless(
)
try:
- lock = acquire_single_instance_lock(
- lock_path=lock_path, meta_path=meta_path
- )
+ lock = acquire_single_instance_lock(lock_path=lock_path, meta_path=meta_path)
except SingleInstanceError:
lock = None
except Exception as exc: # noqa: BLE001 — lock infra must never block boot
@@ -200,7 +197,8 @@ def _acquire_primary_lock_for_headless(
_logging.getLogger(__name__).warning(
"headless lock via desktop_app failed (%s) — trying a direct "
- "FileLock fallback so a sole VPS instance still stays primary", exc,
+ "FileLock fallback so a sole VPS instance still stays primary",
+ exc,
)
lock = _direct_filelock_fallback(lock_path)
@@ -507,9 +505,7 @@ async def _run_headless(args) -> int:
if args.dev:
cfg = cfg.model_copy(update={"ui": cfg.ui.model_copy(update={"dev_mode": True})})
if args.port is not None:
- cfg = cfg.model_copy(
- update={"ui": cfg.ui.model_copy(update={"admin_api_port": args.port})}
- )
+ cfg = cfg.model_copy(update={"ui": cfg.ui.model_copy(update={"admin_api_port": args.port})})
try:
from jarvis.core import control_key
@@ -590,9 +586,7 @@ async def _run_headless(args) -> int:
async def _build_brain_bg() -> None:
try:
- built = await asyncio.to_thread(
- build_default_brain, bus=server.bus, tier="router"
- )
+ built = await asyncio.to_thread(build_default_brain, bus=server.bus, tier="router")
brain_holder["brain"] = built
server.app.state.brain = built
# Re-wire the late-built brain into the task runner: _init_task_stack
@@ -750,9 +744,7 @@ async def _run_headless(args) -> int:
if reply:
role = "system" if _is_brain_diagnostic(reply) else "assistant"
trace = turn_traces.snapshot(turn_started_ms) if role == "assistant" else None
- await chat_store.add_message(
- thread_id=thread_id, role=role, text=reply, trace=trace
- )
+ await chat_store.add_message(thread_id=thread_id, role=role, text=reply, trace=trace)
server.bus.subscribe(MessageSent, _on_user_message)
@@ -888,71 +880,15 @@ async def _run_headless(args) -> int:
def _show_error_dialog(title: str, message: str) -> None:
- """Last-resort visible surface: a native modal box, on every desktop OS.
-
- Reached only when the message provably went nowhere else (see
- ``_report_startup_failure``), because a GUI launch is mute on all three
- platforms: Windows ``pythonw`` has no streams at all, a macOS ``.app`` and a
- Linux ``.desktop`` with ``Terminal=false`` bury stderr in Console.app and
- the journal. Same failure, same class of user, so all three get a dialog.
+ """Native modal box on every desktop OS — see ``jarvis.ui.native_dialog``.
- Windows uses Win32 directly; macOS uses ``osascript``, which every install
- has; Linux tries the two desktop dialog helpers in turn and honestly gives
- up if the distro ships neither — stderr and the log still carry the reason.
-
- Separated from its caller so tests can assert the DECISION to show a box
+ Kept as a module-level seam so tests can assert the DECISION to show a box
without a real one opening: an unattended modal blocks until someone clicks
it, which in CI is a hung run.
"""
- if sys.platform == "win32":
- with contextlib.suppress(Exception):
- import ctypes
+ from jarvis.ui.native_dialog import show_error_dialog
- MB_ICONERROR = 0x10
- ctypes.windll.user32.MessageBoxW(None, message, title, MB_ICONERROR)
- return
-
- import subprocess
-
- from jarvis.core.process_utils import NO_WINDOW_CREATIONFLAGS
-
- def _run(cmd: list[str]) -> bool:
- try:
- subprocess.run( # noqa: S603 — fixed argv, no shell, no user input in argv[0]
- cmd,
- check=False,
- encoding="utf-8",
- errors="replace",
- creationflags=NO_WINDOW_CREATIONFLAGS,
- )
- return True
- except (OSError, ValueError):
- return False # helper not installed → try the next one
-
- if sys.platform == "darwin":
- # AppleScript string literals: backslash first, then the quote.
- def _as(text: str) -> str:
- return text.replace("\\", "\\\\").replace('"', '\\"')
-
- _run(
- [
- "osascript",
- "-e",
- f'display dialog "{_as(message)}" with title "{_as(title)}" '
- 'buttons {"OK"} default button "OK" with icon stop',
- ]
- )
- return
-
- # Linux: no shell, so the text is an argument and needs no escaping.
- if not (os.environ.get("DISPLAY") or os.environ.get("WAYLAND_DISPLAY")):
- return # no graphical session — a dialog has nowhere to appear
- for cmd in (
- ["zenity", "--error", f"--title={title}", f"--text={message}"],
- ["kdialog", "--title", title, "--error", message],
- ):
- if _run(cmd):
- return
+ show_error_dialog(title, message)
def _report_startup_failure(message: str) -> None:
@@ -1039,6 +975,136 @@ def _missing_backend_server() -> str | None:
return None
+def _install_boot_trace(raw_argv: list[str]) -> None:
+ """Start the desktop log file NOW, so every launch leaves a trace.
+
+ Until 2026-08-25 the file sink was installed by ``DesktopApp.__init__`` —
+ after the branded re-exec, the elevation check, config, the control key and
+ the single-instance lock. A launch that ended in any of those (an "already
+ running" bounce, a crash on an import, a lock held by a stuck earlier
+ instance) ran under ``pythonw`` with no console and left NOTHING behind: the
+ user clicked, nothing appeared, and the log could not say why (live incident
+ 2026-08-25, an 11-minute hole between two launches). The sink is cheap
+ (loguru + stdlib) and idempotent, so the later ``DesktopApp`` call is a
+ no-op against it. Best-effort: a log that cannot be opened never blocks boot.
+ """
+ try:
+ from loguru import logger
+
+ from jarvis.ui.desktop_log import _install_desktop_log_sink, desktop_log_path
+
+ _install_desktop_log_sink(desktop_log_path())
+ logger.info(
+ "launcher: start pid={} argv={} cwd={}",
+ os.getpid(),
+ raw_argv,
+ os.getcwd(),
+ )
+ except Exception: # noqa: BLE001, S110 — a mute launch is what we had before
+ pass
+
+
+def _recover_from_already_running(
+ error: Exception,
+ *,
+ focus=None,
+ read_meta=None,
+ ask=None,
+ terminate=None,
+ acquire=None,
+):
+ """The lock is held. Bring the holder forward — or, with consent, evict it.
+
+ Returns a freshly acquired lock when the user chose to stop a stuck holder
+ and the lock could then be taken, else ``None`` (the caller exits 3).
+
+ The honest case — a healthy instance with a window — is handled by focusing
+ it, exactly as before. The case that used to be a silent exit is a holder
+ that is alive but has NO window to bring forward (a previous instance whose
+ teardown wedged, or a lock the eviction logic could not reclaim): the user
+ clicked, nothing happened, and every further click did the same until the
+ stuck process died on its own. Now that turns into one native Yes/No box,
+ and Yes terminates the holder and takes the lock. No consent, no kill: the
+ default button is No and any failure to ask counts as No.
+ """
+ from loguru import logger
+
+ from jarvis.ui import desktop_app as _desktop_app
+
+ focus = focus or _desktop_app.focus_existing_instance_robust
+ read_meta = read_meta or _desktop_app._read_meta
+ terminate = terminate or _desktop_app._terminate_pid
+ acquire = acquire or _desktop_app.acquire_single_instance_lock
+ if ask is None:
+ from jarvis.ui.native_dialog import ask_yes_no
+
+ ask = ask_yes_no
+
+ focused = False
+ try:
+ focused = bool(focus())
+ except Exception as exc: # noqa: BLE001
+ logger.warning("launcher: focusing the running instance failed: {}", exc)
+ logger.warning(
+ "launcher: {} — {}",
+ error,
+ "brought its window forward" if focused else "NO window found",
+ )
+ if focused:
+ return None
+
+ meta = None
+ with contextlib.suppress(Exception):
+ meta = read_meta()
+ pid = None
+ with contextlib.suppress(Exception):
+ pid = int(meta["pid"]) if meta and meta.get("pid") is not None else None
+ if pid is None or pid == os.getpid():
+ _report_startup_failure(
+ f"{APP_DISPLAY_NAME} reports that it is already running, but no window "
+ "could be found and the running process is unknown.\n\n"
+ f"Detail: {error}"
+ )
+ return None
+
+ consented = False
+ try:
+ consented = bool(
+ ask(
+ f"{APP_DISPLAY_NAME} is already running",
+ f"{APP_DISPLAY_NAME} is already running (process {pid}), but it has no "
+ "window that could be brought to the front. It is probably stuck.\n\n"
+ "Stop that process and start fresh?",
+ )
+ )
+ except Exception as exc: # noqa: BLE001
+ logger.warning("launcher: could not ask about the stuck instance: {}", exc)
+ logger.info(
+ "launcher: stuck holder pid={} — user {}",
+ pid,
+ "consented" if consented else "declined",
+ )
+ if not consented:
+ return None
+
+ if not terminate(pid):
+ _report_startup_failure(
+ f"{APP_DISPLAY_NAME} could not stop the running process {pid}. "
+ "End it in the task manager, then start the app again."
+ )
+ return None
+ try:
+ lock = acquire()
+ except Exception as exc: # noqa: BLE001 — SingleInstanceError or lock I/O
+ _report_startup_failure(
+ f"{APP_DISPLAY_NAME} stopped process {pid}, but the start lock is still "
+ f"held. Start the app again in a few seconds.\n\nDetail: {exc}"
+ )
+ return None
+ logger.info("launcher: evicted stuck holder pid={} and took the lock", pid)
+ return lock
+
+
def _run_desktop(cfg, use_lock: bool) -> int:
"""Full desktop app with a pywebview window.
@@ -1058,10 +1124,11 @@ def _run_desktop(cfg, use_lock: bool) -> int:
if use_lock:
try:
lock = acquire_single_instance_lock()
- except SingleInstanceError:
- print("Jarvis is already running.", file=sys.stderr)
- focus_existing_instance_robust()
- return 3
+ except SingleInstanceError as exc:
+ print(f"{APP_DISPLAY_NAME} is already running.", file=sys.stderr)
+ lock = _recover_from_already_running(exc, focus=focus_existing_instance_robust)
+ if lock is None:
+ return 3
# Fix #2 (2026-05-29): tell the backend whether this is the PRIMARY
# instance. Only the lock holder may run the mission crash_recovery sweep;
@@ -1276,10 +1343,16 @@ def _run_desktop_fast(args) -> int | None:
_log.getLogger(__name__).error("fast-boot backend did not signal in 60s")
return None
if holder["already_running"]:
+ from loguru import logger
+
from jarvis.ui.desktop_app import focus_existing_instance_robust
- print("Jarvis is already running.", file=sys.stderr)
- focus_existing_instance_robust()
+ print(f"{APP_DISPLAY_NAME} is already running.", file=sys.stderr)
+ focused = focus_existing_instance_robust()
+ logger.warning(
+ "launcher(fast): already running — {}",
+ "brought its window forward" if focused else "NO window found",
+ )
return 3
app = holder["app"]
if app is None:
@@ -1304,6 +1377,31 @@ def _run_desktop_fast(args) -> int | None:
def main(argv: list[str] | None = None) -> int:
+ """Boot the desktop app; a crash before the window is logged AND shown.
+
+ Everything happens in ``_main``. This wrapper exists for the failure that
+ used to be invisible: an exception on the way to the window, under a
+ ``pythonw`` that has no stderr, ended the process with no trace at all.
+ """
+ try:
+ return _main(argv)
+ except SystemExit:
+ raise
+ except KeyboardInterrupt:
+ return 130
+ except Exception as exc: # noqa: BLE001 — last line of defence, must report
+ with contextlib.suppress(Exception):
+ from loguru import logger
+
+ logger.opt(exception=exc).critical("launcher: crashed before the window")
+ _report_startup_failure(
+ f"{APP_DISPLAY_NAME} crashed while starting, before its window could "
+ f"open.\n\n{type(exc).__name__}: {exc}\n\nThe log file has the details."
+ )
+ return 1
+
+
+def _main(argv: list[str] | None = None) -> int:
# Stamp the boot-profiling t0 as early as possible (only when opted in) so
# BOOT_READY_MS reflects nearly the full in-process cold-start cost. The
# harness's spawn→ready wall-clock remains the authoritative headline; this
@@ -1375,6 +1473,10 @@ def main(argv: list[str] | None = None) -> int:
args = _parse_args(_raw_argv)
+ # From here on every desktop launch is written down — see the helper.
+ if not args.headless:
+ _install_boot_trace(_raw_argv)
+
# Refuse a window we cannot build, BEFORE paying for the boot. Everything
# below this line — config, control key, autostart, backend, voice — takes
# up to a minute and would all be thrown away at the pywebview import. Worse,
@@ -1458,6 +1560,14 @@ def main(argv: list[str] | None = None) -> int:
_reexec = maybe_reexec_through_branded_launcher(list(_raw_argv))
if _reexec is not None:
+ with contextlib.suppress(Exception):
+ from loguru import logger as _rlog
+
+ _rlog.info(
+ "launcher: handed this boot to the branded launcher exe "
+ "(exit {}); the child logs from here on",
+ _reexec,
+ )
return _reexec
except Exception: # noqa: BLE001 — never let branding block boot
pass
@@ -1531,13 +1641,9 @@ def main(argv: list[str] | None = None) -> int:
# CLI-Overrides
if args.dev:
- cfg = cfg.model_copy(
- update={"ui": cfg.ui.model_copy(update={"dev_mode": True})}
- )
+ cfg = cfg.model_copy(update={"ui": cfg.ui.model_copy(update={"dev_mode": True})})
if args.port is not None:
- cfg = cfg.model_copy(
- update={"ui": cfg.ui.model_copy(update={"admin_api_port": args.port})}
- )
+ cfg = cfg.model_copy(update={"ui": cfg.ui.model_copy(update={"admin_api_port": args.port})})
# Per-user Jarvis Control API key — generate-once BEFORE the app serves so
# it exists by the time a local agent (Codex CLI / Claude Code) hits
diff --git a/tests/unit/ui/test_launcher_boot_trace.py b/tests/unit/ui/test_launcher_boot_trace.py
new file mode 100644
index 0000000000..56770d7c4b
--- /dev/null
+++ b/tests/unit/ui/test_launcher_boot_trace.py
@@ -0,0 +1,155 @@
+"""The launcher leaves a trace of EVERY desktop launch, and never bounces mute.
+
+Background (2026-08-25): a launch that ended before ``DesktopApp`` existed —
+an "already running" bounce, a crash on an import, a lock held by a stuck
+earlier instance — ran under ``pythonw`` with no console and no log sink, so
+the user clicked, nothing appeared, and nothing was recorded. These tests pin
+the three answers: the early log path equals the one the app uses later, a
+holder with no window turns into a consent dialog (and only Yes evicts), and a
+crash before the window is reported instead of swallowed.
+"""
+
+from __future__ import annotations
+
+from pathlib import Path
+
+import pytest
+
+from jarvis.ui.web import launcher
+
+
+def test_early_log_path_matches_the_data_dir_the_app_uses(monkeypatch):
+ """The sink is installed once per process: both resolutions MUST agree."""
+ from jarvis.core import config
+ from jarvis.ui.desktop_log import desktop_log_path
+
+ monkeypatch.delenv("JARVIS_DATA_DIR", raising=False)
+ assert desktop_log_path() == config.DATA_DIR / "jarvis_desktop.log"
+
+
+def test_early_log_path_honours_the_env_override(monkeypatch, tmp_path: Path):
+ from jarvis.ui.desktop_log import desktop_log_path
+
+ monkeypatch.setenv("JARVIS_DATA_DIR", str(tmp_path))
+ assert desktop_log_path() == tmp_path / "jarvis_desktop.log"
+
+
+class _Lock:
+ pass
+
+
+class _Calls:
+ def __init__(self, *, focused: bool, meta, consent: bool, killed: bool = True):
+ self.focused = focused
+ self.meta = meta
+ self.consent = consent
+ self.killed = killed
+ self.asked: list[tuple[str, str]] = []
+ self.terminated: list[int] = []
+ self.acquired = 0
+ self.reported: list[str] = []
+
+ def focus(self):
+ return self.focused
+
+ def read_meta(self):
+ return self.meta
+
+ def ask(self, title, message):
+ self.asked.append((title, message))
+ return self.consent
+
+ def terminate(self, pid):
+ self.terminated.append(pid)
+ return self.killed
+
+ def acquire(self):
+ self.acquired += 1
+ return _Lock()
+
+
+def _recover(calls: _Calls):
+ return launcher._recover_from_already_running(
+ RuntimeError("Jarvis is already running (pid=4242)."),
+ focus=calls.focus,
+ read_meta=calls.read_meta,
+ ask=calls.ask,
+ terminate=calls.terminate,
+ acquire=calls.acquire,
+ )
+
+
+def test_a_healthy_holder_is_focused_and_never_asked_about(monkeypatch):
+ calls = _Calls(focused=True, meta={"pid": 4242, "port": 47821}, consent=True)
+ assert _recover(calls) is None
+ assert calls.asked == []
+ assert calls.terminated == []
+
+
+def test_a_holder_without_a_window_is_evicted_only_on_yes(monkeypatch):
+ calls = _Calls(focused=False, meta={"pid": 4242, "port": 47821}, consent=True)
+ lock = _recover(calls)
+ assert isinstance(lock, _Lock)
+ assert calls.terminated == [4242]
+ assert calls.acquired == 1
+ title, message = calls.asked[0]
+ assert "4242" in message and "stuck" in message
+
+
+def test_declining_the_dialog_keeps_the_holder_alive():
+ calls = _Calls(focused=False, meta={"pid": 4242, "port": 47821}, consent=False)
+ assert _recover(calls) is None
+ assert calls.terminated == []
+ assert calls.acquired == 0
+
+
+def test_an_unknown_holder_is_reported_not_killed(monkeypatch):
+ seen: list[str] = []
+ monkeypatch.setattr(launcher, "_report_startup_failure", seen.append)
+ calls = _Calls(focused=False, meta=None, consent=True)
+ assert _recover(calls) is None
+ assert calls.asked == []
+ assert seen and "already running" in seen[0]
+
+
+def test_a_kill_that_did_not_take_is_reported(monkeypatch):
+ seen: list[str] = []
+ monkeypatch.setattr(launcher, "_report_startup_failure", seen.append)
+ calls = _Calls(focused=False, meta={"pid": 4242}, consent=True, killed=False)
+ assert _recover(calls) is None
+ assert calls.acquired == 0
+ assert seen and "4242" in seen[0]
+
+
+def test_run_desktop_bounces_with_exit_3_when_recovery_declines(monkeypatch):
+ from jarvis.ui import desktop_app
+
+ def _raise_lock(*args, **kwargs):
+ raise desktop_app.SingleInstanceError("already running")
+
+ monkeypatch.setattr(desktop_app, "acquire_single_instance_lock", _raise_lock)
+ monkeypatch.setattr(desktop_app, "focus_existing_instance_robust", lambda: False)
+ monkeypatch.setattr(launcher, "_recover_from_already_running", lambda *a, **k: None)
+ assert launcher._run_desktop(cfg=object(), use_lock=True) == 3
+
+
+def test_a_crash_before_the_window_is_reported(monkeypatch):
+ seen: list[str] = []
+ monkeypatch.setattr(launcher, "_report_startup_failure", seen.append)
+
+ def _boom(argv):
+ raise ValueError("no window toolkit today")
+
+ monkeypatch.setattr(launcher, "_main", _boom)
+ assert launcher.main([]) == 1
+ assert seen and "ValueError" in seen[0] and "no window toolkit today" in seen[0]
+
+
+def test_a_system_exit_passes_through_untouched(monkeypatch):
+ def _exit(argv):
+ raise SystemExit(7)
+
+ monkeypatch.setattr(launcher, "_main", _exit)
+ with pytest.raises(SystemExit) as info:
+ launcher.main([])
+ assert info.value.code == 7
diff --git a/tests/unit/ui/test_launcher_visible_startup_failure.py b/tests/unit/ui/test_launcher_visible_startup_failure.py
index 7fb254d1f9..0f76affcb4 100644
--- a/tests/unit/ui/test_launcher_visible_startup_failure.py
+++ b/tests/unit/ui/test_launcher_visible_startup_failure.py
@@ -304,7 +304,8 @@ class TestLauncherRefusesEarly:
"""
import inspect
- src = inspect.getsource(launcher.main)
+ # ``main`` is a thin crash-reporting wrapper; the boot body is ``_main``.
+ src = inspect.getsource(launcher._main)
before_gate = src[: src.index("_missing_window_toolkit")]
assert "if not args.headless:" in before_gate.split("args = _parse_args")[-1], (
"the window-toolkit gate must sit behind `if not args.headless:` — "
@@ -315,7 +316,8 @@ class TestLauncherRefusesEarly:
"""Fail in a second, not after a minute of discarded work."""
import inspect
- src = inspect.getsource(launcher.main)
+ # ``main`` is a thin crash-reporting wrapper; the boot body is ``_main``.
+ src = inspect.getsource(launcher._main)
gate_at = src.index("_missing_window_toolkit")
server_at = src.index("_missing_backend_server")
for later in ("load_config()", "ensure_control_key", "_run_desktop("):
diff --git a/tests/unit/ui/test_native_dialog_and_eviction.py b/tests/unit/ui/test_native_dialog_and_eviction.py
new file mode 100644
index 0000000000..0dd85049e4
--- /dev/null
+++ b/tests/unit/ui/test_native_dialog_and_eviction.py
@@ -0,0 +1,150 @@
+"""Consent dialog semantics and the process-tree fallback of lock eviction.
+
+``ask_yes_no`` may only ever return True on an explicit Yes: a missing helper,
+no display, a closed box or an error all count as No, so the destructive
+branch behind it (stopping a stuck instance) needs a real click. The Windows
+``MessageBoxW`` path cannot run unattended, so these tests exercise the helper
+protocol through the injectable runner on the non-Windows branches.
+"""
+
+from __future__ import annotations
+
+import sys
+
+import pytest
+
+from jarvis.ui import native_dialog
+
+
[email protected](sys.platform == "win32", reason="Win32 branch opens a real box")
+def test_yes_is_exit_zero_from_the_first_installed_helper(monkeypatch):
+ monkeypatch.setenv("DISPLAY", ":0")
+ monkeypatch.setattr(sys, "platform", "linux")
+ seen: list[list[str]] = []
+
+ def _run(cmd):
+ seen.append(cmd)
+ return (True, 0)
+
+ assert native_dialog.ask_yes_no("t", "m", _run=_run) is True
+ assert seen[0][0] == "zenity"
+
+
[email protected](sys.platform == "win32", reason="Win32 branch opens a real box")
+def test_no_and_missing_helpers_are_both_no(monkeypatch):
+ monkeypatch.setenv("DISPLAY", ":0")
+ monkeypatch.setattr(sys, "platform", "linux")
+ assert native_dialog.ask_yes_no("t", "m", _run=lambda cmd: (True, 1)) is False
+ assert native_dialog.ask_yes_no("t", "m", _run=lambda cmd: (False, -1)) is False
+
+
[email protected](sys.platform == "win32", reason="Win32 branch opens a real box")
+def test_no_display_means_no_without_running_anything(monkeypatch):
+ monkeypatch.delenv("DISPLAY", raising=False)
+ monkeypatch.delenv("WAYLAND_DISPLAY", raising=False)
+ monkeypatch.setattr(sys, "platform", "linux")
+
+ def _run(cmd):
+ raise AssertionError("must not run a helper without a display")
+
+ assert native_dialog.ask_yes_no("t", "m", _run=_run) is False
+
+
[email protected](sys.platform == "win32", reason="Win32 branch opens a real box")
+def test_macos_yes_is_a_normal_osascript_return(monkeypatch):
+ monkeypatch.setattr(sys, "platform", "darwin")
+ seen: list[list[str]] = []
+
+ def _run(cmd):
+ seen.append(cmd)
+ return (True, 0)
+
+ assert native_dialog.ask_yes_no("t", 'say "hi"', _run=_run) is True
+ assert seen[0][0] == "osascript"
+ assert '\\"hi\\"' in seen[0][2] # the quote is escaped for AppleScript
+ assert native_dialog.ask_yes_no("t", "m", _run=lambda cmd: (True, 1)) is False
+
+
+def test_eviction_falls_back_to_the_process_tree_kill(monkeypatch):
+ """psutil's kill did not take → the tree kill runs → liveness is re-checked."""
+ from jarvis.ui import desktop_app
+
+ alive = {"value": True}
+ tree_kills: list[int] = []
+
+ class _Proc:
+ def __init__(self, pid):
+ self.pid = pid
+
+ def terminate(self):
+ pass
+
+ def kill(self):
+ pass
+
+ def wait(self, timeout=None):
+ raise TimeoutError("still there")
+
+ class _FakePsutil:
+ Process = _Proc
+
+ monkeypatch.setitem(sys.modules, "psutil", _FakePsutil())
+ monkeypatch.setattr(desktop_app, "_pid_alive", lambda pid: alive["value"])
+
+ def _tree_kill(pid):
+ tree_kills.append(pid)
+ alive["value"] = False
+ return True
+
+ assert desktop_app._terminate_pid(4242, _tree_kill=_tree_kill) is True
+ assert tree_kills == [4242]
+
+
+def test_eviction_reports_honestly_when_nothing_took(monkeypatch):
+ from jarvis.ui import desktop_app
+
+ class _Proc:
+ def __init__(self, pid):
+ pass
+
+ def terminate(self):
+ pass
+
+ def kill(self):
+ pass
+
+ def wait(self, timeout=None):
+ raise TimeoutError("still there")
+
+ class _FakePsutil:
+ Process = _Proc
+
+ monkeypatch.setitem(sys.modules, "psutil", _FakePsutil())
+ monkeypatch.setattr(desktop_app, "_pid_alive", lambda pid: True)
+ monkeypatch.setattr(desktop_app.time, "sleep", lambda s: None)
+ assert desktop_app._terminate_pid(4242, _tree_kill=lambda pid: True) is False
+
+
+def test_a_bound_but_slow_port_is_a_busy_holder_not_a_zombie(monkeypatch):
+ """Only a refused connection (nothing bound) may count as dead."""
+ import httpx
+
+ from jarvis.ui import desktop_app
+
+ monkeypatch.setattr(desktop_app.time, "sleep", lambda s: None)
+
+ def _slow(url, timeout=None):
+ raise httpx.ReadTimeout("busy")
+
+ monkeypatch.setattr(httpx, "get", _slow)
+ assert desktop_app._default_lock_holder_health(47821) is True
+
+ calls = {"n": 0}
+
+ def _refused(url, timeout=None):
+ calls["n"] += 1
+ raise httpx.ConnectError("nothing listens")
+
+ monkeypatch.setattr(httpx, "get", _refused)
+ assert desktop_app._default_lock_holder_health(47821) is False
+ assert calls["n"] == 4 # probed briefly, then declared dead