dynamic-workflow-run-launch-anchor.test.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // dwf run 发起锚点的解析与读回(apps/zcode-cli/packages/dynamic-workflow/docs/execution-engine.md「Token telemetry for subagents」)。
  2. import { describe, expect, it } from "vitest";
  3. import { createRootTraceContext, createSessionId, createTurnId } from "@zcode/contracts";
  4. import { InMemoryJournalStore } from "@zcode/dynamic-workflow";
  5. import {
  6. readRunLaunchAnchor,
  7. readRunScriptPath,
  8. resolveLaunchAnchor,
  9. } from "../src/app/dynamic-workflow-run-launch-anchor.js";
  10. import { runScriptPathField } from "../src/app/dynamic-workflow-run-observation.js";
  11. const trace = createRootTraceContext({
  12. sessionId: createSessionId("anchor"),
  13. turnId: createTurnId("anchor"),
  14. });
  15. describe("resolveLaunchAnchor:优先级", () => {
  16. it("前驱锚点 > 显式 launchInputId > 活动轮解析 > 铸值", () => {
  17. const mint = () => "minted";
  18. expect(
  19. resolveLaunchAnchor({
  20. predecessor: { inputId: "pred" },
  21. requested: "req",
  22. resolveLaunchInputId: () => "active",
  23. trace,
  24. mint,
  25. }),
  26. ).toEqual({ inputId: "pred" });
  27. expect(
  28. resolveLaunchAnchor({ requested: "req", resolveLaunchInputId: () => "active", trace, mint }),
  29. ).toEqual({ inputId: "req" });
  30. expect(resolveLaunchAnchor({ resolveLaunchInputId: () => "active", trace, mint })).toEqual({
  31. inputId: "active",
  32. });
  33. expect(resolveLaunchAnchor({ resolveLaunchInputId: () => undefined, trace, mint })).toEqual({
  34. inputId: "minted",
  35. });
  36. });
  37. it("缺省铸 UUID v7", () => {
  38. const { inputId } = resolveLaunchAnchor({ trace });
  39. expect(inputId).toMatch(
  40. /^[0-9a-f]{8}-[0-9a-f]{4}-7[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/u,
  41. );
  42. });
  43. });
  44. describe("readRunLaunchAnchor:从 journal 读回", () => {
  45. it("首条 run-launched 的 inputId;没有则 undefined", () => {
  46. const journal = new InMemoryJournalStore();
  47. journal.createRun({
  48. runId: "r1",
  49. caps: { maxConcurrency: 1 },
  50. spentTokens: 0,
  51. status: "running",
  52. });
  53. journal.appendEvent("r1", { type: "run-started", runId: "r1", caps: { maxConcurrency: 1 } });
  54. expect(readRunLaunchAnchor(journal, "r1")).toBeUndefined();
  55. journal.appendEvent("r1", { type: "run-launched", inputId: "launch-1", toolCallId: "tool-1" });
  56. // resume 再发一次 run-started 不影响锚点。
  57. journal.appendEvent("r1", { type: "run-started", runId: "r1", caps: { maxConcurrency: 1 } });
  58. expect(readRunLaunchAnchor(journal, "r1")).toEqual({ inputId: "launch-1" });
  59. expect(readRunLaunchAnchor(journal, "missing")).toBeUndefined();
  60. });
  61. });
  62. // 本 run 的脚本文件(docs/dynamic-workflow/launch.md「Script files」的 Provenance)。
  63. //
  64. // 这一组钉的是**冷读路径的缺席语义**:两条读面对一个本进程没有条目的 run 只能扫事件头,而
  65. // 本特性之前发起的 run 要么整条 `run-launched` 都没有(锚点之前),要么有事件但没有这个键。
  66. // 两种都必须让读面上的字段**整个不出现**,而不是出现一个值为 undefined 的键——`GetWorkflowRun`
  67. // 的输出是「无则缺席」的投影,一个 undefined 值会让模型读到一个它无法 `Edit` 的路径。
  68. describe("readRunScriptPath:从 journal 读回脚本文件", () => {
  69. const seedRun = (journal: InMemoryJournalStore, runId: string): void => {
  70. journal.createRun({
  71. runId,
  72. caps: { maxConcurrency: 1 },
  73. spentTokens: 0,
  74. status: "running",
  75. });
  76. journal.appendEvent(runId, { type: "run-started", runId, caps: { maxConcurrency: 1 } });
  77. };
  78. it("整条 run-launched 都没有(锚点之前的老 run)→ undefined", () => {
  79. const journal = new InMemoryJournalStore();
  80. seedRun(journal, "r-old");
  81. expect(readRunScriptPath(journal, "r-old")).toBeUndefined();
  82. // 未知 run 与「没有这条事件」对读面是同一件事。
  83. expect(readRunScriptPath(journal, "missing")).toBeUndefined();
  84. });
  85. it("有 run-launched 但没有这个键(本特性之前的 run)→ undefined", () => {
  86. const journal = new InMemoryJournalStore();
  87. seedRun(journal, "r-no-key");
  88. journal.appendEvent("r-no-key", { type: "run-launched", inputId: "launch-1" });
  89. expect(readRunScriptPath(journal, "r-no-key")).toBeUndefined();
  90. });
  91. it("有这个键 → 首条 run-launched 上的绝对路径,resume 再发 run-started 不影响它", () => {
  92. const journal = new InMemoryJournalStore();
  93. seedRun(journal, "r-path");
  94. journal.appendEvent("r-path", {
  95. type: "run-launched",
  96. inputId: "launch-2",
  97. scriptPath: "/repo/.zcode/workflow-drafts/audit.dwf.ts",
  98. });
  99. journal.appendEvent("r-path", {
  100. type: "run-started",
  101. runId: "r-path",
  102. caps: { maxConcurrency: 1 },
  103. });
  104. expect(readRunScriptPath(journal, "r-path")).toBe("/repo/.zcode/workflow-drafts/audit.dwf.ts");
  105. });
  106. // 冷读的另一半:读回 undefined 之后,两条读面上的字段必须**整个不出现**。
  107. it("runScriptPathField 把 undefined 变成空对象:键不出现,不是值为 undefined", () => {
  108. const absent = runScriptPathField(undefined);
  109. expect(absent).toEqual({});
  110. expect("scriptPath" in absent).toBe(false);
  111. expect(runScriptPathField("/repo/audit.dwf.ts")).toEqual({
  112. scriptPath: "/repo/audit.dwf.ts",
  113. });
  114. });
  115. });