Skip to content

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

Merge remote-tracking branch 'origin/work/e2e-scope-gaps' into e2e-testing

Petr Bauch5f558087341a
diff --cc console/tooling-e2e/src/clients/switch-client.ts
index 798b103e2,65a1715c3..4292b349c
--- a/console/tooling-e2e/src/clients/switch-client.ts
+++ b/console/tooling-e2e/src/clients/switch-client.ts
@@@ -278,61 -501,50 +501,102 @@@ export class SwitchClient
return typeof status === 'string' ? status : null;
}
+ /**
+ * `PUT /gateway/agents/{id}/name` — rename an agent and set its display name
+ * in the same step. Returns the name the server now holds.
+ */
+ async renameAgent(
+ agentId: string,
+ name: string,
+ displayName: string | null
+ ): Promise<{ name: string; displayName: string | null; bridgeFailures: string[] }> {
+ const body = await this.gateway<{
+ agent: { name: string; display_name: string | null };
+ bridge_failures: string[];
+ }>('PUT', `/agents/${agentId}/name`, { name, display_name: displayName });
+ return {
+ name: body.agent.name,
+ displayName: body.agent.display_name,
+ bridgeFailures: body.bridge_failures,
+ };
+ }
+
+ /**
+ * `PUT /gateway/agents/{id}/addressing-policy` with no policy, so anyone in
+ * the room may address the agent, not only its owner.
+ */
+ async openAddressing(agentId: string): Promise<void> {
+ await this.gateway('PUT', `/agents/${agentId}/addressing-policy`, { policy: null });
+ }
+
+ /**
+ * `POST /gateway/collaborations/{bridge}/identities` on the default bridge
+ * of `bridgeType` — link a chat account to the signed-in Switch user, so
+ * Switch can recognise that person in a room (what the rename and delete
+ * chat commands check).
+ */
+ async claimIdentity(
+ bridgeType: string,
+ externalUserId: string,
+ username: string
+ ): Promise<void> {
+ const bridges = await this.gateway<
+ { bridge_id: string; bridge_type: string; is_default?: boolean }[]
+ >('GET', '/collaborations');
+ const ofType = bridges.filter((row) => row.bridge_type === bridgeType);
+ const bridge = ofType.find((row) => row.is_default) ?? ofType[0];
+ if (!bridge) throw new Error(`No ${bridgeType} collaboration bridge on this Switch server`);
+ await this.gateway('POST', `/collaborations/${bridge.bridge_id}/identities`, {
+ external_user_id: externalUserId,
+ username,
+ user_id: null,
+ });
+ }
+
async listAgents(): Promise<AgentSummary[]> {
- const rows = await this.gateway<{ id: string; name: string }[]>('GET', '/agents');
- return rows.map((row) => ({ id: row.id, name: row.name }));
+ const rows = await this.gateway<{ id: string; name: string; created_at: string }[]>(
+ 'GET',
+ '/agents'
+ );
+ return rows.map((row) => ({ id: row.id, name: row.name, createdAt: row.created_at }));
+ }
+
+ /** `GET /gateway/auth/session` — the workspace this admin session is bound to. */
+ async tenantId(): Promise<string> {
+ const state = await this.gateway<{ state: string; tenant: { id: string } | null }>(
+ 'GET',
+ '/auth/session'
+ );
+ if (state.tenant === null) {
+ throw new Error(
+ `The gateway admin's session is bound to no workspace (state '${state.state}').`
+ );
+ }
+ return state.tenant.id;
+ }
+
+ /**
+ * `GET /gateway/tenants/{tenant_id}/usage` — what each client spent, per
+ * metric and model, over `[since, until)`. Counted in whole UTC hours.
+ */
+ async usage(tenantId: string, since: Date, until: Date): Promise<UsageTotal[]> {
+ const query = `since=${encodeURIComponent(since.toISOString())}&until=${encodeURIComponent(until.toISOString())}`;
+ const rows = await this.gateway<
+ {
+ metric: string;
+ client_id: string;
+ client_name: string | null;
+ model: string;
+ amount: number;
+ }[]
+ >('GET', `/tenants/${tenantId}/usage?${query}`);
+ return rows.map((row) => ({
+ metric: row.metric,
+ clientId: row.client_id,
+ clientName: row.client_name,
+ model: row.model,
+ amount: row.amount,
+ }));
}
/** The bridge id of the default collaboration bridge of `type`. */
diff --cc core/switch_core/bridges/collaboration/mattermost/adapter.py
index 4dd9c0056,ac805174e..0870d1afc
--- a/core/switch_core/bridges/collaboration/mattermost/adapter.py
+++ b/core/switch_core/bridges/collaboration/mattermost/adapter.py
@@@ -301,9 -304,46 +307,47 @@@ def _ephemeral(text: str) -> dict[str,
return {"ephemeral_text": text, "skip_slack_parsing": True}
+ class _BotSocketConnection(ClientConnection):
+ """A bot socket's `websockets` connection, remembered so `_close_loop` can
+ close it: mattermostdriver holds it in a local variable and leaves it open
+ when its loop stops, so its transport and file descriptor would otherwise
+ last until the garbage collector found them."""
+
+ def __init__(self, *args: Any, **kwargs: Any) -> None:
+ super().__init__(*args, **kwargs)
+ _BOT_SOCKET_CONNECTIONS.add(self)
+
+
+ _BOT_SOCKET_CONNECTIONS: weakref.WeakSet[_BotSocketConnection] = weakref.WeakSet()
+
+
+ def _close_loop(loop: asyncio.AbstractEventLoop) -> None:
+ """Close the connections opened on `loop`, cancel what is still running on
+ it, then close it.
+
+ A socket's `websockets` connection runs a keepalive task beside it that
+ outlives the socket's own coroutine. A loop dropped with that task still
+ pending is reported as "Task was destroyed but it is pending!" whenever it
+ is collected, at ERROR, in the middle of whatever the server is doing.
+ """
+ connections = [c for c in list(_BOT_SOCKET_CONNECTIONS) if c.loop is loop]
+ if connections:
+ loop.run_until_complete(
+ asyncio.gather(*(c.close() for c in connections), return_exceptions=True)
+ )
+ pending = asyncio.all_tasks(loop)
+ for task in pending:
+ task.cancel()
+ if pending:
+ loop.run_until_complete(asyncio.gather(*pending, return_exceptions=True))
+ loop.run_until_complete(loop.shutdown_asyncgens())
+ loop.close()
+ asyncio.set_event_loop(None)
+
+
class MattermostAdapter(PlatformAdapter):
display_name: ClassVar[str] = "Mattermost"
+ channel_on_delete = "archived"
docs_slug: ClassVar[str | None] = "mattermost"
# A quiet Mattermost bridge opens no websocket and mints no bot token: it
# knows each agent's bot by id, for mentions, and calls the API as the
diff --cc core/switch_core/management/wiring.py
index a2d079bd7,0ec8a8075..ca6b49c04
--- a/core/switch_core/management/wiring.py
+++ b/core/switch_core/management/wiring.py
@@@ -89,9 -89,8 +89,9 @@@ class Management
session_factory=self.session_factory,
)
agent_bridge_app.include_router(controller_router)
- gateway_app.include_router(gateway_router, prefix=GATEWAY_PREFIX)
+ mount_gateway_routes(gateway_app)
protocol.set_agent_removal_listener(self.agent_removed)
+ protocol.set_agent_rename_listener(self.agent_renamed)
enable_agent_management(self.agent_operations)
async def agent_removed(self, tenant_id: str, agent_id: str) -> None:
diff --cc core/tests/switch_core/bridges/collaboration/test_mattermost_bot_sockets.py
index 008f4cd7c,2e03f4133..8972f15ee
--- a/core/tests/switch_core/bridges/collaboration/test_mattermost_bot_sockets.py
+++ b/core/tests/switch_core/bridges/collaboration/test_mattermost_bot_sockets.py
@@@ -13,8 -13,14 +13,15 @@@ import threadin
from collections.abc import Callable
from typing import Any
+ from websockets.asyncio.client import ClientConnection, connect
+ from websockets.asyncio.server import ServerConnection, serve
+
+from switch_core.bridges.collaboration.adapter import AgentIdentity
- from switch_core.bridges.collaboration.mattermost.adapter import MattermostAdapter
+ from switch_core.bridges.collaboration.mattermost.adapter import (
+ MattermostAdapter,
+ _BotSocketConnection,
+ _close_loop,
+ )
from tests.switch_core.bridges.collaboration.test_bridge_agent_display_names import (
_agent,
_bridge,
@@@ -109,3 -115,97 +123,97 @@@ def test_removing_an_agent_closes_its_s
assert bot.disconnected.is_set()
assert not alive
assert bot.connects == 1
+
+
+ class _BotWithKeepalive(_Bot):
+ """A bot driver whose socket, like the `websockets` connection under
+ `mattermostdriver`, runs a keepalive task beside it that outlives the
+ socket's own coroutine unless something cancels it."""
+
+ def __init__(self) -> None:
+ super().__init__()
+ self.keepalive: asyncio.Task[None] | None = None
+ self.loop: asyncio.AbstractEventLoop | None = None
+
+ async def _connect(self, handler: Any) -> None:
+ self.loop = asyncio.get_running_loop()
+ self.keepalive = asyncio.create_task(asyncio.sleep(3600))
+ await super()._connect(handler)
+
+
+ def test_a_closed_socket_leaves_no_task_pending_on_its_loop() -> None:
+ """A loop dropped with a task still pending on it is reported by asyncio
+ as `Task was destroyed but it is pending!` whenever the loop is collected,
+ an error logged in the middle of whatever the server is doing then."""
+ bridge = _bridge(_agent("switchdev", "Switch Dev"))
+ adapter, _ = _mattermost_identity_adapter(bridge, [], "full_name")
+ bot = _BotWithKeepalive()
+ adapter._create_driver = lambda **_kwargs: bot # type: ignore[assignment,method-assign,return-value]
+
+ async def go() -> None:
+ adapter._main_loop = asyncio.get_running_loop()
+ before = set(threading.enumerate())
- await adapter.create_agent_identity("switchdev", "a Switch agent")
++ await adapter.create_agent_identity(_SWITCHDEV)
+ thread = _socket_thread("switchdev", before)
+ await _until(lambda: bot.connects == 1)
+ await adapter.remove_agent_identity("switchdev")
+ await asyncio.to_thread(thread.join, 5)
+ assert not thread.is_alive()
+
+ _run(go())
+
+ assert bot.keepalive is not None and bot.keepalive.cancelled()
+ assert bot.loop is not None and bot.loop.is_closed()
+
+
+ def test_closing_a_socket_loop_closes_the_connection_left_open_on_it() -> None:
+ """mattermostdriver keeps its connection in a local variable and leaves it
+ open when the socket stops, so the loop's closing has to close it, or its
+ file descriptor lasts until the garbage collector finds it."""
+ server_saw_close = threading.Event()
+ listening = threading.Event()
+ port: list[int] = []
+ stop_server = threading.Event()
+
+ def serve_forever() -> None:
+ async def handler(connection: ServerConnection) -> None:
+ await connection.wait_closed()
+ server_saw_close.set()
+
+ async def main() -> None:
+ async with serve(handler, "127.0.0.1", 0) as server:
+ port.append(next(iter(server.sockets)).getsockname()[1])
+ listening.set()
+ await asyncio.to_thread(stop_server.wait, 10)
+
+ asyncio.run(main())
+
+ server_thread = threading.Thread(target=serve_forever, daemon=True)
+ server_thread.start()
+ assert listening.wait(5)
+
+ opened: list[ClientConnection] = []
+
+ def socket_thread() -> None:
+ loop = asyncio.new_event_loop()
+ asyncio.set_event_loop(loop)
+
+ async def open_and_return() -> None:
+ opened.append(
+ await connect(
+ f"ws://127.0.0.1:{port[0]}",
+ create_connection=_BotSocketConnection,
+ close_timeout=2,
+ )
+ )
+
+ loop.run_until_complete(open_and_return())
+ _close_loop(loop)
+
+ thread = threading.Thread(target=socket_thread)
+ thread.start()
+ thread.join(10)
+ stop_server.set()
+
+ assert opened and opened[0].transport.is_closing()
+ assert server_saw_close.wait(5)