sampling-extra-checks.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import assert from "node:assert/strict";
  2. import { randomUUID } from "node:crypto";
  3. import { join } from "node:path";
  4. import type { managedEnvironment } from "./managed-environment.js";
  5. type Environment = Awaited<ReturnType<typeof managedEnvironment>>;
  6. export async function checkSamplingExtras(
  7. env: Environment,
  8. root: string,
  9. check: (name: string, run: () => Promise<void>) => Promise<void>,
  10. ) {
  11. const request = {
  12. messages: [{ role: "user", content: { type: "text", text: "Explain 1/2" } }],
  13. maxTokens: 32,
  14. };
  15. const scope = await env.agent.session(join(root, "sampling-extra-workspace"), "sampling-extra");
  16. await env.agent.call("session/setModel", {
  17. sessionId: scope.sessionId,
  18. model: env.model.selection,
  19. });
  20. const win = await env.createWindow(scope, "sampling");
  21. const page = (code: string) => env.evalPage(win, code);
  22. const ui = (code: string) => win.webContents.executeJavaScript(code);
  23. const sample = (key: string) =>
  24. page(`window.sample(${JSON.stringify(key)},${JSON.stringify(request)})`);
  25. await check(
  26. "task permission modes reuse the same sampling path without extra approval",
  27. async () => {
  28. const before = env.agent.approvals.history.length;
  29. for (const mode of ["plan", "build", "edit", "yolo"]) {
  30. await env.agent.call("session/setMode", { sessionId: scope.sessionId, mode });
  31. assert.ok((await sample(`mode-${mode}`)).result);
  32. }
  33. assert.equal(env.agent.approvals.history.length, before);
  34. assert.equal(
  35. (await env.agent.call("session/read", { sessionId: scope.sessionId })).messages.length,
  36. 0,
  37. );
  38. const usage = await env.agent.call("session/usage", { sessionId: scope.sessionId });
  39. assert.ok(usage.modelRequestCount >= 4);
  40. assert.ok(usage.totalTokens >= 60);
  41. },
  42. );
  43. await check(
  44. "provider error reaches SDK once and releases the instance for the next call",
  45. async () => {
  46. env.model.plans.push({ key: "failed", delayed: false, status: 401 });
  47. assert.ok((await sample("failed")).error);
  48. await ui("window.waitSamplingIdle()");
  49. assert.equal(env.model.requests.history.filter((e) => e.plan?.key === "failed").length, 1);
  50. assert.ok((await sample("after-error")).result);
  51. },
  52. );
  53. await check(
  54. "sampling and the main task run concurrently with independent cancellation",
  55. async () => {
  56. env.model.plans.push({ key: "parallel-sampling", delayed: true });
  57. const sampling = sample("parallel-sampling");
  58. await env.model.requests.wait((e) => e.plan?.key === "parallel-sampling");
  59. const offset = env.agent.notifications.history.length;
  60. const ack = await env.agent.call("v4/command", {
  61. commandId: randomUUID(),
  62. clientId: "sampling-fixture",
  63. sessionId: scope.sessionId,
  64. type: "sendText",
  65. payload: {
  66. text: "A main conversation question",
  67. modelSelection: env.model.selection,
  68. mode: "build",
  69. },
  70. issuedAt: Date.now(),
  71. });
  72. assert.equal(ack.status, "accepted");
  73. const terminal = await env.agent.notifications.wait(
  74. (e) =>
  75. e.method === "v4/telemetry/event" &&
  76. e.params.kind === "turn.terminal" &&
  77. e.params.sessionId === scope.sessionId,
  78. offset,
  79. );
  80. assert.equal(terminal.params.status, "success");
  81. assert.equal((await ui("window.managedState()")).busy, true);
  82. const before = await env.agent.call("session/read", { sessionId: scope.sessionId });
  83. assert.ok(before.messages.length > 0);
  84. await page("window.cancelSample('parallel-sampling')");
  85. assert.ok((await sampling).error);
  86. await env.model.aborted.wait((key) => key === "parallel-sampling");
  87. assert.deepEqual(
  88. (await env.agent.call("session/read", { sessionId: scope.sessionId })).messages,
  89. before.messages,
  90. );
  91. },
  92. );
  93. await check(
  94. "closing task aborts sampling, rejects stale requests and releases keep-warm subscriptions",
  95. async () => {
  96. env.model.plans.push({ key: "close-task", delayed: true });
  97. const result = sample("close-task").catch(() => ({ error: "page closed" }));
  98. await env.model.requests.wait((e) => e.plan?.key === "close-task");
  99. const instance = env.handles.get(win.webContents.id).instance;
  100. await env.agent.call("session/close", { sessionId: scope.sessionId });
  101. assert.ok((await result).error);
  102. await env.model.aborted.wait((key) => key === "close-task");
  103. await assert.rejects(() =>
  104. env.agent.call("mcp/uiSampling", {
  105. ...scope,
  106. instance,
  107. operationId: "after-close",
  108. request,
  109. }),
  110. );
  111. await ui(
  112. "window.setAnchors(false,false).then(() => { window.clearManaged(); return window.waitConnectionsReleased(); })",
  113. );
  114. win.destroy();
  115. },
  116. );
  117. }