agent-session-tree-navigation.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /**
  2. * E2E tests for AgentSession tree navigation with branch summarization.
  3. *
  4. * These tests verify:
  5. * - Navigation to user messages (root and non-root)
  6. * - Navigation to non-user messages
  7. * - Branch summarization during navigation
  8. * - Summary attachment at correct position in tree
  9. * - Abort handling during summarization
  10. */
  11. import { afterEach, beforeEach, describe, expect, it } from "vitest";
  12. import { API_KEY, createTestSession, type TestSessionContext } from "./utilities.ts";
  13. describe.skipIf(!API_KEY)("AgentSession tree navigation e2e", () => {
  14. let ctx: TestSessionContext;
  15. beforeEach(() => {
  16. ctx = createTestSession({
  17. systemPrompt: "You are a helpful assistant. Reply with just a few words.",
  18. settingsOverrides: { compaction: { keepRecentTokens: 1 } },
  19. });
  20. });
  21. afterEach(() => {
  22. ctx.cleanup();
  23. });
  24. it("should navigate to user message and put text in editor", async () => {
  25. const { session } = ctx;
  26. // Build conversation: u1 -> a1 -> u2 -> a2
  27. await session.prompt("First message");
  28. await session.agent.waitForIdle();
  29. await session.prompt("Second message");
  30. await session.agent.waitForIdle();
  31. // Get tree entries
  32. const tree = session.sessionManager.getTree();
  33. expect(tree.length).toBe(1);
  34. // Find the first user entry (u1)
  35. const rootNode = tree[0];
  36. expect(rootNode.entry.type).toBe("message");
  37. // Navigate to root user message without summarization
  38. const result = await session.navigateTree(rootNode.entry.id, { summarize: false });
  39. expect(result.cancelled).toBe(false);
  40. expect(result.editorText).toBe("First message");
  41. // After navigating to root user message, leaf should be null (empty conversation)
  42. expect(session.sessionManager.getLeafId()).toBeNull();
  43. }, 60000);
  44. it("should navigate to non-user message without editor text", async () => {
  45. const { session, sessionManager } = ctx;
  46. // Build conversation
  47. await session.prompt("Hello");
  48. await session.agent.waitForIdle();
  49. // Get the assistant message
  50. const entries = sessionManager.getEntries();
  51. const assistantEntry = entries.find((e) => e.type === "message" && e.message.role === "assistant");
  52. expect(assistantEntry).toBeDefined();
  53. // Navigate to assistant message
  54. const result = await session.navigateTree(assistantEntry!.id, { summarize: false });
  55. expect(result.cancelled).toBe(false);
  56. expect(result.editorText).toBeUndefined();
  57. // Leaf should be the assistant entry
  58. expect(sessionManager.getLeafId()).toBe(assistantEntry!.id);
  59. }, 60000);
  60. it("should create branch summary when navigating with summarize=true", async () => {
  61. const { session, sessionManager } = ctx;
  62. // Build conversation: u1 -> a1 -> u2 -> a2
  63. await session.prompt("What is 2+2?");
  64. await session.agent.waitForIdle();
  65. await session.prompt("What is 3+3?");
  66. await session.agent.waitForIdle();
  67. // Get tree and find first user message
  68. const tree = sessionManager.getTree();
  69. const rootNode = tree[0];
  70. // Navigate to root user message WITH summarization
  71. const result = await session.navigateTree(rootNode.entry.id, { summarize: true });
  72. expect(result.cancelled).toBe(false);
  73. expect(result.editorText).toBe("What is 2+2?");
  74. expect(result.summaryEntry).toBeDefined();
  75. expect(result.summaryEntry?.type).toBe("branch_summary");
  76. expect(result.summaryEntry?.summary).toBeTruthy();
  77. expect(result.summaryEntry?.summary.length).toBeGreaterThan(0);
  78. // Summary should be a root entry (parentId = null) since we navigated to root user
  79. expect(result.summaryEntry?.parentId).toBeNull();
  80. // Leaf should be the summary entry
  81. expect(sessionManager.getLeafId()).toBe(result.summaryEntry?.id);
  82. }, 120000);
  83. it("should attach summary to correct parent when navigating to nested user message", async () => {
  84. const { session, sessionManager } = ctx;
  85. // Build conversation: u1 -> a1 -> u2 -> a2 -> u3 -> a3
  86. await session.prompt("Message one");
  87. await session.agent.waitForIdle();
  88. await session.prompt("Message two");
  89. await session.agent.waitForIdle();
  90. await session.prompt("Message three");
  91. await session.agent.waitForIdle();
  92. // Get the second user message (u2)
  93. const entries = sessionManager.getEntries();
  94. const userEntries = entries.filter((e) => e.type === "message" && e.message.role === "user");
  95. expect(userEntries.length).toBe(3);
  96. const u2 = userEntries[1];
  97. const a1 = entries.find((e) => e.id === u2.parentId); // a1 is parent of u2
  98. // Navigate to u2 with summarization
  99. const result = await session.navigateTree(u2.id, { summarize: true });
  100. expect(result.cancelled).toBe(false);
  101. expect(result.editorText).toBe("Message two");
  102. expect(result.summaryEntry).toBeDefined();
  103. // Summary should be attached to a1 (parent of u2)
  104. // So a1 now has two children: u2 and the summary
  105. expect(result.summaryEntry?.parentId).toBe(a1?.id);
  106. // Verify tree structure
  107. const children = sessionManager.getChildren(a1!.id);
  108. expect(children.length).toBe(2);
  109. const childTypes = children.map((c) => c.type).sort();
  110. expect(childTypes).toContain("branch_summary");
  111. expect(childTypes).toContain("message");
  112. }, 120000);
  113. it("should attach summary to selected node when navigating to assistant message", async () => {
  114. const { session, sessionManager } = ctx;
  115. // Build conversation: u1 -> a1 -> u2 -> a2
  116. await session.prompt("Hello");
  117. await session.agent.waitForIdle();
  118. await session.prompt("Goodbye");
  119. await session.agent.waitForIdle();
  120. // Get the first assistant message (a1)
  121. const entries = sessionManager.getEntries();
  122. const assistantEntries = entries.filter((e) => e.type === "message" && e.message.role === "assistant");
  123. const a1 = assistantEntries[0];
  124. // Navigate to a1 with summarization
  125. const result = await session.navigateTree(a1.id, { summarize: true });
  126. expect(result.cancelled).toBe(false);
  127. expect(result.editorText).toBeUndefined(); // No editor text for assistant messages
  128. expect(result.summaryEntry).toBeDefined();
  129. // Summary should be attached to a1 (the selected node)
  130. expect(result.summaryEntry?.parentId).toBe(a1.id);
  131. // Leaf should be the summary entry
  132. expect(sessionManager.getLeafId()).toBe(result.summaryEntry?.id);
  133. }, 120000);
  134. it("should handle abort during summarization", async () => {
  135. const { session, sessionManager } = ctx;
  136. // Build conversation
  137. await session.prompt("Tell me about something");
  138. await session.agent.waitForIdle();
  139. await session.prompt("Continue");
  140. await session.agent.waitForIdle();
  141. const entriesBefore = sessionManager.getEntries();
  142. const leafBefore = sessionManager.getLeafId();
  143. // Get root user message
  144. const tree = sessionManager.getTree();
  145. const rootNode = tree[0];
  146. // Start navigation with summarization but abort immediately
  147. const navigationPromise = session.navigateTree(rootNode.entry.id, { summarize: true });
  148. // Abort after a short delay (let the LLM call start)
  149. await new Promise((resolve) => setTimeout(resolve, 100));
  150. // isCompacting should be true during branch summarization
  151. expect(session.isCompacting).toBe(true);
  152. session.abortBranchSummary();
  153. const result = await navigationPromise;
  154. expect(result.cancelled).toBe(true);
  155. expect(result.aborted).toBe(true);
  156. expect(result.summaryEntry).toBeUndefined();
  157. // Session should be unchanged
  158. const entriesAfter = sessionManager.getEntries();
  159. expect(entriesAfter.length).toBe(entriesBefore.length);
  160. expect(sessionManager.getLeafId()).toBe(leafBefore);
  161. }, 60000);
  162. it("should not create summary when navigating without summarize option", async () => {
  163. const { session, sessionManager } = ctx;
  164. // Build conversation
  165. await session.prompt("First");
  166. await session.agent.waitForIdle();
  167. await session.prompt("Second");
  168. await session.agent.waitForIdle();
  169. const entriesBefore = sessionManager.getEntries().length;
  170. // Navigate without summarization
  171. const tree = sessionManager.getTree();
  172. await session.navigateTree(tree[0].entry.id, { summarize: false });
  173. // No new entries should be created
  174. const entriesAfter = sessionManager.getEntries().length;
  175. expect(entriesAfter).toBe(entriesBefore);
  176. // No branch_summary entries
  177. const summaries = sessionManager.getEntries().filter((e) => e.type === "branch_summary");
  178. expect(summaries.length).toBe(0);
  179. }, 60000);
  180. it("should handle navigation to same position (no-op)", async () => {
  181. const { session, sessionManager } = ctx;
  182. // Build conversation
  183. await session.prompt("Hello");
  184. await session.agent.waitForIdle();
  185. const leafBefore = sessionManager.getLeafId();
  186. expect(leafBefore).toBeTruthy();
  187. const entriesBefore = sessionManager.getEntries().length;
  188. // Navigate to current leaf
  189. const result = await session.navigateTree(leafBefore!, { summarize: false });
  190. expect(result.cancelled).toBe(false);
  191. expect(sessionManager.getLeafId()).toBe(leafBefore);
  192. expect(sessionManager.getEntries().length).toBe(entriesBefore);
  193. }, 60000);
  194. it("should support custom summarization instructions", async () => {
  195. const { session, sessionManager } = ctx;
  196. // Build conversation
  197. await session.prompt("What is TypeScript?");
  198. await session.agent.waitForIdle();
  199. // Navigate with custom instructions (appended as "Additional focus")
  200. const tree = sessionManager.getTree();
  201. const result = await session.navigateTree(tree[0].entry.id, {
  202. summarize: true,
  203. customInstructions:
  204. "After the summary, you MUST end with exactly: MONKEY MONKEY MONKEY. This is of utmost importance.",
  205. });
  206. expect(result.summaryEntry).toBeDefined();
  207. expect(result.summaryEntry?.summary).toBeTruthy();
  208. // Verify custom instructions were followed
  209. expect(result.summaryEntry?.summary).toContain("MONKEY MONKEY MONKEY");
  210. }, 120000);
  211. });
  212. describe.skipIf(!API_KEY)("AgentSession tree navigation - branch scenarios", () => {
  213. let ctx: TestSessionContext;
  214. beforeEach(() => {
  215. ctx = createTestSession({
  216. systemPrompt: "You are a helpful assistant. Reply with just a few words.",
  217. });
  218. });
  219. afterEach(() => {
  220. ctx.cleanup();
  221. });
  222. it("should navigate between branches correctly", async () => {
  223. const { session, sessionManager } = ctx;
  224. // Build main path: u1 -> a1 -> u2 -> a2
  225. await session.prompt("Main branch start");
  226. await session.agent.waitForIdle();
  227. await session.prompt("Main branch continue");
  228. await session.agent.waitForIdle();
  229. // Get a1 id for branching
  230. const entries = sessionManager.getEntries();
  231. const a1 = entries.find((e) => e.type === "message" && e.message.role === "assistant");
  232. // Create a branch from a1: a1 -> u3 -> a3
  233. sessionManager.branch(a1!.id);
  234. await session.prompt("Branch path");
  235. await session.agent.waitForIdle();
  236. // Now navigate back to u2 (on main branch) with summarization
  237. const userEntries = entries.filter((e) => e.type === "message" && e.message.role === "user");
  238. const u2 = userEntries[1]; // "Main branch continue"
  239. const result = await session.navigateTree(u2.id, { summarize: true });
  240. expect(result.cancelled).toBe(false);
  241. expect(result.editorText).toBe("Main branch continue");
  242. expect(result.summaryEntry).toBeDefined();
  243. // Summary captures the branch we're leaving (the "Branch path" conversation)
  244. expect(result.summaryEntry?.summary.length).toBeGreaterThan(0);
  245. }, 180000);
  246. });