From 74a2f6fb6ec3c91f176086832ae5119d54736620 Mon Sep 17 00:00:00 2001 From: Chris Scott <> Date: Thu, 15 Oct 2026 13:25:01 +0000 Subject: [PATCH] fix(session): follow a moved session's new directory without a reload session.moved is published on the old directory and carries the new one in data.location, so it reached the page. useSSE refreshed the cached session on session.renamed but had no session.moved case, and the event context only invalidated session lists, so the page kept using the old directory and never subscribed the new one. Shells, prompts and other directory-scoped events in the new directory were missed until a refocus, reconnect or reload. Handle session.moved like session.renamed. The refetch flips the page's sessionDirectory, it subscribes the new directory, and the stream's onDirectoriesSubscribed reconciliation loads that directory's running shells and pending prompts. --- e2e/background-activity.spec.ts | 39 +++++++++++++++++++++++++++--- frontend/src/hooks/useSSE.test.tsx | 34 ++++++++++++++++++++++++++ frontend/src/hooks/useSSE.ts | 1 + 3 files changed, 70 insertions(+), 4 deletions(-) diff --git a/e2e/background-activity.spec.ts b/e2e/background-activity.spec.ts index 1e4f6be6..aa5814c9 100644 --- a/e2e/background-activity.spec.ts +++ b/e2e/background-activity.spec.ts @@ -23,7 +23,7 @@ async function requireOk(response: Awaited if (!response.ok()) { throw new Error(`${label} failed: ${response.status()} ${await response.text()}`) } - return response.json() + return response.status() === 204 ? null : response.json() } async function createSession(request: APIRequestContext, directory: string, parentID?: string): Promise { @@ -76,6 +76,12 @@ function createUnregisteredDirectory(name: string): string { return directory } +function waitForDirectorySubscription(page: Page, directory: string) { + return page.waitForResponse((response) => + response.url().includes('/api/sse/subscribe') && (response.request().postData() ?? '').includes(directory), + ) +} + async function navigateWithinApp(page: Page, url: string): Promise { await page.evaluate((target) => { window.history.pushState({}, '', target) @@ -196,9 +202,7 @@ test.describe('session background activity', () => { }) => { const directory = createUnregisteredDirectory('stale') const sessionId = await createSession(request, directory) - const subscribed = page.waitForResponse((response) => - response.url().includes('/api/sse/subscribe') && (response.request().postData() ?? '').includes(directory), - ) + const subscribed = waitForDirectorySubscription(page, directory) await page.goto(`/repos/${repo.id}/sessions/${sessionId}`) await expect(page.getByPlaceholder('Send a message...')).toBeVisible() @@ -218,4 +222,31 @@ test.describe('session background activity', () => { await expect(page.getByPlaceholder('Send a message...')).toBeVisible() await expect(backgroundIndicator(page)).toBeHidden({ timeout: 20_000 }) }) + + test('follows a session moved to another directory without a reload', async ({ page, request, repo }) => { + const source = createUnregisteredDirectory('move-source') + const target = createUnregisteredDirectory('move-target') + const sessionId = await createSession(request, source) + const subscribedToSource = waitForDirectorySubscription(page, source) + + await page.goto(`/repos/${repo.id}/sessions/${sessionId}`) + await expect(page.getByPlaceholder('Send a message...')).toBeVisible() + await subscribedToSource + + const subscribedToTarget = waitForDirectorySubscription(page, target) + await requireOk( + await request.post(`/api/opencode/api/session/${sessionId}/move`, { data: { directory: target } }), + 'move session', + ) + await subscribedToTarget + + const shellId = await startBackgroundShell(request, target, sessionId) + createdShells.push({ directory: target, id: shellId }) + + await expect(backgroundIndicator(page)).toBeVisible() + + await removeShell(request, target, shellId) + + await expect(backgroundIndicator(page)).toBeHidden() + }) }) diff --git a/frontend/src/hooks/useSSE.test.tsx b/frontend/src/hooks/useSSE.test.tsx index d014d512..a5997fa3 100644 --- a/frontend/src/hooks/useSSE.test.tsx +++ b/frontend/src/hooks/useSSE.test.tsx @@ -174,6 +174,40 @@ describe('useSSE', () => { unmount() }) + it('invalidates the cached session query on session.moved so the page follows the new directory', async () => { + const queryClient = new QueryClient({ + defaultOptions: { + queries: { retry: false }, + }, + }) + const invalidateQueries = vi.spyOn(queryClient, 'invalidateQueries') + queryClient.setQueryData(['opencode', 'session', 'session-1', '/repo'], { id: 'session-1' }) + + const { result, unmount } = renderHook( + () => useSSE('/repo', 'session-1'), + { wrapper: createWrapper(queryClient) } + ) + + await waitFor(() => expect(MockEventSource.instances).toHaveLength(1)) + await connect(0, 'client-1') + await waitFor(() => expect(result.current.isConnected).toBe(true)) + invalidateQueries.mockClear() + + act(() => { + MockEventSource.instances[0].emit('message', { + type: 'session.moved', + directory: '/repo', + data: { sessionID: 'session-1', projectID: 'proj-1', subpath: '', location: { directory: '/elsewhere' } }, + }) + }) + + await waitFor(() => { + expect(invalidateQueries).toHaveBeenCalledWith({ queryKey: ['opencode', 'session', 'session-1'] }) + }) + + unmount() + }) + it('invalidates the cached session query on session.renamed', async () => { const queryClient = new QueryClient({ defaultOptions: { diff --git a/frontend/src/hooks/useSSE.ts b/frontend/src/hooks/useSSE.ts index 5d9f18d8..2448b463 100644 --- a/frontend/src/hooks/useSSE.ts +++ b/frontend/src/hooks/useSSE.ts @@ -80,6 +80,7 @@ export const useSSE = (directory?: string | string[], currentSessionId?: string) break case 'session.renamed': + case 'session.moved': invalidateSessionQueryIfCached(queryClient, event.data.sessionID) break