Self-improving AI Employees for everyone
Python ai-agentsai-employeeai-employeesassistantautonomous-agentsco-employeescompanygrok-botinstinctloop-agentsmuseown-your-datapersonalself-hosted
fix(chat): send a message before open threads hear of it
trohitg7d4c403f12a5
diff --git a/docs-internal/chat_protocol.md b/docs-internal/chat_protocol.mdindex eaf92409..d033f03d 100644--- a/docs-internal/chat_protocol.md+++ b/docs-internal/chat_protocol.md@@ -308,7 +308,9 @@ newer version wins in place.`"default"` session, which keeps its unscoped delivery.- **Dispatch.** The `chat_message_received` event (`services/chat/events.py`, source `opencompany://services/chat`)has the run id as its CloudEvent id and carries `message_id` and `run_id` in `data`, so the listener's child run- id is `<slug>-<trigger label>-<run id>`. It is never broadcast.+ id is `<slug>-<trigger label>-<run id>`. It is never broadcast. Open threads hear of the message (`chat.updated`)+ only after it is dispatched, so the sender's answer, which admits the run into their store, reaches them before+ their thread can read the run: told first, the thread showed the run live and read it with `get_chat_run`.- **Start and finish.** MachinaWorkflow, behind the `machina-chat-run-v1` patch, reads the run id only from an eventwith that source and type, claims the run (`chat_run.start`: `pending` or `queued` to `running`, recording theTemporal workflow and run ids) once the firing trigger's output is stored, passes `run_scope {run_id, session_id}`@@ -552,7 +554,8 @@ conversation store's locks (`conversation_lock`), and is refused with:After it commits, the Context listeners hear of each conversation changed, the drafts the runs on the part left madethat still wait are cancelled (`approval_lifecycle` `cancelled`), and an `[update]` note (`branch:<run id>`) tells the-employee what those runs sent anyway. `chat.updated` follows (role `user` for an edit, null for a retry or a switch).+employee what those runs sent anyway. `chat.updated` follows (role `user` for an edit, null for a retry or a switch),+after the dispatch for an edit or a retry, as for a new message.## Feedbackdiff --git a/server/services/chat/handlers.py b/server/services/chat/handlers.pyindex 2e6bddb8..caca40a4 100644--- a/server/services/chat/handlers.py+++ b/server/services/chat/handlers.py@@ -308,7 +308,11 @@ async def handle_send_chat_message(data: Dict[str, Any], websocket: WebSocket) -row, run = admission.message, admission.runif admission.created:- await announce_chat_updated(session_id, "user")+ # Sent before open threads hear of it, so this answer, which+ # registers the run with the sender, reaches the sender before their+ # thread reads the run: told first, their thread showed it live and+ # asked for it (get_chat_run). Sending never raises (emit is+ # fail-soft), so threads always hear of it.await _dispatch(session_id,scope.workflow_id,@@ -318,6 +322,7 @@ async def handle_send_chat_message(data: Dict[str, Any], websocket: WebSocket) -timestamp=timestamp,attachments=attachments,)+ await announce_chat_updated(session_id, "user")response: Dict[str, Any] = {"success": True,@@ -615,10 +620,10 @@ def _branch_refusal(exc: "branches.BranchRefused") -> Dict[str, Any]:async def _answer_branch(database: Any, moved: "branches.Moved", context: Dict[str, Any], *, role: Optional[str], timestamp: str) -> Dict[str, Any]:"""After an edit or a retry committed: settle what the part left made,- tell open threads, and send the message to the chat triggers."""+ send the message to the chat triggers, then tell open threads."""await branches.after_move(database, moved)- await announce_chat_updated(moved.session_id, role)message, run = moved.result["message"], moved.result["run"]+ # Sent first, as a new message is (handle_send_chat_message).await _dispatch(moved.session_id,moved.workflow_id,@@ -628,6 +633,7 @@ async def _answer_branch(database: Any, moved: "branches.Moved", context: Dict[stimestamp=timestamp,attachments=list(message.get("attachments") or []),)+ await announce_chat_updated(moved.session_id, role)response: Dict[str, Any] = {"success": True, "message_id": message["uid"], "run_id": run.run_id}if context.get("delivery") is not None:response["delivery"] = context["delivery"]diff --git a/server/tests/services/chat/_helpers.py b/server/tests/services/chat/_helpers.pyindex 14156607..824976d8 100644--- a/server/tests/services/chat/_helpers.py+++ b/server/tests/services/chat/_helpers.py@@ -74,3 +74,17 @@ async def talking(database, workflow_id: str = "wf", status: str = "running", gedef chat_updates(frames) -> List[Dict[str, Any]]:return [frame["data"]["data"] for frame in frames if frame["type"] == "chat.updated"]+++def updates_at_dispatch(chat, monkeypatch) -> List[int]:+ """How many ``chat.updated`` frames had gone out as each message was+ dispatched (the ``chat`` fixture's dispatch still records it)."""+ seen: List[int] = []+ dispatch = chat.handlers.dispatch_chat_message_received++ async def recording(event_data, **kwargs):+ seen.append(len(chat_updates(chat.frames)))+ await dispatch(event_data, **kwargs)++ monkeypatch.setattr(chat.handlers, "dispatch_chat_message_received", recording)+ return seendiff --git a/server/tests/services/chat/test_branches.py b/server/tests/services/chat/test_branches.pyindex bcf3a950..cefd9549 100644--- a/server/tests/services/chat/test_branches.py+++ b/server/tests/services/chat/test_branches.py@@ -15,7 +15,7 @@ import pytestfrom services.agent_context.conversation import load_conversation, save_conversationfrom services.chat import branches, ledger, notes-from tests.services.chat._helpers import add_control, talking+from tests.services.chat._helpers import add_control, chat_updates, talking, updates_at_dispatchAGENT = "wf:aiAgent:1"CLAIM = dict(temporal_workflow_id="tw-1", temporal_run_id="tr-1")@@ -138,6 +138,16 @@ async def test_an_edit_goes_beside_the_message_and_the_agent_forgets_what_followassert chat.dispatched[-1]["data"]["message"] == "Book Sunday" and chat.dispatched[-1]["event_id"] == edited["run_id"]+async def test_an_edit_is_sent_before_open_threads_hear_of_it(chat, monkeypatch):+ first = await conversation(chat)+ before = len(chat_updates(chat.frames))+ seen = updates_at_dispatch(chat, monkeypatch)+ await chat.handlers.handle_edit_chat_message(+ {"session_id": "wf", "message_id": first["message_id"], "message": "Book Sunday", "expected_revision": await revision(chat)}, None+ )+ assert seen == [before] and len(chat_updates(chat.frames)) == before + 1++async def test_switching_back_restores_what_the_agent_remembered_there(chat):first = await conversation(chat)edited = await chat.handlers.handle_edit_chat_message(diff --git a/server/tests/services/chat/test_handlers.py b/server/tests/services/chat/test_handlers.pyindex b5ac5f72..182725ca 100644--- a/server/tests/services/chat/test_handlers.py+++ b/server/tests/services/chat/test_handlers.py@@ -10,7 +10,15 @@ from __future__ import annotationsimport asynciofrom services.chat import ledger-from tests.services.chat._helpers import FakeSocket, add_control, chat_trigger, chat_updates, saved_workflow, talking+from tests.services.chat._helpers import (+ FakeSocket,+ add_control,+ chat_trigger,+ chat_updates,+ saved_workflow,+ talking,+ updates_at_dispatch,+)async def send(chat, message="Book Saturday", session_id="wf", socket=None, **extra):@@ -37,6 +45,15 @@ async def test_a_message_the_employee_will_answer_starts_a_run(chat):assert chat_updates(chat.frames) == [{"workflow_id": "wf", "session_id": "wf", "role": "user"}]+async def test_the_message_is_sent_before_open_threads_hear_of_it(chat, monkeypatch):+ # Told first, the sender's thread could read the run before the answer+ # registering it arrived, and ask the server for it.+ await talking(chat.database)+ seen = updates_at_dispatch(chat, monkeypatch)+ await send(chat)+ assert seen == [0] and len(chat_updates(chat.frames)) == 1++async def test_a_paused_employee_queues_the_run(chat):await talking(chat.database, status="paused")result = await send(chat)