loader-composition.spec.ts 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /**
  2. * REAL-composition proof: the shipped YAML rows (session store, local
  3. * subprocess runtime, workspace-changes) boot through the vendored Loader and
  4. * a logged turn ends with its recorded changes.
  5. */
  6. import { mkdtemp, rm, writeFile } from 'node:fs/promises'
  7. import { tmpdir } from 'node:os'
  8. import { join } from 'node:path'
  9. import { pathToFileURL } from 'node:url'
  10. import { afterEach, describe, expect, it } from 'vitest'
  11. import { Context } from '@deepseek-ai/cordis'
  12. import Loader from '@deepseek-ai/cordis-plugin-loader'
  13. import Include from '@deepseek-ai/cordis-plugin-include'
  14. import SessionStore, { SessionId } from '@deepseek-ai/dsh-session'
  15. import LocalSubprocessRuntime from '@deepseek-ai/dsh-subprocess-local'
  16. import * as WorkspaceChangesPlugin from '@deepseek-ai/dsh-workspace-changes'
  17. import { changes, endTurn, git, startTurn, toolCall } from './support.ts'
  18. let root: string | undefined
  19. let context: Context | undefined
  20. afterEach(async () => {
  21. await context?.fiber.dispose()
  22. context = undefined
  23. if (root !== undefined) await rm(root, { recursive: true, force: true })
  24. root = undefined
  25. })
  26. describe('real Loader composition', () => {
  27. it('loads the shipped rows and records a turn’s changes', async () => {
  28. root = await mkdtemp(join(tmpdir(), 'dsh-workspace-changes-loader-'))
  29. const cwd = join(root, 'ws')
  30. await writeFile(join(root, 'cordis.yml'), [
  31. "- name: '@deepseek-ai/dsh-session'",
  32. "- name: '@deepseek-ai/dsh-subprocess-local'",
  33. "- name: '@deepseek-ai/dsh-workspace-changes'",
  34. '',
  35. ].join('\n'))
  36. context = new Context()
  37. context.baseUrl = `${pathToFileURL(root).href}/`
  38. await context.plugin(Loader)
  39. context.loader.builtins.include = Include
  40. const modules = new Map<string, unknown>([
  41. ['@deepseek-ai/dsh-session', SessionStore],
  42. ['@deepseek-ai/dsh-subprocess-local', LocalSubprocessRuntime],
  43. ['@deepseek-ai/dsh-workspace-changes', WorkspaceChangesPlugin],
  44. ])
  45. context.loader.internal = {
  46. version: 'v2',
  47. async import(specifier: string) {
  48. if (!modules.has(specifier)) throw new Error(`unexpected Loader import: ${specifier}`)
  49. return modules.get(specifier)
  50. },
  51. } as unknown as NonNullable<typeof context.loader.internal>
  52. await context.loader.create({ name: 'cordis:include', config: { path: pathToFileURL(join(root, 'cordis.yml')).href } })
  53. await context.loader.await()
  54. const unloaded = [...context.loader.entries()]
  55. .filter(entry => entry.fiber === undefined && !entry.disabled)
  56. .map(entry => entry.options.name)
  57. expect(unloaded).toEqual([])
  58. await writeFile(join(root, 'placeholder'), '')
  59. await rm(cwd, { recursive: true, force: true })
  60. git(root, 'init', '-q', cwd)
  61. await writeFile(join(cwd, 'tracked.txt'), 'one\n')
  62. git(cwd, 'add', '-A'); git(cwd, 'commit', '-q', '-m', 'init')
  63. const session = context.sessions.create(SessionId('composed'), { meta: { cwd } })
  64. startTurn(session, 1)
  65. await context.waterfall('tools/pre-execute', { agent: { session } } as never, () => Promise.resolve(undefined as never))
  66. await writeFile(join(cwd, 'tracked.txt'), 'one\ntwo\n')
  67. toolCall(session, 1, 'bash', { command: 'x' })
  68. endTurn(session, 1)
  69. await context.waterfall('tools/pre-execute', { agent: { session } } as never, () => Promise.resolve(undefined as never))
  70. const [recorded, ...rest] = changes(context, session)
  71. expect(rest).toEqual([])
  72. expect(recorded).toEqual({
  73. turn: 1, cwd, total: 1, added: 1, deleted: 0,
  74. files: [{ path: 'tracked.txt', display: 'tracked.txt', added: 1, deleted: 0 }],
  75. snapshot: { before: expect.stringMatching(/^[0-9a-f]+$/) as string, after: expect.stringMatching(/^[0-9a-f]+$/) as string },
  76. })
  77. })
  78. })