dynamic-forms / Function

detectCycles

Detects cycles in the derivation dependency graph.

Uses Kahn's algorithm variant with DFS to detect cycles. Bidirectional sync patterns (A→B→A) are allowed because they stabilize via the equality check at runtime.

Presentation

Returns

CycleDetectionResult -

Result indicating whether a cycle exists and details if found

Parameters

NameTypeDescription
collection
DerivationCollection

The collected derivation entries

Example usage

const collection = collectDerivations(fields);
const result = detectCycles(collection);

if (result.hasCycle) {
  console.error(`Cycle detected: ${result.cyclePath?.join(' -> ')}`);
  throw new Error(result.errorMessage);
}