ptc.spec.ts 107 KB

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