workflow-driver-ask-progress.test.ts 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /**
  2. * driver → 引擎的进度回报(apps/zcode-cli/packages/dynamic-workflow/docs/execution-engine.md
  3. * 「Progress within an ask」):每解析一次 turn 恰好一条 `node-progress`,turn 号在 ask 内从 1 起、
  4. * nudge 轮 +1、换 ask 归零,工具计数与 `usage-updated` 同源,且**先于**同一次 turn 的 usage。
  5. *
  6. * 走真管线(真 driver + 真 AgentRuntime + 脚本化模型),因为被验证的正是 driver 的 turn 编排本身。
  7. * lastTool 的取法与目标线索的分寸在 workflow-driver-tool-activity.test.ts 单测。
  8. */
  9. import { describe, expect, it } from "vitest";
  10. import type { RunEvent } from "@zcode/dynamic-workflow";
  11. import { runDriverScript } from "./workflow-driver.helpers.js";
  12. type ProgressEvent = Extract<RunEvent, { type: "node-progress" }>;
  13. const progressOf = (events: RunEvent[]): ProgressEvent[] =>
  14. events.filter((e): e is ProgressEvent => e.type === "node-progress");
  15. const TYPED_ASK_SCRIPT = [
  16. "interface Summary { title: string; points: string[]; }",
  17. 'const worker = agent("worker", "You summarize repos.");',
  18. 'const s = await worker.ask<Summary>("Summarize the repo");',
  19. "return s;",
  20. ].join("\n");
  21. describe("workflow driver — node-progress", () => {
  22. it("一次 turn 一条,turn 从 1 起,且紧挨着排在 usage-updated 前面", async () => {
  23. const artifact = { title: "Repo", points: ["a"] };
  24. const { events } = await runDriverScript(TYPED_ASK_SCRIPT, {
  25. actorScripts: { "actor#1@1": [{ kind: "submit", result: artifact }] },
  26. });
  27. const progress = progressOf(events);
  28. expect(progress).toHaveLength(1);
  29. expect(progress[0]).toMatchObject({
  30. instance: { siteId: "ask#1", ordinal: 1 },
  31. turn: 1,
  32. // submit_result 这一次调用被观察面数进去了(与 AskStats.toolCalls 同一个计数器)。
  33. toolCalls: 1,
  34. lastTool: { name: "submit_result" },
  35. });
  36. // 顺序是契约:读到新用量的人一定已经读到了挣来它的那次进度。
  37. const index = events.findIndex((e) => e.type === "node-progress");
  38. expect(events[index + 1]?.type).toBe("usage-updated");
  39. });
  40. it("nudge 起的新一轮是第 2 个 turn(同一个 ask 内累加)", async () => {
  41. const valid = { title: "Repo", points: ["x"] };
  42. const { events } = await runDriverScript(TYPED_ASK_SCRIPT, {
  43. actorScripts: {
  44. "actor#1@1": [
  45. { kind: "text", text: "Here is the summary." },
  46. { kind: "submit", result: valid },
  47. ],
  48. },
  49. });
  50. const progress = progressOf(events);
  51. expect(progress.map((e) => e.turn)).toEqual([1, 2]);
  52. // 工具计数跨 nudge 轮累加:第一轮没调工具,第二轮的 submit_result 让它变成 1。
  53. expect(progress.map((e) => e.toolCalls)).toEqual([0, 1]);
  54. expect(progress[0]).not.toHaveProperty("lastTool");
  55. expect(progress[1]?.lastTool).toMatchObject({ name: "submit_result" });
  56. });
  57. it("换 ask 归零:同一个子代理的第二个 ask 又从 turn 1 数起", async () => {
  58. const script = [
  59. 'const worker = agent("worker");',
  60. 'const a = await worker.ask("first");',
  61. 'const b = await worker.ask("second");',
  62. "return `${a}/${b}`;",
  63. ].join("\n");
  64. const { settlement, events } = await runDriverScript(script, {
  65. actorScripts: {
  66. "actor#1@1": [
  67. { kind: "text", text: "A" },
  68. { kind: "text", text: "B" },
  69. ],
  70. },
  71. });
  72. expect(settlement).toEqual({ status: "completed", artifact: "A/B" });
  73. const progress = progressOf(events);
  74. expect(progress.map((e) => [e.instance.siteId, e.turn, e.toolCalls])).toEqual([
  75. ["ask#1", 1, 0],
  76. ["ask#2", 1, 0],
  77. ]);
  78. });
  79. });