loader-composition.spec.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import { mkdtemp, rm, writeFile } from 'node:fs/promises'
  2. import { tmpdir } from 'node:os'
  3. import { join } from 'node:path'
  4. import { pathToFileURL } from 'node:url'
  5. import { afterEach, describe, expect, it } from 'vitest'
  6. import { Context } from '@deepseek-ai/cordis'
  7. import Loader from '@deepseek-ai/cordis-plugin-loader'
  8. import Include from '@deepseek-ai/cordis-plugin-include'
  9. import { ToolCallId } from '@deepseek-ai/dsh-llm'
  10. import { SESSION_FORMAT_VERSION, Session, SessionId } from '@deepseek-ai/dsh-session'
  11. import AgentRegistry from '@deepseek-ai/dsh-agent'
  12. import type { Agent } from '@deepseek-ai/dsh-agent'
  13. import TerminalSessionService from '@deepseek-ai/dsh-terminal'
  14. import * as TerminalLocal from '@deepseek-ai/dsh-terminal-bash'
  15. import SandboxProvider from '@deepseek-ai/dsh-sandbox'
  16. import type { ConfinedArgv, SandboxPolicy } from '@deepseek-ai/dsh-sandbox'
  17. import SandboxPolicyService from '@deepseek-ai/dsh-sandbox-policy'
  18. import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection'
  19. import LocalSubprocessRuntime from '@deepseek-ai/dsh-subprocess-local'
  20. import SystemPrompt from '@deepseek-ai/dsh-system-prompt'
  21. import ToolRuntime from '@deepseek-ai/dsh-tools'
  22. import * as ToolBashPersistent from '@deepseek-ai/dsh-tool-bash-persistent'
  23. import { unsupportedInbox } from '@deepseek-ai/dsh-agent-loop-testkit'
  24. let root: string | undefined
  25. let context: Context | undefined
  26. afterEach(async () => {
  27. await context?.fiber.dispose()
  28. context = undefined
  29. if (root !== undefined) await rm(root, { recursive: true, force: true })
  30. root = undefined
  31. })
  32. class PassthroughSandbox extends SandboxProvider {
  33. confine(argv: readonly string[], _policy: SandboxPolicy): ConfinedArgv {
  34. return { argv: [...argv], enforcement: 'full', denialSignatures: [], runnerFailureRules: [] }
  35. }
  36. }
  37. function agent(ctx: Context, cwd: string): Agent {
  38. const id = SessionId('persistent-bash-loader-agent')
  39. const scope = ctx.plugin(() => {})
  40. const session = Session.create(id, [], {
  41. version: SESSION_FORMAT_VERSION, id, createdAt: 0, cwd, isSeeded: false,
  42. })
  43. const value: Agent = {
  44. id,
  45. options: {},
  46. session,
  47. inbox: unsupportedInbox(),
  48. status: 'idle',
  49. ctx: scope.ctx,
  50. send: () => {},
  51. followup: () => {},
  52. steer: () => ({ outcome: Promise.resolve({ status: 'rejected' as const }) }),
  53. inject: () => {},
  54. cancel() {},
  55. runMaintenance: task => task(new AbortController().signal),
  56. whenIdle: () => Promise.resolve(),
  57. }
  58. ctx.agents.register(value)
  59. return value
  60. }
  61. function text(result: { content: { type: string; text?: string }[] }): string {
  62. return result.content.filter(block => block.type === 'text').map(block => block.text).join('')
  63. }
  64. const suite = process.platform === 'linux' || process.platform === 'darwin' ? describe : describe.skip
  65. suite('persistent Bash through a real cordis.yml Loader composition', () => {
  66. it('preserves cwd and environment across calls', async () => {
  67. root = await mkdtemp(join(tmpdir(), 'dsh-persistent-bash-loader-'))
  68. const configPath = join(root, 'cordis.yml')
  69. await writeFile(configPath, [
  70. "- name: '@deepseek-ai/dsh-agent'",
  71. "- name: '@deepseek-ai/dsh-system-prompt'",
  72. "- name: '@deepseek-ai/dsh-tools'",
  73. "- name: '@deepseek-ai/dsh-terminal'",
  74. "- name: '@deepseek-ai/dsh-test-sandbox'",
  75. "- name: '@deepseek-ai/dsh-session-projection'",
  76. "- name: '@deepseek-ai/dsh-sandbox-policy'",
  77. ' config:',
  78. ' mode: danger-full-access',
  79. ` workspaceRoot: ${JSON.stringify(root)}`,
  80. "- name: '@deepseek-ai/dsh-subprocess-local'",
  81. "- name: '@deepseek-ai/dsh-terminal-bash'",
  82. ' config:',
  83. ' pollIntervalMs: 10',
  84. ' exactProbeAfterMs: 20',
  85. // The silence tier is pushed beyond the send bound, so no send below can
  86. // settle as inferred_idle: every case proves the controlled-prompt fast
  87. // path that the production defaults (3.5s silence) would otherwise mask.
  88. ' idleSilenceMs: 30000',
  89. ' handoffGraceMs: 100',
  90. ' scrollbackLines: 20000',
  91. ' timeoutMs: 2000',
  92. ' disposeGraceMs: 500',
  93. "- name: '@deepseek-ai/dsh-tool-bash-persistent'",
  94. ' config:',
  95. ' timeoutMs: 5000',
  96. '',
  97. ].join('\n'))
  98. context = new Context()
  99. context.baseUrl = pathToFileURL(root).href + '/'
  100. await context.plugin(Loader)
  101. context.loader.builtins.include = Include
  102. const modules = new Map<string, unknown>([
  103. ['@deepseek-ai/dsh-agent', AgentRegistry],
  104. ['@deepseek-ai/dsh-system-prompt', SystemPrompt],
  105. ['@deepseek-ai/dsh-tools', ToolRuntime],
  106. ['@deepseek-ai/dsh-terminal', TerminalSessionService],
  107. ['@deepseek-ai/dsh-test-sandbox', PassthroughSandbox],
  108. ['@deepseek-ai/dsh-session-projection', SessionProjectionRegistry],
  109. ['@deepseek-ai/dsh-sandbox-policy', SandboxPolicyService],
  110. ['@deepseek-ai/dsh-subprocess-local', LocalSubprocessRuntime],
  111. ['@deepseek-ai/dsh-terminal-bash', TerminalLocal],
  112. ['@deepseek-ai/dsh-tool-bash-persistent', ToolBashPersistent],
  113. ])
  114. context.loader.internal = {
  115. version: 'v2',
  116. async import(specifier: string) {
  117. if (!modules.has(specifier)) throw new Error(`unexpected Loader import: ${specifier}`)
  118. return modules.get(specifier)
  119. },
  120. } as unknown as NonNullable<typeof context.loader.internal>
  121. await context.loader.create({ name: 'cordis:include', config: { path: pathToFileURL(configPath).href } })
  122. await context.loader.await()
  123. const owner = agent(context, root)
  124. const signal = new AbortController().signal
  125. const execute = (id: string, command: string) => context!.tools.execute({
  126. signal,
  127. callId: ToolCallId(id),
  128. name: 'bash',
  129. arguments: { command },
  130. agent: owner,
  131. })
  132. expect(context.tools.schemas().map(schema => schema.name)).toEqual(['bash'])
  133. await execute('state', 'export KEEP=loader; mkdir -p nested; cd nested')
  134. const observed = text(await execute('observe', 'printf "cwd=%s keep=%s\\n" "$PWD" "$KEEP"'))
  135. expect(observed).toContain(`cwd=${join(root, 'nested')} keep=loader`)
  136. expect(observed).not.toContain('DSH_PERSISTENT_BASH')
  137. const multiline = text(await execute(
  138. 'multiline',
  139. 'value="line one"\nprintf "%s:%s\\n" "$value" "it\'s fine"',
  140. ))
  141. expect(multiline).toBe("line one:it's fine")
  142. expect(multiline).not.toContain('DSH_PERSISTENT_BASH')
  143. const heredoc = text(await execute(
  144. 'heredoc',
  145. "cat <<'EOF'\nalpha\nbeta\nEOF",
  146. ))
  147. expect(heredoc).toBe('alpha\nbeta')
  148. const pipeline = text(await execute(
  149. 'pipeline',
  150. '{ sleep 0.1; printf "delayed\\n"; } | cat',
  151. ))
  152. expect(pipeline).toBe('delayed')
  153. const large = text(await execute('large-output', 'seq 1 12050'))
  154. expect(large.startsWith('1\n2\n3\n')).toBe(true)
  155. expect(large).toContain('<response clipped>')
  156. expect(large).not.toContain('beginning of this command output was dropped')
  157. // `exec` replaces the wrapper before its end marker prints; the seam's
  158. // stdin_read readiness is what returns the replacement shell's prompt
  159. // instead of spinning until the tool deadline.
  160. const execed = text(await execute('exec-replacement', 'exec bash --noprofile --norc -i'))
  161. expect(execed).toBe('dsh> ')
  162. const exited = text(await execute('exit', 'exit'))
  163. expect(exited).toContain('next bash call starts from the workspace')
  164. expect(text(await execute('after-exit', 'printf "%s\\n" "$PWD"'))).toBe(root)
  165. }, 20_000)
  166. })