case-microcompact.mjs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import { DEFAULT_PROVIDER } from "./constants.mjs";
  2. import {
  3. assertCondition,
  4. assertProviderCapture,
  5. buildCaseResult,
  6. buildRuntimeConfig,
  7. compactBoundaries,
  8. countEvents,
  9. createApp,
  10. hasMicrocompactPlaceholder,
  11. makeCaseDirs,
  12. modelRequests,
  13. stringifyModelRequest,
  14. writeProviderConfig,
  15. } from "./case-utils.mjs";
  16. import { startCaptureProxy, stopCaptureProxy, writeCaptureFile } from "./capture-proxy.mjs";
  17. import { writeJsonFile } from "./artifacts.mjs";
  18. import { prepareWorkspace } from "./workspace.mjs";
  19. const CASE_NAME = "microcompact";
  20. const COMPLETION_MARKER = "MICROCOMPACT_E2E_DONE";
  21. const ALPHA_SENTINEL = "ALPHA_BASH_MICROCOMPACT_SENTINEL";
  22. const BETA_SENTINEL = "BETA_BASH_MICROCOMPACT_SENTINEL";
  23. const MICROCOMPACT_THRESHOLD_TOKENS = 1;
  24. const MICROCOMPACT_KEEP_RECENT_TOOL_RESULTS = 1;
  25. const MICROCOMPACT_MIN_TOKEN_SAVINGS = 1;
  26. export const microcompactCase = {
  27. name: CASE_NAME,
  28. requiresApiKey: true,
  29. async run(input) {
  30. const events = [];
  31. const paths = await makeCaseDirs(input);
  32. let app;
  33. let captureProxy;
  34. try {
  35. await prepareWorkspace({
  36. alphaSentinel: ALPHA_SENTINEL,
  37. betaSentinel: BETA_SENTINEL,
  38. fixtureDir: input.fixtureDir,
  39. workspace: paths.workspace,
  40. });
  41. captureProxy = await startCaptureProxy({ upstreamBaseURL: input.options.baseURL });
  42. await writeProviderConfig(paths.configPath, {
  43. baseURL: captureProxy.baseURL,
  44. model: input.options.model,
  45. providerId: DEFAULT_PROVIDER,
  46. providerName: "DeepSeek",
  47. storageDir: paths.storageDir,
  48. });
  49. app = await createApp({
  50. configPath: paths.configPath,
  51. modules: input.modules,
  52. runtimeConfig: buildRuntimeConfig({
  53. compact: {
  54. microcompact: {
  55. compactableToolNames: ["Bash"],
  56. enabled: true,
  57. keepRecentToolResults: MICROCOMPACT_KEEP_RECENT_TOOL_RESULTS,
  58. minTokenSavings: MICROCOMPACT_MIN_TOKEN_SAVINGS,
  59. thresholdTokens: MICROCOMPACT_THRESHOLD_TOKENS,
  60. },
  61. },
  62. workspace: paths.workspace,
  63. }),
  64. storageDir: paths.storageDir,
  65. });
  66. const result = await app.submitPrompt(buildPrompt(), {
  67. onEvent: async (event) => events.push(event),
  68. });
  69. assertMicrocompactOutcome({
  70. events,
  71. response: result.response,
  72. SessionEventType: input.modules.SessionEventType,
  73. });
  74. assertProviderCapture(captureProxy.records);
  75. await writeCaptureFile(captureProxy, paths.capturePath);
  76. await writeJsonFile(paths.eventsPath, events);
  77. const resultPayload = buildCaseResult({
  78. capturedProviderRequestCount: captureProxy.records.length,
  79. caseName: CASE_NAME,
  80. capturePath: paths.capturePath,
  81. events,
  82. eventsPath: paths.eventsPath,
  83. response: result.response,
  84. resultPath: paths.resultPath,
  85. traceId: result.traceId,
  86. });
  87. await writeJsonFile(paths.resultPath, resultPayload);
  88. return resultPayload;
  89. } catch (error) {
  90. await writeCaptureFile(captureProxy, paths.capturePath);
  91. await writeJsonFile(paths.eventsPath, events);
  92. throw error;
  93. } finally {
  94. await app?.close?.();
  95. await stopCaptureProxy(captureProxy);
  96. }
  97. },
  98. };
  99. function buildPrompt() {
  100. return [
  101. "Run this exact disposable Docker E2E procedure.",
  102. "Use the Bash tool exactly twice. Do not combine the commands into one Bash call.",
  103. "First Bash command: node scripts/emit-alpha.mjs",
  104. "Second Bash command: node scripts/emit-beta.mjs",
  105. "The scripts write marker files and print large stdout; that is intentional.",
  106. "Do not answer until both Bash tool results have been returned.",
  107. `Then answer with ${COMPLETION_MARKER} and mention that both scripts completed.`,
  108. ].join("\n");
  109. }
  110. function assertMicrocompactOutcome(input) {
  111. const { events, response, SessionEventType } = input;
  112. const toolResultEvents = events.filter((event) => event.type === SessionEventType.ToolCallResult);
  113. const microcompactEvents = events.filter(
  114. (event) => event.type === SessionEventType.MicrocompactBoundary,
  115. );
  116. const fullCompactEvents = compactBoundaries(events, SessionEventType);
  117. const finalContext = stringifyModelRequest(modelRequests(events, SessionEventType).at(-1));
  118. const hasAlpha = finalContext.includes(ALPHA_SENTINEL);
  119. const hasBeta = finalContext.includes(BETA_SENTINEL);
  120. assertCondition(toolResultEvents.length >= 2, "expected at least two tool results");
  121. assertCondition(microcompactEvents.length >= 1, "expected a microcompact boundary");
  122. assertCondition(fullCompactEvents.length === 0, "did not expect full compact boundary", {
  123. compactBoundaryCount: fullCompactEvents.length,
  124. });
  125. assertCondition(hasMicrocompactPlaceholder(finalContext), "expected microcompact placeholder", {
  126. finalContextPreview: finalContext.slice(0, 1000),
  127. });
  128. assertCondition(
  129. (hasAlpha || hasBeta) && hasAlpha !== hasBeta,
  130. "expected final request to keep only the most recent Bash sentinel",
  131. {
  132. hasAlpha,
  133. hasBeta,
  134. microcompactBoundaryCount: countEvents(events, SessionEventType.MicrocompactBoundary),
  135. },
  136. );
  137. assertCondition(response.includes(COMPLETION_MARKER), `expected ${COMPLETION_MARKER}`, {
  138. response,
  139. });
  140. }