@devries-cameron20 / t3code-1 / commits / d18690dadc
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(observability): trace attributes keep objects that appear twice (#23677)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Victor Vogel committed Oct 30, 2026 at 15:56 UTC d18690dadc1a34467971f9357dcc2689a77868b7
2 files changed +80 -21
packages/shared/src/observability.test.ts
+33
@@ -214,6 +214,39 @@ describe("observability", () => {
214 );
215 });
216
217 + it("keeps an object that appears twice without marking it circular", () => {
218 + const shared = { cwd: "/repo" };
219 + const sharedMap = new Map([["cwd", "/repo"]]);
220 + const sharedSet = new Set(["/repo"]);
221 +
222 + assert.deepStrictEqual(
223 + compactTraceAttributes({
224 + ctx: { first: shared, second: shared },
225 + list: [shared, shared],
226 + maps: [sharedMap, sharedMap],
227 + sets: [sharedSet, sharedSet],
228 + }),
229 + {
230 + ctx: { first: { cwd: "/repo" }, second: { cwd: "/repo" } },
231 + list: [{ cwd: "/repo" }, { cwd: "/repo" }],
232 + maps: [{ cwd: "/repo" }, { cwd: "/repo" }],
233 + sets: [["/repo"], ["/repo"]],
234 + },
235 + );
236 + });
237 +
238 + it("caps shared references that would expand exponentially", () => {
239 + let graph: Record<string, unknown> = { cwd: "/repo" };
240 + for (let depth = 0; depth < 20; depth++) {
241 + graph = { left: graph, right: graph };
242 + }
243 +
244 + const serialized = JSON.stringify(compactTraceAttributes({ graph }));
245 +
246 + assert.include(serialized, '"[Truncated]"');
247 + assert.isBelow(serialized.length, 1_000_000);
248 + });
249 +
250 nodeServicesIt("node services", (it) => {
251 it.effect(
252 "keeps trace producers responsive during a slow write, bounds the backlog, and drains on close",
packages/shared/src/observability.ts
+47 -21
@@ -205,15 +205,28 @@ function isPlainObject(value: unknown): value is Record<string, unknown> {
205 return typeof value === "object" && value !== null && !Array.isArray(value);
206 }
207
208 -function markSeen(value: object, seen: WeakSet<object>): boolean {
209 - if (seen.has(value)) {
210 - return true;
208 +// Repeated references expand once per path, so a small shared graph can grow exponentially.
209 +const TRACE_ATTRIBUTE_MAX_NODES = 10_000;
210 +
211 +interface NormalizeState {
212 + // Only the current ancestors, so a value repeated under two keys is not a cycle.
213 + readonly seen: WeakSet<object>;
214 + remainingNodes: number;
215 +}
216 +
217 +function enterNode(value: object, state: NormalizeState): string | undefined {
218 + if (state.seen.has(value)) {
219 + return "[Circular]";
220 + }
221 + if (state.remainingNodes <= 0) {
222 + return "[Truncated]";
223 }
212 - seen.add(value);
213 - return false;
224 + state.remainingNodes--;
225 + state.seen.add(value);
226 + return undefined;
227 }
228
216 -function normalizeJsonValue(value: unknown, seen: WeakSet<object> = new WeakSet()): unknown {
229 +function normalizeJsonValue(value: unknown, state: NormalizeState): unknown {
230 if (
231 value === null ||
232 value === undefined ||
@@ -237,46 +250,59 @@ function normalizeJsonValue(value: unknown, seen: WeakSet<object> = new WeakSet(
250 };
251 }
252 if (Array.isArray(value)) {
240 - if (markSeen(value, seen)) {
241 - return "[Circular]";
253 + const marker = enterNode(value, state);
254 + if (marker) {
255 + return marker;
256 }
243 - return value.map((entry) => normalizeJsonValue(entry, seen));
257 + const normalized = value.map((entry) => normalizeJsonValue(entry, state));
258 + state.seen.delete(value);
259 + return normalized;
260 }
261 if (value instanceof Map) {
246 - if (markSeen(value, seen)) {
247 - return "[Circular]";
262 + const marker = enterNode(value, state);
263 + if (marker) {
264 + return marker;
265 }
249 - return Object.fromEntries(
266 + const normalized = Object.fromEntries(
267 Array.from(value.entries(), ([key, entryValue]) => [
268 String(key),
252 - normalizeJsonValue(entryValue, seen),
269 + normalizeJsonValue(entryValue, state),
270 ]),
271 );
272 + state.seen.delete(value);
273 + return normalized;
274 }
275 if (value instanceof Set) {
257 - if (markSeen(value, seen)) {
258 - return "[Circular]";
276 + const marker = enterNode(value, state);
277 + if (marker) {
278 + return marker;
279 }
260 - return Array.from(value.values(), (entry) => normalizeJsonValue(entry, seen));
280 + const normalized = Array.from(value.values(), (entry) => normalizeJsonValue(entry, state));
281 + state.seen.delete(value);
282 + return normalized;
283 }
284 if (!isPlainObject(value)) {
285 return String(value);
286 }
265 - if (markSeen(value, seen)) {
266 - return "[Circular]";
287 + const marker = enterNode(value, state);
288 + if (marker) {
289 + return marker;
290 }
268 - return Object.fromEntries(
269 - Object.entries(value).map(([key, entryValue]) => [key, normalizeJsonValue(entryValue, seen)]),
291 + const normalized = Object.fromEntries(
292 + Object.entries(value).map(([key, entryValue]) => [key, normalizeJsonValue(entryValue, state)]),
293 );
294 + state.seen.delete(value);
295 + return normalized;
296 }
297
298 export function compactTraceAttributes(
299 attributes: Readonly<Record<string, unknown>>,
300 ): TraceAttributes {
301 + const state: NormalizeState = { seen: new WeakSet(), remainingNodes: TRACE_ATTRIBUTE_MAX_NODES };
302 const entries: Array<[string, unknown]> = [];
303 for (const [key, value] of Object.entries(attributes)) {
304 if (value !== undefined) {
279 - entries.push([key, normalizeJsonValue(value)]);
305 + entries.push([key, normalizeJsonValue(value, state)]);
306 }
307 }
308 return Object.fromEntries(entries);