managed-model-checks.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import assert from "node:assert/strict";
  2. import { randomUUID } from "node:crypto";
  3. import type { BrowserWindow } from "electron";
  4. import type { managedEnvironment } from "./managed-environment.js";
  5. type Environment = Awaited<ReturnType<typeof managedEnvironment>>;
  6. export async function checkModel(
  7. env: Environment,
  8. win: BrowserWindow,
  9. scope: any,
  10. check: (name: string, run: () => Promise<void>) => Promise<void>,
  11. ) {
  12. const ui = (code: string) => win.webContents.executeJavaScript(code);
  13. const page = (code: string) => env.evalPage(win, code);
  14. const command = (type: string, payload: unknown) =>
  15. env.agent.call("v4/command", {
  16. commandId: randomUUID(),
  17. clientId: "fixture-client",
  18. sessionId: scope.sessionId,
  19. type,
  20. payload,
  21. issuedAt: Date.now(),
  22. });
  23. const permission = (key: string) => env.agent.approvals.wait((r) => r.params.input?.key === key);
  24. const terminal = (after: number) =>
  25. env.agent.notifications.wait(
  26. (e) =>
  27. e.method === "v4/telemetry/event" &&
  28. e.params.kind === "turn.terminal" &&
  29. e.params.sessionId === scope.sessionId,
  30. after,
  31. );
  32. async function begin(key: string) {
  33. const after = env.agent.notifications.history.length;
  34. env.model.plans.push({ key, delayed: true });
  35. const ack = await command("sendText", {
  36. text: `fixture ${key}`,
  37. modelSelection: env.model.selection,
  38. mode: "build",
  39. });
  40. assert.equal(ack.status, "accepted");
  41. const request = await env.model.requests.wait((r) => r.plan?.key === key);
  42. assert.ok(request.tool);
  43. return after;
  44. }
  45. await check(
  46. "model discovers page tool, duplicate live frames execute once across anchor switch",
  47. async () => {
  48. const offset = env.events.history.length;
  49. const after = await begin("model-page");
  50. await env.agent.respond(await permission("model-page"), "allow");
  51. await env.events.wait((e) => e.type === "claimAppToolCall" && e.result.accepted, offset);
  52. await page("window.waitPageCall('model-page')");
  53. const delivered = env.agent.notifications.history.find((e) =>
  54. e.params?.frame?.payload?.deltas?.some(
  55. (d: any) =>
  56. d.op === "pluginUi.appToolCall" && d.arguments?.key === "model-page" && !d.activity,
  57. ),
  58. );
  59. assert.ok(delivered);
  60. for (let n = 0; n < 5; n++)
  61. win.webContents.send("fixture-notification", { kind: "frame", value: delivered.params });
  62. assert.equal((await ui("window.managedState()")).busy, true);
  63. await ui("window.setAnchors(false, true)");
  64. await page("window.releasePageCall('model-page')");
  65. const result = await env.events.wait((e) => e.type === "resolveAppToolCall", offset);
  66. assert.equal(result.params.result.structuredContent.key, "model-page");
  67. assert.equal(result.result.accepted, true);
  68. assert.deepEqual(await page("window.pageCalls"), [{ key: "model-page", aborted: false }]);
  69. assert.equal(
  70. env.events.history.slice(offset).filter((e) => e.type === "claimAppToolCall").length,
  71. 1,
  72. );
  73. assert.equal((await terminal(after)).params.status, "success");
  74. },
  75. );
  76. await check(
  77. "model cancellation while approval pending rejects late allow without executing page",
  78. async () => {
  79. const after = await begin("model-approval-cancel");
  80. const pending = await permission("model-approval-cancel");
  81. await command("stop", {});
  82. await env.agent.respond(pending, "allow");
  83. await terminal(after);
  84. assert.equal(
  85. await page("window.pageCalls.filter(c => c.key === 'model-approval-cancel').length"),
  86. 0,
  87. );
  88. },
  89. );
  90. await check(
  91. "model cancellation before claim denies stale claim and never invokes SDK handler",
  92. async () => {
  93. const gate = Promise.withResolvers<void>();
  94. env.gates.set("claimAppToolCall", gate.promise);
  95. const offset = env.events.history.length;
  96. const after = await begin("model-claim-cancel");
  97. await env.agent.respond(await permission("model-claim-cancel"), "allow");
  98. await env.events.wait(
  99. (e) => e.type === "app-tools-arrived" && e.method === "claimAppToolCall",
  100. offset,
  101. );
  102. await command("stop", {});
  103. env.gates.delete("claimAppToolCall");
  104. gate.resolve();
  105. const claim = await env.events.wait((e) => e.type === "claimAppToolCall", offset);
  106. assert.equal(claim.result.accepted, false);
  107. await terminal(after);
  108. assert.equal(
  109. await page("window.pageCalls.filter(c => c.key === 'model-claim-cancel').length"),
  110. 0,
  111. );
  112. },
  113. );
  114. await check(
  115. "model cancellation during page execution reaches SDK AbortSignal and rejects late result",
  116. async () => {
  117. const offset = env.events.history.length;
  118. const after = await begin("model-execution-cancel");
  119. await env.agent.respond(await permission("model-execution-cancel"), "allow");
  120. await page("window.waitPageCall('model-execution-cancel')");
  121. await command("stop", {});
  122. await page("window.waitPageAbort('model-execution-cancel')");
  123. const result = await env.events.wait((e) => e.type === "resolveAppToolCall", offset);
  124. assert.equal(result.result.accepted, false);
  125. await terminal(after);
  126. await page("window.releasePageCall('model-execution-cancel')");
  127. assert.equal(
  128. await page("window.pageCalls.filter(c => c.key === 'model-execution-cancel').length"),
  129. 1,
  130. );
  131. assert.equal((await ui("window.managedState()")).busy, false);
  132. },
  133. );
  134. await check("model target stays bound when retry replaces page during approval", async () => {
  135. const old = env.handles.get(win.webContents.id).instance;
  136. const after = await begin("model-replaced");
  137. const pending = await permission("model-replaced");
  138. const offset = env.events.history.length;
  139. await ui("window.retryManaged()");
  140. await env.events.wait((e) => e.type === "phase" && e.phase === "running", offset);
  141. await page("window.fixtureReady");
  142. await env.agent.respond(pending, "allow");
  143. await terminal(after);
  144. assert.notEqual(env.handles.get(win.webContents.id).instance.token, old.token);
  145. assert.deepEqual(await page("window.pageCalls"), []);
  146. });
  147. }