remote-goal-telemetry.test.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { beforeEach, expect, it, vi } from "vitest";
  2. import { runActiveTargetContinuationLoop } from "../src/runtime/methods/target-continuation-loop.js";
  3. import { executeTargetContinuationCommand } from "../src/runtime/methods/target.js";
  4. import type { AgentRuntimeInternal } from "../src/runtime/internal.js";
  5. import { TELEMETRY_INPUT_ORIGIN_ATTRIBUTE } from "../src/runtime/methods/input-intent-persistence.js";
  6. vi.mock("../src/runtime/methods/target.js", () => ({ executeTargetContinuationCommand: vi.fn() }));
  7. it("goal 首轮 intent 清理后,后续自动轮仍保留手机因果来源", async () => {
  8. const execute = vi.mocked(executeTargetContinuationCommand);
  9. execute
  10. .mockResolvedValueOnce({} as never)
  11. .mockResolvedValueOnce({} as never)
  12. .mockResolvedValueOnce(null);
  13. const traceContext = { traceId: "trace", spanId: "span", attributes: {} };
  14. await runActiveTargetContinuationLoop.call(
  15. { runtimeCommandQueue: { hasPending: () => false }, config: {} } as AgentRuntimeInternal,
  16. {
  17. inputId: "mobile-command",
  18. trigger: "user-prompt",
  19. traceContext: traceContext as never,
  20. intent: { inputOrigin: "mobile" } as never,
  21. },
  22. );
  23. expect(execute).toHaveBeenCalledTimes(3);
  24. for (const [options] of execute.mock.calls)
  25. expect(options.traceContext?.attributes).toMatchObject({
  26. [TELEMETRY_INPUT_ORIGIN_ATTRIBUTE]: "mobile",
  27. });
  28. expect(execute.mock.calls[1][0].intent).toBeUndefined();
  29. expect(traceContext.attributes).toEqual({});
  30. });
  31. vi.mock("../src/runtime/methods/background-notifications.js", () => ({
  32. persistBackgroundTaskNotificationBatch: vi.fn(async () => ({
  33. messageId: "batch-message",
  34. text: "batch",
  35. })),
  36. shouldSuppressTaskNotificationRuntimeCommand: () => false,
  37. }));
  38. vi.mock("../src/runtime/off-peak/runtime.js", () => ({ refreshOffPeakWork: vi.fn() }));
  39. beforeEach(() => vi.mocked(executeTargetContinuationCommand).mockReset());
  40. it.each([
  41. ["desktop", "mobile"],
  42. [undefined, "mobile"],
  43. ["mobile", "desktop"],
  44. ["mobile", undefined],
  45. ["desktop", "desktop"],
  46. ])("后台合批 %s / %s 的来源贯穿后续两轮 goal", async (first, second) => {
  47. const { createRuntimeCommandQueue } = await import("../src/runtime/command-queue.js");
  48. const { drainRuntimeCommandQueue } =
  49. await import("../src/runtime/methods/runtime-command-queue.js");
  50. const queue = createRuntimeCommandQueue();
  51. const traces = [first, second].map((origin, index) => ({
  52. traceId: "batch-trace",
  53. spanId: `span-${index}`,
  54. attributes: origin ? { [TELEMETRY_INPUT_ORIGIN_ATTRIBUTE]: origin } : {},
  55. }));
  56. const originalTraces = structuredClone(traces);
  57. traces.forEach((traceContext, index) =>
  58. queue.enqueue({
  59. id: `notification-${index}`,
  60. mode: "task-notification",
  61. priority: "next",
  62. createdAt: new Date(),
  63. source: "background_task",
  64. text: "notification",
  65. traceContext,
  66. } as never),
  67. );
  68. const execute = vi.mocked(executeTargetContinuationCommand);
  69. execute
  70. .mockResolvedValueOnce({} as never)
  71. .mockResolvedValueOnce({} as never)
  72. .mockResolvedValueOnce(null);
  73. const executeTurnCommand = vi
  74. .fn<AgentRuntimeInternal["executeTurnCommand"]>()
  75. .mockResolvedValue({} as never);
  76. await drainRuntimeCommandQueue.call({
  77. runtimeCommandQueue: queue,
  78. config: {},
  79. executeTurnCommand,
  80. } as unknown as AgentRuntimeInternal);
  81. const expectedOrigin = [first, second].includes("mobile") ? "mobile" : "desktop";
  82. expect(executeTurnCommand).toHaveBeenCalledOnce();
  83. const batchOptions = executeTurnCommand.mock.calls[0]?.[2] as
  84. | { traceContext: { attributes: Record<string, string> } }
  85. | undefined;
  86. expect(batchOptions?.traceContext.attributes[TELEMETRY_INPUT_ORIGIN_ATTRIBUTE]).toBe(
  87. expectedOrigin,
  88. );
  89. expect(execute).toHaveBeenCalledTimes(3);
  90. for (const [options] of execute.mock.calls) {
  91. expect(options.traceContext?.attributes?.[TELEMETRY_INPUT_ORIGIN_ATTRIBUTE]).toBe(
  92. expectedOrigin,
  93. );
  94. }
  95. expect(traces).toEqual(originalTraces);
  96. });