run.mjs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #!/usr/bin/env node
  2. import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
  3. import { tmpdir } from "node:os";
  4. import { join, resolve } from "node:path";
  5. import {
  6. startScriptedProvider,
  7. stopScriptedProvider,
  8. writeScriptedCaptureFile,
  9. } from "../compact-microcompact/scripted-provider.mjs";
  10. import { verifyMemoryE2EEvidence, writeMemoryE2EArtifacts } from "./memory-e2e-evidence.mjs";
  11. import {
  12. createMemoryExtractionCloseBarrier,
  13. createProviderHandler,
  14. } from "./memory-e2e-provider.mjs";
  15. import {
  16. createScenarioPaths,
  17. loadZCodeModules,
  18. runMemoryScenarios,
  19. } from "./memory-e2e-runtime.mjs";
  20. async function main() {
  21. const options = parseArgs(process.argv.slice(2));
  22. const artifactsRoot = options.artifacts
  23. ? resolve(options.artifacts)
  24. : await mkdtemp(join(tmpdir(), "zcode-memory-e2e-artifacts-"));
  25. const runRoot = await mkdtemp(join(tmpdir(), "zcode-memory-e2e-run-"));
  26. await mkdir(artifactsRoot, { recursive: true });
  27. const modules = await loadZCodeModules();
  28. const retrievalBranch =
  29. modules.projectMemoryRetrievalBranch.ACTIVE_PROJECT_MEMORY_RETRIEVAL_BRANCH;
  30. const paths = await createScenarioPaths(runRoot, modules);
  31. const closeBarrier = createMemoryExtractionCloseBarrier();
  32. const provider = await startScriptedProvider({
  33. name: "memory-e2e",
  34. handler: createProviderHandler({ closeBarrier, paths, retrievalBranch }),
  35. });
  36. try {
  37. const scenarioResults = await runMemoryScenarios({
  38. closeBarrier,
  39. modules,
  40. paths,
  41. provider,
  42. });
  43. const evidence = await verifyMemoryE2EEvidence({
  44. paths,
  45. provider,
  46. retrievalBranch,
  47. scenarioResults,
  48. });
  49. const result = {
  50. artifactsRoot,
  51. assertions: evidence.assertions,
  52. categoryCounts: evidence.categoryCounts,
  53. retrievalBranch,
  54. status: "passed",
  55. };
  56. await writeMemoryE2EArtifacts({ artifactsRoot, evidence, paths, provider, result });
  57. console.log(JSON.stringify(result, null, 2));
  58. } catch (error) {
  59. const result = {
  60. artifactsRoot,
  61. error: error instanceof Error ? (error.stack ?? error.message) : String(error),
  62. status: "failed",
  63. };
  64. await writeFile(join(artifactsRoot, "result.json"), `${JSON.stringify(result, null, 2)}\n`);
  65. await writeScriptedCaptureFile(provider, join(artifactsRoot, "provider-capture.json"));
  66. throw error;
  67. } finally {
  68. await stopScriptedProvider(provider);
  69. if (!options.keepRunRoot) {
  70. await rm(runRoot, { force: true, recursive: true });
  71. } else {
  72. console.error(`Memory E2E run root kept at: ${runRoot}`);
  73. }
  74. }
  75. }
  76. function parseArgs(args) {
  77. const result = { artifacts: undefined, keepRunRoot: false };
  78. for (let index = 0; index < args.length; index += 1) {
  79. const arg = args[index];
  80. if (arg === "--artifacts") {
  81. result.artifacts = args[index + 1];
  82. index += 1;
  83. continue;
  84. }
  85. if (arg === "--keep-run-root") {
  86. result.keepRunRoot = true;
  87. continue;
  88. }
  89. throw new Error(`Unknown Memory E2E argument: ${arg}`);
  90. }
  91. return result;
  92. }
  93. main().catch((error) => {
  94. console.error(error);
  95. process.exitCode = 1;
  96. });