revert-compact.test.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. import { describe, expect, test, beforeEach, afterEach } from "bun:test"
  2. import path from "path"
  3. import { Session } from "../../src/session"
  4. import { SessionRevert } from "../../src/session/revert"
  5. import { SessionCompaction } from "../../src/session/compaction"
  6. import { MessageV2 } from "../../src/session/message-v2"
  7. import { Log } from "../../src/util/log"
  8. import { Instance } from "../../src/project/instance"
  9. import { Identifier } from "../../src/id/id"
  10. import { tmpdir } from "../fixture/fixture"
  11. const projectRoot = path.join(__dirname, "../..")
  12. Log.init({ print: false })
  13. describe("revert + compact workflow", () => {
  14. test("should properly handle compact command after revert", async () => {
  15. await using tmp = await tmpdir({ git: true })
  16. await Instance.provide({
  17. directory: tmp.path,
  18. fn: async () => {
  19. // Create a session
  20. const session = await Session.create({})
  21. const sessionID = session.id
  22. // Create a user message
  23. const userMsg1 = await Session.updateMessage({
  24. id: Identifier.ascending("message"),
  25. role: "user",
  26. sessionID,
  27. agent: "default",
  28. model: {
  29. providerID: "openai",
  30. modelID: "gpt-4",
  31. },
  32. time: {
  33. created: Date.now(),
  34. },
  35. })
  36. // Add a text part to the user message
  37. await Session.updatePart({
  38. id: Identifier.ascending("part"),
  39. messageID: userMsg1.id,
  40. sessionID,
  41. type: "text",
  42. text: "Hello, please help me",
  43. })
  44. // Create an assistant response message
  45. const assistantMsg1: MessageV2.Assistant = {
  46. id: Identifier.ascending("message"),
  47. role: "assistant",
  48. sessionID,
  49. mode: "default",
  50. agent: "default",
  51. path: {
  52. cwd: tmp.path,
  53. root: tmp.path,
  54. },
  55. cost: 0,
  56. tokens: {
  57. output: 0,
  58. input: 0,
  59. reasoning: 0,
  60. cache: { read: 0, write: 0 },
  61. },
  62. modelID: "gpt-4",
  63. providerID: "openai",
  64. parentID: userMsg1.id,
  65. time: {
  66. created: Date.now(),
  67. },
  68. finish: "end_turn",
  69. }
  70. await Session.updateMessage(assistantMsg1)
  71. // Add a text part to the assistant message
  72. await Session.updatePart({
  73. id: Identifier.ascending("part"),
  74. messageID: assistantMsg1.id,
  75. sessionID,
  76. type: "text",
  77. text: "Sure, I'll help you!",
  78. })
  79. // Create another user message
  80. const userMsg2 = await Session.updateMessage({
  81. id: Identifier.ascending("message"),
  82. role: "user",
  83. sessionID,
  84. agent: "default",
  85. model: {
  86. providerID: "openai",
  87. modelID: "gpt-4",
  88. },
  89. time: {
  90. created: Date.now(),
  91. },
  92. })
  93. await Session.updatePart({
  94. id: Identifier.ascending("part"),
  95. messageID: userMsg2.id,
  96. sessionID,
  97. type: "text",
  98. text: "What's the capital of France?",
  99. })
  100. // Create another assistant response
  101. const assistantMsg2: MessageV2.Assistant = {
  102. id: Identifier.ascending("message"),
  103. role: "assistant",
  104. sessionID,
  105. mode: "default",
  106. agent: "default",
  107. path: {
  108. cwd: tmp.path,
  109. root: tmp.path,
  110. },
  111. cost: 0,
  112. tokens: {
  113. output: 0,
  114. input: 0,
  115. reasoning: 0,
  116. cache: { read: 0, write: 0 },
  117. },
  118. modelID: "gpt-4",
  119. providerID: "openai",
  120. parentID: userMsg2.id,
  121. time: {
  122. created: Date.now(),
  123. },
  124. finish: "end_turn",
  125. }
  126. await Session.updateMessage(assistantMsg2)
  127. await Session.updatePart({
  128. id: Identifier.ascending("part"),
  129. messageID: assistantMsg2.id,
  130. sessionID,
  131. type: "text",
  132. text: "The capital of France is Paris.",
  133. })
  134. // Verify messages before revert
  135. let messages = await Session.messages({ sessionID })
  136. expect(messages.length).toBe(4) // 2 user + 2 assistant messages
  137. const messageIds = messages.map((m) => m.info.id)
  138. expect(messageIds).toContain(userMsg1.id)
  139. expect(messageIds).toContain(userMsg2.id)
  140. expect(messageIds).toContain(assistantMsg1.id)
  141. expect(messageIds).toContain(assistantMsg2.id)
  142. // Revert the last user message (userMsg2)
  143. await SessionRevert.revert({
  144. sessionID,
  145. messageID: userMsg2.id,
  146. })
  147. // Check that revert state is set
  148. let sessionInfo = await Session.get(sessionID)
  149. expect(sessionInfo.revert).toBeDefined()
  150. const revertMessageID = sessionInfo.revert?.messageID
  151. expect(revertMessageID).toBeDefined()
  152. // Messages should still be in the list (not removed yet, just marked for revert)
  153. messages = await Session.messages({ sessionID })
  154. expect(messages.length).toBe(4)
  155. // Now clean up the revert state (this is what the compact endpoint should do)
  156. await SessionRevert.cleanup(sessionInfo)
  157. // After cleanup, the reverted messages (those after the revert point) should be removed
  158. messages = await Session.messages({ sessionID })
  159. const remainingIds = messages.map((m) => m.info.id)
  160. // The revert point is somewhere in the message chain, so we should have fewer messages
  161. expect(messages.length).toBeLessThan(4)
  162. // userMsg2 and assistantMsg2 should be removed (they come after the revert point)
  163. expect(remainingIds).not.toContain(userMsg2.id)
  164. expect(remainingIds).not.toContain(assistantMsg2.id)
  165. // Revert state should be cleared
  166. sessionInfo = await Session.get(sessionID)
  167. expect(sessionInfo.revert).toBeUndefined()
  168. // Clean up
  169. await Session.remove(sessionID)
  170. },
  171. })
  172. })
  173. test("should properly clean up revert state before creating compaction message", async () => {
  174. await using tmp = await tmpdir({ git: true })
  175. await Instance.provide({
  176. directory: tmp.path,
  177. fn: async () => {
  178. // Create a session
  179. const session = await Session.create({})
  180. const sessionID = session.id
  181. // Create initial messages
  182. const userMsg = await Session.updateMessage({
  183. id: Identifier.ascending("message"),
  184. role: "user",
  185. sessionID,
  186. agent: "default",
  187. model: {
  188. providerID: "openai",
  189. modelID: "gpt-4",
  190. },
  191. time: {
  192. created: Date.now(),
  193. },
  194. })
  195. await Session.updatePart({
  196. id: Identifier.ascending("part"),
  197. messageID: userMsg.id,
  198. sessionID,
  199. type: "text",
  200. text: "Hello",
  201. })
  202. const assistantMsg: MessageV2.Assistant = {
  203. id: Identifier.ascending("message"),
  204. role: "assistant",
  205. sessionID,
  206. mode: "default",
  207. agent: "default",
  208. path: {
  209. cwd: tmp.path,
  210. root: tmp.path,
  211. },
  212. cost: 0,
  213. tokens: {
  214. output: 0,
  215. input: 0,
  216. reasoning: 0,
  217. cache: { read: 0, write: 0 },
  218. },
  219. modelID: "gpt-4",
  220. providerID: "openai",
  221. parentID: userMsg.id,
  222. time: {
  223. created: Date.now(),
  224. },
  225. finish: "end_turn",
  226. }
  227. await Session.updateMessage(assistantMsg)
  228. await Session.updatePart({
  229. id: Identifier.ascending("part"),
  230. messageID: assistantMsg.id,
  231. sessionID,
  232. type: "text",
  233. text: "Hi there!",
  234. })
  235. // Revert the user message
  236. await SessionRevert.revert({
  237. sessionID,
  238. messageID: userMsg.id,
  239. })
  240. // Check that revert state is set
  241. let sessionInfo = await Session.get(sessionID)
  242. expect(sessionInfo.revert).toBeDefined()
  243. // Simulate what the compact endpoint does: cleanup revert before creating compaction
  244. await SessionRevert.cleanup(sessionInfo)
  245. // Verify revert state is cleared
  246. sessionInfo = await Session.get(sessionID)
  247. expect(sessionInfo.revert).toBeUndefined()
  248. // Verify messages are properly cleaned up
  249. const messages = await Session.messages({ sessionID })
  250. expect(messages.length).toBe(0) // All messages should be reverted
  251. // Clean up
  252. await Session.remove(sessionID)
  253. },
  254. })
  255. })
  256. })