code-mode.spec.ts 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  1. import { describe, expect, it } from 'vitest'
  2. import { Context } from 'cordis'
  3. import { 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 ToolRegistry, { CodeRunFailedError, RUN_CODE_NAME, TOOL_ABORTED_BEFORE_DISPATCH, defineContentToolFixture, defineTool } from '@deepseek-ai/dsh-tools'
  10. import type { Config, 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. * interface/implementation/consumer shape 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. runtime?: false | { language?: string }
  40. toolOrder?: string[]
  41. }
  42. async function setup(options: SetupOptions = {}) {
  43. const ctx = new Context()
  44. await ctx.plugin(SystemPrompt, { ...options.toolOrder ? { toolOrder: options.toolOrder } : {} })
  45. await ctx.plugin(ToolRegistry, { mode: options.mode ?? 'code' })
  46. let runtime: FakeRuntime | undefined
  47. if (options.runtime !== false) {
  48. await ctx.plugin(FakeRuntime, options.runtime ?? {})
  49. runtime = ctx.codeRuntime as FakeRuntime
  50. }
  51. return { ctx, tools: ctx.tools, systemPrompt: ctx.systemPrompt, runtime: runtime! }
  52. }
  53. /** Mint one production-shaped agent scope that can register scoped tool policy. */
  54. async function mintAgentScope(ctx: Context, name = 'scoped'): Promise<{ scope: Scope; agent: Agent }> {
  55. const agent = { id: SessionId(name) } as Agent
  56. let scope!: Scope
  57. await ctx.plugin(Object.assign((inner: Context) => { scope = createScope(inner, agent) },
  58. { inject: ['tools', 'systemPrompt'] }))
  59. return { scope, agent }
  60. }
  61. /** Register a trivial echo tool; returns the calls it received. */
  62. function registerEcho(ctx: Context, name = 'echo'): unknown[] {
  63. const calls: unknown[] = []
  64. ctx.tools.register(defineTool({
  65. name,
  66. description: `Echo tool ${name}.`,
  67. parameters: { value: { type: 'string', required: true } },
  68. output: {
  69. schema: { type: 'string' },
  70. render: (_args, value) => [{ type: 'text', text: value }],
  71. },
  72. execute(args) {
  73. calls.push(args)
  74. return Promise.resolve(`${name}:${args.value}`)
  75. },
  76. }))
  77. return calls
  78. }
  79. /** A structural fake of the owning agent: captures session appends. */
  80. function fakeAgent(options: { cwd?: string } = { cwd: '/workspace' }): { agent: Agent; events: { type: string; data: unknown }[] } {
  81. const events: { type: string; data: unknown }[] = []
  82. const agent = {
  83. session: {
  84. header: options.cwd === undefined ? {} : { cwd: options.cwd },
  85. append: (type: string, data: unknown) => { events.push({ type, data }) },
  86. },
  87. } as unknown as Agent
  88. return { agent, events }
  89. }
  90. /** Dispatch run_code through the registry pipeline, as the loop would. */
  91. async function runCode(ctx: Context, code: string, extras: { agent?: Agent; signal?: AbortSignal } = {}): Promise<ToolExecutionResult> {
  92. return ctx.tools.execute({
  93. signal: testToolSignal,
  94. callId: CallId('call-1'),
  95. name: RUN_CODE_NAME,
  96. arguments: { code },
  97. ...extras.agent ? { agent: extras.agent } : {},
  98. ...extras.signal ? { signal: extras.signal } : {},
  99. })
  100. }
  101. describe('mode-aware wire contribution', () => {
  102. it("mode 'native' contributes every schema, no run_code, no SDK section — and needs no runtime", async () => {
  103. const { ctx, systemPrompt } = await setup({ mode: 'native', runtime: false })
  104. registerEcho(ctx)
  105. const assembly = await systemPrompt.assemble()
  106. expect(assembly.tools.map(tool => tool.name)).toEqual(['echo'])
  107. expect(assembly.sections.some(section => section.name === 'tools:sdk')).toBe(false)
  108. })
  109. it("mode 'code' contributes exactly [run_code] plus the SDK section declaring the other tools", async () => {
  110. const { ctx, systemPrompt } = await setup({ mode: 'code' })
  111. registerEcho(ctx)
  112. const assembly = await systemPrompt.assemble()
  113. expect(assembly.tools.map(tool => tool.name)).toEqual([RUN_CODE_NAME])
  114. const sdk = assembly.sections.find(section => section.name === 'tools:sdk')
  115. expect(sdk?.text).toContain('declare const tools: {')
  116. expect(sdk?.text).toContain('echo: {')
  117. expect(sdk?.text).not.toContain('run_code:')
  118. })
  119. it.each(['code', 'both'] as const)('treats expert assembly output as authoritative in mode %s', async (mode) => {
  120. const { ctx, systemPrompt } = await setup({ mode })
  121. registerEcho(ctx)
  122. ctx.on('system-prompt/assemble', async (_assembly, _context, next) => {
  123. const assembly = await next()
  124. return {
  125. ...assembly,
  126. sections: assembly.sections.filter(section => section.name !== 'tools:sdk'),
  127. tools: assembly.tools.filter(tool => tool.name !== RUN_CODE_NAME),
  128. }
  129. }, { prepend: true })
  130. const assembly = await systemPrompt.assemble()
  131. expect(assembly.sections.some(section => section.name === 'tools:sdk')).toBe(false)
  132. expect(assembly.tools.some(tool => tool.name === RUN_CODE_NAME)).toBe(false)
  133. })
  134. it.each(['code', 'both'] as const)('lets one scope shadow the default SDK section in mode %s', async (mode) => {
  135. const { ctx, systemPrompt } = await setup({ mode })
  136. registerEcho(ctx)
  137. const { scope, agent } = await mintAgentScope(ctx)
  138. scope.ctx.systemPrompt.section({ name: 'tools:sdk', order: 150, text: 'SCOPED SDK' })
  139. const scoped = await systemPrompt.assemble({ scope: agent })
  140. const global = await systemPrompt.assemble()
  141. expect(scoped.sections.find(section => section.name === 'tools:sdk')?.text).toBe('SCOPED SDK')
  142. expect(global.sections.find(section => section.name === 'tools:sdk')?.text).toContain('declare const tools:')
  143. })
  144. it("mode 'both' contributes every native schema plus run_code, and the SDK section", async () => {
  145. const { ctx, systemPrompt } = await setup({ mode: 'both' })
  146. registerEcho(ctx)
  147. const assembly = await systemPrompt.assemble()
  148. expect(assembly.tools.map(tool => tool.name)).toEqual(['echo', RUN_CODE_NAME])
  149. expect(assembly.sections.some(section => section.name === 'tools:sdk')).toBe(true)
  150. })
  151. it.each(['code', 'both'] as const)('keeps the run_code transport outside scoped allow-list filtering in mode %s', async (mode) => {
  152. const { ctx, systemPrompt, runtime } = await setup({ mode })
  153. registerEcho(ctx, 'echo')
  154. registerEcho(ctx, 'hidden')
  155. const { scope, agent } = await mintAgentScope(ctx)
  156. const lift = scope.ctx.tools.restrict({ allow: ['echo'] })
  157. const assembly = await systemPrompt.assemble({ scope: agent })
  158. expect(assembly.tools.map(tool => tool.name)).toEqual(mode === 'code'
  159. ? [RUN_CODE_NAME]
  160. : ['echo', RUN_CODE_NAME])
  161. const sdk = assembly.sections.find(section => section.name === 'tools:sdk')?.text
  162. expect(sdk).toContain('echo: {')
  163. expect(sdk).not.toContain('hidden:')
  164. runtime.behavior = request => Promise.resolve({
  165. logs: [],
  166. value: Object.keys(request.bindings[0]!.functions).sort().join(','),
  167. })
  168. const result = await runCode(ctx, 'return Object.keys(tools)', { agent })
  169. expect(result.isError).toBe(false)
  170. expect(result.content).toEqual([{ type: 'text', text: 'echo' }])
  171. lift()
  172. const unrestricted = await systemPrompt.assemble({ scope: agent })
  173. expect(unrestricted.tools.map(tool => tool.name)).toEqual(mode === 'code'
  174. ? [RUN_CODE_NAME]
  175. : ['echo', 'hidden', RUN_CODE_NAME])
  176. })
  177. it.each(['code', 'both'] as const)('keeps the run_code transport outside scoped deny-list filtering in mode %s', async (mode) => {
  178. const { ctx, systemPrompt, runtime } = await setup({ mode })
  179. registerEcho(ctx, 'denied')
  180. registerEcho(ctx, 'kept')
  181. const { scope, agent } = await mintAgentScope(ctx)
  182. scope.ctx.tools.restrict({ deny: ['denied'] })
  183. const assembly = await systemPrompt.assemble({ scope: agent })
  184. expect(assembly.tools.map(tool => tool.name)).toEqual(mode === 'code'
  185. ? [RUN_CODE_NAME]
  186. : ['kept', RUN_CODE_NAME])
  187. const sdk = assembly.sections.find(section => section.name === 'tools:sdk')?.text
  188. expect(sdk).not.toContain('denied:')
  189. expect(sdk).toContain('kept: {')
  190. runtime.behavior = request => Promise.resolve({
  191. logs: [],
  192. value: Object.keys(request.bindings[0]!.functions).sort().join(','),
  193. })
  194. const result = await runCode(ctx, 'return Object.keys(tools)', { agent })
  195. expect(result.isError).toBe(false)
  196. expect(result.content).toEqual([{ type: 'text', text: 'kept' }])
  197. })
  198. it.each(['code', 'both'] as const)('reserves run_code against scoped shadows and explicit restrictions in mode %s', async (mode) => {
  199. const { ctx, systemPrompt } = await setup({ mode })
  200. const { scope, agent } = await mintAgentScope(ctx)
  201. const impostor = defineContentToolFixture({
  202. name: RUN_CODE_NAME,
  203. description: 'Scoped impostor.',
  204. parameters: {},
  205. execute: () => Promise.resolve([{ type: 'text' as const, text: 'impostor' }]),
  206. })
  207. expect(() => scope.ctx.tools.register(impostor)).toThrow(/reserved for the Code Mode presentation transport/)
  208. expect(() => ctx.tools.register(impostor)).toThrow(/reserved for the Code Mode presentation transport/)
  209. expect(() => scope.ctx.tools.restrict({ allow: [RUN_CODE_NAME] })).toThrow(/cannot name reserved Code Mode presentation transport/)
  210. expect(() => scope.ctx.tools.restrict({ deny: [RUN_CODE_NAME] })).toThrow(/cannot name reserved Code Mode presentation transport/)
  211. scope.ctx.systemPrompt.section({ name: 'scoped-note', order: 149, text: 'safe note' })
  212. scope.ctx.tools.register(defineContentToolFixture({
  213. name: 'scoped_safe',
  214. description: 'Safe scoped tool.',
  215. parameters: {},
  216. execute: () => Promise.resolve([{ type: 'text' as const, text: 'safe' }]),
  217. }))
  218. const assembly = await systemPrompt.assemble({ scope: agent })
  219. const transports = assembly.tools.filter(tool => tool.name === RUN_CODE_NAME)
  220. expect(transports).toHaveLength(1)
  221. expect(transports[0]?.description).toContain('Execute a TypeScript program')
  222. expect(assembly.sections.find(section => section.name === 'scoped-note')?.text).toBe('safe note')
  223. expect(assembly.sections.find(section => section.name === 'tools:sdk')?.text).toContain('scoped_safe:')
  224. expect(ctx.tools.get(RUN_CODE_NAME, agent)).toBe(ctx.tools.get(RUN_CODE_NAME))
  225. const result = await runCode(ctx, 'return 1', { agent })
  226. expect(result.content).toEqual([{ type: 'text', text: '(run_code completed with no output)' }])
  227. })
  228. 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) => {
  229. const { ctx, systemPrompt } = await setup({
  230. mode,
  231. toolOrder: [RUN_CODE_NAME, '<unlisted-tools>'],
  232. })
  233. registerEcho(ctx)
  234. const { agent } = await mintAgentScope(ctx)
  235. const assembly = await systemPrompt.assemble({ scope: agent })
  236. expect(assembly.tools.map(tool => tool.name)).toEqual(mode === 'code'
  237. ? [RUN_CODE_NAME]
  238. : [RUN_CODE_NAME, 'echo'])
  239. })
  240. it("never exposes run_code to programs, even under mode 'both' (no recursive dispatch path)", async () => {
  241. const { ctx, runtime } = await setup({ mode: 'both' })
  242. registerEcho(ctx)
  243. runtime.behavior = (request) => {
  244. const functions = request.bindings[0]!.functions
  245. return Promise.resolve({
  246. logs: [],
  247. value: JSON.stringify({
  248. names: Object.keys(functions).sort(),
  249. // Own-property AND prototype-chain reads both come back empty —
  250. // there is no handle a program could re-enter run_code through.
  251. runCode: String(functions[RUN_CODE_NAME]),
  252. }),
  253. })
  254. }
  255. const result = await runCode(ctx, 'program')
  256. expect(result.isError).toBe(false)
  257. expect(JSON.parse((result.content[0] as { text: string }).text)).toEqual({ names: ['echo'], runCode: 'undefined' })
  258. })
  259. it('renders byte-identical SDK text across consecutive assemblies of an unchanged tool set', async () => {
  260. const { ctx, systemPrompt } = await setup({ mode: 'code' })
  261. registerEcho(ctx)
  262. const first = await systemPrompt.assemble()
  263. const second = await systemPrompt.assemble()
  264. const text = (assembly: typeof first) => assembly.sections.find(section => section.name === 'tools:sdk')?.text
  265. expect(text(first)).toBe(text(second))
  266. })
  267. it('rejects every assembly when a non-native mode has no code runtime', async () => {
  268. const { systemPrompt } = await setup({ mode: 'code', runtime: false })
  269. await expect(systemPrompt.assemble()).rejects.toThrow(/requires a code runtime/)
  270. })
  271. it("rejects every assembly when the runtime's language is not typescript", async () => {
  272. const { systemPrompt } = await setup({ mode: 'code', runtime: { language: 'python' } })
  273. await expect(systemPrompt.assemble()).rejects.toThrow(/language is "python"/)
  274. })
  275. it("rejects the assembly when toolOrder names a native tool that mode 'code' no longer contributes", async () => {
  276. const { ctx, systemPrompt } = await setup({ mode: 'code', toolOrder: ['echo', '<unlisted-tools>'] })
  277. registerEcho(ctx)
  278. await expect(systemPrompt.assemble()).rejects.toThrow(/toolOrder lists unregistered tool "echo"/)
  279. })
  280. it('removes run_code and the SDK section when the registry fiber disposes (HMR safety)', async () => {
  281. const ctx = new Context()
  282. await ctx.plugin(SystemPrompt, {})
  283. await ctx.plugin(FakeRuntime, {})
  284. const fiber = await ctx.plugin(ToolRegistry, { mode: 'code' })
  285. expect(ctx.tools.get(RUN_CODE_NAME)).toBeDefined()
  286. await fiber.dispose()
  287. const assembly = await ctx.systemPrompt.assemble()
  288. expect(assembly.tools).toEqual([])
  289. expect(assembly.sections.some(section => section.name === 'tools:sdk')).toBe(false)
  290. })
  291. })
  292. describe('the run_code dispatch bridge', () => {
  293. it('bridges tool calls, returns only the curated output, and logs one event per dispatch', async () => {
  294. const { ctx, runtime } = await setup({ mode: 'code' })
  295. const calls = registerEcho(ctx)
  296. const { agent, events } = fakeAgent()
  297. runtime.behavior = async (request) => {
  298. const tools = request.bindings[0]!.functions
  299. const first = await tools.echo!({ value: 'one' })
  300. const second = await tools.echo!({ value: 'two' })
  301. if (typeof first !== 'string' || typeof second !== 'string') throw new Error('echo returned a non-string')
  302. return { logs: [`saw ${first}`], value: second }
  303. }
  304. const result = await runCode(ctx, 'const …: string = …', { agent })
  305. expect(result.isError).toBe(false)
  306. if (result.isError) throw new Error('expected run_code success')
  307. expect(result.value).toEqual({ logs: ['saw echo:one'], result: 'echo:two' })
  308. expect(result.content).toEqual([{ type: 'text', text: 'saw echo:one\necho:two' }])
  309. expect(calls).toEqual([{ value: 'one' }, { value: 'two' }])
  310. const dispatches = events.filter(event => event.type === 'tool/code-dispatch')
  311. expect(dispatches.map(event => event.data)).toEqual([
  312. { parentCallId: 'call-1', subCallId: 'call-1:code:1', name: 'echo', arguments: { value: 'one' }, isError: false, resultSummary: 'echo:one' },
  313. { parentCallId: 'call-1', subCallId: 'call-1:code:2', name: 'echo', arguments: { value: 'two' }, isError: false, resultSummary: 'echo:two' },
  314. ])
  315. expect(result.meta).toEqual({ logs: ['saw echo:one'] })
  316. })
  317. it('exposes only an opaque parent token to nested result observers', async () => {
  318. const { ctx, runtime } = await setup({ mode: 'code' })
  319. registerEcho(ctx)
  320. runtime.behavior = async (request) => {
  321. await request.bindings[0]!.functions.echo!({ value: 'nested' })
  322. return { logs: [], value: 'done' }
  323. }
  324. // Freeze the nested observer's parent correlation. If that were the live
  325. // outer execution object, the timeout-style wrapper could not restore it.
  326. ctx.on('tools/execute', async (exec, next) => {
  327. if (exec.name !== RUN_CODE_NAME) return next()
  328. const previous = exec.signal
  329. exec.signal = new AbortController().signal
  330. const result = await next()
  331. exec.signal = previous
  332. return result
  333. })
  334. ctx.on('tools/result', (exec) => {
  335. if (exec.parent !== undefined) Object.freeze(exec.parent)
  336. })
  337. const result = await runCode(ctx, 'await tools.echo({ value: "nested" })')
  338. expect(result.isError).toBe(false)
  339. expect(result.content).toEqual([{ type: 'text', text: 'done' }])
  340. })
  341. it('serializes Promise.all dispatches: tool executions never overlap, in submission order', async () => {
  342. const { ctx, runtime } = await setup({ mode: 'code' })
  343. const intervals: [string, string][] = []
  344. let active = 0
  345. ctx.tools.register(defineTool({
  346. name: 'probe',
  347. description: 'Records execution overlap.',
  348. parameters: { id: { type: 'string', required: true } },
  349. output: {
  350. schema: { type: 'string' },
  351. render: (_args, value) => [{ type: 'text', text: value }],
  352. },
  353. async execute(args) {
  354. active++
  355. expect(active, 'probe executions overlapped').toBe(1)
  356. intervals.push(['enter', args.id])
  357. await new Promise(resolve => setTimeout(resolve, 20))
  358. intervals.push(['exit', args.id])
  359. active--
  360. return args.id
  361. },
  362. }))
  363. runtime.behavior = async (request) => {
  364. const tools = request.bindings[0]!.functions
  365. const values = await Promise.all([tools.probe!({ id: 'a' }), tools.probe!({ id: 'b' }), tools.probe!({ id: 'c' })])
  366. if (!values.every(value => typeof value === 'string')) throw new Error('probe returned a non-string')
  367. return { logs: [], value: values.join(',') }
  368. }
  369. const result = await runCode(ctx, 'program')
  370. expect(result.isError).toBe(false)
  371. expect(intervals).toEqual([
  372. ['enter', 'a'], ['exit', 'a'],
  373. ['enter', 'b'], ['exit', 'b'],
  374. ['enter', 'c'], ['exit', 'c'],
  375. ])
  376. expect(result.content[0]).toEqual({ type: 'text', text: 'a,b,c' })
  377. })
  378. it('rejects the program-side call when the tool errors, with the tool error text', async () => {
  379. const { ctx, runtime } = await setup({ mode: 'code' })
  380. ctx.tools.register(defineContentToolFixture({
  381. name: 'fail',
  382. description: 'Always fails.',
  383. parameters: {},
  384. execute(): Promise<never> { return Promise.reject(new Error('deliberate failure')) },
  385. }))
  386. runtime.behavior = async (request) => {
  387. try {
  388. await request.bindings[0]!.functions.fail!({})
  389. return { logs: [], value: 'unreachable' }
  390. } catch (error: unknown) {
  391. return { logs: [], value: `caught: ${error instanceof Error ? error.message : String(error)}` }
  392. }
  393. }
  394. const result = await runCode(ctx, 'program')
  395. expect(result.content[0]).toEqual({ type: 'text', text: 'caught: deliberate failure' })
  396. })
  397. it('a tools/pre-execute deny reaches the program as a binding rejection', async () => {
  398. const { ctx, runtime } = await setup({ mode: 'code' })
  399. registerEcho(ctx)
  400. ctx.on('tools/pre-execute', (exec, next) => {
  401. if (exec.name === 'echo') return Promise.resolve({ kind: 'deny' as const, reason: 'not on my watch' })
  402. return next()
  403. })
  404. runtime.behavior = async (request) => {
  405. try {
  406. await request.bindings[0]!.functions.echo!({ value: 'x' })
  407. return { logs: [], value: 'unreachable' }
  408. } catch (error: unknown) {
  409. return { logs: [], value: `denied: ${error instanceof Error ? error.message : String(error)}` }
  410. }
  411. }
  412. const result = await runCode(ctx, 'program')
  413. expect(result.content[0]?.type).toBe('text')
  414. expect((result.content[0] as { text: string }).text).toContain('not on my watch')
  415. })
  416. it('rejects a binding argument that is not lossless JSON, dispatching nothing', async () => {
  417. const { ctx, runtime } = await setup({ mode: 'code' })
  418. const calls = registerEcho(ctx)
  419. const { agent, events } = fakeAgent()
  420. runtime.behavior = async (request) => {
  421. try {
  422. await request.bindings[0]!.functions.echo!({ value: 'x', big: 1n })
  423. return { logs: [], value: 'unreachable' }
  424. } catch (error: unknown) {
  425. return { logs: [], value: error instanceof Error ? error.message : String(error) }
  426. }
  427. }
  428. const result = await runCode(ctx, 'program', { agent })
  429. expect((result.content[0] as { text: string }).text).toContain('lossless JSON')
  430. expect(calls).toEqual([])
  431. expect(events.filter(event => event.type === 'tool/code-dispatch')).toEqual([])
  432. })
  433. it('dispatches and logs independent snapshots of the same lossless JSON value', async () => {
  434. const { ctx, runtime } = await setup({ mode: 'code' })
  435. const calls = registerEcho(ctx)
  436. const { agent, events } = fakeAgent()
  437. runtime.behavior = async (request) => {
  438. const args = Object.assign(Object.create(null) as Record<string, unknown>, { value: 'x', nested: ['same'] })
  439. await request.bindings[0]!.functions.echo!(args)
  440. return { logs: [] }
  441. }
  442. await runCode(ctx, 'program', { agent })
  443. expect(calls).toEqual([{ value: 'x', nested: ['same'] }])
  444. const dispatch = events.find(event => event.type === 'tool/code-dispatch')?.data as SessionEventMap['tool/code-dispatch']
  445. expect(dispatch.arguments).toEqual({ value: 'x', nested: ['same'] })
  446. })
  447. it('defers sub-call additionalContexts onto the outer run_code result', async () => {
  448. const { ctx, runtime } = await setup({ mode: 'code' })
  449. registerEcho(ctx)
  450. ctx.on('tools/post-execute', (exec, _result, next): Promise<PostToolDecision> => {
  451. if (exec.name === 'echo') {
  452. return Promise.resolve({
  453. kind: 'accept' as const,
  454. additionalContexts: [{
  455. content: [{ type: 'text' as const, text: `context for ${exec.callId}` }],
  456. source: { kind: 'plugin' as const, plugin: 'test' },
  457. meta: { callId: exec.callId },
  458. }],
  459. })
  460. }
  461. return next()
  462. })
  463. runtime.behavior = async (request) => {
  464. await request.bindings[0]!.functions.echo!({ value: 'x' })
  465. await request.bindings[0]!.functions.echo!({ value: 'y' })
  466. return { logs: [], value: 'done' }
  467. }
  468. const result = await runCode(ctx, 'program')
  469. expect(result.isError).toBe(false)
  470. expect(result.additionalContexts).toEqual([
  471. {
  472. content: [{ type: 'text', text: 'context for call-1:code:1' }],
  473. source: { kind: 'plugin', plugin: 'test' },
  474. meta: { callId: 'call-1:code:1' },
  475. },
  476. {
  477. content: [{ type: 'text', text: 'context for call-1:code:2' }],
  478. source: { kind: 'plugin', plugin: 'test' },
  479. meta: { callId: 'call-1:code:2' },
  480. },
  481. ])
  482. })
  483. it('keeps sub-call contexts when run_code fails after the nested dispatch', async () => {
  484. const { ctx, runtime } = await setup({ mode: 'both' })
  485. registerEcho(ctx)
  486. ctx.on('tools/post-execute', (exec, _result, next): Promise<PostToolDecision> => {
  487. if (exec.name !== 'echo') return next()
  488. return Promise.resolve({
  489. kind: 'accept',
  490. additionalContexts: [{
  491. content: [{ type: 'text', text: 'nested context' }],
  492. source: { kind: 'plugin', plugin: 'test' },
  493. }],
  494. })
  495. })
  496. runtime.behavior = async (request) => {
  497. await request.bindings[0]!.functions.echo!({ value: 'x' })
  498. return { logs: [], error: { kind: 'exception', message: 'program failed later' } }
  499. }
  500. const result = await runCode(ctx, 'program')
  501. expect(result.isError).toBe(true)
  502. expect(result.additionalContexts).toEqual([{
  503. content: [{ type: 'text', text: 'nested context' }],
  504. source: { kind: 'plugin', plugin: 'test' },
  505. }])
  506. })
  507. it('converts a failed run into a structured isError result carrying kind, message, and captured logs', async () => {
  508. const { ctx, runtime } = await setup({ mode: 'code' })
  509. runtime.behavior = () => Promise.resolve({
  510. logs: ['got this far'],
  511. error: { kind: 'timeout', message: 'compute budget exhausted (300ms busy)' },
  512. })
  513. const result = await runCode(ctx, 'program')
  514. expect(result.isError).toBe(true)
  515. expect(result.error).toMatchObject({ info: { name: 'CodeRunFailedError', code: 'CODE_RUN_FAILED' } })
  516. const text = (result.content[0] as { text: string }).text
  517. expect(text).toContain('code run failed (timeout)')
  518. expect(text).toContain('compute budget exhausted')
  519. expect(text).toContain('got this far')
  520. })
  521. it('CodeRunFailedError is a HarnessError with the CODE_RUN_FAILED code', () => {
  522. const error = new CodeRunFailedError('boom')
  523. expect(error.code).toBe('CODE_RUN_FAILED')
  524. expect(error.name).toBe('CodeRunFailedError')
  525. })
  526. it('aborting the outer signal aborts the in-flight sub-dispatch and abandons queued ones', async () => {
  527. const { ctx, runtime } = await setup({ mode: 'code' })
  528. const seen: string[] = []
  529. let sawAbort = false
  530. ctx.tools.register(defineContentToolFixture({
  531. name: 'slow',
  532. description: 'Slow tool observing its signal.',
  533. parameters: { id: { type: 'string', required: true } },
  534. async execute(args, exec) {
  535. seen.push(args.id)
  536. await new Promise<void>((resolve) => {
  537. const timer = setTimeout(resolve, 500)
  538. exec.signal.addEventListener('abort', () => { sawAbort = true; clearTimeout(timer); resolve() }, { once: true })
  539. })
  540. return [{ type: 'text' as const, text: args.id }]
  541. },
  542. }))
  543. const controller = new AbortController()
  544. runtime.behavior = async (request) => {
  545. const tools = request.bindings[0]!.functions
  546. const calls = [tools.slow!({ id: 'first' }).catch(() => 'rejected'), tools.slow!({ id: 'second' }).catch(() => 'rejected')]
  547. setTimeout(() => { controller.abort('user-cancel') }, 50)
  548. await Promise.all(calls)
  549. // A real runtime would be terminated by the abort; the fake honors the
  550. // contract by reporting the abort as the run failure.
  551. return { logs: [], error: { kind: 'abort', message: 'user-cancel' } }
  552. }
  553. const result = await runCode(ctx, 'program', { signal: controller.signal })
  554. expect(result.isError).toBe(true)
  555. expect((result.content[0] as { text: string }).text).toContain('code run failed (abort)')
  556. expect(seen).toEqual(['first'])
  557. expect(sawAbort).toBe(true)
  558. })
  559. it('a runtime that starts a binding call and then REJECTS still reaches quiescence before returning', async () => {
  560. const { ctx, runtime } = await setup({ mode: 'code' })
  561. const { agent, events } = fakeAgent()
  562. let sawAbort = false
  563. let started!: () => void
  564. const inFlight = new Promise<void>((resolve) => { started = resolve })
  565. ctx.tools.register(defineContentToolFixture({
  566. name: 'slow',
  567. description: 'Slow tool observing its signal.',
  568. parameters: { id: { type: 'string', required: true } },
  569. async execute(args, exec) {
  570. started()
  571. await new Promise<void>((resolve) => {
  572. const timer = setTimeout(resolve, 500)
  573. exec.signal.addEventListener('abort', () => { sawAbort = true; clearTimeout(timer); resolve() }, { once: true })
  574. })
  575. return [{ type: 'text' as const, text: args.id }]
  576. },
  577. }))
  578. runtime.behavior = async (request) => {
  579. // Start a sub-dispatch, keep its rejection held, and fail the run once the tool is
  580. // genuinely in flight — a seam error after work has begun.
  581. request.bindings[0]!.functions.slow!({ id: 'orphan' }).catch(() => 'held')
  582. await inFlight
  583. throw new Error('backend exploded')
  584. }
  585. const result = await runCode(ctx, 'program', { agent })
  586. expect(result.isError).toBe(true)
  587. expect((result.content[0] as { text: string }).text).toContain('backend exploded')
  588. // Quiescence held: the in-flight sub-dispatch was aborted and its event
  589. // logged INSIDE the run_code execution, not after it returned.
  590. expect(sawAbort).toBe(true)
  591. expect(events.filter(event => event.type === 'tool/code-dispatch').map(event => (event.data as { name: string }).name)).toEqual(['slow'])
  592. })
  593. it('runs without an owning agent: dispatches work, event logging is skipped', async () => {
  594. const { ctx, runtime } = await setup({ mode: 'code' })
  595. const calls = registerEcho(ctx)
  596. runtime.behavior = async (request) => {
  597. await request.bindings[0]!.functions.echo!({ value: 'x' })
  598. return { logs: [], value: 'ok' }
  599. }
  600. const result = await runCode(ctx, 'program')
  601. expect(result.isError).toBe(false)
  602. expect(calls).toEqual([{ value: 'x' }])
  603. })
  604. it('executing run_code under a missing runtime is a structured isError, not a crash', async () => {
  605. const ctx = new Context()
  606. await ctx.plugin(SystemPrompt, {})
  607. await ctx.plugin(ToolRegistry, { mode: 'code' })
  608. const result = await runCode(ctx, 'program')
  609. expect(result.isError).toBe(true)
  610. expect((result.content[0] as { text: string }).text).toContain('requires a code runtime')
  611. })
  612. it('presents the PROGRAM as the execute-card title on both call and result (the one slot execute cards always show)', async () => {
  613. const { ctx } = await setup({ mode: 'code' })
  614. const tool = ctx.tools.get(RUN_CODE_NAME)!
  615. // The program IS the title, mirroring how command tools title their cards
  616. // with the command: an ACP client's execute-card header is the only
  617. // always-visible slot (Zed renders no body content and no raw input for
  618. // execute-kind cards without a real terminal).
  619. expect(tool.presentCall?.({ code: 'return 1' })).toEqual({
  620. card: 'generic',
  621. title: 'return 1',
  622. kind: 'execute',
  623. rawInput: 'return 1',
  624. })
  625. const view = tool.presentResult?.({ code: 'return 1' }, {
  626. content: [{ type: 'text', text: 'model-facing' }],
  627. isError: false,
  628. meta: { logs: ['printed'] },
  629. })
  630. // The result omits the title — an update replaces only provided fields,
  631. // so the pending card's program title persists through completion.
  632. expect(view).toEqual({
  633. card: 'generic',
  634. content: [{ type: 'text', text: 'printed' }],
  635. })
  636. // No captured output → no content either; everything pending persists.
  637. expect(tool.presentResult?.({ code: 'x' }, { content: [], isError: false, meta: { logs: [] } }))
  638. .toEqual({ card: 'generic' })
  639. // Replay with an unrecognizable meta falls back to the generic rendering.
  640. expect(tool.presentResult?.({ code: 'x' }, { content: [], isError: false, meta: { logs: [{ text: 'legacy' }], dispatches: 1 } })).toBeUndefined()
  641. expect(tool.presentResult?.({ code: 'x' }, { content: [], isError: false, meta: { other: true } })).toBeUndefined()
  642. expect(tool.presentResult?.({ code: 'x' }, { content: [], isError: false })).toBeUndefined()
  643. })
  644. it('renders non-text sub-result blocks as placeholders and truncates long event summaries', async () => {
  645. const { ctx, runtime } = await setup({ mode: 'code' })
  646. const { agent, events } = fakeAgent()
  647. const long = 'x'.repeat(300)
  648. ctx.tools.register(defineTool({
  649. name: 'mixed',
  650. description: 'Returns mixed content.',
  651. parameters: {},
  652. output: {
  653. schema: { type: 'string' },
  654. render: () => [
  655. { type: 'text', text: long },
  656. { type: 'reasoning', text: 'hidden' },
  657. ],
  658. },
  659. execute() {
  660. return Promise.resolve('mixed-value')
  661. },
  662. }))
  663. runtime.behavior = async (request) => {
  664. const value = await request.bindings[0]!.functions.mixed!({})
  665. return { logs: [], value }
  666. }
  667. const result = await runCode(ctx, 'program', { agent })
  668. expect(result.isError).toBe(false)
  669. expect((result.content[0] as { text: string }).text).toBe('mixed-value')
  670. const dispatch = events.find(event => event.type === 'tool/code-dispatch')?.data as SessionEventMap['tool/code-dispatch']
  671. expect(dispatch.resultSummary.length).toBe(201)
  672. expect(dispatch.resultSummary.endsWith('…')).toBe(true)
  673. })
  674. it('normalizes the session workspace root before bounding durable result summaries', async () => {
  675. const { ctx, runtime } = await setup({ mode: 'code' })
  676. ctx.tools.register(defineTool({
  677. name: 'workspace_path',
  678. description: 'Return a path beneath the session workspace.',
  679. parameters: {},
  680. output: {
  681. schema: { type: 'string' },
  682. render: (_args, value) => [{ type: 'text', text: value }],
  683. },
  684. execute(_args, exec) {
  685. const cwd = exec.agent?.session.header.cwd ?? ''
  686. return Promise.resolve(`<path>${cwd}/nested/task.txt</path>\n${'x'.repeat(240)}`)
  687. },
  688. }))
  689. runtime.behavior = async request => ({
  690. logs: [],
  691. value: await request.bindings[0]!.functions.workspace_path!({}),
  692. })
  693. const short = fakeAgent({ cwd: '/tmp/workspace' })
  694. const long = fakeAgent({ cwd: `/tmp/${'long-segment/'.repeat(30)}workspace` })
  695. const shortResult = await runCode(ctx, 'program', { agent: short.agent })
  696. const longResult = await runCode(ctx, 'program', { agent: long.agent })
  697. const shortDispatch = short.events[0]!.data as SessionEventMap['tool/code-dispatch']
  698. const longDispatch = long.events[0]!.data as SessionEventMap['tool/code-dispatch']
  699. expect(shortResult.content).not.toEqual(longResult.content)
  700. expect(shortDispatch.resultSummary).toBe(longDispatch.resultSummary)
  701. expect(shortDispatch.resultSummary).toHaveLength(201)
  702. expect(shortDispatch.resultSummary).toMatch(/^<path>\.\/nested\/task\.txt<\/path>\n.+…$/)
  703. })
  704. it('leaves result summaries unchanged when a session cwd is absent or is the filesystem root', async () => {
  705. const { ctx, runtime } = await setup({ mode: 'code' })
  706. registerEcho(ctx)
  707. runtime.behavior = async request => ({
  708. logs: [],
  709. value: await request.bindings[0]!.functions.echo!({ value: '/workspace/value' }),
  710. })
  711. const absent = fakeAgent({})
  712. const root = fakeAgent({ cwd: '/' })
  713. await runCode(ctx, 'program', { agent: absent.agent })
  714. await runCode(ctx, 'program', { agent: root.agent })
  715. expect((absent.events[0]!.data as SessionEventMap['tool/code-dispatch']).resultSummary).toBe('echo:/workspace/value')
  716. expect((root.events[0]!.data as SessionEventMap['tool/code-dispatch']).resultSummary).toBe('echo:/workspace/value')
  717. })
  718. it('rejects undefined, getter-throwing, exotic, and unrepresentable binding arguments before dispatch', async () => {
  719. const { ctx, runtime } = await setup({ mode: 'code' })
  720. const calls = registerEcho(ctx)
  721. const { agent, events } = fakeAgent()
  722. runtime.behavior = async (request) => {
  723. const echo = request.bindings[0]!.functions.echo!
  724. const catchMessage = (promise: Promise<unknown>) => promise.then(() => 'resolved', (error: unknown) => error instanceof Error ? error.message : String(error))
  725. return {
  726. logs: [],
  727. value: [
  728. // Root undefined must reject up front: the event log rejects it as
  729. // data, and nothing may execute unlogged.
  730. await catchMessage(echo(undefined)),
  731. await catchMessage(echo(Object.defineProperty({}, 'bad', { enumerable: true, get() { throw 'raw-throw' } }))),
  732. await catchMessage(echo(Object.defineProperty({}, 'bad', { enumerable: true, get() { throw new Error('error-throw') } }))),
  733. await catchMessage(echo(new Date(0))),
  734. // A bare function is a value JSON cannot represent at all.
  735. await catchMessage(echo(() => 1)),
  736. ].join(' | '),
  737. }
  738. }
  739. const result = await runCode(ctx, 'program', { agent })
  740. const text = (result.content[0] as { text: string }).text
  741. expect(text).toContain('call the tool with an arguments object')
  742. expect(text).toContain('lossless JSON: raw-throw')
  743. expect(text).toContain('lossless JSON: error-throw')
  744. expect(text.match(/tool arguments must be lossless JSON/g)).toHaveLength(5)
  745. // None dispatched or logged.
  746. expect(calls).toEqual([])
  747. expect(events.filter(event => event.type === 'tool/code-dispatch')).toEqual([])
  748. })
  749. it('gives the tool and durable log the same immutable argument value', async () => {
  750. const { ctx, runtime } = await setup({ mode: 'code' })
  751. const { agent, events } = fakeAgent()
  752. let mutationSucceeded: boolean | undefined
  753. ctx.tools.register(defineContentToolFixture({
  754. name: 'mutator',
  755. description: 'Attempts to mutate its args object.',
  756. parameters: { list: { type: 'array', required: true } },
  757. execute(args) {
  758. mutationSucceeded = Reflect.set(args.list, 1, 'injected-by-tool')
  759. return Promise.resolve([{ type: 'text' as const, text: 'protected' }])
  760. },
  761. }))
  762. runtime.behavior = async (request) => {
  763. await request.bindings[0]!.functions.mutator!({ list: ['original'] })
  764. return { logs: [] }
  765. }
  766. const result = await runCode(ctx, 'program', { agent })
  767. expect(result.isError).toBe(false)
  768. expect(mutationSucceeded).toBe(false)
  769. const dispatch = events.find(event => event.type === 'tool/code-dispatch')?.data as SessionEventMap['tool/code-dispatch']
  770. expect(dispatch.arguments).toEqual({ list: ['original'] })
  771. })
  772. it('exposes a tool named __proto__ as an ordinary own binding', async () => {
  773. const { ctx, runtime } = await setup({ mode: 'code' })
  774. ctx.tools.register(defineTool({
  775. name: '__proto__',
  776. description: 'A prototype-colliding tool name.',
  777. parameters: {},
  778. output: {
  779. schema: { type: 'string' },
  780. render: (_args, value) => [{ type: 'text', text: value }],
  781. },
  782. execute() { return Promise.resolve('proto-tool-ok') },
  783. }))
  784. runtime.behavior = async (request) => {
  785. const functions = request.bindings[0]!.functions
  786. expect(Object.getPrototypeOf(functions)).toBeNull()
  787. const value = await functions['__proto__']!({})
  788. return { logs: [], value }
  789. }
  790. const result = await runCode(ctx, 'program')
  791. expect(result.isError).toBe(false)
  792. expect(result.content[0]).toEqual({ type: 'text', text: 'proto-tool-ok' })
  793. })
  794. it('renders every non-string JSON root as pretty JSON while preserving strings raw', async () => {
  795. const { ctx, runtime } = await setup({ mode: 'code' })
  796. runtime.behavior = () => Promise.resolve({ logs: [], value: { n: 42, ok: true } })
  797. expect((await runCode(ctx, 'object')).content[0]).toEqual({ type: 'text', text: '{\n "n": 42,\n "ok": true\n}' })
  798. runtime.behavior = () => Promise.resolve({ logs: [], value: {} })
  799. expect((await runCode(ctx, 'empty object')).content[0]).toEqual({ type: 'text', text: '{}' })
  800. const nested = { outer: [{ inner: true }] }
  801. runtime.behavior = () => Promise.resolve({ logs: [], value: nested })
  802. expect((await runCode(ctx, 'nested')).content[0]).toEqual({ type: 'text', text: JSON.stringify(nested, null, 2) })
  803. runtime.behavior = () => Promise.resolve({ logs: [], value: ['x', 2] })
  804. expect((await runCode(ctx, 'array')).content[0]).toEqual({ type: 'text', text: '[\n "x",\n 2\n]' })
  805. runtime.behavior = () => Promise.resolve({ logs: [], value: [] })
  806. expect((await runCode(ctx, 'empty array')).content[0]).toEqual({ type: 'text', text: '[]' })
  807. runtime.behavior = () => Promise.resolve({ logs: [], value: null })
  808. expect((await runCode(ctx, 'null')).content[0]).toEqual({ type: 'text', text: 'null' })
  809. runtime.behavior = () => Promise.resolve({ logs: [], value: 'raw' })
  810. expect((await runCode(ctx, 'string')).content[0]).toEqual({ type: 'text', text: 'raw' })
  811. runtime.behavior = () => Promise.resolve({ logs: [] })
  812. const absent = await runCode(ctx, 'undefined')
  813. expect(absent.content[0]).toEqual({ type: 'text', text: '(run_code completed with no output)' })
  814. expect(absent.isError ? undefined : absent.value).toEqual({ logs: [] })
  815. })
  816. it('renders deeply nested JSON without recursive traversal or quadratic indentation', async () => {
  817. const { ctx, runtime } = await setup({ mode: 'code' })
  818. let value: JsonValue = {
  819. emptyArray: [],
  820. emptyObject: {},
  821. pair: ['leaf', 2],
  822. record: { first: true, second: null },
  823. }
  824. for (let depth = 0; depth < 5_000; depth++) value = [value]
  825. runtime.behavior = () => Promise.resolve({ logs: [], value })
  826. const result = await runCode(ctx, 'deep result')
  827. expect(result.isError).toBe(false)
  828. const text = (result.content[0] as { type: 'text'; text: string }).text
  829. expect(text.startsWith('[\n [\n [')).toBe(true)
  830. expect(text).toContain('"leaf"')
  831. expect(text.endsWith(']')).toBe(true)
  832. expect(text.length).toBeLessThan(11_000)
  833. })
  834. it('short-circuits a pre-aborted outer signal before the code runtime', async () => {
  835. const { ctx, runtime } = await setup({ mode: 'code' })
  836. const calls = registerEcho(ctx)
  837. runtime.behavior = (request) => {
  838. // The fake honors the seam contract for an already-aborted signal.
  839. if (request.signal?.aborted) return Promise.resolve({ logs: [], error: { kind: 'abort' as const, message: String(request.signal.reason) } })
  840. return Promise.resolve({ logs: [], value: 'unreachable' })
  841. }
  842. const controller = new AbortController()
  843. controller.abort('too-late')
  844. const result = await runCode(ctx, 'program', { signal: controller.signal })
  845. expect(result.isError).toBe(true)
  846. expect(result).toEqual({
  847. content: [{ type: 'text', text: 'Error: tool call aborted before dispatch' }],
  848. isError: true,
  849. error: {
  850. message: 'tool call aborted before dispatch',
  851. info: { name: 'AbortError', code: TOOL_ABORTED_BEFORE_DISPATCH },
  852. },
  853. })
  854. expect(runtime.lastRequest).toBeUndefined()
  855. expect(calls).toEqual([])
  856. })
  857. it('reports cancellation after rejecting a late binding without dispatching it', async () => {
  858. const { ctx, runtime } = await setup({ mode: 'code' })
  859. const calls = registerEcho(ctx)
  860. const controller = new AbortController()
  861. runtime.behavior = async (request) => {
  862. controller.abort('cancelled-mid-run')
  863. const message = await request.bindings[0]!.functions.echo!({ value: 'x' })
  864. .then(() => 'resolved', (error: unknown) => error instanceof Error ? error.message : String(error))
  865. return { logs: [], value: message }
  866. }
  867. const result = await runCode(ctx, 'program', { signal: controller.signal })
  868. expect(result.isError).toBe(true)
  869. expect(result.error).toEqual({
  870. message: 'tool call aborted',
  871. info: { name: 'AbortError', code: 'ABORTED' },
  872. })
  873. expect((result.content[0] as { text: string }).text).toBe('Error: tool call aborted')
  874. expect(calls).toEqual([])
  875. })
  876. it('a tool/code-dispatch event never derives a model message', () => {
  877. const session = new Session(SessionId('code-mode-derive'))
  878. session.append('user/message', { content: [{ type: 'text', text: 'hi' }], source: { kind: 'user' } }, { surfaceOp: 'append' })
  879. session.append('tool/code-dispatch', {
  880. parentCallId: CallId('p1'),
  881. subCallId: CallId('p1:code:1'),
  882. name: 'echo',
  883. arguments: { value: 'x' },
  884. isError: false,
  885. resultSummary: 'echo:x',
  886. })
  887. const derived = session.deriveMessages()
  888. expect(derived).toHaveLength(1)
  889. expect(derived[0]?.role).toBe('user')
  890. })
  891. it('defaults to native mode under direct construction with no config', async () => {
  892. const ctx = new Context()
  893. await ctx.plugin(SystemPrompt, {})
  894. const registry = new ToolRegistry(ctx)
  895. expect(registry.get(RUN_CODE_NAME)).toBeUndefined()
  896. const assembly = await ctx.systemPrompt.assemble()
  897. expect(assembly.sections.some(section => section.name === 'tools:sdk')).toBe(false)
  898. })
  899. })