session-replay.test.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. import { describe, expect, test } from "bun:test"
  2. import { replayLocalRows, replaySession } from "@/cli/cmd/run/session-replay"
  3. import type { SessionMessages } from "@/cli/cmd/run/session.shared"
  4. function userMessage(id: string, text: string): SessionMessages[number] {
  5. return {
  6. info: {
  7. id,
  8. sessionID: "session-1",
  9. role: "user",
  10. time: {
  11. created: 1,
  12. },
  13. agent: "build",
  14. model: {
  15. providerID: "openai",
  16. modelID: "gpt-5",
  17. },
  18. },
  19. parts: [
  20. {
  21. id: `${id}-text`,
  22. sessionID: "session-1",
  23. messageID: id,
  24. type: "text",
  25. text,
  26. },
  27. ],
  28. }
  29. }
  30. function assistantInfo(id: string) {
  31. return {
  32. id,
  33. sessionID: "session-1",
  34. role: "assistant" as const,
  35. time: {
  36. created: 2,
  37. },
  38. parentID: "msg-user-1",
  39. modelID: "gpt-5",
  40. providerID: "openai",
  41. mode: "chat",
  42. agent: "build",
  43. path: {
  44. cwd: "/tmp",
  45. root: "/tmp",
  46. },
  47. cost: 0,
  48. tokens: {
  49. input: 1,
  50. output: 1,
  51. reasoning: 0,
  52. cache: {
  53. read: 0,
  54. write: 0,
  55. },
  56. },
  57. }
  58. }
  59. function assistantMessage(id: string, text: string): SessionMessages[number] {
  60. return {
  61. info: assistantInfo(id),
  62. parts: [
  63. {
  64. id: `${id}-text`,
  65. sessionID: "session-1",
  66. messageID: id,
  67. type: "text",
  68. text,
  69. time: {
  70. start: 2,
  71. end: 3,
  72. },
  73. },
  74. ],
  75. }
  76. }
  77. function runningToolMessage(id: string): SessionMessages[number] {
  78. return {
  79. info: assistantInfo(id),
  80. parts: [
  81. {
  82. id: `${id}-tool`,
  83. sessionID: "session-1",
  84. messageID: id,
  85. type: "tool",
  86. callID: `${id}-call`,
  87. tool: "bash",
  88. state: {
  89. status: "running",
  90. input: {
  91. command: "pwd",
  92. },
  93. time: {
  94. start: 2,
  95. },
  96. },
  97. },
  98. ],
  99. }
  100. }
  101. describe("run session replay", () => {
  102. test("replays persisted user and assistant history into scrollback commits", () => {
  103. const out = replaySession({
  104. messages: [
  105. userMessage("msg-user-1", "Hello, whats the weather today?"),
  106. assistantMessage("msg-1", "What city or ZIP code should I check?"),
  107. ],
  108. permissions: [],
  109. questions: [],
  110. thinking: true,
  111. limits: {},
  112. })
  113. expect(out.commits).toEqual([
  114. expect.objectContaining({
  115. kind: "user",
  116. text: "Hello, whats the weather today?",
  117. phase: "start",
  118. source: "system",
  119. messageID: "msg-user-1",
  120. }),
  121. expect.objectContaining({
  122. kind: "assistant",
  123. text: "What city or ZIP code should I check?",
  124. phase: "progress",
  125. source: "assistant",
  126. messageID: "msg-1",
  127. }),
  128. ])
  129. expect(out.patch).toEqual(
  130. expect.objectContaining({
  131. phase: "idle",
  132. status: "",
  133. }),
  134. )
  135. })
  136. test("keeps the footer in a running state for resumed active tools", () => {
  137. const out = replaySession({
  138. messages: [runningToolMessage("msg-1")],
  139. permissions: [],
  140. questions: [],
  141. thinking: true,
  142. limits: {},
  143. })
  144. expect(out.patch).toEqual(
  145. expect.objectContaining({
  146. phase: "running",
  147. status: "running bash",
  148. }),
  149. )
  150. })
  151. test("merges failed local rows ahead of later persisted prompts", () => {
  152. const persisted = {
  153. kind: "user",
  154. text: "successful",
  155. phase: "start",
  156. source: "system",
  157. messageID: "msg-user-2",
  158. } as const
  159. const failed = {
  160. kind: "user",
  161. text: "failed",
  162. phase: "start",
  163. source: "system",
  164. messageID: "msg-user-1",
  165. } as const
  166. const error = {
  167. kind: "error",
  168. text: "network unavailable",
  169. phase: "start",
  170. source: "system",
  171. messageID: "msg-user-1",
  172. } as const
  173. expect(replayLocalRows([userMessage("msg-user-2", "successful")], [persisted], [{ commit: failed }, { commit: error }])).toEqual([
  174. failed,
  175. error,
  176. persisted,
  177. ])
  178. })
  179. test("retains local errors but not duplicate local prompts once a prompt persists", () => {
  180. const persisted = {
  181. kind: "user",
  182. text: "failed after persistence",
  183. phase: "start",
  184. source: "system",
  185. messageID: "msg-user-1",
  186. } as const
  187. const error = {
  188. kind: "error",
  189. text: "connection closed",
  190. phase: "start",
  191. source: "system",
  192. messageID: "msg-user-1",
  193. } as const
  194. expect(replayLocalRows([userMessage("msg-user-1", "failed after persistence")], [persisted], [{ commit: persisted }, { commit: error }])).toEqual([
  195. persisted,
  196. error,
  197. ])
  198. })
  199. test("keeps a local turn failure below assistant output already visible for that turn", () => {
  200. const first = {
  201. kind: "user",
  202. text: "start",
  203. phase: "start",
  204. source: "system",
  205. messageID: "msg-user-1",
  206. } as const
  207. const answer = {
  208. kind: "assistant",
  209. text: "partial answer",
  210. phase: "progress",
  211. source: "assistant",
  212. messageID: "msg-assistant-1",
  213. } as const
  214. const error = {
  215. kind: "error",
  216. text: "stream failed",
  217. phase: "start",
  218. source: "system",
  219. messageID: "msg-user-1",
  220. } as const
  221. const second = {
  222. kind: "user",
  223. text: "retry",
  224. phase: "start",
  225. source: "system",
  226. messageID: "msg-user-2",
  227. } as const
  228. expect(
  229. replayLocalRows(
  230. [userMessage("msg-user-1", "start"), userMessage("msg-user-2", "retry")],
  231. [first, answer, second],
  232. [
  233. {
  234. commit: error,
  235. after: { kind: "assistant", text: "partial answer", phase: "progress", messageID: "msg-assistant-1" },
  236. },
  237. ],
  238. ),
  239. ).toEqual([first, answer, error, second])
  240. })
  241. test("keeps a local failure above assistant output received after the failure", () => {
  242. const first = {
  243. kind: "user",
  244. text: "start",
  245. phase: "start",
  246. source: "system",
  247. messageID: "msg-user-1",
  248. } as const
  249. const error = {
  250. kind: "error",
  251. text: "request failed",
  252. phase: "start",
  253. source: "system",
  254. messageID: "msg-user-1",
  255. } as const
  256. const late = {
  257. kind: "assistant",
  258. text: "late answer",
  259. phase: "progress",
  260. source: "assistant",
  261. messageID: "msg-assistant-1",
  262. } as const
  263. expect(replayLocalRows([userMessage("msg-user-1", "start")], [first, late], [{ commit: error }])).toEqual([
  264. first,
  265. error,
  266. late,
  267. ])
  268. })
  269. test("inserts a local failure between persisted output chunks spanning that failure", () => {
  270. const first = {
  271. kind: "user",
  272. text: "start",
  273. phase: "start",
  274. source: "system",
  275. messageID: "msg-user-1",
  276. } as const
  277. const complete = {
  278. kind: "assistant",
  279. text: "before after",
  280. phase: "progress",
  281. source: "assistant",
  282. messageID: "msg-assistant-1",
  283. partID: "part-1",
  284. } as const
  285. const error = {
  286. kind: "error",
  287. text: "stream failed",
  288. phase: "start",
  289. source: "system",
  290. messageID: "msg-user-1",
  291. } as const
  292. expect(
  293. replayLocalRows([userMessage("msg-user-1", "start")], [first, complete], [
  294. {
  295. commit: error,
  296. after: {
  297. kind: "assistant",
  298. text: "before ",
  299. phase: "progress",
  300. messageID: "msg-assistant-1",
  301. partID: "part-1",
  302. visible: "before ",
  303. },
  304. },
  305. ]),
  306. ).toEqual([first, { ...complete, text: "before " }, error, { ...complete, text: "after" }])
  307. })
  308. test("places an unpersisted failed prompt before live output from that turn", () => {
  309. const prompt = {
  310. kind: "user",
  311. text: "start",
  312. phase: "start",
  313. source: "system",
  314. messageID: "msg-1",
  315. } as const
  316. const answer = {
  317. kind: "assistant",
  318. text: "partial answer",
  319. phase: "progress",
  320. source: "assistant",
  321. messageID: "msg-2",
  322. } as const
  323. const error = {
  324. kind: "error",
  325. text: "stream failed",
  326. phase: "start",
  327. source: "system",
  328. messageID: "msg-1",
  329. } as const
  330. expect(
  331. replayLocalRows([], [answer], [
  332. { commit: prompt },
  333. {
  334. commit: error,
  335. after: { kind: "assistant", text: "partial answer", phase: "progress", messageID: "msg-2" },
  336. },
  337. ]),
  338. ).toEqual([prompt, answer, error])
  339. })
  340. test("anchors a failure after the visible start of a tool that later completes", () => {
  341. const prompt = {
  342. kind: "user",
  343. text: "run ls",
  344. phase: "start",
  345. source: "system",
  346. messageID: "msg-user-1",
  347. } as const
  348. const running = {
  349. kind: "tool",
  350. text: "running bash",
  351. phase: "start",
  352. source: "tool",
  353. messageID: "msg-assistant-1",
  354. partID: "part-tool-1",
  355. toolState: "running",
  356. } as const
  357. const completed = {
  358. kind: "tool",
  359. text: "file.txt",
  360. phase: "final",
  361. source: "tool",
  362. messageID: "msg-assistant-1",
  363. partID: "part-tool-1",
  364. toolState: "completed",
  365. } as const
  366. const error = {
  367. kind: "error",
  368. text: "connection lost",
  369. phase: "start",
  370. source: "system",
  371. messageID: "msg-user-1",
  372. } as const
  373. expect(
  374. replayLocalRows([userMessage("msg-user-1", "run ls")], [prompt, running, completed], [
  375. {
  376. commit: error,
  377. after: {
  378. kind: "tool",
  379. text: "running bash",
  380. phase: "start",
  381. messageID: "msg-assistant-1",
  382. partID: "part-tool-1",
  383. toolState: "running",
  384. },
  385. },
  386. ]),
  387. ).toEqual([prompt, running, error, completed])
  388. })
  389. test("retains an unpersisted local diagnostic before later persisted prompts", () => {
  390. const first = {
  391. kind: "user",
  392. text: "before",
  393. phase: "start",
  394. source: "system",
  395. messageID: "msg-user-1",
  396. } as const
  397. const error = {
  398. kind: "error",
  399. text: "failed to start new session",
  400. phase: "start",
  401. source: "system",
  402. messageID: "msg-user-2",
  403. } as const
  404. const second = {
  405. kind: "user",
  406. text: "after",
  407. phase: "start",
  408. source: "system",
  409. messageID: "msg-user-3",
  410. } as const
  411. expect(
  412. replayLocalRows([userMessage("msg-user-1", "before"), userMessage("msg-user-3", "after")], [first, second], [
  413. { commit: error },
  414. ]),
  415. ).toEqual([first, error, second])
  416. })
  417. })