mcp-telemetry.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. import { describe, expect, it, vi } from "vitest";
  2. import { createMcpTelemetryTracker } from "../src/mcp/telemetry.js";
  3. function memoryProbe(rssKb: number, treeScope: "process_tree" | "direct_process" = "process_tree") {
  4. return {
  5. treeScope,
  6. reset() {},
  7. sampleProcessGroup: async () => undefined,
  8. sampleProcessTrees: async () => new Map([[42_424, [{ pid: 42_424, rssKb }]]]),
  9. };
  10. }
  11. describe("MCP telemetry tracker", () => {
  12. it("reports one successful stdio process start with safe stable and instance identities", () => {
  13. const onEvent = vi.fn();
  14. const tracker = createMcpTelemetryTracker({
  15. arch: "arm64",
  16. idSalt: "device-a",
  17. now: () => 1_000,
  18. onEvent,
  19. platform: "darwin",
  20. randomId: () => "mcp-instance-1",
  21. });
  22. tracker.registerConnection({
  23. connectionId: "connection-1",
  24. isolation: "session",
  25. serverName: "private-company-server",
  26. });
  27. tracker.acquireOwner({
  28. connectionId: "connection-1",
  29. ownerId: "lease-1",
  30. sessionId: "session-1",
  31. });
  32. expect(tracker.recordProcessStarted({ connectionId: "connection-1", pid: 42_424 })).toEqual({
  33. mcpId: expect.stringMatching(/^custom:[a-f0-9]{12}$/),
  34. mcpInstanceId: "mcp-instance-1",
  35. });
  36. expect(onEvent).toHaveBeenCalledOnce();
  37. expect(onEvent).toHaveBeenCalledWith({
  38. arch: "arm64",
  39. kind: "process_start",
  40. mcpId: expect.stringMatching(/^custom:[a-f0-9]{12}$/),
  41. mcpInstanceId: "mcp-instance-1",
  42. mcpIsolation: "session",
  43. mcpSource: "custom",
  44. occurredAt: 1_000,
  45. platform: "darwin",
  46. });
  47. expect(JSON.stringify(onEvent.mock.calls)).not.toContain("private-company-server");
  48. });
  49. it("keeps an official MCP identity readable while escaping unsafe characters", () => {
  50. const onEvent = vi.fn();
  51. const tracker = createMcpTelemetryTracker({
  52. idSalt: "device-a",
  53. onEvent,
  54. randomId: () => "mcp-instance-1",
  55. });
  56. tracker.registerConnection({
  57. connectionId: "connection-1",
  58. isolation: "session",
  59. serverName: "plugin:document-skills:image search/高清!",
  60. source: "builtin",
  61. });
  62. expect(tracker.recordProcessStarted({ connectionId: "connection-1", pid: 42_424 })).toEqual({
  63. mcpId: "builtin:document-skills:image%20search%2F%E9%AB%98%E6%B8%85%21",
  64. mcpInstanceId: "mcp-instance-1",
  65. });
  66. expect(onEvent).toHaveBeenCalledWith(
  67. expect.objectContaining({
  68. mcpId: "builtin:document-skills:image%20search%2F%E9%AB%98%E6%B8%85%21",
  69. mcpSource: "builtin",
  70. }),
  71. );
  72. });
  73. it("does not let malformed Unicode in an official MCP name break process startup", () => {
  74. const tracker = createMcpTelemetryTracker({
  75. idSalt: "device-a",
  76. onEvent: vi.fn(),
  77. randomId: () => "mcp-instance-1",
  78. });
  79. tracker.registerConnection({
  80. connectionId: "connection-1",
  81. isolation: "session",
  82. serverName: "plugin:official-plugin:broken-\uD800-name",
  83. source: "builtin",
  84. });
  85. expect(tracker.recordProcessStarted({ connectionId: "connection-1", pid: 42_424 })).toEqual({
  86. mcpId: "builtin:official-plugin:broken-%EF%BF%BD-name",
  87. mcpInstanceId: "mcp-instance-1",
  88. });
  89. });
  90. it("reports one unexpected crash with the same instance identity and uptime", () => {
  91. let now = 1_000;
  92. const onEvent = vi.fn();
  93. const tracker = createMcpTelemetryTracker({
  94. arch: "x64",
  95. idSalt: "device-a",
  96. now: () => now,
  97. onEvent,
  98. platform: "linux",
  99. randomId: () => "mcp-instance-1",
  100. });
  101. tracker.registerConnection({
  102. connectionId: "connection-1",
  103. isolation: "workspace",
  104. serverName: "node_repl",
  105. });
  106. tracker.acquireOwner({
  107. connectionId: "connection-1",
  108. ownerId: "lease-a",
  109. sessionId: "session-a",
  110. });
  111. tracker.acquireOwner({
  112. connectionId: "connection-1",
  113. ownerId: "lease-b",
  114. sessionId: "session-b",
  115. });
  116. tracker.recordProcessStarted({ connectionId: "connection-1", pid: 42_424 });
  117. now = 11_000;
  118. tracker.recordProcessCrashed({
  119. connectionId: "connection-1",
  120. exitCode: 1,
  121. signal: null,
  122. });
  123. tracker.recordProcessCrashed({
  124. connectionId: "connection-1",
  125. exitCode: 1,
  126. signal: null,
  127. });
  128. expect(onEvent).toHaveBeenCalledTimes(2);
  129. expect(onEvent).toHaveBeenLastCalledWith({
  130. affectedSessionCount: 2,
  131. arch: "x64",
  132. exitCode: 1,
  133. kind: "process_crash",
  134. mcpId: "builtin:node_repl",
  135. mcpInstanceId: "mcp-instance-1",
  136. mcpIsolation: "workspace",
  137. mcpSource: "builtin",
  138. occurredAt: 11_000,
  139. platform: "linux",
  140. signal: null,
  141. uptimeMs: 10_000,
  142. });
  143. });
  144. it("marks a still-running process as suspected orphan only after more than 60 seconds unowned", async () => {
  145. let now = 300_000;
  146. const onEvent = vi.fn();
  147. const tracker = createMcpTelemetryTracker({
  148. idSalt: "device-a",
  149. now: () => now,
  150. onEvent,
  151. randomId: () => "mcp-instance-1",
  152. processProbe: memoryProbe(512_000),
  153. });
  154. tracker.registerConnection({
  155. connectionId: "connection-1",
  156. isolation: "session",
  157. serverName: "private-server",
  158. });
  159. tracker.acquireOwner({
  160. connectionId: "connection-1",
  161. ownerId: "lease-1",
  162. sessionId: "session-1",
  163. });
  164. tracker.recordProcessStarted({ connectionId: "connection-1", pid: 42_424 });
  165. onEvent.mockClear();
  166. tracker.releaseOwner({ connectionId: "connection-1", ownerId: "lease-1" });
  167. now = 360_000;
  168. await tracker.sampleNow();
  169. expect(onEvent).toHaveBeenLastCalledWith(
  170. expect.objectContaining({
  171. kind: "memory",
  172. memoryKb: 512_000,
  173. memoryScope: "process_tree",
  174. orphanSuspected: false,
  175. ownerSessionCount: 0,
  176. unownedSeconds: 60,
  177. }),
  178. );
  179. now = 360_001;
  180. await tracker.sampleNow();
  181. expect(onEvent).toHaveBeenLastCalledWith(
  182. expect.objectContaining({
  183. kind: "memory",
  184. orphanSuspected: true,
  185. unownedSeconds: 60.001,
  186. }),
  187. );
  188. });
  189. it("does not mark a process owned by a non-session control-plane lease as orphaned", async () => {
  190. const onEvent = vi.fn();
  191. const tracker = createMcpTelemetryTracker({
  192. idSalt: "device-a",
  193. now: () => 600_000,
  194. onEvent,
  195. randomId: () => "mcp-instance-1",
  196. processProbe: memoryProbe(64_000, "direct_process"),
  197. });
  198. tracker.registerConnection({
  199. connectionId: "connection-1",
  200. isolation: "session",
  201. serverName: "settings-server",
  202. });
  203. tracker.acquireOwner({ connectionId: "connection-1", ownerId: "protocol-settings" });
  204. tracker.recordProcessStarted({ connectionId: "connection-1", pid: 42_424 });
  205. onEvent.mockClear();
  206. await tracker.sampleNow();
  207. expect(onEvent).toHaveBeenCalledWith(
  208. expect.objectContaining({
  209. kind: "memory",
  210. orphanSuspected: false,
  211. ownerSessionCount: 0,
  212. unownedSeconds: 0,
  213. }),
  214. );
  215. });
  216. it("owns one unref five-minute resource timer", () => {
  217. const handle = { unref: vi.fn() };
  218. const setInterval = vi.fn(() => handle);
  219. const clearInterval = vi.fn();
  220. const tracker = createMcpTelemetryTracker({
  221. idSalt: "device-a",
  222. onEvent: vi.fn(),
  223. timer: { clearInterval, setInterval },
  224. });
  225. tracker.start();
  226. tracker.start();
  227. tracker.stop();
  228. expect(setInterval).toHaveBeenCalledOnce();
  229. expect(setInterval).toHaveBeenCalledWith(expect.any(Function), 5 * 60_000);
  230. expect(handle.unref).toHaveBeenCalledOnce();
  231. expect(clearInterval).toHaveBeenCalledOnce();
  232. expect(clearInterval).toHaveBeenCalledWith(handle);
  233. });
  234. it("keeps every telemetry sink failure outside MCP lifecycle behavior", async () => {
  235. const tracker = createMcpTelemetryTracker({
  236. idSalt: "device-a",
  237. onEvent: () => {
  238. throw new Error("transport closed");
  239. },
  240. processProbe: memoryProbe(1),
  241. });
  242. tracker.registerConnection({
  243. connectionId: "connection-1",
  244. isolation: "session",
  245. serverName: "node_repl",
  246. });
  247. tracker.acquireOwner({
  248. connectionId: "connection-1",
  249. ownerId: "lease-1",
  250. sessionId: "session-1",
  251. });
  252. expect(() =>
  253. tracker.recordProcessStarted({ connectionId: "connection-1", pid: 42_424 }),
  254. ).not.toThrow();
  255. await expect(tracker.sampleNow()).resolves.toBeUndefined();
  256. expect(() =>
  257. tracker.recordProcessCrashed({
  258. connectionId: "connection-1",
  259. exitCode: 1,
  260. signal: null,
  261. }),
  262. ).not.toThrow();
  263. expect(() =>
  264. tracker.recordSessionStartup({
  265. configuredCount: 1,
  266. connectedCount: 1,
  267. failedCount: 0,
  268. processCount: 1,
  269. sessionId: "session-1",
  270. }),
  271. ).not.toThrow();
  272. });
  273. it("forgets a confirmed closed process so a reused PID is not reported as orphaned", async () => {
  274. const onEvent = vi.fn();
  275. const tracker = createMcpTelemetryTracker({
  276. idSalt: "device-a",
  277. onEvent,
  278. processProbe: memoryProbe(128_000),
  279. });
  280. tracker.registerConnection({
  281. connectionId: "connection-1",
  282. isolation: "session",
  283. serverName: "node_repl",
  284. });
  285. tracker.acquireOwner({
  286. connectionId: "connection-1",
  287. ownerId: "lease-1",
  288. sessionId: "session-1",
  289. });
  290. tracker.recordProcessStarted({ connectionId: "connection-1", pid: 42_424 });
  291. tracker.releaseOwner({ connectionId: "connection-1", ownerId: "lease-1" });
  292. tracker.recordProcessClosed({ connectionId: "connection-1" });
  293. onEvent.mockClear();
  294. await tracker.sampleNow();
  295. expect(onEvent).not.toHaveBeenCalled();
  296. });
  297. it("listProcesses 只回报仍有存活进程记录的连接,并带明文 serverName 与插件名", () => {
  298. const tracker = createMcpTelemetryTracker({
  299. idSalt: "device-a",
  300. onEvent: vi.fn(),
  301. randomId: () => "mcp-instance-1",
  302. });
  303. tracker.registerConnection({
  304. connectionId: "plugin-connection",
  305. isolation: "workspace",
  306. serverName: "plugin:computer-use:computer-use",
  307. source: "builtin",
  308. });
  309. tracker.registerConnection({
  310. connectionId: "custom-connection",
  311. isolation: "session",
  312. serverName: "my-custom-server",
  313. });
  314. tracker.registerConnection({
  315. connectionId: "idle-connection",
  316. isolation: "session",
  317. serverName: "plugin:community-plugin:tools",
  318. source: "plugin",
  319. });
  320. tracker.recordProcessStarted({ connectionId: "plugin-connection", pid: 101 });
  321. tracker.recordProcessStarted({ connectionId: "custom-connection", pid: 102 });
  322. expect(tracker.listProcesses()).toEqual([
  323. {
  324. pid: 101,
  325. serverName: "plugin:computer-use:computer-use",
  326. mcpSource: "builtin",
  327. pluginName: "computer-use",
  328. },
  329. { pid: 102, serverName: "my-custom-server", mcpSource: "custom" },
  330. ]);
  331. tracker.recordProcessClosed({ connectionId: "plugin-connection" });
  332. expect(tracker.listProcesses()).toEqual([
  333. { pid: 102, serverName: "my-custom-server", mcpSource: "custom" },
  334. ]);
  335. });
  336. });