fix: keep consecutive requests running and clear finished stop state

Wait for OpenCode session cancellation before reusing a conversation, and replace the session after unconfirmed cancellation. Preserve terminal message state when stale streaming snapshots arrive so finished conversations return to Send.

Validated with full production Electron UI and the configured real text model: queued requests complete, cancellation followed by immediate resend completes, and Stop clears. Focused tests: 362 passed; typecheck and targeted lint passed. Live dispatch counting limitations are recorded in the feature evidence.

Release note: Consecutive OpenCode requests no longer inherit a previous cancellation, and finished conversations no longer retain the Stop button after stale updates.
This commit is contained in:
mesalogo
2026-09-17 10:11:31 +08:00
parent 3639566e1b
commit 94852acf82
9 changed files with 325 additions and 10 deletions
@@ -49,6 +49,9 @@ it.skipIf(!existsSync(binaryPath))(
const gateway = new KnowledgeMcpGateway({} as never)
await gateway.start()
let openEventBodies = 0
let delayedAbort: Promise<void> | undefined
let abortStarted: (() => void) | undefined
let promptCount = 0
const runtime = new OpenCodeRuntime({
embedded: true, binaryPath: '', bundledBinaryPath: binaryPath,
configPath: '', defaultWorkspace: workspace, knowledgeGateway: gateway,
@@ -61,8 +64,14 @@ it.skipIf(!existsSync(binaryPath))(
createClient: (options) => createOpencodeClient({
...options,
fetch: async (request, init) => {
const response = await fetch(request, init)
const url = request instanceof Request ? request.url : request
const pathname = new URL(url).pathname
if (pathname.endsWith('/abort') && delayedAbort) {
abortStarted?.()
await delayedAbort
}
if (pathname.endsWith('/prompt_async')) promptCount++
const response = await fetch(request, init)
if (new URL(url).pathname !== '/event' || !response.body) return response
openEventBodies++
const reader = response.body.getReader()
@@ -130,6 +139,30 @@ it.skipIf(!existsSync(binaryPath))(
expect(run()).resolves.toBe('LIFECYCLE_OK')
])
await expect.poll(() => openEventBodies).toBe(0)
let releaseAbort!: () => void
delayedAbort = new Promise<void>((resolve) => { releaseAbort = resolve })
const abortPending = new Promise<void>((resolve) => { abortStarted = resolve })
const reusedConversation = crypto.randomUUID()
cancelledController = new AbortController()
const first = run(cancelledController, true, reusedConversation).catch((error: unknown) => error)
let second: Promise<string> | undefined
try {
await abortPending
const beforeResend = promptCount
second = run(new AbortController(), false, reusedConversation)
await new Promise((resolve) => setTimeout(resolve, 100))
const beforeAbortCompletes = promptCount
releaseAbort()
expect(await first).toMatchObject({ name: 'AbortError' })
await expect(second).resolves.toBe('LIFECYCLE_OK')
expect(beforeAbortCompletes).toBe(beforeResend)
expect(promptCount).toBe(beforeResend + 1)
await expect.poll(() => openEventBodies).toBe(0)
} finally {
releaseAbort()
await Promise.allSettled([first, second])
delayedAbort = undefined
}
const conversationId = crypto.randomUUID()
await expect(run(new AbortController(), false, conversationId)).resolves.toBe('LIFECYCLE_OK')
await expect(runtime.compactConversation({