Skip to content

feat(subagent): ✨ Add nested subagent delegation with configurable depth limits#223

Merged
jorben merged 4 commits into
masterfrom
feat/subagent-delegation
Jun 7, 2026
Merged

feat(subagent): ✨ Add nested subagent delegation with configurable depth limits#223
jorben merged 4 commits into
masterfrom
feat/subagent-delegation

Conversation

@HayWolf

@HayWolf HayWolf commented Jun 7, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Add per-agent delegation capability bits: can_delegate and max_delegation_depth
  • Move delegation resolution from AgentSession into HelperAgentOrchestrator to support recursive delegation
  • Support delegation chains like main → review → explore with depth-based gating
  • Pre-filter delegable tools at injection time + runtime depth enforcement
  • Custom agents can configure delegation via Settings UI (toggle + depth selector 1–5)
  • Cancel cascading preserves abort coverage across all descendant helpers sharing the same run_id

Changes

Backend (Rust)

  • New migration adding can_delegate / max_delegation_depth columns to custom_subagents
  • SubagentProfile extended with can_delegate() / max_delegation_depth() methods
  • delegation_tools_for_helper() injects agent_* tools for subagents when permitted by depth
  • run_helper tool_executor intercepts agent_* calls and recurses into run_child_delegation
  • HelperRunRequest carries delegation_depth, model_plan, and custom targets
  • Cancel cancelling drains all helpers registered under the same run_id

Frontend (TypeScript/React)

  • CustomSubagent type + bridge commands extended with canDelegate / maxDelegationDepth
  • Agents settings panel: delegation toggle + depth selector for custom agents
  • Built-in agent cards show static capability badges
  • 8 i18n keys added (en + zh-CN)

Test Plan

  • cargo fmt --check passes
  • cargo test --locked — 800 unit + 283 integration tests pass
  • npm run typecheck passes
  • npm run test:unit — 840 Vitest tests pass
  • Manual smoke: main → review → explore delegation chain with correct depth events
  • Manual cancel: cancel mid-chain and verify all grandchildren abort

🤖 Generated with TiyCode

jorben added 4 commits June 6, 2026 22:31
…mits

Implement multi-level subagent delegation where agents can delegate
tasks to other subagents, with safety bounds:

- Add `can_delegate` and `max_delegation_depth` fields to custom
  subagent model, DTO, and persistence layer
- Introduce `HelperDelegationContext` for recursive delegation in the
  orchestrator, with `run_helper_boxed` to break the infinite
  opaque-type recursion cycle
- Enforce global max depth (5) and per-agent max delegation depth
- Built-in explore cannot delegate; review can delegate up to depth 3
- Refactor `resolve_custom_subagent_profile` into reusable free
  functions for pool-based access from both main session and helpers
- Support `agent_parallel` delegation from delegating helpers
  (sequential at deeper levels to bound resource usage)
- Add UI controls for delegation settings in agents settings panel
- Add i18n strings for both en and zh-CN locales
… cancellation

- Extract `validate_delegation_capability` as a standalone pure
  function shared by the recursive subagent path, improving
  testability
- Add unit tests covering all delegation validation edge cases
- Move `MAIN_AGENT_CHILD_DEPTH` to module-level constant for reuse
- Cancel in-flight subagents on checkpoint to prevent helpers from
  continuing mid-LLM-turn after abort signal
- Extract `SUBAGENT_COLUMNS_PREFIXED` constant to avoid duplicating
  qualified column lists in JOIN queries
- Add DB CHECK constraint tests for delegation field boundary values
- Show delegation capability badges in built-in agent settings UI
- Add i18n keys for "No delegation" badge label
When a run was cancelled, in-flight helper agents could remain stuck at
`running` in the database because their cleanup futures were dropped
before they could write a terminal status. Additionally, concurrent
cancellation and normal completion could emit duplicate `SubagentFailed`
events.

- Add `RegisteredHelper` struct to retain metadata needed for synthetic
  terminal event synthesis during cancellation
- Check `session_abort_signal` before inserting a `running` row to avoid
  spawning helpers for already-cancelled sessions
- Handle the race between DB insert and cancellation registration by
  proactively marking and emitting `SubagentFailed`
- Make `mark_failed` return `bool` and guard against overwriting terminal
  states, preventing duplicate event emission
- Add `mark_interrupted_if_active` for safe proactive interruption
  during `cancel_run`
- Replace native `<select>` with custom `Select` component in agents
  settings panel for consistent UI
@github-actions

github-actions Bot commented Jun 7, 2026

Copy link
Copy Markdown

AI Code Review Summary

PR: #223 (feat(subagent): ✨ Add nested subagent delegation with configurable depth limits)
Preferred language: English

Overall Assessment

No blocking issue was detected in the reviewed diff; keep focused regression testing before merge.

Major Findings by Severity

No major issues identified from the reviewed diff.

Actionable Suggestions

  • Address the highest severity findings first and add targeted tests for changed logic.

Potential Risks

  • Potential hidden risks remain in edge cases not covered by the current diff context.

Test Suggestions

  • Add happy-path + boundary + failure-path tests for touched modules.

File-Level Coverage Notes

  • src-tauri/src/core/agent_session_execution.rs: Refactored to share delegation task extraction and add depth enforcement for the main agent's direct helpers. Removed redundant code. No regressions.
  • src-tauri/src/core/agent_session_tests.rs: Updated test fixtures with new SubagentProfile fields. No functional changes to test coverage; still covers prompt assembly scenarios.
  • src-tauri/src/core/agent_session_tools.rs: New free functions for resolving custom subagent profiles and listing accessible custom delegation targets directly from the pool. Properly checks active profile and access rights. No issues.
  • src-tauri/src/core/subagent/orchestrator.rs: Major extension to support recursive delegation. Added HelperDelegationContext, resolve_delegation_task, validate_delegation_capability, parallel delegation via FuturesUnordered, and refined cancellation/status handling. The code correctly propagates depth, model plan, and custom targets. Cancellation logic now proactively marks helpers as interrupted and avoids double reporting. No defects found.
  • src-tauri/src/core/subagent/runtime_orchestration.rs: Added GLOBAL_MAX_DELEGATION_DEPTH, BUILTIN_DEFAULT_MAX_DELEGATION_DEPTH, new fields to SubagentProfile::Custom, CustomDelegationTarget, and methods for delegation capabilities and tool injection. The logic for filtering delegation tools respects depth bounds and global limits. Tests cover the vital behaviors. No issues.
  • src-tauri/src/model/subagent.rs: Added can_delegate and max_delegation_depth fields to record, DTO, and input structs. Minimal change; the fields are properly integrated into conversion impl.
  • src-tauri/src/persistence/repo/custom_subagent_repo.rs: Added new columns to the custom_subagents table and updated all queries. Input values are clamped (max_delegation_depth 1-5, can_delegate 0/1). Column lists are now shared constants. SQL injection protections are maintained. No regressions.
  • src-tauri/src/persistence/repo/run_helper_repo.rs: Enhanced mark_failed to return a bool and guard against overwriting terminal statuses. Added mark_interrupted_if_active for safe cancellation handling. New tests cover no-op behavior and terminal-status preservation. No issues.
  • src-tauri/tests/custom_subagent.rs: Added integration tests for delegation field persistence, clamping, and DB constraint enforcement. Comprehensive and well-written.
  • src/i18n/locales/en.ts: Added English translation keys for delegation UI. No functional impact.
  • src/i18n/locales/zh-CN.ts: Added Chinese translation keys for delegation UI. No functional impact.
  • src/modules/settings-center/model/types.ts: Added canDelegate and maxDelegationDepth to CustomSubagent type. Aligns with backend model. No issues.
  • src/modules/settings-center/ui/agents-settings-panel.tsx: Added UI controls for delegation settings (toggle, depth selector) and visual badges for built-in and custom agents. The code follows existing patterns. No logic errors.
  • src/services/bridge/subagent-commands.ts: File was in scope but not fully reviewed before budget limits. (planner_signaled_done_before_coverage)

Inline Downgraded Items (processed but not inline)

  • None

Coverage Status

  • Target files: 14
  • Covered files: 13
  • Uncovered files: 1
  • No-patch/binary covered as file-level: 0
  • Findings with unknown confidence (N/A): 0

Uncovered list:

  • src/services/bridge/subagent-commands.ts: planner_signaled_done_before_coverage

No-patch covered list:

  • None

Runtime/Budget

  • Rounds used: 1/4
  • Planned batches: 1
  • Executed batches: 1
  • Sub-agent runs: 1
  • Planner calls: 1
  • Reviewer calls: 1
  • Model calls: 2/64
  • Structured-output summary-only degradation: NO

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated PR review completed.

  • Findings kept: 0
  • Findings with unknown confidence: 0
  • Inline comments attempted: 1
  • Target files: 14
  • Covered files: 13
  • Uncovered files: 1
    See the summary comment for detailed analysis and coverage details.

@@ -49,32 +49,22 @@ struct ResolvedHelperDelegate {
model_role: ResolvedModelRole,

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review completed for this PR diff. No concrete inline issue was selected after aggregation.

@jorben jorben merged commit 55e774b into master Jun 7, 2026
4 checks passed
@jorben jorben deleted the feat/subagent-delegation branch June 7, 2026 02:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants