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(agent-runtimes): a setup job reads finished only after its post-run refresh (#948)

Ruben Lütkea3140a9048dc
diff --git a/dodaios/agent_runtimes/manager.py b/dodaios/agent_runtimes/manager.py
index df9479f842..d835763671 100644
--- a/dodaios/agent_runtimes/manager.py
+++ b/dodaios/agent_runtimes/manager.py
@@ -338,7 +338,6 @@ def _failure_message(label: str, kind: str, code: int, runtime: str) -> str:
async def _run(current: RuntimeJob, argv: list[str], env: dict[str, str] | None = None) -> None:
from dodaios.core.process_tree import make_process_tree
- from dodaios.core.process_utils import NO_WINDOW_CREATIONFLAGS
target = driver(current.runtime)
if current.kind == "update":
@@ -346,9 +345,43 @@ async def _run(current: RuntimeJob, argv: list[str], env: dict[str, str] | None
path = _log_path(current.runtime)
missing = _missing_launcher(argv)
tree = make_process_tree(f"{current.runtime}-{current.kind}")
+ # The outcome is published only after the PATH refresh and re-detect
+ # below: anything polling the job (the runtime routes, a test) that sees
+ # it finished must also see the runtime status that outcome produced.
+ outcome: JobState | None = None
+ try:
+ try:
+ outcome = await _run_process(current, argv, env, path, missing, tree, target)
+ finally:
+ tree.close()
+ # What the installer registered on the persistent PATH is visible from
+ # now on (never a Jarvis agent folder); ``--version`` spawns: off the loop.
+ await asyncio.to_thread(_refresh_path)
+ await asyncio.to_thread(target.detect, refresh=True)
+ finally:
+ current.finished_ms = int(time.time() * 1000)
+ if outcome is None:
+ # Cancelled (app shutdown): never leave a job that looks alive.
+ outcome = "failed"
+ current.message = current.message or f"The {current.kind} was interrupted."
+ current.state = outcome
+
+
+async def _run_process(
+ current: RuntimeJob,
+ argv: list[str],
+ env: dict[str, str] | None,
+ path: Path,
+ missing: str,
+ tree: Any,
+ target: Any,
+) -> JobState:
+ """Run the job's command; returns its outcome without publishing it."""
+ from dodaios.core.process_utils import NO_WINDOW_CREATIONFLAGS
+
try:
if missing:
- path.write_text(missing + "\n", encoding="utf-8")
+ await asyncio.to_thread(path.write_text, missing + "\n", encoding="utf-8")
raise OSError(missing)
with path.open("wb") as handle:
# Only the program and its first argument: an inline script body
@@ -373,24 +406,13 @@ async def _run(current: RuntimeJob, argv: list[str], env: dict[str, str] | None
code = -1
current.message = "Stopped after 30 minutes."
current.exit_code = code
- current.state = "done" if code == 0 else "failed"
if code != 0 and not current.message:
current.message = _failure_message(target.label, current.kind, code, current.runtime)
+ return "done" if code == 0 else "failed"
except OSError as exc:
- current.state = "failed"
current.message = missing or f"Could not start the {current.kind}: {exc}"
log.warning("agent runtimes: %s %s could not start: %s", current.runtime, current.kind, exc)
- finally:
- tree.close()
- current.finished_ms = int(time.time() * 1000)
- if current.state == "running":
- # Cancelled (app shutdown): never leave a job that looks alive.
- current.state = "failed"
- current.message = current.message or f"The {current.kind} was interrupted."
- # What the installer registered on the persistent PATH is visible from now
- # on (never a Jarvis agent folder); ``--version`` spawns: off the loop.
- await asyncio.to_thread(_refresh_path)
- await asyncio.to_thread(target.detect, refresh=True)
+ return "failed"
def _refresh_path() -> None:
diff --git a/tests/unit/agent_runtimes/test_runtime_manager.py b/tests/unit/agent_runtimes/test_runtime_manager.py
index 68e1d4c59d..26a869658e 100644
--- a/tests/unit/agent_runtimes/test_runtime_manager.py
+++ b/tests/unit/agent_runtimes/test_runtime_manager.py
@@ -140,6 +140,47 @@ async def test_an_install_runs_the_official_command_and_logs_it(fakes):
assert fakes["hermes"].refreshed >= 1
+async def test_a_job_reads_finished_only_after_its_post_run_refresh(fakes, monkeypatch):
+ """A poller that sees the job finished also sees the refreshed status (#704)."""
+ import threading
+
+ refreshing = threading.Event()
+ release = threading.Event()
+
+ def _slow_refresh() -> None:
+ refreshing.set()
+ release.wait(10)
+
+ monkeypatch.setattr(manager, "_refresh_path", _slow_refresh)
+ await manager.start("hermes", "install")
+ assert await asyncio.to_thread(refreshing.wait, 10)
+ # The installer has exited, the PATH refresh and re-detect are still owed.
+ current = manager.job("hermes")
+ assert current is not None
+ assert current.state == "running" and current.finished_ms is None
+ release.set()
+ done = await _finish("hermes")
+ assert done.state == "done"
+ assert fakes["hermes"].refreshed >= 1
+
+
+async def test_a_cancelled_job_never_looks_alive(fakes, monkeypatch):
+ slow = [sys.executable, "-c", "import time; time.sleep(30)"]
+ monkeypatch.setattr(fakes["hermes"], "install_command", lambda revision=None: slow)
+ started = await manager.start("hermes", "install")
+ task = manager._TASKS["hermes"]
+ log_file = manager._log_path("hermes")
+ for _ in range(200): # inside the job: the installer's log line is written
+ if log_file.exists() and log_file.read_text(encoding="utf-8").startswith("$ "):
+ break
+ await asyncio.sleep(0.05)
+ task.cancel()
+ with pytest.raises(asyncio.CancelledError):
+ await task
+ assert started.state == "failed" and started.finished_ms is not None
+ assert "interrupted" in started.message
+
+
async def test_an_update_stops_the_runtime_first_and_reports_a_failure(fakes):
await manager.start("openclaw", "update")
done = await _finish("openclaw")