From 6fc7032aac3105e016224f3185d9bc997fcd8f8b Mon Sep 17 00:00:00 2001 From: liuhao1024 Date: Sun, 13 Sep 2026 05:17:39 +0800 Subject: [PATCH] fix(desktop): feed the ROUTES_AREA subscription snapshot into contributedRoutes (#109063) React route consumers subscribed via useContributions(ROUTES_AREA) but discarded its snapshot, deriving routes through an independent imperative contributedRoutes() call. With React Compiler enabled the derived routes could stay memoized across a late registration or hot replacement the subscription DID deliver, so an installed plugin's page never mounted while its sidebar entry did (#109063). contributedRoutes() now accepts an explicit contribution snapshot, defaulting to the registry read for imperative callers; both React consumers (ChatRoutesSurface, RouteTilePane) pass their snapshot through. --- .../app/chat/route-tile-reactivity.test.tsx | 78 ++++++++++++++++ apps/desktop/src/app/chat/route-tile.tsx | 8 +- .../app/contrib/surfaces-reactivity.test.tsx | 90 +++++++++++++++++++ apps/desktop/src/app/contrib/surfaces.tsx | 4 +- apps/desktop/src/app/routes.test.ts | 38 +++++++- apps/desktop/src/app/routes.ts | 11 ++- 6 files changed, 220 insertions(+), 9 deletions(-) create mode 100644 apps/desktop/src/app/chat/route-tile-reactivity.test.tsx create mode 100644 apps/desktop/src/app/contrib/surfaces-reactivity.test.tsx diff --git a/apps/desktop/src/app/chat/route-tile-reactivity.test.tsx b/apps/desktop/src/app/chat/route-tile-reactivity.test.tsx new file mode 100644 index 0000000000..b01d843695 --- /dev/null +++ b/apps/desktop/src/app/chat/route-tile-reactivity.test.tsx @@ -0,0 +1,78 @@ +import { act, cleanup, render, screen } from '@testing-library/react' +import { afterEach, describe, expect, it } from 'vitest' + +import { registry } from '@/contrib/registry' + +import { RouteTilePane } from './route-tile' + +// #109063: route tiles subscribe to ROUTES_AREA but used to derive routes via +// an independently-called `contributedRoutes()`, which React Compiler can keep +// memoized across the delivered subscription update. These tests run through +// the real registry, the real `useContributions` hook and the compiler-enabled +// Vite config — mocking the route list would hide exactly this defect. + +afterEach(cleanup) + +/** Register (or hot-replace) the plugin route page mounted at `/resetwatch`. */ +function registerResetwatch(label: string) { + return registry.register({ + id: 'resetwatch', + area: 'routes', + title: `Resetwatch ${label}`, + data: { path: '/resetwatch' }, + render: () =>
{`PLUGIN-PAGE-${label}`}
+ }) +} + +describe('RouteTilePane route-contribution reactivity (#109063)', () => { + it('mounts a plugin page whose route registers after the pane', () => { + render() + expect(screen.getByText(/no page at \/resetwatch/)).toBeTruthy() + + let dispose = () => {} + act(() => { + dispose = registerResetwatch('A') + }) + expect(screen.getByText('PLUGIN-PAGE-A')).toBeTruthy() + + act(() => { + dispose() + }) + }) + + it('hot-replaces the page when the same contribution id re-registers', () => { + let dispose = registerResetwatch('A') + render() + expect(screen.getByText('PLUGIN-PAGE-A')).toBeTruthy() + + act(() => { + dispose() + dispose = registerResetwatch('B') + }) + expect(screen.getByText('PLUGIN-PAGE-B')).toBeTruthy() + expect(screen.queryByText('PLUGIN-PAGE-A')).toBeNull() + + act(() => { + dispose() + }) + }) + + it('returns to the fallback after dispose and mounts again on re-register', () => { + let dispose = registerResetwatch('A') + render() + + act(() => { + dispose() + }) + expect(screen.getByText(/no page at \/resetwatch/)).toBeTruthy() + + act(() => { + dispose = registerResetwatch('A') + }) + expect(screen.getByText('PLUGIN-PAGE-A')).toBeTruthy() + + act(() => { + dispose() + }) + }) +}) diff --git a/apps/desktop/src/app/chat/route-tile.tsx b/apps/desktop/src/app/chat/route-tile.tsx index 1388f48c04..7fc508abd4 100644 --- a/apps/desktop/src/app/chat/route-tile.tsx +++ b/apps/desktop/src/app/chat/route-tile.tsx @@ -47,12 +47,14 @@ function routeTitle(path: string): string { return contributedRoutes().find(r => r.path === path)?.title ?? humanizePath(path) } -function RouteTilePane({ path }: { path: string }) { +export function RouteTilePane({ path }: { path: string }) { const builtin = BUILTIN_PAGES[path] // Subscribe so a plugin page tile appears the moment its route registers. - useContributions(ROUTES_AREA) - const contrib = builtin ? null : contributedRoutes().find(r => r.path === path) + // The snapshot feeds the lookup: under React Compiler an independently + // called contributedRoutes() can stay memoized across that registration. + const contributions = useContributions(ROUTES_AREA) + const contrib = builtin ? null : contributedRoutes(contributions).find(r => r.path === path) if (builtin) { return ( diff --git a/apps/desktop/src/app/contrib/surfaces-reactivity.test.tsx b/apps/desktop/src/app/contrib/surfaces-reactivity.test.tsx new file mode 100644 index 0000000000..b3ae15977e --- /dev/null +++ b/apps/desktop/src/app/contrib/surfaces-reactivity.test.tsx @@ -0,0 +1,90 @@ +import { act, cleanup, render, screen } from '@testing-library/react' +import { atom } from 'nanostores' +import { MemoryRouter } from 'react-router' +import { afterEach, describe, expect, it, vi } from 'vitest' + +import { registry } from '@/contrib/registry' + +import { ChatRoutesSurface } from './surfaces' +import type { WiringActions } from './types' + +// #109063: the workspace surface subscribes to ROUTES_AREA but used to derive +// routes via an independently-called `contributedRoutes()`, which React +// Compiler can keep memoized across the delivered subscription update. The +// registry, `useContributions` and the routes module all stay REAL here — only +// unrelated stores and heavy sibling views are stubbed, because mocking the +// route list is exactly what hid the defect. + +vi.mock('@/store/connections', () => ({ $activeConnectionId: atom('local') })) +vi.mock('@/store/gateway', () => ({ $gateway: atom(null) })) +vi.mock('@/store/profile', () => ({ $activeGatewayProfile: atom('default') })) +vi.mock('@/store/session', () => ({ + $freshDraftReady: atom(false), + $gatewayState: atom('open') +})) +vi.mock('../chat', () => ({ ChatView: () =>
})) +vi.mock('../chat/sidebar', () => ({ ChatSidebar: () => null })) +vi.mock('../right-sidebar/terminal/chrome', () => ({ TerminalPaneChrome: () => null })) +vi.mock('../shell/hooks/use-status-snapshot', () => ({ useStatusSnapshot: () => ({}) })) +vi.mock('../shell/hooks/use-statusbar-items', () => ({ + useStatusbarItems: () => ({ leftStatusbarItems: [], statusbarItems: [] }) +})) +vi.mock('../shell/statusbar-controls', () => ({ StatusbarControls: () => null })) +vi.mock('../shell/model-menu-panel', () => ({ ModelMenuPanel: () => null })) +vi.mock('./latest-actions', () => ({ latestChatActions: () => ({}), latestSidebarActions: () => ({}) })) +vi.mock('./panes', () => ({ setStatusbarItemGroup: vi.fn(), useStatusbarContributions: () => [] })) + +afterEach(cleanup) + +/** Register (or hot-replace) the plugin route page mounted at `/resetwatch`. */ +function registerResetwatch(label: string) { + return registry.register({ + id: 'resetwatch', + area: 'routes', + title: `Resetwatch ${label}`, + data: { path: '/resetwatch' }, + render: () =>
{`PLUGIN-PAGE-${label}`}
+ }) +} + +describe('ChatRoutesSurface route-contribution reactivity (#109063)', () => { + it('mounts a plugin page registered after the surface, tracks replacement, and unmounts on dispose', () => { + const actions = { getGateway: () => null } as unknown as WiringActions + + render( + + + + ) + // Unregistered path falls through the catch-all redirect to the chat view. + expect(screen.getByTestId('chat-view')).toBeTruthy() + + let dispose = () => {} + act(() => { + // Late registration after the surface has mounted and settled. + dispose = registerResetwatch('A') + }) + expect(screen.getByText('PLUGIN-PAGE-A')).toBeTruthy() + expect(screen.queryByTestId('chat-view')).toBeNull() + + act(() => { + dispose() + dispose = registerResetwatch('B') + }) + expect(screen.getByText('PLUGIN-PAGE-B')).toBeTruthy() + + act(() => { + dispose() + }) + expect(screen.getByTestId('chat-view')).toBeTruthy() + + act(() => { + dispose = registerResetwatch('A') + }) + expect(screen.getByText('PLUGIN-PAGE-A')).toBeTruthy() + + act(() => { + dispose() + }) + }) +}) diff --git a/apps/desktop/src/app/contrib/surfaces.tsx b/apps/desktop/src/app/contrib/surfaces.tsx index 5bc3e49307..38fe681ba4 100644 --- a/apps/desktop/src/app/contrib/surfaces.tsx +++ b/apps/desktop/src/app/contrib/surfaces.tsx @@ -122,8 +122,8 @@ export const ChatRoutesSurface = memo(function ChatRoutesSurface({ const activeGatewayProfile = useStore($activeGatewayProfile) const gateway = useStore($gateway) const gatewayState = useStore($gatewayState) - useContributions(ROUTES_AREA) - const routeContributions = contributedRoutes() + const routeSnapshot = useContributions(ROUTES_AREA) + const routeContributions = contributedRoutes(routeSnapshot) const modelMenuContent = useMemo( () => diff --git a/apps/desktop/src/app/routes.test.ts b/apps/desktop/src/app/routes.test.ts index 3c8933d935..b26359b523 100644 --- a/apps/desktop/src/app/routes.test.ts +++ b/apps/desktop/src/app/routes.test.ts @@ -1,6 +1,8 @@ import { describe, expect, it } from 'vitest' -import { NEW_CHAT_ROUTE, primaryRouteSelectedSessionId, sessionRoute, SETTINGS_ROUTE } from './routes' +import { registry } from '@/contrib/registry' + +import { contributedRoutes, NEW_CHAT_ROUTE, primaryRouteSelectedSessionId, ROUTES_AREA, sessionRoute, SETTINGS_ROUTE } from './routes' const SESS_A = 'sess-a' const SESS_B = 'sess-b' @@ -28,3 +30,37 @@ describe('primaryRouteSelectedSessionId', () => { expect(primaryRouteSelectedSessionId(SETTINGS_ROUTE, null)).toBeNull() }) }) + +describe('contributedRoutes', () => { + const page = (id: string, path: string) => ({ + id, + area: ROUTES_AREA, + data: { path }, + render: () => null + }) + + it('reads the registry when called with no arguments', () => { + const dispose = registry.register(page('helper-contract', '/helper-contract')) + + try { + expect(contributedRoutes().some(route => route.path === '/helper-contract')).toBe(true) + } finally { + dispose() + } + }) + + it('uses an explicit snapshot instead of the registry (#109063)', () => { + // React consumers pass their useContributions(ROUTES_AREA) snapshot so the + // compiler-visible subscription stays a dependency of the derived routes. + // Pin the contract: the snapshot wins even when the registry disagrees. + const dispose = registry.register(page('helper-contract', '/helper-contract')) + + try { + const routes = contributedRoutes([page('explicit', '/explicit')]) + + expect(routes.map(route => route.path)).toEqual(['/explicit']) + } finally { + dispose() + } + }) +}) diff --git a/apps/desktop/src/app/routes.ts b/apps/desktop/src/app/routes.ts index 7e5fbd4718..db31f5cb80 100644 --- a/apps/desktop/src/app/routes.ts +++ b/apps/desktop/src/app/routes.ts @@ -3,6 +3,7 @@ import type { ReactNode } from 'react' import { noteActiveTreeGroup, revealTreePane } from '@/components/pane-shell/tree/store' import { registry } from '@/contrib/registry' +import type { Contribution } from '@/contrib/types' type NavigateLike = (to: string, options?: { replace?: boolean }) => void @@ -91,9 +92,13 @@ export interface RouteContribution { path: string } -export function contributedRoutes(): Array<{ key: string; path: string; title?: string; render: () => ReactNode }> { - return registry - .getArea(ROUTES_AREA) +// React consumers must pass their `useContributions(ROUTES_AREA)` snapshot in: +// with React Compiler enabled, an independently-called `contributedRoutes()` +// can stay memoized across a late registration the subscription DID deliver. +export function contributedRoutes( + contributions: readonly Contribution[] = registry.getArea(ROUTES_AREA) +): Array<{ key: string; path: string; title?: string; render: () => ReactNode }> { + return contributions .map(c => ({ key: `${c.source ?? 'core'}:${c.id}`, path: (c.data as RouteContribution | undefined)?.path ?? '',