workflow-effective-selection.test.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import * as executionAdapters from "@zcode/adapters/exec";
  2. import { mkdtemp, rm } from "node:fs/promises";
  3. import { tmpdir } from "node:os";
  4. import { join } from "node:path";
  5. import { createSqliteSessionStore } from "@zcode/adapters/storage";
  6. import { describe, expect, it, vi } from "vitest";
  7. import {
  8. createRegistryBackedTestApp,
  9. createTestProviderRegistry,
  10. } from "./helpers/registry-backed-test-app.js";
  11. describe("Workflow 内显式 Subagent 的有效选择", () => {
  12. it.each(["yolo", "guarded"] as const)(
  13. "%s 的真实 Workflow child 调用 Agent 时继承公共解析端口",
  14. async (mode) => {
  15. const root = await mkdtemp(join(tmpdir(), "zcode-workflow-selection-"));
  16. const store = await createSqliteSessionStore({ dbPath: ":memory:" });
  17. const parent = {
  18. providerId: "parent",
  19. modelId: "model",
  20. options: { reasoningLevel: "high" },
  21. };
  22. const original = {
  23. providerId: "old-account",
  24. modelId: "model",
  25. options: { reasoningLevel: "high" },
  26. };
  27. const effective = { ...original, providerId: "new-account" };
  28. const resolve = vi.fn(() => ({ effectiveSelection: effective }));
  29. const executed: string[] = [];
  30. let requestedChild = false;
  31. const execution = vi.spyOn(executionAdapters, "createNodeExecutionAdapter");
  32. const app = await createRegistryBackedTestApp({
  33. env: { ZCODE_HOME: root },
  34. skipUserConfig: true,
  35. sessionStore: store,
  36. providerRegistry: createTestProviderRegistry([parent, effective]),
  37. resolveEffectiveModelSelection: resolve,
  38. runtimeConfig: {
  39. mode,
  40. workingDirectory: root,
  41. mcp: { enabled: false },
  42. modelSelection: parent,
  43. subagents: {
  44. profiles: [
  45. {
  46. name: "researcher",
  47. description: "Research",
  48. source: "user",
  49. tools: ["Read"],
  50. systemPrompt: "Return a result.",
  51. modelSelection: original,
  52. },
  53. ],
  54. },
  55. },
  56. modelExecutor: {
  57. async generateText(_request, execution) {
  58. executed.push(execution.providerId);
  59. const usage = { inputTokens: 1, outputTokens: 1, totalTokens: 2 };
  60. if (!requestedChild) {
  61. requestedChild = true;
  62. return {
  63. finishReason: "tool-calls",
  64. text: "",
  65. usage,
  66. toolCalls: [
  67. {
  68. id: "workflow-research",
  69. name: "Agent",
  70. input: {
  71. description: "Research",
  72. prompt: "Research",
  73. subagent_type: "researcher",
  74. },
  75. },
  76. ],
  77. };
  78. }
  79. return {
  80. finishReason: "stop",
  81. text: JSON.stringify({ reasoning: "ok", verdict: "pass" }),
  82. usage,
  83. };
  84. },
  85. },
  86. });
  87. try {
  88. await app.runExpertWorkflow({ task: "Research with the researcher." });
  89. expect(execution.mock.calls.length).toBeGreaterThanOrEqual(2);
  90. for (const [options] of execution.mock.calls) {
  91. expect(options?.bashOutputTempDir).toBe(tmpdir());
  92. }
  93. expect(resolve).toHaveBeenCalledWith(original);
  94. expect(executed).toContain("new-account");
  95. expect(executed).not.toContain("old-account");
  96. expect(original.providerId).toBe("old-account");
  97. expect(app.runtime.getSessionModelSelection()).toEqual(parent);
  98. expect(app.runtime.getMode()).toBe(mode);
  99. } finally {
  100. execution.mockRestore();
  101. await app.close?.();
  102. store.close();
  103. await rm(root, { recursive: true, force: true });
  104. }
  105. },
  106. );
  107. });