tools.spec.ts 102 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685
  1. import { describe, expect, expectTypeOf, it } from 'vitest'
  2. import { Context } from 'cordis'
  3. import { CallId, HarnessError, type ContentBlock } 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. defineContentToolFixture, defineTool, JsonSchemaError, parameterSchemaSpecToJsonSchema, validateArgs, ToolArgsError, ToolNotFoundError,
  9. TOOL_ABORTED, TOOL_ABORTED_BEFORE_DISPATCH,
  10. type InferArgs, type JsonValue, type ParameterSchemaSpec, type PreToolDecision, type PostToolDecision,
  11. type JsonSchemaNode, type ToolDefinition, type ToolDispatchExecution, type ToolExecutionResult, type ToolExecutionToken,
  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. output: {
  25. schema: { type: 'string' },
  26. render: (_args, value) => [{ type: 'text', text: value }],
  27. },
  28. async execute(args) {
  29. return args.text ?? ''
  30. },
  31. })
  32. describe('ToolRegistry', () => {
  33. it('registers tools, exposes schemas, and feeds the system-prompt assembly', async () => {
  34. const ctx = await setup()
  35. ctx.tools.register(echoTool)
  36. expect(ctx.tools.schemas()).toEqual([{
  37. name: 'echo',
  38. description: 'echo arguments back',
  39. parameters: { type: 'object', properties: { text: { type: 'string' } } },
  40. }])
  41. // schemas() result must not leak execute — ToolSchema deliberately has no
  42. // 'execute' key, so widen through unknown to probe for the absent property
  43. expect((ctx.tools.schemas()[0] as unknown as Record<string, unknown>).execute).toBeUndefined()
  44. const assembly = await ctx.systemPrompt.assemble()
  45. expect(assembly.tools.map(t => t.name)).toEqual(['echo'])
  46. })
  47. it('schemas() drops host callbacks — they must never reach the model', async () => {
  48. const ctx = await setup()
  49. // Tool definitions contain output, finalization, execution, and presentation
  50. // callbacks. schemas() is an explicit allowlist so none can reach the model.
  51. ctx.tools.register(defineContentToolFixture({
  52. name: 'present',
  53. description: 'has presenters',
  54. parameters: { x: { type: 'string', required: true } },
  55. async execute() { return [] },
  56. finalizeContent: (_exec, result) => result.content,
  57. presentCall: args => ({ card: 'generic', title: args.x }),
  58. presentResult: (args, result) => ({ card: 'generic', title: args.x, content: result.content }),
  59. }))
  60. const schema = ctx.tools.schemas()[0] as unknown as Record<string, unknown>
  61. expect(Object.keys(schema).sort()).toEqual(['description', 'name', 'parameters'])
  62. expect(schema.finalizeContent).toBeUndefined()
  63. expect(schema.presentCall).toBeUndefined()
  64. expect(schema.presentResult).toBeUndefined()
  65. expect(schema.execute).toBeUndefined()
  66. })
  67. it('schemas() excludes timeoutMs — the budget must never reach the model', async () => {
  68. const ctx = await setup()
  69. ctx.tools.register(defineContentToolFixture({
  70. name: 'budgeted', description: 'has a budget', parameters: {}, timeoutMs: 5_000,
  71. async execute() { return [{ type: 'text' as const, text: 'ok' }] },
  72. }))
  73. const schema = ctx.tools.schemas().find(s => s.name === 'budgeted')
  74. expect(schema).toBeDefined()
  75. expect('timeoutMs' in (schema as object)).toBe(false)
  76. })
  77. it('executes a tool and returns its content', async () => {
  78. const ctx = await setup()
  79. ctx.tools.register(echoTool)
  80. let observed: ToolExecutionResult | undefined
  81. ctx.on('tools/result', (_exec, result) => { observed = result })
  82. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'echo', arguments: { text: 'hi' } })
  83. expect(result).toEqual({ content: [{ type: 'text', text: 'hi' }], isError: false, value: 'hi' })
  84. expect(observed).toEqual(result)
  85. })
  86. it('projects presentation metadata from the canonical value', async () => {
  87. const ctx = await setup()
  88. ctx.tools.register({
  89. ...echoTool,
  90. name: 'meta-tool',
  91. output: {
  92. ...echoTool.output,
  93. presentationMeta: () => ({ diffs: [{ path: 'a', oldText: null, newText: 'x' }] }),
  94. },
  95. async execute() {
  96. return 'ok'
  97. },
  98. })
  99. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'meta-tool', arguments: {} })
  100. expect(result).toEqual({
  101. content: [{ type: 'text', text: 'ok' }],
  102. isError: false,
  103. meta: { diffs: [{ path: 'a', oldText: null, newText: 'x' }] },
  104. value: 'ok',
  105. })
  106. })
  107. it('omits meta when no presentation projector is declared', async () => {
  108. const ctx = await setup()
  109. ctx.tools.register({
  110. ...echoTool,
  111. name: 'no-meta-tool',
  112. async execute() {
  113. return 'ok'
  114. },
  115. })
  116. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'no-meta-tool', arguments: {} })
  117. expect(result).toEqual({ content: [{ type: 'text', text: 'ok' }], isError: false, value: 'ok' })
  118. expect('meta' in result).toBe(false)
  119. })
  120. it('normalizes a contract-violating non-cloneable result before final notification', async () => {
  121. const ctx = await setup()
  122. let observedError: boolean | undefined
  123. ctx.on('tools/result', (_exec, result) => { observedError = result.isError })
  124. ctx.tools.register({
  125. ...echoTool,
  126. name: 'bad-meta',
  127. output: {
  128. ...echoTool.output,
  129. presentationMeta: () => (() => undefined) as unknown as JsonValue,
  130. },
  131. async execute() {
  132. return 'ok'
  133. },
  134. })
  135. const result = await ctx.tools.execute({
  136. signal: testToolSignal,
  137. callId: CallId('bad-meta'), name: 'bad-meta', arguments: {},
  138. })
  139. expect(result.isError).toBe(true)
  140. expect(result.content[0]?.type === 'text' && result.content[0].text).toContain('Error:')
  141. expect(result.error).toMatchObject({ info: { name: 'ToolOutputError', code: 'INVALID_TOOL_OUTPUT' } })
  142. expect(observedError).toBe(true)
  143. })
  144. it('finalizes errors discovered while snapshotting non-content result fields', async () => {
  145. const ctx = await setup()
  146. let finalizeCalls = 0
  147. ctx.tools.register({
  148. ...echoTool,
  149. name: 'throwing-meta',
  150. output: {
  151. ...echoTool.output,
  152. presentationMeta() {
  153. const meta = {}
  154. Object.defineProperty(meta, 'value', {
  155. enumerable: true,
  156. get() { throw new Error('snapshot failed: '.repeat(100)) },
  157. })
  158. return meta
  159. },
  160. },
  161. finalizeContent(_exec, result) {
  162. finalizeCalls += 1
  163. const block = result.content[0]
  164. if (block?.type !== 'text') return undefined
  165. return [{ type: 'text', text: block.text.slice(0, 32) }]
  166. },
  167. async execute() {
  168. return 'body'
  169. },
  170. })
  171. const result = await ctx.tools.execute({
  172. signal: testToolSignal,
  173. callId: CallId('throwing-meta'), name: 'throwing-meta', arguments: {},
  174. })
  175. expect(result.isError).toBe(true)
  176. const block = result.content[0]
  177. expect(block?.type).toBe('text')
  178. expect(block?.type === 'text' ? block.text : '').toMatch(/^Error: tool "throwing-meta"/)
  179. expect(block?.type === 'text' ? block.text : '').toHaveLength(32)
  180. expect(finalizeCalls).toBe(1)
  181. })
  182. it('normalizes a throwing final content callback without invoking it again', async () => {
  183. const ctx = await setup()
  184. let finalizeCalls = 0
  185. ctx.tools.register({
  186. ...echoTool,
  187. name: 'throwing-finalizer',
  188. finalizeContent() {
  189. finalizeCalls += 1
  190. throw new Error('finalizer violated its total contract')
  191. },
  192. })
  193. const result = await ctx.tools.execute({
  194. signal: testToolSignal,
  195. callId: CallId('throwing-finalizer'), name: 'throwing-finalizer', arguments: {},
  196. })
  197. expect(result).toEqual({
  198. content: [{ type: 'text', text: 'Error: finalizer violated its total contract' }],
  199. isError: true,
  200. error: { message: 'finalizer violated its total contract' },
  201. })
  202. expect(finalizeCalls).toBe(1)
  203. })
  204. it('requires every raw registration to declare its canonical output', async () => {
  205. const ctx = await setup()
  206. const missingOutput = {
  207. name: 'legacy-content-tool',
  208. description: 'missing output',
  209. parameters: {},
  210. execute: async () => [{ type: 'text', text: 'legacy' }],
  211. } as unknown as ToolDefinition
  212. expect(() => ctx.tools.register(missingOutput))
  213. .toThrow('must declare output { schema, render, presentationMeta? }')
  214. })
  215. it('rejects lossy and schema-mismatched body values before post-execute', async () => {
  216. const ctx = await setup()
  217. ctx.tools.register(defineTool({
  218. name: 'lossy-output',
  219. description: 'lossy',
  220. parameters: {},
  221. output: { schema: { type: 'json' }, render: () => [] },
  222. execute: async () => (() => undefined) as unknown as JsonValue,
  223. }))
  224. ctx.tools.register(defineTool({
  225. name: 'wrong-output',
  226. description: 'wrong schema',
  227. parameters: {},
  228. output: { schema: { type: 'string' }, render: () => [] },
  229. execute: async () => 42 as unknown as string,
  230. }))
  231. const lossy = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('lossy'), name: 'lossy-output', arguments: {} })
  232. const mismatch = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('mismatch'), name: 'wrong-output', arguments: {} })
  233. expect(lossy.error).toMatchObject({ info: { name: 'ToolOutputError', code: 'INVALID_TOOL_OUTPUT' } })
  234. expect(lossy.content[0]?.type === 'text' ? lossy.content[0].text : '').toContain('not lossless JSON')
  235. expect(mismatch.error).toMatchObject({ info: { name: 'ToolOutputError', code: 'INVALID_TOOL_OUTPUT' } })
  236. expect(mismatch.content[0]?.type === 'text' ? mismatch.content[0].text : '').toContain('"value" must be a string')
  237. })
  238. it('classifies a throwing body snapshot as invalid tool output', async () => {
  239. const ctx = await setup()
  240. const hostile = Object.defineProperty({}, 'value', {
  241. enumerable: true,
  242. get: () => { throw new Error('body snapshot getter exploded') },
  243. })
  244. ctx.tools.register(defineTool({
  245. name: 'hostile-body',
  246. description: 'hostile body',
  247. parameters: {},
  248. output: { schema: { type: 'json' }, render: () => [] },
  249. execute: async () => hostile as JsonValue,
  250. }))
  251. const result = await ctx.tools.execute({
  252. signal: testToolSignal,
  253. callId: CallId('hostile-body'), name: 'hostile-body', arguments: {},
  254. })
  255. expect(result.error?.message).toContain('value snapshot failed: body snapshot getter exploded')
  256. expect(result.error?.info).toEqual({ name: 'ToolOutputError', code: 'INVALID_TOOL_OUTPUT' })
  257. })
  258. it.each(['render', 'presentationMeta'] as const)('contains a throwing output.%s projector as one failed call', async (projector) => {
  259. const ctx = await setup()
  260. ctx.tools.register(defineTool({
  261. name: `throwing-${projector}`,
  262. description: projector,
  263. parameters: {},
  264. output: {
  265. schema: { type: 'string' },
  266. render: () => {
  267. if (projector === 'render') throw new Error('renderer exploded')
  268. return [{ type: 'text', text: 'ok' }]
  269. },
  270. presentationMeta: () => {
  271. if (projector === 'presentationMeta') throw new Error('metadata exploded')
  272. return null
  273. },
  274. },
  275. execute: async () => 'ok',
  276. }))
  277. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId(projector), name: `throwing-${projector}`, arguments: {} })
  278. expect(result.isError).toBe(true)
  279. expect(result.error?.message)
  280. .toContain(projector === 'render' ? 'renderer exploded' : 'metadata exploded')
  281. expect(result.error?.info).toEqual({ name: 'ToolOutputError', code: 'INVALID_TOOL_OUTPUT' })
  282. expect('value' in result).toBe(false)
  283. })
  284. it.each(['render', 'presentationMeta'] as const)('contains a throwing output.%s snapshot as one failed call', async (projector) => {
  285. const ctx = await setup()
  286. const hostile = Object.defineProperty({}, 'value', {
  287. enumerable: true,
  288. get: () => { throw new Error('snapshot getter exploded') },
  289. })
  290. ctx.tools.register(defineTool({
  291. name: `hostile-${projector}`,
  292. description: projector,
  293. parameters: {},
  294. output: {
  295. schema: { type: 'string' },
  296. render: () => projector === 'render'
  297. ? hostile as unknown as ContentBlock[]
  298. : [{ type: 'text', text: 'ok' }],
  299. presentationMeta: () => projector === 'presentationMeta'
  300. ? hostile as unknown as JsonValue
  301. : null,
  302. },
  303. execute: async () => 'ok',
  304. }))
  305. const result = await ctx.tools.execute({
  306. signal: testToolSignal,
  307. callId: CallId(`hostile-${projector}`), name: `hostile-${projector}`, arguments: {},
  308. })
  309. expect(result.error?.message).toContain('snapshot getter exploded')
  310. expect(result.error?.info).toEqual({ name: 'ToolOutputError', code: 'INVALID_TOOL_OUTPUT' })
  311. })
  312. it('keeps value/meta through content replacement and recomputes both projections after value replacement', async () => {
  313. const ctx = await setup()
  314. ctx.tools.register(defineTool({
  315. name: 'projected',
  316. description: 'projected',
  317. parameters: {},
  318. output: {
  319. schema: {
  320. type: 'object',
  321. additionalProperties: false,
  322. properties: { text: { type: 'string', required: true } },
  323. },
  324. render: (_args, value) => [{ type: 'text', text: `render:${value.text}` }],
  325. presentationMeta: (_args, value) => ({ projected: value.text }),
  326. },
  327. execute: async () => ({ text: 'body' }),
  328. }))
  329. let replacement: 'content' | 'value' = 'content'
  330. ctx.on('tools/post-execute', async () => {
  331. if (replacement === 'content') {
  332. return { kind: 'accept', content: [{ type: 'text', text: 'policy content' }] }
  333. }
  334. return {
  335. kind: 'accept',
  336. value: { text: 'policy value' },
  337. additionalContexts: [{ content: [{ type: 'text', text: 'value context' }], source: { kind: 'plugin', plugin: 'test' } }],
  338. }
  339. })
  340. const content = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('content'), name: 'projected', arguments: {} })
  341. replacement = 'value'
  342. const value = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('value'), name: 'projected', arguments: {} })
  343. expect(content).toEqual({
  344. isError: false,
  345. value: { text: 'body' },
  346. content: [{ type: 'text', text: 'policy content' }],
  347. meta: { projected: 'body' },
  348. })
  349. expect(value).toEqual({
  350. isError: false,
  351. value: { text: 'policy value' },
  352. content: [{ type: 'text', text: 'render:policy value' }],
  353. meta: { projected: 'policy value' },
  354. additionalContexts: [{ content: [{ type: 'text', text: 'value context' }], source: { kind: 'plugin', plugin: 'test' } }],
  355. })
  356. })
  357. it('fails a post-execute decision that replaces both projections or supplies an invalid value', async () => {
  358. const both = await setup()
  359. both.tools.register(echoTool)
  360. both.on('tools/post-execute', async () => ({
  361. kind: 'accept',
  362. value: 'replacement',
  363. content: [{ type: 'text', text: 'also replacement' }],
  364. } as unknown as PostToolDecision))
  365. const bothResult = await both.tools.execute({ signal: testToolSignal, callId: CallId('both'), name: 'echo', arguments: {} })
  366. expect(bothResult).toMatchObject({
  367. isError: true,
  368. error: { message: 'tools/post-execute accept decision cannot replace both value and content' },
  369. })
  370. const invalid = await setup()
  371. invalid.tools.register(echoTool)
  372. invalid.on('tools/post-execute', async () => ({ kind: 'accept', value: 1 }))
  373. const invalidResult = await invalid.tools.execute({ signal: testToolSignal, callId: CallId('invalid'), name: 'echo', arguments: {} })
  374. expect(invalidResult.error).toMatchObject({ info: { code: 'INVALID_TOOL_OUTPUT' } })
  375. expect('value' in invalidResult).toBe(false)
  376. })
  377. it('turns a post-execute block into a valueless failure', async () => {
  378. const ctx = await setup()
  379. ctx.tools.register(echoTool)
  380. ctx.on('tools/post-execute', async () => ({
  381. kind: 'block',
  382. feedback: [{ type: 'text', text: 'blocked by policy' }],
  383. }))
  384. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('block'), name: 'echo', arguments: { text: 'secret' } })
  385. expect(result).toEqual({
  386. isError: true,
  387. error: { message: 'blocked by policy' },
  388. content: [{ type: 'text', text: 'blocked by policy' }],
  389. })
  390. expect('value' in result).toBe(false)
  391. })
  392. it('replaces a canonical value without manufacturing additional context', async () => {
  393. const ctx = await setup()
  394. ctx.tools.register(echoTool)
  395. ctx.on('tools/post-execute', async () => ({ kind: 'accept', value: 'replacement' }))
  396. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('replace-value'), name: 'echo', arguments: {} })
  397. expect(result).toEqual({
  398. isError: false,
  399. value: 'replacement',
  400. content: [{ type: 'text', text: 'replacement' }],
  401. })
  402. })
  403. it.each([
  404. [[], 'tool result blocked by post-execute policy'],
  405. [[{ type: 'reasoning', text: 'private rationale' }], '[reasoning content]'],
  406. ] as const)('derives a stable failure message from non-text or empty block feedback', async (feedback, message) => {
  407. const ctx = await setup()
  408. ctx.tools.register(echoTool)
  409. ctx.on('tools/post-execute', async () => ({ kind: 'block', feedback: [...feedback] }))
  410. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('block-message'), name: 'echo', arguments: {} })
  411. expect(result.error?.message).toBe(message)
  412. })
  413. it('contains a non-JSON post-execute failure projection as a safe final error', async () => {
  414. const ctx = await setup()
  415. ctx.tools.register(echoTool)
  416. ctx.on('tools/post-execute', async () => ({
  417. kind: 'block',
  418. feedback: [{ type: 'text', text: 'blocked', invalid: () => undefined } as never],
  419. }))
  420. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('invalid-block'), name: 'echo', arguments: {} })
  421. expect(result).toMatchObject({
  422. isError: true,
  423. error: { message: 'tool result must be losslessly JSON-serializable' },
  424. })
  425. })
  426. it('rejects value replacement on a failed dispatch', async () => {
  427. const ctx = await setup()
  428. ctx.tools.register({
  429. ...echoTool,
  430. name: 'throw-before-replace',
  431. async execute() { throw new Error('body failed') },
  432. })
  433. ctx.on('tools/post-execute', async () => ({ kind: 'accept', value: 'replacement' }))
  434. const result = await ctx.tools.execute({
  435. signal: testToolSignal,
  436. callId: CallId('failed-replace'), name: 'throw-before-replace', arguments: {},
  437. })
  438. expect(result.error?.message).toBe('tools/post-execute cannot replace the value of a failed result')
  439. })
  440. it('fails value replacement when the owning tool disappears before post-policy resolves', async () => {
  441. const ctx = await setup()
  442. const dispose = ctx.tools.register(echoTool)
  443. ctx.on('tools/post-execute', async () => {
  444. dispose()
  445. return { kind: 'accept', value: 'replacement' }
  446. })
  447. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('post-disposed'), name: 'echo', arguments: {} })
  448. expect(result.error).toEqual({
  449. message: 'unknown tool "echo"',
  450. info: { name: 'ToolNotFoundError', code: 'UNKNOWN_TOOL' },
  451. })
  452. })
  453. it('normalizes wrapper-authored failure metadata and contexts', async () => {
  454. const ctx = await setup()
  455. ctx.tools.register(echoTool)
  456. ctx.on('tools/execute', async () => ({
  457. isError: true,
  458. error: { message: 'wrapped failure' },
  459. content: [{ type: 'text', text: 'wrapper content' }],
  460. meta: { wrapped: true },
  461. additionalContexts: [{ content: [{ type: 'text', text: 'wrapper context' }], source: { kind: 'plugin', plugin: 'test' } }],
  462. }))
  463. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('wrapper-failure'), name: 'echo', arguments: {} })
  464. expect(result).toEqual({
  465. isError: true,
  466. error: { message: 'wrapped failure' },
  467. content: [{ type: 'text', text: 'wrapper content' }],
  468. meta: { wrapped: true },
  469. additionalContexts: [{ content: [{ type: 'text', text: 'wrapper context' }], source: { kind: 'plugin', plugin: 'test' } }],
  470. })
  471. })
  472. it('fails wrapper-authored success normalization when the owning tool disappears', async () => {
  473. const ctx = await setup()
  474. const dispose = ctx.tools.register(echoTool)
  475. ctx.on('tools/execute', async () => {
  476. dispose()
  477. return { isError: false, value: 'replacement', content: [] }
  478. })
  479. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('wrapper-disposed'), name: 'echo', arguments: {} })
  480. expect(result.error).toEqual({
  481. message: 'unknown tool "echo"',
  482. info: { name: 'ToolNotFoundError', code: 'UNKNOWN_TOOL' },
  483. })
  484. })
  485. it('suppresses presentation metadata only for nested composite dispatches', async () => {
  486. const ctx = await setup()
  487. ctx.tools.register({
  488. ...echoTool,
  489. name: 'meta-suppression',
  490. output: { ...echoTool.output, presentationMeta: () => ({ card: true }) },
  491. })
  492. const direct = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('direct'), name: 'meta-suppression', arguments: {} })
  493. const nested = await ctx.tools.execute({
  494. signal: testToolSignal,
  495. callId: CallId('nested'),
  496. name: 'meta-suppression',
  497. arguments: {},
  498. parent: Symbol('outer') as ToolExecutionToken,
  499. })
  500. expect(direct.meta).toEqual({ card: true })
  501. expect(nested.meta).toBeUndefined()
  502. expect(nested.isError ? undefined : nested.value).toBe('')
  503. })
  504. it('returns isError results for unknown tools and throwing tools', async () => {
  505. const ctx = await setup()
  506. ctx.tools.register({
  507. ...echoTool,
  508. name: 'boom',
  509. async execute() {
  510. throw new Error('exploded')
  511. },
  512. })
  513. const unknown = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'nope', arguments: {} })
  514. expect(unknown.isError).toBe(true)
  515. expect(unknown.content[0]).toMatchObject({ text: 'Error: unknown tool "nope"' })
  516. // An unknown tool is a routable failure class, same as a tool-thrown one.
  517. expect(unknown.error).toEqual({
  518. message: 'unknown tool "nope"',
  519. info: { name: 'ToolNotFoundError', code: 'UNKNOWN_TOOL' },
  520. })
  521. const thrown = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c2'), name: 'boom', arguments: {} })
  522. expect(thrown.isError).toBe(true)
  523. expect(thrown.content[0]).toMatchObject({ text: 'Error: exploded' })
  524. })
  525. it('normalizes a hostile thrown value whose inspection and coercion both throw', async () => {
  526. const ctx = await setup()
  527. ctx.tools.register({
  528. ...echoTool,
  529. name: 'hostile-throw',
  530. async execute() {
  531. throw new Proxy({}, {
  532. getPrototypeOf: () => { throw new Error('prototype trap') },
  533. has: () => { throw new Error('has trap') },
  534. get: () => { throw new Error('get trap') },
  535. })
  536. },
  537. })
  538. await expect(ctx.tools.execute({
  539. signal: testToolSignal,
  540. callId: CallId('hostile'), name: 'hostile-throw', arguments: {},
  541. })).resolves.toMatchObject({
  542. isError: true,
  543. content: [{ type: 'text', text: 'Error: <unprintable thrown value>' }],
  544. })
  545. })
  546. it('ToolNotFoundError carries a stable message and code', async () => {
  547. const { HarnessError } = await import('@deepseek-ai/dsh-llm')
  548. const err = new ToolNotFoundError('ghost')
  549. expect(err).toBeInstanceOf(HarnessError)
  550. expect(err.name).toBe('ToolNotFoundError')
  551. expect(err.code).toBe('UNKNOWN_TOOL')
  552. expect(err.message).toBe('unknown tool "ghost"')
  553. })
  554. it('lets a tools/pre-execute listener deny a call (permission pattern)', async () => {
  555. const ctx = await setup()
  556. ctx.tools.register(echoTool)
  557. let postSawFrozen = false
  558. ctx.on('tools/pre-execute', async (exec, next): Promise<PreToolDecision> => {
  559. if (exec.name === 'echo') return { kind: 'deny', reason: 'denied by policy' }
  560. return next()
  561. })
  562. ctx.on('tools/post-execute', async (_exec, result, next) => {
  563. postSawFrozen = Object.isFrozen(result)
  564. expect(Reflect.set(result, 'content', [])).toBe(false)
  565. return next()
  566. })
  567. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'echo', arguments: { text: 'hi' } })
  568. expect(result.isError).toBe(true)
  569. expect(result.content[0]).toMatchObject({ text: 'Error: denied by policy' })
  570. expect(postSawFrozen).toBe(true)
  571. })
  572. it('an ask decision degrades to deny when no approval seam is mounted', async () => {
  573. const ctx = await setup()
  574. ctx.tools.register(echoTool)
  575. ctx.on('tools/pre-execute', async (_exec, _next): Promise<PreToolDecision> =>
  576. ({ kind: 'ask', reason: 'needs approval' }))
  577. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'echo', arguments: { text: 'hi' } })
  578. expect(result.isError).toBe(true)
  579. expect(result.content[0]).toMatchObject({ text: 'Error: needs approval' })
  580. })
  581. it('an ask decision with no reason degrades to deny with a default message', async () => {
  582. const ctx = await setup()
  583. ctx.tools.register(echoTool)
  584. ctx.on('tools/pre-execute', async (_exec, _next): Promise<PreToolDecision> => ({ kind: 'ask' }))
  585. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'echo', arguments: { text: 'hi' } })
  586. expect(result.isError).toBe(true)
  587. expect(result.content[0]).toMatchObject({ text: 'Error: tool "echo" requires approval (not yet supported)' })
  588. })
  589. describe('ask routing through ctx.approval', () => {
  590. /**
  591. * A minimal Agent stand-in — the approval seam reaches
  592. * `agent.session.append` and folds `.events`; the seeded open turn
  593. * satisfies request()'s enclosure precondition.
  594. */
  595. function fakeAgent(): Agent {
  596. return {
  597. session: { events: [{ type: 'turn/start' }], append: () => ({}) },
  598. } as unknown as Agent
  599. }
  600. async function approvalSetup() {
  601. const ctx = await setup()
  602. await ctx.plugin(ApprovalService)
  603. ctx.tools.register(echoTool)
  604. return ctx
  605. }
  606. it('dispatches the tool when the answerer grants allowed-once, forwarding the ask fields', async () => {
  607. const ctx = await approvalSetup()
  608. const agent = fakeAgent()
  609. const controller = new AbortController()
  610. const seen: ApprovalRequest[] = []
  611. ctx.on('approval/request', (req) => {
  612. seen.push(req)
  613. return Promise.resolve<ApprovalOutcome>('allowed-once')
  614. })
  615. ctx.on('tools/pre-execute', async (_exec, _next): Promise<PreToolDecision> =>
  616. ({ kind: 'ask', reason: 'hook wants a human' }))
  617. const result = await ctx.tools.execute({
  618. callId: CallId('c1'), name: 'echo', arguments: { text: 'hi' }, agent, signal: controller.signal,
  619. })
  620. expect(result).toMatchObject({ isError: false, content: [{ type: 'text', text: 'hi' }] })
  621. expect(seen).toHaveLength(1)
  622. expect(seen[0]).toMatchObject({ agent, toolName: 'echo', callId: 'c1', reason: 'hook wants a human' })
  623. expect(seen[0]?.signal).toBe(controller.signal)
  624. })
  625. it('denies with the user-rejection reason on rejected', async () => {
  626. const ctx = await approvalSetup()
  627. ctx.on('approval/request', () => Promise.resolve<ApprovalOutcome>('rejected'))
  628. ctx.on('tools/pre-execute', async (_exec, _next): Promise<PreToolDecision> => ({ kind: 'ask' }))
  629. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'echo', arguments: {}, agent: fakeAgent() })
  630. expect(result.isError).toBe(true)
  631. expect(result.content[0]).toMatchObject({ text: 'Error: the user rejected tool "echo"' })
  632. })
  633. it('denies with the cancellation reason on cancelled', async () => {
  634. const ctx = await approvalSetup()
  635. ctx.on('approval/request', () => Promise.resolve<ApprovalOutcome>('cancelled'))
  636. ctx.on('tools/pre-execute', async (_exec, _next): Promise<PreToolDecision> => ({ kind: 'ask' }))
  637. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'echo', arguments: {}, agent: fakeAgent() })
  638. expect(result.isError).toBe(true)
  639. expect(result.content[0]).toMatchObject({ text: 'Error: approval for tool "echo" was cancelled' })
  640. })
  641. it('returns ABORTED_BEFORE_DISPATCH when caller cancellation overtakes approval', async () => {
  642. const ctx = await approvalSetup()
  643. const entered = Promise.withResolvers<undefined>()
  644. const release = Promise.withResolvers<ApprovalOutcome>()
  645. let dispatched = 0
  646. ctx.tools.register({
  647. ...echoTool,
  648. name: 'approval-probe',
  649. async execute() { dispatched += 1; return [] },
  650. })
  651. ctx.on('approval/request', () => {
  652. entered.resolve(undefined)
  653. return release.promise
  654. })
  655. ctx.on('tools/pre-execute', async (_exec, _next): Promise<PreToolDecision> => ({ kind: 'ask' }))
  656. const controller = new AbortController()
  657. const pending = ctx.tools.execute({
  658. callId: CallId('approval-cancelled'),
  659. name: 'approval-probe',
  660. arguments: {},
  661. agent: fakeAgent(),
  662. signal: controller.signal,
  663. })
  664. await entered.promise
  665. controller.abort('caller cancelled approval')
  666. release.resolve('allowed-once')
  667. await expect(pending).resolves.toMatchObject({
  668. isError: true,
  669. error: { info: { name: 'AbortError', code: TOOL_ABORTED_BEFORE_DISPATCH } },
  670. })
  671. expect(dispatched).toBe(0)
  672. })
  673. it('denies with the no-channel reason when the seam is mounted but nobody answers', async () => {
  674. const ctx = await approvalSetup()
  675. ctx.on('tools/pre-execute', async (_exec, _next): Promise<PreToolDecision> => ({ kind: 'ask' }))
  676. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'echo', arguments: {}, agent: fakeAgent() })
  677. expect(result.isError).toBe(true)
  678. expect(result.content[0]).toMatchObject({ text: 'Error: tool "echo" requires approval, but no approval channel is available' })
  679. })
  680. it('denies an agent-less execution without asking — nothing to route or audit through', async () => {
  681. const ctx = await approvalSetup()
  682. let asked = false
  683. ctx.on('approval/request', () => {
  684. asked = true
  685. return Promise.resolve<ApprovalOutcome>('allowed-once')
  686. })
  687. ctx.on('tools/pre-execute', async (_exec, _next): Promise<PreToolDecision> => ({ kind: 'ask' }))
  688. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'echo', arguments: {} })
  689. expect(asked).toBe(false)
  690. expect(result.isError).toBe(true)
  691. expect(result.content[0]).toMatchObject({ text: 'Error: tool "echo" requires approval, but the call has no agent to route it through' })
  692. })
  693. it('turns a rogue outcome from a NON-conforming approval stand-in into an isError result', async () => {
  694. // ApprovalService normalizes rogue answers itself; this pins the
  695. // registry's own exhaustiveness backstop by shadowing the service with a
  696. // stand-in that violates the outcome contract.
  697. const ctx = await setup()
  698. ctx.tools.register(echoTool)
  699. ctx.provide('approval', { request: () => Promise.resolve('yolo') } as unknown as ApprovalService)
  700. ctx.on('tools/pre-execute', async (_exec, _next): Promise<PreToolDecision> => ({ kind: 'ask' }))
  701. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'echo', arguments: {}, agent: fakeAgent() })
  702. expect(result.isError).toBe(true)
  703. const text = result.content[0]?.type === 'text' ? result.content[0].text : ''
  704. expect(text).toContain('unreachable')
  705. })
  706. })
  707. it('a tools/post-execute listener can replace the result content (accept) ', async () => {
  708. const ctx = await setup()
  709. ctx.tools.register(echoTool)
  710. ctx.on('tools/post-execute', async (_exec, _result, _next): Promise<PostToolDecision> =>
  711. ({ kind: 'accept', content: [{ type: 'text', text: 'rewritten' }] }))
  712. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'echo', arguments: { text: 'hi' } })
  713. expect(result.isError).toBe(false)
  714. expect(result.content[0]).toMatchObject({ text: 'rewritten' })
  715. })
  716. it('a tools/post-execute block turns the call into an isError with corrective feedback', async () => {
  717. const ctx = await setup()
  718. ctx.tools.register(echoTool)
  719. ctx.on('tools/post-execute', async (_exec, _result, _next): Promise<PostToolDecision> =>
  720. ({ kind: 'block', feedback: [{ type: 'text', text: 'output rejected: try again' }] }))
  721. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'echo', arguments: { text: 'hi' } })
  722. expect(result.isError).toBe(true)
  723. expect(result.content[0]).toMatchObject({ text: 'output rejected: try again' })
  724. })
  725. it('runs the snapshotted final content transform after outer pipeline normalization', async () => {
  726. const ctx = await setup()
  727. const dispose = ctx.tools.register(defineContentToolFixture({
  728. name: 'bounded',
  729. description: 'bounded result',
  730. parameters: {},
  731. async execute() { return [{ type: 'text', text: 'body' }] },
  732. finalizeContent(exec, result) {
  733. expect(exec.name).toBe('bounded')
  734. expect(result.isError).toBe(true)
  735. return [{ type: 'text', text: 'bounded failure' }]
  736. },
  737. }))
  738. ctx.on('tools/pre-execute', async () => {
  739. dispose()
  740. throw new HarnessError('policy failed', 'POLICY_FAILED')
  741. })
  742. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('bounded'), name: 'bounded', arguments: {} })
  743. expect(result).toEqual({
  744. content: [{ type: 'text', text: 'bounded failure' }],
  745. isError: true,
  746. error: {
  747. message: 'policy failed',
  748. info: { name: 'HarnessError', code: 'POLICY_FAILED' },
  749. },
  750. })
  751. })
  752. it('a block decision can ALSO attach additionalContexts', async () => {
  753. const ctx = await setup()
  754. ctx.tools.register(echoTool)
  755. ctx.on('tools/post-execute', async (_exec, _result, _next): Promise<PostToolDecision> =>
  756. ({
  757. kind: 'block',
  758. feedback: [{ type: 'text', text: 'rejected' }],
  759. additionalContexts: [{ content: [{ type: 'text', text: 'why it was rejected' }], source: { kind: 'plugin', plugin: 'test' } }],
  760. }))
  761. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'echo', arguments: { text: 'hi' } })
  762. expect(result.isError).toBe(true)
  763. expect(result.content[0]).toMatchObject({ text: 'rejected' })
  764. expect(result.additionalContexts).toMatchObject([{ content: [{ text: 'why it was rejected' }], source: { kind: 'plugin', plugin: 'test' } }])
  765. })
  766. it('post-execute additionalContexts ride on the result for the loop to buffer', async () => {
  767. const ctx = await setup()
  768. ctx.tools.register(echoTool)
  769. ctx.on('tools/post-execute', async (_exec, _result, _next): Promise<PostToolDecision> =>
  770. ({ kind: 'accept', additionalContexts: [{ content: [{ type: 'text', text: 'fyi' }], source: { kind: 'plugin', plugin: 'test' } }] }))
  771. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'echo', arguments: { text: 'hi' } })
  772. expect(result.additionalContexts).toMatchObject([{ content: [{ text: 'fyi' }], source: { kind: 'plugin', plugin: 'test' } }])
  773. })
  774. it('preserves tool-deferred, execute-wrapper, and post-execute contexts in order', async () => {
  775. const ctx = await setup()
  776. ctx.tools.register(defineContentToolFixture({
  777. name: 'composite',
  778. description: 'composite',
  779. parameters: {},
  780. async execute(_args, exec) {
  781. exec.deferContext({ content: [{ type: 'text', text: 'nested-1' }], source: { kind: 'plugin', plugin: 'nested-1' }, meta: { n: 1 } })
  782. exec.deferContext({ content: [{ type: 'text', text: 'nested-2' }], source: { kind: 'plugin', plugin: 'nested-2' } })
  783. return [{ type: 'text', text: 'done' }]
  784. },
  785. }))
  786. ctx.on('tools/execute', async (_exec, next) => {
  787. const result = await next()
  788. return {
  789. ...result,
  790. additionalContexts: [
  791. ...result.additionalContexts ?? [],
  792. { content: [{ type: 'text', text: 'wrapper' }], source: { kind: 'plugin', plugin: 'wrapper' } },
  793. ],
  794. }
  795. })
  796. ctx.on('tools/post-execute', async (_exec, _result, next): Promise<PostToolDecision> => {
  797. const downstream = await next()
  798. return {
  799. ...downstream,
  800. additionalContexts: [
  801. { content: [{ type: 'text', text: 'post' }], source: { kind: 'plugin', plugin: 'post' } },
  802. ...downstream.additionalContexts ?? [],
  803. ],
  804. }
  805. })
  806. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('composite'), name: 'composite', arguments: {} })
  807. expect(result.additionalContexts?.map(context => context.source)).toEqual([
  808. { kind: 'plugin', plugin: 'nested-1' },
  809. { kind: 'plugin', plugin: 'nested-2' },
  810. { kind: 'plugin', plugin: 'wrapper' },
  811. { kind: 'plugin', plugin: 'post' },
  812. ])
  813. expect(result.additionalContexts?.[0]?.meta).toEqual({ n: 1 })
  814. })
  815. it('keeps deferred contexts when a composite tool throws, but drops them when the outer call is blocked', async () => {
  816. const ctx = await setup()
  817. ctx.tools.register(defineContentToolFixture({
  818. name: 'failing-composite',
  819. description: 'failing composite',
  820. parameters: {},
  821. async execute(_args, exec) {
  822. exec.deferContext({ content: [{ type: 'text', text: 'nested' }], source: { kind: 'plugin', plugin: 'nested' } })
  823. throw new Error('outer failure')
  824. },
  825. }))
  826. const failed = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('failed'), name: 'failing-composite', arguments: {} })
  827. expect(failed.isError).toBe(true)
  828. expect(failed.additionalContexts?.map(context => context.source)).toEqual([{ kind: 'plugin', plugin: 'nested' }])
  829. ctx.on('tools/post-execute', async (): Promise<PostToolDecision> => ({
  830. kind: 'block',
  831. feedback: [{ type: 'text', text: 'blocked' }],
  832. additionalContexts: [{ content: [{ type: 'text', text: 'block-only' }], source: { kind: 'plugin', plugin: 'blocker' } }],
  833. }))
  834. const blocked = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('blocked'), name: 'failing-composite', arguments: {} })
  835. expect(blocked.isError).toBe(true)
  836. expect(blocked.additionalContexts?.map(context => context.source)).toEqual([{ kind: 'plugin', plugin: 'blocker' }])
  837. })
  838. it('composes pre + post waterfalls around dispatch (sandbox-wrap pattern)', async () => {
  839. const ctx = await setup()
  840. ctx.tools.register(echoTool)
  841. const order: string[] = []
  842. ctx.on('tools/pre-execute', async (_exec, next) => {
  843. order.push('pre:before')
  844. const decision = await next()
  845. order.push('pre:after')
  846. return decision
  847. })
  848. ctx.on('tools/post-execute', async (_exec, _result, next) => {
  849. order.push('post:before')
  850. const decision = await next()
  851. order.push('post:after')
  852. return decision
  853. })
  854. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'echo', arguments: { text: 'x' } })
  855. expect(result.isError).toBe(false)
  856. // pre runs fully (gate) before dispatch, then post runs over the result.
  857. expect(order).toEqual(['pre:before', 'pre:after', 'post:before', 'post:after'])
  858. })
  859. it('runs tools/execute after an allowed pre-execute, around dispatch, and before post-execute', async () => {
  860. const ctx = await setup()
  861. const order: string[] = []
  862. ctx.tools.register(defineContentToolFixture({
  863. name: 'traced',
  864. description: 'echo',
  865. parameters: { text: { type: 'string' } },
  866. async execute(args) {
  867. order.push('dispatch')
  868. return [{ type: 'text' as const, text: args.text ?? '' }]
  869. },
  870. }))
  871. ctx.on('tools/pre-execute', async (_exec, next) => { order.push('pre'); return next() })
  872. ctx.on('tools/execute', async (_exec: ToolDispatchExecution, next: () => Promise<ToolExecutionResult>): Promise<ToolExecutionResult> => {
  873. order.push('execute:before')
  874. const result = await next()
  875. order.push('execute:after')
  876. return result
  877. })
  878. ctx.on('tools/post-execute', async (_exec, _result, next) => { order.push('post'); return next() })
  879. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'traced', arguments: { text: 'hi' } })
  880. expect(result).toEqual({ content: [{ type: 'text', text: 'hi' }], isError: false, value: [{ type: 'text', text: 'hi' }] })
  881. // The around seam wraps dispatch; pre gates before it, post runs over its result.
  882. expect(order).toEqual(['pre', 'execute:before', 'dispatch', 'execute:after', 'post'])
  883. })
  884. it('skips dispatch when caller cancellation arrives while pre-execute awaits', async () => {
  885. const ctx = await setup()
  886. let dispatched = 0
  887. ctx.tools.register({
  888. ...echoTool,
  889. name: 'must-not-run',
  890. async execute() { dispatched += 1; return [] },
  891. })
  892. const entered = Promise.withResolvers<undefined>()
  893. const release = Promise.withResolvers<undefined>()
  894. ctx.on('tools/pre-execute', async (_exec, next) => {
  895. entered.resolve(undefined)
  896. await release.promise
  897. return await next()
  898. })
  899. const controller = new AbortController()
  900. const pending = ctx.tools.execute({
  901. callId: CallId('cancelled-in-pre'), name: 'must-not-run', arguments: {}, signal: controller.signal,
  902. })
  903. await entered.promise
  904. controller.abort('cancelled in policy')
  905. release.resolve(undefined)
  906. await expect(pending).resolves.toMatchObject({
  907. content: [{ type: 'text', text: 'Error: tool call aborted before dispatch' }],
  908. isError: true,
  909. error: { info: { name: 'AbortError', code: TOOL_ABORTED_BEFORE_DISPATCH } },
  910. })
  911. expect(dispatched).toBe(0)
  912. })
  913. it('preserves a pre-execute denial that settles after cancellation', async () => {
  914. const ctx = await setup()
  915. let dispatched = 0
  916. ctx.tools.register({
  917. ...echoTool,
  918. name: 'denied-after-cancel',
  919. async execute() { dispatched += 1; return [] },
  920. })
  921. const entered = Promise.withResolvers<undefined>()
  922. const release = Promise.withResolvers<undefined>()
  923. ctx.on('tools/pre-execute', async () => {
  924. entered.resolve(undefined)
  925. await release.promise
  926. return { kind: 'deny', reason: 'policy denied the call' }
  927. })
  928. const controller = new AbortController()
  929. const pending = ctx.tools.execute({
  930. callId: CallId('denied-after-cancel'), name: 'denied-after-cancel', arguments: {}, signal: controller.signal,
  931. })
  932. await entered.promise
  933. controller.abort('cancelled while policy decided')
  934. release.resolve(undefined)
  935. await expect(pending).resolves.toEqual({
  936. content: [{ type: 'text', text: 'Error: policy denied the call' }],
  937. isError: true,
  938. error: { message: 'policy denied the call' },
  939. })
  940. expect(dispatched).toBe(0)
  941. })
  942. it('preserves an async pre-execute failure that settles after cancellation', async () => {
  943. const ctx = await setup()
  944. let dispatched = 0
  945. ctx.tools.register({
  946. ...echoTool,
  947. name: 'must-not-run',
  948. async execute() { dispatched += 1; return [] },
  949. })
  950. const entered = Promise.withResolvers<undefined>()
  951. const release = Promise.withResolvers<undefined>()
  952. ctx.on('tools/pre-execute', async () => {
  953. entered.resolve(undefined)
  954. await release.promise
  955. throw new Error('gate interrupted')
  956. })
  957. const controller = new AbortController()
  958. const pending = ctx.tools.execute({
  959. callId: CallId('cancelled-pre-error'), name: 'must-not-run', arguments: {}, signal: controller.signal,
  960. })
  961. await entered.promise
  962. controller.abort('cancelled in policy')
  963. release.resolve(undefined)
  964. await expect(pending).resolves.toEqual({
  965. content: [{ type: 'text', text: 'Error: gate interrupted' }],
  966. isError: true,
  967. error: { message: 'gate interrupted' },
  968. })
  969. expect(dispatched).toBe(0)
  970. })
  971. it('rechecks caller cancellation after an async around-dispatch wrapper delegates', async () => {
  972. const ctx = await setup()
  973. let dispatched = 0
  974. ctx.tools.register({
  975. ...echoTool,
  976. name: 'must-not-run',
  977. async execute() { dispatched += 1; return [] },
  978. })
  979. const entered = Promise.withResolvers<undefined>()
  980. const release = Promise.withResolvers<undefined>()
  981. const replacement = new AbortController()
  982. ctx.on('tools/execute', async (exec, next) => {
  983. const upstream = exec.signal
  984. exec.signal = replacement.signal
  985. try {
  986. entered.resolve(undefined)
  987. await release.promise
  988. return await next()
  989. } finally {
  990. exec.signal = upstream
  991. }
  992. })
  993. const controller = new AbortController()
  994. const pending = ctx.tools.execute({
  995. callId: CallId('cancelled-in-around'), name: 'must-not-run', arguments: {}, signal: controller.signal,
  996. })
  997. await entered.promise
  998. controller.abort('cancelled in wrapper')
  999. release.resolve(undefined)
  1000. await expect(pending).resolves.toMatchObject({
  1001. isError: true,
  1002. error: { info: { name: 'AbortError', code: TOOL_ABORTED_BEFORE_DISPATCH } },
  1003. })
  1004. expect(dispatched).toBe(0)
  1005. })
  1006. it('skips dispatch when an around wrapper supplies an already-aborted signal', async () => {
  1007. const ctx = await setup()
  1008. let dispatched = 0
  1009. ctx.tools.register({
  1010. ...echoTool,
  1011. name: 'must-not-run',
  1012. async execute() { dispatched += 1; return [] },
  1013. })
  1014. const replacement = AbortSignal.abort('wrapper cancelled')
  1015. ctx.on('tools/execute', async (exec, next) => {
  1016. const upstream = exec.signal
  1017. exec.signal = replacement
  1018. try {
  1019. return await next()
  1020. } finally {
  1021. exec.signal = upstream
  1022. }
  1023. })
  1024. const controller = new AbortController()
  1025. const result = await ctx.tools.execute({
  1026. callId: CallId('cancelled-wrapper'), name: 'must-not-run', arguments: {}, signal: controller.signal,
  1027. })
  1028. expect(result.error).toEqual({
  1029. message: 'tool call aborted before dispatch',
  1030. info: { name: 'AbortError', code: TOOL_ABORTED_BEFORE_DISPATCH },
  1031. })
  1032. expect(dispatched).toBe(0)
  1033. })
  1034. it('uses ABORTED_BEFORE_DISPATCH when cancellation overtakes a wrapper short-circuit', async () => {
  1035. const ctx = await setup()
  1036. let dispatched = 0
  1037. ctx.tools.register({
  1038. ...echoTool,
  1039. name: 'short-circuited',
  1040. async execute() { dispatched += 1; return [] },
  1041. })
  1042. const entered = Promise.withResolvers<undefined>()
  1043. const release = Promise.withResolvers<undefined>()
  1044. ctx.on('tools/execute', async () => {
  1045. entered.resolve(undefined)
  1046. await release.promise
  1047. return {
  1048. value: 'wrapper success',
  1049. content: [{ type: 'text', text: 'wrapper success' }],
  1050. isError: false,
  1051. additionalContexts: [{
  1052. content: [{ type: 'text', text: 'wrapper context' }],
  1053. source: { kind: 'plugin', plugin: 'wrapper' },
  1054. }],
  1055. }
  1056. })
  1057. const controller = new AbortController()
  1058. const pending = ctx.tools.execute({
  1059. callId: CallId('cancelled-short-circuit'),
  1060. name: 'short-circuited',
  1061. arguments: {},
  1062. signal: controller.signal,
  1063. })
  1064. await entered.promise
  1065. controller.abort('cancelled while wrapper waited')
  1066. release.resolve(undefined)
  1067. await expect(pending).resolves.toMatchObject({
  1068. content: [{ type: 'text', text: 'Error: tool call aborted before dispatch' }],
  1069. isError: true,
  1070. error: { info: { name: 'AbortError', code: TOOL_ABORTED_BEFORE_DISPATCH } },
  1071. additionalContexts: [{ source: { kind: 'plugin', plugin: 'wrapper' } }],
  1072. })
  1073. expect(dispatched).toBe(0)
  1074. })
  1075. it('replaces a late wrapper success with ABORTED and preserves deferred contexts', async () => {
  1076. const ctx = await setup()
  1077. ctx.tools.register({
  1078. ...echoTool,
  1079. name: 'completed-before-wrapper',
  1080. async execute(_args, exec) {
  1081. exec.deferContext({
  1082. content: [{ type: 'text', text: 'completed child work' }],
  1083. source: { kind: 'plugin', plugin: 'child' },
  1084. })
  1085. return 'body complete'
  1086. },
  1087. })
  1088. const entered = Promise.withResolvers<undefined>()
  1089. const release = Promise.withResolvers<undefined>()
  1090. ctx.on('tools/execute', async (_exec, next) => {
  1091. const result = await next()
  1092. entered.resolve(undefined)
  1093. await release.promise
  1094. return result
  1095. })
  1096. const controller = new AbortController()
  1097. const pending = ctx.tools.execute({
  1098. callId: CallId('cancelled-after-body'), name: 'completed-before-wrapper', arguments: {}, signal: controller.signal,
  1099. })
  1100. await entered.promise
  1101. controller.abort('cancelled while wrapper settled')
  1102. release.resolve(undefined)
  1103. await expect(pending).resolves.toMatchObject({
  1104. content: [{ type: 'text', text: 'Error: tool call aborted' }],
  1105. isError: true,
  1106. error: { info: { name: 'AbortError', code: TOOL_ABORTED } },
  1107. additionalContexts: [{ source: { kind: 'plugin', plugin: 'child' } }],
  1108. })
  1109. })
  1110. it('replaces a late post-execute success with ABORTED and preserves contexts', async () => {
  1111. const ctx = await setup()
  1112. ctx.tools.register({
  1113. ...echoTool,
  1114. name: 'completed-before-post',
  1115. async execute(_args, exec) {
  1116. exec.deferContext({
  1117. content: [{ type: 'text', text: 'completed child work' }],
  1118. source: { kind: 'plugin', plugin: 'child' },
  1119. })
  1120. return 'body complete'
  1121. },
  1122. })
  1123. const entered = Promise.withResolvers<undefined>()
  1124. const release = Promise.withResolvers<undefined>()
  1125. ctx.on('tools/post-execute', async (_exec, _result, next) => {
  1126. const decision = await next()
  1127. entered.resolve(undefined)
  1128. await release.promise
  1129. return {
  1130. ...decision,
  1131. additionalContexts: [{
  1132. content: [{ type: 'text', text: 'post context' }],
  1133. source: { kind: 'plugin', plugin: 'post' },
  1134. }],
  1135. }
  1136. })
  1137. const controller = new AbortController()
  1138. const pending = ctx.tools.execute({
  1139. callId: CallId('cancelled-in-post'), name: 'completed-before-post', arguments: {}, signal: controller.signal,
  1140. })
  1141. await entered.promise
  1142. controller.abort('cancelled while post policy waits')
  1143. release.resolve(undefined)
  1144. await expect(pending).resolves.toMatchObject({
  1145. content: [{ type: 'text', text: 'Error: tool call aborted' }],
  1146. isError: true,
  1147. error: { info: { name: 'AbortError', code: TOOL_ABORTED } },
  1148. additionalContexts: [
  1149. { source: { kind: 'plugin', plugin: 'child' } },
  1150. { source: { kind: 'plugin', plugin: 'post' } },
  1151. ],
  1152. })
  1153. })
  1154. it('preserves an around-dispatch failure that settles after cancellation', async () => {
  1155. const ctx = await setup()
  1156. let dispatched = 0
  1157. ctx.tools.register({
  1158. ...echoTool,
  1159. name: 'wrapper-failure',
  1160. async execute() { dispatched += 1; return [] },
  1161. })
  1162. const entered = Promise.withResolvers<undefined>()
  1163. const release = Promise.withResolvers<undefined>()
  1164. ctx.on('tools/execute', async () => {
  1165. entered.resolve(undefined)
  1166. await release.promise
  1167. throw new HarnessError('wrapper failed', 'WRAPPER_FAILURE')
  1168. })
  1169. const controller = new AbortController()
  1170. const pending = ctx.tools.execute({
  1171. callId: CallId('wrapper-failure'), name: 'wrapper-failure', arguments: {}, signal: controller.signal,
  1172. })
  1173. await entered.promise
  1174. controller.abort('cancelled while wrapper failed')
  1175. release.resolve(undefined)
  1176. await expect(pending).resolves.toMatchObject({
  1177. content: [{ type: 'text', text: 'Error: wrapper failed' }],
  1178. isError: true,
  1179. error: { info: { name: 'HarnessError', code: 'WRAPPER_FAILURE' } },
  1180. })
  1181. expect(dispatched).toBe(0)
  1182. })
  1183. it('preserves a tool-owned failure after the body observes cancellation', async () => {
  1184. const ctx = await setup()
  1185. const entered = Promise.withResolvers<undefined>()
  1186. ctx.tools.register({
  1187. ...echoTool,
  1188. name: 'tool-failure',
  1189. execute(_args, exec) {
  1190. entered.resolve(undefined)
  1191. return new Promise<never>((_resolve, reject) => {
  1192. exec.signal.addEventListener('abort', () => {
  1193. reject(new HarnessError('tool failed', 'TOOL_FAILURE'))
  1194. }, { once: true })
  1195. })
  1196. },
  1197. })
  1198. const controller = new AbortController()
  1199. const pending = ctx.tools.execute({
  1200. callId: CallId('tool-failure'), name: 'tool-failure', arguments: {}, signal: controller.signal,
  1201. })
  1202. await entered.promise
  1203. controller.abort('cancelled running body')
  1204. await expect(pending).resolves.toMatchObject({
  1205. content: [{ type: 'text', text: 'Error: tool failed' }],
  1206. isError: true,
  1207. error: { info: { name: 'HarnessError', code: 'TOOL_FAILURE' } },
  1208. })
  1209. })
  1210. it('preserves a post-policy failure that settles after cancellation', async () => {
  1211. const ctx = await setup()
  1212. ctx.tools.register(echoTool)
  1213. const entered = Promise.withResolvers<undefined>()
  1214. const release = Promise.withResolvers<undefined>()
  1215. ctx.on('tools/post-execute', async () => {
  1216. entered.resolve(undefined)
  1217. await release.promise
  1218. throw new HarnessError('post-policy failed', 'POST_FAILURE')
  1219. })
  1220. const controller = new AbortController()
  1221. const pending = ctx.tools.execute({
  1222. callId: CallId('post-failure'), name: 'echo', arguments: {}, signal: controller.signal,
  1223. })
  1224. await entered.promise
  1225. controller.abort('cancelled while post-policy failed')
  1226. release.resolve(undefined)
  1227. await expect(pending).resolves.toMatchObject({
  1228. content: [{ type: 'text', text: 'Error: post-policy failed' }],
  1229. isError: true,
  1230. error: { info: { name: 'HarnessError', code: 'POST_FAILURE' } },
  1231. })
  1232. })
  1233. it('fuses caller cancellation back into a wrapper replacement for the running body', async () => {
  1234. const ctx = await setup()
  1235. const entered = Promise.withResolvers<undefined>()
  1236. const replacement = new AbortController()
  1237. let bodySignal: AbortSignal | undefined
  1238. ctx.tools.register({
  1239. ...echoTool,
  1240. name: 'cooperative',
  1241. execute(_args, exec) {
  1242. bodySignal = exec.signal
  1243. entered.resolve(undefined)
  1244. if (exec.signal.aborted) return Promise.resolve('stopped')
  1245. return new Promise<string>((resolve) => {
  1246. exec.signal.addEventListener('abort', () => { resolve('stopped') }, { once: true })
  1247. })
  1248. },
  1249. })
  1250. ctx.on('tools/execute', async (exec, next) => {
  1251. const upstream = exec.signal
  1252. exec.signal = replacement.signal
  1253. try {
  1254. return await next()
  1255. } finally {
  1256. exec.signal = upstream
  1257. }
  1258. })
  1259. const controller = new AbortController()
  1260. const pending = ctx.tools.execute({
  1261. callId: CallId('cancelled-body'), name: 'cooperative', arguments: {}, signal: controller.signal,
  1262. })
  1263. await entered.promise
  1264. expect(bodySignal).not.toBe(controller.signal)
  1265. expect(bodySignal).not.toBe(replacement.signal)
  1266. controller.abort('cancel running body')
  1267. await expect(pending).resolves.toMatchObject({
  1268. isError: true,
  1269. error: { info: { name: 'AbortError', code: TOOL_ABORTED } },
  1270. })
  1271. expect(bodySignal?.aborted).toBe(true)
  1272. expect(replacement.signal.aborted).toBe(false)
  1273. })
  1274. it('restores the required caller signal after around dispatch', async () => {
  1275. const ctx = await setup()
  1276. let postSignal: AbortSignal | undefined
  1277. ctx.on('tools/execute', async (exec, next) => {
  1278. const upstream = exec.signal
  1279. exec.signal = new AbortController().signal
  1280. try {
  1281. return await next()
  1282. } finally {
  1283. exec.signal = upstream
  1284. }
  1285. })
  1286. ctx.on('tools/post-execute', async (exec, _result, next) => {
  1287. postSignal = exec.signal
  1288. return next()
  1289. })
  1290. const controller = new AbortController()
  1291. await ctx.tools.execute({
  1292. callId: CallId('restored-signal'), name: 'echo', arguments: {}, signal: controller.signal,
  1293. })
  1294. expect(postSignal).toBe(controller.signal)
  1295. })
  1296. it('waits for an uncooperative started body before returning ABORTED', async () => {
  1297. const ctx = await setup()
  1298. const entered = Promise.withResolvers<undefined>()
  1299. const release = Promise.withResolvers<string>()
  1300. ctx.tools.register({
  1301. ...echoTool,
  1302. name: 'uncooperative',
  1303. execute(_args, exec) {
  1304. exec.deferContext({
  1305. content: [{ type: 'text', text: 'nested outcome' }],
  1306. source: { kind: 'plugin', plugin: 'nested' },
  1307. })
  1308. entered.resolve(undefined)
  1309. return release.promise
  1310. },
  1311. })
  1312. const controller = new AbortController()
  1313. const pending = ctx.tools.execute({
  1314. callId: CallId('drain-body'), name: 'uncooperative', arguments: {}, signal: controller.signal,
  1315. })
  1316. await entered.promise
  1317. controller.abort('must still drain')
  1318. const state = await Promise.race([
  1319. pending.then(() => 'settled' as const),
  1320. Promise.resolve('pending' as const),
  1321. ])
  1322. expect(state).toBe('pending')
  1323. release.resolve('settled')
  1324. await expect(pending).resolves.toMatchObject({
  1325. isError: true,
  1326. error: { info: { name: 'AbortError', code: TOOL_ABORTED } },
  1327. additionalContexts: [{ source: { kind: 'plugin', plugin: 'nested' } }],
  1328. })
  1329. })
  1330. it('materializes a pre-aborted call and publishes one result without entering pipeline phases', async () => {
  1331. const ctx = await setup()
  1332. const phases = { pre: 0, around: 0, body: 0, post: 0, result: 0 }
  1333. const callerArguments = { nested: { value: 1 } }
  1334. const callerSignal = AbortSignal.abort('already cancelled')
  1335. let argumentReads = 0
  1336. let observedArguments: unknown
  1337. let observedExecution: object | undefined
  1338. let observedToken: symbol | undefined
  1339. let observedSignal: AbortSignal | undefined
  1340. let observedResult: ToolExecutionResult | undefined
  1341. ctx.tools.register({
  1342. ...echoTool,
  1343. name: 'domain-abort',
  1344. async execute() { phases.body += 1; return [] },
  1345. })
  1346. ctx.on('tools/pre-execute', async (_exec, next) => { phases.pre += 1; return next() })
  1347. ctx.on('tools/execute', async (_exec, next) => { phases.around += 1; return next() })
  1348. ctx.on('tools/post-execute', async (_exec, _result, next) => { phases.post += 1; return next() })
  1349. ctx.on('tools/result', (exec, result) => {
  1350. phases.result += 1
  1351. observedExecution = exec
  1352. observedArguments = exec.arguments
  1353. observedToken = exec.token
  1354. observedSignal = exec.signal
  1355. observedResult = result
  1356. })
  1357. const result = await ctx.tools.execute({
  1358. callId: CallId('pre-aborted'),
  1359. name: 'domain-abort',
  1360. get arguments() { argumentReads += 1; return callerArguments },
  1361. signal: callerSignal,
  1362. })
  1363. expect(argumentReads).toBe(1)
  1364. expect(phases).toEqual({ pre: 0, around: 0, body: 0, post: 0, result: 1 })
  1365. expect(result).toEqual({
  1366. content: [{ type: 'text', text: 'Error: tool call aborted before dispatch' }],
  1367. isError: true,
  1368. error: {
  1369. message: 'tool call aborted before dispatch',
  1370. info: { name: 'AbortError', code: TOOL_ABORTED_BEFORE_DISPATCH },
  1371. },
  1372. })
  1373. expect(observedResult).toBe(result)
  1374. expect(Object.isFrozen(observedExecution)).toBe(true)
  1375. expect(typeof observedToken).toBe('symbol')
  1376. expect(observedSignal).toBe(callerSignal)
  1377. expect(Object.isFrozen(result)).toBe(true)
  1378. expect(observedArguments).not.toBe(callerArguments)
  1379. expect(Object.isFrozen(observedArguments)).toBe(true)
  1380. expect(Object.isFrozen((observedArguments as { nested: object }).nested)).toBe(true)
  1381. })
  1382. it('lets argument materialization failure win over a pre-aborted signal', async () => {
  1383. const ctx = await setup()
  1384. let observed = 0
  1385. ctx.on('tools/result', () => { observed += 1 })
  1386. const result = await ctx.tools.execute({
  1387. callId: CallId('invalid-pre-aborted'),
  1388. name: 'missing',
  1389. arguments: { invalid: () => undefined },
  1390. signal: AbortSignal.abort('already cancelled'),
  1391. })
  1392. expect(result).toEqual({
  1393. content: [{ type: 'text', text: 'Error: tool execution arguments must be losslessly JSON-serializable' }],
  1394. isError: true,
  1395. error: { message: 'tool execution arguments must be losslessly JSON-serializable' },
  1396. })
  1397. expect(observed).toBe(1)
  1398. })
  1399. it('a pre-execute deny short-circuits before tools/execute (the seam never runs)', async () => {
  1400. const ctx = await setup()
  1401. ctx.tools.register(echoTool)
  1402. let entered = false
  1403. ctx.on('tools/pre-execute', async (_exec, _next): Promise<PreToolDecision> => ({ kind: 'deny', reason: 'nope' }))
  1404. ctx.on('tools/execute', async (_exec: ToolDispatchExecution, next: () => Promise<ToolExecutionResult>): Promise<ToolExecutionResult> => {
  1405. entered = true
  1406. return next()
  1407. })
  1408. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'echo', arguments: { text: 'hi' } })
  1409. expect(result.isError).toBe(true)
  1410. expect(result.content[0]).toMatchObject({ text: 'Error: nope' })
  1411. expect(entered).toBe(false) // a denied call never enters the around-dispatch seam
  1412. })
  1413. it('a thrown tool is normalized to an isError result BEFORE a tools/execute listener sees next()', async () => {
  1414. const ctx = await setup()
  1415. ctx.tools.register({
  1416. ...echoTool,
  1417. name: 'boom',
  1418. async execute() { throw new HarnessError('kaboom', 'BOOM') },
  1419. })
  1420. let seen: { isError: boolean; error?: unknown } | undefined
  1421. ctx.on('tools/execute', async (_exec: ToolDispatchExecution, next: () => Promise<ToolExecutionResult>): Promise<ToolExecutionResult> => {
  1422. const result = await next()
  1423. // The base next() IS dispatch-with-normalization: the wrapper sees the
  1424. // normalized isError result, never a raw throw from the tool body.
  1425. seen = { isError: result.isError, error: result.error }
  1426. return result
  1427. })
  1428. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'boom', arguments: {} })
  1429. expect(seen).toEqual({
  1430. isError: true,
  1431. error: { message: 'kaboom', info: { name: 'HarnessError', code: 'BOOM' } },
  1432. })
  1433. expect(result.isError).toBe(true)
  1434. expect(result.content[0]).toMatchObject({ text: 'Error: kaboom' })
  1435. })
  1436. it('freezes core dispatch outcomes before around and post listeners can observe them', async () => {
  1437. const ctx = await setup()
  1438. ctx.tools.register(echoTool)
  1439. const mutationAttempts: boolean[] = []
  1440. ctx.on('tools/execute', async (_exec, next) => {
  1441. const result = await next()
  1442. mutationAttempts.push(Reflect.set(result, 'value', 'around mutation'))
  1443. return result
  1444. })
  1445. ctx.on('tools/post-execute', async (_exec, result, next) => {
  1446. mutationAttempts.push(Reflect.set(result, 'value', 'post mutation'))
  1447. return next()
  1448. })
  1449. const result = await ctx.tools.execute({
  1450. signal: testToolSignal,
  1451. callId: CallId('frozen-canonical'), name: 'echo', arguments: { text: 'original' },
  1452. })
  1453. expect(mutationAttempts).toEqual([false, false])
  1454. expect(result.isError ? undefined : result.value).toBe('original')
  1455. })
  1456. it('a thrown tool normalized inside tools/execute still reaches post-execute', async () => {
  1457. const ctx = await setup()
  1458. ctx.tools.register({
  1459. ...echoTool,
  1460. name: 'boom',
  1461. async execute() { throw new Error('exploded') },
  1462. })
  1463. let postSaw: boolean | undefined
  1464. ctx.on('tools/execute', async (_exec: ToolDispatchExecution, next: () => Promise<ToolExecutionResult>): Promise<ToolExecutionResult> => next())
  1465. ctx.on('tools/post-execute', async (_exec, result, next) => {
  1466. postSaw = result.isError
  1467. return next()
  1468. })
  1469. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'boom', arguments: {} })
  1470. expect(postSaw).toBe(true) // the normalized isError still flows through post-execute
  1471. expect(result.isError).toBe(true)
  1472. expect(result.content[0]).toMatchObject({ text: 'Error: exploded' })
  1473. })
  1474. it('re-fuses the caller signal with an around-dispatch replacement for the body', async () => {
  1475. const ctx = await setup()
  1476. let seenSignal: AbortSignal | undefined
  1477. ctx.tools.register({
  1478. ...echoTool,
  1479. name: 'signal-probe',
  1480. async execute(_args, exec) {
  1481. seenSignal = exec.signal
  1482. return 'ok'
  1483. },
  1484. })
  1485. const upstream = new AbortController().signal
  1486. const replacement = new AbortController().signal
  1487. ctx.on('tools/execute', async (exec: ToolDispatchExecution, next: () => Promise<ToolExecutionResult>): Promise<ToolExecutionResult> => {
  1488. expect(exec.signal).toBe(upstream)
  1489. // Cordis next() ignores passed arguments, so a wrapper mutates exec in
  1490. // place (the documented "mutate the shared object, then delegate" idiom).
  1491. exec.signal = replacement
  1492. return next()
  1493. })
  1494. await ctx.tools.execute({ callId: CallId('c1'), name: 'signal-probe', arguments: {}, signal: upstream })
  1495. expect(seenSignal).toBeDefined()
  1496. expect(seenSignal).not.toBe(upstream)
  1497. expect(seenSignal).not.toBe(replacement)
  1498. })
  1499. it('a tools/execute listener can short-circuit dispatch by returning a result without next()', async () => {
  1500. const ctx = await setup()
  1501. let dispatched = false
  1502. ctx.tools.register({
  1503. ...echoTool,
  1504. name: 'never-runs',
  1505. async execute() { dispatched = true; return 'unreachable' },
  1506. })
  1507. ctx.on('tools/execute', async (_exec: ToolDispatchExecution, _next: () => Promise<ToolExecutionResult>): Promise<ToolExecutionResult> =>
  1508. ({ content: [{ type: 'text', text: 'ignored authored content' }], isError: false, value: 'short-circuited' }))
  1509. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'never-runs', arguments: {} })
  1510. expect(dispatched).toBe(false) // returning without next() skips core dispatch
  1511. expect(result.content[0]).toMatchObject({ text: 'short-circuited' })
  1512. })
  1513. it('revalidates a cached canonical result returned from a different dispatch', async () => {
  1514. const ctx = await setup()
  1515. ctx.tools.register({ ...echoTool, name: 'string-output', async execute() { return 'cached' } })
  1516. let objectBodyRan = false
  1517. ctx.tools.register(defineTool({
  1518. name: 'object-output',
  1519. description: 'Return one closed object.',
  1520. parameters: {},
  1521. output: {
  1522. schema: {
  1523. type: 'object',
  1524. properties: { ok: { type: 'boolean', required: true } },
  1525. additionalProperties: false,
  1526. },
  1527. render: (_args, value) => [{ type: 'text', text: String(value.ok) }],
  1528. },
  1529. execute() {
  1530. objectBodyRan = true
  1531. return Promise.resolve({ ok: true })
  1532. },
  1533. }))
  1534. let cached: ToolExecutionResult | undefined
  1535. ctx.on('tools/execute', async (exec, next) => {
  1536. if (exec.name === 'string-output') {
  1537. cached = await next()
  1538. return cached
  1539. }
  1540. if (exec.name === 'object-output') {
  1541. if (cached === undefined) throw new Error('expected the first dispatch result')
  1542. return cached
  1543. }
  1544. return next()
  1545. })
  1546. const first = await ctx.tools.execute({
  1547. signal: testToolSignal, callId: CallId('cached-first'), name: 'string-output', arguments: {},
  1548. })
  1549. const second = await ctx.tools.execute({
  1550. signal: testToolSignal, callId: CallId('cached-second'), name: 'object-output', arguments: {},
  1551. })
  1552. expect(first.isError ? undefined : first.value).toBe('cached')
  1553. expect(objectBodyRan).toBe(false)
  1554. expect(second).toMatchObject({
  1555. isError: true,
  1556. error: { info: { name: 'ToolOutputError', code: 'INVALID_TOOL_OUTPUT' } },
  1557. })
  1558. })
  1559. it('preserves additionalContexts supplied by an around-dispatch result', async () => {
  1560. const ctx = await setup()
  1561. ctx.tools.register(echoTool)
  1562. ctx.on('tools/execute', async () => ({
  1563. content: [{ type: 'text', text: 'short-circuited with context' }],
  1564. isError: false,
  1565. value: 'short-circuited with context',
  1566. additionalContexts: [{
  1567. content: [{ type: 'text', text: 'from around dispatch' }],
  1568. source: { kind: 'plugin', plugin: 'test' },
  1569. }],
  1570. }))
  1571. const result = await ctx.tools.execute({
  1572. signal: testToolSignal,
  1573. callId: CallId('around-context'), name: 'echo', arguments: {},
  1574. })
  1575. expect(result.additionalContexts).toEqual([{
  1576. content: [{ type: 'text', text: 'from around dispatch' }],
  1577. source: { kind: 'plugin', plugin: 'test' },
  1578. }])
  1579. })
  1580. it('returns an isError result when a tools/execute listener throws', async () => {
  1581. const ctx = await setup()
  1582. ctx.tools.register(echoTool)
  1583. ctx.on('tools/execute', async () => { throw new Error('wrapper broke') })
  1584. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'echo', arguments: { text: 'hi' } })
  1585. expect(result).toEqual({
  1586. content: [{ type: 'text', text: 'Error: wrapper broke' }],
  1587. error: { message: 'wrapper broke' },
  1588. isError: true,
  1589. })
  1590. })
  1591. it('returns an isError result when a tools/pre-execute listener throws', async () => {
  1592. const ctx = await setup()
  1593. ctx.tools.register(echoTool)
  1594. ctx.on('tools/pre-execute', async () => {
  1595. throw new Error('permission hook broke')
  1596. })
  1597. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'echo', arguments: { text: 'hi' } })
  1598. expect(result).toEqual({
  1599. content: [{ type: 'text', text: 'Error: permission hook broke' }],
  1600. error: { message: 'permission hook broke' },
  1601. isError: true,
  1602. })
  1603. })
  1604. it('returns an isError result when a tools/post-execute listener throws', async () => {
  1605. const ctx = await setup()
  1606. ctx.tools.register(echoTool)
  1607. ctx.on('tools/post-execute', async () => {
  1608. throw new Error('post hook broke')
  1609. })
  1610. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'echo', arguments: { text: 'hi' } })
  1611. expect(result).toEqual({
  1612. content: [{ type: 'text', text: 'Error: post hook broke' }],
  1613. error: { message: 'post hook broke' },
  1614. isError: true,
  1615. })
  1616. })
  1617. it('preserves structured error info when a tools/pre-execute listener throws HarnessError', async () => {
  1618. const ctx = await setup()
  1619. ctx.tools.register(echoTool)
  1620. ctx.on('tools/pre-execute', async () => {
  1621. throw new HarnessError('denied', 'DENIED')
  1622. })
  1623. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'echo', arguments: { text: 'hi' } })
  1624. expect(result).toMatchObject({
  1625. isError: true,
  1626. error: { message: 'denied', info: { name: 'HarnessError', code: 'DENIED' } },
  1627. })
  1628. })
  1629. it('schemas() snapshots tool schemas instead of exposing registry objects', async () => {
  1630. const ctx = await setup()
  1631. ctx.tools.register(echoTool)
  1632. const first = ctx.tools.schemas()
  1633. const firstParameters = first[0]!.parameters as { properties: Record<string, unknown> }
  1634. firstParameters.properties['mutated'] = { type: 'string' }
  1635. first[0]!.description = 'mutated'
  1636. expect(ctx.tools.schemas()).toEqual([{
  1637. name: 'echo',
  1638. description: 'echo arguments back',
  1639. parameters: { type: 'object', properties: { text: { type: 'string' } } },
  1640. }])
  1641. })
  1642. it('schemas() snapshots deeply nested parameters without using structured-clone recursion', async () => {
  1643. const ctx = await setup()
  1644. const depth = 5_000
  1645. let nested: JsonSchemaNode = { type: 'string' }
  1646. for (let index = 0; index < depth; index++) nested = { oneOf: [nested, { type: 'null' }] }
  1647. ctx.tools.register({
  1648. ...echoTool,
  1649. name: 'deep-schema',
  1650. parameters: { type: 'object', properties: { nested } },
  1651. })
  1652. const projected = ctx.tools.schemas()[0]!.parameters as JsonSchemaNode
  1653. let cursor = projected.properties!.nested!
  1654. let layers = 0
  1655. while (cursor.oneOf !== undefined) {
  1656. cursor = cursor.oneOf[0]!
  1657. layers++
  1658. }
  1659. expect(layers).toBe(depth)
  1660. expect(cursor).toEqual({ type: 'string' })
  1661. })
  1662. it('rejects schema projection when a raw registration is not lossless JSON', async () => {
  1663. const ctx = await setup()
  1664. ctx.tools.register({
  1665. ...echoTool,
  1666. name: 'lossy-schema',
  1667. parameters: { type: 'object', default: Number.NaN },
  1668. })
  1669. expect(() => ctx.tools.schemas())
  1670. .toThrow('tool "lossy-schema" parameters must be lossless JSON before schema projection')
  1671. })
  1672. it('rejects a non-positive or non-finite registration timeout', async () => {
  1673. const ctx = await setup()
  1674. expect(() => ctx.tools.register({ ...echoTool, name: 'zero-timeout', timeoutMs: 0 }))
  1675. .toThrow('timeoutMs must be a positive finite number')
  1676. expect(() => ctx.tools.register({ ...echoTool, name: 'infinite-timeout', timeoutMs: Number.POSITIVE_INFINITY }))
  1677. .toThrow('timeoutMs must be a positive finite number')
  1678. })
  1679. it('rejects duplicate names and unregisters on fiber dispose (HMR safety)', async () => {
  1680. const ctx = await setup()
  1681. ctx.tools.register(echoTool)
  1682. expect(() => ctx.tools.register(echoTool)).toThrow('already registered')
  1683. const fiber = await ctx.plugin(Object.assign((inner: Context) => {
  1684. inner.tools.register({ ...echoTool, name: 'scoped' })
  1685. }, { inject: ['tools'] }))
  1686. expect(ctx.tools.schemas().map(t => t.name)).toEqual(['echo', 'scoped'])
  1687. await fiber.dispose()
  1688. expect(ctx.tools.schemas().map(t => t.name)).toEqual(['echo'])
  1689. })
  1690. it('returns a callable disposer from register() that unregisters the tool', async () => {
  1691. const ctx = await setup()
  1692. ctx.tools.register(echoTool)
  1693. const dispose = ctx.tools.register({ ...echoTool, name: 'disposable' })
  1694. expect(ctx.tools.schemas().map(t => t.name)).toEqual(['echo', 'disposable'])
  1695. dispose()
  1696. expect(ctx.tools.schemas().map(t => t.name)).toEqual(['echo'])
  1697. })
  1698. it('rolls back the tool entry when a tools/change listener throws (P1-1)', async () => {
  1699. const ctx = await setup()
  1700. let threw = false
  1701. ctx.on('tools/change', () => {
  1702. if (!threw) { threw = true; throw new Error('boom change listener') }
  1703. })
  1704. // The throwing emit must roll the entry back, not leak it.
  1705. expect(() => ctx.tools.register(echoTool)).toThrow('boom change listener')
  1706. expect(ctx.tools.get('echo')).toBeUndefined() // rolled back, not leaked
  1707. expect(ctx.tools.schemas()).toHaveLength(0)
  1708. // A subsequent listener-free register of the SAME name succeeds and is
  1709. // exposed exactly once (the duplicate-name check is not wedged).
  1710. const dispose = ctx.tools.register(echoTool)
  1711. expect(ctx.tools.schemas().map(t => t.name)).toEqual(['echo'])
  1712. dispose()
  1713. expect(ctx.tools.get('echo')).toBeUndefined()
  1714. })
  1715. it('register() returns the EXACT effect disposer: a composite yield nests the teardown in order', async () => {
  1716. // Registry methods return the exact Cordis effect disposer so a composite yield places
  1717. // unregistration at its LIFO position. A wrapper would create a concurrent sibling; this async
  1718. // probe yields during earlier teardown and would then observe the tool already removed.
  1719. const ctx = await setup()
  1720. const order: string[] = []
  1721. const fiber = await ctx.plugin(Object.assign((inner: Context) => {
  1722. inner.effect(function* () {
  1723. yield () => { order.push('disposed-last') }
  1724. yield inner.tools.register({ ...echoTool, name: 'nested' })
  1725. order.push('registered')
  1726. yield async () => {
  1727. await new Promise(resolve => setTimeout(resolve, 0))
  1728. order.push(inner.tools.get('nested') ? 'first: still registered' : 'first: already gone')
  1729. }
  1730. })
  1731. }, { inject: ['tools'] }))
  1732. await fiber.dispose()
  1733. expect(order).toEqual(['registered', 'first: still registered', 'disposed-last'])
  1734. expect(ctx.tools.get('nested')).toBeUndefined()
  1735. })
  1736. })
  1737. describe('defineTool / schema DSL', () => {
  1738. it('converts ParameterSchemaSpec to standard JSON Schema with required array', () => {
  1739. const spec = {
  1740. path: { type: 'string', required: true, description: 'Absolute path' },
  1741. offset: { type: 'number' },
  1742. limit: { type: 'number', description: 'Max lines' },
  1743. } satisfies ParameterSchemaSpec
  1744. const jsonSchema = parameterSchemaSpecToJsonSchema(spec)
  1745. expect(jsonSchema).toEqual({
  1746. type: 'object',
  1747. properties: {
  1748. path: { type: 'string', description: 'Absolute path' },
  1749. offset: { type: 'number' },
  1750. limit: { type: 'number', description: 'Max lines' },
  1751. },
  1752. required: ['path'],
  1753. })
  1754. })
  1755. it('handles empty spec (no properties, no required)', () => {
  1756. expect(parameterSchemaSpecToJsonSchema({})).toEqual({
  1757. type: 'object',
  1758. properties: {},
  1759. })
  1760. })
  1761. it('handles nested object spec', () => {
  1762. const spec = {
  1763. config: {
  1764. type: 'object',
  1765. additionalProperties: true,
  1766. required: true,
  1767. properties: {
  1768. host: { type: 'string', required: true },
  1769. port: { type: 'number' },
  1770. },
  1771. },
  1772. } satisfies ParameterSchemaSpec
  1773. const jsonSchema = parameterSchemaSpecToJsonSchema(spec)
  1774. expect(jsonSchema).toEqual({
  1775. type: 'object',
  1776. properties: {
  1777. config: {
  1778. type: 'object',
  1779. additionalProperties: true,
  1780. properties: {
  1781. host: { type: 'string' },
  1782. port: { type: 'number' },
  1783. },
  1784. required: ['host'],
  1785. },
  1786. },
  1787. required: ['config'],
  1788. })
  1789. })
  1790. it('defineTool returns a valid ToolDefinition with typed execute', async () => {
  1791. const ctx = await setup()
  1792. const tool = defineTool({
  1793. name: 'typed-echo',
  1794. description: 'A typed echo tool',
  1795. parameters: {
  1796. text: { type: 'string', required: true },
  1797. uppercase: { type: 'boolean' },
  1798. },
  1799. output: {
  1800. schema: { type: 'string' },
  1801. render: (_args, value) => [{ type: 'text', text: value }],
  1802. },
  1803. async execute(args) {
  1804. // args is typed: { text: string; uppercase?: boolean }
  1805. const result = args.uppercase ? args.text.toUpperCase() : args.text
  1806. return result
  1807. },
  1808. })
  1809. ctx.tools.register(tool)
  1810. expect(ctx.tools.schemas()).toEqual([{
  1811. name: 'typed-echo',
  1812. description: 'A typed echo tool',
  1813. parameters: {
  1814. type: 'object',
  1815. properties: {
  1816. text: { type: 'string' },
  1817. uppercase: { type: 'boolean' },
  1818. },
  1819. required: ['text'],
  1820. },
  1821. }])
  1822. const result = await ctx.tools.execute({
  1823. signal: testToolSignal,
  1824. callId: CallId('c1'),
  1825. name: 'typed-echo',
  1826. arguments: { text: 'hello', uppercase: true },
  1827. })
  1828. expect(result.isError).toBe(false)
  1829. expect(result.isError ? undefined : result.value).toBe('HELLO')
  1830. expect(result.content).toEqual([{ type: 'text', text: 'HELLO' }])
  1831. })
  1832. it('type-level: InferArgs maps required properties to non-optional', () => {
  1833. // Compile-time check: if this compiles, InferArgs is correct.
  1834. // args.a is string (required), args.b is number|undefined (optional).
  1835. const tool = defineTool({
  1836. name: 'type-check',
  1837. description: '',
  1838. parameters: { a: { type: 'string' as const, required: true as const }, b: { type: 'number' as const } },
  1839. output: { schema: { type: 'string' }, render: () => [] },
  1840. async execute(args) {
  1841. expect(typeof args.a).toBe('string')
  1842. void args
  1843. return args.a
  1844. },
  1845. })
  1846. void tool
  1847. })
  1848. it('registry round-trips a defineTool definition (register→schemas→execute)', async () => {
  1849. const ctx = await setup()
  1850. ctx.tools.register(defineTool({
  1851. name: 'roundtrip',
  1852. description: 'Round-trip test',
  1853. parameters: {
  1854. req: { type: 'string', required: true },
  1855. opt: { type: 'number', description: 'Optional number' },
  1856. },
  1857. output: {
  1858. schema: { type: 'string' },
  1859. render: (_args, value) => [{ type: 'text', text: value }],
  1860. },
  1861. async execute(args) {
  1862. return `${args.req}:${args.opt ?? 'none'}`
  1863. },
  1864. }))
  1865. // Schema round-trip: schemas() returns standard JSON Schema
  1866. const schemas = ctx.tools.schemas()
  1867. expect(schemas).toHaveLength(1)
  1868. expect(schemas[0]!.parameters).toEqual({
  1869. type: 'object',
  1870. properties: {
  1871. req: { type: 'string' },
  1872. opt: { type: 'number', description: 'Optional number' },
  1873. },
  1874. required: ['req'],
  1875. })
  1876. // Execution round-trip
  1877. const result = await ctx.tools.execute({
  1878. signal: testToolSignal,
  1879. callId: CallId('c1'),
  1880. name: 'roundtrip',
  1881. arguments: { req: 'hello' },
  1882. })
  1883. expect(result.isError).toBe(false)
  1884. expect(result.content).toEqual([{ type: 'text', text: 'hello:none' }])
  1885. })
  1886. it('still accepts raw JSON-Schema ToolDefinition directly (MCP interop)', async () => {
  1887. const ctx = await setup()
  1888. ctx.tools.register({
  1889. name: 'raw-tool',
  1890. description: 'Raw JSON Schema tool (like an MCP adapter would register)',
  1891. parameters: {
  1892. type: 'object',
  1893. properties: { path: { type: 'string' } },
  1894. required: ['path'],
  1895. },
  1896. output: {
  1897. schema: { type: 'string' },
  1898. render: (_args, value) => [{ type: 'text', text: value as string }],
  1899. },
  1900. async execute(args: unknown) {
  1901. const p = args as { path: string }
  1902. return p.path
  1903. },
  1904. })
  1905. const schemas = ctx.tools.schemas()
  1906. expect(schemas[0]!.parameters).toEqual({
  1907. type: 'object',
  1908. properties: { path: { type: 'string' } },
  1909. required: ['path'],
  1910. })
  1911. const result = await ctx.tools.execute({
  1912. signal: testToolSignal,
  1913. callId: CallId('c1'),
  1914. name: 'raw-tool',
  1915. arguments: { path: '/tmp' },
  1916. })
  1917. expect(result.isError).toBe(false)
  1918. expect(result.content).toEqual([{ type: 'text', text: '/tmp' }])
  1919. })
  1920. })
  1921. describe('schema DSL edge cases', () => {
  1922. it('emits enum values in JSON Schema property', () => {
  1923. const spec = {
  1924. color: { type: 'string', enum: ['red', 'green', 'blue'], description: 'Color choice' },
  1925. } satisfies ParameterSchemaSpec
  1926. const jsonSchema = parameterSchemaSpecToJsonSchema(spec)
  1927. expect(jsonSchema.properties['color']).toMatchObject({
  1928. type: 'string',
  1929. enum: ['red', 'green', 'blue'],
  1930. description: 'Color choice',
  1931. })
  1932. })
  1933. it('emits default value in JSON Schema property', () => {
  1934. const spec = {
  1935. limit: { type: 'number', default: 25 },
  1936. } satisfies ParameterSchemaSpec
  1937. const jsonSchema = parameterSchemaSpecToJsonSchema(spec)
  1938. expect(jsonSchema.properties['limit']).toMatchObject({
  1939. type: 'number',
  1940. default: 25,
  1941. })
  1942. })
  1943. it('handles array items without nested properties (plain type array)', () => {
  1944. const spec = {
  1945. tags: { type: 'array', items: { type: 'string' } },
  1946. } satisfies ParameterSchemaSpec
  1947. const jsonSchema = parameterSchemaSpecToJsonSchema(spec)
  1948. expect(jsonSchema.properties['tags']).toEqual({
  1949. type: 'array',
  1950. items: { type: 'string' },
  1951. })
  1952. })
  1953. it('handles enum and default together in one property', () => {
  1954. const spec = {
  1955. level: { type: 'string', enum: ['low', 'high'], default: 'low' },
  1956. } satisfies ParameterSchemaSpec
  1957. const jsonSchema = parameterSchemaSpecToJsonSchema(spec)
  1958. expect(jsonSchema.properties['level']).toMatchObject({
  1959. type: 'string',
  1960. enum: ['low', 'high'],
  1961. default: 'low',
  1962. })
  1963. })
  1964. it('omits description, enum, default keys when not specified', () => {
  1965. const spec = {
  1966. bare: { type: 'string' },
  1967. } satisfies ParameterSchemaSpec
  1968. const jsonSchema = parameterSchemaSpecToJsonSchema(spec)
  1969. const prop = jsonSchema.properties['bare'] as Record<string, unknown>
  1970. expect(prop).toEqual({ type: 'string' })
  1971. expect('description' in prop).toBe(false)
  1972. expect('enum' in prop).toBe(false)
  1973. expect('default' in prop).toBe(false)
  1974. })
  1975. it('handles array with no items (items omitted)', () => {
  1976. const spec = {
  1977. raw: { type: 'array' },
  1978. } satisfies ParameterSchemaSpec
  1979. const jsonSchema = parameterSchemaSpecToJsonSchema(spec)
  1980. expect(jsonSchema.properties['raw']).toEqual({
  1981. type: 'array',
  1982. })
  1983. })
  1984. it('handles nested object with all-optional properties (no required array)', () => {
  1985. const spec = {
  1986. config: {
  1987. type: 'object',
  1988. additionalProperties: true,
  1989. properties: {
  1990. host: { type: 'string' },
  1991. port: { type: 'number' },
  1992. },
  1993. },
  1994. } satisfies ParameterSchemaSpec
  1995. const jsonSchema = parameterSchemaSpecToJsonSchema(spec)
  1996. expect(jsonSchema.properties['config']).toMatchObject({
  1997. type: 'object',
  1998. properties: {
  1999. host: { type: 'string' },
  2000. port: { type: 'number' },
  2001. },
  2002. })
  2003. const config = jsonSchema.properties['config'] as Record<string, unknown>
  2004. expect('required' in config).toBe(false)
  2005. })
  2006. })
  2007. describe('schema DSL optional and nested contracts', () => {
  2008. it('InferArgs makes non-required keys genuinely optional (omittable)', () => {
  2009. type Args = InferArgs<{
  2010. path: { type: 'string'; required: true }
  2011. limit: { type: 'number' }
  2012. }>
  2013. expectTypeOf<Args>().toEqualTypeOf<{ path: string; limit?: number }>()
  2014. const omitted: Args = { path: '/tmp' }
  2015. expect(omitted.limit).toBeUndefined()
  2016. })
  2017. it('InferArgs recurses into array items, including arrays of objects', () => {
  2018. type Args = InferArgs<{
  2019. names: { type: 'array'; required: true; items: { type: 'string' } }
  2020. servers: {
  2021. type: 'array'
  2022. items: {
  2023. type: 'object'
  2024. additionalProperties: true
  2025. properties: {
  2026. host: { type: 'string'; required: true }
  2027. port: { type: 'number' }
  2028. }
  2029. }
  2030. }
  2031. }>
  2032. expectTypeOf<Args>().toEqualTypeOf<{
  2033. names: string[]
  2034. servers?: ({ host: string; port?: number } & Record<string, JsonValue>)[]
  2035. }>()
  2036. })
  2037. it('runtime JSON Schema matches the array-of-objects inference', () => {
  2038. const spec = {
  2039. servers: {
  2040. type: 'array',
  2041. items: {
  2042. type: 'object',
  2043. additionalProperties: true,
  2044. properties: {
  2045. host: { type: 'string', required: true },
  2046. port: { type: 'number' },
  2047. },
  2048. },
  2049. },
  2050. } satisfies ParameterSchemaSpec
  2051. expect(parameterSchemaSpecToJsonSchema(spec)).toEqual({
  2052. type: 'object',
  2053. properties: {
  2054. servers: {
  2055. type: 'array',
  2056. items: {
  2057. type: 'object',
  2058. additionalProperties: true,
  2059. properties: {
  2060. host: { type: 'string' },
  2061. port: { type: 'number' },
  2062. },
  2063. required: ['host'],
  2064. },
  2065. },
  2066. },
  2067. })
  2068. })
  2069. it('reports messages from non-Error throws (throw { message })', async () => {
  2070. const ctx = await setup()
  2071. ctx.tools.register({
  2072. ...echoTool,
  2073. name: 'object-thrower',
  2074. async execute() {
  2075. // testing non-Error throws on purpose
  2076. throw { message: 'denied by object' }
  2077. },
  2078. })
  2079. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'object-thrower', arguments: {} })
  2080. expect(result.isError).toBe(true)
  2081. expect(result.content[0]).toMatchObject({ text: 'Error: denied by object' })
  2082. })
  2083. it('reports messages from throws of non-objects (throw "string")', async () => {
  2084. const ctx = await setup()
  2085. ctx.tools.register({
  2086. ...echoTool,
  2087. name: 'string-thrower',
  2088. async execute() {
  2089. // testing primitive throws on purpose
  2090. throw 'kaboom'
  2091. },
  2092. })
  2093. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'string-thrower', arguments: {} })
  2094. expect(result.isError).toBe(true)
  2095. expect(result.content[0]).toMatchObject({ text: 'Error: kaboom' })
  2096. })
  2097. it('reports messages from throws of objects without message property', async () => {
  2098. const ctx = await setup()
  2099. ctx.tools.register({
  2100. ...echoTool,
  2101. name: 'object-no-message',
  2102. async execute() {
  2103. // testing object throw without .message
  2104. throw { code: 500 }
  2105. },
  2106. })
  2107. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'object-no-message', arguments: {} })
  2108. expect(result.isError).toBe(true)
  2109. const firstContent = result.content[0]!
  2110. expect(firstContent.type).toBe('text')
  2111. if (firstContent.type === 'text') {
  2112. expect(firstContent.text).toBe('Error: [object Object]')
  2113. }
  2114. })
  2115. })
  2116. describe('ToolRegistry.get', () => {
  2117. it('get() returns the registered tool definition', async () => {
  2118. const ctx = await setup()
  2119. ctx.tools.register(echoTool)
  2120. const tool = ctx.tools.get('echo')
  2121. expect(tool).toBeDefined()
  2122. expect(tool!.name).toBe('echo')
  2123. })
  2124. it('get() returns undefined for unknown tool names', async () => {
  2125. const ctx = await setup()
  2126. expect(ctx.tools.get('nope')).toBeUndefined()
  2127. })
  2128. })
  2129. describe('validateArgs (the runtime-validation Agent Note, part 1)', () => {
  2130. it('returns [] for valid args and is total over malformed input', () => {
  2131. const spec = {
  2132. path: { type: 'string', required: true },
  2133. limit: { type: 'number' },
  2134. } satisfies ParameterSchemaSpec
  2135. expect(validateArgs(spec, { path: '/tmp' })).toEqual([])
  2136. expect(validateArgs(spec, { path: '/tmp', limit: 5 })).toEqual([])
  2137. // never throws regardless of shape
  2138. expect(validateArgs(spec, null)).toHaveLength(1)
  2139. expect(validateArgs(spec, 'nope')).toHaveLength(1)
  2140. expect(validateArgs(spec, [])).toHaveLength(1)
  2141. })
  2142. it('flags a missing required key and a required key present as undefined', () => {
  2143. const spec = { path: { type: 'string', required: true } } satisfies ParameterSchemaSpec
  2144. expect(validateArgs(spec, {})).toEqual(['missing required property "path"'])
  2145. expect(validateArgs(spec, { path: undefined })).toEqual(['missing required property "path"'])
  2146. })
  2147. it('allows extra keys (no additionalProperties:false) and omitted optionals', () => {
  2148. const spec = { path: { type: 'string', required: true } } satisfies ParameterSchemaSpec
  2149. expect(validateArgs(spec, { path: '/tmp', extra: 1 })).toEqual([])
  2150. })
  2151. it('does not apply defaults (validation only)', () => {
  2152. const spec = { limit: { type: 'number', default: 25 } } satisfies ParameterSchemaSpec
  2153. // absent optional is valid, and validation does not synthesize the default
  2154. expect(validateArgs(spec, {})).toEqual([])
  2155. })
  2156. it('type-checks primitives', () => {
  2157. const spec = {
  2158. s: { type: 'string' },
  2159. n: { type: 'number' },
  2160. b: { type: 'boolean' },
  2161. } satisfies ParameterSchemaSpec
  2162. expect(validateArgs(spec, { s: 1 })).toEqual(['"s" must be a string'])
  2163. expect(validateArgs(spec, { n: 'x' })).toEqual(['"n" must be a number'])
  2164. expect(validateArgs(spec, { b: 'x' })).toEqual(['"b" must be a boolean'])
  2165. })
  2166. it('checks enum membership', () => {
  2167. const spec = { color: { type: 'string', enum: ['red', 'green'] } } satisfies ParameterSchemaSpec
  2168. expect(validateArgs(spec, { color: 'red' })).toEqual([])
  2169. expect(validateArgs(spec, { color: 'blue' })).toEqual(['"color" must be one of ["red","green"]'])
  2170. })
  2171. it('enforces type-correct scalar enum declarations', () => {
  2172. const spec = { n: { type: 'number', enum: [1, 2] } } satisfies ParameterSchemaSpec
  2173. expect(validateArgs(spec, { n: 1 })).toEqual([])
  2174. expect(validateArgs(spec, { n: 3 })).toEqual(['"n" must be one of [1,2]'])
  2175. const invalid = { n: { type: 'number', enum: ['1', '2'] } } as unknown as ParameterSchemaSpec
  2176. expect(() => validateArgs(invalid, { n: 1 })).toThrow(JsonSchemaError)
  2177. })
  2178. it('rejects an unknown schema type at the author boundary', () => {
  2179. const spec = { x: { type: 'weird' } } as unknown as ParameterSchemaSpec
  2180. expect(() => validateArgs(spec, { x: 1 })).toThrow(JsonSchemaError)
  2181. })
  2182. it('recurses into nested objects (and an object without properties only type-checks)', () => {
  2183. const spec = {
  2184. config: {
  2185. type: 'object',
  2186. additionalProperties: true,
  2187. required: true,
  2188. properties: { host: { type: 'string', required: true }, port: { type: 'number' } },
  2189. },
  2190. bag: { type: 'object', additionalProperties: true },
  2191. } satisfies ParameterSchemaSpec
  2192. expect(validateArgs(spec, { config: { host: 'h' }, bag: { anything: true } })).toEqual([])
  2193. expect(validateArgs(spec, { config: { port: 9 }, bag: 5 })).toEqual([
  2194. 'missing required property "config.host"',
  2195. '"bag" must be an object',
  2196. ])
  2197. })
  2198. it('recurses into array items (and an array without items only type-checks)', () => {
  2199. const spec = {
  2200. tags: { type: 'array', items: { type: 'string' } },
  2201. raw: { type: 'array' },
  2202. } satisfies ParameterSchemaSpec
  2203. expect(validateArgs(spec, { tags: ['a', 'b'], raw: [1, {}, 'x'] })).toEqual([])
  2204. expect(validateArgs(spec, { tags: ['a', 2] })).toEqual(['"tags[1]" must be a string'])
  2205. // a non-array value for an array-typed prop
  2206. expect(validateArgs(spec, { tags: 'nope' })).toEqual(['"tags" must be an array'])
  2207. })
  2208. it('validates arrays of objects element-wise', () => {
  2209. const spec = {
  2210. servers: {
  2211. type: 'array',
  2212. items: { type: 'object', additionalProperties: true, properties: { host: { type: 'string', required: true } } },
  2213. },
  2214. } satisfies ParameterSchemaSpec
  2215. expect(validateArgs(spec, { servers: [{ host: 'a' }, {}] })).toEqual([
  2216. 'missing required property "servers[1].host"',
  2217. ])
  2218. })
  2219. })
  2220. describe('defineTool validation (the runtime-validation Agent Note, part 1)', () => {
  2221. it('returns an isError result with the violations when the model sends bad args', async () => {
  2222. const ctx = await setup()
  2223. ctx.tools.register(defineContentToolFixture({
  2224. name: 'reader',
  2225. description: 'reads a path',
  2226. parameters: { path: { type: 'string', required: true } },
  2227. async execute(args) {
  2228. return [{ type: 'text', text: args.path }]
  2229. },
  2230. }))
  2231. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'reader', arguments: {} })
  2232. expect(result.isError).toBe(true)
  2233. expect(result.content[0]).toMatchObject({
  2234. text: 'Error: invalid arguments: missing required property "path"',
  2235. })
  2236. })
  2237. it('runs execute normally when args are valid', async () => {
  2238. const ctx = await setup()
  2239. ctx.tools.register(defineContentToolFixture({
  2240. name: 'reader',
  2241. description: 'reads a path',
  2242. parameters: { path: { type: 'string', required: true } },
  2243. async execute(args) {
  2244. return [{ type: 'text', text: `read ${args.path}` }]
  2245. },
  2246. }))
  2247. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'reader', arguments: { path: '/x' } })
  2248. expect(result).toEqual({
  2249. content: [{ type: 'text', text: 'read /x' }],
  2250. isError: false,
  2251. value: [{ type: 'text', text: 'read /x' }],
  2252. })
  2253. })
  2254. it('ToolArgsError carries a stable code and the violation list', () => {
  2255. const err = new ToolArgsError(['missing required property "a"', '"b" must be a number'])
  2256. expect(err).toBeInstanceOf(Error)
  2257. expect(err.name).toBe('ToolArgsError')
  2258. expect(err.code).toBe('INVALID_ARGS')
  2259. expect(err.violations).toEqual(['missing required property "a"', '"b" must be a number'])
  2260. expect(err.message).toBe('invalid arguments: missing required property "a"; "b" must be a number')
  2261. })
  2262. it('a schema-invalid call surfaces the structured error on the result', async () => {
  2263. const ctx = await setup()
  2264. ctx.tools.register(defineContentToolFixture({
  2265. name: 'reader',
  2266. description: 'reads a path',
  2267. parameters: { path: { type: 'string', required: true } },
  2268. async execute(args) {
  2269. return [{ type: 'text', text: args.path }]
  2270. },
  2271. }))
  2272. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'reader', arguments: {} })
  2273. expect(result.isError).toBe(true)
  2274. expect(result.error).toEqual({
  2275. message: 'invalid arguments: missing required property "path"',
  2276. info: { name: 'ToolArgsError', code: 'INVALID_ARGS' },
  2277. })
  2278. })
  2279. it('a tool throwing a HarnessError surfaces its name and code', async () => {
  2280. const { HarnessError } = await import('@deepseek-ai/dsh-llm')
  2281. const ctx = await setup()
  2282. ctx.tools.register({
  2283. ...echoTool,
  2284. name: 'coded',
  2285. async execute() {
  2286. throw new HarnessError('disk full', 'ENOSPC')
  2287. },
  2288. })
  2289. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'coded', arguments: {} })
  2290. expect(result.isError).toBe(true)
  2291. expect(result.error).toEqual({ message: 'disk full', info: { name: 'HarnessError', code: 'ENOSPC' } })
  2292. expect(result.content[0]).toMatchObject({ text: 'Error: disk full' })
  2293. })
  2294. it('a non-HarnessError throw retains only its message', async () => {
  2295. const ctx = await setup()
  2296. ctx.tools.register({
  2297. ...echoTool,
  2298. name: 'plain',
  2299. async execute() {
  2300. throw new Error('just a message')
  2301. },
  2302. })
  2303. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'plain', arguments: {} })
  2304. expect(result.isError).toBe(true)
  2305. expect(result.error).toEqual({ message: 'just a message' })
  2306. expect(result.content[0]).toMatchObject({ text: 'Error: just a message' })
  2307. })
  2308. it('raw-registered tools are NOT validated by defineTool (MCP keeps its own)', async () => {
  2309. const ctx = await setup()
  2310. // A raw ToolDefinition: no defineTool wrapping, so no validateArgs guard.
  2311. ctx.tools.register({
  2312. name: 'raw',
  2313. description: 'raw tool',
  2314. parameters: { type: 'object', properties: { path: { type: 'string' } }, required: ['path'] },
  2315. output: {
  2316. schema: { type: 'string' },
  2317. render: (_args, value) => [{ type: 'text', text: value as string }],
  2318. },
  2319. async execute(args: unknown) {
  2320. return typeof args
  2321. },
  2322. })
  2323. // Missing the "required" path — but raw tools validate their own input, so
  2324. // this reaches execute rather than being rejected by the harness.
  2325. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'raw', arguments: {} })
  2326. expect(result.isError).toBe(false)
  2327. })
  2328. it('attaches a positive-finite timeoutMs to the definition', () => {
  2329. const tool = defineContentToolFixture({
  2330. name: 'x', description: 'd', parameters: {}, timeoutMs: 30_000,
  2331. async execute() { return [{ type: 'text' as const, text: 'ok' }] },
  2332. })
  2333. expect(tool.timeoutMs).toBe(30_000)
  2334. })
  2335. it('omits timeoutMs when not declared', () => {
  2336. const tool = defineContentToolFixture({
  2337. name: 'x', description: 'd', parameters: {},
  2338. async execute() { return [{ type: 'text' as const, text: 'ok' }] },
  2339. })
  2340. expect(tool.timeoutMs).toBeUndefined()
  2341. })
  2342. it('throws when timeoutMs is zero or negative', () => {
  2343. const make = (ms: number) => defineContentToolFixture({
  2344. name: 'x', description: 'd', parameters: {}, timeoutMs: ms,
  2345. async execute() { return [{ type: 'text' as const, text: 'ok' }] },
  2346. })
  2347. expect(() => make(0)).toThrow('timeoutMs must be a positive finite number')
  2348. expect(() => make(-5)).toThrow('positive finite number')
  2349. })
  2350. it('throws when timeoutMs is non-finite', () => {
  2351. expect(() => defineContentToolFixture({
  2352. name: 'x', description: 'd', parameters: {}, timeoutMs: Infinity,
  2353. async execute() { return [{ type: 'text' as const, text: 'ok' }] },
  2354. })).toThrow('positive finite number')
  2355. })
  2356. })
  2357. describe('defineTool presentation (presentCall / presentResult)', () => {
  2358. it('preserves inline enum and const literals in inferred arguments', () => {
  2359. defineTool({
  2360. name: 'literal-args',
  2361. description: 'literal arguments',
  2362. parameters: {
  2363. mode: { type: 'string', enum: ['read', 'write'], required: true },
  2364. attempt: { type: 'integer', const: 1 },
  2365. },
  2366. output: {
  2367. schema: { type: 'null' },
  2368. render: () => [],
  2369. },
  2370. async execute(args) {
  2371. expectTypeOf(args).toEqualTypeOf<{ mode: 'read' | 'write'; attempt?: 1 }>()
  2372. return null
  2373. },
  2374. })
  2375. })
  2376. it('threads presentCall/presentResult onto the ToolDefinition with typed args', () => {
  2377. const tool = defineContentToolFixture({
  2378. name: 'demo',
  2379. description: 'demo',
  2380. parameters: { path: { type: 'string', required: true }, n: { type: 'number' } },
  2381. async execute() { return [{ type: 'text', text: 'ok' }] },
  2382. presentCall(args) {
  2383. // args is typed { path: string; n?: number } — zero casts.
  2384. expectTypeOf(args).toEqualTypeOf<{ path: string; n?: number }>()
  2385. return { card: 'generic', title: `Open ${args.path}`, kind: 'read', rawInput: args.path }
  2386. },
  2387. presentResult(args, result) {
  2388. return { card: 'generic', title: `Opened ${args.path}`, content: result.content }
  2389. },
  2390. })
  2391. expect(tool.presentCall!({ path: '/a', n: 2 })).toEqual({ card: 'generic', title: 'Open /a', kind: 'read', rawInput: '/a' })
  2392. expect(tool.presentResult!({ path: '/a' }, { content: [{ type: 'text', text: 'x' }], isError: false }))
  2393. .toEqual({ card: 'generic', title: 'Opened /a', content: [{ type: 'text', text: 'x' }] })
  2394. })
  2395. it('a tool without presentCall/presentResult leaves them undefined (UI falls back generically)', () => {
  2396. const tool = defineContentToolFixture({
  2397. name: 'plain',
  2398. description: 'plain',
  2399. parameters: { x: { type: 'string', required: true } },
  2400. async execute() { return [] },
  2401. })
  2402. expect(typeof tool.presentCall).toBe('undefined')
  2403. expect(typeof tool.presentResult).toBe('undefined')
  2404. })
  2405. it('presentCall/presentResult validate softly: malformed args return undefined, never throw (display runs on replay)', () => {
  2406. const tool = defineContentToolFixture({
  2407. name: 'demo',
  2408. description: 'demo',
  2409. parameters: { path: { type: 'string', required: true } },
  2410. async execute() { return [] },
  2411. presentCall: args => ({ card: 'generic', title: args.path }),
  2412. presentResult: (args, result) => ({ card: 'generic', title: args.path, content: result.content }),
  2413. })
  2414. // Unlike execute (which throws ToolArgsError on a mismatch), the display
  2415. // methods soft-validate and fall back to undefined so a UI never crashes
  2416. // replaying an old/foreign log entry. The ToolDefinition methods take
  2417. // `unknown`, so malformed shapes pass without a cast.
  2418. expect(tool.presentCall?.({})).toBeUndefined()
  2419. expect(tool.presentResult?.({ wrong: 1 }, { content: [], isError: false })).toBeUndefined()
  2420. })
  2421. })