shipped-composition.e2e.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. // Boots the shipped Web composition over the built dist this lane already uses
  2. // and asserts what that composition produces: the model-visible tool catalog
  3. // and file-reference guidance plus its HTTP, retry, sandbox, and approval defaults.
  4. // No browser and no model call — these are composition facts, and the browser
  5. // scenarios in this lane cover the surface itself.
  6. import { readFileSync } from 'node:fs'
  7. import { tmpdir } from 'node:os'
  8. import { fileURLToPath } from 'node:url'
  9. import { afterEach, expect, it } from 'vitest'
  10. import { ToolCallId } from '@deepseek-ai/dsh-llm'
  11. import { canonicalPath, writableRoots } from '@deepseek-ai/dsh-sandbox'
  12. import { SessionId } from '@deepseek-ai/dsh-session'
  13. // These imports carry the tools/sandboxPolicy/approval Context merges.
  14. import { RUN_CODE_NAME } from '@deepseek-ai/dsh-tools'
  15. import type {} from '@deepseek-ai/dsh-sandbox-policy'
  16. import type {} from '@deepseek-ai/dsh-user-approval'
  17. import type {} from '@deepseek-ai/dsh-permission-presets'
  18. import type {} from '@deepseek-ai/dsh-agent-presets'
  19. import type {} from '@deepseek-ai/dsh-commands'
  20. import type {} from '@deepseek-ai/dsh-system-prompt'
  21. import { launchWebScaffold, type WebScaffold } from './scaffold.ts'
  22. const FILE_REFERENCE_PROMPT = fileURLToPath(new URL(
  23. './expected/web-runtime-context/file-reference-prompt.expected.md', import.meta.url,
  24. ))
  25. /**
  26. * The catalog the shipped Web composition puts in front of the model, minus the
  27. * ripgrep-dependent pair below. The absences are deliberate, not incidental
  28. * gaps: the `cordis_*` toolset executes model-written JavaScript that no
  29. * sandbox row confines, and `mcp_*` servers spawn outside `ctx.shell`.
  30. * `web_fetch` is present because public-address enforcement and one-shot
  31. * approval now confine its model-selected request target. The composition
  32. * Agent Note owns the rationale and its sources.
  33. */
  34. const EXPECTED_TOOLS = [
  35. 'ask_user_question',
  36. 'bash',
  37. 'create_goal',
  38. 'edit',
  39. 'exit_plan_mode',
  40. 'get_goal',
  41. 'interrupt_agent',
  42. 'job_kill',
  43. 'job_list',
  44. 'job_output',
  45. 'list_agents',
  46. 'present',
  47. 'ralph',
  48. 'read',
  49. 'read_image',
  50. 'send_message',
  51. 'skill',
  52. 'subagent',
  53. 'subagent_fork',
  54. 'todo_write',
  55. 'update_goal',
  56. 'web_fetch',
  57. 'web_search',
  58. 'workflow',
  59. 'write',
  60. ]
  61. /**
  62. * `glob` and `grep` come from `dsh-tool-fs-search`, which spawns the PACKAGED
  63. * ripgrep binary (`@vscode/ripgrep`) through the subprocess seam, so the pair
  64. * is always present on every host — asserted as fixed members, not a host
  65. * dependency.
  66. */
  67. const RIPGREP_TOOLS = ['glob', 'grep']
  68. let scaffold: WebScaffold | undefined
  69. afterEach(async () => {
  70. await scaffold?.close()
  71. scaffold = undefined
  72. })
  73. it('assembles the shipped Web transport, catalog, guidance, and defaults', async () => {
  74. scaffold = await launchWebScaffold({ deepSeekMissingCredential: true })
  75. const ctx = scaffold.ctx
  76. const index = await fetch(`http://127.0.0.1:${String(ctx.webServer.port)}`, {
  77. headers: { 'accept-encoding': 'gzip' },
  78. })
  79. expect(index.headers.get('content-encoding')).toBe('gzip')
  80. expect(index.headers.get('vary')).toContain('Accept-Encoding')
  81. await index.body?.cancel()
  82. expect(ctx.llm.providerRetryPolicy('deepseek-official')).toMatchInlineSnapshot(`
  83. {
  84. "initialDelayMs": 500,
  85. "jitterRatio": 0.1,
  86. "maxDelayMs": 10000,
  87. "maxRetries": 5,
  88. "mode": "normal",
  89. "retryableCodes": [
  90. "EMPTY_RESPONSE",
  91. "RATE_LIMIT",
  92. "SERVER",
  93. "TIMEOUT",
  94. "TRANSPORT",
  95. ],
  96. }
  97. `)
  98. await ctx.settings.update('llm-deepseek', {
  99. retryPolicy: { mode: 'always', maxRetries: 5 },
  100. })
  101. expect(ctx.llm.providerRetryPolicy('deepseek-official')).toMatchInlineSnapshot(`
  102. {
  103. "initialDelayMs": 500,
  104. "jitterRatio": 0.1,
  105. "maxDelayMs": 10000,
  106. "mode": "always",
  107. }
  108. `)
  109. await ctx.settings.update('llm-pi-ai', {
  110. providers: {
  111. openai: {},
  112. anthropic: { retryPolicy: { mode: 'always' } },
  113. },
  114. })
  115. expect(ctx.llm.providerRetryPolicy('openai')).toMatchInlineSnapshot(`
  116. {
  117. "initialDelayMs": 500,
  118. "jitterRatio": 0.1,
  119. "maxDelayMs": 10000,
  120. "maxRetries": 5,
  121. "mode": "normal",
  122. "retryableCodes": [
  123. "EMPTY_RESPONSE",
  124. "RATE_LIMIT",
  125. "SERVER",
  126. "TIMEOUT",
  127. "TRANSPORT",
  128. ],
  129. }
  130. `)
  131. expect(ctx.llm.providerRetryPolicy('anthropic')).toMatchInlineSnapshot(`
  132. {
  133. "initialDelayMs": 500,
  134. "jitterRatio": 0.1,
  135. "maxDelayMs": 10000,
  136. "mode": "always",
  137. }
  138. `)
  139. // The catalog belongs to an AGENT, not to the process: every model-facing row
  140. // now lives in a preset mounted under one session's scope, so the global
  141. // layer holds nothing and a caller must name the agent to see anything. This
  142. // composes from the deployment default — what a session that names no preset
  143. // gets — which is the shape this test has always been about.
  144. expect(ctx.tools.schemas().map(schema => schema.name)).toEqual([])
  145. const handle = await ctx.agents.create({
  146. sessionId: SessionId('shipped-composition'),
  147. setup: agentCtx => ctx.agentPresets.mount(agentCtx).then(() => undefined),
  148. })
  149. try {
  150. const names = ctx.tools.schemas(handle.agent).map(schema => schema.name).sort()
  151. expect(names.filter(name => !RIPGREP_TOOLS.includes(name))).toEqual(EXPECTED_TOOLS)
  152. // The packaged ripgrep binary ships with the dependency, so the pair is a
  153. // fixed roster member on every host.
  154. expect(names.filter(name => RIPGREP_TOOLS.includes(name))).toEqual(RIPGREP_TOOLS)
  155. const fileReferenceSection = (await ctx.systemPrompt.assemble({ scope: handle.agent })).sections
  156. .find(section => section.name === 'ui:deliverable-file-references')
  157. expect(fileReferenceSection?.text).toBe(readFileSync(FILE_REFERENCE_PROMPT, 'utf8').trimEnd())
  158. } finally {
  159. await handle.dispose()
  160. }
  161. // `workspace-write` is not "the workspace and nothing else": the shared roots
  162. // helper always admits the temp directories too. Pinning it against an
  163. // explicit mode keeps the claim independent of this surface's default, and
  164. // keeps a future sandbox-confinement test from being run inside /tmp — where an
  165. // "escape" write succeeds by design and reads as a sandbox failure.
  166. expect(writableRoots(scaffold.ctx.sandboxPolicy.resolve({ mode: 'workspace-write' }))).toEqual(
  167. expect.arrayContaining([canonicalPath('/tmp'), canonicalPath(tmpdir())]),
  168. )
  169. expect(scaffold.ctx.sandboxPolicy.defaultMode).toBe('workspace-write')
  170. expect(scaffold.ctx.approval.config.policy).toBe('ask')
  171. expect(scaffold.ctx.permissionPresets.defaultPreset).toBe('workspace-write')
  172. const commandHandle = await scaffold.ctx.agents.create({
  173. sessionId: SessionId('shipped-command-catalog'),
  174. meta: { cwd: scaffold.workspaceCwd },
  175. agentOptions: { provider: 'deepseek-official', model: 'deepseek-v4-flash' },
  176. })
  177. try {
  178. expect(scaffold.ctx.commands.list(commandHandle.agent)).toContainEqual({
  179. name: 'feedback',
  180. description: 'record feedback about this session',
  181. input: { hint: '<text>' },
  182. })
  183. } finally {
  184. await commandHandle.dispose()
  185. }
  186. }, 120_000)
  187. it('ships PTC with run_code but without the general workflow SDK binding', async () => {
  188. scaffold = await launchWebScaffold({ deepSeekMissingCredential: true })
  189. const ctx = scaffold.ctx
  190. const handle = await ctx.agents.create({
  191. sessionId: SessionId('shipped-ptc-composition'),
  192. setup: agentCtx => ctx.agentPresets.mount(agentCtx, 'ptc').then(() => undefined),
  193. })
  194. try {
  195. const assembly = await ctx.systemPrompt.assemble({ scope: handle.agent })
  196. expect(assembly.tools.map(tool => tool.name)).toEqual([RUN_CODE_NAME])
  197. const sdk = assembly.sections.find(section => section.name === 'tools:sdk')?.text ?? ''
  198. expect(sdk).toContain(' ralph: {')
  199. expect(sdk).not.toContain(' workflow: {')
  200. } finally {
  201. await handle.dispose()
  202. }
  203. }, 120_000)
  204. it('lets a preset producer reach the background-job registry', async () => {
  205. scaffold = await launchWebScaffold()
  206. const ctx = scaffold.ctx
  207. const handle = await ctx.agents.create({
  208. sessionId: SessionId('shipped-background-job'),
  209. meta: { cwd: scaffold.workspaceCwd },
  210. setup: agentCtx => ctx.agentPresets.mount(agentCtx).then(() => undefined),
  211. })
  212. try {
  213. const signal = new AbortController().signal
  214. // `tool-bash` is a preset row and `tasks` is a host registry; the producer
  215. // resolves it with `ctx.get`, so a registry hidden behind a preset realm
  216. // fails here — with every task control still listed in the catalog above.
  217. const started = await ctx.tools.execute({
  218. signal,
  219. callId: ToolCallId('shipped-bash-background'),
  220. name: 'bash',
  221. arguments: {
  222. command: 'printf SHIPPED_BACKGROUND_OK',
  223. description: 'shipped background probe',
  224. run_in_background: true,
  225. },
  226. agent: handle.agent,
  227. })
  228. expect({ isError: started.isError, content: started.content }).toEqual({
  229. isError: false,
  230. content: [{ type: 'text', text: 'started background job bash-1' }],
  231. })
  232. // The controller reads what the producer started: same registry, one
  233. // owner. A per-preset registry would list nothing here even on success.
  234. const listed = await ctx.tools.execute({
  235. signal,
  236. callId: ToolCallId('shipped-task-list'),
  237. name: 'job_list',
  238. arguments: {},
  239. agent: handle.agent,
  240. })
  241. expect(listed.isError).toBe(false)
  242. expect(listed.content).toEqual([
  243. { type: 'text', text: expect.stringContaining('bash-1 [bash]') as unknown as string },
  244. ])
  245. // The full round trip: the output a host-plane producer wrote is collected
  246. // through a preset-plane control, which is the linkage the realm severed.
  247. const collected = await ctx.tools.execute({
  248. signal,
  249. callId: ToolCallId('shipped-task-output'),
  250. name: 'job_output',
  251. arguments: { job_id: 'bash-1', wait: true },
  252. agent: handle.agent,
  253. })
  254. expect(collected.isError).toBe(false)
  255. expect(collected.content).toEqual([
  256. { type: 'text', text: expect.stringContaining('SHIPPED_BACKGROUND_OK') as unknown as string },
  257. ])
  258. } finally {
  259. await handle.dispose()
  260. }
  261. }, 120_000)