Skip to content

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

Address review on erasing a person

Petr Bauch391ea83aa779
diff --git a/core/switch_core/bridges/collaboration/collaboration_core.py b/core/switch_core/bridges/collaboration/collaboration_core.py
index 993f90dca..3a4d3914a 100644
--- a/core/switch_core/bridges/collaboration/collaboration_core.py
+++ b/core/switch_core/bridges/collaboration/collaboration_core.py
@@ -2362,7 +2362,6 @@ class CollaborationCore:
self._human_actors.pop(external_user_id, None)
self._names_known_good.discard(external_user_id)
self._human_user_ids.discard(transport_user_id)
- self._human_actor_locks.pop(external_user_id, None)
async def _create_human_actor(
self, external_user_id: str, external_username: str
diff --git a/core/switch_core/db/stores/erasure_store.py b/core/switch_core/db/stores/erasure_store.py
index 6fe201c73..4dbe4cadc 100644
--- a/core/switch_core/db/stores/erasure_store.py
+++ b/core/switch_core/db/stores/erasure_store.py
@@ -11,7 +11,7 @@ from __future__ import annotations
from collections.abc import Collection
from dataclasses import dataclass
-from datetime import UTC, datetime
+from datetime import UTC, datetime, timedelta
from typing import Any, cast
from sqlalchemy import (
@@ -32,19 +32,30 @@ from sqlalchemy.ext.asyncio import AsyncSession
from switch_core.db.models import (
ApprovalRequest,
Client,
+ ClientRoom,
CollaborationBridge,
ExternalUser,
ExternalUserClaim,
+ HostedCutoverItem,
+ HostedWakeMailbox,
Message,
PersonErasure,
+ Room,
User,
require_tenant_id,
)
+from switch_core.db.stores.retention_store import UNREFERENCED_BLOB
#: What `answered_by` reads once the person who answered has been erased.
ERASED_ANSWERER = "erased"
+#: What stands in for an erased person's name in a direct room's name.
+ERASED_NAME = "erased person"
ACTIVE_STATES = ("queued", "running")
+#: A `running` request whose progress has not moved for this long is taken to
+#: belong to a process that died, and is resumed. Progress is recorded after
+#: every batch, so a live one never looks this old.
+STALE_RUNNING = timedelta(minutes=10)
class ErasureAlreadyQueued(Exception):
@@ -81,47 +92,62 @@ def _rowcount(result: Result[Any]) -> int:
class ErasureStore:
- async def list_people(
- self,
- session: AsyncSession,
- external_user_ids: Collection[str] | None = None,
+ async def list_people(self, session: AsyncSession) -> list[Person]:
+ """Every platform identity in the bound tenant."""
+ return await self._people(
+ session, ExternalUser.tenant_id == require_tenant_id()
+ )
+
+ async def people_with_ids(
+ self, session: AsyncSession, external_user_ids: Collection[str]
) -> list[Person]:
- """Every platform identity in the bound tenant, or the ones named."""
- tenant_id = require_tenant_id()
- counts = (
- select(Message.sender_id, func.count().label("n"))
- .where(Message.tenant_id == tenant_id)
- .group_by(Message.sender_id)
- .subquery()
+ """The named identities that are in the bound tenant."""
+ return await self._people(
+ session,
+ (ExternalUser.tenant_id == require_tenant_id())
+ & ExternalUser.id.in_(list(external_user_ids)),
)
- query = (
- select(
- ExternalUser.id,
- ExternalUser.external_username,
- ExternalUser.bridge_id,
- CollaborationBridge.type,
- CollaborationBridge.display_name,
- Client.id,
- Client.transport_user_id,
- func.coalesce(counts.c.n, 0),
- )
- .join(
- CollaborationBridge,
- (CollaborationBridge.tenant_id == ExternalUser.tenant_id)
- & (CollaborationBridge.id == ExternalUser.bridge_id),
- )
- .join(
- Client,
- (Client.tenant_id == ExternalUser.tenant_id)
- & (Client.id == ExternalUser.client_id),
+
+ async def _people(self, session: AsyncSession, condition: Any) -> list[Person]:
+ tenant_id = require_tenant_id()
+ # Correlated, so each identity's count is one lookup on
+ # `ix_messages_tenant_sender` rather than a count of the whole tenant.
+ message_count = (
+ select(func.count())
+ .select_from(Message)
+ .where(
+ Message.tenant_id == tenant_id,
+ Message.sender_id == Client.transport_user_id,
)
- .outerjoin(counts, counts.c.sender_id == Client.transport_user_id)
- .where(ExternalUser.tenant_id == tenant_id)
- .order_by(func.lower(ExternalUser.external_username), ExternalUser.id)
+ .correlate(Client)
+ .scalar_subquery()
)
- if external_user_ids is not None:
- query = query.where(ExternalUser.id.in_(list(external_user_ids)))
- rows = (await session.execute(query)).all()
+ rows = (
+ await session.execute(
+ select(
+ ExternalUser.id,
+ ExternalUser.external_username,
+ ExternalUser.bridge_id,
+ CollaborationBridge.type,
+ CollaborationBridge.display_name,
+ Client.id,
+ Client.transport_user_id,
+ message_count,
+ )
+ .join(
+ CollaborationBridge,
+ (CollaborationBridge.tenant_id == ExternalUser.tenant_id)
+ & (CollaborationBridge.id == ExternalUser.bridge_id),
+ )
+ .join(
+ Client,
+ (Client.tenant_id == ExternalUser.tenant_id)
+ & (Client.id == ExternalUser.client_id),
+ )
+ .where(condition)
+ .order_by(func.lower(ExternalUser.external_username), ExternalUser.id)
+ )
+ ).all()
claims: dict[str, list[Claimant]] = {}
if rows:
@@ -164,14 +190,21 @@ class ErasureStore:
Locks the tenant's erasure queue for the transaction, so two owners
asking at once cannot both pass the "nothing already queued" check.
"""
+ tenant_id = require_tenant_id()
await session.execute(
text("SELECT pg_advisory_xact_lock(hashtext('person_erasures:' || :t))"),
- {"t": require_tenant_id()},
+ {"t": tenant_id},
+ )
+ known = set(
+ (
+ await session.execute(
+ select(ExternalUser.id).where(
+ ExternalUser.tenant_id == tenant_id,
+ ExternalUser.id.in_(external_user_ids),
+ )
+ )
+ ).scalars()
)
- known = {
- p.external_user_id
- for p in await self.list_people(session, external_user_ids)
- }
unknown = set(external_user_ids) - known
if unknown:
raise UnknownIdentity(", ".join(sorted(unknown)))
@@ -179,7 +212,7 @@ class ErasureStore:
select(func.count())
.select_from(PersonErasure)
.where(
- PersonErasure.tenant_id == require_tenant_id(),
+ PersonErasure.tenant_id == tenant_id,
PersonErasure.state.in_(ACTIVE_STATES),
PersonErasure.external_user_ids.op("?|")(
cast_(array(external_user_ids), ARRAY(Text))
@@ -209,33 +242,49 @@ class ErasureStore:
)
return list(result.scalars())
- async def next_pending(self, session: AsyncSession) -> PersonErasure | None:
- """The oldest request still to finish. A `running` one is resumed: the
- work is safe to repeat, and one left running by a restart is otherwise
- never finished."""
- result = await session.execute(
- select(PersonErasure)
+ async def claim_next(
+ self, session: AsyncSession, now: datetime
+ ) -> PersonErasure | None:
+ """Claim the oldest request to work: a queued one, or a running one whose
+ process stopped recording progress. Marks it running in the same
+ statement, and skips a row another process holds, so two processes
+ overlapping in a deploy cannot both work one request."""
+ tenant_id = require_tenant_id()
+ candidate = (
+ select(PersonErasure.id)
.where(
- PersonErasure.tenant_id == require_tenant_id(),
- PersonErasure.state.in_(ACTIVE_STATES),
+ PersonErasure.tenant_id == tenant_id,
+ (PersonErasure.state == "queued")
+ | (
+ (PersonErasure.state == "running")
+ & (PersonErasure.updated_at < now - STALE_RUNNING)
+ ),
)
.order_by(PersonErasure.created_at)
.limit(1)
+ .with_for_update(skip_locked=True)
+ .scalar_subquery()
+ )
+ result = await session.execute(
+ update(PersonErasure)
+ .where(PersonErasure.tenant_id == tenant_id, PersonErasure.id == candidate)
+ .values(state="running", updated_at=now, error=None)
+ .returning(PersonErasure),
+ execution_options={"populate_existing": True},
)
return result.scalar_one_or_none()
- async def mark(
+ async def add_progress(
self,
session: AsyncSession,
erasure_id: str,
*,
- state: str,
- messages_deleted: int,
- files_deleted: int,
- identities_erased: int,
- error: str | None,
+ messages: int,
+ files: int,
+ identities: int,
) -> None:
- finished = state in ("done", "failed")
+ """Add to the request's counts, in the transaction that did the work, so
+ a resumed request neither loses nor double-counts what was done."""
await session.execute(
update(PersonErasure)
.where(
@@ -243,29 +292,107 @@ class ErasureStore:
PersonErasure.id == erasure_id,
)
.values(
- state=state,
- messages_deleted=messages_deleted,
- files_deleted=files_deleted,
- identities_erased=identities_erased,
+ messages_deleted=PersonErasure.messages_deleted + messages,
+ files_deleted=PersonErasure.files_deleted + files,
+ identities_erased=PersonErasure.identities_erased + identities,
+ updated_at=func.now(),
+ )
+ )
+
+ async def finish(
+ self, session: AsyncSession, erasure_id: str, *, error: str | None
+ ) -> PersonErasure:
+ """Mark the request done, or failed with `error`."""
+ result = await session.execute(
+ update(PersonErasure)
+ .where(
+ PersonErasure.tenant_id == require_tenant_id(),
+ PersonErasure.id == erasure_id,
+ )
+ .values(
+ state="failed" if error is not None else "done",
error=error,
- completed_at=datetime.now(UTC) if finished else None,
+ completed_at=datetime.now(UTC),
+ )
+ .returning(PersonErasure),
+ execution_options={"populate_existing": True},
+ )
+ return result.scalar_one()
+
+ async def delete_hosted_copies(
+ self, session: AsyncSession, transport_event_ids: Collection[str]
+ ) -> None:
+ """Delete the copies of these (deleted) messages held for hosted agents:
+ deliveries not yet admitted, and items a cutover would import."""
+ if not transport_event_ids:
+ return
+ ids = list(transport_event_ids)
+ await session.execute(
+ delete(HostedWakeMailbox).where(
+ HostedWakeMailbox.tenant_id == require_tenant_id(),
+ HostedWakeMailbox.message_id.in_(ids),
+ )
+ )
+ await session.execute(
+ delete(HostedCutoverItem).where(
+ HostedCutoverItem.tenant_id == require_tenant_id(),
+ HostedCutoverItem.message_id.in_(ids),
)
)
async def scrub_approval_answers(
self, session: AsyncSession, transport_user_id: str
) -> int:
- """Replace this participant's name on every approval answer they gave."""
+ """Replace this participant's name on every approval answer they gave,
+ and drop the words they typed into it. Which options they picked is
+ kept: it is the agent's record of what it was told to do."""
result = await session.execute(
update(ApprovalRequest)
.where(
ApprovalRequest.tenant_id == require_tenant_id(),
ApprovalRequest.answered_by == transport_user_id,
)
- .values(answered_by=ERASED_ANSWERER)
+ .values(
+ answered_by=ERASED_ANSWERER,
+ answers=text(
+ "(SELECT jsonb_agg(a - 'custom_text') "
+ "FROM jsonb_array_elements(answers) a)"
+ ),
+ )
)
return _rowcount(result)
+ async def scrub_direct_rooms(self, session: AsyncSession, person: Person) -> int:
+ """Take this person's name out of the direct rooms they were in.
+
+ A direct room is named after the person and the agent when the bridge
+ adopts it. Must run while their room memberships still stand.
+ """
+ rooms = (
+ await session.execute(
+ select(Room)
+ .join(
+ ClientRoom,
+ (ClientRoom.tenant_id == Room.tenant_id)
+ & (ClientRoom.room_id == Room.id),
+ )
+ .where(
+ Room.tenant_id == require_tenant_id(),
+ Room.channel_type == "direct",
+ ClientRoom.client_id == person.client_id,
+ )
+ )
+ ).scalars()
+ scrubbed = 0
+ for room in rooms:
+ name = room.name.replace(person.username, ERASED_NAME)
+ description = room.description.replace(person.username, ERASED_NAME)
+ if (name, description) != (room.name, room.description):
+ room.name, room.description = name, description
+ scrubbed += 1
+ await session.flush()
+ return scrubbed
+
async def delete_identity(
self, session: AsyncSession, external_user_id: str
) -> None:
@@ -282,7 +409,7 @@ class ErasureStore:
async def delete_unreferenced_media(
self, session: AsyncSession, uris: Collection[str]
) -> int:
- """Delete the stored files among `uris` that no attachment refers to now.
+ """Delete the stored files among `uris` that nothing refers to now.
Unlike the hourly sweep this has no grace period: these files are known
to have been attached to the erased person's messages, so none is an
@@ -292,12 +419,10 @@ class ErasureStore:
return 0
result = await session.execute(
text(
- """
+ f"""
DELETE FROM media_blobs b
WHERE b.tenant_id = :t AND b.uri = ANY(:uris)
- AND NOT EXISTS (
- SELECT 1 FROM message_attachments a
- WHERE a.tenant_id = b.tenant_id AND a.uri = b.uri)
+ AND {UNREFERENCED_BLOB}
"""
),
{"t": require_tenant_id(), "uris": list(uris)},
diff --git a/core/switch_core/db/stores/message_store.py b/core/switch_core/db/stores/message_store.py
index 9a261df99..c8801a243 100644
--- a/core/switch_core/db/stores/message_store.py
+++ b/core/switch_core/db/stores/message_store.py
@@ -1,4 +1,5 @@
from collections.abc import Collection
+from dataclasses import dataclass
from datetime import datetime
from sqlalchemy import (
@@ -19,6 +20,13 @@ from switch_core.db.notify_ddl import SET_WRITER_ANNOUNCES
from switch_core.db.raw import RawStatement
+@dataclass(frozen=True)
+class DeletedMessages:
+ event_ids: list[str]
+ #: The files the deleted messages carried. Others may still quote them.
+ uris: set[str]
+
+
class MessageStore:
async def create(
self,
@@ -216,49 +224,38 @@ class MessageStore:
See `_delete_where` for what else changes and what does not.
"""
- return await self._delete_where(session, Message.sent_at < cutoff, limit=limit)
+ deleted = await self._delete_where(
+ session, Message.sent_at < cutoff, limit=limit
+ )
+ return deleted.event_ids
async def delete_sent_by(
self, session: AsyncSession, *, transport_user_id: str, limit: int
- ) -> list[str]:
+ ) -> DeletedMessages:
"""Delete up to `limit` of the messages one participant sent in the bound
- tenant, with their attachments. Returns their transport event ids.
+ tenant, with their attachments.
Matched on `sender_id`, the participant's transport id, rather than
`sender_client_id`: every row has the first, while the second is null
- on reconstructed history and on rows older than the column. See
- `_delete_where` for the rest.
+ on reconstructed history, on rows older than the column, and on rows
+ whose client has been deleted. See `_delete_where` for the rest.
"""
return await self._delete_where(
session, Message.sender_id == transport_user_id, limit=limit
)
- async def attachment_uris_sent_by(
- self, session: AsyncSession, *, transport_user_id: str
- ) -> set[str]:
- """The files on every message one participant sent in the bound tenant."""
- result = await session.execute(
- select(MessageAttachment.uri)
- .join(Message, Message.id == MessageAttachment.message_id)
- .where(
- Message.tenant_id == require_tenant_id(),
- Message.sender_id == transport_user_id,
- )
- .distinct()
- )
- return set(result.scalars())
-
async def _delete_where(
self, session: AsyncSession, condition: ColumnElement[bool], *, limit: int
- ) -> list[str]:
+ ) -> DeletedMessages:
"""Delete up to `limit` of the bound tenant's oldest messages matching
- `condition`, with their attachments. Returns their transport event ids.
+ `condition`, with their attachments.
Each room's `seq_floor` is raised to the highest live position deleted
from it, in the same transaction, so `_next_seq` never reissues one.
The bytes behind the attachments are not touched: a blob may be quoted
- by a message that is kept, so `RetentionStore.delete_unreferenced_media`
- finds the ones nothing points at any more.
+ by a message that is kept. The result names them, and
+ `RetentionStore.delete_unreferenced_media` finds the ones nothing
+ points at any more.
"""
rows = (
await session.execute(
@@ -271,7 +268,17 @@ class MessageStore:
)
).all()
if not rows:
- return []
+ return DeletedMessages(event_ids=[], uris=set())
+ message_ids = [r.id for r in rows]
+ uris = set(
+ (
+ await session.execute(
+ select(MessageAttachment.uri).where(
+ MessageAttachment.message_id.in_(message_ids)
+ )
+ )
+ ).scalars()
+ )
floors: dict[str, int] = {}
for row in rows:
floors[row.room_id] = max(floors.get(row.room_id, 0), row.seq)
@@ -282,10 +289,10 @@ class MessageStore:
.where(Room.id == room_id)
.values(seq_floor=func.greatest(Room.seq_floor, seq))
)
- await session.execute(
- delete(Message).where(Message.id.in_([r.id for r in rows]))
+ await session.execute(delete(Message).where(Message.id.in_(message_ids)))
+ return DeletedMessages(
+ event_ids=[row.transport_event_id for row in rows], uris=uris
)
- return [row.transport_event_id for row in rows]
async def head_seqs(
self,
diff --git a/core/switch_core/db/stores/retention_store.py b/core/switch_core/db/stores/retention_store.py
index 3573b76a6..8101096f4 100644
--- a/core/switch_core/db/stores/retention_store.py
+++ b/core/switch_core/db/stores/retention_store.py
@@ -25,26 +25,30 @@ from switch_core.db.models import (
require_tenant_id,
)
+#: A stored file no attachment names and no live hosted-cutover import is
+#: waiting to carry, for a `media_blobs` row aliased `b`. Shared with erasing a
+#: person, which deletes files by the same rule without the age condition.
+UNREFERENCED_BLOB = """
+ NOT EXISTS (
+ SELECT 1 FROM message_attachments a
+ WHERE a.tenant_id = b.tenant_id AND a.uri = b.uri)
+ AND NOT EXISTS (
+ SELECT 1 FROM hosted_cutover_items i
+ JOIN hosted_launches l ON l.tenant_id = i.tenant_id AND l.id = i.launch_id
+ AND l.state NOT IN ('deleting', 'deleted')
+ CROSS JOIN LATERAL jsonb_array_elements(
+ COALESCE(i.payload->'payload'->'attachments', '[]'::jsonb)) f
+ WHERE i.tenant_id = b.tenant_id AND i.disposition = 'import'
+ AND f->>'mxc' = b.uri)
+"""
+
# A blob is written before the message that carries it, in a separate
-# transaction, and a hosted cutover may hold one for an import it has not
-# queued yet. Both are referenced by something other than an attachment row,
-# so a blob is only a candidate once it has had time to gain one, and is
-# never one while a live import names it.
-_UNREFERENCED_BLOBS = """
+# transaction, so it is only a candidate once it has had time to gain one.
+_UNREFERENCED_BLOBS = f"""
SELECT b.id FROM media_blobs b
WHERE b.tenant_id = :tenant_id
AND b.created_at < :created_before
- AND NOT EXISTS (
- SELECT 1 FROM message_attachments a
- WHERE a.tenant_id = b.tenant_id AND a.uri = b.uri)
- AND NOT EXISTS (
- SELECT 1 FROM hosted_cutover_items i
- JOIN hosted_launches l ON l.tenant_id = i.tenant_id AND l.id = i.launch_id
- AND l.state NOT IN ('deleting', 'deleted')
- CROSS JOIN LATERAL jsonb_array_elements(
- COALESCE(i.payload->'payload'->'attachments', '[]'::jsonb)) f
- WHERE i.tenant_id = b.tenant_id AND i.disposition = 'import'
- AND f->>'mxc' = b.uri)
+ AND {UNREFERENCED_BLOB}
ORDER BY b.created_at
LIMIT :limit
"""
diff --git a/core/switch_core/main.py b/core/switch_core/main.py
index 5160e59b1..f60807b44 100644
--- a/core/switch_core/main.py
+++ b/core/switch_core/main.py
@@ -213,6 +213,7 @@ from switch_core.provisioning.postgres import PostgresProvisioning
from switch_core.room_changes import COALESCE_SECONDS, RoomChanges
from switch_core.retention.service import retention_loop
from switch_core.retention.erasure import ErasureService, erasure_loop
+from switch_core.retention.service import retention_loop
from switch_core.room_service import RoomService
from switch_core.session_activity.listener import AgentSessionActivityListener
from switch_core.session_activity.maintenance import (
diff --git a/core/switch_core/migrations/versions/e5c1b7a93d28_person_erasures.py b/core/switch_core/migrations/versions/e5c1b7a93d28_person_erasures.py
index 9d6d4289f..9efb0731d 100644
--- a/core/switch_core/migrations/versions/e5c1b7a93d28_person_erasures.py
+++ b/core/switch_core/migrations/versions/e5c1b7a93d28_person_erasures.py
@@ -4,6 +4,14 @@
works through the queued ones. ``ix_messages_tenant_sender`` serves
finding and counting a person's messages, which nothing indexed before.
+**Not `CONCURRENTLY`**, for the reason ``b8f2d0c41e57`` gives: building it
+holds a lock on ``messages``, blocking sends, until the migration commits. A
+deployment with a large ``messages`` table should build it by hand first, and
+this becomes a no-op:
+
+ CREATE INDEX CONCURRENTLY ix_messages_tenant_sender
+ ON messages (tenant_id, sender_id);
+
The row-level-security DDL is a verbatim copy of ``switch_core/db/rls_ddl.py``
as it stood when this migration was written, copied rather than imported for
the reason ``265ed188ad6f`` gives.
@@ -38,10 +46,10 @@ _CREATE_POLICY = (
def upgrade() -> None:
- op.create_index(
- "ix_messages_tenant_sender",
- "messages",
- ["tenant_id", "sender_id"],
+ # `IF NOT EXISTS` so building it concurrently ahead of time works.
+ op.execute(
+ "CREATE INDEX IF NOT EXISTS ix_messages_tenant_sender "
+ "ON messages (tenant_id, sender_id)"
)
op.create_table(
"person_erasures",
@@ -102,4 +110,4 @@ def downgrade() -> None:
op.execute(f'DROP POLICY IF EXISTS {POLICY_NAME} ON "person_erasures"')
op.drop_index("ix_person_erasures_tenant_id", table_name="person_erasures")
op.drop_table("person_erasures")
- op.drop_index("ix_messages_tenant_sender", table_name="messages")
+ op.execute("DROP INDEX IF EXISTS ix_messages_tenant_sender")
diff --git a/core/switch_core/retention/erasure.py b/core/switch_core/retention/erasure.py
index f3518d918..e1cf11443 100644
--- a/core/switch_core/retention/erasure.py
+++ b/core/switch_core/retention/erasure.py
@@ -4,17 +4,20 @@ A workspace owner queues a request naming one or more platform identities
(`ErasureStore.queue`). `erasure_loop` works the queue: for each identity it
1. deletes every message the identity sent, in every room, archived ones
- included, in batches with their attachments and bridge post mappings;
+ included, in batches with their attachments, bridge post mappings and the
+ copies held for hosted agents;
2. stops the client that stood in for them in rooms;
-3. in one transaction, deletes anything they sent meanwhile, replaces their
- name on approval answers, and deletes their identity row (its claims go by
- cascade), their room memberships and their client;
+3. in one transaction, scrubs their name and words from approval answers and
+ their name from direct rooms' names, deletes their identity row (its
+ claims go by cascade), their room memberships and their client, and then
+ anything they sent meanwhile;
4. tells the running bridge to forget them, so writing again provisions a
new identity;
5. deletes the stored files that only their messages carried.
-Every step is safe to repeat, so a request interrupted by a restart is simply
-worked again. Kept: what other people wrote, including quotes and mentions of
+Every step is safe to repeat, and a request is claimed before it is worked,
+so one interrupted by a restart is resumed by the next pass and two processes
+never work the same one. Kept: what other people wrote, including quotes and mentions of
them; copies on the chat platforms; their Switch account and membership,
which have their own actions. `docs/design/data-retention.md` covers why.
"""
@@ -23,6 +26,7 @@ from __future__ import annotations
import asyncio
import logging
+from datetime import UTC, datetime
from typing import Protocol
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
@@ -68,147 +72,140 @@ class ErasureService:
self._messages = MessageStore()
self._retention = RetentionStore()
- async def work_once(self) -> PersonErasure | None:
- """Work the bound tenant's oldest pending request to its end, if any."""
+ async def work_once(self, now: datetime) -> PersonErasure | None:
+ """Claim the bound tenant's oldest pending request and work it to its
+ end. Returns it as finished, done or failed, or None if there was none.
+
+ A failure before an identity's rows are deleted marks the request
+ failed, with the error, and leaves that identity in place to be queued
+ again. Counts are added in the transaction that did the work, so a
+ request resumed after a restart reports everything it deleted.
+ """
tenant_id = require_tenant_id()
- async with tenant_session(self._sessions, tenant_id) as db:
- erasure = await self._erasures.next_pending(db)
+ async with tenant_session(self._sessions, tenant_id) as db, db.begin():
+ erasure = await self._erasures.claim_next(db, now)
if erasure is None:
return None
- messages = files = identities = 0
+ error: str | None = None
try:
- async with tenant_session(self._sessions, tenant_id) as db, db.begin():
- await self._erasures.mark(
- db,
- erasure.id,
- state="running",
- messages_deleted=erasure.messages_deleted,
- files_deleted=erasure.files_deleted,
- identities_erased=erasure.identities_erased,
- error=None,
- )
- messages, files, identities = (
- erasure.messages_deleted,
- erasure.files_deleted,
- erasure.identities_erased,
- )
async with tenant_session(self._sessions, tenant_id) as db:
- people = await self._erasures.list_people(db, erasure.external_user_ids)
+ people = await self._erasures.people_with_ids(
+ db, erasure.external_user_ids
+ )
for person in people:
- deleted, removed_files = await self._erase(tenant_id, person)
- messages += deleted
- files += removed_files
- identities += 1
- async with tenant_session(self._sessions, tenant_id) as db, db.begin():
- await self._erasures.mark(
- db,
- erasure.id,
- state="running",
- messages_deleted=messages,
- files_deleted=files,
- identities_erased=identities,
- error=None,
- )
+ await self._erase(tenant_id, erasure.id, person)
except Exception as exc:
- logger.exception(
- "Erasure %s in tenant %s failed after %d message(s)",
- erasure.id,
- tenant_id,
- messages,
- )
- async with tenant_session(self._sessions, tenant_id) as db, db.begin():
- await self._erasures.mark(
- db,
- erasure.id,
- state="failed",
- messages_deleted=messages,
- files_deleted=files,
- identities_erased=identities,
- error=f"{type(exc).__name__}: {exc}",
- )
- raise
+ logger.exception("Erasure %s in tenant %s failed", erasure.id, tenant_id)
+ error = f"{type(exc).__name__}: {exc}"
async with tenant_session(self._sessions, tenant_id) as db, db.begin():
- await self._erasures.mark(
- db,
- erasure.id,
- state="done",
- messages_deleted=messages,
- files_deleted=files,
- identities_erased=identities,
- error=None,
- )
- await record_audit_event(
- db,
- tenant_id=tenant_id,
- actor_user_id=erasure.requested_by_user_id,
- action=AuditAction.PERSON_ERASURE_COMPLETED,
- target_type="person_erasure",
- target_id=erasure.id,
- details={
- "messages_deleted": messages,
- "files_deleted": files,
- "identities_erased": identities,
- },
+ finished = await self._erasures.finish(db, erasure.id, error=error)
+ if error is None:
+ await record_audit_event(
+ db,
+ tenant_id=tenant_id,
+ actor_user_id=finished.requested_by_user_id,
+ action=AuditAction.PERSON_ERASURE_COMPLETED,
+ target_type="person_erasure",
+ target_id=finished.id,
+ details={
+ "messages_deleted": finished.messages_deleted,
+ "files_deleted": finished.files_deleted,
+ "identities_erased": finished.identities_erased,
+ },
+ )
+ if error is None:
+ logger.info(
+ "Erasure %s in tenant %s done: %d identities, %d messages, %d files",
+ finished.id,
+ tenant_id,
+ finished.identities_erased,
+ finished.messages_deleted,
+ finished.files_deleted,
)
- logger.info(
- "Erasure %s in tenant %s done: %d identities, %d messages, %d files",
- erasure.id,
- tenant_id,
- identities,
- messages,
- files,
- )
- return erasure
+ return finished
- async def _erase(self, tenant_id: str, person: Person) -> tuple[int, int]:
- async with tenant_session(self._sessions, tenant_id) as db:
- uris = await self._messages.attachment_uris_sent_by(
- db, transport_user_id=person.transport_user_id
- )
- deleted = 0
+ async def _erase(self, tenant_id: str, erasure_id: str, person: Person) -> None:
+ uris: set[str] = set()
while True:
async with tenant_session(self._sessions, tenant_id) as db, db.begin():
- event_ids = await self._messages.delete_sent_by(
- db, transport_user_id=person.transport_user_id, limit=MESSAGE_BATCH
- )
- await self._retention.delete_bridge_mappings(db, event_ids)
- deleted += len(event_ids)
- if len(event_ids) < MESSAGE_BATCH:
+ count = await self._delete_batch(db, erasure_id, person, uris)
+ if count < MESSAGE_BATCH:
break
+ # Stopping the client ends its delivery but not, by itself, a send
+ # already under way. The final transaction deletes the client before
+ # it looks for messages a last time: the delete waits for any insert
+ # naming the client to commit, and once it has committed no insert can
+ # name it, so nothing they send outlives this transaction.
await self._clients.stop(person.client_id)
async with tenant_session(self._sessions, tenant_id) as db, db.begin():
- while True:
- event_ids = await self._messages.delete_sent_by(
- db, transport_user_id=person.transport_user_id, limit=MESSAGE_BATCH
- )
- await self._retention.delete_bridge_mappings(db, event_ids)
- deleted += len(event_ids)
- if len(event_ids) < MESSAGE_BATCH:
- break
await self._erasures.scrub_approval_answers(db, person.transport_user_id)
+ await self._erasures.scrub_direct_rooms(db, person)
await self._erasures.delete_identity(db, person.external_user_id)
await self._clients.delete_record(db, person.client_id)
- await self._bridges.forget_human(
- person.bridge_id, person.external_user_id, person.transport_user_id
- )
+ while (
+ await self._delete_batch(db, erasure_id, person, uris) == MESSAGE_BATCH
+ ):
+ pass
+ await self._erasures.add_progress(
+ db, erasure_id, messages=0, files=0, identities=1
+ )
- async with tenant_session(self._sessions, tenant_id) as db, db.begin():
- files = await self._erasures.delete_unreferenced_media(db, uris)
- return deleted, files
+ # The identity is gone from here on, so a failure below cannot be put
+ # right by queueing it again; it is logged rather than failing the
+ # request. Files missed here are taken by the hourly sweep a day later.
+ try:
+ await self._bridges.forget_human(
+ person.bridge_id, person.external_user_id, person.transport_user_id
+ )
+ except Exception:
+ logger.warning(
+ "Erasure %s: the bridge could not forget identity %s; it does "
+ "when it next restarts",
+ erasure_id,
+ person.external_user_id,
+ exc_info=True,
+ )
+ try:
+ async with tenant_session(self._sessions, tenant_id) as db, db.begin():
+ files = await self._erasures.delete_unreferenced_media(db, uris)
+ await self._erasures.add_progress(
+ db, erasure_id, messages=0, files=files, identities=0
+ )
+ except Exception:
+ logger.warning(
+ "Erasure %s: could not delete the files of identity %s; the "
+ "hourly sweep deletes them a day later",
+ erasure_id,
+ person.external_user_id,
+ exc_info=True,
+ )
+
+ async def _delete_batch(
+ self, db: AsyncSession, erasure_id: str, person: Person, uris: set[str]
+ ) -> int:
+ deleted = await self._messages.delete_sent_by(
+ db, transport_user_id=person.transport_user_id, limit=MESSAGE_BATCH
+ )
+ await self._retention.delete_bridge_mappings(db, deleted.event_ids)
+ await self._erasures.delete_hosted_copies(db, deleted.event_ids)
+ await self._erasures.add_progress(
+ db, erasure_id, messages=len(deleted.event_ids), files=0, identities=0
+ )
+ uris |= deleted.uris
+ return len(deleted.event_ids)
async def erase_once(service: ErasureService, tenant_ids: list[str]) -> None:
for tenant_id in tenant_ids:
with tenant_scope(tenant_id):
try:
- while await service.work_once() is not None:
+ while await service.work_once(datetime.now(UTC)) is not None:
pass
except Exception:
- # Recorded on the request as failed; one tenant's failure must
- # not stop the others' erasures.
+ # One tenant's failure must not stop the others' erasures.
logger.exception("Erasure upkeep failed for tenant %s", tenant_id)
diff --git a/core/tests/switch_core/retention/test_erasure.py b/core/tests/switch_core/retention/test_erasure.py
index 8909a4baf..537283cc1 100644
--- a/core/tests/switch_core/retention/test_erasure.py
+++ b/core/tests/switch_core/retention/test_erasure.py
@@ -9,7 +9,7 @@ other people's messages, files those still quote, and other identities stay.
from __future__ import annotations
import uuid
-from datetime import UTC, datetime
+from datetime import UTC, datetime, timedelta
import pytest
from sqlalchemy import func, select
@@ -25,6 +25,8 @@ from switch_core.db.models import (
CollaborationBridge,
ExternalUser,
ExternalUserClaim,
+ HostedLaunch,
+ HostedWakeMailbox,
MediaBlob,
Message,
MessageAttachment,
@@ -263,7 +265,9 @@ class TestErasing:
erasure_id = await _queue(session_factory, [ana_id])
clients, bridges = _Clients(), _Bridges()
- await ErasureService(session_factory, clients, bridges).work_once()
+ await ErasureService(session_factory, clients, bridges).work_once(
+ datetime.now(UTC)
+ )
async with session_factory() as session:
events = set(
@@ -342,7 +346,9 @@ class TestErasing:
ids = [on_slack.id, on_other.id]
erasure_id = await _queue(session_factory, ids)
- await ErasureService(session_factory, _Clients(), _Bridges()).work_once()
+ await ErasureService(session_factory, _Clients(), _Bridges()).work_once(
+ datetime.now(UTC)
+ )
async with session_factory() as session:
assert await session.scalar(select(func.count()).select_from(Message)) == 0
@@ -366,23 +372,44 @@ class TestErasing:
session_factory, _Clients(fail_on_stop=True), _Bridges()
)
- with pytest.raises(RuntimeError):
- await service.work_once()
+ finished = await service.work_once(datetime.now(UTC))
async with session_factory() as session:
- erasure = await session.get(PersonErasure, erasure_id)
still_there = await session.get(ExternalUser, person_id)
- assert erasure is not None
- assert erasure.state == "failed"
- assert erasure.error == "RuntimeError: client would not stop"
+ assert finished is not None and finished.id == erasure_id
+ assert finished.state == "failed"
+ assert finished.error == "RuntimeError: client would not stop"
assert still_there is not None
- assert await service.work_once() is None
+ assert await service.work_once(datetime.now(UTC)) is None
+ await _queue(session_factory, [person_id])
+
+ async def test_a_failure_after_the_identity_is_gone_does_not_fail_the_request(
+ self, session_factory: async_sessionmaker[AsyncSession]
+ ) -> None:
+ class _BrokenBridges(_Bridges):
+ async def forget_human(self, *args: str) -> None:
+ raise RuntimeError("bridge unreachable")
+
+ async with session_factory() as session:
+ person = await _person(session, await _bridge(session), "ana")
+ await session.commit()
+ person_id = person.id
+ await _queue(session_factory, [person_id])
+
+ finished = await ErasureService(
+ session_factory, _Clients(), _BrokenBridges()
+ ).work_once(datetime.now(UTC))
- async def test_a_request_left_running_by_a_restart_is_finished(
+ assert finished is not None
+ assert (finished.state, finished.identities_erased) == ("done", 1)
+
+ async def test_a_request_left_running_by_a_dead_process_is_finished(
self, session_factory: async_sessionmaker[AsyncSession]
) -> None:
async with session_factory() as session:
person = await _person(session, await _bridge(session), "ana")
+ room = await _room(session, "r")
+ await _say(session, room, await _client_of(session, person), "$a")
await session.commit()
person_id = person.id
erasure_id = await _queue(session_factory, [person_id])
@@ -390,13 +417,128 @@ class TestErasing:
erasure = await session.get(PersonErasure, erasure_id)
assert erasure is not None
erasure.state = "running"
+ erasure.messages_deleted = 7
await session.commit()
+ service = ErasureService(session_factory, _Clients(), _Bridges())
- await ErasureService(session_factory, _Clients(), _Bridges()).work_once()
+ assert await service.work_once(datetime.now(UTC)) is None
+ finished = await service.work_once(datetime.now(UTC) + timedelta(minutes=11))
+ assert finished is not None
+ assert (finished.state, finished.messages_deleted) == ("done", 8)
+
+ async def test_their_words_on_approvals_go_and_their_choice_stays(
+ self, session_factory: async_sessionmaker[AsyncSession]
+ ) -> None:
async with session_factory() as session:
- erasure = await session.get(PersonErasure, erasure_id)
- assert erasure is not None and erasure.state == "done"
+ person = await _person(session, await _bridge(session), "ana")
+ client = await _client_of(session, person)
+ agent = await _agent(session)
+ session.add(
+ ApprovalRequest(
+ agent_id=agent.id,
+ session_id="s",
+ request_id="q",
+ turn_id="t",
+ kind="questions",
+ title="t",
+ options=[],
+ questions=[],
+ state="answered",
+ answered_by=client.transport_user_id,
+ answers=[
+ {
+ "question_id": "q1",
+ "selected_option_ids": ["yes"],
+ "custom_text": "my home address is ...",
+ }
+ ],
+ )
+ )
+ await session.commit()
+ person_id = person.id
+ await _queue(session_factory, [person_id])
+
+ await ErasureService(session_factory, _Clients(), _Bridges()).work_once(
+ datetime.now(UTC)
+ )
+
+ async with session_factory() as session:
+ row = (await session.execute(select(ApprovalRequest))).scalar_one()
+ assert row.answered_by == ERASED_ANSWERER
+ assert row.answers == [{"question_id": "q1", "selected_option_ids": ["yes"]}]
+
+ async def test_their_name_leaves_direct_rooms_and_hosted_copies_go(
+ self, session_factory: async_sessionmaker[AsyncSession]
+ ) -> None:
+ async with session_factory() as session:
+ person = await _person(session, await _bridge(session), "ana")
+ client = await _client_of(session, person)
+ dm = Room(
+ transport_room_id=f"!dm-{uuid.uuid4().hex[:8]}:test",
+ name="Acme Slack: ana / helper",
+ description="Acme Slack DM — ana / helper",
+ channel_type="direct",
+ )
+ channel = Room(
+ transport_room_id=f"!ch-{uuid.uuid4().hex[:8]}:test",
+ name="Acme Slack: ana-fans",
+ description="channel",
+ channel_type="channel_public",
+ )
+ session.add_all([dm, channel])
+ await session.flush()
+ session.add_all(
+ [
+ ClientRoom(client_id=client.id, room_id=dm.id),
+ ClientRoom(client_id=client.id, room_id=channel.id),
+ ]
+ )
+ message = await _say(session, dm, client, "$to-hosted")
+ owner = User(name="launch-owner", email="[email protected]", role="user")
+ session.add(owner)
+ await session.flush()
+ launch = HostedLaunch(
+ id=f"launch-{uuid.uuid4().hex[:8]}",
+ owner_id=owner.id,
+ name="helper",
+ spec={},
+ state="ready",
+ agent_id=str(uuid.uuid4()),
+ )
+ session.add(launch)
+ await session.flush()
+ session.add(
+ HostedWakeMailbox(
+ agent_id=launch.agent_id,
+ room_id=dm.id,
+ message_id=message.transport_event_id,
+ launch_id=launch.id,
+ event={"body": "hello"},
+ addressed_at=datetime.now(UTC),
+ updated_at=datetime.now(UTC),
+ expires_at=datetime.now(UTC) + timedelta(hours=1),
+ )
+ )
+ await session.commit()
+ person_id, dm_id, channel_id = person.id, dm.id, channel.id
+ await _queue(session_factory, [person_id])
+
+ await ErasureService(session_factory, _Clients(), _Bridges()).work_once(
+ datetime.now(UTC)
+ )
+
+ async with session_factory() as session:
+ dm_after = await session.get(Room, dm_id)
+ channel_after = await session.get(Room, channel_id)
+ mailbox = await session.scalar(
+ select(func.count()).select_from(HostedWakeMailbox)
+ )
+ assert dm_after is not None and channel_after is not None
+ assert dm_after.name == "Acme Slack: erased person / helper"
+ assert dm_after.description == "Acme Slack DM — erased person / helper"
+ assert channel_after.name == "Acme Slack: ana-fans"
+ assert mailbox == 0
class TestQueue:
diff --git a/docs/design/data-retention.md b/docs/design/data-retention.md
index 2861e0320..d1b1206b0 100644
--- a/docs/design/data-retention.md
+++ b/docs/design/data-retention.md
@@ -128,9 +128,10 @@ cannot be undone.
**Who a person is.** Everyone in a room is a chat-platform identity: an
`external_users` row on one bridge, standing behind a `clients` row that is
the room participant. Most have no Switch account. One person seen on two
-platforms is two identities. When a member has claimed identities, the
-confirmation dialog offers every identity that member claims, ticked by
-default.
+platforms is two identities. When a member has claimed the chosen identity,
+the dialog also offers the other identities that member claims, unticked:
+claims are self-asserted and several members may claim one account, so a
+shared claimant is a hint, not proof of a shared person.
**The flow.**
@@ -140,24 +141,39 @@ default.
identities, and records `person_erasure.requested` (who asked, the internal
identity ids). It refuses an identity outside the workspace (404) or one
already being erased (409).
-3. A background loop (`retention/erasure.py`, every 5 s) works the queue. For
- each identity it:
- - deletes every message it sent, in every room including archived ones,
- in batches with their attachments and bridge post mappings, keeping each
- room's `seq_floor`;
+3. A background loop (`retention/erasure.py`, every 5 s) claims a request,
+ marking it running in the same statement and skipping one another process
+ holds, and works it. For each identity it:
+ - deletes every message it sent, in every room including archived ones, in
+ batches with their attachments, bridge post mappings and the copies held
+ for hosted agents (wake-mailbox deliveries not yet admitted, cutover
+ items), keeping each room's `seq_floor` and adding to the request's
+ counts in the same transaction;
- stops the client that stood in for the person;
- - in one transaction, deletes anything sent meanwhile, replaces their name
- on approval answers with `erased`, and deletes the identity row (claims
- go by cascade), its room memberships and its client;
+ - in one transaction: drops their name and the words they typed from
+ approval answers (`answered_by` becomes `erased`; the options they picked
+ stay, as the agent's record of what it was told), takes their name out of
+ the names of direct rooms they were in, deletes the identity row (claims
+ go by cascade), its room memberships and its client, and only then
+ deletes anything they sent meanwhile. Deleting the client waits for any
+ send naming it to commit, and afterwards no send can name it, so nothing
+ they send outlives the transaction;
- tells the running bridge to forget the identity, so writing again
provisions a new one;
- - deletes the stored files that only their messages carried. There is no
- grace period, because these files are known to have been attached.
-4. The page polls `GET /tenants/{id}/erasures` for progress. On success the
- request records counts and `person_erasure.completed`. On failure it is
- marked `failed` with the error, not retried, and the owner can queue it
- again. Every step is safe to repeat, so a request interrupted by a restart
- is finished on the next pass.
+ - deletes the stored files that only their messages carried, with the same
+ hosted-cutover guard as the hourly sweep but no grace period, because
+ these files are known to have been attached.
+4. The page polls `GET /tenants/{id}/erasures` for progress, and re-reads the
+ people list once the last request finishes. On success the request records
+ `person_erasure.completed` with its counts.
+5. A failure before an identity's rows are deleted marks the request `failed`
+ with the error. It is not retried, and since the identity is still there
+ the owner can queue it again. A failure after (the bridge could not be
+ told, the files could not be deleted) is logged as a warning and does not
+ fail the request: the identity is gone, so queueing again could not help.
+ The bridge forgets on its next restart, and the hourly sweep takes the
+ files a day later. A request whose process died is resumed by the next
+ pass once its progress is ten minutes old.
**The client row goes, it is not renamed.** Its transport id is built from the
person's username, so keeping it would keep the name. With every message they
@@ -167,23 +183,28 @@ sent deleted, nothing would show a placeholder anyway.
- **Quotes and mentions in other people's messages.** Erasing them would
rewrite other people's records. The usual reading of Art. 17 does not
- require it.
+ require it. This includes mentions as data: another message's `m.mentions`
+ holds the person's transport id, which embeds their username.
- **Their Switch account and membership.** Removing a member is its own
action, and deleting an account is deployment-level because one account may
belong to several workspaces.
- **Copies on chat platforms.** The platform's copy is the customer's to
erase in that platform. The dialog says so.
-- **Agents' copies.** An agent may hold the person's messages in its own
- transcript or memory, on a machine Switch does not control.
+- **Agents' copies.** An agent may hold the person's messages, or their
+ transport id, in its own transcript or memory, on a machine Switch does not
+ control.
- **Session activity lines** may quote what the person wrote. They are pruned
after 7 days.
- **The bridge's in-memory mention map** keeps the platform's
username-to-id pair until the bridge restarts. The person is still on that
platform, so the pair is the platform's data, not Switch's record of them.
+- **Channel room names.** Only direct rooms are named after a person. A
+ channel's name is the platform's.
-**A known window.** A message the person sends after their client is stopped
-but before its rows are deleted fails to post (an error is logged). The
-window lasts one transaction.
+**People from a disconnected chat app.** Disconnecting an app deletes its
+identity and client rows but keeps their messages in the detached rooms, so
+those people are not in the People list and cannot be erased this way. See
+"Not covered yet".
### Data export (not built)
@@ -191,7 +212,7 @@ An owner or admin export of everything held about one person in the workspace,
as machine-readable JSON: account fields, platform identities, and messages
they sent, with attachments. The same identity resolution as erasure applies.
This is the natural next step, since it needs the same identity lookup as
-erasure (`ErasureStore.list_people`).
+erasure (`ErasureStore.people_with_ids`).
### Deleting a workspace (not built)
@@ -228,3 +249,6 @@ personal-data processing.
- Expired controller enrolment codes. Each is tied to an `api_keys` row that
needs to go with it.
- `tasks`, `hosted_operations` and agent-controller operation history.
+- Erasing someone seen only on a chat app that has since been disconnected.
+ Their messages carry their transport id and display name, but no identity
+ row is left to find them by.
diff --git a/gateway/src/pages/workspace/EraseDialog.test.tsx b/gateway/src/pages/workspace/EraseDialog.test.tsx
index 268b11f25..1ff8776e2 100644
--- a/gateway/src/pages/workspace/EraseDialog.test.tsx
+++ b/gateway/src/pages/workspace/EraseDialog.test.tsx
@@ -53,7 +53,7 @@ describe("EraseDialog", () => {
vi.unstubAllGlobals();
});
- it("erases only once the name is typed, with the related identities included", async () => {
+ it("erases only once the name is typed, and only the person unless more are ticked", async () => {
const fetchMock = vi.fn(
async () => new Response(JSON.stringify({ id: "e1", state: "queued" }), { status: 202 }),
);
@@ -69,7 +69,7 @@ describe("EraseDialog", () => {
/>,
);
- expect(screen.getByText(/15 messages they sent/)).toBeTruthy();
+ expect(screen.getByText(/10 messages they sent/)).toBeTruthy();
const erase = screen.getByRole("button", { name: "Erase" });
expect(erase.hasAttribute("disabled")).toBe(true);
fireEvent.change(screen.getByLabelText("Type ana to confirm"), { target: { value: "Ana" } });
@@ -79,29 +79,23 @@ describe("EraseDialog", () => {
await waitFor(() => expect(onQueued).toHaveBeenCalled());
const [, init] = fetchMock.mock.calls[0] as unknown as [string, RequestInit];
- expect(JSON.parse(String(init.body))).toEqual({ external_user_ids: ["a", "b"] });
+ expect(JSON.parse(String(init.body))).toEqual({ external_user_ids: ["a"] });
});
- it("leaves out a related identity that is unticked", async () => {
+ it("keeps what was ticked and typed when the people list refreshes", async () => {
const fetchMock = vi.fn(async () => new Response(JSON.stringify({}), { status: 202 }));
vi.stubGlobal("fetch", fetchMock);
- render(
- <EraseDialog
- tenantId="t1"
- person={ana}
- people={[ana, anaElsewhere]}
- onClose={() => {}}
- onQueued={() => {}}
- />,
- );
+ const props = { tenantId: "t1", person: ana, onClose: () => {}, onQueued: () => {} };
+ const { rerender } = render(<EraseDialog {...props} people={[ana, anaElsewhere]} />);
fireEvent.click(screen.getByLabelText(/ana.b on Bridge b/));
- expect(screen.getByText(/10 messages they sent/)).toBeTruthy();
fireEvent.change(screen.getByLabelText("Type ana to confirm"), { target: { value: "ana" } });
- fireEvent.click(screen.getByRole("button", { name: "Erase" }));
+ rerender(<EraseDialog {...props} people={[{ ...ana }, { ...anaElsewhere }]} />);
+ expect(screen.getByText(/15 messages they sent/)).toBeTruthy();
+ fireEvent.click(screen.getByRole("button", { name: "Erase" }));
await waitFor(() => expect(fetchMock).toHaveBeenCalled());
const [, init] = fetchMock.mock.calls[0] as unknown as [string, RequestInit];
- expect(JSON.parse(String(init.body))).toEqual({ external_user_ids: ["a"] });
+ expect(JSON.parse(String(init.body))).toEqual({ external_user_ids: ["a", "b"] });
});
});
diff --git a/gateway/src/pages/workspace/EraseDialog.tsx b/gateway/src/pages/workspace/EraseDialog.tsx
index a44ab4997..44c0b72e2 100644
--- a/gateway/src/pages/workspace/EraseDialog.tsx
+++ b/gateway/src/pages/workspace/EraseDialog.tsx
@@ -15,8 +15,10 @@ import {
import { useEffect, useMemo, useState } from "react";
import { type Person, eraseIdentities } from "../../data/api";
-/** The identities that are probably the same person as `person`: every one
- * claimed by a member who also claims `person`. */
+/** Identities that may be the same person as `person`: every one claimed by
+ * a member who also claims `person`. Only a hint, offered unticked: claims are
+ * self-asserted and several members may claim one account, so a shared claimant
+ * does not prove a shared person. */
export function relatedIdentities(person: Person, people: Person[]): Person[] {
const claimants = new Set(person.claimed_by.map((c) => c.user_id));
if (claimants.size === 0) return [];
@@ -49,11 +51,15 @@ export default function EraseDialog({ tenantId, person, people, onClose, onQueue
const [submitting, setSubmitting] = useState(false);
const [error, setError] = useState<string | null>(null);
+ // Reset only when a different person is chosen. The list behind `people` is
+ // refreshed while other erasures run, and resetting on every refresh would
+ // undo what the owner ticked or typed just before they confirm.
+ const personId = person?.external_user_id;
useEffect(() => {
- setIncluded(new Set(related.map((p) => p.external_user_id)));
+ setIncluded(new Set());
setTyped("");
setError(null);
- }, [person, related]);
+ }, [personId]);
if (!person) return null;
@@ -104,7 +110,8 @@ export default function EraseDialog({ tenantId, person, people, onClose, onQueue
{related.length > 0 && (
<Stack spacing={0.5}>
<Typography variant="subtitle2">
- Also claimed by {person.claimed_by.map((c) => c.name).join(", ")}:
+ Possibly the same person, claimed by{" "}
+ {person.claimed_by.map((c) => c.name).join(", ")}. Tick any to erase too:
</Typography>
{related.map((other) => (
<FormControlLabel
diff --git a/gateway/src/pages/workspace/PeopleSection.tsx b/gateway/src/pages/workspace/PeopleSection.tsx
index 10f0e6274..1709b7676 100644
--- a/gateway/src/pages/workspace/PeopleSection.tsx
+++ b/gateway/src/pages/workspace/PeopleSection.tsx
@@ -8,7 +8,7 @@ import {
Typography,
} from "@mui/material";
import type { GridColDef } from "@mui/x-data-grid";
-import { useCallback, useEffect, useMemo, useState } from "react";
+import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import DataTable from "../../components/DataTable";
import { type Erasure, type Person, fetchErasures, fetchPeople } from "../../data/api";
import { formatDate, titleCase } from "../../theme/hootFormat";
@@ -54,14 +54,18 @@ export default function PeopleSection({ tenantId }: { tenantId: string }) {
);
const { refetch: refetchErasures } = erasures;
const { refetch: refetchPeople } = people;
+ // Only the requests are polled; the people list is re-read once, after the
+ // last running request has finished, so the erased are gone from it.
useEffect(() => {
if (!active) return;
- const timer = window.setInterval(() => {
- void refetchErasures();
- void refetchPeople();
- }, POLL_MS);
+ const timer = window.setInterval(() => void refetchErasures(), POLL_MS);
return () => window.clearInterval(timer);
- }, [active, refetchErasures, refetchPeople]);
+ }, [active, refetchErasures]);
+ const wasActive = useRef(false);
+ useEffect(() => {
+ if (wasActive.current && !active) void refetchPeople();
+ wasActive.current = active;
+ }, [active, refetchPeople]);
const rows = useMemo<PersonRow[]>(() => {
const needle = search.trim().toLowerCase();
@@ -110,7 +114,7 @@ export default function PeopleSection({ tenantId }: { tenantId: string }) {
);
const queued = async () => {
- await Promise.all([refetchErasures(), refetchPeople()]);
+ await refetchErasures();
};
return (