Skip to content

The agent that grows with you

fix(desktop): deleting a bot roster section no longer brings it back under its old name

teknium13b36436e4ceb
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 f851a481444..28b0fa6d979 100644
--- a/apps/desktop/src/plugins/hermes-bots/user-sections.test.ts
+++ b/apps/desktop/src/plugins/hermes-bots/user-sections.test.ts
@@ -56,6 +56,23 @@ import {
const bot = (name: string) => ({ name }) as RosterRow
const row = (name: string) => ({ bot: bot(name), kind: 'bot' as const })
+/** The next saveBotMeta applies locally at once (as the real one does) but
+ * its server half stays in flight until the returned release is called. */
+function holdNextSave(): () => void {
+ let release = () => {}
+ const gate = new Promise<void>(resolve => (release = resolve))
+ const apply = saveBotMeta.getMockImplementation()!
+
+ saveBotMeta.mockImplementationOnce(async (owner, patch) => {
+ const result = await apply(owner, patch)
+ await gate
+
+ return result
+ })
+
+ return () => release()
+}
+
beforeEach(() => {
storage.clear()
$botMeta.set({})
@@ -124,6 +141,40 @@ describe('user sections', () => {
await vi.waitFor(() => expect($botMeta.get().nanox?.sectionId).toBe(section.id))
})
+ it('a delete still clearing its members does not bring the section back from a member not cleared yet', async () => {
+ const section = createBotSection('Clients', [bot('nanox'), bot('scout')])!
+ await vi.waitFor(() => expect($botMeta.get().scout?.sectionId).toBe(section.id))
+ const roster = [bot('nanox'), bot('scout')]
+ const release = holdNextSave()
+
+ // nanox's clear is stuck on a slow profile write, so scout still carries
+ // the section's id + name while the roster re-derives.
+ deleteBotSection(section.id, roster)
+ adoptBotSectionsFromMeta(roster, $botMeta.get())
+ expect($botSections.get()).toEqual([])
+
+ release()
+ await vi.waitFor(() => expect($botMeta.get().scout?.sectionId).toBeNull())
+ })
+
+ it('a section deleted while a rename is still re-stamping its members does not get them refiled', async () => {
+ const section = createBotSection('Clients', [bot('nanox'), bot('scout')])!
+ await vi.waitFor(() => expect($botMeta.get().scout?.sectionId).toBe(section.id))
+ const roster = [bot('nanox'), bot('scout')]
+ const release = holdNextSave()
+
+ renameBotSection(section.id, 'Customers', roster)
+ deleteBotSection(section.id, roster)
+ release()
+
+ await vi.waitFor(() => expect($botMeta.get().scout?.sectionId).toBeNull())
+ await vi.waitFor(() => expect($botMeta.get().nanox?.sectionId).toBeNull())
+ // Let the rename's loop finish walking: it must not refile anyone.
+ await new Promise(resolve => setTimeout(resolve, 0))
+ expect(Object.values($botMeta.get()).map(meta => meta.sectionId)).toEqual([null, null])
+ expect($botSections.get()).toEqual([])
+ })
+
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 28f119163c1..662699ff542 100644
--- a/apps/desktop/src/plugins/hermes-bots/user-sections.ts
+++ b/apps/desktop/src/plugins/hermes-bots/user-sections.ts
@@ -45,6 +45,14 @@ 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
+ * 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>()
+
/** 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
* "dragging" state outlives the gesture and reads as a broken pane. */
@@ -162,7 +170,11 @@ export function adoptBotSectionsFromMeta(roster: RosterRow[], metaByName: Record
const local = $botSections.get()
const known = new Set(local.map(s => s.id))
const renamed = local.map(s => ({ ...s, name: agreed(s.id) ?? s.name }))
- const adopted = [...names.keys()].filter(id => !known.has(id)).map(id => ({ id, name: [...names.get(id)!][0]! }))
+
+ const adopted = [...names.keys()]
+ .filter(id => !known.has(id) && !deletedSectionIds.has(id))
+ .map(id => ({ id, name: [...names.get(id)!][0]! }))
+
const next = [...renamed, ...adopted]
if (next.some((s, i) => s.id !== local[i]?.id || s.name !== local[i]?.name)) {
@@ -212,6 +224,7 @@ 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)
persistBotSections(list.filter(s => s.id !== id))
void moveBotsToSection(members, null)
@@ -222,6 +235,7 @@ export function deleteBotSection(id: string, roster: RosterRow[] = []): { member
return
}
+ deletedSectionIds.delete(id)
const current = $botSections.get().filter(s => s.id !== id)
current.splice(Math.min(index, current.length), 0, section)
@@ -263,6 +277,13 @@ export async function moveBotsToSection(bots: RosterRow[], sectionId: null | str
continue
}
+ // Deleted while this loop was still walking its members (a rename's
+ // re-stamp stuck behind a slow write): filing the rest would put them back
+ // into a section that no longer exists.
+ if (sectionId && !$botSections.get().some(s => s.id === sectionId)) {
+ return
+ }
+
const current = botRosterMeta(bot, $botMeta.get())
if (botSectionId(bot, $botMeta.get()) !== (sectionId || null) || (current?.sectionName || null) !== sectionName) {