build-context.test.ts 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. type BranchSummaryEntry,
  4. buildSessionContext,
  5. type CompactionEntry,
  6. type ModelChangeEntry,
  7. type SessionEntry,
  8. type SessionMessageEntry,
  9. type ThinkingLevelChangeEntry,
  10. } from "../../src/core/session-manager.ts";
  11. function msg(id: string, parentId: string | null, role: "user" | "assistant", text: string): SessionMessageEntry {
  12. const base = { type: "message" as const, id, parentId, timestamp: "2025-01-01T00:00:00Z" };
  13. if (role === "user") {
  14. return { ...base, message: { role, content: text, timestamp: 1 } };
  15. }
  16. return {
  17. ...base,
  18. message: {
  19. role,
  20. content: [{ type: "text", text }],
  21. api: "anthropic-messages",
  22. provider: "anthropic",
  23. model: "claude-test",
  24. usage: {
  25. input: 1,
  26. output: 1,
  27. cacheRead: 0,
  28. cacheWrite: 0,
  29. totalTokens: 2,
  30. cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
  31. },
  32. stopReason: "stop",
  33. timestamp: 1,
  34. },
  35. };
  36. }
  37. function compaction(id: string, parentId: string | null, summary: string, firstKeptEntryId: string): CompactionEntry {
  38. return {
  39. type: "compaction",
  40. id,
  41. parentId,
  42. timestamp: "2025-01-01T00:00:00Z",
  43. summary,
  44. firstKeptEntryId,
  45. tokensBefore: 1000,
  46. };
  47. }
  48. function branchSummary(id: string, parentId: string | null, summary: string, fromId: string): BranchSummaryEntry {
  49. return { type: "branch_summary", id, parentId, timestamp: "2025-01-01T00:00:00Z", summary, fromId };
  50. }
  51. function thinkingLevel(id: string, parentId: string | null, level: string): ThinkingLevelChangeEntry {
  52. return { type: "thinking_level_change", id, parentId, timestamp: "2025-01-01T00:00:00Z", thinkingLevel: level };
  53. }
  54. function modelChange(id: string, parentId: string | null, provider: string, modelId: string): ModelChangeEntry {
  55. return { type: "model_change", id, parentId, timestamp: "2025-01-01T00:00:00Z", provider, modelId };
  56. }
  57. describe("buildSessionContext", () => {
  58. describe("trivial cases", () => {
  59. it("empty entries returns empty context", () => {
  60. const ctx = buildSessionContext([]);
  61. expect(ctx.messages).toEqual([]);
  62. expect(ctx.thinkingLevel).toBe("off");
  63. expect(ctx.model).toBeNull();
  64. });
  65. it("single user message", () => {
  66. const entries: SessionEntry[] = [msg("1", null, "user", "hello")];
  67. const ctx = buildSessionContext(entries);
  68. expect(ctx.messages).toHaveLength(1);
  69. expect(ctx.messages[0].role).toBe("user");
  70. });
  71. it("simple conversation", () => {
  72. const entries: SessionEntry[] = [
  73. msg("1", null, "user", "hello"),
  74. msg("2", "1", "assistant", "hi there"),
  75. msg("3", "2", "user", "how are you"),
  76. msg("4", "3", "assistant", "great"),
  77. ];
  78. const ctx = buildSessionContext(entries);
  79. expect(ctx.messages).toHaveLength(4);
  80. expect(ctx.messages.map((m) => m.role)).toEqual(["user", "assistant", "user", "assistant"]);
  81. });
  82. it("tracks thinking level changes", () => {
  83. const entries: SessionEntry[] = [
  84. msg("1", null, "user", "hello"),
  85. thinkingLevel("2", "1", "high"),
  86. msg("3", "2", "assistant", "thinking hard"),
  87. ];
  88. const ctx = buildSessionContext(entries);
  89. expect(ctx.thinkingLevel).toBe("high");
  90. expect(ctx.messages).toHaveLength(2);
  91. });
  92. it("tracks model from assistant message", () => {
  93. const entries: SessionEntry[] = [msg("1", null, "user", "hello"), msg("2", "1", "assistant", "hi")];
  94. const ctx = buildSessionContext(entries);
  95. expect(ctx.model).toEqual({ provider: "anthropic", modelId: "claude-test" });
  96. });
  97. it("tracks model from model change entry", () => {
  98. const entries: SessionEntry[] = [
  99. msg("1", null, "user", "hello"),
  100. modelChange("2", "1", "openai", "gpt-4"),
  101. msg("3", "2", "assistant", "hi"),
  102. ];
  103. const ctx = buildSessionContext(entries);
  104. // Assistant message overwrites model change
  105. expect(ctx.model).toEqual({ provider: "anthropic", modelId: "claude-test" });
  106. });
  107. });
  108. describe("with compaction", () => {
  109. it("includes summary before kept messages", () => {
  110. const entries: SessionEntry[] = [
  111. msg("1", null, "user", "first"),
  112. msg("2", "1", "assistant", "response1"),
  113. msg("3", "2", "user", "second"),
  114. msg("4", "3", "assistant", "response2"),
  115. compaction("5", "4", "Summary of first two turns", "3"),
  116. msg("6", "5", "user", "third"),
  117. msg("7", "6", "assistant", "response3"),
  118. ];
  119. const ctx = buildSessionContext(entries);
  120. // Should have: summary + kept (3,4) + after (6,7) = 5 messages
  121. expect(ctx.messages).toHaveLength(5);
  122. expect((ctx.messages[0] as any).summary).toContain("Summary of first two turns");
  123. expect((ctx.messages[1] as any).content).toBe("second");
  124. expect((ctx.messages[2] as any).content[0].text).toBe("response2");
  125. expect((ctx.messages[3] as any).content).toBe("third");
  126. expect((ctx.messages[4] as any).content[0].text).toBe("response3");
  127. });
  128. it("handles compaction keeping from first message", () => {
  129. const entries: SessionEntry[] = [
  130. msg("1", null, "user", "first"),
  131. msg("2", "1", "assistant", "response"),
  132. compaction("3", "2", "Empty summary", "1"),
  133. msg("4", "3", "user", "second"),
  134. ];
  135. const ctx = buildSessionContext(entries);
  136. // Summary + all messages (1,2,4)
  137. expect(ctx.messages).toHaveLength(4);
  138. expect((ctx.messages[0] as any).summary).toContain("Empty summary");
  139. });
  140. it("multiple compactions uses latest", () => {
  141. const entries: SessionEntry[] = [
  142. msg("1", null, "user", "a"),
  143. msg("2", "1", "assistant", "b"),
  144. compaction("3", "2", "First summary", "1"),
  145. msg("4", "3", "user", "c"),
  146. msg("5", "4", "assistant", "d"),
  147. compaction("6", "5", "Second summary", "4"),
  148. msg("7", "6", "user", "e"),
  149. ];
  150. const ctx = buildSessionContext(entries);
  151. // Should use second summary, keep from 4
  152. expect(ctx.messages).toHaveLength(4);
  153. expect((ctx.messages[0] as any).summary).toContain("Second summary");
  154. });
  155. });
  156. describe("with branches", () => {
  157. it("follows path to specified leaf", () => {
  158. // Tree:
  159. // 1 -> 2 -> 3 (branch A)
  160. // \-> 4 (branch B)
  161. const entries: SessionEntry[] = [
  162. msg("1", null, "user", "start"),
  163. msg("2", "1", "assistant", "response"),
  164. msg("3", "2", "user", "branch A"),
  165. msg("4", "2", "user", "branch B"),
  166. ];
  167. const ctxA = buildSessionContext(entries, "3");
  168. expect(ctxA.messages).toHaveLength(3);
  169. expect((ctxA.messages[2] as any).content).toBe("branch A");
  170. const ctxB = buildSessionContext(entries, "4");
  171. expect(ctxB.messages).toHaveLength(3);
  172. expect((ctxB.messages[2] as any).content).toBe("branch B");
  173. });
  174. it("includes branch summary in path", () => {
  175. const entries: SessionEntry[] = [
  176. msg("1", null, "user", "start"),
  177. msg("2", "1", "assistant", "response"),
  178. msg("3", "2", "user", "abandoned path"),
  179. branchSummary("4", "2", "Summary of abandoned work", "3"),
  180. msg("5", "4", "user", "new direction"),
  181. ];
  182. const ctx = buildSessionContext(entries, "5");
  183. expect(ctx.messages).toHaveLength(4);
  184. expect((ctx.messages[2] as any).summary).toContain("Summary of abandoned work");
  185. expect((ctx.messages[3] as any).content).toBe("new direction");
  186. });
  187. it("complex tree with multiple branches and compaction", () => {
  188. // Tree:
  189. // 1 -> 2 -> 3 -> 4 -> compaction(5) -> 6 -> 7 (main path)
  190. // \-> 8 -> 9 (abandoned branch)
  191. // \-> branchSummary(10) -> 11 (resumed from 3)
  192. const entries: SessionEntry[] = [
  193. msg("1", null, "user", "start"),
  194. msg("2", "1", "assistant", "r1"),
  195. msg("3", "2", "user", "q2"),
  196. msg("4", "3", "assistant", "r2"),
  197. compaction("5", "4", "Compacted history", "3"),
  198. msg("6", "5", "user", "q3"),
  199. msg("7", "6", "assistant", "r3"),
  200. // Abandoned branch from 3
  201. msg("8", "3", "user", "wrong path"),
  202. msg("9", "8", "assistant", "wrong response"),
  203. // Branch summary resuming from 3
  204. branchSummary("10", "3", "Tried wrong approach", "9"),
  205. msg("11", "10", "user", "better approach"),
  206. ];
  207. // Main path to 7: summary + kept(3,4) + after(6,7)
  208. const ctxMain = buildSessionContext(entries, "7");
  209. expect(ctxMain.messages).toHaveLength(5);
  210. expect((ctxMain.messages[0] as any).summary).toContain("Compacted history");
  211. expect((ctxMain.messages[1] as any).content).toBe("q2");
  212. expect((ctxMain.messages[2] as any).content[0].text).toBe("r2");
  213. expect((ctxMain.messages[3] as any).content).toBe("q3");
  214. expect((ctxMain.messages[4] as any).content[0].text).toBe("r3");
  215. // Branch path to 11: 1,2,3 + branch_summary + 11
  216. const ctxBranch = buildSessionContext(entries, "11");
  217. expect(ctxBranch.messages).toHaveLength(5);
  218. expect((ctxBranch.messages[0] as any).content).toBe("start");
  219. expect((ctxBranch.messages[1] as any).content[0].text).toBe("r1");
  220. expect((ctxBranch.messages[2] as any).content).toBe("q2");
  221. expect((ctxBranch.messages[3] as any).summary).toContain("Tried wrong approach");
  222. expect((ctxBranch.messages[4] as any).content).toBe("better approach");
  223. });
  224. });
  225. describe("edge cases", () => {
  226. it("uses last entry when leafId not found", () => {
  227. const entries: SessionEntry[] = [msg("1", null, "user", "hello"), msg("2", "1", "assistant", "hi")];
  228. const ctx = buildSessionContext(entries, "nonexistent");
  229. expect(ctx.messages).toHaveLength(2);
  230. });
  231. it("handles orphaned entries gracefully", () => {
  232. const entries: SessionEntry[] = [
  233. msg("1", null, "user", "hello"),
  234. msg("2", "missing", "assistant", "orphan"), // parent doesn't exist
  235. ];
  236. const ctx = buildSessionContext(entries, "2");
  237. // Should only get the orphan since parent chain is broken
  238. expect(ctx.messages).toHaveLength(1);
  239. });
  240. });
  241. });