Retire removed accounts instead of deleting them

Removing an account had no path at all, so a dead session could only be
left running or hand-edited out of the database. The first cut deleted the
account and everything it owned, which threw away conversations and media
that are worth keeping.

Retire instead: keep the row and every row it produced, destroy the stored
cookies and Matrix token. Wiping the credentials is what makes sending and
syncing impossible rather than merely disabled — isEnabledAccount requires
a verified status, so no runtime ever starts, and no session survives for
anything to use. setActive, markLoginRequired and the background credential
refresh now skip retired accounts so none of them quietly revives one;
re-importing a session deliberately does.

Keeping the content visible needed more than keeping the rows. Conversations
and media are only served from running runtimes, so a retired account's
history would have disappeared from the dashboard regardless. Media already
reads the account content table for every listed account. Conversations now
have an archive path that reads the stored ActiveChat payloads directly.

Every write path refuses a retired conversation with a 409: send-reply,
both suggest endpoints, share-media, shareable-media and all read-state
writes. Lazy-loading detail is skipped too — the fallback chat service
belongs to a different account, so it would sync the wrong session and
write the wrong cache. Archived detail derives conversation state from the
transcript alone for the same reason.

Migration 32 rebuilds reddit_accounts to admit the new status, with foreign
keys off and a foreign_key_check before they are restored, as migration 11
does. Two migration tests rewound user_version by counting back from the
head, which silently breaks whenever a migration is appended; they now
target absolute versions.

Conversations never opened before removal keep only their summary. Message
history is lazy-loaded on first open and there is no session left to fetch
the rest with.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
Jamie BruntonandClaude Opus 5 committed 2026-08-12 08:58:22 +01:00
1 parent 3f60f7f665
commit a9cea7ed18
16 files changed
+433 -97

No files matched your search

+33 -10
View File
@@ -134,7 +134,7 @@ describe('multi-account database ownership', () => {
expect(db.pragma('foreign_key_check')).toEqual([]);
});
it('removes an account with everything it owns and leaves other accounts intact', () => {
it('retires an account while keeping every row it collected', () => {
addAccount('t2_alice', 'alice-bot', 100);
addAccount('t2_bob', 'bob-bot', 110);
@@ -174,28 +174,51 @@ describe('multi-account database ownership', () => {
});
}
expect(accounts.delete('t2_alice')).toBe(true);
expect(accounts.retire('t2_alice', 500)).toBe(true);
expect(accounts.get('t2_alice')).toBeNull();
expect(accounts.listPublic().map((account) => account.accountId)).toContain('t2_bob');
// The content the account collected is the point of retiring rather than
// deleting: every row it produced must survive untouched.
for (const table of [
'processed_posts', 'sent_messages', 'successful_messages', 'delivery_outbox',
'activity_log', 'conversation_states', 'reddit_conversations', 'reddit_account_sync_state',
]) {
expect({ table, ...db.prepare(`SELECT COUNT(*) count FROM ${table} WHERE account_id = ?`).get('t2_alice') as object })
.toEqual({ table, count: 0 });
expect({ table, ...db.prepare(`SELECT COUNT(*) count FROM ${table} WHERE account_id = ?`).get('t2_bob') as object })
.toEqual({ table, count: 1 });
}
expect(accounts.get('t2_alice')).toMatchObject({
status: 'removed', isActive: false, removedAt: 500,
});
// Credentials are destroyed, so no session survives to sync or send with.
expect(accounts.get('t2_alice')).toMatchObject({ cookiesJson: '[]', matrixCredentialsJson: '{}' });
expect(accounts.getActive().map((account) => account.accountId)).toEqual(['t2_bob']);
expect(db.pragma('foreign_key_check')).toEqual([]);
});
it('reports an unknown account as not removed and rejects a blank id', () => {
it('refuses to reactivate or refresh a retired account', () => {
addAccount('t2_alice', 'alice-bot', 100);
expect(accounts.retire('t2_alice', 500)).toBe(true);
expect(accounts.setActive('t2_alice', true, 600)).toBe(false);
expect(accounts.markLoginRequired('t2_alice', 600)).toBe(false);
expect(accounts.updateMatrixCredentials('t2_alice', JSON.stringify({ accessToken: 'new' }), 600)).toBe(false);
expect(accounts.get('t2_alice')).toMatchObject({ status: 'removed', matrixCredentialsJson: '{}' });
});
it('revives a retired account when a fresh session is imported for it', () => {
addAccount('t2_alice', 'alice-bot', 100);
accounts.retire('t2_alice', 500);
const revived = addAccount('t2_alice', 'alice-bot', 700);
expect(revived).toMatchObject({ status: 'verified', isActive: true, removedAt: null });
});
it('reports an unknown account as not retired and rejects a blank id', () => {
addAccount('t2_alice', 'alice-bot', 100);
expect(accounts.delete('t2_missing')).toBe(false);
expect(() => accounts.delete(' ')).toThrow(/accountId/);
expect(accounts.get('t2_alice')).not.toBeNull();
expect(accounts.retire('t2_missing')).toBe(false);
expect(() => accounts.retire(' ')).toThrow(/accountId/);
expect(accounts.get('t2_alice')).toMatchObject({ status: 'verified' });
});
it('scopes legacy account-produced table uniqueness by account', () => {