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] Preserve trace IDs across error causes (#3426)
Co-authored-by: codex <codex@users.noreply.github.com>
Julius Marminge committed
Jun 20, 2026 at 11:40 UTC
b6e384fb273b56ab9d12237f27684d1392be1692
2 files changed
+54
-3
packages/client-runtime/src/errors/errorTrace.test.ts
+19
@@ -1,3 +1,4 @@
1
+import * as Cause from "effect/Cause";
2
import { describe, expect, it } from "vite-plus/test";
3
4
import { findErrorTraceId } from "./errorTrace.ts";
@@ -22,4 +23,22 @@ describe("findErrorTraceId", () => {
23
24
expect(findErrorTraceId(error)).toBeNull();
25
});
26
+
27
+ it("finds trace metadata in Effect cause branches", () => {
28
+ const cause = Cause.fromReasons<unknown>([
29
+ Cause.makeFailReason(new Error("first failure")),
30
+ Cause.makeFailReason({ traceId: "trace-secondary" }),
31
+ ]);
32
+
33
+ expect(findErrorTraceId(cause)).toBe("trace-secondary");
34
+ });
35
+
36
+ it("finds trace metadata in aggregate error branches", () => {
37
+ const error = new AggregateError(
38
+ [new Error("first failure"), { traceId: "trace-aggregate" }],
39
+ "request failed",
40
+ );
41
+
42
+ expect(findErrorTraceId(error)).toBe("trace-aggregate");
43
+ });
44
});
packages/client-runtime/src/errors/errorTrace.ts
+35
-3
@@ -1,17 +1,49 @@
1
+import * as Cause from "effect/Cause";
2
+
3
+const MAX_ERROR_TRACE_NODES = 128;
4
+
5
export function findErrorTraceId(error: unknown): string | null {
6
const seen = new Set<object>();
3
- let current: unknown = error;
7
+ const pending: Array<unknown> = [error];
8
+ let inspectedNodeCount = 0;
9
5
- while (typeof current === "object" && current !== null && !seen.has(current)) {
10
+ while (pending.length > 0 && inspectedNodeCount < MAX_ERROR_TRACE_NODES) {
11
+ const current = pending.pop();
12
+ inspectedNodeCount += 1;
13
+ if (typeof current !== "object" || current === null || seen.has(current)) {
14
+ continue;
15
+ }
16
seen.add(current);
17
const record = current as {
18
readonly cause?: unknown;
19
+ readonly errors?: unknown;
20
readonly traceId?: unknown;
21
};
22
if (typeof record.traceId === "string" && record.traceId.trim().length > 0) {
23
return record.traceId;
24
}
14
- current = record.cause;
25
+
26
+ if (Array.isArray(record.errors)) {
27
+ for (let index = record.errors.length - 1; index >= 0; index -= 1) {
28
+ pending.push(record.errors[index]);
29
+ }
30
+ }
31
+ if (Cause.isCause(current)) {
32
+ for (let index = current.reasons.length - 1; index >= 0; index -= 1) {
33
+ const reason = current.reasons[index];
34
+ switch (reason?._tag) {
35
+ case "Fail":
36
+ pending.push(reason.error);
37
+ break;
38
+ case "Die":
39
+ pending.push(reason.defect);
40
+ break;
41
+ }
42
+ }
43
+ }
44
+ if ("cause" in record) {
45
+ pending.push(record.cause);
46
+ }
47
}
48
49
return null;