fix: keep replies streaming while switching windows

Switching away from the desktop window and back could mark a reply that was
still being generated as "上次运行意外中断", freeze its output partway through,
and push the next message into the pending queue even though nothing had
actually failed. The conversation area also shifted on each switch because the
OpenCode runtime controls were unmounted and re-detected on every refresh.

Reading the conversation store no longer treats an in-progress reply as
interrupted; only startup recovery marks a run that an application exit truly
cut off. Runtime feature detection is now keyed on the resolved runtime scope,
so the OpenCode toolbar and manual Compact control stay in place across
refreshes. No request timeout or recovery behavior changed.

Release note: 修复来回切换桌面窗口时,正在生成的回复被误判为"上次运行意外中断"、输出中途停住、之后发送的消息只能进入待发送队列的问题。使用 OpenCode Runtime 时,会话区也不再因重复检测专属功能而抖动。
This commit is contained in:
mesalogo
2026-09-05 16:28:45 +08:00
parent dd8289fdef
commit 1f01c26e04
4 changed files with 316 additions and 49 deletions
+198
View File
@@ -6818,6 +6818,68 @@ describe('App', () => {
screen.getByText('聚焦后读到的最终回复')
).toBeInTheDocument()
})
it('keeps streaming through window switches while the store still reports the run', async () => {
const started = await startStreamingRun()
act(() => {
agentListener?.({
requestId: started.requestId,
type: 'text',
delta: '第一段回答'
})
})
expect(await screen.findByText('第一段回答')).toBeInTheDocument()
// The renderer keeps saving the streaming message, so a refresh reads
// it back as streaming. That must not be reported as an interruption.
vi.mocked(api.conversations.list).mockResolvedValue([
persistedSnapshot(started, {
state: 'streaming',
content: '第一段回答'
})
])
act(() => {
window.dispatchEvent(
new Event('visibilitychange', { bubbles: true })
)
window.dispatchEvent(new Event('focus'))
})
await waitFor(() =>
expect(api.conversations.list).toHaveBeenCalledTimes(2)
)
expect(
screen.queryByText('上次运行意外中断,可以重新发送问题')
).not.toBeInTheDocument()
expect(
screen.queryByRole('button', { name: '重新编辑并发送' })
).not.toBeInTheDocument()
expect(api.conversationQueue.ready).not.toHaveBeenCalled()
// The run is still live, so later deltas and the terminal event must
// still reach the message.
act(() => {
agentListener?.({
requestId: started.requestId,
type: 'text',
delta: ',第二段回答'
})
agentListener?.({
requestId: started.requestId,
type: 'done'
})
})
expect(
await screen.findByText('第一段回答,第二段回答')
).toBeInTheDocument()
await waitFor(() =>
expect(api.conversationQueue.ready).toHaveBeenCalledWith(
started.conversationId
)
)
expect(screen.getByLabelText('发送')).toBeInTheDocument()
})
})
it('retries a failed recovery with a new request ID and cleans up its listener', async () => {
@@ -7694,6 +7756,142 @@ describe('App', () => {
)
})
it('keeps native OpenCode controls mounted across conversation refreshes', async () => {
const conversationId = '00000000-0000-4000-8000-000000000761'
const settings = await api.settings.getRuntime()
vi.mocked(api.settings.getRuntime).mockResolvedValueOnce({
...settings,
provider: 'opencode',
opencodeEmbedded: true,
opencodeModelSource: { kind: 'platform' }
})
vi.mocked(api.agent.getStatus).mockResolvedValue({
id: 'opencode',
label: 'OpenCode',
available: true,
supportsToolExecution: true,
detail: 'Ready'
})
vi.mocked(
api.runtimeCustomization.getSettings
).mockResolvedValueOnce({
opencode: {},
continue: { presets: [] }
})
vi.mocked(
api.runtimeCustomization.getNativeSnapshot
).mockResolvedValueOnce({
provider: 'opencode',
available: true,
inventoryStatus: 'available',
detail: 'Ready',
agents: [
{
id: 'planner',
name: 'Planner',
description: 'Plan before editing',
mode: 'primary',
native: true,
hidden: false
}
],
tools: [],
toolsSupported: true,
commands: [],
lsp: [],
formatters: [],
mcpServers: [],
skills: [],
rules: [],
prompts: [],
resources: [],
resourcesSupported: true,
context: {
strategy: 'native',
manualCompact: true,
detail: 'OpenCode native context'
}
})
// Every read returns freshly parsed rows, so the conversation and its
// nested runtime selection change identity on each refresh even when the
// selection itself is unchanged. The store keeps the persisted timestamp
// in step with the local one, so the incoming copy wins each merge.
vi.mocked(api.conversations.list).mockImplementation(async () => [
{
id: conversationId,
projectId,
runtimeSelection: { provider: 'opencode' },
title: 'OpenCode 会话',
updatedAt: 1_775_000_000_000,
messages: [
{
id: '00000000-0000-4000-8000-000000000762',
role: 'assistant',
content: '已就绪',
createdAt: 1_775_000_000_000,
state: 'complete'
}
]
}
])
let conversationsChangedListener: (() => void) | undefined
vi.mocked(api.conversations.onChanged).mockImplementation(
(listener) => {
conversationsChangedListener = listener
return () => {
if (conversationsChangedListener === listener) {
conversationsChangedListener = undefined
}
}
}
)
render(<App />)
const runtimeToolbar = await screen.findByRole('group', {
name: 'OpenCode 专属功能'
})
expect(await screen.findByText('已就绪')).toBeInTheDocument()
await waitFor(() =>
expect(
api.runtimeCustomization.getNativeSnapshot
).toHaveBeenCalled()
)
vi.mocked(
api.runtimeCustomization.getNativeSnapshot
).mockClear()
// The resolved runtime scope is unchanged, so the runtime controls must
// not unmount and refetch, which visibly shifted the conversation
// layout on every window switch.
const refreshesBefore = vi.mocked(api.conversations.list).mock.calls
.length
act(() => {
conversationsChangedListener?.()
window.dispatchEvent(new Event('focus'))
})
await waitFor(() =>
expect(
vi.mocked(api.conversations.list).mock.calls.length
).toBeGreaterThan(refreshesBefore)
)
// Let the merged conversation render before asserting that the runtime
// scope stayed stable.
await act(async () => {
await Promise.resolve()
})
expect(
screen.getByRole('group', { name: 'OpenCode 专属功能' })
).toBe(runtimeToolbar)
expect(
screen.getByRole('button', { name: /OpenCode Runtime Agent/u })
).toBeInTheDocument()
expect(
api.runtimeCustomization.getNativeSnapshot
).not.toHaveBeenCalled()
})
it('fills editable Continue Prompts and submits the selected preset', async () => {
const presetId = '00000000-0000-4000-8000-000000000721'
const promptId = '00000000-0000-4000-8000-000000000722'
+24 -9
View File
@@ -3393,7 +3393,24 @@ function App(): React.JSX.Element {
)
: undefined
// Conversation refreshes replace the conversation object, so the runtime
// selection identity changes without the selection itself changing. Keying
// this effect on the resolved scope keeps the runtime controls mounted
// instead of clearing and refetching the snapshot on every refresh.
const runtimeNativeScopeKey = [
activeConversation?.remote ? 'remote' : 'local',
activeProjectUsesManagedSsh ? 'managed-ssh' : 'direct',
activeProjectId ?? '',
activeRuntimeSelectionKey
].join('\u0000')
const runtimeNativeScopeKeyRef = useRef<string | undefined>(undefined)
useEffect(() => {
if (runtimeNativeScopeKeyRef.current === runtimeNativeScopeKey) {
return
}
runtimeNativeScopeKeyRef.current = runtimeNativeScopeKey
const selection = activeRuntimeSelectionRef.current
const requestId = runtimeCustomizationRequestRef.current + 1
runtimeCustomizationRequestRef.current = requestId
queueMicrotask(() => {
@@ -3407,22 +3424,21 @@ function App(): React.JSX.Element {
setSelectedContinuePreset('')
})
if (
!activeRuntimeSelection ||
!selection ||
activeConversation?.remote ||
activeProjectUsesManagedSsh ||
(activeRuntimeSelection.provider !== 'opencode' &&
activeRuntimeSelection.provider !== 'continue')
(selection.provider !== 'opencode' &&
selection.provider !== 'continue')
) {
return
}
const provider = activeRuntimeSelection.provider
const provider = selection.provider
void Promise.all([
window.goodbuddy.runtimeCustomization.getSettings(),
window.goodbuddy.runtimeCustomization.getNativeSnapshot({
provider,
...('profileId' in activeRuntimeSelection &&
activeRuntimeSelection.profileId
? { profileId: activeRuntimeSelection.profileId }
...('profileId' in selection && selection.profileId
? { profileId: selection.profileId }
: {}),
...(activeProjectId ? { projectId: activeProjectId } : {})
})
@@ -3445,8 +3461,7 @@ function App(): React.JSX.Element {
activeConversation?.remote,
activeProjectUsesManagedSsh,
activeProjectId,
activeRuntimeSelection,
activeRuntimeSelectionKey
runtimeNativeScopeKey
])
const runtimeAgentOptions = useMemo<