1
0

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. ' config:',
  35. ` dshHome: ${JSON.stringify(join(root, 'home'))}`,
  36. '',
  37. ].join('\n'))
  38. context = new Context()
  39. context.baseUrl = `${pathToFileURL(root).href}/`
  40. await context.plugin(Loader)
  41. context.loader.builtins.include = Include
  42. const modules = new Map<string, unknown>([
  43. ['@deepseek-ai/dsh-session', SessionStore],
  44. ['@deepseek-ai/dsh-subprocess-local', LocalSubprocessRuntime],
  45. ['@deepseek-ai/dsh-workspace-changes', WorkspaceChangesPlugin],
  46. ])
  47. context.loader.internal = {
  48. version: 'v2',
  49. async import(specifier: string) {
  50. if (!modules.has(specifier)) throw new Error(`unexpected Loader import: ${specifier}`)
  51. return modules.get(specifier)
  52. },
  53. } as unknown as NonNullable<typeof context.loader.internal>
  54. await context.loader.create({ name: 'cordis:include', config: { path: pathToFileURL(join(root, 'cordis.yml')).href } })
  55. await context.loader.await()
  56. const unloaded = [...context.loader.entries()]
  57. .filter(entry => entry.fiber === undefined && !entry.disabled)
  58. .map(entry => entry.options.name)
  59. expect(unloaded).toEqual([])
  60. await writeFile(join(root, 'placeholder'), '')
  61. await rm(cwd, { recursive: true, force: true })
  62. git(root, 'init', '-q', cwd)
  63. await writeFile(join(cwd, 'tracked.txt'), 'one\n')
  64. git(cwd, 'add', '-A'); git(cwd, 'commit', '-q', '-m', 'init')
  65. const session = context.sessions.create(SessionId('composed'), { meta: { cwd } })
  66. startTurn(session, 1)
  67. await context.waterfall('tools/pre-execute', { agent: { session } } as never, () => Promise.resolve(undefined as never))
  68. await writeFile(join(cwd, 'tracked.txt'), 'one\ntwo\n')
  69. toolCall(session, 1, 'bash', { command: 'x' })
  70. endTurn(session, 1)
  71. await context.waterfall('tools/pre-execute', { agent: { session } } as never, () => Promise.resolve(undefined as never))
  72. const [recorded, ...rest] = changes(session)
  73. expect(rest).toEqual([])
  74. expect(recorded).toMatchObject({ turn: 1, total: 1, files: [{ path: 'tracked.txt', display: 'tracked.txt', added: 1, deleted: 0 }] })
  75. expect(recorded!.snapshot.before).toMatch(/^[0-9a-f]+$/)
  76. expect(recorded!.snapshot.after).toMatch(/^[0-9a-f]+$/)
  77. })
  78. })