feat(walkthrough): stop an in-flight change walkthrough generation

Add DELETE /api/change-walkthroughs/:sessionId to cancel the generation for a session and source. The service removes the in-flight entry, persists the partial walkthrough, and aborts a per-generation signal threaded through model resolution and every model call. Cancellation is not recorded as a failure and leaves the remaining stops pending. A Stop button in the walkthrough chrome calls the new endpoint.
This commit is contained in:
Chris Scott committed 2026-10-10 17:48:14 +00:00
1 parent 2d134d9de7
commit 0752ebf360
12 files changed
+334 -21

No files matched your search

@@ -2,7 +2,7 @@ import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { act, renderHook, waitFor } from '@testing-library/react'
import type { ReactNode } from 'react'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { changeWalkthroughQueryKey, useChangeWalkthrough, useGenerateChangeWalkthrough } from './useChangeWalkthrough'
import { changeWalkthroughQueryKey, useCancelChangeWalkthrough, useChangeWalkthrough, useGenerateChangeWalkthrough } from './useChangeWalkthrough'
import { DEFAULT_WALKTHROUGH_SOURCE, walkthroughHunksIdentity } from '@opencode-manager/shared/schemas'
import type {
ChangeWalkthrough,
@@ -13,11 +13,13 @@ import type {
const mocks = vi.hoisted(() => ({
getChangeWalkthrough: vi.fn(),
generateChangeWalkthrough: vi.fn(),
cancelChangeWalkthrough: vi.fn(),
}))
vi.mock('@/api/changeWalkthroughs', () => ({
getChangeWalkthrough: mocks.getChangeWalkthrough,
generateChangeWalkthrough: mocks.generateChangeWalkthrough,
cancelChangeWalkthrough: mocks.cancelChangeWalkthrough,
}))
const walkthroughForA: ChangeWalkthrough = {
@@ -187,3 +189,30 @@ describe('useChangeWalkthrough', () => {
await waitFor(() => expect(mocks.getChangeWalkthrough).toHaveBeenCalledTimes(4))
})
})
describe('useCancelChangeWalkthrough', () => {
beforeEach(() => {
vi.clearAllMocks()
})
it('cancels the walkthrough and optimistically clears the generating state', async () => {
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } })
const queryKey = changeWalkthroughQueryKey('ses_A', 'session')
queryClient.setQueryData(queryKey, state({ generating: true }))
mocks.cancelChangeWalkthrough.mockResolvedValue(undefined)
mocks.getChangeWalkthrough.mockResolvedValue(state({ generating: false }))
const { result } = renderHook(() => useCancelChangeWalkthrough('ses_A', DEFAULT_WALKTHROUGH_SOURCE), {
wrapper: createWrapper(queryClient),
})
await act(async () => {
result.current.mutate()
})
expect(mocks.cancelChangeWalkthrough).toHaveBeenCalledWith('ses_A', { kind: 'session' })
await waitFor(() => {
expect(queryClient.getQueryData<ChangeWalkthroughState>(queryKey)?.generating).toBe(false)
})
})
})
+21 -1
View File
@@ -1,5 +1,5 @@
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
import { generateChangeWalkthrough, getChangeWalkthrough } from '@/api/changeWalkthroughs'
import { cancelChangeWalkthrough, generateChangeWalkthrough, getChangeWalkthrough } from '@/api/changeWalkthroughs'
import { walkthroughHunksIdentity, walkthroughSourceKey } from '@opencode-manager/shared/schemas'
import type {
ChangeWalkthrough,
@@ -81,3 +81,23 @@ export function useGenerateChangeWalkthrough(sessionId: string | undefined, sour
},
})
}
export function useCancelChangeWalkthrough(sessionId: string | undefined, source: WalkthroughSource) {
const queryClient = useQueryClient()
const sourceKey = walkthroughSourceKey(source)
return useMutation({
mutationFn: async () => {
const targetSessionId = sessionId!
await cancelChangeWalkthrough(targetSessionId, source)
return { sessionId: targetSessionId, sourceKey }
},
onSuccess: ({ sessionId: targetSessionId, sourceKey: targetSourceKey }) => {
const queryKey = changeWalkthroughQueryKey(targetSessionId, targetSourceKey)
queryClient.setQueryData<ChangeWalkthroughState>(queryKey, (cached) =>
cached ? { ...cached, generating: false, error: null } : cached,
)
queryClient.invalidateQueries({ queryKey })
},
})
}