conversation-session-silent-reminder-timed.test.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // STR01–STR02 静默提醒按时间计时(docs/conversation-session-case-catalog.md「静默提醒(STR)」)。规格见
  2. // apps/zcode-cli/docs/design/v2/loop/meta-user-context.md「Silent-turn reminder」。
  3. // 断言读上游请求体:慢响应满 60 秒后的请求带一次提醒,冷恢复后按原位置原字节回放;快速连续的静默响应不带提醒。
  4. import { clearAppData, waitForDefaultWorkspaceReady } from "#e2e/helpers/desktop-app";
  5. import { getUpstreamRequestEvidence } from "#e2e/helpers/conversation-session-network";
  6. import { reloadElectronSessionSafely } from "#e2e/helpers/e2e-electron-reload";
  7. import {
  8. prepareV4ConversationE2E,
  9. selectV4TaskById,
  10. sendV4Prompt,
  11. waitForV4AssistantMessageContaining,
  12. waitForV4Pane,
  13. } from "#e2e/helpers/v4-conversation";
  14. type Request = { messages: unknown[]; system?: unknown };
  15. const REMINDER =
  16. "The user hasn't heard from you in a while. As you continue, keep them updated when there's something to tell — a finding, a change of plan.";
  17. const SLOW_MARKER = "E2E_SILENT_REMINDER_SLOW";
  18. const SLOW_DONE = "E2E_SILENT_REMINDER_SLOW_DONE";
  19. const COLD_MARKER = "E2E_SILENT_REMINDER_COLD_REPLAY";
  20. const COLD_DONE = "E2E_SILENT_REMINDER_COLD_OK";
  21. const FAST_MARKER = "E2E_SILENT_REMINDER_FAST";
  22. const FAST_DONE = "E2E_SILENT_REMINDER_FAST_DONE";
  23. describe("STR 静默提醒按时间计时", () => {
  24. after(async () => {
  25. await browser.electron.restoreAllMocks();
  26. await clearAppData();
  27. });
  28. it("STR01:两次 31 秒的静默响应后,满 60 秒的请求带一次提醒;冷恢复后原位置原字节回放", async function () {
  29. this.timeout(360000);
  30. await prepareV4ConversationE2E();
  31. const runId = Date.now();
  32. await sendV4Prompt(`${SLOW_MARKER}_${runId}: inspect quietly with the slow fixture.`);
  33. const completed = await waitForV4Pane(
  34. (snapshot) =>
  35. snapshot.sessionId !== null &&
  36. snapshot.sessionId !== "draft" &&
  37. !snapshot.canStop &&
  38. snapshot.timelineText.includes(SLOW_DONE),
  39. "慢响应的一轮没有完成",
  40. 180000,
  41. );
  42. const sessionId = completed.sessionId;
  43. if (!sessionId || sessionId === "draft") {
  44. throw new Error(`静默提醒 case 缺少真实 sessionId: ${sessionId ?? "null"}`);
  45. }
  46. // 第一次工具批次后约 31 秒、第二次之前不到 60 秒:没有提醒;第二次之后至少 62 秒:一次提醒。
  47. expect(reminderCount(await requestByFixture("silent-reminder-slow-1"))).toBe(0);
  48. expect(reminderCount(await requestByFixture("silent-reminder-slow-2"))).toBe(0);
  49. const live = await requestByFixture("silent-reminder-slow-3");
  50. expect(reminderCount(live)).toBe(1);
  51. // 完整重启 Electron、Host 与 CLI:提醒与它的位置来自持久化历史。
  52. await reloadElectronSessionSafely(browser);
  53. await waitForDefaultWorkspaceReady(60000);
  54. await selectV4TaskById(sessionId, 60000);
  55. await waitForV4Pane(
  56. (snapshot) => snapshot.sessionId === sessionId && !snapshot.canStop,
  57. `CLI cold restart 后没有恢复 session ${sessionId}`,
  58. 60000,
  59. );
  60. await sendV4Prompt(`${COLD_MARKER}_${runId}: reply with exactly ${COLD_DONE}.`);
  61. await waitForV4AssistantMessageContaining(COLD_DONE, 60000);
  62. const cold = await requestByFixture("silent-reminder-cold-followup");
  63. // 新一轮的首个请求不收集静默提醒;重启前的提醒原样在前缀里。
  64. expect(reminderCount(cold)).toBe(1);
  65. expect(withoutCacheControl(cold.messages.slice(0, live.messages.length))).toEqual(
  66. withoutCacheControl(live.messages),
  67. );
  68. });
  69. it("STR02:六次快速连续的静默响应不带提醒", async function () {
  70. this.timeout(240000);
  71. await prepareV4ConversationE2E();
  72. const runId = Date.now();
  73. await sendV4Prompt(`${FAST_MARKER}_${runId}: inspect quietly with the fast fixture.`);
  74. await waitForV4Pane(
  75. (snapshot) => !snapshot.canStop && snapshot.timelineText.includes(FAST_DONE),
  76. "快速静默响应的一轮没有完成",
  77. 120000,
  78. );
  79. for (let step = 1; step <= 7; step++) {
  80. expect(reminderCount(await requestByFixture(`silent-reminder-fast-${step}`))).toBe(0);
  81. }
  82. });
  83. });
  84. async function requestByFixture(fixtureId: string): Promise<Request> {
  85. const evidence = (await getUpstreamRequestEvidence({})).filter(
  86. (record) => record.fixtureId === fixtureId,
  87. );
  88. expect(evidence).toHaveLength(1);
  89. return evidence[0]!.requestJson as Request;
  90. }
  91. function reminderCount(request: Request): number {
  92. // MCS 开时提醒是 system 消息,关时是 user 侧 <system-reminder>;两种都在 messages 里。
  93. return JSON.stringify(request.messages).split(JSON.stringify(REMINDER).slice(1, -1)).length - 1;
  94. }
  95. function withoutCacheControl(value: unknown): unknown {
  96. if (Array.isArray(value)) return value.map(withoutCacheControl);
  97. if (!value || typeof value !== "object") return value;
  98. return Object.fromEntries(
  99. Object.entries(value)
  100. .filter(([key]) => key !== "cache_control")
  101. .map(([key, item]) => [key, withoutCacheControl(item)]),
  102. );
  103. }