conversation-session-subagent-provider-registry-cold-start.test.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. import { readFile } from "node:fs/promises";
  2. import { clearAppData } from "#e2e/helpers/desktop-app";
  3. import { assertUpstreamRequestCapture } from "#e2e/helpers/upstream-capture";
  4. import type {
  5. E2ENetworkCaptureArtifact,
  6. E2ENetworkCaptureRecord,
  7. } from "#e2e/helpers/network-capture-proxy";
  8. import { UPSTREAM_ALTERNATE_MODEL, UPSTREAM_MODEL } from "#e2e/helpers/upstream-provider";
  9. import {
  10. E2E_REPLY_TOKEN,
  11. prepareV4ConversationE2E,
  12. sendV4Prompt,
  13. waitForV4AssistantMessageContaining,
  14. waitForV4ConversationState,
  15. waitForV4ComposerText,
  16. waitForV4UserMessageContaining,
  17. } from "#e2e/helpers/v4-conversation";
  18. import { waitForToolCallBlockByToolName } from "#e2e/helpers/conversation-session-tool";
  19. const SUBAGENT_NAME = "e2e-provider-registry-reviewer";
  20. const PARENT_MARKER = "E2E_SUBAGENT_PROVIDER_REGISTRY_PARENT";
  21. const CHILD_MARKER = "E2E_SUBAGENT_PROVIDER_REGISTRY_CHILD";
  22. const CHILD_REPLY_TOKEN = "E2E_SUBAGENT_PROVIDER_REGISTRY_CHILD_OK";
  23. describe("会话区 subagent provider registry 冷启动 E2E", () => {
  24. after(async () => {
  25. await browser.electron.restoreAllMocks();
  26. await clearAppData();
  27. });
  28. it("I19: 冷启动时 subagent 绑定 alternate provider 也应能首轮执行", async function () {
  29. this.timeout(180000);
  30. await prepareV4ConversationE2E();
  31. const marker = `${PARENT_MARKER}_${Date.now()}`;
  32. const prompt =
  33. `${marker}: Use the Agent tool with subagent_type "${SUBAGENT_NAME}". ` +
  34. `Ask it to reply with exactly "${CHILD_REPLY_TOKEN}". ` +
  35. `After the subagent returns, reply with exactly "${E2E_REPLY_TOKEN}" and no other text.`;
  36. await sendV4Prompt(prompt);
  37. await waitForV4ComposerText("", "subagent provider registry 冷启动首发后输入框没有清空");
  38. await waitForV4UserMessageContaining(marker);
  39. const childRecord = await waitForChildSubagentRequestCapture();
  40. expect(readModelFromCapture(childRecord.requestJson)).toBe(UPSTREAM_ALTERNATE_MODEL);
  41. const parentContinuationRecord = await waitForParentContinuationRequestCapture();
  42. assertUpstreamRequestCapture(parentContinuationRecord, {
  43. expectedText: CHILD_REPLY_TOKEN,
  44. model: UPSTREAM_MODEL,
  45. });
  46. await waitForV4AssistantMessageContaining(E2E_REPLY_TOKEN);
  47. await waitForV4ConversationState(
  48. (snapshot) => snapshot.state === "idle" && snapshot.queueCount === 0,
  49. "subagent provider registry 冷启动完成后没有回到 idle",
  50. 90000,
  51. );
  52. const agentBlock = await waitForToolCallBlockByToolName("Agent");
  53. expect(agentBlock.status).toBe("completed");
  54. });
  55. });
  56. async function waitForChildSubagentRequestCapture() {
  57. let latestArtifact: E2ENetworkCaptureArtifact | null = null;
  58. try {
  59. await browser.waitUntil(
  60. async () => {
  61. latestArtifact = await readCaptureArtifact();
  62. return Boolean(findChildSubagentRequest(latestArtifact));
  63. },
  64. {
  65. timeout: 30000,
  66. timeoutMsg: "没有捕获到 alternate provider 的 child subagent 请求",
  67. },
  68. );
  69. } catch (error) {
  70. throw new Error(
  71. `没有捕获到 alternate provider 的 child subagent 请求\n${JSON.stringify(
  72. summarizeChildRequestCandidates(latestArtifact),
  73. null,
  74. 2,
  75. )}`,
  76. { cause: error },
  77. );
  78. }
  79. const record = findChildSubagentRequest(latestArtifact);
  80. if (!record) {
  81. throw new Error("child subagent 请求在等待完成后仍不存在");
  82. }
  83. return record;
  84. }
  85. async function waitForParentContinuationRequestCapture() {
  86. let latestArtifact: E2ENetworkCaptureArtifact | null = null;
  87. try {
  88. await browser.waitUntil(
  89. async () => {
  90. latestArtifact = await readCaptureArtifact();
  91. return Boolean(findParentContinuationRequest(latestArtifact));
  92. },
  93. {
  94. timeout: 30000,
  95. timeoutMsg: "没有捕获到 primary provider 的 parent continuation 请求",
  96. },
  97. );
  98. } catch (error) {
  99. throw new Error(
  100. `没有捕获到 primary provider 的 parent continuation 请求\n${JSON.stringify(
  101. summarizeParentContinuationCandidates(latestArtifact),
  102. null,
  103. 2,
  104. )}`,
  105. { cause: error },
  106. );
  107. }
  108. const record = findParentContinuationRequest(latestArtifact);
  109. if (!record) {
  110. throw new Error("parent continuation 请求在等待完成后仍不存在");
  111. }
  112. return record;
  113. }
  114. async function readCaptureArtifact(): Promise<E2ENetworkCaptureArtifact | null> {
  115. const capturePath = process.env.E2E_PROVIDER_CAPTURE_PATH?.trim();
  116. if (!capturePath) {
  117. return null;
  118. }
  119. try {
  120. return JSON.parse(await readFile(capturePath, "utf-8")) as E2ENetworkCaptureArtifact;
  121. } catch (error) {
  122. if (error instanceof Error && "code" in error && error.code === "ENOENT") {
  123. return null;
  124. }
  125. throw error;
  126. }
  127. }
  128. function findChildSubagentRequest(artifact: E2ENetworkCaptureArtifact | null) {
  129. return (
  130. artifact?.records.find(
  131. (record) =>
  132. record.method === "POST" &&
  133. (record.path.includes("/messages") || record.path.includes("/chat/completions")) &&
  134. record.status === "complete" &&
  135. captureContainsText(record.requestJson, CHILD_MARKER) &&
  136. readModelFromCapture(record.requestJson) === UPSTREAM_ALTERNATE_MODEL,
  137. ) ?? null
  138. );
  139. }
  140. function findParentContinuationRequest(artifact: E2ENetworkCaptureArtifact | null) {
  141. return (
  142. artifact?.records.findLast(
  143. (record) =>
  144. isUpstreamMessageRequest(record) &&
  145. record.status === "complete" &&
  146. captureContainsText(record.requestJson, CHILD_REPLY_TOKEN) &&
  147. readModelFromCapture(record.requestJson) === UPSTREAM_MODEL,
  148. ) ?? null
  149. );
  150. }
  151. function summarizeChildRequestCandidates(artifact: E2ENetworkCaptureArtifact | null) {
  152. return {
  153. records:
  154. artifact?.records
  155. .filter((record) => captureContainsText(record.requestJson, CHILD_MARKER))
  156. .map((record) => ({
  157. model: readModelFromCapture(record.requestJson),
  158. path: record.path,
  159. status: record.status,
  160. statusCode: record.statusCode,
  161. })) ?? [],
  162. };
  163. }
  164. function summarizeParentContinuationCandidates(artifact: E2ENetworkCaptureArtifact | null) {
  165. return {
  166. records:
  167. artifact?.records
  168. .filter((record) => captureContainsText(record.requestJson, CHILD_REPLY_TOKEN))
  169. .map((record) => ({
  170. model: readModelFromCapture(record.requestJson),
  171. path: record.path,
  172. status: record.status,
  173. statusCode: record.statusCode,
  174. })) ?? [],
  175. };
  176. }
  177. function isUpstreamMessageRequest(record: E2ENetworkCaptureRecord) {
  178. return (
  179. record.method === "POST" &&
  180. (record.path.includes("/messages") || record.path.includes("/chat/completions"))
  181. );
  182. }
  183. function readModelFromCapture(value: unknown) {
  184. if (!value || typeof value !== "object" || Array.isArray(value)) {
  185. return null;
  186. }
  187. const model = (value as { model?: unknown }).model;
  188. return typeof model === "string" ? model : null;
  189. }
  190. function captureContainsText(value: unknown, expected: string): boolean {
  191. if (typeof value === "string") {
  192. return value.includes(expected);
  193. }
  194. if (Array.isArray(value)) {
  195. return value.some((item) => captureContainsText(item, expected));
  196. }
  197. if (!value || typeof value !== "object") {
  198. return false;
  199. }
  200. return Object.values(value).some((child) => captureContainsText(child, expected));
  201. }