execution-mode.spec.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /** Covers fail-closed per-call classification and model-schema isolation. */
  2. import { describe, expect, expectTypeOf, it } from 'vitest'
  3. import { Context } from 'cordis'
  4. import { CallId } from '@deepseek-ai/dsh-llm'
  5. import SystemPrompt from '@deepseek-ai/dsh-system-prompt'
  6. import ToolRegistry, {
  7. defineTool,
  8. type ToolDefinition,
  9. type ToolExecutionInput,
  10. type ToolExecutionMode,
  11. } from '@deepseek-ai/dsh-tools'
  12. const testToolSignal = new AbortController().signal
  13. async function setup() {
  14. const ctx = new Context()
  15. await ctx.plugin(SystemPrompt)
  16. await ctx.plugin(ToolRegistry)
  17. return ctx
  18. }
  19. function exec(name: string, args: unknown): ToolExecutionInput {
  20. return { signal: testToolSignal, callId: CallId('c1'), name, arguments: args }
  21. }
  22. describe('ToolRegistry.executionMode', () => {
  23. it('returns parallel only for an explicit true classifier', async () => {
  24. const ctx = await setup()
  25. ctx.tools.register(defineTool({
  26. name: 'safe',
  27. description: 'parallel-safe',
  28. parameters: {},
  29. isConcurrencySafe: () => true,
  30. async execute() { return [] },
  31. }))
  32. expect(ctx.tools.executionMode(exec('safe', {}))).toEqual({ kind: 'parallel' })
  33. })
  34. it('defaults to exclusive for a tool with no isConcurrencySafe declaration', async () => {
  35. const ctx = await setup()
  36. ctx.tools.register(defineTool({
  37. name: 'plain',
  38. description: 'no declaration',
  39. parameters: {},
  40. async execute() { return [] },
  41. }))
  42. expect(ctx.tools.executionMode(exec('plain', {}))).toEqual({ kind: 'exclusive' })
  43. })
  44. it('returns exclusive for an unknown tool', async () => {
  45. const ctx = await setup()
  46. expect(ctx.tools.executionMode(exec('nonexistent', {}))).toEqual({ kind: 'exclusive' })
  47. })
  48. it('returns exclusive when the classifier returns false for these args', async () => {
  49. const ctx = await setup()
  50. ctx.tools.register(defineTool({
  51. name: 'rw',
  52. description: 'read or write',
  53. parameters: { mode: { type: 'string', required: true } },
  54. isConcurrencySafe: args => args.mode === 'read',
  55. async execute() { return [] },
  56. }))
  57. expect(ctx.tools.executionMode(exec('rw', { mode: 'read' }))).toEqual({ kind: 'parallel' })
  58. expect(ctx.tools.executionMode(exec('rw', { mode: 'write' }))).toEqual({ kind: 'exclusive' })
  59. })
  60. it('classifies invalid defineTool arguments as exclusive without throwing', async () => {
  61. const ctx = await setup()
  62. ctx.tools.register(defineTool({
  63. name: 'needs-mode',
  64. description: 'requires mode',
  65. parameters: { mode: { type: 'string', required: true } },
  66. isConcurrencySafe: () => true,
  67. async execute() { return [] },
  68. }))
  69. expect(ctx.tools.executionMode(exec('needs-mode', {}))).toEqual({ kind: 'exclusive' })
  70. })
  71. it('treats a throwing raw classifier as exclusive', async () => {
  72. const ctx = await setup()
  73. const raw: ToolDefinition = {
  74. name: 'thrower',
  75. description: 'classifier throws',
  76. parameters: { type: 'object', properties: {} },
  77. isConcurrencySafe() { throw new Error('boom') },
  78. async execute() { return [] },
  79. }
  80. ctx.tools.register(raw)
  81. expect(ctx.tools.executionMode(exec('thrower', {}))).toEqual({ kind: 'exclusive' })
  82. })
  83. it('treats a truthy non-boolean raw result as exclusive', async () => {
  84. const ctx = await setup()
  85. const raw = {
  86. name: 'truthy',
  87. description: 'classifier returns a truthy string',
  88. parameters: { type: 'object', properties: {} },
  89. isConcurrencySafe() { return 'yes' },
  90. async execute() { return [] },
  91. } as unknown as ToolDefinition
  92. ctx.tools.register(raw)
  93. expect(ctx.tools.executionMode(exec('truthy', {}))).toEqual({ kind: 'exclusive' })
  94. })
  95. it('passes parsed arguments directly to a raw definition', async () => {
  96. const ctx = await setup()
  97. let seen: unknown
  98. ctx.tools.register({
  99. name: 'raw-safe',
  100. description: 'raw',
  101. parameters: { type: 'object', properties: {} },
  102. isConcurrencySafe(args) { seen = args; return true },
  103. async execute() { return [] },
  104. })
  105. expect(ctx.tools.executionMode(exec('raw-safe', { anything: 1 }))).toEqual({ kind: 'parallel' })
  106. expect(seen).toEqual({ anything: 1 })
  107. })
  108. it('isConcurrencySafe never reaches the model-facing schemas() projection', async () => {
  109. const ctx = await setup()
  110. ctx.tools.register(defineTool({
  111. name: 'safe',
  112. description: 'parallel-safe',
  113. parameters: { x: { type: 'string', required: true } },
  114. isConcurrencySafe: () => true,
  115. async execute() { return [] },
  116. }))
  117. const schema = ctx.tools.schemas()[0] as unknown as Record<string, unknown>
  118. expect(Object.keys(schema).sort()).toEqual(['description', 'name', 'parameters'])
  119. expect(schema.isConcurrencySafe).toBeUndefined()
  120. })
  121. it('ToolExecutionMode is the object-tagged union', () => {
  122. expectTypeOf<ToolExecutionMode>().toEqualTypeOf<{ kind: 'parallel' } | { kind: 'exclusive' }>()
  123. })
  124. })