cancel.spec.ts 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. import { 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 'cordis'
  11. import LlmService 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 ToolRegistry, { 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 { MockAdapter, textResponse, toolCallResponse } from './mock-adapter.ts'
  18. function driverDone(agent: Agent): Promise<void> {
  19. return (agent as Agent & { done: Promise<void> }).done
  20. }
  21. async function harness(adapter: MockAdapter) {
  22. const ctx = new Context()
  23. await ctx.plugin(LlmService)
  24. await ctx.plugin(SessionStore)
  25. await ctx.plugin(SystemPrompt)
  26. await ctx.plugin(ToolRegistry)
  27. await ctx.plugin(AgentRegistry)
  28. await ctx.plugin(AgentLoop, { agents: [] })
  29. ctx.llm.registerAdapter(['mock'], adapter)
  30. return ctx
  31. }
  32. function send(agent: Agent, text: string) {
  33. agent.followup(createUserMessage({ content: [{ type: 'text', text }], source: { kind: 'user' } }))
  34. }
  35. /** Resolve on the agent's next idle transition (event-based, not status poll). */
  36. function waitForIdle(ctx: Context, agent: Agent): Promise<void> {
  37. return new Promise((resolve) => {
  38. const dispose = ctx.on('agent/status', (subject, status) => {
  39. if (subject === agent && status === 'idle') { dispose(); resolve() }
  40. })
  41. })
  42. }
  43. /** All user-message texts recorded in the log (to assert what actually ran). */
  44. function userTexts(agent: Agent): string[] {
  45. return agent.session.events
  46. .filter(e => e.type === 'user/message')
  47. .flatMap(e => e.type === 'user/message' ? e.data.content : [])
  48. .flatMap(b => b.type === 'text' ? [b.text] : [])
  49. }
  50. describe('Agent.cancel()', () => {
  51. it('cancel() on an idle agent with nothing queued is a no-op; the next prompt runs (F2 leak guard)', async () => {
  52. const adapter = new MockAdapter([textResponse('reply')])
  53. const ctx = await harness(adapter)
  54. const agent = ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  55. // The loop is parked at the idle wait with nothing queued. A cancel here must
  56. // NOT arm the marker — otherwise the next legitimate prompt would be dropped.
  57. agent.cancel({ kind: 'user' })
  58. send(agent, 'real prompt')
  59. await waitForIdle(ctx, agent)
  60. // The prompt ran: its user message is in the log and one turn completed.
  61. expect(userTexts(agent)).toEqual(['real prompt'])
  62. expect(agent.session.events.some(e => e.type === 'turn/end')).toBe(true)
  63. })
  64. it('cancel({ keepInbox: true }) does not restore work already claimed by a waking send', async () => {
  65. const adapter = new MockAdapter([textResponse('wake reply')])
  66. const ctx = await harness(adapter)
  67. const agent = ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  68. agent.followup(createUserMessage({
  69. content: [{ type: 'text', text: 'preserved' }],
  70. source: { kind: 'user' },
  71. }))
  72. // A waking send starts and claims synchronously, so keepInbox has no
  73. // pending item to preserve by the time this cancellation runs.
  74. agent.cancel({ kind: 'user' }, { keepInbox: true })
  75. expect(agent.session.events.some(event =>
  76. event.type === 'agent/inbox/spliced' && event.data.outcome === 'canceled')).toBe(false)
  77. await agent.whenIdle()
  78. expect(agent.inbox.nextTurn).toHaveLength(0)
  79. expect(userTexts(agent)).toEqual([])
  80. expect(adapter.requests).toHaveLength(0)
  81. expect(agent.session.events.findLast(event => event.type === 'turn/end')?.data.reason)
  82. .toEqual({ kind: 'aborted', reason: { kind: 'user' } })
  83. const idle = waitForIdle(ctx, agent)
  84. send(agent, 'wake it')
  85. await idle
  86. expect(userTexts(agent)).toEqual(['wake it'])
  87. expect(adapter.requests).toHaveLength(1)
  88. })
  89. it('cancel({ keepInbox: true }) parks queued work after an active turn aborts', async () => {
  90. const adapter = new MockAdapter([
  91. 'hang',
  92. textResponse('preserved reply'),
  93. textResponse('wake reply'),
  94. ])
  95. const ctx = await harness(adapter)
  96. const agent = ctx.agentLoop.create(SessionId('keep-after-abort'), { provider: 'mock', model: 'mock' })
  97. send(agent, 'active')
  98. await new Promise(resolve => setTimeout(resolve, 30))
  99. send(agent, 'preserved')
  100. agent.cancel({ kind: 'user' }, { keepInbox: true })
  101. await agent.whenIdle()
  102. expect(userTexts(agent)).toEqual(['active'])
  103. expect(agent.inbox.nextTurn).toHaveLength(1)
  104. expect(adapter.requests).toHaveLength(1)
  105. const idle = waitForIdle(ctx, agent)
  106. send(agent, 'wake it')
  107. await idle
  108. expect(userTexts(agent)).toEqual(['active', 'preserved', 'wake it'])
  109. expect(adapter.requests).toHaveLength(3)
  110. })
  111. it('cancel after waking send closes its synchronously opened turn without a step', async () => {
  112. const adapter = new MockAdapter([textResponse('should not run')])
  113. const ctx = await harness(adapter)
  114. const agent = ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  115. send(agent, 'drop me first')
  116. send(agent, 'drop me second')
  117. agent.cancel({ kind: 'user' })
  118. await new Promise(r => setTimeout(r, 30))
  119. expect(userTexts(agent)).toEqual([])
  120. expect(agent.session.events.filter(event => event.type === 'turn/start')).toHaveLength(1)
  121. expect(agent.session.events.filter(event => event.type === 'step/start')).toHaveLength(0)
  122. expect(agent.session.events.findLast(event => event.type === 'turn/end')?.data.reason)
  123. .toEqual({ kind: 'aborted', reason: { kind: 'user' } })
  124. expect(agent.status).toBe('idle')
  125. })
  126. it('disposal from the running notification drops queued work before turn start', async () => {
  127. const adapter = new MockAdapter([textResponse('should not run')])
  128. const ctx = await harness(adapter)
  129. const handle = await ctx.agents.create({
  130. sessionId: SessionId('dispose-running-session'),
  131. agentOptions: { provider: 'mock', model: 'mock' },
  132. })
  133. const agent = handle.agent
  134. const running = Promise.withResolvers<undefined>()
  135. let disposalDone: Promise<void> | undefined
  136. ctx.on('agent/status', (subject, status) => {
  137. if (subject !== agent || status !== 'running') return
  138. disposalDone = handle.dispose()
  139. running.resolve(undefined)
  140. })
  141. send(agent, 'drop before claim')
  142. await running.promise
  143. if (disposalDone === undefined) throw new Error('running listener did not start disposal')
  144. await disposalDone
  145. await driverDone(agent)
  146. expect(agent.status).toBe('idle')
  147. expect(agent.session.events.some(event => event.type === 'turn/start')).toBe(false)
  148. expect(userTexts(agent)).toEqual([])
  149. expect(adapter.requests).toHaveLength(0)
  150. })
  151. it('a whenIdle() waiter registered BEFORE a pre-step cancel resolves (F1 hang guard)', async () => {
  152. const adapter = new MockAdapter([textResponse('x')])
  153. const ctx = await harness(adapter)
  154. const agent = ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  155. // This waiter cannot rely on a running→idle transition because cancellation
  156. // drops the turn before it runs; the skip path must settle it directly.
  157. send(agent, 'q')
  158. const idle = agent.whenIdle()
  159. agent.cancel({ kind: 'user' })
  160. // Must resolve (not hang). A timeout makes the failure a clear test failure.
  161. await Promise.race([
  162. idle,
  163. new Promise((_r, reject) => setTimeout(() => { reject(new Error('whenIdle hung after pre-step cancel')) }, 1000)),
  164. ])
  165. expect(agent.status).toBe('idle')
  166. })
  167. it('idle-listener cancellation settles its waiter without cancelling later work', async () => {
  168. const adapter = new MockAdapter([textResponse('first reply'), textResponse('later reply')])
  169. const ctx = await harness(adapter)
  170. const agent = ctx.agentLoop.create(SessionId('idle-listener-cancel'), { provider: 'mock', model: 'mock' })
  171. const replacementRegistered = Promise.withResolvers<undefined>()
  172. let replacementObservation: Promise<{ status: string; requests: number; turns: number }> | undefined
  173. ctx.on('agent/status', (subject, status) => {
  174. if (subject !== agent || status !== 'idle' || replacementObservation !== undefined) return
  175. send(agent, 'cancelled replacement')
  176. replacementObservation = agent.whenIdle().then(() => ({
  177. status: agent.status,
  178. requests: adapter.requests.length,
  179. turns: agent.session.events.filter(event => event.type === 'turn/start').length,
  180. }))
  181. agent.cancel({ kind: 'user' })
  182. replacementRegistered.resolve(undefined)
  183. })
  184. send(agent, 'first')
  185. await replacementRegistered.promise
  186. if (replacementObservation === undefined) throw new Error('idle listener did not register replacement work')
  187. await expect(Promise.race([
  188. replacementObservation,
  189. new Promise((_resolve, reject) => setTimeout(() => { reject(new Error('whenIdle hung after idle-listener cancel')) }, 1000)),
  190. ])).resolves.toEqual({ status: 'idle', requests: 1, turns: 2 })
  191. const idle = waitForIdle(ctx, agent)
  192. send(agent, 'later')
  193. await idle
  194. expect(adapter.requests).toHaveLength(2)
  195. expect(userTexts(agent)).toEqual(['first', 'later'])
  196. })
  197. it('replacement work queued after idle-listener cancellation waits for another wakeup', async () => {
  198. const adapter = new MockAdapter([
  199. textResponse('first reply'),
  200. textResponse('replacement reply'),
  201. textResponse('wake reply'),
  202. ])
  203. const ctx = await harness(adapter)
  204. const agent = ctx.agentLoop.create(SessionId('idle-listener-post-cancel-send'), { provider: 'mock', model: 'mock' })
  205. const replacementRegistered = Promise.withResolvers<undefined>()
  206. let replacementIdle: Promise<void> | undefined
  207. ctx.on('agent/status', (subject, status) => {
  208. if (subject !== agent || status !== 'idle' || replacementIdle !== undefined) return
  209. send(agent, 'cancelled replacement')
  210. agent.cancel({ kind: 'user' })
  211. send(agent, 'surviving replacement')
  212. replacementIdle = agent.whenIdle()
  213. replacementRegistered.resolve(undefined)
  214. })
  215. send(agent, 'first')
  216. await replacementRegistered.promise
  217. if (replacementIdle === undefined) throw new Error('idle listener did not register replacement work')
  218. await replacementIdle
  219. expect(adapter.requests).toHaveLength(1)
  220. expect(userTexts(agent)).toEqual(['first'])
  221. expect(agent.inbox.nextTurn).toHaveLength(1)
  222. const idle = waitForIdle(ctx, agent)
  223. send(agent, 'wake it')
  224. await idle
  225. expect(adapter.requests).toHaveLength(3)
  226. expect(userTexts(agent)).toEqual(['first', 'surviving replacement', 'wake it'])
  227. })
  228. it('cancel() mid-step aborts the active turn and drops every queued tail item', async () => {
  229. const adapter = new MockAdapter(['hang'])
  230. const ctx = await harness(adapter)
  231. const agent = ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  232. const reasons: TurnEndReason[] = []
  233. ctx.on('session/event', (_s, event) => { if (event.type === 'turn/end') reasons.push(event.data.reason) })
  234. send(agent, 'go')
  235. await new Promise(r => setTimeout(r, 30))
  236. expect(agent.status).toBe('running')
  237. send(agent, 'queued tail')
  238. agent.cancel({ kind: 'user' })
  239. await waitForIdle(ctx, agent)
  240. expect(reasons).toEqual([{ kind: 'aborted', reason: { kind: 'user' } }])
  241. expect(userTexts(agent)).toEqual(['go'])
  242. expect(agent.session.events.filter(event => event.type === 'turn/start')).toHaveLength(1)
  243. expect(adapter.requests).toHaveLength(1)
  244. })
  245. it('cancel from an assistant/message observer skips execution but balances replay', async () => {
  246. const adapter = new MockAdapter([
  247. toolCallResponse('c1', 'danger', {}),
  248. textResponse('recovered after cancellation'),
  249. ])
  250. const ctx = await harness(adapter)
  251. let executions = 0
  252. ctx.tools.register(defineContentToolFixture({
  253. name: 'danger',
  254. description: 'must not run after cancellation',
  255. parameters: {},
  256. async execute() {
  257. executions += 1
  258. return [{ type: 'text', text: 'ran' }]
  259. },
  260. }))
  261. const agent = ctx.agentLoop.create(SessionId('cancel-after-assistant-message'), { provider: 'mock', model: 'mock' })
  262. const dispose = ctx.on('session/event', (session, event) => {
  263. if (session === agent.session && event.type === 'assistant/message') {
  264. agent.cancel({ kind: 'user' })
  265. }
  266. })
  267. const reasons: TurnEndReason[] = []
  268. ctx.on('session/event', (_session, event) => { if (event.type === 'turn/end') reasons.push(event.data.reason) })
  269. send(agent, 'go')
  270. await waitForIdle(ctx, agent)
  271. dispose()
  272. expect(executions).toBe(0)
  273. expect(reasons).toEqual([{ kind: 'aborted', reason: { kind: 'user' } }])
  274. const call = agent.session.events.find(event => event.type === 'tool/call')
  275. const result = agent.session.events.find(event => event.type === 'tool/result')
  276. expect(call?.type === 'tool/call' ? call.data.callId : undefined).toBe('c1')
  277. expect(result?.type === 'tool/result' ? result.data : undefined).toMatchObject({
  278. message: {
  279. source: { kind: 'tool', callId: 'c1' },
  280. content: [{ type: 'tool-result', toolCallId: 'c1', isError: true }],
  281. },
  282. error: { name: 'AbortError', code: TOOL_ABORTED_BEFORE_DISPATCH },
  283. })
  284. send(agent, 'continue safely')
  285. await waitForIdle(ctx, agent)
  286. const replayedResult = adapter.requests[1]!.messages
  287. .flatMap(message => message.content)
  288. .find(block => block.type === 'tool-result')
  289. expect(replayedResult).toMatchObject({ toolCallId: 'c1', isError: true })
  290. expect(reasons).toEqual([
  291. { kind: 'aborted', reason: { kind: 'user' } },
  292. { kind: 'completed' },
  293. ])
  294. })
  295. it('a prompt sent AFTER a cancelled turn settles runs normally (marker reset)', async () => {
  296. const adapter = new MockAdapter(['hang', textResponse('second reply')])
  297. const ctx = await harness(adapter)
  298. const agent = ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  299. // First turn hangs; cancel it mid-step.
  300. send(agent, 'first')
  301. await new Promise(r => setTimeout(r, 30))
  302. agent.cancel({ kind: 'user' })
  303. await waitForIdle(ctx, agent)
  304. // The marker must have been reset after the cancelled turn — a fresh prompt
  305. // runs to completion rather than being dropped by a stale marker.
  306. send(agent, 'second')
  307. await waitForIdle(ctx, agent)
  308. expect(userTexts(agent)).toContain('second')
  309. // The second turn completed (its reply was streamed).
  310. const reasons = agent.session.events.filter(e => e.type === 'turn/end')
  311. expect(reasons.length).toBe(2)
  312. })
  313. it('cancel from a synchronous step/start session-event listener drops the step (post-step-start window)', async () => {
  314. const adapter = new MockAdapter([textResponse('should not stream')])
  315. const ctx = await harness(adapter)
  316. const agent = ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  317. // A step/start session-event listener fires AFTER step/start is appended
  318. // (and after the pre-step seam), so cancelling there lands in the SECOND
  319. // cancel check (the one that must closeStep() to balance the already-open
  320. // step) — distinct from a turn-start cancel, caught before the step opens.
  321. let streamed = false
  322. ctx.on('session/event', (_s, event) => { if (event.type === 'assistant/chunk') streamed = true })
  323. const dispose = ctx.on('session/event', (session, event) => {
  324. if (session === agent.session && event.type === 'step/start') agent.cancel({ kind: 'user' })
  325. })
  326. const reasons: TurnEndReason[] = []
  327. ctx.on('session/event', (_s, event) => { if (event.type === 'turn/end') reasons.push(event.data.reason) })
  328. send(agent, 'go')
  329. await waitForIdle(ctx, agent)
  330. dispose()
  331. // No step streamed, the turn ended with the coarse aborted outcome, and the
  332. // log is balanced (the open step was closed by the cancel branch).
  333. expect(streamed).toBe(false)
  334. expect(reasons).toEqual([{ kind: 'aborted', reason: { kind: 'user' } }])
  335. const types = agent.session.events.map(e => e.type)
  336. expect(types.filter(t => t === 'step/start').length).toBe(types.filter(t => t === 'step/end').length)
  337. })
  338. it('disposal from a synchronous step/start session-event listener stops before adapter dispatch', async () => {
  339. const adapter = new MockAdapter([textResponse('should not stream')])
  340. const ctx = new Context()
  341. await ctx.plugin(LlmService)
  342. await ctx.plugin(SessionStore)
  343. await ctx.plugin(SystemPrompt)
  344. await ctx.plugin(ToolRegistry)
  345. await ctx.plugin(AgentRegistry)
  346. await ctx.plugin(AgentLoop, { agents: [] })
  347. ctx.llm.registerAdapter(['mock'], adapter)
  348. const handle = await ctx.agents.create({
  349. sessionId: SessionId('dispose-step-start-session'),
  350. agentOptions: { provider: 'mock', model: 'mock' },
  351. })
  352. const agent = handle.agent
  353. let disposalDone: Promise<void> | undefined
  354. let streamed = false
  355. ctx.on('session/event', (_s, event) => { if (event.type === 'assistant/chunk') streamed = true })
  356. ctx.on('session/event', (session, event) => {
  357. if (session === agent.session && event.type === 'step/start') disposalDone = handle.dispose()
  358. })
  359. send(agent, 'go')
  360. await disposalDone
  361. await driverDone(agent)
  362. expect(streamed).toBe(false)
  363. expect(adapter.requests).toHaveLength(0)
  364. expect(agent.session.events.some(e => e.type === 'turn/end')).toBe(false)
  365. const types = agent.session.events.map(e => e.type)
  366. expect(types.filter(t => t === 'step/start').length).toBe(types.filter(t => t === 'step/end').length)
  367. })
  368. it('cancel during the stopping window ends the turn aborted and runs no further step', async () => {
  369. const adapter = new MockAdapter([textResponse('one'), textResponse('two')])
  370. const ctx = await harness(adapter)
  371. const agent = ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  372. let steps = 0
  373. const reasons: TurnEndReason[] = []
  374. ctx.on('session/event', (_session, event) => {
  375. if (event.type === 'step/start') steps += 1
  376. if (event.type === 'turn/end') reasons.push(event.data.reason)
  377. })
  378. let cancelled = false
  379. ctx.on('agent/turn-stopping', (subject) => {
  380. if (subject === agent && !cancelled) {
  381. cancelled = true
  382. agent.cancel({ kind: 'user' })
  383. }
  384. })
  385. send(agent, 'go')
  386. await waitForIdle(ctx, agent)
  387. // Only ONE step ran (the second was cancelled in the stopping window),
  388. // and the shared turn signal classified the durable outcome as aborted.
  389. expect(steps).toBe(1)
  390. expect(reasons).toEqual([{ kind: 'aborted', reason: { kind: 'user' } }])
  391. })
  392. it('cancel from a synchronous agent/status(running) listener drops the turn (window 2)', async () => {
  393. const adapter = new MockAdapter([textResponse('should not run')])
  394. const ctx = await harness(adapter)
  395. const agent = ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  396. // `agent/status` is synchronous, so cancellation can land before the
  397. // durable turn-start commit and must drop the reserved work.
  398. let streamed = false
  399. ctx.on('session/event', (_s, event) => { if (event.type === 'assistant/chunk') streamed = true })
  400. const dispose = ctx.on('agent/status', (subject, status) => {
  401. if (subject === agent && status === 'running') agent.cancel({ kind: 'user' })
  402. })
  403. send(agent, 'go')
  404. await waitForIdle(ctx, agent)
  405. dispose()
  406. // No turn opened, no step streamed, and a later prompt still runs (the marker
  407. // was reset).
  408. expect(streamed).toBe(false)
  409. expect(agent.session.events.some(e => e.type === 'turn/start')).toBe(false)
  410. })
  411. it('a running-listener cancellation parks replacement work until another wakeup', async () => {
  412. const adapter = new MockAdapter([textResponse('A reply'), textResponse('B reply')])
  413. const ctx = await harness(adapter)
  414. const agent = ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  415. let replaced = false
  416. const dispose = ctx.on('agent/status', (subject, status) => {
  417. if (subject !== agent || status !== 'running' || replaced) return
  418. replaced = true
  419. agent.cancel({ kind: 'user' })
  420. send(agent, 'B')
  421. })
  422. send(agent, 'A')
  423. const idle = agent.whenIdle()
  424. await idle
  425. dispose()
  426. expect(userTexts(agent)).toEqual([])
  427. expect(agent.inbox.nextTurn).toHaveLength(1)
  428. const replacementIdle = waitForIdle(ctx, agent)
  429. send(agent, 'C')
  430. await replacementIdle
  431. expect(userTexts(agent)).toEqual(['B', 'C'])
  432. expect(agent.session.events.filter(event => event.type === 'turn/end')).toHaveLength(2)
  433. })
  434. it('a prompt queued during pre-step cancellation waits for another wakeup', async () => {
  435. const adapter = new MockAdapter([textResponse('A reply'), textResponse('B reply')])
  436. const ctx = await harness(adapter)
  437. const agent = ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  438. send(agent, 'A')
  439. const idle = agent.whenIdle()
  440. agent.cancel({ kind: 'user' })
  441. send(agent, 'B')
  442. await idle
  443. expect(userTexts(agent)).toEqual([])
  444. expect(agent.inbox.nextTurn).toHaveLength(1)
  445. const replacementIdle = waitForIdle(ctx, agent)
  446. send(agent, 'C')
  447. await replacementIdle
  448. expect(userTexts(agent)).toEqual(['B', 'C'])
  449. expect(agent.session.events.filter(event => event.type === 'turn/end')).toHaveLength(3)
  450. })
  451. it("cancel clears the turn's steering — it is not re-enqueued as a fresh turn", async () => {
  452. const adapter = new MockAdapter(['hang'])
  453. const ctx = await harness(adapter)
  454. const agent = ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  455. send(agent, 'go')
  456. await new Promise(r => setTimeout(r, 30))
  457. expect(agent.status).toBe('running')
  458. // Steer (joins the running turn's steering FIFO), then cancel: the steering
  459. // must be dropped, NOT re-enqueued as a new queued turn.
  460. agent.steer(createUserMessage({ content: [{ type: 'text', text: 'steer text' }], source: { kind: 'user' } }))
  461. agent.cancel({ kind: 'user' })
  462. await waitForIdle(ctx, agent)
  463. // After the cancelled turn settles, the agent is idle with NO follow-up turn
  464. // started from the dropped steering.
  465. await new Promise(r => setTimeout(r, 30))
  466. expect(agent.status).toBe('idle')
  467. const turnStarts = agent.session.events.filter(e => e.type === 'turn/start')
  468. expect(turnStarts.length).toBe(1) // only the original (cancelled) turn
  469. // The steering text was dropped — it never reached the log.
  470. const flat = agent.session.events
  471. .filter(e => e.type === 'user/message')
  472. .flatMap(e => e.data.content)
  473. .flatMap(b => b.type === 'text' ? [b.text] : [])
  474. expect(flat).not.toContain('steer text')
  475. })
  476. it('parks replacement work queued synchronously by an abort observer', async () => {
  477. const adapter = new MockAdapter([
  478. 'hang',
  479. textResponse('replacement reply'),
  480. textResponse('wake reply'),
  481. ])
  482. const ctx = await harness(adapter)
  483. const agent = ctx.agentLoop.create(SessionId('abort-observer-replacement'), { provider: 'mock', model: 'mock' })
  484. send(agent, 'original')
  485. await expect.poll(() => adapter.requests.length).toBe(1)
  486. const signal = adapter.requests[0]?.signal
  487. if (signal === undefined) throw new Error('model request omitted its turn signal')
  488. signal.addEventListener('abort', () => { send(agent, 'replacement') }, { once: true })
  489. const idle = agent.whenIdle()
  490. agent.cancel({ kind: 'user' })
  491. await Promise.race([
  492. idle,
  493. new Promise((_resolve, reject) => {
  494. setTimeout(() => {
  495. reject(new Error(`replacement did not settle: ${JSON.stringify({
  496. status: agent.status,
  497. requests: adapter.requests.length,
  498. users: userTexts(agent),
  499. events: agent.session.events.map(event => event.type),
  500. })}`))
  501. }, 1000)
  502. }),
  503. ])
  504. expect(adapter.requests).toHaveLength(1)
  505. expect(userTexts(agent)).toEqual(['original'])
  506. expect(agent.inbox.nextTurn).toHaveLength(1)
  507. const reasons = agent.session.events
  508. .filter(event => event.type === 'turn/end')
  509. .map(event => event.type === 'turn/end' ? event.data.reason : undefined)
  510. expect(reasons).toEqual([{ kind: 'aborted', reason: { kind: 'user' } }])
  511. const replacementIdle = waitForIdle(ctx, agent)
  512. send(agent, 'wake it')
  513. await replacementIdle
  514. expect(adapter.requests).toHaveLength(3)
  515. expect(userTexts(agent)).toEqual(['original', 'replacement', 'wake it'])
  516. })
  517. it('keeps the first typed cause for an active turn', async () => {
  518. const adapter = new MockAdapter(['hang'])
  519. const ctx = await harness(adapter)
  520. const agent = ctx.agentLoop.create(SessionId('typed-first-wins'), { provider: 'mock', model: 'mock' })
  521. const supplied: { kind: 'parent' | 'user' } = { kind: 'parent' }
  522. send(agent, 'go')
  523. await expect.poll(() => adapter.requests.length).toBe(1)
  524. agent.cancel(supplied)
  525. agent.cancel({ kind: 'user' })
  526. await waitForIdle(ctx, agent)
  527. const runtimeReason: unknown = adapter.requests[0]?.signal?.reason
  528. expect(runtimeReason).toEqual({ kind: 'parent' })
  529. expect(runtimeReason).toBe(supplied)
  530. const turnEnd = agent.session.events.findLast(event => event.type === 'turn/end')
  531. expect(turnEnd?.type === 'turn/end' && turnEnd.data.reason).toEqual({
  532. kind: 'aborted',
  533. reason: { kind: 'parent' },
  534. })
  535. })
  536. it('preserves the first user cancellation when lifecycle teardown races it', async () => {
  537. const adapter = new MockAdapter(['hang'])
  538. const ctx = await harness(adapter)
  539. const handle = await ctx.agents.create({
  540. sessionId: SessionId('cancel-dispose-race'),
  541. agentOptions: { provider: 'mock', model: 'mock' },
  542. })
  543. const { agent } = handle
  544. send(agent, 'go')
  545. await expect.poll(() => adapter.requests.length).toBe(1)
  546. agent.cancel({ kind: 'user' })
  547. await handle.dispose()
  548. const turnEnd = agent.session.events.findLast(event => event.type === 'turn/end')
  549. expect(turnEnd?.type === 'turn/end' && turnEnd.data.reason).toEqual({ kind: 'aborted', reason: { kind: 'user' } })
  550. })
  551. it.each([
  552. 'pre-step',
  553. 'system-prompt',
  554. 'request',
  555. 'stopping',
  556. 'tool',
  557. ] as const)('lets a cooperative %s boundary settle from the explicit turn signal', async (stage) => {
  558. const adapter = new MockAdapter(stage === 'tool'
  559. ? [toolCallResponse('blocked-tool', 'blocked', {})]
  560. : [textResponse('done')])
  561. const ctx = await harness(adapter)
  562. const agent = ctx.agentLoop.create(SessionId(`cooperative-${stage}`), { provider: 'mock', model: 'mock' })
  563. const started = Promise.withResolvers<undefined>()
  564. const blockUntilAbort = async (signal: AbortSignal): Promise<void> => {
  565. started.resolve(undefined)
  566. if (signal.aborted) return
  567. await new Promise<void>((resolve) => {
  568. signal.addEventListener('abort', () => { resolve() }, { once: true })
  569. })
  570. }
  571. switch (stage) {
  572. case 'pre-step':
  573. ctx.on('agent/pre-step', async (subject, _message, { signal }, next) => {
  574. if (subject === agent) await blockUntilAbort(signal)
  575. return next()
  576. })
  577. break
  578. case 'system-prompt':
  579. ctx.on('system-prompt/assemble', async (_assembly, context, next) => {
  580. if (context.agent === agent) {
  581. if (context.signal === undefined) throw new Error('turn assembly omitted its signal')
  582. await blockUntilAbort(context.signal)
  583. }
  584. return next()
  585. })
  586. break
  587. case 'request':
  588. ctx.on('agent/request', async (subject, _turn, _step, signal, next) => {
  589. if (subject === agent) await blockUntilAbort(signal)
  590. return next()
  591. })
  592. break
  593. case 'stopping':
  594. ctx.on('agent/turn-stopping', async (subject, _turn, signal) => {
  595. if (subject === agent) await blockUntilAbort(signal)
  596. })
  597. break
  598. case 'tool':
  599. ctx.tools.register(defineContentToolFixture({
  600. name: 'blocked',
  601. description: 'wait for cancellation',
  602. parameters: {},
  603. execute: async (_args, exec) => {
  604. if (exec.signal === undefined) throw new Error('tool execution omitted its signal')
  605. await blockUntilAbort(exec.signal)
  606. return [{ type: 'text', text: 'cancelled' }]
  607. },
  608. }))
  609. break
  610. }
  611. send(agent, 'go')
  612. await started.promise
  613. const idle = agent.whenIdle()
  614. agent.cancel({ kind: 'user' })
  615. await idle
  616. const turnEnd = agent.session.events.findLast(event => event.type === 'turn/end')
  617. expect(turnEnd?.type === 'turn/end' && turnEnd.data.reason)
  618. .toEqual({ kind: 'aborted', reason: { kind: 'user' } })
  619. await ctx.fiber.dispose()
  620. })
  621. })