Repository navigation
feat(agent): cap runtime subagent spawn depth at MAX_SPAWN_DEPTH=3 #2234
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
senamakel
merged 3 commits into
tinyhumansai:main
from
srikanthakkaru:feat/agent-spawn-depth-gate
May 19, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| //! Task-local spawn-depth tracking for nested sub-agent delegation. | ||
| //! | ||
| //! Loader-time tier validation prevents built-in and workspace agent | ||
| //! definitions from declaring recursive hierarchies, but runtime calls can | ||
| //! still arrive through tools and MCP surfaces. This task-local is the | ||
| //! defence-in-depth layer that caps the active `run_subagent` chain. | ||
|
|
||
| /// Maximum number of nested `run_subagent` scopes allowed in one task. | ||
| /// | ||
| /// Depth counts sub-agent runs, not the root user-facing agent turn, so the | ||
| /// intended deepest path is `chat -> reasoning -> worker`: | ||
| /// | ||
| /// * reasoning sub-agent: depth 1 | ||
| /// * worker spawned by reasoning: depth 2 | ||
| /// * one final worker handoff: depth 3 | ||
| pub const MAX_SPAWN_DEPTH: usize = 3; | ||
|
|
||
| tokio::task_local! { | ||
| /// Current active `run_subagent` nesting depth for this task. | ||
| static CURRENT_SPAWN_DEPTH: usize; | ||
| } | ||
|
|
||
| /// Return the active sub-agent nesting depth for this task. | ||
| /// | ||
| /// Direct callers outside [`with_spawn_depth`] are at depth 0. | ||
| pub fn current_spawn_depth() -> usize { | ||
| CURRENT_SPAWN_DEPTH.try_with(|depth| *depth).unwrap_or(0) | ||
| } | ||
|
|
||
| /// Run `future` with a specific active sub-agent depth. | ||
| pub async fn with_spawn_depth<F, R>(depth: usize, future: F) -> R | ||
| where | ||
| F: std::future::Future<Output = R>, | ||
| { | ||
| CURRENT_SPAWN_DEPTH.scope(depth, future).await | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[tokio::test] | ||
| async fn current_spawn_depth_defaults_to_zero() { | ||
| assert_eq!(current_spawn_depth(), 0); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn with_spawn_depth_scopes_value_to_future() { | ||
| let observed = with_spawn_depth(2, async { current_spawn_depth() }).await; | ||
| assert_eq!(observed, 2); | ||
| assert_eq!(current_spawn_depth(), 0); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn nested_spawn_depth_scope_restores_outer_value() { | ||
| with_spawn_depth(1, async { | ||
| assert_eq!(current_spawn_depth(), 1); | ||
| with_spawn_depth(2, async { | ||
| assert_eq!(current_spawn_depth(), 2); | ||
| }) | ||
| .await; | ||
| assert_eq!(current_spawn_depth(), 1); | ||
| }) | ||
| .await; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.