shipped-composition.e2e.ts 10 KB

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