Skip to content

Commit ba2e0cc

Browse files
authored
Prevent circular references from breaking run logs in task outputs (#2249)
1 parent b113e5a commit ba2e0cc

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

packages/core/src/v3/utils/ioSerialization.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,15 @@ interface ReplacerOptions {
400400
}
401401

402402
function makeSafeReplacer(options?: ReplacerOptions) {
403+
const seen = new WeakSet<any>();
404+
403405
return function replacer(key: string, value: any) {
406+
if (typeof value === "object" && value !== null) {
407+
if (seen.has(value)) {
408+
return "[Circular]";
409+
}
410+
seen.add(value);
411+
}
404412
// Check if the key should be filtered out
405413
if (options?.filteredKeys?.includes(key)) {
406414
return undefined;

references/hello-world/src/trigger/example.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,3 +301,30 @@ export const resourceMonitorTest = task({
301301
};
302302
},
303303
});
304+
305+
export const circularReferenceTask = task({
306+
id: "circular-reference",
307+
run: async (payload: { message: string }, { ctx }) => {
308+
logger.info("Hello, world from the circular reference task", { message: payload.message });
309+
310+
// Create an object
311+
const user = {
312+
name: "Alice",
313+
details: {
314+
age: 30,
315+
316+
},
317+
};
318+
319+
// Create the circular reference
320+
// @ts-expect-error - This is a circular reference
321+
user.details.user = user;
322+
323+
// Now user.details.user points back to the user object itself
324+
// This creates a circular reference that standard JSON can't handle
325+
326+
return {
327+
user,
328+
};
329+
},
330+
});

0 commit comments

Comments
 (0)