mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 21:04:23 +01:00
feat(walkthrough): stop an in-flight change walkthrough generation
Add DELETE /api/change-walkthroughs/:sessionId to cancel the generation for a session and source. The service removes the in-flight entry, persists the partial walkthrough, and aborts a per-generation signal threaded through model resolution and every model call. Cancellation is not recorded as a failure and leaves the remaining stops pending. A Stop button in the walkthrough chrome calls the new endpoint.
This commit is contained in:
1 parent
2d134d9de7
commit
0752ebf360
12 files changed
+334
-21
No files matched your search
@@ -387,4 +387,31 @@ describe('change walkthrough routes', () => {
|
||||
|
||||
expect(res.status).toBe(404)
|
||||
})
|
||||
|
||||
it('DELETE stops an in-flight generation', async () => {
|
||||
fake.setGenerateImpl(() => new Promise<string>(() => {}))
|
||||
|
||||
const postRes = await app.request(`/change-walkthroughs/${SESSION_ID}`, { method: 'POST' })
|
||||
expect(postRes.status).toBe(202)
|
||||
|
||||
const res = await app.request(`/change-walkthroughs/${SESSION_ID}`, { method: 'DELETE' })
|
||||
expect(res.status).toBe(204)
|
||||
|
||||
const getRes = await app.request(`/change-walkthroughs/${SESSION_ID}`)
|
||||
const state = (await getRes.json()) as { generating: boolean; error: unknown }
|
||||
expect(state.generating).toBe(false)
|
||||
expect(state.error).toBeNull()
|
||||
})
|
||||
|
||||
it('DELETE is a no-op when nothing is generating', async () => {
|
||||
const res = await app.request(`/change-walkthroughs/${SESSION_ID}`, { method: 'DELETE' })
|
||||
|
||||
expect(res.status).toBe(204)
|
||||
})
|
||||
|
||||
it('DELETE rejects an invalid source with 400', async () => {
|
||||
const res = await app.request(`/change-walkthroughs/${SESSION_ID}?source=branch:-x`, { method: 'DELETE' })
|
||||
|
||||
expect(res.status).toBe(400)
|
||||
})
|
||||
})
|
||||
@@ -25,6 +25,7 @@ import { stubLoadedModelCatalog } from '../helpers/stub-opencode-client'
|
||||
import { createCommittedRepo, createGitAuthService, createOrigin, cloneOrigin, git, uniqueName } from '../helpers/git-fixtures'
|
||||
import {
|
||||
ChangeWalkthroughService,
|
||||
WalkthroughCancelledError,
|
||||
buildWalkthroughInput,
|
||||
buildWalkthroughOutline,
|
||||
buildWalkthroughPlanPrompt,
|
||||
@@ -42,6 +43,25 @@ import { ChangeWalkthroughError } from '../../src/services/change-walkthrough-er
|
||||
|
||||
const SESSION_ID = 'ses_walkthrough'
|
||||
|
||||
function abortable<T>(promise: Promise<T>, signal?: AbortSignal): Promise<T> {
|
||||
if (!signal) return promise
|
||||
if (signal.aborted) return Promise.reject(new DOMException('Aborted', 'AbortError'))
|
||||
return new Promise<T>((resolve, reject) => {
|
||||
const onAbort = () => reject(new DOMException('Aborted', 'AbortError'))
|
||||
signal.addEventListener('abort', onAbort, { once: true })
|
||||
promise.then(
|
||||
(value) => {
|
||||
signal.removeEventListener('abort', onAbort)
|
||||
resolve(value)
|
||||
},
|
||||
(error) => {
|
||||
signal.removeEventListener('abort', onAbort)
|
||||
reject(error)
|
||||
},
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
function userMessage(id: string): SessionMessageInfo {
|
||||
return { id, type: 'user', time: { created: 0 }, text: id } as SessionMessageInfo
|
||||
}
|
||||
@@ -185,10 +205,10 @@ function createFakeClient(sessions: Record<string, FakeSession>) {
|
||||
}),
|
||||
},
|
||||
generate: {
|
||||
text: vi.fn(async (input: { prompt: string; model?: ModelRef }) => {
|
||||
text: vi.fn(async (input: { prompt: string; model?: ModelRef }, options?: { signal?: AbortSignal }) => {
|
||||
generateCalls.push(input.prompt)
|
||||
generateModels.push(input.model)
|
||||
return { text: await generateImpl(input.prompt) }
|
||||
return { text: await abortable(generateImpl(input.prompt), options?.signal) }
|
||||
}),
|
||||
},
|
||||
},
|
||||
@@ -2280,4 +2300,70 @@ describe('ChangeWalkthroughService', () => {
|
||||
expect(victimExplainStarted).toBe(1)
|
||||
})
|
||||
})
|
||||
|
||||
describe('cancelGeneration', () => {
|
||||
it('stops a pending generation without recording an error', async () => {
|
||||
fake.setGenerateImpl(() => new Promise<string>(() => {}))
|
||||
|
||||
const pending = service.generate(SESSION_ID, {})
|
||||
await vi.waitFor(() => expect(fake.generateCalls).toHaveLength(1))
|
||||
|
||||
expect(service.cancelGeneration(SESSION_ID)).toBe(true)
|
||||
await expect(pending).rejects.toBeInstanceOf(WalkthroughCancelledError)
|
||||
|
||||
const state = await service.getState(SESSION_ID)
|
||||
expect(state.generating).toBe(false)
|
||||
expect(state.error).toBeNull()
|
||||
})
|
||||
|
||||
it('returns false when nothing is generating', () => {
|
||||
expect(service.cancelGeneration(SESSION_ID)).toBe(false)
|
||||
})
|
||||
|
||||
it('keeps explained stops and persists the partial walkthrough', async () => {
|
||||
sessions[SESSION_ID]!.changes = LARGE_CHANGES
|
||||
fake.setGenerateImpl((prompt) => {
|
||||
if (prompt.includes(PLAN_MARKER)) {
|
||||
return Promise.resolve(LARGE_PLAN_REPLY)
|
||||
}
|
||||
return new Promise<string>(() => {})
|
||||
})
|
||||
|
||||
const pending = service.generate(SESSION_ID, {})
|
||||
|
||||
await vi.waitFor(async () => {
|
||||
const state = await service.getState(SESSION_ID)
|
||||
expect(state.generating).toBe(true)
|
||||
expect(state.walkthrough?.stops).toHaveLength(3)
|
||||
expect(state.walkthrough?.stops.every((stop) => stop.status === 'pending')).toBe(true)
|
||||
})
|
||||
|
||||
expect(service.cancelGeneration(SESSION_ID)).toBe(true)
|
||||
|
||||
await vi.waitFor(async () => {
|
||||
expect((await service.getState(SESSION_ID)).generating).toBe(false)
|
||||
})
|
||||
|
||||
const state = await service.getState(SESSION_ID)
|
||||
expect(state.error).toBeNull()
|
||||
const stored = getChangeWalkthrough(db, SESSION_ID, 'session')
|
||||
expect(stored).not.toBeNull()
|
||||
expect(stored!.stops.every((stop) => stop.status === 'pending')).toBe(true)
|
||||
|
||||
await pending
|
||||
})
|
||||
|
||||
it('aborts only the requested source', async () => {
|
||||
fake.setGenerateImpl(() => new Promise<string>(() => {}))
|
||||
|
||||
const pending = service.generate(SESSION_ID, {})
|
||||
await vi.waitFor(() => expect(fake.generateCalls).toHaveLength(1))
|
||||
|
||||
expect(service.cancelGeneration(SESSION_ID, { kind: 'staged' })).toBe(false)
|
||||
expect((await service.getState(SESSION_ID)).generating).toBe(true)
|
||||
|
||||
expect(service.cancelGeneration(SESSION_ID)).toBe(true)
|
||||
await expect(pending).rejects.toBeInstanceOf(WalkthroughCancelledError)
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user