Skip to content
1 min read · 186 words

Function: findCycle()

ts
function findCycle(view: RawPlanView | PlanGraphView):
  | {
      edgeId: string;
      from: string;
      to: string;
    }
  | undefined;

Defined in: src/batteries/orchestration/plan.ts:207

Find a cycle in the graph, over EVERY edge handle.

Parameters

ParameterTypeDescription
view| RawPlanView | PlanGraphViewThe graph to search.

Returns

| { edgeId: string; from: string; to: string; } | undefined

The closing edge of the first cycle found, or undefined when acyclic.

Remarks

A topological sort over all edges — error and default included — because an error edge back to an ancestor is still a cycle: it can still execute, so it can still loop. A diamond fan-in (two distinct paths reaching one node) is NOT a cycle and is not reported. The function returns the CLOSING edge — the edge whose to is already on the current path — so a caller can name it in an issue. Returns undefined when the graph is acyclic.

The algorithm is an iterative DFS with three node states (unvisited / on-stack / done). When a back edge is found, the edge that closes the cycle is returned immediately.