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): serialize runtime turns and keep routine runs apart

rubenluetke10-beepf801093796fe
diff --git a/docs/agent-runtimes.md b/docs/agent-runtimes.md
index 2c1a6d8972..5b739dca93 100644
--- a/docs/agent-runtimes.md
+++ b/docs/agent-runtimes.md
@@ -31,6 +31,22 @@ capabilities are read from the `initialize` answer, not from version strings.
answering `session/request_permission` from the chat's approval card. History
replayed during `session/load` is swallowed because the chat already shows it.
+## Turns, folders and approvals (both runtimes)
+
+- **One turn at a time per folder.** A runtime's config is written per turn,
+ so the turns of one folder run one after another (`base.TurnSlots`); a turn
+ that waits more than 15 minutes stops with a plain message.
+- **Routine runs get their own folder** (`base.home_key`: `<agent>` for the
+ agent's chat, `<agent>~runs` for every other session). A scheduled run never
+ rewrites the model, tools or session store a chat turn is using, and on
+ OpenClaw it runs on its own Gateway.
+- **Approvals never live in the runtime's config.** Hermes runs with
+ `approvals.mode: manual` and OpenClaw with `tools.exec.mode: ask`; every
+ request reaches Jarvis over ACP and is answered from the chat's stance —
+ Bypass allows without a card, Ask shows the card, Plan refuses.
+- **A finished turn ends.** After the prompt's answer the runtime gets ten
+ seconds to exit on its own, then it is ended; the answer already stands.
+
## Hermes
- **Process:** `hermes acp`, one process per turn, stdin EOF ends it (exit 0).
@@ -72,8 +88,10 @@ replayed during `session/load` is swallowed because the chat already shows it.
(`--url ws://127.0.0.1:<port> --session agent:main:main`). No hand-written
Gateway WebSocket client: its wire protocol requires an exact version match.
- **Isolation:** one state directory (`OPENCLAW_STATE_DIR`,
- `OPENCLAW_CONFIG_PATH`) and one Gateway per agent, on its own loopback port,
- started on the first turn and stopped after an idle period. Start-up is
+ `OPENCLAW_CONFIG_PATH`) and one Gateway per agent folder, on its own
+ loopback port, started on the first turn and stopped after 15 idle minutes.
+ A changed config restarts the Gateway (with no turn in flight) instead of
+ trusting its file watcher to have reloaded in time. Start-up is
about 16 s on Windows with the preset (measured with 2026.9.8). The user's
own `~/.openclaw` and their Gateway service are never touched.
- **Config Jarvis writes** (`openclaw.json`, only long-stable keys):
diff --git a/jarvis/agent_chat/runner_acp.py b/jarvis/agent_chat/runner_acp.py
index bc659560c1..29051e0e35 100644
--- a/jarvis/agent_chat/runner_acp.py
+++ b/jarvis/agent_chat/runner_acp.py
@@ -121,10 +121,10 @@ async def plan_runtime_turn(
resume=launch.acp_resume,
mcp_servers=launch.mcp_servers,
auto_allow=turn.auto_approve,
+ auto_deny=plan_mode,
client_version=__version__,
report_session=launch.vendor_session,
)
- finished = getattr(runtime, "turn_finished", None)
return CliPlan(
argv=launch.argv,
env=launch.env,
@@ -133,5 +133,5 @@ async def plan_runtime_turn(
vendor_session=None,
keep_stdin=True,
acp=acp,
- after_turn=(lambda: finished(agent.agent_id)) if callable(finished) else None,
+ after_turn=launch.release,
)
diff --git a/jarvis/agent_chat/runner_cli.py b/jarvis/agent_chat/runner_cli.py
index 061c961560..363809814c 100644
--- a/jarvis/agent_chat/runner_cli.py
+++ b/jarvis/agent_chat/runner_cli.py
@@ -1296,6 +1296,18 @@ CLI_RUNNERS: Final[frozenset[str]] = frozenset(_PLANNERS)
_CLAUDE_CODE_RUNNERS: Final[frozenset[str]] = frozenset({"claude-cli", "glm-cli"})
+#: How long an ACP runtime may keep running after it answered the prompt.
+_ACP_EXIT_GRACE_S: Final[float] = 10.0
+
+
+def _release_plan(plan: CliPlan | None) -> None:
+ """Run a plan's ``after_turn`` once (an external runtime's turn slot)."""
+ if plan is None or plan.after_turn is None:
+ return
+ release, plan.after_turn = plan.after_turn, None
+ release()
+
+
def supports_cli_runner(runner: str) -> bool:
from jarvis.agent_chat.runner_acp import supports_runner
@@ -2613,7 +2625,15 @@ _RESUME_LOST_MARKERS: Final[tuple[str, ...]] = (
)
-def _resume_was_lost(error: str | None) -> bool:
+def _resume_was_lost(error: str | None, runner: str = "") -> bool:
+ from jarvis.agent_chat.runner_acp import supports_runner
+
+ if supports_runner(runner):
+ # An ACP runtime says so explicitly; "model does not exist" from the
+ # prompt itself must not buy a second, fresh (and paid) turn.
+ from jarvis.agent_runtimes.acp import RESUME_LOST_ERROR
+
+ return (error or "").startswith(RESUME_LOST_ERROR)
low = (error or "").lower()
return any(m in low for m in _RESUME_LOST_MARKERS)
@@ -2812,7 +2832,7 @@ async def run_cli_turn(
outcome.status == "error"
and resume
and not recovery.had_tool_calls
- and _resume_was_lost(outcome.error)
+ and _resume_was_lost(outcome.error, runner)
and not handle.cancel.is_set()
):
log.info(
@@ -2911,6 +2931,8 @@ async def _run_cli_once(
placement = await placement_for_session(session)
remote_token = _REMOTE_PLANNING.set(placement is not None)
planned_prompt = user_text
+ # Set once planning succeeded; an external runtime's plan holds a turn slot.
+ plan: CliPlan | None = None
if placement is not None and not getattr(handle, "tools_disabled", False):
# Also refresh resumed conversations which remember the old missing bridge.
planned_prompt = (
@@ -2948,7 +2970,6 @@ async def _run_cli_once(
# Resolve the installed CLI's effort ladder off the event loop so
# newly available models keep the required model/effort pairing.
await asyncio.to_thread(read_agy_models, required_model=session.model)
- plan: CliPlan
if planner is None:
from jarvis.agent_chat.runner_acp import plan_runtime_turn
@@ -2997,6 +3018,7 @@ async def _run_cli_once(
else:
raise CliUnavailable("The selected runner cannot isolate task tools.")
except CliUnavailable as exc:
+ _release_plan(plan)
return _Outcome("error", str(exc), {}, None, None)
finally:
_REMOTE_PLANNING.reset(remote_token)
@@ -3068,6 +3090,7 @@ async def _run_cli_once(
except (OSError, ValueError) as exc:
if tree is not None:
tree.close()
+ _release_plan(plan)
return _Outcome("error", f"Could not start {runner}: {exc}", {}, None, None)
if plan.acp is not None:
@@ -3104,6 +3127,7 @@ async def _run_cli_once(
)
async def _pump_stdout() -> None:
+ nonlocal grace_kill
assert proc.stdout is not None
while True:
raw = await proc.stdout.readline()
@@ -3129,7 +3153,13 @@ async def _run_cli_once(
await plan.acp.on_message(obj, acp_io)
if plan.acp.saw_result:
# The prompt answered; closing stdin lets the runtime exit.
+ # One that keeps running anyway (a child holding the pipe)
+ # is ended so the chat is not held busy by a finished turn.
_close_stdin()
+ if grace_kill is None:
+ grace_kill = asyncio.get_running_loop().call_later(
+ _ACP_EXIT_GRACE_S, _kill, proc
+ )
continue
if plan.control_init is not None:
if obj.get("type") == "control_request":
@@ -3265,6 +3295,7 @@ async def _run_cli_once(
return str(decision)
acp_io = _AcpIO()
+ grace_kill: asyncio.TimerHandle | None = None
pump = asyncio.create_task(_pump_stdout())
drain = asyncio.create_task(_drain_stderr())
@@ -3297,8 +3328,9 @@ async def _run_cli_once(
await asyncio.wait_for(proc.wait(), timeout=5)
except TimeoutError:
log.warning("Agent CLI did not reap after cancellation")
- if plan.after_turn is not None:
- plan.after_turn()
+ if grace_kill is not None:
+ grace_kill.cancel()
+ _release_plan(plan)
if handle.cancel.is_set():
status = "cancelled"
@@ -3308,7 +3340,10 @@ async def _run_cli_once(
"error",
state.error or getattr(state, "last_tool_error", None),
)
- elif proc.returncode not in (0, None):
+ elif proc.returncode not in (0, None) and not (
+ # An ACP turn that answered and was then ended by the grace timer.
+ plan.acp is not None and plan.acp.saw_result
+ ):
status = "error"
error_text = (
state.error
diff --git a/jarvis/agent_runtimes/acp.py b/jarvis/agent_runtimes/acp.py
index 63bb0b8ba7..42d8cf7db0 100644
--- a/jarvis/agent_runtimes/acp.py
+++ b/jarvis/agent_runtimes/acp.py
@@ -143,6 +143,8 @@ class AcpTurn:
#: Answer the agent's permission requests with "allow" without asking
#: (the chat's Bypass stance). Jarvis' own gates on MCP tools still apply.
auto_allow: bool = False
+ #: Refuse every permission request without asking (Plan mode: read only).
+ auto_deny: bool = False
client_name: str = "personal-jarvis"
client_version: str = ""
#: What the chat stores as its vendor session instead of the ACP session
@@ -319,14 +321,16 @@ class AcpTurn:
by_kind[kind] = str(option.get("optionId") or "")
call = params.get("toolCall") if isinstance(params.get("toolCall"), dict) else {}
call_id = str(call.get("toolCallId") or uuid.uuid4().hex)
- if call_id not in self.emitted_tool_ids and not self.auto_allow:
+ if call_id not in self.emitted_tool_ids and not (self.auto_allow or self.auto_deny):
# The approval card sits on a tool row; give it one.
await self._flush_text(io)
await self._tool_start(call | {"toolCallId": call_id}, io)
name = self._tool_names.get(call_id) or _tool_name(call)
args = call.get("rawInput") if isinstance(call.get("rawInput"), dict) else {}
summary = str(call.get("title") or name)[:200]
- if self.auto_allow:
+ if self.auto_deny:
+ decision = "deny"
+ elif self.auto_allow:
decision = "allow"
else:
await self._flush_text(io)
diff --git a/jarvis/agent_runtimes/base.py b/jarvis/agent_runtimes/base.py
index 22b3ef3878..c46c77c698 100644
--- a/jarvis/agent_runtimes/base.py
+++ b/jarvis/agent_runtimes/base.py
@@ -15,6 +15,7 @@ the person's own Hermes or OpenClaw setup is never read or changed.
from __future__ import annotations
+import asyncio
import json
import logging
import os
@@ -23,6 +24,7 @@ import shutil
import subprocess
import sys
import time
+from collections.abc import Callable
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any, Final, Protocol
@@ -162,6 +164,9 @@ class RuntimeLaunch:
#: What the chat stores as its vendor session after the turn. ``None`` =
#: whatever id the ACP session reported.
vendor_session: str | None = None
+ #: Called exactly once when the turn's process is gone: frees the turn
+ #: slot (and lets an idle Gateway be reaped).
+ release: Callable[[], None] | None = None
class AgentRuntimeDriver(Protocol):
@@ -184,6 +189,54 @@ class AgentRuntimeDriver(Protocol):
# ------------------------------------------------------------------ helpers
+#: How long a turn waits for the agent's previous turn on the same runtime.
+_SLOT_WAIT_S: Final[float] = 15 * 60
+
+
+def home_key(agent_id: str, chat_session_id: str) -> str:
+ """Which runtime folder a chat's turns use.
+
+ The agent's one chat has its own folder; every other session of the
+ agent (routine runs) shares a second one. A routine run therefore never
+ rewrites the config, model or session store a chat turn is using.
+ """
+ return agent_id if chat_session_id == f"society:{agent_id}" else f"{agent_id}~runs"
+
+
+class TurnSlots:
+ """One turn at a time per runtime folder.
+
+ The runtime's config is written per turn; two turns sharing one folder
+ would read each other's settings. A turn waits for the previous one
+ (the agent's chat already queues its own messages), and gives up with a
+ plain message after :data:`_SLOT_WAIT_S`.
+ """
+
+ def __init__(self) -> None:
+ self._locks: dict[str, asyncio.Lock] = {}
+
+ def lock(self, key: str) -> asyncio.Lock:
+ return self._locks.setdefault(key, asyncio.Lock())
+
+ async def acquire(self, key: str) -> Callable[[], None]:
+ lock = self.lock(key)
+ try:
+ await asyncio.wait_for(lock.acquire(), timeout=_SLOT_WAIT_S)
+ except TimeoutError:
+ raise RuntimeUnavailable(
+ "This agent is still busy with another task; try again when it is done."
+ ) from None
+ released = False
+
+ def release() -> None:
+ nonlocal released
+ if not released:
+ released = True
+ lock.release()
+
+ return release
+
+
def runtimes_root() -> Path:
from jarvis.core.paths import user_data_dir
@@ -214,7 +267,7 @@ def write_if_changed(path: Path, text: str) -> bool:
if path.read_text(encoding="utf-8") == text:
return False
except OSError:
- pass
+ pass # no readable file yet: write it below
path.parent.mkdir(parents=True, exist_ok=True)
tmp = path.with_suffix(path.suffix + ".tmp")
tmp.write_text(text, encoding="utf-8")
diff --git a/jarvis/agent_runtimes/hermes.py b/jarvis/agent_runtimes/hermes.py
index a38692c941..f054dc66e0 100644
--- a/jarvis/agent_runtimes/hermes.py
+++ b/jarvis/agent_runtimes/hermes.py
@@ -18,6 +18,7 @@ from __future__ import annotations
import asyncio
import os
import re
+from collections.abc import Callable
from pathlib import Path
from typing import Any, Final
from urllib.parse import urlsplit
@@ -29,10 +30,12 @@ from jarvis.agent_runtimes.base import (
RuntimeStatus,
RuntimeTurn,
RuntimeUnavailable,
+ TurnSlots,
agent_home,
child_env,
first_existing,
format_version,
+ home_key,
is_windows,
parse_version,
persona_text,
@@ -79,6 +82,7 @@ class HermesRuntime:
def __init__(self) -> None:
self._detect = DetectCache()
+ self._slots = TurnSlots()
# ----------------------------------------------------------- detection
@@ -191,9 +195,11 @@ class HermesRuntime:
"background_review": {"enabled": False},
"title_generation": {"enabled": False},
},
- # "manual" asks over ACP (the chat's approval card); never the
- # "smart" guardian, which spends model calls of its own.
- "approvals": {"mode": "off" if turn.auto_approve else "manual"},
+ # "manual" always asks over ACP; Jarvis answers it from the chat's
+ # stance (Bypass allows without a card, Plan refuses), so the file
+ # never depends on which session wrote it last. Never the "smart"
+ # guardian, which spends model calls of its own.
+ "approvals": {"mode": "manual"},
}
disabled = sorted(_disabled_toolsets(turn.denied_native))
if disabled:
@@ -207,7 +213,19 @@ class HermesRuntime:
binary = _binary()
if binary is None:
raise RuntimeUnavailable("Hermes is not installed.")
- home = await asyncio.to_thread(agent_home, NAME, turn.agent_id)
+ release = await self._slots.acquire(home_key(turn.agent_id, turn.session_id))
+ try:
+ return await self._launch(binary, turn, release)
+ except BaseException:
+ release()
+ raise
+
+ async def _launch(
+ self, binary: str, turn: RuntimeTurn, release: Callable[[], None]
+ ) -> RuntimeLaunch:
+ home = await asyncio.to_thread(
+ agent_home, NAME, home_key(turn.agent_id, turn.session_id)
+ )
await asyncio.to_thread(self._write_profile, home, turn)
env = child_env(
{
@@ -239,6 +257,7 @@ class HermesRuntime:
cwd=turn.workspace,
mcp_servers=servers,
acp_resume=turn.resume,
+ release=release,
)
def _write_profile(self, home: Path, turn: RuntimeTurn) -> None:
diff --git a/jarvis/agent_runtimes/manager.py b/jarvis/agent_runtimes/manager.py
index 8b1f5ddb6e..e80988fcc3 100644
--- a/jarvis/agent_runtimes/manager.py
+++ b/jarvis/agent_runtimes/manager.py
@@ -149,5 +149,10 @@ async def _run(current: RuntimeJob, argv: list[str]) -> None:
finally:
tree.close()
current.finished_ms = int(time.time() * 1000)
- target.detect(refresh=True)
+ 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."
_TASKS.pop(current.runtime, None)
+ # ``--version`` spawns a process: off the event loop.
+ await asyncio.to_thread(target.detect, refresh=True)
diff --git a/jarvis/agent_runtimes/openclaw.py b/jarvis/agent_runtimes/openclaw.py
index 83af8b0417..ed0e5a6a86 100644
--- a/jarvis/agent_runtimes/openclaw.py
+++ b/jarvis/agent_runtimes/openclaw.py
@@ -26,6 +26,7 @@ import os
import secrets
import socket
import time
+from collections.abc import Callable
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any, Final
@@ -36,9 +37,11 @@ from jarvis.agent_runtimes.base import (
RuntimeStatus,
RuntimeTurn,
RuntimeUnavailable,
+ TurnSlots,
agent_home,
child_env,
format_version,
+ home_key,
is_windows,
parse_version,
run_version,
@@ -159,7 +162,8 @@ async def _listening(port: int) -> bool:
@dataclass(slots=True)
class _Gateway:
- agent_id: str
+ #: ``home_key``: the agent's chat Gateway, or its routine-runs Gateway.
+ key: str
home: Path
port: int
env_hash: str
@@ -181,6 +185,7 @@ class OpenClawRuntime:
self._detect = DetectCache()
self._gateways: dict[str, _Gateway] = {}
self._locks: dict[str, asyncio.Lock] = {}
+ self._slots = TurnSlots()
self._reaper: asyncio.Task[None] | None = None
# ----------------------------------------------------------- detection
@@ -277,10 +282,10 @@ class OpenClawRuntime:
denied = list(_ALWAYS_DENIED)
if "web" in turn.denied_native:
denied += ["browser", "web_search", "web_fetch"]
- if "shell" in turn.denied_native:
- exec_mode = "deny"
- else:
- exec_mode = "full" if turn.auto_approve else "ask"
+ # "ask" relays every exec approval over ACP; Jarvis answers it from the
+ # chat's stance (Bypass allows without a card), so the file does not
+ # change with the stance of whichever turn wrote it.
+ exec_mode = "deny" if "shell" in turn.denied_native else "ask"
config: dict[str, Any] = {
"gateway": {
"mode": "local",
@@ -316,10 +321,10 @@ class OpenClawRuntime:
"transport": "streamable-http",
"headers": {
"Authorization": f"Bearer ${{{_CONTROL_KEY_ENV}}}",
- # The agent's canonical chat: one config for every
- # session of this Gateway, so a routine run never
- # rewrites it under a running chat turn.
- HEADER_NAME: f"society:{turn.agent_id}",
+ # This turn's chat, so approvals, grants and the
+ # turn context are that session's. Routine runs use
+ # their own Gateway (``home_key``).
+ HEADER_NAME: turn.session_id,
},
}
}
@@ -335,7 +340,23 @@ class OpenClawRuntime:
launcher = _launcher()
if launcher is None:
raise RuntimeUnavailable("OpenClaw is not installed.")
- home = await asyncio.to_thread(agent_home, NAME, turn.agent_id)
+ key = home_key(turn.agent_id, turn.session_id)
+ # One turn at a time per Gateway: its config is this turn's.
+ release_slot = await self._slots.acquire(key)
+ try:
+ return await self._launch(turn, key, launcher, release_slot)
+ except BaseException:
+ release_slot()
+ raise
+
+ async def _launch(
+ self,
+ turn: RuntimeTurn,
+ key: str,
+ launcher: list[str],
+ release_slot: Callable[[], None],
+ ) -> RuntimeLaunch:
+ home = await asyncio.to_thread(agent_home, NAME, key)
token = await asyncio.to_thread(_gateway_token, home)
gateway_env = child_env(
{
@@ -345,12 +366,18 @@ class OpenClawRuntime:
**({_CONTROL_KEY_ENV: turn.control_key} if turn.control_key else {}),
}
)
- port = await self._ensure_gateway(turn, home, token, launcher, gateway_env)
+ gateway = await self._ensure_gateway(turn, key, home, token, launcher, gateway_env)
+
+ def release() -> None:
+ gateway.last_used = time.monotonic()
+ gateway.in_use = max(0, gateway.in_use - 1)
+ release_slot()
+
argv = [
*launcher,
"acp",
"--url",
- f"ws://127.0.0.1:{port}",
+ f"ws://127.0.0.1:{gateway.port}",
"--token-file",
str(home / "gateway.token"),
"--session",
@@ -366,48 +393,55 @@ class OpenClawRuntime:
cwd=turn.workspace,
acp_resume=None,
vendor_session=session_key(turn.agent_id, turn.session_id),
+ release=release,
)
- def turn_finished(self, agent_id: str) -> None:
- gateway = self._gateways.get(agent_id)
- if gateway is not None:
- gateway.in_use = max(0, gateway.in_use - 1)
- gateway.last_used = time.monotonic()
-
async def _ensure_gateway(
self,
turn: RuntimeTurn,
+ key: str,
home: Path,
token: str,
launcher: list[str],
env: dict[str, str],
- ) -> int:
- lock = self._locks.setdefault(turn.agent_id, asyncio.Lock())
- async with lock:
+ ) -> _Gateway:
+ """The running Gateway for ``key``, (re)started when anything changed.
+
+ Called with the turn slot held, so no other turn of this Gateway is in
+ flight: a restart never cuts one off. A changed config restarts too —
+ a running Gateway reloads its file on its own schedule, and this
+ turn's settings (model, tools, the MCP session header) must be the
+ ones it runs with.
+ """
+ async with self._gateway_lock(key):
env_hash = hashlib.sha256(
json.dumps(sorted(env.items())).encode("utf-8")
).hexdigest()
- current = self._gateways.get(turn.agent_id)
- if current is not None and (not current.alive() or current.env_hash != env_hash):
- # A new key or control key needs a new process environment.
+ current = self._gateways.get(key)
+ port = current.port if current is not None and current.alive() else _free_port()
+ config = self.config_for(turn, port=port, token=token)
+ changed = await asyncio.to_thread(
+ write_json_if_changed, home / "openclaw.json", config
+ )
+ if current is not None and (
+ not current.alive() or current.env_hash != env_hash or changed
+ ):
await self._stop_gateway(current)
current = None
- port = current.port if current is not None else _free_port()
- config = self.config_for(turn, port=port, token=token)
- await asyncio.to_thread(write_json_if_changed, home / "openclaw.json", config)
if current is None:
- current = await self._start_gateway(
- turn.agent_id, home, port, env_hash, launcher, env
- )
- self._gateways[turn.agent_id] = current
+ current = await self._start_gateway(key, home, port, env_hash, launcher, env)
+ self._gateways[key] = current
current.in_use += 1
current.last_used = time.monotonic()
self._ensure_reaper()
- return current.port
+ return current
+
+ def _gateway_lock(self, key: str) -> asyncio.Lock:
+ return self._locks.setdefault(key, asyncio.Lock())
async def _start_gateway(
self,
- agent_id: str,
+ key: str,
home: Path,
port: int,
env_hash: str,
@@ -420,8 +454,9 @@ class OpenClawRuntime:
from jarvis.core.process_utils import NO_WINDOW_CREATIONFLAGS
log_path = home / "gateway.log"
- handle = log_path.open("ab")
- tree = make_process_tree(f"openclaw-gateway-{agent_id}")
+ # One run's log at a time: it can quote prompts, so it never grows on.
+ handle = log_path.open("wb")
+ tree = make_process_tree(f"openclaw-gateway-{key}")
try:
proc = await asyncio.create_subprocess_exec(
*launcher,
@@ -443,22 +478,28 @@ class OpenClawRuntime:
tree.close()
raise RuntimeUnavailable(f"OpenClaw could not start: {exc}") from exc
tree.assign(proc.pid)
- gateway = _Gateway(agent_id, home, port, env_hash, proc, tree, handle)
- log.info("agent runtimes: OpenClaw gateway for %s starting on %s", agent_id, port)
- deadline = time.monotonic() + _START_TIMEOUT_S
- while time.monotonic() < deadline:
- if proc.returncode is not None:
- break
- if await _listening(port):
- return gateway
- await asyncio.sleep(0.5)
+ gateway = _Gateway(key, home, port, env_hash, proc, tree, handle)
+ log.info("agent runtimes: OpenClaw gateway %s starting on %s", key, port)
+ try:
+ deadline = time.monotonic() + _START_TIMEOUT_S
+ while time.monotonic() < deadline:
+ if proc.returncode is not None:
+ break
+ if await _listening(port):
+ return gateway
+ await asyncio.sleep(0.5)
+ except BaseException:
+ # A cancelled turn (Stop during a cold start) must not leave a
+ # Gateway behind with the key in its environment.
+ await asyncio.shield(self._stop_gateway(gateway))
+ raise
returncode = proc.returncode
await self._stop_gateway(gateway)
if returncode == _EXIT_CONFIG and not repaired:
log.warning("agent runtimes: OpenClaw rejected its config; running doctor --fix")
await _run_doctor(launcher, env, home)
return await self._start_gateway(
- agent_id, home, port, env_hash, launcher, env, repaired=True
+ key, home, port, env_hash, launcher, env, repaired=True
)
tail = _log_tail(log_path)
if returncode is None:
@@ -466,7 +507,8 @@ class OpenClawRuntime:
raise RuntimeUnavailable(f"OpenClaw stopped while starting (exit {returncode})." + tail)
async def _stop_gateway(self, gateway: _Gateway) -> None:
- self._gateways.pop(gateway.agent_id, None)
+ if self._gateways.get(gateway.key) is gateway:
+ del self._gateways[gateway.key]
if gateway.proc.returncode is None:
with contextlib.suppress(ProcessLookupError, OSError):
gateway.proc.terminate()
@@ -487,21 +529,21 @@ class OpenClawRuntime:
async def _reap_idle(self) -> None:
while self._gateways:
await asyncio.sleep(60)
- now = time.monotonic()
- for gateway in list(self._gateways.values()):
- idle = now - gateway.last_used
- if not gateway.alive() or (gateway.in_use == 0 and idle > IDLE_STOP_S):
- log.info("agent runtimes: stopping idle OpenClaw gateway %s", gateway.agent_id)
- await self._stop_gateway(gateway)
+ for key in list(self._gateways):
+ async with self._gateway_lock(key):
+ gateway = self._gateways.get(key)
+ if gateway is None or gateway.in_use > 0:
+ continue
+ idle = time.monotonic() - gateway.last_used
+ if not gateway.alive() or idle > IDLE_STOP_S:
+ log.info("agent runtimes: stopping idle OpenClaw gateway %s", key)
+ await self._stop_gateway(gateway)
async def stop(self, agent_id: str | None = None) -> None:
- targets = [
- gateway
- for gateway in list(self._gateways.values())
- if agent_id is None or gateway.agent_id == agent_id
- ]
- for gateway in targets:
- await self._stop_gateway(gateway)
+ for key, gateway in list(self._gateways.items()):
+ if agent_id is None or key in (agent_id, f"{agent_id}~runs"):
+ async with self._gateway_lock(key):
+ await self._stop_gateway(gateway)
def session_key(agent_id: str, chat_session_id: str) -> str:
@@ -528,7 +570,7 @@ def _gateway_token(home: Path) -> str:
if token:
return token
except OSError:
- pass
+ pass # no token yet: mint one below
token = secrets.token_urlsafe(32)
write_if_changed(path, token)
with contextlib.suppress(OSError):
diff --git a/jarvis/society/routine_runner.py b/jarvis/society/routine_runner.py
index 676ab51de1..7da9c3e14b 100644
--- a/jarvis/society/routine_runner.py
+++ b/jarvis/society/routine_runner.py
@@ -127,14 +127,25 @@ async def _seat_for_run(runtime: Any, agent: Any, task_id: str) -> tuple[str, st
def _run_runtime(agent: Any, provider: str) -> str:
- """The run keeps its owner's runtime when the run's model can drive it;
- a pinned subscription seat runs on Jarvis' own runtime instead."""
+ """Which runtime a routine run uses.
+
+ The run keeps its owner's Hermes / OpenClaw runtime on the owner's own
+ model — the seat the person chose for that agent. A run pinned to another
+ seat keeps the runtime only when that seat is an API key or local server:
+ a subscription seat (Claude Code's dual row included) runs on Jarvis' own
+ runtime, so a scheduled run never quietly moves onto an API key.
+ """
runtime = str(getattr(agent, "runtime", "") or "jarvis")
if runtime == "jarvis":
return ""
+ from jarvis.agent_chat.service import resolve_runner
from jarvis.agent_runtimes.model_map import supports
- return runtime if supports(provider) else ""
+ if not supports(provider):
+ return ""
+ if provider == str(getattr(agent, "provider", "") or ""):
+ return runtime
+ return "" if resolve_runner(provider, surface=SURFACE).endswith("-cli") else runtime
async def run_owned_routine(
diff --git a/jarvis/ui/web/agent_chat_routes.py b/jarvis/ui/web/agent_chat_routes.py
index 5894078027..f129481455 100644
--- a/jarvis/ui/web/agent_chat_routes.py
+++ b/jarvis/ui/web/agent_chat_routes.py
@@ -937,7 +937,11 @@ async def patch_session(
assert current is not None
if body.cwd is not None:
fields["cwd"] = _validate_cwd(body.cwd) or svc.default_cwd(current.surface)
- runner = resolve_runner(fields.get("provider") or current.provider, surface=current.surface)
+ runner = resolve_runner(
+ fields.get("provider") or current.provider,
+ surface=current.surface,
+ runtime=str(getattr(current, "runtime", "") or ""),
+ )
ladder = ladder_key(current.surface, runner)
if body.permission_mode is not None:
if not is_permission_mode(ladder, body.permission_mode):
diff --git a/jarvis/ui/web/agent_runtime_routes.py b/jarvis/ui/web/agent_runtime_routes.py
index b868ca97a2..fcb0fc8341 100644
--- a/jarvis/ui/web/agent_runtime_routes.py
+++ b/jarvis/ui/web/agent_runtime_routes.py
@@ -9,6 +9,7 @@ own official tools and only when the person presses the button.
from __future__ import annotations
import asyncio
+import time
from typing import Any, Literal
from fastapi import APIRouter, HTTPException, Request
@@ -18,6 +19,11 @@ from jarvis.agent_runtimes.model_map import supported_providers, usable_provider
router = APIRouter(prefix="/api/agent-runtimes", tags=["agent-runtimes"])
+#: ``[checked_at, providers]``: the keyring is read at most every few seconds,
+#: since the UI polls this route while an install runs.
+_USABLE_CACHE: list[Any] = [float("-inf"), []]
+_USABLE_TTL_S = 10.0
+
@router.get("")
async def list_agent_runtimes(request: Request, refresh: bool = False) -> dict[str, Any]:
@@ -25,19 +31,27 @@ async def list_agent_runtimes(request: Request, refresh: bool = False) -> dict[s
plus the Jarvis providers an agent on such a runtime can use right now
(``supported_providers``: saved key or local server; ``all_providers``:
every provider the runtimes can drive once connected)."""
- config = getattr(request.app.state, "config", None)
- if config is None:
- from jarvis.core.config import load_config
+ now = time.monotonic()
+ if refresh or now - _USABLE_CACHE[0] > _USABLE_TTL_S:
+ config = getattr(request.app.state, "config", None)
+ if config is None:
+ from jarvis.core.config import load_config
- config = await asyncio.to_thread(load_config)
+ config = await asyncio.to_thread(load_config)
+ _USABLE_CACHE[:] = [now, await asyncio.to_thread(usable_providers, config)]
return {
"runtimes": await manager.statuses(refresh=refresh),
- "supported_providers": await asyncio.to_thread(usable_providers, config),
+ "supported_providers": list(_USABLE_CACHE[1]),
"all_providers": sorted(supported_providers()),
}
[email protected]("/{runtime}/{action}")
+ "/{runtime}/{action}",
+ # Downloads and runs the project's installer / updater and stops the
+ # runtime's running processes: the CLI asks before sending it.
+ openapi_extra={"x-jarvis-dangerous": True},
+)
async def run_agent_runtime_setup(
runtime: str, action: Literal["install", "update"]
) -> dict[str, Any]:
diff --git a/scripts/spikes/agent_runtimes_e2e.py b/scripts/spikes/agent_runtimes_e2e.py
index c7634bc34e..76355551e6 100644
--- a/scripts/spikes/agent_runtimes_e2e.py
+++ b/scripts/spikes/agent_runtimes_e2e.py
@@ -91,9 +91,8 @@ async def _run(name: str, turn: RuntimeTurn, text: str) -> tuple[AcpTurn, _IO]:
await asyncio.wait_for(pump(), timeout=180)
await asyncio.wait_for(proc.wait(), timeout=30)
- finished = getattr(runtime, "turn_finished", None)
- if callable(finished):
- finished(turn.agent_id)
+ if launch.release is not None:
+ launch.release()
return acp, io
diff --git a/tests/fakes/fake_acp_agent.py b/tests/fakes/fake_acp_agent.py
index 5f0404c356..dfb66a6977 100644
--- a/tests/fakes/fake_acp_agent.py
+++ b/tests/fakes/fake_acp_agent.py
@@ -17,6 +17,8 @@ What a prompt does depends on its text:
* contains ``ASK`` — a ``session/request_permission`` first; the answer's
option id is echoed in the reply;
* contains ``FAIL`` — the prompt request answers a JSON-RPC error;
+* contains ``HANG`` — answers, then keeps running after stdin closes (a
+ runtime whose child holds the pipe);
* otherwise — ``echo: <text>`` streamed in two chunks plus a thought.
``session/load`` of an unknown id answers ``{}`` (Hermes' behaviour).
@@ -27,6 +29,7 @@ from __future__ import annotations
import json
import os
import sys
+import time
import uuid
from pathlib import Path
from typing import Any
@@ -143,6 +146,8 @@ def _prompt(rid: Any, session_id: str, text: str, store: dict[str, list[str]]) -
},
}
)
+ if "HANG" in text:
+ time.sleep(600)
def main() -> int:
diff --git a/tests/unit/agent_runtimes/test_acp_runner.py b/tests/unit/agent_runtimes/test_acp_runner.py
index f5a55efec4..b7259291d2 100644
--- a/tests/unit/agent_runtimes/test_acp_runner.py
+++ b/tests/unit/agent_runtimes/test_acp_runner.py
@@ -48,6 +48,8 @@ def _turn(
mode: str = "bypass",
answer: str = "allow",
report_session: str | None = None,
+ argv: list[str] | None = None,
+ released: list[int] | None = None,
) -> tuple[str | None, list[dict[str, Any]], list[tuple[str, str]], list[str | None]]:
resumes: list[str | None] = []
@@ -56,7 +58,7 @@ def _turn(
env = dict(os.environ)
env["FAKE_ACP_STORE"] = str(tmp_path / "store.json")
return rc.CliPlan(
- argv=[sys.executable, str(_AGENT)],
+ argv=argv or [sys.executable, str(_AGENT)],
env=env,
stdin_text=None,
shape="acp",
@@ -68,8 +70,10 @@ def _turn(
prompt_text=prompt,
resume=resume,
auto_allow=handle.session.permission_mode == "bypass",
+ auto_deny=handle.session.permission_mode == "plan",
report_session=report_session,
),
+ after_turn=(lambda: released.append(1)) if released is not None else None,
)
monkeypatch.setattr(runner_acp, "plan_runtime_turn", fake_plan)
@@ -177,3 +181,33 @@ def test_a_prompt_error_finishes_the_turn_with_that_error(monkeypatch, tmp_path)
def test_a_fixed_session_key_is_what_the_chat_keeps(monkeypatch, tmp_path):
vendor, _, _, _ = _turn(monkeypatch, tmp_path, "hi", report_session="agent:main:main")
assert vendor == "agent:main:main"
+
+
+def test_plan_mode_refuses_the_runtimes_permission_without_a_card(monkeypatch, tmp_path):
+ _, events, asked, _ = _turn(monkeypatch, tmp_path, "ASK now", mode="plan")
+ assert asked == []
+ assert _texts(events) == ["permission: no"]
+
+
+def test_a_runtime_that_keeps_running_after_its_answer_is_ended(monkeypatch, tmp_path):
+ monkeypatch.setattr(rc, "_ACP_EXIT_GRACE_S", 0.5)
+ released: list[int] = []
+ _, events, _, _ = _turn(monkeypatch, tmp_path, "HANG after this", released=released)
+ finished = _finished(events)
+ assert finished["status"] == "done", finished
+ assert finished["duration_ms"] < 60_000
+ assert released == [1]
+
+
+def test_the_turn_slot_is_released_when_the_runtime_cannot_start(monkeypatch, tmp_path):
+ released: list[int] = []
+ missing = str(tmp_path / "no-such-runtime.exe")
+ _, events, _, _ = _turn(monkeypatch, tmp_path, "hi", argv=[missing], released=released)
+ assert _finished(events)["status"] == "error"
+ assert released == [1]
+
+
+def test_the_turn_slot_is_released_once_after_a_normal_turn(monkeypatch, tmp_path):
+ released: list[int] = []
+ _turn(monkeypatch, tmp_path, "hello", released=released)
+ assert released == [1]
diff --git a/tests/unit/agent_runtimes/test_runtime_configs.py b/tests/unit/agent_runtimes/test_runtime_configs.py
index 49df9a787d..b08f4bd44e 100644
--- a/tests/unit/agent_runtimes/test_runtime_configs.py
+++ b/tests/unit/agent_runtimes/test_runtime_configs.py
@@ -94,11 +94,12 @@ def test_hermes_offers_jarvis_tools_directly(tmp_path):
assert HermesRuntime().config_for(_turn(tmp_path))["tools"]["tool_search"] is False
-def test_hermes_approvals_follow_the_chat_stance_and_never_the_guardian(tmp_path):
+def test_hermes_always_asks_jarvis_and_never_the_guardian(tmp_path):
+ """The stance is answered over ACP, so the file never depends on it."""
runtime = HermesRuntime()
- assert runtime.config_for(_turn(tmp_path))["approvals"]["mode"] == "off"
+ bypass = runtime.config_for(_turn(tmp_path))
asked = runtime.config_for(_turn(tmp_path, auto_approve=False))
- assert asked["approvals"]["mode"] == "manual"
+ assert bypass["approvals"] == asked["approvals"] == {"mode": "manual"}
def test_hermes_denied_shell_disables_its_terminal(tmp_path):
@@ -126,6 +127,14 @@ def test_openclaw_config_keeps_both_keys_out_of_the_file(tmp_path):
headers = config["mcp"]["servers"]["jarvis"]["headers"]
assert headers["Authorization"] == "Bearer ${JARVIS_CONTROL_API_KEY}"
assert headers["X-Jarvis-Chat-Session"] == "society:hermit"
+ run = OpenClawRuntime().config_for(
+ _turn(tmp_path, session_id="society:hermit:routine:t1:abc"), port=1, token=_TOKEN
+ )
+ # A routine run's tool calls carry its own session (approvals, grants).
+ assert (
+ run["mcp"]["servers"]["jarvis"]["headers"]["X-Jarvis-Chat-Session"]
+ == "society:hermit:routine:t1:abc"
+ )
def test_openclaw_never_runs_background_turns_or_persona_files(tmp_path):
@@ -141,15 +150,15 @@ def test_openclaw_never_runs_background_turns_or_persona_files(tmp_path):
assert config["gateway"]["bind"] == "loopback"
-def test_openclaw_exec_mode_follows_stance_and_grants(tmp_path):
+def test_openclaw_exec_mode_follows_grants_not_the_stance(tmp_path):
runtime = OpenClawRuntime()
- full = runtime.config_for(_turn(tmp_path), port=1, token=_TOKEN)
+ bypass = runtime.config_for(_turn(tmp_path), port=1, token=_TOKEN)
ask = runtime.config_for(_turn(tmp_path, auto_approve=False), port=1, token=_TOKEN)
denied = runtime.config_for(
_turn(tmp_path, denied_native=frozenset({"shell", "web"})), port=1, token=_TOKEN
)
- assert full["tools"]["exec"]["mode"] == "full"
- assert ask["tools"]["exec"]["mode"] == "ask"
+ assert bypass == ask
+ assert bypass["tools"]["exec"]["mode"] == "ask"
assert denied["tools"]["exec"]["mode"] == "deny"
assert "browser" in denied["tools"]["deny"]
@@ -262,3 +271,28 @@ def test_hermes_model_block_names_the_endpoint_for_session_restore(tmp_path):
route = ModelRoute("ollama", "qwen3", "http://127.0.0.1:11434/v1", "chat_completions", None)
local = HermesRuntime().config_for(_turn(tmp_path, route=route))["model"]
assert local["api_key"] == "no-key-required"
+
+
+def test_routine_runs_use_their_own_runtime_folder():
+ from jarvis.agent_runtimes.base import home_key
+
+ assert home_key("hermit", "society:hermit") == "hermit"
+ assert home_key("hermit", "society:hermit:routine:t1:abc") == "hermit~runs"
+
+
+async def test_turn_slots_run_one_turn_at_a_time():
+ import asyncio
+
+ from jarvis.agent_runtimes.base import TurnSlots
+
+ slots = TurnSlots()
+ release = await slots.acquire("hermit")
+ waiting = asyncio.ensure_future(slots.acquire("hermit"))
+ await asyncio.sleep(0.05)
+ assert not waiting.done()
+ other = await slots.acquire("hermit~runs") # another folder is independent
+ release()
+ release() # releasing twice is harmless
+ second = await asyncio.wait_for(waiting, timeout=1)
+ second()
+ other()
diff --git a/tests/unit/agent_runtimes/test_runtime_manager.py b/tests/unit/agent_runtimes/test_runtime_manager.py
index 132358e737..03b6875cfa 100644
--- a/tests/unit/agent_runtimes/test_runtime_manager.py
+++ b/tests/unit/agent_runtimes/test_runtime_manager.py
@@ -86,6 +86,9 @@ async def test_unknown_runtimes_are_refused(fakes):
def test_routes_list_status_and_start_jobs(fakes):
+ import jarvis.ui.web.agent_runtime_routes as routes
+
+ routes._USABLE_CACHE[:] = [float("-inf"), []]
app = FastAPI()
app.include_router(router)
app.state.config = SimpleNamespace(brain=SimpleNamespace(providers={}))
diff --git a/tests/unit/society/test_agent_runtime_field.py b/tests/unit/society/test_agent_runtime_field.py
index 91265f9983..ddd6778f63 100644
--- a/tests/unit/society/test_agent_runtime_field.py
+++ b/tests/unit/society/test_agent_runtime_field.py
@@ -153,3 +153,21 @@ async def test_switching_runtime_reseats_the_chat_and_drops_the_vendor_session(t
finally:
svc.store.close()
await rt.close()
+
+
+def test_a_routine_run_never_moves_a_subscription_seat_onto_an_api_key(monkeypatch):
+ from jarvis.society import routine_runner
+
+ owner = SimpleNamespace(runtime="hermes", provider="openai")
+ # The owner's own model keeps the owner's runtime.
+ assert routine_runner._run_runtime(owner, "openai") == "hermes"
+ # A pinned seat that is a subscription CLI runs on Jarvis instead.
+ monkeypatch.setattr(
+ "jarvis.agent_chat.service._claude_cli_installed", lambda: True
+ )
+ assert routine_runner._run_runtime(owner, "claude-api") == ""
+ # A pinned API-key seat keeps the runtime; an unsupported one never does.
+ assert routine_runner._run_runtime(owner, "openrouter") == "hermes"
+ assert routine_runner._run_runtime(owner, "openai-codex") == ""
+ jarvis_owner = SimpleNamespace(runtime="jarvis", provider="openai")
+ assert routine_runner._run_runtime(jarvis_owner, "openai") == ""