workflowTimelineHoles.test.ts 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  1. // 留白在时间线模型上的派生(docs/dynamic-workflow/presentation.md「Holes on the timeline」):
  2. // 留白是一站(虚线灯、两侧虚线轨道、尾巴留白的 40px 残段);等待时是当前站;补全后体的阶段站在它的
  3. // 位置上、头(笔 · 名)悬在它们之上;无阶段的补全不抬头,站自己带笔标。类型从不上界面。
  4. // 卡、确认窗与侧板都从这一个模型出发,所以规则钉在这里。
  5. //
  6. // 夹具照分析器的真实输出写(apps/zcode-cli/packages/dynamic-workflow/docs/analysis.md「Sites」):
  7. // 留白 id 是名字键 `hole#<8 位十六进制>`,不随嵌套增长;留白**自己的**阶段带 `fill: <包着它的留白>`
  8. // (顶层没有),补全过的留白即使体以标记开头也留着自己的阶段。这里的十六进制是随手写的形状,
  9. // 渲染器只认形状,不重算哈希。
  10. import { describe, expect, it } from "vitest";
  11. import type { WorkflowRunState } from "@zcode/shared/zcode-protocol-v4";
  12. import type { WorkflowCausalityGraphData } from "@/components/workflow-graph/types.js";
  13. import { laneRefsById } from "@/components/workflow-graph/lane-name.js";
  14. import { buildWorkflowTimeline } from "@/components/workflow-timeline/timeline-model.js";
  15. import {
  16. fillContains,
  17. holeParentsOf,
  18. narrowTimelineToFill,
  19. withoutHeadedHolePhases,
  20. } from "@/components/workflow-timeline/timeline-holes.js";
  21. const H1 = "hole#a0000001"; // 决定分组
  22. const H2 = "hole#a0000002"; // 评判
  23. const H11 = "hole#a0000011"; // 挑选(在 H1 的体里)
  24. const H7 = "hole#a0000007"; // 不存在
  25. const H10 = "hole#a0000010"; // 与 H1 无关的另一个留白
  26. type Step = WorkflowCausalityGraphData["steps"][number];
  27. function participantsOf(steps: readonly Step[]) {
  28. const byId = new Map<string, { id: string; phase: string; lane: string; steps: string[] }>();
  29. for (const step of steps) {
  30. // 留白自己的站点(kind hole、车道 main)不是参与者:留白的阶段没有成员。
  31. if (step.kind === "hole") continue;
  32. const phase = step.phase ?? "unphased";
  33. const id = `${phase}:${step.lane}`;
  34. const existing = byId.get(id);
  35. if (existing) existing.steps.push(step.id);
  36. else byId.set(id, { id, lane: step.lane, phase, steps: [step.id] });
  37. }
  38. return [...byId.values()];
  39. }
  40. /** 探索 → hole 决定分组 : Plan → 执行 → tail hole 评判 : Verdict(设计画布的 flaky-hunt)。 */
  41. const OPEN_STEPS: Step[] = [
  42. { id: "ask#1", kind: "ask", label: "scout", lane: "actor#1", phase: "phase#1" },
  43. { id: H1, kind: "hole", label: "决定分组", lane: "main", phase: H1 },
  44. { id: "ask#2", kind: "ask", label: "member", lane: "actor#2", phase: "phase#2" },
  45. { id: H2, kind: "hole", label: "评判", lane: "main", phase: H2 },
  46. ];
  47. const OPEN: WorkflowCausalityGraphData = {
  48. steps: OPEN_STEPS,
  49. lanes: [
  50. { id: "actor#1", name: "侦察员" },
  51. { id: "actor#2", name: "组员" },
  52. ],
  53. participants: participantsOf(OPEN_STEPS),
  54. handoffs: [],
  55. phases: [
  56. { id: "phase#1", name: "探索" },
  57. { id: H1, name: "决定分组" },
  58. { id: "phase#2", name: "执行" },
  59. { id: H2, name: "评判" },
  60. ],
  61. phaseEdges: [
  62. { from: "phase#1", to: H1 },
  63. { from: H1, to: "phase#2" },
  64. { from: "phase#2", to: H2 },
  65. ],
  66. holes: [
  67. { siteId: H1, name: "决定分组", type: "Plan" },
  68. { siteId: H2, name: "评判", type: "Verdict", tail: true },
  69. ],
  70. };
  71. /**
  72. * 第一处补全之后的有效脚本:体以标记开头、声明了两个阶段。留白自己的阶段没有成员,但分析器留着它
  73. * (侧栏靠它念出「决定分组」);时间线把它交给头,不成站(withoutHeadedHolePhases)。
  74. */
  75. const FILLED_STEPS: Step[] = [
  76. { id: "ask#1", kind: "ask", label: "scout", lane: "actor#1", phase: "phase#1" },
  77. {
  78. id: `${H1}/ask#1`,
  79. kind: "ask",
  80. label: "smoke",
  81. lane: `${H1}/actor#1`,
  82. phase: `${H1}/phase#1`,
  83. fill: H1,
  84. },
  85. {
  86. id: `${H1}/ask#2`,
  87. kind: "ask",
  88. label: "group",
  89. lane: `${H1}/actor#2`,
  90. phase: `${H1}/phase#2`,
  91. fill: H1,
  92. },
  93. { id: "ask#2", kind: "ask", label: "member", lane: "actor#2", phase: "phase#2" },
  94. ];
  95. const FILLED: WorkflowCausalityGraphData = {
  96. steps: FILLED_STEPS,
  97. lanes: [
  98. { id: "actor#1", name: "侦察员" },
  99. { id: `${H1}/actor#1`, name: "冒烟员" },
  100. { id: `${H1}/actor#2`, name: "分组员" },
  101. { id: "actor#2", name: "组员" },
  102. ],
  103. participants: participantsOf(FILLED_STEPS),
  104. handoffs: [],
  105. phases: [
  106. { id: "phase#1", name: "探索" },
  107. { id: H1, name: "决定分组" },
  108. { id: `${H1}/phase#1`, name: "冒烟测试", fill: H1 },
  109. { id: `${H1}/phase#2`, name: "分组", fill: H1 },
  110. { id: "phase#2", name: "执行" },
  111. { id: H2, name: "评判" },
  112. ],
  113. phaseEdges: [
  114. { from: "phase#1", to: H1 },
  115. { from: H1, to: `${H1}/phase#1` },
  116. { from: `${H1}/phase#1`, to: `${H1}/phase#2` },
  117. { from: `${H1}/phase#2`, to: "phase#2" },
  118. { from: "phase#2", to: H2 },
  119. ],
  120. holes: [{ siteId: H2, name: "评判", type: "Verdict", tail: true }],
  121. };
  122. /** 第二处补全没有声明阶段:一次 ask 给评委,落在留白自己的阶段里。 */
  123. const TAIL_FILLED_STEPS: Step[] = [
  124. ...FILLED_STEPS,
  125. { id: `${H2}/ask#1`, kind: "ask", label: "judge", lane: `${H2}/actor#1`, phase: H2, fill: H2 },
  126. ];
  127. const TAIL_FILLED: WorkflowCausalityGraphData = {
  128. ...FILLED,
  129. steps: TAIL_FILLED_STEPS,
  130. lanes: [...FILLED.lanes, { id: `${H2}/actor#1`, name: "评委" }],
  131. participants: participantsOf(TAIL_FILLED_STEPS),
  132. holes: [],
  133. };
  134. function run(overrides: Partial<WorkflowRunState> = {}): WorkflowRunState {
  135. return {
  136. runId: "dwfrun-1",
  137. toolCallId: "tool-wf-1",
  138. status: "running",
  139. usage: { spentTokens: 0, nodesUsed: 0 },
  140. actors: [],
  141. nodes: [],
  142. lastEventSequence: 0,
  143. ...overrides,
  144. };
  145. }
  146. describe("留白站(静态)", () => {
  147. it("开着的留白是一站:hole.state open、类型原文、尾巴留白带 tail;两侧轨道段是虚线", () => {
  148. const model = buildWorkflowTimeline(OPEN, undefined);
  149. expect(model.stations.map((station) => station.id)).toEqual(["phase#1", H1, "phase#2", H2]);
  150. expect(model.stations[1]!.hole).toEqual({ siteId: H1, state: "open" });
  151. expect(model.stations[3]!.hole).toEqual({ siteId: H2, state: "open", tail: true });
  152. expect(model.stations[0]!.hole).toBeUndefined();
  153. expect(model.rails.map((rail) => rail.dashed)).toEqual([true, true, true]);
  154. expect(model.tailStub).toBe("open");
  155. expect(model.fills).toEqual([]);
  156. });
  157. it("留白自己的 kind hole 站点不算 ask:留白站没有药丸、摘要不计它", () => {
  158. const model = buildWorkflowTimeline(OPEN, undefined);
  159. expect(model.stations[1]!.pills).toEqual([]);
  160. expect(model.stations[0]!.pills.map((pill) => pill.lane.id)).toEqual(["actor#1"]);
  161. });
  162. it("没有留白的图:tailStub 缺席、fills 为空、轨道段不带 dashed", () => {
  163. const model = buildWorkflowTimeline(
  164. { ...OPEN, phases: OPEN.phases!.slice(0, 1), phaseEdges: [], holes: [] },
  165. undefined,
  166. );
  167. expect(model.tailStub).toBeUndefined();
  168. expect(model.fills).toEqual([]);
  169. });
  170. });
  171. describe("留白站(活)", () => {
  172. it("run 停在留白:hole.state waiting、站算作当前站(进入它的段行进、runningIndex 落在它上)", () => {
  173. const model = buildWorkflowTimeline(
  174. OPEN,
  175. run({
  176. phases: [{ name: "探索", rounds: 1 }],
  177. currentPhase: "探索",
  178. nodes: [
  179. { siteId: "ask#1", ordinal: 1, phase: "settled", outcome: "ok", phaseName: "探索" },
  180. ],
  181. holes: [
  182. { siteId: H1, ordinal: 1, name: "决定分组", type: "Plan", state: "waiting", since: 5 },
  183. ],
  184. }),
  185. );
  186. const hole = model.stations[1]!;
  187. expect(hole.hole).toEqual({ siteId: H1, state: "waiting", since: 5 });
  188. expect(hole.status).toBe("running");
  189. expect(hole.visited).toBe(true);
  190. expect(model.runningIndex).toBe(1);
  191. expect(model.rails[0]!.ink).toBe("march");
  192. // 还没走到的尾巴留白照旧是 open。
  193. expect(model.stations[3]!.hole?.state).toBe("open");
  194. expect(model.stations[3]!.status).toBe("pending");
  195. });
  196. it("等待中的站带上 run.holes 记的提示语(侧板的等待体画它);补全后不带", () => {
  197. const waiting = buildWorkflowTimeline(
  198. OPEN,
  199. run({
  200. holes: [
  201. {
  202. siteId: H1,
  203. ordinal: 1,
  204. name: "决定分组",
  205. type: "Plan",
  206. prompt: "侦察结果如下,请补全。",
  207. state: "waiting",
  208. },
  209. ],
  210. }),
  211. );
  212. expect(waiting.stations[1]!.hole?.prompt).toBe("侦察结果如下,请补全。");
  213. const filled = buildWorkflowTimeline(
  214. TAIL_FILLED,
  215. run({
  216. holes: [
  217. { siteId: H2, ordinal: 1, name: "评判", type: "Verdict", prompt: "p", state: "filled" },
  218. ],
  219. }),
  220. );
  221. expect(filled.stations[4]!.hole?.prompt).toBeUndefined();
  222. });
  223. });
  224. describe("补全后的时间线", () => {
  225. const filledRun = run({
  226. holes: [
  227. {
  228. siteId: H1,
  229. ordinal: 1,
  230. name: "决定分组",
  231. type: "Plan",
  232. state: "filled",
  233. filledAt: 99,
  234. filledBy: "main",
  235. },
  236. ],
  237. phases: [
  238. { name: "探索", rounds: 1 },
  239. { name: "冒烟测试", rounds: 1 },
  240. ],
  241. currentPhase: "冒烟测试",
  242. });
  243. it("有阶段的补全:体的两站站在留白的位置,头覆盖它们(from/to)、带名与类型;留白自己没有站", () => {
  244. const model = buildWorkflowTimeline(FILLED, filledRun);
  245. expect(model.stations.map((station) => station.id)).toEqual([
  246. "phase#1",
  247. `${H1}/phase#1`,
  248. `${H1}/phase#2`,
  249. "phase#2",
  250. H2,
  251. ]);
  252. expect(model.fills).toEqual([{ holeId: H1, name: "决定分组", from: 1, to: 2, filledAt: 99 }]);
  253. // 体里的站是普通站:没有 hole 记号,进入它们的轨道段不是虚线。
  254. expect(model.stations[1]!.hole).toBeUndefined();
  255. expect(model.stations[1]!.fill).toBe(H1);
  256. expect(model.rails[0]!.dashed).toBeUndefined();
  257. expect(model.stations[1]!.status).toBe("running");
  258. });
  259. it("没有 run(确认窗)时头的名字取自 holeLabels,缺席则退回站点 id", () => {
  260. const labelled = buildWorkflowTimeline(
  261. FILLED,
  262. undefined,
  263. new Map([[H1, { name: "决定分组" }]]),
  264. );
  265. expect(labelled.fills[0]).toMatchObject({ name: "决定分组" });
  266. const bare = buildWorkflowTimeline(FILLED, undefined);
  267. expect(bare.fills[0]).toMatchObject({ holeId: H1, name: H1 });
  268. });
  269. it("无阶段的补全:站保留留白的名与徽标(state filled),不抬头;尾巴留白补全后 tailStub 变成 filled", () => {
  270. const model = buildWorkflowTimeline(
  271. TAIL_FILLED,
  272. run({
  273. holes: [
  274. { siteId: H1, ordinal: 1, name: "决定分组", type: "Plan", state: "filled", filledAt: 1 },
  275. { siteId: H2, ordinal: 1, name: "评判", type: "Verdict", state: "filled", filledAt: 2 },
  276. ],
  277. }),
  278. );
  279. const judge = model.stations[4]!;
  280. expect(judge.hole).toEqual({ siteId: H2, state: "filled", tail: true, filledAt: 2 });
  281. expect(judge.pills.map((pill) => pill.lane.id)).toEqual([`${H2}/actor#1`]);
  282. expect(model.fills.map((fill) => fill.holeId)).toEqual([H1]);
  283. expect(model.tailStub).toBe("filled");
  284. // 进入它的段不再是虚线:脚本的尾巴已经写下。
  285. expect(model.rails[3]!.dashed).toBeUndefined();
  286. });
  287. });
  288. describe("narrowTimelineToFill · 补全行的草稿阶段线", () => {
  289. it("只留补全的站与它们两侧的邻站,邻站 ghost;轨道段与头的下标随之重排", () => {
  290. const model = buildWorkflowTimeline(FILLED, undefined, new Map([[H1, { name: "决定分组" }]]));
  291. const narrowed = narrowTimelineToFill(model, H1);
  292. expect(narrowed).toBeDefined();
  293. expect(narrowed!.stations.map((station) => station.id)).toEqual([
  294. "phase#1",
  295. `${H1}/phase#1`,
  296. `${H1}/phase#2`,
  297. "phase#2",
  298. ]);
  299. expect(narrowed!.stations.map((station) => station.ghost)).toEqual([
  300. true,
  301. undefined,
  302. undefined,
  303. true,
  304. ]);
  305. expect(narrowed!.fills).toEqual([{ holeId: H1, name: "决定分组", from: 1, to: 2 }]);
  306. expect(narrowed!.rails.map((rail) => [rail.from, rail.to])).toEqual([
  307. [0, 1],
  308. [1, 2],
  309. [2, 3],
  310. ]);
  311. expect(narrowed!.arcs).toEqual([]);
  312. expect(narrowed!.tailStub).toBeUndefined();
  313. });
  314. it("无阶段的补全:窗口是留白自己的站加邻站;不认识的留白返回 undefined", () => {
  315. const model = buildWorkflowTimeline(TAIL_FILLED, undefined, new Map([[H2, { name: "评判" }]]));
  316. const narrowed = narrowTimelineToFill(model, H2);
  317. expect(narrowed!.stations.map((station) => station.id)).toEqual(["phase#2", H2]);
  318. expect(narrowed!.stations[1]!.hole?.state).toBe("filled");
  319. expect(narrowed!.tailStub).toBe("filled");
  320. expect(narrowTimelineToFill(model, H7)).toBeUndefined();
  321. });
  322. });
  323. /**
  324. * 嵌套(docs/dynamic-workflow/presentation.md「Holes on the timeline」):外层补全里再留白。id 不拼接,
  325. * 包含关系只在留白自己的阶段 / 站的 `fill` 上(H11 的阶段带 `fill: H1`)。
  326. * 1. 外层开着:H1 一站。
  327. * 2. 外层补全、内层开着:体是「冒烟测试 → H11(挑选)→ 分组」,内层是外层体里的留白站。
  328. * 3. 两层都补全:内层的阶段带 `fill: H11`,仍在外层的跨度里。
  329. */
  330. const INNER_OPEN_STEPS: Step[] = [
  331. { id: "ask#1", kind: "ask", label: "scout", lane: "actor#1", phase: "phase#1" },
  332. {
  333. id: `${H1}/ask#1`,
  334. kind: "ask",
  335. label: "smoke",
  336. lane: `${H1}/actor#1`,
  337. phase: `${H1}/phase#1`,
  338. fill: H1,
  339. },
  340. { id: H11, kind: "hole", label: "挑选", lane: "main", phase: H11, fill: H1 },
  341. {
  342. id: `${H1}/ask#2`,
  343. kind: "ask",
  344. label: "group",
  345. lane: `${H1}/actor#2`,
  346. phase: `${H1}/phase#2`,
  347. fill: H1,
  348. },
  349. { id: "ask#2", kind: "ask", label: "member", lane: "actor#2", phase: "phase#2" },
  350. { id: H2, kind: "hole", label: "评判", lane: "main", phase: H2 },
  351. ];
  352. const INNER_OPEN: WorkflowCausalityGraphData = {
  353. steps: INNER_OPEN_STEPS,
  354. lanes: FILLED.lanes,
  355. participants: participantsOf(INNER_OPEN_STEPS),
  356. handoffs: [],
  357. phases: [
  358. { id: "phase#1", name: "探索" },
  359. { id: H1, name: "决定分组" },
  360. { id: `${H1}/phase#1`, name: "冒烟测试", fill: H1 },
  361. { id: H11, name: "挑选", fill: H1 },
  362. { id: `${H1}/phase#2`, name: "分组", fill: H1 },
  363. { id: "phase#2", name: "执行" },
  364. { id: H2, name: "评判" },
  365. ],
  366. phaseEdges: [
  367. { from: "phase#1", to: H1 },
  368. { from: H1, to: `${H1}/phase#1` },
  369. { from: `${H1}/phase#1`, to: H11 },
  370. { from: H11, to: `${H1}/phase#2` },
  371. { from: `${H1}/phase#2`, to: "phase#2" },
  372. { from: "phase#2", to: H2 },
  373. ],
  374. holes: [
  375. { siteId: H11, name: "挑选", type: "Pick" },
  376. { siteId: H2, name: "评判", type: "Verdict", tail: true },
  377. ],
  378. };
  379. const BOTH_STEPS: Step[] = [
  380. ...INNER_OPEN_STEPS.filter((step) => step.kind !== "hole" || step.id === H2),
  381. {
  382. id: `${H11}/ask#1`,
  383. kind: "ask",
  384. label: "pick",
  385. lane: `${H11}/actor#1`,
  386. phase: `${H11}/phase#1`,
  387. fill: H11,
  388. },
  389. ];
  390. const BOTH: WorkflowCausalityGraphData = {
  391. ...INNER_OPEN,
  392. steps: BOTH_STEPS,
  393. lanes: [...FILLED.lanes, { id: `${H11}/actor#1`, name: "挑选员" }],
  394. participants: participantsOf(BOTH_STEPS),
  395. phases: [
  396. { id: "phase#1", name: "探索" },
  397. { id: H1, name: "决定分组" },
  398. { id: `${H1}/phase#1`, name: "冒烟测试", fill: H1 },
  399. { id: H11, name: "挑选", fill: H1 },
  400. { id: `${H11}/phase#1`, name: "挑出候选", fill: H11 },
  401. { id: `${H1}/phase#2`, name: "分组", fill: H1 },
  402. { id: "phase#2", name: "执行" },
  403. { id: H2, name: "评判" },
  404. ],
  405. phaseEdges: [
  406. { from: "phase#1", to: H1 },
  407. { from: H1, to: `${H1}/phase#1` },
  408. { from: `${H1}/phase#1`, to: H11 },
  409. { from: H11, to: `${H11}/phase#1` },
  410. { from: `${H11}/phase#1`, to: `${H1}/phase#2` },
  411. { from: `${H1}/phase#2`, to: "phase#2" },
  412. { from: "phase#2", to: H2 },
  413. ],
  414. holes: [{ siteId: H2, name: "评判", type: "Verdict", tail: true }],
  415. };
  416. const OUTER_FILLED = {
  417. siteId: H1,
  418. ordinal: 1,
  419. name: "决定分组",
  420. type: "Plan",
  421. state: "filled" as const,
  422. filledAt: 10,
  423. };
  424. describe("嵌套的留白", () => {
  425. it("父表从留白自己的阶段与站读出;fillContains 沿父表向外走,不看 id 的形状拼接", () => {
  426. const parents = holeParentsOf(BOTH, BOTH.phases!);
  427. expect(parents).toEqual({ [H11]: H1 });
  428. expect(holeParentsOf(INNER_OPEN, INNER_OPEN.phases!)).toEqual({ [H11]: H1 });
  429. expect(fillContains(H1, H1, parents)).toBe(true);
  430. expect(fillContains(H1, H11, parents)).toBe(true);
  431. expect(fillContains(H11, H1, parents)).toBe(false);
  432. expect(fillContains(H1, H10, parents)).toBe(false);
  433. // 普通阶段 id 不是留白:不在任何补全里。
  434. expect(fillContains(H1, `${H1}/phase#2`, parents)).toBe(false);
  435. // 环(坏载荷)不会让它转圈。
  436. expect(fillContains(H2, H1, { [H1]: H11, [H11]: H1 })).toBe(false);
  437. });
  438. it("头已代表的留白阶段不成站,进它与出它的边接起来;开着的、有成员的、体里没有站的留白都留着", () => {
  439. const hidden = withoutHeadedHolePhases(BOTH);
  440. expect(hidden.phases!.map((phase) => phase.id)).toEqual([
  441. "phase#1",
  442. `${H1}/phase#1`,
  443. `${H11}/phase#1`,
  444. `${H1}/phase#2`,
  445. "phase#2",
  446. H2,
  447. ]);
  448. // 边的次序不是契约:按集合比。
  449. const edgeKeys = (edges: readonly { from: string; to: string }[] | undefined) =>
  450. (edges ?? []).map((edge) => `${edge.from} > ${edge.to}`).sort();
  451. expect(edgeKeys(hidden.phaseEdges)).toEqual(
  452. edgeKeys([
  453. { from: "phase#1", to: `${H1}/phase#1` },
  454. { from: `${H1}/phase#1`, to: `${H11}/phase#1` },
  455. { from: `${H11}/phase#1`, to: `${H1}/phase#2` },
  456. { from: `${H1}/phase#2`, to: "phase#2" },
  457. { from: "phase#2", to: H2 },
  458. ]),
  459. );
  460. // 开着的内层(INNER_OPEN 的 H11)留着;外层 H1 被头代表,拿掉。
  461. expect(withoutHeadedHolePhases(INNER_OPEN).phases!.map((phase) => phase.id)).toEqual([
  462. "phase#1",
  463. `${H1}/phase#1`,
  464. H11,
  465. `${H1}/phase#2`,
  466. "phase#2",
  467. H2,
  468. ]);
  469. // 无阶段的补全(H2 自己的阶段装着评委的 ask)留着;同一张图里被头代表的 H1 照样拿掉。
  470. expect(withoutHeadedHolePhases(TAIL_FILLED).phases!.map((phase) => phase.id)).toEqual([
  471. "phase#1",
  472. `${H1}/phase#1`,
  473. `${H1}/phase#2`,
  474. "phase#2",
  475. H2,
  476. ]);
  477. // 没有补全的图原样返回(同一个引用:调用方可以放心地每帧调用)。
  478. expect(withoutHeadedHolePhases(OPEN)).toBe(OPEN);
  479. });
  480. it(`状态 1 · 外层开着:只有 ${H1} 一站,没有头`, () => {
  481. const model = buildWorkflowTimeline(OPEN, undefined);
  482. expect(model.stations.map((station) => station.hole?.siteId)).toEqual([
  483. undefined,
  484. H1,
  485. undefined,
  486. H2,
  487. ]);
  488. expect(model.fills).toEqual([]);
  489. });
  490. it("状态 2 · 外层补全、内层开着:内层是外层体里的普通留白站,外层的头盖住含它的三站;run 停在内层时 waiting", () => {
  491. const live = run({
  492. holes: [
  493. OUTER_FILLED,
  494. { siteId: H11, ordinal: 1, name: "挑选", type: "Pick", state: "waiting", since: 20 },
  495. ],
  496. });
  497. const model = buildWorkflowTimeline(INNER_OPEN, live);
  498. expect(model.stations.map((station) => station.id)).toEqual([
  499. "phase#1",
  500. `${H1}/phase#1`,
  501. H11,
  502. `${H1}/phase#2`,
  503. "phase#2",
  504. H2,
  505. ]);
  506. const inner = model.stations[2]!;
  507. expect(inner.hole).toEqual({ siteId: H11, state: "waiting", since: 20 });
  508. expect(inner.fill).toBe(H1);
  509. expect(inner.pills).toEqual([]);
  510. expect(model.runningIndex).toBe(2);
  511. expect(model.rails.filter((rail) => rail.dashed).map((rail) => [rail.from, rail.to])).toEqual([
  512. [1, 2],
  513. [2, 3],
  514. [4, 5],
  515. ]);
  516. expect(model.fills).toEqual([{ holeId: H1, name: "决定分组", from: 1, to: 3, filledAt: 10 }]);
  517. // 没有 run 的静态图(确认窗):内层是开着的留白、外层的头名字取自 holeLabels。
  518. const stat = buildWorkflowTimeline(
  519. INNER_OPEN,
  520. undefined,
  521. new Map([[H1, { name: "决定分组" }]]),
  522. );
  523. expect(stat.stations[2]!.hole?.state).toBe("open");
  524. expect(stat.fills.map((fill) => fill.holeId)).toEqual([H1]);
  525. });
  526. it("状态 3 · 两层都补全:内层的头在外层的跨度里,按 from 升序、同起点外层在前;窄化到内层只留它与两侧邻站", () => {
  527. const live = run({
  528. holes: [
  529. OUTER_FILLED,
  530. { siteId: H11, ordinal: 1, name: "挑选", type: "Pick", state: "filled", filledAt: 30 },
  531. ],
  532. });
  533. const model = buildWorkflowTimeline(BOTH, live);
  534. expect(model.stations[2]!.hole).toBeUndefined();
  535. expect(model.stations[2]!.fill).toBe(H11);
  536. expect(model.fills).toEqual([
  537. { holeId: H1, name: "决定分组", from: 1, to: 3, filledAt: 10 },
  538. { holeId: H11, name: "挑选", from: 2, to: 2, filledAt: 30 },
  539. ]);
  540. const narrowed = narrowTimelineToFill(model, H11)!;
  541. expect(narrowed.stations.map((station) => [station.id, station.ghost ?? false])).toEqual([
  542. [`${H1}/phase#1`, true],
  543. [`${H11}/phase#1`, false],
  544. [`${H1}/phase#2`, true],
  545. ]);
  546. expect(narrowed.fills).toEqual([{ holeId: H11, name: "挑选", from: 1, to: 1, filledAt: 30 }]);
  547. // 外层的体只有内层留白时,外层不被任何普通阶段点名,也算补全过:沿父表(H11 的阶段带
  548. // `fill: H1`)读出。两个留白自己的阶段都没有成员、都被头代表,不成站。
  549. const onlyInner: WorkflowCausalityGraphData = {
  550. ...BOTH,
  551. steps: BOTH_STEPS.filter((step) => step.fill !== H1),
  552. participants: participantsOf(BOTH_STEPS.filter((step) => step.fill !== H1)),
  553. phases: BOTH.phases!.filter((phase) => phase.fill !== H1 || phase.id === H11),
  554. phaseEdges: [
  555. { from: "phase#1", to: H1 },
  556. { from: H1, to: H11 },
  557. { from: H11, to: `${H11}/phase#1` },
  558. { from: `${H11}/phase#1`, to: "phase#2" },
  559. { from: "phase#2", to: H2 },
  560. ],
  561. };
  562. expect(
  563. buildWorkflowTimeline(onlyInner, undefined).fills.map((fill) => [
  564. fill.holeId,
  565. fill.from,
  566. fill.to,
  567. ]),
  568. ).toEqual([
  569. [H1, 1, 1],
  570. [H11, 1, 1],
  571. ]);
  572. });
  573. });
  574. // 修复原因(2026-09-29 testfield 实测):分析器把开着的留白记成 `main` 车道上的一步,放在**含 `hole()`
  575. // 调用的那个阶段**里,还为它发一个参与者(`phase#1:main`)。上面的夹具把留白放进它自己的阶段、又不给它
  576. // 参与者,所以没测到:真实载荷下时间线把 `main` 当成一个没名字的子代理,那一站多出一枚「未命名子代理」。
  577. // 夹具照荒岛奶茶店 run(dwfrun-4d94062b)CreateWorkflow 那份 display 原样裁出。
  578. describe("the main agent's lane on the timeline", () => {
  579. const REAL: WorkflowCausalityGraphData = {
  580. steps: [
  581. {
  582. id: "ask#1",
  583. kind: "ask",
  584. label: "ask",
  585. labelPattern: { head: "点子大王·" },
  586. lane: "actor#1",
  587. phase: "phase#1",
  588. repeat: "serial",
  589. },
  590. {
  591. id: "hole#be89c200",
  592. kind: "hole",
  593. label: "第二步:把脑洞变成菜单",
  594. lane: "main",
  595. phase: "phase#1",
  596. },
  597. ],
  598. lanes: [{ id: "main" }, { id: "actor#1", namePattern: { head: "点子大王·" } }],
  599. participants: [
  600. {
  601. id: "phase#1:actor#1[0]",
  602. phase: "phase#1",
  603. lane: "actor#1",
  604. steps: ["ask#1"],
  605. member: { index: 0, of: 3 },
  606. },
  607. {
  608. id: "phase#1:actor#1[1]",
  609. phase: "phase#1",
  610. lane: "actor#1",
  611. steps: ["ask#1"],
  612. member: { index: 1, of: 3 },
  613. },
  614. {
  615. id: "phase#1:actor#1[2]",
  616. phase: "phase#1",
  617. lane: "actor#1",
  618. steps: ["ask#1"],
  619. member: { index: 2, of: 3 },
  620. },
  621. { id: "phase#1:main", phase: "phase#1", lane: "main", steps: ["hole#be89c200"] },
  622. ],
  623. handoffs: [
  624. { from: "phase#1:actor#1[0]", to: "phase#1:main", types: ["Pitch"] },
  625. { from: "phase#1:actor#1[1]", to: "phase#1:main", types: ["Pitch"] },
  626. { from: "phase#1:actor#1[2]", to: "phase#1:main", types: ["Pitch"] },
  627. ],
  628. phases: [
  629. { id: "phase#1", name: "第一幕:三位点子大王各抛一个奶茶店脑洞" },
  630. { id: "hole#be89c200", name: "第二步:把脑洞变成菜单" },
  631. ],
  632. phaseEdges: [{ from: "phase#1", to: "hole#be89c200" }],
  633. exits: ["hole#be89c200"],
  634. sink: ["hole#be89c200"],
  635. holes: [
  636. {
  637. siteId: "hole#be89c200",
  638. name: "第二步:把脑洞变成菜单",
  639. type: "HandbookReport",
  640. phase: "phase#1",
  641. tail: true,
  642. },
  643. ],
  644. };
  645. it("draws no pill for the lane an open hole waits on", () => {
  646. const model = buildWorkflowTimeline(REAL, undefined);
  647. const lanes = model.stations.map((station) => station.pills.map((pill) => pill.lane.id));
  648. expect(lanes).toEqual([["actor#1", "actor#1", "actor#1"], []]);
  649. });
  650. it("leaves the main lane out of a live run's pills too", () => {
  651. const run = {
  652. runId: "dwfrun-1",
  653. toolCallId: "call-1",
  654. status: "running",
  655. phases: [{ name: "第一幕:三位点子大王各抛一个奶茶店脑洞", ordinal: 1 }],
  656. nodes: [],
  657. actors: [],
  658. } as unknown as WorkflowRunState;
  659. const pills = buildWorkflowTimeline(REAL, run).stations[0]!.pills;
  660. expect(pills.map((pill) => pill.lane.id)).toEqual(["actor#1", "actor#1", "actor#1"]);
  661. });
  662. it("does not count the main lane when numbering anonymous subagents", () => {
  663. // 唯一一条真匿名车道不该因为 `main` 在场就叫「未命名子代理 1」。
  664. const refs = laneRefsById([{ id: "main" }, { id: "actor#1" }]);
  665. expect(refs.get("actor#1")?.anonymousIndex).toBeUndefined();
  666. });
  667. });