workflow-web-search-budget.test.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**
  2. * 每个 ask 的 WebSearch 预算(apps/zcode-cli/packages/dynamic-workflow/docs/execution-engine.md
  3. * 「Search budget」)的 driver 层用例。直接持 runtimeFactory 交出的端口调用 charge()——那正是
  4. * core 的 WebSearch handler 会做的事。装配与 workflow-escalation.test.ts 相同:turn 永不结束的
  5. * stub runtime + 内存 journal。
  6. */
  7. import { describe, expect, it } from "vitest";
  8. import type { WorkflowWebSearchBudgetPort } from "@zcode/contracts";
  9. import {
  10. DEFAULT_WEB_SEARCH_BUDGET_PER_ASK,
  11. InMemoryJournalStore,
  12. type ActorRef,
  13. type InstanceRef,
  14. type PersonaSpec,
  15. type SessionRef,
  16. type WorkflowDriver,
  17. type WorkflowReportSink,
  18. } from "@zcode/dynamic-workflow";
  19. import { createAgentRuntimeWorkflowDriver } from "../src/app/workflow-driver.js";
  20. import { createWorkflowEscalationRegistry } from "../src/app/workflow-escalation-registry.js";
  21. import { fakeFileSystemPort, unsupportedExecutionPort } from "./workflow-driver.helpers.js";
  22. const NOOP_SINK: WorkflowReportSink = {
  23. askSubmitAttempted: () => {},
  24. askTurnEnded: () => {},
  25. askProgress: () => {},
  26. askStats: () => {},
  27. askFailed: () => {},
  28. };
  29. async function openAsk(persona: PersonaSpec = {}): Promise<{
  30. driver: WorkflowDriver;
  31. port: WorkflowWebSearchBudgetPort;
  32. session: SessionRef;
  33. instance: InstanceRef;
  34. }> {
  35. const runId = "dwfrun-55556666-7777-8888";
  36. const journal = new InMemoryJournalStore();
  37. journal.createRun({ runId, caps: { maxConcurrency: 16 }, spentTokens: 0, status: "running" });
  38. const ports: WorkflowWebSearchBudgetPort[] = [];
  39. const driver = createAgentRuntimeWorkflowDriver({
  40. journal,
  41. emit: () => {},
  42. escalationRegistry: createWorkflowEscalationRegistry(),
  43. executionPort: unsupportedExecutionPort(),
  44. fileSystemPort: fakeFileSystemPort({}),
  45. cwd: process.cwd(),
  46. runId,
  47. runtimeFactory: ({ webSearchBudgetPort }) => {
  48. ports.push(webSearchBudgetPort);
  49. return { executeTurn: () => new Promise(() => {}) } as never;
  50. },
  51. })(NOOP_SINK);
  52. const actor: ActorRef = { siteId: "actor#1", ordinal: 1 };
  53. const session = await driver.createActorSession(actor, persona);
  54. const instance: InstanceRef = { siteId: "ask#1", ordinal: 1 };
  55. driver.startAsk(session, instance, { instructions: "go", typed: false });
  56. const port = ports[0];
  57. if (port === undefined) throw new Error("runtimeFactory 没有交出 webSearchBudgetPort");
  58. return { driver, port, session, instance };
  59. }
  60. function chargeTimes(port: WorkflowWebSearchBudgetPort, times: number): string[] {
  61. return Array.from({ length: times }, () => port.charge().kind);
  62. }
  63. describe("workflow WebSearch budget", () => {
  64. it("allows the default number of calls per ask, then refuses with the limit in the message", async () => {
  65. const { port } = await openAsk();
  66. expect(chargeTimes(port, DEFAULT_WEB_SEARCH_BUDGET_PER_ASK).every((k) => k === "charged")).toBe(
  67. true,
  68. );
  69. const over = port.charge();
  70. expect(over).toMatchObject({ kind: "exhausted", limit: DEFAULT_WEB_SEARCH_BUDGET_PER_ASK });
  71. expect(over.kind === "exhausted" && over.message).toContain(
  72. `at most ${DEFAULT_WEB_SEARCH_BUDGET_PER_ASK} WebSearch calls`,
  73. );
  74. });
  75. it("uses the persona's own budget; 0 refuses the first call", async () => {
  76. const wide = await openAsk({ budget: { webSearch: 3 } });
  77. expect(chargeTimes(wide.port, 4)).toEqual(["charged", "charged", "charged", "exhausted"]);
  78. const none = await openAsk({ budget: { webSearch: 0 } });
  79. expect(none.port.charge().kind).toBe("exhausted");
  80. });
  81. it("shares the count with a nudge turn and resets it at the next ask", async () => {
  82. const { driver, port, session, instance } = await openAsk({ budget: { webSearch: 2 } });
  83. expect(chargeTimes(port, 2)).toEqual(["charged", "charged"]);
  84. // nudge 仍在同一个 ask 里:起新 turn 但不归零。
  85. driver.respondToSubmit(instance, { kind: "nudge" });
  86. expect(port.charge().kind).toBe("exhausted");
  87. // 下一个 ask:预算是满的。
  88. driver.startAsk(session, { siteId: "ask#2", ordinal: 1 }, { instructions: "next", typed: false });
  89. expect(chargeTimes(port, 3)).toEqual(["charged", "charged", "exhausted"]);
  90. });
  91. });