shipped-composition.e2e.ts 10 KB

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