workflowCachedSettleAttribution.test.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // 修订 / resume 之后「命中缓存的子代理显示待开始」(2026-09-22 用户报告)的读面回归。
  2. //
  3. // 走真实管线的后两段:事件信封 → 共享 reducer → 时间线模型 / transcript 门。修订 run 从空表
  4. // 开始,缓存命中又没有 node-queued,所以命中的结算必须自己点名子代理
  5. // (docs/dynamic-workflow/presentation.md「Reduction」);transcript 要去持有交换的那条会话
  6. // 里读(「Subagent transcripts」)。
  7. import { describe, expect, it } from "vitest";
  8. import {
  9. reduceWorkflowRunsState,
  10. type WorkflowRunProgressEnvelope,
  11. type WorkflowRunsState,
  12. } from "@zcode/shared/zcode-protocol-v4";
  13. import type { WorkflowCausalityGraphData } from "@/components/workflow-graph/types.js";
  14. import { buildWorkflowTimeline } from "@/components/workflow-timeline/timeline-model.js";
  15. import { workflowActorStartState } from "@/app-shell/workflowRunPanel.js";
  16. const RUN_ID = "dwfrun-b";
  17. /** 一条 `many` 车道:worker 扇出三个实例,同一个 ask 站点——拆卡的那条路径。 */
  18. const FAN_OUT: WorkflowCausalityGraphData = {
  19. exits: ["phase#1"],
  20. handoffs: [],
  21. lanes: [{ id: "actor#1", name: "worker" }],
  22. participants: [
  23. { id: "phase#1:actor#1", lane: "actor#1", many: true, phase: "phase#1", steps: ["ask#1"] },
  24. ],
  25. phases: [{ id: "phase#1", name: "work" }],
  26. steps: [{ id: "ask#1", kind: "ask", label: "ask#1", lane: "actor#1", phase: "phase#1" }],
  27. };
  28. const actorRef = (ordinal: number) => ({ siteId: "actor#1", ordinal });
  29. const askRef = (ordinal: number) => ({ siteId: "ask#1", ordinal });
  30. const ownSession = (ordinal: number) => `sess-b-${ordinal}`;
  31. const predecessorSession = (ordinal: number) => `sess-a-${ordinal}`;
  32. function reduce(events: Array<[string, Record<string, unknown>, string?]>): WorkflowRunsState {
  33. let state: WorkflowRunsState | undefined;
  34. events.forEach(([eventType, payload, actorSessionId], sequence) => {
  35. const envelope: WorkflowRunProgressEnvelope = {
  36. runId: RUN_ID,
  37. toolCallId: "tc-1",
  38. sequence,
  39. eventType,
  40. payload,
  41. ...(actorSessionId === undefined ? {} : { actorSessionId }),
  42. };
  43. state = reduceWorkflowRunsState(state, envelope) ?? state;
  44. });
  45. return state!;
  46. }
  47. /** worker 1、2 命中导入缓存;worker 3 live。`namesActor: false` 是老 journal 的形状。 */
  48. function amendedRun(options: { namesActor: boolean; settled: boolean }): WorkflowRunsState {
  49. const cachedSettle = (ordinal: number): [string, Record<string, unknown>, string?] =>
  50. options.namesActor
  51. ? [
  52. "node-settled",
  53. {
  54. instance: askRef(ordinal),
  55. outcome: "ok",
  56. cached: true,
  57. kind: "ask",
  58. actor: actorRef(ordinal),
  59. actorSeq: 0,
  60. sourceSessionId: predecessorSession(ordinal),
  61. },
  62. ownSession(ordinal),
  63. ]
  64. : ["node-settled", { instance: askRef(ordinal), outcome: "ok", cached: true }];
  65. return reduce([
  66. ["run-started", { caps: { maxConcurrency: 4 } }],
  67. ["phase-entered", { name: "work", ordinal: 1 }],
  68. ...[1, 2, 3].map((ordinal): [string, Record<string, unknown>, string?] => [
  69. "actor-created",
  70. { actor: actorRef(ordinal), name: `w${ordinal}`, phaseName: "work" },
  71. ownSession(ordinal),
  72. ]),
  73. cachedSettle(1),
  74. cachedSettle(2),
  75. ["node-queued", { instance: askRef(3), kind: "ask", actor: actorRef(3), actorSeq: 0 }],
  76. [
  77. "node-dispatched",
  78. { instance: askRef(3), kind: "ask", actor: actorRef(3), actorName: "w3" },
  79. ownSession(3),
  80. ],
  81. ["node-executing", { instance: askRef(3) }],
  82. ...(options.settled
  83. ? ([
  84. ["node-settled", { instance: askRef(3), outcome: "ok" }],
  85. ["run-settled", { status: "completed" }],
  86. ] as Array<[string, Record<string, unknown>, string?]>)
  87. : []),
  88. ]);
  89. }
  90. function pillsOf(state: WorkflowRunsState) {
  91. const run = state.runs[0]!;
  92. return buildWorkflowTimeline(FAN_OUT, run).stations[0]!.pills.map((pill) => ({
  93. key: pill.key,
  94. status: pill.status,
  95. sessionId: pill.instance?.sessionId,
  96. }));
  97. }
  98. describe("修订 run 的缓存命中:拆开的实例卡", () => {
  99. it("命中的两个 worker 是 done,不是待开始(run 还在跑时也一样)", () => {
  100. expect(pillsOf(amendedRun({ namesActor: true, settled: false }))).toEqual([
  101. { key: "phase#1:actor#1@1", status: "done", sessionId: predecessorSession(1) },
  102. { key: "phase#1:actor#1@2", status: "done", sessionId: predecessorSession(2) },
  103. { key: "phase#1:actor#1@3", status: "running", sessionId: ownSession(3) },
  104. ]);
  105. });
  106. it("run 完成之后仍是 done(报告里的形状:待开始一直停到最后)", () => {
  107. expect(
  108. pillsOf(amendedRun({ namesActor: true, settled: true })).map((pill) => pill.status),
  109. ).toEqual(["done", "done", "done"]);
  110. });
  111. it("老 journal(命中不点名子代理):归属不上,卡停在 pending——记录在案的限制,不是回归", () => {
  112. expect(
  113. pillsOf(amendedRun({ namesActor: false, settled: true })).map((pill) => pill.status),
  114. ).toEqual(["pending", "pending", "done"]);
  115. });
  116. });
  117. describe("修订 run 的缓存命中:transcript 门", () => {
  118. const slot = (ordinal: number) => ({ runId: RUN_ID, siteId: "actor#1", ordinal });
  119. it("全部命中的子代理已启动,订阅前驱里持有交换的那条会话", () => {
  120. const runs = amendedRun({ namesActor: true, settled: true }).runs;
  121. expect(workflowActorStartState(runs, slot(1))).toEqual({
  122. state: "started",
  123. sessionId: predecessorSession(1),
  124. });
  125. // live 的那个照旧订阅本 run 自己的会话。
  126. expect(workflowActorStartState(runs, slot(3))).toEqual({
  127. state: "started",
  128. sessionId: ownSession(3),
  129. });
  130. });
  131. it("tab 打开时带的是本 run 铸的 id:门以投影为准,不去订阅那条从未建过的会话", () => {
  132. const runs = amendedRun({ namesActor: true, settled: true }).runs;
  133. expect(
  134. workflowActorStartState(runs, { ...slot(2), actorSessionId: ownSession(2) }).sessionId,
  135. ).toBe(predecessorSession(2));
  136. });
  137. it("老 journal:门仍说未启动(归属不上的命中证明不了任何会话)", () => {
  138. const runs = amendedRun({ namesActor: false, settled: true }).runs;
  139. expect(workflowActorStartState(runs, slot(1)).state).toBe("notStarted");
  140. });
  141. });