Open History to see the last commit
1import { assert, describe, it } from "@effect/vitest";
2import type { ResourceTelemetryProcess } from "@t3tools/contracts";
3
4import { groupProcesses } from "./ProcessGroupMetrics.ts";
5
6const process = (
7 pid: number,
8 ppid: number,
9 name: string,
10 category: ResourceTelemetryProcess["category"] = "server-child",
11 command = name,
12) =>
13 ({
14 identity: { pid, startTimeMs: 1 },
15 ppid,
16 name,
17 command,
18 category,
19 residentBytes: pid * 1_000,
20 cpuPercent: pid,
21 }) as ResourceTelemetryProcess;
22
23describe("groupProcesses", () => {
24 it("sums each direct child of the server with its whole subtree", () => {
25 const totals = groupProcesses([
26 process(10, 1, "node", "server"),
27 process(11, 10, "t3-resource-mon", "resource-monitor"),
28 process(20, 10, "claude"),
29 process(21, 20, "bash"),
30 process(22, 21, "cargo"),
31 process(30, 10, "codex"),
32 process(40, 10, "zsh"),
33 process(50, 10, "chrome-headless"),
34 process(60, 10, "git", "server-child", "git checkout claude/fix"),
35 process(70, 99, "orphan"),
36 // An older Claude package runs as a script under node.
37 process(
38 80,
39 10,
40 "node",
41 "server-child",
42 "node /opt/node_modules/@anthropic-ai/claude-code/cli.js",
43 ),
44 process(90, 10, "node", "server-child", "node /srv/tools/report.js"),
45 ]);
46
47 assert.deepStrictEqual(Object.fromEntries(totals), {
48 server: { residentBytes: 10_000, cpuPercent: 10, count: 1 },
49 "resource-monitor": { residentBytes: 11_000, cpuPercent: 11, count: 1 },
50 // The agent's shell and the build it ran belong to the agent, not the terminal group.
51 claude: { residentBytes: 143_000, cpuPercent: 143, count: 4 },
52 codex: { residentBytes: 30_000, cpuPercent: 30, count: 1 },
53 terminal: { residentBytes: 40_000, cpuPercent: 40, count: 1 },
54 browser: { residentBytes: 50_000, cpuPercent: 50, count: 1 },
55 // `git` whose arguments mention claude, a parentless process, and a node script.
56 other: { residentBytes: 220_000, cpuPercent: 220, count: 3 },
57 });
58 });
59});