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.
This commit is contained in:
Chris Scott committed 2026-10-15 13:25:01 +00:00
1 parent 37b4ab89f0
commit 74a2f6fb6e
3 files changed
+70 -4

No files matched your search

+35 -4
View File
@@ -23,7 +23,7 @@ async function requireOk(response: Awaited<ReturnType<APIRequestContext['post']>
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<string> {
@@ -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<void> {
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()
})
})
+34
View File
@@ -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: {
+1
View File
@@ -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