fix: improve shutdown and project continuity
This commit is contained in:
@@ -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) => {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user