Skip to content

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

Purge a deleted workspace's audit history two years after the deletion

Petr Bauchd311d9051610
diff --git a/core/switch_core/main.py b/core/switch_core/main.py
index 079a5429e..0a2c1326b 100644
--- a/core/switch_core/main.py
+++ b/core/switch_core/main.py
@@ -114,6 +114,7 @@ from switch_core.db import encrypted_json
from switch_core.db.boot_lock import boot_lock
from switch_core.db.connection_budget import check_connection_budget
from switch_core.db.engine import (
+ app_connect_args,
create_engine_from_config,
create_session_factory,
create_unpooled_engine,
@@ -210,6 +211,7 @@ from switch_core.process_identity import new_process_id
from switch_core.process_lease import ProcessLease
from switch_core.provisioning import Provisioning
from switch_core.provisioning.postgres import PostgresProvisioning
+from switch_core.retention.audit_history import check_purge_privilege
from switch_core.retention.erasure import ErasureService, erasure_loop
from switch_core.retention.service import retention_loop
from switch_core.room_changes import COALESCE_SECONDS, RoomChanges
@@ -669,6 +671,28 @@ async def _check_tenant_isolation(config: SwitchConfig, engine: AsyncEngine) ->
return True
+async def _audit_history_sessions(
+ config: SwitchConfig,
+ engine: AsyncEngine,
+ session_factory: async_sessionmaker[AsyncSession],
+) -> tuple[AsyncEngine | None, async_sessionmaker[AsyncSession]]:
+ """The sessions the retention pass purges deleted workspaces' audit history
+ on: the schema owner's, where configured, since the runtime role may not
+ delete audit rows; the runtime ones otherwise. Says at startup, as an
+ error, when the chosen role cannot (`retention/audit_history.py`).
+
+ Returns the owner's engine, for the caller to dispose of, or None."""
+ owner_url = config.owner_database_url
+ if owner_url is None:
+ await check_purge_privilege(engine, owner=False)
+ return None, session_factory
+ owner_engine = create_async_engine(
+ owner_url, poolclass=NullPool, connect_args=app_connect_args(config)
+ )
+ await check_purge_privilege(owner_engine, owner=True)
+ return owner_engine, create_session_factory(owner_engine)
+
+
async def run(config: SwitchConfig, process_id: str) -> None:
# ── Database ─────────────────────────────────────────────────────────────
# The migration and the runtime role's grant re-issue already ran, under
@@ -691,6 +715,9 @@ async def run(config: SwitchConfig, process_id: str) -> None:
instrument_queries(engine)
pool_watermark = install_pool_watermark(engine)
install_hold_timer(engine)
+ history_engine, history_sessions = await _audit_history_sessions(
+ config, engine, session_factory
+ )
# Its connection is held rather than borrowed, so it builds its own outside
# the pool. Nothing subscribes yet; it starts with the server so that the
@@ -1523,7 +1550,9 @@ async def run(config: SwitchConfig, process_id: str) -> None:
if management is not None
else None
)
- data_retention_task = asyncio.create_task(retention_loop(session_factory))
+ data_retention_task = asyncio.create_task(
+ retention_loop(session_factory, history_sessions)
+ )
erasure_task = asyncio.create_task(
erasure_loop(
session_factory,
@@ -1552,6 +1581,8 @@ async def run(config: SwitchConfig, process_id: str) -> None:
connection_sweep_task.cancel()
service_token_task.cancel()
data_retention_task.cancel()
+ if history_engine is not None:
+ await history_engine.dispose()
erasure_task.cancel()
if snapshot_task is not None:
snapshot_task.cancel()
diff --git a/core/switch_core/retention/audit_history.py b/core/switch_core/retention/audit_history.py
new file mode 100644
index 000000000..6d2445658
--- /dev/null
+++ b/core/switch_core/retention/audit_history.py
@@ -0,0 +1,112 @@
+"""Purging a deleted workspace's audit history once its horizon has passed.
+
+Deleting a workspace keeps its `audit_events`: they outlive the tenant on
+purpose, so what was done in it can still be answered for. They are kept for
+`AUDIT_HISTORY_HORIZON` after the deletion and then deleted, as part of the
+hourly data retention pass (`retention/service.py`).
+
+The deletion's time comes from the server's audit log: deleting a workspace
+writes a `workspace.deleted` row there (`accounts/workspace_deletion.py`), and
+that log outlives every workspace. A workspace whose `tenants` row still
+exists is never touched, whatever its log says. Kept:
+
+- the server's own row recording the deletion, which is what proves when the
+ workspace went and so why its history did;
+- the organization's audit log, which is the organization's history rather
+ than the workspace's, and goes with the organization.
+
+The runtime role may not delete audit rows (`db/runtime_role.grant_runtime_role`
+takes `DELETE` back), so this runs on the schema owner's connection where one
+is configured (`SwitchConfig.owner_database_url`). Without one it runs on the
+runtime connection, which works only where that role owns the schema;
+`check_purge_privilege` says at startup when it cannot.
+"""
+
+from __future__ import annotations
+
+import logging
+from datetime import datetime, timedelta
+from typing import Any, cast
+
+from sqlalchemy import CursorResult, delete, select, text
+from sqlalchemy.ext.asyncio import AsyncEngine, AsyncSession, async_sessionmaker
+
+from switch_core.db.audit import ServerAuditAction
+from switch_core.db.models import AuditEvent, ServerAuditEvent, Tenant
+from switch_core.db.session_scope import tenant_session
+from switch_core.tenant_context import no_tenant
+
+logger = logging.getLogger(__name__)
+
+#: How long a deleted workspace's audit history is kept after the deletion.
+AUDIT_HISTORY_HORIZON = timedelta(days=730)
+
+
+async def purge_deleted_workspace_history(
+ history_sessions: async_sessionmaker[AsyncSession], now: datetime
+) -> int:
+ """Delete the audit history of every workspace deleted more than
+ `AUDIT_HISTORY_HORIZON` before `now`, and return how many rows went.
+
+ `history_sessions` must be allowed to delete from `audit_events`: the
+ owner's connection, or a runtime role that owns the schema.
+ """
+ with no_tenant():
+ async with history_sessions() as session:
+ deleted = await session.scalars(
+ select(ServerAuditEvent.target_id)
+ .where(
+ ServerAuditEvent.action == ServerAuditAction.WORKSPACE_DELETED,
+ ServerAuditEvent.target_type == "tenant",
+ ServerAuditEvent.target_id.is_not(None),
+ ServerAuditEvent.occurred_at < now - AUDIT_HISTORY_HORIZON,
+ )
+ .distinct()
+ )
+ tenant_ids = sorted(
+ tenant_id for tenant_id in deleted.all() if tenant_id is not None
+ )
+ purged = 0
+ for tenant_id in tenant_ids:
+ async with tenant_session(history_sessions, tenant_id) as session:
+ async with session.begin():
+ if await session.scalar(
+ select(Tenant.id).where(Tenant.id == tenant_id)
+ ):
+ continue
+ result = await session.execute(
+ delete(AuditEvent).where(AuditEvent.tenant_id == tenant_id)
+ )
+ count = cast("CursorResult[Any]", result).rowcount
+ if count:
+ logger.info(
+ "Purged %d audit rows of workspace %s, deleted more than %s ago",
+ count,
+ tenant_id,
+ AUDIT_HISTORY_HORIZON,
+ )
+ purged += count
+ return purged
+
+
+async def check_purge_privilege(engine: AsyncEngine, *, owner: bool) -> bool:
+ """Whether `engine`'s role may delete audit rows, logged as an error when
+ not: the history of deleted workspaces would then be kept forever."""
+ async with engine.connect() as connection:
+ allowed = bool(
+ await connection.scalar(
+ text(
+ "SELECT has_table_privilege(current_user, 'audit_events', 'DELETE')"
+ )
+ )
+ )
+ if not allowed:
+ logger.error(
+ "Deleted workspaces' audit history will NOT be purged after %s: the "
+ "%s connection may not delete from audit_events. Configure the schema "
+ "owner's connection (DB_OWNER_USER and DB_OWNER_PASSWORD) so the "
+ "retention pass can run it.",
+ AUDIT_HISTORY_HORIZON,
+ "owner's" if owner else "runtime",
+ )
+ return allowed
diff --git a/core/switch_core/retention/service.py b/core/switch_core/retention/service.py
index cc192b9c5..88d5b014f 100644
--- a/core/switch_core/retention/service.py
+++ b/core/switch_core/retention/service.py
@@ -13,7 +13,9 @@ never holds a long lock, and each whether or not the others failed:
3. Deletes settled approval requests, lapsed invitations and expired install
links once `SETTLED_GRACE` has passed.
-The audit log and usage records are never touched; `docs/design/data-retention.md`
+After the tenants, it purges the audit history of workspaces deleted more than
+`AUDIT_HISTORY_HORIZON` ago (`retention/audit_history.py`). A live workspace's
+audit log and usage records are never touched; `docs/design/data-retention.md`
says why, and what retention does not yet cover.
"""
@@ -32,6 +34,7 @@ from switch_core.db.session_scope import tenant_session
from switch_core.db.stores.message_store import MessageStore
from switch_core.db.stores.retention_store import RetentionStore
from switch_core.db.tenant_lookup import all_tenant_ids
+from switch_core.retention.audit_history import purge_deleted_workspace_history
from switch_core.tenant_context import no_tenant, tenant_scope
logger = logging.getLogger(__name__)
@@ -178,9 +181,12 @@ class RetentionService:
async def retain_once(
- session_factory: async_sessionmaker[AsyncSession], now: datetime
+ session_factory: async_sessionmaker[AsyncSession],
+ history_sessions: async_sessionmaker[AsyncSession],
+ now: datetime,
) -> None:
- """One pass over every tenant, each bound before it is touched."""
+ """One pass over every tenant, each bound before it is touched, then the
+ purge of deleted workspaces' audit history on `history_sessions`."""
service = RetentionService(session_factory)
for tenant_id in await all_tenant_ids(session_factory):
with tenant_scope(tenant_id):
@@ -189,9 +195,18 @@ async def retain_once(
except Exception:
# One tenant's failure must not stop the others' retention.
logger.exception("Data retention failed for tenant %s", tenant_id)
+ try:
+ await purge_deleted_workspace_history(history_sessions, now)
+ except Exception:
+ logger.exception(
+ "Data retention could not purge deleted workspaces' audit history"
+ )
-async def retention_loop(session_factory: async_sessionmaker[AsyncSession]) -> None:
+async def retention_loop(
+ session_factory: async_sessionmaker[AsyncSession],
+ history_sessions: async_sessionmaker[AsyncSession],
+) -> None:
"""Retention at start and hourly, as a task of its own.
Not in the session-activity upkeep loop, which also expires approval
@@ -201,7 +216,7 @@ async def retention_loop(session_factory: async_sessionmaker[AsyncSession]) -> N
with no_tenant():
while True:
try:
- await retain_once(session_factory, datetime.now(UTC))
+ await retain_once(session_factory, history_sessions, datetime.now(UTC))
except Exception:
logger.exception("Data retention could not list tenants")
await asyncio.sleep(PASS_EVERY_SECONDS)
diff --git a/core/tests/switch_core/retention/test_audit_history.py b/core/tests/switch_core/retention/test_audit_history.py
new file mode 100644
index 000000000..671400dfb
--- /dev/null
+++ b/core/tests/switch_core/retention/test_audit_history.py
@@ -0,0 +1,156 @@
+"""Purging deleted workspaces' audit history, against real Postgres.
+
+A workspace that exists keeps its history whatever the server's log says; one
+deleted more than the horizon ago loses its `audit_events` but not the
+server's row recording the deletion; one deleted more recently keeps it all.
+"""
+
+from __future__ import annotations
+
+import logging
+from datetime import UTC, datetime, timedelta
+
+import pytest
+from sqlalchemy import func, select
+from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
+
+from switch_core.db.audit import ServerAuditAction
+from switch_core.db.models import AuditEvent, ServerAuditEvent, Tenant
+from switch_core.db.session_scope import tenant_session
+from switch_core.retention.audit_history import (
+ AUDIT_HISTORY_HORIZON,
+ check_purge_privilege,
+ purge_deleted_workspace_history,
+)
+from switch_core.tenant_context import no_tenant
+from tests.conftest import RLSHarness
+
+NOW = datetime(2026, 10, 6, 12, 0, tzinfo=UTC)
+
+LIVE = "audit-history-live"
+LONG_GONE = "audit-history-long-gone"
+RECENTLY_GONE = "audit-history-recently-gone"
+
+
+async def _history(sessions: async_sessionmaker[AsyncSession], tenant_id: str) -> None:
+ async with tenant_session(sessions, tenant_id) as session:
+ session.add(
+ AuditEvent(
+ action="member.removed",
+ target_type="user",
+ target_id="someone",
+ occurred_at=NOW - timedelta(days=1000),
+ )
+ )
+ session.add(
+ AuditEvent(
+ action="tenant.deleted",
+ target_type="tenant",
+ target_id=tenant_id,
+ occurred_at=NOW - timedelta(days=900),
+ )
+ )
+ await session.commit()
+
+
+async def _deleted(
+ sessions: async_sessionmaker[AsyncSession], tenant_id: str, ago: timedelta
+) -> None:
+ with no_tenant():
+ async with sessions() as session:
+ session.add(
+ ServerAuditEvent(
+ action=ServerAuditAction.WORKSPACE_DELETED,
+ target_type="tenant",
+ target_id=tenant_id,
+ occurred_at=NOW - ago,
+ )
+ )
+ await session.commit()
+
+
+async def _audit_rows(
+ sessions: async_sessionmaker[AsyncSession], tenant_id: str
+) -> int:
+ async with tenant_session(sessions, tenant_id) as session:
+ return int(
+ await session.scalar(
+ select(func.count())
+ .select_from(AuditEvent)
+ .where(AuditEvent.tenant_id == tenant_id)
+ )
+ or 0
+ )
+
+
+async def _server_rows(sessions: async_sessionmaker[AsyncSession]) -> int:
+ with no_tenant():
+ async with sessions() as session:
+ return int(
+ await session.scalar(select(func.count()).select_from(ServerAuditEvent))
+ or 0
+ )
+
+
+async def _live_tenant(sessions: async_sessionmaker[AsyncSession]) -> None:
+ with no_tenant():
+ async with sessions() as session:
+ session.add(Tenant(id=LIVE, slug=LIVE, name=LIVE))
+ await session.commit()
+
+
+async def test_only_workspaces_deleted_beyond_the_horizon_lose_their_history(
+ session_factory: async_sessionmaker[AsyncSession],
+) -> None:
+ await _live_tenant(session_factory)
+ for tenant_id in (LIVE, LONG_GONE, RECENTLY_GONE):
+ await _history(session_factory, tenant_id)
+ # A live workspace with an old deletion on file (deleted, then a
+ # workspace with the same id made again) is still never touched.
+ await _deleted(session_factory, LIVE, AUDIT_HISTORY_HORIZON + timedelta(days=30))
+ await _deleted(
+ session_factory, LONG_GONE, AUDIT_HISTORY_HORIZON + timedelta(days=1)
+ )
+ await _deleted(
+ session_factory, RECENTLY_GONE, AUDIT_HISTORY_HORIZON - timedelta(days=1)
+ )
+
+ purged = await purge_deleted_workspace_history(session_factory, NOW)
+
+ assert purged == 2
+ assert await _audit_rows(session_factory, LIVE) == 2
+ assert await _audit_rows(session_factory, LONG_GONE) == 0
+ assert await _audit_rows(session_factory, RECENTLY_GONE) == 2
+ assert await _server_rows(session_factory) == 3
+
+ assert await purge_deleted_workspace_history(session_factory, NOW) == 0
+
+
+async def test_the_owner_may_purge_and_the_runtime_role_may_not(
+ rls_harness: RLSHarness, caplog: pytest.LogCaptureFixture
+) -> None:
+ with caplog.at_level(logging.ERROR, logger="switch_core.retention.audit_history"):
+ assert await check_purge_privilege(rls_harness.owner_engine, owner=True)
+ assert not caplog.records
+
+ assert not await check_purge_privilege(
+ rls_harness.restricted_engine, owner=False
+ )
+ assert "will NOT be purged" in caplog.text
+ assert "DB_OWNER_USER" in caplog.text
+
+
+async def test_the_runtime_role_cannot_purge_and_says_so(
+ rls_harness: RLSHarness,
+) -> None:
+ await _history(rls_harness.owner, LONG_GONE)
+ await _deleted(
+ rls_harness.owner, LONG_GONE, AUDIT_HISTORY_HORIZON + timedelta(days=1)
+ )
+
+ with pytest.raises(Exception, match="permission denied"):
+ await purge_deleted_workspace_history(rls_harness.restricted, NOW)
+ assert await _audit_rows(rls_harness.owner, LONG_GONE) == 2