cancel.spec.ts 31 KB

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