web-agent-presets.e2e.ts 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. import { randomUUID } from 'node:crypto'
  2. import { mkdir, mkdtemp, readFile, stat, writeFile } from 'node:fs/promises'
  3. import { tmpdir } from 'node:os'
  4. import { fileURLToPath } from 'node:url'
  5. import { dirname, join } from 'node:path'
  6. import { Context } from '@deepseek-ai/cordis'
  7. import { boot, healProfilesModuleFallback, loadOverlayPatches } from '@deepseek-ai/dsh-app-boot'
  8. import { provideCmdline } from '@deepseek-ai/dsh-cmdline'
  9. import { SessionId } from '@deepseek-ai/dsh-session'
  10. import type { Agent } from '@deepseek-ai/dsh-agent'
  11. import type { PatchOptions } from '@deepseek-ai/cordis-plugin-include'
  12. import { afterAll, beforeAll, describe, expect, it } from 'vitest'
  13. import { settingsNamespace } from '@deepseek-ai/dsh-settings'
  14. import { resolveSessionPreset, SETTINGS_NAMESPACE } from '@deepseek-ai/dsh-agent-presets'
  15. import { applyChildComposition, childSessionMeta } from '@deepseek-ai/dsh-subagent'
  16. import { CallId } from '@deepseek-ai/dsh-llm'
  17. import type {} from '@deepseek-ai/dsh-compaction-basic'
  18. import type {} from '@deepseek-ai/dsh-skill'
  19. import type {} from '@deepseek-ai/dsh-tools'
  20. // Type-only: resolves `ctx.get('sessionProjections')` and `ctx.get('tokenMeter')`.
  21. import type {} from '@deepseek-ai/dsh-session-projection'
  22. import type {} from '@deepseek-ai/dsh-token-meter'
  23. const CONFIG_DIR = fileURLToPath(new URL('../config/', import.meta.url))
  24. const REPO_ROOT = fileURLToPath(new URL('../../..', import.meta.url))
  25. /** The shipped Web surface: the dsh-base and dsh-web-app bundle patches over an empty preset root. */
  26. const BASE_PATCH = join(REPO_ROOT, 'packages/bundle/base/cordis.patch.yml')
  27. const WEB_PATCH = join(REPO_ROOT, 'packages/bundle/web-app/cordis.patch.yml')
  28. /** The installation anchor whose dependency surface the preset module fallback mirrors. */
  29. const INSTALL_ANCHOR = join(REPO_ROOT, 'apps/cli/package.json')
  30. const EXAMPLES_INSTALL_ANCHOR = join(REPO_ROOT, 'examples/package.json')
  31. const MINIMAL_PROMPT = 'You are a helpful software engineer assistant.'
  32. const MINIMAL_BASH_DESCRIPTION = `Run commands in a bash shell
  33. * When invoking this tool, the contents of the "command" parameter does NOT need to be XML-escaped.
  34. * You don't have access to the internet via this tool.
  35. * You do have access to a mirror of common linux and python packages via apt and pip.
  36. * State is persistent across command calls and discussions with the user.
  37. * To inspect a particular line range of a file, e.g. lines 10-25, try 'sed -n 10,25p /path/to/the/file'.
  38. * Please avoid commands that may produce a very large amount of output.
  39. * Please run long lived commands in the background, e.g. 'sleep 10 &' or start a server in the background.`
  40. /**
  41. * Boot the shipped Web composition, minus the rows that would bind a port,
  42. * touch the network, or write outside the test. Everything that decides an
  43. * agent's capabilities is the real thing, including both shipped presets.
  44. */
  45. async function bootWeb(
  46. settingsFile: string,
  47. extra: PatchOptions[] = [],
  48. extraInstallAnchor?: string,
  49. ): Promise<Context> {
  50. const storageRoot = join(dirname(settingsFile), 'storages')
  51. const patches: PatchOptions[] = [
  52. ...loadOverlayPatches('dsh-test', BASE_PATCH),
  53. ...loadOverlayPatches('dsh-test', WEB_PATCH),
  54. // The settings row defaults to `$DSH_HOME/settings.yaml`. Left alone it
  55. // reads the developer's own document — and since the default preset is a
  56. // setting, a stored `agent-presets.default` would decide this file's
  57. // outcome. Point it at a temp file for the same reason the roster below
  58. // names only the shipped root.
  59. { id: 'settings', config: { path: settingsFile, watch: false } },
  60. // storage-json's root is anchored to the real $DSH_HOME. Unpinned, this
  61. // file writes the developer's own `~/.dsh/storages/` — and then reads it
  62. // back on the next run, so a stored document from any other build decides
  63. // this test's boot. Same reason the settings row above is pinned.
  64. { id: 'storage-json', config: { root: storageRoot } },
  65. // Host rows with side effects outside this process: a bound port, a served
  66. // asset tree, a telemetry exporter. `api-gateway` and `directory-picker`
  67. // stay ENABLED on purpose — the api-proxy is the host row that injects
  68. // `subagents`, `workspace`, and the rest of the agent plane, so disabling
  69. // it would hide exactly the breakage this file exists to catch: a service
  70. // moved into the presets that a host row still waits for. The boot audit
  71. // is that assertion.
  72. { id: 'webserver', disabled: true },
  73. // The web bundle's runtime row injects `webServer`, so it cannot
  74. // activate without the bound port disabled above. It owns dist serving
  75. // and the URL prompt line — surface glue, not anything that decides an
  76. // agent's capabilities, which is all this file asserts.
  77. { id: 'web-runtime', disabled: true },
  78. { id: 'session-telemetry-otel', disabled: true },
  79. // A deployment-level skill on the host registry's GLOBAL layer — the same
  80. // registration shape a repository plugin's skill root uses. The layered
  81. // skills test below proves it reaches preset-composed agents.
  82. { id: 'skill-badge', disabled: false },
  83. { id: 'modules', disabled: true },
  84. { id: 'connection', disabled: true },
  85. // The always-on reload chain waits for the browser roster and bound port
  86. // disabled above.
  87. { id: 'client-hmr', disabled: true },
  88. // The shipped `-auto` chooser resolves its interaction from a running
  89. // host and so waits for the webserver disabled above; the browse variant
  90. // supplies `directoryPicker` without one.
  91. { id: 'directory-picker', disabled: true },
  92. { insert: [
  93. { id: 'directory-picker-browse', name: '@deepseek-ai/dsh-host-directory-picker-browse' },
  94. { id: 'ui-directory-picker-browse', name: '@deepseek-ai/dsh-client-ui-directory-picker-browse' },
  95. ] },
  96. // The roster AppCLIEntry would patch in; only the shipped root, so a
  97. // developer's own `~/.dsh/.preset` cannot change this test's outcome.
  98. // `default` here is the COMPOSITION default — the base layer the settings
  99. // document overrides.
  100. {
  101. id: 'agent-presets',
  102. config: {
  103. default: 'standard',
  104. roots: [{ path: join(CONFIG_DIR, 'agent-presets'), trust: 'system' }],
  105. includeUserRoot: false,
  106. },
  107. },
  108. ...extra,
  109. ]
  110. // The surface is patch layers over an empty preset root, so the root sits
  111. // outside this workspace and bare plugin names cannot resolve by Node's
  112. // upward walk. The flat fallback the preset boot maintains is what makes
  113. // them resolvable — the same mechanism, not a test-only shim.
  114. const home = dirname(settingsFile)
  115. healProfilesModuleFallback(INSTALL_ANCHOR, home)
  116. if (extraInstallAnchor !== undefined) healProfilesModuleFallback(extraInstallAnchor, home)
  117. const profileDir = join(home, 'profiles', 'spec')
  118. await mkdir(profileDir, { recursive: true })
  119. const rootConfig = join(profileDir, 'cordis.yml')
  120. await writeFile(rootConfig, '[]\n')
  121. return await boot('dsh-test', rootConfig, patches, (bootCtx) => {
  122. provideCmdline(bootCtx, { args: [], exit: () => {} })
  123. })
  124. }
  125. const toolNames = (ctx: Context, agent?: Agent): string[] =>
  126. ctx.tools.schemas(agent).map(schema => schema.name).sort()
  127. function toolParameterNames(ctx: Context, agent: Agent, toolName: string): string[] {
  128. const schema = ctx.tools.schemas(agent).find(tool => tool.name === toolName)
  129. if (schema === undefined) throw new Error(`missing tool schema ${toolName}`)
  130. const properties = schema.parameters.properties
  131. if (typeof properties !== 'object' || properties === null || Array.isArray(properties)) {
  132. throw new Error(`${toolName} has invalid parameter properties`)
  133. }
  134. return Object.keys(properties).sort()
  135. }
  136. function enablePresetTool(composition: string, id: string): string {
  137. const row = ` - id: ${id}\n`
  138. const start = composition.indexOf(row)
  139. if (start < 0) throw new Error(`missing preset row ${id}`)
  140. const end = composition.indexOf('\n - id:', start + row.length)
  141. const disabled = composition.indexOf(' disabled: true\n', start)
  142. if (disabled < 0 || (end >= 0 && disabled > end)) {
  143. throw new Error(`preset row ${id} is not disabled`)
  144. }
  145. return composition.slice(0, disabled) + composition.slice(disabled + ' disabled: true\n'.length)
  146. }
  147. let ctx: Context
  148. beforeAll(async () => {
  149. const settingsFile = join(await mkdtemp(join(tmpdir(), 'dsh-web-presets-')), 'settings.yaml')
  150. await writeFile(settingsFile, '{}\n')
  151. ctx = await bootWeb(settingsFile)
  152. }, 120_000)
  153. describe('the shipped Web composition', () => {
  154. it('leaves the global tool layer empty', () => {
  155. // Every model-facing tool belongs to a preset, `ask_user_question`
  156. // included: a tool in the global layer reaches EVERY agent regardless of
  157. // which preset composed it, so a two-tool benchmark surface would really
  158. // present three. A regression here means an agent-plane row came back to
  159. // the host composition.
  160. expect(toolNames(ctx)).toEqual([])
  161. })
  162. it('keeps the token meter and its context-meter projections on the host plane', async () => {
  163. // Read before any preset in this file mounts, which is what makes this an
  164. // ownership assertion rather than a mount-order coincidence: a preset-side
  165. // meter sits behind an `isolate` realm and is invisible to `ctx.get`.
  166. //
  167. // The projection registry is process-wide rather than scope-layered, so a
  168. // preset-side meter would also make the browser's context meter appear for
  169. // a `minimal` session the moment some OTHER session mounted a preset that
  170. // carries one, and vanish entirely in a process that only ever ran
  171. // `minimal`. Host ownership is what makes the meter a per-session fact.
  172. expect(ctx.get('tokenMeter')).toBeDefined()
  173. const projections = ctx.get('sessionProjections')
  174. if (projections === undefined) throw new Error('the Web composition must compose a projection registry')
  175. const handle = await ctx.agents.create({
  176. sessionId: SessionId('preset-minimal-meter'),
  177. setup: agentCtx => ctx.agentPresets.mount(agentCtx, 'minimal').then(() => undefined),
  178. })
  179. try {
  180. // A subset assertion: `tasks`, `goal`, and the rest register into the
  181. // same process-wide table, and this is about the meter's three units.
  182. expect(Object.keys(projections.snapshot(handle.agent.session).values))
  183. .toEqual(expect.arrayContaining(['contextBreakdown', 'contextPressure', 'tokenUsage']))
  184. } finally {
  185. await handle.dispose()
  186. }
  187. })
  188. it('supplies both shipped presets, and only those, from the system root', async () => {
  189. const listed = await ctx.agentPresets.list()
  190. expect(listed.map(preset => preset.id).sort()).toEqual(['code', 'cordis', 'minimal', 'standard'])
  191. expect(listed.every(preset => preset.trust === 'system')).toBe(true)
  192. expect(ctx.agentPresets.defaultId).toBe('standard')
  193. })
  194. it('composes the full agent from `standard`', async () => {
  195. const handle = await ctx.agents.create({
  196. sessionId: SessionId('preset-standard'),
  197. setup: agentCtx => ctx.agentPresets.mount(agentCtx, 'standard').then(() => undefined),
  198. })
  199. try {
  200. // The EXACT catalog, not a spot-check: an omission is this design's
  201. // quietest failure mode, because a row that registers into the wrong
  202. // layer mounts cleanly and simply contributes nothing. `glob`/`grep` are
  203. // excluded for the reason the TUI composition e2e excludes them — they
  204. // depend on ripgrep being present on the machine.
  205. expect(toolNames(ctx, handle.agent).filter(name => name !== 'glob' && name !== 'grep')).toEqual([
  206. 'ask_user_question', 'bash', 'create_goal', 'edit', 'exit_plan_mode',
  207. 'get_goal', 'interrupt_agent', 'job_kill', 'job_list', 'job_output', 'list_agents', 'ralph', 'read', 'read_image', 'send_message', 'skill',
  208. 'subagent', 'subagent_fork', 'todo_write', 'update_goal', 'web_search',
  209. 'workflow', 'write',
  210. ])
  211. } finally {
  212. await handle.dispose()
  213. }
  214. })
  215. it('composes the exact RL prompt and two tools from `minimal`', async () => {
  216. const handle = await ctx.agents.create({
  217. sessionId: SessionId('preset-minimal'),
  218. setup: agentCtx => ctx.agentPresets.mount(agentCtx, 'minimal').then(() => undefined),
  219. })
  220. try {
  221. const assembly = await ctx.systemPrompt.assemble({ scope: handle.agent })
  222. expect(assembly.sections).toEqual([
  223. { name: 'deployment:persona', text: MINIMAL_PROMPT },
  224. ])
  225. expect(assembly.tools.map(tool => tool.name)).toEqual(['bash', 'str_replace_editor'])
  226. expect(assembly.tools.find(tool => tool.name === 'bash')?.description).toBe(MINIMAL_BASH_DESCRIPTION)
  227. expect(JSON.stringify(assembly.tools.find(tool => tool.name === 'str_replace_editor')?.parameters))
  228. .toContain('Absolute path')
  229. expect(ctx.agentPresets.serviceFor(handle.agent, 'compaction')).toBeUndefined()
  230. expect(handle.agent.ctx.get('compaction')).toBeUndefined()
  231. } finally {
  232. await handle.dispose()
  233. }
  234. })
  235. it('keeps two differently composed sessions independent', async () => {
  236. const full = await ctx.agents.create({
  237. sessionId: SessionId('preset-both-full'),
  238. setup: agentCtx => ctx.agentPresets.mount(agentCtx, 'standard').then(() => undefined),
  239. })
  240. const minimal = await ctx.agents.create({
  241. sessionId: SessionId('preset-both-minimal'),
  242. setup: agentCtx => ctx.agentPresets.mount(agentCtx, 'minimal').then(() => undefined),
  243. })
  244. try {
  245. expect(toolNames(ctx, minimal.agent)).toEqual(['bash', 'str_replace_editor'])
  246. expect(toolNames(ctx, full.agent).length).toBeGreaterThan(10)
  247. await minimal.dispose()
  248. // Tearing the minimal session down leaves the full one whole.
  249. expect(toolNames(ctx, full.agent).length).toBeGreaterThan(10)
  250. expect(toolNames(ctx)).toEqual([])
  251. } finally {
  252. await full.dispose()
  253. }
  254. })
  255. it('composes the cordis agent with its own toolset', async () => {
  256. const handle = await ctx.agents.create({
  257. sessionId: SessionId('preset-cordis'),
  258. setup: agentCtx => ctx.agentPresets.mount(agentCtx, 'cordis').then(() => undefined),
  259. })
  260. try {
  261. const tools = toolNames(ctx, handle.agent)
  262. // The self-referential toolset is what distinguishes this preset.
  263. expect(tools).toEqual(expect.arrayContaining([
  264. 'cordis_inspect_list', 'cordis_inspect_query', 'cordis_inspect_self',
  265. 'cordis_define', 'cordis_run', 'cordis_stop', 'cordis_undefine',
  266. ]))
  267. // And it keeps the standard agent's own tools rather than replacing them.
  268. expect(tools).toEqual(expect.arrayContaining(['bash', 'read', 'edit', 'skill']))
  269. expect(tools).not.toContain('str_replace_editor')
  270. // The preset's own authoring skill registers into ITS layer of the host
  271. // registry: the cordis agent's view carries it, the global view does not.
  272. const scoped = (await ctx.skills.list({ scope: handle.agent })).map(skill => skill.name)
  273. expect(scoped).toContain('editing-cordis-compositions')
  274. expect((await ctx.skills.list()).map(skill => skill.name)).not.toContain('editing-cordis-compositions')
  275. } finally {
  276. await handle.dispose()
  277. }
  278. })
  279. it('presents `code` as Code Mode without disturbing a native session beside it', async () => {
  280. const coded = await ctx.agents.create({
  281. sessionId: SessionId('preset-code'),
  282. setup: agentCtx => ctx.agentPresets.mount(agentCtx, 'code').then(() => undefined),
  283. })
  284. const native = await ctx.agents.create({
  285. sessionId: SessionId('preset-code-native'),
  286. setup: agentCtx => ctx.agentPresets.mount(agentCtx, 'standard').then(() => undefined),
  287. })
  288. try {
  289. // One tool reaches the MODEL: the transport. The registry's catalog for
  290. // this agent is unchanged — a code mode collapses the presentation, not
  291. // the capabilities — so the assembly is what carries the claim.
  292. const assembly = await ctx.systemPrompt.assemble({ scope: coded.agent })
  293. expect(assembly.tools.map(tool => tool.name)).toEqual(['run_code'])
  294. expect(toolNames(ctx, coded.agent)).not.toContain('str_replace_editor')
  295. const sdk = assembly.sections.find(section => section.name === 'tools:sdk')?.text ?? ''
  296. expect(sdk).not.toContain('str_replace_editor')
  297. expect(sdk).toContain('web_search')
  298. // The presentation is this agent's alone: the deployment default is
  299. // native, and the session composed from `standard` still sees it.
  300. const nativeAssembly = await ctx.systemPrompt.assemble({ scope: native.agent })
  301. expect(nativeAssembly.tools.map(tool => tool.name)).toContain('bash')
  302. expect(nativeAssembly.tools.map(tool => tool.name)).not.toContain('run_code')
  303. expect(nativeAssembly.sections.some(section => section.name === 'tools:sdk')).toBe(false)
  304. } finally {
  305. await native.dispose()
  306. await coded.dispose()
  307. }
  308. })
  309. it('keeps the self-referential toolset out of every other preset', async () => {
  310. const handle = await ctx.agents.create({
  311. sessionId: SessionId('preset-no-cordis'),
  312. setup: agentCtx => ctx.agentPresets.mount(agentCtx, 'standard').then(() => undefined),
  313. })
  314. try {
  315. // Editing the live runtime is opt-in per session, not ambient.
  316. expect(toolNames(ctx, handle.agent)).not.toContain('cordis_define')
  317. } finally {
  318. await handle.dispose()
  319. }
  320. })
  321. it('ships the composition-authoring skill inside the preset directory', async () => {
  322. // The preset's skill root is derived from its own `baseUrl`, so the skill
  323. // travels with the directory wherever the preset is installed.
  324. const skill = join(
  325. CONFIG_DIR, 'agent-presets', 'cordis', 'skills', 'editing-cordis-compositions', 'SKILL.md',
  326. )
  327. expect((await readFile(skill, 'utf8')).startsWith('---\nname: editing-cordis-compositions')).toBe(true)
  328. })
  329. it('merges the global skill layer into a preset agent\'s catalog, keeping local discovery preset-side', async () => {
  330. const proj = await mkdtemp(join(tmpdir(), 'dsh-preset-skill-proj-'))
  331. await mkdir(join(proj, '.dsh', 'skills', 'project-proof'), { recursive: true })
  332. await writeFile(join(proj, '.dsh', 'skills', 'project-proof', 'SKILL.md'), [
  333. '---',
  334. 'name: project-proof',
  335. 'description: Proves the preset layer discovers project skills beside global ones.',
  336. '---',
  337. '',
  338. 'Project proof body.',
  339. '',
  340. ].join('\n'))
  341. const handle = await ctx.agents.create({
  342. // Unique per run: the composition persists into the ambient DSH home,
  343. // and a fixed id would collide with a log an earlier run left there.
  344. sessionId: SessionId(`preset-skills-standard-${randomUUID()}`),
  345. setup: agentCtx => ctx.agentPresets.mount(agentCtx, 'standard').then(() => undefined),
  346. })
  347. try {
  348. // The host (global) view carries the deployment-level provider alone:
  349. // local discovery moved behind the presets with `skill-filesystem`.
  350. expect((await ctx.skills.list({ cwd: proj })).map(skill => skill.name)).toEqual(['dsh-badge'])
  351. // The standard agent's view merges the global layer with its preset's
  352. // own local discovery over the session cwd.
  353. const scoped = (await ctx.skills.list({ cwd: proj, scope: handle.agent })).map(skill => skill.name)
  354. expect(scoped).toContain('dsh-badge')
  355. expect(scoped).toContain('project-proof')
  356. // The preset's own loader tool resolves the global-layer skill.
  357. const loaded = await ctx.tools.execute({
  358. callId: CallId('preset-skills-load'),
  359. name: 'skill',
  360. arguments: { name: 'dsh-badge' },
  361. signal: new AbortController().signal,
  362. agent: handle.agent,
  363. })
  364. expect(loaded.isError).toBe(false)
  365. expect(JSON.stringify(loaded.content)).toContain('powered by dsh')
  366. } finally {
  367. await handle.dispose()
  368. }
  369. })
  370. it('shows a minimal agent the global layer but no loader tool', async () => {
  371. const handle = await ctx.agents.create({
  372. sessionId: SessionId(`preset-skills-minimal-${randomUUID()}`),
  373. setup: agentCtx => ctx.agentPresets.mount(agentCtx, 'minimal').then(() => undefined),
  374. })
  375. try {
  376. // Layer visibility is the registry's; whether an agent can USE skills
  377. // stays the preset's choice — minimal mounts no `tool-skill`, so its
  378. // tool table has no loader even though the global layer is readable.
  379. expect((await ctx.skills.list({ scope: handle.agent })).map(skill => skill.name)).toContain('dsh-badge')
  380. expect(toolNames(ctx, handle.agent)).toEqual(['bash', 'str_replace_editor'])
  381. } finally {
  382. await handle.dispose()
  383. }
  384. })
  385. it('never rewrites the preset file it composed from', async () => {
  386. // The Loader persists a tree whose plugin self-disposed, and tearing an
  387. // agent down disposes its whole subtree. Inherited, that rewrote the
  388. // shipped composition — truncating it to `[]` the first time a session
  389. // ended — so `PresetTree` refuses to write at all.
  390. const path = join(CONFIG_DIR, 'agent-presets', 'standard', 'agent.cordis.yml')
  391. const before = await readFile(path, 'utf8')
  392. const handle = await ctx.agents.create({
  393. sessionId: SessionId('preset-readonly'),
  394. setup: agentCtx => ctx.agentPresets.mount(agentCtx, 'standard').then(() => undefined),
  395. })
  396. await handle.dispose()
  397. // Slack, not a race the number has to win. The write is driven by the
  398. // Loader's fiber-unload listener, which fires as the subtree's fibers
  399. // settle rather than when `dispose()` resolves, and the Loader exposes no
  400. // flush to await. A regression writes synchronously inside that listener,
  401. // so any wait past settlement fails; a longer one only slows the test.
  402. await new Promise(resolve => setTimeout(resolve, 50))
  403. expect(await readFile(path, 'utf8')).toBe(before)
  404. })
  405. })
  406. describe('product subagent rows in user presets', () => {
  407. let productCtx: Context
  408. const ids = ['products-none', 'products-codex', 'products-claude', 'products-both'] as const
  409. beforeAll(async () => {
  410. const root = await mkdtemp(join(tmpdir(), 'dsh-product-presets-'))
  411. const userRoot = join(root, 'presets')
  412. const settingsFile = join(root, 'settings.yaml')
  413. const standard = await readFile(join(CONFIG_DIR, 'agent-presets', 'standard', 'agent.cordis.yml'), 'utf8')
  414. await writeFile(settingsFile, '{}\n')
  415. for (const id of ids) {
  416. let composition = standard
  417. if (id === 'products-codex' || id === 'products-both') {
  418. composition = enablePresetTool(composition, 'tool-subagent-codex')
  419. }
  420. if (id === 'products-claude' || id === 'products-both') {
  421. composition = enablePresetTool(composition, 'tool-subagent-claude-code')
  422. }
  423. const directory = join(userRoot, id)
  424. await mkdir(directory, { recursive: true })
  425. await writeFile(join(directory, 'agent.cordis.yml'), composition)
  426. }
  427. productCtx = await bootWeb(settingsFile, [
  428. { insert: [
  429. { id: 'subagent-codex', name: '@deepseek-ai/dsh-subagent-codex' },
  430. { id: 'subagent-claude-code', name: '@deepseek-ai/dsh-subagent-claude-code' },
  431. ] },
  432. {
  433. id: 'agent-presets',
  434. config: {
  435. default: 'standard',
  436. roots: [
  437. { path: join(CONFIG_DIR, 'agent-presets'), trust: 'system' },
  438. { path: userRoot, trust: 'user' },
  439. ],
  440. includeUserRoot: false,
  441. },
  442. },
  443. ], EXAMPLES_INSTALL_ANCHOR)
  444. }, 120_000)
  445. afterAll(async () => {
  446. await productCtx.fiber.dispose()
  447. })
  448. it('composes none, either product, or both without changing the shared host registry', async () => {
  449. const expected = new Map<string, string[]>([
  450. ['products-none', []],
  451. ['products-codex', ['subagent_codex']],
  452. ['products-claude', ['subagent_claude_code']],
  453. ['products-both', ['subagent_claude_code', 'subagent_codex']],
  454. ])
  455. expect(productCtx.subagents.list()).toEqual(expect.arrayContaining([
  456. 'spawn', 'fork', 'codex', 'claude-code',
  457. ]))
  458. for (const [id, productTools] of expected) {
  459. const handle = await productCtx.agents.create({
  460. sessionId: SessionId(`preset-${id}`),
  461. setup: agentCtx => productCtx.agentPresets.mount(agentCtx, id).then(() => undefined),
  462. })
  463. try {
  464. const tools = toolNames(productCtx, handle.agent)
  465. expect(tools.filter(name => name === 'subagent_codex' || name === 'subagent_claude_code'))
  466. .toEqual(productTools)
  467. expect(tools).toEqual(expect.arrayContaining(['job_kill', 'job_list', 'job_output']))
  468. for (const productTool of productTools) {
  469. expect(toolParameterNames(productCtx, handle.agent, productTool)).toEqual([
  470. 'description', 'prompt', 'run_in_background',
  471. ])
  472. }
  473. } finally {
  474. await handle.dispose()
  475. }
  476. }
  477. })
  478. it('applies a product-row edit only to later sessions on the preset', async () => {
  479. const preset = await productCtx.agentPresets.resolve('products-none')
  480. const original = await readFile(preset.path, 'utf8')
  481. const existing = await productCtx.agents.create({
  482. sessionId: SessionId('preset-product-generation-existing'),
  483. setup: agentCtx => productCtx.agentPresets.mount(agentCtx, 'products-none').then(() => undefined),
  484. })
  485. try {
  486. expect(toolNames(productCtx, existing.agent)).not.toContain('subagent_codex')
  487. await writeFile(preset.path, enablePresetTool(original, 'tool-subagent-codex'))
  488. const later = await productCtx.agents.create({
  489. sessionId: SessionId('preset-product-generation-later'),
  490. setup: agentCtx => productCtx.agentPresets.mount(agentCtx, 'products-none').then(() => undefined),
  491. })
  492. try {
  493. expect(toolNames(productCtx, existing.agent)).not.toContain('subagent_codex')
  494. expect(toolNames(productCtx, later.agent)).toContain('subagent_codex')
  495. } finally {
  496. await later.dispose()
  497. }
  498. } finally {
  499. await existing.dispose()
  500. await writeFile(preset.path, original)
  501. }
  502. })
  503. })
  504. describe('a switch survives the session', () => {
  505. it('records the choice so the log states what the agent runs', async () => {
  506. const handle = await ctx.agents.create({
  507. sessionId: SessionId('preset-switch-logged'),
  508. meta: { agentPreset: 'standard' },
  509. setup: agentCtx => ctx.agentPresets.mount(agentCtx, 'standard').then(() => undefined),
  510. })
  511. try {
  512. // The api-proxy's select does exactly this pair while the session is blank.
  513. await ctx.agentPresets.recompose(handle.agent.ctx, 'minimal')
  514. handle.agent.session.append('agent-preset/selected', { agentPreset: 'minimal' })
  515. // The header keeps the creation fact; the log carries what it runs.
  516. expect(handle.agent.session.header.agentPreset).toBe('standard')
  517. expect(resolveSessionPreset(handle.agent.session)).toBe('minimal')
  518. } finally {
  519. await handle.dispose()
  520. }
  521. })
  522. it('rebuilds a switched session from the log, not the creation header', () => {
  523. // The exact shape a resume reads back from disk: the header says standard,
  524. // the log records the switch the user made while the session was blank.
  525. const rebuilt = resolveSessionPreset({
  526. header: { version: 0, id: SessionId('x'), createdAt: 0, agentPreset: 'standard' },
  527. events: [
  528. { type: 'agent-preset/selected', seq: 1, time: 0, data: { agentPreset: 'minimal' } },
  529. { type: 'turn/start', seq: 2, time: 0, data: { turn: 0, trigger: { kind: 'message', source: { kind: 'user' } } } },
  530. ] as never,
  531. })
  532. // Reading the header alone would compose the creation-time preset over a
  533. // history another one produced — the replay the blank-only lock prevents.
  534. expect(rebuilt).toBe('minimal')
  535. })
  536. })
  537. describe('a forked session', () => {
  538. it('inherits the composition its seeded history was produced under', async () => {
  539. const parent = await ctx.agents.create({
  540. sessionId: SessionId('preset-fork-parent'),
  541. meta: { agentPreset: 'minimal' },
  542. setup: agentCtx => ctx.agentPresets.mount(agentCtx, 'minimal').then(() => undefined),
  543. })
  544. const inherited = resolveSessionPreset(parent.agent.session)
  545. const child = await ctx.agents.create({
  546. sessionId: SessionId('preset-fork-child'),
  547. meta: {
  548. parentSession: SessionId('preset-fork-parent'),
  549. seedLength: 0,
  550. ...inherited === undefined ? {} : { agentPreset: inherited },
  551. },
  552. setup: agentCtx => ctx.agentPresets.mount(agentCtx, inherited).then(() => undefined),
  553. })
  554. try {
  555. // Composing nothing would leave the child empty: this layer moved every
  556. // model-facing row out of the host plane, so there is nothing to inherit
  557. // for free any more.
  558. expect(toolNames(ctx, child.agent)).toEqual(toolNames(ctx, parent.agent))
  559. expect(toolNames(ctx, child.agent).length).toBeGreaterThan(0)
  560. } finally {
  561. await child.dispose()
  562. await parent.dispose()
  563. }
  564. })
  565. })
  566. describe('a delegated child', () => {
  567. it('runs on the composition its parent runs on', async () => {
  568. const parent = await ctx.agents.create({
  569. sessionId: SessionId('preset-child-parent'),
  570. meta: { agentPreset: 'standard' },
  571. setup: agentCtx => ctx.agentPresets.mount(agentCtx, 'standard').then(() => undefined),
  572. })
  573. // Exactly what an in-process subagent driver's creation window does.
  574. const child = await parent.agent.ctx.agents.create({
  575. sessionId: SessionId('preset-child'),
  576. meta: childSessionMeta(parent.agent, 1, 0),
  577. setup: (agentCtx) => {
  578. applyChildComposition(agentCtx, parent.agent, {})
  579. },
  580. })
  581. try {
  582. expect(toolNames(ctx, child.agent)).toEqual(toolNames(ctx, parent.agent))
  583. // The shipped `standard` preset is the whole coding agent; an empty
  584. // child here is the defect, and equality alone would not catch it.
  585. expect(toolNames(ctx, child.agent)).toContain('bash')
  586. expect(child.agent.session.header.agentPreset).toBe('standard')
  587. } finally {
  588. await child.dispose()
  589. await parent.dispose()
  590. }
  591. })
  592. it('follows a parent that switched preset while blank', async () => {
  593. const parent = await ctx.agents.create({
  594. sessionId: SessionId('preset-child-switch-parent'),
  595. meta: { agentPreset: 'standard' },
  596. setup: agentCtx => ctx.agentPresets.mount(agentCtx, 'standard').then(() => undefined),
  597. })
  598. await ctx.agentPresets.recompose(parent.agent.ctx, 'minimal')
  599. const child = await parent.agent.ctx.agents.create({
  600. sessionId: SessionId('preset-child-switch'),
  601. meta: childSessionMeta(parent.agent, 1, 0),
  602. setup: (agentCtx) => {
  603. applyChildComposition(agentCtx, parent.agent, {})
  604. },
  605. })
  606. try {
  607. // The live scope chain is the authority, not the parent's creation
  608. // header — which still names `standard`.
  609. expect(toolNames(ctx, child.agent)).toEqual(toolNames(ctx, parent.agent))
  610. expect(child.agent.session.header.agentPreset).toBe('minimal')
  611. } finally {
  612. await child.dispose()
  613. await parent.dispose()
  614. }
  615. })
  616. })
  617. describe('a launcher that configures no writable root', () => {
  618. // The claim this default exists for, asserted through the real shipped
  619. // bundles rather than a hand-built context: `apps/cli` patches in only the
  620. // system root, and a person's own presets are found anyway because the
  621. // roster derives `<dshHome>/.agent-presets` itself. `$DSH_HOME` is pointed
  622. // at a temp home BEFORE boot — the derived root is resolved when the plugin
  623. // is constructed, and an unpinned run would read the developer's own.
  624. let derivedCtx: Context
  625. let previousHome: string | undefined
  626. beforeAll(async () => {
  627. const home = await mkdtemp(join(tmpdir(), 'dsh-preset-derived-'))
  628. previousHome = process.env.DSH_HOME
  629. process.env.DSH_HOME = home
  630. await mkdir(join(home, '.agent-presets', 'derived-mine'), { recursive: true })
  631. await writeFile(
  632. join(home, '.agent-presets', 'derived-mine', 'agent.cordis.yml'),
  633. '- id: tool-todo\n name: \'@deepseek-ai/dsh-tool-todo\'\n config:\n allowParallelInProgress: true\n',
  634. )
  635. const settingsFile = join(await mkdtemp(join(tmpdir(), 'dsh-preset-derived-settings-')), 'settings.yaml')
  636. await writeFile(settingsFile, '{}\n')
  637. // Only the shipped root, exactly what `composeProfile` supplies; the
  638. // writable one is the roster's own default rather than this patch's job.
  639. derivedCtx = await bootWeb(settingsFile, [{
  640. id: 'agent-presets',
  641. config: {
  642. default: 'standard',
  643. roots: [{ path: join(CONFIG_DIR, 'agent-presets'), trust: 'system' }],
  644. includeUserRoot: true,
  645. },
  646. }])
  647. }, 120_000)
  648. afterAll(async () => {
  649. if (previousHome === undefined) delete process.env.DSH_HOME
  650. else process.env.DSH_HOME = previousHome
  651. await derivedCtx.fiber.dispose()
  652. })
  653. it('discovers and mounts a preset the person authored under the harness home', async () => {
  654. const listed = await derivedCtx.agentPresets.list()
  655. const mine = listed.find(preset => preset.id === 'derived-mine')
  656. expect(mine).toMatchObject({ trust: 'user' })
  657. // Omitted rather than undefined: a healthy row carries no `broken` key.
  658. expect(mine?.broken).toBeUndefined()
  659. expect(derivedCtx.agentPresets.authorable).toBe(true)
  660. const handle = await derivedCtx.agents.create({
  661. sessionId: SessionId('preset-derived-root'),
  662. setup: agentCtx => derivedCtx.agentPresets.mount(agentCtx, 'derived-mine').then(() => undefined),
  663. })
  664. try {
  665. expect(toolNames(derivedCtx, handle.agent)).toContain('todo_write')
  666. } finally {
  667. await handle.dispose()
  668. }
  669. })
  670. })
  671. describe('authoring a preset on the shipped composition', () => {
  672. let authorCtx: Context
  673. let userRoot: string
  674. beforeAll(async () => {
  675. userRoot = join(await mkdtemp(join(tmpdir(), 'dsh-preset-authoring-')), 'profiles')
  676. const settingsFile = join(await mkdtemp(join(tmpdir(), 'dsh-preset-authoring-settings-')), 'settings.yaml')
  677. await writeFile(settingsFile, '{}\n')
  678. authorCtx = await bootWeb(settingsFile, [{
  679. id: 'agent-presets',
  680. config: {
  681. default: 'standard',
  682. roots: [
  683. { path: join(CONFIG_DIR, 'agent-presets'), trust: 'system' },
  684. // The root does not exist yet: a deployment whose user has authored
  685. // nothing is the normal first-run state.
  686. { path: userRoot, trust: 'user' },
  687. ],
  688. includeUserRoot: false,
  689. },
  690. }])
  691. })
  692. it('refuses to copy over or delete a shipped preset', async () => {
  693. await expect(authorCtx.agentPresets.copy('minimal', 'standard')).rejects.toThrow(/already exists/)
  694. await expect(authorCtx.agentPresets.remove('standard')).rejects.toThrow(/ships with the deployment/)
  695. })
  696. it.each(['../escape', 'a/b', '/abs', 'Upper'])('refuses the uncontainable id %j', async (id) => {
  697. // The id becomes a directory name under the user root, so containment is
  698. // checked on the id rather than on the joined path afterwards.
  699. await expect(authorCtx.agentPresets.copy('minimal', id)).rejects.toThrow()
  700. })
  701. it('copies a shipped preset a session then really composes from', async () => {
  702. await authorCtx.agentPresets.copy('minimal', 'my-agent', '我的模式')
  703. // Round-trips through the roster as a `user` row carrying the given name
  704. // and the source's description, over the source's own composition text.
  705. const preset = await authorCtx.agentPresets.resolve('my-agent')
  706. const source = await authorCtx.agentPresets.resolve('minimal')
  707. expect(preset.trust).toBe('user')
  708. expect(preset.name).toBe('我的模式')
  709. expect(preset.description).toBe(source.description)
  710. expect(await authorCtx.agentPresets.read('my-agent')).toBe(await authorCtx.agentPresets.read('minimal'))
  711. // Owner-only, in an owner-only directory: a composition is executable
  712. // configuration on a machine that may have other users.
  713. expect((await stat(preset.path)).mode & 0o777).toBe(0o600)
  714. const handle = await authorCtx.agents.create({
  715. sessionId: SessionId('preset-authored'),
  716. setup: agentCtx => authorCtx.agentPresets.mount(agentCtx, 'my-agent').then(() => undefined),
  717. })
  718. try {
  719. // The same tools the shipped `minimal` composes, from a directory copied
  720. // through the service into a root outside the installed harness.
  721. expect(toolNames(authorCtx, handle.agent)).toEqual(['bash', 'str_replace_editor'])
  722. } finally {
  723. await handle.dispose()
  724. }
  725. })
  726. it('deletes what it copied', async () => {
  727. await authorCtx.agentPresets.copy('minimal', 'doomed')
  728. await authorCtx.agentPresets.remove('doomed')
  729. expect((await authorCtx.agentPresets.list()).map(preset => preset.id)).not.toContain('doomed')
  730. })
  731. })
  732. /**
  733. * Which preset an unnamed session gets is a user setting layered over the
  734. * composition's own default. The package suite proves the layering against a
  735. * hand-built context; this proves it through the shipped `cordis.yml` — that
  736. * the roster and the settings provider are actually wired to each other, and
  737. * that the id the setting names is the one a session composes from.
  738. */
  739. describe('the default preset as a user setting', () => {
  740. it('composes an unnamed session from the stored default, not the composed one', async () => {
  741. expect(ctx.agentPresets.defaultId).toBe('standard')
  742. await ctx.settings.update(settingsNamespace(SETTINGS_NAMESPACE), { default: 'minimal' })
  743. try {
  744. expect(ctx.agentPresets.defaultId).toBe('minimal')
  745. const handle = await ctx.agents.create({
  746. sessionId: SessionId('preset-user-default'),
  747. setup: agentCtx => ctx.agentPresets.mount(agentCtx).then(() => undefined),
  748. })
  749. try {
  750. // `mount()` with no id resolves the effective default. Two tools, not
  751. // `standard`'s catalog: the setting decided the composition.
  752. expect(toolNames(ctx, handle.agent)).toEqual(['bash', 'str_replace_editor'])
  753. } finally {
  754. await handle.dispose()
  755. }
  756. } finally {
  757. // The context is shared with the rest of the file. `replace({})` drops
  758. // the user section wholesale so the field re-inherits the composition
  759. // base; `update` merges, and would leave the override standing.
  760. await ctx.settings.replace(settingsNamespace(SETTINGS_NAMESPACE), {})
  761. }
  762. expect(ctx.agentPresets.defaultId).toBe('standard')
  763. })
  764. })
  765. describe('a session keeps the preset it was created with', () => {
  766. it('refuses to adopt a live session under a different preset', async () => {
  767. const handle = await ctx.agents.create({
  768. sessionId: SessionId('preset-locked'),
  769. meta: { agentPreset: 'minimal' },
  770. setup: agentCtx => ctx.agentPresets.mount(agentCtx, 'minimal').then(() => undefined),
  771. })
  772. try {
  773. // The api-proxy guard reads exactly this: the header records what the
  774. // session runs, so naming anything else is a caller error rather than a
  775. // switch. Its history was produced under `minimal`'s two tools.
  776. expect(handle.agent.session.header.agentPreset).toBe('minimal')
  777. } finally {
  778. await handle.dispose()
  779. }
  780. })
  781. })