Skip to content

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

Cover retention, erasure and ending sessions end to end

Petr Bauch2d2188831d49
diff --git a/console/tooling-e2e/src/clients/switch-client.ts b/console/tooling-e2e/src/clients/switch-client.ts
index 1724b8b2c..02d026f16 100644
--- a/console/tooling-e2e/src/clients/switch-client.ts
+++ b/console/tooling-e2e/src/clients/switch-client.ts
@@ -238,6 +238,29 @@ export interface WorkspaceMembership {
organization_id: string | null;
}
+/** A workspace's message retention window; `message_retention_days: null` keeps messages forever. */
+export interface RetentionPolicy {
+ message_retention_days: number | null;
+ updated_at: string | null;
+ updated_by_user_id: string | null;
+}
+
+/** One row of `GET /gateway/tenants/{tenant_id}/people`. `kind: 'member'` is someone taking part from Switch Console. */
+export interface Person {
+ id: string;
+ kind: "identity" | "former" | "member";
+ username: string;
+ platform: string;
+ message_count: number;
+}
+
+export interface Erasure {
+ id: string;
+ state: "queued" | "running" | "done" | "failed";
+ messages_deleted: number;
+ error: string | null;
+}
+
export class SwitchClient {
private readonly apiUrl: string;
/** The deployment-wide registration key; agents it registers belong to a bootstrap account. */
@@ -1068,6 +1091,87 @@ export class SwitchClient {
await this.gateway<unknown>("DELETE", `/users/${encodeURIComponent(userId)}`);
}
+ /**
+ * `POST /gateway/auth/sessions/revoke` — sign this account out of every
+ * device, this client included: its cookie stops working too.
+ */
+ async endMySessions(): Promise<void> {
+ await this.gateway<unknown>("POST", "/auth/sessions/revoke");
+ }
+
+ /** `POST /gateway/users/{user_id}/sessions/revoke` — the deployment operator signs someone out of every device. */
+ async endUserSessions(userId: string): Promise<void> {
+ await this.gateway<unknown>(
+ "POST",
+ `/users/${encodeURIComponent(userId)}/sessions/revoke`,
+ );
+ }
+
+ /** `GET /gateway/tenants/{tenant_id}/retention` — how long the workspace keeps messages; null days is forever. */
+ async retentionPolicy(tenantId: string): Promise<RetentionPolicy> {
+ return this.gateway("GET", `/tenants/${encodeURIComponent(tenantId)}/retention`);
+ }
+
+ /** `GET /gateway/tenants/{tenant_id}/retention/preview` — how many messages a window of `days` would delete now. */
+ async previewRetention(
+ tenantId: string,
+ days: number,
+ ): Promise<{ message_retention_days: number; messages_to_delete: number }> {
+ return this.gateway(
+ "GET",
+ `/tenants/${encodeURIComponent(tenantId)}/retention/preview?days=${days}`,
+ );
+ }
+
+ /** `PUT /gateway/tenants/{tenant_id}/retention` — delete messages once they are older than `days`. */
+ async setRetention(tenantId: string, days: number): Promise<RetentionPolicy> {
+ return this.gateway("PUT", `/tenants/${encodeURIComponent(tenantId)}/retention`, {
+ message_retention_days: days,
+ });
+ }
+
+ /** `DELETE /gateway/tenants/{tenant_id}/retention` — keep messages forever again. */
+ async clearRetention(tenantId: string): Promise<RetentionPolicy> {
+ return this.gateway("DELETE", `/tenants/${encodeURIComponent(tenantId)}/retention`);
+ }
+
+ /** `GET /gateway/tenants/{tenant_id}/people` — everyone seen in the workspace's rooms, the list a person is erased from. */
+ async people(tenantId: string): Promise<Person[]> {
+ return this.gateway("GET", `/tenants/${encodeURIComponent(tenantId)}/people`);
+ }
+
+ /** `POST /gateway/tenants/{tenant_id}/erasures` — queue erasing what the named people sent. */
+ async requestErasure(
+ tenantId: string,
+ params: { memberClientIds: string[] },
+ ): Promise<Erasure> {
+ return this.gateway("POST", `/tenants/${encodeURIComponent(tenantId)}/erasures`, {
+ member_client_ids: params.memberClientIds,
+ });
+ }
+
+ /** `GET /gateway/tenants/{tenant_id}/erasures` — the workspace's erasure requests, newest first. */
+ async erasures(tenantId: string): Promise<Erasure[]> {
+ return this.gateway("GET", `/tenants/${encodeURIComponent(tenantId)}/erasures`);
+ }
+
+ /** `POST /gateway/chats` — a direct chat between the signed-in person and one agent. Returns its room id. */
+ async createDirectChat(agentId: string): Promise<string> {
+ const envelope = await this.gateway<{ chat: { roomId: string } }>("POST", "/chats", {
+ agentId,
+ requestId: crypto.randomUUID(),
+ });
+ return envelope.chat.roomId;
+ }
+
+ /** `POST /gateway/chats/{room_id}/messages` — post into a chat as the signed-in person. */
+ async sendChatMessage(roomId: string, body: string): Promise<void> {
+ await this.gateway<unknown>("POST", `/chats/${encodeURIComponent(roomId)}/messages`, {
+ requestId: crypto.randomUUID(),
+ body,
+ });
+ }
+
/** `GET /gateway/operator/workspaces` — every workspace on the server, platform operator only. */
async operatorWorkspaces(): Promise<{ id: string; name: string }[]> {
return this.gateway<{ id: string; name: string }[]>("GET", "/operator/workspaces");
diff --git a/console/tooling-e2e/src/fast/data-protection.ts b/console/tooling-e2e/src/fast/data-protection.ts
new file mode 100644
index 000000000..73540fd57
--- /dev/null
+++ b/console/tooling-e2e/src/fast/data-protection.ts
@@ -0,0 +1,261 @@
+import { expect } from "vitest";
+import {
+ SwitchHttpError,
+ errorDetail,
+ sleep,
+ type SwitchClient,
+} from "../clients/switch-client.ts";
+import type { NameScope } from "../core/run.ts";
+import type { Fixtures } from "../fixtures/fixtures.ts";
+import type { FastScenario } from "./scenario.ts";
+
+const ERASURE_DEADLINE_MS = 60_000;
+const HISTORY_DEADLINE_MS = 30_000;
+
+async function status(call: () => Promise<unknown>): Promise<[number, string]> {
+ try {
+ await call();
+ } catch (error) {
+ if (error instanceof SwitchHttpError) return [error.status, errorDetail(error.body)];
+ throw error;
+ }
+ return [200, ""];
+}
+
+/**
+ * A plain user in the harness admin's workspace, signed in, with their
+ * account deleted afterwards. Deleting an account takes the deployment
+ * operator, so the scenarios using this run only on a throwaway stack.
+ */
+async function colleague(
+ role: string,
+ names: NameScope,
+ switchClient: SwitchClient,
+ fixtures: Fixtures,
+): Promise<{ id: string; name: string; email: string; password: string; client: SwitchClient }> {
+ const password = `e2e-${names.runId}-${crypto.randomUUID()}`;
+ const name = names.name(role);
+ const email = `${name}@e2e.invalid`;
+ const { id } = await switchClient.createUser({ name, email, password, role: "user" });
+ fixtures.defer(`the account ${email}`, async () => {
+ const [code, detail] = await status(() => switchClient.deleteUser(id));
+ if (code !== 200 && code !== 404) throw new Error(`${code}: ${detail}`);
+ });
+ const client = switchClient.as(email, password);
+ // Signing in joins the default workspace.
+ await client.tenantId();
+ return { id, name, email, password, client };
+}
+
+/**
+ * A workspace admin sets how long messages are kept, sees beforehand how many
+ * a window would delete, reads the window back and clears it. A plain member
+ * may not set one.
+ *
+ * The window is set in a workspace made for the run and deleted afterwards
+ * (which takes its retention policy with it), so no real workspace's policy
+ * is ever touched. Whether old messages are then deleted is left to core's
+ * tests: the retention pass runs hourly and the harness cannot age a message.
+ */
+export const retentionWindow: FastScenario = {
+ header: {
+ id: "retention-window",
+ covers: [
+ "GET /gateway/tenants/{tenant_id}/retention",
+ "GET /gateway/tenants/{tenant_id}/retention/preview",
+ "PUT /gateway/tenants/{tenant_id}/retention",
+ "DELETE /gateway/tenants/{tenant_id}/retention",
+ ],
+ lane: "fast",
+ environments: ["ephemeral"],
+ owner: "platform",
+ requires: [],
+ },
+ async run({ names, switchClient, fixtures }) {
+ const member = await colleague("member", names, switchClient, fixtures);
+ const defaultWorkspace = await switchClient.tenantId();
+ const [memberSets] = await status(() =>
+ member.client.setRetention(defaultWorkspace, 3650),
+ );
+ expect(memberSets, "a plain member setting a retention window").toBe(403);
+
+ const makerPassword = `e2e-${names.runId}-${crypto.randomUUID()}`;
+ const makerEmail = `${names.name("keeper")}@e2e.invalid`;
+ // A deployment operator, so they can make a workspace. An operator's
+ // account cannot be deleted, so it stays on the throwaway stack.
+ await switchClient.createUser({
+ name: "E2E retention keeper",
+ email: makerEmail,
+ password: makerPassword,
+ role: "admin",
+ });
+ const maker = switchClient.as(makerEmail, makerPassword);
+ const spaceName = names.name("kept");
+ const spaceId = await maker.createTenant(spaceName);
+ fixtures.defer(`the workspace ${spaceName}`, async () => {
+ const [code, detail] = await status(() => maker.deleteTenant(spaceId, spaceName));
+ if (code !== 200 && code !== 404) throw new Error(`${code}: ${detail}`);
+ });
+
+ expect(
+ (await maker.retentionPolicy(spaceId)).message_retention_days,
+ "a new workspace's window",
+ ).toBeNull();
+
+ const preview = await maker.previewRetention(spaceId, 30);
+ expect(preview, "the preview of a 30-day window").toMatchObject({
+ message_retention_days: 30,
+ messages_to_delete: 0,
+ });
+
+ const set = await maker.setRetention(spaceId, 30);
+ expect(set.message_retention_days, "the window just set").toBe(30);
+ expect(set.updated_at, "when it was set").not.toBeNull();
+ expect(
+ (await maker.retentionPolicy(spaceId)).message_retention_days,
+ "the window read back",
+ ).toBe(30);
+
+ const [tooLong] = await status(() => maker.setRetention(spaceId, 3651));
+ expect(tooLong, "a window over ten years").toBe(422);
+
+ const cleared = await maker.clearRetention(spaceId);
+ expect(cleared.message_retention_days, "the cleared window").toBeNull();
+ expect(
+ (await maker.retentionPolicy(spaceId)).message_retention_days,
+ "the window after clearing",
+ ).toBeNull();
+ },
+};
+
+/**
+ * A workspace admin erases what a person sent from Switch Console. A
+ * colleague chats with an agent; the admin finds them in the workspace's
+ * people, queues their erasure and waits for it to finish, after which the
+ * agent's history of that chat no longer holds their words. A plain member
+ * may not see the list or erase anyone.
+ */
+export const memberErasure: FastScenario = {
+ header: {
+ id: "member-erasure",
+ covers: [
+ "POST /gateway/chats",
+ "POST /gateway/chats/{room_id}/messages",
+ "GET /gateway/tenants/{tenant_id}/people",
+ "POST /gateway/tenants/{tenant_id}/erasures",
+ "GET /gateway/tenants/{tenant_id}/erasures",
+ ],
+ lane: "fast",
+ environments: ["ephemeral"],
+ owner: "platform",
+ requires: [],
+ },
+ async run({ names, switchClient, fixtures }) {
+ const agent = await fixtures.agent("listener");
+ const person = await colleague("talker", names, switchClient, fixtures);
+ const tenantId = await switchClient.tenantId();
+
+ const roomId = await person.client.createDirectChat(agent.id);
+ fixtures.defer(`the chat ${roomId}`, async () => {
+ const [code, detail] = await status(() => switchClient.deleteRoom(roomId));
+ if (code !== 200 && code !== 404) throw new Error(`${code}: ${detail}`);
+ });
+ const marker = names.marker("erase");
+ const said = [`${marker} first`, `${marker} second`];
+ for (const body of said) await person.client.sendChatMessage(roomId, body);
+
+ const heard = async (): Promise<string[]> =>
+ (await switchClient.roomHistory(agent, roomId, 50))
+ .map((event) => event.body)
+ .filter((body) => body.includes(marker));
+ let before: string[] = [];
+ const until = Date.now() + HISTORY_DEADLINE_MS;
+ while (Date.now() < until) {
+ before = await heard();
+ if (before.length === said.length) break;
+ await sleep(500);
+ }
+ expect(before, "what the colleague said, in the agent's history").toEqual(said);
+
+ const [memberLists] = await status(() => person.client.people(tenantId));
+ expect(memberLists, "a plain member listing the people to erase").toBe(403);
+
+ const people = await switchClient.people(tenantId);
+ const row = people.find((p) => p.kind === "member" && p.username === person.name);
+ expect(row, `${person.name} among the workspace's people`).toBeDefined();
+ expect(row!.platform).toBe("switch");
+ expect(row!.message_count).toBeGreaterThanOrEqual(said.length);
+
+ const [memberErases] = await status(() =>
+ person.client.requestErasure(tenantId, { memberClientIds: [row!.id] }),
+ );
+ expect(memberErases, "a plain member erasing someone").toBe(403);
+
+ const queued = await switchClient.requestErasure(tenantId, {
+ memberClientIds: [row!.id],
+ });
+ expect(["queued", "running", "done"]).toContain(queued.state);
+
+ let finished = queued;
+ const deadline = Date.now() + ERASURE_DEADLINE_MS;
+ while (finished.state !== "done" && finished.state !== "failed" && Date.now() < deadline) {
+ await sleep(1_000);
+ const current = (await switchClient.erasures(tenantId)).find((e) => e.id === queued.id);
+ expect(current, "the queued erasure in the workspace's list").toBeDefined();
+ finished = current!;
+ }
+ expect(finished.error, "the erasure's error").toBeNull();
+ expect(finished.state, "the erasure's state").toBe("done");
+ expect(finished.messages_deleted).toBeGreaterThanOrEqual(said.length);
+
+ expect(await heard(), "what the colleague said, after the erasure").toEqual([]);
+ },
+};
+
+/**
+ * Signing out of every device. A person signed in twice ends all their
+ * sessions from one: both cookies stop working, and signing in again works.
+ * The deployment operator does the same for someone else; a plain member may
+ * not.
+ */
+export const endSessions: FastScenario = {
+ header: {
+ id: "end-sessions",
+ covers: [
+ "POST /gateway/auth/sessions/revoke",
+ "POST /gateway/users/{user_id}/sessions/revoke",
+ ],
+ lane: "fast",
+ environments: ["ephemeral"],
+ owner: "platform",
+ requires: [],
+ },
+ async run({ names, switchClient, fixtures }) {
+ const person = await colleague("traveller", names, switchClient, fixtures);
+ const laptop = person.client;
+ const phone = switchClient.as(person.email, person.password);
+ await phone.tenantId();
+
+ await laptop.endMySessions();
+ const [laptopAfter] = await status(() => laptop.tenantId());
+ expect(laptopAfter, "the session that ended them all").toBe(401);
+ const [phoneAfter] = await status(() => phone.tenantId());
+ expect(phoneAfter, "the other device's session").toBe(401);
+ const again = switchClient.as(person.email, person.password);
+ await again.tenantId();
+
+ const other = await colleague("bystander", names, switchClient, fixtures);
+ const [notOperator] = await status(() => again.endUserSessions(other.id));
+ expect(notOperator, "a plain member ending someone's sessions").toBe(403);
+ await other.client.tenantId();
+
+ await switchClient.endUserSessions(person.id);
+ const [revoked] = await status(() => again.tenantId());
+ expect(revoked, "a session the operator ended").toBe(401);
+ const [stillIn] = await status(() => other.client.tenantId());
+ expect(stillIn, "someone else's session, untouched").toBe(200);
+
+ const [nobody] = await status(() => switchClient.endUserSessions(crypto.randomUUID()));
+ expect(nobody, "ending the sessions of no such user").toBe(404);
+ },
+};
diff --git a/console/tooling-e2e/src/fast/index.ts b/console/tooling-e2e/src/fast/index.ts
index 45ed35288..1cc5f88d2 100644
--- a/console/tooling-e2e/src/fast/index.ts
+++ b/console/tooling-e2e/src/fast/index.ts
@@ -2,6 +2,7 @@ import { accountLifecycle } from "./account-lifecycle.ts";
import { agentIdentity } from "./agent-identity.ts";
import { attachmentNames } from "./attachments.ts";
import { clientMistakes } from "./client-mistakes.ts";
+import { endSessions, memberErasure, retentionWindow } from "./data-protection.ts";
import { followUps } from "./follow-ups.ts";
import { addressedDelivery, unaddressedHistory } from "./agent-messaging.ts";
import { mattermostRoundtrip } from "./mattermost-roundtrip.ts";
@@ -27,4 +28,7 @@ export const FAST_SCENARIOS: readonly FastScenario[] = [
organizationMembers,
organizations,
accountLifecycle,
+ retentionWindow,
+ memberErasure,
+ endSessions,
];
diff --git a/console/tooling-e2e/surface/exemptions.json b/console/tooling-e2e/surface/exemptions.json
index 96b4254aa..79ccff243 100644
--- a/console/tooling-e2e/surface/exemptions.json
+++ b/console/tooling-e2e/surface/exemptions.json
@@ -249,12 +249,10 @@
"POST /gateway/api-keys",
"POST /gateway/auth/refresh",
"POST /gateway/auth/signup",
- "POST /gateway/chats",
"POST /gateway/chats/{room_id}/archive",
"POST /gateway/chats/{room_id}/attachments",
"POST /gateway/chats/{room_id}/controls",
"POST /gateway/chats/{room_id}/members",
- "POST /gateway/chats/{room_id}/messages",
"POST /gateway/chats/{room_id}/warm",
"POST /gateway/collaborations",
"POST /gateway/collaborations/setup/slack/check",
diff --git a/console/tooling-e2e/surface/server-surface.json b/console/tooling-e2e/surface/server-surface.json
index bd5ec6a00..85cb5d22e 100644
--- a/console/tooling-e2e/surface/server-surface.json
+++ b/console/tooling-e2e/surface/server-surface.json
@@ -301,6 +301,11 @@
"auth": "session",
"flags": []
},
+ {
+ "route": "DELETE /gateway/tenants/{tenant_id}/retention",
+ "auth": "session",
+ "flags": []
+ },
{
"route": "DELETE /gateway/trust-settings",
"auth": "session",
@@ -918,6 +923,11 @@
"auth": "session",
"flags": []
},
+ {
+ "route": "GET /gateway/tenants/{tenant_id}/erasures",
+ "auth": "session",
+ "flags": []
+ },
{
"route": "GET /gateway/tenants/{tenant_id}/invitations",
"auth": "session",
@@ -933,6 +943,21 @@
"auth": "session",
"flags": []
},
+ {
+ "route": "GET /gateway/tenants/{tenant_id}/people",
+ "auth": "session",
+ "flags": []
+ },
+ {
+ "route": "GET /gateway/tenants/{tenant_id}/retention",
+ "auth": "session",
+ "flags": []
+ },
+ {
+ "route": "GET /gateway/tenants/{tenant_id}/retention/preview",
+ "auth": "session",
+ "flags": []
+ },
{
"route": "GET /gateway/tenants/{tenant_id}/usage",
"auth": "session",
@@ -1089,6 +1114,11 @@
"auth": "session",
"flags": []
},
+ {
+ "route": "POST /gateway/auth/sessions/revoke",
+ "auth": "session",
+ "flags": []
+ },
{
"route": "POST /gateway/auth/signup",
"auth": "none",
@@ -1458,6 +1488,11 @@
"auth": "session",
"flags": []
},
+ {
+ "route": "POST /gateway/tenants/{tenant_id}/erasures",
+ "auth": "session",
+ "flags": []
+ },
{
"route": "POST /gateway/tenants/{tenant_id}/invitations",
"auth": "session",
@@ -1478,6 +1513,11 @@
"auth": "session",
"flags": []
},
+ {
+ "route": "POST /gateway/users/{user_id}/sessions/revoke",
+ "auth": "session",
+ "flags": []
+ },
{
"route": "PUT /gateway/agents/{agent_id}/addressing-policy",
"auth": "session",
@@ -1599,6 +1639,11 @@
"auth": "session",
"flags": []
},
+ {
+ "route": "PUT /gateway/tenants/{tenant_id}/retention",
+ "auth": "session",
+ "flags": []
+ },
{
"route": "PUT /gateway/trust-settings",
"auth": "session",