case-usage-anchor-assertions.mjs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. import { assertCondition } from "./case-utils.mjs";
  2. import {
  3. FULL_DONE,
  4. HYDRATION_DONE,
  5. HYDRATION_SETUP,
  6. INPUT_ONLY_DONE,
  7. REACTIVE_DONE,
  8. REWIND_KEPT,
  9. REWIND_REMOVED,
  10. isCompactRequest,
  11. } from "./case-usage-anchor-provider.mjs";
  12. export function assertUsageAnchorScenario(input) {
  13. const mainRecords = input.records.filter((record) => !isCompactRequestBody(record));
  14. const lastBody = parseRecordBody(mainRecords.at(-1));
  15. const requestText = JSON.stringify(lastBody?.messages ?? []);
  16. const compactRequestCount = input.records.filter(isCompactRequestBody).length;
  17. const lastCompactRequestIndex = input.records.reduce(
  18. (lastIndex, record, index) => (isCompactRequestBody(record) ? index : lastIndex),
  19. -1,
  20. );
  21. const firstMainAfterCompact = input.records
  22. .slice(lastCompactRequestIndex + 1)
  23. .find((record) => !isCompactRequestBody(record));
  24. switch (input.scenario) {
  25. case "full":
  26. assertCondition(input.response.includes(FULL_DONE), `expected ${FULL_DONE}`, {
  27. response: input.response,
  28. });
  29. assertCondition(compactRequestCount >= 1, "full compact must issue a summary request");
  30. assertCondition(
  31. assistantEntries(input.historySnapshots?.beforeRestart).length === 0,
  32. "full compact must remove summarized assistants and their runtime token anchors",
  33. { entries: input.historySnapshots?.beforeRestart },
  34. );
  35. assertCondition(
  36. durableAssistantTokenInput(input.durableSnapshots?.beforeRestart, "USAGE_FULL_SETUP_1") ===
  37. 10_000 &&
  38. durableAssistantTokenInput(
  39. input.durableSnapshots?.beforeRestart,
  40. "USAGE_FULL_SETUP_2",
  41. ) === 10_000,
  42. "full compact must leave the original assistant tokens intact in the durable transcript",
  43. { messages: input.durableSnapshots?.beforeRestart },
  44. );
  45. assertCondition(
  46. !requestText.includes("USAGE_FULL_SETUP_1") && !requestText.includes("USAGE_FULL_SETUP_2"),
  47. "full compact must not rehydrate summarized assistants",
  48. { requestText },
  49. );
  50. assertCondition(
  51. assistantEntries(input.historySnapshots?.afterRestartBeforeFollowUp).length === 0,
  52. "cold hydration after full compact must not recreate summarized assistants or their tokens",
  53. { entries: input.historySnapshots?.afterRestartBeforeFollowUp },
  54. );
  55. assertCondition(
  56. durableAssistantTokenInput(
  57. input.durableSnapshots?.afterRestartBeforeFollowUp,
  58. "USAGE_FULL_SETUP_1",
  59. ) === 10_000,
  60. "cold hydration must not rewrite summarized assistant tokens in the durable transcript",
  61. { messages: input.durableSnapshots?.afterRestartBeforeFollowUp },
  62. );
  63. break;
  64. case "reactive":
  65. assertCondition(input.response.includes(REACTIVE_DONE), `expected ${REACTIVE_DONE}`, {
  66. response: input.response,
  67. });
  68. assertCondition(compactRequestCount >= 1, "reactive overflow must issue a compact request");
  69. assertCondition(
  70. firstMainAfterCompact !== undefined,
  71. "reactive compact must be followed by a real model request",
  72. );
  73. assertCondition(
  74. JSON.stringify(parseRecordBody(firstMainAfterCompact)?.messages ?? []).includes(
  75. "USAGE_REACTIVE_SETUP_2",
  76. ),
  77. "the first request after reactive compact must still contain the preserved tail",
  78. {
  79. preservedAssistantContents: (parseRecordBody(firstMainAfterCompact)?.messages ?? [])
  80. .filter((message) => message?.role === "assistant")
  81. .map((message) => message.content),
  82. },
  83. );
  84. assertCondition(
  85. assistantTokenInput(input.historySnapshots?.beforeRestart, "USAGE_REACTIVE_SETUP_2") === 0,
  86. "reactive compact must invalidate preserved assistant tokens in the live runtime copy",
  87. { entries: input.historySnapshots?.beforeRestart },
  88. );
  89. assertCondition(
  90. durableAssistantTokenInput(
  91. input.durableSnapshots?.beforeRestart,
  92. "USAGE_REACTIVE_SETUP_2",
  93. ) === 10_000,
  94. "reactive compact must not rewrite preserved assistant tokens in the durable transcript",
  95. { messages: input.durableSnapshots?.beforeRestart },
  96. );
  97. assertCondition(
  98. assistantTokenInput(
  99. input.historySnapshots?.afterRestartBeforeFollowUp,
  100. "USAGE_REACTIVE_SETUP_2",
  101. ) === 0,
  102. "cold hydration after reactive compact must keep preserved assistant tokens invalidated",
  103. { entries: input.historySnapshots?.afterRestartBeforeFollowUp },
  104. );
  105. assertCondition(
  106. durableAssistantTokenInput(
  107. input.durableSnapshots?.afterRestartBeforeFollowUp,
  108. "USAGE_REACTIVE_SETUP_2",
  109. ) === 10_000,
  110. "cold hydration must preserve the original preserved-tail tokens in durable storage",
  111. { messages: input.durableSnapshots?.afterRestartBeforeFollowUp },
  112. );
  113. break;
  114. case "input-only":
  115. assertCondition(input.response.includes(INPUT_ONLY_DONE), `expected ${INPUT_ONLY_DONE}`, {
  116. response: input.response,
  117. });
  118. assertCondition(
  119. assistantTokenInput(input.historySnapshots?.beforeRestart, "USAGE_INPUT_ONLY_SETUP") ===
  120. 10_000,
  121. "input-only provider usage must be stored on the committed assistant",
  122. { entries: input.historySnapshots?.beforeRestart },
  123. );
  124. assertCondition(
  125. durableAssistantTokenInput(
  126. input.durableSnapshots?.beforeRestart,
  127. "USAGE_INPUT_ONLY_SETUP",
  128. ) === 10_000,
  129. "input-only provider usage must be persisted on the committed assistant",
  130. { messages: input.durableSnapshots?.beforeRestart },
  131. );
  132. break;
  133. case "hydration":
  134. assertCondition(input.response.includes(HYDRATION_DONE), `expected ${HYDRATION_DONE}`, {
  135. response: input.response,
  136. });
  137. assertCondition(
  138. assistantTokenInput(input.historySnapshots?.beforeRestart, HYDRATION_SETUP) === 10_000,
  139. "the non-empty assistant must persist its provider tokens before hydration",
  140. { entries: input.historySnapshots?.beforeRestart },
  141. );
  142. assertCondition(
  143. durableAssistantTokenInput(input.durableSnapshots?.beforeRestart, HYDRATION_SETUP) ===
  144. 10_000,
  145. "the non-empty assistant tokens must be present in durable storage before hydration",
  146. { messages: input.durableSnapshots?.beforeRestart },
  147. );
  148. assertCondition(
  149. assistantTokenInput(input.historySnapshots?.afterRestartBeforeFollowUp, HYDRATION_SETUP) ===
  150. 10_000,
  151. "cold hydration must keep the non-empty assistant usage anchor",
  152. { entries: input.historySnapshots?.afterRestartBeforeFollowUp },
  153. );
  154. assertCondition(
  155. emptyAssistantEntries(input.historySnapshots?.beforeRestart).length === 1 &&
  156. emptyAssistantEntries(input.historySnapshots?.beforeRestart)[0]?.tokens?.input === 15_000,
  157. "a contentless assistant with valid provider usage must remain the live usage anchor",
  158. { entries: input.historySnapshots?.beforeRestart },
  159. );
  160. assertCondition(
  161. emptyAssistantEntries(input.historySnapshots?.afterRestartBeforeFollowUp).length === 1 &&
  162. emptyAssistantEntries(input.historySnapshots?.afterRestartBeforeFollowUp)[0]?.tokens
  163. ?.input === 15_000,
  164. "cold hydration must restore the same contentless assistant usage anchor",
  165. { entries: input.historySnapshots?.afterRestartBeforeFollowUp },
  166. );
  167. assertCondition(
  168. durableAssistantTokenInput(
  169. input.durableSnapshots?.afterRestartBeforeFollowUp,
  170. HYDRATION_SETUP,
  171. ) === 10_000 &&
  172. durableAssistantEntries(input.durableSnapshots?.afterRestartBeforeFollowUp).some(
  173. (message) => message.content.length === 0 && (message.tokens?.input ?? 0) === 15_000,
  174. ),
  175. "hydration must preserve both the valid anchor and the empty assistant tokens durably",
  176. { messages: input.durableSnapshots?.afterRestartBeforeFollowUp },
  177. );
  178. break;
  179. case "rewind":
  180. assertCondition(input.response.includes(REWIND_KEPT), `expected ${REWIND_KEPT}`, {
  181. response: input.response,
  182. });
  183. assertCondition(
  184. !requestText.includes(REWIND_REMOVED),
  185. "rewind must remove the rewound assistant from the next provider request",
  186. { requestText },
  187. );
  188. assertCondition(
  189. assistantTokenInput(input.historySnapshots?.beforeRewind, REWIND_KEPT) === 10_000,
  190. "rewind must retain the kept branch assistant tokens before branch cut",
  191. { entries: input.historySnapshots?.beforeRewind },
  192. );
  193. assertCondition(
  194. assistantTokenInput(input.historySnapshots?.beforeRewind, REWIND_REMOVED) === 19_000,
  195. "rewind setup must record the removed branch assistant tokens",
  196. { entries: input.historySnapshots?.beforeRewind },
  197. );
  198. assertCondition(
  199. durableAssistantTokenInput(input.durableSnapshots?.beforeRewind, REWIND_KEPT) === 10_000 &&
  200. durableAssistantTokenInput(input.durableSnapshots?.beforeRewind, REWIND_REMOVED) ===
  201. 19_000,
  202. "rewind setup must persist both branch token values",
  203. { messages: input.durableSnapshots?.beforeRewind },
  204. );
  205. assertCondition(
  206. !assistantEntries(input.historySnapshots?.afterRewind).some((message) =>
  207. message.content.includes(REWIND_REMOVED),
  208. ),
  209. "rewind must remove the rewound assistant and its tokens from the live runtime history",
  210. { entries: input.historySnapshots?.afterRewind },
  211. );
  212. assertCondition(
  213. assistantTokenInput(input.historySnapshots?.afterRewind, REWIND_KEPT) === 10_000,
  214. "rewind must retain the kept branch assistant tokens in the live runtime history",
  215. { entries: input.historySnapshots?.afterRewind },
  216. );
  217. assertCondition(
  218. durableAssistantTokenInput(input.durableSnapshots?.afterRewind, REWIND_REMOVED) === 19_000,
  219. "rewind must retain the removed branch tokens only in the durable transcript",
  220. { messages: input.durableSnapshots?.afterRewind },
  221. );
  222. assertCondition(
  223. !assistantEntries(input.historySnapshots?.afterRestartBeforeFollowUp).some((message) =>
  224. message.content.includes(REWIND_REMOVED),
  225. ),
  226. "cold hydration after rewind must not restore the removed branch or its tokens",
  227. { entries: input.historySnapshots?.afterRestartBeforeFollowUp },
  228. );
  229. assertCondition(
  230. assistantTokenInput(input.historySnapshots?.afterRestartBeforeFollowUp, REWIND_KEPT) ===
  231. 10_000,
  232. "cold hydration after rewind must retain the kept branch assistant tokens",
  233. { entries: input.historySnapshots?.afterRestartBeforeFollowUp },
  234. );
  235. assertCondition(
  236. durableAssistantTokenInput(
  237. input.durableSnapshots?.afterRestartBeforeFollowUp,
  238. REWIND_KEPT,
  239. ) === 10_000 &&
  240. durableAssistantTokenInput(
  241. input.durableSnapshots?.afterRestartBeforeFollowUp,
  242. REWIND_REMOVED,
  243. ) === 19_000,
  244. "cold hydration after rewind must preserve durable branch token facts while excluding the removed branch at runtime",
  245. { messages: input.durableSnapshots?.afterRestartBeforeFollowUp },
  246. );
  247. assertCondition(
  248. input.rewindFollowUpRecordIndex !== undefined,
  249. "rewind must capture the immediate follow-up request boundary",
  250. );
  251. assertCondition(
  252. !JSON.stringify(
  253. parseRecordBody(input.records[input.rewindFollowUpRecordIndex])?.messages ?? [],
  254. ).includes(REWIND_REMOVED),
  255. "the immediate post-rewind provider request must not contain the removed branch",
  256. );
  257. break;
  258. default:
  259. throw new Error(`Unknown usage anchor scenario: ${input.scenario}`);
  260. }
  261. }
  262. function assistantEntries(snapshot) {
  263. return (snapshot ?? []).filter((entry) => entry.role === "assistant");
  264. }
  265. function emptyAssistantEntries(snapshot) {
  266. return assistantEntries(snapshot).filter((entry) => entry.content.trim().length === 0);
  267. }
  268. function assistantTokenInput(snapshot, marker) {
  269. const entry = assistantEntries(snapshot).find((candidate) => candidate.content.includes(marker));
  270. return entry?.tokens?.input ?? 0;
  271. }
  272. function durableAssistantEntries(snapshot) {
  273. return snapshot ?? [];
  274. }
  275. function durableAssistantTokenInput(snapshot, marker) {
  276. const entry = durableAssistantEntries(snapshot).find((candidate) =>
  277. candidate.content.includes(marker),
  278. );
  279. return entry?.tokens?.input ?? 0;
  280. }
  281. function isCompactRequestBody(record) {
  282. return isCompactRequest(parseRecordBody(record));
  283. }
  284. function parseRecordBody(record) {
  285. try {
  286. return JSON.parse(record?.requestBody ?? "{}");
  287. } catch {
  288. return {};
  289. }
  290. }