ptc.spec.ts 85 KB

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