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.
This commit is contained in:
liuhao1024
2026-09-13 05:17:39 +08:00
committed by Teknium
parent f9c3a8a186
commit 6fc7032aac
6 changed files with 220 additions and 9 deletions
@@ -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: () => <div>{`PLUGIN-PAGE-${label}`}</div>
})
}
describe('RouteTilePane route-contribution reactivity (#109063)', () => {
it('mounts a plugin page whose route registers after the pane', () => {
render(<RouteTilePane path="/resetwatch" />)
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(<RouteTilePane path="/resetwatch" />)
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(<RouteTilePane path="/resetwatch" />)
act(() => {
dispose()
})
expect(screen.getByText(/no page at \/resetwatch/)).toBeTruthy()
act(() => {
dispose = registerResetwatch('A')
})
expect(screen.getByText('PLUGIN-PAGE-A')).toBeTruthy()
act(() => {
dispose()
})
})
})
+5 -3
View File
@@ -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 (
@@ -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<unknown>(null) }))
vi.mock('@/store/profile', () => ({ $activeGatewayProfile: atom('default') }))
vi.mock('@/store/session', () => ({
$freshDraftReady: atom(false),
$gatewayState: atom('open')
}))
vi.mock('../chat', () => ({ ChatView: () => <div data-testid="chat-view" /> }))
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: () => <div>{`PLUGIN-PAGE-${label}`}</div>
})
}
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(
<MemoryRouter initialEntries={['/resetwatch']}>
<ChatRoutesSurface actions={actions} />
</MemoryRouter>
)
// 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()
})
})
})
+2 -2
View File
@@ -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(
() =>
+37 -1
View File
@@ -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()
}
})
})
+8 -3
View File
@@ -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 ?? '',