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(agents): a placement in a room the agent is no longer in is left out, not the whole set refused

raullopez-sandbox1746e10b78d1
diff --git a/core/switch_core/bridges/agent/api/handlers.py b/core/switch_core/bridges/agent/api/handlers.py
index 39faae991..953e48d52 100644
--- a/core/switch_core/bridges/agent/api/handlers.py
+++ b/core/switch_core/bridges/agent/api/handlers.py
@@ -1558,9 +1558,12 @@ async def connection_placements(
here after each change and on every (re)connect, so a restart of either
side converges on what the watcher knows. A room another connection holds
is taken over, as `connect_to_room` takes it, and that connection is sent
- `room_released`. Every room must be one the agent belongs to, and every
- session id at most `MAX_ID_LENGTH` characters; a request that breaks
- either is refused whole and changes nothing.
+ `room_released`. A session placed in a room the agent does not belong to
+ (any more: the room was deleted, or the agent taken out) is left out and
+ named in `refused`; the rest are placed. A watcher stating the whole set
+ each time would otherwise have every placement refused, again and again,
+ for one room that has gone. A session id longer than `MAX_ID_LENGTH`
+ characters refuses the request whole and changes nothing.
"""
if agent_id != agent.id:
raise HTTPException(
@@ -1575,20 +1578,24 @@ async def connection_placements(
)
conn = await _admitted_connection(protocol, agent, req)
+ refused: dict[str, str] = {}
for room_id in sorted(set(req.placements.values())):
try:
await protocol.require_room_member(agent.id, room_id)
- except ValueError as exc:
- raise HTTPException(status_code=404, detail=str(exc)) from exc
- except PermissionError as exc:
- raise HTTPException(status_code=403, detail=str(exc)) from exc
+ except (ValueError, PermissionError) as exc:
+ refused[room_id] = str(exc)
+ placements = {
+ session_id: room_id
+ for session_id, room_id in req.placements.items()
+ if room_id not in refused
+ }
async with protocol.connections.slots(agent.id):
async with protocol.connections.recorded(agent.id):
conn = _current_connection(protocol, agent, req)
before = protocol.connections.connection_placements(conn)
try:
- released = protocol.connections.replace_placements(conn, req.placements)
+ released = protocol.connections.replace_placements(conn, placements)
except ValueError as exc:
raise HTTPException(status_code=400, detail=str(exc)) from exc
@@ -1596,7 +1603,7 @@ async def connection_placements(
# recorded, so a move that fails hands nothing over, and before the
# slots are let go, so a later claim of the room cannot have its count
# overwritten by this one.
- for session_id, room_id in req.placements.items():
+ for session_id, room_id in placements.items():
if before.get(session_id) != room_id:
await protocol.event_buffer.hand_counting_to(
agent.tenant_id,
@@ -1627,6 +1634,11 @@ async def connection_placements(
}
for lost in released
],
+ "refused": [
+ {"session_id": session_id, "room_id": room_id, "reason": refused[room_id]}
+ for session_id, room_id in req.placements.items()
+ if room_id in refused
+ ],
}
diff --git a/core/tests/switch_core/bridges/agent/api/test_connection_placements.py b/core/tests/switch_core/bridges/agent/api/test_connection_placements.py
index 989c5cce8..cc9e08e88 100644
--- a/core/tests/switch_core/bridges/agent/api/test_connection_placements.py
+++ b/core/tests/switch_core/bridges/agent/api/test_connection_placements.py
@@ -94,6 +94,7 @@ async def test_placements_replace_what_the_connection_had(
"placements": {"session-1": ROOM_B},
"rooms": [ROOM_B],
"released": [],
+ "refused": [],
}
assert protocol.connections.session_in_room(AGENT_ID, ROOM_A) is None
@@ -131,20 +132,43 @@ async def test_a_room_another_connection_holds_is_taken_over(
@pytest.mark.asyncio
-async def test_a_room_the_agent_is_not_in_refuses_the_whole_request(
+async def test_a_room_the_agent_is_not_in_is_left_out_and_named_and_the_rest_placed(
db_buffer: EventBuffer,
) -> None:
protocol = _Protocol(db_buffer)
conn = _open(protocol, "watcher")
await _place(protocol, conn, {"session-1": ROOM_A})
- with pytest.raises(HTTPException) as refused:
- await _place(protocol, conn, {"session-1": ROOM_B, "session-2": FOREIGN_ROOM})
+ answer = await _place(
+ protocol, conn, {"session-1": ROOM_B, "session-2": FOREIGN_ROOM}
+ )
- assert refused.value.status_code == 403
- assert FOREIGN_ROOM in str(refused.value.detail)
- assert protocol.connections.connection_placements(conn) == {"session-1": ROOM_A}
- assert conn.rooms == {ROOM_A}
+ assert answer["placements"] == {"session-1": ROOM_B}
+ assert [(each["session_id"], each["room_id"]) for each in answer["refused"]] == [
+ ("session-2", FOREIGN_ROOM)
+ ]
+ assert protocol.connections.connection_placements(conn) == {"session-1": ROOM_B}
+ assert conn.rooms == {ROOM_B}
+
+
+async def test_a_room_that_is_gone_is_left_out_every_time_it_is_stated(
+ db_buffer: EventBuffer,
+) -> None:
+ protocol = _Protocol(db_buffer)
+
+ async def gone(agent_id: str, room_id: str) -> None:
+ if room_id == "room-deleted":
+ raise ValueError(f"Room not found: {room_id}")
+
+ protocol.require_room_member = gone # type: ignore[method-assign]
+ conn = _open(protocol, "watcher")
+ for _ in range(2):
+ answer = await _place(
+ protocol, conn, {"session-1": ROOM_A, "stale": "room-deleted"}
+ )
+ assert answer["placements"] == {"session-1": ROOM_A}
+ assert [each["room_id"] for each in answer["refused"]] == ["room-deleted"]
@pytest.mark.asyncio