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(workbench): web Chat stop reuses the IM /stop path to interrupt the backend turn

cyh29d8e82330e8
diff --git a/core/internal_server.py b/core/internal_server.py
index 03a3f7bf1..c8d507563 100644
--- a/core/internal_server.py
+++ b/core/internal_server.py
@@ -162,6 +162,17 @@ def create_app(controller: "Controller") -> FastAPI:
)
if task.done():
return {"ok": True, "session_id": session_id, "status": "already_finished"}
+ # Interrupt the agent's backend turn through the SAME path the IM
+ # ``/stop`` command uses, so the underlying agent run is actually
+ # stopped (Claude interrupt / Codex turn-interrupt / OpenCode abort) —
+ # not just this SSE proxy task. Without it the agent keeps running and
+ # its late reply leaks into the next turn's stream. Reuses
+ # ``command_handler.handle_stop`` verbatim with the session's own
+ # routing context, so stop is one mechanism across IM, CLI, and web.
+ try:
+ await controller.command_handler.handle_stop(_build_session_context(session_id))
+ except Exception:
+ logger.exception("internal cancel: backend stop failed for session=%s", session_id)
task.cancel()
return {"ok": True, "session_id": session_id, "status": "cancel_requested"}
@@ -289,8 +300,34 @@ def _build_dispatch_payload(payload: dict[str, Any]) -> tuple[str, MessageContex
if not isinstance(session_id, str) or not session_id.strip():
raise ValueError("session_id is required")
- user_id = payload.get("user_id") or "workbench"
- channel_id = payload.get("channel_id") or session_id
+ context = _build_session_context(
+ session_id,
+ user_id=payload.get("user_id"),
+ channel_id=payload.get("channel_id"),
+ platform=payload.get("platform"),
+ thread_id=payload.get("thread_id"),
+ message_id=payload.get("message_id"),
+ )
+ return text, context
+
+
+def _build_session_context(
+ session_id: str,
+ *,
+ user_id: Optional[str] = None,
+ channel_id: Optional[str] = None,
+ platform: Optional[str] = None,
+ thread_id: Optional[str] = None,
+ message_id: Optional[str] = None,
+) -> MessageContext:
+ """Build the avibe ``MessageContext`` for a workbench session.
+
+ Shared by the dispatch endpoint and the cancel endpoint so a stop reuses
+ the exact same session-routing context (chosen agent / model / effort,
+ native session id, workdir) the turn ran under — that's what lets cancel
+ reuse the IM ``/stop`` path to interrupt the right backend session.
+ Defaults to ``platform="avibe"``.
+ """
platform_specific: dict[str, Any] = {"workbench_session_id": session_id}
session_row = _lookup_session(session_id)
@@ -310,15 +347,14 @@ def _build_dispatch_payload(payload: dict[str, Any]) -> tuple[str, MessageContex
if session_row.get("agent_name"):
platform_specific["vibe_agent_name"] = session_row["agent_name"]
- context = MessageContext(
- user_id=str(user_id),
- channel_id=str(channel_id),
- platform=payload.get("platform") or "avibe",
- thread_id=payload.get("thread_id"),
- message_id=payload.get("message_id"),
+ return MessageContext(
+ user_id=str(user_id or "workbench"),
+ channel_id=str(channel_id or session_id),
+ platform=platform or "avibe",
+ thread_id=thread_id,
+ message_id=message_id,
platform_specific=platform_specific,
)
- return text, context
def _lookup_session(session_id: str) -> Optional[dict[str, Any]]:
diff --git a/tests/test_internal_server.py b/tests/test_internal_server.py
index b2c07cc62..a61e8f4c9 100644
--- a/tests/test_internal_server.py
+++ b/tests/test_internal_server.py
@@ -75,6 +75,10 @@ def _build_controller_double(handler=None):
sink["done_event"].set()
controller.mark_turn_complete = _mark_turn_complete
+
+ # Cancel reuses the IM /stop path to interrupt the backend turn.
+ controller.command_handler = MagicMock()
+ controller.command_handler.handle_stop = AsyncMock(return_value=True)
return controller
@@ -415,13 +419,20 @@ def test_cancel_returns_404_when_session_not_in_flight():
assert body["code"] == "not_in_flight"
-def test_cancel_marks_in_flight_session_as_requested():
- """When a dispatch is in flight, ``cancel`` finds the task and asks
- asyncio to cancel it. The endpoint returns immediately — completion
- of the cancel is observed by the SSE consumer through a ``cancelled``
+def test_cancel_marks_in_flight_session_as_requested(monkeypatch, tmp_path):
+ """When a dispatch is in flight, ``cancel`` interrupts the backend turn
+ through the shared ``/stop`` path AND cancels the SSE proxy task. The
+ endpoint returns immediately; the SSE consumer observes a ``cancelled``
chunk.
"""
+ # Hermetic SQLite so cancel's session-context lookup doesn't touch the
+ # real home (the test session isn't persisted; lookup resolves to None).
+ monkeypatch.setenv("VIBE_REMOTE_HOME", str(tmp_path))
+ from storage.importer import ensure_sqlite_state
+
+ ensure_sqlite_state()
+
started = asyncio.Event()
cancelled = asyncio.Event()
@@ -471,6 +482,8 @@ def test_cancel_marks_in_flight_session_as_requested():
# The SSE consumer sees the cancelled chunk before turn.end.
chunk_kinds = [data.get("kind") for name, data in events if name == "turn.chunk"]
assert "cancelled" in chunk_kinds
+ # Cancel also interrupted the backend turn via the shared /stop path.
+ controller.command_handler.handle_stop.assert_awaited()
def test_dispatch_emits_error_chunk_on_handler_exception():