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
Cascade a room's attachments and tasks with the room; tighten two scenarios
Petr Bauch7aa3f0c15c57
diff --git a/console/tooling-e2e/src/clients/mattermost-client.ts b/console/tooling-e2e/src/clients/mattermost-client.tsindex 8b0813acb..8a4de5aae 100644--- a/console/tooling-e2e/src/clients/mattermost-client.ts+++ b/console/tooling-e2e/src/clients/mattermost-client.ts@@ -136,6 +136,32 @@ export class MattermostClient {return { client: new MattermostClient({ ...env, token }, environment), user, token };}+ /**+ * Take a person the harness signed up off the server again: deactivate them+ * (`DELETE /api/v4/users/{id}`, as themselves), or, where Mattermost does not+ * let a person deactivate themselves, take them out of every team and sign+ * them out, and say that the account itself is left behind.+ */+ async leave(userId: string): Promise<void> {+ try {+ await this.request('DELETE', `/users/${userId}`);+ return;+ } catch (error) {+ if (!(error instanceof MattermostHttpError) || ![401, 403, 501].includes(error.status)) {+ throw error;+ }+ const teams = await this.request<MattermostTeam[]>('GET', `/users/${userId}/teams`);+ for (const team of teams) {+ await this.request('DELETE', `/teams/${team.id}/members/${userId}`);+ }+ await this.request('POST', '/users/logout');+ console.warn(+ `Mattermost does not let ${userId} deactivate itself here (${error.status}); ` ++ 'it was taken out of its teams and signed out, but the account remains.'+ );+ }+ }+/** `POST /api/v4/teams/{id}/members` — join a team that is open to anyone, as the token's user. */async joinTeam(teamId: string, userId: string): Promise<void> {await this.request('POST', `/teams/${teamId}/members`, { team_id: teamId, user_id: userId });diff --git a/console/tooling-e2e/src/provider/scenarios.ts b/console/tooling-e2e/src/provider/scenarios.tsindex c47a6e7fc..4e1aaabfa 100644--- a/console/tooling-e2e/src/provider/scenarios.ts+++ b/console/tooling-e2e/src/provider/scenarios.ts@@ -2,7 +2,12 @@ import { randomBytes } from 'node:crypto';import { readFile } from 'node:fs/promises';import path from 'node:path';import { stringify } from 'yaml';-import { MattermostClient, sleep, type MattermostPost } from '../clients/mattermost-client.ts';+import {+ MattermostClient,+ sleep,+ type MattermostPost,+ type MattermostUser,+} from '../clients/mattermost-client.ts';import { OWNER_ONLY, TemplateRefusal, type TemplateRun } from '../clients/switch-client.ts';import type { NameScope } from '../core/run.ts';import type { ScenarioHeader } from '../core/scenario.ts';@@ -471,6 +476,9 @@ export const interrupt: Scenario = (harness) =>* lets anyone sign up: the throwaway stack and a local one. On both, the person* the harness posts as is the identity the admin who owns the agent claimed.*/+/** How long a refused answer is given to run the command anyway, before the owner answers. */+const REFUSED_QUIET_MS = 20_000;+export const cardAnswerRefused: Scenario = (harness, names) =>scenario('card-answer-refused', async (record) => {const mattermostEnv = harness.env.mattermost;@@ -485,77 +493,96 @@ export const cardAnswerRefused: Scenario = (harness, names) =>password,});secrets.record(other.token);- const team = await harness.mattermost.findTeam(mattermostEnv.team);- await other.client.joinTeam(team.id, other.user.id);- await harness.mattermost.addUserToChannel(harness.channel.id, other.user.id);-- await harness.switch.setAddressingPolicy(harness.agent.id, OWNER_ONLY);try {- const file = `refused-${harness.runId}.txt`;- const since = await ask(- harness,- `run the shell command \`echo ${OK_MARKER} > ${file}\` and then reply with the single word done`+ return await refuseThenApprove(harness, record, other);+ } finally {+ await other.client.leave(other.user.id);+ }+ });++async function refuseThenApprove(+ harness: Harness,+ record: (posts: MattermostPost[]) => void,+ other: { client: MattermostClient; user: MattermostUser }+): Promise<string> {+ const mattermostEnv = harness.env.mattermost!;+ const username = other.user.username;+ const team = await harness.mattermost.findTeam(mattermostEnv.team);+ await other.client.joinTeam(team.id, other.user.id);+ await harness.mattermost.addUserToChannel(harness.channel.id, other.user.id);++ await harness.switch.setAddressingPolicy(harness.agent.id, OWNER_ONLY);+ try {+ const file = `refused-${harness.runId}.txt`;+ const since = await ask(+ harness,+ `run the shell command \`echo ${OK_MARKER} > ${file}\` and then reply with the single word done`+ );+ const card = await expectBotPost(harness.mattermost, {+ where: sharedRoom(harness),+ since,+ record,+ predicate: (post) =>+ requestHandle(post.message) !== null &&+ APPROVAL_HINTS.some((hint) => lower(post).includes(hint)),+ describe: 'an approval card for the shell command',+ });+ const handle = requestHandle(card.message)!;+ const thread = card.root_id === '' ? card.id : card.root_id;++ const refusedAt = Date.now() - 1;+ await other.client.post({ channelId: harness.channel.id, message: `${handle} 1` });+ // Said by the bridge's own admin account, whatever it is called here.+ const isNotice = (post: MattermostPost): boolean =>+ post.root_id === thread &&+ post.message.includes(`answer to ${handle} did not land`) &&+ post.message.includes('may not address this agent');+ let notice: MattermostPost | undefined;+ const until = Date.now() + 60_000;+ while (notice === undefined && Date.now() < until) {+ const posts = await harness.mattermost.postsSince(harness.channel.id, refusedAt);+ record(posts);+ notice = posts.find(isNotice);+ if (notice === undefined) await sleep(2_000);+ }+ if (notice === undefined) {+ const seen = await harness.mattermost.postsSince(harness.channel.id, refusedAt);+ throw new Error(+ `${username}'s answer to ${handle} was not refused in the card's thread. Channel said: ${summarise(seen)}`);- const card = await expectBotPost(harness.mattermost, {- where: sharedRoom(harness),- since,- record,- predicate: (post) =>- requestHandle(post.message) !== null &&- APPROVAL_HINTS.some((hint) => lower(post).includes(hint)),- describe: 'an approval card for the shell command',- });- const handle = requestHandle(card.message)!;- const thread = card.root_id === '' ? card.id : card.root_id;-- const refusedAt = Date.now() - 1;- await other.client.post({ channelId: harness.channel.id, message: `${handle} 1` });- // Said by the bridge's own admin account, whatever it is called here.- const isNotice = (post: MattermostPost): boolean =>- post.root_id === thread &&- post.message.includes(`answer to ${handle} did not land`) &&- post.message.includes('may not address this agent');- let notice: MattermostPost | undefined;- const until = Date.now() + 60_000;- while (notice === undefined && Date.now() < until) {- const posts = await harness.mattermost.postsSince(harness.channel.id, refusedAt);- record(posts);- notice = posts.find(isNotice);- if (notice === undefined) await sleep(2_000);- }- if (notice === undefined) {- const seen = await harness.mattermost.postsSince(harness.channel.id, refusedAt);- throw new Error(- `${username}'s answer to ${handle} was not refused in the card's thread. Channel said: ${summarise(seen)}`- );- }- if (notice.user_id === other.user.id || notice.user_id === harness.human.id) {- throw new Error(`The refusal notice was posted by a person, not by Switch: ${notice.id}`);- }+ }+ if (notice.user_id === other.user.id || notice.user_id === harness.human.id) {+ throw new Error(`The refusal notice was posted by a person, not by Switch: ${notice.id}`);+ }+ // A refused answer must not run the command: give it time to, if it would.+ const quietUntil = Date.now() + REFUSED_QUIET_MS;+ while (Date.now() < quietUntil) {const ranEarly = (await harness.mattermost.postsFrom(harness.channel.id, harness.bot.id, refusedAt)).filter((post) =>!isRuntimeStatusPost(post) && !isNoSessionNotice(post) && lower(post).includes('done'));- record(ranEarly);if (ranEarly.length > 0) {- throw new Error(`The command ran on the answer of someone who may not address the agent.`);+ record(ranEarly);+ throw new Error('The command ran on the answer of someone who may not address the agent.');}-- const approvedAt = await answerCard(harness, card, 1);- const done = await expectBotPost(harness.mattermost, {- where: sharedRoom(harness),- since: approvedAt,- record,- predicate: (post) => lower(post).includes('done'),- describe: "a 'done' reply after the owner approved the command",- });- return `refused in ${notice.id}, approved by the owner, completed in ${done.id}`;- } finally {- await harness.switch.openAddressing(harness.agent.id);+ await sleep(2_000);}- });++ const approvedAt = await answerCard(harness, card, 1);+ const done = await expectBotPost(harness.mattermost, {+ where: sharedRoom(harness),+ since: approvedAt,+ record,+ predicate: (post) => lower(post).includes('done'),+ describe: "a 'done' reply after the owner approved the command",+ });+ return `refused in ${notice.id}, approved by the owner, completed in ${done.id}`;+ } finally {+ await harness.switch.openAddressing(harness.agent.id);+ }+}// ── template ─────────────────────────────────────────────────────────────────@@ -625,20 +652,22 @@ export const templateAgent: Scenario = (harness, names) =>run: () => harness.switch.deleteAgentByName(made.name),});}- const room = await harness.switch.getRoom(run.room.room_id);- const channelId = room.externalChannelId;+ const roomId = run.room.room_id;undo.push({- label: `room ${room.name} (${room.id})`,+ label: `room ${run.room.room_name} (${roomId})`,run: async () => {- await harness.switch.deleteRoom(room.id);- if (channelId === null) return;- const left = await harness.mattermost.getChannel(channelId);- if (!left.delete_at) await harness.mattermost.archiveChannel(channelId);+ const { externalChannelId } = await harness.switch.getRoom(roomId);+ await harness.switch.deleteRoom(roomId);+ if (externalChannelId === null) return;+ const left = await harness.mattermost.getChannel(externalChannelId);+ if (!left.delete_at) await harness.mattermost.archiveChannel(externalChannelId);},});let failed = false;try {+ const room = await harness.switch.getRoom(roomId);+ const channelId = room.externalChannelId;if (run.created_agents.length !== 1) {throw new Error(`The template made ${run.created_agents.length} agents, not one.`);}diff --git a/core/switch_core/db/models.py b/core/switch_core/db/models.pyindex d8a0e2c8f..06d3f8c8b 100644--- a/core/switch_core/db/models.py+++ b/core/switch_core/db/models.py@@ -878,6 +878,7 @@ room_agents = Table(["tenant_id", "room_id"],["rooms.tenant_id", "rooms.id"],name="fk_room_agents_room",+ ondelete="CASCADE",),ForeignKeyConstraint(["tenant_id", "agent_id"],@@ -903,6 +904,7 @@ room_skills = Table(["tenant_id", "room_id"],["rooms.tenant_id", "rooms.id"],name="fk_room_skills_room",+ ondelete="CASCADE",),ForeignKeyConstraint(["tenant_id", "skill_id"],@@ -1108,6 +1110,7 @@ class Task(TenantScoped, Base):["tenant_id", "room_id"],["rooms.tenant_id", "rooms.id"],name="fk_tasks_room",+ ondelete="CASCADE",),ForeignKeyConstraint(["tenant_id", "requester_agent_id"],@@ -1162,6 +1165,7 @@ room_references = Table(["tenant_id", "room_id"],["rooms.tenant_id", "rooms.id"],name="fk_room_references_room",+ ondelete="CASCADE",),ForeignKeyConstraint(["tenant_id", "reference_id"],@@ -1187,6 +1191,7 @@ room_documents = Table(["tenant_id", "room_id"],["rooms.tenant_id", "rooms.id"],name="fk_room_documents_room",+ ondelete="CASCADE",),ForeignKeyConstraint(["tenant_id", "document_id"],@@ -1372,6 +1377,7 @@ room_packages = Table(["tenant_id", "room_id"],["rooms.tenant_id", "rooms.id"],name="fk_room_packages_room",+ ondelete="CASCADE",),ForeignKeyConstraint(["tenant_id", "package_id"],diff --git a/core/switch_core/db/stores/room_store.py b/core/switch_core/db/stores/room_store.pyindex 1730754d2..16c1a288d 100644--- a/core/switch_core/db/stores/room_store.py+++ b/core/switch_core/db/stores/room_store.py@@ -15,10 +15,6 @@ from switch_core.db.models import (RoomGroup,require_tenant_id,room_agents,- room_documents,- room_packages,- room_references,- room_skills,)from switch_core.db.stores.delivery_cursor_store import DeliveryCursorStore@@ -252,14 +248,6 @@ class RoomStore:async def delete(self, session: AsyncSession, room_id: str) -> None:await session.execute(delete(ClientRoom).where(ClientRoom.room_id == room_id))- for attached in (- room_agents,- room_references,- room_documents,- room_packages,- room_skills,- ):- await session.execute(delete(attached).where(attached.c.room_id == room_id))room = await session.get(Room, room_id)if room:await session.delete(room)diff --git a/core/switch_core/migrations/versions/b7e2f4a91c35_cascade_room_attachments.py b/core/switch_core/migrations/versions/b7e2f4a91c35_cascade_room_attachments.pynew file mode 100644index 000000000..1c761bf41--- /dev/null+++ b/core/switch_core/migrations/versions/b7e2f4a91c35_cascade_room_attachments.py@@ -0,0 +1,44 @@+"""Delete what hangs off a room with the room.++The rows that attach agents, references, documents, packages and skills to a+room, and the room's tasks, now go with it, as its messages and roles already+did.+"""++from alembic import op++revision: str = "b7e2f4a91c35"+down_revision = "f68b679599ab"+branch_labels = None+depends_on = None++# (constraint, table) for every foreign key from a room's dependents to the room.+_ROOM_KEYS = [+ ("fk_room_agents_room", "room_agents"),+ ("fk_room_skills_room", "room_skills"),+ ("fk_room_references_room", "room_references"),+ ("fk_room_documents_room", "room_documents"),+ ("fk_room_packages_room", "room_packages"),+ ("fk_tasks_room", "tasks"),+]+++def _recreate(ondelete: str | None) -> None:+ for name, table in _ROOM_KEYS:+ op.drop_constraint(name, table, type_="foreignkey")+ op.create_foreign_key(+ name,+ table,+ "rooms",+ ["tenant_id", "room_id"],+ ["tenant_id", "id"],+ ondelete=ondelete,+ )+++def upgrade() -> None:+ _recreate("CASCADE")+++def downgrade() -> None:+ _recreate(None)diff --git a/core/tests/switch_core/db/stores/test_room_store.py b/core/tests/switch_core/db/stores/test_room_store.pyindex f51c71211..c0ccad45b 100644--- a/core/tests/switch_core/db/stores/test_room_store.py+++ b/core/tests/switch_core/db/stores/test_room_store.py@@ -3,15 +3,21 @@ from __future__ import annotationsimport uuidimport pytest+from sqlalchemy import insert, selectfrom sqlalchemy.ext.asyncio import AsyncSession, async_sessionmakerfrom switch_core.db.models import (+ Agent,+ ApiKey,+ Client,Package,Reference,Room,+ Task,Tenant,TenantNotBoundError,User,+ room_agents,)from switch_core.db.session_scope import tenant_sessionfrom switch_core.db.stores.package_store import PackageStore@@ -30,6 +36,33 @@ async def _make_room(session: AsyncSession, transport_room_id: str) -> Room:return room+async def _make_agent(session: AsyncSession, user_id: str) -> Agent:+ api_key = ApiKey(+ user_id=user_id,+ key_hash="hash-member",+ encrypted_key="enc",+ label="member",+ type="agent",+ )+ client = Client(+ transport_user_id="@member:test.local", display_name="member", type="agent"+ )+ session.add_all([api_key, client])+ await session.flush()+ agent = Agent(+ name="member",+ description="member",+ agent_type="always_on",+ connector_type="claude_code",+ integration_profile={"connection_model": "always_on"},+ client_id=client.id,+ api_key_id=api_key.id,+ )+ session.add(agent)+ await session.flush()+ return agent++class TestGetByTransportRoomIdMultiTenant:"""`Room.matrix_room_id` is unique per tenant(`uq_rooms_tenant_matrix_room_id`), not globally — two tenants may each@@ -123,7 +156,7 @@ class TestGetByTransportRoomIdRequiresATenant:class TestDelete:- async def test_deletes_a_room_with_a_package_and_a_reference_attached(+ async def test_deletes_a_room_with_what_hangs_off_it(self, session_factory: async_sessionmaker[AsyncSession]) -> None:packages, references = PackageStore(), ReferenceStore()@@ -158,8 +191,23 @@ class TestDelete:)await packages.attach_to_room(session, room.id, package.id)await references.attach_to_room(session, room.id, reference.id)+ agent = await _make_agent(session, owner.id)+ await session.execute(+ insert(room_agents).values(room_id=room.id, agent_id=agent.id)+ )+ session.add(+ Task(+ room_id=room.id,+ requester_agent_id=agent.id,+ performer_agent_id=agent.id,+ summary="s",+ description="d",+ status="pending",+ )+ )await session.commit()room_id, package_id, reference_id = room.id, package.id, reference.id+ agent_id = agent.idasync with session_factory() as session:await RoomStore().delete(session, room_id)@@ -169,3 +217,6 @@ class TestDelete:assert await session.get(Room, room_id) is Noneassert await packages.get(session, package_id) is not Noneassert await session.get(Reference, reference_id) is not None+ assert await session.get(Agent, agent_id) is not None+ tasks = await session.execute(select(Task).where(Task.room_id == room_id))+ assert tasks.scalars().all() == []