run-metadata.test.ts 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /**
  2. * Run 元数据经**真实入口**落库:runWorkflowScript → 引擎构造 → createRun。
  3. *
  4. * 直接 new WorkflowEngine 的单测(dynamic-workflow 包内)只证明引擎会写这四个字段;
  5. * 这里证明 harness 真的把它们递下去了——中间少接一个字段的表现是 dwf_run 里永远为空,
  6. * 而 resume 的 script_hash 校验因此永远没有比对对象。
  7. *
  8. * harness 刻意**不**自己算 scriptHash:编译一次的路径传 `lowered`(此时 harness 手里根本
  9. * 没有原始脚本文本),若 harness "顺手" 哈希它看到的文本,那条路径上落库的就是 lowered
  10. * 函数体的哈希——一个静默错误的比对对象。哈希归调用方。
  11. */
  12. import { describe, expect, it } from "vitest";
  13. import { InMemoryJournalStore } from "@zcode/dynamic-workflow";
  14. import { runWorkflowScript } from "../src/index.js";
  15. import { AutoDriver } from "./auto-driver.js";
  16. import { askSpecsFor, TEST_CWD } from "./helpers.js";
  17. const RUN = "run";
  18. const SCRIPT = ['const a = agent("a");', 'const r = await a.ask("hello");', "return r;"].join("\n");
  19. describe("run metadata through the real entry point", () => {
  20. it("forwards scriptText / scriptHash / parentSessionId / cwd / toolCallId onto the journaled run record", async () => {
  21. const journal = new InMemoryJournalStore();
  22. const driver = new AutoDriver(journal, { asks: { "ask#1": () => ({ type: "text", finalText: "hi" }) } });
  23. const settlement = await runWorkflowScript({
  24. scriptText: SCRIPT,
  25. runId: RUN,
  26. caps: { maxConcurrency: 16 },
  27. askSpecs: askSpecsFor(SCRIPT),
  28. validate: () => [],
  29. makeDriver: (sink) => {
  30. driver.attach(sink);
  31. return driver;
  32. },
  33. timeoutMs: 20000,
  34. // 调用方给定的元数据:verbatim 递到 EngineConfig,harness 不加工。
  35. scriptHash: "caller-computed-hash",
  36. parentSessionId: "sess_parent",
  37. cwd: TEST_CWD,
  38. toolCallId: "call-origin",
  39. });
  40. expect(settlement).toEqual({ status: "completed", artifact: "hi" });
  41. const run = journal.getRun(RUN);
  42. expect(run).toMatchObject({
  43. runId: RUN,
  44. status: "completed",
  45. // scriptText 落的是调用方给的脚本原文,不是 lowered 之后的函数体。
  46. scriptText: SCRIPT,
  47. scriptHash: "caller-computed-hash",
  48. parentSessionId: "sess_parent",
  49. cwd: TEST_CWD,
  50. // 重启后工具卡 join / resume 通知锚点的唯一持久来源(apps/zcode-cli/packages/dynamic-workflow/docs/execution-engine.md)。
  51. toolCallId: "call-origin",
  52. });
  53. });
  54. it("carries the run's subagent model on the journaled run-launched event, reasoning segment included", async () => {
  55. // 子代理模型零 SQL 地活在 `run-launched` 里(docs/dynamic-workflow/launch.md),与发起锚点、
  56. // 阶段表同车:harness 只把 launch 整块 verbatim 转交 EngineConfig,不读、不解析、不加工。
  57. const journal = new InMemoryJournalStore();
  58. const driver = new AutoDriver(journal, {
  59. asks: { "ask#1": () => ({ type: "text", finalText: "hi" }) },
  60. });
  61. await runWorkflowScript({
  62. scriptText: SCRIPT,
  63. runId: RUN,
  64. cwd: TEST_CWD,
  65. caps: { maxConcurrency: 16 },
  66. askSpecs: askSpecsFor(SCRIPT),
  67. validate: () => [],
  68. makeDriver: (sink) => {
  69. driver.attach(sink);
  70. return driver;
  71. },
  72. timeoutMs: 20000,
  73. launch: { inputId: "input-origin", subagentModel: "zhipu/glm-5.3-flash$high" },
  74. });
  75. const launched = journal
  76. .listEvents(RUN, { types: "all", reportItems: "all" })
  77. .find((stored) => stored.event.type === "run-launched");
  78. expect(launched?.event).toEqual({
  79. type: "run-launched",
  80. inputId: "input-origin",
  81. // 档位段原样带着走:`$high` 是选择的一部分,宿主从这条事件读回整串。
  82. subagentModel: "zhipu/glm-5.3-flash$high",
  83. });
  84. // 行上没有这条事实:它不是 dwf_run 的列(用户裁决不做迁移)。
  85. expect("subagentModel" in journal.getRun(RUN)!).toBe(false);
  86. });
  87. it("omits subagentModel entirely when launch carries none", async () => {
  88. // 缺席即「子代理跑在会话模型上」。落成值为 undefined 的键会让读侧把「没设」读成
  89. // 「设了一个空模型」——两者在 AmendWorkflow 的三态里是不同的意思。
  90. const journal = new InMemoryJournalStore();
  91. const driver = new AutoDriver(journal, {
  92. asks: { "ask#1": () => ({ type: "text", finalText: "hi" }) },
  93. });
  94. await runWorkflowScript({
  95. scriptText: SCRIPT,
  96. runId: RUN,
  97. cwd: TEST_CWD,
  98. caps: { maxConcurrency: 16 },
  99. askSpecs: askSpecsFor(SCRIPT),
  100. validate: () => [],
  101. makeDriver: (sink) => {
  102. driver.attach(sink);
  103. return driver;
  104. },
  105. timeoutMs: 20000,
  106. launch: { inputId: "input-origin" },
  107. });
  108. const launched = journal
  109. .listEvents(RUN, { types: "all", reportItems: "all" })
  110. .find((stored) => stored.event.type === "run-launched");
  111. expect("subagentModel" in launched!.event).toBe(false);
  112. });
  113. it("journals the authored scriptText, not the lowered body, when both are given", async () => {
  114. // 「编译一次」的形态:调用方自己编译得到 lowered,同时把作者原文交下来。落库的必须是
  115. // 原文——两段文本刻意不同,所以"顺手拿 lowered 顶替"会被这条断言抓住。
  116. const journal = new InMemoryJournalStore();
  117. const driver = new AutoDriver(journal, {});
  118. const settlement = await runWorkflowScript({
  119. cwd: TEST_CWD,
  120. lowered: "return 7;",
  121. runId: RUN,
  122. caps: { maxConcurrency: 16 },
  123. askSpecs: new Map(),
  124. validate: () => [],
  125. makeDriver: (sink) => {
  126. driver.attach(sink);
  127. return driver;
  128. },
  129. timeoutMs: 20000,
  130. scriptText: "return 3 + 4; // authored",
  131. scriptHash: "hash-of-source",
  132. });
  133. expect(settlement).toEqual({ status: "completed", artifact: 7 });
  134. expect(journal.getRun(RUN)).toMatchObject({
  135. scriptText: "return 3 + 4; // authored",
  136. scriptHash: "hash-of-source",
  137. });
  138. });
  139. it("forwards the run name onto the journaled run record", async () => {
  140. // name 与 scriptText / cwd 同一条元数据路。harness 少接这一个字段的表现是 dwf_run.name
  141. // 永远为空,而宿主的枚举面只能列出裸 runId(run 内省工具的标签就此失效)。
  142. const journal = new InMemoryJournalStore();
  143. const driver = new AutoDriver(journal, {});
  144. await runWorkflowScript({
  145. cwd: TEST_CWD,
  146. lowered: "return 1;",
  147. runId: RUN,
  148. caps: { maxConcurrency: 16 },
  149. askSpecs: new Map(),
  150. validate: () => [],
  151. makeDriver: (sink) => {
  152. driver.attach(sink);
  153. return driver;
  154. },
  155. timeoutMs: 20000,
  156. name: "nightly triage",
  157. });
  158. expect(journal.getRun(RUN)).toMatchObject({ name: "nightly triage" });
  159. });
  160. it("omits the metadata entirely when the caller passes none", async () => {
  161. const journal = new InMemoryJournalStore();
  162. const driver = new AutoDriver(journal, {});
  163. await runWorkflowScript({
  164. cwd: TEST_CWD,
  165. lowered: "return 1;",
  166. runId: RUN,
  167. caps: { maxConcurrency: 16 },
  168. askSpecs: new Map(),
  169. validate: () => [],
  170. makeDriver: (sink) => {
  171. driver.attach(sink);
  172. return driver;
  173. },
  174. timeoutMs: 20000,
  175. });
  176. const run = journal.getRun(RUN)!;
  177. for (const field of ["scriptText", "scriptHash", "parentSessionId", "name"]) {
  178. expect(field in run).toBe(false);
  179. }
  180. });
  181. it("rejects a resume whose scriptHash does not match the journaled one", async () => {
  182. // 端到端地证明这条校验现在有比对对象了:第一趟落库哈希,第二趟换一个哈希即被拒。
  183. const journal = new InMemoryJournalStore();
  184. const driver = new AutoDriver(journal, { asks: { "ask#1": () => ({ type: "text", finalText: "hi" }) } });
  185. const base = {
  186. runId: RUN,
  187. caps: { maxConcurrency: 16 },
  188. askSpecs: askSpecsFor(SCRIPT),
  189. validate: () => [],
  190. timeoutMs: 20000,
  191. } as const;
  192. await runWorkflowScript({
  193. cwd: TEST_CWD,
  194. ...base,
  195. scriptText: SCRIPT,
  196. scriptHash: "hash-v1",
  197. makeDriver: (sink) => {
  198. driver.attach(sink);
  199. return driver;
  200. },
  201. });
  202. expect(journal.getRun(RUN)?.scriptHash).toBe("hash-v1");
  203. const resumed = new AutoDriver(journal, {});
  204. await expect(
  205. runWorkflowScript({
  206. cwd: TEST_CWD,
  207. ...base,
  208. scriptText: `${SCRIPT}\n// edited`,
  209. scriptHash: "hash-v2",
  210. makeDriver: (sink) => {
  211. resumed.attach(sink);
  212. return resumed;
  213. },
  214. }),
  215. ).rejects.toMatchObject({
  216. code: "ScriptHashMismatch",
  217. mismatch: { expected: "hash-v1", got: "hash-v2" },
  218. });
  219. // 被拒的 resume 不得改写既有记录(它仍可用 hash-v1 的脚本 resume)。
  220. expect(journal.getRun(RUN)).toMatchObject({ status: "completed", scriptHash: "hash-v1" });
  221. });
  222. });