managed-main.ts 4.3 KB

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