dynamic-workflow-run-replay.test.ts 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. // dwf run 冷回放的结算来源(docs/dynamic-workflow/presentation.md「Cold replay」):
  2. // 行是终态时,回放的尾部 `run-settled` 一律按行铸造——journal 里存着的那条(无论新旧词表)让位;
  3. // 尾部没有结算(进程死亡 / 孤儿收敛只改行)时追加。
  4. //
  5. // 修复原因(2026-09-15,桌面实测):2026-09-14 终态词表重构之前写下的 `run-settled` 带旧词
  6. // cancelled / failed,事件载荷原样回放,reducer 认不出,run 被留在 running。这里用真实 SQLite
  7. // journal 落下一模一样的字节(appendEvent 原样存 JSON),钉住回放 + reducer 之后 run 必须是终态。
  8. import { SessionEventType, type SessionEvent } from "@zcode/contracts";
  9. import { ProductProjection } from "../src/zcode-protocol-v4/product-projection.js";
  10. import { mkdtemp, rm } from "node:fs/promises";
  11. import { tmpdir } from "node:os";
  12. import { join } from "node:path";
  13. import { afterEach, beforeEach, describe, expect, it } from "vitest";
  14. import { createSqliteSessionStore, type DwfRunSessionListItem } from "@zcode/adapters/storage";
  15. import type { JournalStorePort, RunEvent } from "@zcode/dynamic-workflow";
  16. import { reduceWorkflowRunsState, type WorkflowRunsState } from "@zcode/shared/zcode-protocol-v4";
  17. import { toProgressPayload } from "../src/app/dynamic-workflow-run-launch.js";
  18. import { replayRunProgress } from "../src/app/dynamic-workflow-run-replay.js";
  19. import { mintActorSessionId } from "../src/app/workflow-driver.js";
  20. import { resolveDynamicWorkflowJournalStore } from "../src/app/dynamic-workflow-run-service.js";
  21. const PARENT = "sess_replay_parent";
  22. // 冷回放的第三个参数:本进程的并发天花板,只落在 `run-started` 载荷上(docs/dynamic-workflow/concurrency.md「Two bounds on a run」)。
  23. // 这里的用例只看结算,取一个固定值即可;live 侧(toProgressPayload)传同一个值,逐字节契约才成立。
  24. const CEILING = 6;
  25. type Journal = JournalStorePort & {
  26. listRunsByParentSession(parentSessionId: string, limit: number): DwfRunSessionListItem[];
  27. };
  28. let tempRoot: string;
  29. let store: Awaited<ReturnType<typeof createSqliteSessionStore>>;
  30. let journal: Journal;
  31. beforeEach(async () => {
  32. tempRoot = await mkdtemp(join(tmpdir(), "zcode-dwf-replay-"));
  33. store = await createSqliteSessionStore({ dbPath: join(tempRoot, "session.sqlite") });
  34. const resolved = resolveDynamicWorkflowJournalStore(store as never);
  35. if (resolved === undefined) throw new Error("sqlite store must expose the dwf journal");
  36. journal = resolved as Journal;
  37. });
  38. afterEach(async () => {
  39. await store.close?.();
  40. await rm(tempRoot, { force: true, recursive: true });
  41. });
  42. function createRun(runId: string): void {
  43. journal.createRun({
  44. runId,
  45. parentSessionId: PARENT,
  46. cwd: "/repo",
  47. toolCallId: `call-${runId}`,
  48. scriptText: "const a = agent('a');",
  49. scriptHash: "sha-a",
  50. caps: { maxConcurrency: 4 },
  51. spentTokens: 0,
  52. status: "running",
  53. });
  54. journal.appendEvent(runId, {
  55. type: "run-started",
  56. runId,
  57. caps: { maxConcurrency: 4 },
  58. } as RunEvent);
  59. journal.appendEvent(runId, {
  60. type: "actor-created",
  61. actor: { siteId: "actor#1", ordinal: 1 },
  62. name: "a",
  63. } as RunEvent);
  64. }
  65. /** 旧词表的结算事件:`appendEvent` 原样存 JSON,落下的就是 2026-09-14 之前的字节。 */
  66. function appendLegacySettle(runId: string, status: "cancelled" | "failed", error?: unknown): void {
  67. journal.appendEvent(runId, {
  68. type: "run-settled",
  69. status,
  70. ...(error === undefined ? {} : { error }),
  71. } as unknown as RunEvent);
  72. }
  73. function rowOf(runId: string): DwfRunSessionListItem {
  74. const row = journal.listRunsByParentSession(PARENT, 8).find((item) => item.runId === runId);
  75. if (row === undefined) throw new Error(`row missing for ${runId}`);
  76. return row;
  77. }
  78. function reduce(runId: string): WorkflowRunsState["runs"][number] {
  79. let state: WorkflowRunsState | undefined;
  80. for (const payload of replayRunProgress(rowOf(runId), journal, CEILING)) {
  81. state = reduceWorkflowRunsState(state, payload) ?? state;
  82. }
  83. const run = state?.runs.find((item) => item.runId === runId);
  84. if (run === undefined) throw new Error("run not projected");
  85. return run;
  86. }
  87. describe("replayRunProgress — 结算按行铸造", () => {
  88. it("旧词表 run-settled{cancelled}(行已译成 stopped)→ 回放尾部是行铸的 stopped,reducer 落终态且可恢复", () => {
  89. createRun("dwfrun-legacy-cancelled");
  90. appendLegacySettle("dwfrun-legacy-cancelled", "cancelled");
  91. journal.updateRunStatus("dwfrun-legacy-cancelled", "stopped", { stopReason: "user" });
  92. const payloads = replayRunProgress(rowOf("dwfrun-legacy-cancelled"), journal, CEILING);
  93. const stored = journal.listEvents("dwfrun-legacy-cancelled", {
  94. types: "all",
  95. reportItems: "all",
  96. });
  97. // 一进一出:存着的结算被替换,不是追加——条数与 journal 一致,水位沿用被替换那条的 sequence。
  98. expect(payloads).toHaveLength(stored.length);
  99. const last = payloads.at(-1);
  100. expect(last).toMatchObject({
  101. eventType: "run-settled",
  102. sequence: stored.at(-1)?.sequence,
  103. payload: { status: "stopped", stopReason: "user", resumable: true },
  104. });
  105. expect(
  106. payloads.some((payload) => (payload.payload as { status?: unknown }).status === "cancelled"),
  107. ).toBe(false);
  108. const run = reduce("dwfrun-legacy-cancelled");
  109. expect(run.status).toBe("stopped");
  110. expect(run.stopReason).toBe("user");
  111. expect(run.resumable).toBe(true);
  112. });
  113. it("旧词表 run-settled{failed}(行是 errored)→ errored,失败文案来自行", () => {
  114. createRun("dwfrun-legacy-failed");
  115. appendLegacySettle("dwfrun-legacy-failed", "failed", { code: "DriverError", message: "boom" });
  116. journal.updateRunStatus("dwfrun-legacy-failed", "errored", {
  117. failure: { code: "DriverError", message: "boom" },
  118. });
  119. const run = reduce("dwfrun-legacy-failed");
  120. expect(run.status).toBe("errored");
  121. expect(run.error).toBe("boom");
  122. expect(run.resumable).toBeUndefined();
  123. });
  124. it("当前词表:行铸的结算与存着的那条逐字节相同(live 与冷回放同一份字节的契约不变)", () => {
  125. createRun("dwfrun-current");
  126. journal.updateRunStatus("dwfrun-current", "stopped", { stopReason: "model" });
  127. journal.appendEvent("dwfrun-current", {
  128. type: "run-settled",
  129. status: "stopped",
  130. stopReason: "model",
  131. } as RunEvent);
  132. const row = rowOf("dwfrun-current");
  133. const live = journal
  134. .listEvents("dwfrun-current", { types: "all", reportItems: "all" })
  135. .map((entry) =>
  136. toProgressPayload({
  137. event: entry.event,
  138. runId: row.runId,
  139. sequence: entry.sequence,
  140. toolCallId: row.toolCallId!,
  141. concurrencyCeiling: CEILING,
  142. }),
  143. );
  144. expect(JSON.stringify(replayRunProgress(row, journal, CEILING))).toBe(JSON.stringify(live));
  145. });
  146. it("行是终态而事件流没有结算(孤儿收敛只改了行)→ 追加一条 interrupted 结算,显式合成结算不推进真实水位", () => {
  147. createRun("dwfrun-orphan");
  148. journal.updateRunStatus("dwfrun-orphan", "stopped", {
  149. stopReason: "interrupted",
  150. failure: { code: "Interrupted", message: "owner exited" },
  151. });
  152. const stored = journal.listEvents("dwfrun-orphan", { types: "all", reportItems: "all" });
  153. const payloads = replayRunProgress(rowOf("dwfrun-orphan"), journal, CEILING);
  154. expect(payloads).toHaveLength(stored.length + 1);
  155. expect(payloads.at(-1)).toMatchObject({
  156. eventType: "run-settled",
  157. sequence: stored.at(-1)?.sequence ?? 0,
  158. syntheticSettlement: true,
  159. payload: { status: "stopped", stopReason: "interrupted", resumable: true },
  160. });
  161. expect(reduce("dwfrun-orphan").status).toBe("stopped");
  162. });
  163. it("中途的上一世结算(cancel → resume)原样保留,只替换尾部那条", () => {
  164. createRun("dwfrun-resumed");
  165. appendLegacySettle("dwfrun-resumed", "cancelled");
  166. journal.appendEvent("dwfrun-resumed", {
  167. type: "run-started",
  168. runId: "dwfrun-resumed",
  169. caps: { maxConcurrency: 4 },
  170. } as RunEvent);
  171. journal.updateRunStatus("dwfrun-resumed", "completed");
  172. journal.appendEvent("dwfrun-resumed", { type: "run-settled", status: "completed" } as RunEvent);
  173. const payloads = replayRunProgress(rowOf("dwfrun-resumed"), journal, CEILING);
  174. const settles = payloads.filter((payload) => payload.eventType === "run-settled");
  175. expect(settles.map((payload) => (payload.payload as { status: string }).status)).toEqual([
  176. "cancelled",
  177. "completed",
  178. ]);
  179. expect(reduce("dwfrun-resumed").status).toBe("completed");
  180. });
  181. // ── 本 run 的子代理模型(docs/dynamic-workflow/launch.md)───────────────────
  182. // 它与发起锚点同住 `run-launched`(零 SQL,dwf_run 上没有这一列):live 侧从 launch 入参拿到
  183. // 规范串并挂在 `run-started` 载荷上,冷回放从同一条事件读回。两侧必须给出同一个键,
  184. // 否则重启前后的投影就不再逐字节一致。
  185. it("run-launched 上的 subagentModel 进 run-started 载荷;冷回放与 live 逐字节相等", () => {
  186. const SUBAGENT = "zhipu/glm-5.3-flash$high";
  187. createRun("dwfrun-subagent");
  188. journal.appendEvent("dwfrun-subagent", {
  189. type: "run-launched",
  190. inputId: "input-origin",
  191. subagentModel: SUBAGENT,
  192. } as RunEvent);
  193. journal.updateRunStatus("dwfrun-subagent", "completed");
  194. journal.appendEvent("dwfrun-subagent", {
  195. type: "run-settled",
  196. status: "completed",
  197. } as RunEvent);
  198. const row = rowOf("dwfrun-subagent");
  199. const payloads = replayRunProgress(row, journal, CEILING);
  200. const started = payloads.find((payload) => payload.eventType === "run-started");
  201. expect(started?.payload).toMatchObject({
  202. subagentModel: SUBAGENT,
  203. concurrencyCeiling: CEILING,
  204. });
  205. // 派生字段只挂在 run-started 上:其余事件带着它只会让每一条载荷都重复同一条 run 级事实。
  206. // `run-launched` 自己不算——那条载荷是事件的原样内容,模型串本来就写在里面。
  207. for (const payload of payloads) {
  208. if (payload.eventType === "run-started" || payload.eventType === "run-launched") continue;
  209. expect("subagentModel" in payload.payload).toBe(false);
  210. }
  211. // live 侧同一条铸造链、同一个值 → 逐字节相等(本模块唯一的契约)。
  212. const live = journal
  213. .listEvents("dwfrun-subagent", { types: "all", reportItems: "all" })
  214. .map((entry) =>
  215. toProgressPayload({
  216. event: entry.event,
  217. runId: row.runId,
  218. sequence: entry.sequence,
  219. toolCallId: row.toolCallId!,
  220. launchInputId: "input-origin",
  221. concurrencyCeiling: CEILING,
  222. subagentModel: SUBAGENT,
  223. }),
  224. );
  225. expect(JSON.stringify(payloads)).toBe(JSON.stringify(live));
  226. });
  227. it("run-launched 没带子代理模型:run-started 载荷整个不带这个键", () => {
  228. createRun("dwfrun-no-subagent");
  229. journal.appendEvent("dwfrun-no-subagent", {
  230. type: "run-launched",
  231. inputId: "input-origin",
  232. } as RunEvent);
  233. const started = replayRunProgress(rowOf("dwfrun-no-subagent"), journal, CEILING).find(
  234. (payload) => payload.eventType === "run-started",
  235. );
  236. // 缺席而不是空串:读侧据此判断这个 run 有没有设过子代理模型。
  237. expect("subagentModel" in started!.payload).toBe(false);
  238. });
  239. // ── 脚本点名的模型(docs/dynamic-workflow/launch.md「Models the script names」)──────────
  240. // 绑定表同住 `run-launched`;actor-created 的派生 `model` 据它把 persona 的名字映射成规范串。
  241. // 冷回放从同一条事件读回同一张表,两侧载荷逐字节相等。
  242. it("actor-created 载荷的派生 model 来自 run-launched 的绑定表;冷回放与 live 逐字节相等", () => {
  243. const BINDINGS = { "GLM-5.3-Flash": "zhipu/GLM-5.3-Flash" };
  244. createRun("dwfrun-bindings");
  245. journal.appendEvent("dwfrun-bindings", {
  246. type: "run-launched",
  247. inputId: "input-origin",
  248. modelBindings: BINDINGS,
  249. } as RunEvent);
  250. journal.appendEvent("dwfrun-bindings", {
  251. type: "actor-created",
  252. actor: { siteId: "actor#7", ordinal: 1 },
  253. name: "评审员",
  254. persona: { name: "评审员", model: "GLM-5.3-Flash" },
  255. } as RunEvent);
  256. journal.appendEvent("dwfrun-bindings", {
  257. type: "actor-created",
  258. actor: { siteId: "actor#8", ordinal: 1 },
  259. name: "旁观者",
  260. persona: { name: "旁观者" },
  261. } as RunEvent);
  262. const row = rowOf("dwfrun-bindings");
  263. const payloads = replayRunProgress(row, journal, CEILING);
  264. // createRun 自己也建了一个子代理(无模型);这里只看本用例加的两个。
  265. const created = payloads.filter(
  266. (payload) =>
  267. payload.eventType === "actor-created" &&
  268. ["评审员", "旁观者"].includes(String(payload.payload.name)),
  269. );
  270. expect(created[0]?.payload).toMatchObject({ model: "zhipu/GLM-5.3-Flash" });
  271. expect("model" in created[1]!.payload).toBe(false);
  272. const live = journal
  273. .listEvents("dwfrun-bindings", { types: "all", reportItems: "all" })
  274. .map((entry) =>
  275. toProgressPayload({
  276. event: entry.event,
  277. runId: row.runId,
  278. sequence: entry.sequence,
  279. toolCallId: row.toolCallId!,
  280. launchInputId: "input-origin",
  281. concurrencyCeiling: CEILING,
  282. modelBindings: BINDINGS,
  283. }),
  284. );
  285. expect(JSON.stringify(payloads)).toBe(JSON.stringify(live));
  286. });
  287. // ── ask 的 node-dispatched(apps/zcode-cli/packages/dynamic-workflow/docs/execution-engine.md「Events」)─────
  288. // 它重复实例的出生事实并点名子代理,所以与 actor-created 一样在接缝上补 `actorSessionId`。
  289. // 冷回放走的是同一条铸造链,envelope 必须一字不差——否则重启前后投影就能收不同的实例进表。
  290. it("ask 的 node-dispatched 带出生事实与 actorSessionId;冷回放与 live 逐字节相等", () => {
  291. const actor = { siteId: "actor#1", ordinal: 1 };
  292. createRun("dwfrun-dispatch");
  293. journal.appendEvent("dwfrun-dispatch", {
  294. type: "node-dispatched",
  295. instance: { siteId: "ask#1", ordinal: 1 },
  296. kind: "ask",
  297. actor,
  298. actorName: "planner",
  299. actorPhaseName: "Prepare",
  300. phaseName: "Work",
  301. instructionsHead: "写一份调研大纲",
  302. } as RunEvent);
  303. // 同一条 journal 上的 world-read 派发:没有 actor,就不该长出会话 id。
  304. journal.appendEvent("dwfrun-dispatch", {
  305. type: "node-dispatched",
  306. instance: { siteId: "world-read#1", ordinal: 1 },
  307. } as RunEvent);
  308. const row = rowOf("dwfrun-dispatch");
  309. const payloads = replayRunProgress(row, journal, CEILING);
  310. const [ask, world] = payloads.filter((payload) => payload.eventType === "node-dispatched");
  311. expect(ask).toMatchObject({
  312. actorSessionId: mintActorSessionId("dwfrun-dispatch", actor),
  313. payload: {
  314. instance: { siteId: "ask#1", ordinal: 1 },
  315. kind: "ask",
  316. actor,
  317. actorName: "planner",
  318. actorPhaseName: "Prepare",
  319. phaseName: "Work",
  320. instructionsHead: "写一份调研大纲",
  321. },
  322. });
  323. // 铸的是与 actor-created 同一个 id:两条事件说的是同一个子代理的同一份转录。
  324. const created = payloads.find((payload) => payload.eventType === "actor-created");
  325. expect(ask?.actorSessionId).toBe(created?.actorSessionId);
  326. expect(world).toMatchObject({ payload: { instance: { siteId: "world-read#1", ordinal: 1 } } });
  327. expect("actorSessionId" in world!).toBe(false);
  328. const live = journal
  329. .listEvents("dwfrun-dispatch", { types: "all", reportItems: "all" })
  330. .map((entry) =>
  331. toProgressPayload({
  332. event: entry.event,
  333. runId: row.runId,
  334. sequence: entry.sequence,
  335. toolCallId: row.toolCallId!,
  336. concurrencyCeiling: CEILING,
  337. }),
  338. );
  339. expect(JSON.stringify(payloads)).toBe(JSON.stringify(live));
  340. });
  341. it("ask 的缓存结算带出生事实与 actorSessionId;world 与 live 的结算不补;冷回放与 live 逐字节相等", () => {
  342. const actor = { siteId: "actor#1", ordinal: 1 };
  343. createRun("dwfrun-cached");
  344. // 导入命中:答案读自前驱的会话。
  345. journal.appendEvent("dwfrun-cached", {
  346. type: "node-settled",
  347. instance: { siteId: "ask#1", ordinal: 1 },
  348. outcome: "ok",
  349. cached: true,
  350. kind: "ask",
  351. actor,
  352. actorSeq: 0,
  353. instructionsHead: "调研",
  354. sourceSessionId: "sess-predecessor",
  355. } as RunEvent);
  356. // 重放命中:答案是本 run 自己跑出来的。
  357. journal.appendEvent("dwfrun-cached", {
  358. type: "node-settled",
  359. instance: { siteId: "ask#2", ordinal: 1 },
  360. outcome: "ok",
  361. cached: true,
  362. kind: "ask",
  363. actor,
  364. actorSeq: 1,
  365. } as RunEvent);
  366. // 没有子代理的两条:world-read 的缓存结算,以及 live 的结算(不是出生事件,不点名子代理)。
  367. journal.appendEvent("dwfrun-cached", {
  368. type: "node-settled",
  369. instance: { siteId: "world-read#1", ordinal: 1 },
  370. outcome: "ok",
  371. cached: true,
  372. } as RunEvent);
  373. journal.appendEvent("dwfrun-cached", {
  374. type: "node-settled",
  375. instance: { siteId: "ask#3", ordinal: 1 },
  376. outcome: "ok",
  377. } as RunEvent);
  378. const row = rowOf("dwfrun-cached");
  379. const payloads = replayRunProgress(row, journal, CEILING);
  380. const settles = payloads.filter((payload) => payload.eventType === "node-settled");
  381. const own = mintActorSessionId("dwfrun-cached", actor);
  382. expect(settles.map((payload) => payload.actorSessionId)).toEqual([
  383. own,
  384. own,
  385. undefined,
  386. undefined,
  387. ]);
  388. // 载荷原样:前驱会话留在载荷里(引擎发了什么),派生字段在载荷之外。
  389. expect(settles[0]).toMatchObject({
  390. payload: { kind: "ask", actor, actorSeq: 0, sourceSessionId: "sess-predecessor" },
  391. });
  392. expect("actorSessionId" in settles[2]!).toBe(false);
  393. expect("actorSessionId" in settles[3]!).toBe(false);
  394. const live = journal
  395. .listEvents("dwfrun-cached", { types: "all", reportItems: "all" })
  396. .map((entry) =>
  397. toProgressPayload({
  398. event: entry.event,
  399. runId: row.runId,
  400. sequence: entry.sequence,
  401. toolCallId: row.toolCallId!,
  402. concurrencyCeiling: CEILING,
  403. }),
  404. );
  405. expect(JSON.stringify(payloads)).toBe(JSON.stringify(live));
  406. // 归约面:两条缓存结算都归到子代理名下;最后一条是本 run 的重放命中,sessionId 指回本 run。
  407. const run = reduce("dwfrun-cached");
  408. expect(
  409. run.nodes.filter((node) => node.actorSiteId === "actor#1").map((node) => node.siteId),
  410. ).toEqual(["ask#1", "ask#2"]);
  411. expect(run.actors[0]?.sessionId).toBe(own);
  412. });
  413. it("行不是终态(本会话真在飞的 run 由调用方排除;这里是另一进程的)→ 事件原样回放,不追加结算", () => {
  414. createRun("dwfrun-live");
  415. const stored = journal.listEvents("dwfrun-live", { types: "all", reportItems: "all" });
  416. const payloads = replayRunProgress(rowOf("dwfrun-live"), journal, CEILING);
  417. expect(payloads).toHaveLength(stored.length);
  418. expect(payloads.some((payload) => payload.eventType === "run-settled")).toBe(false);
  419. expect(reduce("dwfrun-live").status).toBe("running");
  420. });
  421. });
  422. it("孤儿行合成结算不占用恢复启动序号,恢复后停止控制可用且成功只计一次", () => {
  423. const runId = "orphan-resume";
  424. createRun(runId);
  425. journal.updateRunStatus(runId, "stopped", { stopReason: "interrupted" });
  426. const projection = new ProductProjection(PARENT, "epoch");
  427. let seq = 0;
  428. const emit = (payload: ReturnType<typeof replayRunProgress>[number]) => {
  429. projection.applyEvent({
  430. id: `event-${++seq}`,
  431. sessionId: PARENT,
  432. sequenceNumber: seq,
  433. timestamp: new Date(),
  434. traceId: "trace",
  435. type: SessionEventType.DynamicWorkflowRunProgress,
  436. payload,
  437. } as SessionEvent);
  438. };
  439. const coldPayloads = replayRunProgress(rowOf(runId), journal, CEILING);
  440. for (const payload of coldPayloads) emit(payload);
  441. expect(projection.getSnapshot().workflowRuns?.runs[0]?.status).toBe("stopped");
  442. const started = journal.appendEvent(runId, {
  443. type: "run-started",
  444. runId,
  445. caps: { maxConcurrency: 4 },
  446. } as RunEvent);
  447. emit(
  448. toProgressPayload({
  449. event: started.event,
  450. runId,
  451. sequence: started.sequence,
  452. concurrencyCeiling: CEILING,
  453. }),
  454. );
  455. // stop control 由运行态派生;下一条引擎事件尚未发生时必须已恢复运行态。
  456. // 迟到的旧合成停止也不能覆盖已经恢复的真实运行态。
  457. emit(coldPayloads.at(-1)!);
  458. expect(projection.getSnapshot().workflowRuns?.runs[0]?.status).toBe("running");
  459. expect(projection.getSnapshot().completedActivityCount).toBe(0);
  460. const settled = journal.appendEvent(runId, {
  461. type: "run-settled",
  462. status: "completed",
  463. } as RunEvent);
  464. const payload = toProgressPayload({
  465. event: settled.event,
  466. runId,
  467. sequence: settled.sequence,
  468. concurrencyCeiling: CEILING,
  469. });
  470. emit(payload);
  471. emit(payload);
  472. expect(projection.getSnapshot().completedActivityCount).toBe(1);
  473. });