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: restore stdio for windowed desktop startup

rubenluetke10-beepc57c760ee1f9
diff --git a/jarvis/core/process_utils.py b/jarvis/core/process_utils.py
index 1b071b6e6e..e698eff669 100644
--- a/jarvis/core/process_utils.py
+++ b/jarvis/core/process_utils.py
@@ -1,9 +1,9 @@
-"""Cross-platform helpers for the Windows quirks a ``pythonw`` desktop app hits.
+"""Cross-platform helpers for the process quirks a desktop app hits.
-Two unrelated ones live here: spawning subprocesses without flashing console
-windows (``NO_WINDOW_CREATIONFLAGS``, below) and stopping Windows from replacing
-an unresponsive window with an opaque ghost (``disable_windows_app_ghosting``).
-Both are no-ops off Windows.
+The helpers cover a windowed Python process without standard streams, spawning
+subprocesses without flashing console windows (``NO_WINDOW_CREATIONFLAGS``),
+and stopping Windows from replacing an unresponsive window with an opaque ghost
+(``disable_windows_app_ghosting``). The Windows-only helpers are no-ops elsewhere.
Background:
The desktop app runs under ``pythonw.exe`` (no attached console). When a
@@ -32,10 +32,47 @@ parameter is silently ignored by the subprocess machinery.
from __future__ import annotations
+import os
import shutil
import subprocess
import sys
from pathlib import Path
+from typing import TextIO
+
+_NULL_STANDARD_STREAMS: list[TextIO] = []
+
+
+def ensure_standard_streams() -> None:
+ """Make ``stdout`` and ``stderr`` safe in a windowed Python process.
+
+ ``pythonw.exe`` and windowed PyInstaller bootloaders expose both streams as
+ ``None``. Libraries such as Uvicorn still call ``sys.stdout.isatty()`` while
+ configuring logging, and our own fatal-startup paths write to stderr. Give
+ those callers a real text stream backed by the platform null device. The
+ desktop file log remains the durable diagnostic surface; this only prevents
+ no-console launches from crashing while trying to report a crash.
+
+ Existing streams are preserved. On Windows, reconfigure them to UTF-8 when
+ the stream supports it, matching the historical launcher behavior.
+ """
+ for name in ("stdout", "stderr"):
+ stream = getattr(sys, name, None)
+ if stream is None:
+ replacement = open( # noqa: SIM115 - intentionally process-lifetime
+ os.devnull,
+ "w",
+ encoding="utf-8",
+ errors="replace",
+ )
+ setattr(sys, name, replacement)
+ _NULL_STANDARD_STREAMS.append(replacement)
+ continue
+ if sys.platform == "win32":
+ try:
+ stream.reconfigure(encoding="utf-8", errors="replace")
+ except (AttributeError, OSError):
+ pass
+
if sys.platform == "win32":
NO_WINDOW_CREATIONFLAGS: int = getattr(subprocess, "CREATE_NO_WINDOW", 0x08000000)
@@ -125,5 +162,6 @@ def disable_windows_app_ghosting() -> bool:
__all__ = [
"NO_WINDOW_CREATIONFLAGS",
"disable_windows_app_ghosting",
+ "ensure_standard_streams",
"resolve_executable",
]
diff --git a/jarvis/ui/desktop_app.py b/jarvis/ui/desktop_app.py
index bb94159930..c9122d9595 100644
--- a/jarvis/ui/desktop_app.py
+++ b/jarvis/ui/desktop_app.py
@@ -27,15 +27,20 @@ from contextlib import suppress
from pathlib import Path
from typing import TYPE_CHECKING, Any
-# Windows UTF-8 fix (analogous to jarvis.__main__)
+from filelock import FileLock, Timeout
+
+from jarvis.core.branding import PRODUCT_NAME as WINDOW_TITLE
+from jarvis.core.config import DATA_DIR, JarvisConfig, load_config
+from jarvis.core.process_utils import ensure_standard_streams
+
+if TYPE_CHECKING:
+ from jarvis.ui.web.server import WebServer
+
+# Direct ``python -m jarvis.ui.desktop_app`` entry points bypass the launcher,
+# so they need the same pythonw/PyInstaller stream repair here as well.
+ensure_standard_streams()
+
if sys.platform == "win32":
- try:
- sys.stdout.reconfigure(encoding="utf-8", errors="replace") # type: ignore[union-attr]
- sys.stderr.reconfigure(encoding="utf-8", errors="replace") # type: ignore[union-attr]
- except (AttributeError, OSError):
- # Nothing to log to yet — this IS the call that makes the streams
- # usable, and no logger exists this early in the process.
- pass
try:
from jarvis.ui.icon_utils import ensure_windows_app_identity
@@ -45,14 +50,6 @@ if sys.platform == "win32":
# not stop the app from starting on any other OS.
pass
-from filelock import FileLock, Timeout
-
-from jarvis.core.branding import PRODUCT_NAME as WINDOW_TITLE
-from jarvis.core.config import DATA_DIR, JarvisConfig, load_config
-
-if TYPE_CHECKING:
- from jarvis.ui.web.server import WebServer
-
# ---------------------------------------------------------------------------
# Constants
diff --git a/jarvis/ui/web/launcher.py b/jarvis/ui/web/launcher.py
index e802a8faeb..f4f50c372d 100644
--- a/jarvis/ui/web/launcher.py
+++ b/jarvis/ui/web/launcher.py
@@ -20,6 +20,8 @@ import sys
import time
from jarvis.core.branding import CONFIG_FILE_NAME
+from jarvis.core.process_utils import ensure_standard_streams
+from jarvis.core.win32_dpi import ensure_dpi_awareness as _ensure_dpi_awareness
# Boot-profiling anchor (opt-in via JARVIS_BOOT_PROFILE=1). ``main()`` stamps the
# earliest in-process moment our code runs; ``_run_headless`` emits one
@@ -29,13 +31,10 @@ from jarvis.core.branding import CONFIG_FILE_NAME
# process. None means "not profiling" → no line is emitted (zero prod change).
_BOOT_PROFILE_T0: float | None = None
-# Windows-UTF8-Fix
-if sys.platform == "win32":
- try:
- sys.stdout.reconfigure(encoding="utf-8", errors="replace")
- sys.stderr.reconfigure(encoding="utf-8", errors="replace")
- except (AttributeError, OSError):
- pass
+# A windowed interpreter (pythonw / a GUI PyInstaller build) has no standard
+# streams. Uvicorn probes stdout while configuring its formatter, so repair the
+# streams before any desktop/backend construction can begin.
+ensure_standard_streams()
# DPI awareness — claim PER_MONITOR_AWARE for the whole process BEFORE anything
# imports pywebview. Windows honours only the FIRST process-awareness claim, and
@@ -46,8 +45,6 @@ if sys.platform == "win32":
# x=-1989 while physically at x=-1326; clicks 1/4 hits vs 4/4 with the early
# claim). Same root as the JarvisBar HiDPI shrink (commit 7a6e7d17). No-op off
# Windows.
-from jarvis.core.win32_dpi import ensure_dpi_awareness as _ensure_dpi_awareness
-
_ensure_dpi_awareness()
# Taskbar-Icon-Fix (Windows): the unique AUMID must be set BEFORE pywebview
diff --git a/tests/unit/core/test_process_utils_standard_streams.py b/tests/unit/core/test_process_utils_standard_streams.py
new file mode 100644
index 0000000000..0ab9e5d4ee
--- /dev/null
+++ b/tests/unit/core/test_process_utils_standard_streams.py
@@ -0,0 +1,66 @@
+"""Regression guards for windowed-Python standard streams."""
+
+from __future__ import annotations
+
+import io
+import sys
+
+from jarvis.core.process_utils import ensure_standard_streams
+
+
+def test_missing_windowed_streams_are_replaced(monkeypatch) -> None:
+ monkeypatch.setattr(sys, "stdout", None)
+ monkeypatch.setattr(sys, "stderr", None)
+
+ ensure_standard_streams()
+
+ stdout = sys.stdout
+ stderr = sys.stderr
+ assert stdout is not None
+ assert stderr is not None
+ assert isinstance(stdout.isatty(), bool)
+ assert isinstance(stderr.isatty(), bool)
+ assert stdout.write("discarded") == len("discarded")
+ assert stderr.write("discarded") == len("discarded")
+ stdout.flush()
+ stderr.flush()
+
+
+def test_existing_streams_are_preserved(monkeypatch) -> None:
+ stdout = io.StringIO()
+ stderr = io.StringIO()
+ monkeypatch.setattr(sys, "stdout", stdout)
+ monkeypatch.setattr(sys, "stderr", stderr)
+
+ ensure_standard_streams()
+
+ assert sys.stdout is stdout
+ assert sys.stderr is stderr
+
+
+def test_stream_repair_is_idempotent(monkeypatch) -> None:
+ monkeypatch.setattr(sys, "stdout", None)
+ monkeypatch.setattr(sys, "stderr", None)
+
+ ensure_standard_streams()
+ stdout = sys.stdout
+ stderr = sys.stderr
+ ensure_standard_streams()
+
+ assert sys.stdout is stdout
+ assert sys.stderr is stderr
+
+
+def test_uvicorn_logging_accepts_repaired_windowed_streams(monkeypatch) -> None:
+ import uvicorn
+
+ monkeypatch.setattr(sys, "stdout", None)
+ monkeypatch.setattr(sys, "stderr", None)
+ ensure_standard_streams()
+
+ async def app(scope, receive, send) -> None:
+ del scope, receive, send
+
+ config = uvicorn.Config(app=app, log_level="warning")
+
+ assert config.log_level == "warning"