rewind.test.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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("校验已提交回退的消息清单,兼容旧事件缺席", () => {
  77. const base = { rewindId: "rewind", scope: "conversation", strategy: "active_chain" };
  78. expect(
  79. parseRewindTriggeredPayload({ ...base, removedMessageIds: ["new-user", "new-message"] }),
  80. ).toMatchObject({ removedMessageIds: ["new-user", "new-message"] });
  81. expect(parseRewindTriggeredPayload(base)).not.toHaveProperty("removedMessageIds");
  82. expect(() => parseRewindTriggeredPayload({ ...base, removedMessageIds: [""] })).toThrow();
  83. });
  84. it("validates workspace checkpoint artifact payloads", () => {
  85. expect(
  86. parseWorkspaceCheckpointArtifact({
  87. version: 1,
  88. kind: "workspace_file_before_change",
  89. createdAt: "2026-05-04T00:00:00.000Z",
  90. toolCallId: "tool_1",
  91. toolName: "Write",
  92. files: [
  93. {
  94. path: "/work/demo.ts",
  95. existedBefore: true,
  96. beforeContent: "old",
  97. structuredPatch: [
  98. {
  99. oldStart: 1,
  100. oldLines: 1,
  101. newStart: 1,
  102. newLines: 1,
  103. lines: ["-old", "+new"],
  104. },
  105. ],
  106. },
  107. ],
  108. }),
  109. ).toMatchObject({
  110. kind: "workspace_file_before_change",
  111. files: [{ beforeContent: "old", existedBefore: true }],
  112. });
  113. expect(() =>
  114. workspaceCheckpointArtifactSchema.parse({
  115. version: 1,
  116. kind: "workspace_file_before_change",
  117. createdAt: "2026-05-04T00:00:00.000Z",
  118. toolCallId: "tool_1",
  119. toolName: "Write",
  120. files: [],
  121. }),
  122. ).toThrow();
  123. });
  124. it("allows conversation rewind when the target is in the active chain", () => {
  125. const result = evaluateRewindTarget({
  126. items: [item("old"), boundary(), item("msg_summary"), item("msg_after")],
  127. scope: RewindScope.Conversation,
  128. targetMessageId: "msg_after",
  129. });
  130. expect(result).toMatchObject({
  131. compactBoundaryId: "compact_1",
  132. reason: "target_in_active_chain",
  133. strategy: RewindStrategy.ActiveChain,
  134. targetStatus: RewindTargetStatus.ActiveChain,
  135. });
  136. expect(result.allowedScopes).toEqual([RewindScope.Conversation]);
  137. });
  138. it("requires a checkpoint for workspace rewind even inside the active chain", () => {
  139. const result = evaluateRewindTarget({
  140. checkpointAvailable: false,
  141. items: [boundary(), item("msg_after")],
  142. scope: RewindScope.Workspace,
  143. targetMessageId: "msg_after",
  144. });
  145. expect(result).toMatchObject({
  146. reason: "checkpoint_required_for_workspace_rewind",
  147. strategy: RewindStrategy.Unavailable,
  148. targetStatus: RewindTargetStatus.ActiveChain,
  149. });
  150. });
  151. it("allows file-only rewind for compact-covered targets with checkpoints", () => {
  152. const result = evaluateRewindTarget({
  153. checkpointAvailable: true,
  154. items: [item("msg_before"), boundary(), item("msg_summary")],
  155. scope: RewindScope.Workspace,
  156. targetMessageId: "msg_before",
  157. });
  158. expect(result).toMatchObject({
  159. compactBoundaryId: "compact_1",
  160. reason: "target_covered_by_compact_file_only_available",
  161. strategy: RewindStrategy.FileOnly,
  162. targetStatus: RewindTargetStatus.CoveredByCompact,
  163. });
  164. expect(result.allowedScopes).toEqual([RewindScope.Workspace]);
  165. });
  166. it("rebuilds the active branch for compact-covered conversation rewind", () => {
  167. const result = evaluateRewindTarget({
  168. checkpointAvailable: true,
  169. items: [item("msg_before"), boundary(), item("msg_summary")],
  170. scope: RewindScope.Conversation,
  171. targetMessageId: "msg_before",
  172. });
  173. expect(result).toMatchObject({
  174. compactBoundaryId: "compact_1",
  175. reason: "target_covered_by_compact_active_branch_rebuild",
  176. strategy: RewindStrategy.ActiveChain,
  177. targetStatus: RewindTargetStatus.CoveredByCompact,
  178. });
  179. expect(result.allowedScopes).toEqual([
  180. RewindScope.Conversation,
  181. RewindScope.Workspace,
  182. RewindScope.Both,
  183. ]);
  184. });
  185. it("selects only the latest append-only branch across consecutive branch cuts", () => {
  186. const messages = [
  187. "old-user",
  188. "old-assistant",
  189. "edited-user",
  190. "edited-assistant",
  191. "final-user",
  192. ].map((id) => ({ info: { id: id as MessageId } }));
  193. expect(
  194. selectActiveConversationBranch(messages, {
  195. branchCutAfterMessageId: "edited-assistant" as MessageId,
  196. rewindKeptMessageIds: [],
  197. rewindTargetMessageId: "edited-user" as MessageId,
  198. }).map((message) => message.info.id),
  199. ).toEqual(["final-user"]);
  200. });
  201. it("marks missing targets unavailable", () => {
  202. const result = evaluateRewindTarget({
  203. items: [boundary(), item("msg_summary")],
  204. scope: RewindScope.Conversation,
  205. targetMessageId: "missing",
  206. });
  207. expect(result).toMatchObject({
  208. reason: "target_not_found",
  209. strategy: RewindStrategy.Unavailable,
  210. targetStatus: RewindTargetStatus.Missing,
  211. });
  212. });
  213. function boundary(): CompactContextItem {
  214. return item("boundary", { compactBoundary: boundaryPayload });
  215. }
  216. function item(id: string, extra?: Partial<CompactContextItem>): CompactContextItem {
  217. return {
  218. id,
  219. ...extra,
  220. };
  221. }
  222. });