subagent-effective-execution.test.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import { afterEach, describe, expect, it, vi } from "vitest";
  2. import { createSessionId, type ModelSelection } from "@zcode/contracts";
  3. import { createTestAgentRuntime } from "./test-agent-runtime.js";
  4. import { createTestModelFactory } from "./test-runtime-model.js";
  5. import { createTestSessionEventStore } from "./test-event-store.js";
  6. import { AgentRuntime } from "../src/runtime.js";
  7. describe("Subagent 有效选择到实际执行", () => {
  8. afterEach(() => vi.restoreAllMocks());
  9. const parent = {
  10. providerId: "custom-provider",
  11. modelId: "parent-model",
  12. options: { reasoningLevel: "high" },
  13. };
  14. const original = {
  15. providerId: "account:bigmodel-individual-coding-plan",
  16. modelId: "GLM-5.3",
  17. options: { reasoningLevel: "high" },
  18. };
  19. const effective = { ...original, providerId: "account:bigmodel-team-coding-plan" };
  20. it.each(["explicit", "inherit", "inherit-plan", "override", "unavailable"] as const)(
  21. "%s:通过 Agent tool 创建真实 child 并检查请求使用的模型",
  22. async (kind) => {
  23. const childSelections: ModelSelection[] = [];
  24. const resolveEffectiveModelSelection = vi.fn(() => ({
  25. effectiveSelection: kind === "unavailable" || kind === "override" ? null : effective,
  26. }));
  27. let mainCalls = 0;
  28. const executeTurnSpy = vi.spyOn(AgentRuntime.prototype, "executeTurn");
  29. const runtime = createTestAgentRuntime(
  30. createSessionId(`effective-child-${kind}`),
  31. {
  32. mode: "yolo",
  33. planEnabled: kind === "inherit-plan",
  34. workingDirectory: "/workspace/project",
  35. modelSelection: parent,
  36. subagents: {
  37. profiles: [
  38. {
  39. name: "researcher",
  40. description: "Research code",
  41. source: "user",
  42. tools: ["Read"],
  43. systemPrompt: "Return a short result.",
  44. ...(kind.startsWith("inherit") ? {} : { modelSelection: original }),
  45. },
  46. ],
  47. },
  48. },
  49. {
  50. eventStore: createTestSessionEventStore(),
  51. resolveEffectiveModelSelection,
  52. modelFactory: createTestModelFactory({
  53. async generateText(_request, observation) {
  54. const metadata = observation.invocationContext?.metadata as
  55. | { querySource?: string }
  56. | undefined;
  57. const usage = { inputTokens: 1, outputTokens: 1, totalTokens: 2 };
  58. if (metadata?.querySource === "subagent") {
  59. childSelections.push({
  60. providerId: observation.model.providerId,
  61. modelId: observation.model.modelId,
  62. options: { reasoningLevel: observation.model.options.reasoningLevel! },
  63. });
  64. return { finishReason: "stop", text: "child done", usage };
  65. }
  66. if (++mainCalls === 1)
  67. return {
  68. finishReason: "tool-calls",
  69. text: "",
  70. usage,
  71. toolCalls: [
  72. {
  73. id: "call_child",
  74. name: "Agent",
  75. input: {
  76. description: "Research",
  77. prompt: "Research code",
  78. subagent_type: "researcher",
  79. },
  80. },
  81. ],
  82. };
  83. return { finishReason: "stop", text: "done", usage };
  84. },
  85. }),
  86. },
  87. );
  88. await runtime.executeTurn(
  89. "Run the researcher.",
  90. undefined,
  91. kind === "override"
  92. ? {
  93. intent: {
  94. sourceCommandId: "execution-override",
  95. kind: "sendText",
  96. requestedDelivery: "start-now",
  97. modelSelection: effective,
  98. },
  99. modelExecution: {
  100. selectionScope: "execution",
  101. subagents: { foregroundModel: "submission", background: "deny" },
  102. },
  103. }
  104. : undefined,
  105. );
  106. expect(childSelections).toEqual(
  107. kind === "unavailable" ? [] : [kind.startsWith("inherit") ? parent : effective],
  108. );
  109. expect(resolveEffectiveModelSelection).toHaveBeenCalledTimes(
  110. kind === "explicit" || kind === "unavailable" ? 1 : 0,
  111. );
  112. expect(original.providerId).toBe("account:bigmodel-individual-coding-plan");
  113. expect(runtime.getSessionModelSelection()).toEqual(parent);
  114. if (kind === "inherit-plan") {
  115. const child = executeTurnSpy.mock.contexts.find((context) => context !== runtime);
  116. expect(child?.getMode()).toBe("yolo");
  117. expect(child?.getPlanEnabled()).toBe(true);
  118. }
  119. executeTurnSpy.mockRestore();
  120. },
  121. );
  122. });