conversation-session-dynamic-workflow-gray-always-on-overflow.test.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. import { releaseControlledReplay } from "#e2e/helpers/controlled-replay";
  2. import { clearAppData, pinE2ELocale } from "#e2e/helpers/desktop-app";
  3. import {
  4. approveV4Permission,
  5. prepareV4ConversationE2E,
  6. sendV4Prompt,
  7. waitForV4AssistantMessageContaining,
  8. waitForV4PermissionDialog,
  9. } from "#e2e/helpers/v4-conversation";
  10. import { getLatestUpstreamToolResultByToolCallId } from "#e2e/helpers/conversation-session-network";
  11. // 时间线的溢出与并行结构(docs/dynamic-workflow/presentation.md「Drawing the timeline」:「Rails and
  12. // arcs」「Stream rails」「Past six participants」「Overflow: the ledge」;「The timeline model」「The run
  13. // pane」「The spine」):
  14. // 六个阶段,宽过会话列。「铺开」九个子代理并行,其中六个是带类型的 ask、只回文字不提交,催交一轮后
  15. // 由 run 引擎判失败(脚本 catch 住,run 继续);「产出」「消化」是用 channel 相连的两段 future,
  16. // 流水线让两站成为一条流轨;「起草」「复核」循环两轮,复核 → 起草是一条回弧;最后「收尾」。
  17. //
  18. // 文件名带 `gray-always-on`:配置 mock 网关按文件名下发 alwaysOn,工作流工具才在工具集里。
  19. // case-local 回放只决定模型调用;编译、确认窗、run 引擎、子代理会话、流水线与完成通知走产品链路。
  20. // 两处运行态用受控流挂起:产出员的第二问与消化员的第一问同时挂着(两站都在跑,流在流动);
  21. // 复核员第二轮挂着(循环第二轮,镜头居中运行站)。断言做完再释放,不靠延时抢时序。
  22. const RUN_NAME = "E2E 检查流水线";
  23. const STATION_NAMES = ["铺开", "产出", "消化", "起草", "复核", "收尾"];
  24. const CHECKERS = ["一", "二", "三", "四", "五", "六", "七", "八", "九"].map((n) => `检查员${n}`);
  25. const FAILED_CHECKERS = CHECKERS.slice(3);
  26. let runId = "";
  27. interface TimelineSnapshot {
  28. digest: { x: number; y: number; width: number; height: number; expanded: string | null };
  29. runStatus: string | null;
  30. stations: Array<{ text: string; title: string; status: string; folded: boolean }>;
  31. rails: Array<{
  32. from: string;
  33. to: string;
  34. kind: string | null;
  35. ink: string | null;
  36. chevrons: string | null;
  37. flowing: number;
  38. }>;
  39. arcs: Array<{ from: string; to: string; ink: string | null; stream: boolean }>;
  40. roster: {
  41. flag: string | null;
  42. pills: Array<{ name: string; status: string }>;
  43. more: { count: string | null; text: string; failed: string | null; faces: number } | null;
  44. } | null;
  45. ledges: Record<
  46. "left" | "right",
  47. { lamps: Array<{ index: number; label: string }>; more: string | null } | null
  48. >;
  49. scroller: { scrollLeft: number; clientWidth: number; scrollWidth: number } | null;
  50. scrollbar: { trackWidth: number; thumbWidth: number } | null;
  51. }
  52. /** 一次读出这张 run 卡时间线上要断言的全部状态(失败时整份打进报错,便于对照截图)。 */
  53. async function readTimeline(id: string): Promise<TimelineSnapshot | null> {
  54. return browser.execute((runIdArg: string) => {
  55. const digest = document.querySelector<HTMLElement>(
  56. `[data-workflow-run-digest="true"][data-workflow-run-id="${runIdArg}"]`,
  57. );
  58. if (digest === null) return null;
  59. const all = (selector: string, root: ParentNode = digest) =>
  60. Array.from(root.querySelectorAll<HTMLElement>(selector));
  61. const rect = digest.getBoundingClientRect();
  62. const firstPills = digest.querySelector<HTMLElement>('[data-testid="workflow-timeline-pills"]');
  63. const moreRow = firstPills?.querySelector<HTMLElement>(
  64. '[data-testid="workflow-roster-more-row"]',
  65. );
  66. const ledge = (side: "left" | "right") => {
  67. const root = digest.querySelector<HTMLElement>(
  68. `[data-testid="workflow-timeline-ledge-${side}"]`,
  69. );
  70. if (root === null) return null;
  71. return {
  72. lamps: all('[data-testid="workflow-timeline-ledge-lamp"]', root).map((lamp) => ({
  73. index: Number(lamp.getAttribute("data-station-index")),
  74. label: lamp.getAttribute("aria-label") ?? "",
  75. })),
  76. more:
  77. root.querySelector('[data-testid="workflow-timeline-ledge-more"]')?.textContent ?? null,
  78. };
  79. };
  80. const scroller = digest.querySelector<HTMLElement>(
  81. '[data-testid="workflow-timeline-scroller"]',
  82. );
  83. const track = digest.querySelector<HTMLElement>('[data-testid="workflow-timeline-scrollbar"]');
  84. const thumb = track?.querySelector<HTMLElement>(
  85. '[data-testid="workflow-timeline-scrollbar-thumb"]',
  86. );
  87. return {
  88. digest: {
  89. x: rect.x,
  90. y: rect.y,
  91. width: rect.width,
  92. height: rect.height,
  93. expanded: digest.getAttribute("data-expanded"),
  94. },
  95. runStatus: digest.getAttribute("data-workflow-run-status"),
  96. stations: all('[data-testid="workflow-timeline-station"]').map((station) => ({
  97. text: station.textContent ?? "",
  98. title:
  99. station
  100. .querySelector('[data-testid="workflow-timeline-station-head"]')
  101. ?.getAttribute("title") ?? "",
  102. status: station.getAttribute("data-station-status") ?? "",
  103. folded: station.getAttribute("data-station-folded") === "true",
  104. })),
  105. rails: all('[data-testid="workflow-timeline-rail"]').map((rail) => {
  106. const chevrons = rail.querySelector('[data-testid="workflow-stream-chevrons"]');
  107. return {
  108. from: rail.getAttribute("data-rail-from") ?? "",
  109. to: rail.getAttribute("data-rail-to") ?? "",
  110. kind: rail.getAttribute("data-rail-kind"),
  111. ink: rail.getAttribute("data-rail-ink"),
  112. chevrons: chevrons?.getAttribute("data-stream-ink") ?? null,
  113. flowing: chevrons?.querySelectorAll(".wf-stream-flow").length ?? 0,
  114. };
  115. }),
  116. arcs: all('[data-testid="workflow-timeline-arc"]').map((arc) => ({
  117. from: arc.getAttribute("data-arc-from") ?? "",
  118. to: arc.getAttribute("data-arc-to") ?? "",
  119. ink: arc.getAttribute("data-arc-ink"),
  120. stream: arc.getAttribute("data-arc-stream") === "true",
  121. })),
  122. roster:
  123. firstPills === null || firstPills === undefined
  124. ? null
  125. : {
  126. flag: firstPills.getAttribute("data-station-roster"),
  127. pills: all('[data-testid="workflow-agent-pill"]', firstPills).map((pill) => ({
  128. name: (pill.textContent ?? "").trim(),
  129. status: pill.getAttribute("data-agent-status") ?? "",
  130. })),
  131. more:
  132. moreRow === null || moreRow === undefined
  133. ? null
  134. : {
  135. count: moreRow.getAttribute("data-more-count"),
  136. text: moreRow.textContent ?? "",
  137. failed:
  138. moreRow.querySelector('[data-testid="workflow-more-failed"]')
  139. ?.textContent ?? null,
  140. faces:
  141. moreRow.querySelector('[data-testid="workflow-more-deck"]')
  142. ?.childElementCount ?? 0,
  143. },
  144. },
  145. ledges: { left: ledge("left"), right: ledge("right") },
  146. scroller:
  147. scroller === null
  148. ? null
  149. : {
  150. scrollLeft: scroller.scrollLeft,
  151. clientWidth: scroller.clientWidth,
  152. scrollWidth: scroller.scrollWidth,
  153. },
  154. scrollbar:
  155. track === null || thumb === null || thumb === undefined
  156. ? null
  157. : {
  158. trackWidth: track.getBoundingClientRect().width,
  159. thumbWidth: thumb.getBoundingClientRect().width,
  160. },
  161. };
  162. }, id);
  163. }
  164. /** 轮询时间线直到满足条件;超时把最后一份快照写进日志。 */
  165. async function waitForTimeline(
  166. predicate: (snapshot: TimelineSnapshot) => boolean,
  167. message: string,
  168. timeout = 120000,
  169. ): Promise<TimelineSnapshot> {
  170. let last: TimelineSnapshot | null = null;
  171. try {
  172. await browser.waitUntil(
  173. async () => {
  174. last = await readTimeline(runId);
  175. return last !== null && predicate(last);
  176. },
  177. { timeout, interval: 250, timeoutMsg: message },
  178. );
  179. } catch (error) {
  180. // wdio 的超时错误在定时器里就被报告,换掉 message 也带不进报告:快照写进日志,原错误照抛。
  181. console.log(`${message}\n最后一次时间线快照:${JSON.stringify(last)}`);
  182. throw error;
  183. }
  184. return last!;
  185. }
  186. function railOf(snapshot: TimelineSnapshot, from: number, to: number) {
  187. return snapshot.rails.find((rail) => rail.from === String(from) && rail.to === String(to));
  188. }
  189. function arcOf(snapshot: TimelineSnapshot, from: number, to: number) {
  190. return snapshot.arcs.find((arc) => arc.from === String(from) && arc.to === String(to));
  191. }
  192. /** 檐上的每枚灯:标签是「站名 · 状态」,且对应的站在内容里是折叠的。 */
  193. function expectLedgeConsistent(snapshot: TimelineSnapshot, side: "left" | "right") {
  194. const ledge = snapshot.ledges[side]!;
  195. for (const lamp of ledge.lamps) {
  196. expect(lamp.label).toBe(
  197. `${STATION_NAMES[lamp.index]} · ${statusWord(snapshot.stations[lamp.index]!.status)}`,
  198. );
  199. expect(snapshot.stations[lamp.index]!.folded).toBe(true);
  200. }
  201. // 最多三枚灯,其余收进 `+n`(presentation.md「Overflow: the ledge」)。檐这一侧折叠的站:从这一侧的
  202. // 边缘数起、直到第一个开着的站。
  203. const ordered = side === "left" ? snapshot.stations : [...snapshot.stations].reverse();
  204. const open = ordered.findIndex((station) => !station.folded);
  205. const foldedOnSide = open === -1 ? ordered.length : open;
  206. expect(ledge.lamps.length).toBe(Math.min(3, foldedOnSide));
  207. if (foldedOnSide > 3) expect(ledge.more).toBe(`+${foldedOnSide - 3}`);
  208. else expect(ledge.more).toBeNull();
  209. }
  210. function statusWord(status: string): string {
  211. return (
  212. { done: "已完成", failed: "已失败", running: "运行中", pending: "待开始" }[status] ?? status
  213. );
  214. }
  215. describe("动态工作流 · 时间线溢出、名册与并行结构", () => {
  216. before(async () => {
  217. await prepareV4ConversationE2E();
  218. // 断言中文文案:固定界面语言,不跟随宿主系统语言(容器里默认英文)。
  219. await pinE2ELocale("zh-CN");
  220. });
  221. after(async () => {
  222. await browser.electron.restoreAllMocks();
  223. await clearAppData();
  224. });
  225. it("DWV-01 九人一站折成五枚钉住的失败药丸加「还有 4 个 ✕ 1」;产出与消化之间是流动的流轨;点「还有」打开侧板落到这一站,名单列全九人", async function () {
  226. this.timeout(300000);
  227. await sendV4Prompt("E2E_DWV_START 用工作流检查九项、流水线消化条目、起草复核两轮后定稿。");
  228. await waitForV4PermissionDialog();
  229. await approveV4Permission();
  230. await browser.waitUntil(
  231. async () =>
  232. (await getLatestUpstreamToolResultByToolCallId("toolu_dwv_create"))?.isError === false,
  233. { timeout: 60000, timeoutMsg: "CreateWorkflow 未执行成功" },
  234. );
  235. const toolResult = await getLatestUpstreamToolResultByToolCallId("toolu_dwv_create");
  236. const launched = /started in the background with ID: (\S+?)\./.exec(toolResult?.content ?? "");
  237. expect(launched).not.toBeNull();
  238. runId = launched![1]!;
  239. // 第一处挂起:产出员第二问与消化员第一问都挂着,两站都在跑。
  240. const live = await waitForTimeline(
  241. (snapshot) =>
  242. snapshot.stations.length === 6 &&
  243. snapshot.stations[1]?.status === "running" &&
  244. snapshot.stations[2]?.status === "running" &&
  245. railOf(snapshot, 1, 2)?.ink === "march" &&
  246. (snapshot.roster?.more?.failed ?? null) !== null,
  247. "产出 / 消化没有同时进入运行态,或「铺开」没有折成名册",
  248. );
  249. expect(live.runStatus).toBe("running");
  250. expect(
  251. await $(
  252. `[data-workflow-run-digest="true"][data-workflow-run-id="${runId}"] [data-testid="workflow-card-name"]`,
  253. ).getText(),
  254. ).toBe(RUN_NAME);
  255. expect(live.stations.map((station) => station.text)).toEqual(
  256. STATION_NAMES.map((name) => expect.stringContaining(name)),
  257. );
  258. // 名册(presentation.md「Past six participants」):settled 的一站先钉失败,再按参与者顺序;
  259. // 六人失败,五个钉住,第六个失败者与三个完成者藏在「还有 4 个」里,红色 ✕ 1 只数藏着的失败。
  260. expect(live.roster!.flag).toBe("true");
  261. expect(live.roster!.pills).toHaveLength(5);
  262. for (const pill of live.roster!.pills) {
  263. expect(pill.status).toBe("failed");
  264. expect(FAILED_CHECKERS).toContain(pill.name);
  265. }
  266. expect(live.roster!.more).toEqual({
  267. count: "4",
  268. text: expect.stringContaining("还有 4 个"),
  269. failed: "1",
  270. faces: 3,
  271. });
  272. // 站的灯按折叠规则:全部结算且有失败即 failed(脚本 catch 住不改变节点的结局)。
  273. expect(live.stations[0]!.status).toBe("failed");
  274. // 流轨(presentation.md「Stream rails」):产出 → 消化顶替普通轨道,两端都在跑时三枚 chevron
  275. // 用警示色依次亮起;回弧这时还没有任何一端被访问过,是 faint。
  276. const stream = railOf(live, 1, 2)!;
  277. expect(stream.kind).toBe("stream");
  278. expect(stream.chevrons).toBe("march");
  279. expect(stream.flowing).toBe(3);
  280. expect(railOf(live, 0, 1)?.kind).toBeNull();
  281. expect(arcOf(live, 4, 3)).toEqual({ from: "4", to: "3", ink: "faint", stream: false });
  282. // 点「还有 4 个」:开 run 侧板、落到「铺开」、门打开,名单只列没钉住的四人,按失败、完成分组。
  283. await $(
  284. `[data-workflow-run-digest="true"][data-workflow-run-id="${runId}"] [data-testid="workflow-roster-more-row"]`,
  285. ).click();
  286. const section = await $(
  287. '[data-testid="workflow-run-phases"] [data-testid="workflow-run-phase"][data-phase-open="true"]:has([data-testid="workflow-roster-more-row"][data-door="open"])',
  288. );
  289. await section.waitForExist({ timeout: 30000 });
  290. expect(await section.$('[data-testid="workflow-run-phase-toggle"]').getText()).toContain(
  291. "铺开",
  292. );
  293. const door = await section.$('[data-testid="workflow-roster-more-row"]');
  294. expect(await door.getAttribute("aria-expanded")).toBe("true");
  295. expect(await door.getAttribute("data-more-count")).toBe("4");
  296. const roll = await section.$('[data-testid="workflow-roster-roll"]');
  297. await roll.waitForExist({ timeout: 10000 });
  298. const pane = await browser.execute(() => {
  299. const root = document.querySelector(
  300. '[data-testid="workflow-run-phase"][data-phase-open="true"]:has([data-testid="workflow-roster-roll"])',
  301. );
  302. const names = (scope: string) =>
  303. Array.from(
  304. root?.querySelectorAll<HTMLElement>(`${scope} [data-testid="workflow-agent-pill"]`) ?? [],
  305. ).map((pill) => ({
  306. text: pill.textContent ?? "",
  307. status: pill.getAttribute("data-agent-status") ?? "",
  308. }));
  309. return {
  310. pins: names('[data-testid="workflow-roster-pins"]'),
  311. roll: names('[data-testid="workflow-roster-roll"]'),
  312. groups: Array.from(
  313. root?.querySelectorAll<HTMLElement>('[data-testid="workflow-roll-heading"]') ?? [],
  314. ).map((heading) => ({
  315. group: heading.getAttribute("data-roll-group"),
  316. // 人数与词分在两个 span 里(人数加粗),textContent 之间没有空格:去掉空白再比。
  317. text: (heading.textContent ?? "").replace(/\s+/g, ""),
  318. })),
  319. };
  320. });
  321. expect(pane.groups).toEqual([
  322. { group: "failed", text: "1个已失败" },
  323. { group: "done", text: "3个已完成" },
  324. ]);
  325. expect(pane.pins).toHaveLength(5);
  326. expect(pane.roll.map((pill) => pill.status)).toEqual(["failed", "done", "done", "done"]);
  327. // 钉住的五人加名单里的四人恰好是九位检查员,各出现一次。
  328. const listed = [...pane.pins, ...pane.roll].map(
  329. (pill) => CHECKERS.find((name) => pill.text.includes(name)) ?? pill.text,
  330. );
  331. expect([...listed].sort()).toEqual([...CHECKERS].sort());
  332. await releaseControlledReplay("dwv-produce-2");
  333. await releaseControlledReplay("dwv-digest-1");
  334. });
  335. it("DWV-02 循环第二轮:回弧变实、起草站写「⟳ 2」,镜头居中运行站、早先的站折上左檐并有滚动条;点檐上的灯滚回去,卡片原地不动", async function () {
  336. this.timeout(300000);
  337. // 第二处挂起:复核员第二轮挂着,正在运行的是「复核」(下标 4)。
  338. const held = await waitForTimeline(
  339. (snapshot) =>
  340. snapshot.stations[4]?.status === "running" &&
  341. snapshot.stations[3]?.title === "起草 · 已到访 2 次" &&
  342. arcOf(snapshot, 4, 3)?.ink === "strong" &&
  343. snapshot.ledges.left !== null &&
  344. snapshot.scroller !== null &&
  345. // 镜头滚动有动画:半路的快照里最靠内容的灯,落定后可能已折进「+n」,下面就点不到了。
  346. // 所以等镜头落到运行站(下标 4)的居中位置(presentation.md「Camera」)。
  347. Math.abs(
  348. snapshot.scroller.scrollLeft - (4 * 192 - (snapshot.scroller.clientWidth - 168) / 2),
  349. ) <= 1,
  350. "复核没有进入第二轮运行态,或镜头没有落到运行站、把早先的站折到左檐",
  351. );
  352. // 回弧(presentation.md「Rails and arcs」「Ink」):源站被访问、目标站已到第二轮即 strong;
  353. // 循环上的站写轮次,站头 title 说「已到访 2 次」。
  354. expect(arcOf(held, 4, 3)).toEqual({ from: "4", to: "3", ink: "strong", stream: false });
  355. expect(held.stations[3]!.title).toBe("起草 · 已到访 2 次");
  356. // 流轨两端都已结束:有条目过去过、此刻没有在过,是 strong,不再流动。
  357. expect(railOf(held, 1, 2)).toEqual(
  358. expect.objectContaining({ kind: "stream", ink: "strong", chevrons: "strong", flowing: 0 }),
  359. );
  360. // 檐(presentation.md「Overflow: the ledge」):灯滚出视口的站折到左檐,檐上的灯与折叠的站一一对应。
  361. const scroller = held.scroller!;
  362. expect(scroller.scrollWidth).toBeGreaterThan(scroller.clientWidth);
  363. expectLedgeConsistent(held, "left");
  364. expect(held.stations[4]!.folded).toBe(false);
  365. // 滚动条:拇指长 = 视口² / 内容,至少 24px。
  366. expect(held.scrollbar).not.toBeNull();
  367. const thumb = Math.max(24, scroller.clientWidth ** 2 / scroller.scrollWidth);
  368. expect(Math.abs(held.scrollbar!.thumbWidth - thumb)).toBeLessThanOrEqual(1.5);
  369. // 点檐上最靠内容的那枚灯:镜头把那一站带回视野。时间线自己滚,卡片的位置与大小不变、也不被折起。
  370. const target = held.ledges.left!.lamps.at(-1)!.index;
  371. // 镜头的落点:那一站居中(presentation.md「Camera」,`index × 192 − (clientWidth − 168) / 2`,夹在 0)。
  372. const landing = Math.max(0, target * 192 - (scroller.clientWidth - 168) / 2);
  373. await $(
  374. `[data-workflow-run-digest="true"][data-workflow-run-id="${runId}"] [data-testid="workflow-timeline-ledge-lamp"][data-station-index="${target}"]`,
  375. ).click();
  376. const moved = await waitForTimeline(
  377. (snapshot) =>
  378. snapshot.scroller !== null &&
  379. Math.abs(snapshot.scroller.scrollLeft - landing) <= 1 &&
  380. snapshot.stations[target]?.folded === false &&
  381. !(snapshot.ledges.left?.lamps ?? []).some((lamp) => lamp.index === target),
  382. `点檐上的灯后「${STATION_NAMES[target]}」没有滚回视野`,
  383. 30000,
  384. );
  385. expect(moved.digest).toEqual(held.digest);
  386. if (moved.ledges.left !== null) expectLedgeConsistent(moved, "left");
  387. if (moved.ledges.right !== null) expectLedgeConsistent(moved, "right");
  388. await releaseControlledReplay("dwv-review-2");
  389. await waitForTimeline((snapshot) => snapshot.runStatus === "completed", "run 没有到达完成态");
  390. await waitForV4AssistantMessageContaining("E2E_DWV_DONE", 120000);
  391. });
  392. });