From aeea96b466e0c00ed26f8992d9048d3109bb4d30 Mon Sep 17 00:00:00 2001 From: Chris Scott <99081550+chriswritescode-dev@users.noreply.github.com> Date: Thu, 8 Oct 2026 12:22:13 -0400 Subject: [PATCH] =?UTF-8?q?section=2013:=20Phase=2013=20=E2=80=94=20Source?= =?UTF-8?q?=20picker=20in=20the=20walkthrough=20panel?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/api/changeWalkthroughs.ts | 10 +- .../components/repo/MultiRunSheet.test.tsx | 4 +- .../session/ChangesWalkthroughSheet.test.tsx | 104 +++++++++++- .../session/ChangesWalkthroughSheet.tsx | 156 +++++++++++++++++- .../src/hooks/useChangeWalkthrough.test.tsx | 35 +++- frontend/src/hooks/useChangeWalkthrough.ts | 31 ++-- 6 files changed, 311 insertions(+), 29 deletions(-) diff --git a/frontend/src/api/changeWalkthroughs.ts b/frontend/src/api/changeWalkthroughs.ts index 352c318c..e66abe67 100644 --- a/frontend/src/api/changeWalkthroughs.ts +++ b/frontend/src/api/changeWalkthroughs.ts @@ -1,16 +1,22 @@ import { fetchWrapper } from './fetchWrapper' import { API_BASE_URL } from '@/config' +import { walkthroughSourceKey } from '@opencode-manager/shared/schemas' import type { ChangeWalkthroughState, GenerateChangeWalkthroughRequest, + WalkthroughSource, } from '@opencode-manager/shared/schemas' function walkthroughUrl(sessionId: string): string { return `${API_BASE_URL}/api/change-walkthroughs/${encodeURIComponent(sessionId)}` } -export async function getChangeWalkthrough(sessionId: string): Promise { - return fetchWrapper(walkthroughUrl(sessionId)) +export async function getChangeWalkthrough( + sessionId: string, + source: WalkthroughSource, +): Promise { + const query = `?source=${encodeURIComponent(walkthroughSourceKey(source))}` + return fetchWrapper(`${walkthroughUrl(sessionId)}${query}`) } export async function generateChangeWalkthrough( diff --git a/frontend/src/components/repo/MultiRunSheet.test.tsx b/frontend/src/components/repo/MultiRunSheet.test.tsx index a9116121..1b47ae26 100644 --- a/frontend/src/components/repo/MultiRunSheet.test.tsx +++ b/frontend/src/components/repo/MultiRunSheet.test.tsx @@ -500,7 +500,9 @@ describe('MultiRunSheet', () => { await user.click(within(card).getByRole('button', { name: /walkthrough/i })) expect(await screen.findByText('Change walkthrough')).toBeInTheDocument() - await waitFor(() => expect(mocks.getChangeWalkthrough).toHaveBeenCalledWith('ses_11')) + await waitFor(() => + expect(mocks.getChangeWalkthrough).toHaveBeenCalledWith('ses_11', { kind: 'session' }), + ) }) it('lists fusions with truncation and opens the fusion session', async () => { diff --git a/frontend/src/components/session/ChangesWalkthroughSheet.test.tsx b/frontend/src/components/session/ChangesWalkthroughSheet.test.tsx index 516c5efc..7e5abec5 100644 --- a/frontend/src/components/session/ChangesWalkthroughSheet.test.tsx +++ b/frontend/src/components/session/ChangesWalkthroughSheet.test.tsx @@ -111,7 +111,7 @@ describe('ChangesWalkthroughSheet', () => { await user.click(generate) await waitFor(() => { - expect(mocks.generateChangeWalkthrough).toHaveBeenCalledWith('ses_1', {}) + expect(mocks.generateChangeWalkthrough).toHaveBeenCalledWith('ses_1', { source: { kind: 'session' } }) }) expect(await screen.findByText('This change adds a greeting.')).toBeInTheDocument() expect(screen.getByText('Stop 1 of 2')).toBeInTheDocument() @@ -156,7 +156,7 @@ describe('ChangesWalkthroughSheet', () => { await user.click(screen.getByRole('button', { name: 'Update walkthrough' })) await waitFor(() => { - expect(mocks.generateChangeWalkthrough).toHaveBeenCalledWith('ses_1', {}) + expect(mocks.generateChangeWalkthrough).toHaveBeenCalledWith('ses_1', { source: { kind: 'session' } }) }) }) @@ -170,10 +170,106 @@ describe('ChangesWalkthroughSheet', () => { await user.click(screen.getByRole('button', { name: 'Regenerate walkthrough' })) await waitFor(() => { - expect(mocks.generateChangeWalkthrough).toHaveBeenCalledWith('ses_1', { regenerate: true }) + expect(mocks.generateChangeWalkthrough).toHaveBeenCalledWith('ses_1', { + regenerate: true, + source: { kind: 'session' }, + }) }) }) + it('switches to staged changes', async () => { + const user = userEvent.setup() + mocks.getChangeWalkthrough.mockResolvedValue(state({ walkthrough: null })) + mocks.generateChangeWalkthrough.mockResolvedValue(state()) + renderSheet() + + await user.click(screen.getByRole('combobox', { name: 'Changes to walk through' })) + await user.click(await screen.findByRole('option', { name: 'Staged' })) + + await waitFor(() => { + expect(mocks.getChangeWalkthrough).toHaveBeenCalledWith('ses_1', { kind: 'staged' }) + }) + + await user.click(await screen.findByRole('button', { name: 'Generate walkthrough' })) + + await waitFor(() => { + expect(mocks.generateChangeWalkthrough).toHaveBeenCalledWith('ses_1', { source: { kind: 'staged' } }) + }) + }) + + it('sends the branch base', async () => { + const user = userEvent.setup() + mocks.getChangeWalkthrough.mockResolvedValue(state({ walkthrough: null })) + mocks.generateChangeWalkthrough.mockResolvedValue(state()) + renderSheet() + + await user.click(screen.getByRole('combobox', { name: 'Changes to walk through' })) + await user.click(await screen.findByRole('option', { name: 'Branch vs base' })) + + const base = await screen.findByLabelText('Base branch') + await user.type(base, 'develop') + await user.tab() + + await waitFor(() => { + expect(mocks.getChangeWalkthrough).toHaveBeenCalledWith('ses_1', { kind: 'branch', base: 'develop' }) + }) + + await user.click(screen.getByRole('button', { name: 'Generate walkthrough' })) + + await waitFor(() => { + expect(mocks.generateChangeWalkthrough).toHaveBeenCalledWith('ses_1', { + source: { kind: 'branch', base: 'develop' }, + }) + }) + }) + + it('ignores an invalid pull request number', async () => { + const user = userEvent.setup() + mocks.getChangeWalkthrough.mockResolvedValue(state({ walkthrough: null })) + mocks.generateChangeWalkthrough.mockResolvedValue(state()) + renderSheet() + + await user.click(screen.getByRole('combobox', { name: 'Changes to walk through' })) + await user.click(await screen.findByRole('option', { name: 'Pull request' })) + + const number = await screen.findByLabelText('Pull request number') + await user.type(number, '0') + await user.tab() + + await user.click(screen.getByRole('button', { name: 'Generate walkthrough' })) + + await waitFor(() => { + expect(mocks.generateChangeWalkthrough).toHaveBeenCalledWith('ses_1', { source: { kind: 'session' } }) + }) + expect(mocks.getChangeWalkthrough).not.toHaveBeenCalledWith( + 'ses_1', + expect.objectContaining({ kind: 'pullRequest' }), + ) + }) + + it('clears a generation error when the source changes', async () => { + const user = userEvent.setup() + mocks.getChangeWalkthrough.mockResolvedValue(state({ walkthrough: null })) + mocks.generateChangeWalkthrough.mockRejectedValue( + new FetchError('Not a git repository', 409, 'WALKTHROUGH_NOT_A_REPO'), + ) + renderSheet() + + await user.click(screen.getByRole('combobox', { name: 'Changes to walk through' })) + await user.click(await screen.findByRole('option', { name: 'Branch vs base' })) + + await user.click(await screen.findByRole('button', { name: 'Generate walkthrough' })) + expect(await screen.findByText('Not a git repository')).toBeInTheDocument() + + await user.click(screen.getByRole('combobox', { name: 'Changes to walk through' })) + await user.click(await screen.findByRole('option', { name: 'Staged' })) + + await waitFor(() => { + expect(screen.queryByText('Not a git repository')).not.toBeInTheDocument() + }) + expect(await screen.findByRole('button', { name: 'Generate walkthrough' })).toBeVisible() + }) + it('shows progress while the server is generating instead of the generate button', async () => { mocks.getChangeWalkthrough.mockResolvedValue(state({ walkthrough: null, generating: true })) renderSheet() @@ -222,7 +318,7 @@ describe('ChangesWalkthroughSheet', () => { await user.click(screen.getByRole('button', { name: 'Retry unexplained stops' })) await waitFor(() => { - expect(mocks.generateChangeWalkthrough).toHaveBeenCalledWith('ses_1', {}) + expect(mocks.generateChangeWalkthrough).toHaveBeenCalledWith('ses_1', { source: { kind: 'session' } }) }) }) diff --git a/frontend/src/components/session/ChangesWalkthroughSheet.tsx b/frontend/src/components/session/ChangesWalkthroughSheet.tsx index 3df7ce7f..0fbc5ac5 100644 --- a/frontend/src/components/session/ChangesWalkthroughSheet.tsx +++ b/frontend/src/components/session/ChangesWalkthroughSheet.tsx @@ -4,16 +4,25 @@ import { ChevronLeft, ChevronRight, Loader2, RefreshCw } from 'lucide-react' import { SideDrawer, SideDrawerHeader } from '@/components/ui/side-drawer' import { Button } from '@/components/ui/button' import { Badge } from '@/components/ui/badge' +import { Input } from '@/components/ui/input' +import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select' import { DiffLines } from '@/components/file-browser/DiffLines' import { ScheduleRunMarkdown } from '@/components/schedules/ScheduleRunMarkdown' import { useChangeWalkthrough, useGenerateChangeWalkthrough } from '@/hooks/useChangeWalkthrough' import { GIT_STATUS_COLORS, GIT_STATUS_LABELS } from '@/lib/git-status-styles' import { cn } from '@/lib/utils' -import { WalkthroughOmittedFileSchema } from '@opencode-manager/shared/schemas' +import { + DEFAULT_WALKTHROUGH_SOURCE, + describeWalkthroughSource, + walkthroughSourceKey, + WalkthroughOmittedFileSchema, + WalkthroughSourceSchema, +} from '@opencode-manager/shared/schemas' import type { ChangeWalkthrough, WalkthroughHunk, WalkthroughOmittedFile, + WalkthroughSource, WalkthroughStop, } from '@opencode-manager/shared/schemas' @@ -86,6 +95,8 @@ interface ChangesWalkthroughContextValue { selectStop: (index: number) => void detailRef: RefObject failedStopCount: number + source: WalkthroughSource + setSource: (source: WalkthroughSource) => void } const ChangesWalkthroughContext = createContext(null) @@ -96,6 +107,128 @@ function useChangesWalkthrough(): ChangesWalkthroughContextValue { return value } +const WALKTHROUGH_SOURCE_OPTIONS: { value: WalkthroughSource['kind']; label: string }[] = [ + { value: 'session', label: 'This session' }, + { value: 'uncommitted', label: 'Uncommitted' }, + { value: 'staged', label: 'Staged' }, + { value: 'unstaged', label: 'Unstaged' }, + { value: 'branch', label: 'Branch vs base' }, + { value: 'pullRequest', label: 'Pull request' }, +] + +function walkthroughSourceLabel(source: WalkthroughSource): string { + return source.kind === 'session' ? 'the changes in this session' : describeWalkthroughSource(source) +} + +function sourceBase(source: WalkthroughSource): string { + return source.kind === 'branch' || source.kind === 'pullRequest' ? source.base ?? '' : '' +} + +function sourceNumber(source: WalkthroughSource): string { + return source.kind === 'pullRequest' ? String(source.number) : '' +} + +/** Chooses which set of changes the walkthrough covers; git sources commit as soon as they are valid. */ +const WalkthroughSourcePicker = memo(function WalkthroughSourcePicker() { + const { source, setSource } = useChangesWalkthrough() + const [kind, setKind] = useState(source.kind) + const [baseInput, setBaseInput] = useState(sourceBase(source)) + const [numberInput, setNumberInput] = useState(sourceNumber(source)) + + useEffect(() => { + setKind(source.kind) + setBaseInput(sourceBase(source)) + setNumberInput(sourceNumber(source)) + }, [source]) + + const commit = useCallback( + (candidate: unknown) => { + const parsed = WalkthroughSourceSchema.safeParse(candidate) + if (parsed.success) setSource(parsed.data) + }, + [setSource], + ) + + const branchSource = useCallback((): WalkthroughSource => { + const base = baseInput.trim() + return base ? { kind: 'branch', base } : { kind: 'branch' } + }, [baseInput]) + + const pullRequestSource = useCallback((): WalkthroughSource => { + const base = baseInput.trim() + return base + ? { kind: 'pullRequest', number: Number(numberInput), base } + : { kind: 'pullRequest', number: Number(numberInput) } + }, [baseInput, numberInput]) + + const commitBase = useCallback(() => { + if (kind === 'branch') { + commit(branchSource()) + } else if (kind === 'pullRequest') { + commit(pullRequestSource()) + } + }, [kind, commit, branchSource, pullRequestSource]) + + const commitNumber = useCallback(() => { + commit(pullRequestSource()) + }, [commit, pullRequestSource]) + + const handleKindChange = useCallback( + (next: WalkthroughSource['kind']) => { + setKind(next) + if (next === 'pullRequest') return + commit(next === 'branch' ? branchSource() : { kind: next }) + }, + [commit, branchSource], + ) + + return ( +
+ + + {kind === 'branch' || kind === 'pullRequest' ? ( + setBaseInput(event.target.value)} + onBlur={commitBase} + onKeyDown={(event) => { + if (event.key === 'Enter') commitBase() + }} + /> + ) : null} + + {kind === 'pullRequest' ? ( + setNumberInput(event.target.value)} + onBlur={commitNumber} + onKeyDown={(event) => { + if (event.key === 'Enter') commitNumber() + }} + /> + ) : null} +
+ ) +}) + interface ChangesWalkthroughProviderProps { sessionId: string active: boolean @@ -104,8 +237,10 @@ interface ChangesWalkthroughProviderProps { /** Loads a session's change walkthrough and shares its state with the surrounding chrome and body. */ export function ChangesWalkthroughProvider({ sessionId, active, children }: ChangesWalkthroughProviderProps) { - const stateQuery = useChangeWalkthrough(sessionId, active) - const generate = useGenerateChangeWalkthrough(sessionId) + const [source, setSource] = useState(DEFAULT_WALKTHROUGH_SOURCE) + const sourceKey = walkthroughSourceKey(source) + const stateQuery = useChangeWalkthrough(sessionId, active, source) + const generate = useGenerateChangeWalkthrough(sessionId, source) const resetGenerate = generate.reset const [stopIndex, setStopIndex] = useState(0) const detailRef = useRef(null) @@ -119,11 +254,15 @@ export function ChangesWalkthroughProvider({ sessionId, active, children }: Chan useEffect(() => { resetGenerate() - }, [active, sessionId, resetGenerate]) + }, [active, sessionId, sourceKey, resetGenerate]) + + useEffect(() => { + setSource(DEFAULT_WALKTHROUGH_SOURCE) + }, [sessionId]) useEffect(() => { setStopIndex(0) - }, [active, sessionId, walkthrough?.createdAt]) + }, [active, sessionId, sourceKey, walkthrough?.createdAt]) const hunksById = new Map(walkthrough?.hunks.map((hunk) => [hunk.id, hunk]) ?? []) const stops = walkthrough?.stops ?? [] @@ -158,6 +297,8 @@ export function ChangesWalkthroughProvider({ sessionId, active, children }: Chan selectStop, detailRef, failedStopCount, + source, + setSource, } return {children} @@ -232,12 +373,15 @@ export const ChangesWalkthroughView = memo(function ChangesWalkthroughView() { selectStop, detailRef, failedStopCount, + source, } = useChangesWalkthrough() const readyStopCount = stops.filter((stop) => stop.status === 'ready').length return (
+ + {error ? (

{walkthroughErrorMessage(error)}

@@ -264,7 +408,7 @@ export const ChangesWalkthroughView = memo(function ChangesWalkthroughView() { generating ? null : (

- Generate a step-by-step walkthrough of the changes in this session. + Generate a step-by-step walkthrough of {walkthroughSourceLabel(source)}.

diff --git a/frontend/src/hooks/useChangeWalkthrough.test.tsx b/frontend/src/hooks/useChangeWalkthrough.test.tsx index 6803087d..900a1bca 100644 --- a/frontend/src/hooks/useChangeWalkthrough.test.tsx +++ b/frontend/src/hooks/useChangeWalkthrough.test.tsx @@ -3,6 +3,7 @@ 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 { DEFAULT_WALKTHROUGH_SOURCE } from '@opencode-manager/shared/schemas' import type { ChangeWalkthrough, ChangeWalkthroughState } from '@opencode-manager/shared/schemas' const mocks = vi.hoisted(() => ({ @@ -55,7 +56,7 @@ describe('useGenerateChangeWalkthrough', () => { mocks.generateChangeWalkthrough.mockReturnValue(pending.promise) const { result, rerender } = renderHook( - ({ sessionId }: { sessionId: string }) => useGenerateChangeWalkthrough(sessionId), + ({ sessionId }: { sessionId: string }) => useGenerateChangeWalkthrough(sessionId, DEFAULT_WALKTHROUGH_SOURCE), { initialProps: { sessionId: 'ses_A' }, wrapper: createWrapper(queryClient) }, ) @@ -63,7 +64,7 @@ describe('useGenerateChangeWalkthrough', () => { result.current.mutate({}) }) await waitFor(() => { - expect(mocks.generateChangeWalkthrough).toHaveBeenCalledWith('ses_A', {}) + expect(mocks.generateChangeWalkthrough).toHaveBeenCalledWith('ses_A', { source: { kind: 'session' } }) }) rerender({ sessionId: 'ses_B' }) @@ -73,9 +74,11 @@ describe('useGenerateChangeWalkthrough', () => { }) await waitFor(() => { - expect(queryClient.getQueryData(changeWalkthroughQueryKey('ses_A'))).toEqual(state({ generating: true })) + expect(queryClient.getQueryData(changeWalkthroughQueryKey('ses_A', 'session'))).toEqual( + state({ generating: true }), + ) }) - expect(queryClient.getQueryData(changeWalkthroughQueryKey('ses_B'))).toBeUndefined() + expect(queryClient.getQueryData(changeWalkthroughQueryKey('ses_B', 'session'))).toBeUndefined() }) }) @@ -92,7 +95,9 @@ describe('useChangeWalkthrough', () => { .mockResolvedValueOnce(state({ generating: true })) .mockResolvedValueOnce(state({ walkthrough: walkthroughForA })) - const { result } = renderHook(() => useChangeWalkthrough('ses_A', true), { wrapper: createWrapper(queryClient) }) + const { result } = renderHook(() => useChangeWalkthrough('ses_A', true, DEFAULT_WALKTHROUGH_SOURCE), { + wrapper: createWrapper(queryClient), + }) await waitFor(() => expect(result.current.data?.generating).toBe(true)) @@ -110,4 +115,24 @@ describe('useChangeWalkthrough', () => { vi.useRealTimers() } }) + + it('invalidating the session key refetches every source', async () => { + const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } }) + mocks.getChangeWalkthrough.mockResolvedValue(state()) + + renderHook(() => useChangeWalkthrough('ses_A', true, { kind: 'staged' }), { + wrapper: createWrapper(queryClient), + }) + renderHook(() => useChangeWalkthrough('ses_A', true, { kind: 'unstaged' }), { + wrapper: createWrapper(queryClient), + }) + + await waitFor(() => expect(mocks.getChangeWalkthrough).toHaveBeenCalledTimes(2)) + + await act(async () => { + await queryClient.invalidateQueries({ queryKey: changeWalkthroughQueryKey('ses_A') }) + }) + + await waitFor(() => expect(mocks.getChangeWalkthrough).toHaveBeenCalledTimes(4)) + }) }) diff --git a/frontend/src/hooks/useChangeWalkthrough.ts b/frontend/src/hooks/useChangeWalkthrough.ts index 8438606b..b68cbbb0 100644 --- a/frontend/src/hooks/useChangeWalkthrough.ts +++ b/frontend/src/hooks/useChangeWalkthrough.ts @@ -1,34 +1,43 @@ import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query' import { generateChangeWalkthrough, getChangeWalkthrough } from '@/api/changeWalkthroughs' -import type { GenerateChangeWalkthroughRequest } from '@opencode-manager/shared/schemas' +import { walkthroughSourceKey } from '@opencode-manager/shared/schemas' +import type { GenerateChangeWalkthroughRequest, WalkthroughSource } from '@opencode-manager/shared/schemas' const GENERATING_POLL_INTERVAL_MS = 2_000 -export function changeWalkthroughQueryKey(sessionId: string) { - return ['change-walkthrough', sessionId] as const +export function changeWalkthroughQueryKey(sessionId: string, sourceKey?: string) { + return sourceKey === undefined + ? (['change-walkthrough', sessionId] as const) + : (['change-walkthrough', sessionId, sourceKey] as const) } -export function useChangeWalkthrough(sessionId: string | undefined, enabled: boolean) { +export function useChangeWalkthrough( + sessionId: string | undefined, + enabled: boolean, + source: WalkthroughSource, +) { + const sourceKey = walkthroughSourceKey(source) return useQuery({ - queryKey: changeWalkthroughQueryKey(sessionId ?? ''), - queryFn: () => getChangeWalkthrough(sessionId!), + queryKey: changeWalkthroughQueryKey(sessionId ?? '', sourceKey), + queryFn: () => getChangeWalkthrough(sessionId!, source), enabled: enabled && !!sessionId, staleTime: 30_000, refetchInterval: (query) => (query.state.data?.generating ? GENERATING_POLL_INTERVAL_MS : false), }) } -export function useGenerateChangeWalkthrough(sessionId: string | undefined) { +export function useGenerateChangeWalkthrough(sessionId: string | undefined, source: WalkthroughSource) { const queryClient = useQueryClient() + const sourceKey = walkthroughSourceKey(source) return useMutation({ mutationFn: async (request: GenerateChangeWalkthroughRequest) => { const targetSessionId = sessionId! - const state = await generateChangeWalkthrough(targetSessionId, request) - return { sessionId: targetSessionId, state } + const state = await generateChangeWalkthrough(targetSessionId, { ...request, source }) + return { sessionId: targetSessionId, sourceKey, state } }, - onSuccess: ({ sessionId: targetSessionId, state }) => { - queryClient.setQueryData(changeWalkthroughQueryKey(targetSessionId), state) + onSuccess: ({ sessionId: targetSessionId, sourceKey: targetSourceKey, state }) => { + queryClient.setQueryData(changeWalkthroughQueryKey(targetSessionId, targetSourceKey), state) }, }) }