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(agentic-ide): end a hosted Claude turn when no app is attached

rubenluetke10-beepa82af76a66ab
diff --git a/jarvis/agent_chat/runner_cli.py b/jarvis/agent_chat/runner_cli.py
index 1148071549..adf1cc5803 100644
--- a/jarvis/agent_chat/runner_cli.py
+++ b/jarvis/agent_chat/runner_cli.py
@@ -3228,6 +3228,9 @@ async def _run_cli_once(
),
keep_stdin=plan.keep_stdin,
meta=_hosted_meta(handle, runner, plan, cwd, started_at),
+ # Claude Code keeps reading stdin after its result line; the
+ # host closes it there so the CLI ends with no app attached.
+ close_stdin_on="result" if plan.keep_stdin and plan.shape == "claude" else "",
)
if hosted is not None:
proc = hosted
diff --git a/jarvis/agent_chat/turn_host.py b/jarvis/agent_chat/turn_host.py
index ec552f6af0..dad9a1739f 100644
--- a/jarvis/agent_chat/turn_host.py
+++ b/jarvis/agent_chat/turn_host.py
@@ -111,6 +111,10 @@ class HostedTurn:
exit_code: int | None = None
streaming: bool = False
pumps: list[asyncio.Task[None]] = field(default_factory=list)
+ #: A stdout JSON line of this ``type`` ends the turn: stdin is closed so
+ #: the CLI exits, even with no app attached to do it (Claude Code's
+ #: ``result``; it otherwise waits on its open stdin for ever).
+ close_stdin_on: str = ""
@property
def alive(self) -> bool:
@@ -397,6 +401,7 @@ class TurnHost:
started_at=time.time(),
# The spawning client reads from the first line on.
streaming=True,
+ close_stdin_on=str(frame.get("close_stdin_on") or ""),
)
self._turns[turn.turn_id] = turn
if stdin_data is not None:
@@ -463,6 +468,8 @@ class TurnHost:
continue
turn.lines.append(text)
turn.kept_bytes += len(raw)
+ if turn.close_stdin_on and _line_type(text) == turn.close_stdin_on:
+ _close_stdin(turn)
client = self._client
if turn.streaming and client is not None and not client.closed:
await client.send(
@@ -510,6 +517,17 @@ async def _write_stdin(turn: HostedTurn, text: str) -> None:
logger.debug("Turn host: stdin write for {} failed: {}", turn.turn_id, exc)
+def _line_type(text: str) -> str:
+ """The ``type`` of a JSON stdout line, or "" for anything else."""
+ if '"type"' not in text:
+ return ""
+ try:
+ obj = json.loads(text)
+ except ValueError:
+ return ""
+ return str(obj.get("type") or "") if isinstance(obj, dict) else ""
+
+
def _close_stdin(turn: HostedTurn) -> None:
stdin = turn.proc.stdin
if stdin is None or stdin.is_closing():
diff --git a/jarvis/agent_chat/turn_host_client.py b/jarvis/agent_chat/turn_host_client.py
index a3619e28ba..baa8a6b9f0 100644
--- a/jarvis/agent_chat/turn_host_client.py
+++ b/jarvis/agent_chat/turn_host_client.py
@@ -324,6 +324,7 @@ class TurnHostClient:
stdin: str | None,
keep_stdin: bool,
meta: dict[str, Any],
+ close_stdin_on: str = "",
) -> HostedCli:
reply = await self._request(
{
@@ -333,6 +334,7 @@ class TurnHostClient:
"env": dict(env) if env is not None else None,
"stdin": stdin,
"keep_stdin": keep_stdin,
+ "close_stdin_on": close_stdin_on,
"meta": meta,
}
)
@@ -614,6 +616,7 @@ async def spawn(
stdin: str | None,
keep_stdin: bool,
meta: dict[str, Any],
+ close_stdin_on: str = "",
) -> HostedCli | None:
"""Start ``argv`` in the turn host, or ``None`` when no host can take it."""
client = await get_client(start=True)
@@ -621,7 +624,13 @@ async def spawn(
return None
try:
return await client.spawn(
- argv, cwd=cwd, env=env, stdin=stdin, keep_stdin=keep_stdin, meta=meta
+ argv,
+ cwd=cwd,
+ env=env,
+ stdin=stdin,
+ keep_stdin=keep_stdin,
+ meta=meta,
+ close_stdin_on=close_stdin_on,
)
except (ConnectionError, RuntimeError, OSError) as exc:
log.warning("turn host: spawn refused (%s) — the CLI runs in-process", exc)
diff --git a/tests/unit/agent_chat/test_turn_host.py b/tests/unit/agent_chat/test_turn_host.py
index fdbd870eed..f1e0ec4762 100644
--- a/tests/unit/agent_chat/test_turn_host.py
+++ b/tests/unit/agent_chat/test_turn_host.py
@@ -517,3 +517,37 @@ def test_the_app_start_does_nothing_outside_a_real_app_process() -> None:
host_mode.reset()
assert agent_chat_routes.schedule_turn_reattach(SimpleNamespace()) is None
+
+
+# Prints its result line, then keeps reading stdin until it is closed.
+_RESULT_THEN_WAIT = (
+ "import json, sys\n"
+ "print(json.dumps({'type': 'result', 'subtype': 'success'}), flush=True)\n"
+ "sys.stdin.read()\n"
+)
+
+
+async def test_the_host_closes_stdin_after_the_result_when_no_app_is_attached(
+ tmp_path: Path,
+) -> None:
+ host, task, port = await _start_host(tmp_path)
+ try:
+ client = await _client(port)
+ cli = await client.spawn(
+ [sys.executable, "-c", _RESULT_THEN_WAIT],
+ cwd=str(tmp_path),
+ env=None,
+ stdin="prompt\n",
+ keep_stdin=True,
+ meta={"turn_id": "t6"},
+ close_stdin_on="result",
+ )
+ client.detach() # the app is gone before the CLI even answered
+ for _ in range(200):
+ turn = host._turns.get(cli.host_id)
+ if turn is not None and turn.exit_code is not None:
+ break
+ await asyncio.sleep(0.05)
+ assert host._turns[cli.host_id].exit_code == 0
+ finally:
+ await _stop(host, task)