---
url: >-
  https://adk.nht.io/api/@nhtio/adk/batteries/orchestration/plan/functions/findCycle.md
---

# Function: findCycle()

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

Defined in: [src/batteries/orchestration/plan.ts:207](https://github.com/NHTIO/ADK/blob/v1.20260906.0/src/src/batteries/orchestration/plan.ts#L207)

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

## Parameters

| Parameter | Type                                                                                                           | Description          |
| --------- | -------------------------------------------------------------------------------------------------------------- | -------------------- |
| `view`    | | [`RawPlanView`](../../types/interfaces/RawPlanView.md) | [`PlanGraphView`](../interfaces/PlanGraphView.md) | The 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.
