Skip to content

The agent that grows with you

fix(desktop): let a deleted bot section's shield expire when its member clear finishes

teknium162ac203abe63
diff --git a/apps/desktop/src/plugins/hermes-bots/user-sections.test.ts b/apps/desktop/src/plugins/hermes-bots/user-sections.test.ts
index 28b0fa6d979..0664d303d31 100644
--- a/apps/desktop/src/plugins/hermes-bots/user-sections.test.ts
+++ b/apps/desktop/src/plugins/hermes-bots/user-sections.test.ts
@@ -50,6 +50,7 @@ import {
loadBotSections,
moveBotsToSection,
renameBotSection,
+ resetBotSectionDeletes,
UNASSIGNED_SECTION_KEY
} from './user-sections'
@@ -77,6 +78,7 @@ beforeEach(() => {
storage.clear()
$botMeta.set({})
$botSections.set([])
+ resetBotSectionDeletes()
saveBotMeta.mockClear()
})
@@ -175,6 +177,39 @@ describe('user sections', () => {
expect($botSections.get()).toEqual([])
})
+ it('the shield on a deleted section lasts exactly until its latest delete finishes clearing members', async () => {
+ const section = createBotSection('Clients', [bot('nanox')])!
+ await vi.waitFor(() => expect($botMeta.get().nanox?.sectionId).toBe(section.id))
+ const roster = [bot('nanox')]
+
+ // Another desktop that still has "Clients" files scout into it.
+ const adoptWithScout = () =>
+ adoptBotSectionsFromMeta([bot('nanox'), bot('scout')], {
+ ...$botMeta.get(),
+ scout: { sectionId: section.id, sectionName: 'Clients' }
+ })
+
+ const flush = () => new Promise(resolve => setTimeout(resolve, 0))
+
+ // Delete, Undo, delete again: the first delete's clear finishing must not
+ // lift the shield while the second delete is still clearing.
+ const releaseFirst = holdNextSave()
+ deleteBotSection(section.id, roster).undo()
+ const releaseSecond = holdNextSave()
+ deleteBotSection(section.id, roster)
+ releaseFirst()
+ await flush()
+ adoptWithScout()
+ expect($botSections.get()).toEqual([])
+
+ // Once the clear is done, a member carrying the id was filed there again
+ // elsewhere, and the section is adopted like any other desktop's.
+ releaseSecond()
+ await flush()
+ adoptWithScout()
+ expect($botSections.get()).toEqual([{ id: section.id, name: 'Clients' }])
+ })
+
it('a second desktop rebuilds sections it never created from the id + name on each member', () => {
// This machine has no section records, only the members' synced ui_meta.
const meta = {
diff --git a/apps/desktop/src/plugins/hermes-bots/user-sections.ts b/apps/desktop/src/plugins/hermes-bots/user-sections.ts
index 662699ff542..c6f5aab2536 100644
--- a/apps/desktop/src/plugins/hermes-bots/user-sections.ts
+++ b/apps/desktop/src/plugins/hermes-bots/user-sections.ts
@@ -45,13 +45,21 @@ export interface BotSection {
/** `[{ id, name }]`, in display order. */
export const $botSections = atom<BotSection[]>([])
-/** Sections deleted on this desktop this session. A delete clears its members
+/** Sections whose delete is still clearing its members. A delete clears them
* one profile write at a time, and a slow write (a remote gateway, a pooled
* backend still starting) leaves the rest carrying the id AND the name for
* seconds — long enough for adoptBotSectionsFromMeta to rebuild the section
* the user just deleted, under whatever name a not-yet-cleared member still
- * carries. Undo takes the id back out. */
-const deletedSectionIds = new Set<string>()
+ * carries. The entry expires when that clear finishes (after it, a member
+ * carrying the id was filed there again — by another desktop — and adopting
+ * it is right); Undo takes it out early. The token keeps an earlier delete's
+ * clear from expiring a later delete of the same id. */
+const pendingSectionDeletes = new Map<string, symbol>()
+
+/** Test seam. */
+export function resetBotSectionDeletes(): void {
+ pendingSectionDeletes.clear()
+}
/** Roster key of the bot in flight during a drag. Session-only, and cleared
* on dragend even when the drop lands outside any target — a stuck
@@ -172,7 +180,7 @@ export function adoptBotSectionsFromMeta(roster: RosterRow[], metaByName: Record
const renamed = local.map(s => ({ ...s, name: agreed(s.id) ?? s.name }))
const adopted = [...names.keys()]
- .filter(id => !known.has(id) && !deletedSectionIds.has(id))
+ .filter(id => !known.has(id) && !pendingSectionDeletes.has(id))
.map(id => ({ id, name: [...names.get(id)!][0]! }))
const next = [...renamed, ...adopted]
@@ -224,9 +232,15 @@ export function deleteBotSection(id: string, roster: RosterRow[] = []): { member
const section = list[index]
const members = (roster || []).filter(bot => botSectionId(bot, $botMeta.get()) === id)
- deletedSectionIds.add(id)
+ const token = Symbol(id)
+
+ pendingSectionDeletes.set(id, token)
persistBotSections(list.filter(s => s.id !== id))
- void moveBotsToSection(members, null)
+ void moveBotsToSection(members, null).finally(() => {
+ if (pendingSectionDeletes.get(id) === token) {
+ pendingSectionDeletes.delete(id)
+ }
+ })
return {
members,
@@ -235,7 +249,7 @@ export function deleteBotSection(id: string, roster: RosterRow[] = []): { member
return
}
- deletedSectionIds.delete(id)
+ pendingSectionDeletes.delete(id)
const current = $botSections.get().filter(s => s.id !== id)
current.splice(Math.min(index, current.length), 0, section)