code-mode.spec.ts 85 KB

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