Repository navigation
feat(core): allow sub-agents to spawn nested sub-agents up to a configurable depth #6189
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
Changes from 5 commits
b8e8046
2c50077
819739a
07c002b
5477164
ed1177b
4b0c5ac
6598ab8
699cb26
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -206,6 +206,7 @@ export interface CliArgs { | |
| maxSessionTurns: number | undefined; | ||
| maxWallTime: string | undefined; | ||
| maxToolCalls: number | undefined; | ||
| maxSubagentDepth: number | undefined; | ||
| coreTools: string[] | undefined; | ||
| excludeTools: string[] | undefined; | ||
| disabledSlashCommands: string[] | undefined; | ||
|
|
@@ -887,6 +888,11 @@ export async function parseArguments(): Promise<CliArgs> { | |
| description: | ||
| 'Maximum cumulative tool calls executed during the run (success or failure; `structured_output` under --json-schema is exempt). Aborts with exit code 55 when exceeded. -1 / unset means no limit; 0 means "no tool calls allowed" (first call aborts). Capped at 1,000,000 to catch typos.', | ||
| }) | ||
| .option('max-subagent-depth', { | ||
| type: 'number', | ||
| description: | ||
| 'Maximum sub-agent nesting depth (1-based levels). 1 keeps sub-agents available but disables nesting; capped at 100. Overrides model.maxSubagentDepth from settings. Defaults to 5.', | ||
| }) | ||
| .option('core-tools', { | ||
| type: 'array', | ||
| string: true, | ||
|
|
@@ -1235,6 +1241,32 @@ function resolveMaxToolCalls(argv: CliArgs, settings: Settings): number { | |
| return -1; | ||
| } | ||
|
|
||
| /** | ||
| * Resolves the sub-agent nesting cap. Order of precedence: | ||
| * `--max-subagent-depth` flag, then `model.maxSubagentDepth` from settings, | ||
| * else undefined (Config applies the default of 5). | ||
| * | ||
| * Yargs accepts `NaN` from non-numeric flag values, and Config's clamp | ||
| * would silently fall back to the default — validate up front so a typo | ||
| * fails loudly. Settings values stay lenient (Config clamps them) so a bad | ||
| * settings.json cannot break startup. | ||
| */ | ||
| function resolveMaxSubagentDepth( | ||
| argv: CliArgs, | ||
| settings: Settings, | ||
| ): number | undefined { | ||
| const value = argv.maxSubagentDepth; | ||
| if (value !== undefined && value !== null) { | ||
| if (!Number.isInteger(value) || value < 1 || value > 100) { | ||
| throw new Error( | ||
| `--max-subagent-depth must be an integer between 1 and 100; got ${value}.`, | ||
| ); | ||
| } | ||
| return value; | ||
| } | ||
| return settings.model?.maxSubagentDepth; | ||
| } | ||
|
|
||
| export function isDebugMode(argv: CliArgs): boolean { | ||
| if (argv.debug) return true; | ||
| const debugVal = process.env['DEBUG']; | ||
|
|
@@ -1996,6 +2028,8 @@ export async function loadCliConfig( | |
| argv.maxSessionTurns ?? settings.model?.maxSessionTurns ?? -1, | ||
| maxWallTimeSeconds: resolveMaxWallTimeSeconds(argv, settings), | ||
| maxToolCalls: resolveMaxToolCalls(argv, settings), | ||
| // Undefined flows through to Config's default (5) and clamp logic. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Suggestion] No — qwen3.7-max via Qwen Code /review
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added in 2c50077 — |
||
| maxSubagentDepth: resolveMaxSubagentDepth(argv, settings), | ||
| experimentalZedIntegration: argv.acp || argv.experimentalAcp || false, | ||
| cronEnabled: settings.experimental?.cron ?? true, | ||
| agentTeamEnabled: settings.experimental?.agentTeam ?? false, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1028,7 +1028,11 @@ export class BackgroundAgentResumeService { | |
| } | ||
| }; | ||
|
|
||
| const framedRunBody = () => runWithAgentContext(meta.agentId, runBody); | ||
| // Restore the persisted launch depth so a resumed nested agent keeps | ||
| // its original nesting level (and spawn eligibility) instead of | ||
| // recomputing to depth 0 from this top-level resume frame. | ||
| const framedRunBody = () => | ||
| runWithAgentContext(meta.agentId, runBody, meta.depth); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
So a malformed or hand-edited sidecar with This is exactly the threat model the same resume path already defends against for the sibling field: Suggest mirroring the normalize pattern before pinning: const restoredDepth = Number.isFinite(meta.depth)
? Math.max(0, Math.floor(meta.depth!))
: undefined; // fall back to auto-increment
const framedRunBody = () =>
runWithAgentContext(meta.agentId, runBody, restoredDepth);
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 4b0c5ac — |
||
| void (target.isFork ? runInForkContext(framedRunBody) : framedRunBody()); | ||
| return entry; | ||
| } catch (error) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cleanup: the {default 5, min 1, max 100} triple is hand-duplicated at three sites.
This validator (bounds in the condition and the error text),
normalizeMaxSubagentDepthin coreconfig.ts, andsettingsSchema.ts'sdefault: 5(the vscodesettings.schema.jsonis generated from it, so that one's free). If core's cap or default ever changes, this flag validator drifts — and the failure mode is precisely the one this function's docstring says it exists to prevent: an explicitly passed--max-subagent-depthaccepted here, then silently clamped by core.The codebase already has the exact pattern for this:
DEFAULT_STOP_HOOK_BLOCK_CAPandSENSITIVE_SPAN_ATTRIBUTE_MAX_LENGTH_LIMITare exported from core and consumed bysettingsSchema.ts(both asdefault:andmaximum:). Suggest exportingDEFAULT_MAX_SUBAGENT_DEPTH/MAX_SUBAGENT_DEPTH_LIMITnext tonormalizeMaxSubagentDepth, using them there, here (condition + interpolated message), and in the schema default.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in 4b0c5ac following the DEFAULT_STOP_HOOK_BLOCK_CAP pattern —
DEFAULT_MAX_SUBAGENT_DEPTHandMAX_SUBAGENT_DEPTH_LIMITare exported from core next to the normalizer and consumed by it, the CLI flag validator (condition + interpolated error message), and settingsSchema'sdefault:. The vscode schema stays generated.