From e7dc106af74885e93e08322e628f611513730a9f Mon Sep 17 00:00:00 2001 From: Chris Scott <99081550+chriswritescode-dev@users.noreply.github.com> Date: Thu, 8 Oct 2026 15:27:30 -0400 Subject: [PATCH] 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. --- frontend/src/contexts/EventContext.test.tsx | 48 +++++++++++++++++++++ frontend/src/contexts/EventContext.tsx | 11 +++++ 2 files changed, 59 insertions(+) diff --git a/frontend/src/contexts/EventContext.test.tsx b/frontend/src/contexts/EventContext.test.tsx index b0f77597..31218351 100644 --- a/frontend/src/contexts/EventContext.test.tsx +++ b/frontend/src/contexts/EventContext.test.tsx @@ -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(, { 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(, { 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') diff --git a/frontend/src/contexts/EventContext.tsx b/frontend/src/contexts/EventContext.tsx index bcef20aa..cd8fd19c 100644 --- a/frontend/src/contexts/EventContext.tsx +++ b/frontend/src/contexts/EventContext.tsx @@ -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 type FormsBySession = Record type SSEHealthState = Pick @@ -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))]