From 444a43aa1dd5314d3b43b3c9b91bdab31cec71d5 Mon Sep 17 00:00:00 2001 From: Chris Scott <99081550+chriswritescode-dev@users.noreply.github.com> Date: Sat, 3 Oct 2026 11:03:06 -0400 Subject: [PATCH] section 13: Phase 13: Multi-run launcher and run groups in the repo page --- frontend/src/api/multiRuns.ts | 27 ++ .../components/repo/MultiRunDialog.test.tsx | 217 +++++++++++ .../src/components/repo/MultiRunDialog.tsx | 336 ++++++++++++++++++ frontend/src/hooks/useMultiRuns.ts | 66 ++++ frontend/src/hooks/useRepoSiblings.ts | 10 +- frontend/src/pages/RepoDetail.tsx | 22 +- 6 files changed, 674 insertions(+), 4 deletions(-) create mode 100644 frontend/src/api/multiRuns.ts create mode 100644 frontend/src/components/repo/MultiRunDialog.test.tsx create mode 100644 frontend/src/components/repo/MultiRunDialog.tsx create mode 100644 frontend/src/hooks/useMultiRuns.ts diff --git a/frontend/src/api/multiRuns.ts b/frontend/src/api/multiRuns.ts new file mode 100644 index 00000000..fb2f6784 --- /dev/null +++ b/frontend/src/api/multiRuns.ts @@ -0,0 +1,27 @@ +import { fetchWrapper } from './fetchWrapper' +import { API_BASE_URL } from '@/config' +import type { LaunchMultiRunRequest, MultiRun } from '@opencode-manager/shared/schemas' + +export async function listMultiRuns(repoId: number): Promise { + const res = await fetchWrapper<{ runs: MultiRun[] }>(`${API_BASE_URL}/api/multi-runs`, { + params: { repoId }, + }) + return res.runs +} + +export async function launchMultiRun(request: LaunchMultiRunRequest): Promise { + const res = await fetchWrapper<{ run: MultiRun }>(`${API_BASE_URL}/api/multi-runs`, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify(request), + }) + return res.run +} + +export async function discardMultiRunEntry(runId: number, entryId: number): Promise { + const res = await fetchWrapper<{ run: MultiRun }>( + `${API_BASE_URL}/api/multi-runs/${encodeURIComponent(runId)}/entries/${encodeURIComponent(entryId)}/discard`, + { method: 'POST' }, + ) + return res.run +} diff --git a/frontend/src/components/repo/MultiRunDialog.test.tsx b/frontend/src/components/repo/MultiRunDialog.test.tsx new file mode 100644 index 00000000..356e1628 --- /dev/null +++ b/frontend/src/components/repo/MultiRunDialog.test.tsx @@ -0,0 +1,217 @@ +import { describe, it, expect, vi, beforeEach } from 'vitest' +import { render, screen, waitFor, within } from '@testing-library/react' +import userEvent from '@testing-library/user-event' +import { QueryClient, QueryClientProvider } from '@tanstack/react-query' +import type { ReactNode } from 'react' +import { MultiRunDialog } from './MultiRunDialog' +import type { MultiRun } from '@opencode-manager/shared/schemas' + +const mocks = vi.hoisted(() => ({ + listMultiRuns: vi.fn(), + launchMultiRun: vi.fn(), + discardMultiRunEntry: vi.fn(), + useProvidersWithModels: vi.fn(), +})) + +const mockNavigate = vi.fn() + +vi.mock('react-router-dom', async (importOriginal) => ({ + ...(await importOriginal('react-router-dom')), + useNavigate: () => mockNavigate, +})) + +vi.mock('@/api/multiRuns', () => ({ + listMultiRuns: mocks.listMultiRuns, + launchMultiRun: mocks.launchMultiRun, + discardMultiRunEntry: mocks.discardMultiRunEntry, +})) + +vi.mock('@/hooks/useProvidersWithModels', () => ({ + useProvidersWithModels: mocks.useProvidersWithModels, +})) + +vi.mock('@/lib/toast', () => ({ + showToast: { + success: vi.fn(), + error: vi.fn(), + warning: vi.fn(), + info: vi.fn(), + }, +})) + +const providers = [ + { + id: 'openai', + name: 'OpenAI', + source: 'configured', + isConnected: true, + models: [ + { id: 'gpt-4o', name: 'GPT-4o' }, + { id: 'gpt-4o-mini', name: 'GPT-4o mini' }, + { id: 'gpt-4.1', name: 'GPT-4.1' }, + ], + }, + { + id: 'anthropic', + name: 'Anthropic', + source: 'configured', + isConnected: true, + models: [ + { id: 'claude-opus', name: 'Claude Opus' }, + { id: 'claude-sonnet', name: 'Claude Sonnet' }, + { id: 'claude-haiku', name: 'Claude Haiku' }, + ], + }, +] + +const startedRun: MultiRun = { + id: 3, + repoId: 7, + name: 'Sweep', + prompt: 'go', + isolated: true, + baseRef: 'main', + createdAt: 1, + entries: [ + { + id: 11, + model: 'openai/gpt-4o', + status: 'started', + sessionId: 'ses_1', + directory: '/workspaces/sweep-1', + isolated: true, + error: null, + createdAt: 1, + updatedAt: 1, + }, + { + id: 12, + model: 'anthropic/claude-opus', + status: 'failed', + sessionId: null, + directory: null, + isolated: true, + error: 'launch failed', + createdAt: 1, + updatedAt: 1, + }, + ], +} + +function createWrapper() { + const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } }) + return ({ children }: { children: ReactNode }) => ( + {children} + ) +} + +function renderDialog(overrides: Partial> = {}) { + return render( + , + { wrapper: createWrapper() }, + ) +} + +describe('MultiRunDialog', () => { + beforeEach(() => { + vi.clearAllMocks() + mocks.useProvidersWithModels.mockReturnValue({ data: providers, isLoading: false }) + mocks.listMultiRuns.mockResolvedValue([]) + }) + + it('caps model selection at five', async () => { + const user = userEvent.setup() + renderDialog() + + const names = ['GPT-4o', 'GPT-4o mini', 'GPT-4.1', 'Claude Opus', 'Claude Sonnet'] + for (const name of names) { + await user.click(screen.getByRole('checkbox', { name })) + } + + expect(screen.getByText('5/5 selected')).toBeInTheDocument() + expect(screen.getByRole('checkbox', { name: 'Claude Haiku' })).toBeDisabled() + + await user.click(screen.getByRole('checkbox', { name: 'Claude Haiku' })) + expect(screen.getByText('5/5 selected')).toBeInTheDocument() + }) + + it('submits the launch request', async () => { + const user = userEvent.setup() + mocks.launchMultiRun.mockResolvedValue(startedRun) + renderDialog() + + await user.type(screen.getByLabelText('Group name'), 'Sweep') + await user.type(screen.getByLabelText('Prompt'), 'go') + await user.click(screen.getByRole('checkbox', { name: 'GPT-4o' })) + await user.click(screen.getByRole('button', { name: 'Launch' })) + + await waitFor(() => { + expect(mocks.launchMultiRun).toHaveBeenCalledWith({ + repoId: 7, + name: 'Sweep', + prompt: 'go', + models: ['openai/gpt-4o'], + isolate: true, + baseRef: 'main', + }) + }) + }) + + it('lists run entries with Open and Discard actions', async () => { + const user = userEvent.setup() + mocks.listMultiRuns.mockResolvedValue([startedRun]) + renderDialog() + + await user.click(screen.getByRole('tab', { name: 'Runs' })) + + await waitFor(() => { + expect(screen.getByText('openai/gpt-4o')).toBeInTheDocument() + }) + expect(screen.getByText('launch failed')).toBeInTheDocument() + expect(screen.getByRole('button', { name: /open/i })).toBeInTheDocument() + expect(screen.getAllByRole('button', { name: /discard/i })).toHaveLength(2) + }) + + it('opens a started run in the workspaces tab', async () => { + const user = userEvent.setup() + mocks.listMultiRuns.mockResolvedValue([startedRun]) + renderDialog() + + await user.click(screen.getByRole('tab', { name: 'Runs' })) + await waitFor(() => { + expect(screen.getByRole('button', { name: /open/i })).toBeInTheDocument() + }) + + await user.click(screen.getByRole('button', { name: /open/i })) + + expect(mockNavigate).toHaveBeenCalledWith('/repos/7/sessions/ses_1?repoTab=workspaces') + }) + + it('discards an entry after confirmation', async () => { + const user = userEvent.setup() + mocks.listMultiRuns.mockResolvedValue([startedRun]) + mocks.discardMultiRunEntry.mockResolvedValue(startedRun) + renderDialog() + + await user.click(screen.getByRole('tab', { name: 'Runs' })) + await waitFor(() => { + expect(screen.getAllByRole('button', { name: /discard/i })).toHaveLength(2) + }) + + await user.click(screen.getAllByRole('button', { name: /discard/i })[0]) + + const confirmDialog = await screen.findByRole('dialog', { name: 'Discard run' }) + await user.click(within(confirmDialog).getByRole('button', { name: 'Discard' })) + + await waitFor(() => { + expect(mocks.discardMultiRunEntry).toHaveBeenCalledWith(3, 11) + }) + }) +}) diff --git a/frontend/src/components/repo/MultiRunDialog.tsx b/frontend/src/components/repo/MultiRunDialog.tsx new file mode 100644 index 00000000..ca30f837 --- /dev/null +++ b/frontend/src/components/repo/MultiRunDialog.tsx @@ -0,0 +1,336 @@ +import { useEffect, useMemo, useState } from 'react' +import { useNavigate } from 'react-router-dom' +import { Loader2, Trash2, ArrowUpRight } from 'lucide-react' +import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from '@/components/ui/dialog' +import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs' +import { Button } from '@/components/ui/button' +import { Input } from '@/components/ui/input' +import { Textarea } from '@/components/ui/textarea' +import { Switch } from '@/components/ui/switch' +import { Checkbox } from '@/components/ui/checkbox' +import { Badge } from '@/components/ui/badge' +import { Label } from '@/components/ui/label' +import { ConfirmDestructiveDialog } from '@/components/ui/confirm-destructive-dialog' +import { SessionStatusIndicator } from '@/components/ui/session-status-indicator' +import { useProvidersWithModels } from '@/hooks/useProvidersWithModels' +import { useDiscardMultiRunEntry, useLaunchMultiRun, useMultiRuns } from '@/hooks/useMultiRuns' +import { + MULTI_RUN_MAX_MODELS, + type LaunchMultiRunRequest, + type MultiRunEntry, + type MultiRunEntryStatus, +} from '@opencode-manager/shared/schemas' + +interface MultiRunDialogProps { + repoId: number + directory?: string + defaultBaseRef?: string + open: boolean + onOpenChange: (open: boolean) => void +} + +interface PendingDiscard { + runId: number + entryId: number + model: string + isolated: boolean +} + +const STATUS_LABELS: Record = { + starting: 'Starting', + started: 'Started', + failed: 'Failed', + discarded: 'Discarded', +} + +export function MultiRunDialog({ + repoId, + directory, + defaultBaseRef, + open, + onOpenChange, +}: MultiRunDialogProps) { + const navigate = useNavigate() + const [activeTab, setActiveTab] = useState('launch') + const [name, setName] = useState('') + const [prompt, setPrompt] = useState('') + const [selectedModels, setSelectedModels] = useState([]) + const [isolate, setIsolate] = useState(true) + const [baseRef, setBaseRef] = useState('') + const [pendingDiscard, setPendingDiscard] = useState(null) + + const { data: providers } = useProvidersWithModels({ enabled: open, directory }) + const runsQuery = useMultiRuns(repoId, open) + const launch = useLaunchMultiRun(repoId) + const discard = useDiscardMultiRunEntry(repoId) + + useEffect(() => { + if (!open) return + setActiveTab('launch') + setName('') + setPrompt('') + setSelectedModels([]) + setIsolate(true) + setBaseRef(defaultBaseRef ?? '') + }, [open, defaultBaseRef]) + + const modelGroups = useMemo( + () => providers.filter((provider) => provider.models.length > 0), + [providers], + ) + + const toggleModel = (value: string, checked: boolean) => { + setSelectedModels((current) => { + if (checked) { + if (current.includes(value) || current.length >= MULTI_RUN_MAX_MODELS) return current + return [...current, value] + } + return current.filter((model) => model !== value) + }) + } + + const canSubmit = + name.trim().length > 0 && prompt.trim().length > 0 && selectedModels.length > 0 && !launch.isPending + + const handleLaunch = async () => { + const request: LaunchMultiRunRequest = { + repoId, + name: name.trim(), + prompt: prompt.trim(), + models: selectedModels, + isolate, + ...(baseRef.trim() ? { baseRef: baseRef.trim() } : {}), + } + await launch.mutateAsync(request) + setActiveTab('runs') + } + + const openEntry = (entry: MultiRunEntry) => { + if (!entry.sessionId) return + onOpenChange(false) + const suffix = entry.isolated ? '?repoTab=workspaces' : '' + navigate(`/repos/${repoId}/sessions/${entry.sessionId}${suffix}`) + } + + const confirmDiscard = () => { + if (!pendingDiscard) return + discard.mutate( + { runId: pendingDiscard.runId, entryId: pendingDiscard.entryId }, + { onSuccess: () => setPendingDiscard(null) }, + ) + } + + const runs = runsQuery.data ?? [] + + return ( + <> + + + + Multi-run + + Run one prompt on up to {MULTI_RUN_MAX_MODELS} models at once, each in its own session. + + + + +
+ + Launch + Runs + +
+ + +
+ + setName(event.target.value)} + placeholder="Sweep" + /> +
+ +
+ +