runtime-queue-auto-drain.test.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import { describe, expect, it, vi } from "vitest";
  2. import {
  3. completeExternalQueueDrain,
  4. drainPendingInput,
  5. hasInlineGuidePendingInput,
  6. } from "../src/runtime/methods/steering.js";
  7. describe("guide 行内 drain 的外层 FIFO 门", () => {
  8. it("外层普通队列恢复期间阻止 guide 越过旧项,消费到空后恢复 tool-batch drain", () => {
  9. const activeTurn = {
  10. pendingInputs: [{ id: "new-running-input", commandKind: undefined, delivery: "guide" }],
  11. };
  12. const runtime = {
  13. activeTurn,
  14. pendingInputReservations: new Map<string, string>(),
  15. queueExternalDrainActive: true,
  16. };
  17. expect(hasInlineGuidePendingInput.call(runtime as never, activeTurn as never)).toBe(false);
  18. completeExternalQueueDrain.call(runtime as never);
  19. expect(hasInlineGuidePendingInput.call(runtime as never, activeTurn as never)).toBe(true);
  20. });
  21. it("普通 future queue 在前时仍允许后续 guide 走当前轮", () => {
  22. const activeTurn = {
  23. pendingInputs: [
  24. { id: "future-queue", commandKind: undefined, delivery: "queue" },
  25. { id: "current-guide", commandKind: undefined, delivery: "guide" },
  26. ],
  27. };
  28. const runtime = {
  29. activeTurn,
  30. pendingInputReservations: new Map<string, string>(),
  31. queueExternalDrainActive: false,
  32. };
  33. expect(hasInlineGuidePendingInput.call(runtime as never, activeTurn as never)).toBe(true);
  34. });
  35. it("行内 drain 只取最早 guide,并把普通 future queue 留在原位", async () => {
  36. const futureQueue = {
  37. id: "future-queue",
  38. input: "future work",
  39. queuedAt: new Date(0),
  40. delivery: "queue",
  41. };
  42. const currentGuide = {
  43. id: "current-guide",
  44. input: "guide current work",
  45. queuedAt: new Date(0),
  46. delivery: "guide",
  47. };
  48. const activeTurn = {
  49. pendingInputs: [futureQueue, currentGuide],
  50. turnId: "turn-current",
  51. };
  52. const appendedEvents: Array<{ payload: { pendingInputIds?: string[] } }> = [];
  53. const runtime = {
  54. activeTurn,
  55. appendEvent: vi.fn(async (event) => appendedEvents.push(event)),
  56. createEvent: vi.fn((_type, payload) => ({ payload })),
  57. messageHistory: { addEntries: vi.fn() },
  58. pendingInputReservations: new Map<string, string>(),
  59. persistUserPrompt: vi.fn(async () => undefined),
  60. sessionId: "session-current",
  61. workingDirectory: "/workspace",
  62. };
  63. await drainPendingInput.call(runtime as never, {
  64. activeTurn: activeTurn as never,
  65. events: [],
  66. traceContext: { traceId: "trace-current" } as never,
  67. });
  68. expect(activeTurn.pendingInputs).toEqual([futureQueue]);
  69. expect(runtime.messageHistory.addEntries).toHaveBeenCalledOnce();
  70. expect(JSON.stringify(runtime.messageHistory.addEntries.mock.calls[0])).toContain(
  71. "guide current work",
  72. );
  73. expect(JSON.stringify(runtime.messageHistory.addEntries.mock.calls[0])).not.toContain(
  74. "future work",
  75. );
  76. expect(appendedEvents).toEqual([
  77. expect.objectContaining({
  78. payload: expect.objectContaining({ pendingInputIds: ["current-guide"] }),
  79. }),
  80. ]);
  81. });
  82. });