Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

fix(computer): start a driver-retired session again instead of dying (#2838)

Maxc5f21910ca30
diff --git a/core/computer_server.py b/core/computer_server.py
index 773271f55..4b2cd06a2 100644
--- a/core/computer_server.py
+++ b/core/computer_server.py
@@ -57,6 +57,15 @@ _UPSTREAM_DETAIL_LIMIT = 2048
_UPSTREAM_DIAGNOSTIC_SCAN_LIMIT = 8192
_UPSTREAM_DIAGNOSTIC_LOG_LIMIT = 20
_ADMISSION_RETRY_LIMIT = 1
+# Cua retires a named session after five idle minutes and then refuses every
+# ordinary call for that label before dispatching it; only ``start_session``
+# revives the name. The daemon gate reports it as ``tool_invocation_failed``
+# and the in-tool admission as an unstructured refusal, both with this text.
+_DRIVER_SESSION_ENDED_CODES = frozenset({"tool_invocation_failed", "upstream_tool_error"})
+_DRIVER_SESSION_ENDED_DETAIL = "session has ended"
+_SESSION_LIFECYCLE_TOOLS = frozenset(
+ {"start_session", "end_session", "get_session", "list_sessions"}
+)
_NO_SCRIPT_HANDLE = object()
_PROXY_PROCESS_ENV_KEYS = (
"HOME",
@@ -108,6 +117,26 @@ class _DaemonGenerationChanged(RuntimeError):
"""The desktop state changed before this call could be admitted."""
+class _DriverSessionEnded(RuntimeError):
+ """The driver refused a call because it had already retired the session."""
+
+
+def _driver_session_ended(result: Mapping[str, Any]) -> bool:
+ """Whether the driver refused this call, undispatched, as an ended session."""
+
+ if result.get("isError") is not True:
+ return False
+ structured = result.get("structuredContent")
+ if not isinstance(structured, Mapping):
+ return False
+ detail = structured.get("detail")
+ return (
+ structured.get("code") in _DRIVER_SESSION_ENDED_CODES
+ and isinstance(detail, str)
+ and detail.startswith(_DRIVER_SESSION_ENDED_DETAIL)
+ )
+
+
class Upstream(Protocol):
serial: int
@@ -1165,8 +1194,13 @@ class ComputerUseServer:
except ComputerServerError as exc:
return _error_result(exc.code, str(exc), dispatched=False)
- for admission_attempt in range(_ADMISSION_RETRY_LIMIT + 1):
+ admission_attempt = 0
+ # Revive a driver-retired session at most once per call. A second
+ # refusal right after a successful start is returned as-is.
+ revival_available = name not in _SESSION_LIFECYCLE_TOOLS
+ while True:
upstream: Upstream | None = None
+ session_state: _SessionState | None = None
pre_forward = True
try:
status = await asyncio.to_thread(self._status_reader)
@@ -1206,11 +1240,14 @@ class ComputerUseServer:
}
if discovery_tool not in self._upstream_accepts_session:
forwarded_discovery.pop("session", None)
- return await self._upstream_call(
+ discovered = await self._upstream_call(
upstream,
discovery_tool,
forwarded_discovery,
)
+ if revival_available and _driver_session_ended(discovered):
+ raise _DriverSessionEnded(discovery_tool)
+ return discovered
try:
validation_error = await validate_handle_is_current(
@@ -1244,6 +1281,13 @@ class ComputerUseServer:
name,
forwarded,
)
+ if _driver_session_ended(result):
+ if revival_available:
+ raise _DriverSessionEnded(name)
+ # Still refused after a fresh start: the driver
+ # ran nothing, so say so instead of leaving the
+ # outcome ambiguous.
+ return _not_dispatched_error_result(result)
if name in _OBSERVE_TOOLS and not result.get("isError"):
self._record_observation(arguments, result, session_state)
@@ -1258,10 +1302,23 @@ class ComputerUseServer:
self._commit_session_ended(session_state)
self._sessions.pop(session, None)
return result
+ except _DriverSessionEnded:
+ # The driver ran nothing. Its element tokens and snapshots
+ # for the ended episode are retired, so drop the matching
+ # Controller observations and start the name again.
+ if session_state is not None:
+ self._commit_session_ended(session_state)
+ revival_available = False
+ logger.info(
+ "computer-use driver retired session %s; starting it again",
+ session,
+ )
+ continue
except _DaemonGenerationChanged:
if upstream is not None:
await self._discard_upstream(upstream)
if admission_attempt < _ADMISSION_RETRY_LIMIT:
+ admission_attempt += 1
continue
return _error_result(
"computer_use_unavailable",
@@ -1308,8 +1365,6 @@ class ComputerUseServer:
error_type=type(exc).__name__,
)
- raise AssertionError("computer-use admission loop exhausted")
-
async def end_session_if_active(self, session: str) -> dict[str, Any]:
"""Retire a session created by an earlier call without creating one."""
diff --git a/tests/test_computer_script_sessions.py b/tests/test_computer_script_sessions.py
index abc1303c8..28641328c 100644
--- a/tests/test_computer_script_sessions.py
+++ b/tests/test_computer_script_sessions.py
@@ -420,3 +420,153 @@ def test_script_routes_reject_an_old_turn_before_reaching_computer_server() -> N
},
),
]
+
+
+def test_driver_retired_session_keeps_cli_working_after_scripts_and_turns(
+ tmp_path,
+) -> None:
+ """A script ending, or a Turn ending, never leaves Computer Use dead.
+
+ Live acceptance: a script waited 300 s on ``avibe.confirm`` and timed out;
+ the driver's five-minute idle TTL had retired the named session meanwhile,
+ and every later CLI call in that Avibe Session, in this Turn and the next,
+ was refused with "session has ended". Here the fake driver retires the name
+ at those same points and each following CLI observe must still succeed.
+ """
+
+ from core.computer_server import ComputerUseServer
+ from core.computer_use import ComputerUseStatus
+ from tests.test_computer_server import DriverSessionUpstream, _state
+
+ async def exercise() -> tuple[list[dict], list[str]]:
+ state_path, state = _state(tmp_path)
+ driver = DriverSessionUpstream()
+ controller = SimpleNamespace(
+ computer_use_server=ComputerUseServer(
+ state_path=state_path,
+ status_reader=lambda: ComputerUseStatus("ready", None, state),
+ upstream_factory=lambda _state: asyncio.sleep(0, result=driver),
+ ),
+ computer_script_sessions=None,
+ controller_id="controller-test",
+ _session_id_from_context=lambda context: context.platform_specific[
+ "agent_session_id"
+ ],
+ )
+ app = internal_server.create_app(controller)
+ tasks = [asyncio.create_task(asyncio.sleep(60)) for _ in range(2)]
+ controller.session_turns.in_flight["ses-script"] = session_turns.Turn(
+ task=tasks[0],
+ context=_context("turn-1"),
+ logical_turn_id="turn-1",
+ )
+ outcomes: list[dict] = []
+ try:
+ async with httpx.AsyncClient(
+ transport=httpx.ASGITransport(app=app),
+ base_url="http://test",
+ ) as client:
+
+ async def open_script(timeout_seconds: float) -> str:
+ opened = await client.post(
+ "/internal/computer-script/open",
+ json={
+ "caller_session_id": "ses-script",
+ "timeout_seconds": timeout_seconds,
+ },
+ )
+ return opened.json()["connection_token"]
+
+ async def script_observe(token: str) -> dict:
+ response = await client.post(
+ "/internal/computer-script/request",
+ json={
+ "connection_token": token,
+ "tool": "list_apps",
+ "arguments": {},
+ "descriptor": None,
+ },
+ )
+ return response.json()
+
+ async def cli_observe() -> dict:
+ response = await client.post(
+ "/internal/computer-use",
+ json={
+ "caller_session_id": "ses-script",
+ "tool": "list_apps",
+ "arguments": {},
+ },
+ )
+ return response.json()
+
+ # 1. A script times out (e.g. waiting on an unanswered confirm).
+ timed_out = await open_script(0.05)
+ outcomes.append(await script_observe(timed_out))
+ await asyncio.sleep(0.1)
+ outcomes.append(await script_observe(timed_out))
+ driver.expire_idle("ses-script")
+ outcomes.append(await cli_observe())
+
+ # 2. A script succeeds and closes its connection.
+ succeeded = await open_script(30)
+ outcomes.append(await script_observe(succeeded))
+ closed = await client.post(
+ "/internal/computer-script/close",
+ json={"connection_token": succeeded},
+ )
+ assert closed.json() == {"ok": True, "closed": True}
+ driver.expire_idle("ses-script")
+ outcomes.append(await cli_observe())
+
+ # 3. The next Turn of the same Avibe Session.
+ controller.session_turns.in_flight["ses-script"] = session_turns.Turn(
+ task=tasks[1],
+ context=_context("turn-2"),
+ logical_turn_id="turn-2",
+ )
+ driver.expire_idle("ses-script")
+ outcomes.append(await cli_observe())
+ outcomes.append(await cli_observe())
+ finally:
+ controller.computer_script_sessions.close_all()
+ for task in tasks:
+ task.cancel()
+ try:
+ await task
+ except asyncio.CancelledError:
+ pass
+ names = [params["name"] for _method, params in driver.calls]
+ return outcomes, names
+
+ outcomes, names = asyncio.run(exercise())
+ (
+ script_first,
+ script_late,
+ cli_after_timeout,
+ script_ok,
+ cli_after_success,
+ cli_next_turn,
+ cli_next_turn_again,
+ ) = outcomes
+ assert script_first["ok"] is True
+ assert script_late["error"] == "script_timeout"
+ assert script_ok["ok"] is True
+ for cli in (cli_after_timeout, cli_after_success, cli_next_turn, cli_next_turn_again):
+ assert cli["ok"] is True, cli
+ assert cli["session_id"] == "ses-script"
+ assert names == [
+ "start_session",
+ "list_apps", # script, before its timeout
+ "list_apps", # CLI: refused, the driver retired the name
+ "start_session",
+ "list_apps",
+ "list_apps", # successful script
+ "list_apps", # CLI after the script closed: refused
+ "start_session",
+ "list_apps",
+ "list_apps", # next Turn: refused
+ "start_session",
+ "list_apps",
+ "list_apps", # still the same live driver session
+ ]
diff --git a/tests/test_computer_server.py b/tests/test_computer_server.py
index 8fe603aa3..ab0161520 100644
--- a/tests/test_computer_server.py
+++ b/tests/test_computer_server.py
@@ -222,6 +222,72 @@ class FakeUpstream:
self.alive = False
+_DRIVER_LIFECYCLE_TOOLS = frozenset(
+ {"start_session", "end_session", "get_session", "list_sessions"}
+)
+
+
+def driver_session_ended_refusal(tool: str, session: str) -> dict[str, Any]:
+ """The exact wire result cua-driver 0.31.0 returns for an ended name.
+
+ ``serve.rs`` refuses the call before dispatch, and the proxy wraps the
+ daemon error through ``tool_error_result`` + ``conforming_error_envelope``.
+ """
+
+ return {
+ "jsonrpc": "2.0",
+ "id": 1,
+ "result": {
+ "content": [
+ {
+ "type": "text",
+ "text": (
+ f"session has ended; tool call '{tool}' was rejected. "
+ f"Call start_session with session '{session}' to start "
+ "it again, or use a new session label."
+ ),
+ }
+ ],
+ "isError": True,
+ "structuredContent": {
+ "exit_code": 1,
+ "code": "tool_invocation_failed",
+ },
+ },
+ }
+
+
+class DriverSessionUpstream(FakeUpstream):
+ """FakeUpstream with Cua's named-session tombstones.
+
+ The real driver retires a named session after five idle minutes
+ (``DEFAULT_SESSION_IDLE_TTL``) or on ``end_session``; afterwards only
+ ``start_session`` revives the name and every other call is refused before
+ it is dispatched.
+ """
+
+ def __init__(self, *, revivable: bool = True) -> None:
+ super().__init__()
+ self.blocked_tools = set()
+ self.ended: set[str] = set()
+ self.revivable = revivable
+
+ def expire_idle(self, session: str) -> None:
+ self.ended.add(session)
+
+ async def request(self, method: str, params: Mapping[str, Any]) -> dict[str, Any]:
+ name = params.get("name")
+ session = (params.get("arguments") or {}).get("session")
+ if name == "start_session" and self.revivable:
+ self.ended.discard(session)
+ elif name == "end_session":
+ self.ended.add(session)
+ elif name not in _DRIVER_LIFECYCLE_TOOLS and session in self.ended:
+ self.calls.append((method, json.loads(json.dumps(params))))
+ return driver_session_ended_refusal(str(name), str(session))
+ return await super().request(method, params)
+
+
class _TestStdin:
def __init__(self) -> None:
self.writes: list[bytes] = []
@@ -1276,6 +1342,161 @@ async def test_named_sessions_start_independently_and_revive_after_end(
]
+async def test_driver_idle_retired_session_is_started_again_once(
+ tmp_path: Path,
+) -> None:
+ """A Session idle past the driver TTL keeps working instead of dying."""
+
+ state_path, state = _state(tmp_path)
+ upstream = DriverSessionUpstream()
+ server = ComputerUseServer(
+ state_path=state_path,
+ status_reader=lambda: ComputerUseStatus("ready", None, state),
+ upstream_factory=lambda _state: asyncio.sleep(0, result=upstream),
+ )
+
+ first = await server.call_tool("list_apps", {"session": "ses-a"})
+ upstream.expire_idle("ses-a")
+ second = await server.call_tool("list_apps", {"session": "ses-a"})
+ third = await server.call_tool("list_apps", {"session": "ses-a"})
+
+ assert first.get("isError") is not True
+ assert second.get("isError") is not True
+ assert third.get("isError") is not True
+ names = [params["name"] for _method, params in upstream.calls]
+ assert names == [
+ "start_session",
+ "list_apps",
+ "list_apps",
+ "start_session",
+ "list_apps",
+ "list_apps",
+ ]
+
+
+async def test_driver_retired_session_drops_observations_before_input(
+ tmp_path: Path,
+) -> None:
+ """The driver retires the ended episode's tokens, so input must re-observe."""
+
+ state_path, state = _state(tmp_path)
+ upstream = DriverSessionUpstream()
+ server = ComputerUseServer(
+ state_path=state_path,
+ status_reader=lambda: ComputerUseStatus("ready", None, state),
+ upstream_factory=lambda _state: asyncio.sleep(0, result=upstream),
+ )
+ target = {"session": "ses-a", "pid": 7, "window_id": 9}
+ await server.call_tool("get_window_state", target)
+ before = await server.call_tool("click", {**target, "x": 1, "y": 2})
+ upstream.expire_idle("ses-a")
+ refused = await server.call_tool("click", {**target, "x": 1, "y": 2})
+ await server.call_tool("get_window_state", target)
+ after = await server.call_tool("click", {**target, "x": 1, "y": 2})
+
+ assert before.get("isError") is not True
+ assert refused["structuredContent"]["code"] == "observe_first"
+ assert refused["structuredContent"]["dispatched"] is False
+ assert after.get("isError") is not True
+ names = [params["name"] for _method, params in upstream.calls]
+ assert names == [
+ "start_session",
+ "get_window_state",
+ "click",
+ "click",
+ "start_session",
+ "get_window_state",
+ "click",
+ ]
+
+
+async def test_driver_session_refused_after_fresh_start_is_not_dispatched(
+ tmp_path: Path,
+) -> None:
+ """Revival is bounded to one start, and the final refusal is unambiguous."""
+
+ state_path, state = _state(tmp_path)
+ upstream = DriverSessionUpstream(revivable=False)
+ server = ComputerUseServer(
+ state_path=state_path,
+ status_reader=lambda: ComputerUseStatus("ready", None, state),
+ upstream_factory=lambda _state: asyncio.sleep(0, result=upstream),
+ )
+
+ await server.call_tool("list_apps", {"session": "ses-a"})
+ upstream.expire_idle("ses-a")
+ refused = await server.call_tool("list_apps", {"session": "ses-a"})
+
+ assert refused["isError"] is True
+ assert refused["structuredContent"]["code"] == "tool_invocation_failed"
+ assert refused["structuredContent"]["dispatched"] is False
+ assert refused["structuredContent"]["detail"].startswith("session has ended;")
+ names = [params["name"] for _method, params in upstream.calls]
+ assert names == [
+ "start_session",
+ "list_apps",
+ "list_apps",
+ "start_session",
+ "list_apps",
+ ]
+
+
+async def test_in_tool_session_ended_refusal_also_starts_the_session_again(
+ tmp_path: Path,
+) -> None:
+ """The driver's in-tool admission refusal has a second, unstructured shape."""
+
+ state_path, state = _state(tmp_path)
+ upstream = DriverSessionUpstream()
+ server = ComputerUseServer(
+ state_path=state_path,
+ status_reader=lambda: ComputerUseStatus("ready", None, state),
+ upstream_factory=lambda _state: asyncio.sleep(0, result=upstream),
+ )
+ refusals = 0
+ base_request = upstream.request
+
+ async def request(method: str, params: Mapping[str, Any]) -> dict[str, Any]:
+ nonlocal refusals
+ if params.get("name") == "list_windows" and refusals == 0:
+ refusals += 1
+ upstream.calls.append((method, json.loads(json.dumps(params))))
+ return {
+ "jsonrpc": "2.0",
+ "id": 1,
+ "result": {
+ "content": [{"type": "text", "text": "session has ended"}],
+ "isError": True,
+ "structuredContent": {
+ "status": "refused",
+ "refusal": {
+ "code": "session_ended",
+ "message": "session has ended",
+ },
+ },
+ },
+ }
+ return await base_request(method, params)
+
+ upstream.request = request # type: ignore[method-assign]
+ await server.call_tool("list_apps", {"session": "ses-a"})
+ result = await server.call_tool("list_windows", {"session": "ses-a"})
+
+ assert result.get("isError") is not True
+ names = [params["name"] for _method, params in upstream.calls]
+ assert names == [
+ "start_session",
+ "list_apps",
+ "list_windows",
+ "start_session",
+ "list_windows",
+ ]
+
+
@pytest.mark.asyncio
async def test_end_session_if_active_releases_without_starting_unknown_session(
tmp_path: Path,