The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { assert, describe, it } from "@effect/vitest"; |
| 2 | import type { ResourceTelemetryProcess, ResourceTelemetrySnapshot } from "@t3tools/contracts"; |
| 3 | import * as Clock from "effect/Clock"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as Layer from "effect/Layer"; |
| 6 | import * as Logger from "effect/Logger"; |
| 7 | import * as References from "effect/References"; |
| 8 | import * as Ref from "effect/Ref"; |
| 9 | import * as TestClock from "effect/testing/TestClock"; |
| 10 | |
| 11 | import * as ServerConfig from "../config.ts"; |
| 12 | import * as ProcessGroupMetrics from "./ProcessGroupMetrics.ts"; |
| 13 | import * as ResourceTelemetry from "./ResourceTelemetry.ts"; |
| 14 | |
| 15 | const { groupProcesses } = ProcessGroupMetrics; |
| 16 | |
| 17 | const process = ( |
| 18 | pid: number, |
| 19 | ppid: number, |
| 20 | name: string, |
| 21 | category: ResourceTelemetryProcess["category"] = "server-child", |
| 22 | command = name, |
| 23 | ) => |
| 24 | ({ |
| 25 | identity: { pid, startTimeMs: 1 }, |
| 26 | ppid, |
| 27 | name, |
| 28 | command, |
| 29 | category, |
| 30 | residentBytes: pid * 1_000, |
| 31 | cpuPercent: pid, |
| 32 | }) as ResourceTelemetryProcess; |
| 33 | |
| 34 | describe("groupProcesses", () => { |
| 35 | it("sums each direct child of the server with its whole subtree", () => { |
| 36 | const totals = groupProcesses([ |
| 37 | process(10, 1, "node", "server"), |
| 38 | process(11, 10, "t3-resource-mon", "resource-monitor"), |
| 39 | process(20, 10, "claude"), |
| 40 | process(21, 20, "bash"), |
| 41 | process(22, 21, "cargo"), |
| 42 | process(30, 10, "codex"), |
| 43 | process(40, 10, "zsh"), |
| 44 | process(50, 10, "chrome-headless"), |
| 45 | process(60, 10, "git", "server-child", "git checkout claude/fix"), |
| 46 | process(70, 99, "orphan"), |
| 47 | // An older Claude package runs as a script under node. |
| 48 | process( |
| 49 | 80, |
| 50 | 10, |
| 51 | "node", |
| 52 | "server-child", |
| 53 | "node /opt/node_modules/@anthropic-ai/claude-code/cli.js", |
| 54 | ), |
| 55 | process(90, 10, "node", "server-child", "node /srv/tools/report.js"), |
| 56 | ]); |
| 57 | |
| 58 | assert.deepStrictEqual(Object.fromEntries(totals), { |
| 59 | server: { residentBytes: 10_000, cpuPercent: 10, count: 1 }, |
| 60 | "resource-monitor": { residentBytes: 11_000, cpuPercent: 11, count: 1 }, |
| 61 | // The agent's shell and the build it ran belong to the agent, not the terminal group. |
| 62 | claude: { residentBytes: 143_000, cpuPercent: 143, count: 4 }, |
| 63 | codex: { residentBytes: 30_000, cpuPercent: 30, count: 1 }, |
| 64 | terminal: { residentBytes: 40_000, cpuPercent: 40, count: 1 }, |
| 65 | browser: { residentBytes: 50_000, cpuPercent: 50, count: 1 }, |
| 66 | // `git` whose arguments mention claude, a parentless process, and a node script. |
| 67 | other: { residentBytes: 220_000, cpuPercent: 220, count: 3 }, |
| 68 | }); |
| 69 | }); |
| 70 | }); |
| 71 | |
| 72 | describe("ProcessGroupMetrics.layer", () => { |
| 73 | const notRunning = new ResourceTelemetry.ResourceTelemetryRefreshFailed({ |
| 74 | operation: "refresh", |
| 75 | cause: new Error("Resource monitor is unavailable: sidecar is not running"), |
| 76 | }); |
| 77 | const logsAfter = ( |
| 78 | refresh: ResourceTelemetry.ResourceTelemetry["Service"]["refresh"], |
| 79 | afterStart: Effect.Effect<void>, |
| 80 | ) => { |
| 81 | const logs: Array<string> = []; |
| 82 | const logger = Logger.make(({ logLevel, message }) => { |
| 83 | logs.push(`${logLevel.toUpperCase()} ${String([message].flat()[0])}`); |
| 84 | }); |
| 85 | const telemetry = { refresh } as unknown as ResourceTelemetry.ResourceTelemetry["Service"]; |
| 86 | return Effect.gen(function* () { |
| 87 | yield* Layer.build( |
| 88 | ProcessGroupMetrics.layer.pipe( |
| 89 | Layer.provide(Layer.succeed(ResourceTelemetry.ResourceTelemetry, telemetry)), |
| 90 | Layer.provide( |
| 91 | ServerConfig.layer({ |
| 92 | otlpMetricsUrl: "http://localhost:4318/v1/metrics", |
| 93 | } as ServerConfig.ServerConfig["Service"]), |
| 94 | ), |
| 95 | ), |
| 96 | ); |
| 97 | yield* afterStart; |
| 98 | return logs; |
| 99 | }).pipe( |
| 100 | Effect.scoped, |
| 101 | Effect.provide( |
| 102 | Layer.mergeAll( |
| 103 | Logger.layer([logger], { mergeWithExisting: false }), |
| 104 | Layer.succeed(References.MinimumLogLevel, "Debug"), |
| 105 | ), |
| 106 | ), |
| 107 | ); |
| 108 | }; |
| 109 | const emptySnapshot = { processes: [] } as unknown as ResourceTelemetrySnapshot; |
| 110 | |
| 111 | it.effect("logs the first failed sample and the recovery, not every repeat", () => |
| 112 | Effect.gen(function* () { |
| 113 | const outcomes = yield* Ref.make<ReadonlyArray<"fail" | "ok">>(["fail", "fail", "ok"]); |
| 114 | const refresh = Ref.modify(outcomes, ([next = "ok", ...rest]) => [next, rest] as const).pipe( |
| 115 | Effect.flatMap((next) => |
| 116 | next === "fail" ? Effect.fail(notRunning) : Effect.succeed(emptySnapshot), |
| 117 | ), |
| 118 | ); |
| 119 | const logs = yield* logsAfter(refresh, TestClock.adjust("3 minutes")); |
| 120 | assert.deepStrictEqual(logs, [ |
| 121 | "WARN process group metrics are not exported: sample failed", |
| 122 | "DEBUG process group metrics sample failed", |
| 123 | "INFO process group metrics sample recovered", |
| 124 | ]); |
| 125 | }), |
| 126 | ); |
| 127 | |
| 128 | it.effect("does not report the sidecar as failed while it is still starting", () => |
| 129 | Effect.gen(function* () { |
| 130 | // The sidecar answers once its handshake completes, a few seconds after the server starts. |
| 131 | const refresh = Clock.currentTimeMillis.pipe( |
| 132 | Effect.flatMap((now) => |
| 133 | now >= 5_000 ? Effect.succeed(emptySnapshot) : Effect.fail(notRunning), |
| 134 | ), |
| 135 | ); |
| 136 | const logs = yield* logsAfter(refresh, TestClock.adjust("1 minute")); |
| 137 | assert.deepStrictEqual(logs, []); |
| 138 | }), |
| 139 | ); |
| 140 | }); |