loop.spec.ts 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786
  1. import { describe, expect, it, vi } from 'vitest'
  2. import { Context } from '@deepseek-ai/cordis'
  3. import LlmRuntime, { createUserMessage, ToolCallId, LlmError, ReasoningEffortId, StreamChunk, expandAssistantStream } from '@deepseek-ai/dsh-llm'
  4. import SessionStore, { SessionId, TurnEndReason } from '@deepseek-ai/dsh-session'
  5. import SystemPrompt from '@deepseek-ai/dsh-system-prompt'
  6. import ToolRuntime, { defineContentToolFixture } from '@deepseek-ai/dsh-tools'
  7. import AgentRegistry, { type Agent, type AssistantStreamFrame } from '@deepseek-ai/dsh-agent'
  8. import AgentLoop from '@deepseek-ai/dsh-agent-loop'
  9. import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection'
  10. import { MockAdapter, maxTokensResponse, textResponse, toolCallResponse } from './mock-adapter.ts'
  11. function driverDone(agent: Agent): Promise<void> {
  12. return (agent as Agent & { done: Promise<void> }).done
  13. }
  14. async function harness(adapter: MockAdapter, persona = '') {
  15. const ctx = new Context()
  16. await ctx.plugin(LlmRuntime)
  17. await ctx.plugin(SessionStore)
  18. await ctx.plugin(SessionProjectionRegistry)
  19. await ctx.plugin(SystemPrompt, { persona })
  20. await ctx.plugin(ToolRuntime)
  21. await ctx.plugin(AgentRegistry)
  22. await ctx.plugin(AgentLoop, { agents: [] })
  23. ctx.llm.registerAdapter(['mock'], adapter)
  24. return ctx
  25. }
  26. /** Wait for the agent's next transition to idle after a waking send. */
  27. function waitForIdle(ctx: Context, agent: Agent): Promise<void> {
  28. return new Promise((resolve) => {
  29. const dispose = ctx.on('agent/status', ({ agent: subject, status }) => {
  30. if (subject === agent && status === 'idle') {
  31. dispose()
  32. resolve()
  33. }
  34. })
  35. })
  36. }
  37. function send(agent: Agent, text: string) {
  38. agent.followup(createUserMessage({ content: [{ type: 'text', text }], source: { kind: 'user' } }))
  39. }
  40. /** All user-message texts recorded in the log (to assert what actually ran). */
  41. function userTexts(agent: Agent): string[] {
  42. return agent.session.snapshotEvents()
  43. .filter(e => e.type === 'user/message')
  44. .flatMap(e => e.type === 'user/message' ? e.data.content : [])
  45. .flatMap(b => b.type === 'text' ? [b.text] : [])
  46. }
  47. describe('agent loop', () => {
  48. it('publishes one dense live attempt and commits one v2 message with the exact embedded stream', async () => {
  49. const ctx = await harness(new MockAdapter([textResponse('live')]))
  50. const agent = await ctx.agentLoop.create(SessionId('live-assistant-attempt'), {
  51. provider: 'mock',
  52. model: 'mock',
  53. })
  54. const frames: AssistantStreamFrame[] = []
  55. let committedAfterMessage = false
  56. ctx.on('agent/assistant-stream', ({ agent: subject, frame }) => {
  57. if (subject !== agent) return
  58. frames.push(frame)
  59. if (frame.type === 'end' && frame.outcome.kind === 'committed') {
  60. committedAfterMessage = agent.session.snapshotEvents().at(-1)?.type === 'assistant/message'
  61. }
  62. })
  63. send(agent, 'stream this')
  64. await waitForIdle(ctx, agent)
  65. expect(frames.at(0)?.type).toBe('start')
  66. const start = frames.at(0)
  67. if (start?.type === 'start') {
  68. expect(typeof start.startedTime).toBe('number')
  69. expect(Number.isSafeInteger(start.startedTime)).toBe(true)
  70. }
  71. expect(frames.at(-1)?.type).toBe('end')
  72. expect(frames.map(frame => frame.revision)).toEqual(
  73. frames.map((_frame, index) => index + 1),
  74. )
  75. const chunks = frames.filter(
  76. (frame): frame is Extract<AssistantStreamFrame, { type: 'chunk' }> => frame.type === 'chunk',
  77. )
  78. expect(chunks.map(frame => frame.index)).toEqual(chunks.map((_frame, index) => index))
  79. expect(agent.session.snapshotEvents().some(event => (event.type as string) === 'assistant/chunk')).toBe(false)
  80. const message = agent.session.snapshotEvents().findLast(event => event.type === 'assistant/message')
  81. expect(message?.type === 'assistant/message'
  82. ? expandAssistantStream(message.data.stream).map(member => member.chunk)
  83. : undefined).toStrictEqual(chunks.map(frame => frame.chunk))
  84. const end = frames.at(-1)
  85. expect(end).toMatchObject({
  86. type: 'end',
  87. index: chunks.length,
  88. outcome: { kind: 'committed', eventType: 'assistant/message', seq: message?.seq },
  89. })
  90. if (end?.type === 'end') {
  91. expect(committedAfterMessage).toBe(true)
  92. }
  93. })
  94. it('abandons the live row when durable Assistant settlement is rejected', async () => {
  95. const ctx = await harness(new MockAdapter([textResponse('cannot commit')]))
  96. const agent = await ctx.agentLoop.create(SessionId('abandoned-assistant-attempt'), {
  97. provider: 'mock',
  98. model: 'mock',
  99. })
  100. const frames: AssistantStreamFrame[] = []
  101. ctx.on('agent/assistant-stream', ({ agent: subject, frame }) => {
  102. if (subject === agent) frames.push(frame)
  103. })
  104. const append = agent.session.append.bind(agent.session)
  105. Object.defineProperty(agent.session, 'append', {
  106. configurable: true,
  107. value: (...args: Parameters<typeof agent.session.append>): ReturnType<typeof agent.session.append> => {
  108. if (args[0] === 'assistant/message') throw new Error('settlement rejected')
  109. return Reflect.apply(append, agent.session, args) as ReturnType<typeof agent.session.append>
  110. },
  111. })
  112. send(agent, 'stream this')
  113. await waitForIdle(ctx, agent)
  114. expect(frames.at(-1)).toMatchObject({
  115. type: 'end',
  116. outcome: { kind: 'abandoned' },
  117. })
  118. expect(agent.session.snapshotEvents().some(event => event.type === 'assistant/message')).toBe(false)
  119. })
  120. it('abandons a started live attempt when final block assembly fails', async () => {
  121. const chunks: StreamChunk[] = [
  122. { type: 'block-start', index: 0, blockType: 'external-block' } as unknown as StreamChunk,
  123. { type: 'finish', reason: { kind: 'stop' } },
  124. ]
  125. const ctx = await harness(new MockAdapter([chunks]))
  126. const agent = await ctx.agentLoop.create(SessionId('assistant-assembly-failure'), {
  127. provider: 'mock',
  128. model: 'mock',
  129. })
  130. const frames: AssistantStreamFrame[] = []
  131. ctx.on('agent/assistant-stream', ({ agent: subject, frame }) => {
  132. if (subject === agent) frames.push(frame)
  133. })
  134. send(agent, 'produce an incomplete external block')
  135. await waitForIdle(ctx, agent)
  136. expect(frames.map(frame => frame.revision)).toEqual([1, 2, 3, 4])
  137. expect(frames.at(-1)).toMatchObject({
  138. type: 'end',
  139. index: 2,
  140. outcome: { kind: 'abandoned' },
  141. })
  142. expect(agent.session.snapshotEvents().some(event => (
  143. event.type === 'assistant/message' || event.type === 'assistant/attempt'
  144. ))).toBe(false)
  145. expect(agent.session.snapshotEvents().at(-1)).toMatchObject({
  146. type: 'turn/end',
  147. data: { reason: { kind: 'error' } },
  148. })
  149. })
  150. it('settles a failed attempt before retrying with a new dense attempt', async () => {
  151. const failed: StreamChunk[] = [
  152. { type: 'usage', usage: { inputTokens: 1, outputTokens: 0 } },
  153. { type: 'finish', reason: { kind: 'error', failure: { message: 'retry', code: 'SERVER' } } },
  154. ]
  155. const ctx = await harness(new MockAdapter([failed, textResponse('recovered')]))
  156. const agent = await ctx.agentLoop.create(SessionId('assistant-retry-attempt'), {
  157. provider: 'mock',
  158. model: 'mock',
  159. })
  160. const frames: AssistantStreamFrame[] = []
  161. ctx.on('agent/assistant-stream', ({ agent: subject, frame }) => {
  162. if (subject === agent) frames.push(frame)
  163. })
  164. ctx.on('agent/request-error', async () => ({ kind: 'retry' as const }))
  165. send(agent, 'retry once')
  166. await waitForIdle(ctx, agent)
  167. expect(frames.map(frame => frame.revision)).toEqual(frames.map((_frame, index) => index + 1))
  168. expect(frames.filter(frame => frame.type === 'start')).toHaveLength(2)
  169. expect(frames.filter(frame => frame.type === 'end').map(frame => (
  170. frame.outcome.kind === 'committed' ? frame.outcome.eventType : frame.outcome.kind
  171. ))).toEqual(['assistant/attempt', 'assistant/message'])
  172. })
  173. it('does not emit an end frame when prepared dispatch throws before start', async () => {
  174. const ctx = await harness(new MockAdapter([]))
  175. const agent = await ctx.agentLoop.create(SessionId('assistant-dispatch-throw'), {
  176. provider: 'mock',
  177. model: 'mock',
  178. })
  179. const frames: AssistantStreamFrame[] = []
  180. ctx.on('agent/assistant-stream', ({ agent: subject, frame }) => {
  181. if (subject === agent) frames.push(frame)
  182. })
  183. const prepareCall = ctx.llm.prepareCall.bind(ctx.llm)
  184. vi.spyOn(ctx.llm, 'prepareCall').mockImplementation(async (config, signal) => {
  185. const prepared = await prepareCall(config, signal)
  186. return {
  187. ...prepared,
  188. stream: () => { throw new Error('dispatch failed before start') },
  189. }
  190. })
  191. send(agent, 'fail before streaming')
  192. await waitForIdle(ctx, agent)
  193. expect(frames).toEqual([])
  194. expect(agent.session.snapshotEvents().at(-1)).toMatchObject({
  195. type: 'turn/end',
  196. data: { reason: { kind: 'error' } },
  197. })
  198. })
  199. it('preserves the stream failure when its durable attempt settlement is also rejected', async () => {
  200. const streamFailure = new Error('provider transport failed')
  201. const settlementFailure = new Error('attempt settlement rejected')
  202. const ctx = await harness(new MockAdapter([textResponse('partial')]))
  203. const agent = await ctx.agentLoop.create(SessionId('double-assistant-failure'), {
  204. provider: 'mock',
  205. model: 'mock',
  206. })
  207. ctx.on('llm/stream', async function* (_options, next) {
  208. for await (const chunk of next()) {
  209. yield chunk
  210. if (chunk.type === 'text-delta') throw streamFailure
  211. }
  212. })
  213. const append = agent.session.append.bind(agent.session)
  214. Object.defineProperty(agent.session, 'append', {
  215. configurable: true,
  216. value: (...args: Parameters<typeof agent.session.append>): ReturnType<typeof agent.session.append> => {
  217. if (args[0] === 'assistant/attempt') throw settlementFailure
  218. return Reflect.apply(append, agent.session, args) as ReturnType<typeof agent.session.append>
  219. },
  220. })
  221. const errors: unknown[] = []
  222. ctx.on('agent/error', ({ agent: subject, error }) => {
  223. if (subject === agent) errors.push(error)
  224. })
  225. send(agent, 'stream this')
  226. await waitForIdle(ctx, agent)
  227. const combined = errors.find((error): error is AggregateError => error instanceof AggregateError)
  228. expect(combined?.errors).toEqual([streamFailure, settlementFailure])
  229. expect(combined?.cause).toBe(streamFailure)
  230. })
  231. it.each([0, -1, 1.5, Number.NaN, Number.MAX_SAFE_INTEGER + 1])(
  232. 'rejects invalid AgentOptions.maxTokens %s before publication',
  233. async (maxTokens) => {
  234. const ctx = await harness(new MockAdapter([]))
  235. await expect(ctx.agentLoop.create(
  236. SessionId('invalid-max-tokens'),
  237. { provider: 'mock', model: 'mock', maxTokens },
  238. )).rejects.toThrow('agent maxTokens must be a positive safe integer')
  239. expect(ctx.agents.list()).toEqual([])
  240. expect(ctx.sessions.list()).toEqual([])
  241. },
  242. )
  243. it('seeds a valid AgentOptions.maxTokens into the first model request', async () => {
  244. const adapter = new MockAdapter([textResponse('bounded')])
  245. const ctx = await harness(adapter)
  246. const agent = await ctx.agentLoop.create(
  247. SessionId('valid-max-tokens'),
  248. { provider: 'mock', model: 'mock', maxTokens: 256 },
  249. )
  250. send(agent, 'use the configured output limit')
  251. await waitForIdle(ctx, agent)
  252. expect(adapter.requests[0]?.maxTokens).toBe(256)
  253. })
  254. it('seeds an AgentOptions reasoning effort into the first model request', async () => {
  255. const effort = ReasoningEffortId('high')
  256. const adapter = new MockAdapter([textResponse('reasoned')], {
  257. efforts: [{ id: effort, name: 'High' }],
  258. defaultEffort: effort,
  259. })
  260. const ctx = await harness(adapter)
  261. const agent = await ctx.agentLoop.create(
  262. SessionId('configured-reasoning-effort'),
  263. { provider: 'mock', model: 'mock', reasoningEffort: effort },
  264. )
  265. send(agent, 'use the configured reasoning effort')
  266. await waitForIdle(ctx, agent)
  267. expect(adapter.requests[0]?.reasoningEffort).toBe(effort)
  268. })
  269. it('validates reasoning effort in declarative agent config', () => {
  270. const effort = ReasoningEffortId('high')
  271. expect(AgentLoop.Config({
  272. agents: [{ id: 'configured-agent', reasoningEffort: effort }],
  273. }).agents[0]?.reasoningEffort).toBe(effort)
  274. expect(() => AgentLoop.Config({
  275. agents: [{ id: 'configured-agent', reasoningEffort: ReasoningEffortId('') }],
  276. })).toThrow()
  277. })
  278. it('rejects concurrent maintenance while one job owns the Agent', async () => {
  279. const ctx = await harness(new MockAdapter([textResponse('unused')]))
  280. const agent = await ctx.agentLoop.create(SessionId('concurrent-maintenance'), {
  281. provider: 'mock', model: 'mock',
  282. })
  283. const started = Promise.withResolvers<undefined>()
  284. const finish = Promise.withResolvers<undefined>()
  285. const active = agent.runMaintenance(async () => {
  286. started.resolve(undefined)
  287. await finish.promise
  288. })
  289. await started.promise
  290. expect(() => agent.runMaintenance(async () => {})).toThrow('already has active work')
  291. finish.resolve(undefined)
  292. await active
  293. })
  294. it('cancels queued wakeup work together with an active maintenance task', async () => {
  295. const adapter = new MockAdapter([textResponse('park reply')])
  296. const ctx = await harness(adapter)
  297. const agent = await ctx.agentLoop.create(SessionId('cancel-maintenance-wakeup'), {
  298. provider: 'mock',
  299. model: 'mock',
  300. })
  301. const started = Promise.withResolvers<undefined>()
  302. const maintenance = agent.runMaintenance(async (signal) => {
  303. started.resolve(undefined)
  304. await new Promise<void>((_resolve, reject) => {
  305. signal.addEventListener('abort', () => {
  306. reject(new Error('maintenance aborted', { cause: signal.reason }))
  307. }, { once: true })
  308. })
  309. })
  310. await started.promise
  311. send(agent, 'discard this wakeup') // latched behind the live maintenance task
  312. agent.cancel({ kind: 'user' }) // drops the queue and the latch, aborts maintenance
  313. send(agent, 'park after cancellation') // newer intent: re-latched, replays at convergence
  314. await expect(maintenance).rejects.toThrow('maintenance aborted')
  315. await agent.whenIdle()
  316. // The pre-cancel wakeup is gone; the post-cancel wake replays at convergence.
  317. expect(userTexts(agent)).toEqual(['park after cancellation'])
  318. expect(agent.inbox.nextTurn).toHaveLength(0)
  319. expect(adapter.requests).toHaveLength(1)
  320. })
  321. it('rejects a second maintenance task while one is active', async () => {
  322. const ctx = await harness(new MockAdapter([]))
  323. const agent = await ctx.agentLoop.create(SessionId('maintenance-busy'), {
  324. provider: 'mock',
  325. model: 'mock',
  326. })
  327. const started = Promise.withResolvers<undefined>()
  328. const finish = Promise.withResolvers<undefined>()
  329. const maintenance = agent.runMaintenance(async () => {
  330. started.resolve(undefined)
  331. await finish.promise
  332. })
  333. await started.promise
  334. expect(() => agent.runMaintenance(async () => {})).toThrow(/already has active work/)
  335. finish.resolve(undefined)
  336. await maintenance
  337. await agent.whenIdle()
  338. })
  339. it('replays a wake latched behind maintenance at convergence', async () => {
  340. const adapter = new MockAdapter([textResponse('wake reply')])
  341. const ctx = await harness(adapter)
  342. const agent = await ctx.agentLoop.create(SessionId('maintenance-wake-replay'), {
  343. provider: 'mock',
  344. model: 'mock',
  345. })
  346. const started = Promise.withResolvers<undefined>()
  347. const finish = Promise.withResolvers<undefined>()
  348. const maintenance = agent.runMaintenance(async () => {
  349. started.resolve(undefined)
  350. await finish.promise
  351. })
  352. await started.promise
  353. send(agent, 'wake behind maintenance')
  354. finish.resolve(undefined)
  355. await maintenance
  356. await agent.whenIdle()
  357. expect(userTexts(agent)).toEqual(['wake behind maintenance'])
  358. expect(adapter.requests).toHaveLength(1)
  359. })
  360. it('suppresses the replay when a latched maintenance wake is removed', async () => {
  361. const adapter = new MockAdapter([])
  362. const ctx = await harness(adapter)
  363. const agent = await ctx.agentLoop.create(SessionId('maintenance-wake-removed'), {
  364. provider: 'mock',
  365. model: 'mock',
  366. })
  367. const started = Promise.withResolvers<undefined>()
  368. const finish = Promise.withResolvers<undefined>()
  369. const maintenance = agent.runMaintenance(async () => {
  370. started.resolve(undefined)
  371. await finish.promise
  372. })
  373. await started.promise
  374. const wake = createUserMessage({ content: [{ type: 'text', text: 'removed wake' }], source: { kind: 'user' } })
  375. agent.followup(wake)
  376. agent.inbox.remove(wake.id)
  377. finish.resolve(undefined)
  378. await maintenance
  379. await agent.whenIdle()
  380. expect(userTexts(agent)).toEqual([])
  381. expect(adapter.requests).toEqual([])
  382. expect(agent.session.snapshotEvents().filter(e => e.type === 'turn/start')).toHaveLength(0)
  383. })
  384. it('runs a simple turn: queued message → model → idle, with ordered events', async () => {
  385. const adapter = new MockAdapter([textResponse('hello there')])
  386. const ctx = await harness(adapter)
  387. const agent = await ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  388. // All boundaries — turn and step — are durable session events on the
  389. // session/event feed (no agent/* mirror). Record them in fire order to
  390. // assert the full boundary nesting.
  391. const order: string[] = []
  392. ctx.on('session/event', (_session, event) => {
  393. if (event.type === 'turn/start' || event.type === 'step/start' || event.type === 'step/end' || event.type === 'turn/end') {
  394. order.push(event.type)
  395. }
  396. })
  397. send(agent, 'hi')
  398. await waitForIdle(ctx, agent)
  399. expect(order).toEqual(['turn/start', 'step/start', 'step/end', 'turn/end'])
  400. const types = agent.session.snapshotEvents().map(e => e.type)
  401. // Durable inbox receipt precedes the turn-owned transcript.
  402. expect(types[0]).toBe('agent/inbox/spliced')
  403. expect(types).toContain('turn/start')
  404. expect(types).toContain('user/message')
  405. expect(types).toContain('assistant/message')
  406. const assistantMessage = agent.session.snapshotEvents().find(e => e.type === 'assistant/message')
  407. expect(assistantMessage?.type === 'assistant/message' && assistantMessage.data.usage).toEqual({ inputTokens: 10, outputTokens: 'hello there'.length })
  408. expect(types.at(-1)).toBe('turn/end')
  409. // derived history: user + assistant
  410. const messages = agent.session.deriveMessages()
  411. expect(messages.map(m => m.role)).toEqual(['user', 'assistant'])
  412. expect(messages[1]!.content).toEqual([{ type: 'text', text: 'hello there' }])
  413. })
  414. it('round-trips tool calls: model requests tool → executes → result in next request', async () => {
  415. const adapter = new MockAdapter([
  416. toolCallResponse('c1', 'echo', { text: 'ping' }, 'calling echo'),
  417. textResponse('done'),
  418. ])
  419. const ctx = await harness(adapter)
  420. ctx.tools.register(defineContentToolFixture({
  421. name: 'echo',
  422. description: 'echo back',
  423. parameters: { text: { type: 'string' } },
  424. async execute(args) {
  425. return [{ type: 'text', text: `echo: ${args.text}` }]
  426. },
  427. }))
  428. const agent = await ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  429. send(agent, 'use the tool')
  430. await waitForIdle(ctx, agent)
  431. // two model calls happened (tool-call step, then final step)
  432. expect(adapter.requests).toHaveLength(2)
  433. // the second request's derived history contains the tool result
  434. const secondMessages = adapter.requests[1]!.messages
  435. const toolResultMessage = secondMessages.find(m =>
  436. m.content.some(b => b.type === 'tool-result'))
  437. expect(toolResultMessage).toBeDefined()
  438. const block = toolResultMessage!.content.find(b => b.type === 'tool-result')!
  439. expect(block).toMatchObject({ toolCallId: 'c1', isError: false })
  440. expect((block).content).toEqual([{ type: 'text', text: 'echo: ping' }])
  441. // session log records call + result
  442. const types = agent.session.snapshotEvents().map(e => e.type)
  443. expect(types).toContain('tool/call')
  444. expect(types).toContain('tool/result')
  445. })
  446. it('renders harness identity, then the persona, then tool guidance — with {{variables}} resolved', async () => {
  447. const adapter = new MockAdapter([textResponse('ok')])
  448. // The persona is a TEMPLATE: {{model}} is the loop-registered variable
  449. // projecting this agent's configured model, so the model knows its own name.
  450. const ctx = await harness(adapter, 'You are a test agent on {{model}}.')
  451. ctx.systemPrompt.section({ name: 'tool:noop', order: 100, text: 'Use the noop tool wisely.' })
  452. ctx.tools.register(defineContentToolFixture({
  453. name: 'noop',
  454. description: 'does nothing',
  455. parameters: {},
  456. async execute() {
  457. return []
  458. },
  459. }))
  460. const agent = await ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  461. send(agent, 'hi')
  462. await waitForIdle(ctx, agent)
  463. const request = adapter.requests[0]
  464. expect(request!.system).toBe('You are an AI agent powered by DeepSeek Harness.\n\nYou are a test agent on mock.\n\nUse the noop tool wisely.')
  465. expect(request!.tools?.map(t => t.name)).toEqual(['noop'])
  466. })
  467. it('resolves {{cwd}} from the agent session workspace (factory create with meta.cwd)', async () => {
  468. const adapter = new MockAdapter([textResponse('ok')])
  469. const ctx = await harness(adapter, 'Working in {{cwd}}.')
  470. const handle = await ctx.agents.create({
  471. sessionId: SessionId('s-cwd'),
  472. meta: { cwd: '/work/space' },
  473. agentOptions: { provider: 'mock', model: 'mock' },
  474. })
  475. const agent = handle.agent
  476. send(agent, 'hi')
  477. await waitForIdle(ctx, agent)
  478. expect(adapter.requests[0]!.system).toBe('You are an AI agent powered by DeepSeek Harness.\n\nWorking in /work/space.')
  479. })
  480. it('contains a strict-variable render failure: the turn errors, the loop keeps serving turns', async () => {
  481. // A missing cwd variable must fail one turn without preventing a later valid turn.
  482. const adapter = new MockAdapter([textResponse('ok after rescue')])
  483. const ctx = await harness(adapter, 'In {{cwd}}.')
  484. const errors: Error[] = []
  485. ctx.on('agent/error', ({ error }) => {
  486. if (error instanceof Error) errors.push(error)
  487. })
  488. const agent = await ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  489. send(agent, 'hi')
  490. await waitForIdle(ctx, agent)
  491. expect(adapter.requests).toHaveLength(0) // the request was never sent
  492. expect(errors.map(error => error.message)).toEqual([
  493. 'prompt variable "{{cwd}}" has no value for this assembly (section "deployment:persona")',
  494. ])
  495. const turnEnd = agent.session.snapshotEvents().find(e => e.type === 'turn/end')
  496. expect(turnEnd?.type === 'turn/end' && turnEnd.data.reason.kind).toBe('error')
  497. expect(turnEnd?.type === 'turn/end' && turnEnd.data.reason.kind === 'error'
  498. ? turnEnd.data.reason.error.message
  499. : '').toContain('no value for this assembly')
  500. // The loop survived: a waterfall listener rescues {{cwd}} and the SAME
  501. // agent completes a real model turn.
  502. ctx.on('system-prompt/assemble', async (assembly, _context, next) => {
  503. assembly.variables['cwd'] = '/rescued'
  504. return next()
  505. })
  506. send(agent, 'again')
  507. await waitForIdle(ctx, agent)
  508. expect(adapter.requests).toHaveLength(1)
  509. expect(adapter.requests[0]!.system).toBe('You are an AI agent powered by DeepSeek Harness.\n\nIn /rescued.')
  510. const turnEnds = agent.session.snapshotEvents().filter(e => e.type === 'turn/end')
  511. expect(turnEnds).toHaveLength(2)
  512. expect(turnEnds[1]?.type === 'turn/end' && turnEnds[1].data.reason.kind).toBe('completed')
  513. })
  514. it('supports the model-via-agent/request path with a {{model}} persona: the supplier states it via the assemble waterfall', async () => {
  515. // AgentOptions.model unset: the model arrives in the agent/request
  516. // waterfall (the loop's documented fallback — see runStep's no-model
  517. // error). {{model}} renders BEFORE that waterfall, so the SAME plugin
  518. // states the fact early on system-prompt/assemble — the owner of a
  519. // late-bound fact owns stating it wherever it is claimed.
  520. const adapter = new MockAdapter([textResponse('ok')])
  521. const ctx = await harness(adapter, 'You run on {{model}}.')
  522. ctx.on('system-prompt/assemble', async (assembly, _context, next) => {
  523. assembly.variables['provider'] = 'mock'
  524. assembly.variables['model'] = 'mock'
  525. return next()
  526. })
  527. ctx.on('agent/request', async (_payload, next) => {
  528. const config = await next()
  529. return { ...config, provider: 'mock', model: 'mock' }
  530. })
  531. const agent = await ctx.agentLoop.create(SessionId('a-late-model'), {})
  532. send(agent, 'hi')
  533. await waitForIdle(ctx, agent)
  534. expect(adapter.requests).toHaveLength(1)
  535. expect(adapter.requests[0]!.model).toBe('mock')
  536. expect(adapter.requests[0]!.system).toBe('You are an AI agent powered by DeepSeek Harness.\n\nYou run on mock.')
  537. })
  538. it('omits the system field when system-prompt/assemble short-circuits with an empty assembly', async () => {
  539. // The documented escape valve: a deployment that must drop the harness
  540. // openers short-circuits the assemble waterfall; the request then carries
  541. // NO system field at all (not an empty string).
  542. const adapter = new MockAdapter([textResponse('ok')])
  543. const ctx = await harness(adapter)
  544. ctx.on('system-prompt/assemble', async () => ({ sections: [], contexts: [], tools: [], variables: {} }))
  545. const agent = await ctx.agentLoop.create(SessionId('a-no-system'), { provider: 'mock', model: 'mock' })
  546. send(agent, 'hi')
  547. await waitForIdle(ctx, agent)
  548. expect(adapter.requests).toHaveLength(1)
  549. expect('system' in adapter.requests[0]!).toBe(false)
  550. })
  551. it('materializes changed runtime context at the history tail without rewriting the system header', async () => {
  552. const adapter = new MockAdapter([
  553. textResponse('one'),
  554. textResponse('two'),
  555. textResponse('three'),
  556. textResponse('four'),
  557. textResponse('five'),
  558. ])
  559. const ctx = await harness(adapter)
  560. let mode = 'read-only'
  561. const dispose = ctx.systemPrompt.context({ name: 'policy', order: 0, text: () => `Mode: ${mode}.` })
  562. const agent = await ctx.agentLoop.create(SessionId('a-runtime-context'), { provider: 'mock', model: 'mock' })
  563. const contextEvents = () => agent.session.snapshotEvents().flatMap(event =>
  564. event.type === 'user/message'
  565. && event.data.source.kind === 'plugin'
  566. && event.data.source.plugin === '@deepseek-ai/dsh-system-prompt'
  567. ? [event]
  568. : [])
  569. send(agent, 'first')
  570. await waitForIdle(ctx, agent)
  571. expect(contextEvents()).toHaveLength(1)
  572. expect(contextEvents()[0]?.data.content).toEqual([{
  573. type: 'text',
  574. text: 'Current runtime context. This snapshot supersedes earlier runtime-context snapshots.\n\nMode: read-only.',
  575. }])
  576. send(agent, 'unchanged')
  577. await waitForIdle(ctx, agent)
  578. expect(contextEvents()).toHaveLength(1)
  579. mode = 'danger-full-access'
  580. send(agent, 'changed')
  581. await waitForIdle(ctx, agent)
  582. expect(contextEvents()).toHaveLength(2)
  583. const changedBlock = contextEvents()[1]?.data.content[0]
  584. expect(changedBlock?.type).toBe('text')
  585. if (changedBlock?.type !== 'text') throw new Error('changed runtime context is not text')
  586. expect(changedBlock.text).toContain('danger-full-access')
  587. dispose()
  588. send(agent, 'cleared')
  589. await waitForIdle(ctx, agent)
  590. expect(contextEvents()).toHaveLength(3)
  591. expect(contextEvents()[2]?.data.content).toEqual([{
  592. type: 'text',
  593. text: 'Current runtime context: none. Earlier runtime-context snapshots no longer apply.',
  594. }])
  595. send(agent, 'still clear')
  596. await waitForIdle(ctx, agent)
  597. expect(contextEvents()).toHaveLength(3)
  598. expect(adapter.requests.map(request => request.system)).toEqual(Array(5).fill(adapter.requests[0]?.system))
  599. expect(agent.session.snapshotEvents().flatMap(event =>
  600. event.type === 'request/header' ? [event.data.reason] : [])).toEqual(['initial'])
  601. })
  602. it('re-emits unchanged runtime context when a surface replacement removed the retained snapshot', async () => {
  603. const adapter = new MockAdapter([textResponse('one'), textResponse('two')])
  604. const ctx = await harness(adapter)
  605. ctx.systemPrompt.context({ name: 'policy', order: 0, text: 'Mode: read-only.' })
  606. const agent = await ctx.agentLoop.create(SessionId('a-runtime-context-compacted'), { provider: 'mock', model: 'mock' })
  607. send(agent, 'first')
  608. await waitForIdle(ctx, agent)
  609. const contextEvent = agent.session.snapshotEvents().find(event =>
  610. event.type === 'user/message'
  611. && event.data.source.kind === 'plugin'
  612. && event.data.source.plugin === '@deepseek-ai/dsh-system-prompt')
  613. if (contextEvent?.type !== 'user/message') throw new Error('first turn did not materialize runtime context')
  614. agent.session.append('user/message', createUserMessage({
  615. content: [{ type: 'text', text: 'compacted summary' }],
  616. source: { kind: 'plugin', plugin: 'test-compaction' },
  617. }), {
  618. surfaceOp: { op: 'replace', start: contextEvent.seq, end: contextEvent.seq },
  619. sourceEventSeqs: [contextEvent.seq],
  620. })
  621. send(agent, 'after compaction')
  622. await waitForIdle(ctx, agent)
  623. const runtimeContexts = agent.session.snapshotEvents().flatMap(event =>
  624. event.type === 'user/message'
  625. && event.data.source.kind === 'plugin'
  626. && event.data.source.plugin === '@deepseek-ai/dsh-system-prompt'
  627. ? [event]
  628. : [])
  629. expect(runtimeContexts).toHaveLength(2)
  630. expect(adapter.requests[1]?.messages.some(message =>
  631. message.source.kind === 'plugin'
  632. && message.source.plugin === '@deepseek-ai/dsh-system-prompt')).toBe(true)
  633. })
  634. it('clears compacted runtime context after the active set becomes empty', async () => {
  635. const adapter = new MockAdapter([textResponse('one'), textResponse('two')])
  636. const ctx = await harness(adapter)
  637. const dispose = ctx.systemPrompt.context({ name: 'policy', order: 0, text: 'Mode: read-only.' })
  638. const agent = await ctx.agentLoop.create(SessionId('a-runtime-context-compacted-clear'), { provider: 'mock', model: 'mock' })
  639. send(agent, 'first')
  640. await waitForIdle(ctx, agent)
  641. const contextEvent = agent.session.snapshotEvents().find(event =>
  642. event.type === 'user/message'
  643. && event.data.source.kind === 'plugin'
  644. && event.data.source.plugin === '@deepseek-ai/dsh-system-prompt')
  645. if (contextEvent?.type !== 'user/message') throw new Error('first turn did not materialize runtime context')
  646. agent.session.append('user/message', createUserMessage({
  647. content: [{ type: 'text', text: 'summary retaining old mode: read-only' }],
  648. source: { kind: 'plugin', plugin: 'test-compaction' },
  649. }), {
  650. surfaceOp: { op: 'replace', start: contextEvent.seq, end: contextEvent.seq },
  651. sourceEventSeqs: [contextEvent.seq],
  652. })
  653. dispose()
  654. send(agent, 'after compaction')
  655. await waitForIdle(ctx, agent)
  656. const clearing = adapter.requests[1]?.messages.find(message =>
  657. message.source.kind === 'plugin'
  658. && message.source.plugin === '@deepseek-ai/dsh-system-prompt')
  659. expect(clearing?.content).toEqual([{
  660. type: 'text',
  661. text: 'Current runtime context: none. Earlier runtime-context snapshots no longer apply.',
  662. }])
  663. })
  664. it('does not clear runtime context after an unrelated replacement', async () => {
  665. const adapter = new MockAdapter([textResponse('ok')])
  666. const ctx = await harness(adapter)
  667. const agent = await ctx.agentLoop.create(SessionId('a-runtime-context-unrelated-compaction'), { provider: 'mock', model: 'mock' })
  668. const original = agent.session.append('user/message', createUserMessage({
  669. content: [{ type: 'text', text: 'old context' }],
  670. source: { kind: 'plugin', plugin: 'test-context' },
  671. }), { surfaceOp: 'append' })
  672. agent.session.append('user/message', createUserMessage({
  673. content: [{ type: 'text', text: 'compacted summary' }],
  674. source: { kind: 'plugin', plugin: 'test-compaction' },
  675. }), {
  676. surfaceOp: { op: 'replace', start: original.seq, end: original.seq },
  677. sourceEventSeqs: [original.seq],
  678. })
  679. send(agent, 'after compaction')
  680. await waitForIdle(ctx, agent)
  681. expect(adapter.requests[0]?.messages.some(message =>
  682. message.source.kind === 'plugin'
  683. && message.source.plugin === '@deepseek-ai/dsh-system-prompt')).toBe(false)
  684. })
  685. it('replaces a malformed retained runtime-context message with the current complete snapshot', async () => {
  686. const adapter = new MockAdapter([textResponse('ok')])
  687. const ctx = await harness(adapter)
  688. ctx.systemPrompt.context({ name: 'policy', order: 0, text: 'Mode: read-only.' })
  689. const agent = await ctx.agentLoop.create(SessionId('a-runtime-context-malformed'), { provider: 'mock', model: 'mock' })
  690. agent.session.append('user/message', createUserMessage({
  691. content: [{ type: 'text', text: 'broken' }, { type: 'text', text: 'snapshot' }],
  692. source: { kind: 'plugin', plugin: '@deepseek-ai/dsh-system-prompt' },
  693. }), { surfaceOp: 'append' })
  694. send(agent, 'repair context')
  695. await waitForIdle(ctx, agent)
  696. const runtimeContexts = agent.session.snapshotEvents().flatMap(event =>
  697. event.type === 'user/message'
  698. && event.data.source.kind === 'plugin'
  699. && event.data.source.plugin === '@deepseek-ai/dsh-system-prompt'
  700. ? [event]
  701. : [])
  702. expect(runtimeContexts).toHaveLength(2)
  703. expect(runtimeContexts[1]?.data.content).toEqual([{
  704. type: 'text',
  705. text: 'Current runtime context. This snapshot supersedes earlier runtime-context snapshots.\n\nMode: read-only.',
  706. }])
  707. })
  708. it('records exact replay chunks inside the durable assistant message', async () => {
  709. const adapter = new MockAdapter([textResponse('abc')])
  710. const ctx = await harness(adapter)
  711. const agent = await ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  712. send(agent, 'hi')
  713. await waitForIdle(ctx, agent)
  714. const message = agent.session.snapshotEvents().find(e => e.type === 'assistant/message')
  715. const chunks = message?.type === 'assistant/message' ? expandAssistantStream(message.data.stream) : []
  716. // textResponse('abc') = block-start + 3 deltas + block-end + usage + finish = 7
  717. expect(chunks).toHaveLength(7)
  718. const deltaText = chunks
  719. .map(member => member.chunk)
  720. .filter((c: StreamChunk): c is Extract<StreamChunk, { type: 'text-delta' }> => c.type === 'text-delta')
  721. .map(c => c.text)
  722. .join('')
  723. expect(deltaText).toBe('abc')
  724. })
  725. it('injects steering between steps and continues the turn', async () => {
  726. const adapter = new MockAdapter([
  727. toolCallResponse('c1', 'slow', {}),
  728. textResponse('addressed the steering'),
  729. ])
  730. const ctx = await harness(adapter)
  731. const agent = await ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  732. ctx.tools.register(defineContentToolFixture({
  733. name: 'slow',
  734. description: '',
  735. parameters: {},
  736. async execute() {
  737. // steer while the turn is running (during tool execution)
  738. agent.steer(createUserMessage({ content: [{ type: 'text', text: 'change of plans' }], source: { kind: 'user' } }))
  739. return [{ type: 'text', text: 'tool done' }]
  740. },
  741. }))
  742. send(agent, 'start')
  743. await waitForIdle(ctx, agent)
  744. const steering = agent.session.snapshotEvents().find(e =>
  745. e.type === 'user/message' && JSON.stringify(e.data.content).includes('change of plans'))
  746. expect(steering).toBeDefined()
  747. // The entered batch is appended after the second step opens and before its
  748. // request derives history.
  749. const steeringSeq = steering!.seq
  750. const secondStepStart = agent.session.snapshotEvents().filter(e => e.type === 'step/start')[1]
  751. expect(secondStepStart).toBeDefined()
  752. expect(steeringSeq).toBeGreaterThan(secondStepStart!.seq)
  753. // the second model request saw the steering content
  754. const secondRequest = adapter.requests[1]
  755. const flat = JSON.stringify(secondRequest!.messages)
  756. expect(flat).toContain('change of plans')
  757. })
  758. it('starts idle steering synchronously and enters later steering at the next step', async () => {
  759. const adapter = new MockAdapter([textResponse('first'), textResponse('second')])
  760. const ctx = await harness(adapter)
  761. const agent = await ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  762. const idle = waitForIdle(ctx, agent)
  763. agent.steer(createUserMessage({ content: [{ type: 'text', text: 'first idle steer' }], source: { kind: 'user' } }))
  764. expect(agent.status).toBe('running')
  765. expect(agent.session.snapshotEvents().filter(event => event.type === 'turn/start')).toHaveLength(1)
  766. agent.steer(createUserMessage({ content: [{ type: 'text', text: 'second idle steer' }], source: { kind: 'user' } }))
  767. await idle
  768. expect(agent.session.snapshotEvents().filter(event => event.type === 'turn/start')).toHaveLength(1)
  769. expect(agent.session.snapshotEvents()
  770. .filter(event => event.type === 'user/message')
  771. .map(event => event.data.content)).toEqual([
  772. [{ type: 'text', text: 'first idle steer' }],
  773. [{ type: 'text', text: 'second idle steer' }],
  774. ])
  775. expect(adapter.requests).toHaveLength(2)
  776. expect(JSON.stringify(adapter.requests[0]?.messages)).toContain('first idle steer')
  777. expect(JSON.stringify(adapter.requests[0]?.messages)).not.toContain('second idle steer')
  778. expect(JSON.stringify(adapter.requests[1]?.messages)).toContain('second idle steer')
  779. })
  780. it('stops after a throwing pre-step listener and retains later steering until a wakeup', async () => {
  781. const adapter = new MockAdapter([textResponse('recovered')])
  782. const ctx = await harness(adapter)
  783. const agent = await ctx.agentLoop.create(SessionId('failed-steering'), { provider: 'mock', model: 'mock' })
  784. let fail = true
  785. ctx.on('agent/pre-step', ({ agent: subject }, next) => {
  786. if (subject !== agent || !fail) return next()
  787. fail = false
  788. subject.steer(createUserMessage({ content: [{ type: 'text', text: 'pending steering' }], source: { kind: 'user' } }))
  789. throw new Error('pre-step failed')
  790. })
  791. send(agent, 'prompt')
  792. await waitForIdle(ctx, agent)
  793. expect(adapter.requests).toHaveLength(0)
  794. expect(agent.session.snapshotEvents().filter(event => event.type === 'turn/start')).toHaveLength(1)
  795. expect(agent.session.snapshotEvents().filter(event => event.type === 'turn/end')).toHaveLength(1)
  796. expect(agent.inbox.nextStep).toHaveLength(1)
  797. send(agent, 'resume')
  798. await waitForIdle(ctx, agent)
  799. expect(adapter.requests).toHaveLength(1)
  800. expect(agent.session.snapshotEvents().filter(event => event.type === 'turn/start')).toHaveLength(2)
  801. expect(JSON.stringify(adapter.requests[0]?.messages)).toContain('pending steering')
  802. })
  803. it('inject() while idle durably stages context without opening a turn', async () => {
  804. const adapter = new MockAdapter([textResponse('ok')])
  805. const ctx = await harness(adapter)
  806. const agent = await ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  807. agent.inject(createUserMessage({ content: [{ type: 'text', text: 'file changed: a.ts' }], source: { kind: 'plugin', plugin: 'watcher' } }))
  808. expect(agent.status).toBe('idle')
  809. expect(adapter.requests).toHaveLength(0)
  810. expect(agent.session.snapshotEvents().filter(event => event.type === 'turn/start')).toHaveLength(0)
  811. expect(agent.session.snapshotEvents().at(-1)).toMatchObject({
  812. type: 'agent/inbox/spliced',
  813. data: {
  814. target: 'next-step',
  815. inserted: [{
  816. role: 'user',
  817. content: [{ type: 'text', text: 'file changed: a.ts' }],
  818. source: { kind: 'plugin', plugin: 'watcher' },
  819. }],
  820. },
  821. })
  822. send(agent, 'go')
  823. await waitForIdle(ctx, agent)
  824. expect(agent.session.snapshotEvents().filter(event => event.type === 'turn/start')).toHaveLength(1)
  825. const flat = JSON.stringify(adapter.requests[0]!.messages)
  826. expect(flat).toContain('file changed: a.ts')
  827. expect(flat).not.toContain('<context source=')
  828. })
  829. it('inject() persists structured context content verbatim with durable source', async () => {
  830. const adapter = new MockAdapter([textResponse('ok')])
  831. const ctx = await harness(adapter)
  832. const agent = await ctx.agentLoop.create(SessionId('raw-context'), { provider: 'mock', model: 'mock' })
  833. const text = '<system-reminder>Additional instructions from: pkg/AGENTS.md</system-reminder>'
  834. agent.inject(createUserMessage({ content: [{ type: 'text', text }], source: { kind: 'plugin', plugin: 'agent-instructions' } }))
  835. send(agent, 'go')
  836. await waitForIdle(ctx, agent)
  837. const contextEvent = agent.session.snapshotEvents().find(event => event.type === 'user/message' && event.data.source.kind === 'plugin')
  838. expect(contextEvent?.type === 'user/message' && contextEvent.data.source)
  839. .toEqual({ kind: 'plugin', plugin: 'agent-instructions' })
  840. const requestText = JSON.stringify(adapter.requests[0]!.messages)
  841. expect(requestText).toContain('Additional instructions from: pkg/AGENTS.md')
  842. expect(requestText).not.toContain('<context source=')
  843. })
  844. it('defers inject() during tool execution until after the tool result', async () => {
  845. const adapter = new MockAdapter([
  846. toolCallResponse('c1', 'noticer', {}, 'calling'),
  847. textResponse('done'),
  848. ])
  849. const ctx = await harness(adapter)
  850. const agent = await ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  851. let visibleDuringTool = false
  852. ctx.tools.register(defineContentToolFixture({
  853. name: 'noticer',
  854. description: 'injects a notice',
  855. parameters: {},
  856. async execute() {
  857. await Promise.resolve()
  858. const first = { type: 'text' as const, text: 'mid-turn notice' }
  859. agent.inject(createUserMessage({ content: [first], source: { kind: 'plugin', plugin: 'x' } }))
  860. first.text = 'mutated after inject'
  861. agent.inject(createUserMessage({ content: [{ type: 'text', text: 'second notice' }], source: { kind: 'plugin', plugin: 'x' } }))
  862. visibleDuringTool = agent.session.snapshotEvents().some(e => e.type === 'user/message' && e.data.source.kind === 'plugin')
  863. return [{ type: 'text', text: 'ok' }]
  864. },
  865. }))
  866. send(agent, 'go')
  867. await waitForIdle(ctx, agent)
  868. expect(visibleDuringTool).toBe(false)
  869. // The injection stays in the open turn, but its user-role context cannot
  870. // split the assistant tool call from the provider's tool-result message.
  871. const turnStarts = agent.session.snapshotEvents().filter(e => e.type === 'turn/start')
  872. expect(turnStarts).toHaveLength(1)
  873. const result = agent.session.snapshotEvents().find(e => e.type === 'tool/result')!
  874. const contexts = agent.session.snapshotEvents().filter(e => e.type === 'user/message' && e.data.source.kind === 'plugin')
  875. expect(contexts).toHaveLength(2)
  876. expect(result.seq).toBeLessThan(contexts[0]!.seq)
  877. expect(contexts.flatMap(event => event.type === 'user/message' ? event.data.content : []))
  878. .toEqual([
  879. { type: 'text', text: 'mid-turn notice' },
  880. { type: 'text', text: 'second notice' },
  881. ])
  882. const secondRequest = adapter.requests[1]!.messages
  883. const resultIndex = secondRequest.findIndex(message =>
  884. message.content.some(block => block.type === 'tool-result'))
  885. const contextIndexes = secondRequest.flatMap((message, index) =>
  886. message.content.some(block => block.type === 'text'
  887. && (block.text.includes('mid-turn notice') || block.text.includes('second notice')))
  888. ? [index]
  889. : [])
  890. expect(resultIndex).toBeGreaterThanOrEqual(0)
  891. expect(contextIndexes).toHaveLength(2)
  892. expect(contextIndexes.every(index => index > resultIndex)).toBe(true)
  893. })
  894. it('rejects non-JSON context before it enters the active tool-batch FIFO', async () => {
  895. const adapter = new MockAdapter([
  896. toolCallResponse('c1', 'invalid-injector', {}, 'calling'),
  897. textResponse('done'),
  898. ])
  899. const ctx = await harness(adapter)
  900. const agent = await ctx.agentLoop.create(SessionId('invalid-context'), { provider: 'mock', model: 'mock' })
  901. ctx.tools.register(defineContentToolFixture({
  902. name: 'invalid-injector',
  903. description: 'attempts an invalid context injection',
  904. parameters: {},
  905. async execute() {
  906. expect(() => {
  907. agent.inject(createUserMessage({ content: [{ type: 'text', text: 'invalid' }], source: { kind: 'plugin', plugin: 'test', bigint: 1n } as never }))
  908. }).toThrow('agent context must be losslessly JSON-serializable')
  909. return [{ type: 'text', text: 'rejected invalid context' }]
  910. },
  911. }))
  912. send(agent, 'go')
  913. await waitForIdle(ctx, agent)
  914. expect(agent.session.snapshotEvents().some(event => event.type === 'user/message' && event.data.source.kind === 'plugin')).toBe(false)
  915. })
  916. it('agent/turn-stopping can steer another step (/loop pattern)', async () => {
  917. const adapter = new MockAdapter([
  918. textResponse('step 1'),
  919. textResponse('step 2'),
  920. textResponse('step 3'),
  921. ])
  922. const ctx = await harness(adapter)
  923. const agent = await ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  924. let steps = 0
  925. ctx.on('session/event', (_session, event) => { if (event.type === 'step/end') steps++ })
  926. ctx.on('agent/turn-stopping', ({ agent: subject }) => {
  927. if (steps < 3) {
  928. subject.steer(createUserMessage({ content: [{ type: 'text', text: 'continue' }], source: { kind: 'plugin', plugin: 'loop-test' } }))
  929. }
  930. })
  931. send(agent, 'go')
  932. await waitForIdle(ctx, agent)
  933. expect(steps).toBe(3)
  934. expect(adapter.requests).toHaveLength(3)
  935. })
  936. it('a tool can conclude the turn despite owing a follow-up request', async () => {
  937. const adapter = new MockAdapter([toolCallResponse('c1', 'echo', { text: 'x' })])
  938. const ctx = await harness(adapter)
  939. ctx.tools.register(defineContentToolFixture({
  940. name: 'echo',
  941. description: '',
  942. parameters: { text: { type: 'string' } },
  943. async execute(args, exec) {
  944. exec.concludeTurn()
  945. return [{ type: 'text', text: String(args.text) }]
  946. },
  947. }))
  948. const agent = await ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  949. send(agent, 'go')
  950. await waitForIdle(ctx, agent)
  951. // only one model call despite the tool call requesting a follow-up
  952. expect(adapter.requests).toHaveLength(1)
  953. // The tool still executes and durably records its result.
  954. expect(agent.session.snapshotEvents().some(e => e.type === 'tool/result')).toBe(true)
  955. })
  956. it('continues for steering that arrived during a concluding tool step', async () => {
  957. const adapter = new MockAdapter([
  958. toolCallResponse('c1', 'finalize', {}),
  959. textResponse('next turn reply'),
  960. ])
  961. const ctx = await harness(adapter)
  962. const agent = await ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  963. ctx.tools.register(defineContentToolFixture({
  964. name: 'finalize',
  965. description: '',
  966. parameters: {},
  967. async execute(_args, exec) {
  968. // Steering lands while the concluding tool is still executing.
  969. agent.steer(createUserMessage({ content: [{ type: 'text', text: 'late steering' }], source: { kind: 'user' } }))
  970. exec.concludeTurn()
  971. return [{ type: 'text', text: 'final' }]
  972. },
  973. }))
  974. send(agent, 'go')
  975. await waitForIdle(ctx, agent)
  976. expect(adapter.requests).toHaveLength(2)
  977. const events = agent.session.snapshotEvents().map(event => event.type)
  978. expect(events.filter(type => type === 'turn/end')).toHaveLength(1)
  979. expect(JSON.stringify(adapter.requests[1]?.messages)).toContain('late steering')
  980. const texts = adapter.requests[1]!.messages
  981. .flatMap(message => message.content)
  982. .filter(block => block.type === 'text')
  983. .map(block => block.text)
  984. expect(texts).toContain('late steering')
  985. })
  986. it('agent/request waterfall switches models by returning a replacement config; the switch is logged', async () => {
  987. const adapter = new MockAdapter([textResponse('ok')])
  988. const ctx = await harness(adapter)
  989. const agent = await ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  990. ctx.on('agent/request', async (_payload, next) => {
  991. const config = await next()
  992. // The seed is frozen — config is not a mutable per-call knob; a switch
  993. // is proposed by returning a replacement, and the loop logs it.
  994. expect(Object.isFrozen(config)).toBe(true)
  995. expect(() => { (config as { model: string }).model = 'other-model' }).toThrow(TypeError)
  996. return { ...config, model: 'other-model' }
  997. })
  998. send(agent, 'hi')
  999. await waitForIdle(ctx, agent)
  1000. expect(adapter.requests[0]!.model).toBe('other-model')
  1001. // The header event records what the request ACTUALLY used — the switch is
  1002. // a reconstructable fact, not silent drift.
  1003. const headerEvent = agent.session.snapshotEvents().find(e => e.type === 'request/header')
  1004. expect(headerEvent?.type === 'request/header' && headerEvent.data.header.config.model).toBe('other-model')
  1005. })
  1006. it('agent/pre-step fires once per proposed step before the step is opened', async () => {
  1007. const adapter = new MockAdapter([
  1008. toolCallResponse('c1', 'echo', {}, 'calling echo'),
  1009. textResponse('done'),
  1010. ])
  1011. const ctx = await harness(adapter)
  1012. ctx.tools.register(defineContentToolFixture({
  1013. name: 'echo', description: 'echo', parameters: {},
  1014. async execute() { return [{ type: 'text', text: 'echoed' }] },
  1015. }))
  1016. const agent = await ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  1017. const fires: { turn: number; step: number; signal: AbortSignal }[] = []
  1018. ctx.on('agent/pre-step', ({ agent: subject, turn, step, signal }, next) => {
  1019. if (subject === agent) fires.push({ turn, step, signal })
  1020. return next()
  1021. })
  1022. send(agent, 'go')
  1023. await waitForIdle(ctx, agent)
  1024. expect(fires.map(({ turn, step }) => ({ turn, step }))).toEqual([
  1025. { turn: 1, step: 1 },
  1026. { turn: 1, step: 2 },
  1027. ])
  1028. expect(fires.every(({ signal }) => signal instanceof AbortSignal)).toBe(true)
  1029. })
  1030. it('agent/pre-step fires before its step boundary opens and before the request', async () => {
  1031. const adapter = new MockAdapter([textResponse('ok')])
  1032. const ctx = await harness(adapter)
  1033. const agent = await ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  1034. let boundaryOpen = true
  1035. ctx.on('agent/pre-step', ({ agent: subject }, next) => {
  1036. if (subject === agent) boundaryOpen = subject.session.snapshotEvents().at(-1)?.type === 'step/start'
  1037. return next()
  1038. })
  1039. send(agent, 'go')
  1040. await waitForIdle(ctx, agent)
  1041. expect(boundaryOpen).toBe(false)
  1042. expect(adapter.requests).toHaveLength(1)
  1043. })
  1044. it('a throwing agent/pre-step listener fails the proposal, not the loop', async () => {
  1045. const adapter = new MockAdapter([textResponse('second turn ok')])
  1046. const ctx = await harness(adapter)
  1047. const agent = await ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  1048. let throwOnce = true
  1049. ctx.on('agent/pre-step', (_payload, next) => {
  1050. if (throwOnce) { throwOnce = false; throw new Error('boom in pre-step') }
  1051. return next()
  1052. })
  1053. const errors: Error[] = []
  1054. ctx.on('agent/error', ({ error }) => {
  1055. if (error instanceof Error) errors.push(error)
  1056. })
  1057. send(agent, 'first')
  1058. await waitForIdle(ctx, agent)
  1059. // The first proposal failed inside a balanced turn without calling the model.
  1060. expect(errors.map(error => error.message)).toEqual(['boom in pre-step'])
  1061. expect(adapter.requests.length).toBe(0)
  1062. expect(agent.session.snapshotEvents().some(event => event.type === 'turn/start')).toBe(true)
  1063. expect(agent.session.snapshotEvents().some(event => event.type === 'turn/end')).toBe(true)
  1064. // The loop survived: a second prompt runs a normal completed turn.
  1065. send(agent, 'second')
  1066. await waitForIdle(ctx, agent)
  1067. expect(adapter.requests.length).toBe(1)
  1068. const lastTurnEnd = agent.session.snapshotEvents().findLast(e => e.type === 'turn/end')
  1069. expect(lastTurnEnd?.type === 'turn/end' && lastTurnEnd.data.reason).toEqual({ kind: 'completed' })
  1070. })
  1071. it('cancel() mid-stream ends the turn with reason aborted', async () => {
  1072. const adapter = new MockAdapter(['hang'])
  1073. const ctx = await harness(adapter)
  1074. const agent = await ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  1075. const reasons: TurnEndReason[] = []
  1076. const frames: AssistantStreamFrame[] = []
  1077. ctx.on('session/event', (_s, event) => { if (event.type === 'turn/end') reasons.push(event.data.reason) })
  1078. ctx.on('agent/assistant-stream', ({ frame }) => { frames.push(frame) })
  1079. send(agent, 'go')
  1080. // wait until the stream is hanging, then cancel
  1081. await new Promise(r => setTimeout(r, 30))
  1082. expect(agent.status).toBe('running')
  1083. agent.cancel({ kind: 'user' })
  1084. await waitForIdle(ctx, agent)
  1085. expect(reasons).toEqual([{ kind: 'aborted', reason: { kind: 'user' } }])
  1086. const chunks = frames.filter(frame => frame.type === 'chunk')
  1087. expect(frames.at(-1)).toMatchObject({
  1088. type: 'end',
  1089. index: chunks.length,
  1090. outcome: { kind: 'committed', eventType: 'assistant/message' },
  1091. })
  1092. })
  1093. it('surfaces max-tokens as the turn-end reason when the last step is cut off', async () => {
  1094. // A single step that ends with a max-tokens finish (no tool calls): the
  1095. // turn stops by default and ends max-tokens, not completed.
  1096. const adapter = new MockAdapter([maxTokensResponse('truncat')])
  1097. const ctx = await harness(adapter)
  1098. const agent = await ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  1099. const reasons: TurnEndReason[] = []
  1100. ctx.on('session/event', (_s, event) => { if (event.type === 'turn/end') reasons.push(event.data.reason) })
  1101. send(agent, 'go')
  1102. await waitForIdle(ctx, agent)
  1103. expect(adapter.requests).toHaveLength(1)
  1104. expect(reasons).toEqual([{ kind: 'max-tokens' }])
  1105. // Assert the durable row, not only the live listener.
  1106. const turnEnd = agent.session.snapshotEvents().findLast(e => e.type === 'turn/end')
  1107. expect(turnEnd!.data.reason).toEqual({ kind: 'max-tokens' })
  1108. })
  1109. it('a max-tokens step earlier in a turn still surfaces as max-tokens after a later completed step', async () => {
  1110. // Step 1 is cut off (max-tokens, no tool calls → would stop by default), so continuation
  1111. // must be FORCED to reach step 2 which finishes normally (stop).
  1112. const adapter = new MockAdapter([
  1113. maxTokensResponse('first half'),
  1114. textResponse('second half'),
  1115. ])
  1116. const ctx = await harness(adapter)
  1117. const agent = await ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  1118. let steps = 0
  1119. ctx.on('session/event', (_session, event) => { if (event.type === 'step/end') steps++ })
  1120. // Force exactly one continuation (step 1 → step 2), then defer to default
  1121. // (step 2 is a plain stop with no tool calls → stops).
  1122. ctx.on('agent/turn-stopping', ({ agent: subject }) => {
  1123. if (steps < 2) {
  1124. subject.steer(createUserMessage({ content: [{ type: 'text', text: 'continue after truncation' }], source: { kind: 'plugin', plugin: 'max-tokens-test' } }))
  1125. }
  1126. })
  1127. const reasons: TurnEndReason[] = []
  1128. ctx.on('session/event', (_s, event) => { if (event.type === 'turn/end') reasons.push(event.data.reason) })
  1129. send(agent, 'go')
  1130. await waitForIdle(ctx, agent)
  1131. expect(steps).toBe(2)
  1132. expect(adapter.requests).toHaveLength(2)
  1133. expect(adapter.requests[1]!.messages).toEqual([
  1134. {
  1135. id: expect.any(String) as unknown,
  1136. role: 'user',
  1137. content: [{ type: 'text', text: 'go' }],
  1138. source: { kind: 'user' },
  1139. },
  1140. {
  1141. id: expect.any(String) as unknown,
  1142. role: 'assistant',
  1143. content: [{ type: 'text', text: 'first half' }],
  1144. source: { kind: 'model', provider: 'mock', model: 'mock' },
  1145. },
  1146. {
  1147. id: expect.any(String) as unknown,
  1148. role: 'user',
  1149. content: [{ type: 'text', text: 'continue after truncation' }],
  1150. source: { kind: 'plugin', plugin: 'max-tokens-test' },
  1151. },
  1152. ])
  1153. // A max-token step is sticky: the later completed step must not
  1154. // downgrade the turn outcome.
  1155. expect(reasons).toEqual([{ kind: 'max-tokens' }])
  1156. })
  1157. it('a completed step after no max-tokens keeps the turn completed (max-tokens does not leak across turns)', async () => {
  1158. // Two consecutive turns: turn 1 is cut off (max-tokens), turn 2 is a clean
  1159. // stop. The per-turn reason must be independent — turn 2 ends completed.
  1160. const adapter = new MockAdapter([maxTokensResponse('cut'), textResponse('clean')])
  1161. const ctx = await harness(adapter)
  1162. const agent = await ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  1163. const reasons: TurnEndReason[] = []
  1164. ctx.on('session/event', (_s, event) => { if (event.type === 'turn/end') reasons.push(event.data.reason) })
  1165. send(agent, 'first')
  1166. await waitForIdle(ctx, agent)
  1167. send(agent, 'second')
  1168. await waitForIdle(ctx, agent)
  1169. expect(reasons).toEqual([{ kind: 'max-tokens' }, { kind: 'completed' }])
  1170. })
  1171. it('does not dispatch tool calls from a max-tokens-truncated step', async () => {
  1172. const callId = ToolCallId('c1')
  1173. const adapter = new MockAdapter([[
  1174. { type: 'block-start', index: 0, blockType: 'tool-call' },
  1175. { type: 'tool-call-delta', index: 0, id: callId, name: 'echo', argumentsDelta: '{"text":"x"}' },
  1176. { type: 'block-end', index: 0, block: { type: 'tool-call', id: callId, name: 'echo', arguments: '{"text":"x"}' } },
  1177. { type: 'usage', usage: { inputTokens: 10, outputTokens: 5 } },
  1178. { type: 'finish', reason: { kind: 'max-tokens' } },
  1179. ]])
  1180. const ctx = await harness(adapter)
  1181. let executions = 0
  1182. ctx.tools.register(defineContentToolFixture({
  1183. name: 'echo',
  1184. description: '',
  1185. parameters: { text: { type: 'string' } },
  1186. async execute() {
  1187. executions += 1
  1188. return [{ type: 'text', text: 'should not run' }]
  1189. },
  1190. }))
  1191. const agent = await ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  1192. const reasons: TurnEndReason[] = []
  1193. ctx.on('session/event', (_s, event) => { if (event.type === 'turn/end') reasons.push(event.data.reason) })
  1194. send(agent, 'go')
  1195. await waitForIdle(ctx, agent)
  1196. expect(executions).toBe(0)
  1197. expect(agent.session.snapshotEvents().some(e => e.type === 'tool/call')).toBe(false)
  1198. expect(agent.session.deriveMessages()).toEqual([{
  1199. id: expect.any(String) as unknown,
  1200. role: 'user',
  1201. content: [{ type: 'text', text: 'go' }],
  1202. source: { kind: 'user' },
  1203. }])
  1204. expect(reasons).toEqual([{ kind: 'max-tokens' }])
  1205. // Empty content still needs an assistant/message to carry usage; derivation
  1206. // skips that host so it does not create a spurious assistant turn.
  1207. const assistantMessage = agent.session.snapshotEvents().find(e => e.type === 'assistant/message')
  1208. expect(assistantMessage?.type === 'assistant/message' && assistantMessage.data).toMatchObject({
  1209. turn: 1,
  1210. step: 1,
  1211. message: {
  1212. id: expect.any(String) as unknown,
  1213. role: 'assistant',
  1214. content: [],
  1215. source: { kind: 'model', provider: 'mock', model: 'mock' },
  1216. },
  1217. usage: { inputTokens: 10, outputTokens: 5 },
  1218. })
  1219. })
  1220. it('appends an empty completion anchor for a max-tokens step with no usage', async () => {
  1221. // The truncated tool call is dropped from durable content, while the
  1222. // successful provider call still needs an exact replay anchor.
  1223. const callId = ToolCallId('c1')
  1224. const adapter = new MockAdapter([[
  1225. { type: 'block-start', index: 0, blockType: 'tool-call' },
  1226. { type: 'tool-call-delta', index: 0, id: callId, name: 'echo', argumentsDelta: '{"text":"x"}' },
  1227. { type: 'block-end', index: 0, block: { type: 'tool-call', id: callId, name: 'echo', arguments: '{"text":"x"}' } },
  1228. { type: 'finish', reason: { kind: 'max-tokens' } },
  1229. ]])
  1230. const ctx = await harness(adapter)
  1231. ctx.tools.register(defineContentToolFixture({
  1232. name: 'echo',
  1233. description: '',
  1234. parameters: { text: { type: 'string' } },
  1235. async execute() { return [{ type: 'text', text: 'should not run' }] },
  1236. }))
  1237. const agent = await ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  1238. const reasons: TurnEndReason[] = []
  1239. ctx.on('session/event', (_s, event) => { if (event.type === 'turn/end') reasons.push(event.data.reason) })
  1240. send(agent, 'go')
  1241. await waitForIdle(ctx, agent)
  1242. expect(reasons).toEqual([{ kind: 'max-tokens' }])
  1243. const assistant = agent.session.snapshotEvents().find(e => e.type === 'assistant/message')!
  1244. expect(assistant.type === 'assistant/message' && assistant.data).toMatchObject({
  1245. turn: 1,
  1246. step: 1,
  1247. message: {
  1248. id: expect.any(String) as unknown,
  1249. role: 'assistant',
  1250. content: [],
  1251. source: { kind: 'model', provider: 'mock', model: 'mock' },
  1252. },
  1253. })
  1254. expect(assistant.sourceEventSeqs).toBeUndefined()
  1255. expect(assistant.type === 'assistant/message' ? assistant.data.stream.length : 0).toBeGreaterThan(0)
  1256. expect(agent.session.deriveMessages()).toEqual([{
  1257. id: expect.any(String) as unknown,
  1258. role: 'user',
  1259. content: [{ type: 'text', text: 'go' }],
  1260. source: { kind: 'user' },
  1261. }])
  1262. })
  1263. it('appends an empty completion anchor for a normal stop with no usage', async () => {
  1264. // A clean content-less call stays absent from derived messages but remains
  1265. // a durable successful-call boundary for replay consumers.
  1266. const adapter = new MockAdapter([[{ type: 'finish', reason: { kind: 'stop' } }]])
  1267. const ctx = await harness(adapter)
  1268. const agent = await ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  1269. const reasons: TurnEndReason[] = []
  1270. ctx.on('session/event', (_s, event) => { if (event.type === 'turn/end') reasons.push(event.data.reason) })
  1271. send(agent, 'go')
  1272. await waitForIdle(ctx, agent)
  1273. expect(reasons).toEqual([{ kind: 'completed' }])
  1274. const assistant = agent.session.snapshotEvents().find(e => e.type === 'assistant/message')!
  1275. expect(assistant.type === 'assistant/message' && assistant.data).toMatchObject({
  1276. turn: 1,
  1277. step: 1,
  1278. message: {
  1279. id: expect.any(String) as unknown,
  1280. role: 'assistant',
  1281. content: [],
  1282. source: { kind: 'model', provider: 'mock', model: 'mock' },
  1283. },
  1284. })
  1285. expect(assistant.sourceEventSeqs).toBeUndefined()
  1286. expect(assistant.type === 'assistant/message' ? assistant.data.stream.length : 0).toBe(1)
  1287. expect(agent.session.deriveMessages()).toEqual([{
  1288. id: expect.any(String) as unknown,
  1289. role: 'user',
  1290. content: [{ type: 'text', text: 'go' }],
  1291. source: { kind: 'user' },
  1292. }])
  1293. })
  1294. it('keeps safe max-tokens assistant content while dropping truncated tool calls', async () => {
  1295. const callId = ToolCallId('c1')
  1296. const adapter = new MockAdapter([[
  1297. { type: 'block-start', index: 0, blockType: 'text' },
  1298. { type: 'text-delta', index: 0, text: 'partial text' },
  1299. { type: 'block-end', index: 0, block: { type: 'text', text: 'partial text' } },
  1300. { type: 'block-start', index: 1, blockType: 'tool-call' },
  1301. { type: 'tool-call-delta', index: 1, id: callId, name: 'echo', argumentsDelta: '{"text"' },
  1302. {
  1303. type: 'finish',
  1304. reason: { kind: 'max-tokens' },
  1305. replayState: { response: { responseId: 'resp-1' }, blocks: ['text-meta', 'tool-meta'] },
  1306. },
  1307. ], textResponse('continued')])
  1308. const ctx = await harness(adapter)
  1309. const agent = await ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  1310. send(agent, 'go')
  1311. await waitForIdle(ctx, agent)
  1312. send(agent, 'continue')
  1313. await waitForIdle(ctx, agent)
  1314. expect(agent.session.snapshotEvents().some(e => e.type === 'tool/call')).toBe(false)
  1315. // The follow-up request replays the truncated message with its replay
  1316. // metadata pruned in step with the dropped tool call.
  1317. expect(adapter.requests[1]?.messages[1]?.source).toEqual({
  1318. kind: 'model',
  1319. provider: 'mock',
  1320. model: 'mock',
  1321. replayState: { response: { responseId: 'resp-1' }, blocks: ['text-meta'] },
  1322. })
  1323. expect(agent.session.deriveMessages()).toEqual([
  1324. {
  1325. id: expect.any(String) as unknown,
  1326. role: 'user',
  1327. content: [{ type: 'text', text: 'go' }],
  1328. source: { kind: 'user' },
  1329. },
  1330. {
  1331. id: expect.any(String) as unknown,
  1332. role: 'assistant',
  1333. content: [{ type: 'text', text: 'partial text' }],
  1334. source: {
  1335. kind: 'model',
  1336. provider: 'mock',
  1337. model: 'mock',
  1338. replayState: { response: { responseId: 'resp-1' }, blocks: ['text-meta'] },
  1339. },
  1340. },
  1341. {
  1342. id: expect.any(String) as unknown,
  1343. role: 'user',
  1344. content: [{ type: 'text', text: 'continue' }],
  1345. source: { kind: 'user' },
  1346. },
  1347. {
  1348. id: expect.any(String) as unknown,
  1349. role: 'assistant',
  1350. content: [{ type: 'text', text: 'continued' }],
  1351. source: { kind: 'model', provider: 'mock', model: 'mock' },
  1352. },
  1353. ])
  1354. })
  1355. it('contains a step/end observer failure without changing continuation', async () => {
  1356. const adapter = new MockAdapter([
  1357. toolCallResponse('c1', 'echo', { text: 'x' }),
  1358. textResponse('continued after tool call'),
  1359. ])
  1360. const ctx = await harness(adapter)
  1361. ctx.tools.register(defineContentToolFixture({
  1362. name: 'echo',
  1363. description: '',
  1364. parameters: { text: { type: 'string' } },
  1365. async execute(args) {
  1366. return [{ type: 'text', text: String(args.text) }]
  1367. },
  1368. }))
  1369. const agent = await ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  1370. let threw = false
  1371. // Post-commit session observers cannot control the loop. The tool call still
  1372. // drives the second model request, and the turn completes normally.
  1373. ctx.on('session/event', (_session, event) => {
  1374. if (event.type === 'step/end' && !threw) { threw = true; throw new Error('bad step/end listener') }
  1375. })
  1376. send(agent, 'go')
  1377. await waitForIdle(ctx, agent)
  1378. expect(adapter.requests).toHaveLength(2)
  1379. const turnEnd = agent.session.snapshotEvents().findLast(e => e.type === 'turn/end')
  1380. expect(turnEnd?.type === 'turn/end' && turnEnd.data.reason.kind).toBe('completed')
  1381. })
  1382. it('contains a reentrant send attempted during durable inbox publication', async () => {
  1383. const adapter = new MockAdapter([textResponse('first')])
  1384. const ctx = await harness(adapter)
  1385. const agent = await ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  1386. let nested = false
  1387. ctx.on('session/event', (session, event) => {
  1388. if (session !== agent.session || event.type !== 'agent/inbox/spliced'
  1389. || event.data.inserted.length === 0 || nested) return
  1390. nested = true
  1391. send(agent, 'queued listener message')
  1392. })
  1393. const idle = waitForIdle(ctx, agent)
  1394. send(agent, 'outer message')
  1395. await idle
  1396. const turns = agent.session.snapshotEvents().filter(event => event.type === 'turn/start')
  1397. const messages = agent.session.snapshotEvents()
  1398. .filter(event => event.type === 'user/message')
  1399. .map(event => event.data.content)
  1400. expect(turns).toHaveLength(1)
  1401. expect(messages).toEqual([[{ type: 'text', text: 'outer message' }]])
  1402. })
  1403. it('preserves independent turn sources across an adjacent microtask send', async () => {
  1404. const adapter = new MockAdapter([textResponse('first'), textResponse('second')])
  1405. const ctx = await harness(adapter)
  1406. const agent = await ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  1407. const idle = waitForIdle(ctx, agent)
  1408. agent.followup(createUserMessage({ content: [{ type: 'text', text: 'user message' }], source: { kind: 'user' } }))
  1409. await Promise.resolve()
  1410. agent.followup(createUserMessage({ content: [{ type: 'text', text: 'plugin message' }], source: { kind: 'plugin', plugin: 'test' } }))
  1411. await idle
  1412. const turns = agent.session.snapshotEvents().filter(event => event.type === 'turn/start')
  1413. const sources = agent.session.snapshotEvents()
  1414. .filter(event => event.type === 'user/message')
  1415. .map(event => event.data.source)
  1416. expect(turns).toHaveLength(2)
  1417. expect(sources).toEqual([
  1418. { kind: 'user' },
  1419. { kind: 'plugin', plugin: 'test' },
  1420. ])
  1421. })
  1422. it('keeps a session-listener send after dequeue in the following turn', async () => {
  1423. const adapter = new MockAdapter([textResponse('first'), textResponse('second')])
  1424. const ctx = await harness(adapter)
  1425. const agent = await ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  1426. const turns: number[] = []
  1427. ctx.on('session/event', (_s, event) => { if (event.type === 'turn/start') turns.push(event.data.turn) })
  1428. // Queue the second from the first turn's durable Assistant settlement.
  1429. let queued = false
  1430. ctx.on('session/event', (_s, event) => {
  1431. if (event.type === 'assistant/message' && !queued) {
  1432. queued = true
  1433. queueMicrotask(() => { send(agent, 'second message') })
  1434. }
  1435. })
  1436. send(agent, 'first message')
  1437. await waitForIdle(ctx, agent)
  1438. expect(turns).toEqual([1, 2])
  1439. expect(adapter.requests).toHaveLength(2)
  1440. expect(JSON.stringify(adapter.requests[1]!.messages)).toContain('first')
  1441. expect(JSON.stringify(adapter.requests[1]!.messages)).toContain('second message')
  1442. })
  1443. it('keeps a model-adapter callback send in the following turn', async () => {
  1444. const agentRef: { current?: Agent } = {}
  1445. const adapter = new MockAdapter([
  1446. () => {
  1447. const agent = agentRef.current
  1448. if (agent === undefined) throw new Error('model callback ran before agent setup')
  1449. send(agent, 'model callback message')
  1450. return textResponse('first')
  1451. },
  1452. textResponse('second'),
  1453. ])
  1454. const ctx = await harness(adapter)
  1455. const agent = await ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  1456. agentRef.current = agent
  1457. const idle = waitForIdle(ctx, agent)
  1458. send(agent, 'outer message')
  1459. await idle
  1460. const messages = agent.session.snapshotEvents()
  1461. .filter(event => event.type === 'user/message')
  1462. .map(event => event.data.content)
  1463. expect(agent.session.snapshotEvents().filter(event => event.type === 'turn/start')).toHaveLength(2)
  1464. expect(messages).toEqual([
  1465. [{ type: 'text', text: 'outer message' }],
  1466. [{ type: 'text', text: 'model callback message' }],
  1467. ])
  1468. })
  1469. it('records normalized model errors on the turn boundary', async () => {
  1470. const adapter = new MockAdapter([]) // script exhausted → throws
  1471. const ctx = await harness(adapter)
  1472. const agent = await ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  1473. const errors: unknown[] = []
  1474. const reasons: TurnEndReason[] = []
  1475. ctx.on('agent/error', ({ error }) => {
  1476. errors.push(error)
  1477. })
  1478. ctx.on('session/event', (_s, event) => { if (event.type === 'turn/end') reasons.push(event.data.reason) })
  1479. send(agent, 'hi')
  1480. await waitForIdle(ctx, agent)
  1481. expect(errors).toHaveLength(1)
  1482. expect(errors[0]).toBeInstanceOf(LlmError)
  1483. expect((errors[0] as LlmError).failure).toEqual({
  1484. message: 'MockAdapter: script exhausted',
  1485. code: 'UNKNOWN',
  1486. })
  1487. expect(reasons[0]).toMatchObject({ kind: 'error' })
  1488. // The durable failure and live relay describe the same failed turn.
  1489. const turnEnd = agent.session.snapshotEvents().find(e => e.type === 'turn/end')
  1490. expect(turnEnd?.type === 'turn/end' && turnEnd.data.reason).toMatchObject({ kind: 'error' })
  1491. })
  1492. it('disposing the loop fiber mid-turn stops the loop (HMR safety)', async () => {
  1493. const adapter = new MockAdapter(['hang'])
  1494. const ctx = await harness(adapter)
  1495. let agent!: Agent
  1496. const fiber = await ctx.plugin(Object.assign(async (inner: Context) => {
  1497. agent = await inner.agentLoop.create(SessionId('scoped'), { provider: 'mock', model: 'mock' })
  1498. }, { inject: ['agentLoop'] }))
  1499. expect(ctx.agents.get(SessionId('scoped'))).toBe(agent)
  1500. send(agent, 'go')
  1501. await new Promise(r => setTimeout(r, 30))
  1502. expect(agent.status).toBe('running')
  1503. await fiber.dispose()
  1504. await driverDone(agent)
  1505. expect(ctx.agents.get(SessionId('scoped'))).toBeUndefined()
  1506. })
  1507. it('creates agents from config on startup', async () => {
  1508. const effort = ReasoningEffortId('high')
  1509. const adapter = new MockAdapter([textResponse('from config')], {
  1510. efforts: [{ id: effort, name: 'High' }],
  1511. defaultEffort: effort,
  1512. })
  1513. const ctx = new Context()
  1514. await ctx.plugin(LlmRuntime)
  1515. await ctx.plugin(SessionStore)
  1516. await ctx.plugin(SessionProjectionRegistry)
  1517. await ctx.plugin(SystemPrompt)
  1518. await ctx.plugin(ToolRuntime)
  1519. await ctx.plugin(AgentRegistry)
  1520. await ctx.plugin(AgentLoop, {
  1521. agents: [{ id: SessionId('config-agent'), provider: 'mock', model: 'mock', reasoningEffort: effort }],
  1522. })
  1523. ctx.llm.registerAdapter(['mock'], adapter)
  1524. const agent = ctx.agents.list()[0]!
  1525. expect(agent).toBeDefined()
  1526. expect(agent.id).toBe(agent.session.id)
  1527. expect(agent.id).toMatch(/^config-agent-session-/)
  1528. expect(agent.options.model).toBe('mock')
  1529. // the agent is alive: send triggers a turn
  1530. send(agent, 'hi')
  1531. await waitForIdle(ctx, agent)
  1532. expect(adapter.requests).toHaveLength(1)
  1533. expect(adapter.requests[0]?.reasoningEffort).toBe(effort)
  1534. const header = agent.session.snapshotEvents().find(event => event.type === 'request/header')
  1535. expect(header?.type === 'request/header' && header.data.header.config.reasoningEffort).toBe(effort)
  1536. })
  1537. it('attaches config agent cwd to the fresh session header', async () => {
  1538. const ctx = new Context()
  1539. await ctx.plugin(LlmRuntime)
  1540. await ctx.plugin(SessionStore)
  1541. await ctx.plugin(SessionProjectionRegistry)
  1542. await ctx.plugin(SystemPrompt)
  1543. await ctx.plugin(ToolRuntime)
  1544. await ctx.plugin(AgentRegistry)
  1545. await ctx.plugin(AgentLoop, {
  1546. agents: [{ id: SessionId('config-agent'), provider: 'mock', model: 'mock', cwd: '/work/project' }],
  1547. })
  1548. const agent = ctx.agents.list()[0]!
  1549. expect(agent.session.header.cwd).toBe('/work/project')
  1550. })
  1551. it('replays a session log into an identical derived history', async () => {
  1552. const adapter = new MockAdapter([
  1553. toolCallResponse('c1', 'echo', { text: 'x' }),
  1554. textResponse('done'),
  1555. ])
  1556. const ctx = await harness(adapter)
  1557. ctx.tools.register(defineContentToolFixture({
  1558. name: 'echo',
  1559. description: '',
  1560. parameters: { text: { type: 'string' } },
  1561. async execute(args) {
  1562. return [{ type: 'text', text: String(args.text) }]
  1563. },
  1564. }))
  1565. const agent = await ctx.agentLoop.create(SessionId('a1'), { provider: 'mock', model: 'mock' })
  1566. send(agent, 'run')
  1567. await waitForIdle(ctx, agent)
  1568. const replayed = ctx.sessions.create(SessionId('replayed'), { seed: [...agent.session.snapshotEvents()] })
  1569. expect(replayed.deriveMessages()).toEqual(agent.session.deriveMessages())
  1570. // event-by-event identity of types over the inherited prefix
  1571. expect(replayed.snapshotEvents().slice(0, agent.session.seq).map(e => e.type)).toEqual(
  1572. agent.session.snapshotEvents().map(e => e.type))
  1573. expect(replayed.snapshotEvents().at(-1)?.type).toBe('session/end-seed')
  1574. })
  1575. })