fix(sessions): prevent duplicate goal starts and lost autosave updates

- Block prompt submission while an armed goal is starting.
- Launch multi-runs with mutate so a failed launch is not an
  unhandled rejection.
- Save changed session automation fields in one update so concurrent
  autosaves cannot overwrite each other.
This commit is contained in:
Chris Scott committed 2026-10-03 22:51:03 -04:00
1 parent 96e02e9646
commit 2072fae1b6
6 files changed
+137 -38

No files matched your search

@@ -1,5 +1,5 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
import { render, screen, fireEvent, waitFor } from '@testing-library/react'
import { render, screen, fireEvent, waitFor, act } from '@testing-library/react'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { PromptInput } from './PromptInput'
import { useUIState } from '@/stores/uiStateStore'
@@ -47,10 +47,26 @@ vi.mock('@/hooks/useOpenCode', async (importOriginal) => {
}
})
vi.mock('@/hooks/useSessionGoals', () => ({
useSessionGoal: mocks.useSessionGoal,
useStartSessionGoal: () => ({ mutateAsync: mocks.startGoal, isPending: false }),
}))
vi.mock('@/hooks/useSessionGoals', async () => {
const { useState } = await import('react')
return {
useSessionGoal: mocks.useSessionGoal,
useStartSessionGoal: () => {
const [isPending, setIsPending] = useState(false)
return {
mutateAsync: async (variables: unknown) => {
setIsPending(true)
try {
return await mocks.startGoal(variables)
} finally {
setIsPending(false)
}
},
isPending,
}
},
}
})
vi.mock('@/hooks/useSessionPermissionMode', () => ({
useSessionPermissionMode: mocks.useSessionPermissionMode,
@@ -241,6 +257,33 @@ describe('PromptInput goal mode', () => {
expect(mocks.startGoal.mock.invocationCallOrder[0]).toBeLessThan(mocks.sendPrompt.mock.invocationCallOrder[0])
})
it('does not start the goal again while it is still starting', async () => {
stubMatchMedia(true)
let resolveGoal: (value: unknown) => void = () => {}
mocks.startGoal.mockImplementation(
() => new Promise((resolve) => {
resolveGoal = resolve
}),
)
renderComponent({ isStreamingResponse: true })
const input = await screen.findByPlaceholderText('Send a message...')
fireEvent.change(input, { target: { value: 'Ship the feature' } })
fireEvent.click(screen.getByRole('button', { name: GOAL_BUTTON }))
fireEvent.click(screen.getByTitle('Queue message'))
await waitFor(() => expect(mocks.startGoal).toHaveBeenCalledTimes(1))
fireEvent.click(screen.getByTitle('Queue message'))
expect(mocks.startGoal).toHaveBeenCalledTimes(1)
expect(mocks.sendPrompt).not.toHaveBeenCalled()
await act(async () => {
resolveGoal({ id: 1 })
})
})
it('does not start an armed goal for a slash command', async () => {
stubMatchMedia(true)
mocks.useCommands.mockReturnValue({ filterCommands: () => [{ name: 'review' }] })
@@ -231,10 +231,10 @@ export const PromptInput = memo(forwardRef<PromptInputHandle, PromptInputProps>(
const setStoredAgent = useSessionAgentStore((s) => s.setAgent)
const sendPrompt = useSendPrompt(directory)
const sendShell = useSendShell(directory)
const isPromptSubmitPending = sendPrompt.isPending || sendShell.isPending
const startGoal = useStartSessionGoal()
const isPromptSubmitPending = sendPrompt.isPending || sendShell.isPending || startGoal.isPending
const interruptSession = useInterruptSession()
const { data: sessionGoal } = useSessionGoal(sessionID)
const startGoal = useStartSessionGoal()
const { data: permissionMode } = useSessionPermissionMode(sessionID)
const { filterCommands } = useCommands({ directory })
const isExactCommandPrompt = (value: string) => {
@@ -311,6 +311,7 @@ export const PromptInput = memo(forwardRef<PromptInputHandle, PromptInputProps>(
const handleSubmit = async () => {
if (!prompt.trim() && imageAttachments.length === 0) return
if (startGoal.isPending) return
pendingVoiceAutoSubmitRef.current = false
setIsVoiceAutoSendPending(false)