1
0

settings.spec.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /** The `agent-loop` settings section layered over the composition entry. */
  2. import { describe, expect, it } from 'vitest'
  3. import { Context } from '@deepseek-ai/cordis'
  4. import type { Fiber } from '@deepseek-ai/cordis'
  5. import LlmRuntime from '@deepseek-ai/dsh-llm'
  6. import SessionStore from '@deepseek-ai/dsh-session'
  7. import SystemPrompt from '@deepseek-ai/dsh-system-prompt'
  8. import ToolRuntime from '@deepseek-ai/dsh-tools'
  9. import AgentRegistry from '@deepseek-ai/dsh-agent'
  10. import { SettingsProvider } from '@deepseek-ai/dsh-settings'
  11. import type { SettingsNamespace } from '@deepseek-ai/dsh-settings'
  12. import AgentLoop, { AGENT_LOOP_SETTINGS_NAMESPACE } from '@deepseek-ai/dsh-agent-loop'
  13. /** The smallest real provider: one in-memory document, always writable. */
  14. class MemorySettings extends SettingsProvider {
  15. doc: Record<string, unknown> = {}
  16. get writable(): boolean {
  17. return true
  18. }
  19. protected load(): Promise<Record<string, unknown>> {
  20. return Promise.resolve(structuredClone(this.doc))
  21. }
  22. protected persist(ns: SettingsNamespace, section: Record<string, unknown>): Promise<void> {
  23. this.doc = { ...this.doc, [ns]: structuredClone(section) }
  24. return Promise.resolve()
  25. }
  26. }
  27. async function boot(): Promise<{ ctx: Context; settingsFiber: Fiber; loopFiber: Fiber }> {
  28. const ctx = new Context()
  29. await ctx.plugin(LlmRuntime)
  30. await ctx.plugin(SessionStore)
  31. await ctx.plugin(SystemPrompt)
  32. await ctx.plugin(ToolRuntime)
  33. await ctx.plugin(AgentRegistry)
  34. const settingsFiber = ctx.plugin(MemorySettings)
  35. await settingsFiber.await()
  36. const loopFiber = ctx.plugin(AgentLoop, { agents: [], maxParallelToolCalls: 4 })
  37. await loopFiber.await()
  38. return { ctx, settingsFiber, loopFiber }
  39. }
  40. describe('agent-loop settings section', () => {
  41. it('layers the stored parallel cap over the composition entry', async () => {
  42. const bench = await boot()
  43. expect(bench.ctx.agentLoop.config.maxParallelToolCalls).toBe(4)
  44. await bench.ctx.settings.update(AGENT_LOOP_SETTINGS_NAMESPACE, { maxParallelToolCalls: 1 })
  45. expect(bench.ctx.agentLoop.config.maxParallelToolCalls).toBe(1)
  46. await bench.ctx.fiber.dispose()
  47. })
  48. it('refuses a non-positive cap at the write', async () => {
  49. const bench = await boot()
  50. await expect(bench.ctx.settings.update(AGENT_LOOP_SETTINGS_NAMESPACE, { maxParallelToolCalls: 0 }))
  51. .rejects.toThrow()
  52. expect(bench.ctx.agentLoop.config.maxParallelToolCalls).toBe(4)
  53. await bench.ctx.fiber.dispose()
  54. })
  55. it('never offers the composed agents array to the settings document', async () => {
  56. const bench = await boot()
  57. const descriptor = bench.ctx.settings.describe().find(row => String(row.ns) === 'agent-loop')
  58. expect(Object.keys(descriptor?.value as object)).toEqual(['maxParallelToolCalls'])
  59. await bench.ctx.fiber.dispose()
  60. })
  61. it('keeps serving the composed agents array to its own consumers', async () => {
  62. const bench = await boot()
  63. await bench.ctx.settings.update(AGENT_LOOP_SETTINGS_NAMESPACE, { maxParallelToolCalls: 2 })
  64. expect(bench.ctx.agentLoop.config.agents).toEqual([])
  65. await bench.ctx.fiber.dispose()
  66. })
  67. it('falls back to the composition entry when the settings provider detaches', async () => {
  68. const bench = await boot()
  69. await bench.ctx.settings.update(AGENT_LOOP_SETTINGS_NAMESPACE, { maxParallelToolCalls: 1 })
  70. expect(bench.ctx.agentLoop.config.maxParallelToolCalls).toBe(1)
  71. await bench.settingsFiber.dispose()
  72. expect(bench.ctx.agentLoop.config.maxParallelToolCalls).toBe(4)
  73. await bench.ctx.fiber.dispose()
  74. })
  75. it('releases the namespace when the service unloads', async () => {
  76. const bench = await boot()
  77. expect(bench.ctx.settings.describe().map(row => String(row.ns))).toContain('agent-loop')
  78. await bench.loopFiber.dispose()
  79. expect(bench.ctx.settings.describe().map(row => String(row.ns))).not.toContain('agent-loop')
  80. await bench.ctx.fiber.dispose()
  81. })
  82. })