Skip to content

Commit ff2ba43

Browse files
only21milclaude
andcommitted
fix(observability): cap trace attribute nodes so shared graphs stay small
Ancestor-only cycle detection expands a repeated child once per path, so 30 levels of objects that each reference one child twice would build over a billion values. Normalization now stops after 10,000 container nodes per attribute record and writes "[Truncated]" in their place. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 7b32573 commit ff2ba43

2 files changed

Lines changed: 53 additions & 24 deletions

File tree

‎packages/shared/src/observability.test.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,18 @@ describe("observability", () => {
227227
);
228228
});
229229

230+
it("caps shared references that would expand exponentially", () => {
231+
let graph: Record<string, unknown> = { cwd: "/repo" };
232+
for (let depth = 0; depth < 20; depth++) {
233+
graph = { left: graph, right: graph };
234+
}
235+
236+
const serialized = JSON.stringify(compactTraceAttributes({ graph }));
237+
238+
assert.include(serialized, '"[Truncated]"');
239+
assert.isBelow(serialized.length, 1_000_000);
240+
});
241+
230242
nodeServicesIt("node services", (it) => {
231243
it.effect(
232244
"keeps trace producers responsive during a slow write, bounds the backlog, and drains on close",

‎packages/shared/src/observability.ts‎

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -178,16 +178,28 @@ function isPlainObject(value: unknown): value is Record<string, unknown> {
178178
return typeof value === "object" && value !== null && !Array.isArray(value);
179179
}
180180

181-
// `seen` holds only the current ancestors, so a value repeated under two keys is not a cycle.
182-
function markSeen(value: object, seen: WeakSet<object>): boolean {
183-
if (seen.has(value)) {
184-
return true;
181+
// Repeated references expand once per path, so a small shared graph can grow exponentially.
182+
const TRACE_ATTRIBUTE_MAX_NODES = 10_000;
183+
184+
interface NormalizeState {
185+
// Only the current ancestors, so a value repeated under two keys is not a cycle.
186+
readonly seen: WeakSet<object>;
187+
remainingNodes: number;
188+
}
189+
190+
function enterNode(value: object, state: NormalizeState): string | undefined {
191+
if (state.seen.has(value)) {
192+
return "[Circular]";
193+
}
194+
if (state.remainingNodes <= 0) {
195+
return "[Truncated]";
185196
}
186-
seen.add(value);
187-
return false;
197+
state.remainingNodes--;
198+
state.seen.add(value);
199+
return undefined;
188200
}
189201

190-
function normalizeJsonValue(value: unknown, seen: WeakSet<object> = new WeakSet()): unknown {
202+
function normalizeJsonValue(value: unknown, state: NormalizeState): unknown {
191203
if (
192204
value === null ||
193205
value === undefined ||
@@ -211,54 +223,59 @@ function normalizeJsonValue(value: unknown, seen: WeakSet<object> = new WeakSet(
211223
};
212224
}
213225
if (Array.isArray(value)) {
214-
if (markSeen(value, seen)) {
215-
return "[Circular]";
226+
const marker = enterNode(value, state);
227+
if (marker) {
228+
return marker;
216229
}
217-
const normalized = value.map((entry) => normalizeJsonValue(entry, seen));
218-
seen.delete(value);
230+
const normalized = value.map((entry) => normalizeJsonValue(entry, state));
231+
state.seen.delete(value);
219232
return normalized;
220233
}
221234
if (value instanceof Map) {
222-
if (markSeen(value, seen)) {
223-
return "[Circular]";
235+
const marker = enterNode(value, state);
236+
if (marker) {
237+
return marker;
224238
}
225239
const normalized = Object.fromEntries(
226240
Array.from(value.entries(), ([key, entryValue]) => [
227241
String(key),
228-
normalizeJsonValue(entryValue, seen),
242+
normalizeJsonValue(entryValue, state),
229243
]),
230244
);
231-
seen.delete(value);
245+
state.seen.delete(value);
232246
return normalized;
233247
}
234248
if (value instanceof Set) {
235-
if (markSeen(value, seen)) {
236-
return "[Circular]";
249+
const marker = enterNode(value, state);
250+
if (marker) {
251+
return marker;
237252
}
238-
const normalized = Array.from(value.values(), (entry) => normalizeJsonValue(entry, seen));
239-
seen.delete(value);
253+
const normalized = Array.from(value.values(), (entry) => normalizeJsonValue(entry, state));
254+
state.seen.delete(value);
240255
return normalized;
241256
}
242257
if (!isPlainObject(value)) {
243258
return String(value);
244259
}
245-
if (markSeen(value, seen)) {
246-
return "[Circular]";
260+
const marker = enterNode(value, state);
261+
if (marker) {
262+
return marker;
247263
}
248264
const normalized = Object.fromEntries(
249-
Object.entries(value).map(([key, entryValue]) => [key, normalizeJsonValue(entryValue, seen)]),
265+
Object.entries(value).map(([key, entryValue]) => [key, normalizeJsonValue(entryValue, state)]),
250266
);
251-
seen.delete(value);
267+
state.seen.delete(value);
252268
return normalized;
253269
}
254270

255271
export function compactTraceAttributes(
256272
attributes: Readonly<Record<string, unknown>>,
257273
): TraceAttributes {
274+
const state: NormalizeState = { seen: new WeakSet(), remainingNodes: TRACE_ATTRIBUTE_MAX_NODES };
258275
const entries: Array<[string, unknown]> = [];
259276
for (const [key, value] of Object.entries(attributes)) {
260277
if (value !== undefined) {
261-
entries.push([key, normalizeJsonValue(value)]);
278+
entries.push([key, normalizeJsonValue(value, state)]);
262279
}
263280
}
264281
return Object.fromEntries(entries);

0 commit comments

Comments
 (0)