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 person and the group they make commit together

raullopez-sandbox283d41961673
diff --git a/core/switch_core/chats/service.py b/core/switch_core/chats/service.py
index a202dd40a..0d4266031 100644
--- a/core/switch_core/chats/service.py
+++ b/core/switch_core/chats/service.py
@@ -791,9 +791,10 @@ class ChatService:
async def _make_group(self, session: AsyncSession, room: Room) -> None:
"""Turn a Switch Console direct chat into a group, for good.
- Committed before the person or agent is added, so no message routes
- as direct once the room has them. A bridged room's type is its
- platform's and is never touched.
+ Written in the transaction that adds the person or agent, with the
+ room row locked, so no message routes as direct once the room has
+ them and a failed add leaves the chat as it was. A bridged room's
+ type is its platform's and is never touched.
"""
if room.channel_type != "direct" or "chat_operation_id" not in (
room.metadata_ or {}
@@ -815,12 +816,18 @@ class ChatService:
target = await self._require_tenant_member(
session, tenant_id, target_user_id
)
+ client = await self.member_actor(tenant_id, target)
+ async with tenant_session(self.session_factory, tenant_id) as session:
+ room = await self._locked_room(session, room.id)
# The creator is the direct chat's own person; anyone else makes a group.
if target_user_id not in (room.created_by, room.owner_id):
await self._make_group(session, room)
- await session.commit()
- client = await self.member_actor(tenant_id, target)
- await self._join(tenant_id, room, client)
+ with tenant_scope(tenant_id):
+ joined = await self._provisioning.record_join(
+ session, room.transport_room_id, client.transport_user_id
+ )
+ await session.commit()
+ joined()
await self._make_lasting(tenant_id, target_user_id, room.id)
async with tenant_session(self.session_factory, tenant_id) as session:
member_ids = [member.id for member, _ in await self.members(session, room)]
@@ -891,14 +898,24 @@ class ChatService:
async with tenant_session(self.session_factory, tenant_id) as session:
return await self._room(session, tenant_id, room_id)
+ async def _locked_room(self, session: AsyncSession, room_id: str) -> Room:
+ """The room row, locked against another membership change until this
+ transaction ends. `FOR NO KEY UPDATE`, which the key-share lock an
+ insert referencing the room takes does not conflict with."""
+ locked = await session.scalar(
+ select(Room)
+ .where(Room.id == room_id)
+ .with_for_update(key_share=True)
+ .execution_options(populate_existing=True)
+ )
+ assert locked is not None
+ return locked
+
async def _group_if_several_agents(self, session: AsyncSession, room: Room) -> None:
"""`_make_group` once the room holds more than one agent, in the
transaction that added the agent. The room row is locked before counting, so a
concurrent add counts this one's agent once this one commits."""
- locked = await session.scalar(
- select(Room).where(Room.id == room.id).with_for_update(key_share=True)
- )
- assert locked is not None
+ locked = await self._locked_room(session, room.id)
if len(await self._room_store.get_agent_ids(session, room.id)) > 1:
await self._make_group(session, locked)
diff --git a/core/switch_core/provisioning/port.py b/core/switch_core/provisioning/port.py
index 781ee6be2..00745a238 100644
--- a/core/switch_core/provisioning/port.py
+++ b/core/switch_core/provisioning/port.py
@@ -17,7 +17,11 @@ failure raises.
from __future__ import annotations
-from typing import Protocol, runtime_checkable
+from collections.abc import Callable
+from typing import TYPE_CHECKING, Protocol, runtime_checkable
+
+if TYPE_CHECKING:
+ from sqlalchemy.ext.asyncio import AsyncSession
@runtime_checkable
@@ -32,6 +36,14 @@ class Provisioning(Protocol):
"""Put a user in a room. Already a member is success."""
...
+ async def record_join(
+ self, session: AsyncSession, room_id: str, user_id: str
+ ) -> Callable[[], None]:
+ """Put a client that is never live in a room within the caller's
+ transaction; what to run once the caller has committed. Already a
+ member is success."""
+ ...
+
async def kick_user(self, room_id: str, user_id: str) -> None:
"""Take a user out of a room. Already out is success."""
...
diff --git a/core/switch_core/provisioning/postgres.py b/core/switch_core/provisioning/postgres.py
index 505f87ba8..411b62daf 100644
--- a/core/switch_core/provisioning/postgres.py
+++ b/core/switch_core/provisioning/postgres.py
@@ -31,6 +31,8 @@ from typing import TYPE_CHECKING
from switch_core.db.models import ClientRoom, Message
if TYPE_CHECKING:
+ from collections.abc import Callable
+
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
from switch_core.db.stores.client_store import ClientStore
@@ -116,35 +118,58 @@ class PostgresProvisioning:
return
async with self._session_factory() as session:
- if not await self._room_store.add_client(
- session, client_id, switch_room_id
- ):
- # Recorded by another writer meanwhile, which wrote the
- # arrival too.
- return
- arrival = Message(
- room_id=switch_room_id,
- transport_event_id=f"sw_{uuid.uuid4().hex}",
- sender_id=user_id,
- sender_client_id=client_id,
- sender_name=display_name,
- event_type=MEMBERSHIP_EVENT_TYPE,
- msgtype=None,
- body=None,
- formatted_body=None,
- thread_root_event_id=None,
- content={"membership": "join", "displayname": display_name},
- )
- routed = await self._router.store(
+ committed = await self._write_join(
session,
- tenant_id=tenant_id,
- room_id=switch_room_id,
- transport_room_id=room_id,
- message=arrival,
- attachments=[],
+ room_id,
+ user_id,
+ (switch_room_id, tenant_id, client_id, display_name),
)
await session.commit()
- routed.committed()
+ committed()
+
+ async def record_join(
+ self, session: AsyncSession, room_id: str, user_id: str
+ ) -> Callable[[], None]:
+ """Write a membership and its arrival in the caller's transaction, for
+ a client that is never live, so it commits with whatever else the
+ caller writes. Returns what to run once the caller has committed.
+ Already a member is success, with nothing to run."""
+ resolved = await self._resolve(session, room_id, user_id)
+ return await self._write_join(session, room_id, user_id, resolved)
+
+ async def _write_join(
+ self,
+ session: AsyncSession,
+ room_id: str,
+ user_id: str,
+ resolved: tuple[str, str, str, str],
+ ) -> Callable[[], None]:
+ switch_room_id, tenant_id, client_id, display_name = resolved
+ if not await self._room_store.add_client(session, client_id, switch_room_id):
+ # Recorded by another writer meanwhile, which wrote the arrival too.
+ return lambda: None
+ arrival = Message(
+ room_id=switch_room_id,
+ transport_event_id=f"sw_{uuid.uuid4().hex}",
+ sender_id=user_id,
+ sender_client_id=client_id,
+ sender_name=display_name,
+ event_type=MEMBERSHIP_EVENT_TYPE,
+ msgtype=None,
+ body=None,
+ formatted_body=None,
+ thread_root_event_id=None,
+ content={"membership": "join", "displayname": display_name},
+ )
+ routed = await self._router.store(
+ session,
+ tenant_id=tenant_id,
+ room_id=switch_room_id,
+ transport_room_id=room_id,
+ message=arrival,
+ attachments=[],
+ )
+ return routed.committed
async def kick_user(self, room_id: str, user_id: str) -> None:
"""Remove a membership, stopping a live client's delivery with it.
diff --git a/core/tests/switch_core/gateway/test_chats_groups.py b/core/tests/switch_core/gateway/test_chats_groups.py
index 665401c42..f7ec2ccce 100644
--- a/core/tests/switch_core/gateway/test_chats_groups.py
+++ b/core/tests/switch_core/gateway/test_chats_groups.py
@@ -5,13 +5,14 @@ a thread — against real Postgres, through the shared harness."""
from __future__ import annotations
import asyncio
+from collections.abc import Callable
import httpx
import pytest
-from sqlalchemy import select
+from sqlalchemy import func, select
from sqlalchemy.ext.asyncio import AsyncSession
-from switch_core.db.models import Agent, Room, room_agents
+from switch_core.db.models import Agent, Message, Room, room_agents
from tests.switch_core.gateway.conftest import _Harness
from tests.switch_core.gateway.test_chats_routes import (
_code,
@@ -177,16 +178,55 @@ async def test_an_owner_grant_does_not_make_a_group(chats: _Harness) -> None:
assert await _addressed(chats, room_id, agent, "hello") is True
-async def test_concurrent_adds_leave_one_consistent_group(chats: _Harness) -> None:
+async def test_a_person_who_fails_to_commit_leaves_the_chat_direct(
+ chats: _Harness, monkeypatch: pytest.MonkeyPatch
+) -> None:
agent = await chats.agent("helper", owner=chats.alice)
room_id = await chats.new_chat(chats.alice, agent, "c1")
+ provisioning = chats.service._provisioning
+ record_join = provisioning.record_join
+
+ async def join_then_fail(
+ session: AsyncSession, room_id: str, user_id: str
+ ) -> Callable[[], None]:
+ await record_join(session, room_id, user_id)
+ raise RuntimeError("lost before commit")
+
+ monkeypatch.setattr(provisioning, "record_join", join_then_fail)
+ with pytest.raises(RuntimeError, match="lost before commit"):
+ await chats.invite(chats.alice, room_id, chats.bob)
+ assert await _channel_type(chats, room_id) == "direct"
+ assert await _reads(chats, chats.bob, room_id) == 403
+ async with chats.factory() as session:
+ bob = await chats.service.member_client(session, chats.bob.id)
+ assert bob is not None
+ arrivals = await session.scalar(
+ select(func.count())
+ .select_from(Message)
+ .where(Message.room_id == room_id, Message.sender_client_id == bob.id)
+ )
+ assert arrivals == 0
+
+
+async def test_concurrent_adds_leave_one_consistent_group(
+ chats: _Harness, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ agent = await chats.agent("helper", owner=chats.alice)
+ room_id = await chats.new_chat(chats.alice, agent, "c1")
+ lock = chats.service._locked_room
+ both_ready = asyncio.Barrier(2)
+
+ async def together(session: AsyncSession, locked_room_id: str) -> Room:
+ await both_ready.wait()
+ return await lock(session, locked_room_id)
+
+ monkeypatch.setattr(chats.service, "_locked_room", together)
responses = await asyncio.gather(
chats.invite(chats.alice, room_id, chats.bob),
chats.invite(chats.alice, room_id, chats.admin),
- chats.invite(chats.alice, room_id, chats.bob),
)
- assert [r.status_code for r in responses] == [200, 200, 200]
+ assert [r.status_code for r in responses] == [200, 200]
assert await _channel_type(chats, room_id) == "channel_private"
members = await chats.client.get(
f"/chats/{room_id}/members", headers=chats.as_user(chats.alice)
diff --git a/core/tests/switch_core/gateway/test_held_connections.py b/core/tests/switch_core/gateway/test_held_connections.py
index 38d9eff64..678de0c37 100644
--- a/core/tests/switch_core/gateway/test_held_connections.py
+++ b/core/tests/switch_core/gateway/test_held_connections.py
@@ -87,6 +87,11 @@ class _Provisioning:
def __init__(self, calls: _Calls) -> None:
self._calls = calls
+ async def record_join(
+ self, session: object, room_id: str, user_id: str
+ ) -> Callable[[], None]:
+ raise NotImplementedError
+
async def invite_to_room(self, transport_room_id: str, user_id: str) -> None:
self._calls.record("matrix invite")
diff --git a/core/tests/switch_core/test_room_service_change_bridge.py b/core/tests/switch_core/test_room_service_change_bridge.py
index 727099d1b..e4405e7f9 100644
--- a/core/tests/switch_core/test_room_service_change_bridge.py
+++ b/core/tests/switch_core/test_room_service_change_bridge.py
@@ -1,5 +1,6 @@
from __future__ import annotations
+from collections.abc import Callable
from types import SimpleNamespace
from typing import Any
@@ -142,6 +143,11 @@ class _FakeProvisioning:
def __init__(self, events: list[Any]) -> None:
self._events = events
+ async def record_join(
+ self, session: object, room_id: str, user_id: str
+ ) -> Callable[[], None]:
+ raise NotImplementedError
+
async def invite_to_room(
self, transport_room_id: str, transport_user_id: str
) -> None:
diff --git a/core/tests/switch_core/test_room_service_default_bridge_multi_tenant.py b/core/tests/switch_core/test_room_service_default_bridge_multi_tenant.py
index bb28624fc..7b58b8650 100644
--- a/core/tests/switch_core/test_room_service_default_bridge_multi_tenant.py
+++ b/core/tests/switch_core/test_room_service_default_bridge_multi_tenant.py
@@ -25,6 +25,7 @@ that resolved the *wrong* tenant's default would fail loudly here too.
from __future__ import annotations
import uuid
+from collections.abc import Callable
from typing import Any
import pytest
@@ -52,6 +53,11 @@ class _FakeProvisioning:
self._n += 1
return f"!room-{self._n}:switch.local"
+ async def record_join(
+ self, session: object, room_id: str, user_id: str
+ ) -> Callable[[], None]:
+ raise NotImplementedError
+
async def invite_to_room(self, room_id: str, user_id: str) -> None:
self.invited.append((room_id, user_id))
diff --git a/core/tests/switch_core/test_room_service_foreign_bridge.py b/core/tests/switch_core/test_room_service_foreign_bridge.py
index fe6b88a59..c191b73a3 100644
--- a/core/tests/switch_core/test_room_service_foreign_bridge.py
+++ b/core/tests/switch_core/test_room_service_foreign_bridge.py
@@ -16,6 +16,7 @@ records every platform call so a test can assert none was made.
from __future__ import annotations
import uuid
+from collections.abc import Callable
from typing import Any
import pytest
@@ -41,6 +42,11 @@ class _FakeProvisioning:
self._n += 1
return f"!room-{self._n}:switch.local"
+ async def record_join(
+ self, session: object, room_id: str, user_id: str
+ ) -> Callable[[], None]:
+ raise NotImplementedError
+
async def invite_to_room(self, room_id: str, user_id: str) -> None:
return None
diff --git a/core/tests/switch_core/test_room_service_pool_holds.py b/core/tests/switch_core/test_room_service_pool_holds.py
index 870158ae9..7723393f4 100644
--- a/core/tests/switch_core/test_room_service_pool_holds.py
+++ b/core/tests/switch_core/test_room_service_pool_holds.py
@@ -16,6 +16,7 @@ written until the transport side is in place.
from __future__ import annotations
+from collections.abc import Callable
from types import SimpleNamespace
from typing import Any
@@ -62,6 +63,11 @@ class _RecordingProvisioning:
self._tracker = tracker
self.calls: list[tuple[str, str, int]] = []
+ async def record_join(
+ self, session: object, room_id: str, user_id: str
+ ) -> Callable[[], None]:
+ raise NotImplementedError
+
async def invite_to_room(
self, transport_room_id: str, transport_user_id: str
) -> None:
diff --git a/core/tests/switch_core/test_room_service_reconcile.py b/core/tests/switch_core/test_room_service_reconcile.py
index e5d12ef9d..e698efddf 100644
--- a/core/tests/switch_core/test_room_service_reconcile.py
+++ b/core/tests/switch_core/test_room_service_reconcile.py
@@ -1,5 +1,6 @@
from __future__ import annotations
+from collections.abc import Callable
from types import SimpleNamespace
from typing import Any
@@ -74,6 +75,11 @@ class _FakeProvisioning:
def __init__(self) -> None:
self.invited: list[tuple[str, str]] = []
+ async def record_join(
+ self, session: object, room_id: str, user_id: str
+ ) -> Callable[[], None]:
+ raise NotImplementedError
+
async def invite_to_room(
self, transport_room_id: str, transport_user_id: str
) -> None:
diff --git a/core/tests/switch_core/test_room_service_tenant_bindings.py b/core/tests/switch_core/test_room_service_tenant_bindings.py
index dc81072b0..c41ada544 100644
--- a/core/tests/switch_core/test_room_service_tenant_bindings.py
+++ b/core/tests/switch_core/test_room_service_tenant_bindings.py
@@ -38,6 +38,7 @@ from __future__ import annotations
import asyncio
import inspect
import uuid
+from collections.abc import Callable
from types import SimpleNamespace
from typing import Any
from unittest.mock import MagicMock
@@ -143,6 +144,11 @@ class _FakeProvisioning:
self.invited: list[tuple[str, str]] = []
self._fail_for = fail_for or set()
+ async def record_join(
+ self, session: object, room_id: str, user_id: str
+ ) -> Callable[[], None]:
+ raise NotImplementedError
+
async def invite_to_room(
self, transport_room_id: str, transport_user_id: str
) -> None: