The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { assert, describe, it } from "@effect/vitest"; |
| 2 | import * as DateTime from "effect/DateTime"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as FileSystem from "effect/FileSystem"; |
| 5 | import * as Layer from "effect/Layer"; |
| 6 | import * as Logger from "effect/Logger"; |
| 7 | import * as Option from "effect/Option"; |
| 8 | import * as PlatformError from "effect/PlatformError"; |
| 9 | import * as References from "effect/References"; |
| 10 | import * as Stream from "effect/Stream"; |
| 11 | |
| 12 | import * as TraceDiagnostics from "./TraceDiagnostics.ts"; |
| 13 | |
| 14 | function ns(ms: number): string { |
| 15 | return String(BigInt(ms) * 1_000_000n); |
| 16 | } |
| 17 | |
| 18 | function record(input: { |
| 19 | readonly name: string; |
| 20 | readonly traceId: string; |
| 21 | readonly spanId: string; |
| 22 | readonly startMs: number; |
| 23 | readonly durationMs: number; |
| 24 | readonly exit?: { readonly _tag: "Success" | "Failure" | "Interrupted"; readonly cause?: string }; |
| 25 | readonly events?: ReadonlyArray<unknown>; |
| 26 | }) { |
| 27 | return JSON.stringify({ |
| 28 | type: "effect-span", |
| 29 | name: input.name, |
| 30 | traceId: input.traceId, |
| 31 | spanId: input.spanId, |
| 32 | sampled: true, |
| 33 | kind: "internal", |
| 34 | startTimeUnixNano: ns(input.startMs), |
| 35 | endTimeUnixNano: ns(input.startMs + input.durationMs), |
| 36 | durationMs: input.durationMs, |
| 37 | attributes: {}, |
| 38 | events: input.events ?? [], |
| 39 | links: [], |
| 40 | exit: input.exit ?? { _tag: "Success" }, |
| 41 | }); |
| 42 | } |
| 43 | |
| 44 | const traceFilePath = "/tmp/server.trace.ndjson"; |
| 45 | const readAt = DateTime.makeUnsafe("2026-05-05T10:00:00.000Z"); |
| 46 | |
| 47 | function aggregateLines(lines: ReadonlyArray<string>) { |
| 48 | const aggregator = TraceDiagnostics.makeTraceDiagnosticsAggregator(); |
| 49 | lines.forEach(aggregator.addLine); |
| 50 | return aggregator.finish({ |
| 51 | traceFilePath, |
| 52 | scannedFilePaths: TraceDiagnostics.toRotatedTracePaths(traceFilePath, 1), |
| 53 | readAt, |
| 54 | }); |
| 55 | } |
| 56 | |
| 57 | function readTraces(fileSystem: Partial<FileSystem.FileSystem>) { |
| 58 | return TraceDiagnostics.readTraceDiagnostics({ traceFilePath, maxFiles: 1, readAt }).pipe( |
| 59 | Effect.provide(TraceDiagnostics.layer.pipe(Layer.provide(FileSystem.layerNoop(fileSystem)))), |
| 60 | ); |
| 61 | } |
| 62 | |
| 63 | describe("TraceDiagnostics", () => { |
| 64 | it("includes browser failures and interruptions in diagnostics", () => { |
| 65 | const diagnostics = aggregateLines( |
| 66 | [ |
| 67 | { status: { code: "2", message: "Browser request failed" }, attributes: {} }, |
| 68 | { status: { code: "0" }, attributes: { "effect.fiber.interrupted": true } }, |
| 69 | { status: { code: "1", message: "Interrupted" }, attributes: {} }, |
| 70 | { status: { code: "1" }, attributes: {} }, |
| 71 | ].map((outcome, index) => |
| 72 | JSON.stringify({ |
| 73 | type: "otlp-span", |
| 74 | name: "browser.request", |
| 75 | traceId: `trace-${index}`, |
| 76 | spanId: `span-${index}`, |
| 77 | startTimeUnixNano: ns(index * 100), |
| 78 | endTimeUnixNano: ns(index * 100 + 10), |
| 79 | durationMs: 10, |
| 80 | ...outcome, |
| 81 | }), |
| 82 | ), |
| 83 | ); |
| 84 | |
| 85 | assert.equal(diagnostics.recordCount, 4); |
| 86 | assert.equal(diagnostics.failureCount, 1); |
| 87 | assert.equal(diagnostics.interruptionCount, 2); |
| 88 | assert.equal(diagnostics.topSpansByCount[0]?.failureCount, 1); |
| 89 | assert.equal(diagnostics.commonFailures[0]?.cause, "Browser request failed"); |
| 90 | assert.equal(diagnostics.latestFailures[0]?.cause, "Browser request failed"); |
| 91 | }); |
| 92 | |
| 93 | it.effect("aggregates failures, slow spans, log levels, and parse errors", () => |
| 94 | Effect.sync(() => { |
| 95 | const diagnostics = aggregateLines([ |
| 96 | record({ |
| 97 | name: "server.getConfig", |
| 98 | traceId: "trace-a", |
| 99 | spanId: "span-a", |
| 100 | startMs: 1_000, |
| 101 | durationMs: 50, |
| 102 | }), |
| 103 | "not-json", |
| 104 | record({ |
| 105 | name: "orchestration.dispatch", |
| 106 | traceId: "trace-b", |
| 107 | spanId: "span-b", |
| 108 | startMs: 2_000, |
| 109 | durationMs: 1_500, |
| 110 | exit: { _tag: "Failure", cause: "Provider crashed" }, |
| 111 | events: [ |
| 112 | { |
| 113 | name: "provider failed", |
| 114 | timeUnixNano: ns(3_400), |
| 115 | attributes: { "effect.logLevel": "Error" }, |
| 116 | }, |
| 117 | ], |
| 118 | }), |
| 119 | record({ |
| 120 | name: "orchestration.dispatch", |
| 121 | traceId: "trace-c", |
| 122 | spanId: "span-c", |
| 123 | startMs: 4_000, |
| 124 | durationMs: 250, |
| 125 | exit: { _tag: "Failure", cause: "Provider crashed" }, |
| 126 | }), |
| 127 | record({ |
| 128 | name: "git.status", |
| 129 | traceId: "trace-d", |
| 130 | spanId: "span-d", |
| 131 | startMs: 5_000, |
| 132 | durationMs: 25, |
| 133 | exit: { _tag: "Interrupted", cause: "Interrupted" }, |
| 134 | events: [ |
| 135 | { |
| 136 | name: "status delayed", |
| 137 | timeUnixNano: ns(5_010), |
| 138 | attributes: { "effect.logLevel": "Warning" }, |
| 139 | }, |
| 140 | ], |
| 141 | }), |
| 142 | ]); |
| 143 | |
| 144 | assert.equal(diagnostics.recordCount, 4); |
| 145 | assert.equal(DateTime.formatIso(diagnostics.readAt), "2026-05-05T10:00:00.000Z"); |
| 146 | assert.equal( |
| 147 | Option.match(diagnostics.firstSpanAt, { |
| 148 | onNone: () => null, |
| 149 | onSome: DateTime.formatIso, |
| 150 | }), |
| 151 | "1970-01-01T00:00:01.000Z", |
| 152 | ); |
| 153 | assert.equal( |
| 154 | Option.match(diagnostics.lastSpanAt, { |
| 155 | onNone: () => null, |
| 156 | onSome: DateTime.formatIso, |
| 157 | }), |
| 158 | "1970-01-01T00:00:05.025Z", |
| 159 | ); |
| 160 | assert.equal(diagnostics.parseErrorCount, 1); |
| 161 | assert.equal(diagnostics.failureCount, 2); |
| 162 | assert.equal(diagnostics.interruptionCount, 1); |
| 163 | assert.equal(diagnostics.slowSpanCount, 1); |
| 164 | assert.equal(diagnostics.logLevelCounts.Error, 1); |
| 165 | assert.equal(diagnostics.logLevelCounts.Warning, 1); |
| 166 | assert.equal(diagnostics.commonFailures[0]?.name, "orchestration.dispatch"); |
| 167 | assert.equal(diagnostics.commonFailures[0]?.count, 2); |
| 168 | assert.equal(diagnostics.latestFailures[0]?.traceId, "trace-c"); |
| 169 | assert.equal(diagnostics.slowestSpans[0]?.traceId, "trace-b"); |
| 170 | assert.equal(diagnostics.latestWarningAndErrorLogs[0]?.message, "status delayed"); |
| 171 | assert.equal(diagnostics.topSpansByCount[0]?.name, "orchestration.dispatch"); |
| 172 | }), |
| 173 | ); |
| 174 | |
| 175 | it.effect("streams rotated files into the same result as reading them whole", () => |
| 176 | Effect.gen(function* () { |
| 177 | const files = new Map([ |
| 178 | [ |
| 179 | `${traceFilePath}.1`, |
| 180 | [ |
| 181 | record({ |
| 182 | name: "server.getConfig", |
| 183 | traceId: "trace-a", |
| 184 | spanId: "span-a", |
| 185 | startMs: 1_000, |
| 186 | durationMs: 50, |
| 187 | }), |
| 188 | "not-json", |
| 189 | record({ |
| 190 | name: "orchestration.dispatch", |
| 191 | traceId: "trace-b", |
| 192 | spanId: "span-b", |
| 193 | startMs: 2_000, |
| 194 | durationMs: 1_500, |
| 195 | exit: { _tag: "Failure", cause: "Provider crashed: café 🔥" }, |
| 196 | }), |
| 197 | "", |
| 198 | ].join("\r\n"), |
| 199 | ], |
| 200 | [ |
| 201 | traceFilePath, |
| 202 | [ |
| 203 | record({ |
| 204 | name: "git.status", |
| 205 | traceId: "trace-c", |
| 206 | spanId: "span-c", |
| 207 | startMs: 3_000, |
| 208 | durationMs: 25, |
| 209 | exit: { _tag: "Interrupted", cause: "Interrupted" }, |
| 210 | events: [ |
| 211 | { |
| 212 | name: "status delayed ⏳", |
| 213 | timeUnixNano: ns(3_010), |
| 214 | attributes: { "effect.logLevel": "Warning" }, |
| 215 | }, |
| 216 | ], |
| 217 | }), |
| 218 | "", |
| 219 | record({ |
| 220 | name: "orchestration.dispatch", |
| 221 | traceId: "trace-d", |
| 222 | spanId: "span-d", |
| 223 | startMs: 4_000, |
| 224 | durationMs: 250, |
| 225 | exit: { _tag: "Failure", cause: "Provider crashed: café 🔥" }, |
| 226 | }), |
| 227 | ].join("\n"), |
| 228 | ], |
| 229 | ]); |
| 230 | const encoder = new TextEncoder(); |
| 231 | |
| 232 | const diagnostics = yield* readTraces({ |
| 233 | stream: (path) => |
| 234 | Stream.fromIterable( |
| 235 | Array.from(encoder.encode(files.get(path)), (byte) => Uint8Array.of(byte)), |
| 236 | ), |
| 237 | }); |
| 238 | |
| 239 | assert.equal(diagnostics.recordCount, 4); |
| 240 | assert.deepStrictEqual( |
| 241 | diagnostics, |
| 242 | aggregateLines([...files.values()].flatMap((text) => text.split(/\r?\n/))), |
| 243 | ); |
| 244 | }), |
| 245 | ); |
| 246 | |
| 247 | it.effect("keeps loaded trace data when one rotated trace file fails to read", () => |
| 248 | Effect.gen(function* () { |
| 249 | const readFailure = PlatformError.systemError({ |
| 250 | _tag: "PermissionDenied", |
| 251 | module: "FileSystem", |
| 252 | method: "open", |
| 253 | description: "permission denied", |
| 254 | pathOrDescriptor: `${traceFilePath}.1`, |
| 255 | }); |
| 256 | const logAnnotations: Array<Record<string, unknown>> = []; |
| 257 | const logger = Logger.make<unknown, void>((options) => { |
| 258 | logAnnotations.push({ ...options.fiber.getRef(References.CurrentLogAnnotations) }); |
| 259 | }); |
| 260 | |
| 261 | const diagnostics = yield* readTraces({ |
| 262 | stream: (path) => |
| 263 | path === `${traceFilePath}.1` |
| 264 | ? Stream.fail(readFailure) |
| 265 | : Stream.make( |
| 266 | new TextEncoder().encode( |
| 267 | record({ |
| 268 | name: "server.getConfig", |
| 269 | traceId: "trace-a", |
| 270 | spanId: "span-a", |
| 271 | startMs: 1_000, |
| 272 | durationMs: 50, |
| 273 | }), |
| 274 | ), |
| 275 | ), |
| 276 | }).pipe(Effect.provide(Logger.layer([logger], { mergeWithExisting: false }))); |
| 277 | |
| 278 | assert.equal(diagnostics.recordCount, 1); |
| 279 | assert.equal( |
| 280 | Option.getOrElse(diagnostics.partialFailure, () => false), |
| 281 | true, |
| 282 | ); |
| 283 | assert.deepStrictEqual(Option.getOrUndefined(diagnostics.error), { |
| 284 | kind: "trace-file-read-failed", |
| 285 | message: `Failed to read local trace file '${traceFilePath}.1'.`, |
| 286 | }); |
| 287 | assert.deepStrictEqual(diagnostics.scannedFilePaths, [`${traceFilePath}.1`, traceFilePath]); |
| 288 | |
| 289 | const failureLog = logAnnotations.find( |
| 290 | (annotations) => annotations.traceFilePath === `${traceFilePath}.1`, |
| 291 | ); |
| 292 | assert.exists(failureLog); |
| 293 | assert.deepStrictEqual(failureLog, { |
| 294 | traceFilePath: `${traceFilePath}.1`, |
| 295 | errorTag: "TraceFileReadError", |
| 296 | causeTag: "PermissionDenied", |
| 297 | }); |
| 298 | }), |
| 299 | ); |
| 300 | }); |