Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

perf(desktop): reduce boot imports and pace terminal sockets (#211)

Ruben Lütkef6b6e6d2bb46
diff --git a/jarvis/plugins/brain/_openai_base.py b/jarvis/plugins/brain/_openai_base.py
index 761d472956..6ba19db866 100644
--- a/jarvis/plugins/brain/_openai_base.py
+++ b/jarvis/plugins/brain/_openai_base.py
@@ -29,30 +29,38 @@ log = logging.getLogger(__name__)
CLIENT_TIMEOUT = httpx.Timeout(connect=5.0, read=30.0, write=30.0, pool=30.0)
-def _stream_options_supported() -> bool:
- """One-shot detection: does the installed openai SDK know ``stream_options``?
-
- `stream_options` was introduced in openai>=1.30 (June 2024) — older
- versions raise ``TypeError: got unexpected keyword argument`` directly
- on the call. We check the signature once at module-load time and cache
- the result. For future API changes, the re-try path in
- ``run_openai_chat`` is the safety net.
- """
- try:
- from openai.resources.chat.completions import AsyncCompletions
+_STREAM_OPTIONS_WARNING_EMITTED = False
- sig = inspect.signature(AsyncCompletions.create)
- return "stream_options" in sig.parameters
- except Exception: # noqa: BLE001 — detection must never kill the import
- return False
+def _stream_options_supported(client: Any) -> bool:
+ """Read the concrete client's signature without importing the OpenAI SDK.
-_STREAM_OPTIONS_SUPPORTED = _stream_options_supported()
-if not _STREAM_OPTIONS_SUPPORTED:
- log.warning(
- "openai SDK does not know 'stream_options' — likely openai<1.30. "
- "Provider runs without inline usage tracking. Recommendation: pip install -U openai."
+ Older SDKs reject ``stream_options`` before sending a request. The caller
+ already holds a client, so its ``create`` signature is the relevant
+ capability probe. Opaque wrappers get the option and rely on the existing
+ TypeError retry if they reject it.
+ """
+ global _STREAM_OPTIONS_WARNING_EMITTED
+ try:
+ create = client.chat.completions.create
+ parameters = inspect.signature(create).parameters
+ except (TypeError, ValueError) as exc:
+ log.debug(
+ "OpenAI-compatible client signature unavailable; using optional-kwarg retry: %s",
+ exc,
+ )
+ return True
+ supported = "stream_options" in parameters or any(
+ parameter.kind is inspect.Parameter.VAR_KEYWORD
+ for parameter in parameters.values()
)
+ if not supported and not _STREAM_OPTIONS_WARNING_EMITTED:
+ _STREAM_OPTIONS_WARNING_EMITTED = True
+ log.warning(
+ "OpenAI-compatible client does not accept 'stream_options'. "
+ "Provider runs without inline usage tracking."
+ )
+ return supported
def _tool_output_text(message: BrainMessage) -> str:
@@ -546,7 +554,7 @@ async def stream_complete(
# the unconditional call would raise a TypeError and crash the plugin
# chain with "AsyncCompletions.create() got an unexpected keyword argument"
# — the user then hears the "unreachable" diagnostic instead of an answer.
- if _STREAM_OPTIONS_SUPPORTED:
+ if _stream_options_supported(client):
kwargs["stream_options"] = {"include_usage": True}
# Sanitize tool names to the OpenAI/Anthropic rule and keep a reverse map so
# the model's tool_call resolves back to the ORIGINAL tool name (e.g. the
diff --git a/jarvis/ui/web/frontend/src/components/visualization/OutputPreviewCards.test.tsx b/jarvis/ui/web/frontend/src/components/visualization/OutputPreviewCards.test.tsx
index cdbfe5315d..1a22768c9f 100644
--- a/jarvis/ui/web/frontend/src/components/visualization/OutputPreviewCards.test.tsx
+++ b/jarvis/ui/web/frontend/src/components/visualization/OutputPreviewCards.test.tsx
@@ -128,8 +128,10 @@ describe("OutputPreview cards", () => {
it("shows a short file whole", async () => {
installFetchMock([file("run.sh")], { "run.sh": "#!/bin/sh\n# Start it.\nnpm start\n" });
renderPreview(RUN);
- const card = await screen.findByTestId("output-preview-code-card");
- expect(card.getAttribute("data-open")).toBe("true");
+ const card = await screen.findByTestId("output-preview-code-card", {}, { timeout: 5_000 });
+ await waitFor(() => expect(card.getAttribute("data-open")).toBe("true"), {
+ timeout: 5_000,
+ });
expect(within(card).getByTestId("output-preview-code-source").textContent).toContain(
"npm start",
);
diff --git a/jarvis/ui/web/frontend/src/components/workspace/WorkspaceTerminal.test.tsx b/jarvis/ui/web/frontend/src/components/workspace/WorkspaceTerminal.test.tsx
new file mode 100644
index 0000000000..b87fb7ba0a
--- /dev/null
+++ b/jarvis/ui/web/frontend/src/components/workspace/WorkspaceTerminal.test.tsx
@@ -0,0 +1,92 @@
+import { render } from "@testing-library/react";
+import { act } from "react";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+import { BURST, resetConnectBudgetForTests } from "@/lib/connectBudget";
+
+const sockets = vi.hoisted(() => ({ opened: 0 }));
+
+vi.mock("@xterm/xterm", () => ({
+ Terminal: class {
+ cols = 80;
+ rows = 24;
+ options: Record<string, unknown> = {};
+ loadAddon() {}
+ open() {}
+ write() {}
+ focus() {}
+ onData() { return { dispose() {} }; }
+ dispose() {}
+ },
+}));
+vi.mock("@xterm/addon-fit", () => ({ FitAddon: class { fit() {} } }));
+vi.mock("@xterm/addon-web-links", () => ({ WebLinksAddon: class {} }));
+vi.mock("../agentic/terminalNewline", () => ({
+ installNewlineBridge: () => () => undefined,
+}));
+vi.mock("@/hooks/useTheme", () => ({ useThemeValue: () => "dark" }));
+vi.mock("@/lib/terminalFont", () => ({
+ TERMINAL_FONT_STACK: "monospace",
+ syncTerminalFont: () => () => undefined,
+}));
+vi.mock("@/lib/terminalLinks", () => ({
+ activateTerminalLink: () => undefined,
+ TERMINAL_OSC_LINK_HANDLER: {},
+}));
+
+import { WorkspaceTerminal } from "./WorkspaceTerminal";
+
+class ResizeObserverHarness {
+ observe() {}
+ disconnect() {}
+}
+
+class WebSocketHarness {
+ static OPEN = 1;
+ readyState = 0;
+ onopen: (() => void) | null = null;
+ onmessage: ((event: MessageEvent) => void) | null = null;
+ onerror: (() => void) | null = null;
+ onclose: ((event: CloseEvent) => void) | null = null;
+ constructor(_url: string) { sockets.opened += 1; }
+ send() {}
+ close() {}
+}
+
+describe("WorkspaceTerminal connection budget", () => {
+ beforeEach(() => {
+ vi.useFakeTimers();
+ resetConnectBudgetForTests();
+ sockets.opened = 0;
+ vi.stubGlobal("ResizeObserver", ResizeObserverHarness);
+ vi.stubGlobal("WebSocket", WebSocketHarness);
+ });
+
+ afterEach(() => {
+ resetConnectBudgetForTests();
+ vi.useRealTimers();
+ vi.unstubAllGlobals();
+ });
+
+ it("paces a burst of workspace terminal mounts", () => {
+ const panes = Array.from({ length: 20 }, (_, index) =>
+ render(<WorkspaceTerminal paneKey={`pane-${index}`} title={`Pane ${index}`} />),
+ );
+ act(() => vi.advanceTimersByTime(0));
+ expect(sockets.opened).toBe(BURST);
+ act(() => vi.advanceTimersByTime(5_000));
+ expect(sockets.opened).toBe(20);
+ panes.forEach((pane) => pane.unmount());
+ });
+
+ it("cancels queued connects when panes unmount", () => {
+ const panes = Array.from({ length: 20 }, (_, index) =>
+ render(<WorkspaceTerminal paneKey={`pane-${index}`} title={`Pane ${index}`} />),
+ );
+ act(() => vi.advanceTimersByTime(0));
+ expect(sockets.opened).toBe(BURST);
+ panes.forEach((pane) => pane.unmount());
+ act(() => vi.advanceTimersByTime(5_000));
+ expect(sockets.opened).toBe(BURST);
+ });
+});
diff --git a/jarvis/ui/web/frontend/src/components/workspace/WorkspaceTerminal.tsx b/jarvis/ui/web/frontend/src/components/workspace/WorkspaceTerminal.tsx
index d40bd2e765..129edf42f2 100644
--- a/jarvis/ui/web/frontend/src/components/workspace/WorkspaceTerminal.tsx
+++ b/jarvis/ui/web/frontend/src/components/workspace/WorkspaceTerminal.tsx
@@ -21,6 +21,7 @@ import {
} from "../agentic/terminalThemes";
import { useThemeValue } from "@/hooks/useTheme";
import { TERMINAL_FONT_STACK, syncTerminalFont } from "@/lib/terminalFont";
+import { requestConnect } from "@/lib/connectBudget";
import {
activateTerminalLink,
TERMINAL_OSC_LINK_HANDLER,
@@ -122,6 +123,7 @@ export function WorkspaceTerminal({
}
let ws: WebSocket | null = null;
+ let cancelConnect: () => void = () => {};
let disposed = false;
let everLive = false;
@@ -154,63 +156,74 @@ export function WorkspaceTerminal({
if (agentName) params.agent = agentName;
else if (installName) params.install = installName;
- ws = new WebSocket(buildUrl(paneKey, params));
- ws.onopen = () => {
- setStatus("connecting");
- // Push the ACTUAL pane size to the PTY now that we can send. The spawn
- // used a best-effort size (the mount-time fit often runs before the
- // grid cell is measured), and resizes fired while the socket was still
- // connecting were dropped — so without this the agent's full-screen TUI
- // keeps drawing at the wrong dimensions (cramped / clipped on the
- // right). A second deferred fit catches any late grid layout.
- sendResize();
- requestAnimationFrame(sendResize);
- };
- ws.onmessage = (ev) => {
- let msg: { t?: string; d?: string; code?: number; message?: string };
+ // Workspace panes share the same connection budget as the app and IDE
+ // sockets. A grid mounting at once must not bypass the wake-storm cap.
+ cancelConnect = requestConnect(() => {
+ if (disposed) return;
try {
- msg = JSON.parse(ev.data as string);
+ ws = new WebSocket(buildUrl(paneKey, params));
} catch {
+ setStatus("error");
+ setError("Connection to the terminal failed.");
return;
}
- if (msg.t === "o") term.write(msg.d ?? "");
- else if (msg.t === "ready") {
- everLive = true;
- setStatus("live");
- term.focus();
- } else if (msg.t === "exit") {
- setStatus("exited");
- term.write(`\r\n\x1b[33m[process exited: ${msg.code ?? "?"}]\x1b[0m\r\n`);
- } else if (msg.t === "error") {
+ ws.onopen = () => {
+ setStatus("connecting");
+ // Push the ACTUAL pane size to the PTY now that we can send. The spawn
+ // used a best-effort size (the mount-time fit often runs before the
+ // grid cell is measured), and resizes fired while the socket was still
+ // connecting were dropped — so without this the agent's full-screen TUI
+ // keeps drawing at the wrong dimensions (cramped / clipped on the
+ // right). A second deferred fit catches any late grid layout.
+ sendResize();
+ requestAnimationFrame(sendResize);
+ };
+ ws.onmessage = (ev) => {
+ let msg: { t?: string; d?: string; code?: number; message?: string };
+ try {
+ msg = JSON.parse(ev.data as string);
+ } catch {
+ return;
+ }
+ if (msg.t === "o") term.write(msg.d ?? "");
+ else if (msg.t === "ready") {
+ everLive = true;
+ setStatus("live");
+ term.focus();
+ } else if (msg.t === "exit") {
+ setStatus("exited");
+ term.write(`\r\n\x1b[33m[process exited: ${msg.code ?? "?"}]\x1b[0m\r\n`);
+ } else if (msg.t === "error") {
+ setStatus("error");
+ setError(msg.message ?? "terminal error");
+ }
+ };
+ ws.onerror = () => {
setStatus("error");
- setError(msg.message ?? "terminal error");
- }
- };
- ws.onerror = () => {
- setStatus("error");
- setError("Connection to the terminal failed.");
- };
- ws.onclose = (ev) => {
- if (everLive) {
- setStatus("exited");
- } else if (!disposed) {
- // Closed during the handshake (e.g. 4401 auth) — surface it honestly
- // instead of hanging on "connecting" forever (mirrors PtyTerminal).
- setStatus("error");
- setError((e) =>
- e ??
- (ev.code === 4401
- ? "Terminal authorization failed — reopen to retry."
- : `Terminal connection closed (code ${ev.code || "?"}).`),
- );
- }
- };
+ setError("Connection to the terminal failed.");
+ };
+ ws.onclose = (ev) => {
+ if (everLive) {
+ setStatus("exited");
+ } else if (!disposed) {
+ // Closed during the handshake (e.g. 4401 auth) — surface it honestly
+ // instead of hanging on "connecting" forever (mirrors PtyTerminal).
+ setStatus("error");
+ setError((e) =>
+ e ??
+ (ev.code === 4401
+ ? "Terminal authorization failed — reopen to retry."
+ : `Terminal connection closed (code ${ev.code || "?"}).`),
+ );
+ }
+ };
- term.onData((data) => {
- if (ws && ws.readyState === WebSocket.OPEN) {
- ws.send(JSON.stringify({ t: "i", d: data }));
- }
- });
+ term.onData((data) => {
+ if (ws && ws.readyState === WebSocket.OPEN) {
+ ws.send(JSON.stringify({ t: "i", d: data }));
+ }
+ });
+ }, 0);
}
window.addEventListener("resize", sendResize);
@@ -219,6 +232,7 @@ export function WorkspaceTerminal({
return () => {
disposed = true;
+ cancelConnect();
window.removeEventListener("resize", sendResize);
ro.disconnect();
disposeFontSync();
diff --git a/jarvis/ui/web/missions_ws_routes.py b/jarvis/ui/web/missions_ws_routes.py
index be0027aa95..5dafff7a78 100644
--- a/jarvis/ui/web/missions_ws_routes.py
+++ b/jarvis/ui/web/missions_ws_routes.py
@@ -20,9 +20,11 @@ sub-mission tasks may emit bursts (worker spawn + worker progress +
critic verdict all at once). A window size of 200 covers a few seconds of
backlog before the first drop.
"""
+
from __future__ import annotations
import asyncio
+import contextlib
import logging
from typing import TYPE_CHECKING, Any
from uuid import uuid4
@@ -85,9 +87,7 @@ class ConnectionManager:
and merge it after the replay, skipping anything the replay already
covers so the client never sees a duplicate.
"""
- queue: asyncio.Queue[EventEnvelope | dict[str, Any]] = asyncio.Queue(
- maxsize=_QUEUE_MAXSIZE
- )
+ queue: asyncio.Queue[EventEnvelope | dict[str, Any]] = asyncio.Queue(maxsize=_QUEUE_MAXSIZE)
async with self._lock:
self._clients[client_id] = queue
@@ -99,15 +99,9 @@ class ConnectionManager:
if isinstance(item, EventEnvelope):
live_during_replay.append(item)
- max_replayed_seq = (
- replay[-1].seq
- if replay and replay[-1].seq is not None
- else last_seq
- )
+ max_replayed_seq = replay[-1].seq if replay and replay[-1].seq is not None else last_seq
merged = list(replay) + [
- env
- for env in live_during_replay
- if env.seq is not None and env.seq > max_replayed_seq
+ env for env in live_during_replay if env.seq is not None and env.seq > max_replayed_seq
]
for env in merged:
self._enqueue(client_id, queue, env)
@@ -197,9 +191,7 @@ async def _drain_client_frames(ws: WebSocket) -> None:
# ("WebSocket is not connected ...") instead of
# WebSocketDisconnect. `continue` here would re-poll the dead
# socket forever — treat it as terminal.
- logger.debug(
- "missions_ws: reader socket error (%s) — closing", exc
- )
+ logger.debug("missions_ws: reader socket error (%s) — closing", exc)
return
except ValueError as exc:
# Malformed JSON (json.JSONDecodeError is a ValueError) on an
@@ -210,9 +202,7 @@ async def _drain_client_frames(ws: WebSocket) -> None:
)
continue
except Exception as exc: # noqa: BLE001
- logger.warning(
- "missions_ws: reader unexpected error (%s) — closing", exc
- )
+ logger.warning("missions_ws: reader unexpected error (%s) — closing", exc)
return
# Reserved for future control frames (pause/resume etc.).
if isinstance(msg, dict) and msg.get("type") == "ping":
@@ -243,9 +233,7 @@ async def missions_ws(ws: WebSocket) -> None:
# 1. Hello frame (5s timeout).
try:
- first = await asyncio.wait_for(
- ws.receive_json(), timeout=_HELLO_TIMEOUT_S
- )
+ first = await asyncio.wait_for(ws.receive_json(), timeout=_HELLO_TIMEOUT_S)
except TimeoutError:
await ws.close(code=4400, reason="hello timeout")
return
@@ -279,10 +267,21 @@ async def missions_ws(ws: WebSocket) -> None:
_drain_client_frames(ws), name=f"missions_ws-reader-{client_id[:8]}"
)
- # 3. Writer loop. Stop on WS disconnect.
+ # 3. Writer loop. Wake on either an event or an idle client disconnect.
try:
while True:
- env = await queue.get()
+ get_task = asyncio.create_task(queue.get())
+ try:
+ done, _ = await asyncio.wait(
+ {reader_task, get_task}, return_when=asyncio.FIRST_COMPLETED
+ )
+ if reader_task in done:
+ break
+ env = get_task.result()
+ finally:
+ get_task.cancel()
+ with contextlib.suppress(asyncio.CancelledError):
+ await get_task
frame = env.model_dump(mode="json") if isinstance(env, EventEnvelope) else env
try:
await ws.send_json(frame)
@@ -299,13 +298,16 @@ async def missions_ws(ws: WebSocket) -> None:
reader_task.cancel()
try:
await reader_task
- except (asyncio.CancelledError, Exception): # noqa: BLE001
- pass
+ except asyncio.CancelledError:
+ # Expected when the writer exits before the client reader.
+ pass # noqa: S110
+ except Exception:
+ logger.exception("missions_ws: reader failed for client=%s", client_id)
await conn_mgr.disconnect(client_id)
try:
await ws.close()
- except Exception: # noqa: BLE001
- pass
+ except Exception as exc: # noqa: BLE001
+ logger.debug("missions_ws: close failed for client=%s: %s", client_id, exc)
__all__ = ["ConnectionManager", "router"]
diff --git a/scripts/perf_live_snapshot.py b/scripts/perf_live_snapshot.py
new file mode 100644
index 0000000000..f43759d916
--- /dev/null
+++ b/scripts/perf_live_snapshot.py
@@ -0,0 +1,269 @@
+"""Read-only resource snapshot of a running Jarvis desktop session.
+
+Usage::
+
+ python scripts/perf_live_snapshot.py --seconds 60 --interval 5
+ python scripts/perf_live_snapshot.py --pid 1234 --seconds 30 --json out.json
+ python scripts/perf_live_snapshot.py --pid 1234 --health-url http://127.0.0.1:47821/api/health
+
+The process tree is sampled without starting, stopping, or modifying processes.
+CPU percentages use one logical core as 100%; divide by ``logical_cpus`` for
+whole-machine percentage. RSS is resident memory and is not additive across
+shared pages. On Windows, commit is psutil's pagefile (private committed bytes);
+other platforms report it as unavailable. HTTP probes are optional GETs to a
+caller-supplied health URL, not representative chat or voice workflow latency.
+"""
+
+from __future__ import annotations
+
+import argparse
+import json
+import statistics
+import sys
+import time
+import urllib.request
+from datetime import UTC, datetime
+from pathlib import Path
+from typing import Any
+
+import psutil # type: ignore[import-untyped]
+
+
+def _identity(proc: psutil.Process) -> tuple[int, float]:
+ return proc.pid, proc.create_time()
+
+
+def _backend_pids() -> list[int]:
+ result = []
+ for proc in psutil.process_iter(["cmdline"]):
+ try:
+ args = proc.info["cmdline"] or []
+ if any("jarvis.ui.web.launcher" in arg for arg in args):
+ result.append(proc.pid)
+ except (psutil.Error, OSError):
+ continue
+ return result
+
+
+def _role(proc: psutil.Process, root_pid: int) -> str:
+ if proc.pid == root_pid:
+ return "backend"
+ try:
+ name = proc.name().lower()
+ args = proc.cmdline()
+ except (psutil.Error, OSError):
+ return "unknown"
+ if name == "msedgewebview2.exe":
+ kind = next((arg.partition("=")[2] for arg in args if arg.startswith("--type=")), "host")
+ return f"webview:{kind}"
+ if any("local_realtime" in arg for arg in args):
+ return "local-realtime"
+ if any("dictation" in arg.lower() and "preview" in arg.lower() for arg in args):
+ return "dictation-preview"
+ if any(token in name for token in ("codex", "claude", "antigravity", "gemini", "agy")):
+ return "coding-agent"
+ if name in {"ollama.exe", "ollama", "llama-server.exe", "llama-server"}:
+ return "model-server"
+ if name in {"npm", "npm.cmd", "npm.exe", "vite", "vite.exe"}:
+ return "build"
+ if name in {"node.exe", "node"} and any("vite" in arg.lower() for arg in args):
+ return "build"
+ return "other-child"
+
+
+def _snapshot(proc: psutil.Process, root_pid: int, scope: str) -> dict[str, Any] | None:
+ try:
+ mem = proc.memory_info()
+ cpu = proc.cpu_times()
+ connections: dict[str, int] | None = None
+ try:
+ connections = {"established": 0, "listen": 0, "other": 0}
+ for conn in proc.net_connections(kind="inet"):
+ status = conn.status.lower()
+ key = status if status in ("established", "listen") else "other"
+ connections[key] += 1
+ except (psutil.AccessDenied, OSError, NotImplementedError):
+ pass # Permission or platform does not expose per-process sockets.
+ return {
+ "pid": proc.pid,
+ "created": proc.create_time(),
+ "role": _role(proc, root_pid),
+ "scope": scope,
+ "cpu_seconds": cpu.user + cpu.system,
+ "rss_mb": round(mem.rss / 1e6, 2),
+ "commit_mb": round(mem.pagefile / 1e6, 2)
+ if sys.platform == "win32" and hasattr(mem, "pagefile")
+ else None,
+ "connections": connections,
+ }
+ except (psutil.Error, OSError):
+ return None
+
+
+def _health_probe(url: str) -> dict[str, Any]:
+ start = time.perf_counter()
+ try:
+ with urllib.request.urlopen(url, timeout=2.0) as response: # noqa: S310
+ response.read(4096)
+ return {
+ "status": response.status,
+ "latency_ms": round((time.perf_counter() - start) * 1000, 2),
+ }
+ except (OSError, ValueError) as exc:
+ return {
+ "error": type(exc).__name__,
+ "latency_ms": round((time.perf_counter() - start) * 1000, 2),
+ }
+
+
+def _capture(root: psutil.Process) -> dict[tuple[int, float], dict[str, Any]]:
+ try:
+ family = [root, *root.children(recursive=True)]
+ except psutil.Error:
+ family = [root]
+ family_pids = {proc.pid for proc in family}
+ records = [_snapshot(proc, root.pid, "jarvis-family") for proc in family]
+ # Keep unrelated coding agents, builds, model servers and other Jarvis
+ # instances visible without charging their resource use to this backend.
+ for proc in psutil.process_iter(["name", "cmdline"]):
+ if proc.pid in family_pids:
+ continue
+ role = _role(proc, root.pid)
+ if role in {"coding-agent", "model-server", "build", "dictation-preview", "local-realtime"}:
+ records.append(_snapshot(proc, root.pid, "external"))
+ return {(row["pid"], row["created"]): row for row in records if row is not None}
+
+
+def measure(
+ root_pid: int, seconds: float, interval: float, health_url: str | None
+) -> dict[str, Any]:
+ root = psutil.Process(root_pid)
+ root_identity = _identity(root)
+ start = time.perf_counter()
+ previous_time = start
+ previous = _capture(root)
+ samples: list[dict[str, Any]] = []
+ probes: list[dict[str, Any]] = []
+ while time.perf_counter() - start < seconds:
+ time.sleep(min(interval, max(0, seconds - (time.perf_counter() - start))))
+ now = time.perf_counter()
+ current = _capture(root)
+ if _identity(root) != root_identity:
+ raise RuntimeError("Jarvis backend exited or PID was reused during sampling")
+ wall = now - previous_time
+ rows = []
+ for key, row in current.items():
+ old = previous.get(key)
+ if old is None:
+ continue # A new process has no CPU baseline for this interval.
+ measured = {k: v for k, v in row.items() if k != "cpu_seconds"}
+ measured["cpu_one_core_pct"] = round(
+ max(0, row["cpu_seconds"] - old["cpu_seconds"]) / wall * 100, 2
+ )
+ rows.append(measured)
+ samples.append(
+ {
+ "elapsed_seconds": round(now - start, 2),
+ "window_seconds": round(wall, 2),
+ "processes": rows,
+ }
+ )
+ if health_url:
+ probes.append(_health_probe(health_url))
+ previous, previous_time = current, now
+ return {
+ "timestamp_utc": datetime.now(UTC).isoformat(),
+ "platform": sys.platform,
+ "logical_cpus": psutil.cpu_count(logical=True),
+ "root_pid": root_pid,
+ "root_created": root_identity[1],
+ "other_backend_pids": [pid for pid in _backend_pids() if pid != root_pid],
+ "duration_seconds": round(time.perf_counter() - start, 2),
+ "samples": samples,
+ "health_probes": probes,
+ }
+
+
+def _summary(result: dict[str, Any]) -> str:
+ samples = result["samples"]
+ roles: dict[str, list[dict[str, Any]]] = {}
+ for sample in samples:
+ grouped: dict[str, list[dict[str, Any]]] = {}
+ for row in sample["processes"]:
+ grouped.setdefault(f"{row['scope']}/{row['role']}", []).append(row)
+ for role, rows in grouped.items():
+ commit = [row["commit_mb"] for row in rows]
+ connections = [row["connections"] for row in rows if row["connections"]]
+ roles.setdefault(role, []).append(
+ {
+ "cpu_one_core_pct": sum(row["cpu_one_core_pct"] for row in rows),
+ "rss_mb": sum(row["rss_mb"] for row in rows),
+ "commit_mb": round(sum(commit), 2)
+ if all(value is not None for value in commit)
+ else None,
+ "connections": {
+ key: sum(conn[key] for conn in connections)
+ for key in ("established", "listen", "other")
+ }
+ if connections
+ else None,
+ "count": len(rows),
+ }
+ )
+ lines = [
+ (
+ f"backend={result['root_pid']} duration={result['duration_seconds']}s "
+ f"logical_cpus={result['logical_cpus']}"
+ ),
+ f"other_backends={result['other_backend_pids']}",
+ "scope/role count CPU%/core avg/max RSS MB commit MB TCP",
+ ]
+ for role, rows in sorted(roles.items()):
+ cpus = [row["cpu_one_core_pct"] for row in rows]
+ latest = rows[-1]
+ conn = latest["connections"]
+ lines.append(
+ f"{role:<33} {latest['count']:5d} {statistics.mean(cpus):6.2f}/{max(cpus):6.2f}"
+ f" {latest['rss_mb']:7.1f} {str(latest['commit_mb']):>10} {str(conn):>20}"
+ )
+ if result["health_probes"]:
+ ok = [p["latency_ms"] for p in result["health_probes"] if p.get("status") == 200]
+ median = statistics.median(ok) if ok else "unavailable"
+ lines.append(
+ f"health GET: {len(ok)}/{len(result['health_probes'])} HTTP 200, "
+ f"latency median={median} ms"
+ )
+ return "\n".join(lines)
+
+
+def main() -> int:
+ parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0])
+ parser.add_argument(
+ "--pid", type=int, help="Jarvis backend PID; required when multiple are running"
+ )
+ parser.add_argument("--seconds", type=float, default=60.0)
+ parser.add_argument("--interval", type=float, default=5.0)
+ parser.add_argument(
+ "--health-url",
+ help="optional GET URL for health latency, e.g. http://127.0.0.1:47821/api/health",
+ )
+ parser.add_argument("--json", type=Path, help="write raw samples to JSON")
+ args = parser.parse_args()
+ if args.seconds <= 0 or args.interval <= 0:
+ parser.error("--seconds and --interval must be positive")
+ backends = _backend_pids()
+ if args.pid is None and len(backends) != 1:
+ parser.error(f"expected one Jarvis backend; found {backends}. Pass --pid")
+ pid = args.pid if args.pid is not None else backends[0]
+ try:
+ result = measure(pid, args.seconds, args.interval, args.health_url)
+ except (psutil.Error, RuntimeError) as exc:
+ parser.exit(2, f"sampling failed: {exc}\n")
+ print(_summary(result))
+ if args.json:
+ args.json.write_text(json.dumps(result, indent=2), encoding="utf-8")
+ return 0
+
+
+if __name__ == "__main__":
+ raise SystemExit(main())
diff --git a/tests/missions/api/test_missions_ws.py b/tests/missions/api/test_missions_ws.py
index 52b5bdf693..c71924a7a2 100644
--- a/tests/missions/api/test_missions_ws.py
+++ b/tests/missions/api/test_missions_ws.py
@@ -1,7 +1,9 @@
"""WebSocket tests for /api/missions/ws (hello + replay + live fanout)."""
+
from __future__ import annotations
import asyncio
+import time
from pathlib import Path
import pytest
@@ -13,14 +15,17 @@ from starlette.websockets import WebSocketDisconnect
from jarvis.missions.events import EventEnvelope, MissionDispatched
from jarvis.missions.manager import MissionManager
from jarvis.ui.web.missions_auth import (
- issue_token,
reset_tokens,
+)
+from jarvis.ui.web.missions_auth import (
router as missions_auth_router,
)
from jarvis.ui.web.missions_routes import router as missions_router
from jarvis.ui.web.missions_ws_routes import (
ConnectionManager,
_drain_client_frames,
+)
+from jarvis.ui.web.missions_ws_routes import (
router as missions_ws_router,
)
@@ -104,16 +109,12 @@ def test_ws_closes_4401_on_invalid_token(app: FastAPI) -> None:
with TestClient(app) as client:
with pytest.raises(WebSocketDisconnect) as exc_info:
with client.websocket_connect("/api/missions/ws") as ws:
- ws.send_json(
- {"type": "hello", "last_seq": 0, "token": "bogus"}
- )
+ ws.send_json({"type": "hello", "last_seq": 0, "token": "bogus"})
ws.receive_json()
assert exc_info.value.code == 4401
-def test_ws_accepts_valid_token_and_streams_replay(
- app: FastAPI, manager: MissionManager
-) -> None:
+def test_ws_accepts_valid_token_and_streams_replay(app: FastAPI, manager: MissionManager) -> None:
"""Dispatch a mission BEFORE the WS connect → replay delivers it."""
with TestClient(app) as client:
d1 = client.post("/api/missions/dispatch", json={"prompt": "first"})
@@ -133,9 +134,7 @@ def test_ws_accepts_valid_token_and_streams_replay(
assert f2["payload"]["prompt"] == "second"
-def test_ws_replay_respects_last_seq(
- app: FastAPI, manager: MissionManager
-) -> None:
+def test_ws_replay_respects_last_seq(app: FastAPI, manager: MissionManager) -> None:
"""``last_seq=1`` skips the first event and delivers only the second."""
with TestClient(app) as client:
client.post("/api/missions/dispatch", json={"prompt": "first"})
@@ -149,9 +148,7 @@ def test_ws_replay_respects_last_seq(
assert frame["payload"]["prompt"] == "second"
-def test_ws_live_fanout_after_connect(
- app: FastAPI, manager: MissionManager
-) -> None:
+def test_ws_live_fanout_after_connect(app: FastAPI, manager: MissionManager) -> None:
"""Dispatch AFTER the connect → the event lands via fanout at the client."""
with TestClient(app) as client:
token = client.get("/api/missions/auth/token").json()["token"]
@@ -159,9 +156,7 @@ def test_ws_live_fanout_after_connect(
with client.websocket_connect("/api/missions/ws") as ws:
ws.send_json({"type": "hello", "last_seq": 0, "token": token})
# Now trigger a mission → the live frame must arrive
- d = client.post(
- "/api/missions/dispatch", json={"prompt": "live!"}
- )
+ d = client.post("/api/missions/dispatch", json={"prompt": "live!"})
assert d.status_code == 201
mid = d.json()["mission_id"]
frame = ws.receive_json()
@@ -170,6 +165,24 @@ def test_ws_live_fanout_after_connect(
assert frame["payload"]["prompt"] == "live!"
+def test_ws_idle_disconnect_unregisters_client(app: FastAPI) -> None:
+ """An idle closed socket must not retain its queue until another event arrives."""
+ with TestClient(app) as client:
+ token = client.get("/api/missions/auth/token").json()["token"]
+ conn_mgr = app.state.missions_ws_manager
+ with client.websocket_connect("/api/missions/ws") as ws:
+ ws.send_json({"type": "hello", "last_seq": 0, "token": token})
+ deadline = time.monotonic() + 1.0
+ while conn_mgr.client_count != 1 and time.monotonic() < deadline:
+ time.sleep(0.01)
+ assert conn_mgr.client_count == 1
+ ws.close()
+ deadline = time.monotonic() + 0.5
+ while conn_mgr.client_count and time.monotonic() < deadline:
+ time.sleep(0.01)
+ assert conn_mgr.client_count == 0
+
+
# ---------------------------------------------------------------------------
# FIX 1 — AP-20: the client-frame reader must never spin on a dead socket
# ---------------------------------------------------------------------------
diff --git a/tests/unit/plugins/brain/test_openai_base_stream_options_lazy.py b/tests/unit/plugins/brain/test_openai_base_stream_options_lazy.py
new file mode 100644
index 0000000000..038d409eb4
--- /dev/null
+++ b/tests/unit/plugins/brain/test_openai_base_stream_options_lazy.py
@@ -0,0 +1,97 @@
+"""The OpenAI SDK capability probe runs only when a provider is used."""
+
+from __future__ import annotations
+
+import subprocess
+import sys
+from types import SimpleNamespace
+from typing import Any
+
+from jarvis.core.process_utils import NO_WINDOW_CREATIONFLAGS
+from jarvis.core.protocols import BrainMessage, BrainRequest
+from jarvis.plugins.brain import _openai_base
+
+
+class _Client:
+ def __init__(self) -> None:
+ self.calls: list[dict[str, Any]] = []
+ self.chat = SimpleNamespace(completions=SimpleNamespace(create=self._create))
+
+ async def _create(self, **kwargs: Any) -> Any:
+ self.calls.append(kwargs)
+
+ async def _empty_stream() -> Any:
+ if False: # pragma: no cover - marks this as an async generator
+ yield None
+
+ return _empty_stream()
+
+
+async def _drain(client: _Client) -> None:
+ req = BrainRequest(messages=(BrainMessage(role="user", content="hello"),))
+ async for _ in _openai_base.stream_complete(client, "test-model", req):
+ pass
+
+
+def test_module_import_does_not_import_openai_sdk() -> None:
+ code = (
+ "import sys; "
+ "import jarvis.plugins.brain._openai_base as module; "
+ "assert 'openai' not in sys.modules; "
+ "assert callable(module._stream_options_supported)"
+ )
+ result = subprocess.run(
+ [sys.executable, "-c", code],
+ capture_output=True,
+ text=True,
+ encoding="utf-8",
+ creationflags=NO_WINDOW_CREATIONFLAGS,
+ check=False,
+ )
+ assert result.returncode == 0, result.stderr
+
+
+async def test_provider_call_uses_concrete_client_capability() -> None:
+ client = _Client()
+
+ await _drain(client)
+ await _drain(client)
+
+ assert [call["stream_options"] for call in client.calls] == [
+ {"include_usage": True},
+ {"include_usage": True},
+ ]
+
+
+async def test_unsupported_sdk_omits_usage_option() -> None:
+ client = _Client()
+
+ async def _old_create(
+ *,
+ model: str,
+ messages: list[Any],
+ max_tokens: int,
+ temperature: float,
+ stream: bool,
+ ) -> Any:
+ client.calls.append(
+ {
+ "model": model,
+ "messages": messages,
+ "max_tokens": max_tokens,
+ "temperature": temperature,
+ "stream": stream,
+ }
+ )
+
+ async def _empty_stream() -> Any:
+ if False: # pragma: no cover - marks this as an async generator
+ yield None
+
+ return _empty_stream()
+
+ client.chat.completions.create = _old_create
+
+ await _drain(client)
+
+ assert "stream_options" not in client.calls[0]
diff --git a/tests/unit/scripts/test_perf_live_snapshot.py b/tests/unit/scripts/test_perf_live_snapshot.py
new file mode 100644
index 0000000000..6f35385782
--- /dev/null
+++ b/tests/unit/scripts/test_perf_live_snapshot.py
@@ -0,0 +1,55 @@
+"""Focused checks for the read-only live resource sampler."""
+
+from scripts.perf_live_snapshot import _role, _summary
+
+
+class _Process:
+ def __init__(self, pid: int, name: str, args: list[str]) -> None:
+ self.pid = pid
+ self._name = name
+ self._args = args
+
+ def name(self) -> str:
+ return self._name
+
+ def cmdline(self) -> list[str]:
+ return self._args
+
+
+def test_roles_keep_hosted_agent_and_webview_separate() -> None:
+ assert _role(_Process(10, "python.exe", []), 10) == "backend"
+ assert _role(_Process(11, "msedgewebview2.exe", ["--type=gpu-process"]), 10) == (
+ "webview:gpu-process"
+ )
+ assert _role(_Process(12, "codex.exe", []), 10) == "coding-agent"
+
+
+def test_summary_aggregates_multiple_processes_in_one_role() -> None:
+ def row(pid: int, cpu: float, rss: float) -> dict:
+ return {
+ "pid": pid,
+ "scope": "jarvis-family",
+ "role": "webview:renderer",
+ "cpu_one_core_pct": cpu,
+ "rss_mb": rss,
+ "commit_mb": 20.0,
+ "connections": {"established": 1, "listen": 0, "other": 0},
+ }
+
+ result = {
+ "root_pid": 10,
+ "duration_seconds": 2.0,
+ "logical_cpus": 16,
+ "other_backend_pids": [20],
+ "health_probes": [],
+ "samples": [
+ {"processes": [row(11, 2, 100), row(12, 3, 200)]},
+ {"processes": [row(11, 4, 110), row(12, 6, 210)]},
+ ],
+ }
+ summary = _summary(result)
+ line = next(line for line in summary.splitlines() if "webview:renderer" in line)
+ assert "2 " in line
+ assert "7.50/ 10.00" in line
+ assert "320.0" in line
+ assert "40.0" in line