case-full-compact.mjs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. import { DEFAULT_PROVIDER } from "./constants.mjs";
  2. import {
  3. assertCondition,
  4. assertProviderCapture,
  5. buildCaseResult,
  6. buildRuntimeConfig,
  7. compactBoundaries,
  8. createApp,
  9. makeCaseDirs,
  10. modelRequests,
  11. stringifyModelRequest,
  12. writeProviderConfig,
  13. } from "./case-utils.mjs";
  14. import { startCaptureProxy, stopCaptureProxy, writeCaptureFile } from "./capture-proxy.mjs";
  15. import { writeJsonFile } from "./artifacts.mjs";
  16. import { prepareWorkspace } from "./workspace.mjs";
  17. const MANUAL_CASE_NAME = "manual-full-compact";
  18. const AUTO_CASE_NAME = "auto-full-compact";
  19. const MANUAL_DONE_MARKER = "MANUAL_FULL_COMPACT_E2E_DONE";
  20. const MANUAL_SUMMARY_MARKER = "MANUAL_FULL_COMPACT_SUMMARY_MARKER";
  21. const AUTO_DONE_MARKER = "AUTO_FULL_COMPACT_E2E_DONE";
  22. const AUTO_SENTINEL = "AUTO_BASH_FULL_COMPACT_SENTINEL";
  23. export const manualFullCompactCase = {
  24. name: MANUAL_CASE_NAME,
  25. requiresApiKey: true,
  26. async run(input) {
  27. return runRealFullCompactCase(input, {
  28. assert: assertManualOutcome,
  29. caseName: MANUAL_CASE_NAME,
  30. compactConfig: { bufferTokens: 1_000, contextWindow: 1_000_000 },
  31. prompts: [
  32. "Manual compact setup A. Reply with MANUAL_SETUP_ALPHA_ACK only. Do not use tools.",
  33. "Manual compact setup B. Reply with MANUAL_SETUP_BETA_ACK only. Do not use tools.",
  34. `/compact Include ${MANUAL_SUMMARY_MARKER}. Preserve that the next continuation must answer ${MANUAL_DONE_MARKER}.`,
  35. `Continue the manual full compact E2E. Answer with ${MANUAL_DONE_MARKER} only.`,
  36. ],
  37. });
  38. },
  39. };
  40. export const autoFullCompactCase = {
  41. name: AUTO_CASE_NAME,
  42. requiresApiKey: true,
  43. async run(input) {
  44. return runRealFullCompactCase(input, {
  45. assert: assertAutoOutcome,
  46. autoSentinel: AUTO_SENTINEL,
  47. caseName: AUTO_CASE_NAME,
  48. compactConfig: {
  49. bufferTokens: 1,
  50. contextWindow: 180,
  51. microcompact: { enabled: false },
  52. },
  53. prompts: [
  54. [
  55. "Run the auto compact E2E.",
  56. "Use Bash exactly once with this command: node scripts/emit-auto.mjs",
  57. "After the Bash result returns, answer with AUTO_FULL_COMPACT_E2E_DONE.",
  58. "The Bash output intentionally has many lines so runtime performs auto compact before the final answer.",
  59. ].join("\n"),
  60. ],
  61. });
  62. },
  63. };
  64. async function runRealFullCompactCase(input, scenario) {
  65. const events = [];
  66. const paths = await makeCaseDirs({ ...input, caseName: scenario.caseName });
  67. let app;
  68. let captureProxy;
  69. try {
  70. await prepareWorkspace({
  71. autoSentinel: scenario.autoSentinel,
  72. fixtureDir: input.fixtureDir,
  73. workspace: paths.workspace,
  74. });
  75. captureProxy = await startCaptureProxy({ upstreamBaseURL: input.options.baseURL });
  76. await writeProviderConfig(paths.configPath, {
  77. baseURL: captureProxy.baseURL,
  78. model: input.options.model,
  79. providerId: DEFAULT_PROVIDER,
  80. providerName: "DeepSeek",
  81. storageDir: paths.storageDir,
  82. });
  83. app = await createApp({
  84. configPath: paths.configPath,
  85. modules: input.modules,
  86. runtimeConfig: buildRuntimeConfig({
  87. compact: scenario.compactConfig,
  88. workspace: paths.workspace,
  89. }),
  90. storageDir: paths.storageDir,
  91. });
  92. let result;
  93. for (const prompt of scenario.prompts) {
  94. result = await app.submitPrompt(prompt, {
  95. onEvent: async (event) => events.push(event),
  96. });
  97. }
  98. scenario.assert({
  99. events,
  100. response: result?.response ?? "",
  101. modules: input.modules,
  102. });
  103. assertProviderCapture(captureProxy.records);
  104. await writeCaptureFile(captureProxy, paths.capturePath);
  105. await writeJsonFile(paths.eventsPath, events);
  106. const resultPayload = buildCaseResult({
  107. capturedProviderRequestCount: captureProxy.records.length,
  108. caseName: scenario.caseName,
  109. capturePath: paths.capturePath,
  110. events,
  111. eventsPath: paths.eventsPath,
  112. response: result?.response ?? "",
  113. resultPath: paths.resultPath,
  114. traceId: result?.traceId,
  115. });
  116. await writeJsonFile(paths.resultPath, resultPayload);
  117. return resultPayload;
  118. } catch (error) {
  119. await writeCaptureFile(captureProxy, paths.capturePath);
  120. await writeJsonFile(paths.eventsPath, events);
  121. throw error;
  122. } finally {
  123. await app?.close?.();
  124. await stopCaptureProxy(captureProxy);
  125. }
  126. }
  127. function assertManualOutcome(input) {
  128. const { SessionEventType, CompactTrigger, CompactPhase } = input.modules;
  129. const boundaries = compactBoundaries(input.events, SessionEventType);
  130. const compactRequests = modelRequests(input.events, SessionEventType).filter(
  131. (event) => event.payload?.querySource === "compact",
  132. );
  133. const finalContext = stringifyModelRequest(modelRequests(input.events, SessionEventType).at(-1));
  134. assertCondition(boundaries.length >= 1, "expected a manual compact boundary");
  135. assertCondition(boundaries.at(-1)?.payload?.trigger === CompactTrigger.Manual, "expected manual trigger", {
  136. boundary: boundaries.at(-1)?.payload,
  137. });
  138. assertCondition(
  139. boundaries.at(-1)?.payload?.phase === CompactPhase.StandaloneTurn,
  140. "expected standalone compact phase",
  141. { boundary: boundaries.at(-1)?.payload },
  142. );
  143. assertCondition(compactRequests.length >= 1, "expected a compact summary model request");
  144. assertCondition(compactRequests.every((event) => event.payload?.toolCount === 0), "compact must not expose tools");
  145. assertCondition(
  146. stringifyModelRequest(compactRequests.at(-1)).includes(MANUAL_SUMMARY_MARKER),
  147. "expected custom compact instruction to reach summary request",
  148. );
  149. assertCondition(
  150. finalContext.includes("This session is being continued from a previous conversation"),
  151. "expected post-compact continuation context",
  152. );
  153. assertCondition(input.response.includes(MANUAL_DONE_MARKER), `expected ${MANUAL_DONE_MARKER}`, {
  154. response: input.response,
  155. });
  156. }
  157. function assertAutoOutcome(input) {
  158. const { SessionEventType, CompactTrigger, CompactPhase } = input.modules;
  159. const boundaries = compactBoundaries(input.events, SessionEventType);
  160. const toolResults = input.events.filter((event) => event.type === SessionEventType.ToolCallResult);
  161. const compactRequests = modelRequests(input.events, SessionEventType).filter(
  162. (event) => event.payload?.querySource === "compact",
  163. );
  164. assertCondition(toolResults.length >= 1, "expected at least one Bash tool result");
  165. assertCondition(boundaries.length >= 1, "expected an auto compact boundary");
  166. assertCondition(boundaries.at(-1)?.payload?.trigger === CompactTrigger.Auto, "expected auto trigger", {
  167. boundary: boundaries.at(-1)?.payload,
  168. });
  169. assertCondition(boundaries.at(-1)?.payload?.phase === CompactPhase.MidTurn, "expected mid-turn compact", {
  170. boundary: boundaries.at(-1)?.payload,
  171. });
  172. assertCondition(compactRequests.length >= 1, "expected compact summary request");
  173. assertCondition(input.response.includes(AUTO_DONE_MARKER), `expected ${AUTO_DONE_MARKER}`, {
  174. response: input.response,
  175. });
  176. }