Skip to content
Developer & DevOps

Circular Reference Cycle Witness Extractor with Local Serializable Report

9 nodes 43 3 Manual trigger
Download

Loading workflow…

Workflow Description

A fully local workflow that inspects JavaScript objects for circular references without any external call. A Code node emits serializable fixture specs, and the detector builds the circular graphs in memory because circular objects cannot be passed between n8n nodes. The detector walks the graph iteratively using a WeakMap of first paths per identity and a WeakSet active-path stack, separating true cycles from shared references, and builds a serializable inspection copy where repeats become path markers without mutating the original. An IF node splits cases and Merge joins both branches into one report.

How it works

  1. 1.Manual Trigger fires with a single empty item.
  2. 2.Build Fictional Samples emits five serializable fixture specs in the fixture field.
  3. 3.Cycle Witness Detector builds each circular graph in memory from the fixture name, or inspects item.json.payload for real data.
  4. 4.The detector traverses values with an explicit stack plus WeakMap/WeakSet and extracts startPath and closePath per cycle while counting shared references.
  5. 5.It builds a safeCopy where every repeated reference becomes a __circularRef or __sharedRef marker with its original path.
  6. 6.Has Cycles? routes cycleCount greater than zero to output 0 and the rest to output 1.
  7. 7.Tag With Cycles and Tag Clean add a branch field while keeping all other fields.
  8. 8.Combine Branches appends both branches through two separate inputs, clean items passing a NoOp node.
  9. 9.Build Final Report runs once for all items and returns exactly one report item with counters, cycle witnesses and serializability checks.

Use cases

  • Diagnosing Converting circular structure to JSON failures inside Code nodes.
  • Pinpointing the path that closes a cycle before passing data downstream.
  • Separating legitimately reused shared objects from actual cycles in report structures.
  • Producing a serializable inspection snapshot of a complex structure.

Requirements

  • n8n 2.48.7 or newer with manualTrigger, code, set, if, merge and noOp nodes.
  • Built-in Code node JavaScript only, no require, network or filesystem.
  • No credentials needed because the workflow is fully local.
  • For real data, build or receive the object inside the same Code node, or pass a serializable object in item.json.payload.
  • Adjustable MAX_NODES cap to prevent unbounded traversal.

Setup and testing in n8n

  1. Import the JSON into n8n and click Execute Workflow.
  2. Open Cycle Witness Detector and verify self_reference yields startPath $.a and closePath $.a.self.
  3. Verify two_node_cycle yields startPath $.x, closePath $.x.peer.back and sharedRefCount 1.
  4. Verify shared_only yields cycleCount 0 and sharedRefCount 2.
  5. Verify invalid_non_object (string payload) does not throw and returns a string safeCopy.
  6. Open Build Final Report and confirm one item with totalCycles 2 and reportSerializable true.
  7. For real data, replace fixture specs with logic that builds your object inside the detector, or pass a serializable object in payload.

Service Value

Ready-made workflow template for automation delivery and service execution.

Apps Used

Manual Trigger Code If Set No Op Merge

Details

Trigger Manual trigger
Nodes 9
Apps 6
Views 43
Downloads 3

How to Use

  1. 1.Click "Download Template"
  2. 2.Open your n8n dashboard
  3. 3.Go to Workflows > Import from File
  4. 4.Select downloaded file and configure credentials

Nodes Used (9)

/

Manual Trigger

Manual Trigger

#1

Build Fictional Samples

Code

#2

Cycle Witness Detector

Code

#3

Has Cycles?

If

#4

Tag With Cycles

Set

#5

Tag Clean

Set

#6

Clean Branch Marker

No Op

#7

Combine Branches

Merge

#8

Build Final Report

Code

#9

Note Input Stage

Sticky Note

#10

Note Detection Stage

Sticky Note

#11

Note Routing Stage

Sticky Note

#12
Stay in the loop

Fresh workflows, straight to your inbox

Get newly published DROPIDEA workflows, a clear overview of what they do, and a link to explore them.

  • New workflows
  • Clear overviews
  • Setup guides

Be first to discover what is new

Your name, your email, and one confirmation.

Email confirmation · Unsubscribe anytime