Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

fix(chats): a create that loses the claim checks it is the same request

raullopez-sandboxb369d215e0b9
diff --git a/core/switch_core/chats/service.py b/core/switch_core/chats/service.py
index 7dd3c6b49..fff1da5f5 100644
--- a/core/switch_core/chats/service.py
+++ b/core/switch_core/chats/service.py
@@ -1275,7 +1275,7 @@ class ChatService:
"Only the agent's owner can give it a greeting.",
)
if operation is None:
- await self._claim(
+ claimed = await self._claim(
session,
tenant_id=tenant_id,
user_id=user.id,
@@ -1286,6 +1286,17 @@ class ChatService:
room_id=None,
)
await session.commit()
+ if not claimed:
+ # The same request id, claimed first elsewhere: it has to
+ # be the same request before anything is done for it.
+ operation = await self._operation(session, user.id, request_id)
+ assert operation is not None
+ self._require_same(operation, "create_chat", payload_hash)
+ if operation.state == "done" and operation.room_id is not None:
+ room, _ = await self.require_member(
+ session, tenant_id, user, operation.room_id
+ )
+ return room
operation_id = self._operation_id(tenant_id, user.id, request_id)
found = await self._room_of_operation(session, operation_id)
if found is None:
diff --git a/core/tests/switch_core/gateway/test_chats_groups.py b/core/tests/switch_core/gateway/test_chats_groups.py
index 92be67912..f0cfb9f34 100644
--- a/core/tests/switch_core/gateway/test_chats_groups.py
+++ b/core/tests/switch_core/gateway/test_chats_groups.py
@@ -17,7 +17,7 @@ from switch_core.bridges.agent.protocol.agent_connections import AgentConnection
from switch_core.bridges.agent.registration_bootstrap import (
BOOTSTRAP_OWNER_MARKER_META_KEY,
)
-from switch_core.chats.service import ChatService
+from switch_core.chats.service import ChatError, ChatService
from switch_core.db.models import Agent, Client, Message, Room, User, room_agents
from switch_core.gateway import chats as chats_module
from tests.switch_core.gateway.conftest import (
@@ -1181,3 +1181,53 @@ async def test_mention_spans_place_people_and_agents_in_the_body(
mentionSpans=[{"kind": "user", "id": chats.admin.id, "offset": 0, "length": 6}],
)
assert _code(stranger) == "USER_NOT_IN_CHAT"
+
+
+async def test_one_request_id_with_two_payloads_on_two_processes_is_one_request(
+ chats: _Harness, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ agent = await chats.agent("helper", owner=chats.alice)
+ other = _second_process(chats)
+ both_read = asyncio.Barrier(2)
+ for service in (chats.service, other):
+ read = service._operation
+
+ async def read_then_wait(
+ session: AsyncSession, user_id: str, request_id: str, read: Any = read
+ ) -> Any:
+ found = await read(session, user_id, request_id)
+ # Both read "no operation" before either claims it.
+ if found is None:
+ await both_read.wait()
+ return found
+
+ monkeypatch.setattr(service, "_operation", read_then_wait)
+
+ async def create(service: ChatService, member: User) -> Any:
+ try:
+ return await service.create_chat(
+ "00000000-0000-0000-0000-000000000000",
+ chats.alice,
+ agent_ids=[agent.id],
+ member_user_ids=[member.id],
+ kind=None,
+ name=None,
+ request_id="c1",
+ greeting=None,
+ )
+ except ChatError as refused:
+ return refused
+
+ outcomes = await asyncio.gather(
+ create(chats.service, chats.bob), create(other, chats.admin)
+ )
+ rooms = [each for each in outcomes if isinstance(each, Room)]
+ refusals = [each for each in outcomes if isinstance(each, ChatError)]
+ assert len(rooms) == 1 and len(refusals) == 1
+ assert refusals[0].code == "REQUEST_REUSED"
+ async with chats.factory() as session:
+ assert await session.scalar(select(func.count()).select_from(Room)) == 1
+ members = await chats.client.get(
+ f"/chats/{rooms[0].id}/members", headers=chats.as_user(chats.alice)
+ )
+ assert len({m["userId"] for m in members.json()["members"]}) == 2