Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.
[codex] Preserve trace IDs across error causes (#3426)
Julius Marmingeb6e384fb273b
diff --git a/packages/client-runtime/src/errors/errorTrace.test.ts b/packages/client-runtime/src/errors/errorTrace.test.tsindex 075049bd5..255098999 100644--- a/packages/client-runtime/src/errors/errorTrace.test.ts+++ b/packages/client-runtime/src/errors/errorTrace.test.ts@@ -1,3 +1,4 @@+import * as Cause from "effect/Cause";import { describe, expect, it } from "vite-plus/test";import { findErrorTraceId } from "./errorTrace.ts";@@ -22,4 +23,22 @@ describe("findErrorTraceId", () => {expect(findErrorTraceId(error)).toBeNull();});++ it("finds trace metadata in Effect cause branches", () => {+ const cause = Cause.fromReasons<unknown>([+ Cause.makeFailReason(new Error("first failure")),+ Cause.makeFailReason({ traceId: "trace-secondary" }),+ ]);++ expect(findErrorTraceId(cause)).toBe("trace-secondary");+ });++ it("finds trace metadata in aggregate error branches", () => {+ const error = new AggregateError(+ [new Error("first failure"), { traceId: "trace-aggregate" }],+ "request failed",+ );++ expect(findErrorTraceId(error)).toBe("trace-aggregate");+ });});diff --git a/packages/client-runtime/src/errors/errorTrace.ts b/packages/client-runtime/src/errors/errorTrace.tsindex ec1b2a6b2..74deb37c4 100644--- a/packages/client-runtime/src/errors/errorTrace.ts+++ b/packages/client-runtime/src/errors/errorTrace.ts@@ -1,17 +1,49 @@+import * as Cause from "effect/Cause";++const MAX_ERROR_TRACE_NODES = 128;+export function findErrorTraceId(error: unknown): string | null {const seen = new Set<object>();- let current: unknown = error;+ const pending: Array<unknown> = [error];+ let inspectedNodeCount = 0;- while (typeof current === "object" && current !== null && !seen.has(current)) {+ while (pending.length > 0 && inspectedNodeCount < MAX_ERROR_TRACE_NODES) {+ const current = pending.pop();+ inspectedNodeCount += 1;+ if (typeof current !== "object" || current === null || seen.has(current)) {+ continue;+ }seen.add(current);const record = current as {readonly cause?: unknown;+ readonly errors?: unknown;readonly traceId?: unknown;};if (typeof record.traceId === "string" && record.traceId.trim().length > 0) {return record.traceId;}- current = record.cause;++ if (Array.isArray(record.errors)) {+ for (let index = record.errors.length - 1; index >= 0; index -= 1) {+ pending.push(record.errors[index]);+ }+ }+ if (Cause.isCause(current)) {+ for (let index = current.reasons.length - 1; index >= 0; index -= 1) {+ const reason = current.reasons[index];+ switch (reason?._tag) {+ case "Fail":+ pending.push(reason.error);+ break;+ case "Die":+ pending.push(reason.defect);+ break;+ }+ }+ }+ if ("cause" in record) {+ pending.push(record.cause);+ }}return null;