managed-retention-checks.ts 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import assert from "node:assert/strict";
  2. import { join } from "node:path";
  3. import type { managedEnvironment } from "./managed-environment.js";
  4. export async function checkRetention(
  5. env: Awaited<ReturnType<typeof managedEnvironment>>,
  6. root: string,
  7. check: (name: string, run: () => Promise<void>) => Promise<void>,
  8. ) {
  9. await check(
  10. "ordinary MCP page recycles after five minutes and restores submitted widget state",
  11. async () => {
  12. const scope = await env.agent.session(join(root, "non-retained"), "non-retained");
  13. const win = await env.createWindow(scope);
  14. const ui = (code: string) => win.webContents.executeJavaScript(code);
  15. const old = env.guests.get(win.webContents.id)!;
  16. assert.notEqual(env.handles.get(win.webContents.id).resourceMeta?.backgroundRetention, true);
  17. await env.evalPage(win, "window.unsaved='lost'; window.saveWidget({draft:'saved'})");
  18. await ui("window.setAnchors(false,false)");
  19. assert.equal(
  20. await ui("getComputedStyle(document.querySelector('[data-sandbox-page-root]')).display"),
  21. "none",
  22. );
  23. const destroyed = new Promise<void>((resolve) => old.once("destroyed", resolve));
  24. await ui("window.expireOffscreen()");
  25. await destroyed;
  26. assert.equal((await ui("window.managedState()")).actual.phase, "idle");
  27. await ui("window.setAnchors(true,false)");
  28. await ui("window.waitManagedPhase('running')");
  29. assert.notEqual(env.guests.get(win.webContents.id), old);
  30. assert.deepEqual(await env.evalPage(win, "window.readWidget()"), { draft: "saved" });
  31. assert.equal(await env.evalPage(win, "typeof window.unsaved"), "undefined");
  32. win.destroy();
  33. },
  34. );
  35. await check("reopening while recycle is in flight rebuilds the visible page", async () => {
  36. const scope = await env.agent.session(join(root, "non-retained-reopen"), "non-retained");
  37. const win = await env.createWindow(scope);
  38. const ui = (code: string) => win.webContents.executeJavaScript(code);
  39. const old = env.guests.get(win.webContents.id)!;
  40. await ui("window.setAnchors(false,false)");
  41. const gate = Promise.withResolvers<void>();
  42. env.gates.set("return:recycleInstance", gate.promise);
  43. const destroyed = new Promise<void>((resolve) => old.once("destroyed", resolve));
  44. try {
  45. await ui("window.expireOffscreen()");
  46. await env.events.wait(
  47. (event) =>
  48. event.type === "bridge" &&
  49. event.method === "recycleInstance" &&
  50. event.owner === win.webContents.id,
  51. );
  52. await ui("window.setAnchors(true,false)");
  53. } finally {
  54. env.gates.delete("return:recycleInstance");
  55. gate.resolve();
  56. }
  57. await destroyed;
  58. await ui("window.waitManagedPhase('running')");
  59. assert.notEqual(env.guests.get(win.webContents.id), old);
  60. assert.equal(await ui("document.querySelectorAll('webview').length"), 1);
  61. win.destroy();
  62. });
  63. await check(
  64. "five retained MCP pages evict one after thirty minutes, four remain alive",
  65. async () => {
  66. const scope = await env.agent.session(join(root, "retention-budget"), "retention-budget");
  67. const win = await env.createWindow(scope);
  68. const ui = (code: string) => win.webContents.executeJavaScript(code);
  69. assert.equal(env.handles.get(win.webContents.id).resourceMeta?.backgroundRetention, true);
  70. await ui("window.setAnchors(false,false)");
  71. const old = env.guests.get(win.webContents.id)!;
  72. const keys = await ui("window.addRetentionPages(4)");
  73. await ui("window.expireOffscreen()");
  74. assert.equal(old.isDestroyed(), false);
  75. assert.equal(await ui("document.querySelectorAll('webview').length"), 5);
  76. const destroyed = new Promise<void>((resolve) => old.once("destroyed", resolve));
  77. await ui("window.advanceRetention(30 * 60_000)");
  78. await destroyed;
  79. assert.deepEqual(
  80. await ui(`window.retentionStates(${JSON.stringify(keys)})`),
  81. Array(4).fill("running"),
  82. );
  83. assert.equal(await ui("document.querySelectorAll('webview').length"), 4);
  84. win.destroy();
  85. },
  86. );
  87. }