fix(commands): reload location catalogs on stream reconnect

OpenCode returns an empty command list for a location until it has
loaded that location's config, then emits a command.updated event. When
the browser stream or the upstream SSE connection is reconnecting, that
event is missed and nothing reloads the list, leaving the slash menu
stuck on the builtin commands until the OpenCode server is restarted.

Reload commands, agents and skills on both a browser stream reconnect
and an upstream resync, and handle skill.updated so skill changes
refresh like agent and command changes already do.
This commit is contained in:
Chris Scott committed 2026-10-08 15:27:30 -04:00
1 parent 7f891f43cd
commit e7dc106af7
2 files changed
+59

No files matched your search

@@ -814,6 +814,7 @@ describe('EventProvider permissions and forms', () => {
it.each<[string, string[]]>([
['agent.updated', ['opencode', 'agents']],
['command.updated', ['opencode', 'commands']],
['skill.updated', ['opencode', 'skills']],
])('invalidates the %s cache', async (type, queryKey) => {
const queryClient = createTestQueryClient()
const invalidateQueries = vi.spyOn(queryClient, 'invalidateQueries')
@@ -835,6 +836,53 @@ describe('EventProvider permissions and forms', () => {
})
})
it('invalidates the location catalog caches on an upstream resync', async () => {
const queryClient = createTestQueryClient()
const invalidateQueries = vi.spyOn(queryClient, 'invalidateQueries')
render(<Harness />, { wrapper: createWrapper(queryClient) })
await waitFor(() => expect(mocks.subscribeGlobalMonitor).toHaveBeenCalled())
invalidateQueries.mockClear()
const lastSubscribeCall = mocks.subscribeGlobalMonitor.mock.calls[mocks.subscribeGlobalMonitor.mock.calls.length - 1]
const onResync = lastSubscribeCall[0].onResync as (() => void) | undefined
act(() => {
onResync?.()
})
await waitFor(() => {
expect(invalidateQueries).toHaveBeenCalledWith({ queryKey: ['opencode', 'commands'] })
expect(invalidateQueries).toHaveBeenCalledWith({ queryKey: ['opencode', 'agents'] })
expect(invalidateQueries).toHaveBeenCalledWith({ queryKey: ['opencode', 'skills'] })
})
})
it('invalidates the location catalog caches when the browser stream reconnects', async () => {
const queryClient = createTestQueryClient()
const invalidateQueries = vi.spyOn(queryClient, 'invalidateQueries')
render(<Harness />, { wrapper: createWrapper(queryClient) })
await waitFor(() => expect(mocks.subscribeGlobalMonitor).toHaveBeenCalled())
invalidateQueries.mockClear()
const lastSubscribeCall = mocks.subscribeGlobalMonitor.mock.calls[mocks.subscribeGlobalMonitor.mock.calls.length - 1]
const handleStatusChange = lastSubscribeCall[0].onStatusChange as (connected: boolean) => void
act(() => {
handleStatusChange(false)
handleStatusChange(true)
})
await waitFor(() => {
expect(invalidateQueries).toHaveBeenCalledWith({ queryKey: ['opencode', 'commands'] })
expect(invalidateQueries).toHaveBeenCalledWith({ queryKey: ['opencode', 'agents'] })
expect(invalidateQueries).toHaveBeenCalledWith({ queryKey: ['opencode', 'skills'] })
})
})
it('invalidates both config caches on config.updated', async () => {
const queryClient = createTestQueryClient()
const invalidateQueries = vi.spyOn(queryClient, 'invalidateQueries')
+11
View File
@@ -29,6 +29,12 @@ import {
const STATUS_POLL_INTERVAL_MS = 5000
const LOCATION_CATALOG_QUERY_KEYS = [
['opencode', 'commands'],
['opencode', 'agents'],
['opencode', 'skills'],
] as const
type PermissionsBySession = Record<string, PermissionRequest[]>
type FormsBySession = Record<string, FormInfo[]>
type SSEHealthState = Pick<EventStreamHealthState, 'isConnected' | 'isHealthy' | 'isStalled'>
@@ -568,6 +574,9 @@ export function EventProvider({ children }: { children: React.ReactNode }) {
case 'command.updated':
invalidateQueryKeysDebounced(queryClient, [['opencode', 'commands']])
break
case 'skill.updated':
invalidateQueryKeysDebounced(queryClient, [['opencode', 'skills']])
break
case 'config.updated':
invalidateQueryKeysDebounced(queryClient, [['opencode', 'config'], ['opencode-config']])
break
@@ -599,6 +608,7 @@ export function EventProvider({ children }: { children: React.ReactNode }) {
fetchInitialPendingData()
fetchInitialSessionStatuses()
invalidateChildSessionCaches(queryClient)
invalidateQueryKeysDebounced(queryClient, LOCATION_CATALOG_QUERY_KEYS)
startStatusPoll()
} else {
statusSyncVersionRef.current += 1
@@ -610,6 +620,7 @@ export function EventProvider({ children }: { children: React.ReactNode }) {
void reconcilePendingActionsForDirectories(collectTrackedDirectories())
void fetchInitialSessionStatuses()
invalidateChildSessionCaches(queryClient)
invalidateQueryKeysDebounced(queryClient, LOCATION_CATALOG_QUERY_KEYS)
}
const initialDirectories = [...new Set((reposRef.current ?? []).map(r => r.fullPath))]