managed-main.ts 4.5 KB

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