web-agent-presets.e2e.ts 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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 'cordis'
  7. import { boot, healProfilesModuleFallback, loadOverlayPatches } from '@deepseek-ai/dsh-app-boot'
  8. import { SessionId } from '@deepseek-ai/dsh-session'
  9. import type { Agent } from '@deepseek-ai/dsh-agent'
  10. import type { PatchOptions } from '@cordisjs/plugin-include'
  11. import { beforeAll, describe, expect, it } from 'vitest'
  12. import { settingsNamespace } from '@deepseek-ai/dsh-settings'
  13. import { resolveSessionPreset, SETTINGS_NAMESPACE } from '@deepseek-ai/dsh-agent-presets'
  14. import { applyChildComposition, childSessionMeta } from '@deepseek-ai/dsh-subagent'
  15. import { CallId } from '@deepseek-ai/dsh-llm'
  16. import type {} from '@deepseek-ai/dsh-skill'
  17. import type {} from '@deepseek-ai/dsh-tools'
  18. const CONFIG_DIR = fileURLToPath(new URL('../config/', import.meta.url))
  19. const REPO_ROOT = fileURLToPath(new URL('../../..', import.meta.url))
  20. /** The shipped Web surface: the dsh-base and dsh-web-app bundle patches over an empty preset root. */
  21. const BASE_PATCH = join(REPO_ROOT, 'packages/bundle/base/cordis.patch.yml')
  22. const WEB_PATCH = join(REPO_ROOT, 'packages/bundle/web-app/cordis.patch.yml')
  23. /** The installation anchor whose dependency surface the preset module fallback mirrors. */
  24. const INSTALL_ANCHOR = join(REPO_ROOT, 'apps/cli/package.json')
  25. /**
  26. * Boot the shipped Web composition, minus the rows that would bind a port,
  27. * touch the network, or write outside the test. Everything that decides an
  28. * agent's capabilities is the real thing, including both shipped presets.
  29. */
  30. async function bootWeb(settingsFile: string, extra: PatchOptions[] = []): Promise<Context> {
  31. const storageRoot = join(dirname(settingsFile), 'storages')
  32. const patches: PatchOptions[] = [
  33. ...loadOverlayPatches('dsh-test', BASE_PATCH),
  34. ...loadOverlayPatches('dsh-test', WEB_PATCH),
  35. // The settings row defaults to `$DSH_HOME/settings.yaml`. Left alone it
  36. // reads the developer's own document — and since the default preset is a
  37. // setting, a stored `agent-presets.default` would decide this file's
  38. // outcome. Point it at a temp file for the same reason the roster below
  39. // names only the shipped root.
  40. { id: 'settings', config: { path: settingsFile, watch: false } },
  41. // storage-json's root is anchored to the real $DSH_HOME. Unpinned, this
  42. // file writes the developer's own `~/.dsh/storages/` — and then reads it
  43. // back on the next run, so a stored document from any other build decides
  44. // this test's boot. Same reason the settings row above is pinned.
  45. { id: 'storage-json', config: { root: storageRoot } },
  46. // Host rows with side effects outside this process: a bound port, a served
  47. // asset tree, a telemetry exporter. `api-gateway` and `directory-picker`
  48. // stay ENABLED on purpose — the api-proxy is the host row that injects
  49. // `subagents`, `workspace`, and the rest of the agent plane, so disabling
  50. // it would hide exactly the breakage this file exists to catch: a service
  51. // moved into the presets that a host row still waits for. The boot audit
  52. // is that assertion.
  53. { id: 'webserver', disabled: true },
  54. // The web bundle's runtime row injects `httpServer`, so it cannot
  55. // activate without the bound port disabled above. It owns dist serving
  56. // and the URL prompt line — surface glue, not anything that decides an
  57. // agent's capabilities, which is all this file asserts.
  58. { id: 'web-runtime', disabled: true },
  59. { id: 'telemetry-otel', disabled: true },
  60. // A deployment-level skill on the host registry's GLOBAL layer — the same
  61. // registration shape a repository plugin's skill root uses. The layered
  62. // skills test below proves it reaches preset-composed agents.
  63. { id: 'skill-badge', disabled: false },
  64. { id: 'modules', disabled: true },
  65. { id: 'connection', disabled: true },
  66. // The shipped `-auto` chooser resolves its interaction from a running
  67. // host and so waits for the webserver disabled above; the browse variant
  68. // supplies `directoryPicker` without one.
  69. { id: 'directory-picker', disabled: true },
  70. { insert: [{ id: 'directory-picker-browse', name: '@deepseek-ai/dsh-host-directory-picker-browse' }] },
  71. // The roster AppCLIEntry would patch in; only the shipped root, so a
  72. // developer's own `~/.dsh/.preset` cannot change this test's outcome.
  73. // `default` here is the COMPOSITION default — the base layer the settings
  74. // document overrides.
  75. {
  76. id: 'agent-presets',
  77. config: { default: 'standard', roots: [{ path: join(CONFIG_DIR, 'agent-presets'), trust: 'system' }] },
  78. },
  79. ...extra,
  80. ]
  81. // The surface is patch layers over an empty preset root, so the root sits
  82. // outside this workspace and bare plugin names cannot resolve by Node's
  83. // upward walk. The flat fallback the preset boot maintains is what makes
  84. // them resolvable — the same mechanism, not a test-only shim.
  85. const home = dirname(settingsFile)
  86. healProfilesModuleFallback(INSTALL_ANCHOR, home)
  87. const profileDir = join(home, 'profiles', 'spec')
  88. await mkdir(profileDir, { recursive: true })
  89. const rootConfig = join(profileDir, 'cordis.yml')
  90. await writeFile(rootConfig, '[]\n')
  91. return await boot('dsh-test', rootConfig, patches)
  92. }
  93. const toolNames = (ctx: Context, agent?: Agent): string[] =>
  94. ctx.tools.schemas(agent).map(schema => schema.name).sort()
  95. let ctx: Context
  96. beforeAll(async () => {
  97. const settingsFile = join(await mkdtemp(join(tmpdir(), 'dsh-web-presets-')), 'settings.yaml')
  98. await writeFile(settingsFile, '{}\n')
  99. ctx = await bootWeb(settingsFile)
  100. }, 120_000)
  101. describe('the shipped Web composition', () => {
  102. it('leaves the global tool layer empty', () => {
  103. // Every model-facing tool belongs to a preset, `ask_user_question`
  104. // included: a tool in the global layer reaches EVERY agent regardless of
  105. // which preset composed it, so a two-tool benchmark surface would really
  106. // present three. A regression here means an agent-plane row came back to
  107. // the host composition.
  108. expect(toolNames(ctx)).toEqual([])
  109. })
  110. it('supplies both shipped presets, and only those, from the system root', async () => {
  111. const listed = await ctx.agentPresets.list()
  112. expect(listed.map(preset => preset.id).sort()).toEqual(['code', 'cordis', 'minimal', 'standard'])
  113. expect(listed.every(preset => preset.trust === 'system')).toBe(true)
  114. expect(ctx.agentPresets.defaultId).toBe('standard')
  115. })
  116. it('composes the full agent from `standard`', async () => {
  117. const handle = await ctx.agents.create({
  118. sessionId: SessionId('preset-standard'),
  119. setup: agentCtx => ctx.agentPresets.mount(agentCtx, 'standard').then(() => undefined),
  120. })
  121. try {
  122. // The EXACT catalog, not a spot-check: an omission is this design's
  123. // quietest failure mode, because a row that registers into the wrong
  124. // layer mounts cleanly and simply contributes nothing. `glob`/`grep` are
  125. // excluded for the reason the TUI composition e2e excludes them — they
  126. // depend on ripgrep being present on the machine.
  127. expect(toolNames(ctx, handle.agent).filter(name => name !== 'glob' && name !== 'grep')).toEqual([
  128. 'ask_user_question', 'bash', 'create_goal', 'edit', 'exit_plan_mode',
  129. 'get_goal', 'interrupt_agent', 'list_agents', 'ralph', 'read', 'send_message', 'skill',
  130. 'str_replace_editor', 'subagent', 'subagent_fork', 'task_kill',
  131. 'task_list', 'task_output', 'todo_write', 'update_goal', 'web_search',
  132. 'workflow', 'write',
  133. ])
  134. } finally {
  135. await handle.dispose()
  136. }
  137. })
  138. it('composes exactly two tools from `minimal`', async () => {
  139. const handle = await ctx.agents.create({
  140. sessionId: SessionId('preset-minimal'),
  141. setup: agentCtx => ctx.agentPresets.mount(agentCtx, 'minimal').then(() => undefined),
  142. })
  143. try {
  144. // Exactly what the preset lists — nothing arrives from the host.
  145. expect(toolNames(ctx, handle.agent)).toEqual(['bash', 'str_replace_editor'])
  146. } finally {
  147. await handle.dispose()
  148. }
  149. })
  150. it('keeps two differently composed sessions independent', async () => {
  151. const full = await ctx.agents.create({
  152. sessionId: SessionId('preset-both-full'),
  153. setup: agentCtx => ctx.agentPresets.mount(agentCtx, 'standard').then(() => undefined),
  154. })
  155. const minimal = await ctx.agents.create({
  156. sessionId: SessionId('preset-both-minimal'),
  157. setup: agentCtx => ctx.agentPresets.mount(agentCtx, 'minimal').then(() => undefined),
  158. })
  159. try {
  160. expect(toolNames(ctx, minimal.agent)).toEqual(['bash', 'str_replace_editor'])
  161. expect(toolNames(ctx, full.agent).length).toBeGreaterThan(10)
  162. await minimal.dispose()
  163. // Tearing the minimal session down leaves the full one whole.
  164. expect(toolNames(ctx, full.agent).length).toBeGreaterThan(10)
  165. expect(toolNames(ctx)).toEqual([])
  166. } finally {
  167. await full.dispose()
  168. }
  169. })
  170. it('composes the cordis agent with its own toolset', async () => {
  171. const handle = await ctx.agents.create({
  172. sessionId: SessionId('preset-cordis'),
  173. setup: agentCtx => ctx.agentPresets.mount(agentCtx, 'cordis').then(() => undefined),
  174. })
  175. try {
  176. const tools = toolNames(ctx, handle.agent)
  177. // The self-referential toolset is what distinguishes this preset.
  178. expect(tools).toEqual(expect.arrayContaining(['cordis_inspect', 'cordis_mount', 'cordis_unmount']))
  179. // And it keeps the standard agent's own tools rather than replacing them.
  180. expect(tools).toEqual(expect.arrayContaining(['bash', 'read', 'edit', 'skill']))
  181. // The preset's own authoring skill registers into ITS layer of the host
  182. // registry: the cordis agent's view carries it, the global view does not.
  183. const scoped = (await ctx.skills.list({ scope: handle.agent })).map(skill => skill.name)
  184. expect(scoped).toContain('editing-cordis-compositions')
  185. expect((await ctx.skills.list()).map(skill => skill.name)).not.toContain('editing-cordis-compositions')
  186. } finally {
  187. await handle.dispose()
  188. }
  189. })
  190. it('presents `code` as Code Mode without disturbing a native session beside it', async () => {
  191. const coded = await ctx.agents.create({
  192. sessionId: SessionId('preset-code'),
  193. setup: agentCtx => ctx.agentPresets.mount(agentCtx, 'code').then(() => undefined),
  194. })
  195. const native = await ctx.agents.create({
  196. sessionId: SessionId('preset-code-native'),
  197. setup: agentCtx => ctx.agentPresets.mount(agentCtx, 'standard').then(() => undefined),
  198. })
  199. try {
  200. // One tool reaches the MODEL: the transport. The registry's catalog for
  201. // this agent is unchanged — a code mode collapses the presentation, not
  202. // the capabilities — so the assembly is what carries the claim.
  203. const assembly = await ctx.systemPrompt.assemble({ scope: coded.agent })
  204. expect(assembly.tools.map(tool => tool.name)).toEqual(['run_code'])
  205. expect(toolNames(ctx, coded.agent)).toContain('str_replace_editor')
  206. const sdk = assembly.sections.find(section => section.name === 'tools:sdk')?.text ?? ''
  207. expect(sdk).toContain('str_replace_editor')
  208. expect(sdk).toContain('web_search')
  209. // The presentation is this agent's alone: the deployment default is
  210. // native, and the session composed from `standard` still sees it.
  211. const nativeAssembly = await ctx.systemPrompt.assemble({ scope: native.agent })
  212. expect(nativeAssembly.tools.map(tool => tool.name)).toContain('bash')
  213. expect(nativeAssembly.tools.map(tool => tool.name)).not.toContain('run_code')
  214. expect(nativeAssembly.sections.some(section => section.name === 'tools:sdk')).toBe(false)
  215. } finally {
  216. await native.dispose()
  217. await coded.dispose()
  218. }
  219. })
  220. it('keeps the self-referential toolset out of every other preset', async () => {
  221. const handle = await ctx.agents.create({
  222. sessionId: SessionId('preset-no-cordis'),
  223. setup: agentCtx => ctx.agentPresets.mount(agentCtx, 'standard').then(() => undefined),
  224. })
  225. try {
  226. // Editing the live runtime is opt-in per session, not ambient.
  227. expect(toolNames(ctx, handle.agent)).not.toContain('cordis_mount')
  228. } finally {
  229. await handle.dispose()
  230. }
  231. })
  232. it('ships the composition-authoring skill inside the preset directory', async () => {
  233. // The preset's skill root is derived from its own `baseUrl`, so the skill
  234. // travels with the directory wherever the preset is installed.
  235. const skill = join(
  236. CONFIG_DIR, 'agent-presets', 'cordis', 'skills', 'editing-cordis-compositions', 'SKILL.md',
  237. )
  238. expect((await readFile(skill, 'utf8')).startsWith('---\nname: editing-cordis-compositions')).toBe(true)
  239. })
  240. it('merges the global skill layer into a preset agent\'s catalog, keeping local discovery preset-side', async () => {
  241. const proj = await mkdtemp(join(tmpdir(), 'dsh-preset-skill-proj-'))
  242. await mkdir(join(proj, '.dsh', 'skills', 'project-proof'), { recursive: true })
  243. await writeFile(join(proj, '.dsh', 'skills', 'project-proof', 'SKILL.md'), [
  244. '---',
  245. 'name: project-proof',
  246. 'description: Proves the preset layer discovers project skills beside global ones.',
  247. '---',
  248. '',
  249. 'Project proof body.',
  250. '',
  251. ].join('\n'))
  252. const handle = await ctx.agents.create({
  253. // Unique per run: the composition persists into the ambient DSH home,
  254. // and a fixed id would collide with a log an earlier run left there.
  255. sessionId: SessionId(`preset-skills-standard-${randomUUID()}`),
  256. setup: agentCtx => ctx.agentPresets.mount(agentCtx, 'standard').then(() => undefined),
  257. })
  258. try {
  259. // The host (global) view carries the deployment-level provider alone:
  260. // local discovery moved behind the presets with `skill-local`.
  261. expect((await ctx.skills.list({ cwd: proj })).map(skill => skill.name)).toEqual(['dsh-badge'])
  262. // The standard agent's view merges the global layer with its preset's
  263. // own local discovery over the session cwd.
  264. const scoped = (await ctx.skills.list({ cwd: proj, scope: handle.agent })).map(skill => skill.name)
  265. expect(scoped).toContain('dsh-badge')
  266. expect(scoped).toContain('project-proof')
  267. // The preset's own loader tool resolves the global-layer skill.
  268. const loaded = await ctx.tools.execute({
  269. callId: CallId('preset-skills-load'),
  270. name: 'skill',
  271. arguments: { name: 'dsh-badge' },
  272. signal: new AbortController().signal,
  273. agent: handle.agent,
  274. })
  275. expect(loaded.isError).toBe(false)
  276. expect(JSON.stringify(loaded.content)).toContain('powered by dsh')
  277. } finally {
  278. await handle.dispose()
  279. }
  280. })
  281. it('shows a minimal agent the global layer but no loader tool', async () => {
  282. const handle = await ctx.agents.create({
  283. sessionId: SessionId(`preset-skills-minimal-${randomUUID()}`),
  284. setup: agentCtx => ctx.agentPresets.mount(agentCtx, 'minimal').then(() => undefined),
  285. })
  286. try {
  287. // Layer visibility is the registry's; whether an agent can USE skills
  288. // stays the preset's choice — minimal mounts no `tool-skill`, so its
  289. // tool table has no loader even though the global layer is readable.
  290. expect((await ctx.skills.list({ scope: handle.agent })).map(skill => skill.name)).toContain('dsh-badge')
  291. expect(toolNames(ctx, handle.agent)).toEqual(['bash', 'str_replace_editor'])
  292. } finally {
  293. await handle.dispose()
  294. }
  295. })
  296. it('never rewrites the preset file it composed from', async () => {
  297. // The Loader persists a tree whose plugin self-disposed, and tearing an
  298. // agent down disposes its whole subtree. Inherited, that rewrote the
  299. // shipped composition — truncating it to `[]` the first time a session
  300. // ended — so `PresetTree` refuses to write at all.
  301. const path = join(CONFIG_DIR, 'agent-presets', 'standard', 'agent.cordis.yml')
  302. const before = await readFile(path, 'utf8')
  303. const handle = await ctx.agents.create({
  304. sessionId: SessionId('preset-readonly'),
  305. setup: agentCtx => ctx.agentPresets.mount(agentCtx, 'standard').then(() => undefined),
  306. })
  307. await handle.dispose()
  308. // Slack, not a race the number has to win. The write is driven by the
  309. // Loader's fiber-unload listener, which fires as the subtree's fibers
  310. // settle rather than when `dispose()` resolves, and the Loader exposes no
  311. // flush to await. A regression writes synchronously inside that listener,
  312. // so any wait past settlement fails; a longer one only slows the test.
  313. await new Promise(resolve => setTimeout(resolve, 50))
  314. expect(await readFile(path, 'utf8')).toBe(before)
  315. })
  316. it('gives each session its own persona', async () => {
  317. const handle = await ctx.agents.create({
  318. sessionId: SessionId('preset-persona'),
  319. setup: agentCtx => ctx.agentPresets.mount(agentCtx, 'minimal').then(() => undefined),
  320. })
  321. try {
  322. const assembly = await ctx.systemPrompt.assemble({ scope: handle.agent })
  323. expect(assembly.sections.find(section => section.name === 'deployment:persona')?.text)
  324. .toContain('You are a coding agent powered by')
  325. } finally {
  326. await handle.dispose()
  327. }
  328. })
  329. })
  330. describe('a switch survives the session', () => {
  331. it('records the choice so the log states what the agent runs', async () => {
  332. const handle = await ctx.agents.create({
  333. sessionId: SessionId('preset-switch-logged'),
  334. meta: { agentPreset: 'standard' },
  335. setup: agentCtx => ctx.agentPresets.mount(agentCtx, 'standard').then(() => undefined),
  336. })
  337. try {
  338. // The api-proxy's select does exactly this pair while the session is blank.
  339. await ctx.agentPresets.recompose(handle.agent.ctx, 'minimal')
  340. handle.agent.session.append('agent-preset/selected', { agentPreset: 'minimal' })
  341. // The header keeps the creation fact; the log carries what it runs.
  342. expect(handle.agent.session.header.agentPreset).toBe('standard')
  343. expect(resolveSessionPreset(handle.agent.session)).toBe('minimal')
  344. } finally {
  345. await handle.dispose()
  346. }
  347. })
  348. it('rebuilds a switched session from the log, not the creation header', () => {
  349. // The exact shape a resume reads back from disk: the header says standard,
  350. // the log records the switch the user made while the session was blank.
  351. const rebuilt = resolveSessionPreset({
  352. header: { version: 0, id: SessionId('x'), createdAt: 0, agentPreset: 'standard' },
  353. events: [
  354. { type: 'agent-preset/selected', seq: 1, time: 0, data: { agentPreset: 'minimal' } },
  355. { type: 'turn/start', seq: 2, time: 0, data: { turn: 0, trigger: { kind: 'message', source: { kind: 'user' } } } },
  356. ] as never,
  357. })
  358. // Reading the header alone would compose the creation-time preset over a
  359. // history another one produced — the replay the blank-only lock prevents.
  360. expect(rebuilt).toBe('minimal')
  361. })
  362. })
  363. describe('a forked session', () => {
  364. it('inherits the composition its seeded history was produced under', async () => {
  365. const parent = await ctx.agents.create({
  366. sessionId: SessionId('preset-fork-parent'),
  367. meta: { agentPreset: 'minimal' },
  368. setup: agentCtx => ctx.agentPresets.mount(agentCtx, 'minimal').then(() => undefined),
  369. })
  370. const inherited = resolveSessionPreset(parent.agent.session)
  371. const child = await ctx.agents.create({
  372. sessionId: SessionId('preset-fork-child'),
  373. meta: {
  374. parentSession: SessionId('preset-fork-parent'),
  375. seedLength: 0,
  376. ...inherited === undefined ? {} : { agentPreset: inherited },
  377. },
  378. setup: agentCtx => ctx.agentPresets.mount(agentCtx, inherited).then(() => undefined),
  379. })
  380. try {
  381. // Composing nothing would leave the child empty: this layer moved every
  382. // model-facing row out of the host plane, so there is nothing to inherit
  383. // for free any more.
  384. expect(toolNames(ctx, child.agent)).toEqual(toolNames(ctx, parent.agent))
  385. expect(toolNames(ctx, child.agent).length).toBeGreaterThan(0)
  386. } finally {
  387. await child.dispose()
  388. await parent.dispose()
  389. }
  390. })
  391. })
  392. describe('a delegated child', () => {
  393. it('runs on the composition its parent runs on', async () => {
  394. const parent = await ctx.agents.create({
  395. sessionId: SessionId('preset-child-parent'),
  396. meta: { agentPreset: 'standard' },
  397. setup: agentCtx => ctx.agentPresets.mount(agentCtx, 'standard').then(() => undefined),
  398. })
  399. // Exactly what an in-process subagent driver's creation window does.
  400. const child = await parent.agent.ctx.agents.create({
  401. sessionId: SessionId('preset-child'),
  402. meta: childSessionMeta(parent.agent, 1, 0),
  403. setup: (agentCtx) => {
  404. applyChildComposition(agentCtx, parent.agent, {})
  405. },
  406. })
  407. try {
  408. expect(toolNames(ctx, child.agent)).toEqual(toolNames(ctx, parent.agent))
  409. // The shipped `standard` preset is the whole coding agent; an empty
  410. // child here is the defect, and equality alone would not catch it.
  411. expect(toolNames(ctx, child.agent)).toContain('bash')
  412. expect(child.agent.session.header.agentPreset).toBe('standard')
  413. } finally {
  414. await child.dispose()
  415. await parent.dispose()
  416. }
  417. })
  418. it('follows a parent that switched preset while blank', async () => {
  419. const parent = await ctx.agents.create({
  420. sessionId: SessionId('preset-child-switch-parent'),
  421. meta: { agentPreset: 'standard' },
  422. setup: agentCtx => ctx.agentPresets.mount(agentCtx, 'standard').then(() => undefined),
  423. })
  424. await ctx.agentPresets.recompose(parent.agent.ctx, 'minimal')
  425. const child = await parent.agent.ctx.agents.create({
  426. sessionId: SessionId('preset-child-switch'),
  427. meta: childSessionMeta(parent.agent, 1, 0),
  428. setup: (agentCtx) => {
  429. applyChildComposition(agentCtx, parent.agent, {})
  430. },
  431. })
  432. try {
  433. // The live scope chain is the authority, not the parent's creation
  434. // header — which still names `standard`.
  435. expect(toolNames(ctx, child.agent)).toEqual(toolNames(ctx, parent.agent))
  436. expect(child.agent.session.header.agentPreset).toBe('minimal')
  437. } finally {
  438. await child.dispose()
  439. await parent.dispose()
  440. }
  441. })
  442. })
  443. describe('authoring a preset on the shipped composition', () => {
  444. let authorCtx: Context
  445. let userRoot: string
  446. beforeAll(async () => {
  447. userRoot = join(await mkdtemp(join(tmpdir(), 'dsh-preset-authoring-')), 'profiles')
  448. const settingsFile = join(await mkdtemp(join(tmpdir(), 'dsh-preset-authoring-settings-')), 'settings.yaml')
  449. await writeFile(settingsFile, '{}\n')
  450. authorCtx = await bootWeb(settingsFile, [{
  451. id: 'agent-presets',
  452. config: {
  453. default: 'standard',
  454. roots: [
  455. { path: join(CONFIG_DIR, 'agent-presets'), trust: 'system' },
  456. // The root does not exist yet: a deployment whose user has authored
  457. // nothing is the normal first-run state.
  458. { path: userRoot, trust: 'user' },
  459. ],
  460. },
  461. }])
  462. })
  463. it('refuses to copy over or delete a shipped preset', async () => {
  464. await expect(authorCtx.agentPresets.copy('minimal', 'standard')).rejects.toThrow(/already exists/)
  465. await expect(authorCtx.agentPresets.remove('standard')).rejects.toThrow(/ships with the deployment/)
  466. })
  467. it.each(['../escape', 'a/b', '/abs', 'Upper'])('refuses the uncontainable id %j', async (id) => {
  468. // The id becomes a directory name under the user root, so containment is
  469. // checked on the id rather than on the joined path afterwards.
  470. await expect(authorCtx.agentPresets.copy('minimal', id)).rejects.toThrow()
  471. })
  472. it('copies a shipped preset a session then really composes from', async () => {
  473. await authorCtx.agentPresets.copy('minimal', 'my-agent', '我的模式')
  474. // Round-trips through the roster as a `user` row carrying the given name
  475. // and the source's description, over the source's own composition text.
  476. const preset = await authorCtx.agentPresets.resolve('my-agent')
  477. const source = await authorCtx.agentPresets.resolve('minimal')
  478. expect(preset.trust).toBe('user')
  479. expect(preset.name).toBe('我的模式')
  480. expect(preset.description).toBe(source.description)
  481. expect(await authorCtx.agentPresets.read('my-agent')).toBe(await authorCtx.agentPresets.read('minimal'))
  482. // Owner-only, in an owner-only directory: a composition is executable
  483. // configuration on a machine that may have other users.
  484. expect((await stat(preset.path)).mode & 0o777).toBe(0o600)
  485. const handle = await authorCtx.agents.create({
  486. sessionId: SessionId('preset-authored'),
  487. setup: agentCtx => authorCtx.agentPresets.mount(agentCtx, 'my-agent').then(() => undefined),
  488. })
  489. try {
  490. // The same tools the shipped `minimal` composes, from a directory copied
  491. // through the service into a root outside the installed harness.
  492. expect(toolNames(authorCtx, handle.agent)).toEqual(['bash', 'str_replace_editor'])
  493. } finally {
  494. await handle.dispose()
  495. }
  496. })
  497. it('deletes what it copied', async () => {
  498. await authorCtx.agentPresets.copy('minimal', 'doomed')
  499. await authorCtx.agentPresets.remove('doomed')
  500. expect((await authorCtx.agentPresets.list()).map(preset => preset.id)).not.toContain('doomed')
  501. })
  502. })
  503. /**
  504. * Which preset an unnamed session gets is a user setting layered over the
  505. * composition's own default. The package suite proves the layering against a
  506. * hand-built context; this proves it through the shipped `cordis.yml` — that
  507. * the roster and the settings provider are actually wired to each other, and
  508. * that the id the setting names is the one a session composes from.
  509. */
  510. describe('the default preset as a user setting', () => {
  511. it('composes an unnamed session from the stored default, not the composed one', async () => {
  512. expect(ctx.agentPresets.defaultId).toBe('standard')
  513. await ctx.settings.update(settingsNamespace(SETTINGS_NAMESPACE), { default: 'minimal' })
  514. try {
  515. expect(ctx.agentPresets.defaultId).toBe('minimal')
  516. const handle = await ctx.agents.create({
  517. sessionId: SessionId('preset-user-default'),
  518. setup: agentCtx => ctx.agentPresets.mount(agentCtx).then(() => undefined),
  519. })
  520. try {
  521. // `mount()` with no id resolves the effective default. Two tools, not
  522. // `standard`'s catalog: the setting decided the composition.
  523. expect(toolNames(ctx, handle.agent)).toEqual(['bash', 'str_replace_editor'])
  524. } finally {
  525. await handle.dispose()
  526. }
  527. } finally {
  528. // The context is shared with the rest of the file. `replace({})` drops
  529. // the user section wholesale so the field re-inherits the composition
  530. // base; `update` merges, and would leave the override standing.
  531. await ctx.settings.replace(settingsNamespace(SETTINGS_NAMESPACE), {})
  532. }
  533. expect(ctx.agentPresets.defaultId).toBe('standard')
  534. })
  535. })
  536. describe('a session keeps the preset it was created with', () => {
  537. it('refuses to adopt a live session under a different preset', async () => {
  538. const handle = await ctx.agents.create({
  539. sessionId: SessionId('preset-locked'),
  540. meta: { agentPreset: 'minimal' },
  541. setup: agentCtx => ctx.agentPresets.mount(agentCtx, 'minimal').then(() => undefined),
  542. })
  543. try {
  544. // The api-proxy guard reads exactly this: the header records what the
  545. // session runs, so naming anything else is a caller error rather than a
  546. // switch. Its history was produced under `minimal`'s two tools.
  547. expect(handle.agent.session.header.agentPreset).toBe('minimal')
  548. } finally {
  549. await handle.dispose()
  550. }
  551. })
  552. })