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(browser): stop the owning planner when an action is denied

rubenluetke10-beepa7d4206ee88b
diff --git a/jarvis/agent_chat/service.py b/jarvis/agent_chat/service.py
index 340b4dec27..c55fb02d45 100644
--- a/jarvis/agent_chat/service.py
+++ b/jarvis/agent_chat/service.py
@@ -794,7 +794,8 @@ class AgentChatService:
)
return turn_id
- async def cancel(self, session_id: str) -> bool:
+ def signal_cancel(self, session_id: str) -> bool:
+ """Stop planning synchronously before releasing an in-flight tool reply."""
run = self._running.get(session_id)
if run is None or run.task is None or run.task.done():
return False
@@ -803,13 +804,21 @@ class AgentChatService:
fut = self._approvals.get(aid)
if fut is not None and not fut.done():
fut.set_result("cancel")
+ return True
+
+ async def cancel(self, session_id: str) -> bool:
+ run = self._running.get(session_id)
+ if not self.signal_cancel(session_id):
+ return False
+ assert run is not None and run.task is not None
try:
await asyncio.wait_for(asyncio.shield(run.task), timeout=15.0)
except TimeoutError:
run.task.cancel()
await asyncio.gather(run.task, return_exceptions=True)
except asyncio.CancelledError:
- if asyncio.current_task().cancelling():
+ task = asyncio.current_task()
+ if task is not None and task.cancelling():
raise
except Exception as exc: # noqa: BLE001 — the task reported its own end already
log.debug("agent chat cancel: task ended with %s", exc)
diff --git a/jarvis/ui/web/society_routes.py b/jarvis/ui/web/society_routes.py
index e4a9e718cd..bcbd2bf94d 100644
--- a/jarvis/ui/web/society_routes.py
+++ b/jarvis/ui/web/society_routes.py
@@ -996,6 +996,7 @@ async def list_approvals(request: Request, agent_id: str | None = None) -> dict[
async def resolve_approval(
approval_id: str, body: ResolveApprovalBody, request: Request
) -> dict[str, Any]:
+ """Resolve an approval; denying a live browser action also stops its owning chat."""
from jarvis.society.proposals import kind_of
rt = await _runtime(request)
@@ -1010,6 +1011,29 @@ async def resolve_approval(
"detail": f"use POST /api/society/proposals/{approval_id}/resolve",
},
)
+ if (
+ current is not None
+ and not body.approve
+ and str(current.state) == "pending"
+ and current.capability == "core:browser"
+ and current.action.get("resume_in_place")
+ ):
+ # A denied browser action must not release the parent planner to try
+ # the same operation through a desktop or shell tool. Signal before
+ # resolving the approval, while the live ownership still identifies
+ # this exact turn. An old approval cannot stop a newer/unrelated chat.
+ live = getattr(getattr(rt, "browser", None), "live", None)
+ session = getattr(live, "sessions", {}).get(current.agent_id)
+ chat = getattr(request.app.state, "agent_chat", None)
+ if (
+ session is not None
+ and current.trace_id
+ and session.active_trace == current.trace_id
+ and session.active_chat
+ and session.run_lock.locked()
+ and chat is not None
+ ):
+ chat.signal_cancel(session.active_chat)
try:
item = await rt.approvals.resolve(approval_id, approve=body.approve, note=body.note)
except KeyError as exc:
diff --git a/tests/contract/test_browser_contract.py b/tests/contract/test_browser_contract.py
index d6da3493df..ed7e75ebbe 100644
--- a/tests/contract/test_browser_contract.py
+++ b/tests/contract/test_browser_contract.py
@@ -329,6 +329,65 @@ async def test_bare_cancel_does_not_mark_the_turn_stopped(tmp_path):
assert ("test-bot", "trace-1") in live.stopped_turns
+ "state,active_trace,chat_id,approve,running,stops",
+ [
+ ("pending", "turn", "root-chat", False, True, True),
+ ("pending", "turn", "society:test", False, True, True),
+ ("pending", "new-turn", "root-chat", False, True, False),
+ ("denied", "turn", "root-chat", False, True, False),
+ ("approved", "turn", "root-chat", False, True, False),
+ ("pending", "turn", "", False, True, False),
+ ("pending", "turn", "root-chat", True, True, False),
+ ("pending", "turn", "root-chat", False, False, False),
+ ],
+)
+async def test_browser_denial_stops_only_its_planner_before_releasing_reply(
+ monkeypatch, state, active_trace, chat_id, approve, running, stops
+):
+ import asyncio
+
+ from jarvis.ui.web import society_routes as routes
+
+ seen = []
+ lock = asyncio.Lock()
+ if running:
+ await lock.acquire()
+ session = SimpleNamespace(active_trace=active_trace, active_chat=chat_id, run_lock=lock)
+ item = SimpleNamespace(
+ agent_id="test",
+ trace_id="turn",
+ capability="core:browser",
+ state=state,
+ action={"resume_in_place": True},
+ to_dict=lambda: {},
+ )
+
+ async def get(_):
+ return item
+
+ async def resolve(*_args, **_kwargs):
+ seen.append("resolve")
+ return item
+
+ async def runtime(_):
+ return SimpleNamespace(
+ approvals=SimpleNamespace(get=get, resolve=resolve),
+ browser=SimpleNamespace(live=SimpleNamespace(sessions={"test": session})),
+ )
+
+ request = SimpleNamespace(
+ app=SimpleNamespace(
+ state=SimpleNamespace(
+ agent_chat=SimpleNamespace(signal_cancel=lambda sid: seen.append(sid)),
+ )
+ )
+ )
+ monkeypatch.setattr(routes, "_runtime", runtime)
+ await routes.resolve_approval("approval", routes.ResolveApprovalBody(approve=approve), request)
+ assert seen == ([chat_id, "resolve"] if stops else ["resolve"])
+
+
async def test_jarvis_chat_uses_the_lead_browser_with_its_selected_model(tmp_path, monkeypatch):
from jarvis.agent_chat.surface_kits import kit_for
from jarvis.agent_chat.tool_catalog import build_catalog, resolve_choices
diff --git a/tests/unit/agent_chat/test_cancel_signal.py b/tests/unit/agent_chat/test_cancel_signal.py
new file mode 100644
index 0000000000..4cf0768137
--- /dev/null
+++ b/tests/unit/agent_chat/test_cancel_signal.py
@@ -0,0 +1,27 @@
+"""Nested browser denials stop the owner before the rejected tool returns."""
+
+import asyncio
+from types import SimpleNamespace
+
+from jarvis.agent_chat.service import AgentChatService
+
+
+async def test_cancel_signal_is_immediate_and_does_not_touch_another_chat():
+ loop = asyncio.get_running_loop()
+ own = SimpleNamespace(task=loop.create_future(), cancel=asyncio.Event())
+ other = SimpleNamespace(task=loop.create_future(), cancel=asyncio.Event())
+ service = object.__new__(AgentChatService)
+ service._running = {"own": own, "other": other}
+ service._approvals = {"a": loop.create_future(), "b": loop.create_future()}
+ service._approval_session = {"a": "own", "b": "other"}
+ try:
+ assert service.signal_cancel("own")
+ assert own.cancel.is_set()
+ assert service._approvals["a"].result() == "cancel"
+ assert not other.cancel.is_set()
+ assert not service._approvals["b"].done()
+ assert not service.signal_cancel("missing")
+ finally:
+ own.task.cancel()
+ other.task.cancel()
+ service._approvals["b"].cancel()
diff --git a/tests/unit/agent_chat/test_controls.py b/tests/unit/agent_chat/test_controls.py
index 2c6d530795..58f0b87bc2 100644
--- a/tests/unit/agent_chat/test_controls.py
+++ b/tests/unit/agent_chat/test_controls.py
@@ -320,7 +320,9 @@ def test_message_uses_executor_exact_text_sender_and_idempotency(monkeypatch: An
return None
runtime = SimpleNamespace(
- conversations=SimpleNamespace(checkpoint=lambda _: (0, "")), turn_completed=completed
+ conversations=SimpleNamespace(checkpoint=lambda _: (0, "")),
+ turn_completed=completed,
+ browser=SimpleNamespace(live=SimpleNamespace(sessions={})),
)
monkeypatch.setattr("jarvis.society.runtime.current_runtime", lambda: runtime)
monkeypatch.setattr("jarvis.society.agent_tools.MessageAgentTool", MessageTool)