interception.spec.ts 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. import { describe, expect, it } from 'vitest'
  2. import { Context } from 'cordis'
  3. import LlmService, { CallId, type Message } from '@deepseek-ai/dsh-llm'
  4. import SessionStore, { type SessionEvent, type TurnEndReason } from '@deepseek-ai/dsh-session'
  5. import SystemPrompt from '@deepseek-ai/dsh-system-prompt'
  6. import ToolRegistry, { defineTool, type PostToolDecision, type PreToolDecision } from '@deepseek-ai/dsh-tools'
  7. import AgentRegistry, {
  8. AgentId,
  9. type ContinuationDecision,
  10. type PromptDecision,
  11. type SessionStartSource,
  12. } from '@deepseek-ai/dsh-agent'
  13. import AgentLoop, { type ReactLoopAgent } from '@deepseek-ai/dsh-agent-loop'
  14. import { MockAdapter, textResponse, toolCallResponse } from './mock-adapter.ts'
  15. /**
  16. * The interception seams introduced by the hooks taxonomy: `agent/prompt-submit`,
  17. * `agent/session-start`, the reshaped `agent/turn-continuation`
  18. * ({@link ContinuationDecision}), and the `tools/pre-execute` / `tools/post-execute`
  19. * split with `additionalContext` buffering. These verify the canonical event
  20. * surface a hook bridge (or a native plugin) programs against, WITHOUT any
  21. * external protocol — a native plugin uses the typed decisions directly.
  22. */
  23. async function harness(adapter: MockAdapter) {
  24. const ctx = new Context()
  25. await ctx.plugin(LlmService)
  26. await ctx.plugin(SessionStore)
  27. await ctx.plugin(SystemPrompt)
  28. await ctx.plugin(ToolRegistry)
  29. await ctx.plugin(AgentRegistry)
  30. await ctx.plugin(AgentLoop, { agents: [] })
  31. ctx.llm.registerAdapter(['mock'], adapter)
  32. return ctx
  33. }
  34. function waitForIdle(ctx: Context, agent: ReactLoopAgent): Promise<void> {
  35. return new Promise((resolve) => {
  36. const dispose = ctx.on('agent/status', (subject, status) => {
  37. if (subject === agent && status === 'idle') {
  38. dispose()
  39. resolve()
  40. }
  41. })
  42. })
  43. }
  44. function send(agent: ReactLoopAgent, text: string) {
  45. agent.send([{ type: 'text', text }])
  46. }
  47. function events(agent: ReactLoopAgent): SessionEvent[] {
  48. return [...agent.session.events]
  49. }
  50. describe('agent/prompt-submit', () => {
  51. it('allow (default via next) records the user/message unchanged', async () => {
  52. const adapter = new MockAdapter([textResponse('ok')])
  53. const ctx = await harness(adapter)
  54. const agent = ctx.agentLoop.create(AgentId('a1'), { model: 'mock' })
  55. const seen: string[] = []
  56. ctx.on('agent/prompt-submit', async (_agent, content, _source, next) => {
  57. seen.push(content.map(b => (b.type === 'text' ? b.text : '')).join(''))
  58. return next()
  59. })
  60. send(agent, 'hello')
  61. await waitForIdle(ctx, agent)
  62. expect(seen).toEqual(['hello'])
  63. const userMsg = events(agent).find(e => e.type === 'user/message')
  64. expect(userMsg?.type === 'user/message' && userMsg.data.content).toEqual([{ type: 'text', text: 'hello' }])
  65. })
  66. it('allow with content REWRITES the prompt before it is recorded', async () => {
  67. const adapter = new MockAdapter([textResponse('ok')])
  68. const ctx = await harness(adapter)
  69. const agent = ctx.agentLoop.create(AgentId('a1'), { model: 'mock' })
  70. ctx.on('agent/prompt-submit', async (): Promise<PromptDecision> =>
  71. ({ kind: 'allow', content: [{ type: 'text', text: 'REWRITTEN' }] }))
  72. send(agent, 'original')
  73. await waitForIdle(ctx, agent)
  74. const userMsg = events(agent).find(e => e.type === 'user/message')
  75. expect(userMsg?.type === 'user/message' && userMsg.data.content).toEqual([{ type: 'text', text: 'REWRITTEN' }])
  76. // the rewritten prompt is what reached the model
  77. expect(JSON.stringify(adapter.requests[0]!.messages)).toContain('REWRITTEN')
  78. expect(JSON.stringify(adapter.requests[0]!.messages)).not.toContain('original')
  79. })
  80. it('allow with additionalContext injects a separate context/message into the turn', async () => {
  81. const adapter = new MockAdapter([textResponse('ok')])
  82. const ctx = await harness(adapter)
  83. const agent = ctx.agentLoop.create(AgentId('a1'), { model: 'mock' })
  84. ctx.on('agent/prompt-submit', async (): Promise<PromptDecision> =>
  85. ({
  86. kind: 'allow',
  87. additionalContext: { content: [{ type: 'text', text: 'extra ctx' }], source: { kind: 'plugin', plugin: 'test' } },
  88. }))
  89. send(agent, 'go')
  90. await waitForIdle(ctx, agent)
  91. const log = events(agent)
  92. const userMsg = log.find(e => e.type === 'user/message')
  93. const ctxMsg = log.find(e => e.type === 'context/message')
  94. expect(userMsg).toBeDefined()
  95. expect(ctxMsg?.type === 'context/message' && ctxMsg.data.content).toEqual([{ type: 'text', text: 'extra ctx' }])
  96. expect(ctxMsg?.type === 'context/message' && ctxMsg.data.source).toEqual({ kind: 'plugin', plugin: 'test' })
  97. // both the prompt and the injected context reach the model
  98. const sent = JSON.stringify(adapter.requests[0]!.messages)
  99. expect(sent).toContain('extra ctx')
  100. })
  101. it('a prompt-submit rewrite + additionalContext is VISIBLE to the agent/pre-step seam (merged ordering)', async () => {
  102. // Prompt rewrites and injected context land before `agent/pre-step`, so a
  103. // compaction listener measures the current surface before the single derive.
  104. const adapter = new MockAdapter([textResponse('ok')])
  105. const ctx = await harness(adapter)
  106. const agent = ctx.agentLoop.create(AgentId('a1'), { model: 'mock' })
  107. ctx.on('agent/prompt-submit', async (): Promise<PromptDecision> =>
  108. ({
  109. kind: 'allow',
  110. content: [{ type: 'text', text: 'REWRITTEN prompt' }],
  111. additionalContext: { content: [{ type: 'text', text: 'injected ctx' }], source: { kind: 'plugin', plugin: 'test' } },
  112. }))
  113. // The pre-step seam (where compaction lives) derives the surface it would act
  114. // on. Capture what it sees on the first step.
  115. let preStepDerived: string | undefined
  116. ctx.on('agent/pre-step', (subject, _turn, step) => {
  117. if (subject === agent && step === 1) preStepDerived = JSON.stringify(subject.session.deriveMessages())
  118. })
  119. send(agent, 'ORIGINAL prompt')
  120. await waitForIdle(ctx, agent)
  121. // The pre-step seam ran and saw BOTH the rewrite (not the original) and the
  122. // injected context — i.e. the prompt-submit effects landed before it.
  123. expect(preStepDerived).toBeDefined()
  124. expect(preStepDerived).toContain('REWRITTEN prompt')
  125. expect(preStepDerived).toContain('injected ctx')
  126. expect(preStepDerived).not.toContain('ORIGINAL prompt')
  127. })
  128. it('block drops the (only) prompt → zero-step turn ends rejected, model never called', async () => {
  129. const adapter = new MockAdapter([textResponse('should not run')])
  130. const ctx = await harness(adapter)
  131. const agent = ctx.agentLoop.create(AgentId('a1'), { model: 'mock' })
  132. ctx.on('agent/prompt-submit', async (): Promise<PromptDecision> =>
  133. ({ kind: 'block', reason: 'blocked by policy' }))
  134. const reasons: TurnEndReason[] = []
  135. ctx.on('session/event', (_s, event: SessionEvent) => { if (event.type === 'turn/end') reasons.push(event.data.reason) })
  136. send(agent, 'do something')
  137. await waitForIdle(ctx, agent)
  138. // the model was never called
  139. expect(adapter.requests).toHaveLength(0)
  140. // the turn opened and closed balanced, with no user/message and no step
  141. const log = events(agent)
  142. expect(log.some(e => e.type === 'turn/start')).toBe(true)
  143. expect(log.some(e => e.type === 'turn/end')).toBe(true)
  144. expect(log.some(e => e.type === 'user/message')).toBe(false)
  145. expect(log.some(e => e.type === 'step/start')).toBe(false)
  146. // the veto is recorded durably as a prompt/blocked in the open turn
  147. const blocked = log.find(e => e.type === 'prompt/blocked')
  148. expect(blocked?.type === 'prompt/blocked' && blocked.data).toMatchObject({
  149. content: [{ type: 'text', text: 'do something' }],
  150. reason: 'blocked by policy',
  151. })
  152. // ended rejected with the block reason
  153. expect(reasons).toEqual([{ kind: 'rejected', reason: 'blocked by policy' }])
  154. const turnEnd = log.findLast(e => e.type === 'turn/end')
  155. expect(turnEnd?.type === 'turn/end' && turnEnd.data.reason).toEqual({ kind: 'rejected', reason: 'blocked by policy' })
  156. })
  157. it('adjacent blocked and allowed prompts keep independent turn outcomes', async () => {
  158. const adapter = new MockAdapter([textResponse('ran once')])
  159. const ctx = await harness(adapter)
  160. const agent = ctx.agentLoop.create(AgentId('a1'), { model: 'mock' })
  161. ctx.on('agent/prompt-submit', async (_agent, content, _source, next): Promise<PromptDecision> => {
  162. const text = content.map(b => (b.type === 'text' ? b.text : '')).join('')
  163. return text === 'secret' ? { kind: 'block', reason: 'policy: no secrets' } : next()
  164. })
  165. const reasons: TurnEndReason[] = []
  166. ctx.on('session/event', (_s, event: SessionEvent) => { if (event.type === 'turn/end') reasons.push(event.data.reason) })
  167. // Both sends land before the driver wakes, but each remains its own turn.
  168. send(agent, 'secret')
  169. send(agent, 'safe')
  170. await waitForIdle(ctx, agent)
  171. const log = events(agent)
  172. // The allowed prompt became a user/message and drove exactly one model call.
  173. const userMsgs = log.filter(e => e.type === 'user/message')
  174. expect(userMsgs).toHaveLength(1)
  175. expect(userMsgs[0]?.type === 'user/message' && userMsgs[0].data.content).toEqual([{ type: 'text', text: 'safe' }])
  176. expect(adapter.requests.length).toBeGreaterThanOrEqual(1)
  177. // the blocked prompt is durably recorded, with its content + reason
  178. const blocked = log.filter(e => e.type === 'prompt/blocked')
  179. expect(blocked).toHaveLength(1)
  180. expect(blocked[0]?.type === 'prompt/blocked' && blocked[0].data).toMatchObject({
  181. content: [{ type: 'text', text: 'secret' }],
  182. reason: 'policy: no secrets',
  183. })
  184. expect(log.filter(e => e.type === 'turn/start')).toHaveLength(2)
  185. expect(reasons).toEqual([
  186. { kind: 'rejected', reason: 'policy: no secrets' },
  187. { kind: 'completed' },
  188. ])
  189. })
  190. it('a throwing prompt-submit listener ends its turn balanced while an adjacent message survives', async () => {
  191. const adapter = new MockAdapter([textResponse('after')])
  192. const ctx = await harness(adapter)
  193. const agent = ctx.agentLoop.create(AgentId('a1'), { model: 'mock' })
  194. let threw = false
  195. ctx.on('agent/prompt-submit', async () => {
  196. if (!threw) { threw = true; throw new Error('prompt hook broke') }
  197. return { kind: 'allow' as const }
  198. })
  199. const errors: Error[] = []
  200. ctx.on('agent/error', (_a, _t, _s, error) => void errors.push(error))
  201. const idle = waitForIdle(ctx, agent)
  202. send(agent, 'first')
  203. send(agent, 'second')
  204. await idle
  205. expect(errors.map(e => e.message)).toEqual(['prompt hook broke'])
  206. // The failed prompt owns one balanced error turn; the adjacent prompt owns
  207. // the following normal turn without an intermediate idle transition.
  208. const log = events(agent)
  209. expect(log.filter(e => e.type === 'turn/start')).toHaveLength(2)
  210. expect(log.filter(e => e.type === 'turn/end')).toHaveLength(2)
  211. expect(adapter.requests).toHaveLength(1)
  212. expect(JSON.stringify(adapter.requests[0]!.messages)).toContain('second')
  213. })
  214. })
  215. describe('agent/session-start', () => {
  216. it('fires once with source "startup" for a fresh create, before the first turn', async () => {
  217. const adapter = new MockAdapter([textResponse('ok')])
  218. const ctx = await harness(adapter)
  219. const sources: SessionStartSource[] = []
  220. ctx.on('agent/session-start', (_agent, source) => void sources.push(source))
  221. const agent = ctx.agentLoop.create(AgentId('a1'), { model: 'mock' })
  222. // fires synchronously at create, before any turn
  223. expect(sources).toEqual(['startup'])
  224. expect(events(agent).some(e => e.type === 'turn/start')).toBe(false)
  225. send(agent, 'go')
  226. await waitForIdle(ctx, agent)
  227. // still only one session-start
  228. expect(sources).toEqual(['startup'])
  229. })
  230. it('a session-start listener can inject context the first request sees', async () => {
  231. const adapter = new MockAdapter([textResponse('ok')])
  232. const ctx = await harness(adapter)
  233. ctx.on('agent/session-start', (agent) => {
  234. agent.inject([{ type: 'text', text: 'session preamble' }], { source: { kind: 'plugin', plugin: 'test' } })
  235. })
  236. const agent = ctx.agentLoop.create(AgentId('a1'), { model: 'mock' })
  237. send(agent, 'go')
  238. await waitForIdle(ctx, agent)
  239. // the injected context reached the model on the first (only) request
  240. expect(JSON.stringify(adapter.requests[0]!.messages)).toContain('session preamble')
  241. // and is recorded with the plugin source, never mislabeled as a user prompt
  242. const ctxMsg = events(agent).find(e => e.type === 'context/message')
  243. expect(ctxMsg?.type === 'context/message' && ctxMsg.data.source).toEqual({ kind: 'plugin', plugin: 'test' })
  244. })
  245. it('a throwing session-start listener does not abort agent construction', async () => {
  246. const adapter = new MockAdapter([textResponse('ok')])
  247. const ctx = await harness(adapter)
  248. ctx.on('agent/session-start', () => { throw new Error('session-start hook broke') })
  249. // create must not throw — the listener error is contained/logged
  250. const agent = ctx.agentLoop.create(AgentId('a1'), { model: 'mock' })
  251. expect(agent.id).toBe(AgentId('a1'))
  252. // and the agent still runs
  253. send(agent, 'go')
  254. await waitForIdle(ctx, agent)
  255. expect(adapter.requests).toHaveLength(1)
  256. })
  257. })
  258. describe('agent/session-prefix', () => {
  259. it('dispatches to global and matching agent-scope listeners only', async () => {
  260. const adapter = new MockAdapter([textResponse('a done'), textResponse('b done')])
  261. const ctx = await harness(adapter)
  262. const agentA = ctx.agentLoop.create(AgentId('prefix-a'), { model: 'mock' })
  263. const agentB = ctx.agentLoop.create(AgentId('prefix-b'), { model: 'mock' })
  264. const seen: string[] = []
  265. ctx.on('agent/session-prefix', async (agent, _prefix, _signal, next) => {
  266. seen.push(`global:${agent.id}`)
  267. return next()
  268. })
  269. agentA.ctx.on('agent/session-prefix', async (agent, _prefix, _signal, next) => {
  270. seen.push(`a:${agent.id}`)
  271. return next()
  272. })
  273. agentB.ctx.on('agent/session-prefix', async (agent, _prefix, _signal, next) => {
  274. seen.push(`b:${agent.id}`)
  275. return next()
  276. })
  277. send(agentA, 'run a')
  278. await waitForIdle(ctx, agentA)
  279. send(agentB, 'run b')
  280. await waitForIdle(ctx, agentB)
  281. expect(seen).toEqual([
  282. 'global:prefix-a', 'a:prefix-a',
  283. 'global:prefix-b', 'b:prefix-b',
  284. ])
  285. })
  286. it('composes once per loop instance and fronts every request; the header records it; history stays untouched', async () => {
  287. const adapter = new MockAdapter([
  288. toolCallResponse('c1', 'echo', { text: 'ping' }),
  289. textResponse('done'),
  290. textResponse('again'),
  291. ])
  292. const ctx = await harness(adapter)
  293. ctx.tools.register(defineTool({
  294. name: 'echo', description: 'echo', parameters: { text: { type: 'string' } },
  295. async execute(args) { return [{ type: 'text', text: String(args.text) }] },
  296. }))
  297. const agent = ctx.agentLoop.create(AgentId('a1'), { model: 'mock' })
  298. const reminder: Message = { role: 'user', content: [{ type: 'text', text: '<system-reminder>catalog</system-reminder>' }] }
  299. let composed = 0
  300. ctx.on('agent/session-prefix', async (_agent, _prefix, _signal, next): Promise<Message[]> => {
  301. composed += 1
  302. return [...await next(), reminder]
  303. })
  304. send(agent, 'go')
  305. await waitForIdle(ctx, agent)
  306. send(agent, 'next turn')
  307. await waitForIdle(ctx, agent)
  308. // Three requests (two turns), ONE composition: the frozen product is
  309. // reused verbatim, so the prefix cannot drift mid-session.
  310. expect(adapter.requests).toHaveLength(3)
  311. expect(composed).toBe(1)
  312. for (const request of adapter.requests) {
  313. expect(request.messages[0]).toEqual(reminder)
  314. }
  315. // The anchoring snapshot is the prefix's durable record — and the ONLY
  316. // header event: reuse means no request/header-delta ever.
  317. const headerEvents = events(agent).filter(e => e.type === 'request/header' || e.type === 'request/header-delta')
  318. expect(headerEvents).toHaveLength(1)
  319. expect(headerEvents[0]?.type === 'request/header' && headerEvents[0].data.header.messagePrefix).toEqual([reminder])
  320. // Never session history: the derivation starts at the real user prompt.
  321. expect(agent.session.deriveMessages()[0]).toEqual({ role: 'user', content: [{ type: 'text', text: 'go' }] })
  322. })
  323. it('composes before the first pre-step and hands the prefix to the seam (pressure gates see the real value)', async () => {
  324. const adapter = new MockAdapter([textResponse('ok')])
  325. const ctx = await harness(adapter)
  326. const agent = ctx.agentLoop.create(AgentId('a1'), { model: 'mock' })
  327. const reminder: Message = { role: 'user', content: [{ type: 'text', text: 'opener' }] }
  328. const order: string[] = []
  329. ctx.on('agent/session-prefix', async (_agent, _prefix, _signal, next): Promise<Message[]> => {
  330. order.push('compose')
  331. return [reminder, ...await next()]
  332. })
  333. const seen: (readonly Message[])[] = []
  334. ctx.on('agent/pre-step', (_agent, _turn, _step, _system, sessionPrefix) => {
  335. order.push('pre-step')
  336. seen.push(sessionPrefix)
  337. })
  338. send(agent, 'hi')
  339. await waitForIdle(ctx, agent)
  340. // Composition precedes the pre-step seam, and the seam receives THIS
  341. // instance's composed prefix — a token-pressure gate (compaction) counts
  342. // what the request will actually carry, never a stale logged prefix.
  343. expect(order).toEqual(['compose', 'pre-step'])
  344. expect(seen[0]).toEqual([reminder])
  345. })
  346. it('the canonical prepend pattern composes contributions in registration order', async () => {
  347. const adapter = new MockAdapter([textResponse('ok')])
  348. const ctx = await harness(adapter)
  349. const agent = ctx.agentLoop.create(AgentId('a1'), { model: 'mock' })
  350. // Both listeners use the canonical `[mine, ...await next()]` prepend: the
  351. // waterfall unwinds innermost-first (the second listener's array is built
  352. // first), so prepending puts the FIRST-registered contribution first.
  353. ctx.on('agent/session-prefix', async (_agent, _prefix, _signal, next): Promise<Message[]> => {
  354. return [{ role: 'user', content: [{ type: 'text', text: 'first' }] }, ...await next()]
  355. })
  356. ctx.on('agent/session-prefix', async (_agent, _prefix, _signal, next): Promise<Message[]> => {
  357. return [{ role: 'user', content: [{ type: 'text', text: 'second' }] }, ...await next()]
  358. })
  359. send(agent, 'hi')
  360. await waitForIdle(ctx, agent)
  361. const texts = adapter.requests[0]!.messages.map(m => m.content[0]?.type === 'text' ? m.content[0].text : '')
  362. expect(texts).toEqual(['first', 'second', 'hi'])
  363. })
  364. it('with no contributions the header omits messagePrefix and the request is the bare derivation', async () => {
  365. const adapter = new MockAdapter([textResponse('ok')])
  366. const ctx = await harness(adapter)
  367. const agent = ctx.agentLoop.create(AgentId('a1'), { model: 'mock' })
  368. // A listener that delegates without contributing — the canonical no-op.
  369. ctx.on('agent/session-prefix', async (_agent, _prefix, _signal, next) => next())
  370. send(agent, 'hi')
  371. await waitForIdle(ctx, agent)
  372. const headerEvent = events(agent).find(e => e.type === 'request/header')
  373. expect(headerEvent?.type === 'request/header' && 'messagePrefix' in headerEvent.data.header).toBe(false)
  374. expect(adapter.requests[0]!.messages).toEqual([{ role: 'user', content: [{ type: 'text', text: 'hi' }] }])
  375. })
  376. it('the frozen seed rejects in-place mutation — a contribution is a returned extension', async () => {
  377. const adapter = new MockAdapter([textResponse('ok')])
  378. const ctx = await harness(adapter)
  379. const agent = ctx.agentLoop.create(AgentId('a1'), { model: 'mock' })
  380. let mutationError: unknown
  381. ctx.on('agent/session-prefix', async (_agent, prefix, _signal, next): Promise<Message[]> => {
  382. try {
  383. prefix.push({ role: 'user', content: [{ type: 'text', text: 'smuggled' }] })
  384. } catch (error: unknown) {
  385. mutationError = error
  386. }
  387. return next()
  388. })
  389. send(agent, 'hi')
  390. await waitForIdle(ctx, agent)
  391. expect(mutationError).toBeInstanceOf(TypeError)
  392. expect(adapter.requests[0]!.messages).toEqual([{ role: 'user', content: [{ type: 'text', text: 'hi' }] }])
  393. })
  394. it('mutating a listener-held reference after composition cannot alter later requests (the cache is a frozen clone)', async () => {
  395. const adapter = new MockAdapter([
  396. toolCallResponse('c1', 'echo', { text: 'ping' }),
  397. textResponse('done'),
  398. ])
  399. const ctx = await harness(adapter)
  400. ctx.tools.register(defineTool({
  401. name: 'echo', description: 'echo', parameters: { text: { type: 'string' } },
  402. async execute(args) { return [{ type: 'text', text: String(args.text) }] },
  403. }))
  404. const agent = ctx.agentLoop.create(AgentId('a1'), { model: 'mock' })
  405. const held: Message = { role: 'user', content: [{ type: 'text', text: 'v1' }] }
  406. ctx.on('agent/session-prefix', async (_agent, _prefix, _signal, next): Promise<Message[]> => [...await next(), held])
  407. send(agent, 'go')
  408. await waitForIdle(ctx, agent)
  409. // The listener mutates the object it contributed AFTER composition; the
  410. // cached prefix is a deep-frozen clone, so step 2's request is unchanged.
  411. held.content = [{ type: 'text', text: 'v2' }]
  412. expect(adapter.requests[1]!.messages[0]).toEqual({ role: 'user', content: [{ type: 'text', text: 'v1' }] })
  413. expect(events(agent).filter(e => e.type === 'request/header-delta')).toHaveLength(0)
  414. })
  415. })
  416. describe('agent/turn-continuation (ContinuationDecision)', () => {
  417. it('a continue decision with a reason records next-step steering in the same turn', async () => {
  418. const adapter = new MockAdapter([textResponse('step 1 no tools'), textResponse('step 2')])
  419. const ctx = await harness(adapter)
  420. const agent = ctx.agentLoop.create(AgentId('a1'), { model: 'mock' })
  421. let forced = false
  422. ctx.on('agent/turn-continuation', async (_agent, _turn, _default, next): Promise<ContinuationDecision> => {
  423. if (!forced) {
  424. forced = true
  425. return { action: 'continue', reason: { content: [{ type: 'text', text: 'keep going on the goal' }], source: { kind: 'plugin', plugin: 'goal' } } }
  426. }
  427. return next()
  428. })
  429. send(agent, 'go')
  430. await waitForIdle(ctx, agent)
  431. const log = events(agent)
  432. // The continuation stays in the turn, is logged with provenance before step 2,
  433. // and reaches that step's request.
  434. expect(log.filter(e => e.type === 'turn/start')).toHaveLength(1)
  435. expect(log.filter(e => e.type === 'step/start')).toHaveLength(2)
  436. const steering = log.find(e => e.type === 'steering/message')
  437. expect(steering?.type === 'steering/message' && steering.data.content).toEqual([{ type: 'text', text: 'keep going on the goal' }])
  438. expect(steering?.type === 'steering/message' && steering.data.source).toEqual({ kind: 'plugin', plugin: 'goal' })
  439. expect(JSON.stringify(adapter.requests[1]!.messages)).toContain('keep going on the goal')
  440. })
  441. it('a stop decision ends the turn even when the step had tool calls', async () => {
  442. const adapter = new MockAdapter([toolCallResponse('c1', 'echo', { text: 'hi' })])
  443. const ctx = await harness(adapter)
  444. ctx.tools.register(defineTool({
  445. name: 'echo', description: 'echo', parameters: { text: { type: 'string' } },
  446. async execute(args) { return [{ type: 'text', text: String(args.text) }] },
  447. }))
  448. const agent = ctx.agentLoop.create(AgentId('a1'), { model: 'mock' })
  449. ctx.on('agent/turn-continuation', async (): Promise<ContinuationDecision> => ({ action: 'stop' }))
  450. send(agent, 'go')
  451. await waitForIdle(ctx, agent)
  452. // default would have continued (had tool calls), but the stop decision wins
  453. expect(adapter.requests).toHaveLength(1)
  454. expect(events(agent).some(e => e.type === 'tool/result')).toBe(true)
  455. })
  456. })
  457. describe('tools/post-execute additionalContext buffering across a multi-call step', () => {
  458. it('appends each call\'s additionalContext only AFTER all tool/results, preserving adjacency', async () => {
  459. // One assistant step with TWO tool calls; the second model response stops.
  460. const twoCalls = [
  461. { type: 'block-start' as const, index: 0, blockType: 'tool-call' as const },
  462. { type: 'block-end' as const, index: 0, block: { type: 'tool-call' as const, id: CallId('c1'), name: 'echo', arguments: '{"text":"a"}' } },
  463. { type: 'block-start' as const, index: 1, blockType: 'tool-call' as const },
  464. { type: 'block-end' as const, index: 1, block: { type: 'tool-call' as const, id: CallId('c2'), name: 'echo', arguments: '{"text":"b"}' } },
  465. { type: 'usage' as const, usage: { inputTokens: 5, outputTokens: 5 } },
  466. { type: 'finish' as const, reason: { kind: 'tool-calls' as const } },
  467. ]
  468. const adapter = new MockAdapter([twoCalls, textResponse('done')])
  469. const ctx = await harness(adapter)
  470. ctx.tools.register(defineTool({
  471. name: 'echo', description: 'echo', parameters: { text: { type: 'string' } },
  472. async execute(args) { return [{ type: 'text', text: String(args.text) }] },
  473. }))
  474. const agent = ctx.agentLoop.create(AgentId('a1'), { model: 'mock' })
  475. // Each call attaches additionalContext naming itself.
  476. ctx.on('tools/post-execute', async (exec, _result): Promise<PostToolDecision> =>
  477. ({ kind: 'accept', additionalContext: { content: [{ type: 'text', text: `ctx-${exec.callId}` }], source: { kind: 'plugin', plugin: 'p' } } }))
  478. send(agent, 'go')
  479. await waitForIdle(ctx, agent)
  480. // Event order in the log: both tool/results, THEN both context/messages —
  481. // never interleaved (which would break tool-call/result adjacency).
  482. const types = events(agent).map(e => e.type)
  483. const firstResult = types.indexOf('tool/result')
  484. const lastResult = types.lastIndexOf('tool/result')
  485. const firstCtx = types.indexOf('context/message')
  486. expect(firstResult).toBeGreaterThanOrEqual(0)
  487. expect(lastResult).toBeGreaterThan(firstResult) // two results
  488. expect(firstCtx).toBeGreaterThan(lastResult) // context only after ALL results
  489. // both contexts present
  490. const ctxTexts = events(agent)
  491. .filter(e => e.type === 'context/message')
  492. .flatMap(e => (e.type === 'context/message' ? e.data.content : []))
  493. .map(b => (b.type === 'text' ? b.text : ''))
  494. expect(ctxTexts).toEqual(['ctx-c1', 'ctx-c2'])
  495. })
  496. })
  497. describe('tools/pre-execute gate (native-plugin permission pattern, end-to-end through the loop)', () => {
  498. it('deny short-circuits dispatch into an isError result the model sees', async () => {
  499. const adapter = new MockAdapter([toolCallResponse('c1', 'danger', {}), textResponse('ok')])
  500. const ctx = await harness(adapter)
  501. let ran = false
  502. ctx.tools.register(defineTool({
  503. name: 'danger', description: 'danger', parameters: {},
  504. async execute() { ran = true; return [{ type: 'text', text: 'should not run' }] },
  505. }))
  506. const agent = ctx.agentLoop.create(AgentId('a1'), { model: 'mock' })
  507. ctx.on('tools/pre-execute', async (exec, next): Promise<PreToolDecision> => {
  508. if (exec.name === 'danger') return { kind: 'deny', reason: 'blocked dangerous tool' }
  509. return next()
  510. })
  511. send(agent, 'go')
  512. await waitForIdle(ctx, agent)
  513. expect(ran).toBe(false)
  514. const result = events(agent).find(e => e.type === 'tool/result')
  515. expect(result?.type === 'tool/result' && result.data.isError).toBe(true)
  516. expect(result?.type === 'tool/result'
  517. && result.data.content.some(b => b.type === 'text' && b.text.includes('blocked dangerous tool'))).toBe(true)
  518. })
  519. })
  520. describe('worked example: a native hook plugin is just a cordis plugin on the seams', () => {
  521. // The whole point of the interception taxonomy: a "native hook" needs no dsh-hook-protocol,
  522. // no external command, no hook/* log — it is an ordinary cordis plugin subscribing to the
  523. // canonical events and returning typed decisions.
  524. const NativeGuard = {
  525. name: 'native-guard',
  526. apply(ctx: Context) {
  527. // 1. SessionStart: seed a standing instruction.
  528. ctx.on('agent/session-start', (agent, source) => {
  529. agent.inject(
  530. [{ type: 'text', text: `policy active (started: ${source})` }],
  531. { source: { kind: 'plugin', plugin: 'native-guard' } },
  532. )
  533. })
  534. // 2. PromptSubmit: block a forbidden prompt, annotate the rest.
  535. ctx.on('agent/prompt-submit', async (_agent, content, _source, next): Promise<PromptDecision> => {
  536. const text = content.map(b => (b.type === 'text' ? b.text : '')).join('')
  537. if (text.includes('rm -rf')) return { kind: 'block', reason: 'destructive prompt blocked' }
  538. return next()
  539. })
  540. // 3. PreToolUse: deny a dangerous tool by name.
  541. ctx.on('tools/pre-execute', async (exec, next): Promise<PreToolDecision> => {
  542. if (exec.name === 'danger') return { kind: 'deny', reason: 'danger tool denied' }
  543. return next()
  544. })
  545. // 4. PostToolUse: attach context after a tool runs.
  546. ctx.on('tools/post-execute', async (_exec, _result, next): Promise<PostToolDecision> => {
  547. const decision = await next()
  548. if (decision.kind === 'accept') {
  549. return { kind: 'accept', additionalContext: { content: [{ type: 'text', text: 'audited' }], source: { kind: 'plugin', plugin: 'native-guard' } } }
  550. }
  551. return decision
  552. })
  553. },
  554. }
  555. it('all four seams fire for a real allowed turn with a tool call', async () => {
  556. const adapter = new MockAdapter([toolCallResponse('c1', 'echo', { text: 'hi' }), textResponse('done')])
  557. const ctx = await harness(adapter)
  558. await ctx.plugin(NativeGuard)
  559. ctx.tools.register(defineTool({
  560. name: 'echo', description: 'echo', parameters: { text: { type: 'string' } },
  561. async execute(args) { return [{ type: 'text', text: String(args.text) }] },
  562. }))
  563. const agent = ctx.agentLoop.create(AgentId('a1'), { model: 'mock' })
  564. send(agent, 'please echo hi')
  565. await waitForIdle(ctx, agent)
  566. const log = events(agent)
  567. // session-start preamble injected
  568. expect(log.some(e => e.type === 'context/message'
  569. && e.data.content.some(b => b.type === 'text' && b.text.includes('policy active (started: startup)')))).toBe(true)
  570. // prompt allowed → user/message recorded
  571. expect(log.some(e => e.type === 'user/message')).toBe(true)
  572. // tool ran (echo allowed) and post-execute attached "audited" context
  573. expect(log.some(e => e.type === 'tool/result' && !e.data.isError)).toBe(true)
  574. expect(log.some(e => e.type === 'context/message'
  575. && e.data.content.some(b => b.type === 'text' && b.text === 'audited'))).toBe(true)
  576. // NO hook/* events — a native plugin needs none
  577. expect(log.some(e => e.type.startsWith('hook/'))).toBe(false)
  578. })
  579. it('the same plugin blocks a destructive prompt → rejected turn, model never called', async () => {
  580. const adapter = new MockAdapter([textResponse('should not run')])
  581. const ctx = await harness(adapter)
  582. await ctx.plugin(NativeGuard)
  583. const agent = ctx.agentLoop.create(AgentId('a2'), { model: 'mock' })
  584. const reasons: TurnEndReason[] = []
  585. ctx.on('session/event', (_s, event: SessionEvent) => { if (event.type === 'turn/end') reasons.push(event.data.reason) })
  586. send(agent, 'run rm -rf /')
  587. await waitForIdle(ctx, agent)
  588. expect(adapter.requests).toHaveLength(0)
  589. expect(reasons).toEqual([{ kind: 'rejected', reason: 'destructive prompt blocked' }])
  590. })
  591. it('HMR-safety: disposing the plugin fiber removes all four listeners', async () => {
  592. const adapter = new MockAdapter([textResponse('ok')])
  593. const ctx = await harness(adapter)
  594. const fiber = await ctx.plugin(NativeGuard)
  595. await fiber.dispose()
  596. // After disposal, a destructive prompt is NOT blocked (the listener is gone).
  597. const agent = ctx.agentLoop.create(AgentId('a3'), { model: 'mock' })
  598. send(agent, 'run rm -rf /')
  599. await waitForIdle(ctx, agent)
  600. // the prompt ran (not rejected) — proving the prompt-submit listener was disposed
  601. expect(adapter.requests).toHaveLength(1)
  602. expect(events(agent).some(e => e.type === 'user/message')).toBe(true)
  603. })
  604. })