loop.spec.ts 67 KB

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