tools.spec.ts 102 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687
  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' } })
  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. })
  814. it('keeps deferred contexts when a composite tool throws, but drops them when the outer call is blocked', async () => {
  815. const ctx = await setup()
  816. ctx.tools.register(defineContentToolFixture({
  817. name: 'failing-composite',
  818. description: 'failing composite',
  819. parameters: {},
  820. async execute(_args, exec) {
  821. exec.deferContext({ content: [{ type: 'text', text: 'nested' }], source: { kind: 'plugin', plugin: 'nested' } })
  822. throw new Error('outer failure')
  823. },
  824. }))
  825. const failed = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('failed'), name: 'failing-composite', arguments: {} })
  826. expect(failed.isError).toBe(true)
  827. expect(failed.additionalContexts?.map(context => context.source)).toEqual([{ kind: 'plugin', plugin: 'nested' }])
  828. ctx.on('tools/post-execute', async (): Promise<PostToolDecision> => ({
  829. kind: 'block',
  830. feedback: [{ type: 'text', text: 'blocked' }],
  831. additionalContexts: [{ content: [{ type: 'text', text: 'block-only' }], source: { kind: 'plugin', plugin: 'blocker' } }],
  832. }))
  833. const blocked = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('blocked'), name: 'failing-composite', arguments: {} })
  834. expect(blocked.isError).toBe(true)
  835. expect(blocked.additionalContexts?.map(context => context.source)).toEqual([{ kind: 'plugin', plugin: 'blocker' }])
  836. })
  837. it('composes pre + post waterfalls around dispatch (sandbox-wrap pattern)', async () => {
  838. const ctx = await setup()
  839. ctx.tools.register(echoTool)
  840. const order: string[] = []
  841. ctx.on('tools/pre-execute', async (_exec, next) => {
  842. order.push('pre:before')
  843. const decision = await next()
  844. order.push('pre:after')
  845. return decision
  846. })
  847. ctx.on('tools/post-execute', async (_exec, _result, next) => {
  848. order.push('post:before')
  849. const decision = await next()
  850. order.push('post:after')
  851. return decision
  852. })
  853. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'echo', arguments: { text: 'x' } })
  854. expect(result.isError).toBe(false)
  855. // pre runs fully (gate) before dispatch, then post runs over the result.
  856. expect(order).toEqual(['pre:before', 'pre:after', 'post:before', 'post:after'])
  857. })
  858. it('runs tools/execute after an allowed pre-execute, around dispatch, and before post-execute', async () => {
  859. const ctx = await setup()
  860. const order: string[] = []
  861. ctx.tools.register(defineContentToolFixture({
  862. name: 'traced',
  863. description: 'echo',
  864. parameters: { text: { type: 'string' } },
  865. async execute(args) {
  866. order.push('dispatch')
  867. return [{ type: 'text' as const, text: args.text ?? '' }]
  868. },
  869. }))
  870. ctx.on('tools/pre-execute', async (_exec, next) => { order.push('pre'); return next() })
  871. ctx.on('tools/execute', async (_exec: ToolDispatchExecution, next: () => Promise<ToolExecutionResult>): Promise<ToolExecutionResult> => {
  872. order.push('execute:before')
  873. const result = await next()
  874. order.push('execute:after')
  875. return result
  876. })
  877. ctx.on('tools/post-execute', async (_exec, _result, next) => { order.push('post'); return next() })
  878. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'traced', arguments: { text: 'hi' } })
  879. expect(result).toEqual({ content: [{ type: 'text', text: 'hi' }], isError: false, value: [{ type: 'text', text: 'hi' }] })
  880. // The around seam wraps dispatch; pre gates before it, post runs over its result.
  881. expect(order).toEqual(['pre', 'execute:before', 'dispatch', 'execute:after', 'post'])
  882. })
  883. it('skips dispatch when caller cancellation arrives while pre-execute awaits', async () => {
  884. const ctx = await setup()
  885. let dispatched = 0
  886. ctx.tools.register({
  887. ...echoTool,
  888. name: 'must-not-run',
  889. async execute() { dispatched += 1; return [] },
  890. })
  891. const entered = Promise.withResolvers<undefined>()
  892. const release = Promise.withResolvers<undefined>()
  893. ctx.on('tools/pre-execute', async (_exec, next) => {
  894. entered.resolve(undefined)
  895. await release.promise
  896. return await next()
  897. })
  898. const controller = new AbortController()
  899. const pending = ctx.tools.execute({
  900. callId: CallId('cancelled-in-pre'), name: 'must-not-run', arguments: {}, signal: controller.signal,
  901. })
  902. await entered.promise
  903. controller.abort('cancelled in policy')
  904. release.resolve(undefined)
  905. await expect(pending).resolves.toMatchObject({
  906. content: [{ type: 'text', text: 'Error: tool call aborted before dispatch' }],
  907. isError: true,
  908. error: { info: { name: 'AbortError', code: TOOL_ABORTED_BEFORE_DISPATCH } },
  909. })
  910. expect(dispatched).toBe(0)
  911. })
  912. it('preserves a pre-execute denial that settles after cancellation', async () => {
  913. const ctx = await setup()
  914. let dispatched = 0
  915. ctx.tools.register({
  916. ...echoTool,
  917. name: 'denied-after-cancel',
  918. async execute() { dispatched += 1; return [] },
  919. })
  920. const entered = Promise.withResolvers<undefined>()
  921. const release = Promise.withResolvers<undefined>()
  922. ctx.on('tools/pre-execute', async () => {
  923. entered.resolve(undefined)
  924. await release.promise
  925. return { kind: 'deny', reason: 'policy denied the call' }
  926. })
  927. const controller = new AbortController()
  928. const pending = ctx.tools.execute({
  929. callId: CallId('denied-after-cancel'), name: 'denied-after-cancel', arguments: {}, signal: controller.signal,
  930. })
  931. await entered.promise
  932. controller.abort('cancelled while policy decided')
  933. release.resolve(undefined)
  934. await expect(pending).resolves.toEqual({
  935. content: [{ type: 'text', text: 'Error: policy denied the call' }],
  936. isError: true,
  937. error: { message: 'policy denied the call' },
  938. })
  939. expect(dispatched).toBe(0)
  940. })
  941. it('preserves an async pre-execute failure that settles after cancellation', async () => {
  942. const ctx = await setup()
  943. let dispatched = 0
  944. ctx.tools.register({
  945. ...echoTool,
  946. name: 'must-not-run',
  947. async execute() { dispatched += 1; return [] },
  948. })
  949. const entered = Promise.withResolvers<undefined>()
  950. const release = Promise.withResolvers<undefined>()
  951. ctx.on('tools/pre-execute', async () => {
  952. entered.resolve(undefined)
  953. await release.promise
  954. throw new Error('gate interrupted')
  955. })
  956. const controller = new AbortController()
  957. const pending = ctx.tools.execute({
  958. callId: CallId('cancelled-pre-error'), name: 'must-not-run', arguments: {}, signal: controller.signal,
  959. })
  960. await entered.promise
  961. controller.abort('cancelled in policy')
  962. release.resolve(undefined)
  963. await expect(pending).resolves.toEqual({
  964. content: [{ type: 'text', text: 'Error: gate interrupted' }],
  965. isError: true,
  966. error: { message: 'gate interrupted' },
  967. })
  968. expect(dispatched).toBe(0)
  969. })
  970. it('rechecks caller cancellation after an async around-dispatch wrapper delegates', async () => {
  971. const ctx = await setup()
  972. let dispatched = 0
  973. ctx.tools.register({
  974. ...echoTool,
  975. name: 'must-not-run',
  976. async execute() { dispatched += 1; return [] },
  977. })
  978. const entered = Promise.withResolvers<undefined>()
  979. const release = Promise.withResolvers<undefined>()
  980. const replacement = new AbortController()
  981. ctx.on('tools/execute', async (exec, next) => {
  982. const upstream = exec.signal
  983. exec.signal = replacement.signal
  984. try {
  985. entered.resolve(undefined)
  986. await release.promise
  987. return await next()
  988. } finally {
  989. exec.signal = upstream
  990. }
  991. })
  992. const controller = new AbortController()
  993. const pending = ctx.tools.execute({
  994. callId: CallId('cancelled-in-around'), name: 'must-not-run', arguments: {}, signal: controller.signal,
  995. })
  996. await entered.promise
  997. controller.abort('cancelled in wrapper')
  998. release.resolve(undefined)
  999. await expect(pending).resolves.toMatchObject({
  1000. isError: true,
  1001. error: { info: { name: 'AbortError', code: TOOL_ABORTED_BEFORE_DISPATCH } },
  1002. })
  1003. expect(dispatched).toBe(0)
  1004. })
  1005. it('skips dispatch when an around wrapper supplies an already-aborted signal', async () => {
  1006. const ctx = await setup()
  1007. let dispatched = 0
  1008. ctx.tools.register({
  1009. ...echoTool,
  1010. name: 'must-not-run',
  1011. async execute() { dispatched += 1; return [] },
  1012. })
  1013. const replacement = AbortSignal.abort('wrapper cancelled')
  1014. ctx.on('tools/execute', async (exec, next) => {
  1015. const upstream = exec.signal
  1016. exec.signal = replacement
  1017. try {
  1018. return await next()
  1019. } finally {
  1020. exec.signal = upstream
  1021. }
  1022. })
  1023. const controller = new AbortController()
  1024. const result = await ctx.tools.execute({
  1025. callId: CallId('cancelled-wrapper'), name: 'must-not-run', arguments: {}, signal: controller.signal,
  1026. })
  1027. expect(result.error).toEqual({
  1028. message: 'tool call aborted before dispatch',
  1029. info: { name: 'AbortError', code: TOOL_ABORTED_BEFORE_DISPATCH },
  1030. })
  1031. expect(dispatched).toBe(0)
  1032. })
  1033. it('uses ABORTED_BEFORE_DISPATCH when cancellation overtakes a wrapper short-circuit', async () => {
  1034. const ctx = await setup()
  1035. let dispatched = 0
  1036. ctx.tools.register({
  1037. ...echoTool,
  1038. name: 'short-circuited',
  1039. async execute() { dispatched += 1; return [] },
  1040. })
  1041. const entered = Promise.withResolvers<undefined>()
  1042. const release = Promise.withResolvers<undefined>()
  1043. ctx.on('tools/execute', async () => {
  1044. entered.resolve(undefined)
  1045. await release.promise
  1046. return {
  1047. value: 'wrapper success',
  1048. content: [{ type: 'text', text: 'wrapper success' }],
  1049. isError: false,
  1050. additionalContexts: [{
  1051. content: [{ type: 'text', text: 'wrapper context' }],
  1052. source: { kind: 'plugin', plugin: 'wrapper' },
  1053. }],
  1054. }
  1055. })
  1056. const controller = new AbortController()
  1057. const pending = ctx.tools.execute({
  1058. callId: CallId('cancelled-short-circuit'),
  1059. name: 'short-circuited',
  1060. arguments: {},
  1061. signal: controller.signal,
  1062. })
  1063. await entered.promise
  1064. controller.abort('cancelled while wrapper waited')
  1065. release.resolve(undefined)
  1066. await expect(pending).resolves.toMatchObject({
  1067. content: [{ type: 'text', text: 'Error: tool call aborted before dispatch' }],
  1068. isError: true,
  1069. error: { info: { name: 'AbortError', code: TOOL_ABORTED_BEFORE_DISPATCH } },
  1070. additionalContexts: [{ source: { kind: 'plugin', plugin: 'wrapper' } }],
  1071. })
  1072. expect(dispatched).toBe(0)
  1073. })
  1074. it('replaces a late wrapper success with ABORTED and preserves deferred contexts', async () => {
  1075. const ctx = await setup()
  1076. ctx.tools.register({
  1077. ...echoTool,
  1078. name: 'completed-before-wrapper',
  1079. async execute(_args, exec) {
  1080. exec.deferContext({
  1081. content: [{ type: 'text', text: 'completed child work' }],
  1082. source: { kind: 'plugin', plugin: 'child' },
  1083. })
  1084. return 'body complete'
  1085. },
  1086. })
  1087. const entered = Promise.withResolvers<undefined>()
  1088. const release = Promise.withResolvers<undefined>()
  1089. ctx.on('tools/execute', async (_exec, next) => {
  1090. const result = await next()
  1091. entered.resolve(undefined)
  1092. await release.promise
  1093. return result
  1094. })
  1095. const controller = new AbortController()
  1096. const pending = ctx.tools.execute({
  1097. callId: CallId('cancelled-after-body'), name: 'completed-before-wrapper', arguments: {}, signal: controller.signal,
  1098. })
  1099. await entered.promise
  1100. controller.abort('cancelled while wrapper settled')
  1101. release.resolve(undefined)
  1102. await expect(pending).resolves.toMatchObject({
  1103. content: [{ type: 'text', text: 'Error: tool call aborted' }],
  1104. isError: true,
  1105. error: { info: { name: 'AbortError', code: TOOL_ABORTED } },
  1106. additionalContexts: [{ source: { kind: 'plugin', plugin: 'child' } }],
  1107. })
  1108. })
  1109. it('replaces a late post-execute success with ABORTED and preserves contexts', async () => {
  1110. const ctx = await setup()
  1111. ctx.tools.register({
  1112. ...echoTool,
  1113. name: 'completed-before-post',
  1114. async execute(_args, exec) {
  1115. exec.deferContext({
  1116. content: [{ type: 'text', text: 'completed child work' }],
  1117. source: { kind: 'plugin', plugin: 'child' },
  1118. })
  1119. return 'body complete'
  1120. },
  1121. })
  1122. const entered = Promise.withResolvers<undefined>()
  1123. const release = Promise.withResolvers<undefined>()
  1124. ctx.on('tools/post-execute', async (_exec, _result, next) => {
  1125. const decision = await next()
  1126. entered.resolve(undefined)
  1127. await release.promise
  1128. return {
  1129. ...decision,
  1130. additionalContexts: [{
  1131. content: [{ type: 'text', text: 'post context' }],
  1132. source: { kind: 'plugin', plugin: 'post' },
  1133. }],
  1134. }
  1135. })
  1136. const controller = new AbortController()
  1137. const pending = ctx.tools.execute({
  1138. callId: CallId('cancelled-in-post'), name: 'completed-before-post', arguments: {}, signal: controller.signal,
  1139. })
  1140. await entered.promise
  1141. controller.abort('cancelled while post policy waits')
  1142. release.resolve(undefined)
  1143. await expect(pending).resolves.toMatchObject({
  1144. content: [{ type: 'text', text: 'Error: tool call aborted' }],
  1145. isError: true,
  1146. error: { info: { name: 'AbortError', code: TOOL_ABORTED } },
  1147. additionalContexts: [
  1148. { source: { kind: 'plugin', plugin: 'child' } },
  1149. { source: { kind: 'plugin', plugin: 'post' } },
  1150. ],
  1151. })
  1152. })
  1153. it('preserves an around-dispatch failure that settles after cancellation', async () => {
  1154. const ctx = await setup()
  1155. let dispatched = 0
  1156. ctx.tools.register({
  1157. ...echoTool,
  1158. name: 'wrapper-failure',
  1159. async execute() { dispatched += 1; return [] },
  1160. })
  1161. const entered = Promise.withResolvers<undefined>()
  1162. const release = Promise.withResolvers<undefined>()
  1163. ctx.on('tools/execute', async () => {
  1164. entered.resolve(undefined)
  1165. await release.promise
  1166. throw new HarnessError('wrapper failed', 'WRAPPER_FAILURE')
  1167. })
  1168. const controller = new AbortController()
  1169. const pending = ctx.tools.execute({
  1170. callId: CallId('wrapper-failure'), name: 'wrapper-failure', arguments: {}, signal: controller.signal,
  1171. })
  1172. await entered.promise
  1173. controller.abort('cancelled while wrapper failed')
  1174. release.resolve(undefined)
  1175. await expect(pending).resolves.toMatchObject({
  1176. content: [{ type: 'text', text: 'Error: wrapper failed' }],
  1177. isError: true,
  1178. error: { info: { name: 'HarnessError', code: 'WRAPPER_FAILURE' } },
  1179. })
  1180. expect(dispatched).toBe(0)
  1181. })
  1182. it('preserves a tool-owned failure after the body observes cancellation', async () => {
  1183. const ctx = await setup()
  1184. const entered = Promise.withResolvers<undefined>()
  1185. ctx.tools.register({
  1186. ...echoTool,
  1187. name: 'tool-failure',
  1188. execute(_args, exec) {
  1189. entered.resolve(undefined)
  1190. return new Promise<never>((_resolve, reject) => {
  1191. exec.signal.addEventListener('abort', () => {
  1192. reject(new HarnessError('tool failed', 'TOOL_FAILURE'))
  1193. }, { once: true })
  1194. })
  1195. },
  1196. })
  1197. const controller = new AbortController()
  1198. const pending = ctx.tools.execute({
  1199. callId: CallId('tool-failure'), name: 'tool-failure', arguments: {}, signal: controller.signal,
  1200. })
  1201. await entered.promise
  1202. controller.abort('cancelled running body')
  1203. await expect(pending).resolves.toMatchObject({
  1204. content: [{ type: 'text', text: 'Error: tool failed' }],
  1205. isError: true,
  1206. error: { info: { name: 'HarnessError', code: 'TOOL_FAILURE' } },
  1207. })
  1208. })
  1209. it('preserves a post-policy failure that settles after cancellation', async () => {
  1210. const ctx = await setup()
  1211. ctx.tools.register(echoTool)
  1212. const entered = Promise.withResolvers<undefined>()
  1213. const release = Promise.withResolvers<undefined>()
  1214. ctx.on('tools/post-execute', async () => {
  1215. entered.resolve(undefined)
  1216. await release.promise
  1217. throw new HarnessError('post-policy failed', 'POST_FAILURE')
  1218. })
  1219. const controller = new AbortController()
  1220. const pending = ctx.tools.execute({
  1221. callId: CallId('post-failure'), name: 'echo', arguments: {}, signal: controller.signal,
  1222. })
  1223. await entered.promise
  1224. controller.abort('cancelled while post-policy failed')
  1225. release.resolve(undefined)
  1226. await expect(pending).resolves.toMatchObject({
  1227. content: [{ type: 'text', text: 'Error: post-policy failed' }],
  1228. isError: true,
  1229. error: { info: { name: 'HarnessError', code: 'POST_FAILURE' } },
  1230. })
  1231. })
  1232. it('fuses caller cancellation back into a wrapper replacement for the running body', async () => {
  1233. const ctx = await setup()
  1234. const entered = Promise.withResolvers<undefined>()
  1235. const replacement = new AbortController()
  1236. let bodySignal: AbortSignal | undefined
  1237. ctx.tools.register({
  1238. ...echoTool,
  1239. name: 'cooperative',
  1240. execute(_args, exec) {
  1241. bodySignal = exec.signal
  1242. entered.resolve(undefined)
  1243. if (exec.signal.aborted) return Promise.resolve('stopped')
  1244. return new Promise<string>((resolve) => {
  1245. exec.signal.addEventListener('abort', () => { resolve('stopped') }, { once: true })
  1246. })
  1247. },
  1248. })
  1249. ctx.on('tools/execute', async (exec, next) => {
  1250. const upstream = exec.signal
  1251. exec.signal = replacement.signal
  1252. try {
  1253. return await next()
  1254. } finally {
  1255. exec.signal = upstream
  1256. }
  1257. })
  1258. const controller = new AbortController()
  1259. const pending = ctx.tools.execute({
  1260. callId: CallId('cancelled-body'), name: 'cooperative', arguments: {}, signal: controller.signal,
  1261. })
  1262. await entered.promise
  1263. expect(bodySignal).not.toBe(controller.signal)
  1264. expect(bodySignal).not.toBe(replacement.signal)
  1265. controller.abort('cancel running body')
  1266. await expect(pending).resolves.toMatchObject({
  1267. isError: true,
  1268. error: { info: { name: 'AbortError', code: TOOL_ABORTED } },
  1269. })
  1270. expect(bodySignal?.aborted).toBe(true)
  1271. expect(replacement.signal.aborted).toBe(false)
  1272. })
  1273. it('restores the required caller signal after around dispatch', async () => {
  1274. const ctx = await setup()
  1275. let postSignal: AbortSignal | undefined
  1276. ctx.on('tools/execute', async (exec, next) => {
  1277. const upstream = exec.signal
  1278. exec.signal = new AbortController().signal
  1279. try {
  1280. return await next()
  1281. } finally {
  1282. exec.signal = upstream
  1283. }
  1284. })
  1285. ctx.on('tools/post-execute', async (exec, _result, next) => {
  1286. postSignal = exec.signal
  1287. return next()
  1288. })
  1289. const controller = new AbortController()
  1290. await ctx.tools.execute({
  1291. callId: CallId('restored-signal'), name: 'echo', arguments: {}, signal: controller.signal,
  1292. })
  1293. expect(postSignal).toBe(controller.signal)
  1294. })
  1295. it('waits for an uncooperative started body before returning ABORTED', async () => {
  1296. const ctx = await setup()
  1297. const entered = Promise.withResolvers<undefined>()
  1298. const release = Promise.withResolvers<string>()
  1299. ctx.tools.register({
  1300. ...echoTool,
  1301. name: 'uncooperative',
  1302. execute(_args, exec) {
  1303. exec.deferContext({
  1304. content: [{ type: 'text', text: 'nested outcome' }],
  1305. source: { kind: 'plugin', plugin: 'nested' },
  1306. })
  1307. entered.resolve(undefined)
  1308. return release.promise
  1309. },
  1310. })
  1311. const controller = new AbortController()
  1312. const pending = ctx.tools.execute({
  1313. callId: CallId('drain-body'), name: 'uncooperative', arguments: {}, signal: controller.signal,
  1314. })
  1315. await entered.promise
  1316. controller.abort('must still drain')
  1317. const state = await Promise.race([
  1318. pending.then(() => 'settled' as const),
  1319. Promise.resolve('pending' as const),
  1320. ])
  1321. expect(state).toBe('pending')
  1322. release.resolve('settled')
  1323. await expect(pending).resolves.toMatchObject({
  1324. isError: true,
  1325. error: { info: { name: 'AbortError', code: TOOL_ABORTED } },
  1326. additionalContexts: [{ source: { kind: 'plugin', plugin: 'nested' } }],
  1327. })
  1328. })
  1329. it('materializes a pre-aborted call and publishes one result without entering pipeline phases', async () => {
  1330. const ctx = await setup()
  1331. const phases = { pre: 0, around: 0, body: 0, post: 0, result: 0 }
  1332. const callerArguments = { nested: { value: 1 } }
  1333. const callerSignal = AbortSignal.abort('already cancelled')
  1334. let argumentReads = 0
  1335. let observedArguments: unknown
  1336. let observedExecution: object | undefined
  1337. let observedToken: symbol | undefined
  1338. let observedSignal: AbortSignal | undefined
  1339. let observedResult: ToolExecutionResult | undefined
  1340. ctx.tools.register({
  1341. ...echoTool,
  1342. name: 'domain-abort',
  1343. async execute() { phases.body += 1; return [] },
  1344. })
  1345. ctx.on('tools/pre-execute', async (_exec, next) => { phases.pre += 1; return next() })
  1346. ctx.on('tools/execute', async (_exec, next) => { phases.around += 1; return next() })
  1347. ctx.on('tools/post-execute', async (_exec, _result, next) => { phases.post += 1; return next() })
  1348. ctx.on('tools/result', (exec, result) => {
  1349. phases.result += 1
  1350. observedExecution = exec
  1351. observedArguments = exec.arguments
  1352. observedToken = exec.token
  1353. observedSignal = exec.signal
  1354. observedResult = result
  1355. })
  1356. const result = await ctx.tools.execute({
  1357. callId: CallId('pre-aborted'),
  1358. name: 'domain-abort',
  1359. get arguments() { argumentReads += 1; return callerArguments },
  1360. signal: callerSignal,
  1361. })
  1362. expect(argumentReads).toBe(1)
  1363. expect(phases).toEqual({ pre: 0, around: 0, body: 0, post: 0, result: 1 })
  1364. expect(result).toEqual({
  1365. content: [{ type: 'text', text: 'Error: tool call aborted before dispatch' }],
  1366. isError: true,
  1367. error: {
  1368. message: 'tool call aborted before dispatch',
  1369. info: { name: 'AbortError', code: TOOL_ABORTED_BEFORE_DISPATCH },
  1370. },
  1371. })
  1372. expect(observedResult).toBe(result)
  1373. expect(Object.isFrozen(observedExecution)).toBe(true)
  1374. expect(typeof observedToken).toBe('symbol')
  1375. expect(observedSignal).toBe(callerSignal)
  1376. expect(Object.isFrozen(result)).toBe(true)
  1377. expect(observedArguments).not.toBe(callerArguments)
  1378. expect(Object.isFrozen(observedArguments)).toBe(true)
  1379. expect(Object.isFrozen((observedArguments as { nested: object }).nested)).toBe(true)
  1380. })
  1381. it('lets argument materialization failure win over a pre-aborted signal', async () => {
  1382. const ctx = await setup()
  1383. let observed = 0
  1384. ctx.on('tools/result', () => { observed += 1 })
  1385. const result = await ctx.tools.execute({
  1386. callId: CallId('invalid-pre-aborted'),
  1387. name: 'missing',
  1388. arguments: { invalid: () => undefined },
  1389. signal: AbortSignal.abort('already cancelled'),
  1390. })
  1391. expect(result).toEqual({
  1392. content: [{ type: 'text', text: 'Error: tool execution arguments must be losslessly JSON-serializable' }],
  1393. isError: true,
  1394. error: { message: 'tool execution arguments must be losslessly JSON-serializable' },
  1395. })
  1396. expect(observed).toBe(1)
  1397. })
  1398. it('a pre-execute deny short-circuits before tools/execute (the seam never runs)', async () => {
  1399. const ctx = await setup()
  1400. ctx.tools.register(echoTool)
  1401. let entered = false
  1402. ctx.on('tools/pre-execute', async (_exec, _next): Promise<PreToolDecision> => ({ kind: 'deny', reason: 'nope' }))
  1403. ctx.on('tools/execute', async (_exec: ToolDispatchExecution, next: () => Promise<ToolExecutionResult>): Promise<ToolExecutionResult> => {
  1404. entered = true
  1405. return next()
  1406. })
  1407. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'echo', arguments: { text: 'hi' } })
  1408. expect(result.isError).toBe(true)
  1409. expect(result.content[0]).toMatchObject({ text: 'Error: nope' })
  1410. expect(entered).toBe(false) // a denied call never enters the around-dispatch seam
  1411. })
  1412. it('a thrown tool is normalized to an isError result BEFORE a tools/execute listener sees next()', async () => {
  1413. const ctx = await setup()
  1414. ctx.tools.register({
  1415. ...echoTool,
  1416. name: 'boom',
  1417. async execute() { throw new HarnessError('kaboom', 'BOOM') },
  1418. })
  1419. let seen: { isError: boolean; error?: unknown } | undefined
  1420. ctx.on('tools/execute', async (_exec: ToolDispatchExecution, next: () => Promise<ToolExecutionResult>): Promise<ToolExecutionResult> => {
  1421. const result = await next()
  1422. // The base next() IS dispatch-with-normalization: the wrapper sees the
  1423. // normalized isError result, never a raw throw from the tool body.
  1424. seen = { isError: result.isError, error: result.error }
  1425. return result
  1426. })
  1427. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'boom', arguments: {} })
  1428. expect(seen).toEqual({
  1429. isError: true,
  1430. error: { message: 'kaboom', info: { name: 'HarnessError', code: 'BOOM' } },
  1431. })
  1432. expect(result.isError).toBe(true)
  1433. expect(result.content[0]).toMatchObject({ text: 'Error: kaboom' })
  1434. })
  1435. it('freezes core dispatch outcomes before around and post listeners can observe them', async () => {
  1436. const ctx = await setup()
  1437. ctx.tools.register(echoTool)
  1438. const mutationAttempts: boolean[] = []
  1439. ctx.on('tools/execute', async (_exec, next) => {
  1440. const result = await next()
  1441. mutationAttempts.push(Reflect.set(result, 'value', 'around mutation'))
  1442. return result
  1443. })
  1444. ctx.on('tools/post-execute', async (_exec, result, next) => {
  1445. mutationAttempts.push(Reflect.set(result, 'value', 'post mutation'))
  1446. return next()
  1447. })
  1448. const result = await ctx.tools.execute({
  1449. signal: testToolSignal,
  1450. callId: CallId('frozen-canonical'), name: 'echo', arguments: { text: 'original' },
  1451. })
  1452. expect(mutationAttempts).toEqual([false, false])
  1453. expect(result.isError ? undefined : result.value).toBe('original')
  1454. })
  1455. it('a thrown tool normalized inside tools/execute still reaches post-execute', async () => {
  1456. const ctx = await setup()
  1457. ctx.tools.register({
  1458. ...echoTool,
  1459. name: 'boom',
  1460. async execute() { throw new Error('exploded') },
  1461. })
  1462. let postSaw: boolean | undefined
  1463. ctx.on('tools/execute', async (_exec: ToolDispatchExecution, next: () => Promise<ToolExecutionResult>): Promise<ToolExecutionResult> => next())
  1464. ctx.on('tools/post-execute', async (_exec, result, next) => {
  1465. postSaw = result.isError
  1466. return next()
  1467. })
  1468. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'boom', arguments: {} })
  1469. expect(postSaw).toBe(true) // the normalized isError still flows through post-execute
  1470. expect(result.isError).toBe(true)
  1471. expect(result.content[0]).toMatchObject({ text: 'Error: exploded' })
  1472. })
  1473. it('re-fuses the caller signal with an around-dispatch replacement for the body', async () => {
  1474. const ctx = await setup()
  1475. let seenSignal: AbortSignal | undefined
  1476. ctx.tools.register({
  1477. ...echoTool,
  1478. name: 'signal-probe',
  1479. async execute(_args, exec) {
  1480. seenSignal = exec.signal
  1481. return 'ok'
  1482. },
  1483. })
  1484. const upstream = new AbortController().signal
  1485. const replacement = new AbortController().signal
  1486. ctx.on('tools/execute', async (exec: ToolDispatchExecution, next: () => Promise<ToolExecutionResult>): Promise<ToolExecutionResult> => {
  1487. expect(exec.signal).toBe(upstream)
  1488. // Cordis next() ignores passed arguments, so a wrapper mutates exec in
  1489. // place (the documented "mutate the shared object, then delegate" idiom).
  1490. exec.signal = replacement
  1491. return next()
  1492. })
  1493. await ctx.tools.execute({ callId: CallId('c1'), name: 'signal-probe', arguments: {}, signal: upstream })
  1494. expect(seenSignal).toBeDefined()
  1495. expect(seenSignal).not.toBe(upstream)
  1496. expect(seenSignal).not.toBe(replacement)
  1497. })
  1498. it('a tools/execute listener can short-circuit dispatch by returning a result without next()', async () => {
  1499. const ctx = await setup()
  1500. let dispatched = false
  1501. ctx.tools.register({
  1502. ...echoTool,
  1503. name: 'never-runs',
  1504. async execute() { dispatched = true; return 'unreachable' },
  1505. })
  1506. ctx.on('tools/execute', async (_exec: ToolDispatchExecution, _next: () => Promise<ToolExecutionResult>): Promise<ToolExecutionResult> =>
  1507. ({ content: [{ type: 'text', text: 'ignored authored content' }], isError: false, value: 'short-circuited' }))
  1508. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'never-runs', arguments: {} })
  1509. expect(dispatched).toBe(false) // returning without next() skips core dispatch
  1510. expect(result.content[0]).toMatchObject({ text: 'short-circuited' })
  1511. })
  1512. it('revalidates a cached canonical result returned from a different dispatch', async () => {
  1513. const ctx = await setup()
  1514. ctx.tools.register({ ...echoTool, name: 'string-output', async execute() { return 'cached' } })
  1515. let objectBodyRan = false
  1516. ctx.tools.register(defineTool({
  1517. name: 'object-output',
  1518. description: 'Return one closed object.',
  1519. parameters: {},
  1520. output: {
  1521. schema: {
  1522. type: 'object',
  1523. properties: { ok: { type: 'boolean', required: true } },
  1524. additionalProperties: false,
  1525. },
  1526. render: (_args, value) => [{ type: 'text', text: String(value.ok) }],
  1527. },
  1528. execute() {
  1529. objectBodyRan = true
  1530. return Promise.resolve({ ok: true })
  1531. },
  1532. }))
  1533. let cached: ToolExecutionResult | undefined
  1534. ctx.on('tools/execute', async (exec, next) => {
  1535. if (exec.name === 'string-output') {
  1536. cached = await next()
  1537. return cached
  1538. }
  1539. if (exec.name === 'object-output') {
  1540. if (cached === undefined) throw new Error('expected the first dispatch result')
  1541. return cached
  1542. }
  1543. return next()
  1544. })
  1545. const first = await ctx.tools.execute({
  1546. signal: testToolSignal, callId: CallId('cached-first'), name: 'string-output', arguments: {},
  1547. })
  1548. const second = await ctx.tools.execute({
  1549. signal: testToolSignal, callId: CallId('cached-second'), name: 'object-output', arguments: {},
  1550. })
  1551. expect(first.isError ? undefined : first.value).toBe('cached')
  1552. expect(objectBodyRan).toBe(false)
  1553. expect(second).toMatchObject({
  1554. isError: true,
  1555. error: { info: { name: 'ToolOutputError', code: 'INVALID_TOOL_OUTPUT' } },
  1556. })
  1557. })
  1558. it('preserves additionalContexts supplied by an around-dispatch result', async () => {
  1559. const ctx = await setup()
  1560. ctx.tools.register(echoTool)
  1561. ctx.on('tools/execute', async () => ({
  1562. content: [{ type: 'text', text: 'short-circuited with context' }],
  1563. isError: false,
  1564. value: 'short-circuited with context',
  1565. additionalContexts: [{
  1566. content: [{ type: 'text', text: 'from around dispatch' }],
  1567. source: { kind: 'plugin', plugin: 'test' },
  1568. }],
  1569. }))
  1570. const result = await ctx.tools.execute({
  1571. signal: testToolSignal,
  1572. callId: CallId('around-context'), name: 'echo', arguments: {},
  1573. })
  1574. expect(result.additionalContexts).toEqual([{
  1575. content: [{ type: 'text', text: 'from around dispatch' }],
  1576. source: { kind: 'plugin', plugin: 'test' },
  1577. }])
  1578. })
  1579. it('returns an isError result when a tools/execute listener throws', async () => {
  1580. const ctx = await setup()
  1581. ctx.tools.register(echoTool)
  1582. ctx.on('tools/execute', async () => { throw new Error('wrapper broke') })
  1583. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'echo', arguments: { text: 'hi' } })
  1584. expect(result).toEqual({
  1585. content: [{ type: 'text', text: 'Error: wrapper broke' }],
  1586. error: { message: 'wrapper broke' },
  1587. isError: true,
  1588. })
  1589. })
  1590. it('returns an isError result when a tools/pre-execute listener throws', async () => {
  1591. const ctx = await setup()
  1592. ctx.tools.register(echoTool)
  1593. ctx.on('tools/pre-execute', async () => {
  1594. throw new Error('permission hook broke')
  1595. })
  1596. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'echo', arguments: { text: 'hi' } })
  1597. expect(result).toEqual({
  1598. content: [{ type: 'text', text: 'Error: permission hook broke' }],
  1599. error: { message: 'permission hook broke' },
  1600. isError: true,
  1601. })
  1602. })
  1603. it('returns an isError result when a tools/post-execute listener throws', async () => {
  1604. const ctx = await setup()
  1605. ctx.tools.register(echoTool)
  1606. ctx.on('tools/post-execute', async () => {
  1607. throw new Error('post hook broke')
  1608. })
  1609. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'echo', arguments: { text: 'hi' } })
  1610. expect(result).toEqual({
  1611. content: [{ type: 'text', text: 'Error: post hook broke' }],
  1612. error: { message: 'post hook broke' },
  1613. isError: true,
  1614. })
  1615. })
  1616. it('preserves structured error info when a tools/pre-execute listener throws HarnessError', async () => {
  1617. const ctx = await setup()
  1618. ctx.tools.register(echoTool)
  1619. ctx.on('tools/pre-execute', async () => {
  1620. throw new HarnessError('denied', 'DENIED')
  1621. })
  1622. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'echo', arguments: { text: 'hi' } })
  1623. expect(result).toMatchObject({
  1624. isError: true,
  1625. error: { message: 'denied', info: { name: 'HarnessError', code: 'DENIED' } },
  1626. })
  1627. })
  1628. it('schemas() snapshots tool schemas instead of exposing registry objects', async () => {
  1629. const ctx = await setup()
  1630. ctx.tools.register(echoTool)
  1631. const first = ctx.tools.schemas()
  1632. const firstParameters = first[0]!.parameters as { properties: Record<string, unknown> }
  1633. firstParameters.properties['mutated'] = { type: 'string' }
  1634. first[0]!.description = 'mutated'
  1635. expect(ctx.tools.schemas()).toEqual([{
  1636. name: 'echo',
  1637. description: 'echo arguments back',
  1638. parameters: { type: 'object', properties: { text: { type: 'string' } } },
  1639. }])
  1640. })
  1641. it('schemas() snapshots deeply nested parameters without using structured-clone recursion', async () => {
  1642. const ctx = await setup()
  1643. const depth = 5_000
  1644. let nested: JsonSchemaNode = { type: 'string' }
  1645. for (let index = 0; index < depth; index++) nested = { oneOf: [nested, { type: 'null' }] }
  1646. ctx.tools.register({
  1647. ...echoTool,
  1648. name: 'deep-schema',
  1649. parameters: { type: 'object', properties: { nested } },
  1650. })
  1651. const projected = ctx.tools.schemas()[0]!.parameters as JsonSchemaNode
  1652. let cursor = projected.properties!.nested!
  1653. let layers = 0
  1654. while (cursor.oneOf !== undefined) {
  1655. cursor = cursor.oneOf[0]!
  1656. layers++
  1657. }
  1658. expect(layers).toBe(depth)
  1659. expect(cursor).toEqual({ type: 'string' })
  1660. })
  1661. it('rejects schema projection when a raw registration is not lossless JSON', async () => {
  1662. const ctx = await setup()
  1663. ctx.tools.register({
  1664. ...echoTool,
  1665. name: 'lossy-schema',
  1666. parameters: { type: 'object', default: Number.NaN },
  1667. })
  1668. expect(() => ctx.tools.schemas())
  1669. .toThrow('tool "lossy-schema" parameters must be lossless JSON before schema projection')
  1670. })
  1671. it('rejects a non-positive or non-finite registration timeout', async () => {
  1672. const ctx = await setup()
  1673. expect(() => ctx.tools.register({ ...echoTool, name: 'zero-timeout', timeoutMs: 0 }))
  1674. .toThrow('timeoutMs must be a positive finite number')
  1675. expect(() => ctx.tools.register({ ...echoTool, name: 'infinite-timeout', timeoutMs: Number.POSITIVE_INFINITY }))
  1676. .toThrow('timeoutMs must be a positive finite number')
  1677. })
  1678. it('rejects duplicate names and unregisters on fiber dispose (HMR safety)', async () => {
  1679. const ctx = await setup()
  1680. ctx.tools.register(echoTool)
  1681. expect(() => ctx.tools.register(echoTool)).toThrow('already registered')
  1682. const fiber = await ctx.plugin(Object.assign((inner: Context) => {
  1683. inner.tools.register({ ...echoTool, name: 'scoped' })
  1684. }, { inject: ['tools'] }))
  1685. expect(ctx.tools.schemas().map(t => t.name)).toEqual(['echo', 'scoped'])
  1686. await fiber.dispose()
  1687. expect(ctx.tools.schemas().map(t => t.name)).toEqual(['echo'])
  1688. })
  1689. it('returns a callable disposer from register() that unregisters the tool', async () => {
  1690. const ctx = await setup()
  1691. ctx.tools.register(echoTool)
  1692. // Register a second tool and call its returned disposer directly
  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. // Verify types at runtime via typeof
  1842. expect(typeof args.a).toBe('string')
  1843. // args.b should be undefined when not provided
  1844. void args
  1845. return args.a
  1846. },
  1847. })
  1848. void tool
  1849. })
  1850. it('registry round-trips a defineTool definition (register→schemas→execute)', async () => {
  1851. const ctx = await setup()
  1852. ctx.tools.register(defineTool({
  1853. name: 'roundtrip',
  1854. description: 'Round-trip test',
  1855. parameters: {
  1856. req: { type: 'string', required: true },
  1857. opt: { type: 'number', description: 'Optional number' },
  1858. },
  1859. output: {
  1860. schema: { type: 'string' },
  1861. render: (_args, value) => [{ type: 'text', text: value }],
  1862. },
  1863. async execute(args) {
  1864. return `${args.req}:${args.opt ?? 'none'}`
  1865. },
  1866. }))
  1867. // Schema round-trip: schemas() returns standard JSON Schema
  1868. const schemas = ctx.tools.schemas()
  1869. expect(schemas).toHaveLength(1)
  1870. expect(schemas[0]!.parameters).toEqual({
  1871. type: 'object',
  1872. properties: {
  1873. req: { type: 'string' },
  1874. opt: { type: 'number', description: 'Optional number' },
  1875. },
  1876. required: ['req'],
  1877. })
  1878. // Execution round-trip
  1879. const result = await ctx.tools.execute({
  1880. signal: testToolSignal,
  1881. callId: CallId('c1'),
  1882. name: 'roundtrip',
  1883. arguments: { req: 'hello' },
  1884. })
  1885. expect(result.isError).toBe(false)
  1886. expect(result.content).toEqual([{ type: 'text', text: 'hello:none' }])
  1887. })
  1888. it('still accepts raw JSON-Schema ToolDefinition directly (MCP interop)', async () => {
  1889. const ctx = await setup()
  1890. ctx.tools.register({
  1891. name: 'raw-tool',
  1892. description: 'Raw JSON Schema tool (like an MCP adapter would register)',
  1893. parameters: {
  1894. type: 'object',
  1895. properties: { path: { type: 'string' } },
  1896. required: ['path'],
  1897. },
  1898. output: {
  1899. schema: { type: 'string' },
  1900. render: (_args, value) => [{ type: 'text', text: value as string }],
  1901. },
  1902. async execute(args: unknown) {
  1903. const p = args as { path: string }
  1904. return p.path
  1905. },
  1906. })
  1907. const schemas = ctx.tools.schemas()
  1908. expect(schemas[0]!.parameters).toEqual({
  1909. type: 'object',
  1910. properties: { path: { type: 'string' } },
  1911. required: ['path'],
  1912. })
  1913. const result = await ctx.tools.execute({
  1914. signal: testToolSignal,
  1915. callId: CallId('c1'),
  1916. name: 'raw-tool',
  1917. arguments: { path: '/tmp' },
  1918. })
  1919. expect(result.isError).toBe(false)
  1920. expect(result.content).toEqual([{ type: 'text', text: '/tmp' }])
  1921. })
  1922. })
  1923. describe('schema DSL edge cases', () => {
  1924. it('emits enum values in JSON Schema property', () => {
  1925. const spec = {
  1926. color: { type: 'string', enum: ['red', 'green', 'blue'], description: 'Color choice' },
  1927. } satisfies ParameterSchemaSpec
  1928. const jsonSchema = parameterSchemaSpecToJsonSchema(spec)
  1929. expect(jsonSchema.properties['color']).toMatchObject({
  1930. type: 'string',
  1931. enum: ['red', 'green', 'blue'],
  1932. description: 'Color choice',
  1933. })
  1934. })
  1935. it('emits default value in JSON Schema property', () => {
  1936. const spec = {
  1937. limit: { type: 'number', default: 25 },
  1938. } satisfies ParameterSchemaSpec
  1939. const jsonSchema = parameterSchemaSpecToJsonSchema(spec)
  1940. expect(jsonSchema.properties['limit']).toMatchObject({
  1941. type: 'number',
  1942. default: 25,
  1943. })
  1944. })
  1945. it('handles array items without nested properties (plain type array)', () => {
  1946. const spec = {
  1947. tags: { type: 'array', items: { type: 'string' } },
  1948. } satisfies ParameterSchemaSpec
  1949. const jsonSchema = parameterSchemaSpecToJsonSchema(spec)
  1950. expect(jsonSchema.properties['tags']).toEqual({
  1951. type: 'array',
  1952. items: { type: 'string' },
  1953. })
  1954. })
  1955. it('handles enum and default together in one property', () => {
  1956. const spec = {
  1957. level: { type: 'string', enum: ['low', 'high'], default: 'low' },
  1958. } satisfies ParameterSchemaSpec
  1959. const jsonSchema = parameterSchemaSpecToJsonSchema(spec)
  1960. expect(jsonSchema.properties['level']).toMatchObject({
  1961. type: 'string',
  1962. enum: ['low', 'high'],
  1963. default: 'low',
  1964. })
  1965. })
  1966. it('omits description, enum, default keys when not specified', () => {
  1967. const spec = {
  1968. bare: { type: 'string' },
  1969. } satisfies ParameterSchemaSpec
  1970. const jsonSchema = parameterSchemaSpecToJsonSchema(spec)
  1971. const prop = jsonSchema.properties['bare'] as Record<string, unknown>
  1972. expect(prop).toEqual({ type: 'string' })
  1973. expect('description' in prop).toBe(false)
  1974. expect('enum' in prop).toBe(false)
  1975. expect('default' in prop).toBe(false)
  1976. })
  1977. it('handles array with no items (items omitted)', () => {
  1978. const spec = {
  1979. raw: { type: 'array' },
  1980. } satisfies ParameterSchemaSpec
  1981. const jsonSchema = parameterSchemaSpecToJsonSchema(spec)
  1982. expect(jsonSchema.properties['raw']).toEqual({
  1983. type: 'array',
  1984. })
  1985. })
  1986. it('handles nested object with all-optional properties (no required array)', () => {
  1987. const spec = {
  1988. config: {
  1989. type: 'object',
  1990. additionalProperties: true,
  1991. properties: {
  1992. host: { type: 'string' },
  1993. port: { type: 'number' },
  1994. },
  1995. },
  1996. } satisfies ParameterSchemaSpec
  1997. const jsonSchema = parameterSchemaSpecToJsonSchema(spec)
  1998. expect(jsonSchema.properties['config']).toMatchObject({
  1999. type: 'object',
  2000. properties: {
  2001. host: { type: 'string' },
  2002. port: { type: 'number' },
  2003. },
  2004. })
  2005. const config = jsonSchema.properties['config'] as Record<string, unknown>
  2006. expect('required' in config).toBe(false)
  2007. })
  2008. })
  2009. describe('schema DSL optional and nested contracts', () => {
  2010. it('InferArgs makes non-required keys genuinely optional (omittable)', () => {
  2011. type Args = InferArgs<{
  2012. path: { type: 'string'; required: true }
  2013. limit: { type: 'number' }
  2014. }>
  2015. expectTypeOf<Args>().toEqualTypeOf<{ path: string; limit?: number }>()
  2016. const omitted: Args = { path: '/tmp' }
  2017. expect(omitted.limit).toBeUndefined()
  2018. })
  2019. it('InferArgs recurses into array items, including arrays of objects', () => {
  2020. type Args = InferArgs<{
  2021. names: { type: 'array'; required: true; items: { type: 'string' } }
  2022. servers: {
  2023. type: 'array'
  2024. items: {
  2025. type: 'object'
  2026. additionalProperties: true
  2027. properties: {
  2028. host: { type: 'string'; required: true }
  2029. port: { type: 'number' }
  2030. }
  2031. }
  2032. }
  2033. }>
  2034. expectTypeOf<Args>().toEqualTypeOf<{
  2035. names: string[]
  2036. servers?: ({ host: string; port?: number } & Record<string, JsonValue>)[]
  2037. }>()
  2038. })
  2039. it('runtime JSON Schema matches the array-of-objects inference', () => {
  2040. const spec = {
  2041. servers: {
  2042. type: 'array',
  2043. items: {
  2044. type: 'object',
  2045. additionalProperties: true,
  2046. properties: {
  2047. host: { type: 'string', required: true },
  2048. port: { type: 'number' },
  2049. },
  2050. },
  2051. },
  2052. } satisfies ParameterSchemaSpec
  2053. expect(parameterSchemaSpecToJsonSchema(spec)).toEqual({
  2054. type: 'object',
  2055. properties: {
  2056. servers: {
  2057. type: 'array',
  2058. items: {
  2059. type: 'object',
  2060. additionalProperties: true,
  2061. properties: {
  2062. host: { type: 'string' },
  2063. port: { type: 'number' },
  2064. },
  2065. required: ['host'],
  2066. },
  2067. },
  2068. },
  2069. })
  2070. })
  2071. it('reports messages from non-Error throws (throw { message })', async () => {
  2072. const ctx = await setup()
  2073. ctx.tools.register({
  2074. ...echoTool,
  2075. name: 'object-thrower',
  2076. async execute() {
  2077. // testing non-Error throws on purpose
  2078. throw { message: 'denied by object' }
  2079. },
  2080. })
  2081. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'object-thrower', arguments: {} })
  2082. expect(result.isError).toBe(true)
  2083. expect(result.content[0]).toMatchObject({ text: 'Error: denied by object' })
  2084. })
  2085. it('reports messages from throws of non-objects (throw "string")', async () => {
  2086. const ctx = await setup()
  2087. ctx.tools.register({
  2088. ...echoTool,
  2089. name: 'string-thrower',
  2090. async execute() {
  2091. // testing primitive throws on purpose
  2092. throw 'kaboom'
  2093. },
  2094. })
  2095. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'string-thrower', arguments: {} })
  2096. expect(result.isError).toBe(true)
  2097. expect(result.content[0]).toMatchObject({ text: 'Error: kaboom' })
  2098. })
  2099. it('reports messages from throws of objects without message property', async () => {
  2100. const ctx = await setup()
  2101. ctx.tools.register({
  2102. ...echoTool,
  2103. name: 'object-no-message',
  2104. async execute() {
  2105. // testing object throw without .message
  2106. throw { code: 500 }
  2107. },
  2108. })
  2109. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'object-no-message', arguments: {} })
  2110. expect(result.isError).toBe(true)
  2111. const firstContent = result.content[0]!
  2112. expect(firstContent.type).toBe('text')
  2113. if (firstContent.type === 'text') {
  2114. expect(firstContent.text).toBe('Error: [object Object]')
  2115. }
  2116. })
  2117. })
  2118. describe('ToolRegistry.get', () => {
  2119. it('get() returns the registered tool definition', async () => {
  2120. const ctx = await setup()
  2121. ctx.tools.register(echoTool)
  2122. const tool = ctx.tools.get('echo')
  2123. expect(tool).toBeDefined()
  2124. expect(tool!.name).toBe('echo')
  2125. })
  2126. it('get() returns undefined for unknown tool names', async () => {
  2127. const ctx = await setup()
  2128. expect(ctx.tools.get('nope')).toBeUndefined()
  2129. })
  2130. })
  2131. describe('validateArgs (the runtime-validation Agent Note, part 1)', () => {
  2132. it('returns [] for valid args and is total over malformed input', () => {
  2133. const spec = {
  2134. path: { type: 'string', required: true },
  2135. limit: { type: 'number' },
  2136. } satisfies ParameterSchemaSpec
  2137. expect(validateArgs(spec, { path: '/tmp' })).toEqual([])
  2138. expect(validateArgs(spec, { path: '/tmp', limit: 5 })).toEqual([])
  2139. // never throws regardless of shape
  2140. expect(validateArgs(spec, null)).toHaveLength(1)
  2141. expect(validateArgs(spec, 'nope')).toHaveLength(1)
  2142. expect(validateArgs(spec, [])).toHaveLength(1)
  2143. })
  2144. it('flags a missing required key and a required key present as undefined', () => {
  2145. const spec = { path: { type: 'string', required: true } } satisfies ParameterSchemaSpec
  2146. expect(validateArgs(spec, {})).toEqual(['missing required property "path"'])
  2147. expect(validateArgs(spec, { path: undefined })).toEqual(['missing required property "path"'])
  2148. })
  2149. it('allows extra keys (no additionalProperties:false) and omitted optionals', () => {
  2150. const spec = { path: { type: 'string', required: true } } satisfies ParameterSchemaSpec
  2151. expect(validateArgs(spec, { path: '/tmp', extra: 1 })).toEqual([])
  2152. })
  2153. it('does not apply defaults (validation only)', () => {
  2154. const spec = { limit: { type: 'number', default: 25 } } satisfies ParameterSchemaSpec
  2155. // absent optional is valid, and validation does not synthesize the default
  2156. expect(validateArgs(spec, {})).toEqual([])
  2157. })
  2158. it('type-checks primitives', () => {
  2159. const spec = {
  2160. s: { type: 'string' },
  2161. n: { type: 'number' },
  2162. b: { type: 'boolean' },
  2163. } satisfies ParameterSchemaSpec
  2164. expect(validateArgs(spec, { s: 1 })).toEqual(['"s" must be a string'])
  2165. expect(validateArgs(spec, { n: 'x' })).toEqual(['"n" must be a number'])
  2166. expect(validateArgs(spec, { b: 'x' })).toEqual(['"b" must be a boolean'])
  2167. })
  2168. it('checks enum membership', () => {
  2169. const spec = { color: { type: 'string', enum: ['red', 'green'] } } satisfies ParameterSchemaSpec
  2170. expect(validateArgs(spec, { color: 'red' })).toEqual([])
  2171. expect(validateArgs(spec, { color: 'blue' })).toEqual(['"color" must be one of ["red","green"]'])
  2172. })
  2173. it('enforces type-correct scalar enum declarations', () => {
  2174. const spec = { n: { type: 'number', enum: [1, 2] } } satisfies ParameterSchemaSpec
  2175. expect(validateArgs(spec, { n: 1 })).toEqual([])
  2176. expect(validateArgs(spec, { n: 3 })).toEqual(['"n" must be one of [1,2]'])
  2177. const invalid = { n: { type: 'number', enum: ['1', '2'] } } as unknown as ParameterSchemaSpec
  2178. expect(() => validateArgs(invalid, { n: 1 })).toThrow(JsonSchemaError)
  2179. })
  2180. it('rejects an unknown schema type at the author boundary', () => {
  2181. const spec = { x: { type: 'weird' } } as unknown as ParameterSchemaSpec
  2182. expect(() => validateArgs(spec, { x: 1 })).toThrow(JsonSchemaError)
  2183. })
  2184. it('recurses into nested objects (and an object without properties only type-checks)', () => {
  2185. const spec = {
  2186. config: {
  2187. type: 'object',
  2188. additionalProperties: true,
  2189. required: true,
  2190. properties: { host: { type: 'string', required: true }, port: { type: 'number' } },
  2191. },
  2192. bag: { type: 'object', additionalProperties: true },
  2193. } satisfies ParameterSchemaSpec
  2194. expect(validateArgs(spec, { config: { host: 'h' }, bag: { anything: true } })).toEqual([])
  2195. expect(validateArgs(spec, { config: { port: 9 }, bag: 5 })).toEqual([
  2196. 'missing required property "config.host"',
  2197. '"bag" must be an object',
  2198. ])
  2199. })
  2200. it('recurses into array items (and an array without items only type-checks)', () => {
  2201. const spec = {
  2202. tags: { type: 'array', items: { type: 'string' } },
  2203. raw: { type: 'array' },
  2204. } satisfies ParameterSchemaSpec
  2205. expect(validateArgs(spec, { tags: ['a', 'b'], raw: [1, {}, 'x'] })).toEqual([])
  2206. expect(validateArgs(spec, { tags: ['a', 2] })).toEqual(['"tags[1]" must be a string'])
  2207. // a non-array value for an array-typed prop
  2208. expect(validateArgs(spec, { tags: 'nope' })).toEqual(['"tags" must be an array'])
  2209. })
  2210. it('validates arrays of objects element-wise', () => {
  2211. const spec = {
  2212. servers: {
  2213. type: 'array',
  2214. items: { type: 'object', additionalProperties: true, properties: { host: { type: 'string', required: true } } },
  2215. },
  2216. } satisfies ParameterSchemaSpec
  2217. expect(validateArgs(spec, { servers: [{ host: 'a' }, {}] })).toEqual([
  2218. 'missing required property "servers[1].host"',
  2219. ])
  2220. })
  2221. })
  2222. describe('defineTool validation (the runtime-validation Agent Note, part 1)', () => {
  2223. it('returns an isError result with the violations when the model sends bad args', async () => {
  2224. const ctx = await setup()
  2225. ctx.tools.register(defineContentToolFixture({
  2226. name: 'reader',
  2227. description: 'reads a path',
  2228. parameters: { path: { type: 'string', required: true } },
  2229. async execute(args) {
  2230. return [{ type: 'text', text: args.path }]
  2231. },
  2232. }))
  2233. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'reader', arguments: {} })
  2234. expect(result.isError).toBe(true)
  2235. expect(result.content[0]).toMatchObject({
  2236. text: 'Error: invalid arguments: missing required property "path"',
  2237. })
  2238. })
  2239. it('runs execute normally when args are valid', async () => {
  2240. const ctx = await setup()
  2241. ctx.tools.register(defineContentToolFixture({
  2242. name: 'reader',
  2243. description: 'reads a path',
  2244. parameters: { path: { type: 'string', required: true } },
  2245. async execute(args) {
  2246. return [{ type: 'text', text: `read ${args.path}` }]
  2247. },
  2248. }))
  2249. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'reader', arguments: { path: '/x' } })
  2250. expect(result).toEqual({
  2251. content: [{ type: 'text', text: 'read /x' }],
  2252. isError: false,
  2253. value: [{ type: 'text', text: 'read /x' }],
  2254. })
  2255. })
  2256. it('ToolArgsError carries a stable code and the violation list', () => {
  2257. const err = new ToolArgsError(['missing required property "a"', '"b" must be a number'])
  2258. expect(err).toBeInstanceOf(Error)
  2259. expect(err.name).toBe('ToolArgsError')
  2260. expect(err.code).toBe('INVALID_ARGS')
  2261. expect(err.violations).toEqual(['missing required property "a"', '"b" must be a number'])
  2262. expect(err.message).toBe('invalid arguments: missing required property "a"; "b" must be a number')
  2263. })
  2264. it('a schema-invalid call surfaces the structured error on the result', async () => {
  2265. const ctx = await setup()
  2266. ctx.tools.register(defineContentToolFixture({
  2267. name: 'reader',
  2268. description: 'reads a path',
  2269. parameters: { path: { type: 'string', required: true } },
  2270. async execute(args) {
  2271. return [{ type: 'text', text: args.path }]
  2272. },
  2273. }))
  2274. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'reader', arguments: {} })
  2275. expect(result.isError).toBe(true)
  2276. expect(result.error).toEqual({
  2277. message: 'invalid arguments: missing required property "path"',
  2278. info: { name: 'ToolArgsError', code: 'INVALID_ARGS' },
  2279. })
  2280. })
  2281. it('a tool throwing a HarnessError surfaces its name and code', async () => {
  2282. const { HarnessError } = await import('@deepseek-ai/dsh-llm')
  2283. const ctx = await setup()
  2284. ctx.tools.register({
  2285. ...echoTool,
  2286. name: 'coded',
  2287. async execute() {
  2288. throw new HarnessError('disk full', 'ENOSPC')
  2289. },
  2290. })
  2291. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'coded', arguments: {} })
  2292. expect(result.isError).toBe(true)
  2293. expect(result.error).toEqual({ message: 'disk full', info: { name: 'HarnessError', code: 'ENOSPC' } })
  2294. expect(result.content[0]).toMatchObject({ text: 'Error: disk full' })
  2295. })
  2296. it('a non-HarnessError throw retains only its message', async () => {
  2297. const ctx = await setup()
  2298. ctx.tools.register({
  2299. ...echoTool,
  2300. name: 'plain',
  2301. async execute() {
  2302. throw new Error('just a message')
  2303. },
  2304. })
  2305. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'plain', arguments: {} })
  2306. expect(result.isError).toBe(true)
  2307. expect(result.error).toEqual({ message: 'just a message' })
  2308. expect(result.content[0]).toMatchObject({ text: 'Error: just a message' })
  2309. })
  2310. it('raw-registered tools are NOT validated by defineTool (MCP keeps its own)', async () => {
  2311. const ctx = await setup()
  2312. // A raw ToolDefinition: no defineTool wrapping, so no validateArgs guard.
  2313. ctx.tools.register({
  2314. name: 'raw',
  2315. description: 'raw tool',
  2316. parameters: { type: 'object', properties: { path: { type: 'string' } }, required: ['path'] },
  2317. output: {
  2318. schema: { type: 'string' },
  2319. render: (_args, value) => [{ type: 'text', text: value as string }],
  2320. },
  2321. async execute(args: unknown) {
  2322. return typeof args
  2323. },
  2324. })
  2325. // Missing the "required" path — but raw tools validate their own input, so
  2326. // this reaches execute rather than being rejected by the harness.
  2327. const result = await ctx.tools.execute({ signal: testToolSignal, callId: CallId('c1'), name: 'raw', arguments: {} })
  2328. expect(result.isError).toBe(false)
  2329. })
  2330. it('attaches a positive-finite timeoutMs to the definition', () => {
  2331. const tool = defineContentToolFixture({
  2332. name: 'x', description: 'd', parameters: {}, timeoutMs: 30_000,
  2333. async execute() { return [{ type: 'text' as const, text: 'ok' }] },
  2334. })
  2335. expect(tool.timeoutMs).toBe(30_000)
  2336. })
  2337. it('omits timeoutMs when not declared', () => {
  2338. const tool = defineContentToolFixture({
  2339. name: 'x', description: 'd', parameters: {},
  2340. async execute() { return [{ type: 'text' as const, text: 'ok' }] },
  2341. })
  2342. expect(tool.timeoutMs).toBeUndefined()
  2343. })
  2344. it('throws when timeoutMs is zero or negative', () => {
  2345. const make = (ms: number) => defineContentToolFixture({
  2346. name: 'x', description: 'd', parameters: {}, timeoutMs: ms,
  2347. async execute() { return [{ type: 'text' as const, text: 'ok' }] },
  2348. })
  2349. expect(() => make(0)).toThrow('timeoutMs must be a positive finite number')
  2350. expect(() => make(-5)).toThrow('positive finite number')
  2351. })
  2352. it('throws when timeoutMs is non-finite', () => {
  2353. expect(() => defineContentToolFixture({
  2354. name: 'x', description: 'd', parameters: {}, timeoutMs: Infinity,
  2355. async execute() { return [{ type: 'text' as const, text: 'ok' }] },
  2356. })).toThrow('positive finite number')
  2357. })
  2358. })
  2359. describe('defineTool presentation (presentCall / presentResult)', () => {
  2360. it('preserves inline enum and const literals in inferred arguments', () => {
  2361. defineTool({
  2362. name: 'literal-args',
  2363. description: 'literal arguments',
  2364. parameters: {
  2365. mode: { type: 'string', enum: ['read', 'write'], required: true },
  2366. attempt: { type: 'integer', const: 1 },
  2367. },
  2368. output: {
  2369. schema: { type: 'null' },
  2370. render: () => [],
  2371. },
  2372. async execute(args) {
  2373. expectTypeOf(args).toEqualTypeOf<{ mode: 'read' | 'write'; attempt?: 1 }>()
  2374. return null
  2375. },
  2376. })
  2377. })
  2378. it('threads presentCall/presentResult onto the ToolDefinition with typed args', () => {
  2379. const tool = defineContentToolFixture({
  2380. name: 'demo',
  2381. description: 'demo',
  2382. parameters: { path: { type: 'string', required: true }, n: { type: 'number' } },
  2383. async execute() { return [{ type: 'text', text: 'ok' }] },
  2384. presentCall(args) {
  2385. // args is typed { path: string; n?: number } — zero casts.
  2386. expectTypeOf(args).toEqualTypeOf<{ path: string; n?: number }>()
  2387. return { card: 'generic', title: `Open ${args.path}`, kind: 'read', rawInput: args.path }
  2388. },
  2389. presentResult(args, result) {
  2390. return { card: 'generic', title: `Opened ${args.path}`, content: result.content }
  2391. },
  2392. })
  2393. expect(tool.presentCall!({ path: '/a', n: 2 })).toEqual({ card: 'generic', title: 'Open /a', kind: 'read', rawInput: '/a' })
  2394. expect(tool.presentResult!({ path: '/a' }, { content: [{ type: 'text', text: 'x' }], isError: false }))
  2395. .toEqual({ card: 'generic', title: 'Opened /a', content: [{ type: 'text', text: 'x' }] })
  2396. })
  2397. it('a tool without presentCall/presentResult leaves them undefined (UI falls back generically)', () => {
  2398. const tool = defineContentToolFixture({
  2399. name: 'plain',
  2400. description: 'plain',
  2401. parameters: { x: { type: 'string', required: true } },
  2402. async execute() { return [] },
  2403. })
  2404. expect(typeof tool.presentCall).toBe('undefined')
  2405. expect(typeof tool.presentResult).toBe('undefined')
  2406. })
  2407. it('presentCall/presentResult validate softly: malformed args return undefined, never throw (display runs on replay)', () => {
  2408. const tool = defineContentToolFixture({
  2409. name: 'demo',
  2410. description: 'demo',
  2411. parameters: { path: { type: 'string', required: true } },
  2412. async execute() { return [] },
  2413. presentCall: args => ({ card: 'generic', title: args.path }),
  2414. presentResult: (args, result) => ({ card: 'generic', title: args.path, content: result.content }),
  2415. })
  2416. // Unlike execute (which throws ToolArgsError on a mismatch), the display
  2417. // methods soft-validate and fall back to undefined so a UI never crashes
  2418. // replaying an old/foreign log entry. The ToolDefinition methods take
  2419. // `unknown`, so malformed shapes pass without a cast.
  2420. expect(tool.presentCall?.({})).toBeUndefined()
  2421. expect(tool.presentResult?.({ wrong: 1 }, { content: [], isError: false })).toBeUndefined()
  2422. })
  2423. })