loop.spec.ts 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. import { describe, expect, it } from 'vitest'
  2. import { Context } from 'cordis'
  3. import LlmService, { StreamChunk } from '@deepseek-ai/dsh-llm'
  4. import SessionStore, { TurnEndReason } from '@deepseek-ai/dsh-session'
  5. import SystemPrompt from '@deepseek-ai/dsh-system-prompt'
  6. import ToolRegistry, { defineTool } from '@deepseek-ai/dsh-tools'
  7. import AgentRegistry from '@deepseek-ai/dsh-agent'
  8. import AgentLoop, { LoopAgent } from '@deepseek-ai/dsh-agent-loop'
  9. import { MockAdapter, maxTokensResponse, textResponse, toolCallResponse } from './mock-adapter.ts'
  10. async function harness(adapter: MockAdapter) {
  11. const ctx = new Context()
  12. await ctx.plugin(LlmService)
  13. await ctx.plugin(SessionStore)
  14. await ctx.plugin(SystemPrompt)
  15. await ctx.plugin(ToolRegistry)
  16. await ctx.plugin(AgentRegistry)
  17. await ctx.plugin(AgentLoop, { agents: [] })
  18. ctx.llm.registerAdapter(['mock'], adapter)
  19. return ctx
  20. }
  21. /**
  22. * Wait for the agent's NEXT transition to idle. Always event-based: callers
  23. * invoke this right after send(), when the loop hasn't woken yet (status is
  24. * still 'idle' synchronously), so polling the current status would lie.
  25. */
  26. function waitForIdle(ctx: Context, agent: LoopAgent): Promise<void> {
  27. return new Promise((resolve) => {
  28. const dispose = ctx.on('agent/status', (subject, status) => {
  29. if (subject === agent && status === 'idle') {
  30. dispose()
  31. resolve()
  32. }
  33. })
  34. })
  35. }
  36. function send(agent: LoopAgent, text: string) {
  37. agent.send([{ type: 'text', text }])
  38. }
  39. describe('agent loop', () => {
  40. it('runs a simple turn: queued message → model → idle, with ordered events', async () => {
  41. const adapter = new MockAdapter([textResponse('hello there')])
  42. const ctx = await harness(adapter)
  43. const agent = ctx.agentLoop.create('a1', { model: 'mock' })
  44. const order: string[] = []
  45. for (const name of ['agent/turn-start', 'agent/step-start', 'agent/step-end', 'agent/turn-end'] as const) {
  46. ctx.on(name, () => void order.push(name))
  47. }
  48. send(agent, 'hi')
  49. await waitForIdle(ctx, agent)
  50. expect(order).toEqual(['agent/turn-start', 'agent/step-start', 'agent/step-end', 'agent/turn-end'])
  51. const types = agent.session.events.map(e => e.type)
  52. // turn/start opens the turn, THEN the queued user message is recorded inside
  53. // it (every event is turn-enclosed), then assembled message + usage.
  54. expect(types[0]).toBe('turn/start')
  55. expect(types[1]).toBe('user/message')
  56. expect(types).toContain('assistant/message')
  57. expect(types).toContain('usage')
  58. expect(types.at(-1)).toBe('turn/end')
  59. // derived history: user + assistant
  60. const messages = agent.session.deriveMessages()
  61. expect(messages.map(m => m.role)).toEqual(['user', 'assistant'])
  62. expect(messages[1]!.content).toEqual([{ type: 'text', text: 'hello there' }])
  63. })
  64. it('round-trips tool calls: model requests tool → executes → result in next request', async () => {
  65. const adapter = new MockAdapter([
  66. toolCallResponse('c1', 'echo', { text: 'ping' }, 'calling echo'),
  67. textResponse('done'),
  68. ])
  69. const ctx = await harness(adapter)
  70. ctx.tools.register(defineTool({
  71. name: 'echo',
  72. description: 'echo back',
  73. parameters: { text: { type: 'string' } },
  74. async execute(args) {
  75. return [{ type: 'text', text: `echo: ${args.text}` }]
  76. },
  77. }))
  78. const agent = ctx.agentLoop.create('a1', { model: 'mock' })
  79. send(agent, 'use the tool')
  80. await waitForIdle(ctx, agent)
  81. // two model calls happened (tool-call step, then final step)
  82. expect(adapter.requests).toHaveLength(2)
  83. // the second request's derived history contains the tool result
  84. const secondMessages = adapter.requests[1]!.messages
  85. const toolResultMessage = secondMessages.find(m =>
  86. m.content.some(b => b.type === 'tool-result'))
  87. expect(toolResultMessage).toBeDefined()
  88. const block = toolResultMessage!.content.find(b => b.type === 'tool-result')!
  89. expect(block).toMatchObject({ toolCallId: 'c1', isError: false })
  90. expect((block).content).toEqual([{ type: 'text', text: 'echo: ping' }])
  91. // session log records call + result
  92. const types = agent.session.events.map(e => e.type)
  93. expect(types).toContain('tool/call')
  94. expect(types).toContain('tool/result')
  95. })
  96. it('passes assembled system prompt and tool schemas into the request', async () => {
  97. const adapter = new MockAdapter([textResponse('ok')])
  98. const ctx = await harness(adapter)
  99. ctx.systemPrompt.section({ name: 'persona', order: 0, text: 'You are a test agent.' })
  100. ctx.tools.register(defineTool({
  101. name: 'noop',
  102. description: 'does nothing',
  103. parameters: {},
  104. async execute() {
  105. return []
  106. },
  107. }))
  108. const agent = ctx.agentLoop.create('a1', { model: 'mock', systemPrompt: 'Agent-specific suffix.' })
  109. send(agent, 'hi')
  110. await waitForIdle(ctx, agent)
  111. const request = adapter.requests[0]
  112. expect(request!.system).toBe('You are a test agent.\n\nAgent-specific suffix.')
  113. expect(request!.tools?.map(t => t.name)).toEqual(['noop'])
  114. })
  115. it('records raw chunks for replay and emits agent/stream-chunk', async () => {
  116. const adapter = new MockAdapter([textResponse('abc')])
  117. const ctx = await harness(adapter)
  118. const agent = ctx.agentLoop.create('a1', { model: 'mock' })
  119. const streamed: StreamChunk[] = []
  120. ctx.on('agent/stream-chunk', (_agent, _turn, _step, chunk) => void streamed.push(chunk))
  121. send(agent, 'hi')
  122. await waitForIdle(ctx, agent)
  123. const chunkEvents = agent.session.events.filter(e => e.type === 'assistant/chunk')
  124. // textResponse('abc') = block-start + 3 deltas + block-end + usage + finish = 7
  125. expect(chunkEvents).toHaveLength(7)
  126. expect(streamed).toHaveLength(7)
  127. // replay: chunk events alone re-assemble to the recorded assistant message
  128. const deltaText = chunkEvents
  129. .flatMap(e => e.type === 'assistant/chunk' ? [e.data.chunk] : [])
  130. .filter((c: StreamChunk): c is Extract<StreamChunk, { type: 'text-delta' }> => c.type === 'text-delta')
  131. .map(c => c.text)
  132. .join('')
  133. expect(deltaText).toBe('abc')
  134. })
  135. it('injects steering between steps and continues the turn', async () => {
  136. const adapter = new MockAdapter([
  137. toolCallResponse('c1', 'slow', {}),
  138. textResponse('addressed the steering'),
  139. ])
  140. const ctx = await harness(adapter)
  141. const agent = ctx.agentLoop.create('a1', { model: 'mock' })
  142. ctx.tools.register(defineTool({
  143. name: 'slow',
  144. description: '',
  145. parameters: {},
  146. async execute() {
  147. // steer while the turn is running (during tool execution)
  148. agent.steer([{ type: 'text', text: 'change of plans' }])
  149. return [{ type: 'text', text: 'tool done' }]
  150. },
  151. }))
  152. send(agent, 'start')
  153. await waitForIdle(ctx, agent)
  154. const types = agent.session.events.map(e => e.type)
  155. expect(types).toContain('steering/message')
  156. // steering recorded before the second step's request derived its history
  157. const steeringSeq = agent.session.events.find(e => e.type === 'steering/message')!.seq
  158. const secondStepStart = agent.session.events.filter(e => e.type === 'step/start')[1]
  159. expect(secondStepStart).toBeDefined()
  160. expect(steeringSeq).toBeLessThan(secondStepStart!.seq)
  161. // the second model request saw the steering content
  162. const secondRequest = adapter.requests[1]
  163. const flat = JSON.stringify(secondRequest!.messages)
  164. expect(flat).toContain('change of plans')
  165. })
  166. it('steering while idle behaves like send (starts a turn)', async () => {
  167. const adapter = new MockAdapter([textResponse('ok')])
  168. const ctx = await harness(adapter)
  169. const agent = ctx.agentLoop.create('a1', { model: 'mock' })
  170. agent.steer([{ type: 'text', text: 'hello' }])
  171. await waitForIdle(ctx, agent)
  172. expect(agent.session.events.some(e => e.type === 'user/message')).toBe(true)
  173. })
  174. it('inject() while idle wraps context in a one-shot turn, visible to the next request', async () => {
  175. const adapter = new MockAdapter([textResponse('ok')])
  176. const ctx = await harness(adapter)
  177. const agent = ctx.agentLoop.create('a1', { model: 'mock' })
  178. agent.inject([{ type: 'text', text: 'file changed: a.ts' }], { source: { kind: 'plugin', plugin: 'watcher' } })
  179. // The idle inject records a self-contained turn (turn/start → context/message
  180. // → turn/end) so the event stays turn-enclosed, but does NOT run the model.
  181. await new Promise(r => setTimeout(r, 20))
  182. expect(agent.status).toBe('idle')
  183. expect(adapter.requests).toHaveLength(0)
  184. const injectedTurn = agent.session.events.filter(e => e.type === 'turn/start')
  185. expect(injectedTurn).toHaveLength(1)
  186. const it0 = injectedTurn[0]!
  187. expect(it0.type === 'turn/start' && it0.data.trigger.kind).toBe('injection')
  188. expect(agent.session.events.at(-1)!.type).toBe('turn/end') // turn-enclosed
  189. send(agent, 'go')
  190. await waitForIdle(ctx, agent)
  191. const flat = JSON.stringify(adapter.requests[0]!.messages)
  192. expect(flat).toContain('file changed: a.ts')
  193. expect(flat).toContain('<context source=\\"plugin\\">')
  194. })
  195. it('inject() while running appends into the open turn (no extra synthetic turn)', async () => {
  196. const adapter = new MockAdapter([
  197. toolCallResponse('c1', 'noticer', {}, 'calling'),
  198. textResponse('done'),
  199. ])
  200. const ctx = await harness(adapter)
  201. const agent = ctx.agentLoop.create('a1', { model: 'mock' })
  202. // A tool that injects mid-execution: at this point the agent is running, so
  203. // inject must append the context/message into the ALREADY-open turn rather
  204. // than wrap it in its own one-shot turn.
  205. ctx.tools.register(defineTool({
  206. name: 'noticer',
  207. description: 'injects a notice',
  208. parameters: {},
  209. async execute() {
  210. agent.inject([{ type: 'text', text: 'mid-turn notice' }], { source: { kind: 'plugin', plugin: 'x' } })
  211. return [{ type: 'text', text: 'ok' }]
  212. },
  213. }))
  214. send(agent, 'go')
  215. await waitForIdle(ctx, agent)
  216. // Exactly ONE turn ran (no synthetic injection turn), and the mid-turn
  217. // context/message sits inside it.
  218. const turnStarts = agent.session.events.filter(e => e.type === 'turn/start')
  219. expect(turnStarts).toHaveLength(1)
  220. const ts0 = turnStarts[0]!
  221. expect(ts0.type === 'turn/start' && ts0.data.trigger.kind).toBe('message')
  222. expect(agent.session.events.some(e => e.type === 'context/message')).toBe(true)
  223. })
  224. it('agent/turn-continuation can force-continue (/loop pattern) and force-stop', async () => {
  225. // force-continue: model never calls tools, but a plugin forces 3 steps
  226. const adapter = new MockAdapter([
  227. textResponse('step 1'),
  228. textResponse('step 2'),
  229. textResponse('step 3'),
  230. ])
  231. const ctx = await harness(adapter)
  232. const agent = ctx.agentLoop.create('a1', { model: 'mock' })
  233. let steps = 0
  234. ctx.on('agent/step-end', () => void steps++)
  235. ctx.on('agent/turn-continuation', async (_agent, _turn, _defaultDecision, next) => {
  236. if (steps < 3) return true
  237. return next()
  238. })
  239. send(agent, 'go')
  240. await waitForIdle(ctx, agent)
  241. expect(steps).toBe(3)
  242. expect(adapter.requests).toHaveLength(3)
  243. })
  244. it('agent/turn-continuation can veto continuation despite tool calls (budget-guard pattern)', async () => {
  245. const adapter = new MockAdapter([toolCallResponse('c1', 'echo', { text: 'x' })])
  246. const ctx = await harness(adapter)
  247. ctx.tools.register(defineTool({
  248. name: 'echo',
  249. description: '',
  250. parameters: { text: { type: 'string' } },
  251. async execute(args) {
  252. return [{ type: 'text', text: String(args.text) }]
  253. },
  254. }))
  255. const agent = ctx.agentLoop.create('a1', { model: 'mock' })
  256. ctx.on('agent/turn-continuation', async () => false as const)
  257. send(agent, 'go')
  258. await waitForIdle(ctx, agent)
  259. // only one model call despite the tool call requesting a follow-up
  260. expect(adapter.requests).toHaveLength(1)
  261. // tool still executed before the decision
  262. expect(agent.session.events.some(e => e.type === 'tool/result')).toBe(true)
  263. })
  264. it('agent/request waterfall can rewrite the request (model-switch pattern)', async () => {
  265. const adapter = new MockAdapter([textResponse('ok')])
  266. const ctx = await harness(adapter)
  267. ctx.llm.registerAdapter(['other-model'], adapter)
  268. const agent = ctx.agentLoop.create('a1', { model: 'mock' })
  269. ctx.on('agent/request', async (_agent, _turn, _step, options, next) => {
  270. options.model = 'other-model'
  271. return next()
  272. })
  273. send(agent, 'hi')
  274. await waitForIdle(ctx, agent)
  275. expect(adapter.requests[0]!.model).toBe('other-model')
  276. })
  277. it('abort() mid-stream ends the turn with reason aborted', async () => {
  278. const adapter = new MockAdapter(['hang'])
  279. const ctx = await harness(adapter)
  280. const agent = ctx.agentLoop.create('a1', { model: 'mock' })
  281. const reasons: TurnEndReason[] = []
  282. ctx.on('agent/turn-end', (_agent, _turn, reason) => void reasons.push(reason))
  283. send(agent, 'go')
  284. // wait until the stream is hanging, then abort
  285. await new Promise(r => setTimeout(r, 30))
  286. expect(agent.status).toBe('running')
  287. agent.abort('user interrupt')
  288. await waitForIdle(ctx, agent)
  289. expect(reasons).toEqual([{ kind: 'aborted', reason: 'user interrupt' }])
  290. })
  291. it('surfaces max-tokens as the turn-end reason when the last step is cut off', async () => {
  292. // A single step that ends with a max-tokens finish (no tool calls): the
  293. // turn stops by default and ends max-tokens, not completed.
  294. const adapter = new MockAdapter([maxTokensResponse('truncat')])
  295. const ctx = await harness(adapter)
  296. const agent = ctx.agentLoop.create('a1', { model: 'mock' })
  297. const reasons: TurnEndReason[] = []
  298. ctx.on('agent/turn-end', (_agent, _turn, reason) => void reasons.push(reason))
  299. send(agent, 'go')
  300. await waitForIdle(ctx, agent)
  301. expect(adapter.requests).toHaveLength(1)
  302. expect(reasons).toEqual([{ kind: 'max-tokens' }])
  303. // and the reason is recorded in the log's turn/end event
  304. const turnEnd = agent.session.events.findLast(e => e.type === 'turn/end')
  305. expect(turnEnd!.data.reason).toEqual({ kind: 'max-tokens' })
  306. })
  307. it('a max-tokens step earlier in a turn still surfaces as max-tokens after a later completed step', async () => {
  308. // Step 1 is cut off (max-tokens, no tool calls → would stop by default), so
  309. // continuation must be FORCED to reach step 2 which finishes normally
  310. // (stop). The rule "any max-tokens step surfaces as max-tokens" means the
  311. // turn ends max-tokens even though the LAST step completed cleanly.
  312. const adapter = new MockAdapter([
  313. maxTokensResponse('first half'),
  314. textResponse('second half'),
  315. ])
  316. const ctx = await harness(adapter)
  317. const agent = ctx.agentLoop.create('a1', { model: 'mock' })
  318. let steps = 0
  319. ctx.on('agent/step-end', () => void steps++)
  320. // Force exactly one continuation (step 1 → step 2), then defer to default
  321. // (step 2 is a plain stop with no tool calls → stops).
  322. ctx.on('agent/turn-continuation', async (_agent, _turn, _defaultDecision, next) => {
  323. if (steps < 2) return true
  324. return next()
  325. })
  326. const reasons: TurnEndReason[] = []
  327. ctx.on('agent/turn-end', (_agent, _turn, reason) => void reasons.push(reason))
  328. send(agent, 'go')
  329. await waitForIdle(ctx, agent)
  330. expect(steps).toBe(2)
  331. expect(adapter.requests).toHaveLength(2)
  332. expect(reasons).toEqual([{ kind: 'max-tokens' }])
  333. })
  334. it('a completed step after no max-tokens keeps the turn completed (max-tokens does not leak across turns)', async () => {
  335. // Two consecutive turns: turn 1 is cut off (max-tokens), turn 2 is a clean
  336. // stop. The per-turn reason must be independent — turn 2 ends completed.
  337. const adapter = new MockAdapter([maxTokensResponse('cut'), textResponse('clean')])
  338. const ctx = await harness(adapter)
  339. const agent = ctx.agentLoop.create('a1', { model: 'mock' })
  340. const reasons: TurnEndReason[] = []
  341. ctx.on('agent/turn-end', (_agent, _turn, reason) => void reasons.push(reason))
  342. send(agent, 'first')
  343. await waitForIdle(ctx, agent)
  344. send(agent, 'second')
  345. await waitForIdle(ctx, agent)
  346. expect(reasons).toEqual([{ kind: 'max-tokens' }, { kind: 'completed' }])
  347. })
  348. it('chains queued messages into consecutive turns', async () => {
  349. const adapter = new MockAdapter([textResponse('first'), textResponse('second')])
  350. const ctx = await harness(adapter)
  351. const agent = ctx.agentLoop.create('a1', { model: 'mock' })
  352. const turns: number[] = []
  353. ctx.on('agent/turn-start', (_agent, turn) => void turns.push(turn))
  354. // queue two messages while idle — first starts turn 1 immediately;
  355. // queue the second during turn 1 via a stream-chunk hook
  356. let queued = false
  357. ctx.on('agent/stream-chunk', () => {
  358. if (!queued) {
  359. queued = true
  360. send(agent, 'second message')
  361. }
  362. })
  363. send(agent, 'first message')
  364. await waitForIdle(ctx, agent)
  365. expect(turns).toEqual([1, 2])
  366. expect(adapter.requests).toHaveLength(2)
  367. })
  368. it('awaits session/flush at turn end (persistence checkpoint)', async () => {
  369. const adapter = new MockAdapter([textResponse('ok')])
  370. const ctx = await harness(adapter)
  371. const agent = ctx.agentLoop.create('a1', { model: 'mock' })
  372. let flushed = 0
  373. let flushedBeforeIdle = false
  374. ctx.on('session/flush', async (session) => {
  375. await new Promise(r => setTimeout(r, 10))
  376. flushed++
  377. flushedBeforeIdle = agent.status !== 'idle'
  378. void session
  379. })
  380. send(agent, 'hi')
  381. await waitForIdle(ctx, agent)
  382. expect(flushed).toBe(1)
  383. expect(flushedBeforeIdle).toBe(true)
  384. })
  385. it('errors from the model surface as agent/error and end the turn', async () => {
  386. const adapter = new MockAdapter([]) // script exhausted → throws
  387. const ctx = await harness(adapter)
  388. const agent = ctx.agentLoop.create('a1', { model: 'mock' })
  389. const errors: Error[] = []
  390. const reasons: TurnEndReason[] = []
  391. ctx.on('agent/error', (_agent, _turn, _step, error) => void errors.push(error))
  392. ctx.on('agent/turn-end', (_agent, _turn, reason) => void reasons.push(reason))
  393. send(agent, 'hi')
  394. await waitForIdle(ctx, agent)
  395. expect(errors).toHaveLength(1)
  396. expect(errors[0]!.message).toContain('script exhausted')
  397. expect(reasons[0]).toMatchObject({ kind: 'error' })
  398. expect(agent.session.events.some(e => e.type === 'error')).toBe(true)
  399. })
  400. it('disposing the loop fiber mid-turn stops the loop (HMR safety)', async () => {
  401. const adapter = new MockAdapter(['hang'])
  402. const ctx = await harness(adapter)
  403. let agent!: LoopAgent
  404. const fiber = await ctx.plugin(Object.assign((inner: Context) => {
  405. agent = inner.agentLoop.create('scoped', { model: 'mock' })
  406. }, { inject: ['agentLoop'] }))
  407. expect(ctx.agents.get('scoped')).toBe(agent)
  408. send(agent, 'go')
  409. await new Promise(r => setTimeout(r, 30))
  410. expect(agent.status).toBe('running')
  411. await fiber.dispose()
  412. await agent.done
  413. expect(agent.status).toBe('disposed')
  414. expect(ctx.agents.get('scoped')).toBeUndefined()
  415. expect(() => { send(agent, 'too late') }).toThrow('disposed')
  416. })
  417. it('creates agents from config on startup', async () => {
  418. const adapter = new MockAdapter([textResponse('from config')])
  419. const ctx = new Context()
  420. await ctx.plugin(LlmService)
  421. await ctx.plugin(SessionStore)
  422. await ctx.plugin(SystemPrompt)
  423. await ctx.plugin(ToolRegistry)
  424. await ctx.plugin(AgentRegistry)
  425. await ctx.plugin(AgentLoop, {
  426. agents: [{ id: 'config-agent', model: 'mock', systemPrompt: 'Config prompt' }],
  427. })
  428. ctx.llm.registerAdapter(['mock'], adapter)
  429. const agent = ctx.agents.get('config-agent')! as LoopAgent
  430. expect(agent).toBeDefined()
  431. expect(agent.id).toBe('config-agent')
  432. expect(agent.options.model).toBe('mock')
  433. // the agent is alive: send triggers a turn
  434. send(agent, 'hi')
  435. await waitForIdle(ctx, agent)
  436. expect(adapter.requests).toHaveLength(1)
  437. })
  438. it('replays a session log into an identical derived history', async () => {
  439. const adapter = new MockAdapter([
  440. toolCallResponse('c1', 'echo', { text: 'x' }),
  441. textResponse('done'),
  442. ])
  443. const ctx = await harness(adapter)
  444. ctx.tools.register(defineTool({
  445. name: 'echo',
  446. description: '',
  447. parameters: { text: { type: 'string' } },
  448. async execute(args) {
  449. return [{ type: 'text', text: String(args.text) }]
  450. },
  451. }))
  452. const agent = ctx.agentLoop.create('a1', { model: 'mock' })
  453. send(agent, 'run')
  454. await waitForIdle(ctx, agent)
  455. const replayed = ctx.sessions.create('replayed', { seed: [...agent.session.events] })
  456. expect(replayed.deriveMessages()).toEqual(agent.session.deriveMessages())
  457. // event-by-event identity of types
  458. expect(replayed.events.map(e => e.type)).toEqual(
  459. agent.session.events.map(e => e.type))
  460. })
  461. })