loader-composition.spec.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Proves `allowParallelInProgress` is real configurability and not a constant:
  2. // the flag is set in a cordis.yml booted through the real Loader, and both faces
  3. // it controls — the model-facing description and the accepted input — follow it.
  4. import { mkdtemp, rm, writeFile } from 'node:fs/promises'
  5. import { tmpdir } from 'node:os'
  6. import { join } from 'node:path'
  7. import { pathToFileURL } from 'node:url'
  8. import { afterEach, describe, expect, it } from 'vitest'
  9. import { Context } from '@deepseek-ai/cordis'
  10. import Loader from '@deepseek-ai/cordis-plugin-loader'
  11. import Include from '@deepseek-ai/cordis-plugin-include'
  12. import { CallId } from '@deepseek-ai/dsh-llm'
  13. import { Session, SessionId } from '@deepseek-ai/dsh-session'
  14. import AgentRegistry, { Inbox } from '@deepseek-ai/dsh-agent'
  15. import type { Agent } from '@deepseek-ai/dsh-agent'
  16. import SystemPrompt from '@deepseek-ai/dsh-system-prompt'
  17. import ToolRuntime from '@deepseek-ai/dsh-tools'
  18. import * as ToolTodo from '@deepseek-ai/dsh-tool-todo'
  19. let root: string | undefined
  20. let context: Context | undefined
  21. afterEach(async () => {
  22. await context?.fiber.dispose()
  23. context = undefined
  24. if (root !== undefined) await rm(root, { recursive: true, force: true })
  25. root = undefined
  26. })
  27. function agent(ctx: Context): Agent {
  28. const scope = ctx.plugin(() => {})
  29. const id = SessionId('todo-loader-agent')
  30. const session = Session.create(id)
  31. const value: Agent = {
  32. id, options: {}, session, inbox: new Inbox(session, { inserted: () => {}, discarded: () => {}, claimed: () => {} }),
  33. status: 'idle', ctx: scope.ctx,
  34. followup: () => {}, steer: () => {}, inject: () => {}, send: () => {}, cancel() {},
  35. runMaintenance: task => task(new AbortController().signal),
  36. whenIdle: () => Promise.resolve(),
  37. }
  38. ctx.agents.register(value)
  39. return value
  40. }
  41. function resultText(result: { content: { type: string; text?: string }[] }): string {
  42. return result.content.filter(block => block.type === 'text').map(block => block.text).join('')
  43. }
  44. /**
  45. * Boot a cordis.yml carrying the given tool-todo config block.
  46. * @param configLines - YAML lines nested under the tool's `config:` key.
  47. * @returns the booted context.
  48. */
  49. async function boot(configLines: readonly string[]): Promise<Context> {
  50. root = await mkdtemp(join(tmpdir(), 'dsh-todo-loader-'))
  51. const configPath = join(root, 'cordis.yml')
  52. await writeFile(configPath, [
  53. "- name: '@deepseek-ai/dsh-agent'",
  54. "- name: '@deepseek-ai/dsh-system-prompt'",
  55. "- name: '@deepseek-ai/dsh-tools'",
  56. "- name: '@deepseek-ai/dsh-tool-todo'",
  57. ...configLines.length > 0 ? [' config:', ...configLines] : [],
  58. '',
  59. ].join('\n'))
  60. const ctx = new Context()
  61. context = ctx
  62. ctx.baseUrl = pathToFileURL(root).href + '/'
  63. await ctx.plugin(Loader)
  64. ctx.loader.builtins.include = Include
  65. const modules = new Map<string, unknown>([
  66. ['@deepseek-ai/dsh-agent', AgentRegistry],
  67. ['@deepseek-ai/dsh-system-prompt', SystemPrompt],
  68. ['@deepseek-ai/dsh-tools', ToolRuntime],
  69. ['@deepseek-ai/dsh-tool-todo', ToolTodo],
  70. ])
  71. ctx.loader.internal = {
  72. version: 'v2',
  73. async import(specifier: string) {
  74. if (!modules.has(specifier)) throw new Error(`unexpected Loader import: ${specifier}`)
  75. return modules.get(specifier)
  76. },
  77. } as unknown as NonNullable<typeof ctx.loader.internal>
  78. await ctx.loader.create({ name: 'cordis:include', config: { path: pathToFileURL(configPath).href } })
  79. await ctx.loader.await()
  80. return ctx
  81. }
  82. const PARALLEL_TODOS = [
  83. { content: 'run subagent a', status: 'in_progress' },
  84. { content: 'run subagent b', status: 'in_progress' },
  85. ]
  86. describe('tool-todo real Loader composition through cordis.yml', () => {
  87. it('allowParallelInProgress: false narrows the description and rejects a parallel write', async () => {
  88. const ctx = await boot([' allowParallelInProgress: false'])
  89. const description = ctx.tools.schemas().find(s => s.name === 'todo_write')?.description ?? ''
  90. expect(description).toContain('Keep AT MOST ONE todo `in_progress`')
  91. expect(description).not.toContain('several at once')
  92. const owner = agent(ctx)
  93. const result = await ctx.tools.execute({
  94. signal: new AbortController().signal,
  95. callId: CallId('parallel'),
  96. name: 'todo_write',
  97. arguments: { todos: PARALLEL_TODOS },
  98. agent: owner,
  99. })
  100. expect(result.isError).toBe(true)
  101. expect(resultText(result)).toContain('at most one task may be in_progress')
  102. expect(owner.session.events.some(e => e.type === 'todo/write')).toBe(false)
  103. }, 30_000)
  104. it('allowParallelInProgress: true permits a parallel write end to end', async () => {
  105. const ctx = await boot([' allowParallelInProgress: true'])
  106. const description = ctx.tools.schemas().find(s => s.name === 'todo_write')?.description ?? ''
  107. expect(description).toContain('several at once when work genuinely runs in parallel')
  108. const owner = agent(ctx)
  109. const result = await ctx.tools.execute({
  110. signal: new AbortController().signal,
  111. callId: CallId('parallel-enabled'),
  112. name: 'todo_write',
  113. arguments: { todos: PARALLEL_TODOS },
  114. agent: owner,
  115. })
  116. expect(result.isError).toBe(false)
  117. expect(owner.session.events.findLast(e => e.type === 'todo/write')?.data.todos).toEqual(PARALLEL_TODOS)
  118. }, 30_000)
  119. it.each([
  120. { label: 'is omitted', configLines: [], failure: '$.allowParallelInProgress missing required value' },
  121. { label: 'is not boolean', configLines: [' allowParallelInProgress: "no"'], failure: '$.allowParallelInProgress expected boolean' },
  122. ])('fails loading when allowParallelInProgress $label', async ({ configLines, failure }) => {
  123. // The policy is self-contained, so misconfiguration fails at load: the
  124. // entry's apply rejects and boot never reaches a running tool.
  125. await expect(boot(configLines)).rejects.toThrow(failure)
  126. }, 30_000)
  127. })