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(onboarding): resolve agent route loading stall and branch redirect (#14795)

Inneie00c299d1c94
diff --git a/packages/agent-tracing/src/viewer/index.ts b/packages/agent-tracing/src/viewer/index.ts
index e9b1ebccbd6..129e8415cf0 100644
--- a/packages/agent-tracing/src/viewer/index.ts
+++ b/packages/agent-tracing/src/viewer/index.ts
@@ -63,7 +63,6 @@ export function resolveCeSnapshot(
/** @deprecated Use resolveCeSnapshot instead. */
export const resolveCeEvent = resolveCeSnapshot;
-
/**
* Resolve messages for a step, supporting both legacy (full) and incremental (delta) formats.
*/
diff --git a/packages/database/src/models/agent.ts b/packages/database/src/models/agent.ts
index f745784137b..ef495833867 100644
--- a/packages/database/src/models/agent.ts
+++ b/packages/database/src/models/agent.ts
@@ -616,7 +616,12 @@ export class AgentModel {
const persistConfig = getAgentPersistConfig(slug);
if (!persistConfig) return null;
- // 4. Create the builtin agent with persist config
+ // 4. Create the builtin agent with persist config.
+ // Idempotent under concurrent callers: two parallel requests for the same
+ // (userId, slug) both see no existing row and race to insert. Without
+ // `onConflictDoNothing`, the loser hits the `agents_slug_user_id_unique`
+ // constraint; with it, the loser's `.returning()` is empty and we re-read
+ // the row that won.
const result = await this.db
.insert(agents)
.values({
@@ -626,8 +631,15 @@ export class AgentModel {
userId: this.userId,
virtual: true,
})
+ .onConflictDoNothing({ target: [agents.slug, agents.userId] })
.returning();
- return result[0];
+ if (result[0]) return result[0];
+
+ return (
+ (await this.db.query.agents.findFirst({
+ where: and(eq(agents.slug, slug), eq(agents.userId, this.userId)),
+ })) ?? null
+ );
};
}
diff --git a/plugins/vite/sharedRendererConfig.ts b/plugins/vite/sharedRendererConfig.ts
index a9aff257cd8..89723c52c67 100644
--- a/plugins/vite/sharedRendererConfig.ts
+++ b/plugins/vite/sharedRendererConfig.ts
@@ -173,6 +173,7 @@ export function sharedRendererDefine(options: { isElectron: boolean; isMobile: b
'__DEV__': process.env.NODE_ENV !== 'production' ? 'true' : 'false',
'__ELECTRON__': JSON.stringify(options.isElectron),
'__MOBILE__': JSON.stringify(options.isMobile),
+ '__TEST__': 'false',
...nextPublicDefine,
// Keep a safe fallback so generic `process.env` access won't crash in browser runtime.
'process.env': '{}',
diff --git a/src/features/Onboarding/Common/index.tsx b/src/features/Onboarding/Common/index.tsx
index c49073dd8a1..47bf4f8de40 100644
--- a/src/features/Onboarding/Common/index.tsx
+++ b/src/features/Onboarding/Common/index.tsx
@@ -62,6 +62,12 @@ const CommonOnboardingPage = memo(() => {
remappedRef.current = true;
}, [isUserStateInit]);
+ useEffect(() => {
+ if (__TEST__) return;
+ void import('@/routes/onboarding/agent');
+ void import('@/routes/onboarding/classic');
+ }, []);
+
const goNextFromTelemetry = useCallback(() => {
setSearchParams({ step: '2' }, { replace: true });
}, [setSearchParams]);
diff --git a/src/routes/onboarding/agent/index.test.tsx b/src/routes/onboarding/agent/index.test.tsx
index 2664efe557b..fd7e85afeb8 100644
--- a/src/routes/onboarding/agent/index.test.tsx
+++ b/src/routes/onboarding/agent/index.test.tsx
@@ -1,4 +1,4 @@
-import { render, screen } from '@testing-library/react';
+import { render, screen, waitFor } from '@testing-library/react';
import { MemoryRouter, Route, Routes } from 'react-router-dom';
import { afterEach, describe, expect, it, vi } from 'vitest';
@@ -32,11 +32,12 @@ const renderAgentRoute = async ({
vi.doMock('@/features/Onboarding/Agent', () => ({
default: () => <div>Agent onboarding</div>,
}));
+ const serverConfigState = {
+ featureFlags: { enableAgentOnboarding: enabled },
+ serverConfigInit,
+ };
function selectFromServerConfigStore(selector: (state: Record<string, unknown>) => unknown) {
- return selector({
- featureFlags: { enableAgentOnboarding: enabled },
- serverConfigInit,
- });
+ return selector(serverConfigState);
}
vi.doMock('@/store/serverConfig', () => ({
@@ -84,7 +85,7 @@ describe('AgentOnboardingRoute', () => {
it('renders the agent onboarding page when the feature is enabled', async () => {
await renderAgentRoute({ enabled: true });
- expect(screen.getByText('Agent onboarding')).toBeInTheDocument();
+ await waitFor(() => expect(screen.getByText('Agent onboarding')).toBeInTheDocument());
});
it('shows a loading state before the server config is initialized', async () => {
@@ -102,7 +103,7 @@ describe('AgentOnboardingRoute', () => {
it('redirects to classic onboarding when the feature is disabled', async () => {
await renderAgentRoute({ enabled: false });
- expect(screen.getByText('Classic onboarding')).toBeInTheDocument();
+ await waitFor(() => expect(screen.getByText('Classic onboarding')).toBeInTheDocument());
});
it('redirects to classic onboarding on desktop builds', async () => {
@@ -114,7 +115,14 @@ describe('AgentOnboardingRoute', () => {
it('redirects to /onboarding when the shared prefix is incomplete', async () => {
await renderAgentRoute({ commonStepsCompleted: false, enabled: true });
- expect(screen.getByText('Common onboarding')).toBeInTheDocument();
+ await waitFor(() => expect(screen.getByText('Common onboarding')).toBeInTheDocument());
+ });
+
+ it('redirects to /onboarding instead of classic when shared prefix is incomplete even if the runtime flag is off', async () => {
+ await renderAgentRoute({ commonStepsCompleted: false, enabled: false });
+
+ await waitFor(() => expect(screen.getByText('Common onboarding')).toBeInTheDocument());
+ expect(screen.queryByText('Classic onboarding')).not.toBeInTheDocument();
});
it('redirects to classic when AGENT_ONBOARDING_ENABLED master switch is off', async () => {
diff --git a/src/routes/onboarding/agent/index.tsx b/src/routes/onboarding/agent/index.tsx
index 8ad03217f32..0db45176d42 100644
--- a/src/routes/onboarding/agent/index.tsx
+++ b/src/routes/onboarding/agent/index.tsx
@@ -23,14 +23,14 @@ const AgentOnboardingRoute = memo(() => {
if (!serverConfigInit || !isUserStateInit) return <Loading debugId="AgentOnboardingRoute" />;
- if (!enableAgentOnboarding) {
- return <Navigate replace to="/onboarding/classic" />;
- }
-
if (!commonStepsCompleted) {
return <Navigate replace to="/onboarding" />;
}
+ if (!enableAgentOnboarding) {
+ return <Navigate replace to="/onboarding/classic" />;
+ }
+
return <AgentOnboardingPage />;
});
diff --git a/src/types/global.d.ts b/src/types/global.d.ts
index 4a8a516c96c..5d4a5276a3c 100644
--- a/src/types/global.d.ts
+++ b/src/types/global.d.ts
@@ -39,6 +39,9 @@ declare global {
/** Vite define: development mode (NODE_ENV !== 'production') */
const __DEV__: boolean;
+ /** Vite define: running under Vitest */
+ const __TEST__: boolean;
+
/** Vite define: current bundle is mobile variant */
const __MOBILE__: boolean;
diff --git a/vitest.config.mts b/vitest.config.mts
index cc3f584262a..9e18392d09b 100644
--- a/vitest.config.mts
+++ b/vitest.config.mts
@@ -35,6 +35,7 @@ export default defineConfig({
'__DEV__': process.env.NODE_ENV !== 'production' ? 'true' : 'false',
'__ELECTRON__': 'false',
'__MOBILE__': 'false',
+ '__TEST__': 'true',
},
optimizeDeps: {
exclude: ['crypto', 'util', 'tty'],