The best way to control Claude Code, Codex and more from any computer or phone
Preserve trace file read causes (#3300)
Co-authored-by: codex <codex@users.noreply.github.com>
Julius Marminge committed
Jun 20, 2026 at 13:11 UTC
6e9b43cb84c1b93c348d76880a0bf0083f395e47
2 files changed
+85
-30
apps/server/src/diagnostics/TraceDiagnostics.test.ts
+36
-11
@@ -3,8 +3,10 @@ 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
11
import * as TraceDiagnostics from "./TraceDiagnostics.ts";
12
@@ -187,18 +189,17 @@ describe("TraceDiagnostics", () => {
189
it.effect("keeps loaded trace data when one rotated trace file fails to read", () =>
190
Effect.gen(function* () {
191
const traceFilePath = "/tmp/server.trace.ndjson";
192
+ const readFailure = PlatformError.systemError({
193
+ _tag: "PermissionDenied",
194
+ module: "FileSystem",
195
+ method: "readFileString",
196
+ description: "permission denied",
197
+ pathOrDescriptor: `${traceFilePath}.1`,
198
+ });
199
const fileSystemLayer = FileSystem.layerNoop({
200
readFileString: (path) =>
201
path === `${traceFilePath}.1`
193
- ? Effect.fail(
194
- PlatformError.systemError({
195
- _tag: "PermissionDenied",
196
- module: "FileSystem",
197
- method: "readFileString",
198
- description: "permission denied",
199
- pathOrDescriptor: path,
200
- }),
201
- )
202
+ ? Effect.fail(readFailure)
203
: Effect.succeed(
204
record({
205
name: "server.getConfig",
@@ -209,20 +210,44 @@ describe("TraceDiagnostics", () => {
210
}),
211
),
212
});
213
+ const logAnnotations: Array<Record<string, unknown>> = [];
214
+ const logger = Logger.make<unknown, void>((options) => {
215
+ logAnnotations.push({ ...options.fiber.getRef(References.CurrentLogAnnotations) });
216
+ });
217
218
const diagnostics = yield* TraceDiagnostics.readTraceDiagnostics({
219
traceFilePath,
220
maxFiles: 1,
221
readAt: DateTime.makeUnsafe("2026-05-05T10:00:00.000Z"),
217
- }).pipe(Effect.provide(TraceDiagnostics.layer.pipe(Layer.provide(fileSystemLayer))));
222
+ }).pipe(
223
+ Effect.provide(
224
+ Layer.mergeAll(
225
+ TraceDiagnostics.layer.pipe(Layer.provide(fileSystemLayer)),
226
+ Logger.layer([logger], { mergeWithExisting: false }),
227
+ ),
228
+ ),
229
+ );
230
231
assert.equal(diagnostics.recordCount, 1);
232
assert.equal(
233
Option.getOrElse(diagnostics.partialFailure, () => false),
234
true,
235
);
224
- assert.equal(Option.getOrUndefined(diagnostics.error)?.kind, "trace-file-read-failed");
236
+ assert.deepStrictEqual(Option.getOrUndefined(diagnostics.error), {
237
+ kind: "trace-file-read-failed",
238
+ message: `Failed to read local trace file '${traceFilePath}.1'.`,
239
+ });
240
assert.deepStrictEqual(diagnostics.scannedFilePaths, [`${traceFilePath}.1`, traceFilePath]);
241
+
242
+ const failureLog = logAnnotations.find(
243
+ (annotations) => annotations.traceFilePath === `${traceFilePath}.1`,
244
+ );
245
+ assert.exists(failureLog);
246
+ assert.deepStrictEqual(failureLog, {
247
+ traceFilePath: `${traceFilePath}.1`,
248
+ errorTag: "TraceFileReadError",
249
+ causeTag: "PermissionDenied",
250
+ });
251
}),
252
);
253
apps/server/src/diagnostics/TraceDiagnostics.ts
+49
-19
@@ -14,6 +14,8 @@ import * as FileSystem from "effect/FileSystem";
14
import * as Layer from "effect/Layer";
15
import * as Option from "effect/Option";
16
import * as PlatformError from "effect/PlatformError";
17
+import * as Result from "effect/Result";
18
+import * as Schema from "effect/Schema";
19
20
interface TraceRecordLike {
21
readonly name?: unknown;
@@ -39,6 +41,19 @@ export interface TraceDiagnosticsOptions {
41
readonly readAt?: DateTime.Utc;
42
}
43
44
+export class TraceFileReadError extends Schema.TaggedErrorClass<TraceFileReadError>()(
45
+ "TraceFileReadError",
46
+ {
47
+ traceFilePath: Schema.String,
48
+ causeTag: Schema.String,
49
+ cause: Schema.Defect(),
50
+ },
51
+) {
52
+ override get message(): string {
53
+ return `Failed to read local trace file '${this.traceFilePath}'.`;
54
+ }
55
+}
56
+
57
export class TraceDiagnostics extends Context.Service<
58
TraceDiagnostics,
59
{
@@ -153,10 +168,6 @@ function isNotFoundError(error: PlatformError.PlatformError): boolean {
168
return error.reason._tag === "NotFound";
169
}
170
156
-function platformErrorMessage(error: PlatformError.PlatformError): string {
157
- return error.message || String(error);
158
-}
159
-
171
function insertBoundedSlowestSpan(
172
slowestSpans: ServerTraceDiagnosticsSpanOccurrence[],
173
span: ServerTraceDiagnosticsSpanOccurrence,
@@ -377,22 +388,26 @@ export function aggregateTraceDiagnostics(
388
389
type TraceFileReadResult =
390
| { readonly _tag: "Loaded"; readonly path: string; readonly text: string }
380
- | { readonly _tag: "Missing"; readonly path: string }
381
- | { readonly _tag: "Failed"; readonly path: string; readonly message: string };
391
+ | { readonly _tag: "Missing"; readonly path: string };
392
393
function readTraceFile(
394
fileSystem: FileSystem.FileSystem,
395
path: string,
386
-): Effect.Effect<TraceFileReadResult> {
396
+): Effect.Effect<TraceFileReadResult, TraceFileReadError> {
397
return fileSystem.readFileString(path).pipe(
388
- Effect.map((text) => ({ _tag: "Loaded" as const, path, text })),
389
- Effect.catch((error: PlatformError.PlatformError) =>
390
- Effect.succeed(
391
- isNotFoundError(error)
392
- ? { _tag: "Missing" as const, path }
393
- : { _tag: "Failed" as const, path, message: platformErrorMessage(error) },
394
- ),
395
- ),
398
+ Effect.map((text): TraceFileReadResult => ({ _tag: "Loaded", path, text })),
399
+ Effect.catchTags({
400
+ PlatformError: (cause) =>
401
+ isNotFoundError(cause)
402
+ ? Effect.succeed<TraceFileReadResult>({ _tag: "Missing", path })
403
+ : Effect.fail(
404
+ new TraceFileReadError({
405
+ traceFilePath: path,
406
+ causeTag: cause.reason._tag,
407
+ cause,
408
+ }),
409
+ ),
410
+ }),
411
);
412
}
413
@@ -405,19 +420,34 @@ export const make = Effect.gen(function* () {
420
const slowSpanThresholdMs = options.slowSpanThresholdMs ?? DEFAULT_SLOW_SPAN_THRESHOLD_MS;
421
const paths = toRotatedTracePaths(options.traceFilePath, options.maxFiles);
422
const results = yield* Effect.all(
408
- paths.map((path) => readTraceFile(fileSystem, path)),
423
+ paths.map((path) =>
424
+ readTraceFile(fileSystem, path).pipe(
425
+ Effect.tapError((cause) =>
426
+ Effect.logWarning("Failed to read local trace file.").pipe(
427
+ Effect.annotateLogs({
428
+ traceFilePath: cause.traceFilePath,
429
+ errorTag: cause._tag,
430
+ causeTag: cause.causeTag,
431
+ }),
432
+ ),
433
+ ),
434
+ Effect.result,
435
+ ),
436
+ ),
437
{
438
concurrency: 1,
439
},
440
);
441
const files = results.flatMap((result) =>
414
- result._tag === "Loaded" ? [{ path: result.path, text: result.text }] : [],
442
+ Result.isSuccess(result) && result.success._tag === "Loaded"
443
+ ? [{ path: result.success.path, text: result.success.text }]
444
+ : [],
445
);
416
- const readFailure = results.find((result) => result._tag === "Failed");
446
+ const readFailure = results.find(Result.isFailure);
447
const readFailureError = readFailure
448
? ({
449
kind: "trace-file-read-failed",
420
- message: readFailure.message.trim() || `Failed to read ${readFailure.path}.`,
450
+ message: readFailure.failure.message,
451
} satisfies TraceDiagnosticsErrorSummary)
452
: undefined;
453