The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
/**
* #95279: the Bot Mode model picker's catalog read must always SETTLE, and it
* must not churn the network on every surface remount.
*
* The read rides the BOT's own socket — a second, lazily dialed pool backend
* that can wedge (cold pool spawn, dropped remote hop) without the primary
* socket noticing. Two defects made the picker unusable while Bots was active:
*
* 1. The RPC had no deadline, so a wedged dial left it pending forever and
* the spinner never settled ("model picker never settles").
* 2. Every fetch forced `refresh: true`, bypassing the react-query cache, so
* each Bots view remount (tab re-front, dialog reopen, pane visibility
* flip) knocked the picker back into loading and discarded the staged
* selection mid-edit.
*
* Pinned here through the real component: bounded settlement into the
* free-text fallback, one cached unforced read across remounts, and no
* dispatch at all for a row whose connection is gone.
*/
import type * as HermesSdk from '@hermes/plugin-sdk'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { fireEvent, render, screen, waitFor } from '@testing-library/react'
import type { ReactNode } from 'react'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { translateBots } from './i18n-test-helper'
import { ModelPicker } from './model-picker'
import type { RosterRow } from './types'
const { hostMock } = vi.hoisted(() => ({
hostMock: {
request: vi.fn(),
requestProfile: vi.fn(),
state: { connectionId: { get: () => 'local' }, gateway: { get: () => 'open' }, profile: { get: () => 'default' } }
}
}))
vi.mock('@hermes/plugin-sdk', async () => {
const { useQuery } = await import('@tanstack/react-query')
const { catalogProviderMatches, useI18n } = await vi.importActual<typeof HermesSdk>('@hermes/plugin-sdk')
return {
Button: (props: React.ComponentProps<'button'>) => <button {...props} />,
catalogProviderMatches,
GlyphSpinner: () => <span data-testid="spinner" />,
host: hostMock,
Input: (props: React.ComponentProps<'input'>) => <input {...props} />,
Select: ({ children }: { children: ReactNode }) => <div>{children}</div>,
SelectContent: ({ children }: { children: ReactNode }) => <div>{children}</div>,
SelectItem: ({ children }: { children: ReactNode }) => <div>{children}</div>,
SelectTrigger: ({ children }: { children: ReactNode }) => <div>{children}</div>,
SelectValue: () => null,
useI18n,
usePluginI18n: () => translateBots,
useQuery
}
})
vi.mock('./shared', () => ({ ID: 'hermes-bots' }))
const remoteBot = {
connectionId: 'remote-a',
name: 'default',
remoteSource: true,
route: { connectionId: 'remote-a', mode: 'remote', profile: 'default', targetProfile: 'backend-default' },
sourceScoped: true,
targetProfile: 'backend-default'
} as RosterRow
/** One client per render group, so "did the remount refetch?" is a real
* question about the cache rather than about a fresh store. */
let client: QueryClient
function mount(bot: null | RosterRow) {
const wrapper = ({ children }: { children: ReactNode }) => (
<QueryClientProvider client={client}>{children}</QueryClientProvider>
)
return render(<ModelPicker bot={bot} onChange={vi.fn()} value={{ model: '', provider: '' }} />, { wrapper })
}
/** Mount with the selection a saved bot profile would carry into the editor. */
function mountWithSelection(bot: null | RosterRow, value: { model: string; provider: string }) {
const wrapper = ({ children }: { children: ReactNode }) => (
<QueryClientProvider client={client}>{children}</QueryClientProvider>
)
return render(<ModelPicker bot={bot} onChange={vi.fn()} value={value} />, { wrapper })
}
/** The fallback the picker paints when the catalog is unavailable. */
const isFreeText = (container: HTMLElement) =>
Boolean(container.querySelector('input[placeholder*="omnirouter"]')) &&
!container.querySelector('[data-testid="spinner"]')
beforeEach(() => {
vi.clearAllMocks()
client = new QueryClient({ defaultOptions: { queries: { retry: false } } })
})
afterEach(() => {
vi.useRealTimers()
})
describe('a bot gateway that never answers', () => {
it('settles into the free-text fallback instead of spinning forever', async () => {
vi.useFakeTimers({ shouldAdvanceTime: true })
hostMock.requestProfile.mockReturnValue(new Promise(() => undefined))
const { container } = mount(remoteBot)
expect(container.querySelector('[data-testid="spinner"]')).toBeTruthy()
await vi.advanceTimersByTimeAsync(30_000)
await waitFor(() => expect(isFreeText(container)).toBe(true))
})
})
describe('the catalog read', () => {
it('rides the bot’s own route, unforced, and survives a remount from cache', async () => {
hostMock.requestProfile.mockResolvedValue({ providers: [{ models: ['m1'], name: 'Prov', slug: 'prov' }] })
const first = mount(remoteBot)
await waitFor(() => expect(hostMock.requestProfile).toHaveBeenCalledTimes(1))
expect(hostMock.requestProfile).toHaveBeenCalledWith(remoteBot.route, 'model.options', {
explicit_only: false,
include_unconfigured: true
})
// A forced refresh here is what re-entered the loading state on every
// remount and wiped the user's in-progress pick.
expect(hostMock.requestProfile.mock.calls[0][2]).not.toHaveProperty('refresh')
first.unmount()
const second = mount(remoteBot)
await waitFor(() => expect(second.container.querySelector('[data-testid="spinner"]')).toBeNull())
expect(hostMock.requestProfile).toHaveBeenCalledTimes(1)
})
it('never dispatches for a row whose connection was removed', async () => {
const orphan = { name: 'ghost', remoteSource: true } as RosterRow
const { container } = mount(orphan)
await waitFor(() => expect(isFreeText(container)).toBe(true))
expect(hostMock.request).not.toHaveBeenCalled()
expect(hostMock.requestProfile).not.toHaveBeenCalled()
})
it('reads the ambient gateway for a local bot', async () => {
hostMock.request.mockResolvedValue({ providers: [] })
mount({ name: 'default' } as RosterRow)
await waitFor(() => expect(hostMock.request).toHaveBeenCalledTimes(1))
expect(hostMock.request).toHaveBeenCalledWith('model.options', {
explicit_only: false,
include_unconfigured: true
})
})
it('shares one cache entry between two bots on the same route', async () => {
hostMock.requestProfile.mockResolvedValue({ providers: [{ models: ['m1'], slug: 'prov' }] })
mount(remoteBot)
await waitFor(() => expect(hostMock.requestProfile).toHaveBeenCalledTimes(1))
mount({ ...remoteBot, name: 'default' } as RosterRow)
await waitFor(() => expect(hostMock.requestProfile).toHaveBeenCalledTimes(1))
})
})
describe('the manual-entry form vs the dropdowns (#121875)', () => {
it('shows the dropdowns on the FIRST open once the async catalog resolves', async () => {
// The saved provider is known to the catalog, but the read is still in
// flight at mount — exactly the first-open state of the Edit flow. The
// picker must not latch the manual-entry form from the empty inventory
// it paints before the data arrives.
let resolveCatalog!: (value: { providers: Array<{ models: string[]; name?: string; slug: string }> }) => void
hostMock.requestProfile.mockReturnValue(
new Promise(resolve => {
resolveCatalog = resolve
})
)
const { container } = mountWithSelection(remoteBot, { model: 'm1', provider: 'prov' })
// Still loading: spinner, no form yet.
expect(container.querySelector('[data-testid="spinner"]')).toBeTruthy()
resolveCatalog({ providers: [{ models: ['m1'], name: 'Prov', slug: 'prov' }] })
await waitFor(() => expect(container.querySelector('[data-testid="spinner"]')).toBeNull())
expect(isFreeText(container)).toBe(false)
})
it('keeps the manual form for a provider the catalog does not know, and Back to dropdowns still returns', async () => {
hostMock.requestProfile.mockResolvedValue({ providers: [{ models: ['m1'], slug: 'prov' }] })
const { container } = mountWithSelection(remoteBot, { model: 'custom-model', provider: 'my-router' })
// Unknown even to the LOADED inventory: the manual form is the only way
// to keep editing the hand-typed provider/model pair.
await waitFor(() => expect(isFreeText(container)).toBe(true))
// The explicit "Back to dropdowns" gesture still wins over the derived
// form — it is the user's choice, not the catalog's.
fireEvent.click(screen.getByRole('button'))
await waitFor(() => expect(isFreeText(container)).toBe(false))
})
})
describe('a saved custom provider', () => {
it('matches its catalog row through the custom:<key> alias', async () => {
// model.options reports a user-defined provider as `custom:<key>`, while
// the catalog row carries the bare key as its slug plus the alias list.
hostMock.requestProfile.mockResolvedValue({
providers: [{ aliases: ['custom:lab', 'lab'], models: ['lab-small', 'lab-large'], name: 'Lab', slug: 'lab' }]
})
const { container } = mountWithSelection(remoteBot, { model: 'lab-large', provider: 'custom:lab' })
await waitFor(() => expect(container.querySelector('[data-testid="spinner"]')).toBeNull())
expect(isFreeText(container)).toBe(false)
expect(screen.getByText('lab-small')).toBeTruthy()
})
})