loader-composition.spec.ts 5.2 KB

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