workflow-effective-selection.test.ts 3.5 KB

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