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 24, 22:45 UTC
[codex] Structure process resource sampling failures (#3415)
Co-authored-by: codex <codex@users.noreply.github.com>
Julius Marminge committed
Jun 20, 2026 at 11:11 UTC
d84ebe831922167d556e16cb40a238a904e0f14a
3 files changed
+90
-20
apps/server/src/diagnostics/ProcessResourceMonitor.test.ts
+30
-3
@@ -111,7 +111,7 @@ describe("ProcessResourceMonitor", () => {
111
readAtMs: DateTime.toEpochMillis(secondAt),
112
windowMs: 60_000,
113
bucketMs: 10_000,
114
- lastError: null,
114
+ lastFailure: null,
115
});
116
117
expect(Option.isNone(result.error)).toBe(true);
@@ -171,7 +171,7 @@ describe("ProcessResourceMonitor", () => {
171
readAtMs: DateTime.toEpochMillis(secondAt),
172
windowMs: 60_000,
173
bucketMs: 10_000,
174
- lastError: null,
174
+ lastFailure: null,
175
});
176
177
expect(result.topProcesses).toHaveLength(1);
@@ -218,11 +218,38 @@ describe("ProcessResourceMonitor", () => {
218
readAtMs: DateTime.toEpochMillis(sampledAt),
219
windowMs: 60_000,
220
bucketMs: 10_000,
221
- lastError: null,
221
+ lastFailure: null,
222
});
223
224
expect(result.topProcesses).toHaveLength(36);
225
expect(result.topProcesses.some((process) => process.command === "worker 34")).toBe(true);
226
}),
227
);
228
+
229
+ it.effect("exposes bounded failure diagnostics while retaining the exact cause", () =>
230
+ Effect.sync(() => {
231
+ const readAt = DateTime.makeUnsafe("2026-05-05T10:00:00.000Z");
232
+ const cause = new Error("stderr included credential=secret-value");
233
+ const failure = new ProcessResourceMonitor.ProcessResourceSamplingError({
234
+ failureTag: "ProcessDiagnosticsQueryFailedError",
235
+ cause,
236
+ });
237
+
238
+ const result = ProcessResourceMonitor.aggregateProcessResourceHistory({
239
+ samples: [],
240
+ readAt,
241
+ readAtMs: DateTime.toEpochMillis(readAt),
242
+ windowMs: 60_000,
243
+ bucketMs: 10_000,
244
+ lastFailure: failure,
245
+ });
246
+
247
+ expect(failure.cause).toBe(cause);
248
+ expect(Option.getOrThrow(result.error)).toEqual({
249
+ failureTag: "ProcessDiagnosticsQueryFailedError",
250
+ message: "Failed to sample process resources (ProcessDiagnosticsQueryFailedError).",
251
+ });
252
+ expect(Option.getOrThrow(result.error).message).not.toContain("secret-value");
253
+ }),
254
+ );
255
});
apps/server/src/diagnostics/ProcessResourceMonitor.ts
+49
-17
@@ -1,8 +1,10 @@
1
-import type {
2
- ServerProcessResourceHistoryBucket,
3
- ServerProcessResourceHistoryInput,
4
- ServerProcessResourceHistoryResult,
5
- ServerProcessResourceHistorySummary,
1
+import {
2
+ ServerProcessResourceHistoryFailureTag,
3
+ type ServerProcessResourceHistoryBucket,
4
+ type ServerProcessResourceHistoryFailureTag as ServerProcessResourceHistoryFailureTagType,
5
+ type ServerProcessResourceHistoryInput,
6
+ type ServerProcessResourceHistoryResult,
7
+ type ServerProcessResourceHistorySummary,
8
} from "@t3tools/contracts";
9
import * as Context from "effect/Context";
10
import * as DateTime from "effect/DateTime";
@@ -10,6 +12,7 @@ import * as Effect from "effect/Effect";
12
import * as Layer from "effect/Layer";
13
import * as Option from "effect/Option";
14
import * as Ref from "effect/Ref";
15
+import * as Schema from "effect/Schema";
16
import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner";
17
18
import * as ProcessDiagnostics from "./ProcessDiagnostics.ts";
@@ -31,9 +34,21 @@ export interface ProcessResourceSample {
34
readonly isServerRoot: boolean;
35
}
36
37
+export class ProcessResourceSamplingError extends Schema.TaggedErrorClass<ProcessResourceSamplingError>()(
38
+ "ProcessResourceSamplingError",
39
+ {
40
+ failureTag: ServerProcessResourceHistoryFailureTag,
41
+ cause: Schema.Defect(),
42
+ },
43
+) {
44
+ override get message(): string {
45
+ return `Failed to sample process resources (${this.failureTag}).`;
46
+ }
47
+}
48
+
49
interface MonitorState {
50
readonly samples: ReadonlyArray<ProcessResourceSample>;
36
- readonly lastError: string | null;
51
+ readonly lastFailure: ProcessResourceSamplingError | null;
52
}
53
54
export class ProcessResourceMonitor extends Context.Service<
@@ -218,7 +233,7 @@ export function aggregateProcessResourceHistory(input: {
233
readonly readAtMs: number;
234
readonly windowMs: number;
235
readonly bucketMs: number;
221
- readonly lastError: string | null;
236
+ readonly lastFailure: ProcessResourceSamplingError | null;
237
}): ServerProcessResourceHistoryResult {
238
const windowMs = Math.max(1_000, input.windowMs);
239
const bucketMs = Math.max(1_000, input.bucketMs);
@@ -239,13 +254,29 @@ export function aggregateProcessResourceHistory(input: {
254
totalCpuSecondsApprox,
255
buckets: buildBuckets({ samples, nowMs: input.readAtMs, windowMs, bucketMs }),
256
topProcesses,
242
- error: input.lastError ? Option.some({ message: input.lastError }) : Option.none(),
257
+ error: input.lastFailure
258
+ ? Option.some({
259
+ failureTag: input.lastFailure.failureTag,
260
+ message: input.lastFailure.message,
261
+ })
262
+ : Option.none(),
263
};
264
}
265
266
export const make = Effect.gen(function* () {
267
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
248
- const state = yield* Ref.make<MonitorState>({ samples: [], lastError: null });
268
+ const state = yield* Ref.make<MonitorState>({ samples: [], lastFailure: null });
269
+
270
+ const recordSamplingFailure = (cause: {
271
+ readonly _tag: ServerProcessResourceHistoryFailureTagType;
272
+ }) =>
273
+ Ref.update(state, (current) => ({
274
+ ...current,
275
+ lastFailure: new ProcessResourceSamplingError({
276
+ failureTag: cause._tag,
277
+ cause,
278
+ }),
279
+ }));
280
281
const sampleOnce = Effect.gen(function* () {
282
const sampledAt = yield* DateTime.now;
@@ -261,15 +292,16 @@ export const make = Effect.gen(function* () {
292
});
293
yield* Ref.update(state, (current) => ({
294
samples: trimSamples([...current.samples, ...samples], sampledAtMs),
264
- lastError: null,
295
+ lastFailure: null,
296
}));
297
}).pipe(
267
- Effect.catch((error: unknown) =>
268
- Ref.update(state, (current) => ({
269
- ...current,
270
- lastError: error instanceof Error ? error.message : "Failed to sample process resources.",
271
- })),
272
- ),
298
+ Effect.catchTags({
299
+ ProcessDiagnosticsQueryTimeoutError: recordSamplingFailure,
300
+ ProcessDiagnosticsQueryFailedError: recordSamplingFailure,
301
+ ProcessDiagnosticsServerProcessSignalError: recordSamplingFailure,
302
+ ProcessDiagnosticsNotDescendantError: recordSamplingFailure,
303
+ ProcessDiagnosticsSignalFailedError: recordSamplingFailure,
304
+ }),
305
);
306
307
yield* Effect.forever(sampleOnce.pipe(Effect.andThen(Effect.sleep(SAMPLE_INTERVAL_MS)))).pipe(
@@ -287,7 +319,7 @@ export const make = Effect.gen(function* () {
319
readAtMs,
320
windowMs: input.windowMs,
321
bucketMs: input.bucketMs,
290
- lastError: current.lastError,
322
+ lastFailure: current.lastFailure,
323
});
324
});
325
packages/contracts/src/server.ts
+11
@@ -364,6 +364,16 @@ export const ServerProcessResourceHistorySummary = Schema.Struct({
364
});
365
export type ServerProcessResourceHistorySummary = typeof ServerProcessResourceHistorySummary.Type;
366
367
+export const ServerProcessResourceHistoryFailureTag = Schema.Literals([
368
+ "ProcessDiagnosticsQueryTimeoutError",
369
+ "ProcessDiagnosticsQueryFailedError",
370
+ "ProcessDiagnosticsServerProcessSignalError",
371
+ "ProcessDiagnosticsNotDescendantError",
372
+ "ProcessDiagnosticsSignalFailedError",
373
+]);
374
+export type ServerProcessResourceHistoryFailureTag =
375
+ typeof ServerProcessResourceHistoryFailureTag.Type;
376
+
377
export const ServerProcessResourceHistoryResult = Schema.Struct({
378
readAt: Schema.DateTimeUtc,
379
windowMs: NonNegativeInt,
@@ -375,6 +385,7 @@ export const ServerProcessResourceHistoryResult = Schema.Struct({
385
topProcesses: Schema.Array(ServerProcessResourceHistorySummary),
386
error: Schema.Option(
387
Schema.Struct({
388
+ failureTag: ServerProcessResourceHistoryFailureTag,
389
message: TrimmedNonEmptyString,
390
}),
391
),