code-mode.spec.ts 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. import { describe, expect, it } from 'vitest'
  2. import { Context } from 'cordis'
  3. import { CallId } from '@deepseek-ai/dsh-llm'
  4. import SystemPrompt from '@deepseek-ai/dsh-system-prompt'
  5. import { CodeRuntime } from '@deepseek-ai/dsh-code-runtime'
  6. import type { CodeRunRequest, CodeRunResult } from '@deepseek-ai/dsh-code-runtime'
  7. import ToolRegistry, { CodeRunFailedError, RUN_CODE_NAME, defineTool } from '@deepseek-ai/dsh-tools'
  8. import type { Config, PostToolDecision, ToolExecutionResult } from '@deepseek-ai/dsh-tools'
  9. import type { Agent } from '@deepseek-ai/dsh-agent'
  10. import { Session, SessionId } from '@deepseek-ai/dsh-session'
  11. import type { SessionEventMap } from '@deepseek-ai/dsh-session'
  12. /**
  13. * Code Mode unit tier (per the RFC's plan): provider contribution per mode,
  14. * misconfiguration rejections, the run_code dispatch bridge (serialization,
  15. * abort, JSON normalization, error mapping, events, quiescence), and HMR
  16. * safety — all against an in-repo fake runtime, exactly the
  17. * interface/implementation/consumer shape the seam promises.
  18. */
  19. /** A scriptable in-repo CodeRuntime: each test sets `behavior` to drive the bindings however it needs. */
  20. class FakeRuntime extends CodeRuntime {
  21. readonly language: string
  22. readonly isolation = 'fake'
  23. behavior: (request: CodeRunRequest) => Promise<CodeRunResult> = () => Promise.resolve({ logs: [] })
  24. lastRequest?: CodeRunRequest
  25. constructor(ctx: Context, config: { language?: string } = {}) {
  26. super(ctx)
  27. this.language = config.language ?? 'typescript'
  28. }
  29. run(request: CodeRunRequest): Promise<CodeRunResult> {
  30. this.lastRequest = request
  31. return this.behavior(request)
  32. }
  33. }
  34. interface SetupOptions {
  35. mode?: Config['mode']
  36. runtime?: false | { language?: string }
  37. toolOrder?: string[]
  38. }
  39. async function setup(options: SetupOptions = {}) {
  40. const ctx = new Context()
  41. await ctx.plugin(SystemPrompt, { ...options.toolOrder ? { toolOrder: options.toolOrder } : {} })
  42. await ctx.plugin(ToolRegistry, { mode: options.mode ?? 'code' })
  43. let runtime: FakeRuntime | undefined
  44. if (options.runtime !== false) {
  45. await ctx.plugin(FakeRuntime, options.runtime ?? {})
  46. runtime = ctx.codeRuntime as FakeRuntime
  47. }
  48. return { ctx, tools: ctx.tools, systemPrompt: ctx.systemPrompt, runtime: runtime! }
  49. }
  50. /** Register a trivial echo tool; returns the calls it received. */
  51. function registerEcho(ctx: Context, name = 'echo'): unknown[] {
  52. const calls: unknown[] = []
  53. ctx.tools.register(defineTool({
  54. name,
  55. description: `Echo tool ${name}.`,
  56. parameters: { value: { type: 'string', required: true } },
  57. execute(args) {
  58. calls.push(args)
  59. return Promise.resolve([{ type: 'text' as const, text: `${name}:${args.value}` }])
  60. },
  61. }))
  62. return calls
  63. }
  64. /** A structural fake of the owning agent: captures session appends. */
  65. function fakeAgent(options: { cwd?: string } = { cwd: '/workspace' }): { agent: Agent; events: { type: string; data: unknown }[] } {
  66. const events: { type: string; data: unknown }[] = []
  67. const agent = {
  68. session: {
  69. header: options.cwd === undefined ? {} : { cwd: options.cwd },
  70. append: (type: string, data: unknown) => { events.push({ type, data }) },
  71. },
  72. } as unknown as Agent
  73. return { agent, events }
  74. }
  75. /** Dispatch run_code through the registry pipeline, as the loop would. */
  76. async function runCode(ctx: Context, code: string, extras: { agent?: Agent; signal?: AbortSignal } = {}): Promise<ToolExecutionResult> {
  77. return ctx.tools.execute({
  78. callId: CallId('call-1'),
  79. name: RUN_CODE_NAME,
  80. arguments: { code },
  81. ...extras.agent ? { agent: extras.agent } : {},
  82. ...extras.signal ? { signal: extras.signal } : {},
  83. })
  84. }
  85. describe('mode-aware wire contribution', () => {
  86. it("mode 'native' contributes every schema, no run_code, no SDK section — and needs no runtime", async () => {
  87. const { ctx, systemPrompt } = await setup({ mode: 'native', runtime: false })
  88. registerEcho(ctx)
  89. const assembly = await systemPrompt.assemble()
  90. expect(assembly.tools.map(tool => tool.name)).toEqual(['echo'])
  91. expect(assembly.sections.some(section => section.name === 'tools:sdk')).toBe(false)
  92. })
  93. it("mode 'code' contributes exactly [run_code] plus the SDK section declaring the other tools", async () => {
  94. const { ctx, systemPrompt } = await setup({ mode: 'code' })
  95. registerEcho(ctx)
  96. const assembly = await systemPrompt.assemble()
  97. expect(assembly.tools.map(tool => tool.name)).toEqual([RUN_CODE_NAME])
  98. const sdk = assembly.sections.find(section => section.name === 'tools:sdk')
  99. expect(sdk?.text).toContain('declare const tools: {')
  100. expect(sdk?.text).toContain('echo(args:')
  101. expect(sdk?.text).not.toContain('run_code(args:')
  102. })
  103. it("mode 'both' contributes every native schema plus run_code, and the SDK section", async () => {
  104. const { ctx, systemPrompt } = await setup({ mode: 'both' })
  105. registerEcho(ctx)
  106. const assembly = await systemPrompt.assemble()
  107. expect(assembly.tools.map(tool => tool.name)).toEqual(['echo', RUN_CODE_NAME])
  108. expect(assembly.sections.some(section => section.name === 'tools:sdk')).toBe(true)
  109. })
  110. it("never exposes run_code to programs, even under mode 'both' (no recursive dispatch path)", async () => {
  111. const { ctx, runtime } = await setup({ mode: 'both' })
  112. registerEcho(ctx)
  113. runtime.behavior = (request) => {
  114. const functions = request.bindings[0]!.functions
  115. return Promise.resolve({
  116. logs: [],
  117. value: JSON.stringify({
  118. names: Object.keys(functions).sort(),
  119. // Own-property AND prototype-chain reads both come back empty —
  120. // there is no handle a program could re-enter run_code through.
  121. runCode: String(functions[RUN_CODE_NAME]),
  122. }),
  123. })
  124. }
  125. const result = await runCode(ctx, 'program')
  126. expect(result.isError).toBe(false)
  127. expect(JSON.parse((result.content[0] as { text: string }).text)).toEqual({ names: ['echo'], runCode: 'undefined' })
  128. })
  129. it('renders byte-identical SDK text across consecutive assemblies of an unchanged tool set', async () => {
  130. const { ctx, systemPrompt } = await setup({ mode: 'code' })
  131. registerEcho(ctx)
  132. const first = await systemPrompt.assemble()
  133. const second = await systemPrompt.assemble()
  134. const text = (assembly: typeof first) => assembly.sections.find(section => section.name === 'tools:sdk')?.text
  135. expect(text(first)).toBe(text(second))
  136. })
  137. it('rejects every assembly when a non-native mode has no code runtime', async () => {
  138. const { systemPrompt } = await setup({ mode: 'code', runtime: false })
  139. await expect(systemPrompt.assemble()).rejects.toThrow(/requires a code runtime/)
  140. })
  141. it("rejects every assembly when the runtime's language is not typescript", async () => {
  142. const { systemPrompt } = await setup({ mode: 'code', runtime: { language: 'python' } })
  143. await expect(systemPrompt.assemble()).rejects.toThrow(/language is "python"/)
  144. })
  145. it("rejects the assembly when toolOrder names a native tool that mode 'code' no longer contributes", async () => {
  146. const { ctx, systemPrompt } = await setup({ mode: 'code', toolOrder: ['echo', '<unlisted-tools>'] })
  147. registerEcho(ctx)
  148. await expect(systemPrompt.assemble()).rejects.toThrow(/toolOrder lists unregistered tool "echo"/)
  149. })
  150. it('removes run_code and the SDK section when the registry fiber disposes (HMR safety)', async () => {
  151. const ctx = new Context()
  152. await ctx.plugin(SystemPrompt, {})
  153. await ctx.plugin(FakeRuntime, {})
  154. const fiber = await ctx.plugin(ToolRegistry, { mode: 'code' })
  155. expect(ctx.tools.get(RUN_CODE_NAME)).toBeDefined()
  156. await fiber.dispose()
  157. const assembly = await ctx.systemPrompt.assemble()
  158. expect(assembly.tools).toEqual([])
  159. expect(assembly.sections.some(section => section.name === 'tools:sdk')).toBe(false)
  160. })
  161. })
  162. describe('the run_code dispatch bridge', () => {
  163. it('bridges tool calls, returns only the curated output, and logs one event per dispatch', async () => {
  164. const { ctx, runtime } = await setup({ mode: 'code' })
  165. const calls = registerEcho(ctx)
  166. const { agent, events } = fakeAgent()
  167. runtime.behavior = async (request) => {
  168. const tools = request.bindings[0]!.functions
  169. const first = await tools.echo!({ value: 'one' })
  170. const second = await tools.echo!({ value: 'two' })
  171. return { logs: [{ source: 'console', level: 'log', text: `saw ${String(first)}` }], value: second }
  172. }
  173. const result = await runCode(ctx, 'const …: string = …', { agent })
  174. expect(result.isError).toBe(false)
  175. expect(result.content).toEqual([{ type: 'text', text: 'saw echo:one\necho:two' }])
  176. expect(calls).toEqual([{ value: 'one' }, { value: 'two' }])
  177. const dispatches = events.filter(event => event.type === 'tool/code-dispatch')
  178. expect(dispatches.map(event => event.data)).toEqual([
  179. { parentCallId: 'call-1', subCallId: 'call-1:code:1', name: 'echo', arguments: { value: 'one' }, isError: false, resultSummary: 'echo:one' },
  180. { parentCallId: 'call-1', subCallId: 'call-1:code:2', name: 'echo', arguments: { value: 'two' }, isError: false, resultSummary: 'echo:two' },
  181. ])
  182. expect(result.meta).toEqual({ logs: [{ source: 'console', level: 'log', text: 'saw echo:one' }], dispatches: 2 })
  183. })
  184. it('serializes Promise.all dispatches: tool executions never overlap, in submission order', async () => {
  185. const { ctx, runtime } = await setup({ mode: 'code' })
  186. const intervals: [string, string][] = []
  187. let active = 0
  188. ctx.tools.register(defineTool({
  189. name: 'probe',
  190. description: 'Records execution overlap.',
  191. parameters: { id: { type: 'string', required: true } },
  192. async execute(args) {
  193. active++
  194. expect(active, 'probe executions overlapped').toBe(1)
  195. intervals.push(['enter', args.id])
  196. await new Promise(resolve => setTimeout(resolve, 20))
  197. intervals.push(['exit', args.id])
  198. active--
  199. return [{ type: 'text' as const, text: args.id }]
  200. },
  201. }))
  202. runtime.behavior = async (request) => {
  203. const tools = request.bindings[0]!.functions
  204. const values = await Promise.all([tools.probe!({ id: 'a' }), tools.probe!({ id: 'b' }), tools.probe!({ id: 'c' })])
  205. return { logs: [], value: values.join(',') }
  206. }
  207. const result = await runCode(ctx, 'program')
  208. expect(result.isError).toBe(false)
  209. expect(intervals).toEqual([
  210. ['enter', 'a'], ['exit', 'a'],
  211. ['enter', 'b'], ['exit', 'b'],
  212. ['enter', 'c'], ['exit', 'c'],
  213. ])
  214. expect(result.content[0]).toEqual({ type: 'text', text: 'a,b,c' })
  215. })
  216. it('rejects the program-side call when the tool errors, with the tool error text', async () => {
  217. const { ctx, runtime } = await setup({ mode: 'code' })
  218. ctx.tools.register(defineTool({
  219. name: 'fail',
  220. description: 'Always fails.',
  221. parameters: {},
  222. execute(): Promise<never> { return Promise.reject(new Error('deliberate failure')) },
  223. }))
  224. runtime.behavior = async (request) => {
  225. try {
  226. await request.bindings[0]!.functions.fail!({})
  227. return { logs: [], value: 'unreachable' }
  228. } catch (error: unknown) {
  229. return { logs: [], value: `caught: ${error instanceof Error ? error.message : String(error)}` }
  230. }
  231. }
  232. const result = await runCode(ctx, 'program')
  233. expect(result.content[0]).toEqual({ type: 'text', text: 'caught: Error: deliberate failure' })
  234. })
  235. it('a tools/pre-execute deny reaches the program as a binding rejection', async () => {
  236. const { ctx, runtime } = await setup({ mode: 'code' })
  237. registerEcho(ctx)
  238. ctx.on('tools/pre-execute', (exec, next) => {
  239. if (exec.name === 'echo') return Promise.resolve({ kind: 'deny' as const, reason: 'not on my watch' })
  240. return next()
  241. })
  242. runtime.behavior = async (request) => {
  243. try {
  244. await request.bindings[0]!.functions.echo!({ value: 'x' })
  245. return { logs: [], value: 'unreachable' }
  246. } catch (error: unknown) {
  247. return { logs: [], value: `denied: ${error instanceof Error ? error.message : String(error)}` }
  248. }
  249. }
  250. const result = await runCode(ctx, 'program')
  251. expect(result.content[0]?.type).toBe('text')
  252. expect((result.content[0] as { text: string }).text).toContain('not on my watch')
  253. })
  254. it('rejects a binding argument that does not survive JSON normalization, dispatching nothing', async () => {
  255. const { ctx, runtime } = await setup({ mode: 'code' })
  256. const calls = registerEcho(ctx)
  257. const { agent, events } = fakeAgent()
  258. runtime.behavior = async (request) => {
  259. try {
  260. await request.bindings[0]!.functions.echo!({ value: 'x', big: 1n })
  261. return { logs: [], value: 'unreachable' }
  262. } catch (error: unknown) {
  263. return { logs: [], value: error instanceof Error ? error.message : String(error) }
  264. }
  265. }
  266. const result = await runCode(ctx, 'program', { agent })
  267. expect((result.content[0] as { text: string }).text).toContain('JSON-serializable')
  268. expect(calls).toEqual([])
  269. expect(events.filter(event => event.type === 'tool/code-dispatch')).toEqual([])
  270. })
  271. it('dispatches the JSON-normalized value: what the tool sees is what the event logs', async () => {
  272. const { ctx, runtime } = await setup({ mode: 'code' })
  273. const calls = registerEcho(ctx)
  274. const { agent, events } = fakeAgent()
  275. runtime.behavior = async (request) => {
  276. // A Date survives structured clone but is not JSON; the bridge
  277. // normalizes it to its JSON form (an ISO string) BEFORE dispatch.
  278. await request.bindings[0]!.functions.echo!({ value: 'x', when: new Date(0) }).catch(() => undefined)
  279. return { logs: [] }
  280. }
  281. await runCode(ctx, 'program', { agent })
  282. expect(calls).toEqual([{ value: 'x', when: '1970-01-01T00:00:00.000Z' }])
  283. const dispatch = events.find(event => event.type === 'tool/code-dispatch')?.data as SessionEventMap['tool/code-dispatch']
  284. expect(dispatch.arguments).toEqual({ value: 'x', when: '1970-01-01T00:00:00.000Z' })
  285. })
  286. it('defers sub-call additionalContexts onto the outer run_code result', async () => {
  287. const { ctx, runtime } = await setup({ mode: 'code' })
  288. registerEcho(ctx)
  289. ctx.on('tools/post-execute', (exec, _result, next): Promise<PostToolDecision> => {
  290. if (exec.name === 'echo') {
  291. return Promise.resolve({
  292. kind: 'accept' as const,
  293. additionalContexts: [{
  294. content: [{ type: 'text' as const, text: `context for ${exec.callId}` }],
  295. source: { kind: 'plugin' as const, plugin: 'test' },
  296. envelope: 'raw' as const,
  297. meta: { callId: exec.callId },
  298. }],
  299. })
  300. }
  301. return next()
  302. })
  303. runtime.behavior = async (request) => {
  304. await request.bindings[0]!.functions.echo!({ value: 'x' })
  305. await request.bindings[0]!.functions.echo!({ value: 'y' })
  306. return { logs: [], value: 'done' }
  307. }
  308. const result = await runCode(ctx, 'program')
  309. expect(result.isError).toBe(false)
  310. expect(result.additionalContexts).toEqual([
  311. {
  312. content: [{ type: 'text', text: 'context for call-1:code:1' }],
  313. source: { kind: 'plugin', plugin: 'test' },
  314. envelope: 'raw',
  315. meta: { callId: 'call-1:code:1' },
  316. },
  317. {
  318. content: [{ type: 'text', text: 'context for call-1:code:2' }],
  319. source: { kind: 'plugin', plugin: 'test' },
  320. envelope: 'raw',
  321. meta: { callId: 'call-1:code:2' },
  322. },
  323. ])
  324. })
  325. it('keeps sub-call contexts when run_code fails after the nested dispatch', async () => {
  326. const { ctx, runtime } = await setup({ mode: 'both' })
  327. registerEcho(ctx)
  328. ctx.on('tools/post-execute', (exec, _result, next): Promise<PostToolDecision> => {
  329. if (exec.name !== 'echo') return next()
  330. return Promise.resolve({
  331. kind: 'accept',
  332. additionalContexts: [{
  333. content: [{ type: 'text', text: 'nested context' }],
  334. source: { kind: 'plugin', plugin: 'test' },
  335. }],
  336. })
  337. })
  338. runtime.behavior = async (request) => {
  339. await request.bindings[0]!.functions.echo!({ value: 'x' })
  340. return { logs: [], error: { kind: 'exception', message: 'program failed later' } }
  341. }
  342. const result = await runCode(ctx, 'program')
  343. expect(result.isError).toBe(true)
  344. expect(result.additionalContexts).toEqual([{
  345. content: [{ type: 'text', text: 'nested context' }],
  346. source: { kind: 'plugin', plugin: 'test' },
  347. }])
  348. })
  349. it('converts a failed run into a structured isError result carrying kind, message, and captured logs', async () => {
  350. const { ctx, runtime } = await setup({ mode: 'code' })
  351. runtime.behavior = () => Promise.resolve({
  352. logs: [{ source: 'console', level: 'log', text: 'got this far' }],
  353. error: { kind: 'timeout', message: 'compute budget exhausted (300ms busy)' },
  354. })
  355. const result = await runCode(ctx, 'program')
  356. expect(result.isError).toBe(true)
  357. expect(result.error).toEqual({ name: 'CodeRunFailedError', code: 'CODE_RUN_FAILED' })
  358. const text = (result.content[0] as { text: string }).text
  359. expect(text).toContain('code run failed (timeout)')
  360. expect(text).toContain('compute budget exhausted')
  361. expect(text).toContain('got this far')
  362. })
  363. it('CodeRunFailedError is a HarnessError with the CODE_RUN_FAILED code', () => {
  364. const error = new CodeRunFailedError('boom')
  365. expect(error.code).toBe('CODE_RUN_FAILED')
  366. expect(error.name).toBe('CodeRunFailedError')
  367. })
  368. it('aborting the outer signal aborts the in-flight sub-dispatch and abandons queued ones', async () => {
  369. const { ctx, runtime } = await setup({ mode: 'code' })
  370. const seen: string[] = []
  371. let sawAbort = false
  372. ctx.tools.register(defineTool({
  373. name: 'slow',
  374. description: 'Slow tool observing its signal.',
  375. parameters: { id: { type: 'string', required: true } },
  376. async execute(args, exec) {
  377. seen.push(args.id)
  378. await new Promise<void>((resolve) => {
  379. const timer = setTimeout(resolve, 500)
  380. exec.signal?.addEventListener('abort', () => { sawAbort = true; clearTimeout(timer); resolve() }, { once: true })
  381. })
  382. return [{ type: 'text' as const, text: args.id }]
  383. },
  384. }))
  385. const controller = new AbortController()
  386. runtime.behavior = async (request) => {
  387. const tools = request.bindings[0]!.functions
  388. const calls = [tools.slow!({ id: 'first' }).catch(() => 'rejected'), tools.slow!({ id: 'second' }).catch(() => 'rejected')]
  389. setTimeout(() => { controller.abort('user-cancel') }, 50)
  390. await Promise.all(calls)
  391. // A real runtime would be terminated by the abort; the fake honors the
  392. // contract by reporting the abort as the run failure.
  393. return { logs: [], error: { kind: 'abort', message: 'user-cancel' } }
  394. }
  395. const result = await runCode(ctx, 'program', { signal: controller.signal })
  396. expect(result.isError).toBe(true)
  397. expect((result.content[0] as { text: string }).text).toContain('code run failed (abort)')
  398. expect(seen).toEqual(['first'])
  399. expect(sawAbort).toBe(true)
  400. })
  401. it('a runtime that starts a binding call and then REJECTS still reaches quiescence before returning', async () => {
  402. const { ctx, runtime } = await setup({ mode: 'code' })
  403. const { agent, events } = fakeAgent()
  404. let sawAbort = false
  405. let started!: () => void
  406. const inFlight = new Promise<void>((resolve) => { started = resolve })
  407. ctx.tools.register(defineTool({
  408. name: 'slow',
  409. description: 'Slow tool observing its signal.',
  410. parameters: { id: { type: 'string', required: true } },
  411. async execute(args, exec) {
  412. started()
  413. await new Promise<void>((resolve) => {
  414. const timer = setTimeout(resolve, 500)
  415. exec.signal?.addEventListener('abort', () => { sawAbort = true; clearTimeout(timer); resolve() }, { once: true })
  416. })
  417. return [{ type: 'text' as const, text: args.id }]
  418. },
  419. }))
  420. runtime.behavior = async (request) => {
  421. // Start a sub-dispatch, keep its rejection held, and fail the run once
  422. // the tool is genuinely in flight — a seam error AFTER work has begun.
  423. // The bridge's settlement still owes quiescence: without the finally,
  424. // run_code would return now and the slow tool would finish (and log)
  425. // afterwards.
  426. request.bindings[0]!.functions.slow!({ id: 'orphan' }).catch(() => 'held')
  427. await inFlight
  428. throw new Error('backend exploded')
  429. }
  430. const result = await runCode(ctx, 'program', { agent })
  431. expect(result.isError).toBe(true)
  432. expect((result.content[0] as { text: string }).text).toContain('backend exploded')
  433. // Quiescence held: the in-flight sub-dispatch was aborted and its event
  434. // logged INSIDE the run_code execution, not after it returned.
  435. expect(sawAbort).toBe(true)
  436. expect(events.filter(event => event.type === 'tool/code-dispatch').map(event => (event.data as { name: string }).name)).toEqual(['slow'])
  437. })
  438. it('runs without an owning agent: dispatches work, event logging is skipped', async () => {
  439. const { ctx, runtime } = await setup({ mode: 'code' })
  440. const calls = registerEcho(ctx)
  441. runtime.behavior = async (request) => {
  442. await request.bindings[0]!.functions.echo!({ value: 'x' })
  443. return { logs: [], value: 'ok' }
  444. }
  445. const result = await runCode(ctx, 'program')
  446. expect(result.isError).toBe(false)
  447. expect(calls).toEqual([{ value: 'x' }])
  448. })
  449. it('executing run_code under a missing runtime is a structured isError, not a crash', async () => {
  450. const ctx = new Context()
  451. await ctx.plugin(SystemPrompt, {})
  452. await ctx.plugin(ToolRegistry, { mode: 'code' })
  453. const result = await runCode(ctx, 'program')
  454. expect(result.isError).toBe(true)
  455. expect((result.content[0] as { text: string }).text).toContain('requires a code runtime')
  456. })
  457. it('presents the PROGRAM as the execute-card title on both call and result (the one slot execute cards always show)', async () => {
  458. const { ctx } = await setup({ mode: 'code' })
  459. const tool = ctx.tools.get(RUN_CODE_NAME)!
  460. // The program IS the title, mirroring how command tools title their cards
  461. // with the command: an ACP client's execute-card header is the only
  462. // always-visible slot (Zed renders no body content and no raw input for
  463. // execute-kind cards without a real terminal).
  464. expect(tool.presentCall?.({ code: 'return 1' })).toEqual({
  465. card: 'generic',
  466. title: 'return 1',
  467. kind: 'execute',
  468. rawInput: 'return 1',
  469. })
  470. const view = tool.presentResult?.({ code: 'return 1' }, {
  471. content: [{ type: 'text', text: 'model-facing' }],
  472. isError: false,
  473. meta: { logs: [{ source: 'console', level: 'log', text: 'printed' }], dispatches: 1 },
  474. })
  475. // The result omits the title — an update replaces only provided fields,
  476. // so the pending card's program title persists through completion.
  477. expect(view).toEqual({
  478. card: 'generic',
  479. content: [{ type: 'text', text: 'printed' }],
  480. })
  481. // No captured output → no content either; everything pending persists.
  482. expect(tool.presentResult?.({ code: 'x' }, { content: [], isError: false, meta: { logs: [], dispatches: 2 } }))
  483. .toEqual({ card: 'generic' })
  484. // Replay with an unrecognizable meta falls back to the generic rendering.
  485. expect(tool.presentResult?.({ code: 'x' }, { content: [], isError: false, meta: { other: true } })).toBeUndefined()
  486. expect(tool.presentResult?.({ code: 'x' }, { content: [], isError: false })).toBeUndefined()
  487. })
  488. it('renders non-text sub-result blocks as placeholders and truncates long event summaries', async () => {
  489. const { ctx, runtime } = await setup({ mode: 'code' })
  490. const { agent, events } = fakeAgent()
  491. const long = 'x'.repeat(300)
  492. ctx.tools.register(defineTool({
  493. name: 'mixed',
  494. description: 'Returns mixed content.',
  495. parameters: {},
  496. execute() {
  497. return Promise.resolve([
  498. { type: 'text' as const, text: long },
  499. { type: 'reasoning' as const, text: 'hidden' },
  500. ])
  501. },
  502. }))
  503. runtime.behavior = async (request) => {
  504. const value = await request.bindings[0]!.functions.mixed!({})
  505. return { logs: [], value }
  506. }
  507. const result = await runCode(ctx, 'program', { agent })
  508. expect(result.isError).toBe(false)
  509. expect((result.content[0] as { text: string }).text).toBe(`${long}\n[reasoning content]`)
  510. const dispatch = events.find(event => event.type === 'tool/code-dispatch')?.data as SessionEventMap['tool/code-dispatch']
  511. expect(dispatch.resultSummary.length).toBe(201)
  512. expect(dispatch.resultSummary.endsWith('…')).toBe(true)
  513. })
  514. it('normalizes the session workspace root before bounding durable result summaries', async () => {
  515. const { ctx, runtime } = await setup({ mode: 'code' })
  516. ctx.tools.register(defineTool({
  517. name: 'workspace_path',
  518. description: 'Return a path beneath the session workspace.',
  519. parameters: {},
  520. execute(_args, exec) {
  521. const cwd = exec.agent?.session.header.cwd ?? ''
  522. return Promise.resolve([{ type: 'text' as const, text: `<path>${cwd}/nested/task.txt</path>\n${'x'.repeat(240)}` }])
  523. },
  524. }))
  525. runtime.behavior = async request => ({
  526. logs: [],
  527. value: await request.bindings[0]!.functions.workspace_path!({}),
  528. })
  529. const short = fakeAgent({ cwd: '/tmp/workspace' })
  530. const long = fakeAgent({ cwd: `/tmp/${'long-segment/'.repeat(30)}workspace` })
  531. const shortResult = await runCode(ctx, 'program', { agent: short.agent })
  532. const longResult = await runCode(ctx, 'program', { agent: long.agent })
  533. const shortDispatch = short.events[0]!.data as SessionEventMap['tool/code-dispatch']
  534. const longDispatch = long.events[0]!.data as SessionEventMap['tool/code-dispatch']
  535. expect(shortResult.content).not.toEqual(longResult.content)
  536. expect(shortDispatch.resultSummary).toBe(longDispatch.resultSummary)
  537. expect(shortDispatch.resultSummary).toHaveLength(201)
  538. expect(shortDispatch.resultSummary).toMatch(/^<path>\.\/nested\/task\.txt<\/path>\n.+…$/)
  539. })
  540. it('leaves result summaries unchanged when a session cwd is absent or is the filesystem root', async () => {
  541. const { ctx, runtime } = await setup({ mode: 'code' })
  542. registerEcho(ctx)
  543. runtime.behavior = async request => ({
  544. logs: [],
  545. value: await request.bindings[0]!.functions.echo!({ value: '/workspace/value' }),
  546. })
  547. const absent = fakeAgent({})
  548. const root = fakeAgent({ cwd: '/' })
  549. await runCode(ctx, 'program', { agent: absent.agent })
  550. await runCode(ctx, 'program', { agent: root.agent })
  551. expect((absent.events[0]!.data as SessionEventMap['tool/code-dispatch']).resultSummary).toBe('echo:/workspace/value')
  552. expect((root.events[0]!.data as SessionEventMap['tool/code-dispatch']).resultSummary).toBe('echo:/workspace/value')
  553. })
  554. it('rejects undefined, JSON-throwing, and JSON-unrepresentable binding arguments BEFORE dispatch', async () => {
  555. const { ctx, runtime } = await setup({ mode: 'code' })
  556. const calls = registerEcho(ctx)
  557. const { agent, events } = fakeAgent()
  558. runtime.behavior = async (request) => {
  559. const echo = request.bindings[0]!.functions.echo!
  560. const catchMessage = (promise: Promise<unknown>) => promise.then(() => 'resolved', (error: unknown) => error instanceof Error ? error.message : String(error))
  561. return {
  562. logs: [],
  563. value: [
  564. // Root undefined must reject up front: the event log rejects it as
  565. // data, and nothing may execute unlogged.
  566. await catchMessage(echo(undefined)),
  567. // A toJSON that throws a NON-Error propagates out of JSON.stringify.
  568. await catchMessage(echo({ toJSON() { throw 'raw-throw' } })),
  569. // A bare function is a value JSON cannot represent at all.
  570. await catchMessage(echo(() => 1)),
  571. ].join(' | '),
  572. }
  573. }
  574. const result = await runCode(ctx, 'program', { agent })
  575. const text = (result.content[0] as { text: string }).text
  576. expect(text).toContain('call the tool with an arguments object')
  577. expect(text).toContain('JSON-serializable: raw-throw')
  578. expect(text).toContain('a value JSON cannot represent')
  579. // None of the three dispatched, none logged.
  580. expect(calls).toEqual([])
  581. expect(events.filter(event => event.type === 'tool/code-dispatch')).toEqual([])
  582. })
  583. it('logs the value the tool RECEIVED even when the tool mutates its arguments', async () => {
  584. const { ctx, runtime } = await setup({ mode: 'code' })
  585. const { agent, events } = fakeAgent()
  586. ctx.tools.register(defineTool({
  587. name: 'mutator',
  588. description: 'Mutates its own args object.',
  589. parameters: { list: { type: 'array', required: true } },
  590. execute(args) {
  591. args.list.push('injected-by-tool')
  592. return Promise.resolve([{ type: 'text' as const, text: 'mutated' }])
  593. },
  594. }))
  595. runtime.behavior = async (request) => {
  596. await request.bindings[0]!.functions.mutator!({ list: ['original'] })
  597. return { logs: [] }
  598. }
  599. const result = await runCode(ctx, 'program', { agent })
  600. expect(result.isError).toBe(false)
  601. const dispatch = events.find(event => event.type === 'tool/code-dispatch')?.data as SessionEventMap['tool/code-dispatch']
  602. expect(dispatch.arguments).toEqual({ list: ['original'] })
  603. })
  604. it('exposes a tool named __proto__ as an ordinary own binding', async () => {
  605. const { ctx, runtime } = await setup({ mode: 'code' })
  606. ctx.tools.register(defineTool({
  607. name: '__proto__',
  608. description: 'A prototype-colliding tool name.',
  609. parameters: {},
  610. execute() { return Promise.resolve([{ type: 'text' as const, text: 'proto-tool-ok' }]) },
  611. }))
  612. runtime.behavior = async (request) => {
  613. const functions = request.bindings[0]!.functions
  614. expect(Object.getPrototypeOf(functions)).toBeNull()
  615. const value = await functions['__proto__']!({})
  616. return { logs: [], value }
  617. }
  618. const result = await runCode(ctx, 'program')
  619. expect(result.isError).toBe(false)
  620. expect(result.content[0]).toEqual({ type: 'text', text: 'proto-tool-ok' })
  621. })
  622. it('renders a non-string completion value inspect-style', async () => {
  623. const { ctx, runtime } = await setup({ mode: 'code' })
  624. runtime.behavior = () => Promise.resolve({ logs: [], value: { n: 42 } })
  625. const result = await runCode(ctx, 'program')
  626. expect((result.content[0] as { text: string }).text).toBe('{ n: 42 }')
  627. })
  628. it('reports a pre-aborted outer signal as the run failure without dispatching anything', async () => {
  629. const { ctx, runtime } = await setup({ mode: 'code' })
  630. const calls = registerEcho(ctx)
  631. runtime.behavior = (request) => {
  632. // The fake honors the seam contract for an already-aborted signal.
  633. if (request.signal?.aborted) return Promise.resolve({ logs: [], error: { kind: 'abort' as const, message: String(request.signal.reason) } })
  634. return Promise.resolve({ logs: [], value: 'unreachable' })
  635. }
  636. const controller = new AbortController()
  637. controller.abort('too-late')
  638. const result = await runCode(ctx, 'program', { signal: controller.signal })
  639. expect(result.isError).toBe(true)
  640. expect((result.content[0] as { text: string }).text).toContain('code run failed (abort)')
  641. expect(calls).toEqual([])
  642. })
  643. it('rejects a binding invoked after the run is over without dispatching it', async () => {
  644. const { ctx, runtime } = await setup({ mode: 'code' })
  645. const calls = registerEcho(ctx)
  646. const controller = new AbortController()
  647. runtime.behavior = async (request) => {
  648. controller.abort('cancelled-mid-run')
  649. const message = await request.bindings[0]!.functions.echo!({ value: 'x' })
  650. .then(() => 'resolved', (error: unknown) => error instanceof Error ? error.message : String(error))
  651. return { logs: [], value: message }
  652. }
  653. const result = await runCode(ctx, 'program', { signal: controller.signal })
  654. expect(result.isError).toBe(false)
  655. expect((result.content[0] as { text: string }).text).toContain('not dispatched')
  656. expect(calls).toEqual([])
  657. })
  658. it('a tool/code-dispatch event never derives a model message', () => {
  659. const session = new Session(SessionId('code-mode-derive'))
  660. session.append('user/message', { content: [{ type: 'text', text: 'hi' }], source: { kind: 'user' } }, { surfaceOp: 'append' })
  661. session.append('tool/code-dispatch', {
  662. parentCallId: CallId('p1'),
  663. subCallId: CallId('p1:code:1'),
  664. name: 'echo',
  665. arguments: { value: 'x' },
  666. isError: false,
  667. resultSummary: 'echo:x',
  668. })
  669. const derived = session.deriveMessages()
  670. expect(derived).toHaveLength(1)
  671. expect(derived[0]?.role).toBe('user')
  672. })
  673. it('defaults to native mode under direct construction with no config', async () => {
  674. const ctx = new Context()
  675. await ctx.plugin(SystemPrompt, {})
  676. const registry = new ToolRegistry(ctx)
  677. expect(registry.get(RUN_CODE_NAME)).toBeUndefined()
  678. const assembly = await ctx.systemPrompt.assemble()
  679. expect(assembly.sections.some(section => section.name === 'tools:sdk')).toBe(false)
  680. })
  681. })