fix(session): close checkpoint cancellation races

This commit is contained in:
Yichen Jiang
2026-07-21 17:58:34 +08:00
parent f1e0410a5d
commit 380a94febb
15 changed files with 136 additions and 49 deletions

View File

@@ -343,6 +343,35 @@ describe('Agent.cancel()', () => {
expect(adapter.requests).toHaveLength(1)
})
it('preserves cancellation when disposal follows during post-step work', async () => {
const adapter = new MockAdapter([textResponse('done')])
const ctx = await harness(adapter)
const entered = Promise.withResolvers<undefined>()
const release = Promise.withResolvers<undefined>()
const handle = await ctx.agents.create({
sessionId: SessionId('cancel-then-dispose'),
agentOptions: { provider: 'mock', model: 'mock' },
})
const agent = handle.agent
ctx.on('agent/post-step', async (subject) => {
if (subject !== agent) return
entered.resolve(undefined)
await release.promise
})
send(agent, 'go')
await entered.promise
agent.cancel('user cancelled')
const disposed = handle.dispose()
release.resolve(undefined)
await disposed
const turnEnd = agent.session.events.findLast(event => event.type === 'turn/end')
expect(turnEnd?.type === 'turn/end' && turnEnd.data.reason)
.toEqual({ kind: 'aborted', reason: 'user cancelled' })
await ctx.fiber.dispose()
})
it('cancel() with no reason defaults to "cancelled" when aborting an in-flight step', async () => {
const adapter = new MockAdapter(['hang'])
const ctx = await harness(adapter)