rewind.test.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. import { describe, expect, it } from "vitest";
  2. import { CompactTrigger, type CompactBoundaryPayload } from "../src/compact/index.js";
  3. import type { CompactContextItem } from "../src/compact/index.js";
  4. import type { MessageId, TraceId } from "../src/interfaces/shared.js";
  5. import {
  6. RewindScope,
  7. RewindStrategy,
  8. RewindTargetStatus,
  9. checkpointCreatedPayloadSchema,
  10. evaluateRewindTarget,
  11. parseCheckpointCreatedPayload,
  12. parseRewindTriggeredPayload,
  13. parseWorkspaceCheckpointArtifact,
  14. rewindTriggeredPayloadSchema,
  15. selectActiveConversationBranch,
  16. workspaceCheckpointArtifactSchema,
  17. } from "../src/rewind/index.js";
  18. describe("rewind contracts", () => {
  19. const boundaryPayload: CompactBoundaryPayload = {
  20. boundaryId: "compact_1",
  21. trigger: CompactTrigger.Manual,
  22. summarySource: "model",
  23. preCompactTokenCount: 120000,
  24. truePostCompactTokenCount: 20000,
  25. summarizedMessageCount: 8,
  26. summaryMessageIds: ["msg_summary" as MessageId],
  27. traceId: "trace_1" as TraceId,
  28. };
  29. it("validates checkpoint and rewind payloads at runtime", () => {
  30. expect(
  31. parseCheckpointCreatedPayload({
  32. checkpointId: "checkpoint_1",
  33. messageId: "msg_1",
  34. targetMessageId: "msg_1",
  35. toolMessageId: "msg_assistant_1",
  36. scope: RewindScope.Workspace,
  37. snapshotRef: "artifact://snapshot_1",
  38. }),
  39. ).toMatchObject({
  40. checkpointId: "checkpoint_1",
  41. targetMessageId: "msg_1",
  42. toolMessageId: "msg_assistant_1",
  43. scope: "workspace",
  44. });
  45. expect(
  46. parseRewindTriggeredPayload({
  47. rewindId: "rewind_1",
  48. scope: RewindScope.Workspace,
  49. strategy: RewindStrategy.FileOnly,
  50. branchCutAfterMessageId: "msg_cut",
  51. branchGeneration: 2,
  52. targetMessageId: "msg_1",
  53. targetCheckpointId: "checkpoint_1",
  54. }),
  55. ).toMatchObject({
  56. rewindId: "rewind_1",
  57. branchGeneration: 2,
  58. strategy: "file_only",
  59. });
  60. expect(() =>
  61. checkpointCreatedPayloadSchema.parse({
  62. checkpointId: "checkpoint_1",
  63. messageId: "msg_1",
  64. scope: "everything",
  65. snapshotRef: "artifact://snapshot_1",
  66. }),
  67. ).toThrow();
  68. expect(() =>
  69. rewindTriggeredPayloadSchema.parse({
  70. rewindId: "rewind_1",
  71. scope: RewindScope.Workspace,
  72. strategy: "time_travel",
  73. }),
  74. ).toThrow();
  75. });
  76. it("validates workspace checkpoint artifact payloads", () => {
  77. expect(
  78. parseWorkspaceCheckpointArtifact({
  79. version: 1,
  80. kind: "workspace_file_before_change",
  81. createdAt: "2026-05-04T00:00:00.000Z",
  82. toolCallId: "tool_1",
  83. toolName: "Write",
  84. files: [
  85. {
  86. path: "/work/demo.ts",
  87. existedBefore: true,
  88. beforeContent: "old",
  89. structuredPatch: [
  90. {
  91. oldStart: 1,
  92. oldLines: 1,
  93. newStart: 1,
  94. newLines: 1,
  95. lines: ["-old", "+new"],
  96. },
  97. ],
  98. },
  99. ],
  100. }),
  101. ).toMatchObject({
  102. kind: "workspace_file_before_change",
  103. files: [{ beforeContent: "old", existedBefore: true }],
  104. });
  105. expect(() =>
  106. workspaceCheckpointArtifactSchema.parse({
  107. version: 1,
  108. kind: "workspace_file_before_change",
  109. createdAt: "2026-05-04T00:00:00.000Z",
  110. toolCallId: "tool_1",
  111. toolName: "Write",
  112. files: [],
  113. }),
  114. ).toThrow();
  115. });
  116. it("allows conversation rewind when the target is in the active chain", () => {
  117. const result = evaluateRewindTarget({
  118. items: [item("old"), boundary(), item("msg_summary"), item("msg_after")],
  119. scope: RewindScope.Conversation,
  120. targetMessageId: "msg_after",
  121. });
  122. expect(result).toMatchObject({
  123. compactBoundaryId: "compact_1",
  124. reason: "target_in_active_chain",
  125. strategy: RewindStrategy.ActiveChain,
  126. targetStatus: RewindTargetStatus.ActiveChain,
  127. });
  128. expect(result.allowedScopes).toEqual([RewindScope.Conversation]);
  129. });
  130. it("requires a checkpoint for workspace rewind even inside the active chain", () => {
  131. const result = evaluateRewindTarget({
  132. checkpointAvailable: false,
  133. items: [boundary(), item("msg_after")],
  134. scope: RewindScope.Workspace,
  135. targetMessageId: "msg_after",
  136. });
  137. expect(result).toMatchObject({
  138. reason: "checkpoint_required_for_workspace_rewind",
  139. strategy: RewindStrategy.Unavailable,
  140. targetStatus: RewindTargetStatus.ActiveChain,
  141. });
  142. });
  143. it("allows file-only rewind for compact-covered targets with checkpoints", () => {
  144. const result = evaluateRewindTarget({
  145. checkpointAvailable: true,
  146. items: [item("msg_before"), boundary(), item("msg_summary")],
  147. scope: RewindScope.Workspace,
  148. targetMessageId: "msg_before",
  149. });
  150. expect(result).toMatchObject({
  151. compactBoundaryId: "compact_1",
  152. reason: "target_covered_by_compact_file_only_available",
  153. strategy: RewindStrategy.FileOnly,
  154. targetStatus: RewindTargetStatus.CoveredByCompact,
  155. });
  156. expect(result.allowedScopes).toEqual([RewindScope.Workspace]);
  157. });
  158. it("rebuilds the active branch for compact-covered conversation rewind", () => {
  159. const result = evaluateRewindTarget({
  160. checkpointAvailable: true,
  161. items: [item("msg_before"), boundary(), item("msg_summary")],
  162. scope: RewindScope.Conversation,
  163. targetMessageId: "msg_before",
  164. });
  165. expect(result).toMatchObject({
  166. compactBoundaryId: "compact_1",
  167. reason: "target_covered_by_compact_active_branch_rebuild",
  168. strategy: RewindStrategy.ActiveChain,
  169. targetStatus: RewindTargetStatus.CoveredByCompact,
  170. });
  171. expect(result.allowedScopes).toEqual([
  172. RewindScope.Conversation,
  173. RewindScope.Workspace,
  174. RewindScope.Both,
  175. ]);
  176. });
  177. it("selects only the latest append-only branch across consecutive branch cuts", () => {
  178. const messages = ["old-user", "old-assistant", "edited-user", "edited-assistant", "final-user"]
  179. .map((id) => ({ info: { id: id as MessageId } }));
  180. expect(
  181. selectActiveConversationBranch(messages, {
  182. branchCutAfterMessageId: "edited-assistant" as MessageId,
  183. rewindKeptMessageIds: [],
  184. rewindTargetMessageId: "edited-user" as MessageId,
  185. }).map((message) => message.info.id),
  186. ).toEqual(["final-user"]);
  187. });
  188. it("marks missing targets unavailable", () => {
  189. const result = evaluateRewindTarget({
  190. items: [boundary(), item("msg_summary")],
  191. scope: RewindScope.Conversation,
  192. targetMessageId: "missing",
  193. });
  194. expect(result).toMatchObject({
  195. reason: "target_not_found",
  196. strategy: RewindStrategy.Unavailable,
  197. targetStatus: RewindTargetStatus.Missing,
  198. });
  199. });
  200. function boundary(): CompactContextItem {
  201. return item("boundary", { compactBoundary: boundaryPayload });
  202. }
  203. function item(id: string, extra?: Partial<CompactContextItem>): CompactContextItem {
  204. return {
  205. id,
  206. ...extra,
  207. };
  208. }
  209. });