cancel.spec.ts 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  1. import { ToolCallId, createUserMessage } from '@deepseek-ai/dsh-llm'
  2. /**
  3. * Tests for the queue-aware `Agent.cancel()` primitive. The default clears
  4. * queued and steering work, while `keepInbox` preserves pending input for a
  5. * later wake after the active turn reaches quiescence. The suite
  6. * covers every landing window plus signal reset and `whenIdle()` quiescence.
  7. * @module dsh-agent-loop/tests/cancel
  8. */
  9. import { describe, expect, it } from 'vitest'
  10. import { Context } from '@deepseek-ai/cordis'
  11. import LlmRuntime from '@deepseek-ai/dsh-llm'
  12. import SessionStore, { SessionId, TurnEndReason } from '@deepseek-ai/dsh-session'
  13. import SystemPrompt from '@deepseek-ai/dsh-system-prompt'
  14. import ToolRuntime, { defineContentToolFixture, TOOL_ABORTED_BEFORE_DISPATCH } from '@deepseek-ai/dsh-tools'
  15. import AgentRegistry, { type Agent } from '@deepseek-ai/dsh-agent'
  16. import AgentLoop from '@deepseek-ai/dsh-agent-loop'
  17. import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection'
  18. import { MockAdapter, textResponse, toolCallResponse } from './mock-adapter.ts'
  19. function driverDone(agent: Agent): Promise<void> {
  20. return (agent as Agent & { done: Promise<void> }).done
  21. }
  22. async function harness(adapter: MockAdapter) {
  23. const ctx = new Context()
  24. await ctx.plugin(LlmRuntime)
  25. await ctx.plugin(SessionStore)
  26. await ctx.plugin(SessionProjectionRegistry)
  27. await ctx.plugin(SystemPrompt)
  28. await ctx.plugin(ToolRuntime)
  29. await ctx.plugin(AgentRegistry)
  30. await ctx.plugin(AgentLoop, { agents: [] })
  31. ctx.llm.registerAdapter(['mock'], adapter)
  32. return ctx
  33. }
  34. function send(agent: Agent, text: string) {
  35. agent.followup(createUserMessage({ content: [{ type: 'text', text }], source: { kind: 'user' } }))
  36. }
  37. /** Resolve on the agent's next idle transition (event-based, not status poll). */
  38. function waitForIdle(ctx: Context, agent: Agent): Promise<void> {
  39. return new Promise((resolve) => {
  40. const dispose = ctx.on('agent/status', ({ agent: subject, status }) => {
  41. if (subject === agent && status === 'idle') { dispose(); resolve() }
  42. })
  43. })
  44. }
  45. /** All user-message texts recorded in the log (to assert what actually ran). */
  46. function userTexts(agent: Agent): string[] {
  47. return agent.session.events
  48. .filter(e => e.type === 'user/message')
  49. .flatMap(e => e.type === 'user/message' ? e.data.content : [])
  50. .flatMap(b => b.type === 'text' ? [b.text] : [])
  51. }
  52. describe('Agent.cancel()', () => {
  53. it('cancel() on an idle agent with nothing queued is a no-op; the next prompt runs (F2 leak guard)', async () => {
  54. const adapter = new MockAdapter([textResponse('reply')])
  55. const ctx = await harness(adapter)
  56. const agent = ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  57. // The loop is parked at the idle wait with nothing queued. A cancel here must
  58. // NOT arm the marker — otherwise the next legitimate prompt would be dropped.
  59. agent.cancel({ kind: 'user' })
  60. send(agent, 'real prompt')
  61. await waitForIdle(ctx, agent)
  62. // The prompt ran: its user message is in the log and one turn completed.
  63. expect(userTexts(agent)).toEqual(['real prompt'])
  64. expect(agent.session.events.some(e => e.type === 'turn/end')).toBe(true)
  65. })
  66. it('cancel({ keepInbox: true }) does not restore work already claimed by a waking send', async () => {
  67. const adapter = new MockAdapter([textResponse('wake reply')])
  68. const ctx = await harness(adapter)
  69. const agent = ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  70. agent.followup(createUserMessage({
  71. content: [{ type: 'text', text: 'preserved' }],
  72. source: { kind: 'user' },
  73. }))
  74. // A waking send starts and claims synchronously, so keepInbox has no
  75. // pending item to preserve by the time this cancellation runs.
  76. agent.cancel({ kind: 'user' }, { keepInbox: true })
  77. expect(agent.session.events.some(event =>
  78. event.type === 'agent/inbox/spliced' && event.data.outcome === 'canceled')).toBe(false)
  79. await agent.whenIdle()
  80. expect(agent.inbox.nextTurn).toHaveLength(0)
  81. expect(userTexts(agent)).toEqual([])
  82. expect(adapter.requests).toHaveLength(0)
  83. expect(agent.session.events.findLast(event => event.type === 'turn/end')?.data.reason)
  84. .toEqual({ kind: 'aborted', reason: { kind: 'user' } })
  85. const idle = waitForIdle(ctx, agent)
  86. send(agent, 'wake it')
  87. await idle
  88. expect(userTexts(agent)).toEqual(['wake it'])
  89. expect(adapter.requests).toHaveLength(1)
  90. })
  91. it('cancel({ keepInbox: true }) parks queued work after an active turn aborts', async () => {
  92. const adapter = new MockAdapter([
  93. 'hang',
  94. textResponse('preserved reply'),
  95. textResponse('wake reply'),
  96. ])
  97. const ctx = await harness(adapter)
  98. const agent = ctx.agentLoop.create(SessionId('keep-after-abort'), { provider: 'mock', model: 'mock' })
  99. send(agent, 'active')
  100. await new Promise(resolve => setTimeout(resolve, 30))
  101. send(agent, 'preserved')
  102. agent.cancel({ kind: 'user' }, { keepInbox: true })
  103. await agent.whenIdle()
  104. expect(userTexts(agent)).toEqual(['active'])
  105. expect(agent.inbox.nextTurn).toHaveLength(1)
  106. expect(adapter.requests).toHaveLength(1)
  107. const idle = waitForIdle(ctx, agent)
  108. send(agent, 'wake it')
  109. await idle
  110. expect(userTexts(agent)).toEqual(['active', 'preserved', 'wake it'])
  111. expect(adapter.requests).toHaveLength(3)
  112. })
  113. it('cancel({ keepInbox: true }) latches a waking send landing in the abort-to-idle window', async () => {
  114. const adapter = new MockAdapter(['hang', textResponse('B reply')])
  115. const ctx = await harness(adapter)
  116. const agent = ctx.agentLoop.create(SessionId('latch-window'), { provider: 'mock', model: 'mock' })
  117. send(agent, 'active')
  118. await new Promise(resolve => setTimeout(resolve, 30))
  119. // The abort signal is set but the driver has not converged to idle yet:
  120. // the waking send must be latched, not parked until another wake.
  121. agent.cancel({ kind: 'user' }, { keepInbox: true })
  122. send(agent, 'B')
  123. await agent.whenIdle()
  124. expect(userTexts(agent)).toEqual(['active', 'B'])
  125. expect(adapter.requests).toHaveLength(2)
  126. expect(agent.inbox.nextTurn).toHaveLength(0)
  127. expect(agent.session.events.filter(e => e.type === 'turn/end').map(e =>
  128. e.type === 'turn/end' ? e.data.reason : null)).toEqual([
  129. { kind: 'aborted', reason: { kind: 'user' } },
  130. { kind: 'completed' },
  131. ])
  132. })
  133. it('cancel() without keepInbox clears a latched wake alongside the inbox', async () => {
  134. const adapter = new MockAdapter(['hang', textResponse('C reply')])
  135. const ctx = await harness(adapter)
  136. const agent = ctx.agentLoop.create(SessionId('latch-cleared'), { provider: 'mock', model: 'mock' })
  137. send(agent, 'active')
  138. await new Promise(resolve => setTimeout(resolve, 30))
  139. agent.cancel({ kind: 'user' }, { keepInbox: true })
  140. send(agent, 'B') // latched behind the aborted activity
  141. agent.cancel({ kind: 'user' }) // drops the inbox and the latch with it
  142. await agent.whenIdle()
  143. expect(userTexts(agent)).toEqual(['active'])
  144. expect(agent.inbox.nextTurn).toHaveLength(0)
  145. expect(adapter.requests).toHaveLength(1)
  146. send(agent, 'C')
  147. await agent.whenIdle()
  148. expect(userTexts(agent)).toEqual(['active', 'C'])
  149. expect(adapter.requests).toHaveLength(2)
  150. })
  151. it('removing the latched wake before convergence suppresses the replay', async () => {
  152. const adapter = new MockAdapter(['hang'])
  153. const ctx = await harness(adapter)
  154. const agent = ctx.agentLoop.create(SessionId('removed-latched-wake'), { provider: 'mock', model: 'mock' })
  155. send(agent, 'active')
  156. await new Promise(resolve => setTimeout(resolve, 30))
  157. agent.cancel({ kind: 'user' }, { keepInbox: true })
  158. const steer = createUserMessage({ content: [{ type: 'text', text: 'steer me' }], source: { kind: 'user' } })
  159. agent.steer(steer) // latched behind the aborted activity
  160. agent.inbox.remove(steer.id) // the wake is retracted before convergence
  161. await agent.whenIdle()
  162. expect(userTexts(agent)).toEqual(['active'])
  163. expect(adapter.requests).toHaveLength(1)
  164. expect(agent.inbox.nextTurn).toHaveLength(0)
  165. expect(agent.status).toBe('idle')
  166. // No replay with nothing to run: the latched message is gone, so no
  167. // empty follow-up turn is recorded.
  168. expect(agent.session.events.filter(e => e.type === 'turn/start')).toHaveLength(1)
  169. })
  170. it('latches a wake arriving deep into a slow abort convergence', async () => {
  171. // The stream notices the abort only after 50ms, so the driver stays in
  172. // the abort-to-idle window long after `cancel()` returned: the wake must
  173. // be latched across the whole window, not just the same-tick case.
  174. const adapter = new MockAdapter(['hang-slow', textResponse('B reply')])
  175. const ctx = await harness(adapter)
  176. const agent = ctx.agentLoop.create(SessionId('slow-convergence'), { provider: 'mock', model: 'mock' })
  177. send(agent, 'A')
  178. await new Promise(resolve => setTimeout(resolve, 30))
  179. agent.cancel({ kind: 'user' }, { keepInbox: true })
  180. await new Promise(resolve => setTimeout(resolve, 10))
  181. send(agent, 'B')
  182. await agent.whenIdle()
  183. expect(userTexts(agent)).toEqual(['A', 'B'])
  184. expect(adapter.requests).toHaveLength(2)
  185. expect(agent.inbox.nextTurn).toHaveLength(0)
  186. })
  187. it('does not latch a wake landing after disposal begins', async () => {
  188. const adapter = new MockAdapter(['hang-slow', textResponse('late reply')])
  189. const ctx = await harness(adapter)
  190. const handle = await ctx.agents.create({
  191. sessionId: SessionId('dispose-window-wake'),
  192. agentOptions: { provider: 'mock', model: 'mock' },
  193. })
  194. const agent = handle.agent
  195. send(agent, 'active')
  196. await new Promise(resolve => setTimeout(resolve, 30))
  197. // Dispose cancels with `{ kind: 'disposed' }`; a wake landing in the
  198. // abort-to-idle window must not latch, so `whenIdle()` does not wait on
  199. // a model turn over the session being torn down.
  200. const disposal = handle.dispose()
  201. setTimeout(() => { send(agent, 'late wake') }, 10)
  202. await disposal
  203. expect(adapter.requests).toHaveLength(1)
  204. expect(userTexts(agent)).toEqual(['active'])
  205. })
  206. it('cancel after waking send closes its synchronously opened turn without a step', async () => {
  207. const adapter = new MockAdapter([textResponse('should not run')])
  208. const ctx = await harness(adapter)
  209. const agent = ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  210. send(agent, 'drop me first')
  211. send(agent, 'drop me second')
  212. agent.cancel({ kind: 'user' })
  213. await new Promise(r => setTimeout(r, 30))
  214. expect(userTexts(agent)).toEqual([])
  215. expect(agent.session.events.filter(event => event.type === 'turn/start')).toHaveLength(1)
  216. expect(agent.session.events.filter(event => event.type === 'step/start')).toHaveLength(0)
  217. expect(agent.session.events.findLast(event => event.type === 'turn/end')?.data.reason)
  218. .toEqual({ kind: 'aborted', reason: { kind: 'user' } })
  219. expect(agent.status).toBe('idle')
  220. })
  221. it('disposal from the running notification drops queued work before turn start', async () => {
  222. const adapter = new MockAdapter([textResponse('should not run')])
  223. const ctx = await harness(adapter)
  224. const handle = await ctx.agents.create({
  225. sessionId: SessionId('dispose-running-session'),
  226. agentOptions: { provider: 'mock', model: 'mock' },
  227. })
  228. const agent = handle.agent
  229. const running = Promise.withResolvers<undefined>()
  230. let disposalDone: Promise<void> | undefined
  231. ctx.on('agent/status', ({ agent: subject, status }) => {
  232. if (subject !== agent || status !== 'running') return
  233. disposalDone = handle.dispose()
  234. running.resolve(undefined)
  235. })
  236. send(agent, 'drop before claim')
  237. await running.promise
  238. if (disposalDone === undefined) throw new Error('running listener did not start disposal')
  239. await disposalDone
  240. await driverDone(agent)
  241. expect(agent.status).toBe('idle')
  242. expect(agent.session.events.some(event => event.type === 'turn/start')).toBe(false)
  243. expect(userTexts(agent)).toEqual([])
  244. expect(adapter.requests).toHaveLength(0)
  245. })
  246. it('a whenIdle() waiter registered BEFORE a pre-step cancel resolves (F1 hang guard)', async () => {
  247. const adapter = new MockAdapter([textResponse('x')])
  248. const ctx = await harness(adapter)
  249. const agent = ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  250. // This waiter cannot rely on a running→idle transition because cancellation
  251. // drops the turn before it runs; the skip path must settle it directly.
  252. send(agent, 'q')
  253. const idle = agent.whenIdle()
  254. agent.cancel({ kind: 'user' })
  255. // Must resolve (not hang). A timeout makes the failure a clear test failure.
  256. await Promise.race([
  257. idle,
  258. new Promise((_r, reject) => setTimeout(() => { reject(new Error('whenIdle hung after pre-step cancel')) }, 1000)),
  259. ])
  260. expect(agent.status).toBe('idle')
  261. })
  262. it('idle-listener cancellation settles its waiter without cancelling later work', async () => {
  263. const adapter = new MockAdapter([textResponse('first reply'), textResponse('later reply')])
  264. const ctx = await harness(adapter)
  265. const agent = ctx.agentLoop.create(SessionId('idle-listener-cancel'), { provider: 'mock', model: 'mock' })
  266. const replacementRegistered = Promise.withResolvers<undefined>()
  267. let replacementObservation: Promise<{ status: string; requests: number; turns: number }> | undefined
  268. ctx.on('agent/status', ({ agent: subject, status }) => {
  269. if (subject !== agent || status !== 'idle' || replacementObservation !== undefined) return
  270. send(agent, 'cancelled replacement')
  271. replacementObservation = agent.whenIdle().then(() => ({
  272. status: agent.status,
  273. requests: adapter.requests.length,
  274. turns: agent.session.events.filter(event => event.type === 'turn/start').length,
  275. }))
  276. agent.cancel({ kind: 'user' })
  277. replacementRegistered.resolve(undefined)
  278. })
  279. send(agent, 'first')
  280. await replacementRegistered.promise
  281. if (replacementObservation === undefined) throw new Error('idle listener did not register replacement work')
  282. await expect(Promise.race([
  283. replacementObservation,
  284. new Promise((_resolve, reject) => setTimeout(() => { reject(new Error('whenIdle hung after idle-listener cancel')) }, 1000)),
  285. ])).resolves.toEqual({ status: 'idle', requests: 1, turns: 2 })
  286. const idle = waitForIdle(ctx, agent)
  287. send(agent, 'later')
  288. await idle
  289. expect(adapter.requests).toHaveLength(2)
  290. expect(userTexts(agent)).toEqual(['first', 'later'])
  291. })
  292. it('replacement work queued after idle-listener cancellation replays at convergence', async () => {
  293. const adapter = new MockAdapter([
  294. textResponse('first reply'),
  295. textResponse('replacement reply'),
  296. textResponse('wake reply'),
  297. ])
  298. const ctx = await harness(adapter)
  299. const agent = ctx.agentLoop.create(SessionId('idle-listener-post-cancel-send'), { provider: 'mock', model: 'mock' })
  300. const replacementRegistered = Promise.withResolvers<undefined>()
  301. let replacementIdle: Promise<void> | undefined
  302. ctx.on('agent/status', ({ agent: subject, status }) => {
  303. if (subject !== agent || status !== 'idle' || replacementIdle !== undefined) return
  304. send(agent, 'cancelled replacement')
  305. agent.cancel({ kind: 'user' })
  306. send(agent, 'surviving replacement')
  307. replacementIdle = agent.whenIdle()
  308. replacementRegistered.resolve(undefined)
  309. })
  310. send(agent, 'first')
  311. await replacementRegistered.promise
  312. if (replacementIdle === undefined) throw new Error('idle listener did not register replacement work')
  313. await replacementIdle
  314. // The wake sent after the cancel fired is latched: the surviving
  315. // replacement runs at convergence without a third message.
  316. expect(adapter.requests).toHaveLength(2)
  317. expect(userTexts(agent)).toEqual(['first', 'surviving replacement'])
  318. expect(agent.inbox.nextTurn).toHaveLength(0)
  319. const idle = waitForIdle(ctx, agent)
  320. send(agent, 'wake it')
  321. await idle
  322. expect(adapter.requests).toHaveLength(3)
  323. expect(userTexts(agent)).toEqual(['first', 'surviving replacement', 'wake it'])
  324. })
  325. it('cancel() mid-step aborts the active turn and drops every queued tail item', async () => {
  326. const adapter = new MockAdapter(['hang'])
  327. const ctx = await harness(adapter)
  328. const agent = ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  329. const reasons: TurnEndReason[] = []
  330. ctx.on('session/event', (_s, event) => { if (event.type === 'turn/end') reasons.push(event.data.reason) })
  331. send(agent, 'go')
  332. await new Promise(r => setTimeout(r, 30))
  333. expect(agent.status).toBe('running')
  334. send(agent, 'queued tail')
  335. agent.cancel({ kind: 'user' })
  336. await waitForIdle(ctx, agent)
  337. expect(reasons).toEqual([{ kind: 'aborted', reason: { kind: 'user' } }])
  338. expect(userTexts(agent)).toEqual(['go'])
  339. expect(agent.session.events.filter(event => event.type === 'turn/start')).toHaveLength(1)
  340. expect(adapter.requests).toHaveLength(1)
  341. })
  342. it('cancel from an assistant/message observer skips execution but balances replay', async () => {
  343. const adapter = new MockAdapter([
  344. toolCallResponse('c1', 'danger', {}),
  345. textResponse('recovered after cancellation'),
  346. ])
  347. const ctx = await harness(adapter)
  348. let executions = 0
  349. ctx.tools.register(defineContentToolFixture({
  350. name: 'danger',
  351. description: 'must not run after cancellation',
  352. parameters: {},
  353. async execute() {
  354. executions += 1
  355. return [{ type: 'text', text: 'ran' }]
  356. },
  357. }))
  358. const agent = ctx.agentLoop.create(SessionId('cancel-after-assistant-message'), { provider: 'mock', model: 'mock' })
  359. const dispose = ctx.on('session/event', (session, event) => {
  360. if (session === agent.session && event.type === 'assistant/message') {
  361. agent.cancel({ kind: 'user' })
  362. }
  363. })
  364. const reasons: TurnEndReason[] = []
  365. ctx.on('session/event', (_session, event) => { if (event.type === 'turn/end') reasons.push(event.data.reason) })
  366. send(agent, 'go')
  367. await waitForIdle(ctx, agent)
  368. dispose()
  369. expect(executions).toBe(0)
  370. expect(reasons).toEqual([{ kind: 'aborted', reason: { kind: 'user' } }])
  371. const call = agent.session.events.find(event => event.type === 'tool/call')
  372. const result = agent.session.events.find(event => event.type === 'tool/result')
  373. expect(call?.type === 'tool/call' ? call.data.callId : undefined).toBe('c1')
  374. expect(result?.type === 'tool/result' ? result.data : undefined).toMatchObject({
  375. message: {
  376. source: { kind: 'tool', callId: 'c1' },
  377. content: [{ type: 'tool-result', toolCallId: 'c1', isError: true }],
  378. },
  379. error: { name: 'AbortError', code: TOOL_ABORTED_BEFORE_DISPATCH },
  380. })
  381. send(agent, 'continue safely')
  382. await waitForIdle(ctx, agent)
  383. const replayedResult = adapter.requests[1]!.messages
  384. .flatMap(message => message.content)
  385. .find(block => block.type === 'tool-result')
  386. expect(replayedResult).toMatchObject({ toolCallId: 'c1', isError: true })
  387. expect(reasons).toEqual([
  388. { kind: 'aborted', reason: { kind: 'user' } },
  389. { kind: 'completed' },
  390. ])
  391. })
  392. it('a prompt sent AFTER a cancelled turn settles runs normally (marker reset)', async () => {
  393. const adapter = new MockAdapter(['hang', textResponse('second reply')])
  394. const ctx = await harness(adapter)
  395. const agent = ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  396. // First turn hangs; cancel it mid-step.
  397. send(agent, 'first')
  398. await new Promise(r => setTimeout(r, 30))
  399. agent.cancel({ kind: 'user' })
  400. await waitForIdle(ctx, agent)
  401. // The marker must have been reset after the cancelled turn — a fresh prompt
  402. // runs to completion rather than being dropped by a stale marker.
  403. send(agent, 'second')
  404. await waitForIdle(ctx, agent)
  405. expect(userTexts(agent)).toContain('second')
  406. // The second turn completed (its reply was streamed).
  407. const reasons = agent.session.events.filter(e => e.type === 'turn/end')
  408. expect(reasons.length).toBe(2)
  409. })
  410. it('cancel mid-stream finalizes the streamed prefix onto the surface', async () => {
  411. const adapter = new MockAdapter(['hang', textResponse('after')])
  412. const ctx = await harness(adapter)
  413. const agent = ctx.agentLoop.create(SessionId('partial-finalize'), { provider: 'mock', model: 'mock' })
  414. send(agent, 'go')
  415. await new Promise(r => setTimeout(r, 30))
  416. agent.cancel({ kind: 'user' })
  417. await waitForIdle(ctx, agent)
  418. // The prefix the user watched stream is committed as the step's message,
  419. // carrying the truncation marker and citing exactly the chunk events that
  420. // delivered it.
  421. const message = agent.session.events.find(e => e.type === 'assistant/message')
  422. expect(message?.type === 'assistant/message' ? message.data.message.content : undefined)
  423. .toEqual([{ type: 'text', text: 'partial' }])
  424. expect(message?.type === 'assistant/message' ? message.data.interrupted : undefined).toBe(true)
  425. const chunkSeqs = agent.session.events.filter(e => e.type === 'assistant/chunk').map(e => e.seq)
  426. expect(message?.sourceEventSeqs).toEqual(chunkSeqs)
  427. const types = agent.session.events.map(e => e.type)
  428. expect(types.indexOf('assistant/message')).toBeLessThan(types.indexOf('step/end'))
  429. expect(types.indexOf('step/end')).toBeLessThan(types.indexOf('turn/end'))
  430. // The next request derives the finalized prefix: the model sees what the user saw.
  431. send(agent, 'continue')
  432. await waitForIdle(ctx, agent)
  433. const replayed = adapter.requests[1]!.messages
  434. .filter(m => m.role === 'assistant')
  435. .flatMap(m => m.content)
  436. .flatMap(b => b.type === 'text' ? [b.text] : [])
  437. expect(replayed).toContain('partial')
  438. })
  439. it('cancel during reasoning-only streaming finalizes the reasoning prefix', async () => {
  440. const adapter = new MockAdapter([{
  441. hangAfter: [
  442. { type: 'block-start', index: 0, blockType: 'reasoning' },
  443. { type: 'reasoning-delta', index: 0, text: 'thinking about it' },
  444. { type: 'usage', usage: { inputTokens: 7, outputTokens: 4 } },
  445. ],
  446. }])
  447. const ctx = await harness(adapter)
  448. const agent = ctx.agentLoop.create(SessionId('reasoning-finalize'), { provider: 'mock', model: 'mock' })
  449. send(agent, 'go')
  450. await new Promise(r => setTimeout(r, 30))
  451. agent.cancel({ kind: 'user' })
  452. await waitForIdle(ctx, agent)
  453. const message = agent.session.events.find(e => e.type === 'assistant/message')
  454. expect(message?.type === 'assistant/message' ? message.data.message.content : undefined)
  455. .toEqual([{ type: 'reasoning', text: 'thinking about it' }])
  456. // A usage chunk delivered before the cancel travels with the finalized prefix.
  457. expect(message?.type === 'assistant/message' ? message.data.usage : undefined)
  458. .toEqual({ inputTokens: 7, outputTokens: 4 })
  459. })
  460. it('cancel drops a half-streamed tool call and keeps the completed text before it', async () => {
  461. const adapter = new MockAdapter([{
  462. hangAfter: [
  463. { type: 'block-start', index: 0, blockType: 'text' },
  464. { type: 'text-delta', index: 0, text: 'reading the file' },
  465. { type: 'block-end', index: 0, block: { type: 'text', text: 'reading the file' } },
  466. { type: 'block-start', index: 1, blockType: 'tool-call' },
  467. { type: 'tool-call-delta', index: 1, id: ToolCallId('c1'), name: 'read', argumentsDelta: '{"pa' },
  468. ],
  469. }])
  470. const ctx = await harness(adapter)
  471. const agent = ctx.agentLoop.create(SessionId('tool-call-drop'), { provider: 'mock', model: 'mock' })
  472. send(agent, 'go')
  473. await new Promise(r => setTimeout(r, 30))
  474. agent.cancel({ kind: 'user' })
  475. await waitForIdle(ctx, agent)
  476. // The undispatched call is dropped whole — no dangling tool_use to pair.
  477. const message = agent.session.events.find(e => e.type === 'assistant/message')
  478. expect(message?.type === 'assistant/message' ? message.data.message.content : undefined)
  479. .toEqual([{ type: 'text', text: 'reading the file' }])
  480. expect(agent.session.events.some(e => e.type === 'tool/call')).toBe(false)
  481. })
  482. it('cancel during error recovery does not finalize the failed stream', async () => {
  483. const adapter = new MockAdapter([[
  484. { type: 'block-start', index: 0, blockType: 'text' },
  485. { type: 'text-delta', index: 0, text: 'doomed partial' },
  486. { type: 'finish', reason: { kind: 'error', failure: { message: 'boom', code: 'SERVER_ERROR' } } },
  487. ]])
  488. const ctx = await harness(adapter)
  489. const agent = ctx.agentLoop.create(SessionId('recovery-cancel'), { provider: 'mock', model: 'mock' })
  490. // Cancellation lands while agent/request-error is in flight — the window
  491. // dsh-llm-retry opens when its backoff waits after appending llm/retry.
  492. ctx.on('agent/request-error', async ({ agent: subject }) => {
  493. if (subject === agent) subject.cancel({ kind: 'user' })
  494. })
  495. send(agent, 'go')
  496. await waitForIdle(ctx, agent)
  497. // The failed stream's prefix stays off the surface: clients reset it on
  498. // retry, and provider failures commit nothing.
  499. expect(agent.session.events.some(e => e.type === 'assistant/message')).toBe(false)
  500. const end = agent.session.events.find(e => e.type === 'turn/end')
  501. expect(end?.type === 'turn/end' ? end.data.reason.kind : undefined).toBe('aborted')
  502. })
  503. it('retry discards the failed attempt; the final message cites only its own chunks', async () => {
  504. const adapter = new MockAdapter([
  505. [
  506. { type: 'block-start', index: 0, blockType: 'text' },
  507. { type: 'text-delta', index: 0, text: 'doomed partial' },
  508. { type: 'finish', reason: { kind: 'error', failure: { message: 'boom', code: 'SERVER_ERROR' } } },
  509. ],
  510. textResponse('recovered'),
  511. ])
  512. const ctx = await harness(adapter)
  513. const agent = ctx.agentLoop.create(SessionId('retry-discards-content'), { provider: 'mock', model: 'mock' })
  514. ctx.on('agent/request-error', async () => ({ kind: 'retry' as const }))
  515. send(agent, 'go')
  516. await waitForIdle(ctx, agent)
  517. const messages = agent.session.events.filter(e => e.type === 'assistant/message')
  518. expect(messages).toHaveLength(1)
  519. const message = messages[0]!
  520. expect(message.type === 'assistant/message' ? message.data.message.content : undefined)
  521. .toEqual([{ type: 'text', text: 'recovered' }])
  522. expect(message.type === 'assistant/message' ? message.data.interrupted : undefined).toBeUndefined()
  523. // The abandoned attempt's chunks stay out of the completion's source set.
  524. const doomedSeqs = agent.session.events
  525. .filter(e => e.type === 'assistant/chunk'
  526. && e.data.chunk.type === 'text-delta' && e.data.chunk.text === 'doomed partial')
  527. .map(e => e.seq)
  528. expect(doomedSeqs).toHaveLength(1)
  529. expect(message.sourceEventSeqs).not.toContain(doomedSeqs[0])
  530. })
  531. it('cancel before any visible content finalizes nothing', async () => {
  532. const adapter = new MockAdapter([{
  533. hangAfter: [
  534. { type: 'block-start', index: 0, blockType: 'tool-call' },
  535. { type: 'tool-call-delta', index: 0, id: ToolCallId('c1'), name: 'read', argumentsDelta: '{"pa' },
  536. ],
  537. }])
  538. const ctx = await harness(adapter)
  539. const agent = ctx.agentLoop.create(SessionId('nothing-to-finalize'), { provider: 'mock', model: 'mock' })
  540. send(agent, 'go')
  541. await new Promise(r => setTimeout(r, 30))
  542. agent.cancel({ kind: 'user' })
  543. await waitForIdle(ctx, agent)
  544. expect(agent.session.events.some(e => e.type === 'assistant/message')).toBe(false)
  545. })
  546. it('cancel from a synchronous step/start session-event listener drops the step (post-step-start window)', async () => {
  547. const adapter = new MockAdapter([textResponse('should not stream')])
  548. const ctx = await harness(adapter)
  549. const agent = ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  550. // A step/start session-event listener fires AFTER step/start is appended
  551. // (and after the pre-step extension point), so cancelling there lands in the SECOND
  552. // cancel check (the one that must closeStep() to balance the already-open
  553. // step) — distinct from a turn-start cancel, caught before the step opens.
  554. let streamed = false
  555. ctx.on('session/event', (_s, event) => { if (event.type === 'assistant/chunk') streamed = true })
  556. const dispose = ctx.on('session/event', (session, event) => {
  557. if (session === agent.session && event.type === 'step/start') agent.cancel({ kind: 'user' })
  558. })
  559. const reasons: TurnEndReason[] = []
  560. ctx.on('session/event', (_s, event) => { if (event.type === 'turn/end') reasons.push(event.data.reason) })
  561. send(agent, 'go')
  562. await waitForIdle(ctx, agent)
  563. dispose()
  564. // No step streamed, the turn ended with the coarse aborted outcome, and the
  565. // log is balanced (the open step was closed by the cancel branch).
  566. expect(streamed).toBe(false)
  567. expect(reasons).toEqual([{ kind: 'aborted', reason: { kind: 'user' } }])
  568. const types = agent.session.events.map(e => e.type)
  569. expect(types.filter(t => t === 'step/start').length).toBe(types.filter(t => t === 'step/end').length)
  570. })
  571. it('disposal from a synchronous step/start session-event listener stops before adapter dispatch', async () => {
  572. const adapter = new MockAdapter([textResponse('should not stream')])
  573. const ctx = new Context()
  574. await ctx.plugin(LlmRuntime)
  575. await ctx.plugin(SessionStore)
  576. await ctx.plugin(SessionProjectionRegistry)
  577. await ctx.plugin(SystemPrompt)
  578. await ctx.plugin(ToolRuntime)
  579. await ctx.plugin(AgentRegistry)
  580. await ctx.plugin(AgentLoop, { agents: [] })
  581. ctx.llm.registerAdapter(['mock'], adapter)
  582. const handle = await ctx.agents.create({
  583. sessionId: SessionId('dispose-step-start-session'),
  584. agentOptions: { provider: 'mock', model: 'mock' },
  585. })
  586. const agent = handle.agent
  587. let disposalDone: Promise<void> | undefined
  588. let streamed = false
  589. ctx.on('session/event', (_s, event) => { if (event.type === 'assistant/chunk') streamed = true })
  590. ctx.on('session/event', (session, event) => {
  591. if (session === agent.session && event.type === 'step/start') disposalDone = handle.dispose()
  592. })
  593. send(agent, 'go')
  594. await disposalDone
  595. await driverDone(agent)
  596. expect(streamed).toBe(false)
  597. expect(adapter.requests).toHaveLength(0)
  598. expect(agent.session.events.some(e => e.type === 'turn/end')).toBe(false)
  599. const types = agent.session.events.map(e => e.type)
  600. expect(types.filter(t => t === 'step/start').length).toBe(types.filter(t => t === 'step/end').length)
  601. })
  602. it('cancel during the stopping window ends the turn aborted and runs no further step', async () => {
  603. const adapter = new MockAdapter([textResponse('one'), textResponse('two')])
  604. const ctx = await harness(adapter)
  605. const agent = ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  606. let steps = 0
  607. const reasons: TurnEndReason[] = []
  608. ctx.on('session/event', (_session, event) => {
  609. if (event.type === 'step/start') steps += 1
  610. if (event.type === 'turn/end') reasons.push(event.data.reason)
  611. })
  612. let cancelled = false
  613. ctx.on('agent/turn-stopping', ({ agent: subject }) => {
  614. if (subject === agent && !cancelled) {
  615. cancelled = true
  616. agent.cancel({ kind: 'user' })
  617. }
  618. })
  619. send(agent, 'go')
  620. await waitForIdle(ctx, agent)
  621. // Only ONE step ran (the second was cancelled in the stopping window),
  622. // and the shared turn signal classified the durable outcome as aborted.
  623. expect(steps).toBe(1)
  624. expect(reasons).toEqual([{ kind: 'aborted', reason: { kind: 'user' } }])
  625. })
  626. it('cancel from a synchronous agent/status(running) listener drops the turn (window 2)', async () => {
  627. const adapter = new MockAdapter([textResponse('should not run')])
  628. const ctx = await harness(adapter)
  629. const agent = ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  630. // `agent/status` is synchronous, so cancellation can land before the
  631. // durable turn-start commit and must drop the reserved work.
  632. let streamed = false
  633. ctx.on('session/event', (_s, event) => { if (event.type === 'assistant/chunk') streamed = true })
  634. const dispose = ctx.on('agent/status', ({ agent: subject, status }) => {
  635. if (subject === agent && status === 'running') agent.cancel({ kind: 'user' })
  636. })
  637. send(agent, 'go')
  638. await waitForIdle(ctx, agent)
  639. dispose()
  640. // No turn opened, no step streamed, and a later prompt still runs (the marker
  641. // was reset).
  642. expect(streamed).toBe(false)
  643. expect(agent.session.events.some(e => e.type === 'turn/start')).toBe(false)
  644. })
  645. it('a running-listener cancellation replays replacement work at convergence', async () => {
  646. const adapter = new MockAdapter([textResponse('A reply'), textResponse('B reply')])
  647. const ctx = await harness(adapter)
  648. const agent = ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  649. let replaced = false
  650. const dispose = ctx.on('agent/status', ({ agent: subject, status }) => {
  651. if (subject !== agent || status !== 'running' || replaced) return
  652. replaced = true
  653. agent.cancel({ kind: 'user' })
  654. send(agent, 'B')
  655. })
  656. send(agent, 'A')
  657. const idle = agent.whenIdle()
  658. await idle
  659. dispose()
  660. // B's wake was latched behind the cancelled driver: it runs on its own.
  661. expect(userTexts(agent)).toEqual(['B'])
  662. expect(agent.inbox.nextTurn).toHaveLength(0)
  663. expect(adapter.requests).toHaveLength(1)
  664. const replacementIdle = waitForIdle(ctx, agent)
  665. send(agent, 'C')
  666. await replacementIdle
  667. expect(userTexts(agent)).toEqual(['B', 'C'])
  668. expect(adapter.requests).toHaveLength(2)
  669. expect(agent.session.events.filter(event => event.type === 'turn/end')).toHaveLength(2)
  670. })
  671. it('a prompt queued during pre-step cancellation replays at convergence', async () => {
  672. const adapter = new MockAdapter([textResponse('A reply'), textResponse('B reply')])
  673. const ctx = await harness(adapter)
  674. const agent = ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  675. send(agent, 'A')
  676. const idle = agent.whenIdle()
  677. agent.cancel({ kind: 'user' })
  678. send(agent, 'B')
  679. await idle
  680. expect(userTexts(agent)).toEqual(['B'])
  681. expect(agent.inbox.nextTurn).toHaveLength(0)
  682. expect(adapter.requests).toHaveLength(1)
  683. const replacementIdle = waitForIdle(ctx, agent)
  684. send(agent, 'C')
  685. await replacementIdle
  686. expect(userTexts(agent)).toEqual(['B', 'C'])
  687. expect(adapter.requests).toHaveLength(2)
  688. expect(agent.session.events.filter(event => event.type === 'turn/end')).toHaveLength(3)
  689. })
  690. it("cancel clears the turn's steering — it is not re-enqueued as a fresh turn", async () => {
  691. const adapter = new MockAdapter(['hang'])
  692. const ctx = await harness(adapter)
  693. const agent = ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  694. send(agent, 'go')
  695. await new Promise(r => setTimeout(r, 30))
  696. expect(agent.status).toBe('running')
  697. // Steer (joins the running turn's steering FIFO), then cancel: the steering
  698. // must be dropped, NOT re-enqueued as a new queued turn.
  699. agent.steer(createUserMessage({ content: [{ type: 'text', text: 'steer text' }], source: { kind: 'user' } }))
  700. agent.cancel({ kind: 'user' })
  701. await waitForIdle(ctx, agent)
  702. // After the cancelled turn settles, the agent is idle with NO follow-up turn
  703. // started from the dropped steering.
  704. await new Promise(r => setTimeout(r, 30))
  705. expect(agent.status).toBe('idle')
  706. const turnStarts = agent.session.events.filter(e => e.type === 'turn/start')
  707. expect(turnStarts.length).toBe(1) // only the original (cancelled) turn
  708. // The steering text was dropped — it never reached the log.
  709. const flat = agent.session.events
  710. .filter(e => e.type === 'user/message')
  711. .flatMap(e => e.data.content)
  712. .flatMap(b => b.type === 'text' ? [b.text] : [])
  713. expect(flat).not.toContain('steer text')
  714. })
  715. it('replays replacement work queued synchronously by an abort observer', async () => {
  716. const adapter = new MockAdapter([
  717. 'hang',
  718. textResponse('replacement reply'),
  719. textResponse('wake reply'),
  720. ])
  721. const ctx = await harness(adapter)
  722. const agent = ctx.agentLoop.create(SessionId('abort-observer-replacement'), { provider: 'mock', model: 'mock' })
  723. send(agent, 'original')
  724. await expect.poll(() => adapter.requests.length).toBe(1)
  725. const signal = adapter.requests[0]?.signal
  726. if (signal === undefined) throw new Error('model request omitted its turn signal')
  727. signal.addEventListener('abort', () => { send(agent, 'replacement') }, { once: true })
  728. const idle = agent.whenIdle()
  729. agent.cancel({ kind: 'user' })
  730. await Promise.race([
  731. idle,
  732. new Promise((_resolve, reject) => {
  733. setTimeout(() => {
  734. reject(new Error(`replacement did not settle: ${JSON.stringify({
  735. status: agent.status,
  736. requests: adapter.requests.length,
  737. users: userTexts(agent),
  738. events: agent.session.events.map(event => event.type),
  739. })}`))
  740. }, 1000)
  741. }),
  742. ])
  743. // The abort-observer wake was latched: replacement runs at convergence,
  744. // so the original turn is followed by a completed replacement turn.
  745. expect(adapter.requests).toHaveLength(2)
  746. expect(userTexts(agent)).toEqual(['original', 'replacement'])
  747. expect(agent.inbox.nextTurn).toHaveLength(0)
  748. const reasons = agent.session.events
  749. .filter(event => event.type === 'turn/end')
  750. .map(event => event.type === 'turn/end' ? event.data.reason : undefined)
  751. expect(reasons).toEqual([{ kind: 'aborted', reason: { kind: 'user' } }, { kind: 'completed' }])
  752. const replacementIdle = waitForIdle(ctx, agent)
  753. send(agent, 'wake it')
  754. await replacementIdle
  755. expect(adapter.requests).toHaveLength(3)
  756. expect(userTexts(agent)).toEqual(['original', 'replacement', 'wake it'])
  757. })
  758. it('keeps the first typed cause for an active turn', async () => {
  759. const adapter = new MockAdapter(['hang'])
  760. const ctx = await harness(adapter)
  761. const agent = ctx.agentLoop.create(SessionId('typed-first-wins'), { provider: 'mock', model: 'mock' })
  762. const supplied: { kind: 'parent' | 'user' } = { kind: 'parent' }
  763. send(agent, 'go')
  764. await expect.poll(() => adapter.requests.length).toBe(1)
  765. agent.cancel(supplied)
  766. agent.cancel({ kind: 'user' })
  767. await waitForIdle(ctx, agent)
  768. const runtimeReason: unknown = adapter.requests[0]?.signal?.reason
  769. expect(runtimeReason).toEqual({ kind: 'parent' })
  770. expect(runtimeReason).toBe(supplied)
  771. const turnEnd = agent.session.events.findLast(event => event.type === 'turn/end')
  772. expect(turnEnd?.type === 'turn/end' && turnEnd.data.reason).toEqual({
  773. kind: 'aborted',
  774. reason: { kind: 'parent' },
  775. })
  776. })
  777. it('preserves the first user cancellation when lifecycle teardown races it', async () => {
  778. const adapter = new MockAdapter(['hang'])
  779. const ctx = await harness(adapter)
  780. const handle = await ctx.agents.create({
  781. sessionId: SessionId('cancel-dispose-race'),
  782. agentOptions: { provider: 'mock', model: 'mock' },
  783. })
  784. const { agent } = handle
  785. send(agent, 'go')
  786. await expect.poll(() => adapter.requests.length).toBe(1)
  787. agent.cancel({ kind: 'user' })
  788. await handle.dispose()
  789. const turnEnd = agent.session.events.findLast(event => event.type === 'turn/end')
  790. expect(turnEnd?.type === 'turn/end' && turnEnd.data.reason).toEqual({ kind: 'aborted', reason: { kind: 'user' } })
  791. })
  792. it.each([
  793. 'pre-step',
  794. 'system-prompt',
  795. 'request',
  796. 'stopping',
  797. 'tool',
  798. ] as const)('lets a cooperative %s boundary settle from the explicit turn signal', async (stage) => {
  799. const adapter = new MockAdapter(stage === 'tool'
  800. ? [toolCallResponse('blocked-tool', 'blocked', {})]
  801. : [textResponse('done')])
  802. const ctx = await harness(adapter)
  803. const agent = ctx.agentLoop.create(SessionId(`cooperative-${stage}`), { provider: 'mock', model: 'mock' })
  804. const started = Promise.withResolvers<undefined>()
  805. const blockUntilAbort = async (signal: AbortSignal): Promise<void> => {
  806. started.resolve(undefined)
  807. if (signal.aborted) return
  808. await new Promise<void>((resolve) => {
  809. signal.addEventListener('abort', () => { resolve() }, { once: true })
  810. })
  811. }
  812. switch (stage) {
  813. case 'pre-step':
  814. ctx.on('agent/pre-step', async ({ agent: subject, signal }, next) => {
  815. if (subject === agent) await blockUntilAbort(signal)
  816. return next()
  817. })
  818. break
  819. case 'system-prompt':
  820. ctx.on('system-prompt/assemble', async (_assembly, context, next) => {
  821. if (context.agent === agent) {
  822. if (context.signal === undefined) throw new Error('turn assembly omitted its signal')
  823. await blockUntilAbort(context.signal)
  824. }
  825. return next()
  826. })
  827. break
  828. case 'request':
  829. ctx.on('agent/request', async ({ agent: subject, signal }, next) => {
  830. if (subject === agent) await blockUntilAbort(signal)
  831. return next()
  832. })
  833. break
  834. case 'stopping':
  835. ctx.on('agent/turn-stopping', async ({ agent: subject, signal }) => {
  836. if (subject === agent) await blockUntilAbort(signal)
  837. })
  838. break
  839. case 'tool':
  840. ctx.tools.register(defineContentToolFixture({
  841. name: 'blocked',
  842. description: 'wait for cancellation',
  843. parameters: {},
  844. execute: async (_args, exec) => {
  845. if (exec.signal === undefined) throw new Error('tool execution omitted its signal')
  846. await blockUntilAbort(exec.signal)
  847. return [{ type: 'text', text: 'cancelled' }]
  848. },
  849. }))
  850. break
  851. }
  852. send(agent, 'go')
  853. await started.promise
  854. const idle = agent.whenIdle()
  855. agent.cancel({ kind: 'user' })
  856. await idle
  857. const turnEnd = agent.session.events.findLast(event => event.type === 'turn/end')
  858. expect(turnEnd?.type === 'turn/end' && turnEnd.data.reason)
  859. .toEqual({ kind: 'aborted', reason: { kind: 'user' } })
  860. await ctx.fiber.dispose()
  861. })
  862. })