tools.spec.ts 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058
  1. import { describe, expect, expectTypeOf, it } from 'vitest'
  2. import { Context } from 'cordis'
  3. import { CallId, HarnessError } from '@deepseek-ai/dsh-llm'
  4. import SystemPrompt from '@deepseek-ai/dsh-system-prompt'
  5. import type { Agent } from '@deepseek-ai/dsh-agent'
  6. import ApprovalService, { type ApprovalOutcome, type ApprovalRequest } from '@deepseek-ai/dsh-user-approval'
  7. import ToolRegistry, {
  8. defineTool, schemaSpecToJsonSchema, validateArgs, ToolArgsError, ToolNotFoundError,
  9. TOOL_ABORTED, TOOL_ABORTED_BEFORE_DISPATCH,
  10. type InferArgs, type SchemaSpec, type PreToolDecision, type PostToolDecision,
  11. type ToolDispatchExecution, type ToolExecutionResult,
  12. } from '@deepseek-ai/dsh-tools'
  13. const testToolSignal = new AbortController().signal
  14. async function setup() {
  15. const ctx = new Context()
  16. await ctx.plugin(SystemPrompt)
  17. await ctx.plugin(ToolRegistry)
  18. return ctx
  19. }
  20. const echoTool = defineTool({
  21. name: 'echo',
  22. description: 'echo arguments back',
  23. parameters: { text: { type: 'string' } },
  24. async execute(args) {
  25. return [{ type: 'text' as const, text: args.text ?? '' }]
  26. },
  27. })
  28. describe('ToolRegistry', () => {
  29. it('registers tools, exposes schemas, and feeds the system-prompt assembly', async () => {
  30. const ctx = await setup()
  31. ctx.tools.register(echoTool)
  32. expect(ctx.tools.schemas()).toEqual([{
  33. name: 'echo',
  34. description: 'echo arguments back',
  35. parameters: { type: 'object', properties: { text: { type: 'string' } } },
  36. }])
  37. // schemas() result must not leak execute — ToolSchema deliberately has no
  38. // 'execute' key, so widen through unknown to probe for the absent property
  39. expect((ctx.tools.schemas()[0] as unknown as Record<string, unknown>).execute).toBeUndefined()
  40. const assembly = await ctx.systemPrompt.assemble()
  41. expect(assembly.tools.map(t => t.name)).toEqual(['echo'])
  42. })
  43. it('schemas() drops the UI presentation callbacks — they must never reach the model', async () => {
  44. const ctx = await setup()
  45. // A tool that declares presentCall/presentResult (functions). schemas() feeds
  46. // the system-prompt assembly → the model request, so those callbacks (and
  47. // `execute`) must be stripped: a function in the JSON tool schema would
  48. // corrupt the request. schemas() is an explicit allowlist, so it can't leak.
  49. ctx.tools.register(defineTool({
  50. name: 'present',
  51. description: 'has presenters',
  52. parameters: { x: { type: 'string', required: true } },
  53. async execute() { return [] },
  54. presentCall: args => ({ card: 'generic', title: args.x }),
  55. presentResult: (args, result) => ({ card: 'generic', title: args.x, content: result.content }),
  56. }))
  57. const schema = ctx.tools.schemas()[0] as unknown as Record<string, unknown>
  58. expect(Object.keys(schema).sort()).toEqual(['description', 'name', 'parameters'])
  59. expect(schema.presentCall).toBeUndefined()
  60. expect(schema.presentResult).toBeUndefined()
  61. expect(schema.execute).toBeUndefined()
  62. })
  63. it('schemas() excludes timeoutMs — the budget must never reach the model', async () => {
  64. const ctx = await setup()
  65. ctx.tools.register(defineTool({
  66. name: 'budgeted', description: 'has a budget', parameters: {}, timeoutMs: 5_000,
  67. async execute() { return [{ type: 'text' as const, text: 'ok' }] },
  68. }))
  69. const schema = ctx.tools.schemas().find(s => s.name === 'budgeted')
  70. expect(schema).toBeDefined()
  71. expect('timeoutMs' in (schema as object)).toBe(false)
  72. })
  73. it('executes a tool and returns its content', async () => {
  74. const ctx = await setup()
  75. ctx.tools.register(echoTool)
  76. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'echo', arguments: { text: 'hi' } })
  77. expect(result).toEqual({ content: [{ type: 'text', text: 'hi' }], isError: false })
  78. })
  79. it('threads a tool-attached meta (object return form) onto the result', async () => {
  80. const ctx = await setup()
  81. ctx.tools.register({
  82. ...echoTool,
  83. name: 'meta-tool',
  84. async execute() {
  85. return { content: [{ type: 'text', text: 'ok' }], meta: { diffs: [{ path: 'a', oldText: null, newText: 'x' }] } }
  86. },
  87. })
  88. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'meta-tool', arguments: {} })
  89. expect(result).toEqual({
  90. content: [{ type: 'text', text: 'ok' }],
  91. isError: false,
  92. meta: { diffs: [{ path: 'a', oldText: null, newText: 'x' }] },
  93. })
  94. })
  95. it('omits meta when the object return form supplies none', async () => {
  96. const ctx = await setup()
  97. ctx.tools.register({
  98. ...echoTool,
  99. name: 'no-meta-tool',
  100. async execute() {
  101. return { content: [{ type: 'text', text: 'ok' }] }
  102. },
  103. })
  104. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'no-meta-tool', arguments: {} })
  105. expect(result).toEqual({ content: [{ type: 'text', text: 'ok' }], isError: false })
  106. expect('meta' in result).toBe(false)
  107. })
  108. it('normalizes a contract-violating non-cloneable result before final notification', async () => {
  109. const ctx = await setup()
  110. let observedError: boolean | undefined
  111. ctx.on('tools/result', (_exec, result) => { observedError = result.isError })
  112. ctx.tools.register({
  113. ...echoTool,
  114. name: 'bad-meta',
  115. async execute() {
  116. return { content: [], meta: () => undefined }
  117. },
  118. })
  119. const result = await ctx.tools.execute({
  120. signal: testToolSignal,
  121. callId: CallId('bad-meta'), name: 'bad-meta', arguments: {},
  122. })
  123. expect(result.isError).toBe(true)
  124. expect(result.content[0]?.type === 'text' && result.content[0].text).toContain('Error:')
  125. expect(observedError).toBe(true)
  126. })
  127. it('returns isError results for unknown tools and throwing tools', async () => {
  128. const ctx = await setup()
  129. ctx.tools.register({
  130. ...echoTool,
  131. name: 'boom',
  132. async execute() {
  133. throw new Error('exploded')
  134. },
  135. })
  136. const unknown = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'nope', arguments: {} })
  137. expect(unknown.isError).toBe(true)
  138. expect(unknown.content[0]).toMatchObject({ text: 'Error: unknown tool "nope"' })
  139. // An unknown tool is a routable failure class, same as a tool-thrown one.
  140. expect(unknown.error).toEqual({ name: 'ToolNotFoundError', code: 'UNKNOWN_TOOL' })
  141. const thrown = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c2'), name: 'boom', arguments: {} })
  142. expect(thrown.isError).toBe(true)
  143. expect(thrown.content[0]).toMatchObject({ text: 'Error: exploded' })
  144. })
  145. it('normalizes a hostile thrown value whose inspection and coercion both throw', async () => {
  146. const ctx = await setup()
  147. ctx.tools.register({
  148. ...echoTool,
  149. name: 'hostile-throw',
  150. async execute() {
  151. throw new Proxy({}, {
  152. getPrototypeOf: () => { throw new Error('prototype trap') },
  153. has: () => { throw new Error('has trap') },
  154. get: () => { throw new Error('get trap') },
  155. })
  156. },
  157. })
  158. await expect(ctx.tools.execute({
  159. signal: testToolSignal,
  160. callId: CallId('hostile'), name: 'hostile-throw', arguments: {},
  161. })).resolves.toMatchObject({
  162. isError: true,
  163. content: [{ type: 'text', text: 'Error: <unprintable thrown value>' }],
  164. })
  165. })
  166. it('ToolNotFoundError carries a stable message and code', async () => {
  167. const { HarnessError } = await import('@deepseek-ai/dsh-llm')
  168. const err = new ToolNotFoundError('ghost')
  169. expect(err).toBeInstanceOf(HarnessError)
  170. expect(err.name).toBe('ToolNotFoundError')
  171. expect(err.code).toBe('UNKNOWN_TOOL')
  172. expect(err.message).toBe('unknown tool "ghost"')
  173. })
  174. it('lets a tools/pre-execute listener deny a call (permission pattern)', async () => {
  175. const ctx = await setup()
  176. ctx.tools.register(echoTool)
  177. ctx.on('tools/pre-execute', async (exec, next): Promise<PreToolDecision> => {
  178. if (exec.name === 'echo') return { kind: 'deny', reason: 'denied by policy' }
  179. return next()
  180. })
  181. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'echo', arguments: { text: 'hi' } })
  182. expect(result.isError).toBe(true)
  183. expect(result.content[0]).toMatchObject({ text: 'Error: denied by policy' })
  184. })
  185. it('an ask decision degrades to deny when no approval seam is mounted', async () => {
  186. const ctx = await setup()
  187. ctx.tools.register(echoTool)
  188. ctx.on('tools/pre-execute', async (_exec, _next): Promise<PreToolDecision> =>
  189. ({ kind: 'ask', reason: 'needs approval' }))
  190. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'echo', arguments: { text: 'hi' } })
  191. expect(result.isError).toBe(true)
  192. expect(result.content[0]).toMatchObject({ text: 'Error: needs approval' })
  193. })
  194. it('an ask decision with no reason degrades to deny with a default message', async () => {
  195. const ctx = await setup()
  196. ctx.tools.register(echoTool)
  197. ctx.on('tools/pre-execute', async (_exec, _next): Promise<PreToolDecision> => ({ kind: 'ask' }))
  198. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'echo', arguments: { text: 'hi' } })
  199. expect(result.isError).toBe(true)
  200. expect(result.content[0]).toMatchObject({ text: 'Error: tool "echo" requires approval (not yet supported)' })
  201. })
  202. describe('ask routing through ctx.approval', () => {
  203. /**
  204. * A minimal Agent stand-in — the approval seam reaches
  205. * `agent.session.append` and folds `.events`; the seeded open turn
  206. * satisfies request()'s enclosure precondition.
  207. */
  208. function fakeAgent(): Agent {
  209. return {
  210. session: { events: [{ type: 'turn/start' }], append: () => ({}) },
  211. } as unknown as Agent
  212. }
  213. async function approvalSetup() {
  214. const ctx = await setup()
  215. await ctx.plugin(ApprovalService)
  216. ctx.tools.register(echoTool)
  217. return ctx
  218. }
  219. it('dispatches the tool when the answerer grants allowed-once, forwarding the ask fields', async () => {
  220. const ctx = await approvalSetup()
  221. const agent = fakeAgent()
  222. const controller = new AbortController()
  223. const seen: ApprovalRequest[] = []
  224. ctx.on('approval/request', (req) => {
  225. seen.push(req)
  226. return Promise.resolve<ApprovalOutcome>('allowed-once')
  227. })
  228. ctx.on('tools/pre-execute', async (_exec, _next): Promise<PreToolDecision> =>
  229. ({ kind: 'ask', reason: 'hook wants a human' }))
  230. const result = await ctx.tools.execute({
  231. callId: CallId('c1'), name: 'echo', arguments: { text: 'hi' }, agent, signal: controller.signal,
  232. })
  233. expect(result).toMatchObject({ isError: false, content: [{ type: 'text', text: 'hi' }] })
  234. expect(seen).toHaveLength(1)
  235. expect(seen[0]).toMatchObject({ agent, toolName: 'echo', callId: 'c1', reason: 'hook wants a human' })
  236. expect(seen[0]?.signal).toBe(controller.signal)
  237. })
  238. it('denies with the user-rejection reason on rejected', async () => {
  239. const ctx = await approvalSetup()
  240. ctx.on('approval/request', () => Promise.resolve<ApprovalOutcome>('rejected'))
  241. ctx.on('tools/pre-execute', async (_exec, _next): Promise<PreToolDecision> => ({ kind: 'ask' }))
  242. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'echo', arguments: {}, agent: fakeAgent() })
  243. expect(result.isError).toBe(true)
  244. expect(result.content[0]).toMatchObject({ text: 'Error: the user rejected tool "echo"' })
  245. })
  246. it('denies with the cancellation reason on cancelled', async () => {
  247. const ctx = await approvalSetup()
  248. ctx.on('approval/request', () => Promise.resolve<ApprovalOutcome>('cancelled'))
  249. ctx.on('tools/pre-execute', async (_exec, _next): Promise<PreToolDecision> => ({ kind: 'ask' }))
  250. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'echo', arguments: {}, agent: fakeAgent() })
  251. expect(result.isError).toBe(true)
  252. expect(result.content[0]).toMatchObject({ text: 'Error: approval for tool "echo" was cancelled' })
  253. })
  254. it('returns ABORTED_BEFORE_DISPATCH when caller cancellation overtakes approval', async () => {
  255. const ctx = await approvalSetup()
  256. const entered = Promise.withResolvers<undefined>()
  257. const release = Promise.withResolvers<ApprovalOutcome>()
  258. let dispatched = 0
  259. ctx.tools.register({
  260. ...echoTool,
  261. name: 'approval-probe',
  262. async execute() { dispatched += 1; return [] },
  263. })
  264. ctx.on('approval/request', () => {
  265. entered.resolve(undefined)
  266. return release.promise
  267. })
  268. ctx.on('tools/pre-execute', async (_exec, _next): Promise<PreToolDecision> => ({ kind: 'ask' }))
  269. const controller = new AbortController()
  270. const pending = ctx.tools.execute({
  271. callId: CallId('approval-cancelled'),
  272. name: 'approval-probe',
  273. arguments: {},
  274. agent: fakeAgent(),
  275. signal: controller.signal,
  276. })
  277. await entered.promise
  278. controller.abort('caller cancelled approval')
  279. release.resolve('allowed-once')
  280. await expect(pending).resolves.toMatchObject({
  281. isError: true,
  282. error: { name: 'AbortError', code: TOOL_ABORTED_BEFORE_DISPATCH },
  283. })
  284. expect(dispatched).toBe(0)
  285. })
  286. it('denies with the no-channel reason when the seam is mounted but nobody answers', async () => {
  287. const ctx = await approvalSetup()
  288. ctx.on('tools/pre-execute', async (_exec, _next): Promise<PreToolDecision> => ({ kind: 'ask' }))
  289. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'echo', arguments: {}, agent: fakeAgent() })
  290. expect(result.isError).toBe(true)
  291. expect(result.content[0]).toMatchObject({ text: 'Error: tool "echo" requires approval, but no approval channel is available' })
  292. })
  293. it('denies an agent-less execution without asking — nothing to route or audit through', async () => {
  294. const ctx = await approvalSetup()
  295. let asked = false
  296. ctx.on('approval/request', () => {
  297. asked = true
  298. return Promise.resolve<ApprovalOutcome>('allowed-once')
  299. })
  300. ctx.on('tools/pre-execute', async (_exec, _next): Promise<PreToolDecision> => ({ kind: 'ask' }))
  301. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'echo', arguments: {} })
  302. expect(asked).toBe(false)
  303. expect(result.isError).toBe(true)
  304. expect(result.content[0]).toMatchObject({ text: 'Error: tool "echo" requires approval, but the call has no agent to route it through' })
  305. })
  306. it('turns a rogue outcome from a NON-conforming approval stand-in into an isError result', async () => {
  307. // ApprovalService normalizes rogue answers itself; this pins the
  308. // registry's own exhaustiveness backstop by shadowing the service with a
  309. // stand-in that violates the outcome contract.
  310. const ctx = await setup()
  311. ctx.tools.register(echoTool)
  312. ctx.provide('approval', { request: () => Promise.resolve('yolo') } as unknown as ApprovalService)
  313. ctx.on('tools/pre-execute', async (_exec, _next): Promise<PreToolDecision> => ({ kind: 'ask' }))
  314. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'echo', arguments: {}, agent: fakeAgent() })
  315. expect(result.isError).toBe(true)
  316. const text = result.content[0]?.type === 'text' ? result.content[0].text : ''
  317. expect(text).toContain('unreachable')
  318. })
  319. })
  320. it('a tools/post-execute listener can replace the result content (accept) ', async () => {
  321. const ctx = await setup()
  322. ctx.tools.register(echoTool)
  323. ctx.on('tools/post-execute', async (_exec, _result, _next): Promise<PostToolDecision> =>
  324. ({ kind: 'accept', content: [{ type: 'text', text: 'rewritten' }] }))
  325. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'echo', arguments: { text: 'hi' } })
  326. expect(result.isError).toBe(false)
  327. expect(result.content[0]).toMatchObject({ text: 'rewritten' })
  328. })
  329. it('a tools/post-execute block turns the call into an isError with corrective feedback', async () => {
  330. const ctx = await setup()
  331. ctx.tools.register(echoTool)
  332. ctx.on('tools/post-execute', async (_exec, _result, _next): Promise<PostToolDecision> =>
  333. ({ kind: 'block', feedback: [{ type: 'text', text: 'output rejected: try again' }] }))
  334. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'echo', arguments: { text: 'hi' } })
  335. expect(result.isError).toBe(true)
  336. expect(result.content[0]).toMatchObject({ text: 'output rejected: try again' })
  337. })
  338. it('a block decision can ALSO attach additionalContexts', async () => {
  339. const ctx = await setup()
  340. ctx.tools.register(echoTool)
  341. ctx.on('tools/post-execute', async (_exec, _result, _next): Promise<PostToolDecision> =>
  342. ({
  343. kind: 'block',
  344. feedback: [{ type: 'text', text: 'rejected' }],
  345. additionalContexts: [{ content: [{ type: 'text', text: 'why it was rejected' }], source: { kind: 'plugin', plugin: 'test' } }],
  346. }))
  347. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'echo', arguments: { text: 'hi' } })
  348. expect(result.isError).toBe(true)
  349. expect(result.content[0]).toMatchObject({ text: 'rejected' })
  350. expect(result.additionalContexts).toMatchObject([{ content: [{ text: 'why it was rejected' }], source: { kind: 'plugin', plugin: 'test' } }])
  351. })
  352. it('post-execute additionalContexts ride on the result for the loop to buffer', async () => {
  353. const ctx = await setup()
  354. ctx.tools.register(echoTool)
  355. ctx.on('tools/post-execute', async (_exec, _result, _next): Promise<PostToolDecision> =>
  356. ({ kind: 'accept', additionalContexts: [{ content: [{ type: 'text', text: 'fyi' }], source: { kind: 'plugin', plugin: 'test' } }] }))
  357. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'echo', arguments: { text: 'hi' } })
  358. expect(result.additionalContexts).toMatchObject([{ content: [{ text: 'fyi' }], source: { kind: 'plugin', plugin: 'test' } }])
  359. })
  360. it('preserves tool-deferred, execute-wrapper, and post-execute contexts in order', async () => {
  361. const ctx = await setup()
  362. ctx.tools.register(defineTool({
  363. name: 'composite',
  364. description: 'composite',
  365. parameters: {},
  366. async execute(_args, exec) {
  367. exec.deferContext({ content: [{ type: 'text', text: 'nested-1' }], source: { kind: 'plugin', plugin: 'nested-1' }, meta: { n: 1 } })
  368. exec.deferContext({ content: [{ type: 'text', text: 'nested-2' }], source: { kind: 'plugin', plugin: 'nested-2' } })
  369. return [{ type: 'text', text: 'done' }]
  370. },
  371. }))
  372. ctx.on('tools/execute', async (_exec, next) => {
  373. const result = await next()
  374. return {
  375. ...result,
  376. additionalContexts: [
  377. ...result.additionalContexts ?? [],
  378. { content: [{ type: 'text', text: 'wrapper' }], source: { kind: 'plugin', plugin: 'wrapper' } },
  379. ],
  380. }
  381. })
  382. ctx.on('tools/post-execute', async (_exec, _result, next): Promise<PostToolDecision> => {
  383. const downstream = await next()
  384. return {
  385. ...downstream,
  386. additionalContexts: [
  387. { content: [{ type: 'text', text: 'post' }], source: { kind: 'plugin', plugin: 'post' } },
  388. ...downstream.additionalContexts ?? [],
  389. ],
  390. }
  391. })
  392. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('composite'), name: 'composite', arguments: {} })
  393. expect(result.additionalContexts?.map(context => context.source)).toEqual([
  394. { kind: 'plugin', plugin: 'nested-1' },
  395. { kind: 'plugin', plugin: 'nested-2' },
  396. { kind: 'plugin', plugin: 'wrapper' },
  397. { kind: 'plugin', plugin: 'post' },
  398. ])
  399. expect(result.additionalContexts?.[0]?.meta).toEqual({ n: 1 })
  400. })
  401. it('keeps deferred contexts when a composite tool throws, but drops them when the outer call is blocked', async () => {
  402. const ctx = await setup()
  403. ctx.tools.register(defineTool({
  404. name: 'failing-composite',
  405. description: 'failing composite',
  406. parameters: {},
  407. async execute(_args, exec) {
  408. exec.deferContext({ content: [{ type: 'text', text: 'nested' }], source: { kind: 'plugin', plugin: 'nested' } })
  409. throw new Error('outer failure')
  410. },
  411. }))
  412. const failed = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('failed'), name: 'failing-composite', arguments: {} })
  413. expect(failed.isError).toBe(true)
  414. expect(failed.additionalContexts?.map(context => context.source)).toEqual([{ kind: 'plugin', plugin: 'nested' }])
  415. ctx.on('tools/post-execute', async (): Promise<PostToolDecision> => ({
  416. kind: 'block',
  417. feedback: [{ type: 'text', text: 'blocked' }],
  418. additionalContexts: [{ content: [{ type: 'text', text: 'block-only' }], source: { kind: 'plugin', plugin: 'blocker' } }],
  419. }))
  420. const blocked = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('blocked'), name: 'failing-composite', arguments: {} })
  421. expect(blocked.isError).toBe(true)
  422. expect(blocked.additionalContexts?.map(context => context.source)).toEqual([{ kind: 'plugin', plugin: 'blocker' }])
  423. })
  424. it('composes pre + post waterfalls around dispatch (sandbox-wrap pattern)', async () => {
  425. const ctx = await setup()
  426. ctx.tools.register(echoTool)
  427. const order: string[] = []
  428. ctx.on('tools/pre-execute', async (_exec, next) => {
  429. order.push('pre:before')
  430. const decision = await next()
  431. order.push('pre:after')
  432. return decision
  433. })
  434. ctx.on('tools/post-execute', async (_exec, _result, next) => {
  435. order.push('post:before')
  436. const decision = await next()
  437. order.push('post:after')
  438. return decision
  439. })
  440. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'echo', arguments: { text: 'x' } })
  441. expect(result.isError).toBe(false)
  442. // pre runs fully (gate) before dispatch, then post runs over the result.
  443. expect(order).toEqual(['pre:before', 'pre:after', 'post:before', 'post:after'])
  444. })
  445. it('runs tools/execute after an allowed pre-execute, around dispatch, and before post-execute', async () => {
  446. const ctx = await setup()
  447. const order: string[] = []
  448. ctx.tools.register(defineTool({
  449. name: 'traced',
  450. description: 'echo',
  451. parameters: { text: { type: 'string' } },
  452. async execute(args) {
  453. order.push('dispatch')
  454. return [{ type: 'text' as const, text: args.text ?? '' }]
  455. },
  456. }))
  457. ctx.on('tools/pre-execute', async (_exec, next) => { order.push('pre'); return next() })
  458. ctx.on('tools/execute', async (_exec: ToolDispatchExecution, next: () => Promise<ToolExecutionResult>): Promise<ToolExecutionResult> => {
  459. order.push('execute:before')
  460. const result = await next()
  461. order.push('execute:after')
  462. return result
  463. })
  464. ctx.on('tools/post-execute', async (_exec, _result, next) => { order.push('post'); return next() })
  465. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'traced', arguments: { text: 'hi' } })
  466. expect(result).toEqual({ content: [{ type: 'text', text: 'hi' }], isError: false })
  467. // The around seam wraps dispatch; pre gates before it, post runs over its result.
  468. expect(order).toEqual(['pre', 'execute:before', 'dispatch', 'execute:after', 'post'])
  469. })
  470. it('skips dispatch when caller cancellation arrives while pre-execute awaits', async () => {
  471. const ctx = await setup()
  472. let dispatched = 0
  473. ctx.tools.register({
  474. ...echoTool,
  475. name: 'must-not-run',
  476. async execute() { dispatched += 1; return [] },
  477. })
  478. const entered = Promise.withResolvers<undefined>()
  479. const release = Promise.withResolvers<undefined>()
  480. ctx.on('tools/pre-execute', async (_exec, next) => {
  481. entered.resolve(undefined)
  482. await release.promise
  483. return await next()
  484. })
  485. const controller = new AbortController()
  486. const pending = ctx.tools.execute({
  487. callId: CallId('cancelled-in-pre'), name: 'must-not-run', arguments: {}, signal: controller.signal,
  488. })
  489. await entered.promise
  490. controller.abort('cancelled in policy')
  491. release.resolve(undefined)
  492. await expect(pending).resolves.toMatchObject({
  493. content: [{ type: 'text', text: 'Error: tool call aborted before dispatch' }],
  494. isError: true,
  495. error: { name: 'AbortError', code: TOOL_ABORTED_BEFORE_DISPATCH },
  496. })
  497. expect(dispatched).toBe(0)
  498. })
  499. it('preserves a pre-execute denial that settles after cancellation', async () => {
  500. const ctx = await setup()
  501. let dispatched = 0
  502. ctx.tools.register({
  503. ...echoTool,
  504. name: 'denied-after-cancel',
  505. async execute() { dispatched += 1; return [] },
  506. })
  507. const entered = Promise.withResolvers<undefined>()
  508. const release = Promise.withResolvers<undefined>()
  509. ctx.on('tools/pre-execute', async () => {
  510. entered.resolve(undefined)
  511. await release.promise
  512. return { kind: 'deny', reason: 'policy denied the call' }
  513. })
  514. const controller = new AbortController()
  515. const pending = ctx.tools.execute({
  516. callId: CallId('denied-after-cancel'), name: 'denied-after-cancel', arguments: {}, signal: controller.signal,
  517. })
  518. await entered.promise
  519. controller.abort('cancelled while policy decided')
  520. release.resolve(undefined)
  521. await expect(pending).resolves.toEqual({
  522. content: [{ type: 'text', text: 'Error: policy denied the call' }],
  523. isError: true,
  524. })
  525. expect(dispatched).toBe(0)
  526. })
  527. it('preserves an async pre-execute failure that settles after cancellation', async () => {
  528. const ctx = await setup()
  529. let dispatched = 0
  530. ctx.tools.register({
  531. ...echoTool,
  532. name: 'must-not-run',
  533. async execute() { dispatched += 1; return [] },
  534. })
  535. const entered = Promise.withResolvers<undefined>()
  536. const release = Promise.withResolvers<undefined>()
  537. ctx.on('tools/pre-execute', async () => {
  538. entered.resolve(undefined)
  539. await release.promise
  540. throw new Error('gate interrupted')
  541. })
  542. const controller = new AbortController()
  543. const pending = ctx.tools.execute({
  544. callId: CallId('cancelled-pre-error'), name: 'must-not-run', arguments: {}, signal: controller.signal,
  545. })
  546. await entered.promise
  547. controller.abort('cancelled in policy')
  548. release.resolve(undefined)
  549. await expect(pending).resolves.toEqual({
  550. content: [{ type: 'text', text: 'Error: gate interrupted' }],
  551. isError: true,
  552. })
  553. expect(dispatched).toBe(0)
  554. })
  555. it('rechecks caller cancellation after an async around-dispatch wrapper delegates', async () => {
  556. const ctx = await setup()
  557. let dispatched = 0
  558. ctx.tools.register({
  559. ...echoTool,
  560. name: 'must-not-run',
  561. async execute() { dispatched += 1; return [] },
  562. })
  563. const entered = Promise.withResolvers<undefined>()
  564. const release = Promise.withResolvers<undefined>()
  565. const replacement = new AbortController()
  566. ctx.on('tools/execute', async (exec, next) => {
  567. const upstream = exec.signal
  568. exec.signal = replacement.signal
  569. try {
  570. entered.resolve(undefined)
  571. await release.promise
  572. return await next()
  573. } finally {
  574. exec.signal = upstream
  575. }
  576. })
  577. const controller = new AbortController()
  578. const pending = ctx.tools.execute({
  579. callId: CallId('cancelled-in-around'), name: 'must-not-run', arguments: {}, signal: controller.signal,
  580. })
  581. await entered.promise
  582. controller.abort('cancelled in wrapper')
  583. release.resolve(undefined)
  584. await expect(pending).resolves.toMatchObject({
  585. isError: true,
  586. error: { name: 'AbortError', code: TOOL_ABORTED_BEFORE_DISPATCH },
  587. })
  588. expect(dispatched).toBe(0)
  589. })
  590. it('skips dispatch when an around wrapper supplies an already-aborted signal', async () => {
  591. const ctx = await setup()
  592. let dispatched = 0
  593. ctx.tools.register({
  594. ...echoTool,
  595. name: 'must-not-run',
  596. async execute() { dispatched += 1; return [] },
  597. })
  598. const replacement = AbortSignal.abort('wrapper cancelled')
  599. ctx.on('tools/execute', async (exec, next) => {
  600. const upstream = exec.signal
  601. exec.signal = replacement
  602. try {
  603. return await next()
  604. } finally {
  605. exec.signal = upstream
  606. }
  607. })
  608. const controller = new AbortController()
  609. const result = await ctx.tools.execute({
  610. callId: CallId('cancelled-wrapper'), name: 'must-not-run', arguments: {}, signal: controller.signal,
  611. })
  612. expect(result.error).toEqual({ name: 'AbortError', code: TOOL_ABORTED_BEFORE_DISPATCH })
  613. expect(dispatched).toBe(0)
  614. })
  615. it('uses ABORTED_BEFORE_DISPATCH when cancellation overtakes a wrapper short-circuit', async () => {
  616. const ctx = await setup()
  617. let dispatched = 0
  618. ctx.tools.register({
  619. ...echoTool,
  620. name: 'short-circuited',
  621. async execute() { dispatched += 1; return [] },
  622. })
  623. const entered = Promise.withResolvers<undefined>()
  624. const release = Promise.withResolvers<undefined>()
  625. ctx.on('tools/execute', async () => {
  626. entered.resolve(undefined)
  627. await release.promise
  628. return {
  629. content: [{ type: 'text', text: 'wrapper success' }],
  630. isError: false,
  631. additionalContexts: [{
  632. content: [{ type: 'text', text: 'wrapper context' }],
  633. source: { kind: 'plugin', plugin: 'wrapper' },
  634. }],
  635. }
  636. })
  637. const controller = new AbortController()
  638. const pending = ctx.tools.execute({
  639. callId: CallId('cancelled-short-circuit'),
  640. name: 'short-circuited',
  641. arguments: {},
  642. signal: controller.signal,
  643. })
  644. await entered.promise
  645. controller.abort('cancelled while wrapper waited')
  646. release.resolve(undefined)
  647. await expect(pending).resolves.toMatchObject({
  648. content: [{ type: 'text', text: 'Error: tool call aborted before dispatch' }],
  649. isError: true,
  650. error: { name: 'AbortError', code: TOOL_ABORTED_BEFORE_DISPATCH },
  651. additionalContexts: [{ source: { kind: 'plugin', plugin: 'wrapper' } }],
  652. })
  653. expect(dispatched).toBe(0)
  654. })
  655. it('replaces a late wrapper success with ABORTED and preserves deferred contexts', async () => {
  656. const ctx = await setup()
  657. ctx.tools.register({
  658. ...echoTool,
  659. name: 'completed-before-wrapper',
  660. async execute(_args, exec) {
  661. exec.deferContext({
  662. content: [{ type: 'text', text: 'completed child work' }],
  663. source: { kind: 'plugin', plugin: 'child' },
  664. })
  665. return [{ type: 'text', text: 'body complete' }]
  666. },
  667. })
  668. const entered = Promise.withResolvers<undefined>()
  669. const release = Promise.withResolvers<undefined>()
  670. ctx.on('tools/execute', async (_exec, next) => {
  671. const result = await next()
  672. entered.resolve(undefined)
  673. await release.promise
  674. return result
  675. })
  676. const controller = new AbortController()
  677. const pending = ctx.tools.execute({
  678. callId: CallId('cancelled-after-body'), name: 'completed-before-wrapper', arguments: {}, signal: controller.signal,
  679. })
  680. await entered.promise
  681. controller.abort('cancelled while wrapper settled')
  682. release.resolve(undefined)
  683. await expect(pending).resolves.toMatchObject({
  684. content: [{ type: 'text', text: 'Error: tool call aborted' }],
  685. isError: true,
  686. error: { name: 'AbortError', code: TOOL_ABORTED },
  687. additionalContexts: [{ source: { kind: 'plugin', plugin: 'child' } }],
  688. })
  689. })
  690. it('replaces a late post-execute success with ABORTED and preserves contexts', async () => {
  691. const ctx = await setup()
  692. ctx.tools.register({
  693. ...echoTool,
  694. name: 'completed-before-post',
  695. async execute(_args, exec) {
  696. exec.deferContext({
  697. content: [{ type: 'text', text: 'completed child work' }],
  698. source: { kind: 'plugin', plugin: 'child' },
  699. })
  700. return [{ type: 'text', text: 'body complete' }]
  701. },
  702. })
  703. const entered = Promise.withResolvers<undefined>()
  704. const release = Promise.withResolvers<undefined>()
  705. ctx.on('tools/post-execute', async (_exec, _result, next) => {
  706. const decision = await next()
  707. entered.resolve(undefined)
  708. await release.promise
  709. return {
  710. ...decision,
  711. additionalContexts: [{
  712. content: [{ type: 'text', text: 'post context' }],
  713. source: { kind: 'plugin', plugin: 'post' },
  714. }],
  715. }
  716. })
  717. const controller = new AbortController()
  718. const pending = ctx.tools.execute({
  719. callId: CallId('cancelled-in-post'), name: 'completed-before-post', arguments: {}, signal: controller.signal,
  720. })
  721. await entered.promise
  722. controller.abort('cancelled while post policy waits')
  723. release.resolve(undefined)
  724. await expect(pending).resolves.toMatchObject({
  725. content: [{ type: 'text', text: 'Error: tool call aborted' }],
  726. isError: true,
  727. error: { name: 'AbortError', code: 'ABORTED' },
  728. additionalContexts: [
  729. { source: { kind: 'plugin', plugin: 'child' } },
  730. { source: { kind: 'plugin', plugin: 'post' } },
  731. ],
  732. })
  733. })
  734. it('preserves an around-dispatch failure that settles after cancellation', async () => {
  735. const ctx = await setup()
  736. let dispatched = 0
  737. ctx.tools.register({
  738. ...echoTool,
  739. name: 'wrapper-failure',
  740. async execute() { dispatched += 1; return [] },
  741. })
  742. const entered = Promise.withResolvers<undefined>()
  743. const release = Promise.withResolvers<undefined>()
  744. ctx.on('tools/execute', async () => {
  745. entered.resolve(undefined)
  746. await release.promise
  747. throw new HarnessError('wrapper failed', 'WRAPPER_FAILURE')
  748. })
  749. const controller = new AbortController()
  750. const pending = ctx.tools.execute({
  751. callId: CallId('wrapper-failure'), name: 'wrapper-failure', arguments: {}, signal: controller.signal,
  752. })
  753. await entered.promise
  754. controller.abort('cancelled while wrapper failed')
  755. release.resolve(undefined)
  756. await expect(pending).resolves.toMatchObject({
  757. content: [{ type: 'text', text: 'Error: wrapper failed' }],
  758. isError: true,
  759. error: { name: 'HarnessError', code: 'WRAPPER_FAILURE' },
  760. })
  761. expect(dispatched).toBe(0)
  762. })
  763. it('preserves a tool-owned failure after the body observes cancellation', async () => {
  764. const ctx = await setup()
  765. const entered = Promise.withResolvers<undefined>()
  766. ctx.tools.register({
  767. ...echoTool,
  768. name: 'tool-failure',
  769. execute(_args, exec) {
  770. entered.resolve(undefined)
  771. return new Promise<never[]>((_resolve, reject) => {
  772. exec.signal.addEventListener('abort', () => {
  773. reject(new HarnessError('tool failed', 'TOOL_FAILURE'))
  774. }, { once: true })
  775. })
  776. },
  777. })
  778. const controller = new AbortController()
  779. const pending = ctx.tools.execute({
  780. callId: CallId('tool-failure'), name: 'tool-failure', arguments: {}, signal: controller.signal,
  781. })
  782. await entered.promise
  783. controller.abort('cancelled running body')
  784. await expect(pending).resolves.toMatchObject({
  785. content: [{ type: 'text', text: 'Error: tool failed' }],
  786. isError: true,
  787. error: { name: 'HarnessError', code: 'TOOL_FAILURE' },
  788. })
  789. })
  790. it('preserves a post-policy failure that settles after cancellation', async () => {
  791. const ctx = await setup()
  792. ctx.tools.register(echoTool)
  793. const entered = Promise.withResolvers<undefined>()
  794. const release = Promise.withResolvers<undefined>()
  795. ctx.on('tools/post-execute', async () => {
  796. entered.resolve(undefined)
  797. await release.promise
  798. throw new HarnessError('post-policy failed', 'POST_FAILURE')
  799. })
  800. const controller = new AbortController()
  801. const pending = ctx.tools.execute({
  802. callId: CallId('post-failure'), name: 'echo', arguments: {}, signal: controller.signal,
  803. })
  804. await entered.promise
  805. controller.abort('cancelled while post-policy failed')
  806. release.resolve(undefined)
  807. await expect(pending).resolves.toMatchObject({
  808. content: [{ type: 'text', text: 'Error: post-policy failed' }],
  809. isError: true,
  810. error: { name: 'HarnessError', code: 'POST_FAILURE' },
  811. })
  812. })
  813. it('fuses caller cancellation back into a wrapper replacement for the running body', async () => {
  814. const ctx = await setup()
  815. const entered = Promise.withResolvers<undefined>()
  816. const replacement = new AbortController()
  817. let bodySignal: AbortSignal | undefined
  818. ctx.tools.register({
  819. ...echoTool,
  820. name: 'cooperative',
  821. execute(_args, exec) {
  822. bodySignal = exec.signal
  823. entered.resolve(undefined)
  824. if (exec.signal.aborted) return Promise.resolve([])
  825. return new Promise((resolve) => {
  826. exec.signal.addEventListener('abort', () => { resolve([]) }, { once: true })
  827. })
  828. },
  829. })
  830. ctx.on('tools/execute', async (exec, next) => {
  831. const upstream = exec.signal
  832. exec.signal = replacement.signal
  833. try {
  834. return await next()
  835. } finally {
  836. exec.signal = upstream
  837. }
  838. })
  839. const controller = new AbortController()
  840. const pending = ctx.tools.execute({
  841. callId: CallId('cancelled-body'), name: 'cooperative', arguments: {}, signal: controller.signal,
  842. })
  843. await entered.promise
  844. expect(bodySignal).not.toBe(controller.signal)
  845. expect(bodySignal).not.toBe(replacement.signal)
  846. controller.abort('cancel running body')
  847. await expect(pending).resolves.toMatchObject({
  848. isError: true,
  849. error: { name: 'AbortError', code: 'ABORTED' },
  850. })
  851. expect(bodySignal?.aborted).toBe(true)
  852. expect(replacement.signal.aborted).toBe(false)
  853. })
  854. it('restores the required caller signal after around dispatch', async () => {
  855. const ctx = await setup()
  856. let postSignal: AbortSignal | undefined
  857. ctx.on('tools/execute', async (exec, next) => {
  858. const upstream = exec.signal
  859. exec.signal = new AbortController().signal
  860. try {
  861. return await next()
  862. } finally {
  863. exec.signal = upstream
  864. }
  865. })
  866. ctx.on('tools/post-execute', async (exec, _result, next) => {
  867. postSignal = exec.signal
  868. return next()
  869. })
  870. const controller = new AbortController()
  871. await ctx.tools.execute({
  872. callId: CallId('restored-signal'), name: 'echo', arguments: {}, signal: controller.signal,
  873. })
  874. expect(postSignal).toBe(controller.signal)
  875. })
  876. it('waits for an uncooperative started body before returning ABORTED', async () => {
  877. const ctx = await setup()
  878. const entered = Promise.withResolvers<undefined>()
  879. const release = Promise.withResolvers<never[]>()
  880. ctx.tools.register({
  881. ...echoTool,
  882. name: 'uncooperative',
  883. execute(_args, exec) {
  884. exec.deferContext({
  885. content: [{ type: 'text', text: 'nested outcome' }],
  886. source: { kind: 'plugin', plugin: 'nested' },
  887. })
  888. entered.resolve(undefined)
  889. return release.promise
  890. },
  891. })
  892. const controller = new AbortController()
  893. const pending = ctx.tools.execute({
  894. callId: CallId('drain-body'), name: 'uncooperative', arguments: {}, signal: controller.signal,
  895. })
  896. await entered.promise
  897. controller.abort('must still drain')
  898. const state = await Promise.race([
  899. pending.then(() => 'settled' as const),
  900. Promise.resolve('pending' as const),
  901. ])
  902. expect(state).toBe('pending')
  903. release.resolve([])
  904. await expect(pending).resolves.toMatchObject({
  905. isError: true,
  906. error: { name: 'AbortError', code: 'ABORTED' },
  907. additionalContexts: [{ source: { kind: 'plugin', plugin: 'nested' } }],
  908. })
  909. })
  910. it('materializes a pre-aborted call and publishes one result without entering pipeline phases', async () => {
  911. const ctx = await setup()
  912. const phases = { pre: 0, around: 0, body: 0, post: 0, result: 0 }
  913. const callerArguments = { nested: { value: 1 } }
  914. const callerSignal = AbortSignal.abort('already cancelled')
  915. let argumentReads = 0
  916. let observedArguments: unknown
  917. let observedExecution: object | undefined
  918. let observedToken: symbol | undefined
  919. let observedSignal: AbortSignal | undefined
  920. let observedResult: ToolExecutionResult | undefined
  921. ctx.tools.register({
  922. ...echoTool,
  923. name: 'domain-abort',
  924. async execute() { phases.body += 1; return [] },
  925. })
  926. ctx.on('tools/pre-execute', async (_exec, next) => { phases.pre += 1; return next() })
  927. ctx.on('tools/execute', async (_exec, next) => { phases.around += 1; return next() })
  928. ctx.on('tools/post-execute', async (_exec, _result, next) => { phases.post += 1; return next() })
  929. ctx.on('tools/result', (exec, result) => {
  930. phases.result += 1
  931. observedExecution = exec
  932. observedArguments = exec.arguments
  933. observedToken = exec.token
  934. observedSignal = exec.signal
  935. observedResult = result
  936. })
  937. const result = await ctx.tools.execute({
  938. callId: CallId('pre-aborted'),
  939. name: 'domain-abort',
  940. get arguments() { argumentReads += 1; return callerArguments },
  941. signal: callerSignal,
  942. })
  943. expect(argumentReads).toBe(1)
  944. expect(phases).toEqual({ pre: 0, around: 0, body: 0, post: 0, result: 1 })
  945. expect(result).toEqual({
  946. content: [{ type: 'text', text: 'Error: tool call aborted before dispatch' }],
  947. isError: true,
  948. error: { name: 'AbortError', code: TOOL_ABORTED_BEFORE_DISPATCH },
  949. })
  950. expect(observedResult).toBe(result)
  951. expect(Object.isFrozen(observedExecution)).toBe(true)
  952. expect(typeof observedToken).toBe('symbol')
  953. expect(observedSignal).toBe(callerSignal)
  954. expect(Object.isFrozen(result)).toBe(true)
  955. expect(observedArguments).not.toBe(callerArguments)
  956. expect(Object.isFrozen(observedArguments)).toBe(true)
  957. expect(Object.isFrozen((observedArguments as { nested: object }).nested)).toBe(true)
  958. })
  959. it('lets argument materialization failure win over a pre-aborted signal', async () => {
  960. const ctx = await setup()
  961. let observed = 0
  962. ctx.on('tools/result', () => { observed += 1 })
  963. const result = await ctx.tools.execute({
  964. callId: CallId('invalid-pre-aborted'),
  965. name: 'missing',
  966. arguments: { invalid: () => undefined },
  967. signal: AbortSignal.abort('already cancelled'),
  968. })
  969. expect(result).toEqual({
  970. content: [{ type: 'text', text: 'Error: tool execution arguments must be losslessly JSON-serializable' }],
  971. isError: true,
  972. })
  973. expect(observed).toBe(1)
  974. })
  975. it('a pre-execute deny short-circuits before tools/execute (the seam never runs)', async () => {
  976. const ctx = await setup()
  977. ctx.tools.register(echoTool)
  978. let entered = false
  979. ctx.on('tools/pre-execute', async (_exec, _next): Promise<PreToolDecision> => ({ kind: 'deny', reason: 'nope' }))
  980. ctx.on('tools/execute', async (_exec: ToolDispatchExecution, next: () => Promise<ToolExecutionResult>): Promise<ToolExecutionResult> => {
  981. entered = true
  982. return next()
  983. })
  984. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'echo', arguments: { text: 'hi' } })
  985. expect(result.isError).toBe(true)
  986. expect(result.content[0]).toMatchObject({ text: 'Error: nope' })
  987. expect(entered).toBe(false) // a denied call never enters the around-dispatch seam
  988. })
  989. it('a thrown tool is normalized to an isError result BEFORE a tools/execute listener sees next()', async () => {
  990. const ctx = await setup()
  991. ctx.tools.register({
  992. ...echoTool,
  993. name: 'boom',
  994. async execute() { throw new HarnessError('kaboom', 'BOOM') },
  995. })
  996. let seen: { isError: boolean; error?: unknown } | undefined
  997. ctx.on('tools/execute', async (_exec: ToolDispatchExecution, next: () => Promise<ToolExecutionResult>): Promise<ToolExecutionResult> => {
  998. const result = await next()
  999. // The base next() IS dispatch-with-normalization: the wrapper sees the
  1000. // normalized isError result, never a raw throw from the tool body.
  1001. seen = { isError: result.isError, error: result.error }
  1002. return result
  1003. })
  1004. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'boom', arguments: {} })
  1005. expect(seen).toEqual({ isError: true, error: { name: 'HarnessError', code: 'BOOM' } })
  1006. expect(result.isError).toBe(true)
  1007. expect(result.content[0]).toMatchObject({ text: 'Error: kaboom' })
  1008. })
  1009. it('a thrown tool normalized inside tools/execute still reaches post-execute', async () => {
  1010. const ctx = await setup()
  1011. ctx.tools.register({
  1012. ...echoTool,
  1013. name: 'boom',
  1014. async execute() { throw new Error('exploded') },
  1015. })
  1016. let postSaw: boolean | undefined
  1017. ctx.on('tools/execute', async (_exec: ToolDispatchExecution, next: () => Promise<ToolExecutionResult>): Promise<ToolExecutionResult> => next())
  1018. ctx.on('tools/post-execute', async (_exec, result, next) => {
  1019. postSaw = result.isError
  1020. return next()
  1021. })
  1022. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'boom', arguments: {} })
  1023. expect(postSaw).toBe(true) // the normalized isError still flows through post-execute
  1024. expect(result.isError).toBe(true)
  1025. expect(result.content[0]).toMatchObject({ text: 'Error: exploded' })
  1026. })
  1027. it('re-fuses the caller signal with an around-dispatch replacement for the body', async () => {
  1028. const ctx = await setup()
  1029. let seenSignal: AbortSignal | undefined
  1030. ctx.tools.register({
  1031. ...echoTool,
  1032. name: 'signal-probe',
  1033. async execute(_args, exec) {
  1034. seenSignal = exec.signal
  1035. return [{ type: 'text' as const, text: 'ok' }]
  1036. },
  1037. })
  1038. const upstream = new AbortController().signal
  1039. const replacement = new AbortController().signal
  1040. ctx.on('tools/execute', async (exec: ToolDispatchExecution, next: () => Promise<ToolExecutionResult>): Promise<ToolExecutionResult> => {
  1041. expect(exec.signal).toBe(upstream)
  1042. // Cordis next() ignores passed arguments, so a wrapper mutates exec in
  1043. // place (the documented "mutate the shared object, then delegate" idiom).
  1044. exec.signal = replacement
  1045. return next()
  1046. })
  1047. await ctx.tools.execute({ callId: CallId('c1'), name: 'signal-probe', arguments: {}, signal: upstream })
  1048. expect(seenSignal).toBeDefined()
  1049. expect(seenSignal).not.toBe(upstream)
  1050. expect(seenSignal).not.toBe(replacement)
  1051. })
  1052. it('a tools/execute listener can short-circuit dispatch by returning a result without next()', async () => {
  1053. const ctx = await setup()
  1054. let dispatched = false
  1055. ctx.tools.register({
  1056. ...echoTool,
  1057. name: 'never-runs',
  1058. async execute() { dispatched = true; return [] },
  1059. })
  1060. ctx.on('tools/execute', async (_exec: ToolDispatchExecution, _next: () => Promise<ToolExecutionResult>): Promise<ToolExecutionResult> =>
  1061. ({ content: [{ type: 'text', text: 'short-circuited' }], isError: false }))
  1062. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'never-runs', arguments: {} })
  1063. expect(dispatched).toBe(false) // returning without next() skips core dispatch
  1064. expect(result.content[0]).toMatchObject({ text: 'short-circuited' })
  1065. })
  1066. it('preserves additionalContexts supplied by an around-dispatch result', async () => {
  1067. const ctx = await setup()
  1068. ctx.tools.register(echoTool)
  1069. ctx.on('tools/execute', async () => ({
  1070. content: [{ type: 'text', text: 'short-circuited with context' }],
  1071. isError: false,
  1072. additionalContexts: [{
  1073. content: [{ type: 'text', text: 'from around dispatch' }],
  1074. source: { kind: 'plugin', plugin: 'test' },
  1075. }],
  1076. }))
  1077. const result = await ctx.tools.execute({
  1078. signal: testToolSignal,
  1079. callId: CallId('around-context'), name: 'echo', arguments: {},
  1080. })
  1081. expect(result.additionalContexts).toEqual([{
  1082. content: [{ type: 'text', text: 'from around dispatch' }],
  1083. source: { kind: 'plugin', plugin: 'test' },
  1084. }])
  1085. })
  1086. it('returns an isError result when a tools/execute listener throws', async () => {
  1087. const ctx = await setup()
  1088. ctx.tools.register(echoTool)
  1089. ctx.on('tools/execute', async () => { throw new Error('wrapper broke') })
  1090. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'echo', arguments: { text: 'hi' } })
  1091. expect(result).toEqual({
  1092. content: [{ type: 'text', text: 'Error: wrapper broke' }],
  1093. isError: true,
  1094. })
  1095. })
  1096. it('returns an isError result when a tools/pre-execute listener throws', async () => {
  1097. const ctx = await setup()
  1098. ctx.tools.register(echoTool)
  1099. ctx.on('tools/pre-execute', async () => {
  1100. throw new Error('permission hook broke')
  1101. })
  1102. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'echo', arguments: { text: 'hi' } })
  1103. expect(result).toEqual({
  1104. content: [{ type: 'text', text: 'Error: permission hook broke' }],
  1105. isError: true,
  1106. })
  1107. })
  1108. it('returns an isError result when a tools/post-execute listener throws', async () => {
  1109. const ctx = await setup()
  1110. ctx.tools.register(echoTool)
  1111. ctx.on('tools/post-execute', async () => {
  1112. throw new Error('post hook broke')
  1113. })
  1114. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'echo', arguments: { text: 'hi' } })
  1115. expect(result).toEqual({
  1116. content: [{ type: 'text', text: 'Error: post hook broke' }],
  1117. isError: true,
  1118. })
  1119. })
  1120. it('preserves structured error info when a tools/pre-execute listener throws HarnessError', async () => {
  1121. const ctx = await setup()
  1122. ctx.tools.register(echoTool)
  1123. ctx.on('tools/pre-execute', async () => {
  1124. throw new HarnessError('denied', 'DENIED')
  1125. })
  1126. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'echo', arguments: { text: 'hi' } })
  1127. expect(result).toMatchObject({
  1128. isError: true,
  1129. error: { name: 'HarnessError', code: 'DENIED' },
  1130. })
  1131. })
  1132. it('schemas() snapshots tool schemas instead of exposing registry objects', async () => {
  1133. const ctx = await setup()
  1134. ctx.tools.register(echoTool)
  1135. const first = ctx.tools.schemas()
  1136. const firstParameters = first[0]!.parameters as { properties: Record<string, unknown> }
  1137. firstParameters.properties['mutated'] = { type: 'string' }
  1138. first[0]!.description = 'mutated'
  1139. expect(ctx.tools.schemas()).toEqual([{
  1140. name: 'echo',
  1141. description: 'echo arguments back',
  1142. parameters: { type: 'object', properties: { text: { type: 'string' } } },
  1143. }])
  1144. })
  1145. it('rejects a non-positive or non-finite registration timeout', async () => {
  1146. const ctx = await setup()
  1147. expect(() => ctx.tools.register({ ...echoTool, name: 'zero-timeout', timeoutMs: 0 }))
  1148. .toThrow('timeoutMs must be a positive finite number')
  1149. expect(() => ctx.tools.register({ ...echoTool, name: 'infinite-timeout', timeoutMs: Number.POSITIVE_INFINITY }))
  1150. .toThrow('timeoutMs must be a positive finite number')
  1151. })
  1152. it('rejects duplicate names and unregisters on fiber dispose (HMR safety)', async () => {
  1153. const ctx = await setup()
  1154. ctx.tools.register(echoTool)
  1155. expect(() => ctx.tools.register(echoTool)).toThrow('already registered')
  1156. const fiber = await ctx.plugin(Object.assign((inner: Context) => {
  1157. inner.tools.register({ ...echoTool, name: 'scoped' })
  1158. }, { inject: ['tools'] }))
  1159. expect(ctx.tools.schemas().map(t => t.name)).toEqual(['echo', 'scoped'])
  1160. await fiber.dispose()
  1161. expect(ctx.tools.schemas().map(t => t.name)).toEqual(['echo'])
  1162. })
  1163. it('returns a callable disposer from register() that unregisters the tool', async () => {
  1164. const ctx = await setup()
  1165. ctx.tools.register(echoTool)
  1166. // Register a second tool and call its returned disposer directly
  1167. const dispose = ctx.tools.register({ ...echoTool, name: 'disposable' })
  1168. expect(ctx.tools.schemas().map(t => t.name)).toEqual(['echo', 'disposable'])
  1169. dispose()
  1170. expect(ctx.tools.schemas().map(t => t.name)).toEqual(['echo'])
  1171. })
  1172. it('rolls back the tool entry when a tools/change listener throws (P1-1)', async () => {
  1173. const ctx = await setup()
  1174. let threw = false
  1175. ctx.on('tools/change', () => {
  1176. if (!threw) { threw = true; throw new Error('boom change listener') }
  1177. })
  1178. // The throwing emit must roll the entry back, not leak it.
  1179. expect(() => ctx.tools.register(echoTool)).toThrow('boom change listener')
  1180. expect(ctx.tools.get('echo')).toBeUndefined() // rolled back, not leaked
  1181. expect(ctx.tools.schemas()).toHaveLength(0)
  1182. // A subsequent listener-free register of the SAME name succeeds and is
  1183. // exposed exactly once (the duplicate-name check is not wedged).
  1184. const dispose = ctx.tools.register(echoTool)
  1185. expect(ctx.tools.schemas().map(t => t.name)).toEqual(['echo'])
  1186. dispose()
  1187. expect(ctx.tools.get('echo')).toBeUndefined()
  1188. })
  1189. it('register() returns the EXACT effect disposer: a composite yield nests the teardown in order', async () => {
  1190. // Registry methods return the exact Cordis effect disposer so a composite yield places
  1191. // unregistration at its LIFO position. A wrapper would create a concurrent sibling; this async
  1192. // probe yields during earlier teardown and would then observe the tool already removed.
  1193. const ctx = await setup()
  1194. const order: string[] = []
  1195. const fiber = await ctx.plugin(Object.assign((inner: Context) => {
  1196. inner.effect(function* () {
  1197. yield () => { order.push('disposed-last') }
  1198. yield inner.tools.register({ ...echoTool, name: 'nested' })
  1199. order.push('registered')
  1200. yield async () => {
  1201. await new Promise(resolve => setTimeout(resolve, 0))
  1202. order.push(inner.tools.get('nested') ? 'first: still registered' : 'first: already gone')
  1203. }
  1204. })
  1205. }, { inject: ['tools'] }))
  1206. await fiber.dispose()
  1207. expect(order).toEqual(['registered', 'first: still registered', 'disposed-last'])
  1208. expect(ctx.tools.get('nested')).toBeUndefined()
  1209. })
  1210. })
  1211. describe('defineTool / schema DSL', () => {
  1212. it('converts SchemaSpec to standard JSON Schema with required array', () => {
  1213. const spec = {
  1214. path: { type: 'string', required: true, description: 'Absolute path' },
  1215. offset: { type: 'number' },
  1216. limit: { type: 'number', description: 'Max lines' },
  1217. } satisfies SchemaSpec
  1218. const jsonSchema = schemaSpecToJsonSchema(spec)
  1219. expect(jsonSchema).toEqual({
  1220. type: 'object',
  1221. properties: {
  1222. path: { type: 'string', description: 'Absolute path' },
  1223. offset: { type: 'number' },
  1224. limit: { type: 'number', description: 'Max lines' },
  1225. },
  1226. required: ['path'],
  1227. })
  1228. })
  1229. it('handles empty spec (no properties, no required)', () => {
  1230. expect(schemaSpecToJsonSchema({})).toEqual({
  1231. type: 'object',
  1232. properties: {},
  1233. })
  1234. })
  1235. it('handles nested object spec', () => {
  1236. const spec = {
  1237. config: {
  1238. type: 'object',
  1239. required: true,
  1240. properties: {
  1241. host: { type: 'string', required: true },
  1242. port: { type: 'number' },
  1243. },
  1244. },
  1245. } satisfies SchemaSpec
  1246. const jsonSchema = schemaSpecToJsonSchema(spec)
  1247. expect(jsonSchema).toEqual({
  1248. type: 'object',
  1249. properties: {
  1250. config: {
  1251. type: 'object',
  1252. properties: {
  1253. host: { type: 'string' },
  1254. port: { type: 'number' },
  1255. },
  1256. required: ['host'],
  1257. },
  1258. },
  1259. required: ['config'],
  1260. })
  1261. })
  1262. it('defineTool returns a valid ToolDefinition with typed execute', async () => {
  1263. const ctx = await setup()
  1264. const tool = defineTool({
  1265. name: 'typed-echo',
  1266. description: 'A typed echo tool',
  1267. parameters: {
  1268. text: { type: 'string', required: true },
  1269. uppercase: { type: 'boolean' },
  1270. },
  1271. async execute(args) {
  1272. // args is typed: { text: string; uppercase?: boolean }
  1273. const result = args.uppercase ? args.text.toUpperCase() : args.text
  1274. return [{ type: 'text', text: result }]
  1275. },
  1276. })
  1277. ctx.tools.register(tool)
  1278. expect(ctx.tools.schemas()).toEqual([{
  1279. name: 'typed-echo',
  1280. description: 'A typed echo tool',
  1281. parameters: {
  1282. type: 'object',
  1283. properties: {
  1284. text: { type: 'string' },
  1285. uppercase: { type: 'boolean' },
  1286. },
  1287. required: ['text'],
  1288. },
  1289. }])
  1290. const result = await ctx.tools.execute({
  1291. signal: testToolSignal,
  1292. callId: CallId('c1'),
  1293. name: 'typed-echo',
  1294. arguments: { text: 'hello', uppercase: true },
  1295. })
  1296. expect(result.isError).toBe(false)
  1297. expect(result.content).toEqual([{ type: 'text', text: 'HELLO' }])
  1298. })
  1299. it('type-level: InferArgs maps required properties to non-optional', () => {
  1300. // Compile-time check: if this compiles, InferArgs is correct.
  1301. // args.a is string (required), args.b is number|undefined (optional).
  1302. const tool = defineTool({
  1303. name: 'type-check',
  1304. description: '',
  1305. parameters: { a: { type: 'string' as const, required: true as const }, b: { type: 'number' as const } },
  1306. async execute(args) {
  1307. // Verify types at runtime via typeof
  1308. expect(typeof args.a).toBe('string')
  1309. // args.b should be undefined when not provided
  1310. void args
  1311. return [{ type: 'text', text: args.a }]
  1312. },
  1313. })
  1314. void tool
  1315. })
  1316. it('registry round-trips a defineTool definition (register→schemas→execute)', async () => {
  1317. const ctx = await setup()
  1318. ctx.tools.register(defineTool({
  1319. name: 'roundtrip',
  1320. description: 'Round-trip test',
  1321. parameters: {
  1322. req: { type: 'string', required: true },
  1323. opt: { type: 'number', description: 'Optional number' },
  1324. },
  1325. async execute(args) {
  1326. return [{ type: 'text', text: `${args.req}:${args.opt ?? 'none'}` }]
  1327. },
  1328. }))
  1329. // Schema round-trip: schemas() returns standard JSON Schema
  1330. const schemas = ctx.tools.schemas()
  1331. expect(schemas).toHaveLength(1)
  1332. expect(schemas[0]!.parameters).toEqual({
  1333. type: 'object',
  1334. properties: {
  1335. req: { type: 'string' },
  1336. opt: { type: 'number', description: 'Optional number' },
  1337. },
  1338. required: ['req'],
  1339. })
  1340. // Execution round-trip
  1341. const result = await ctx.tools.execute({
  1342. signal: testToolSignal,
  1343. callId: CallId('c1'),
  1344. name: 'roundtrip',
  1345. arguments: { req: 'hello' },
  1346. })
  1347. expect(result.isError).toBe(false)
  1348. expect(result.content).toEqual([{ type: 'text', text: 'hello:none' }])
  1349. })
  1350. it('still accepts raw JSON-Schema ToolDefinition directly (MCP interop)', async () => {
  1351. const ctx = await setup()
  1352. ctx.tools.register({
  1353. name: 'raw-tool',
  1354. description: 'Raw JSON Schema tool (like an MCP adapter would register)',
  1355. parameters: {
  1356. type: 'object',
  1357. properties: { path: { type: 'string' } },
  1358. required: ['path'],
  1359. },
  1360. async execute(args: unknown) {
  1361. const p = args as { path: string }
  1362. return [{ type: 'text', text: p.path }]
  1363. },
  1364. })
  1365. const schemas = ctx.tools.schemas()
  1366. expect(schemas[0]!.parameters).toEqual({
  1367. type: 'object',
  1368. properties: { path: { type: 'string' } },
  1369. required: ['path'],
  1370. })
  1371. const result = await ctx.tools.execute({
  1372. signal: testToolSignal,
  1373. callId: CallId('c1'),
  1374. name: 'raw-tool',
  1375. arguments: { path: '/tmp' },
  1376. })
  1377. expect(result.isError).toBe(false)
  1378. expect(result.content).toEqual([{ type: 'text', text: '/tmp' }])
  1379. })
  1380. })
  1381. describe('schema DSL edge cases', () => {
  1382. it('emits enum values in JSON Schema property', () => {
  1383. const spec = {
  1384. color: { type: 'string', enum: ['red', 'green', 'blue'], description: 'Color choice' },
  1385. } satisfies SchemaSpec
  1386. const jsonSchema = schemaSpecToJsonSchema(spec)
  1387. expect(jsonSchema.properties['color']).toMatchObject({
  1388. type: 'string',
  1389. enum: ['red', 'green', 'blue'],
  1390. description: 'Color choice',
  1391. })
  1392. })
  1393. it('emits default value in JSON Schema property', () => {
  1394. const spec = {
  1395. limit: { type: 'number', default: 25 },
  1396. } satisfies SchemaSpec
  1397. const jsonSchema = schemaSpecToJsonSchema(spec)
  1398. expect(jsonSchema.properties['limit']).toMatchObject({
  1399. type: 'number',
  1400. default: 25,
  1401. })
  1402. })
  1403. it('handles array items without nested properties (plain type array)', () => {
  1404. const spec = {
  1405. tags: { type: 'array', items: { type: 'string' } },
  1406. } satisfies SchemaSpec
  1407. const jsonSchema = schemaSpecToJsonSchema(spec)
  1408. expect(jsonSchema.properties['tags']).toEqual({
  1409. type: 'array',
  1410. items: { type: 'string' },
  1411. })
  1412. })
  1413. it('handles enum and default together in one property', () => {
  1414. const spec = {
  1415. level: { type: 'string', enum: ['low', 'high'], default: 'low' },
  1416. } satisfies SchemaSpec
  1417. const jsonSchema = schemaSpecToJsonSchema(spec)
  1418. expect(jsonSchema.properties['level']).toMatchObject({
  1419. type: 'string',
  1420. enum: ['low', 'high'],
  1421. default: 'low',
  1422. })
  1423. })
  1424. it('omits description, enum, default keys when not specified', () => {
  1425. const spec = {
  1426. bare: { type: 'string' },
  1427. } satisfies SchemaSpec
  1428. const jsonSchema = schemaSpecToJsonSchema(spec)
  1429. const prop = jsonSchema.properties['bare'] as Record<string, unknown>
  1430. expect(prop).toEqual({ type: 'string' })
  1431. expect('description' in prop).toBe(false)
  1432. expect('enum' in prop).toBe(false)
  1433. expect('default' in prop).toBe(false)
  1434. })
  1435. it('handles array with no items (items omitted)', () => {
  1436. const spec = {
  1437. raw: { type: 'array' },
  1438. } satisfies SchemaSpec
  1439. const jsonSchema = schemaSpecToJsonSchema(spec)
  1440. expect(jsonSchema.properties['raw']).toEqual({
  1441. type: 'array',
  1442. })
  1443. })
  1444. it('handles nested object with all-optional properties (no required array)', () => {
  1445. const spec = {
  1446. config: {
  1447. type: 'object',
  1448. properties: {
  1449. host: { type: 'string' },
  1450. port: { type: 'number' },
  1451. },
  1452. },
  1453. } satisfies SchemaSpec
  1454. const jsonSchema = schemaSpecToJsonSchema(spec)
  1455. expect(jsonSchema.properties['config']).toMatchObject({
  1456. type: 'object',
  1457. properties: {
  1458. host: { type: 'string' },
  1459. port: { type: 'number' },
  1460. },
  1461. })
  1462. const config = jsonSchema.properties['config'] as Record<string, unknown>
  1463. expect('required' in config).toBe(false)
  1464. })
  1465. })
  1466. describe('schema DSL optional and nested contracts', () => {
  1467. it('InferArgs makes non-required keys genuinely optional (omittable)', () => {
  1468. type Args = InferArgs<{
  1469. path: { type: 'string'; required: true }
  1470. limit: { type: 'number' }
  1471. }>
  1472. expectTypeOf<Args>().toEqualTypeOf<{ path: string; limit?: number }>()
  1473. const omitted: Args = { path: '/tmp' }
  1474. expect(omitted.limit).toBeUndefined()
  1475. })
  1476. it('InferArgs recurses into array items, including arrays of objects', () => {
  1477. type Args = InferArgs<{
  1478. names: { type: 'array'; required: true; items: { type: 'string' } }
  1479. servers: {
  1480. type: 'array'
  1481. items: {
  1482. type: 'object'
  1483. properties: {
  1484. host: { type: 'string'; required: true }
  1485. port: { type: 'number' }
  1486. }
  1487. }
  1488. }
  1489. }>
  1490. expectTypeOf<Args>().toEqualTypeOf<{
  1491. names: string[]
  1492. servers?: { host: string; port?: number }[]
  1493. }>()
  1494. })
  1495. it('runtime JSON Schema matches the array-of-objects inference', () => {
  1496. const spec = {
  1497. servers: {
  1498. type: 'array',
  1499. items: {
  1500. type: 'object',
  1501. properties: {
  1502. host: { type: 'string', required: true },
  1503. port: { type: 'number' },
  1504. },
  1505. },
  1506. },
  1507. } satisfies SchemaSpec
  1508. expect(schemaSpecToJsonSchema(spec)).toEqual({
  1509. type: 'object',
  1510. properties: {
  1511. servers: {
  1512. type: 'array',
  1513. items: {
  1514. type: 'object',
  1515. properties: {
  1516. host: { type: 'string' },
  1517. port: { type: 'number' },
  1518. },
  1519. required: ['host'],
  1520. },
  1521. },
  1522. },
  1523. })
  1524. })
  1525. it('reports messages from non-Error throws (throw { message })', async () => {
  1526. const ctx = await setup()
  1527. ctx.tools.register({
  1528. ...echoTool,
  1529. name: 'object-thrower',
  1530. async execute() {
  1531. // testing non-Error throws on purpose
  1532. throw { message: 'denied by object' }
  1533. },
  1534. })
  1535. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'object-thrower', arguments: {} })
  1536. expect(result.isError).toBe(true)
  1537. expect(result.content[0]).toMatchObject({ text: 'Error: denied by object' })
  1538. })
  1539. it('reports messages from throws of non-objects (throw "string")', async () => {
  1540. const ctx = await setup()
  1541. ctx.tools.register({
  1542. ...echoTool,
  1543. name: 'string-thrower',
  1544. async execute() {
  1545. // testing primitive throws on purpose
  1546. throw 'kaboom'
  1547. },
  1548. })
  1549. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'string-thrower', arguments: {} })
  1550. expect(result.isError).toBe(true)
  1551. expect(result.content[0]).toMatchObject({ text: 'Error: kaboom' })
  1552. })
  1553. it('reports messages from throws of objects without message property', async () => {
  1554. const ctx = await setup()
  1555. ctx.tools.register({
  1556. ...echoTool,
  1557. name: 'object-no-message',
  1558. async execute() {
  1559. // testing object throw without .message
  1560. throw { code: 500 }
  1561. },
  1562. })
  1563. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'object-no-message', arguments: {} })
  1564. expect(result.isError).toBe(true)
  1565. const firstContent = result.content[0]!
  1566. expect(firstContent.type).toBe('text')
  1567. if (firstContent.type === 'text') {
  1568. expect(firstContent.text).toBe('Error: [object Object]')
  1569. }
  1570. })
  1571. })
  1572. describe('ToolRegistry.get', () => {
  1573. it('get() returns the registered tool definition', async () => {
  1574. const ctx = await setup()
  1575. ctx.tools.register(echoTool)
  1576. const tool = ctx.tools.get('echo')
  1577. expect(tool).toBeDefined()
  1578. expect(tool!.name).toBe('echo')
  1579. })
  1580. it('get() returns undefined for unknown tool names', async () => {
  1581. const ctx = await setup()
  1582. expect(ctx.tools.get('nope')).toBeUndefined()
  1583. })
  1584. })
  1585. describe('validateArgs (the runtime-validation Agent Note, part 1)', () => {
  1586. it('returns [] for valid args and is total over malformed input', () => {
  1587. const spec = {
  1588. path: { type: 'string', required: true },
  1589. limit: { type: 'number' },
  1590. } satisfies SchemaSpec
  1591. expect(validateArgs(spec, { path: '/tmp' })).toEqual([])
  1592. expect(validateArgs(spec, { path: '/tmp', limit: 5 })).toEqual([])
  1593. // never throws regardless of shape
  1594. expect(validateArgs(spec, null)).toHaveLength(1)
  1595. expect(validateArgs(spec, 'nope')).toHaveLength(1)
  1596. expect(validateArgs(spec, [])).toHaveLength(1)
  1597. })
  1598. it('flags a missing required key and a required key present as undefined', () => {
  1599. const spec = { path: { type: 'string', required: true } } satisfies SchemaSpec
  1600. expect(validateArgs(spec, {})).toEqual(['missing required property "path"'])
  1601. expect(validateArgs(spec, { path: undefined })).toEqual(['missing required property "path"'])
  1602. })
  1603. it('allows extra keys (no additionalProperties:false) and omitted optionals', () => {
  1604. const spec = { path: { type: 'string', required: true } } satisfies SchemaSpec
  1605. expect(validateArgs(spec, { path: '/tmp', extra: 1 })).toEqual([])
  1606. })
  1607. it('does not apply defaults (validation only)', () => {
  1608. const spec = { limit: { type: 'number', default: 25 } } satisfies SchemaSpec
  1609. // absent optional is valid, and validation does not synthesize the default
  1610. expect(validateArgs(spec, {})).toEqual([])
  1611. })
  1612. it('type-checks primitives', () => {
  1613. const spec = {
  1614. s: { type: 'string' },
  1615. n: { type: 'number' },
  1616. b: { type: 'boolean' },
  1617. } satisfies SchemaSpec
  1618. expect(validateArgs(spec, { s: 1 })).toEqual(['"s" must be a string'])
  1619. expect(validateArgs(spec, { n: 'x' })).toEqual(['"n" must be a number'])
  1620. expect(validateArgs(spec, { b: 'x' })).toEqual(['"b" must be a boolean'])
  1621. })
  1622. it('checks enum membership', () => {
  1623. const spec = { color: { type: 'string', enum: ['red', 'green'] } } satisfies SchemaSpec
  1624. expect(validateArgs(spec, { color: 'red' })).toEqual([])
  1625. expect(validateArgs(spec, { color: 'blue' })).toEqual(['"color" must be one of ["red","green"]'])
  1626. })
  1627. it('checks enum uniformly with the converter (enum on a non-string prop)', () => {
  1628. // The converter emits `enum` regardless of type; the validator must agree.
  1629. // `enum` is string[], so a number value can never be a member.
  1630. const spec = { n: { type: 'number', enum: ['1', '2'] } } as unknown as SchemaSpec
  1631. expect(validateArgs(spec, { n: 1 })).toEqual(['"n" must be one of ["1","2"]'])
  1632. })
  1633. it('rejects an unknown SchemaType at runtime (assertNever guard)', () => {
  1634. const spec = { x: { type: 'weird' } } as unknown as SchemaSpec
  1635. expect(() => validateArgs(spec, { x: 1 })).toThrow(/unreachable variant.*validateArgs/)
  1636. })
  1637. it('recurses into nested objects (and an object without properties only type-checks)', () => {
  1638. const spec = {
  1639. config: {
  1640. type: 'object',
  1641. required: true,
  1642. properties: { host: { type: 'string', required: true }, port: { type: 'number' } },
  1643. },
  1644. bag: { type: 'object' },
  1645. } satisfies SchemaSpec
  1646. expect(validateArgs(spec, { config: { host: 'h' }, bag: { anything: true } })).toEqual([])
  1647. expect(validateArgs(spec, { config: { port: 9 }, bag: 5 })).toEqual([
  1648. 'missing required property "config.host"',
  1649. '"bag" must be an object',
  1650. ])
  1651. })
  1652. it('recurses into array items (and an array without items only type-checks)', () => {
  1653. const spec = {
  1654. tags: { type: 'array', items: { type: 'string' } },
  1655. raw: { type: 'array' },
  1656. } satisfies SchemaSpec
  1657. expect(validateArgs(spec, { tags: ['a', 'b'], raw: [1, {}, 'x'] })).toEqual([])
  1658. expect(validateArgs(spec, { tags: ['a', 2] })).toEqual(['"tags[1]" must be a string'])
  1659. // a non-array value for an array-typed prop
  1660. expect(validateArgs(spec, { tags: 'nope' })).toEqual(['"tags" must be an array'])
  1661. })
  1662. it('validates arrays of objects element-wise', () => {
  1663. const spec = {
  1664. servers: {
  1665. type: 'array',
  1666. items: { type: 'object', properties: { host: { type: 'string', required: true } } },
  1667. },
  1668. } satisfies SchemaSpec
  1669. expect(validateArgs(spec, { servers: [{ host: 'a' }, {}] })).toEqual([
  1670. 'missing required property "servers[1].host"',
  1671. ])
  1672. })
  1673. })
  1674. describe('defineTool validation (the runtime-validation Agent Note, part 1)', () => {
  1675. it('returns an isError result with the violations when the model sends bad args', async () => {
  1676. const ctx = await setup()
  1677. ctx.tools.register(defineTool({
  1678. name: 'reader',
  1679. description: 'reads a path',
  1680. parameters: { path: { type: 'string', required: true } },
  1681. async execute(args) {
  1682. return [{ type: 'text', text: args.path }]
  1683. },
  1684. }))
  1685. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'reader', arguments: {} })
  1686. expect(result.isError).toBe(true)
  1687. expect(result.content[0]).toMatchObject({
  1688. text: 'Error: invalid arguments: missing required property "path"',
  1689. })
  1690. })
  1691. it('runs execute normally when args are valid', async () => {
  1692. const ctx = await setup()
  1693. ctx.tools.register(defineTool({
  1694. name: 'reader',
  1695. description: 'reads a path',
  1696. parameters: { path: { type: 'string', required: true } },
  1697. async execute(args) {
  1698. return [{ type: 'text', text: `read ${args.path}` }]
  1699. },
  1700. }))
  1701. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'reader', arguments: { path: '/x' } })
  1702. expect(result).toEqual({ content: [{ type: 'text', text: 'read /x' }], isError: false })
  1703. })
  1704. it('ToolArgsError carries a stable code and the violation list', () => {
  1705. const err = new ToolArgsError(['missing required property "a"', '"b" must be a number'])
  1706. expect(err).toBeInstanceOf(Error)
  1707. expect(err.name).toBe('ToolArgsError')
  1708. expect(err.code).toBe('INVALID_ARGS')
  1709. expect(err.violations).toEqual(['missing required property "a"', '"b" must be a number'])
  1710. expect(err.message).toBe('invalid arguments: missing required property "a"; "b" must be a number')
  1711. })
  1712. it('a schema-invalid call surfaces the structured error on the result', async () => {
  1713. const ctx = await setup()
  1714. ctx.tools.register(defineTool({
  1715. name: 'reader',
  1716. description: 'reads a path',
  1717. parameters: { path: { type: 'string', required: true } },
  1718. async execute(args) {
  1719. return [{ type: 'text', text: args.path }]
  1720. },
  1721. }))
  1722. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'reader', arguments: {} })
  1723. expect(result.isError).toBe(true)
  1724. expect(result.error).toEqual({ name: 'ToolArgsError', code: 'INVALID_ARGS' })
  1725. })
  1726. it('a tool throwing a HarnessError surfaces its name and code', async () => {
  1727. const { HarnessError } = await import('@deepseek-ai/dsh-llm')
  1728. const ctx = await setup()
  1729. ctx.tools.register({
  1730. ...echoTool,
  1731. name: 'coded',
  1732. async execute() {
  1733. throw new HarnessError('disk full', 'ENOSPC')
  1734. },
  1735. })
  1736. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'coded', arguments: {} })
  1737. expect(result.isError).toBe(true)
  1738. expect(result.error).toEqual({ name: 'HarnessError', code: 'ENOSPC' })
  1739. expect(result.content[0]).toMatchObject({ text: 'Error: disk full' })
  1740. })
  1741. it('a non-HarnessError throw has no structured error (only the text)', async () => {
  1742. const ctx = await setup()
  1743. ctx.tools.register({
  1744. ...echoTool,
  1745. name: 'plain',
  1746. async execute() {
  1747. throw new Error('just a message')
  1748. },
  1749. })
  1750. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'plain', arguments: {} })
  1751. expect(result.isError).toBe(true)
  1752. expect(result.error).toBeUndefined()
  1753. expect(result.content[0]).toMatchObject({ text: 'Error: just a message' })
  1754. })
  1755. it('raw-registered tools are NOT validated by defineTool (MCP keeps its own)', async () => {
  1756. const ctx = await setup()
  1757. // A raw ToolDefinition: no defineTool wrapping, so no validateArgs guard.
  1758. ctx.tools.register({
  1759. name: 'raw',
  1760. description: 'raw tool',
  1761. parameters: { type: 'object', properties: { path: { type: 'string' } }, required: ['path'] },
  1762. async execute(args: unknown) {
  1763. return [{ type: 'text', text: typeof args }]
  1764. },
  1765. })
  1766. // Missing the "required" path — but raw tools validate their own input, so
  1767. // this reaches execute rather than being rejected by the harness.
  1768. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'raw', arguments: {} })
  1769. expect(result.isError).toBe(false)
  1770. })
  1771. it('attaches a positive-finite timeoutMs to the definition', () => {
  1772. const tool = defineTool({
  1773. name: 'x', description: 'd', parameters: {}, timeoutMs: 30_000,
  1774. async execute() { return [{ type: 'text' as const, text: 'ok' }] },
  1775. })
  1776. expect(tool.timeoutMs).toBe(30_000)
  1777. })
  1778. it('omits timeoutMs when not declared', () => {
  1779. const tool = defineTool({
  1780. name: 'x', description: 'd', parameters: {},
  1781. async execute() { return [{ type: 'text' as const, text: 'ok' }] },
  1782. })
  1783. expect(tool.timeoutMs).toBeUndefined()
  1784. })
  1785. it('throws when timeoutMs is zero or negative', () => {
  1786. const make = (ms: number) => defineTool({
  1787. name: 'x', description: 'd', parameters: {}, timeoutMs: ms,
  1788. async execute() { return [{ type: 'text' as const, text: 'ok' }] },
  1789. })
  1790. expect(() => make(0)).toThrow('timeoutMs must be a positive finite number')
  1791. expect(() => make(-5)).toThrow('positive finite number')
  1792. })
  1793. it('throws when timeoutMs is non-finite', () => {
  1794. expect(() => defineTool({
  1795. name: 'x', description: 'd', parameters: {}, timeoutMs: Infinity,
  1796. async execute() { return [{ type: 'text' as const, text: 'ok' }] },
  1797. })).toThrow('positive finite number')
  1798. })
  1799. })
  1800. describe('defineTool presentation (presentCall / presentResult)', () => {
  1801. it('threads presentCall/presentResult onto the ToolDefinition with typed args', () => {
  1802. const tool = defineTool({
  1803. name: 'demo',
  1804. description: 'demo',
  1805. parameters: { path: { type: 'string', required: true }, n: { type: 'number' } },
  1806. async execute() { return [{ type: 'text', text: 'ok' }] },
  1807. presentCall(args) {
  1808. // args is typed { path: string; n?: number } — zero casts.
  1809. expectTypeOf(args).toEqualTypeOf<{ path: string; n?: number }>()
  1810. return { card: 'generic', title: `Open ${args.path}`, kind: 'read', rawInput: args.path }
  1811. },
  1812. presentResult(args, result) {
  1813. return { card: 'generic', title: `Opened ${args.path}`, content: result.content }
  1814. },
  1815. })
  1816. expect(tool.presentCall!({ path: '/a', n: 2 })).toEqual({ card: 'generic', title: 'Open /a', kind: 'read', rawInput: '/a' })
  1817. expect(tool.presentResult!({ path: '/a' }, { content: [{ type: 'text', text: 'x' }], isError: false }))
  1818. .toEqual({ card: 'generic', title: 'Opened /a', content: [{ type: 'text', text: 'x' }] })
  1819. })
  1820. it('a tool without presentCall/presentResult leaves them undefined (UI falls back generically)', () => {
  1821. const tool = defineTool({
  1822. name: 'plain',
  1823. description: 'plain',
  1824. parameters: { x: { type: 'string', required: true } },
  1825. async execute() { return [] },
  1826. })
  1827. expect(typeof tool.presentCall).toBe('undefined')
  1828. expect(typeof tool.presentResult).toBe('undefined')
  1829. })
  1830. it('presentCall/presentResult validate softly: malformed args return undefined, never throw (display runs on replay)', () => {
  1831. const tool = defineTool({
  1832. name: 'demo',
  1833. description: 'demo',
  1834. parameters: { path: { type: 'string', required: true } },
  1835. async execute() { return [] },
  1836. presentCall: args => ({ card: 'generic', title: args.path }),
  1837. presentResult: (args, result) => ({ card: 'generic', title: args.path, content: result.content }),
  1838. })
  1839. // Unlike execute (which throws ToolArgsError on a mismatch), the display
  1840. // methods soft-validate and fall back to undefined so a UI never crashes
  1841. // replaying an old/foreign log entry. The ToolDefinition methods take
  1842. // `unknown`, so malformed shapes pass without a cast.
  1843. expect(tool.presentCall?.({})).toBeUndefined()
  1844. expect(tool.presentResult?.({ wrong: 1 }, { content: [], isError: false })).toBeUndefined()
  1845. })
  1846. })