cancel.spec.ts 41 KB

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