persistent-command-facts.test.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. import { describe, expect, it, vi } from "vitest";
  2. import type {
  3. MessageWithParts,
  4. SessionEntryInfo,
  5. SessionId,
  6. SessionInputRecord,
  7. SessionStorePort,
  8. } from "@zcode/contracts";
  9. import {
  10. loadPersistentCommandFacts,
  11. savePersistentCommandFact,
  12. V4_COMMAND_FACT_SESSION_ENTRY,
  13. } from "../src/zcode-protocol-v4/persistent-command-facts.js";
  14. const sessionId = "session-1" as SessionId;
  15. function storeFixture() {
  16. const savedEntries: SessionEntryInfo[] = [];
  17. const messages = [
  18. {
  19. info: {
  20. id: "message-1",
  21. role: "user",
  22. anchor: { sourceCommandId: "command-transcript" },
  23. },
  24. parts: [],
  25. },
  26. {
  27. info: { id: "message-2", role: "assistant" },
  28. parts: [
  29. {
  30. type: "timeline",
  31. sourceCommandId: "command-timeline-part",
  32. },
  33. ],
  34. },
  35. ] as unknown as MessageWithParts[];
  36. const entries = [
  37. {
  38. id: "entry-child",
  39. sessionID: sessionId,
  40. type: V4_COMMAND_FACT_SESSION_ENTRY,
  41. time: { created: 1, updated: 1 },
  42. data: {
  43. source: "child",
  44. ack: {
  45. commandId: "command-child",
  46. status: "accepted",
  47. revisionAtDecision: 0,
  48. result: { type: "forkAssistant", sessionId: "child-1" },
  49. },
  50. },
  51. },
  52. ] satisfies SessionEntryInfo[];
  53. const discarded = [
  54. {
  55. id: "queue_command-discarded",
  56. sessionID: sessionId,
  57. kind: "sendText",
  58. delivery: "queue",
  59. payload: {
  60. text: "lost on restart",
  61. conversationInputIntent: { sourceCommandId: "command-discarded" },
  62. },
  63. admittedSequence: 1,
  64. status: "discarded",
  65. statusReason: "session_resumed",
  66. time: { created: 1, updated: 2 },
  67. },
  68. {
  69. id: "queue_command-admitted-before-restart",
  70. sessionID: sessionId,
  71. kind: "sendText",
  72. delivery: "queue",
  73. payload: {
  74. text: "admitted before process death",
  75. intent: { sourceCommandId: "command-admitted-before-restart" },
  76. },
  77. admittedSequence: 2,
  78. status: "admitted",
  79. time: { created: 2, updated: 2 },
  80. },
  81. {
  82. id: "queue_command-user-removed",
  83. sessionID: sessionId,
  84. kind: "sendText",
  85. delivery: "queue",
  86. payload: {
  87. text: "user removed queue item",
  88. conversationInputIntent: { sourceCommandId: "command-user-removed" },
  89. },
  90. admittedSequence: 3,
  91. status: "cancelled",
  92. statusReason: "user_removed",
  93. time: { created: 3, updated: 4 },
  94. },
  95. {
  96. id: "queue_command-child-start-failed",
  97. sessionID: sessionId,
  98. kind: "sendText",
  99. delivery: "startNow",
  100. payload: {
  101. text: "edited child input",
  102. conversationInputIntent: { sourceCommandId: "command-child-start-failed" },
  103. },
  104. admittedSequence: 4,
  105. status: "failed",
  106. statusReason: "fault.command.childStartFailed",
  107. time: { created: 4, updated: 5 },
  108. },
  109. ] satisfies SessionInputRecord[];
  110. const store = {
  111. messages: vi.fn().mockResolvedValue(messages),
  112. sessionEntries: vi.fn().mockResolvedValue(entries),
  113. listSessionInputs: vi.fn().mockResolvedValue(discarded),
  114. settleSessionInput: vi.fn().mockResolvedValue(undefined),
  115. saveSessionEntry: vi.fn().mockImplementation(async (entry: SessionEntryInfo) => {
  116. savedEntries.push(entry);
  117. }),
  118. } as unknown as SessionStorePort;
  119. return { savedEntries, store };
  120. }
  121. describe("persistent command facts", () => {
  122. it("loads transcript/timeline/child/discarded by exact sourceCommandId", async () => {
  123. const { store } = storeFixture();
  124. const facts = await loadPersistentCommandFacts(store, sessionId, {
  125. discardAdmittedOnLoad: true,
  126. });
  127. expect(facts.transcript).toEqual([
  128. {
  129. commandId: "command-transcript",
  130. status: "accepted",
  131. revisionAtDecision: 0,
  132. },
  133. ]);
  134. expect(facts.timeline).toEqual([
  135. {
  136. commandId: "command-timeline-part",
  137. status: "accepted",
  138. revisionAtDecision: 0,
  139. },
  140. ]);
  141. expect(facts.child?.[0]).toMatchObject({
  142. commandId: "command-child",
  143. result: { sessionId: "child-1" },
  144. });
  145. expect(facts.discarded).toEqual([
  146. {
  147. commandId: "command-discarded",
  148. status: "failed",
  149. reasonCode: "fault.command.inputDiscardedOnRestart",
  150. message: "Input was discarded when the CLI restarted; confirm before resending.",
  151. revisionAtDecision: 0,
  152. result: { type: "inputDisposition", delivery: "queue" },
  153. },
  154. {
  155. commandId: "command-admitted-before-restart",
  156. status: "failed",
  157. reasonCode: "fault.command.inputDiscardedOnRestart",
  158. message: "Input was discarded when the CLI restarted; confirm before resending.",
  159. revisionAtDecision: 0,
  160. result: { type: "inputDisposition", delivery: "queue" },
  161. },
  162. {
  163. commandId: "command-user-removed",
  164. status: "failed",
  165. reasonCode: "fault.command.inputCancelled",
  166. message: "Input was cancelled before it entered the transcript.",
  167. revisionAtDecision: 0,
  168. },
  169. ]);
  170. expect(store.settleSessionInput).toHaveBeenCalledWith({
  171. id: "queue_command-admitted-before-restart",
  172. sessionID: sessionId,
  173. status: "discarded",
  174. reason: "session_resumed",
  175. });
  176. expect(store.settleSessionInput).toHaveBeenCalledTimes(1);
  177. expect(facts.discarded?.some((fact) => fact.commandId === "command-child-start-failed")).toBe(
  178. false,
  179. );
  180. });
  181. it("keeps a user-removed input queryable after the in-memory LRU is gone", async () => {
  182. const { store } = storeFixture();
  183. const facts = await loadPersistentCommandFacts(store, sessionId);
  184. expect(facts.discarded).toContainEqual({
  185. commandId: "command-user-removed",
  186. status: "failed",
  187. reasonCode: "fault.command.inputCancelled",
  188. message: "Input was cancelled before it entered the transcript.",
  189. revisionAtDecision: 0,
  190. });
  191. expect(
  192. facts.discarded?.some((fact) => fact.commandId === "command-admitted-before-restart"),
  193. ).toBe(false);
  194. expect(store.settleSessionInput).not.toHaveBeenCalled();
  195. });
  196. it("saves deterministic child/timeline entries for restart lookup", async () => {
  197. const { savedEntries, store } = storeFixture();
  198. await savePersistentCommandFact(
  199. store,
  200. sessionId,
  201. "child",
  202. {
  203. commandId: "command-fork",
  204. status: "accepted",
  205. revisionAtDecision: 0,
  206. result: { type: "forkAssistant", sessionId: "child-2" },
  207. },
  208. {
  209. parentSessionId: sessionId,
  210. sourceCommandId: "command-fork",
  211. forkTarget: {
  212. productTurnId: "product-1",
  213. transcriptTurnId: "runtime-1",
  214. orderedMessageIds: ["user-1", "assistant-1"],
  215. boundaryMessageId: "assistant-1",
  216. },
  217. },
  218. );
  219. expect(savedEntries[0]).toMatchObject({
  220. id: "v4_command_fact:child:command-fork",
  221. sessionID: sessionId,
  222. type: V4_COMMAND_FACT_SESSION_ENTRY,
  223. data: {
  224. source: "child",
  225. ack: {
  226. commandId: "command-fork",
  227. result: { sessionId: "child-2" },
  228. },
  229. metadata: {
  230. parentSessionId: sessionId,
  231. sourceCommandId: "command-fork",
  232. forkTarget: expect.objectContaining({ boundaryMessageId: "assistant-1" }),
  233. },
  234. },
  235. });
  236. });
  237. });