scheduler.test.ts 7.1 KB

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