Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

fix: preserve deleted conversation history during Swarm migration (#1778)

0xken155b75ce3bb3bc
diff --git a/backend/integration/superTabMigration.test.ts b/backend/integration/superTabMigration.test.ts
index 0c61616e7..a9f79aa73 100644
--- a/backend/integration/superTabMigration.test.ts
+++ b/backend/integration/superTabMigration.test.ts
@@ -120,14 +120,18 @@ describe('migrating one Swarm to parts (design 6.1.2)', () => {
expect(await partCount(tabId)).toBe(0)
})
- it('refuses to migrate a Swarm with a message whose conversation it does not have', async () => {
+ it('preserves messages whose conversations were already deleted before migration', async () => {
const { tabId } = await legacySwarm()
await db.superTabMessage.create({ data: { id: `${tabId}-orphan`, tabId, conversationId: 'gone', sequence: 7, author: {}, text: 'lost',
recipientIds: [], mentions: [] } })
+ const before = await db.superTabMessage.findMany({ where: { tabId }, select: { id: true, conversationId: true, sequence: true },
+ orderBy: [{ conversationId: 'asc' }, { sequence: 'asc' }, { id: 'asc' }] })
const report = await migrateSwarm(db, tabId, { now: () => NOW })
- expect(report).toMatchObject({ ok: false })
- expect(report.error).toContain('1 message in a conversation it does not have: gone')
- expect(await partCount(tabId)).toBe(0)
+ expect(report).toMatchObject({ tabId, ok: true, messages: before.length })
+ expect(await db.superTab.findUnique({ where: { id: tabId }, select: { format: true } })).toEqual({ format: 2 })
+ expect((await db.$transaction(tx => parts.loadAll(tx, tabId))).groups.map(group => group.key)).not.toContain('gone')
+ expect(await db.superTabMessage.findMany({ where: { tabId }, select: { id: true, conversationId: true, sequence: true },
+ orderBy: [{ conversationId: 'asc' }, { sequence: 'asc' }, { id: 'asc' }] })).toEqual(before)
})
})
diff --git a/backend/integration/superTabRead.test.ts b/backend/integration/superTabRead.test.ts
index 453446bdc..087b2808c 100644
--- a/backend/integration/superTabRead.test.ts
+++ b/backend/integration/superTabRead.test.ts
@@ -143,12 +143,23 @@ describe('first load (design 4.3)', () => {
expect(await db.superTab.findUnique({ where: { id: tabId } })).toMatchObject({ format: 2 })
})
- it('answers "being upgraded" when a Swarm cannot be migrated yet, and every client retries that', async () => {
+ it('opens a legacy Swarm with deleted-conversation history without exposing that history', async () => {
const s = swarm(MIGRATED)
const tabId = `tab-${randomUUID()}`
await writeLegacySwarm(db, { state: { ...structuredClone(s.state), id: tabId }, revision: 3 })
await db.superTabMessage.create({ data: { id: `${tabId}-orphan`, tabId, conversationId: 'gone', sequence: 1, author: {}, text: 'x', recipientIds: [], mentions: [] } })
const { status, body } = await get(`/api/super-tabs/${tabId}/parts`)
+ expect(status).toBe(200)
+ expect(keys(body.data.groups)).not.toContain('gone')
+ expect((await get(`/api/super-tabs/${tabId}/conversations/gone/messages`)).status).toBe(404)
+ expect((await db.superTabMessage.findUnique({ where: { id: `${tabId}-orphan` } }))?.text).toBe('x')
+ })
+
+ it('answers "being upgraded" when a Swarm cannot be migrated yet, and every client retries that', async () => {
+ const s = swarm(MIGRATED)
+ const tabId = `tab-${randomUUID()}`
+ await writeLegacySwarm(db, { state: { ...structuredClone(s.state), id: tabId, version: 2 as 1 }, revision: 3 })
+ const { status, body } = await get(`/api/super-tabs/${tabId}/parts`)
expect(status).toBe(503)
expect(body.error!.code).toBe('SWARM_MIGRATING')
})
diff --git a/backend/src/superTabs/migration/migrate.ts b/backend/src/superTabs/migration/migrate.ts
index a516703eb..b53e9b390 100644
--- a/backend/src/superTabs/migration/migrate.ts
+++ b/backend/src/superTabs/migration/migrate.ts
@@ -57,8 +57,11 @@ async function migrateIn(tx: Prisma.TransactionClient, tabId: string, at: number
const differs = stateDifference(parts.toLegacyState(state), parts.toLegacyState(parts.reassemble(stored)))
if (differs) fail(`its parts would not reassemble into its state: ${differs}`)
const groups = new Set(stored.groups.map(g => g.key))
- const orphaned = [...before.conversationIds].filter(id => !groups.has(id))
- if (orphaned.length) fail(`${orphaned.length} message${orphaned.length === 1 ? '' : 's'} in a conversation it does not have: ${orphaned.join(', ')}`)
+ // Deleted conversations may have left message history behind in the old store. Keep those messages untouched and
+ // inaccessible, but do not allow the split to orphan a conversation that is still present after normalization.
+ const activeGroups = new Set(state.conversations.map(group => group.id))
+ const newlyOrphaned = [...before.conversationIds].filter(id => activeGroups.has(id) && !groups.has(id))
+ if (newlyOrphaned.length) fail(`${newlyOrphaned.length} existing conversation${newlyOrphaned.length === 1 ? '' : 's'} lost its message group: ${newlyOrphaned.join(', ')}`)
const touched = fingerprintDifference(before, await fingerprint(tx, tabId))
if (touched) fail(touched)
const fenced = await tx.superTab.updateMany({ where: { id: tabId, revision: row.revision, format: 1 },
diff --git a/docs/design/2026-10-26-swarm-state-parts.md b/docs/design/2026-10-26-swarm-state-parts.md
index f9f86e7fd..f919673ab 100644
--- a/docs/design/2026-10-26-swarm-state-parts.md
+++ b/docs/design/2026-10-26-swarm-state-parts.md
@@ -975,8 +975,9 @@ The same function serves commands (3.1), the CLI's `status` (5.3), verification
`closingSessions`, `machineCleanups`, `overlapAlerts`. Every other array keeps its order. The first differing path
is logged.
- **Messages:** before and after, `count` and `sha256` of the ordered list of `(conversationId, sequence, id)` for
- the tab match. Because messages are not written, this proves the migration did not touch them. It also checks
- that every message's `conversationId` has a group part, so no history is orphaned.
+ the tab match. Because messages are not written, this proves the migration did not touch them. A message in a
+ conversation still present after normalization must have a group part. Historical messages of conversations
+ already deleted in the old state remain unchanged and inaccessible; migration must not create a new orphan.
- **Counts unchanged:** deliveries, route decisions, attempts and operations for the tab.
- **Reports:** a per-Swarm line `{tabId, bytes, groups, dormant, parts, ms, ok}` and a total, written to the worker
log and to `artifacts/swarm-parts-migration-<date>.json` by the dry-run tool.
@@ -1198,4 +1199,4 @@ skill texts reference `throughRevision` only through the wake-up text, which cha
- Machines report every 15 s. With the 30 s overlap every host tick resent its Swarm in full.
- A host's rows carry only its own reading, which it sent. So the host form wakes on `SwarmMachine.hostChangedAt`,
which `parts/commit.ts` moves only when a machine part's settings or ledger change, never when only its reading
- does. Desktop and web feeds still get readings by `changedAt`.
\ No newline at end of file
+ does. Desktop and web feeds still get readings by `changedAt`.