Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: erase Timeline events and Tribe touchpoints when a mail/calendar account is deleted or an iMessage chat is purged (#12984) (#13060)

[._.]/f7b377652f33
diff --git a/scripts/lib/dbTestFiles.js b/scripts/lib/dbTestFiles.js
index 7255c190d4..318cf683c3 100644
--- a/scripts/lib/dbTestFiles.js
+++ b/scripts/lib/dbTestFiles.js
@@ -55,6 +55,7 @@ export const DB_TEST_INCLUDE = [
'services/beeperTribe.db.test.js',
'services/tribe.db.test.js',
'services/tribePurge.db.test.js',
+ 'services/accountErasure.db.test.js',
'services/beeperSync.db.test.js',
'services/githubNotifications.db.test.js',
'services/creativeDirector/projectsDB.test.js',
diff --git a/server/routes/calendar.js b/server/routes/calendar.js
index db519e323a..078105a1f7 100644
--- a/server/routes/calendar.js
+++ b/server/routes/calendar.js
@@ -181,6 +181,11 @@ router.delete('/accounts/:id', asyncHandler(async (req, res) => {
// the cache. Retry cleanup even if an earlier attempt removed the account.
await calendarAccounts.deleteAccount(req.params.id);
await calendarSync.deleteCache(req.params.id);
+ // Derived copies (titles, attendees) live in the Timeline and Tribe (#12984).
+ // Lazy: these reach settings/DB modules that most route suites (and boot) never need.
+ const [humanActivity, { deleteAutoTouchpoints }] = await Promise.all([import('../services/humanActivity.js'), import('../services/tribe.js')]);
+ await humanActivity.deleteEvents({ accountId: req.params.id });
+ await deleteAutoTouchpoints({ accountId: req.params.id });
req.app.get('io')?.emit('calendar:changed', {});
res.status(204).send();
}));
diff --git a/server/routes/calendar.test.js b/server/routes/calendar.test.js
index ff976a68c8..391b17735a 100644
--- a/server/routes/calendar.test.js
+++ b/server/routes/calendar.test.js
@@ -3,6 +3,8 @@ import express from 'express';
import { request } from '../lib/testHelper.js';
import { ServerError, errorEvents } from '../lib/errorHandler.js';
+vi.mock('../services/humanActivity.js', () => ({ deleteEvents: vi.fn().mockResolvedValue({ deleted: 0 }) }));
+vi.mock('../services/tribe.js', () => ({ deleteAutoTouchpoints: vi.fn().mockResolvedValue({ touchpoints: 0, auditRows: 0 }) }));
vi.mock('../services/calendarAccounts.js', () => ({
listAccounts: vi.fn(),
createAccount: vi.fn(),
@@ -65,6 +67,8 @@ vi.mock('../services/userTimezone.js', () => ({
import calendarRoutes from './calendar.js';
import * as calendarAccounts from '../services/calendarAccounts.js';
import * as calendarSync from '../services/calendarSync.js';
+import * as humanActivity from '../services/humanActivity.js';
+import * as tribe from '../services/tribe.js';
import { getUserTimezone } from '../services/userTimezone.js';
import * as dailyReview from '../services/dailyReview.js';
import * as calendarGoogleSync from '../services/calendarGoogleSync.js';
@@ -118,6 +122,10 @@ describe('Calendar Routes — normalized error handling', () => {
}
expect(calendarSync.deleteCache).toHaveBeenCalledTimes(3);
expect(calendarSync.deleteCache).toHaveBeenLastCalledWith(ACCOUNT_ID);
+ // Derived Timeline/Tribe copies go only after the cache is gone, and again on retry (#12984).
+ expect(humanActivity.deleteEvents).toHaveBeenCalledTimes(2);
+ expect(humanActivity.deleteEvents).toHaveBeenLastCalledWith({ accountId: ACCOUNT_ID });
+ expect(tribe.deleteAutoTouchpoints).toHaveBeenLastCalledWith({ accountId: ACCOUNT_ID });
expect(io.emit.mock.calls.filter(([event]) => event === 'calendar:changed')).toHaveLength(2);
});
diff --git a/server/routes/messages.cleanup.test.js b/server/routes/messages.cleanup.test.js
index eae8128a1d..5906c01cfc 100644
--- a/server/routes/messages.cleanup.test.js
+++ b/server/routes/messages.cleanup.test.js
@@ -8,6 +8,8 @@ import { request } from '../lib/testHelper.js';
import { errorEvents } from '../lib/errorHandler.js';
const tempRoot = mkdtempSync(join(tmpdir(), 'messages-cleanup-test-'));
+vi.mock('../services/humanActivity.js', () => ({ deleteEvents: vi.fn().mockResolvedValue({ deleted: 0 }) }));
+vi.mock('../services/tribe.js', () => ({ deleteAutoTouchpoints: vi.fn().mockResolvedValue({ touchpoints: 0, auditRows: 0 }) }));
vi.mock('../lib/fileUtils.js', async (importOriginal) => {
const actual = await importOriginal();
return {
diff --git a/server/routes/messages.js b/server/routes/messages.js
index d8901eb7e1..d80ce8ff7a 100644
--- a/server/routes/messages.js
+++ b/server/routes/messages.js
@@ -131,6 +131,11 @@ router.delete('/accounts/:id', asyncHandler(async (req, res) => {
await messageAccounts.updateAccount(req.params.id, { enabled: false });
await messageSync.deleteCache(req.params.id);
await messageDrafts.deleteDraftsByAccountId(req.params.id);
+ // Derived copies (subjects, previews, participants) live in the Timeline and Tribe (#12984).
+ // Lazy: these reach settings/DB modules that most route suites (and boot) never need.
+ const [humanActivity, { deleteAutoTouchpoints }] = await Promise.all([import('../services/humanActivity.js'), import('../services/tribe.js')]);
+ await humanActivity.deleteEvents({ accountId: req.params.id });
+ await deleteAutoTouchpoints({ accountId: req.params.id });
await messageAccounts.deleteAccount(req.params.id);
req.app.get('io')?.emit('messages:changed', {});
res.status(204).send();
diff --git a/server/routes/messages.test.js b/server/routes/messages.test.js
index 99f8c23c0e..e6a1b2a14a 100644
--- a/server/routes/messages.test.js
+++ b/server/routes/messages.test.js
@@ -3,6 +3,8 @@ import express from 'express';
import { request } from '../lib/testHelper.js';
import messagesRoutes from './messages.js';
+vi.mock('../services/humanActivity.js', () => ({ deleteEvents: vi.fn().mockResolvedValue({ deleted: 0 }) }));
+vi.mock('../services/tribe.js', () => ({ deleteAutoTouchpoints: vi.fn().mockResolvedValue({ touchpoints: 0, auditRows: 0 }) }));
vi.mock('../services/messageEvaluator.js', () => ({ evaluateMessages: vi.fn(), generateReplyBody: vi.fn() }));
import { evaluateMessages, generateReplyBody } from '../services/messageEvaluator.js';
import { ServerError, errorEvents } from '../lib/errorHandler.js';
@@ -70,6 +72,8 @@ vi.mock('../services/messageTokenExtractor.js', () => ({
import * as messageAccounts from '../services/messageAccounts.js';
import * as messageSync from '../services/messageSync.js';
import * as messageDrafts from '../services/messageDrafts.js';
+import * as humanActivity from '../services/humanActivity.js';
+import * as tribe from '../services/tribe.js';
import * as messageSender from '../services/messageSender.js';
import * as messagePlaywrightSync from '../services/messagePlaywrightSync.js';
import * as messageTokenExtractor from '../services/messageTokenExtractor.js';
@@ -262,6 +266,8 @@ describe('Messages Routes', () => {
expect(messageAccounts.deleteAccount).toHaveBeenCalledWith(VALID_UUID);
expect(messageSync.deleteCache).toHaveBeenCalledWith(VALID_UUID);
expect(messageDrafts.deleteDraftsByAccountId).toHaveBeenCalledWith(VALID_UUID);
+ expect(humanActivity.deleteEvents).toHaveBeenCalledWith({ accountId: VALID_UUID });
+ expect(tribe.deleteAutoTouchpoints).toHaveBeenCalledWith({ accountId: VALID_UUID });
});
it('should return 400 for invalid UUID', async () => {
diff --git a/server/services/accountErasure.db.test.js b/server/services/accountErasure.db.test.js
new file mode 100644
index 0000000000..b7bb705b02
--- /dev/null
+++ b/server/services/accountErasure.db.test.js
@@ -0,0 +1,125 @@
+/**
+ * Postgres-backed proof that deleting a mail/calendar account or purging an
+ * iMessage chat erases the subjects, previews and attendees sync copied into
+ * the Timeline and Tribe, including the `record_audit` snapshots (#12984).
+ *
+ * `*.db.test.js` → runs ONLY via `npm run test:db` against `portos_test`.
+ * Fixtures use placeholder data and a per-run nonce; assertions are scoped to
+ * this run's own ids.
+ */
+import { afterAll, describe, expect, it } from 'vitest';
+import { checkHealth, close, ensureSchema, query } from '../lib/db.js';
+import { requireDbOrSkip } from '../lib/dbTestGate.js';
+import * as humanActivity from './humanActivity.js';
+import * as tribe from './tribe.js';
+import { purgeConversation, encodeChatKey } from './imessageManage.js';
+
+let dbReady = false;
+let skipReason = '';
+{
+ const health = await checkHealth().catch((e) => ({ connected: false, error: e?.message }));
+ if (!health.connected) {
+ skipReason = `Postgres not reachable (${health.error || 'no connection'})`;
+ } else {
+ await ensureSchema().catch(() => {});
+ dbReady = true;
+ }
+}
+const runDb = requireDbOrSkip('services/accountErasure.db.test', dbReady, skipReason);
+
+const nonce = `erase${Date.now()}`;
+const MAIL_A = `mail-a_${nonce}`; // underscore: a LIKE metacharacter that must not widen the match
+const MAIL_B = `mailXa_${nonce}`;
+const CAL_A = `cal-a-${nonce}`;
+const CAL_B = `cal-b-${nonce}`;
+const CHAT_A = `chat-a;${nonce}`;
+const CHAT_B = `chat-b;${nonce}`;
+const SOURCES = [`mail-${nonce}`, `calendar-${nonce}`];
+const IMESSAGE = 'imessage'; // purgeConversation is scoped to the real source; chat guids carry the nonce
+const personIds = [];
+
+afterAll(async () => {
+ if (!dbReady) return;
+ await query('DELETE FROM human_activity_events WHERE source = ANY($1::text[]) OR metadata->>\'chatGuid\' = ANY($2::text[])', [SOURCES, [CHAT_A, CHAT_B]]).catch(() => {});
+ if (personIds.length) {
+ await query('DELETE FROM tribe_people WHERE id = ANY($1::uuid[])', [personIds]).catch(() => {});
+ await query(
+ `DELETE FROM record_audit WHERE table_name = 'tribe_touchpoints' AND row_snapshot->>'person_id' = ANY($1::text[])`,
+ [personIds],
+ ).catch(() => {});
+ }
+ await close();
+});
+
+const count = async (sql, params) => Number((await query(sql, params)).rows[0].n);
+const touchpointCount = (where, params) => count(`SELECT COUNT(*) AS n FROM tribe_touchpoints WHERE person_id = $1 AND ${where}`, [personIds[0], ...params]);
+
+async function seed() {
+ const person = await tribe.createPerson({ name: `Example Person ${nonce}`, emails: [`p-${nonce}@example.com`] });
+ personIds.push(person.id);
+ const tp = (data) => tribe.autoCreateTouchpoint(person.id, { happenedAt: '2026-07-04T15:00:00Z', channel: 'x', summary: 'Example subject line', ...data });
+ await tp({ source: 'message', dedupeKey: `msg:${MAIL_A}:t1:2026-07-04` });
+ await tp({ source: 'message', dedupeKey: `msg:${MAIL_B}:t1:2026-07-04` });
+ await tp({ source: 'calendar', calendarAccountId: CAL_A, calendarEventId: 'e1', dedupeKey: `cal:${CAL_A}:e1` });
+ await tp({ source: 'calendar', calendarAccountId: CAL_B, calendarEventId: 'e1', dedupeKey: `cal:${CAL_B}:e1` });
+ await tp({ source: 'imessage', dedupeKey: `imsg:${CHAT_A}:2026-07-04` });
+ await tp({ source: 'imessage', dedupeKey: `imsg:${CHAT_B}:2026-07-04` });
+ const ev = (source, accountId, key, metadata = {}) => ({
+ source, accountId, kind: 'message.received', happenedAt: '2026-07-04T15:00:00Z', dedupeKey: key, title: 'Example subject', metadata,
+ });
+ await humanActivity.recordEvents([
+ ev(SOURCES[0], MAIL_A, 'a1'), ev(SOURCES[0], MAIL_B, 'b1'),
+ ev(SOURCES[1], CAL_A, 'a2'), ev(SOURCES[1], CAL_B, 'b2'),
+ ev(IMESSAGE, null, `i1-${nonce}`, { chatGuid: CHAT_A }), ev(IMESSAGE, null, `i2-${nonce}`, { chatGuid: CHAT_B }),
+ ]);
+}
+
+const auditRows = () => count(
+ `SELECT COUNT(*) AS n FROM record_audit WHERE table_name = 'tribe_touchpoints' AND row_snapshot->>'person_id' = $1
+ AND (row_snapshot->>'dedupe_key' LIKE ANY($2::text[]))`,
+ [personIds[0], [`msg:${MAIL_A}:%`, `cal:${CAL_A}:%`, `imsg:${CHAT_A}:%`]],
+);
+const events = (accountId) => count('SELECT COUNT(*) AS n FROM human_activity_events WHERE account_id = $1', [accountId]);
+
+describe.skipIf(!runDb)('derived-data erasure (#12984)', () => {
+ it('mail account delete removes its events, message touchpoints and audit snapshots, sparing other accounts', async () => {
+ await seed();
+ await humanActivity.deleteEvents({ accountId: MAIL_A });
+ const result = await tribe.deleteAutoTouchpoints({ accountId: MAIL_A });
+ expect(result.touchpoints).toBe(1);
+ expect(await events(MAIL_A)).toBe(0);
+ expect(await events(MAIL_B)).toBe(1);
+ expect(await touchpointCount(`dedupe_key LIKE 'msg:%'`, [])).toBe(1); // MAIL_B survives the `_` wildcard
+ expect(await touchpointCount(`dedupe_key = $2`, [`msg:${MAIL_B}:t1:2026-07-04`])).toBe(1);
+ expect(await count(
+ `SELECT COUNT(*) AS n FROM record_audit WHERE table_name = 'tribe_touchpoints' AND row_snapshot->>'dedupe_key' LIKE $1`,
+ [`msg:${MAIL_A}:%`],
+ )).toBe(0);
+ });
+
+ it('calendar account delete removes its events and touchpoints, sparing other accounts', async () => {
+ await humanActivity.deleteEvents({ accountId: CAL_A });
+ await tribe.deleteAutoTouchpoints({ accountId: CAL_A });
+ expect(await events(CAL_A)).toBe(0);
+ expect(await events(CAL_B)).toBe(1);
+ expect(await touchpointCount(`calendar_account_id = $2`, [CAL_A])).toBe(0);
+ expect(await touchpointCount(`calendar_account_id = $2`, [CAL_B])).toBe(1);
+ });
+
+ it('iMessage purge removes that chat\'s touchpoints and snapshots only', async () => {
+ await purgeConversation(encodeChatKey(CHAT_A));
+ expect(await touchpointCount(`dedupe_key = $2`, [`imsg:${CHAT_A}:2026-07-04`])).toBe(0);
+ expect(await touchpointCount(`dedupe_key = $2`, [`imsg:${CHAT_B}:2026-07-04`])).toBe(1);
+ const chatEvents = (guid) => count(`SELECT COUNT(*) AS n FROM human_activity_events WHERE metadata->>'chatGuid' = $1`, [guid]);
+ expect(await chatEvents(CHAT_A)).toBe(0);
+ expect(await chatEvents(CHAT_B)).toBe(1);
+ expect(await auditRows()).toBe(0);
+ });
+
+ it('empty selectors delete nothing', async () => {
+ expect(await humanActivity.deleteEvents({})).toEqual({ deleted: 0 });
+ expect(await humanActivity.deleteEvents({ source: SOURCES[0] })).toEqual({ deleted: 0 });
+ expect(await tribe.deleteAutoTouchpoints({})).toEqual({ touchpoints: 0, auditRows: 0 });
+ expect(await events(MAIL_B)).toBe(1);
+ });
+});
diff --git a/server/services/humanActivity.js b/server/services/humanActivity.js
index ef48735cf1..838041c343 100644
--- a/server/services/humanActivity.js
+++ b/server/services/humanActivity.js
@@ -485,17 +485,20 @@ export async function listEvents({ from, to, source, accountId, kind, personId,
*
* Safety: refuses unbounded wipes. Accepts either:
* - `ids` (optionally scoped with `source`), or
- * - `source` + (`chatGuid` and/or `handle`)
+ * - `source` + (`chatGuid` and/or `handle`), or
+ * - `accountId` (alone or with `source`) — erases everything an account's sync
+ * copied in when the account is deleted (#12984)
*
* Used by PortOS-side managers (iMessage #2413, later Signal/etc.) — never
* mutates the external source (chat.db, Gmail, …).
*/
-export async function deleteEvents({ ids, source, chatGuid, handle } = {}) {
+export async function deleteEvents({ ids, source, chatGuid, handle, accountId } = {}) {
await ensureReady();
const hasIds = Array.isArray(ids) && ids.length > 0;
const hasChat = chatGuid != null && String(chatGuid).length > 0;
const hasHandle = handle != null && String(handle).length > 0;
- if (!hasIds && !(source && (hasChat || hasHandle))) return { deleted: 0 };
+ const hasAccount = accountId != null && String(accountId).length > 0;
+ if (!hasIds && !hasAccount && !(source && (hasChat || hasHandle))) return { deleted: 0 };
const clauses = [];
const params = [];
@@ -507,6 +510,10 @@ export async function deleteEvents({ ids, source, chatGuid, handle } = {}) {
params.push(String(source));
clauses.push(`source = $${params.length}`);
}
+ if (hasAccount) {
+ params.push(String(accountId));
+ clauses.push(`account_id = $${params.length}`);
+ }
if (hasChat) {
params.push(String(chatGuid));
clauses.push(`metadata->>'chatGuid' = $${params.length}`);
diff --git a/server/services/imessageManage.js b/server/services/imessageManage.js
index e480601ef7..fe3e36aab0 100644
--- a/server/services/imessageManage.js
+++ b/server/services/imessageManage.js
@@ -326,6 +326,9 @@ export async function purgeConversation(chatKey) {
const chatGuid = decodeChatKey(chatKey);
if (chatGuid == null) return { deleted: 0, chatGuid: null };
const result = await humanActivity.deleteEvents({ source: SOURCE, chatGuid });
+ // Touchpoint summaries carry the chat name; dedupe keys are imsg:<chatGuid>:<day>.
+ const { deleteAutoTouchpoints } = await import('./tribe.js');
+ await deleteAutoTouchpoints({ dedupePrefix: `imsg:${chatGuid}:` });
return { ...result, chatGuid, chatKey: encodeChatKey(chatGuid) };
}
diff --git a/server/services/tribe.js b/server/services/tribe.js
index e9e0767fcc..0240651f61 100644
--- a/server/services/tribe.js
+++ b/server/services/tribe.js
@@ -443,6 +443,55 @@ export async function listTouchpoints(personId, limit = 50) {
return result.rows.map(rowToTouchpoint);
}
+const escapeLike = (value) => String(value).replace(/[\\%_]/g, (ch) => `\\${ch}`);
+
+/**
+ * Erase the touchpoints a synced source wrote, plus their `record_audit`
+ * snapshots (which would otherwise keep the subject/title for the 30-day Tribe
+ * sweep), in one transaction (#12984). Selectors:
+ * - `accountId`: message touchpoints (`msg:<id>:%`) and every calendar
+ * touchpoint of that account, including hand-logged ones — the account and
+ * its event details are going away together.
+ * - `dedupePrefix`: any other source's dedupe-key family (e.g. `imsg:<chat>:`).
+ * Returns counts only. An empty selector deletes nothing.
+ */
+export async function deleteAutoTouchpoints({ accountId, dedupePrefix } = {}) {
+ const clauses = [];
+ const params = [];
+ if (accountId != null && String(accountId).length > 0) {
+ params.push(`msg:${escapeLike(accountId)}:%`, String(accountId));
+ clauses.push(`(source = 'message' AND dedupe_key LIKE $${params.length - 1})`);
+ clauses.push(`(source = 'calendar' AND calendar_account_id = $${params.length})`);
+ }
+ if (dedupePrefix != null && String(dedupePrefix).length > 0) {
+ params.push(`${escapeLike(dedupePrefix)}%`);
+ clauses.push(`dedupe_key LIKE $${params.length}`);
+ }
+ if (clauses.length === 0) return { touchpoints: 0, auditRows: 0 };
+ await ensureReady();
+ const result = await withTransaction(async (client) => {
+ const removed = await client.query(
+ `DELETE FROM tribe_touchpoints WHERE ${clauses.join(' OR ')} RETURNING id`,
+ params,
+ );
+ const ids = removed.rows.map((row) => String(row.id));
+ let auditRows = 0;
+ if (ids.length > 0) {
+ const snapshots = await client.query(
+ `DELETE FROM record_audit
+ WHERE table_name = 'tribe_touchpoints' AND record_id = ANY($1::text[])`,
+ [ids],
+ );
+ auditRows = snapshots.rowCount;
+ }
+ return { touchpoints: ids.length, auditRows };
+ });
+ if (result.touchpoints > 0) {
+ console.log(`🧹 Erased ${result.touchpoints} touchpoint(s) and ${result.auditRows} audit snapshot(s)`);
+ }
+ return result;
+}
+
export async function createTouchpoint(personId, data = {}) {
await ensureReady();
const person = await getPerson(personId);