Skip to content

The agent that grows with you

fix(desktop): cancelling New Bot mid-create no longer leaves the draft profile

Tekniumbda3e3f2c9ee
diff --git a/apps/desktop/src/plugins/hermes-bots/create-agent-lazy-profile.test.tsx b/apps/desktop/src/plugins/hermes-bots/create-agent-lazy-profile.test.tsx
index 1f6964abb28..9cfd8594d04 100644
--- a/apps/desktop/src/plugins/hermes-bots/create-agent-lazy-profile.test.tsx
+++ b/apps/desktop/src/plugins/hermes-bots/create-agent-lazy-profile.test.tsx
@@ -312,6 +312,56 @@ describe('materializing the draft profile', () => {
})
})
+// #141623: the draft profile is minted before the dialog finishes. Cancelling
+// must delete the draft THIS dialog created, including one whose profiles.create
+// is still in flight when the user backs out — it used to survive as a bot.
+describe('cancelling New Bot discards its draft profile', () => {
+ it('deletes a draft that already exists', async () => {
+ await renderDialog(true)
+ fireEvent.click(screen.getByRole('button', { name: 'Capabilities' }))
+ await waitFor(() => expect(mocks.skillsView.at(-1)).toMatchObject({ fixedProfile: 'inbox-triage' }))
+
+ fireEvent.click(screen.getByRole('button', { name: 'Cancel' }))
+
+ await waitFor(() => expect(mocks.deleteBot).toHaveBeenCalledWith({ name: 'inbox-triage' }))
+ expect(mocks.createCanonicalChat).not.toHaveBeenCalled()
+ })
+
+ it('deletes the draft whose profiles.create was still in flight at cancel', async () => {
+ const base = mocks.request.getMockImplementation()!
+ let finishCreate!: () => void
+ mocks.request.mockImplementation((method: string, params?: unknown) =>
+ method === 'profiles.create'
+ ? new Promise(resolve => {
+ finishCreate = () => resolve({})
+ })
+ : base(method, params)
+ )
+
+ await renderDialog(true)
+ fireEvent.click(screen.getByRole('button', { name: 'Capabilities' }))
+ await waitFor(() => expect(createCalls()).toHaveLength(1))
+
+ fireEvent.click(screen.getByRole('button', { name: 'Cancel' }))
+ finishCreate()
+
+ await waitFor(() => expect(mocks.deleteBot).toHaveBeenCalledWith({ name: 'inbox-triage' }))
+ // The abandoned draft is not configured or adopted as a finished bot.
+ expect(mocks.saveBotMeta).not.toHaveBeenCalled()
+ expect(mocks.createCanonicalChat).not.toHaveBeenCalled()
+ })
+
+ it('never deletes a bot the dialog finished creating', async () => {
+ await renderDialog(true)
+ fireEvent.click(screen.getByRole('button', { name: 'Create Bot' }))
+ await waitFor(() => expect(mocks.createCanonicalChat).toHaveBeenCalledWith('inbox-triage', { kickoff: true }))
+
+ // Reopened and cancelled without touching anything: nothing to discard.
+ fireEvent.click(screen.getByRole('button', { name: 'Cancel' }))
+ expect(mocks.deleteBot).not.toHaveBeenCalled()
+ })
+})
+
describe('provider readiness before the intro turn', () => {
const notReady = { error: 'No usable credentials found for openrouter.', ok: false }
diff --git a/apps/desktop/src/plugins/hermes-bots/create-dialog.tsx b/apps/desktop/src/plugins/hermes-bots/create-dialog.tsx
index bae27341c4d..6b8964c4923 100644
--- a/apps/desktop/src/plugins/hermes-bots/create-dialog.tsx
+++ b/apps/desktop/src/plugins/hermes-bots/create-dialog.tsx
@@ -191,6 +191,9 @@ export function CreateAgentDialog({ open, onClose, onConfigureModel, roster }: C
// button + MCP setup buttons). Distinct from createdRef on purpose:
// createdRef must stay a slug string for its sibling consumers.
const flightRef = useRef<Promise<null | string> | null>(null)
+ // Bumped by every discard: a profiles.create still in flight when the user
+ // cancels belongs to an abandoned draft and must not adopt the dialog.
+ const draftGenRef = useRef(0)
const [title, setTitle] = useState('')
const [description, setDescription] = useState('')
// Default shapes mode: deterministic blob face drawn from the agent's name
@@ -294,16 +297,13 @@ export function CreateAgentDialog({ open, onClose, onConfigureModel, roster }: C
// Create Agent it is a DRAFT. Cancelling the dialog deletes it, so
// preconfigure-then-back-out leaves zero residue. Best-effort and
// fire-and-forget: a failed cleanup surfaces a toast, never blocks close.
- const discardDraft = () => {
- const draft = createdRef.current
-
- if (!draft) {
- return
- }
-
- createdRef.current = null
- flightRef.current = null
-
+ //
+ // A cancel that lands while profiles.create is still in flight orphaned the
+ // profile that flight mints: createdRef is only set once the RPC returns, so
+ // the discard saw nothing to delete and the draft survived as a bot
+ // (#141623). The flight now checks, on return, whether its draft was
+ // discarded meanwhile and deletes it itself.
+ const removeDraftProfile = (draft: string) => {
const discard = remoteTarget
? requestForTarget('cli.exec', {
argv: ['profile', 'delete', draft, '--yes']
@@ -322,6 +322,18 @@ export function CreateAgentDialog({ open, onClose, onConfigureModel, roster }: C
.catch(err => host.notifyError(err, `Could not clean up draft profile "${draft}"`))
}
+ const discardDraft = () => {
+ const draft = createdRef.current
+
+ draftGenRef.current += 1
+ createdRef.current = null
+ flightRef.current = null
+
+ if (draft) {
+ removeDraftProfile(draft)
+ }
+ }
+
const reset = () => {
setName('')
setTitle('')
@@ -454,6 +466,8 @@ export function CreateAgentDialog({ open, onClose, onConfigureModel, roster }: C
return Promise.resolve(null)
}
+ const generation = draftGenRef.current
+
const flight = singleFlight(flightRef, async () => {
await requestForTarget('profiles.create', {
name: slug,
@@ -482,6 +496,16 @@ export function CreateAgentDialog({ open, onClose, onConfigureModel, roster }: C
}
: {})
})
+
+ // Discarded while the create was in flight: this dialog minted the
+ // profile and nobody will complete it, so delete it on the target it
+ // was created on and configure nothing.
+ if (generation !== draftGenRef.current) {
+ removeDraftProfile(slug)
+
+ return null
+ }
+
createdRef.current = slug
// Apply capability picks from the Advanced tabs (best-effort; the