Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(trash): side pre-snapshot workspace roots by live visibility (#21834)

Lobehub Coder4745e295a70c
diff --git a/packages/database/src/models/__tests__/trash.test.ts b/packages/database/src/models/__tests__/trash.test.ts
index 1d52d14acb7..34b42adf096 100644
--- a/packages/database/src/models/__tests__/trash.test.ts
+++ b/packages/database/src/models/__tests__/trash.test.ts
@@ -379,11 +379,52 @@ describe('TrashModel', () => {
(await wsModel.list({ visibility })).items.map((item) => item.resourceId);
expect(await sideIds('public')).toEqual([]);
expect(await sideIds('private')).toEqual(['tpc_flip']);
+ });
- // A row registered before the snapshot existed cannot prove it was shared.
- await serverDB.update(trashItems).set({ meta: null }).where(eq(trashItems.id, root.id));
- expect(await sideIds('public')).toEqual([]);
- expect(await sideIds('private')).toEqual(['tpc_flip']);
+ it('sides a root registered before the snapshot existed by its live visibility', async () => {
+ const wsModel = new TrashModel(serverDB, userId, workspaceId);
+ await serverDB.insert(agents).values([
+ { id: 'agt_legacy_private', userId, visibility: 'private', workspaceId },
+ { id: 'agt_legacy_public', userId, visibility: 'public', workspaceId },
+ ]);
+ await serverDB.insert(topics).values([
+ { agentId: 'agt_legacy_private', id: 'tpc_legacy_private', userId, workspaceId },
+ { agentId: 'agt_legacy_public', id: 'tpc_legacy_public', userId, workspaceId },
+ ]);
+ await serverDB.insert(messages).values({
+ content: 'shared',
+ id: 'msg_legacy_public',
+ role: 'user',
+ topicId: 'tpc_legacy_public',
+ userId,
+ });
+ const legacyRoots = [
+ ['agt_legacy_private', 'agent'],
+ ['agt_legacy_public', 'agent'],
+ ['tpc_legacy_private', 'topic'],
+ ['msg_legacy_public', 'message'],
+ ] as const;
+ for (const [resourceId, resourceType] of legacyRoots) {
+ await wsModel.register({
+ deletedAt: at('2026-08-01T00:00:00Z'),
+ root: { projectId: null, resourceId, resourceType },
+ });
+ }
+ // Rows trashed before #21537 carry no `private` key, or no meta at all.
+ await serverDB
+ .update(trashItems)
+ .set({ meta: sql`${trashItems.meta} - 'private'` })
+ .where(eq(trashItems.resourceType, 'agent'));
+ await serverDB
+ .update(trashItems)
+ .set({ meta: null })
+ .where(sql`${trashItems.resourceType} <> 'agent'`);
+
+ const sideIds = async (visibility: 'private' | 'public') =>
+ (await wsModel.list({ visibility })).items.map((item) => item.resourceId).sort();
+ expect(await sideIds('private')).toEqual(['agt_legacy_private', 'tpc_legacy_private']);
+ expect(await sideIds('public')).toEqual(['agt_legacy_public', 'msg_legacy_public']);
+ expect(await wsModel.countByType({ visibility: 'private' })).toEqual({ agent: 1, topic: 1 });
});
it('keeps a root whose source or project is gone off the public side', async () => {
diff --git a/packages/database/src/models/trash.ts b/packages/database/src/models/trash.ts
index 92c699d0e4a..e0eb08cbf81 100644
--- a/packages/database/src/models/trash.ts
+++ b/packages/database/src/models/trash.ts
@@ -142,10 +142,11 @@ const livePrivateWhere = (): SQL =>
* `registerMany`), so making a still-live parent public later does not expose
* it.
*
- * It fails closed: a root that cannot prove it was shared stays on its
- * deleter's private side — one registered before the snapshot existed (its
- * parents may have changed since), or one whose source row or recorded
- * project was removed outside the trash (a hard delete or an FK cascade).
+ * A root registered before the snapshot existed has no `meta.private`; it
+ * falls back to the live check, like every other workspace list. Otherwise it
+ * fails closed: a root whose source row or recorded project was removed
+ * outside the trash (a hard delete or an FK cascade) cannot prove it was
+ * shared, so it stays on its deleter's private side.
*/
const privateRootWhere = (): SQL =>
or(
@@ -156,7 +157,7 @@ const privateRootWhere = (): SQL =>
),
// COALESCE: a NULL here would turn the whole OR, and its NOT, into NULL.
sql`COALESCE((${trashItems.meta} ->> 'private') = 'true', false)`,
- sql`NOT COALESCE(jsonb_exists(${trashItems.meta}, 'private'), false)`,
+ and(sql`NOT COALESCE(jsonb_exists(${trashItems.meta}, 'private'), false)`, livePrivateWhere()),
) as SQL;
const toTrashItem = (row: TrashItemRow): TrashItem => ({