managed-main.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import { checkRetention } from "./managed-retention-checks.js";
  2. import { app, protocol } from "electron";
  3. import assert from "node:assert/strict";
  4. import { writeFile } from "node:fs/promises";
  5. import { join } from "node:path";
  6. import { PLUGIN_SANDBOX_PRIVILEGED_SCHEME } from "../../src/main/pluginSandbox/index.js";
  7. import { managedEnvironment } from "./managed-environment.js";
  8. import { checkPermissions } from "./managed-permission-checks.js";
  9. import { checkLifecycle } from "./managed-lifecycle-checks.js";
  10. import { checkManaged } from "./managed-checks.js";
  11. import { checkSessionReady } from "./managed-late-checks.js";
  12. const [root, profile, node, cli] = process.argv.slice(2) as [string, string, string, string];
  13. app.setPath("userData", profile);
  14. protocol.registerSchemesAsPrivileged([PLUGIN_SANDBOX_PRIVILEGED_SCHEME]);
  15. app.on("window-all-closed", () => {});
  16. app
  17. .whenReady()
  18. .then(async () => {
  19. const env = await managedEnvironment(root, node, cli);
  20. const checks: { name: string; ms: number }[] = [];
  21. const check = async (name: string, run: () => Promise<void>) => {
  22. const start = performance.now();
  23. let timer: ReturnType<typeof setTimeout>;
  24. try {
  25. await Promise.race([
  26. run(),
  27. new Promise<never>((_, reject) => {
  28. timer = setTimeout(
  29. () => reject(new Error(`Timed out: ${name}`)),
  30. name.includes("keep-warm") || name.startsWith("cold session") ? 45_000 : 20_000,
  31. );
  32. }),
  33. ]);
  34. } catch (error) {
  35. await writeFile(
  36. join(root, "managed-failure.json"),
  37. JSON.stringify(
  38. {
  39. name,
  40. requests: env.model.requests.history,
  41. approvals: env.agent.approvals.history,
  42. events: env.events.history.slice(-20),
  43. notifications: env.agent.notifications.history.slice(-12),
  44. },
  45. null,
  46. 2,
  47. ),
  48. );
  49. throw error;
  50. } finally {
  51. clearTimeout(timer!);
  52. }
  53. checks.push({ name, ms: performance.now() - start });
  54. process.stdout.write(`PASS ${name}\n`);
  55. };
  56. try {
  57. const scope = await env.agent.session(join(root, "managed-workspace"), "managed-delayed");
  58. const started = performance.now();
  59. const opening = env.createWindow(scope);
  60. void opening.catch(() => {});
  61. await check("App opening waits for MCP startup and runs without reload", async () => {
  62. await env.agent.events.wait(
  63. (e) => e.type === "initialize-waiting" && e.source === "managed",
  64. );
  65. const arrived = await env.events.wait(
  66. (e) => e.type === "bridge-arrived" && e.method === "prepareSandbox",
  67. );
  68. const owner = env.windows.find((w) => w.webContents.id === arrived.owner)!;
  69. assert.equal(
  70. (await owner.webContents.executeJavaScript("window.managedState()")).phase,
  71. "preparing",
  72. );
  73. env.agent.release("managed", "initialize");
  74. await opening;
  75. assert.equal(
  76. env.events.history.some((e) => e.type === "phase" && e.phase === "error"),
  77. false,
  78. );
  79. assert.equal(env.events.history.filter((e) => e.type === "registered").length, 1);
  80. });
  81. const win = await opening;
  82. const coldPageMs = performance.now() - started;
  83. await writeFile(
  84. join(root, "managed-inline.png"),
  85. (await win.webContents.capturePage()).toPNG(),
  86. );
  87. await check(
  88. "production React card and managed page establish one live conversation lease",
  89. async () => {
  90. const state = await win.webContents.executeJavaScript("window.managedState()");
  91. assert.equal(state.phase, "running");
  92. assert.equal(state.views, 1);
  93. assert.equal(state.listeners.frame, 1);
  94. assert.equal(env.events.history.filter((e) => e.type === "subscribed").length, 1);
  95. },
  96. );
  97. await checkManaged(env, win, scope, check);
  98. await checkSessionReady(env, root, check);
  99. await checkPermissions(env, root, check);
  100. await checkRetention(env, root, check);
  101. await checkLifecycle(env, win, scope, check);
  102. await writeFile(
  103. join(root, "managed-storage-identity.json"),
  104. JSON.stringify({ appIdentity: env.handles.get(win.webContents.id).instance.appIdentity }),
  105. );
  106. await writeFile(
  107. join(root, "managed-results.json"),
  108. JSON.stringify({ coldPageMs, checks }, null, 2),
  109. );
  110. } finally {
  111. await env.close();
  112. }
  113. app.exit(0);
  114. })
  115. .catch((error) => {
  116. process.stderr.write(String(error.stack ?? error) + "\n");
  117. app.exit(1);
  118. });