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.
Python agent-orchestrationai-agentai-coding-assistantclaude-codecoding-agentcomputer-usedesktop-applocal-firstmcppersonal-jarvisvoice-assistantvoice-coding
perf(missions): run worktree git work off the event loop
rubenluetke10-beepfdc0bcfcedcc
diff --git a/jarvis/missions/kontrollierer/orchestrator.py b/jarvis/missions/kontrollierer/orchestrator.pyindex 2bea6598f2..2b0ce221a7 100644--- a/jarvis/missions/kontrollierer/orchestrator.py+++ b/jarvis/missions/kontrollierer/orchestrator.py@@ -28,6 +28,7 @@ import reimport shutilimport subprocessimport tempfile+import threadingimport timefrom collections.abc import Callablefrom contextlib import suppress@@ -748,6 +749,13 @@ class TaskOutcome:class Kontrollierer:"""Orchestrator class that drives a mission end-to-end."""+ #: Worktree create/remove run git in a worker thread (off the shared event+ #: loop) but stay serialized, as they were while they blocked the loop:+ #: parallel ``git worktree add`` on one repo races for its admin files.+ #: Class-wide because every instance works on the same checkout; a thread+ #: lock because only worker threads ever take it.+ _worktree_lock: Final[threading.Lock] = threading.Lock()+def __init__(self,*,@@ -1167,7 +1175,14 @@ class Kontrollierer:# the whole codebase first. The AGENTS.md contract is# materialised into BOTH shapes identically (below), and# `_capture_diff` works against both because each has a HEAD.- worktree = self._worktrees.create(+ #+ # Off the loop: a full worktree of this repo is several git+ # subprocesses and thousands of file writes. Run inline it+ # froze every route and WebSocket of the desktop app for 15 s++ # (loop-watchdog stack 2026-10-02: worktree.create ->+ # _write_base_sha -> CreateProcess).+ worktree = await asyncio.to_thread(+ self._create_worktree,mission_slug=_short_slug(mission_prompt),task_id=step.task_id,needs_repo=step.needs_repo,@@ -1217,30 +1232,49 @@ class Kontrollierer:reflections=reflections,)finally:- # Persist worker artifacts BEFORE the worktree teardown.- # Without this step the worktree (and everything the agent- # wrote into it) is gone the moment the task finishes,- # leaving the "outputs folder" the user expects in the- # sub-agents-outputs sidebar permanently empty even on a- # successful mission.- try:- self._archive_task_artifacts(+ # Archive + teardown run in a worker thread (git and file+ # copies would otherwise stall the loop) and are shielded: a+ # second cancellation interrupts only this wait, never the+ # cleanup, which the thread always finishes.+ await asyncio.shield(+ asyncio.to_thread(+ self._finish_task_workspace,worktree=worktree,mission_dir=mission_dir,task_id=step.task_id,)- except Exception: # noqa: BLE001- logger.warning(- "artifact archive failed for %s", worktree, exc_info=True- )- # Worktree cleanup MUST run for every return path so we don't- # leak per-task git worktrees on disk (Phase-6 hard rule #4).- try:- self._worktrees.remove(worktree, force=True)- except Exception: # noqa: BLE001- logger.warning(- "worktree cleanup failed for %s", worktree, exc_info=True- )+ )++ def _create_worktree(self, **kwargs: Any) -> Path:+ """``WorktreeManager.create``, one at a time. Worker thread only."""+ with self._worktree_lock:+ return self._worktrees.create(**kwargs)++ def _finish_task_workspace(+ self, *, worktree: Path, mission_dir: Path, task_id: str+ ) -> None:+ """Archive a task's artifacts, then remove its worktree. Worker thread only."""+ # Persist worker artifacts BEFORE the worktree teardown.+ # Without this step the worktree (and everything the agent+ # wrote into it) is gone the moment the task finishes,+ # leaving the "outputs folder" the user expects in the+ # sub-agents-outputs sidebar permanently empty even on a+ # successful mission.+ try:+ self._archive_task_artifacts(+ worktree=worktree,+ mission_dir=mission_dir,+ task_id=task_id,+ )+ except Exception: # noqa: BLE001+ logger.warning("artifact archive failed for %s", worktree, exc_info=True)+ # Worktree cleanup MUST run for every return path so we don't+ # leak per-task git worktrees on disk (Phase-6 hard rule #4).+ try:+ with self._worktree_lock:+ self._worktrees.remove(worktree, force=True)+ except Exception: # noqa: BLE001+ logger.warning("worktree cleanup failed for %s", worktree, exc_info=True)async def _run_iterations(self,@@ -1368,7 +1402,8 @@ class Kontrollierer:return TaskOutcome.ERRORcontinue- diff_text = self._capture_diff(worktree)+ # git add/diff/ls-files over the whole worktree: off the loop.+ diff_text = await asyncio.to_thread(self._capture_diff, worktree)log_text = self._read_stream_log(log_dir)# Out-of-worktree deliverables (live mission_019e7abd, 2026-05-30):# a task may legitimately target an absolute path outside thediff --git a/tests/missions/kontrollierer/test_worktree_setup_failure.py b/tests/missions/kontrollierer/test_worktree_setup_failure.pyindex db9101ecea..f360ae477f 100644--- a/tests/missions/kontrollierer/test_worktree_setup_failure.py+++ b/tests/missions/kontrollierer/test_worktree_setup_failure.py@@ -211,3 +211,47 @@ async def test_source_checkout_capability_failure_is_distinct(k._setup_failure_reason["m-source-unavailable"]== "source_checkout_unavailable")+++class _ThreadRecordingWorktrees:+ """Records which thread ran ``create``, then fails like the path cap."""++ def __init__(self) -> None:+ self.create_thread: int | None = None++ def create(+ self, *, mission_slug: str, task_id: str, needs_repo: bool = True+ ): # noqa: ANN201+ import threading++ self.create_thread = threading.get_ident()+ raise ValueError("Worktree path is too long (250 > 200): ...")+++async def test_worktree_create_runs_off_the_event_loop(tmp_path: Path) -> None:+ """``git worktree add`` must not block the loop every route shares.++ A full worktree is several git subprocesses and thousands of file writes;+ run inline it froze the desktop app for 15 s+ (loop watchdog, 2026-10-02).+ """+ import threading++ worktrees = _ThreadRecordingWorktrees()+ k = object.__new__(Kontrollierer)+ k._budget = _NoopBudget()+ k._setup_failure_reason = {}+ k._worktrees = worktrees++ outcome = await k._run_task_with_critic_loop(+ mission_id="m-off-loop",+ mission_prompt="build a thing",+ step=Step(slug="w", prompt="do w"),+ mission_dir=tmp_path,+ reflections=object(),+ sem=asyncio.Semaphore(1),+ )++ assert outcome == TaskOutcome.SETUP_FAILED+ assert worktrees.create_thread is not None+ assert worktrees.create_thread != threading.get_ident()