@@ -61,6 +61,8 @@ public final class WorkflowAgent implements Agent {
6161
6262 private final ObjectMapper mapper ;
6363
64+ private final int subflowMaxDepth ;
65+
6466 private final Map <String , StoredContinuation > continuations = new ConcurrentHashMap <>();
6567
6668 private WorkflowAgent (Builder builder ) {
@@ -70,6 +72,7 @@ private WorkflowAgent(Builder builder) {
7072 this .tools = Objects .requireNonNull (builder .tools , "tools must not be null" );
7173 this .registry = Objects .requireNonNull (builder .registry , "registry must not be null" );
7274 this .mapper = Objects .requireNonNull (builder .mapper , "mapper must not be null" );
75+ this .subflowMaxDepth = builder .subflowMaxDepth ;
7376 }
7477
7578 public static Builder builder (WorkflowDefinition definition ) {
@@ -101,6 +104,8 @@ public static final class Builder {
101104
102105 private ObjectMapper mapper = new ObjectMapper ();
103106
107+ private int subflowMaxDepth = 16 ;
108+
104109 private Builder (WorkflowDefinition definition ) {
105110 this .definition = Objects .requireNonNull (definition , "definition must not be null" );
106111 }
@@ -136,6 +141,14 @@ public Builder objectMapper(ObjectMapper mapper) {
136141 return this ;
137142 }
138143
144+ public Builder subflowMaxDepth (int subflowMaxDepth ) {
145+ if (subflowMaxDepth < 1 ) {
146+ throw new IllegalArgumentException ("subflowMaxDepth must be greater than or equal to 1" );
147+ }
148+ this .subflowMaxDepth = subflowMaxDepth ;
149+ return this ;
150+ }
151+
139152 public WorkflowAgent build () {
140153 return new WorkflowAgent (this );
141154 }
@@ -307,12 +320,52 @@ private ExecutionResult executeStep(WorkflowStep step, RunData data, AgentExecut
307320 throw new WorkflowExecutionException ("Workflow loop exceeded maxIterations: " + loop .id ());
308321 }
309322 if (step instanceof WorkflowStep .Subflow subflow ) {
310- WorkflowDefinition nested = registry .get (subflow .workflowId (), subflow .version ());
311- return executeSteps (nested .steps (), 0 , data , context );
323+ return executeSubflow (subflow , data , context );
312324 }
313325 throw new WorkflowExecutionException ("Unsupported workflow step: " + step .id ());
314326 }
315327
328+ /**
329+ * Enters a subflow through the run's call chain (issue #273): a reference that is
330+ * already on the chain closes a cycle, a chain at the configured depth limit is
331+ * refused before recursing, and both fail with a domain error locating the
332+ * reference chain instead of exhausting the JVM stack. The chain lives on the run
333+ * data, so it survives suspensions and is copied per parallel branch. A
334+ * suspension removes the entry from the chain on the way out: the resumed work
335+ * continues inside the subflow, and re-entering the same subflow from the resumed
336+ * steps is sequential reuse, not a cycle — the chain check still bounds whatever
337+ * the resumed segment does.
338+ */
339+ private ExecutionResult executeSubflow (WorkflowStep .Subflow subflow , RunData data , AgentExecutionContext context ) {
340+ String node = subflow .workflowId () + "@" + subflow .version ();
341+ if (data .subflowChain .contains (node )) {
342+ List <String > cycle = new ArrayList <>(data .subflowChain .subList (data .subflowChain .indexOf (node ),
343+ data .subflowChain .size ()));
344+ cycle .add (node );
345+ throw new WorkflowSubflowRecursionException (
346+ "Workflow subflow reference closes a cycle on the call chain: " + String .join (" -> " , cycle ),
347+ cycle );
348+ }
349+ if (data .subflowChain .size () >= subflowMaxDepth ) {
350+ // The reported chain starts at the executing definition, so the diagnosis
351+ // shows the whole path from the root of the run.
352+ List <String > extended = new ArrayList <>();
353+ extended .add (definition .id () + "@" + definition .version ());
354+ extended .addAll (data .subflowChain );
355+ extended .add (node );
356+ throw new WorkflowSubflowRecursionException ("Workflow subflow call chain exceeded the configured depth "
357+ + "limit " + subflowMaxDepth + ": " + String .join (" -> " , extended ), extended );
358+ }
359+ WorkflowDefinition nested = registry .get (subflow .workflowId (), subflow .version ());
360+ data .subflowChain .add (node );
361+ try {
362+ return executeSteps (nested .steps (), 0 , data , context );
363+ }
364+ finally {
365+ data .subflowChain .remove (data .subflowChain .size () - 1 );
366+ }
367+ }
368+
316369 private ExecutionResult executeAgent (WorkflowStep .Agent step , RunData data , AgentExecutionContext context ) {
317370 Agent agent = Objects .requireNonNull (agentResolver .apply (step .agentRef ()),
318371 "agent resolver returned no agent for " + step .agentRef ());
@@ -618,6 +671,10 @@ private static final class RunData {
618671
619672 private final List <ChatMessage > messages ;
620673
674+ // The subflow call chain (id@version, caller first) of this run; survives
675+ // suspensions through the continuation closures and is copied per branch.
676+ private final List <String > subflowChain ;
677+
621678 private Long inputTokens = 0L ;
622679
623680 private Long outputTokens = 0L ;
@@ -629,13 +686,15 @@ private RunData(AgentRequest request) {
629686 this .state = new LinkedHashMap <>(request .inputs ());
630687 this .stepOutputs = new LinkedHashMap <>();
631688 this .messages = new ArrayList <>(request .messages ());
689+ this .subflowChain = new ArrayList <>();
632690 }
633691
634692 private RunData (RunData source ) {
635693 this .inputs = new LinkedHashMap <>(source .inputs );
636694 this .state = new LinkedHashMap <>(source .state );
637695 this .stepOutputs = new LinkedHashMap <>(source .stepOutputs );
638696 this .messages = new ArrayList <>(source .messages );
697+ this .subflowChain = new ArrayList <>(source .subflowChain );
639698 this .inputTokens = source .inputTokens ;
640699 this .outputTokens = source .outputTokens ;
641700 this .totalTokens = source .totalTokens ;
0 commit comments