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(agent): stop the delete confirmation from loading forever (#23727)

Rdmclin290fd993b8f9f
diff --git a/src/features/AgentDeleteConfirm/index.test.ts b/src/features/AgentDeleteConfirm/index.test.ts
index 90284f42a50..0dc211dfdd6 100644
--- a/src/features/AgentDeleteConfirm/index.test.ts
+++ b/src/features/AgentDeleteConfirm/index.test.ts
@@ -1,15 +1,19 @@
/**
* @vitest-environment happy-dom
*/
+import { act, render, screen } from '@testing-library/react';
+import { type ReactElement } from 'react';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { agentService } from '@/services/agent';
import {
+ AGENT_DELETE_IMPACT_TIMEOUT_MS,
confirmRemoveAgent,
getOtherCreatorWarning,
getRemoveAgentAlert,
getRemoveAgentConfirmState,
+ lookupAgentDeleteImpact,
} from './index';
const createModalMock = vi.hoisted(() => vi.fn());
@@ -186,4 +190,98 @@ describe('confirmRemoveAgent', () => {
expect(createModalMock).toHaveBeenCalledTimes(1);
expect(consoleError).toHaveBeenCalled();
});
+
+ describe('dialog content', () => {
+ // React only replays a suspended tree inside an awaited act scope.
+ const renderDialog = async () => {
+ await act(async () => {
+ render((createModalMock.mock.calls[0][0] as { content: ReactElement }).content);
+ });
+ };
+ const deleteButton = () => screen.getByRole('button', { name: 'delete' });
+
+ afterEach(() => {
+ vi.useRealTimers();
+ });
+
+ it('replaces the placeholder with the impact copy once the lookup resolves', async () => {
+ let resolveImpact!: (impact: Impact) => void;
+ vi.spyOn(agentService, 'getAgentDeleteImpact').mockReturnValue(
+ new Promise<Impact>((resolve) => {
+ resolveImpact = resolve;
+ }),
+ );
+
+ confirmRemoveAgent({ agentId: 'agent-1', onConfirm: vi.fn() });
+ await renderDialog();
+
+ expect(deleteButton()).toBeDisabled();
+
+ await act(async () => {
+ resolveImpact({ ...own, foreignTopicCount: 2, hasForeignContent: true });
+ });
+
+ expect(
+ screen.getByText('confirmRemoveSessionItemAlertWithOthersCount:2'),
+ ).toBeInTheDocument();
+ expect(deleteButton()).toBeEnabled();
+ });
+
+ it('falls back to the impact-unknown copy and allows Delete when the lookup hangs', async () => {
+ vi.useFakeTimers();
+ vi.spyOn(agentService, 'getAgentDeleteImpact').mockReturnValue(new Promise<Impact>(() => {}));
+ vi.spyOn(console, 'error').mockImplementation(() => {});
+ const onConfirm = vi.fn();
+
+ confirmRemoveAgent({ agentId: 'agent-1', onConfirm });
+ await renderDialog();
+ expect(deleteButton()).toBeDisabled();
+
+ await act(async () => {
+ await vi.advanceTimersByTimeAsync(AGENT_DELETE_IMPACT_TIMEOUT_MS);
+ });
+
+ expect(screen.getByText('confirmRemoveSessionItemAlertImpactUnknown')).toBeInTheDocument();
+ expect(deleteButton()).toBeEnabled();
+
+ await act(async () => {
+ deleteButton().click();
+ });
+ // An unknown impact acknowledges nothing, so the server guard still decides.
+ expect(onConfirm).toHaveBeenCalledWith({
+ acknowledgeForeignContent: false,
+ acknowledgeOtherCreator: false,
+ });
+ });
+ });
+});
+
+describe('lookupAgentDeleteImpact', () => {
+ afterEach(() => {
+ vi.useRealTimers();
+ vi.restoreAllMocks();
+ });
+
+ it('settles as null when the lookup outlives the timeout', async () => {
+ vi.useFakeTimers();
+ vi.spyOn(agentService, 'getAgentDeleteImpact').mockReturnValue(new Promise<Impact>(() => {}));
+ vi.spyOn(console, 'error').mockImplementation(() => {});
+
+ const result = lookupAgentDeleteImpact('agent-1', 1000);
+ await vi.advanceTimersByTimeAsync(1000);
+
+ await expect(result).resolves.toBeNull();
+ });
+
+ it('returns the impact when the lookup lands before the timeout', async () => {
+ vi.useFakeTimers();
+ const impact = { ...own, foreignTopicCount: 0, hasForeignContent: false };
+ vi.spyOn(agentService, 'getAgentDeleteImpact').mockResolvedValue(impact);
+
+ const result = lookupAgentDeleteImpact('agent-1', 1000);
+
+ await expect(result).resolves.toEqual(impact);
+ // The timer is cleared, so nothing fires once the lookup has settled.
+ expect(vi.getTimerCount()).toBe(0);
+ });
});
diff --git a/src/features/AgentDeleteConfirm/index.tsx b/src/features/AgentDeleteConfirm/index.tsx
index 3c957802efc..26ef2158d3e 100644
--- a/src/features/AgentDeleteConfirm/index.tsx
+++ b/src/features/AgentDeleteConfirm/index.tsx
@@ -145,6 +145,44 @@ const LoadedConfirm = ({ impact, onConfirm }: LoadedConfirmProps) => {
);
};
+/**
+ * How long the dialog waits for the impact lookup before it gives up and shows
+ * the impact-unknown copy. The lookup scans the agent's topics and messages and
+ * can run into the function timeout on large agents; without a bound the
+ * dialog would sit on its placeholder with Delete disabled for good.
+ */
+export const AGENT_DELETE_IMPACT_TIMEOUT_MS = 8000;
+
+/**
+ * Look up what deleting `agentId` would take down, settling as `null` when the
+ * lookup fails, throws synchronously, or outlives `timeoutMs`. `null` is the
+ * "can't tell" state: the dialog warns that every topic goes and acknowledges
+ * nothing, so the server guard still decides for non-owners.
+ */
+export const lookupAgentDeleteImpact = (
+ agentId: string,
+ timeoutMs: number = AGENT_DELETE_IMPACT_TIMEOUT_MS,
+): Promise<AgentDeleteImpact | null> =>
+ new Promise<AgentDeleteImpact | null>((resolve) => {
+ const timer = setTimeout(() => {
+ console.error('[confirmRemoveAgent] impact lookup timed out', { agentId, timeoutMs });
+ resolve(null);
+ }, timeoutMs);
+ const settle = (value: AgentDeleteImpact | null) => {
+ clearTimeout(timer);
+ resolve(value);
+ };
+
+ // Built inside a promise executor so a lookup that throws synchronously
+ // still settles as "failed".
+ new Promise<AgentDeleteImpact>((resolveLookup) =>
+ resolveLookup(agentService.getAgentDeleteImpact(agentId)),
+ ).then(settle, (error) => {
+ console.error('[confirmRemoveAgent]', error);
+ settle(null);
+ });
+ });
+
/**
* Open the shared agent deletion confirmation. Every delete entry point (the
* sidebar, the mobile list, the agent profile) goes through here so authors
@@ -153,17 +191,11 @@ const LoadedConfirm = ({ impact, onConfirm }: LoadedConfirmProps) => {
* The dialog opens right away, bound to `agentId`: its mask keeps the user on
* the agent they started from instead of a late dialog surfacing over another
* one. It shows a placeholder with Delete disabled until the impact lookup
- * settles (see `getRemoveAgentConfirmState`).
+ * settles (see `getRemoveAgentConfirmState`), which takes at most
+ * `AGENT_DELETE_IMPACT_TIMEOUT_MS`.
*/
export const confirmRemoveAgent = ({ agentId, onConfirm }: ConfirmRemoveAgentOptions): void => {
- // Built inside a promise executor so a lookup that throws synchronously
- // still settles as "failed" and the dialog opens with the impact-unknown copy.
- const impact = new Promise<AgentDeleteImpact>((resolve) =>
- resolve(agentService.getAgentDeleteImpact(agentId)),
- ).catch((error) => {
- console.error('[confirmRemoveAgent]', error);
- return null;
- });
+ const impact = lookupAgentDeleteImpact(agentId);
createModal({
content: (