cancel.spec.ts 43 KB

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