zcodeAgentService.resourceTelemetry.test.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
  2. import { tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. import type { AgentLaneResourceSample } from "@zcode/shared";
  5. import { afterEach, describe, expect, it } from "vitest";
  6. import { createZCodeAgentService } from "../src/zcode-agent/zcodeAgentService.js";
  7. const RESOURCE_SAMPLE_FAKE_AGENT = `
  8. let buffer = "";
  9. function send(value) { process.stdout.write(JSON.stringify(value) + "\\n"); }
  10. process.stdin.on("data", (chunk) => {
  11. buffer += chunk.toString("utf8");
  12. let newline;
  13. while ((newline = buffer.indexOf("\\n")) >= 0) {
  14. const line = buffer.slice(0, newline);
  15. buffer = buffer.slice(newline + 1);
  16. if (!line.trim()) continue;
  17. const message = JSON.parse(line);
  18. if (message.method !== "session/list" || message.id === undefined) continue;
  19. send({ id: message.id, result: { sessions: [] } });
  20. send({
  21. method: "process/resourceSample",
  22. params: {
  23. platform: "linux",
  24. arch: "x64",
  25. logicalCpuCount: 8,
  26. intervalMs: 60000,
  27. cpuCores: 1.5,
  28. cpuPercent: 18.75,
  29. rssKb: 131072,
  30. workspacePath: "/must-not-pass",
  31. },
  32. });
  33. send({
  34. method: "process/resourceSample",
  35. params: {
  36. platform: "linux",
  37. arch: "x64",
  38. logicalCpuCount: 8,
  39. intervalMs: 60000,
  40. cpuCores: 1.5,
  41. cpuPercent: 18.75,
  42. rssKb: 131072,
  43. lane: "chat",
  44. },
  45. });
  46. send({
  47. method: "process/resourceSample",
  48. params: {
  49. platform: "linux",
  50. arch: "x64",
  51. logicalCpuCount: 8,
  52. intervalMs: 60000,
  53. cpuCores: 1.5,
  54. cpuPercent: 18.75,
  55. rssKb: 131072,
  56. },
  57. });
  58. send({
  59. method: "process/resourceSample",
  60. params: {
  61. platform: "linux",
  62. arch: "x64",
  63. logicalCpuCount: 8,
  64. intervalMs: 60000,
  65. cpuCores: 1.5,
  66. cpuPercent: 18.75,
  67. rssKb: 131072,
  68. heapUsedKb: 20480,
  69. uptimeMinutes: 12,
  70. totalMemoryGb: 16,
  71. instanceToken: "9f2c4a1b7d0e5638",
  72. },
  73. });
  74. }
  75. });
  76. `;
  77. describe("ZCodeAgentService process resource telemetry", () => {
  78. const tempDirs: string[] = [];
  79. afterEach(() => {
  80. for (const dir of tempDirs.splice(0)) {
  81. rmSync(dir, { recursive: true, force: true });
  82. }
  83. });
  84. it("PRT-025 旧格式样本通过校验并分发,新字段透传,lane 由 services 按进程管理器打标", async () => {
  85. const workspacePath = mkdtempSync(join(tmpdir(), "zcode-resource-telemetry-"));
  86. tempDirs.push(workspacePath);
  87. const scriptPath = join(workspacePath, "resource-agent.cjs");
  88. writeFileSync(scriptPath, RESOURCE_SAMPLE_FAKE_AGENT);
  89. const service = createZCodeAgentService({
  90. commandResolver: () => ({ command: process.execPath, args: [scriptPath] }),
  91. });
  92. const samples: AgentLaneResourceSample[] = [];
  93. const subscription = service.onDynamicProcessResourceSample()((sample) => {
  94. samples.push(sample);
  95. });
  96. try {
  97. await service.listSessions({ workspacePath });
  98. await new Promise((resolve) => setTimeout(resolve, 20));
  99. const base = {
  100. platform: "linux",
  101. arch: "x64",
  102. logicalCpuCount: 8,
  103. intervalMs: 60_000,
  104. cpuCores: 1.5,
  105. cpuPercent: 18.75,
  106. rssKb: 131_072,
  107. } as const;
  108. // 第一条夹带 workspacePath、第二条自报 lane,两条都被协议层严格拒绝;
  109. // 通过的两条(旧格式与带新字段)都由 services 补上 chat lane。
  110. expect(samples).toEqual([
  111. { ...base, lane: "chat" },
  112. {
  113. ...base,
  114. lane: "chat",
  115. heapUsedKb: 20_480,
  116. uptimeMinutes: 12,
  117. totalMemoryGb: 16,
  118. instanceToken: "9f2c4a1b7d0e5638",
  119. },
  120. ]);
  121. } finally {
  122. subscription.dispose();
  123. await service.disposeAllAndWait();
  124. }
  125. });
  126. });