loop.spec.ts 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  1. import { describe, expect, it } from 'vitest'
  2. import { Context } from 'cordis'
  3. import LlmService, { CallId, 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, { ReactLoopAgent } from '@deepseek-ai/dsh-agent-loop'
  9. import { MockAdapter, maxTokensResponse, textResponse, toolCallResponse } from './mock-adapter'
  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: ReactLoopAgent): 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: ReactLoopAgent, 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(adapter.requests[1]!.messages).toEqual([
  333. { role: 'user', content: [{ type: 'text', text: 'go' }] },
  334. { role: 'assistant', content: [{ type: 'text', text: 'first half' }] },
  335. ])
  336. expect(reasons).toEqual([{ kind: 'max-tokens' }])
  337. })
  338. it('a completed step after no max-tokens keeps the turn completed (max-tokens does not leak across turns)', async () => {
  339. // Two consecutive turns: turn 1 is cut off (max-tokens), turn 2 is a clean
  340. // stop. The per-turn reason must be independent — turn 2 ends completed.
  341. const adapter = new MockAdapter([maxTokensResponse('cut'), textResponse('clean')])
  342. const ctx = await harness(adapter)
  343. const agent = ctx.agentLoop.create('a1', { model: 'mock' })
  344. const reasons: TurnEndReason[] = []
  345. ctx.on('agent/turn-end', (_agent, _turn, reason) => void reasons.push(reason))
  346. send(agent, 'first')
  347. await waitForIdle(ctx, agent)
  348. send(agent, 'second')
  349. await waitForIdle(ctx, agent)
  350. expect(reasons).toEqual([{ kind: 'max-tokens' }, { kind: 'completed' }])
  351. })
  352. it('does not dispatch tool calls from a max-tokens-truncated step', async () => {
  353. const callId = CallId('c1')
  354. const adapter = new MockAdapter([[
  355. { type: 'block-start', index: 0, blockType: 'tool-call' },
  356. { type: 'tool-call-delta', index: 0, id: callId, name: 'echo', argumentsDelta: '{"text":"x"}' },
  357. { type: 'block-end', index: 0, block: { type: 'tool-call', id: callId, name: 'echo', arguments: '{"text":"x"}' } },
  358. { type: 'usage', usage: { inputTokens: 10, outputTokens: 5 } },
  359. { type: 'finish', reason: { kind: 'max-tokens' } },
  360. ]])
  361. const ctx = await harness(adapter)
  362. let executions = 0
  363. ctx.tools.register(defineTool({
  364. name: 'echo',
  365. description: '',
  366. parameters: { text: { type: 'string' } },
  367. async execute() {
  368. executions += 1
  369. return [{ type: 'text', text: 'should not run' }]
  370. },
  371. }))
  372. const agent = ctx.agentLoop.create('a1', { model: 'mock' })
  373. const reasons: TurnEndReason[] = []
  374. ctx.on('agent/turn-end', (_agent, _turn, reason) => void reasons.push(reason))
  375. send(agent, 'go')
  376. await waitForIdle(ctx, agent)
  377. expect(executions).toBe(0)
  378. expect(agent.session.events.some(e => e.type === 'tool/call')).toBe(false)
  379. expect(agent.session.deriveMessages()).toEqual([{ role: 'user', content: [{ type: 'text', text: 'go' }] }])
  380. expect(reasons).toEqual([{ kind: 'max-tokens' }])
  381. })
  382. it('keeps safe max-tokens assistant content while dropping truncated tool calls', async () => {
  383. const callId = CallId('c1')
  384. const adapter = new MockAdapter([[
  385. { type: 'block-start', index: 0, blockType: 'text' },
  386. { type: 'text-delta', index: 0, text: 'partial text' },
  387. { type: 'block-end', index: 0, block: { type: 'text', text: 'partial text' } },
  388. { type: 'block-start', index: 1, blockType: 'tool-call' },
  389. { type: 'tool-call-delta', index: 1, id: callId, name: 'echo', argumentsDelta: '{"text"' },
  390. { type: 'finish', reason: { kind: 'max-tokens' } },
  391. ]])
  392. const ctx = await harness(adapter)
  393. let stepResults = 0
  394. ctx.on('agent/step-result', async (_agent, _turn, _step, message, next) => {
  395. stepResults += 1
  396. expect(message.content).toEqual([{ type: 'text', text: 'partial text' }])
  397. return next()
  398. })
  399. const agent = ctx.agentLoop.create('a1', { model: 'mock' })
  400. send(agent, 'go')
  401. await waitForIdle(ctx, agent)
  402. expect(stepResults).toBe(1)
  403. expect(agent.session.events.some(e => e.type === 'tool/call')).toBe(false)
  404. expect(agent.session.deriveMessages()).toEqual([
  405. { role: 'user', content: [{ type: 'text', text: 'go' }] },
  406. { role: 'assistant', content: [{ type: 'text', text: 'partial text' }] },
  407. ])
  408. })
  409. it('stops the turn when agent/step-end listener failure has recorded an error', async () => {
  410. const adapter = new MockAdapter([
  411. toolCallResponse('c1', 'echo', { text: 'x' }),
  412. textResponse('should not run'),
  413. ])
  414. const ctx = await harness(adapter)
  415. ctx.tools.register(defineTool({
  416. name: 'echo',
  417. description: '',
  418. parameters: { text: { type: 'string' } },
  419. async execute(args) {
  420. return [{ type: 'text', text: String(args.text) }]
  421. },
  422. }))
  423. const agent = ctx.agentLoop.create('a1', { model: 'mock' })
  424. let threw = false
  425. ctx.on('agent/step-end', () => {
  426. if (!threw) { threw = true; throw new Error('bad step-end listener') }
  427. })
  428. send(agent, 'go')
  429. await waitForIdle(ctx, agent)
  430. expect(adapter.requests).toHaveLength(1)
  431. const turnEnd = agent.session.events.findLast(e => e.type === 'turn/end')
  432. expect(turnEnd?.type === 'turn/end' && turnEnd.data.reason.kind).toBe('error')
  433. })
  434. it('chains queued messages into consecutive turns', async () => {
  435. const adapter = new MockAdapter([textResponse('first'), textResponse('second')])
  436. const ctx = await harness(adapter)
  437. const agent = ctx.agentLoop.create('a1', { model: 'mock' })
  438. const turns: number[] = []
  439. ctx.on('agent/turn-start', (_agent, turn) => void turns.push(turn))
  440. // queue two messages while idle — first starts turn 1 immediately;
  441. // queue the second during turn 1 via a stream-chunk hook
  442. let queued = false
  443. ctx.on('agent/stream-chunk', () => {
  444. if (!queued) {
  445. queued = true
  446. send(agent, 'second message')
  447. }
  448. })
  449. send(agent, 'first message')
  450. await waitForIdle(ctx, agent)
  451. expect(turns).toEqual([1, 2])
  452. expect(adapter.requests).toHaveLength(2)
  453. })
  454. it('awaits session/flush at turn end (persistence checkpoint)', async () => {
  455. const adapter = new MockAdapter([textResponse('ok')])
  456. const ctx = await harness(adapter)
  457. const agent = ctx.agentLoop.create('a1', { model: 'mock' })
  458. let flushed = 0
  459. let flushedBeforeIdle = false
  460. ctx.on('session/flush', async (session) => {
  461. await new Promise(r => setTimeout(r, 10))
  462. flushed++
  463. flushedBeforeIdle = agent.status !== 'idle'
  464. void session
  465. })
  466. send(agent, 'hi')
  467. await waitForIdle(ctx, agent)
  468. expect(flushed).toBe(1)
  469. expect(flushedBeforeIdle).toBe(true)
  470. })
  471. it('errors from the model surface as agent/error and end the turn', async () => {
  472. const adapter = new MockAdapter([]) // script exhausted → throws
  473. const ctx = await harness(adapter)
  474. const agent = ctx.agentLoop.create('a1', { model: 'mock' })
  475. const errors: Error[] = []
  476. const reasons: TurnEndReason[] = []
  477. ctx.on('agent/error', (_agent, _turn, _step, error) => void errors.push(error))
  478. ctx.on('agent/turn-end', (_agent, _turn, reason) => void reasons.push(reason))
  479. send(agent, 'hi')
  480. await waitForIdle(ctx, agent)
  481. expect(errors).toHaveLength(1)
  482. expect(errors[0]!.message).toContain('script exhausted')
  483. expect(reasons[0]).toMatchObject({ kind: 'error' })
  484. expect(agent.session.events.some(e => e.type === 'error')).toBe(true)
  485. })
  486. it('disposing the loop fiber mid-turn stops the loop (HMR safety)', async () => {
  487. const adapter = new MockAdapter(['hang'])
  488. const ctx = await harness(adapter)
  489. let agent!: ReactLoopAgent
  490. const fiber = await ctx.plugin(Object.assign((inner: Context) => {
  491. agent = inner.agentLoop.create('scoped', { model: 'mock' })
  492. }, { inject: ['agentLoop'] }))
  493. expect(ctx.agents.get('scoped')).toBe(agent)
  494. send(agent, 'go')
  495. await new Promise(r => setTimeout(r, 30))
  496. expect(agent.status).toBe('running')
  497. await fiber.dispose()
  498. await agent.done
  499. expect(agent.status).toBe('disposed')
  500. expect(ctx.agents.get('scoped')).toBeUndefined()
  501. expect(() => { send(agent, 'too late') }).toThrow('disposed')
  502. })
  503. it('creates agents from config on startup', async () => {
  504. const adapter = new MockAdapter([textResponse('from config')])
  505. const ctx = new Context()
  506. await ctx.plugin(LlmService)
  507. await ctx.plugin(SessionStore)
  508. await ctx.plugin(SystemPrompt)
  509. await ctx.plugin(ToolRegistry)
  510. await ctx.plugin(AgentRegistry)
  511. await ctx.plugin(AgentLoop, {
  512. agents: [{ id: 'config-agent', model: 'mock', systemPrompt: 'Config prompt' }],
  513. })
  514. ctx.llm.registerAdapter(['mock'], adapter)
  515. const agent = ctx.agents.get('config-agent')! as ReactLoopAgent
  516. expect(agent).toBeDefined()
  517. expect(agent.id).toBe('config-agent')
  518. expect(agent.options.model).toBe('mock')
  519. // the agent is alive: send triggers a turn
  520. send(agent, 'hi')
  521. await waitForIdle(ctx, agent)
  522. expect(adapter.requests).toHaveLength(1)
  523. })
  524. it('replays a session log into an identical derived history', async () => {
  525. const adapter = new MockAdapter([
  526. toolCallResponse('c1', 'echo', { text: 'x' }),
  527. textResponse('done'),
  528. ])
  529. const ctx = await harness(adapter)
  530. ctx.tools.register(defineTool({
  531. name: 'echo',
  532. description: '',
  533. parameters: { text: { type: 'string' } },
  534. async execute(args) {
  535. return [{ type: 'text', text: String(args.text) }]
  536. },
  537. }))
  538. const agent = ctx.agentLoop.create('a1', { model: 'mock' })
  539. send(agent, 'run')
  540. await waitForIdle(ctx, agent)
  541. const replayed = ctx.sessions.create('replayed', { seed: [...agent.session.events] })
  542. expect(replayed.deriveMessages()).toEqual(agent.session.deriveMessages())
  543. // event-by-event identity of types
  544. expect(replayed.events.map(e => e.type)).toEqual(
  545. agent.session.events.map(e => e.type))
  546. })
  547. })