scheduler.test.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // ============================================================
  2. // Test imports
  3. // ============================================================
  4. import { beforeEach, describe, expect, it } from "vitest";
  5. import { ToolScheduler, READ_ONLY_TOOLS } from "../src/tool/scheduler.js";
  6. import type { ToolDependency } from "../src/tool/scheduler.js";
  7. import type { ToolCallId } from "@zcode/contracts";
  8. // -----------------------------------------------
  9. // Tool Scheduler Tests
  10. // -----------------------------------------------
  11. describe("ToolScheduler", () => {
  12. let scheduler: ToolScheduler;
  13. beforeEach(() => {
  14. scheduler = new ToolScheduler();
  15. });
  16. it("treats WebSearch as a default read-only concurrent tool", () => {
  17. expect(READ_ONLY_TOOLS.has("WebSearch")).toBe(true);
  18. });
  19. it("does not treat Agent and Task as default read-only concurrent tools", () => {
  20. expect(READ_ONLY_TOOLS.has("Agent")).toBe(false);
  21. expect(READ_ONLY_TOOLS.has("Task")).toBe(false);
  22. });
  23. describe("basic scheduling", () => {
  24. it("should handle empty tool list", () => {
  25. const schedule = scheduler.schedule([]);
  26. expect(schedule.items).toHaveLength(0);
  27. expect(schedule.parallelGroups).toHaveLength(0);
  28. });
  29. it("should handle single tool", () => {
  30. const tools: ToolDependency[] = [{ toolCallId: "tool_1" as ToolCallId, dependsOn: [] }];
  31. const schedule = scheduler.schedule(tools);
  32. expect(schedule.items).toHaveLength(1);
  33. });
  34. it("should detect circular dependencies", () => {
  35. const tools: ToolDependency[] = [
  36. { toolCallId: "tool_1" as ToolCallId, dependsOn: ["tool_2" as ToolCallId] },
  37. { toolCallId: "tool_2" as ToolCallId, dependsOn: ["tool_1" as ToolCallId] },
  38. ];
  39. expect(() => scheduler.schedule(tools)).toThrow();
  40. });
  41. });
  42. describe("parallel execution", () => {
  43. it("should group independent tools in parallel", () => {
  44. const tools: ToolDependency[] = [
  45. { toolCallId: "tool_1" as ToolCallId, dependsOn: [] },
  46. { toolCallId: "tool_2" as ToolCallId, dependsOn: [] },
  47. { toolCallId: "tool_3" as ToolCallId, dependsOn: [] },
  48. ];
  49. const schedule = scheduler.schedule(tools);
  50. expect(schedule.parallelGroups).toHaveLength(1);
  51. });
  52. it("should serialize side-effecting tools between safe batches", () => {
  53. const tools: ToolDependency[] = [
  54. {
  55. toolCallId: "tool_read_1" as ToolCallId,
  56. toolName: "Read",
  57. dependsOn: [],
  58. readOnly: true,
  59. concurrentSafe: true,
  60. destructive: false,
  61. sideEffectScope: "none",
  62. },
  63. {
  64. toolCallId: "tool_write" as ToolCallId,
  65. toolName: "Write",
  66. dependsOn: [],
  67. readOnly: false,
  68. concurrentSafe: false,
  69. destructive: false,
  70. sideEffectScope: "workspace",
  71. },
  72. {
  73. toolCallId: "tool_read_2" as ToolCallId,
  74. toolName: "Grep",
  75. dependsOn: [],
  76. readOnly: true,
  77. concurrentSafe: true,
  78. destructive: false,
  79. sideEffectScope: "none",
  80. },
  81. ];
  82. const schedule = scheduler.schedule(tools);
  83. expect(schedule.parallelGroups).toEqual([
  84. ["tool_read_1"],
  85. ["tool_write"],
  86. ["tool_read_2"],
  87. ]);
  88. expect(schedule.items.map((item) => item.canRunParallel)).toEqual([true, false, true]);
  89. });
  90. it("uses subagent metadata to decide whether Agent and Task can run in parallel", () => {
  91. const explicitlySerializedSchedule = scheduler.schedule([
  92. {
  93. toolCallId: "agent_1" as ToolCallId,
  94. toolName: "Agent",
  95. dependsOn: [],
  96. readOnly: true,
  97. concurrentSafe: false,
  98. sideEffectScope: "session",
  99. },
  100. {
  101. toolCallId: "task_1" as ToolCallId,
  102. toolName: "Task",
  103. dependsOn: [],
  104. readOnly: true,
  105. concurrentSafe: false,
  106. sideEffectScope: "session",
  107. },
  108. ]);
  109. const enabledSchedule = scheduler.schedule([
  110. {
  111. toolCallId: "agent_1" as ToolCallId,
  112. toolName: "Agent",
  113. dependsOn: [],
  114. readOnly: true,
  115. concurrentSafe: true,
  116. sideEffectScope: "session",
  117. },
  118. {
  119. toolCallId: "task_1" as ToolCallId,
  120. toolName: "Task",
  121. dependsOn: [],
  122. readOnly: true,
  123. concurrentSafe: true,
  124. sideEffectScope: "session",
  125. },
  126. ]);
  127. expect(explicitlySerializedSchedule.parallelGroups).toEqual([["agent_1"], ["task_1"]]);
  128. expect(enabledSchedule.parallelGroups).toEqual([["agent_1", "task_1"]]);
  129. });
  130. });
  131. describe("maxConcurrency", () => {
  132. it("should split parallel group when exceeding maxConcurrency", () => {
  133. const schedulerWithLimit = new ToolScheduler({ maxConcurrency: 2 });
  134. const tools: ToolDependency[] = [
  135. { toolCallId: "tool_1" as ToolCallId, dependsOn: [] },
  136. { toolCallId: "tool_2" as ToolCallId, dependsOn: [] },
  137. { toolCallId: "tool_3" as ToolCallId, dependsOn: [] },
  138. { toolCallId: "tool_4" as ToolCallId, dependsOn: [] },
  139. { toolCallId: "tool_5" as ToolCallId, dependsOn: [] },
  140. ];
  141. const schedule = schedulerWithLimit.schedule(tools);
  142. // With maxConcurrency=2, 5 tools should be split into 3 groups: [2, 2, 1]
  143. expect(schedule.parallelGroups).toHaveLength(3);
  144. expect(schedule.parallelGroups[0]).toHaveLength(2);
  145. expect(schedule.parallelGroups[1]).toHaveLength(2);
  146. expect(schedule.parallelGroups[2]).toHaveLength(1);
  147. });
  148. it("should respect custom maxConcurrency of 1", () => {
  149. const schedulerWithLimit = new ToolScheduler({ maxConcurrency: 1 });
  150. const tools: ToolDependency[] = [
  151. { toolCallId: "tool_1" as ToolCallId, dependsOn: [] },
  152. { toolCallId: "tool_2" as ToolCallId, dependsOn: [] },
  153. { toolCallId: "tool_3" as ToolCallId, dependsOn: [] },
  154. ];
  155. const schedule = schedulerWithLimit.schedule(tools);
  156. // With maxConcurrency=1, all tools should be serialized
  157. expect(schedule.parallelGroups).toHaveLength(3);
  158. expect(schedule.parallelGroups[0]).toHaveLength(1);
  159. expect(schedule.parallelGroups[1]).toHaveLength(1);
  160. expect(schedule.parallelGroups[2]).toHaveLength(1);
  161. });
  162. it("should not split when tool count equals maxConcurrency", () => {
  163. const schedulerWithLimit = new ToolScheduler({ maxConcurrency: 3 });
  164. const tools: ToolDependency[] = [
  165. { toolCallId: "tool_1" as ToolCallId, dependsOn: [] },
  166. { toolCallId: "tool_2" as ToolCallId, dependsOn: [] },
  167. { toolCallId: "tool_3" as ToolCallId, dependsOn: [] },
  168. ];
  169. const schedule = schedulerWithLimit.schedule(tools);
  170. // With maxConcurrency=3 and exactly 3 tools, should be one group
  171. expect(schedule.parallelGroups).toHaveLength(1);
  172. expect(schedule.parallelGroups[0]).toHaveLength(3);
  173. });
  174. it("should default to maxConcurrency=10", () => {
  175. const defaultScheduler = new ToolScheduler();
  176. const tools: ToolDependency[] = Array.from({ length: 15 }, (_, i) => ({
  177. toolCallId: `tool_${i}` as ToolCallId,
  178. dependsOn: [],
  179. }));
  180. const schedule = defaultScheduler.schedule(tools);
  181. // Default maxConcurrency is 10, so 15 tools should be [10, 5]
  182. expect(schedule.parallelGroups).toHaveLength(2);
  183. expect(schedule.parallelGroups[0]).toHaveLength(10);
  184. expect(schedule.parallelGroups[1]).toHaveLength(5);
  185. });
  186. });
  187. });