Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.
Python agent-orchestrationaiai-agentschatopsdiscordlangchainmcpmicrosoft-teamsmulti-agentopen-sourceopenaiself-hostedslack
fix(core): serialize incarnation draws so concurrent opens keep their batch
raullopez-sandbox2889f05c2c70
diff --git a/core/switch_core/bridges/agent/protocol/connection_rows.py b/core/switch_core/bridges/agent/protocol/connection_rows.pyindex c978ca245..12420ac2a 100644--- a/core/switch_core/bridges/agent/protocol/connection_rows.py+++ b/core/switch_core/bridges/agent/protocol/connection_rows.py@@ -159,6 +159,7 @@ class SharedConnections:self._bus = busself._lease = leaseself._incarnations: list[int] = []+ self._incarnation_lock = asyncio.Lock()# Bumped every time an agent's state here is set from the rows or# written to them, so a full pass that read the rows before a move# does not set them back to what they were before it.@@ -221,11 +222,12 @@ class SharedConnections:latest = max((row.incarnation for row in rows.connections.values()), default=0)- self._incarnations = [n for n in self._incarnations if n > latest]- if not self._incarnations:- self._incarnations.extend(- await self._store.incarnations(session, INCARNATION_BATCH)- )+ async with self._incarnation_lock:+ self._incarnations = [n for n in self._incarnations if n > latest]+ if not self._incarnations:+ self._incarnations = await self._store.incarnations(+ session, INCARNATION_BATCH+ )self._apply(agent_id, tenant_id, rows)# Compared with what is recorded rather than with the copy just# set from it: a claim of a connection this process has closed,diff --git a/core/tests/switch_core/bridges/agent/protocol/test_connections_across_processes.py b/core/tests/switch_core/bridges/agent/protocol/test_connections_across_processes.pyindex 37278e9bd..099566270 100644--- a/core/tests/switch_core/bridges/agent/protocol/test_connections_across_processes.py+++ b/core/tests/switch_core/bridges/agent/protocol/test_connections_across_processes.py@@ -1172,3 +1172,55 @@ class TestADeletedAgent:(conn,) = openedassert conn.closure is Noneassert a.registry.get(CONN) is conn+++class TestIncarnationsDrawnAtOnce:+ async def test_two_agents_opening_while_a_batch_is_drawn_both_get_one(+ self,+ processes: tuple[Process, Process],+ session_factory: SessionFactory,+ monkeypatch: pytest.MonkeyPatch,+ ) -> None:+ """Two agents connecting at once on a fresh process: the second+ reaches the draw while the first's batch is still being fetched. Both+ open, each on a number of its own."""+ a, _ = processes+ await seed_agent(session_factory, "agent-2")+ draw = AgentConnectionStore.incarnations+ read_agent = AgentConnectionStore.read_agent+ drawing = asyncio.Event()+ release = asyncio.Event()+ second_read = asyncio.Event()++ async def held_draw(+ store: AgentConnectionStore, session: AsyncSession, count: int+ ) -> list[int]:+ drawing.set()+ await release.wait()+ return await draw(store, session, count)++ async def noting_read(+ store: AgentConnectionStore,+ session: AsyncSession,+ agent_id: str,+ **kwargs: Any,+ ) -> Any:+ rows = await read_agent(store, session, agent_id, **kwargs)+ if agent_id == "agent-2":+ second_read.set()+ return rows++ monkeypatch.setattr(AgentConnectionStore, "incarnations", held_draw)+ monkeypatch.setattr(AgentConnectionStore, "read_agent", noting_read)++ first = asyncio.create_task(_open(a))+ await asyncio.wait_for(drawing.wait(), WAIT_SECONDS)+ second = asyncio.create_task(+ _open(a, agent_id="agent-2", connection_id="conn-2")+ )+ await asyncio.wait_for(second_read.wait(), WAIT_SECONDS)+ release.set()+ opened = await asyncio.wait_for(asyncio.gather(first, second), WAIT_SECONDS)++ generations = [conn.stream_generation for conn in opened]+ assert len(set(generations)) == 2