loader-composition.spec.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 { CallId } from '@deepseek-ai/dsh-llm'
  10. import { Session, SessionId } from '@deepseek-ai/dsh-session'
  11. import AgentRegistry, { Inbox } from '@deepseek-ai/dsh-agent'
  12. import type { Agent } from '@deepseek-ai/dsh-agent'
  13. import SystemPrompt from '@deepseek-ai/dsh-system-prompt'
  14. import ToolRuntime from '@deepseek-ai/dsh-tools'
  15. import TerminalSessionService from '@deepseek-ai/dsh-terminal'
  16. import SandboxProvider from '@deepseek-ai/dsh-sandbox'
  17. import type { ConfinedArgv, SandboxPolicy } from '@deepseek-ai/dsh-sandbox'
  18. import SandboxPolicyService from '@deepseek-ai/dsh-sandbox-policy'
  19. import LocalSubprocessRuntime from '@deepseek-ai/dsh-subprocess-local'
  20. import * as TerminalLocal from '@deepseek-ai/dsh-terminal-bash'
  21. import * as ToolPty from '@deepseek-ai/dsh-tool-terminal'
  22. let root: string | undefined
  23. let context: Context | undefined
  24. afterEach(async () => {
  25. await context?.fiber.dispose()
  26. context = undefined
  27. if (root !== undefined) await rm(root, { recursive: true, force: true })
  28. root = undefined
  29. })
  30. class PassthroughSandbox extends SandboxProvider {
  31. confine(argv: readonly string[], _policy: SandboxPolicy): ConfinedArgv {
  32. return { argv: [...argv], enforcement: 'full', denialSignatures: [], runnerFailureRules: [] }
  33. }
  34. }
  35. function agent(ctx: Context): Agent {
  36. const scope = ctx.plugin(() => {})
  37. const id = SessionId('pty-loader-agent')
  38. const session = Session.create(id)
  39. const value: Agent = {
  40. id, options: {}, session, inbox: new Inbox(session, { inserted: () => {}, discarded: () => {}, claimed: () => {} }),
  41. status: 'idle',
  42. ctx: scope.ctx,
  43. send: () => {},
  44. followup: () => {}, steer: () => {}, inject: () => {}, cancel() {},
  45. runMaintenance: job => job(new AbortController().signal),
  46. whenIdle: () => Promise.resolve(),
  47. }
  48. ctx.agents.register(value)
  49. return value
  50. }
  51. function resultText(result: { content: { type: string; text?: string }[] }): string {
  52. return result.content.filter(block => block.type === 'text').map(block => block.text).join('')
  53. }
  54. const suite = process.platform === 'linux' || process.platform === 'darwin' ? describe : describe.skip
  55. suite('terminal real Loader composition through cordis.yml', () => {
  56. it('boots cordis.yml and preserves shell state across real tool calls', async () => {
  57. root = await mkdtemp(join(tmpdir(), 'dsh-pty-loader-'))
  58. const configPath = join(root, 'cordis.yml')
  59. await writeFile(configPath, [
  60. "- name: '@deepseek-ai/dsh-agent'",
  61. "- name: '@deepseek-ai/dsh-system-prompt'",
  62. "- name: '@deepseek-ai/dsh-tools'",
  63. "- name: '@deepseek-ai/dsh-terminal'",
  64. "- name: '@deepseek-ai/dsh-test-sandbox'",
  65. "- name: '@deepseek-ai/dsh-sandbox-policy'",
  66. ' config:',
  67. ' mode: danger-full-access',
  68. ` workspaceRoot: ${JSON.stringify(root)}`,
  69. "- name: '@deepseek-ai/dsh-subprocess-local'",
  70. "- name: '@deepseek-ai/dsh-terminal-bash'",
  71. ' config:',
  72. ' pollIntervalMs: 10',
  73. ' exactProbeAfterMs: 20',
  74. ' idleSilenceMs: 250',
  75. ' handoffGraceMs: 250',
  76. ' timeoutMs: 2000',
  77. ' disposeGraceMs: 500',
  78. "- name: '@deepseek-ai/dsh-tool-terminal'",
  79. '',
  80. ].join('\n'))
  81. context = new Context()
  82. context.baseUrl = pathToFileURL(root).href + '/'
  83. await context.plugin(Loader)
  84. context.loader.builtins.include = Include
  85. const modules = new Map<string, unknown>([
  86. ['@deepseek-ai/dsh-agent', AgentRegistry],
  87. ['@deepseek-ai/dsh-system-prompt', SystemPrompt],
  88. ['@deepseek-ai/dsh-tools', ToolRuntime],
  89. ['@deepseek-ai/dsh-terminal', TerminalSessionService],
  90. ['@deepseek-ai/dsh-test-sandbox', PassthroughSandbox],
  91. ['@deepseek-ai/dsh-sandbox-policy', SandboxPolicyService],
  92. ['@deepseek-ai/dsh-subprocess-local', LocalSubprocessRuntime],
  93. ['@deepseek-ai/dsh-terminal-bash', TerminalLocal],
  94. ['@deepseek-ai/dsh-tool-terminal', ToolPty],
  95. ])
  96. context.loader.internal = {
  97. version: 'v2',
  98. async import(specifier: string) {
  99. if (!modules.has(specifier)) throw new Error(`unexpected Loader import: ${specifier}`)
  100. return modules.get(specifier)
  101. },
  102. } as unknown as NonNullable<typeof context.loader.internal>
  103. await context.loader.create({ name: 'cordis:include', config: { path: pathToFileURL(configPath).href } })
  104. await context.loader.await()
  105. const owner = agent(context)
  106. const signal = new AbortController().signal
  107. const spawn = await context.tools.execute({
  108. signal, callId: CallId('spawn'), name: 'terminal_open', arguments: { type: 'shell', name: 'main', cwd: root }, agent: owner,
  109. })
  110. expect(resultText(spawn)).toContain('started terminal session pty-1 (main)')
  111. await context.tools.execute({
  112. signal, callId: CallId('state'), name: 'terminal_send', arguments: { sessionId: 'pty-1', text: 'export KEEP=loader; cd /' }, agent: owner,
  113. })
  114. const read = await context.tools.execute({
  115. signal, callId: CallId('read'), name: 'terminal_send', arguments: { sessionId: 'pty-1', text: 'printf "cwd=%s keep=%s\\n" "$PWD" "$KEEP"' }, agent: owner,
  116. })
  117. expect(resultText(read)).toContain('cwd=/ keep=loader')
  118. expect(context.terminals.list(owner)).toHaveLength(1)
  119. }, 15_000)
  120. })