conversation-session-plugin-reference-append-only-resume.test.ts 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // PLG13 / PLG14 provider-wire 候选:
  2. // 1) 多轮会话内每次真实 provider `messages` 只能在尾部追加;
  3. // 2) 同进程切走再打开(热恢复)不得改写已经发送的历史;
  4. // 3) 保留 profile 完整重启 Desktop/Host/CLI(冷恢复)后仍不得重算历史 reminder。
  5. //
  6. // Bug 根因防线:只检查当前请求“还含有 plugin_reference”无法发现旧 reminder 被删除、
  7. // 重排或重建。这里逐项比较相邻请求的完整 messages 前缀,直接锁住 Prefix Cache 合同。
  8. import { DEFAULT_WORKSPACE, clearAppData } from "#e2e/helpers/desktop-app";
  9. import { startDraftFromProject } from "#e2e/helpers/project-sidebar";
  10. import {
  11. assertUpstreamRequestCapture,
  12. waitForUpstreamNetworkCapture,
  13. } from "#e2e/helpers/upstream-capture";
  14. import {
  15. restartIntoWorkspacePreservingProfile,
  16. seedPersistedModelSelection,
  17. seedReplayProvider,
  18. waitForSelectedModel,
  19. } from "#e2e/helpers/model-provider-restart";
  20. import {
  21. prepareV4ConversationE2E,
  22. selectV4TaskById,
  23. sendV4Prompt,
  24. switchV4Model,
  25. waitForV4AssistantMessageContaining,
  26. waitForV4Pane,
  27. waitForV4TimelineContaining,
  28. } from "#e2e/helpers/v4-conversation";
  29. const PROVIDER_ID = "e2e-plugin-reference-append-only";
  30. const PROVIDER_NAME = "Plugin Reference Append Only E2E";
  31. const MODEL_ID = "plugin-reference-append-only-model";
  32. const PLUGIN_STABLE_ID = "skill-creator@zcode-plugins-official";
  33. const U1_MARKER = "E2E_PLUGIN_APPEND_ONLY_U1";
  34. const U2_MARKER = "E2E_PLUGIN_APPEND_ONLY_U2";
  35. const U3_MARKER = "E2E_PLUGIN_APPEND_ONLY_U3_HOT";
  36. const U4_MARKER = "E2E_PLUGIN_APPEND_ONLY_U4_COLD";
  37. const PARKING_MARKER = "E2E_PLUGIN_APPEND_ONLY_PARKING";
  38. const A1_MARKER = "PLUGIN_APPEND_ONLY_A1";
  39. const A2_MARKER = "PLUGIN_APPEND_ONLY_A2";
  40. const A3_MARKER = "PLUGIN_APPEND_ONLY_A3_HOT";
  41. const A4_MARKER = "PLUGIN_APPEND_ONLY_A4_COLD";
  42. const PARKING_REPLY = "PLUGIN_APPEND_ONLY_PARKING_OK";
  43. const PLUGIN_REFERENCE_TAG = "<plugin_reference>";
  44. type ProviderMessage = unknown;
  45. describe("PLG13/PLG14 Plugin 引用多轮热/冷恢复 append-only", () => {
  46. after(async () => {
  47. await browser.electron.restoreAllMocks();
  48. await clearAppData();
  49. });
  50. it("真实 provider messages 在多轮、热恢复与冷恢复后保持严格前缀", async function () {
  51. this.timeout(360_000);
  52. await prepareV4ConversationE2E({ skipProvider: true });
  53. await seedReplayProvider({
  54. id: PROVIDER_ID,
  55. models: [MODEL_ID],
  56. name: PROVIDER_NAME,
  57. });
  58. await seedPersistedModelSelection({
  59. modelId: MODEL_ID,
  60. providerId: PROVIDER_ID,
  61. reasoningLevel: "disabled",
  62. });
  63. await restartIntoWorkspacePreservingProfile();
  64. await prepareV4ConversationE2E({ skipProvider: true });
  65. // 最近选择不决定已创建草稿的当前模型;前置通过真实选择器采用本例 Provider。
  66. await switchV4Model(PROVIDER_ID, MODEL_ID, "disabled");
  67. await waitForSelectedModel(PROVIDER_ID, MODEL_ID);
  68. const u1Prompt =
  69. `${U1_MARKER}: Use [@skill-creator](plugin://${PLUGIN_STABLE_ID}), ` +
  70. `then reply with exactly "${A1_MARKER}".`;
  71. const request1 = await sendAndCapture(u1Prompt, U1_MARKER, A1_MARKER);
  72. const messages1 = readProviderMessages(request1.requestJson, "Req1");
  73. const reminder1 = readSinglePluginReminder(messages1, "Req1");
  74. const targetSessionId = await readIdleBoundSession("U1 没有创建目标 session");
  75. const u2Prompt = `${U2_MARKER}: Reply with exactly "${A2_MARKER}".`;
  76. const request2 = await sendAndCapture(u2Prompt, U2_MARKER, A2_MARKER);
  77. const messages2 = readProviderMessages(request2.requestJson, "Req2");
  78. assertStrictMessagePrefix(messages1, messages2, "U1 → U2");
  79. expect(readSinglePluginReminder(messages2, "Req2")).toEqual(reminder1);
  80. // PLG13 热恢复:同一 Desktop/Host/CLI 进程内,先完成并停留在另一条 task,
  81. // 再重新打开目标 task。只切换 renderer 选中态,不创建新的 runtime 进程。
  82. // 全局新建会生成另一个 cwd,冷重启 helper 随后会切回原项目并自动打开目标任务。
  83. // parking 必须在同一项目中,才能覆盖同 Host/CLI 热切换和冷启动后的显式恢复。
  84. await startDraftFromProject(DEFAULT_WORKSPACE);
  85. await switchV4Model(PROVIDER_ID, MODEL_ID, "disabled");
  86. await sendV4Prompt(`${PARKING_MARKER}: Reply with exactly "${PARKING_REPLY}".`);
  87. await waitForV4AssistantMessageContaining(PARKING_REPLY, 60_000);
  88. const parkingSessionId = await readIdleBoundSession("parking session 没有完成");
  89. expect(parkingSessionId).not.toBe(targetSessionId);
  90. await selectV4TaskById(targetSessionId);
  91. await waitForV4TimelineContaining(U2_MARKER, 60_000);
  92. const u3Prompt = `${U3_MARKER}: Reply with exactly "${A3_MARKER}".`;
  93. const request3 = await sendAndCapture(u3Prompt, U3_MARKER, A3_MARKER);
  94. const messages3 = readProviderMessages(request3.requestJson, "Req3-hot");
  95. assertStrictMessagePrefix(messages2, messages3, "U2 → hot U3");
  96. expect(readSinglePluginReminder(messages3, "Req3-hot")).toEqual(reminder1);
  97. // PLG14 冷恢复:先切回 parking,避免目标 task 在记录前被自动打开;随后保留
  98. // profile 完整重启 Electron、Host 和 CLI,再由侧栏重新打开目标 task。
  99. await selectV4TaskById(parkingSessionId);
  100. await restartIntoWorkspacePreservingProfile();
  101. await waitForSelectedModel(PROVIDER_ID, MODEL_ID);
  102. await waitForV4Pane(
  103. (snapshot) => snapshot.sessionId !== targetSessionId,
  104. "冷启动后目标 session 在显式选择前已被自动打开",
  105. 60_000,
  106. );
  107. await selectV4TaskById(targetSessionId, 30_000);
  108. await waitForV4TimelineContaining(U3_MARKER, 60_000);
  109. const u4Prompt = `${U4_MARKER}: Reply with exactly "${A4_MARKER}".`;
  110. const request4 = await sendAndCapture(u4Prompt, U4_MARKER, A4_MARKER);
  111. const messages4 = readProviderMessages(request4.requestJson, "Req4-cold");
  112. assertStrictMessagePrefix(messages3, messages4, "hot U3 → cold U4");
  113. expect(readSinglePluginReminder(messages4, "Req4-cold")).toEqual(reminder1);
  114. // 历史 reminder 只允许在模型输入中存在;热/冷恢复都不能把它投影成用户气泡。
  115. const timelineHasReminder = await browser.execute(
  116. (tag) => (document.body.textContent ?? "").includes(tag),
  117. PLUGIN_REFERENCE_TAG,
  118. );
  119. expect(timelineHasReminder).toBe(false);
  120. });
  121. });
  122. async function sendAndCapture(prompt: string, marker: string, reply: string) {
  123. await sendV4Prompt(prompt);
  124. await waitForV4AssistantMessageContaining(reply, 60_000);
  125. await waitForV4Pane(
  126. (snapshot) =>
  127. Boolean(snapshot.sessionId && snapshot.sessionId !== "draft") && !snapshot.canStop,
  128. `${marker} 没有完成并回到 idle`,
  129. 90_000,
  130. );
  131. const request = await waitForUpstreamNetworkCapture(marker);
  132. assertUpstreamRequestCapture(request, { expectedText: prompt, model: MODEL_ID });
  133. return request;
  134. }
  135. async function readIdleBoundSession(timeoutMsg: string): Promise<string> {
  136. const snapshot = await waitForV4Pane(
  137. (current) => Boolean(current.sessionId && current.sessionId !== "draft") && !current.canStop,
  138. timeoutMsg,
  139. 90_000,
  140. );
  141. if (!snapshot.sessionId || snapshot.sessionId === "draft") {
  142. throw new Error(`${timeoutMsg}; sessionId=${snapshot.sessionId}`);
  143. }
  144. return snapshot.sessionId;
  145. }
  146. function readProviderMessages(requestJson: unknown, label: string): ProviderMessage[] {
  147. if (!requestJson || typeof requestJson !== "object" || Array.isArray(requestJson)) {
  148. throw new Error(`${label} provider request 不是 JSON object`);
  149. }
  150. const messages = (requestJson as Record<string, unknown>).messages;
  151. if (!Array.isArray(messages)) {
  152. throw new Error(`${label} provider request 缺少 messages 数组`);
  153. }
  154. return messages;
  155. }
  156. function assertStrictMessagePrefix(
  157. previous: readonly ProviderMessage[],
  158. next: readonly ProviderMessage[],
  159. label: string,
  160. ) {
  161. expect(next.length).toBeGreaterThan(previous.length);
  162. expect(next.slice(0, previous.length)).toEqual(previous);
  163. if (next.length <= previous.length) {
  164. throw new Error(
  165. `${label} 没有在历史尾部追加消息:previous=${previous.length}, next=${next.length}`,
  166. );
  167. }
  168. }
  169. function readSinglePluginReminder(
  170. messages: readonly ProviderMessage[],
  171. label: string,
  172. ): ProviderMessage {
  173. const reminders = messages.filter((message) =>
  174. JSON.stringify(message).includes(PLUGIN_REFERENCE_TAG),
  175. );
  176. expect(reminders).toHaveLength(1);
  177. if (reminders.length !== 1) {
  178. throw new Error(`${label} plugin_reference 数量不是 1:${reminders.length}`);
  179. }
  180. expect(JSON.stringify(reminders[0])).toContain(PLUGIN_STABLE_ID);
  181. return reminders[0];
  182. }