inheritance.spec.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /**
  2. * Delegation policy through child session events appended before publication:
  3. * the parent's sandbox override plus the pinned `approval/policy: never`.
  4. */
  5. import { afterEach, beforeEach, describe, expect, it } from 'vitest'
  6. import { mkdtemp, readFile, realpath, rm } from 'node:fs/promises'
  7. import { tmpdir } from 'node:os'
  8. import { join } from 'node:path'
  9. import { Context } from '@deepseek-ai/cordis'
  10. import type { Agent } from '@deepseek-ai/dsh-agent'
  11. import AgentLoop from '@deepseek-ai/dsh-agent-loop'
  12. import { mountAgentLoopTestDependencies } from '@deepseek-ai/dsh-agent-loop-testkit'
  13. import SandboxedFileSystem from '@deepseek-ai/dsh-fs-sandbox'
  14. import type { ContentBlock } from '@deepseek-ai/dsh-llm'
  15. import SandboxPolicyService, { setSandboxMode } from '@deepseek-ai/dsh-sandbox-policy'
  16. import { SessionId, type SessionEvent } from '@deepseek-ai/dsh-session'
  17. import * as ToolFs from '@deepseek-ai/dsh-tool-fs'
  18. import ApprovalService from '@deepseek-ai/dsh-user-approval'
  19. import { snapshotSubagentDescriptor } from '@deepseek-ai/dsh-subagent'
  20. import { MockAdapter, textResponse, toolCallResponse } from '../../../core/agent-loop/tests/mock-adapter.ts'
  21. import { startInProcessRun } from '../src/index.ts'
  22. type Script = ConstructorParameters<typeof MockAdapter>[0]
  23. const READ_ONLY_DENIAL = '[sandbox: file access denied under read-only mode]'
  24. const contexts: Context[] = []
  25. let workspace: string
  26. beforeEach(async () => {
  27. workspace = await realpath(await mkdtemp(join(tmpdir(), 'dsh-inherit-')))
  28. })
  29. afterEach(async () => {
  30. for (const ctx of contexts.splice(0).reverse()) await ctx.fiber.dispose()
  31. await rm(workspace, { recursive: true, force: true })
  32. })
  33. async function setupWalled(script: Script): Promise<{ ctx: Context; parent: Agent }> {
  34. const ctx = new Context()
  35. contexts.push(ctx)
  36. await mountAgentLoopTestDependencies(ctx)
  37. await ctx.plugin(SandboxPolicyService, { mode: 'workspace-write', workspaceRoot: workspace })
  38. await ctx.plugin(SandboxedFileSystem, { cwd: workspace })
  39. await ctx.plugin(ToolFs)
  40. await ctx.plugin(ApprovalService)
  41. await ctx.plugin(AgentLoop, { agents: [] })
  42. ctx.llm.registerAdapter(['mock'], new MockAdapter(script))
  43. const parent = await ctx.agentLoop.create(
  44. SessionId('parent'),
  45. { provider: 'mock', model: 'mock' },
  46. { cwd: workspace },
  47. )
  48. return { ctx, parent }
  49. }
  50. function spawnRequest(parent: Agent) {
  51. return {
  52. label: 'child task',
  53. prompt: [{ type: 'text' as const, text: 'child task' }],
  54. parent,
  55. signal: new AbortController().signal,
  56. descriptor: snapshotSubagentDescriptor({
  57. mode: 'one-shot',
  58. provider: 'spawn',
  59. label: 'child task',
  60. }),
  61. }
  62. }
  63. function toolResultTexts(agent: Agent): string[] {
  64. return agent.session.snapshotEvents()
  65. .filter((event): event is SessionEvent<'tool/result'> => event.type === 'tool/result')
  66. .map(event => event.data.message.content
  67. .flatMap(block => block.content)
  68. .filter((block): block is Extract<ContentBlock, { type: 'text' }> => block.type === 'text')
  69. .map(block => block.text)
  70. .join(''))
  71. }
  72. describe('in-process policy inheritance', () => {
  73. it('records the parent sandbox override and the approval pin before publishing a spawn child', async () => {
  74. const script: Script = []
  75. const { ctx, parent } = await setupWalled(script)
  76. const blocked = join(workspace, 'spawn-blocked.txt')
  77. setSandboxMode(parent.session, 'read-only')
  78. // No parent approval override: the child pin must not depend on one.
  79. expect(ctx.approval.overrideOf(parent.session)).toBeUndefined()
  80. const parentLogLength = parent.session.snapshotEvents().length
  81. script.push(
  82. toolCallResponse('write', 'write', { file_path: blocked, content: 'escaped' }),
  83. textResponse('child done'),
  84. )
  85. const run = await startInProcessRun(spawnRequest(parent), {})
  86. try {
  87. const result = await run.result
  88. const child = run.localAgent as Agent
  89. await expect(readFile(blocked, 'utf8')).rejects.toMatchObject({ code: 'ENOENT' })
  90. expect(toolResultTexts(child).join('\n')).toContain(READ_ONLY_DENIAL)
  91. expect(result.stopReason).toBe('completed')
  92. expect(child.session.snapshotEvents().slice(0, 2)).toMatchObject([
  93. { type: 'sandbox/mode', seq: 0, data: { mode: 'read-only', source: 'delegation' } },
  94. { type: 'approval/policy', seq: 1, data: { policy: 'never', source: 'delegation' } },
  95. ])
  96. expect(child.session.firstLiveSeq).toBe(0)
  97. expect(child.session.header.isSeeded).toBe(false)
  98. expect(child.session.inheritedEventCount).toBe(0)
  99. expect(ctx.sandboxPolicy.overrideOf(child.session)).toBe('read-only')
  100. expect(ctx.approval.overrideOf(child.session)).toBe('never')
  101. const request = child.session.snapshotEvents().find(
  102. (event): event is SessionEvent<'request/header'> => event.type === 'request/header',
  103. )
  104. const systemNode = child.session.snapshotEvents().find(
  105. (event): event is SessionEvent<'system/message'> => event.type === 'system/message',
  106. )
  107. const runtimeContext = child.session.snapshotEvents().find(
  108. (event): event is SessionEvent<'user/message'> => event.type === 'user/message'
  109. && event.data.source.kind === 'plugin'
  110. && event.data.source.plugin === '@deepseek-ai/dsh-system-prompt',
  111. )
  112. if (request === undefined || systemNode === undefined || runtimeContext === undefined) {
  113. throw new Error('child request lacks its system node or runtime policy context')
  114. }
  115. expect(systemNode.seq).toBeLessThan(runtimeContext.seq)
  116. expect(runtimeContext.seq).toBeLessThan(request.seq)
  117. const contextText = runtimeContext.data.content
  118. .filter((block): block is Extract<ContentBlock, { type: 'text' }> => block.type === 'text')
  119. .map(block => block.text)
  120. .join('\n')
  121. expect(contextText).toContain('Current DSH file policy: read-only')
  122. expect(contextText).toContain('Approval prompts are disabled')
  123. // The statement rides runtime context; the system node (surface node 0) stays uniform.
  124. expect(contextText).toContain('You are a delegated subagent')
  125. const systemHead = child.session.deriveMessages()[0]
  126. if (systemHead?.role !== 'system') throw new Error('child surface node 0 is not a system message')
  127. expect(child.session.surface.nodes[0]).toBe(systemNode.seq)
  128. const systemText = systemHead.content
  129. .filter((block): block is Extract<ContentBlock, { type: 'text' }> => block.type === 'text')
  130. .map(block => block.text)
  131. .join('\n')
  132. expect(systemText).not.toContain('Approval prompts are disabled')
  133. expect(systemText).not.toContain('You are a delegated subagent')
  134. expect(parent.session.snapshotEvents()).toHaveLength(parentLogLength)
  135. } finally {
  136. await run.dispose()
  137. }
  138. })
  139. it('places inherited events after a fork prefix so fresh policy wins stale seed state', async () => {
  140. const script: Script = []
  141. const { ctx, parent } = await setupWalled(script)
  142. const blocked = join(workspace, 'fork-blocked.txt')
  143. setSandboxMode(parent.session, 'workspace-write')
  144. const seed = parent.session.snapshotEvents()
  145. setSandboxMode(parent.session, 'read-only')
  146. script.push(
  147. toolCallResponse('write', 'write', { file_path: blocked, content: 'escaped' }),
  148. textResponse('child done'),
  149. )
  150. const run = await startInProcessRun(spawnRequest(parent), { seed })
  151. try {
  152. await run.result
  153. const child = run.localAgent as Agent
  154. expect(child.session.header.isSeeded).toBe(true)
  155. expect(child.session.inheritedEventCount).toBe(1)
  156. expect(child.session.firstLiveSeq).toBe(seed.length)
  157. // seq 1 is the constructor's end-seed marker.
  158. expect(child.session.snapshotEvents().filter(event => event.type === 'sandbox/mode')).toMatchObject([
  159. { seq: 0, data: { mode: 'workspace-write' } },
  160. { seq: 2, data: { mode: 'read-only', source: 'delegation' } },
  161. ])
  162. await expect(readFile(blocked, 'utf8')).rejects.toMatchObject({ code: 'ENOENT' })
  163. expect(ctx.sandboxPolicy.overrideOf(child.session)).toBe('read-only')
  164. setSandboxMode(child.session, 'danger-full-access')
  165. expect(ctx.sandboxPolicy.overrideOf(child.session)).toBe('danger-full-access')
  166. } finally {
  167. await run.dispose()
  168. }
  169. })
  170. it('captures policy at delegation before asynchronous child creation', async () => {
  171. const script: Script = [textResponse('child done')]
  172. const { ctx, parent } = await setupWalled(script)
  173. setSandboxMode(parent.session, 'read-only')
  174. const starting = startInProcessRun(spawnRequest(parent), {})
  175. setSandboxMode(parent.session, 'danger-full-access')
  176. const run = await starting
  177. try {
  178. await run.result
  179. const child = run.localAgent as Agent
  180. expect(ctx.sandboxPolicy.overrideOf(parent.session)).toBe('danger-full-access')
  181. expect(ctx.sandboxPolicy.overrideOf(child.session)).toBe('read-only')
  182. } finally {
  183. await run.dispose()
  184. }
  185. })
  186. it('leaves an unswitched sandbox on the deployment default while still pinning approval', async () => {
  187. const script: Script = []
  188. const { parent } = await setupWalled(script)
  189. const allowed = join(workspace, 'default-allowed.txt')
  190. script.push(
  191. toolCallResponse('write', 'write', { file_path: allowed, content: 'fine' }),
  192. textResponse('child done'),
  193. )
  194. const run = await startInProcessRun(spawnRequest(parent), {})
  195. try {
  196. await run.result
  197. const child = run.localAgent as Agent
  198. expect(await readFile(allowed, 'utf8')).toBe('fine')
  199. expect(child.session.snapshotEvents().some(event => event.type === 'sandbox/mode')).toBe(false)
  200. expect(child.session.snapshotEvents().filter(event => event.type === 'approval/policy')).toMatchObject([
  201. { seq: 0, data: { policy: 'never', source: 'delegation' } },
  202. ])
  203. expect(child.session.firstLiveSeq).toBe(0)
  204. } finally {
  205. await run.dispose()
  206. }
  207. })
  208. it('rejects a child escalation deterministically even when an answerer would allow it', async () => {
  209. const script: Script = []
  210. const { ctx, parent } = await setupWalled(script)
  211. // A granting answerer proves the pin resolves before any answerer runs.
  212. let consulted = false
  213. ctx.on('approval/request', () => {
  214. consulted = true
  215. return Promise.resolve('allowed-once' as const)
  216. })
  217. const blocked = join(workspace, 'escalation-blocked.txt')
  218. setSandboxMode(parent.session, 'read-only')
  219. script.push(
  220. toolCallResponse('write', 'write', {
  221. file_path: blocked,
  222. content: 'escaped',
  223. sandbox_permissions: 'workspace-write',
  224. justification: 'test escalation from a delegated child',
  225. }),
  226. textResponse('child done'),
  227. )
  228. const run = await startInProcessRun(spawnRequest(parent), {})
  229. try {
  230. await run.result
  231. const child = run.localAgent as Agent
  232. await expect(readFile(blocked, 'utf8')).rejects.toMatchObject({ code: 'ENOENT' })
  233. expect(consulted).toBe(false)
  234. expect(toolResultTexts(child).join('\n'))
  235. .toContain('the user rejected escalating this operation to "workspace-write"')
  236. const asked = child.session.snapshotEvents().find(
  237. (event): event is SessionEvent<'approval/asked'> => event.type === 'approval/asked',
  238. )
  239. const decided = child.session.snapshotEvents().find(
  240. (event): event is SessionEvent<'approval/decided'> => event.type === 'approval/decided',
  241. )
  242. expect(asked?.data.toolName).toBe('write')
  243. expect(decided?.data).toMatchObject({ id: asked?.data.id, outcome: 'rejected' })
  244. } finally {
  245. await run.dispose()
  246. }
  247. })
  248. })