ptc.spec.ts 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910
  1. import { describe, expect, it } from 'vitest'
  2. import { Context } from '@deepseek-ai/cordis'
  3. import { createUserMessage, ToolCallId } from '@deepseek-ai/dsh-llm'
  4. import { createScope } from '@deepseek-ai/dsh-scope'
  5. import type { Scope } from '@deepseek-ai/dsh-scope'
  6. import SystemPrompt, { renderPrompt } from '@deepseek-ai/dsh-system-prompt'
  7. import { CodeRuntime } from '@deepseek-ai/dsh-code-runtime'
  8. import type { CodeRunRequest, CodeRunResult } from '@deepseek-ai/dsh-code-runtime'
  9. import ToolRuntime, { CodeRunFailedError, RUN_CODE_NAME, TOOL_ABORTED_BEFORE_DISPATCH, defineContentToolFixture, defineTool } from '@deepseek-ai/dsh-tools'
  10. import type { Config, JsonSchemaNode, PostToolDecision, ToolExecutionResult } from '@deepseek-ai/dsh-tools'
  11. import type { Agent } from '@deepseek-ai/dsh-agent'
  12. import { Session, SessionId } from '@deepseek-ai/dsh-session'
  13. import type { SessionEventMap } from '@deepseek-ai/dsh-session'
  14. import type { JsonValue } from '@deepseek-ai/dsh-util-values'
  15. const testToolSignal = new AbortController().signal
  16. /**
  17. * PTC mode unit tier (per the Agent Note's plan): provider contribution per mode,
  18. * misconfiguration rejections, the run_code dispatch bridge (serialization,
  19. * abort, JSON normalization, error mapping, events, quiescence), and HMR
  20. * safety — all against an in-repo fake runtime, exactly the
  21. * Service Definition / Service Provider / Consumer roles the seam promises.
  22. */
  23. /** A scriptable in-repo CodeRuntime: each test sets `behavior` to drive the bindings however it needs. */
  24. class FakeRuntime extends CodeRuntime {
  25. readonly language: string
  26. readonly isolation = 'fake'
  27. behavior: (request: CodeRunRequest) => Promise<CodeRunResult> = () => Promise.resolve({ logs: [] })
  28. lastRequest?: CodeRunRequest
  29. constructor(ctx: Context, config: { language?: string } = {}) {
  30. super(ctx)
  31. this.language = config.language ?? 'typescript'
  32. }
  33. run(request: CodeRunRequest): Promise<CodeRunResult> {
  34. this.lastRequest = request
  35. return this.behavior(request)
  36. }
  37. }
  38. interface SetupOptions {
  39. mode?: Config['mode']
  40. maxParallelSubCalls?: number
  41. runtime?: false | { language?: string }
  42. toolOrder?: string[]
  43. }
  44. async function setup(options: SetupOptions = {}) {
  45. const ctx = new Context()
  46. await ctx.plugin(SystemPrompt, { ...options.toolOrder ? { toolOrder: options.toolOrder } : {} })
  47. await ctx.plugin(ToolRuntime, { mode: options.mode ?? 'ptc', ...options.maxParallelSubCalls !== undefined ? { maxParallelSubCalls: options.maxParallelSubCalls } : {} })
  48. let runtime: FakeRuntime | undefined
  49. if (options.runtime !== false) {
  50. await ctx.plugin(FakeRuntime, options.runtime ?? {})
  51. runtime = ctx.codeRuntime as FakeRuntime
  52. }
  53. return { ctx, tools: ctx.tools, systemPrompt: ctx.systemPrompt, runtime: runtime! }
  54. }
  55. /** Mint an agent scope configured like production that can register scoped tool policy. */
  56. async function mintAgentScope(ctx: Context, name = 'scoped'): Promise<{ scope: Scope; agent: Agent }> {
  57. const agent = { id: SessionId(name) } as Agent
  58. let scope!: Scope
  59. await ctx.plugin(Object.assign((inner: Context) => { scope = createScope(inner, agent) },
  60. { inject: ['tools', 'systemPrompt'] }))
  61. return { scope, agent }
  62. }
  63. /** Register a trivial echo tool; returns the calls it received. */
  64. function registerEcho(ctx: Context, name = 'echo'): unknown[] {
  65. const calls: unknown[] = []
  66. ctx.tools.register(defineTool({
  67. name,
  68. description: `Echo tool ${name}.`,
  69. parameters: { value: { type: 'string', required: true } },
  70. output: {
  71. schema: { type: 'string' },
  72. render: (_args, value) => [{ type: 'text', text: value }],
  73. },
  74. execute(args) {
  75. calls.push(args)
  76. return Promise.resolve(`${name}:${args.value}`)
  77. },
  78. }))
  79. return calls
  80. }
  81. /** A structural fake of the owning agent: captures session appends. */
  82. function fakeAgent(): { agent: Agent; events: { type: string; data: unknown }[] } {
  83. const events: { type: string; data: unknown }[] = []
  84. const agent = {
  85. session: {
  86. header: { cwd: '/workspace' },
  87. append: (type: string, data: unknown) => { events.push({ type, data }) },
  88. },
  89. } as unknown as Agent
  90. return { agent, events }
  91. }
  92. /** Dispatch run_code through the registry pipeline, as the loop would. */
  93. async function runCode(
  94. ctx: Context,
  95. code: string,
  96. extras: { agent?: Agent; signal?: AbortSignal; description?: string } = {},
  97. ): Promise<ToolExecutionResult> {
  98. return ctx.tools.execute({
  99. signal: testToolSignal,
  100. callId: ToolCallId('call-1'),
  101. name: RUN_CODE_NAME,
  102. arguments: { code, description: extras.description ?? 'Run the test program' },
  103. ...extras.agent ? { agent: extras.agent } : {},
  104. ...extras.signal ? { signal: extras.signal } : {},
  105. })
  106. }
  107. describe('mode-aware wire contribution', () => {
  108. it.each([
  109. { mode: 'ptc', language: 'typescript' },
  110. { mode: 'both', language: 'typescript' },
  111. { mode: 'ptc', language: 'python' },
  112. { mode: 'both', language: 'python' },
  113. ] as const)('preserves literal braces in the $language SDK under $mode', async ({ mode, language }) => {
  114. const { ctx, systemPrompt } = await setup({ mode, runtime: { language } })
  115. try {
  116. systemPrompt.variable('model', () => 'actual-model')
  117. const description = 'Expand {{item}} with {{model}} or {{ model }}.'
  118. ctx.tools.register(defineTool({
  119. name: 'template',
  120. description,
  121. parameters: { value: { type: 'string', description, enum: ['{{item}}', '{{model}}'], required: true } },
  122. output: {
  123. schema: { type: 'string', enum: ['{{item}}', '{{model}}'] },
  124. render: (_args, value) => [{ type: 'text', text: value }],
  125. },
  126. execute: args => Promise.resolve(args.value),
  127. }))
  128. const assembly = await systemPrompt.assemble()
  129. const sdk = assembly.sections.find(section => section.name === 'tools:sdk')
  130. expect(sdk).toBeDefined()
  131. const prompt = renderPrompt(assembly)
  132. expect(prompt).toContain(description)
  133. expect(prompt).toContain(sdk!.text)
  134. expect(prompt).not.toContain('actual-model')
  135. } finally {
  136. await ctx.fiber.dispose()
  137. }
  138. })
  139. it("mode 'native' contributes every schema, no run_code, no SDK section — and needs no runtime", async () => {
  140. const { ctx, systemPrompt } = await setup({ mode: 'native', runtime: false })
  141. registerEcho(ctx)
  142. const assembly = await systemPrompt.assemble()
  143. expect(assembly.tools.map(tool => tool.name)).toEqual(['echo'])
  144. expect(assembly.sections.some(section => section.name === 'tools:sdk')).toBe(false)
  145. })
  146. it("mode 'ptc' contributes exactly [run_code] plus the SDK section declaring the other tools", async () => {
  147. const { ctx, systemPrompt } = await setup({ mode: 'ptc' })
  148. registerEcho(ctx)
  149. const assembly = await systemPrompt.assemble()
  150. expect(assembly.tools.map(tool => tool.name)).toEqual([RUN_CODE_NAME])
  151. const sdk = assembly.sections.find(section => section.name === 'tools:sdk')
  152. expect(sdk?.text).toContain('declare const tools: {')
  153. expect(sdk?.text).toContain('echo: {')
  154. expect(sdk?.text).not.toContain('run_code:')
  155. expect(sdk?.text).not.toContain('tools.bash(')
  156. })
  157. it("mode 'ptc' states the run_code-only rule BEFORE the per-tool guidance that names each tool", async () => {
  158. const { ctx, systemPrompt } = await setup({ mode: 'ptc' })
  159. registerEcho(ctx)
  160. // Stand in for a real tool's guidance section, which names its tool without
  161. // saying how it is reached.
  162. ctx.systemPrompt.section({
  163. name: 'tool:echo',
  164. order: ctx.systemPrompt.getSectionOrder('TOOL_READ'),
  165. text: 'Use the echo tool.',
  166. })
  167. const assembly = await systemPrompt.assemble()
  168. const names = assembly.sections.map(section => section.name)
  169. const rule = assembly.sections.find(section => section.name === 'tools:ptc-only')
  170. expect(rule?.text).toContain(`\`${RUN_CODE_NAME}\` is the only tool you can call directly`)
  171. // The rule is worthless after the guidance it qualifies.
  172. expect(names.indexOf('tools:ptc-only')).toBeLessThan(names.indexOf('tool:echo'))
  173. expect(names.indexOf('tools:ptc-only')).toBeLessThan(names.indexOf('tools:sdk'))
  174. })
  175. it("mode 'both' omits the run_code-only rule, because native calls do execute there", async () => {
  176. const { ctx, systemPrompt } = await setup({ mode: 'both' })
  177. registerEcho(ctx)
  178. const assembly = await systemPrompt.assemble()
  179. // Registered (the deployment is non-native) but empty, so the renderer
  180. // drops it: `both` executes the native call the rule would forbid.
  181. expect(assembly.sections.find(section => section.name === 'tools:ptc-only')?.text).toBe('')
  182. expect(assembly.tools.map(tool => tool.name)).toContain('echo')
  183. })
  184. it('projects deeply nested output schemas into the PTC mode SDK without structured-clone recursion', async () => {
  185. const { ctx, systemPrompt } = await setup({ mode: 'ptc' })
  186. let output: JsonSchemaNode = { type: 'string' }
  187. for (let depth = 0; depth < 5_000; depth++) {
  188. output = { oneOf: [output, { type: 'null' }] }
  189. }
  190. ctx.tools.register({
  191. name: 'deep_output',
  192. description: 'Return a deeply nested output union.',
  193. parameters: { type: 'object', properties: {} },
  194. output: {
  195. schema: output,
  196. render: (_args, value) => [{ type: 'text', text: typeof value === 'string' ? value : 'null' }],
  197. },
  198. execute() { return Promise.resolve('ok') },
  199. })
  200. const assembly = await systemPrompt.assemble()
  201. const sdk = assembly.sections.find(section => section.name === 'tools:sdk')?.text
  202. expect(sdk).toContain('deep_output: Record<string, JsonValue>;')
  203. expect(sdk).toContain('deep_output: string | null')
  204. })
  205. it.each(['ptc', 'both'] as const)('treats expert assembly output as authoritative in mode %s', async (mode) => {
  206. const { ctx, systemPrompt } = await setup({ mode })
  207. registerEcho(ctx)
  208. ctx.on('system-prompt/assemble', async (_assembly, _context, next) => {
  209. const assembly = await next()
  210. return {
  211. ...assembly,
  212. sections: assembly.sections.filter(section => section.name !== 'tools:sdk'),
  213. tools: assembly.tools.filter(tool => tool.name !== RUN_CODE_NAME),
  214. }
  215. }, { prepend: true })
  216. const assembly = await systemPrompt.assemble()
  217. expect(assembly.sections.some(section => section.name === 'tools:sdk')).toBe(false)
  218. expect(assembly.tools.some(tool => tool.name === RUN_CODE_NAME)).toBe(false)
  219. })
  220. it.each(['ptc', 'both'] as const)('lets one scope shadow the default SDK section in mode %s', async (mode) => {
  221. const { ctx, systemPrompt } = await setup({ mode })
  222. registerEcho(ctx)
  223. const { scope, agent } = await mintAgentScope(ctx)
  224. scope.ctx.systemPrompt.section({
  225. name: 'tools:sdk',
  226. order: scope.ctx.systemPrompt.getSectionOrder('TOOLS_SDK'),
  227. text: 'SCOPED SDK',
  228. })
  229. const scoped = await systemPrompt.assemble({ scope: agent })
  230. const global = await systemPrompt.assemble()
  231. expect(scoped.sections.find(section => section.name === 'tools:sdk')?.text).toBe('SCOPED SDK')
  232. expect(global.sections.find(section => section.name === 'tools:sdk')?.text).toContain('declare const tools:')
  233. })
  234. it("mode 'both' contributes every native schema plus run_code, and the SDK section", async () => {
  235. const { ctx, systemPrompt } = await setup({ mode: 'both' })
  236. registerEcho(ctx)
  237. const assembly = await systemPrompt.assemble()
  238. expect(assembly.tools.map(tool => tool.name)).toEqual(['echo', RUN_CODE_NAME])
  239. expect(assembly.sections.some(section => section.name === 'tools:sdk')).toBe(true)
  240. })
  241. it.each(['ptc', 'both'] as const)('keeps the run_code transport outside scoped allow-list filtering in mode %s', async (mode) => {
  242. const { ctx, systemPrompt, runtime } = await setup({ mode })
  243. registerEcho(ctx, 'echo')
  244. registerEcho(ctx, 'hidden')
  245. const { scope, agent } = await mintAgentScope(ctx)
  246. const lift = scope.ctx.tools.restrict({ allow: ['echo'] })
  247. const assembly = await systemPrompt.assemble({ scope: agent })
  248. expect(assembly.tools.map(tool => tool.name)).toEqual(mode === 'ptc'
  249. ? [RUN_CODE_NAME]
  250. : ['echo', RUN_CODE_NAME])
  251. const sdk = assembly.sections.find(section => section.name === 'tools:sdk')?.text
  252. expect(sdk).toContain('echo: {')
  253. expect(sdk).not.toContain('hidden:')
  254. runtime.behavior = request => Promise.resolve({
  255. logs: [],
  256. value: Object.keys(request.bindings[0]!.functions).sort().join(','),
  257. })
  258. const result = await runCode(ctx, 'return Object.keys(tools)', { agent })
  259. expect(result.isError).toBe(false)
  260. expect(result.content).toEqual([{ type: 'text', text: 'echo' }])
  261. lift()
  262. const unrestricted = await systemPrompt.assemble({ scope: agent })
  263. expect(unrestricted.tools.map(tool => tool.name)).toEqual(mode === 'ptc'
  264. ? [RUN_CODE_NAME]
  265. : ['echo', 'hidden', RUN_CODE_NAME])
  266. })
  267. it.each(['ptc', 'both'] as const)('keeps the run_code transport outside scoped deny-list filtering in mode %s', async (mode) => {
  268. const { ctx, systemPrompt, runtime } = await setup({ mode })
  269. registerEcho(ctx, 'denied')
  270. registerEcho(ctx, 'kept')
  271. const { scope, agent } = await mintAgentScope(ctx)
  272. scope.ctx.tools.restrict({ deny: ['denied'] })
  273. const assembly = await systemPrompt.assemble({ scope: agent })
  274. expect(assembly.tools.map(tool => tool.name)).toEqual(mode === 'ptc'
  275. ? [RUN_CODE_NAME]
  276. : ['kept', RUN_CODE_NAME])
  277. const sdk = assembly.sections.find(section => section.name === 'tools:sdk')?.text
  278. expect(sdk).not.toContain('denied:')
  279. expect(sdk).toContain('kept: {')
  280. runtime.behavior = request => Promise.resolve({
  281. logs: [],
  282. value: Object.keys(request.bindings[0]!.functions).sort().join(','),
  283. })
  284. const result = await runCode(ctx, 'return Object.keys(tools)', { agent })
  285. expect(result.isError).toBe(false)
  286. expect(result.content).toEqual([{ type: 'text', text: 'kept' }])
  287. })
  288. it.each(['ptc', 'both'] as const)('reserves run_code against scoped shadows and explicit restrictions in mode %s', async (mode) => {
  289. const { ctx, systemPrompt } = await setup({ mode })
  290. const { scope, agent } = await mintAgentScope(ctx)
  291. const impostor = defineContentToolFixture({
  292. name: RUN_CODE_NAME,
  293. description: 'Scoped impostor.',
  294. parameters: {},
  295. execute: () => Promise.resolve([{ type: 'text' as const, text: 'impostor' }]),
  296. })
  297. expect(() => scope.ctx.tools.register(impostor)).toThrow(/reserved for the PTC mode presentation transport/)
  298. expect(() => ctx.tools.register(impostor)).toThrow(/reserved for the PTC mode presentation transport/)
  299. expect(() => scope.ctx.tools.restrict({ allow: [RUN_CODE_NAME] })).toThrow(/cannot name reserved PTC mode presentation transport/)
  300. expect(() => scope.ctx.tools.restrict({ deny: [RUN_CODE_NAME] })).toThrow(/cannot name reserved PTC mode presentation transport/)
  301. scope.ctx.systemPrompt.section({
  302. name: 'scoped-note',
  303. order: scope.ctx.systemPrompt.getSectionOrder('TOOLS_SDK') - 10,
  304. text: 'safe note',
  305. })
  306. scope.ctx.tools.register(defineContentToolFixture({
  307. name: 'scoped_safe',
  308. description: 'Safe scoped tool.',
  309. parameters: {},
  310. execute: () => Promise.resolve([{ type: 'text' as const, text: 'safe' }]),
  311. }))
  312. const assembly = await systemPrompt.assemble({ scope: agent })
  313. const transports = assembly.tools.filter(tool => tool.name === RUN_CODE_NAME)
  314. expect(transports).toHaveLength(1)
  315. expect(transports[0]?.description).toContain('Execute a TypeScript program')
  316. expect(assembly.sections.find(section => section.name === 'scoped-note')?.text).toBe('safe note')
  317. expect(assembly.sections.find(section => section.name === 'tools:sdk')?.text).toContain('scoped_safe:')
  318. expect(ctx.tools.get(RUN_CODE_NAME, agent)).toBe(ctx.tools.get(RUN_CODE_NAME))
  319. const result = await runCode(ctx, 'return 1', { agent })
  320. expect(result.content).toEqual([{ type: 'text', text: '(run_code completed with no output)' }])
  321. })
  322. it.each(['ptc', 'both'] as const)('keeps run_code in the toolOrder universe without exposing it as a restriction target in mode %s', async (mode) => {
  323. const { ctx, systemPrompt } = await setup({
  324. mode,
  325. toolOrder: [RUN_CODE_NAME, '<unlisted-tools>'],
  326. })
  327. registerEcho(ctx)
  328. const { agent } = await mintAgentScope(ctx)
  329. const assembly = await systemPrompt.assemble({ scope: agent })
  330. expect(assembly.tools.map(tool => tool.name)).toEqual(mode === 'ptc'
  331. ? [RUN_CODE_NAME]
  332. : [RUN_CODE_NAME, 'echo'])
  333. })
  334. it("never exposes run_code to programs, even under mode 'both' (no recursive dispatch path)", async () => {
  335. const { ctx, runtime } = await setup({ mode: 'both' })
  336. registerEcho(ctx)
  337. runtime.behavior = (request) => {
  338. expect(request.bindings[0]!.errorClass).toEqual({
  339. name: 'ToolCallError',
  340. memberNameProperty: 'toolName',
  341. })
  342. const functions = request.bindings[0]!.functions
  343. return Promise.resolve({
  344. logs: [],
  345. value: JSON.stringify({
  346. names: Object.keys(functions).sort(),
  347. // Own-property AND prototype-chain reads both come back empty —
  348. // there is no handle a program could re-enter run_code through.
  349. runCode: String(functions[RUN_CODE_NAME]),
  350. }),
  351. })
  352. }
  353. const result = await runCode(ctx, 'program')
  354. expect(result.isError).toBe(false)
  355. expect(JSON.parse((result.content[0] as { text: string }).text)).toEqual({ names: ['echo'], runCode: 'undefined' })
  356. })
  357. it('renders byte-identical SDK text across consecutive assemblies of an unchanged tool set', async () => {
  358. const { ctx, systemPrompt } = await setup({ mode: 'ptc' })
  359. registerEcho(ctx)
  360. const first = await systemPrompt.assemble()
  361. const second = await systemPrompt.assemble()
  362. const text = (assembly: typeof first) => assembly.sections.find(section => section.name === 'tools:sdk')?.text
  363. expect(text(first)).toBe(text(second))
  364. })
  365. it('rejects every assembly when a non-native mode has no code runtime', async () => {
  366. const { systemPrompt } = await setup({ mode: 'ptc', runtime: false })
  367. await expect(systemPrompt.assemble()).rejects.toThrow(/requires a code runtime/)
  368. })
  369. it('rejects every assembly when the runtime language has no registered SDK renderer', async () => {
  370. const { systemPrompt } = await setup({ mode: 'ptc', runtime: { language: 'ruby' } })
  371. await expect(systemPrompt.assemble()).rejects.toThrow(/no SDK renderer registered for runtime language "ruby"/)
  372. })
  373. it('assembles under a python runtime by picking the Python SDK renderer', async () => {
  374. const { ctx, systemPrompt } = await setup({ mode: 'ptc', runtime: { language: 'python' } })
  375. registerEcho(ctx)
  376. const assembly = await systemPrompt.assemble()
  377. const sdk = assembly.sections.find(section => section.name === 'tools:sdk')
  378. expect(sdk?.text).toContain('class Tools(Protocol):')
  379. expect(sdk?.text).toContain('async def echo(self, args:')
  380. expect(sdk?.text).toContain('top-level `await`')
  381. })
  382. it("assembles under a python runtime in mode 'both' as well, SDK and schema together", async () => {
  383. // `both` reaches the same wireSchemas/requireCodeRuntime/SDK-section code
  384. // as `ptc`, so this pins the mode-by-language matrix rather than a
  385. // separate path — including that the `wireSchemas` projection behind
  386. // `assembly.tools` picks the Python flavor under `both` instead of hitting
  387. // the flavor-table guard.
  388. const { ctx, systemPrompt } = await setup({ mode: 'both', runtime: { language: 'python' } })
  389. registerEcho(ctx)
  390. const assembly = await systemPrompt.assemble()
  391. expect(assembly.sections.find(section => section.name === 'tools:sdk')?.text).toContain('class Tools(Protocol):')
  392. const runCodeSchema = assembly.tools.find(tool => tool.name === RUN_CODE_NAME)
  393. expect(runCodeSchema?.description).toContain('Execute a Python program')
  394. // `both` keeps the native tools alongside run_code; `ptc` does not.
  395. expect(assembly.tools.map(tool => tool.name)).toContain('echo')
  396. })
  397. it('emits a TypeScript-flavored run_code schema under a typescript runtime', async () => {
  398. const { ctx, systemPrompt } = await setup({ mode: 'ptc', runtime: { language: 'typescript' } })
  399. registerEcho(ctx)
  400. const assembly = await systemPrompt.assemble()
  401. const runCodeSchema = assembly.tools.find(tool => tool.name === RUN_CODE_NAME)
  402. expect(runCodeSchema?.description).toContain('Execute a TypeScript program')
  403. expect(runCodeSchema?.description).toContain('BODY of an')
  404. // Both required arguments are named here, not only in the parameter
  405. // schema: prose that describes the call as "pass the program" is what
  406. // leads a model to emit `{code}` alone and fail INVALID_ARGS.
  407. expect(runCodeSchema?.description).toContain('`description`')
  408. const codeParam = (runCodeSchema?.parameters as { properties: { code: { description: string } } }).properties.code
  409. expect(codeParam.description).toBe('The program: the body of an async TypeScript function.')
  410. })
  411. it('emits a Python-flavored run_code schema under a python runtime (matches the SDK language)', async () => {
  412. const { ctx, systemPrompt } = await setup({ mode: 'ptc', runtime: { language: 'python' } })
  413. registerEcho(ctx)
  414. const assembly = await systemPrompt.assemble()
  415. const runCodeSchema = assembly.tools.find(tool => tool.name === RUN_CODE_NAME)
  416. expect(runCodeSchema?.description).toContain('Execute a Python program')
  417. expect(runCodeSchema?.description).toContain('`return <value>`')
  418. expect(runCodeSchema?.description).toContain('`description`')
  419. expect(runCodeSchema?.description).not.toContain('TypeScript')
  420. const codeParam = (runCodeSchema?.parameters as { properties: { code: { description: string } } }).properties.code
  421. expect(codeParam.description).toBe('The program: the body of an async Python function.')
  422. })
  423. it('resolves the run_code schema flavor lazily and fails loud on a language absent from the flavor table', async () => {
  424. // The flavor getter reads the runtime directly (peekRuntime), so it — not
  425. // requireCodeRuntime — owns the flavor-table guard. Keeping
  426. // RUN_CODE_FLAVORS in step with SDK_RENDERERS is the compiler's job (both
  427. // are `satisfies`-checked against CodeSdkLanguage), so what the guard
  428. // covers is a mounted runtime naming a language absent from both tables,
  429. // which throws when the schema is projected. Assembly's
  430. // requireCodeRuntime rejects such a language earlier; this reaches the
  431. // guard on its own.
  432. const { ctx } = await setup({ mode: 'ptc', runtime: { language: 'ruby' } })
  433. const definition = ctx.tools.get(RUN_CODE_NAME)
  434. // Names the known languages, symmetric with the SDK_RENDERERS guard: this
  435. // is the reachable rejection, so it must be at least as diagnosable.
  436. expect(() => definition?.description)
  437. .toThrow(/no run_code schema flavor registered for runtime language "ruby" \(known: "typescript", "python"\)/)
  438. })
  439. it('degrades the run_code flavor to TypeScript when no runtime is mounted', async () => {
  440. // Any reader of the definition without a mounted runtime uses this fallback; the
  441. // shipped one is the tool-catalog generator, which boots the registry under
  442. // `mode: ptc` and reads run_code's schema WITHOUT a runtime. peekRuntime
  443. // returns undefined there, so the flavor getter degrades to the TS default
  444. // rather than throwing. None of those readers feeds a model: assembly goes
  445. // through wireSchemas, which requires a runtime first.
  446. const { ctx } = await setup({ mode: 'ptc', runtime: false })
  447. const definition = ctx.tools.get(RUN_CODE_NAME)
  448. expect(definition?.description).toContain('Execute a TypeScript program')
  449. const params = definition?.parameters as { properties: { code: { description: string } } }
  450. expect(params.properties.code.description).toBe('The program: the body of an async TypeScript function.')
  451. })
  452. it("rejects the assembly when toolOrder names a native tool that mode 'ptc' no longer contributes", async () => {
  453. const { ctx, systemPrompt } = await setup({ mode: 'ptc', toolOrder: ['echo', '<unlisted-tools>'] })
  454. registerEcho(ctx)
  455. await expect(systemPrompt.assemble()).rejects.toThrow(/toolOrder lists unregistered tool "echo"/)
  456. })
  457. it('removes run_code and the SDK section when the registry fiber disposes (HMR safety)', async () => {
  458. const ctx = new Context()
  459. await ctx.plugin(SystemPrompt, {})
  460. await ctx.plugin(FakeRuntime, {})
  461. const fiber = await ctx.plugin(ToolRuntime, { mode: 'ptc' })
  462. expect(ctx.tools.get(RUN_CODE_NAME)).toBeDefined()
  463. await fiber.dispose()
  464. const assembly = await ctx.systemPrompt.assemble()
  465. expect(assembly.tools).toEqual([])
  466. expect(assembly.sections.some(section => section.name === 'tools:sdk')).toBe(false)
  467. })
  468. })
  469. describe('the sub-dispatch scheduler (native concurrency contract)', () => {
  470. /** Register a tool whose calls resolve only when the test releases them; returns live-call telemetry. */
  471. function registerGated(ctx: Context, name: string, concurrencySafe: boolean) {
  472. const gates: (() => void)[] = []
  473. let live = 0
  474. let peak = 0
  475. const order: string[] = []
  476. ctx.tools.register(defineTool({
  477. name,
  478. description: `Gated tool ${name}.`,
  479. parameters: { id: { type: 'string', required: true } },
  480. output: {
  481. schema: { type: 'string' },
  482. render: (_args, value) => [{ type: 'text', text: value }],
  483. },
  484. ...concurrencySafe ? { isConcurrencySafe: () => true } : {},
  485. async execute(args, exec) {
  486. order.push(`start:${args.id}`)
  487. live++
  488. peak = Math.max(peak, live)
  489. // Abort-observing like a real tool: the run-scoped abort releases the
  490. // gate so the bridge's drain reaches quiescence.
  491. await new Promise<void>((release) => {
  492. gates.push(release)
  493. exec.signal.addEventListener('abort', () => { release() }, { once: true })
  494. })
  495. live--
  496. order.push(`end:${args.id}`)
  497. return `${name}:${args.id}`
  498. },
  499. }))
  500. const release = (): void => { gates.shift()?.() }
  501. const releaseAll = (): void => { while (gates.length > 0) gates.shift()!() }
  502. return { order, release, releaseAll, peakLive: () => peak, pending: () => gates.length }
  503. }
  504. it('overlaps concurrency-safe calls under Promise.all and logs a start event per dispatch', async () => {
  505. const { ctx, runtime } = await setup({ mode: 'ptc' })
  506. const gated = registerGated(ctx, 'safe_read', true)
  507. const { agent, events } = fakeAgent()
  508. runtime.behavior = async (request) => {
  509. const tools = request.bindings[0]!.functions
  510. const all = Promise.all([
  511. tools.safe_read!({ id: 'a' }),
  512. tools.safe_read!({ id: 'b' }),
  513. tools.safe_read!({ id: 'c' }),
  514. ])
  515. // All three must be START-able without any completion (overlap proof).
  516. await expect.poll(() => gated.pending()).toBe(3)
  517. gated.releaseAll()
  518. return { logs: [], value: (await all).map(String).join(',') }
  519. }
  520. const result = await runCode(ctx, 'program', { agent })
  521. expect(result.isError).toBe(false)
  522. expect(gated.peakLive()).toBe(3)
  523. if (result.isError) throw new Error('expected success')
  524. expect(result.value).toMatchObject({ result: 'safe_read:a,safe_read:b,safe_read:c' })
  525. // One start per dispatch, paired with its settle by subCallId, starts in submission order.
  526. const starts = events.filter(event => event.type === 'tool/ptc-dispatch-start').map(event => event.data as { subCallId: string })
  527. const settles = events.filter(event => event.type === 'tool/ptc-dispatch').map(event => event.data as { subCallId: string })
  528. expect(starts.map(start => start.subCallId)).toEqual(['call-1:ptc:1', 'call-1:ptc:2', 'call-1:ptc:3'])
  529. expect(new Set(settles.map(settle => settle.subCallId))).toEqual(new Set(starts.map(start => start.subCallId)))
  530. })
  531. it('an exclusive call bars overlap: safe calls drain first, it runs alone, later calls wait', async () => {
  532. const { ctx, runtime } = await setup({ mode: 'ptc' })
  533. const safe = registerGated(ctx, 'safe_read', true)
  534. const unsafe = registerGated(ctx, 'writer', false)
  535. runtime.behavior = async (request) => {
  536. const tools = request.bindings[0]!.functions
  537. const reads = [tools.safe_read!({ id: 'r1' }), tools.safe_read!({ id: 'r2' })]
  538. const write = tools.writer!({ id: 'w' })
  539. const tail = tools.safe_read!({ id: 'r3' })
  540. await expect.poll(() => safe.pending()).toBe(2)
  541. // The exclusive call must NOT have started while the pool is live.
  542. expect(unsafe.pending()).toBe(0)
  543. safe.releaseAll()
  544. await expect.poll(() => unsafe.pending()).toBe(1)
  545. // The trailing safe call must NOT start while the exclusive one runs.
  546. expect(safe.pending()).toBe(0)
  547. unsafe.release()
  548. await expect.poll(() => safe.pending()).toBe(1)
  549. safe.releaseAll()
  550. await Promise.all([...reads, write, tail])
  551. return { logs: [], value: 'ordered' }
  552. }
  553. const result = await runCode(ctx, 'program')
  554. expect(result.isError).toBe(false)
  555. expect(safe.order.slice(0, 2)).toEqual(['start:r1', 'start:r2'])
  556. expect(unsafe.order).toEqual(['start:w', 'end:w'])
  557. // r3 started only after w ended.
  558. expect(safe.order.indexOf('start:r3')).toBeGreaterThan(safe.order.indexOf('end:r1'))
  559. })
  560. it('maxParallelSubCalls caps the overlap window', async () => {
  561. const { ctx, runtime } = await setup({ mode: 'ptc', maxParallelSubCalls: 2 })
  562. const gated = registerGated(ctx, 'safe_read', true)
  563. runtime.behavior = async (request) => {
  564. const tools = request.bindings[0]!.functions
  565. const all = Promise.all([
  566. tools.safe_read!({ id: 'a' }),
  567. tools.safe_read!({ id: 'b' }),
  568. tools.safe_read!({ id: 'c' }),
  569. ])
  570. await expect.poll(() => gated.pending()).toBe(2)
  571. // The third call waits for a slot.
  572. expect(gated.pending()).toBe(2)
  573. gated.release()
  574. await expect.poll(() => gated.pending()).toBe(2)
  575. gated.releaseAll()
  576. await all
  577. return { logs: [], value: 'capped' }
  578. }
  579. const result = await runCode(ctx, 'program')
  580. if (result.isError) console.error('CAP-FAIL:', (result.content[0] as { text: string }).text)
  581. expect(result.isError).toBe(false)
  582. expect(gated.peakLive()).toBe(2)
  583. })
  584. it('a tool unregistered between binding enumeration and dispatch fails as unknown tool', async () => {
  585. const { ctx, runtime } = await setup({ mode: 'ptc' })
  586. const calls: unknown[] = []
  587. const dispose = ctx.tools.register(defineTool({
  588. name: 'ephemeral',
  589. description: 'Unregistered between binding enumeration and dispatch.',
  590. parameters: {},
  591. output: {
  592. schema: { type: 'string' },
  593. render: (_args, value) => [{ type: 'text', text: value }],
  594. },
  595. execute() {
  596. calls.push('ran')
  597. return Promise.resolve('ok')
  598. },
  599. }))
  600. runtime.behavior = async (request) => {
  601. // The binding exists (enumerated at run start); the registry mutation
  602. // makes prepare resolve UNKNOWN_TOOL as a final-result, which commits
  603. // through scheduler.finish (no post-execute).
  604. dispose()
  605. const message = await request.bindings[0]!.functions.ephemeral!({})
  606. .then(() => 'resolved', (error: unknown) => error instanceof Error ? error.message : String(error))
  607. return { logs: [], value: message }
  608. }
  609. const result = await runCode(ctx, 'program')
  610. expect(result.isError).toBe(false)
  611. if (result.isError) throw new Error('expected success')
  612. expect(result.value).toMatchObject({ result: 'unknown tool "ephemeral"' })
  613. expect(calls).toEqual([])
  614. })
  615. it('ordered pre-execute never overlaps: a slow policy on one call delays the next start', async () => {
  616. const { ctx, runtime } = await setup({ mode: 'ptc' })
  617. const gated = registerGated(ctx, 'safe_read', true)
  618. const stages: string[] = []
  619. let releaseGate: (() => void) | undefined
  620. ctx.on('tools/pre-execute', async (preExec, next) => {
  621. if (preExec.name !== 'safe_read') return next()
  622. stages.push(`pre-enter:${String(preExec.callId)}`)
  623. if (releaseGate === undefined) {
  624. // The FIRST call's policy awaits an asynchronous decision.
  625. await new Promise<void>((resolve) => { releaseGate = resolve })
  626. }
  627. stages.push(`pre-exit:${String(preExec.callId)}`)
  628. return next()
  629. })
  630. runtime.behavior = async (request) => {
  631. const tools = request.bindings[0]!.functions
  632. const all = Promise.all([tools.safe_read!({ id: 'a' }), tools.safe_read!({ id: 'b' })])
  633. // Both submissions are in; the second pre-execute must NOT have entered
  634. // while the first is still awaiting its policy decision.
  635. await expect.poll(() => stages.length).toBeGreaterThanOrEqual(1)
  636. expect(stages).toEqual(['pre-enter:call-1:ptc:1'])
  637. releaseGate!()
  638. await expect.poll(() => gated.pending()).toBe(2)
  639. gated.releaseAll()
  640. await all
  641. return { logs: [], value: 'ordered-prepare' }
  642. }
  643. const result = await runCode(ctx, 'program')
  644. expect(result.isError).toBe(false)
  645. expect(stages).toEqual([
  646. 'pre-enter:call-1:ptc:1', 'pre-exit:call-1:ptc:1',
  647. 'pre-enter:call-1:ptc:2', 'pre-exit:call-1:ptc:2',
  648. ])
  649. })
  650. it('an exclusive call holds its barrier through post-execute: the next start waits for the commit', async () => {
  651. const { ctx, runtime } = await setup({ mode: 'ptc' })
  652. const writer = registerGated(ctx, 'writer', false)
  653. const reader = registerGated(ctx, 'safe_read', true)
  654. const stages: string[] = []
  655. let releasePost: (() => void) | undefined
  656. ctx.on('tools/post-execute', async (postExec, _result, next): Promise<PostToolDecision> => {
  657. if (postExec.name === 'writer') {
  658. stages.push('post-enter:writer')
  659. await new Promise<void>((resolve) => { releasePost = resolve })
  660. stages.push('post-exit:writer')
  661. }
  662. return next()
  663. })
  664. runtime.behavior = async (request) => {
  665. const tools = request.bindings[0]!.functions
  666. const w = tools.writer!({ id: 'w' })
  667. const r = tools.safe_read!({ id: 'r' })
  668. await expect.poll(() => writer.pending()).toBe(1)
  669. writer.release()
  670. // The writer's body is done and its async post-execute is running; the
  671. // parallel read must not have STARTED (no pre/body) while the exclusive
  672. // call's pipeline is still open.
  673. await expect.poll(() => stages).toContain('post-enter:writer')
  674. expect(reader.pending()).toBe(0)
  675. releasePost!()
  676. await w
  677. await expect.poll(() => reader.pending()).toBe(1)
  678. reader.releaseAll()
  679. await r
  680. return { logs: [], value: 'barrier-through-commit' }
  681. }
  682. const result = await runCode(ctx, 'program')
  683. expect(result.isError).toBe(false)
  684. expect(stages).toEqual(['post-enter:writer', 'post-exit:writer'])
  685. })
  686. it('run settlement drains a commit already in progress: the settle event is appended inside the turn', async () => {
  687. const { ctx, runtime } = await setup({ mode: 'ptc' })
  688. const gated = registerGated(ctx, 'safe_read', true)
  689. const { agent, events } = fakeAgent()
  690. let releasePost: (() => void) | undefined
  691. ctx.on('tools/post-execute', async (postExec, _result, next): Promise<PostToolDecision> => {
  692. if (postExec.name === 'safe_read') {
  693. await new Promise<void>((resolve) => { releasePost = resolve })
  694. }
  695. return next()
  696. })
  697. runtime.behavior = async (request) => {
  698. // Fire-and-forget: the program returns while the sub-call's async
  699. // post-execute commit is mid-flight.
  700. request.bindings[0]!.functions.safe_read!({ id: 'a' }).catch(() => 'run-over')
  701. await expect.poll(() => gated.pending()).toBe(1)
  702. gated.release()
  703. await expect.poll(() => releasePost !== undefined).toBe(true)
  704. queueMicrotask(() => { releasePost!() })
  705. return { logs: [], value: 'returned-early' }
  706. }
  707. const result = await runCode(ctx, 'program', { agent })
  708. expect(result.isError).toBe(false)
  709. // The drain awaited the in-progress commit: the settle event exists and
  710. // preceded the run_code turn closing (all appends happen inside
  711. // execute()). The run's settlement aborted the sub-call's signal while
  712. // its post-execute was mid-flight, so the native cancellation contract
  713. // replaces the successful outcome with the aborted result — the event is
  714. // still durable and in-turn, which is the invariant under test.
  715. const settles = events.filter(event => event.type === 'tool/ptc-dispatch')
  716. expect(settles).toHaveLength(1)
  717. expect(settles[0]?.data).toMatchObject({ name: 'safe_read', isError: true })
  718. })
  719. it('post-execute and context commitment stay in submission order under out-of-order completion', async () => {
  720. const { ctx, runtime } = await setup({ mode: 'ptc' })
  721. const gated = registerGated(ctx, 'safe_read', true)
  722. const postOrder: string[] = []
  723. ctx.on('tools/post-execute', async (postExec, _result, next): Promise<PostToolDecision> => {
  724. if (postExec.name === 'safe_read') {
  725. postOrder.push(String(postExec.callId))
  726. return {
  727. kind: 'accept' as const,
  728. additionalContexts: [createUserMessage({
  729. content: [{ type: 'text' as const, text: `ctx:${String(postExec.callId)}` }],
  730. source: { kind: 'plugin' as const, plugin: 'order-probe' },
  731. })],
  732. }
  733. }
  734. return next()
  735. })
  736. runtime.behavior = async (request) => {
  737. const tools = request.bindings[0]!.functions
  738. const all = Promise.all([tools.safe_read!({ id: 'a' }), tools.safe_read!({ id: 'b' })])
  739. await expect.poll(() => gated.pending()).toBe(2)
  740. // Complete b FIRST (out of submission order), then a.
  741. gated.release() // releases a (FIFO gate) — invert: release twice reversed is not possible;
  742. gated.releaseAll()
  743. await all
  744. return { logs: [], value: 'ordered-commit' }
  745. }
  746. const result = await runCode(ctx, 'program')
  747. expect(result.isError).toBe(false)
  748. // Post-execute observed submission order regardless of completion interleave.
  749. expect(postOrder).toEqual(['call-1:ptc:1', 'call-1:ptc:2'])
  750. // Deferred contexts reach the outer result in the same order.
  751. expect(result.additionalContexts?.map(c => (c.content[0] as { text: string }).text))
  752. .toEqual(['ctx:call-1:ptc:1', 'ctx:call-1:ptc:2'])
  753. })
  754. it('a queued-unstarted call abandoned by run settlement logs no start event', async () => {
  755. const { ctx, runtime } = await setup({ mode: 'ptc' })
  756. const gated = registerGated(ctx, 'writer', false)
  757. const { agent, events } = fakeAgent()
  758. const abandoned: string[] = []
  759. runtime.behavior = async (request) => {
  760. const tools = request.bindings[0]!.functions
  761. // First exclusive call occupies the pool; the second queues unstarted.
  762. // Both rejections are captured (abandonment fires only at settlement,
  763. // AFTER this program has already failed — awaiting it here would deadlock).
  764. tools.writer!({ id: 'w1' }).catch(() => 'settled-under-abort')
  765. tools.writer!({ id: 'w2' }).catch((error: unknown) => {
  766. abandoned.push(error instanceof Error ? error.message : String(error))
  767. })
  768. await expect.poll(() => gated.pending()).toBe(1)
  769. // Fail the program while w1 is in flight and w2 is queued unstarted.
  770. throw new Error('program failed with a queued call')
  771. }
  772. const result = await runCode(ctx, 'program', { agent })
  773. expect(result.isError).toBe(true)
  774. const starts = events.filter(event => event.type === 'tool/ptc-dispatch-start').map(event => (event.data as { subCallId: string }).subCallId)
  775. const settles = events.filter(event => event.type === 'tool/ptc-dispatch').map(event => (event.data as { subCallId: string }).subCallId)
  776. // w1 started and settled under the abort; w2 never started and never
  777. // settled — no start event, no settle event, binding rejected with the
  778. // abandonment message at drain time.
  779. expect(starts).toEqual(['call-1:ptc:1'])
  780. expect(settles).toEqual(['call-1:ptc:1'])
  781. expect(abandoned).toEqual(['run_code run is over (run_code settled); writer tool call abandoned'])
  782. })
  783. })
  784. describe('the run_code dispatch bridge', () => {
  785. it('bridges tool calls, returns only the curated output, and logs one event per dispatch', async () => {
  786. const { ctx, runtime } = await setup({ mode: 'ptc' })
  787. const calls = registerEcho(ctx)
  788. const { agent, events } = fakeAgent()
  789. runtime.behavior = async (request) => {
  790. const tools = request.bindings[0]!.functions
  791. const first = await tools.echo!({ value: 'one' })
  792. const second = await tools.echo!({ value: 'two' })
  793. if (typeof first !== 'string' || typeof second !== 'string') throw new Error('echo returned a non-string')
  794. return { logs: [`saw ${first}`], value: second }
  795. }
  796. const result = await runCode(ctx, 'const …: string = …', { agent })
  797. expect(result.isError).toBe(false)
  798. if (result.isError) throw new Error('expected run_code success')
  799. expect(result.value).toEqual({ logs: ['saw echo:one'], result: 'echo:two' })
  800. expect(result.content).toEqual([{ type: 'text', text: 'saw echo:one\necho:two' }])
  801. expect(calls).toEqual([{ value: 'one' }, { value: 'two' }])
  802. const dispatches = events.filter(event => event.type === 'tool/ptc-dispatch')
  803. expect(dispatches.map(event => event.data)).toEqual([
  804. {
  805. rootCallId: 'call-1', parentCallId: 'call-1', subCallId: 'call-1:ptc:1', name: 'echo',
  806. arguments: { value: 'one' }, isError: false, content: [{ type: 'text', text: 'echo:one' }],
  807. },
  808. {
  809. rootCallId: 'call-1', parentCallId: 'call-1', subCallId: 'call-1:ptc:2', name: 'echo',
  810. arguments: { value: 'two' }, isError: false, content: [{ type: 'text', text: 'echo:two' }],
  811. },
  812. ])
  813. expect(result.meta).toBeUndefined()
  814. })
  815. it('exposes only an opaque parent token to nested result observers', async () => {
  816. const { ctx, runtime } = await setup({ mode: 'ptc' })
  817. registerEcho(ctx)
  818. runtime.behavior = async (request) => {
  819. await request.bindings[0]!.functions.echo!({ value: 'nested' })
  820. return { logs: [], value: 'done' }
  821. }
  822. // Freeze the nested observer's parent correlation. If that were the live
  823. // outer execution object, the timeout-style wrapper could not restore it.
  824. ctx.on('tools/execute', async (exec, next) => {
  825. if (exec.name !== RUN_CODE_NAME) return next()
  826. const previous = exec.signal
  827. exec.signal = new AbortController().signal
  828. const result = await next()
  829. exec.signal = previous
  830. return result
  831. })
  832. ctx.on('tools/result', (exec) => {
  833. if (exec.parent !== undefined) Object.freeze(exec.parent)
  834. })
  835. const result = await runCode(ctx, 'await tools.echo({ value: "nested" })')
  836. expect(result.isError).toBe(false)
  837. expect(result.content).toEqual([{ type: 'text', text: 'done' }])
  838. })
  839. it('forwards a nested terminal conclusion onto the successful run_code result', async () => {
  840. const { ctx, runtime } = await setup({ mode: 'ptc' })
  841. ctx.tools.register(defineTool({
  842. name: 'finalize',
  843. description: 'Terminal tool.',
  844. parameters: {},
  845. output: {
  846. schema: { type: 'string' },
  847. render: (_args, value) => [{ type: 'text', text: value }],
  848. },
  849. execute(_args, exec) {
  850. exec.concludeTurn()
  851. return Promise.resolve('done')
  852. },
  853. }))
  854. runtime.behavior = async (request) => {
  855. await request.bindings[0]!.functions.finalize!({})
  856. return { logs: [], value: 'program complete' }
  857. }
  858. const concluded = await runCode(ctx, 'await tools.finalize({})')
  859. expect(concluded.isError).toBe(false)
  860. expect(concluded.concludesTurn).toBe(true)
  861. // A policy that converts the nested success into an error strips the
  862. // marker with the result type: the recovering program cannot conclude.
  863. const veto = ctx.on('tools/post-execute', async (exec, _result, next): Promise<PostToolDecision> => {
  864. if (exec.name !== 'finalize') return next()
  865. return { kind: 'block', feedback: [{ type: 'text', text: 'terminal rejected' }] }
  866. })
  867. runtime.behavior = async (request) => {
  868. await request.bindings[0]!.functions.finalize!({}).catch(() => undefined)
  869. return { logs: [], value: 'recovered' }
  870. }
  871. const recovered = await runCode(ctx, 'await tools.finalize({}).catch(() => {})')
  872. veto()
  873. expect(recovered.isError).toBe(false)
  874. expect(recovered.concludesTurn).toBeUndefined()
  875. })
  876. it('serializes Promise.all dispatches: tool executions never overlap, in submission order', async () => {
  877. const { ctx, runtime } = await setup({ mode: 'ptc' })
  878. const intervals: [string, string][] = []
  879. let active = 0
  880. ctx.tools.register(defineTool({
  881. name: 'probe',
  882. description: 'Records execution overlap.',
  883. parameters: { id: { type: 'string', required: true } },
  884. output: {
  885. schema: { type: 'string' },
  886. render: (_args, value) => [{ type: 'text', text: value }],
  887. },
  888. async execute(args) {
  889. active++
  890. expect(active, 'probe executions overlapped').toBe(1)
  891. intervals.push(['enter', args.id])
  892. await new Promise(resolve => setTimeout(resolve, 20))
  893. intervals.push(['exit', args.id])
  894. active--
  895. return args.id
  896. },
  897. }))
  898. runtime.behavior = async (request) => {
  899. const tools = request.bindings[0]!.functions
  900. const values = await Promise.all([tools.probe!({ id: 'a' }), tools.probe!({ id: 'b' }), tools.probe!({ id: 'c' })])
  901. if (!values.every(value => typeof value === 'string')) throw new Error('probe returned a non-string')
  902. return { logs: [], value: values.join(',') }
  903. }
  904. const result = await runCode(ctx, 'program')
  905. expect(result.isError).toBe(false)
  906. expect(intervals).toEqual([
  907. ['enter', 'a'], ['exit', 'a'],
  908. ['enter', 'b'], ['exit', 'b'],
  909. ['enter', 'c'], ['exit', 'c'],
  910. ])
  911. expect(result.content[0]).toEqual({ type: 'text', text: 'a,b,c' })
  912. })
  913. it('rejects the program-side call when the tool errors, with the tool error text', async () => {
  914. const { ctx, runtime } = await setup({ mode: 'ptc' })
  915. ctx.tools.register(defineContentToolFixture({
  916. name: 'fail',
  917. description: 'Always fails.',
  918. parameters: {},
  919. execute(): Promise<never> { return Promise.reject(new Error('deliberate failure')) },
  920. }))
  921. runtime.behavior = async (request) => {
  922. try {
  923. await request.bindings[0]!.functions.fail!({})
  924. return { logs: [], value: 'unreachable' }
  925. } catch (error: unknown) {
  926. return { logs: [], value: `caught: ${error instanceof Error ? error.message : String(error)}` }
  927. }
  928. }
  929. const result = await runCode(ctx, 'program')
  930. expect(result.content[0]).toEqual({ type: 'text', text: 'caught: deliberate failure' })
  931. })
  932. it('a throwing tools/ptc-dispatch-log listener is contained: the original settled content is logged', async () => {
  933. const { ctx, runtime } = await setup({ mode: 'ptc' })
  934. registerEcho(ctx)
  935. ctx.on('tools/ptc-dispatch-log', () => { throw new Error('log-content listener failed') })
  936. const { agent, events } = fakeAgent()
  937. runtime.behavior = async (request) => {
  938. const value = await request.bindings[0]!.functions.echo!({ value: 'x' })
  939. return { logs: [], value: value as string }
  940. }
  941. const result = await runCode(ctx, 'program', { agent })
  942. expect(result.isError).toBe(false)
  943. const settle = events.find(event => event.type === 'tool/ptc-dispatch')
  944. expect(settle?.data).toMatchObject({ name: 'echo', isError: false, content: [{ type: 'text', text: 'echo:x' }] })
  945. })
  946. it('a throwing tools/pre-execute listener settles the sub-call without post-execute', async () => {
  947. const { ctx, runtime } = await setup({ mode: 'ptc' })
  948. const calls = registerEcho(ctx)
  949. const postExecuted: string[] = []
  950. ctx.on('tools/pre-execute', (exec, next) => {
  951. if (exec.name === 'echo') throw new Error('gate exploded')
  952. return next()
  953. })
  954. ctx.on('tools/post-execute', (exec, _result, next): Promise<PostToolDecision> => {
  955. if (exec.name === 'echo') postExecuted.push(exec.name)
  956. return next()
  957. })
  958. const { agent, events } = fakeAgent()
  959. runtime.behavior = async (request) => {
  960. const message = await request.bindings[0]!.functions.echo!({ value: 'x' })
  961. .then(() => 'resolved', (error: unknown) => error instanceof Error ? error.message : String(error))
  962. return { logs: [], value: message }
  963. }
  964. const result = await runCode(ctx, 'program', { agent })
  965. expect(result.isError).toBe(false)
  966. if (result.isError) throw new Error('expected success')
  967. expect(result.value).toMatchObject({ result: 'gate exploded' })
  968. // The pipeline failure is final: the body never ran and post-execute was
  969. // skipped, yet the settle event still carries the error outcome.
  970. expect(calls).toEqual([])
  971. expect(postExecuted).toEqual([])
  972. const settles = events.filter(event => event.type === 'tool/ptc-dispatch')
  973. expect(settles).toHaveLength(1)
  974. expect(settles[0]?.data).toMatchObject({ name: 'echo', isError: true })
  975. })
  976. it('a tools/pre-execute deny reaches the program as a binding rejection', async () => {
  977. const { ctx, runtime } = await setup({ mode: 'ptc' })
  978. registerEcho(ctx)
  979. ctx.on('tools/pre-execute', (exec, next) => {
  980. if (exec.name === 'echo') return Promise.resolve({ kind: 'deny' as const, reason: 'not on my watch' })
  981. return next()
  982. })
  983. runtime.behavior = async (request) => {
  984. try {
  985. await request.bindings[0]!.functions.echo!({ value: 'x' })
  986. return { logs: [], value: 'unreachable' }
  987. } catch (error: unknown) {
  988. return { logs: [], value: `denied: ${error instanceof Error ? error.message : String(error)}` }
  989. }
  990. }
  991. const result = await runCode(ctx, 'program')
  992. expect(result.content[0]?.type).toBe('text')
  993. expect((result.content[0] as { text: string }).text).toContain('not on my watch')
  994. })
  995. it('rejects a binding argument that is not lossless JSON, dispatching nothing', async () => {
  996. const { ctx, runtime } = await setup({ mode: 'ptc' })
  997. const calls = registerEcho(ctx)
  998. const { agent, events } = fakeAgent()
  999. runtime.behavior = async (request) => {
  1000. try {
  1001. await request.bindings[0]!.functions.echo!({ value: 'x', big: 1n })
  1002. return { logs: [], value: 'unreachable' }
  1003. } catch (error: unknown) {
  1004. return { logs: [], value: error instanceof Error ? error.message : String(error) }
  1005. }
  1006. }
  1007. const result = await runCode(ctx, 'program', { agent })
  1008. expect((result.content[0] as { text: string }).text).toContain('lossless JSON')
  1009. expect(calls).toEqual([])
  1010. expect(events.filter(event => event.type === 'tool/ptc-dispatch')).toEqual([])
  1011. })
  1012. it('dispatches and logs independent snapshots of the same lossless JSON value', async () => {
  1013. const { ctx, runtime } = await setup({ mode: 'ptc' })
  1014. const calls = registerEcho(ctx)
  1015. const { agent, events } = fakeAgent()
  1016. runtime.behavior = async (request) => {
  1017. const args = Object.assign(Object.create(null) as Record<string, unknown>, { value: 'x', nested: ['same'] })
  1018. await request.bindings[0]!.functions.echo!(args)
  1019. return { logs: [] }
  1020. }
  1021. await runCode(ctx, 'program', { agent })
  1022. expect(calls).toEqual([{ value: 'x', nested: ['same'] }])
  1023. const dispatch = events.find(event => event.type === 'tool/ptc-dispatch')?.data as SessionEventMap['tool/ptc-dispatch']
  1024. expect(dispatch.arguments).toEqual({ value: 'x', nested: ['same'] })
  1025. })
  1026. it('defers sub-call additionalContexts onto the outer run_code result', async () => {
  1027. const { ctx, runtime } = await setup({ mode: 'ptc' })
  1028. registerEcho(ctx)
  1029. ctx.on('tools/post-execute', (exec, _result, next): Promise<PostToolDecision> => {
  1030. if (exec.name === 'echo') {
  1031. return Promise.resolve({
  1032. kind: 'accept' as const,
  1033. additionalContexts: [createUserMessage({
  1034. content: [{ type: 'text' as const, text: `context for ${exec.callId}` }],
  1035. source: { kind: 'plugin' as const, plugin: 'test' },
  1036. })],
  1037. })
  1038. }
  1039. return next()
  1040. })
  1041. runtime.behavior = async (request) => {
  1042. await request.bindings[0]!.functions.echo!({ value: 'x' })
  1043. await request.bindings[0]!.functions.echo!({ value: 'y' })
  1044. return { logs: [], value: 'done' }
  1045. }
  1046. const result = await runCode(ctx, 'program')
  1047. expect(result.isError).toBe(false)
  1048. expect(result.additionalContexts).toMatchObject([
  1049. {
  1050. role: 'user',
  1051. content: [{ type: 'text', text: 'context for call-1:ptc:1' }],
  1052. source: { kind: 'plugin', plugin: 'test' },
  1053. },
  1054. {
  1055. role: 'user',
  1056. content: [{ type: 'text', text: 'context for call-1:ptc:2' }],
  1057. source: { kind: 'plugin', plugin: 'test' },
  1058. },
  1059. ])
  1060. })
  1061. it('defers image-bearing final sub-call content onto the outer run_code result', async () => {
  1062. const { ctx, runtime } = await setup({ mode: 'ptc' })
  1063. ctx.tools.register(defineContentToolFixture({
  1064. name: 'image_result',
  1065. description: 'Return one durable image.',
  1066. parameters: {},
  1067. execute: () => Promise.resolve([
  1068. { type: 'text', text: 'image result' },
  1069. {
  1070. type: 'image',
  1071. attachment: {
  1072. attachmentId: 'sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' as never,
  1073. mediaType: 'image/png', bytes: 1, width: 1, height: 1,
  1074. },
  1075. },
  1076. ]),
  1077. }))
  1078. runtime.behavior = async (request) => {
  1079. await request.bindings[0]!.functions.image_result!({})
  1080. return { logs: [], value: 'done' }
  1081. }
  1082. const result = await runCode(ctx, 'program')
  1083. expect(result.additionalContexts).toMatchObject([{
  1084. role: 'user',
  1085. source: { kind: 'plugin', plugin: 'tools-ptc' },
  1086. content: [
  1087. { type: 'text', text: 'image result' },
  1088. { type: 'image', attachment: { mediaType: 'image/png', bytes: 1, width: 1, height: 1 } },
  1089. ],
  1090. }])
  1091. })
  1092. it('does not defer images removed by a nested post-execute decision', async () => {
  1093. for (const decision of ['block', 'replace'] as const) {
  1094. const { ctx, runtime } = await setup({ mode: 'ptc' })
  1095. ctx.tools.register(defineContentToolFixture({
  1096. name: 'image_result',
  1097. description: 'Return one durable image.',
  1098. parameters: {},
  1099. execute: () => Promise.resolve([{
  1100. type: 'image',
  1101. attachment: {
  1102. attachmentId: 'sha256:bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb' as never,
  1103. mediaType: 'image/png', bytes: 1, width: 1, height: 1,
  1104. },
  1105. }]),
  1106. }))
  1107. ctx.on('tools/post-execute', (exec, _result, next): Promise<PostToolDecision> => {
  1108. if (exec.name !== 'image_result') return next()
  1109. return Promise.resolve(decision === 'block'
  1110. ? { kind: 'block', feedback: [{ type: 'text', text: 'blocked' }] }
  1111. : { kind: 'accept', content: [{ type: 'text', text: 'replaced' }] })
  1112. })
  1113. runtime.behavior = async (request) => {
  1114. await request.bindings[0]!.functions.image_result!({}).catch(() => undefined)
  1115. return { logs: [], value: 'done' }
  1116. }
  1117. const result = await runCode(ctx, 'program')
  1118. expect(result.additionalContexts).toBeUndefined()
  1119. await ctx.fiber.dispose()
  1120. }
  1121. })
  1122. it('keeps sub-call contexts when run_code fails after the nested dispatch', async () => {
  1123. const { ctx, runtime } = await setup({ mode: 'both' })
  1124. registerEcho(ctx)
  1125. ctx.on('tools/post-execute', (exec, _result, next): Promise<PostToolDecision> => {
  1126. if (exec.name !== 'echo') return next()
  1127. return Promise.resolve({
  1128. kind: 'accept',
  1129. additionalContexts: [createUserMessage({
  1130. content: [{ type: 'text', text: 'nested context' }],
  1131. source: { kind: 'plugin', plugin: 'test' },
  1132. })],
  1133. })
  1134. })
  1135. runtime.behavior = async (request) => {
  1136. await request.bindings[0]!.functions.echo!({ value: 'x' })
  1137. return { logs: [], error: { kind: 'exception', message: 'program failed later' } }
  1138. }
  1139. const result = await runCode(ctx, 'program')
  1140. expect(result.isError).toBe(true)
  1141. expect(result.additionalContexts).toEqual([{
  1142. id: expect.any(String) as unknown,
  1143. role: 'user',
  1144. content: [{ type: 'text', text: 'nested context' }],
  1145. source: { kind: 'plugin', plugin: 'test' },
  1146. }])
  1147. })
  1148. it('converts a failed run into a structured isError result carrying kind, message, and captured logs', async () => {
  1149. const { ctx, runtime } = await setup({ mode: 'ptc' })
  1150. runtime.behavior = () => Promise.resolve({
  1151. logs: ['got this far'],
  1152. error: { kind: 'timeout', message: 'compute budget exhausted (300ms busy)' },
  1153. })
  1154. const result = await runCode(ctx, 'program')
  1155. expect(result.isError).toBe(true)
  1156. expect(result.error).toMatchObject({ info: { name: 'CodeRunFailedError', code: 'CODE_RUN_FAILED' } })
  1157. const text = (result.content[0] as { text: string }).text
  1158. expect(text).toContain('code run failed (timeout)')
  1159. expect(text).toContain('compute budget exhausted')
  1160. expect(text).toContain('got this far')
  1161. })
  1162. it('CodeRunFailedError is a HarnessError with the CODE_RUN_FAILED code', () => {
  1163. const error = new CodeRunFailedError('boom')
  1164. expect(error.code).toBe('CODE_RUN_FAILED')
  1165. expect(error.name).toBe('CodeRunFailedError')
  1166. })
  1167. it('aborting the outer signal aborts the in-flight sub-dispatch and abandons queued ones', async () => {
  1168. const { ctx, runtime } = await setup({ mode: 'ptc' })
  1169. const seen: string[] = []
  1170. let sawAbort = false
  1171. ctx.tools.register(defineContentToolFixture({
  1172. name: 'slow',
  1173. description: 'Slow tool observing its signal.',
  1174. parameters: { id: { type: 'string', required: true } },
  1175. async execute(args, exec) {
  1176. seen.push(args.id)
  1177. await new Promise<void>((resolve) => {
  1178. const timer = setTimeout(resolve, 500)
  1179. exec.signal.addEventListener('abort', () => { sawAbort = true; clearTimeout(timer); resolve() }, { once: true })
  1180. })
  1181. return [{ type: 'text' as const, text: args.id }]
  1182. },
  1183. }))
  1184. const controller = new AbortController()
  1185. runtime.behavior = async (request) => {
  1186. const tools = request.bindings[0]!.functions
  1187. const calls = [tools.slow!({ id: 'first' }).catch(() => 'rejected'), tools.slow!({ id: 'second' }).catch(() => 'rejected')]
  1188. setTimeout(() => { controller.abort('user-cancel') }, 50)
  1189. await Promise.all(calls)
  1190. // A real runtime would be terminated by the abort; the fake honors the
  1191. // contract by reporting the abort as the run failure.
  1192. return { logs: [], error: { kind: 'abort', message: 'user-cancel' } }
  1193. }
  1194. const result = await runCode(ctx, 'program', { signal: controller.signal })
  1195. expect(result.isError).toBe(true)
  1196. expect((result.content[0] as { text: string }).text).toContain('code run failed (abort)')
  1197. expect(seen).toEqual(['first'])
  1198. expect(sawAbort).toBe(true)
  1199. })
  1200. it('a runtime that starts a binding call and then REJECTS still reaches quiescence before returning', async () => {
  1201. const { ctx, runtime } = await setup({ mode: 'ptc' })
  1202. const { agent, events } = fakeAgent()
  1203. let sawAbort = false
  1204. let started!: () => void
  1205. const inFlight = new Promise<void>((resolve) => { started = resolve })
  1206. ctx.tools.register(defineContentToolFixture({
  1207. name: 'slow',
  1208. description: 'Slow tool observing its signal.',
  1209. parameters: { id: { type: 'string', required: true } },
  1210. async execute(args, exec) {
  1211. started()
  1212. await new Promise<void>((resolve) => {
  1213. const timer = setTimeout(resolve, 500)
  1214. exec.signal.addEventListener('abort', () => { sawAbort = true; clearTimeout(timer); resolve() }, { once: true })
  1215. })
  1216. return [{ type: 'text' as const, text: args.id }]
  1217. },
  1218. }))
  1219. runtime.behavior = async (request) => {
  1220. // Start a sub-dispatch, keep its rejection held, and fail the run once the tool is
  1221. // genuinely in flight — a seam error after work has begun.
  1222. request.bindings[0]!.functions.slow!({ id: 'orphan' }).catch(() => 'held')
  1223. await inFlight
  1224. throw new Error('backend exploded')
  1225. }
  1226. const result = await runCode(ctx, 'program', { agent })
  1227. expect(result.isError).toBe(true)
  1228. expect((result.content[0] as { text: string }).text).toContain('backend exploded')
  1229. // Quiescence held: the in-flight sub-dispatch was aborted and its event
  1230. // logged INSIDE the run_code execution, not after it returned.
  1231. expect(sawAbort).toBe(true)
  1232. expect(events.filter(event => event.type === 'tool/ptc-dispatch').map(event => (event.data as { name: string }).name)).toEqual(['slow'])
  1233. })
  1234. it('runs without an owning agent: dispatches work, event logging is skipped', async () => {
  1235. const { ctx, runtime } = await setup({ mode: 'ptc' })
  1236. const calls = registerEcho(ctx)
  1237. runtime.behavior = async (request) => {
  1238. await request.bindings[0]!.functions.echo!({ value: 'x' })
  1239. return { logs: [], value: 'ok' }
  1240. }
  1241. const result = await runCode(ctx, 'program')
  1242. expect(result.isError).toBe(false)
  1243. expect(calls).toEqual([{ value: 'x' }])
  1244. })
  1245. it('executing run_code under a missing runtime is a structured isError, not a crash', async () => {
  1246. const ctx = new Context()
  1247. await ctx.plugin(SystemPrompt, {})
  1248. await ctx.plugin(ToolRuntime, { mode: 'ptc' })
  1249. const result = await runCode(ctx, 'program')
  1250. expect(result.isError).toBe(true)
  1251. expect((result.content[0] as { text: string }).text).toContain('requires a code runtime')
  1252. })
  1253. it('presents the model-authored description as the execute-card title over the program input', async () => {
  1254. const { ctx } = await setup({ mode: 'ptc' })
  1255. const tool = ctx.tools.get(RUN_CODE_NAME)!
  1256. // The description labels the card (the bash description precedent); the
  1257. // program itself remains the expanded raw input.
  1258. expect(tool.presentCall?.({ code: 'return 1', description: 'Return the constant one' })).toEqual({
  1259. card: 'generic',
  1260. title: 'Return the constant one',
  1261. kind: 'execute',
  1262. rawInput: 'return 1',
  1263. })
  1264. })
  1265. it('rejects a whitespace-only description with a structured isError', async () => {
  1266. const { ctx } = await setup({ mode: 'ptc' })
  1267. const result = await runCode(ctx, 'return 1', { description: ' ' })
  1268. expect(result.isError).toBe(true)
  1269. expect((result.content[0] as { text: string }).text).toContain('invalid description')
  1270. })
  1271. it.each([
  1272. ['logs only', { logs: ['printed'] }, 'printed'],
  1273. ['result only', { logs: [], value: 'returned' }, 'returned'],
  1274. ['logs plus result', { logs: ['printed'], value: 'returned' }, 'printed\nreturned'],
  1275. ['no output', { logs: [] }, '(run_code completed with no output)'],
  1276. ] as [string, CodeRunResult, string][])('keeps %s in durable content without a result presenter', async (_name, output, text) => {
  1277. const { ctx, runtime } = await setup({ mode: 'ptc' })
  1278. runtime.behavior = () => Promise.resolve(output)
  1279. const result = await runCode(ctx, 'return 1')
  1280. const tool = ctx.tools.get(RUN_CODE_NAME)!
  1281. expect(result.content).toEqual([{ type: 'text', text }])
  1282. // Presenters keep the pending program title and render this durable content
  1283. // through their generic fallback. Omitting a result view also prevents the
  1284. // host frame from carrying the same raw content a second time.
  1285. expect('presentResult' in tool).toBe(false)
  1286. })
  1287. it('keeps a post-policy spill preview in durable content without a result presenter', async () => {
  1288. const { ctx, runtime } = await setup({ mode: 'ptc' })
  1289. const preview = 'HEAD\n\n(Omitted 100 bytes. Full formatted result stored at: /tmp/run-code.txt.)\n\nTAIL'
  1290. runtime.behavior = () => Promise.resolve({ logs: ['printed'], value: 'returned' })
  1291. ctx.on('tools/post-execute', (exec, _result, next): Promise<PostToolDecision> => {
  1292. if (exec.name !== RUN_CODE_NAME) return next()
  1293. return Promise.resolve({ kind: 'accept', content: [{ type: 'text', text: preview }] })
  1294. })
  1295. const result = await runCode(ctx, 'return 1')
  1296. const tool = ctx.tools.get(RUN_CODE_NAME)!
  1297. expect(result.content).toEqual([{ type: 'text', text: preview }])
  1298. expect('presentResult' in tool).toBe(false)
  1299. })
  1300. it('keeps canonical failure content durable without a result presenter', async () => {
  1301. const { ctx, runtime } = await setup({ mode: 'ptc' })
  1302. runtime.behavior = () => Promise.resolve({
  1303. logs: ['captured before failure'],
  1304. error: { kind: 'output-limit', message: 'outer output exceeded 8 bytes' },
  1305. })
  1306. const result = await runCode(ctx, 'return 1')
  1307. const tool = ctx.tools.get(RUN_CODE_NAME)!
  1308. expect(result.isError).toBe(true)
  1309. expect(result.content).toEqual([{
  1310. type: 'text',
  1311. text: 'Error: code run failed (output-limit): outer output exceeded 8 bytes\nCaptured output:\ncaptured before failure',
  1312. }])
  1313. expect('presentResult' in tool).toBe(false)
  1314. })
  1315. it('logs the complete sub-result content verbatim, non-text blocks and long text included', async () => {
  1316. const { ctx, runtime } = await setup({ mode: 'ptc' })
  1317. const { agent, events } = fakeAgent()
  1318. const long = 'x'.repeat(300)
  1319. ctx.tools.register(defineTool({
  1320. name: 'mixed',
  1321. description: 'Returns mixed content.',
  1322. parameters: {},
  1323. output: {
  1324. schema: { type: 'string' },
  1325. render: () => [
  1326. { type: 'text', text: long },
  1327. { type: 'reasoning', text: 'hidden' },
  1328. ],
  1329. },
  1330. execute() {
  1331. return Promise.resolve('mixed-value')
  1332. },
  1333. }))
  1334. runtime.behavior = async (request) => {
  1335. const value = await request.bindings[0]!.functions.mixed!({})
  1336. return { logs: [], value }
  1337. }
  1338. const result = await runCode(ctx, 'program', { agent })
  1339. expect(result.isError).toBe(false)
  1340. expect((result.content[0] as { text: string }).text).toBe('mixed-value')
  1341. const dispatch = events.find(event => event.type === 'tool/ptc-dispatch')?.data as SessionEventMap['tool/ptc-dispatch']
  1342. expect(dispatch.content).toEqual([
  1343. { type: 'text', text: long },
  1344. { type: 'reasoning', text: 'hidden' },
  1345. ])
  1346. })
  1347. it('rejects undefined, getter-throwing, exotic, and unrepresentable binding arguments before dispatch', async () => {
  1348. const { ctx, runtime } = await setup({ mode: 'ptc' })
  1349. const calls = registerEcho(ctx)
  1350. const { agent, events } = fakeAgent()
  1351. runtime.behavior = async (request) => {
  1352. const echo = request.bindings[0]!.functions.echo!
  1353. const catchMessage = (promise: Promise<unknown>) => promise.then(() => 'resolved', (error: unknown) => error instanceof Error ? error.message : String(error))
  1354. return {
  1355. logs: [],
  1356. value: [
  1357. // Root undefined must reject up front: the event log rejects it as
  1358. // data, and nothing may execute unlogged.
  1359. await catchMessage(echo(undefined)),
  1360. await catchMessage(echo(Object.defineProperty({}, 'bad', { enumerable: true, get() { throw 'raw-throw' } }))),
  1361. await catchMessage(echo(Object.defineProperty({}, 'bad', { enumerable: true, get() { throw new Error('error-throw') } }))),
  1362. await catchMessage(echo(new Date(0))),
  1363. // A bare function is a value JSON cannot represent at all.
  1364. await catchMessage(echo(() => 1)),
  1365. ].join(' | '),
  1366. }
  1367. }
  1368. const result = await runCode(ctx, 'program', { agent })
  1369. const text = (result.content[0] as { text: string }).text
  1370. expect(text).toContain('call the tool with an arguments object')
  1371. expect(text).toContain('lossless JSON: raw-throw')
  1372. expect(text).toContain('lossless JSON: error-throw')
  1373. expect(text.match(/tool arguments must be lossless JSON/g)).toHaveLength(5)
  1374. // None dispatched or logged.
  1375. expect(calls).toEqual([])
  1376. expect(events.filter(event => event.type === 'tool/ptc-dispatch')).toEqual([])
  1377. })
  1378. it('dispatches and durably logs binding arguments deeper than the structured-clone call stack', async () => {
  1379. const { ctx, runtime } = await setup({ mode: 'ptc' })
  1380. const depth = 5_000
  1381. let observedDepth = 0
  1382. let observedLeaf: JsonValue | undefined
  1383. ctx.tools.register(defineTool({
  1384. name: 'deep_args',
  1385. description: 'Measure a deeply nested JSON argument.',
  1386. parameters: { nested: { type: 'json', required: true } },
  1387. output: {
  1388. schema: { type: 'integer' },
  1389. render: (_args, value) => [{ type: 'text', text: String(value) }],
  1390. },
  1391. execute(args) {
  1392. let cursor = args.nested
  1393. while (Array.isArray(cursor)) {
  1394. if (cursor.length !== 1) throw new Error('expected one item per nesting layer')
  1395. observedDepth++
  1396. cursor = cursor[0]!
  1397. }
  1398. observedLeaf = cursor
  1399. return Promise.resolve(observedDepth)
  1400. },
  1401. }))
  1402. const session = Session.create(SessionId('deep-code-arguments'))
  1403. const agent = { session } as Agent
  1404. runtime.behavior = async (request) => {
  1405. let nested: JsonValue = 'leaf'
  1406. for (let index = 0; index < depth; index++) nested = [nested]
  1407. const value = await request.bindings[0]!.functions.deep_args!({ nested })
  1408. return { logs: [], value }
  1409. }
  1410. const result = await runCode(ctx, 'return tools.deep_args(...)', { agent })
  1411. expect(result.isError).toBe(false)
  1412. expect(result.isError ? undefined : result.value).toEqual({ logs: [], result: depth })
  1413. expect({ observedDepth, observedLeaf }).toEqual({ observedDepth: depth, observedLeaf: 'leaf' })
  1414. const dispatch = session.snapshotEvents().find(event => event.type === 'tool/ptc-dispatch')
  1415. if (dispatch === undefined) throw new Error('expected a durable tool/ptc-dispatch event')
  1416. const logged = dispatch.data.arguments as { nested: JsonValue }
  1417. let loggedDepth = 0
  1418. let loggedCursor = logged.nested
  1419. while (Array.isArray(loggedCursor)) {
  1420. if (loggedCursor.length !== 1) throw new Error('expected one logged item per nesting layer')
  1421. loggedDepth++
  1422. loggedCursor = loggedCursor[0]!
  1423. }
  1424. expect({ loggedDepth, loggedCursor }).toEqual({ loggedDepth: depth, loggedCursor: 'leaf' })
  1425. })
  1426. it('gives the tool and durable log the same immutable argument value', async () => {
  1427. const { ctx, runtime } = await setup({ mode: 'ptc' })
  1428. const { agent, events } = fakeAgent()
  1429. let mutationSucceeded: boolean | undefined
  1430. ctx.tools.register(defineContentToolFixture({
  1431. name: 'mutator',
  1432. description: 'Attempts to mutate its args object.',
  1433. parameters: { list: { type: 'array', required: true } },
  1434. execute(args) {
  1435. mutationSucceeded = Reflect.set(args.list, 1, 'injected-by-tool')
  1436. return Promise.resolve([{ type: 'text' as const, text: 'protected' }])
  1437. },
  1438. }))
  1439. runtime.behavior = async (request) => {
  1440. await request.bindings[0]!.functions.mutator!({ list: ['original'] })
  1441. return { logs: [] }
  1442. }
  1443. const result = await runCode(ctx, 'program', { agent })
  1444. expect(result.isError).toBe(false)
  1445. expect(mutationSucceeded).toBe(false)
  1446. const dispatch = events.find(event => event.type === 'tool/ptc-dispatch')?.data as SessionEventMap['tool/ptc-dispatch']
  1447. expect(dispatch.arguments).toEqual({ list: ['original'] })
  1448. })
  1449. it('exposes a tool named __proto__ as an ordinary own binding', async () => {
  1450. const { ctx, runtime } = await setup({ mode: 'ptc' })
  1451. ctx.tools.register(defineTool({
  1452. name: '__proto__',
  1453. description: 'A prototype-colliding tool name.',
  1454. parameters: {},
  1455. output: {
  1456. schema: { type: 'string' },
  1457. render: (_args, value) => [{ type: 'text', text: value }],
  1458. },
  1459. execute() { return Promise.resolve('proto-tool-ok') },
  1460. }))
  1461. runtime.behavior = async (request) => {
  1462. const functions = request.bindings[0]!.functions
  1463. expect(Object.getPrototypeOf(functions)).toBeNull()
  1464. const value = await functions['__proto__']!({})
  1465. return { logs: [], value }
  1466. }
  1467. const result = await runCode(ctx, 'program')
  1468. expect(result.isError).toBe(false)
  1469. expect(result.content[0]).toEqual({ type: 'text', text: 'proto-tool-ok' })
  1470. })
  1471. it('renders every non-string JSON root as pretty JSON while preserving strings raw', async () => {
  1472. const { ctx, runtime } = await setup({ mode: 'ptc' })
  1473. runtime.behavior = () => Promise.resolve({ logs: [], value: { n: 42, ok: true } })
  1474. expect((await runCode(ctx, 'object')).content[0]).toEqual({ type: 'text', text: '{\n "n": 42,\n "ok": true\n}' })
  1475. runtime.behavior = () => Promise.resolve({ logs: [], value: {} })
  1476. expect((await runCode(ctx, 'empty object')).content[0]).toEqual({ type: 'text', text: '{}' })
  1477. const nested = { outer: [{ inner: true }] }
  1478. runtime.behavior = () => Promise.resolve({ logs: [], value: nested })
  1479. expect((await runCode(ctx, 'nested')).content[0]).toEqual({ type: 'text', text: JSON.stringify(nested, null, 2) })
  1480. runtime.behavior = () => Promise.resolve({ logs: [], value: ['x', 2] })
  1481. expect((await runCode(ctx, 'array')).content[0]).toEqual({ type: 'text', text: '[\n "x",\n 2\n]' })
  1482. runtime.behavior = () => Promise.resolve({ logs: [], value: [] })
  1483. expect((await runCode(ctx, 'empty array')).content[0]).toEqual({ type: 'text', text: '[]' })
  1484. runtime.behavior = () => Promise.resolve({ logs: [], value: null })
  1485. expect((await runCode(ctx, 'null')).content[0]).toEqual({ type: 'text', text: 'null' })
  1486. runtime.behavior = () => Promise.resolve({ logs: [], value: 'raw' })
  1487. expect((await runCode(ctx, 'string')).content[0]).toEqual({ type: 'text', text: 'raw' })
  1488. runtime.behavior = () => Promise.resolve({ logs: [] })
  1489. const absent = await runCode(ctx, 'undefined')
  1490. expect(absent.content[0]).toEqual({ type: 'text', text: '(run_code completed with no output)' })
  1491. expect(absent.isError ? undefined : absent.value).toEqual({ logs: [] })
  1492. })
  1493. it('renders deeply nested JSON without recursive traversal or quadratic indentation', async () => {
  1494. const { ctx, runtime } = await setup({ mode: 'ptc' })
  1495. let value: JsonValue = {
  1496. emptyArray: [],
  1497. emptyObject: {},
  1498. pair: ['leaf', 2],
  1499. record: { first: true, second: null },
  1500. }
  1501. for (let depth = 0; depth < 5_000; depth++) value = [value]
  1502. runtime.behavior = () => Promise.resolve({ logs: [], value })
  1503. const result = await runCode(ctx, 'deep result')
  1504. expect(result.isError).toBe(false)
  1505. const text = (result.content[0] as { type: 'text'; text: string }).text
  1506. expect(text.startsWith('[\n [\n [')).toBe(true)
  1507. expect(text).toContain('"leaf"')
  1508. expect(text.endsWith(']')).toBe(true)
  1509. expect(text.length).toBeLessThan(11_000)
  1510. })
  1511. it('short-circuits a pre-aborted outer signal before the code runtime', async () => {
  1512. const { ctx, runtime } = await setup({ mode: 'ptc' })
  1513. const calls = registerEcho(ctx)
  1514. runtime.behavior = (request) => {
  1515. // The fake honors the seam contract for an already-aborted signal.
  1516. if (request.signal?.aborted) return Promise.resolve({ logs: [], error: { kind: 'abort' as const, message: String(request.signal.reason) } })
  1517. return Promise.resolve({ logs: [], value: 'unreachable' })
  1518. }
  1519. const controller = new AbortController()
  1520. controller.abort('too-late')
  1521. const result = await runCode(ctx, 'program', { signal: controller.signal })
  1522. expect(result.isError).toBe(true)
  1523. expect(result).toEqual({
  1524. content: [{ type: 'text', text: 'Error: tool call aborted before dispatch' }],
  1525. isError: true,
  1526. error: {
  1527. message: 'tool call aborted before dispatch',
  1528. info: { name: 'AbortError', code: TOOL_ABORTED_BEFORE_DISPATCH },
  1529. },
  1530. })
  1531. expect(runtime.lastRequest).toBeUndefined()
  1532. expect(calls).toEqual([])
  1533. })
  1534. it('reports cancellation after rejecting a late binding without dispatching it', async () => {
  1535. const { ctx, runtime } = await setup({ mode: 'ptc' })
  1536. const calls = registerEcho(ctx)
  1537. const controller = new AbortController()
  1538. runtime.behavior = async (request) => {
  1539. controller.abort('cancelled-mid-run')
  1540. const message = await request.bindings[0]!.functions.echo!({ value: 'x' })
  1541. .then(() => 'resolved', (error: unknown) => error instanceof Error ? error.message : String(error))
  1542. return { logs: [], value: message }
  1543. }
  1544. const result = await runCode(ctx, 'program', { signal: controller.signal })
  1545. expect(result.isError).toBe(true)
  1546. expect(result.error).toEqual({
  1547. message: 'tool call aborted',
  1548. info: { name: 'AbortError', code: 'ABORTED' },
  1549. })
  1550. expect((result.content[0] as { text: string }).text).toBe('Error: tool call aborted')
  1551. expect(calls).toEqual([])
  1552. })
  1553. it('a tool/ptc-dispatch event never derives a model message', () => {
  1554. const session = Session.create(SessionId('ptc-derive'))
  1555. session.append('user/message', createUserMessage({
  1556. content: [{ type: 'text', text: 'hi' }], source: { kind: 'user' },
  1557. }), { surfaceOp: 'append' })
  1558. session.append('tool/ptc-dispatch', {
  1559. rootCallId: ToolCallId('p1'),
  1560. parentCallId: ToolCallId('p1'),
  1561. subCallId: ToolCallId('p1:ptc:1'),
  1562. name: 'echo',
  1563. arguments: { value: 'x' },
  1564. isError: false,
  1565. content: [{ type: 'text', text: 'echo:x' }],
  1566. })
  1567. const derived = session.deriveMessages()
  1568. expect(derived).toHaveLength(1)
  1569. expect(derived[0]?.role).toBe('user')
  1570. })
  1571. it('direct construction rejects a non-positive parallel sub-call cap at load', async () => {
  1572. const ctx = new Context()
  1573. await ctx.plugin(SystemPrompt, {})
  1574. expect(() => new ToolRuntime(ctx, { mode: 'ptc', maxParallelSubCalls: 0 }))
  1575. .toThrow('maxParallelSubCalls must be a positive integer')
  1576. })
  1577. it('direct construction in PTC mode defaults the parallel sub-call cap', async () => {
  1578. const ctx = new Context()
  1579. await ctx.plugin(SystemPrompt, {})
  1580. const registry = new ToolRuntime(ctx, { mode: 'ptc' })
  1581. expect(registry.get(RUN_CODE_NAME)).toBeDefined()
  1582. })
  1583. it('defaults to native mode under direct construction with no config', async () => {
  1584. const ctx = new Context()
  1585. await ctx.plugin(SystemPrompt, {})
  1586. const registry = new ToolRuntime(ctx)
  1587. expect(registry.get(RUN_CODE_NAME)).toBeUndefined()
  1588. const assembly = await ctx.systemPrompt.assemble()
  1589. expect(assembly.sections.some(section => section.name === 'tools:sdk')).toBe(false)
  1590. })
  1591. it('denies a model-direct native-tool call under PTC mode as UNKNOWN_TOOL', async () => {
  1592. const ctx = new Context()
  1593. await ctx.plugin(SystemPrompt, {})
  1594. const registry = new ToolRuntime(ctx, { mode: 'ptc' })
  1595. registerEcho(ctx, 'write')
  1596. const result = await registry.execute({
  1597. signal: testToolSignal,
  1598. callId: ToolCallId('call-1'),
  1599. name: 'write',
  1600. arguments: { text: 'hello' },
  1601. })
  1602. expect(result.isError).toBe(true)
  1603. expect(result.error?.info).toEqual({ name: 'ToolNotFoundError', code: 'UNKNOWN_TOOL' })
  1604. // The name IS declared to this model, so a bare `unknown tool` reads as a
  1605. // broken deployment. The denial carries the route instead.
  1606. expect(result.error?.message).toBe(
  1607. `unknown tool "write": only \`${RUN_CODE_NAME}\` is callable directly — call \`write\` from inside a \`${RUN_CODE_NAME}\` program instead`,
  1608. )
  1609. })
  1610. it('routes a pre-aborted collapsed call through ABORTED_BEFORE_DISPATCH', async () => {
  1611. const ctx = new Context()
  1612. await ctx.plugin(SystemPrompt, {})
  1613. const registry = new ToolRuntime(ctx, { mode: 'ptc' })
  1614. registerEcho(ctx, 'write')
  1615. const aborted = new AbortController()
  1616. aborted.abort()
  1617. const result = await registry.execute({
  1618. signal: aborted.signal,
  1619. callId: ToolCallId('call-1'),
  1620. name: 'write',
  1621. arguments: { text: 'hello' },
  1622. })
  1623. expect(result.isError).toBe(true)
  1624. expect(result.error?.info?.code).toBe(TOOL_ABORTED_BEFORE_DISPATCH)
  1625. })
  1626. })
  1627. /**
  1628. * Presentation is per agent, because an agent preset composes it: one
  1629. * deployment runs a PTC mode agent beside native ones, and neither may see
  1630. * the other's catalog. The deployment `mode` is the default those agents
  1631. * shadow, not a process-wide fact.
  1632. */
  1633. describe('per-agent presentation', () => {
  1634. it('gives one agent PTC mode while the deployment stays native', async () => {
  1635. const { ctx, systemPrompt } = await setup({ mode: 'native' })
  1636. const calls = registerEcho(ctx)
  1637. const { scope, agent } = await mintAgentScope(ctx)
  1638. scope.ctx.tools.presentAs('ptc')
  1639. const coded = await systemPrompt.assemble({ scope: agent })
  1640. expect(coded.tools.map(tool => tool.name)).toEqual([RUN_CODE_NAME])
  1641. expect(coded.sections.find(section => section.name === 'tools:sdk')?.text)
  1642. .toContain('echo')
  1643. // Announced surface and callable surface must agree for THIS agent, whose
  1644. // mode is its own rather than the deployment's.
  1645. const denied = await ctx.tools.execute({
  1646. signal: testToolSignal,
  1647. callId: ToolCallId('coded-direct'),
  1648. name: 'echo',
  1649. arguments: { value: 'coded' },
  1650. agent,
  1651. })
  1652. expect(denied.error?.info).toEqual({ name: 'ToolNotFoundError', code: 'UNKNOWN_TOOL' })
  1653. expect(calls).toEqual([])
  1654. // The deployment default is untouched: an agent that declared nothing —
  1655. // and the global view behind it — still sees the native catalog.
  1656. const native = await systemPrompt.assemble()
  1657. expect(native.tools.map(tool => tool.name)).toEqual(['echo'])
  1658. expect(native.sections.some(section => section.name === 'tools:sdk')).toBe(false)
  1659. })
  1660. it('inherits a STANDING preset scope\'s mode down the chain, agents beside it unaffected', async () => {
  1661. const { bindScopeParent } = await import('@deepseek-ai/dsh-scope')
  1662. const { ctx, systemPrompt } = await setup({ mode: 'native' })
  1663. const calls = registerEcho(ctx)
  1664. // The preset's standing scope declares once; the agent only PARENTS to it
  1665. // (the per-preset standing mount configuration has no per-agent declaration).
  1666. const standing = await mintAgentScope(ctx, 'preset:ptc-like')
  1667. standing.scope.ctx.tools.presentAs('ptc')
  1668. const joined = await mintAgentScope(ctx, 'joined-agent')
  1669. bindScopeParent(joined.agent, standing.agent)
  1670. const loner = await mintAgentScope(ctx, 'loner-agent')
  1671. expect(ctx.tools.get(RUN_CODE_NAME, joined.agent)).toBeDefined()
  1672. const coded = await systemPrompt.assemble({ scope: joined.agent })
  1673. expect(coded.tools.map(tool => tool.name)).toEqual([RUN_CODE_NAME])
  1674. // Through the EXECUTOR, not just the wire: the deployment default is
  1675. // `native` here, so a collapse predicate reading it instead of this
  1676. // scope's effective mode would announce [run_code] and still execute the
  1677. // native call — the bypass, reopened for exactly the preset composition
  1678. // `dsh-agent-tool-presentation` produces.
  1679. expect(ctx.tools.executionMode({
  1680. signal: testToolSignal,
  1681. callId: ToolCallId('preset-coded-schedule'),
  1682. name: 'echo',
  1683. arguments: { value: 'joined' },
  1684. agent: joined.agent,
  1685. })).toEqual({ kind: 'exclusive' })
  1686. const denied = await ctx.tools.execute({
  1687. signal: testToolSignal,
  1688. callId: ToolCallId('preset-coded-direct'),
  1689. name: 'echo',
  1690. arguments: { value: 'joined' },
  1691. agent: joined.agent,
  1692. })
  1693. expect(denied.error?.info).toEqual({ name: 'ToolNotFoundError', code: 'UNKNOWN_TOOL' })
  1694. expect(calls).toEqual([])
  1695. // A sibling that never parented stays native, as does the global view.
  1696. expect(ctx.tools.get(RUN_CODE_NAME, loner.agent)).toBeUndefined()
  1697. const native = await systemPrompt.assemble({ scope: loner.agent })
  1698. expect(native.tools.map(tool => tool.name)).toEqual(['echo'])
  1699. const allowed = await ctx.tools.execute({
  1700. signal: testToolSignal,
  1701. callId: ToolCallId('native-sibling-direct'),
  1702. name: 'echo',
  1703. arguments: { value: 'loner' },
  1704. agent: loner.agent,
  1705. })
  1706. expect(allowed).toMatchObject({ isError: false, value: 'echo:loner' })
  1707. expect(calls).toEqual([{ value: 'loner' }])
  1708. })
  1709. it('keeps run_code out of a native agent\'s dispatch table', async () => {
  1710. const { ctx } = await setup({ mode: 'native' })
  1711. registerEcho(ctx)
  1712. const coded = await mintAgentScope(ctx, 'coded')
  1713. const plain = await mintAgentScope(ctx, 'plain')
  1714. coded.scope.ctx.tools.presentAs('ptc')
  1715. // Not merely hidden from the prompt: the transport one agent presents must
  1716. // not be dispatchable by another that never presented it.
  1717. expect(ctx.tools.get(RUN_CODE_NAME, coded.agent)).toBeDefined()
  1718. expect(ctx.tools.get(RUN_CODE_NAME, plain.agent)).toBeUndefined()
  1719. expect(ctx.tools.get(RUN_CODE_NAME)).toBeUndefined()
  1720. })
  1721. it('lets an agent opt out of a PTC mode deployment', async () => {
  1722. const { ctx, systemPrompt } = await setup({ mode: 'ptc' })
  1723. registerEcho(ctx)
  1724. const { scope, agent } = await mintAgentScope(ctx)
  1725. scope.ctx.tools.presentAs('native')
  1726. const assembly = await systemPrompt.assemble({ scope: agent })
  1727. expect(assembly.tools.map(tool => tool.name)).toEqual(['echo'])
  1728. // The deployment's global section still reaches this scope; rendering it
  1729. // empty is what keeps the opted-out agent's prompt free of an SDK.
  1730. expect(assembly.sections.find(section => section.name === 'tools:sdk')?.text).toBe('')
  1731. })
  1732. it('restores the deployment default when the agent unloads', async () => {
  1733. const { ctx, systemPrompt } = await setup({ mode: 'native' })
  1734. registerEcho(ctx)
  1735. const { scope, agent } = await mintAgentScope(ctx)
  1736. const dispose = scope.ctx.tools.presentAs('ptc')
  1737. dispose()
  1738. const assembly = await systemPrompt.assemble({ scope: agent })
  1739. expect(assembly.tools.map(tool => tool.name)).toEqual(['echo'])
  1740. expect(assembly.sections.some(section => section.name === 'tools:sdk')).toBe(false)
  1741. })
  1742. it('refuses a second declaration for the same agent', async () => {
  1743. const { ctx } = await setup({ mode: 'native' })
  1744. const { scope } = await mintAgentScope(ctx)
  1745. scope.ctx.tools.presentAs('ptc')
  1746. // Two answers to "which form does the model see" is a contradiction, and
  1747. // silently keeping either one would make the composition unreadable.
  1748. expect(() => scope.ctx.tools.presentAs('both'))
  1749. .toThrow('conflicts with "ptc" already declared')
  1750. })
  1751. it('refuses an unscoped declaration', async () => {
  1752. const { ctx } = await setup({ mode: 'native' })
  1753. expect(() => ctx.tools.presentAs('ptc'))
  1754. .toThrow('requires a scoped context')
  1755. })
  1756. it('reserves run_code even where no agent presents it', async () => {
  1757. const { ctx } = await setup({ mode: 'native' })
  1758. // The name must stay free under a native deployment too: an agent preset
  1759. // mounting later would otherwise collide with whatever took it.
  1760. expect(() => registerEcho(ctx, RUN_CODE_NAME)).toThrow('is reserved')
  1761. })
  1762. it('reports the missing runtime against the agent\'s own mode', async () => {
  1763. const { ctx, systemPrompt } = await setup({ mode: 'native', runtime: false })
  1764. registerEcho(ctx)
  1765. const { scope, agent } = await mintAgentScope(ctx)
  1766. scope.ctx.tools.presentAs('both')
  1767. await expect(systemPrompt.assemble({ scope: agent }))
  1768. .rejects.toThrow('mode "both" requires a code runtime')
  1769. })
  1770. })