fix: improve shutdown and project continuity

This commit is contained in:
lofyer
2026-08-07 18:46:26 +08:00
parent 53d18e2b06
commit 954b42ef55
10 changed files with 245 additions and 34 deletions
+25
View File
@@ -135,6 +135,31 @@ describe('AgentRuntimeController', () => {
await controller.dispose()
})
it('forces runtime disposal when active work does not stop during shutdown', async () => {
const runtime = new TestRuntime(true)
const controller = new AgentRuntimeController(runtime, 1)
const stream = controller.run(
{
requestId: '1c608898-ecb7-4081-8174-2b6a52f53b12',
conversationId: 'conversation-shutdown',
prompt: 'test',
workMode: 'ask'
},
new AbortController().signal
)
const pendingEvent = stream.next()
await runtime.started
await controller.dispose()
expect(runtime.dispose).toHaveBeenCalledOnce()
runtime.finish()
await expect(pendingEvent).resolves.toMatchObject({
value: { type: 'text' }
})
await stream.return()
})
it.each(['ask', 'plan'] as const)(
'denies tool authorization in %s mode without prompting the user',
async (workMode) => {
+19 -4
View File
@@ -22,7 +22,10 @@ export class AgentRuntimeController implements AgentRuntime {
private replacementQueue: Promise<void> = Promise.resolve()
private closing = false
constructor(runtime: AgentRuntime) {
constructor(
runtime: AgentRuntime,
private readonly shutdownGraceMs = 2_000
) {
this.current = {
runtime,
activeRequests: 0,
@@ -212,9 +215,21 @@ export class AgentRuntimeController implements AgentRuntime {
async dispose(): Promise<void> {
this.closing = true
const operation = this.replacementQueue.then(() =>
this.retire(this.current)
)
const operation = this.replacementQueue.then(async () => {
const slot = this.current
const disposal = this.retire(slot)
if (slot.activeRequests === 0) {
return disposal
}
await Promise.race([
disposal,
new Promise<void>((resolve) =>
setTimeout(resolve, this.shutdownGraceMs)
)
])
await this.disposeSlot(slot)
return disposal
})
this.replacementQueue = operation.catch(() => undefined)
await operation
}