tui-main-session-effects.test.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // WP-F 效果级回归:actor/子会话的原始事件绝不进主转写(user directive 2026-08-23)。
  2. //
  3. // 与 tui-main-session-filter.test.ts 的分工:那个文件钉谓词,这个文件钉**流水线的效果**
  4. // (闸门 → 去重 → applySessionEventToState)。有些回归只在效果层面看得见——尤其是
  5. // actor 的 turn_complete 会不会经 applyTurnCompleteFallbackResponse 把 actor 的答案
  6. // 追加成主转写里的一条 agent 消息。
  7. import assert from "node:assert/strict";
  8. import test from "node:test";
  9. import { SessionEventType, createSessionEvent, type SessionEvent } from "@zcode/contracts";
  10. import { getZCodeCopy } from "@zcode/i18n";
  11. import { applyMainSessionEvent } from "../src/app-session-event-handler.js";
  12. import type { Message } from "../src/app-model.js";
  13. import { EMPTY_TUI_WORKFLOW_MIRROR, type TuiWorkflowMirror } from "../src/app-workflow-mirror.js";
  14. const COPY = getZCodeCopy("en-US").tui;
  15. const MAIN = "session-main";
  16. const ACTOR = "session-actor-1";
  17. /**
  18. * 收集所有可观察副作用,用来断言「什么都没发生」。
  19. *
  20. * 接的是 **getter** 而不是 id:显式传 `undefined` 会重新触发默认参数替换,
  21. * 于是「主会话 id 未知」这个用例其实还在按 MAIN 过滤——踩过一次,这里钉住。
  22. */
  23. function harness(getMainSessionId: () => string | undefined = () => MAIN) {
  24. const state = {
  25. messages: [] as Message[],
  26. statuses: [] as string[],
  27. lastEvents: [] as string[],
  28. mirror: EMPTY_TUI_WORKFLOW_MIRROR as TuiWorkflowMirror,
  29. };
  30. const applied = new Set<string>();
  31. const input = {
  32. copy: COPY,
  33. getMainSessionId,
  34. setActiveTurnId: () => {},
  35. setCacheStats: () => {},
  36. setContextUsage: () => {},
  37. setLastError: () => {},
  38. setLastEvent: (value: string) => state.lastEvents.push(value),
  39. setLiveModelText: () => {},
  40. setMessages: (update: unknown) => {
  41. state.messages =
  42. typeof update === "function"
  43. ? (update as (current: Message[]) => Message[])(state.messages)
  44. : (update as Message[]);
  45. },
  46. setModel: () => {},
  47. setNetworkRequests: () => {},
  48. setStatus: (value: string) => state.statuses.push(value),
  49. setTodos: () => {},
  50. setUsage: () => {},
  51. setWorkflowMirror: (update: unknown) => {
  52. state.mirror =
  53. typeof update === "function"
  54. ? (update as (current: TuiWorkflowMirror) => TuiWorkflowMirror)(state.mirror)
  55. : (update as TuiWorkflowMirror);
  56. },
  57. assistantMessageIdsByToolCallId: new Map<string, string>(),
  58. toolNamesById: new Map<string, string>(),
  59. } as unknown as Parameters<typeof applyMainSessionEvent>[2];
  60. return {
  61. state,
  62. apply: (event: SessionEvent) => applyMainSessionEvent(event, applied, input),
  63. };
  64. }
  65. function event(sessionId: string, type: SessionEventType, payload: unknown = {}): SessionEvent {
  66. return { ...createSessionEvent(type, sessionId as never, payload), sessionId } as SessionEvent;
  67. }
  68. // ── 载荷最重的一条:actor 的 turn_complete 绝不经兜底写进主转写 ──
  69. test("an actor turn_complete never appends its answer through the fallback", () => {
  70. const h = harness();
  71. const applied = h.apply(
  72. event(ACTOR, SessionEventType.TurnComplete, { response: "actor internal answer" }),
  73. );
  74. assert.equal(applied, false);
  75. // 这条是整个 WP-F 里最要命的:兜底会把任意 turn_complete.response 追加成 agent 消息,
  76. // 所以没有闸门的话 actor 的中间答案会原文出现在主转写里。
  77. assert.deepEqual(h.state.messages, []);
  78. assert.deepEqual(h.state.statuses, []);
  79. });
  80. test("a main-session turn_complete still appends through the fallback", () => {
  81. const h = harness();
  82. assert.equal(
  83. h.apply(event(MAIN, SessionEventType.TurnComplete, { response: "the real answer" })),
  84. true,
  85. );
  86. assert.equal(h.state.messages.length, 1);
  87. assert.equal(h.state.messages[0]?.content, "the real answer");
  88. });
  89. // ── actor 的其余噪音:不产生任何转写 part、不改状态行 ──
  90. test("actor submit_result / tool_call_* create no transcript parts and no status changes", () => {
  91. const h = harness();
  92. const toolEvents: readonly [SessionEventType, unknown][] = [
  93. [
  94. SessionEventType.ToolCallScheduled,
  95. { toolCallId: "tc-a", toolName: "submit_result", input: {} },
  96. ],
  97. [SessionEventType.ToolCallStarted, { toolCallId: "tc-a", toolName: "submit_result" }],
  98. [
  99. SessionEventType.ToolCallResult,
  100. { toolCallId: "tc-a", toolName: "submit_result", result: { content: "ok" } },
  101. ],
  102. [SessionEventType.ToolCallError, { toolCallId: "tc-a", toolName: "submit_result" }],
  103. ];
  104. for (const [type, payload] of toolEvents) {
  105. assert.equal(h.apply(event(ACTOR, type, payload)), false, `${type} passed the gate`);
  106. }
  107. assert.deepEqual(h.state.messages, []);
  108. assert.deepEqual(h.state.statuses, []);
  109. assert.deepEqual(h.state.lastEvents, []);
  110. });
  111. test("actor model_streaming and assistant_message never reach the transcript", () => {
  112. const h = harness();
  113. h.apply(event(ACTOR, SessionEventType.ModelStreaming, { delta: "half a thought" }));
  114. h.apply(event(ACTOR, SessionEventType.AssistantMessage, { content: "actor prose" }));
  115. assert.deepEqual(h.state.messages, []);
  116. });
  117. test("actor turn lifecycle events do not touch the main status line", () => {
  118. const h = harness();
  119. h.apply(event(ACTOR, SessionEventType.TurnStarted, {}));
  120. h.apply(event(ACTOR, SessionEventType.ModelRequest, {}));
  121. assert.deepEqual(h.state.statuses, []);
  122. });
  123. // ── 没有过滤过头:三个必须活着的表面 ──
  124. test("dwf progress on the parent session still reduces into the mirror", () => {
  125. const h = harness();
  126. const applied = h.apply(
  127. event(MAIN, SessionEventType.DynamicWorkflowRunProgress, {
  128. runId: "dwfrun-1",
  129. toolCallId: "tc-1",
  130. sequence: 0,
  131. eventType: "run-started",
  132. payload: { caps: { maxConcurrency: 4 } },
  133. }),
  134. );
  135. assert.equal(applied, true);
  136. // 卡片的生命线:闸门收得太紧就会把工具卡打回死的文本投影。
  137. assert.equal(h.state.mirror.state.runs[0]?.runId, "dwfrun-1");
  138. assert.equal(h.state.mirror.state.runs[0]?.status, "running");
  139. });
  140. test("a mirrored subagent tool event still renders (it carries the parent sessionId)", () => {
  141. // mirrorSubagentToolEvent 用 createSessionEvent(type, parentSessionId, ...) 铸造,
  142. // 所以 Agent 工具的渲染不受 WP-F 影响。
  143. const h = harness();
  144. const applied = h.apply(
  145. event(MAIN, SessionEventType.ToolCallScheduled, {
  146. toolCallId: "tc-agent",
  147. toolName: "Agent",
  148. input: { description: "explore" },
  149. }),
  150. );
  151. assert.equal(applied, true);
  152. assert.equal(h.state.messages.length, 1);
  153. });
  154. test("the actor gate does not depend on event kind — only on session", () => {
  155. // 同一种事件,父会话过、子会话不过。闸门是会话级的,不是按类型拉黑名单。
  156. const h = harness();
  157. assert.equal(h.apply(event(MAIN, SessionEventType.AssistantMessage, { content: "main" })), true);
  158. assert.equal(
  159. h.apply(event(ACTOR, SessionEventType.AssistantMessage, { content: "actor" })),
  160. false,
  161. );
  162. assert.deepEqual(
  163. h.state.messages.map((message) => message.content),
  164. ["main"],
  165. );
  166. });
  167. // ── 闸门与去重的组合 ──
  168. test("the gate runs before dedup, so actor events never consume dedup slots", () => {
  169. const h = harness();
  170. const actorEvent = event(ACTOR, SessionEventType.AssistantMessage, { content: "noise" });
  171. h.apply(actorEvent);
  172. h.apply(actorEvent);
  173. // 主会话事件仍照常去重(第一条应用、第二条丢弃),证明窗口没被 actor 事件搅乱。
  174. const mainEvent = event(MAIN, SessionEventType.AssistantMessage, { content: "signal" });
  175. assert.equal(h.apply(mainEvent), true);
  176. assert.equal(h.apply(mainEvent), false);
  177. assert.deepEqual(
  178. h.state.messages.map((message) => message.content),
  179. ["signal"],
  180. );
  181. });
  182. test("an unknown main session id fails open so the transcript is never blank", () => {
  183. const h = harness(() => undefined);
  184. assert.equal(h.apply(event(ACTOR, SessionEventType.AssistantMessage, { content: "x" })), true);
  185. });