labels.test.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. import { describe, expect, it } from "vitest";
  2. import { type LabelEntry, SessionManager } from "../../src/core/session-manager.ts";
  3. describe("SessionManager labels", () => {
  4. it("sets and gets labels", () => {
  5. const session = SessionManager.inMemory();
  6. const msgId = session.appendMessage({ role: "user", content: "hello", timestamp: 1 });
  7. // No label initially
  8. expect(session.getLabel(msgId)).toBeUndefined();
  9. // Set a label
  10. const labelId = session.appendLabelChange(msgId, "checkpoint");
  11. expect(session.getLabel(msgId)).toBe("checkpoint");
  12. // Label entry should be in entries
  13. const entries = session.getEntries();
  14. const labelEntry = entries.find((e) => e.type === "label") as LabelEntry;
  15. expect(labelEntry).toBeDefined();
  16. expect(labelEntry.id).toBe(labelId);
  17. expect(labelEntry.targetId).toBe(msgId);
  18. expect(labelEntry.label).toBe("checkpoint");
  19. });
  20. it("clears labels with undefined", () => {
  21. const session = SessionManager.inMemory();
  22. const msgId = session.appendMessage({ role: "user", content: "hello", timestamp: 1 });
  23. session.appendLabelChange(msgId, "checkpoint");
  24. expect(session.getLabel(msgId)).toBe("checkpoint");
  25. // Clear the label
  26. session.appendLabelChange(msgId, undefined);
  27. expect(session.getLabel(msgId)).toBeUndefined();
  28. });
  29. it("last label wins", () => {
  30. const session = SessionManager.inMemory();
  31. const msgId = session.appendMessage({ role: "user", content: "hello", timestamp: 1 });
  32. session.appendLabelChange(msgId, "first");
  33. session.appendLabelChange(msgId, "second");
  34. const lastLabelId = session.appendLabelChange(msgId, "third");
  35. expect(session.getLabel(msgId)).toBe("third");
  36. const entries = session.getEntries();
  37. const lastLabelEntry = entries.find((e) => e.id === lastLabelId) as LabelEntry;
  38. const tree = session.getTree();
  39. const msgNode = tree.find((n) => n.entry.id === msgId);
  40. expect(msgNode?.labelTimestamp).toBe(lastLabelEntry.timestamp);
  41. });
  42. it("labels are included in tree nodes", () => {
  43. const session = SessionManager.inMemory();
  44. const msg1Id = session.appendMessage({ role: "user", content: "hello", timestamp: 1 });
  45. const msg2Id = session.appendMessage({
  46. role: "assistant",
  47. content: [{ type: "text", text: "hi" }],
  48. api: "anthropic-messages",
  49. provider: "anthropic",
  50. model: "test",
  51. usage: {
  52. input: 1,
  53. output: 1,
  54. cacheRead: 0,
  55. cacheWrite: 0,
  56. totalTokens: 2,
  57. cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
  58. },
  59. stopReason: "stop",
  60. timestamp: 2,
  61. });
  62. const msg1LabelId = session.appendLabelChange(msg1Id, "start");
  63. const msg2LabelId = session.appendLabelChange(msg2Id, "response");
  64. const entries = session.getEntries();
  65. const msg1LabelEntry = entries.find((e) => e.id === msg1LabelId) as LabelEntry;
  66. const msg2LabelEntry = entries.find((e) => e.id === msg2LabelId) as LabelEntry;
  67. const tree = session.getTree();
  68. // Find the message nodes (skip label entries)
  69. const msg1Node = tree.find((n) => n.entry.id === msg1Id);
  70. expect(msg1Node?.label).toBe("start");
  71. expect(msg1Node?.labelTimestamp).toBe(msg1LabelEntry.timestamp);
  72. // msg2 is a child of msg1
  73. const msg2Node = msg1Node?.children.find((n) => n.entry.id === msg2Id);
  74. expect(msg2Node?.label).toBe("response");
  75. expect(msg2Node?.labelTimestamp).toBe(msg2LabelEntry.timestamp);
  76. });
  77. it("labels are preserved in createBranchedSession", () => {
  78. const session = SessionManager.inMemory();
  79. const msg1Id = session.appendMessage({ role: "user", content: "hello", timestamp: 1 });
  80. const msg2Id = session.appendMessage({
  81. role: "assistant",
  82. content: [{ type: "text", text: "hi" }],
  83. api: "anthropic-messages",
  84. provider: "anthropic",
  85. model: "test",
  86. usage: {
  87. input: 1,
  88. output: 1,
  89. cacheRead: 0,
  90. cacheWrite: 0,
  91. totalTokens: 2,
  92. cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
  93. },
  94. stopReason: "stop",
  95. timestamp: 2,
  96. });
  97. const msg1LabelId = session.appendLabelChange(msg1Id, "important");
  98. const msg2LabelId = session.appendLabelChange(msg2Id, "also-important");
  99. const originalEntries = session.getEntries();
  100. const msg1LabelEntry = originalEntries.find((e) => e.id === msg1LabelId) as LabelEntry;
  101. const msg2LabelEntry = originalEntries.find((e) => e.id === msg2LabelId) as LabelEntry;
  102. // Branch from msg2 (in-memory mode returns null, but updates internal state)
  103. session.createBranchedSession(msg2Id);
  104. // Labels should be preserved
  105. expect(session.getLabel(msg1Id)).toBe("important");
  106. expect(session.getLabel(msg2Id)).toBe("also-important");
  107. // New label entries should exist
  108. const entries = session.getEntries();
  109. const labelEntries = entries.filter((e) => e.type === "label") as LabelEntry[];
  110. expect(labelEntries).toHaveLength(2);
  111. const tree = session.getTree();
  112. const msg1Node = tree.find((n) => n.entry.id === msg1Id);
  113. const msg2Node = msg1Node?.children.find((n) => n.entry.id === msg2Id);
  114. expect(msg1Node?.labelTimestamp).toBe(msg1LabelEntry.timestamp);
  115. expect(msg2Node?.labelTimestamp).toBe(msg2LabelEntry.timestamp);
  116. });
  117. it("labels not on path are not preserved in createBranchedSession", () => {
  118. const session = SessionManager.inMemory();
  119. const msg1Id = session.appendMessage({ role: "user", content: "hello", timestamp: 1 });
  120. const msg2Id = session.appendMessage({
  121. role: "assistant",
  122. content: [{ type: "text", text: "hi" }],
  123. api: "anthropic-messages",
  124. provider: "anthropic",
  125. model: "test",
  126. usage: {
  127. input: 1,
  128. output: 1,
  129. cacheRead: 0,
  130. cacheWrite: 0,
  131. totalTokens: 2,
  132. cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
  133. },
  134. stopReason: "stop",
  135. timestamp: 2,
  136. });
  137. const msg3Id = session.appendMessage({ role: "user", content: "followup", timestamp: 3 });
  138. // Label all messages
  139. session.appendLabelChange(msg1Id, "first");
  140. session.appendLabelChange(msg2Id, "second");
  141. session.appendLabelChange(msg3Id, "third");
  142. // Branch from msg2 (excludes msg3)
  143. session.createBranchedSession(msg2Id);
  144. // Only labels for msg1 and msg2 should be preserved
  145. expect(session.getLabel(msg1Id)).toBe("first");
  146. expect(session.getLabel(msg2Id)).toBe("second");
  147. expect(session.getLabel(msg3Id)).toBeUndefined();
  148. });
  149. it("labels are not included in buildSessionContext", () => {
  150. const session = SessionManager.inMemory();
  151. const msgId = session.appendMessage({ role: "user", content: "hello", timestamp: 1 });
  152. session.appendLabelChange(msgId, "checkpoint");
  153. const ctx = session.buildSessionContext();
  154. expect(ctx.messages).toHaveLength(1);
  155. expect(ctx.messages[0].role).toBe("user");
  156. });
  157. it("throws when labeling non-existent entry", () => {
  158. const session = SessionManager.inMemory();
  159. expect(() => session.appendLabelChange("non-existent", "label")).toThrow("Entry non-existent not found");
  160. });
  161. });