off-peak-cancel.test.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { expect, it, vi } from "vitest";
  2. import { createSessionId } from "@zcode/contracts";
  3. import { createTestAgentRuntime } from "./test-agent-runtime.js";
  4. import { createTestSessionEventStore } from "./test-event-store.js";
  5. import { createTestModelFactory } from "./test-runtime-model.js";
  6. import { createTestModelSelection } from "./test-model-selection.js";
  7. import { controlOffPeakRuntime, getOffPeakExecution } from "../src/runtime/off-peak/runtime.js";
  8. import { InMemoryRuntimeTaskRegistry } from "../src/runtime-task/registry.js";
  9. it.each(["completed", "cancelled"])(
  10. "旧执行已 %s 时迟到 cancel 不应停止新用户轮",
  11. async (terminal) => {
  12. const registry = new InMemoryRuntimeTaskRegistry();
  13. let finish!: () => void;
  14. const gate = new Promise<void>((r) => {
  15. finish = r;
  16. });
  17. let calls = 0;
  18. const runtime = createTestAgentRuntime(
  19. createSessionId("review-cancel"),
  20. {
  21. modelSelection: createTestModelSelection("user/normal"),
  22. titleGeneration: { enabled: false },
  23. workingDirectory: "/workspace",
  24. },
  25. {
  26. runtimeTaskRegistry: registry,
  27. eventStore: createTestSessionEventStore(),
  28. modelFactory: createTestModelFactory({
  29. async generateText() {
  30. if (calls === 0 && terminal === "cancelled")
  31. registry.register({
  32. taskId: "background",
  33. type: "local_bash",
  34. status: "running",
  35. isBackgrounded: true,
  36. startedAt: new Date(),
  37. } as never);
  38. if (++calls === 2) await gate;
  39. return {
  40. text: "done",
  41. finishReason: "stop",
  42. usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
  43. };
  44. },
  45. }),
  46. },
  47. );
  48. await runtime.executeTurn("idle", undefined, {
  49. offPeakTaskId: "task",
  50. intent: {
  51. sourceCommandId: "idle",
  52. kind: "sendText",
  53. modelSelection: createTestModelSelection("idle/model"),
  54. } as never,
  55. modelExecution: {
  56. selectionScope: "execution",
  57. offPeak: { executionId: "old", ticketId: "one", providerErrorCode: "3102" },
  58. requestDependencies: {
  59. requestAuth: { source: { resolve: async () => ({ apiKey: "test" }) } },
  60. },
  61. },
  62. });
  63. if (terminal === "cancelled") await getOffPeakExecution(runtime as never)!.cancel();
  64. await expect.poll(() => getOffPeakExecution(runtime as never)?.lifecycle.terminal).toBe(true);
  65. const pending = runtime.executeTurn("new user", undefined, {
  66. intent: {
  67. sourceCommandId: "new",
  68. admissionSeq: 2,
  69. kind: "sendText",
  70. modelSelection: createTestModelSelection("paid/new"),
  71. } as never,
  72. });
  73. await expect.poll(() => calls).toBe(2);
  74. const stop = vi.spyOn(runtime, "stopActiveForegroundExecution");
  75. try {
  76. await controlOffPeakRuntime(runtime as never, {
  77. sessionId: runtime.sessionId,
  78. executionId: "old",
  79. action: "cancel",
  80. });
  81. expect(stop).not.toHaveBeenCalled();
  82. finish();
  83. await expect(pending).resolves.toMatchObject({ response: "done" });
  84. } finally {
  85. finish();
  86. await pending.catch(() => undefined);
  87. }
  88. },
  89. );