desktopAgentExceptionTelemetry.test.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { beforeEach, describe, expect, it, vi } from "vitest";
  2. import type { HostAgentProcessExceptionResponse } from "@zcode/shared";
  3. const { sendEvent, sendCustom } = vi.hoisted(() => ({ sendEvent: vi.fn(), sendCustom: vi.fn() }));
  4. vi.mock("@arms/rum-electron", () => ({ default: { sendEvent, sendCustom, setConfig: vi.fn() } }));
  5. vi.mock("electron", () => ({ BrowserWindow: {} }));
  6. vi.mock("../src/main/desktopCrashCapture.js", () => ({ registerCrashEventMonitor: vi.fn() }));
  7. vi.mock("../src/main/resourceManagerWindow.js", () => ({ getResourceManagerWindowId: vi.fn() }));
  8. const logger = { info: vi.fn(), warn: vi.fn(), error: vi.fn() };
  9. const event: HostAgentProcessExceptionResponse = {
  10. type: "agent-process-exception",
  11. pid: 42,
  12. provider: "glm",
  13. lane: "mcp-status",
  14. workspacePath: "/Users/alice/repo",
  15. runtimeGeneration: 1,
  16. runtimeInstanceId: "agent-test",
  17. diagnostic: {
  18. version: 1,
  19. errorId: "1f51b7dc-c52a-46ad-bc97-d2f984e6e3de",
  20. kind: "uncaughtException",
  21. origin: "uncaughtException",
  22. name: "TypeError",
  23. message: "boom token=secret sk-1234567890abcdefghijklmnopqrstuv /Users/alice/repo",
  24. stack:
  25. "TypeError: boom\n at run (/Users/alice/repo/cli.js:1:2)\n at next (C:\\Users\\alice\\lib.js:3:4)",
  26. occurredAt: 123456,
  27. },
  28. };
  29. async function telemetry() {
  30. const module = await import("../src/main/desktopStabilityTelemetry.js");
  31. module.configureDesktopStabilityTelemetry({
  32. deviceMid: "probe-device",
  33. platform: "darwin",
  34. appVersion: "test-version",
  35. armsEnv: "local",
  36. });
  37. return module;
  38. }
  39. describe("Agent JS exception telemetry", () => {
  40. beforeEach(() => {
  41. vi.resetModules();
  42. vi.clearAllMocks();
  43. sendEvent.mockReset();
  44. });
  45. it("reports an exception with stack and runtime attribution without waiting for exit", async () => {
  46. const { reportAgentProcessExceptionToArms } = await telemetry();
  47. reportAgentProcessExceptionToArms(event, logger);
  48. expect(sendEvent).toHaveBeenCalledOnce();
  49. const payload = sendEvent.mock.calls[0]![0];
  50. expect(payload).toMatchObject({
  51. event_type: "exception",
  52. type: "error",
  53. source: "uncaughtException",
  54. name: "TypeError",
  55. timestamp: 123456,
  56. properties: {
  57. process_role: "agent",
  58. error_id: event.diagnostic.errorId,
  59. runtime_instance_id: "agent-test",
  60. runtime_generation: "1",
  61. process_error_origin: "uncaughtException",
  62. lane: "mcp-status",
  63. app_version: "test-version",
  64. arms_env: "local",
  65. device_mid: "probe-device",
  66. },
  67. });
  68. expect(payload.stack).toContain("at run (<workspace>/cli.js:1:2)");
  69. expect(payload.stack).toContain("<home>");
  70. for (const secret of [
  71. "alice",
  72. "token=secret",
  73. "sk-1234567890abcdefghijklmnopqrstuv",
  74. "workspacePath",
  75. ]) {
  76. expect(JSON.stringify(payload)).not.toContain(secret);
  77. }
  78. expect(sendCustom).not.toHaveBeenCalled();
  79. expect(logger.error).not.toHaveBeenCalled();
  80. });
  81. it("deduplicates an occurrence, while preserving repeated errors and different runtimes", async () => {
  82. const { reportAgentProcessExceptionToArms } = await telemetry();
  83. reportAgentProcessExceptionToArms(event, logger);
  84. reportAgentProcessExceptionToArms(event, logger);
  85. reportAgentProcessExceptionToArms({ ...event, runtimeInstanceId: "agent-other" }, logger);
  86. reportAgentProcessExceptionToArms(
  87. {
  88. ...event,
  89. diagnostic: {
  90. ...event.diagnostic,
  91. errorId: "2f51b7dc-c52a-46ad-bc97-d2f984e6e3de",
  92. kind: "unhandledRejection",
  93. origin: "unhandledRejection",
  94. },
  95. },
  96. logger,
  97. );
  98. expect(sendEvent).toHaveBeenCalledTimes(3);
  99. expect(sendEvent.mock.calls[2]?.[0].source).toBe("unhandledRejection");
  100. });
  101. it("bounds details and does not mark failed submissions as delivered", async () => {
  102. const { reportAgentProcessExceptionToArms } = await telemetry();
  103. sendEvent.mockImplementationOnce(() => {
  104. throw new Error("SDK unavailable");
  105. });
  106. const longEvent = { ...event, diagnostic: { ...event.diagnostic, stack: "x".repeat(16_000) } };
  107. expect(() => reportAgentProcessExceptionToArms(longEvent, logger)).not.toThrow();
  108. reportAgentProcessExceptionToArms(longEvent, logger);
  109. expect(sendEvent).toHaveBeenCalledTimes(2);
  110. expect(sendEvent.mock.calls[1]?.[0].stack).toHaveLength(4000);
  111. expect(logger.warn).toHaveBeenCalledOnce();
  112. });
  113. it("keeps only a bounded dedup history", async () => {
  114. const { reportAgentProcessExceptionToArms } = await telemetry();
  115. for (let i = 0; i <= 1024; i++) {
  116. reportAgentProcessExceptionToArms({ ...event, runtimeInstanceId: `agent-${i}` }, logger);
  117. }
  118. reportAgentProcessExceptionToArms({ ...event, runtimeInstanceId: "agent-1024" }, logger);
  119. expect(sendEvent).toHaveBeenCalledTimes(1025);
  120. reportAgentProcessExceptionToArms({ ...event, runtimeInstanceId: "agent-0" }, logger);
  121. expect(sendEvent).toHaveBeenCalledTimes(1026);
  122. });
  123. });