agent-delegate

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Agent Delegate Skill

Agent Delegate Skill

Spawn specialized sub-agents to handle specific phases of complex tasks. Each delegate receives curated context from the parent session and returns structured results that merge back into the main conversation.
生成专业子代理以处理复杂任务的特定阶段。每个代理会从父会话中接收精心整理的上下文,并返回结构化结果,这些结果会合并回主对话中。

When to Use

适用场景

USE this skill when:
  • A task requires multiple distinct areas of expertise (security review + performance + style)
  • You need to break down a complex problem into specialized phases
  • Different parts of a task need different system prompts/personalities
  • You want parallel execution of independent subtasks
  • Building recursive problem-solving workflows
DON'T use for:
  • Simple single-focus tasks (delegate adds overhead)
  • Tasks requiring continuous human interaction
  • When you already have the expertise needed
  • Deep recursion (capped at depth 3 to prevent infinite loops)
在以下场景使用该技能:
  • 任务需要多个不同领域的专业知识(安全审查 + 性能优化 + 代码风格检查)
  • 需要将复杂问题拆解为多个专业阶段处理
  • 任务的不同部分需要不同的系统提示词/角色设定
  • 需要并行执行独立的子任务
  • 构建递归式问题解决工作流
请勿在以下场景使用:
  • 简单的单一焦点任务(委托会增加额外开销)
  • 需要持续人机交互的任务
  • 您已具备完成任务所需专业知识的情况
  • 深度递归场景(已限制最大深度为3,防止无限循环)

Architecture

架构

┌─────────────────────────────────────────┐
│         Parent Agent Session            │
│  "Review this entire codebase"          │
└──────────────┬──────────────────────────┘
               │ delegate()
    ┌──────────────────────┐
    │  Delegate Context    │
    │  - Parent prompt     │
    │  - Relevant files    │
    │  - Parent's analysis │
    │  - Depth counter     │
    └──────────┬───────────┘
    ┌──────────▼───────────┐
    │  Sub-Agent Session   │
    │  "Check for security │
    │   vulnerabilities"   │
    └──────────┬───────────┘
    ┌──────────────────────┐
    │  Structured Result   │
    │  { findings, files,  │
    │    severity, next }  │
    └──────────────────────┘
┌─────────────────────────────────────────┐
│         Parent Agent Session            │
│  "Review this entire codebase"          │
└──────────────┬──────────────────────────┘
               │ delegate()
    ┌──────────────────────┐
    │  Delegate Context    │
    │  - Parent prompt     │
    │  - Relevant files    │
    │  - Parent's analysis │
    │  - Depth counter     │
    └──────────┬───────────┘
    ┌──────────▼───────────┐
    │  Sub-Agent Session   │
    │  "Check for security │
    │   vulnerabilities"   │
    └──────────┬───────────┘
    ┌──────────────────────┐
    │  Structured Result   │
    │  { findings, files,  │
    │    severity, next }  │
    └──────────────────────┘

Commands

命令

bash
undefined
bash
undefined

Delegate a subtask with default settings

Delegate a subtask with default settings

node /job/.pi/skills/agent-delegate/delegate.js
--task "Review for security vulnerabilities"
node /job/.pi/skills/agent-delegate/delegate.js
--task "Review for security vulnerabilities"

Delegate with custom system prompt

Delegate with custom system prompt

node /job/.pi/skills/agent-delegate/delegate.js
--task "Analyze performance bottlenecks"
--system "You are a performance optimization expert"
--model "claude-sonnet-4-5-20250929"
node /job/.pi/skills/agent-delegate/delegate.js
--task "Analyze performance bottlenecks"
--system "You are a performance optimization expert"
--model "claude-sonnet-4-5-20250929"

Delegate with file context

Delegate with file context

node /job/.pi/skills/agent-delegate/delegate.js
--task "Review this code"
--files "./src/auth.ts ./src/api.ts"
node /job/.pi/skills/agent-delegate/delegate.js
--task "Review this code"
--files "./src/auth.ts ./src/api.ts"

Parallel delegation (spawn multiple agents)

Parallel delegation (spawn multiple agents)

node /job/.pi/skills/agent-delegate/delegate.js
--parallel
--task-1 "Check for security issues"
--task-2 "Check for performance issues"
--task-3 "Check for code style"
node /job/.pi/skills/agent-delegate/delegate.js
--parallel
--task-1 "Check for security issues"
--task-2 "Check for performance issues"
--task-3 "Check for code style"

With parent context

With parent context

node /job/.pi/skills/agent-delegate/delegate.js
--task "Build on this analysis"
--context "./parent_analysis.md"
undefined
node /job/.pi/skills/agent-delegate/delegate.js
--task "Build on this analysis"
--context "./parent_analysis.md"
undefined

Command-Line Options

命令行选项

OptionDescriptionDefault
--task
The task prompt for the delegateRequired
--system
Custom system prompt for delegateInherited from parent
--model
LLM model to use
claude-sonnet-4-5-20250929
--files
Space-separated file paths to includeNone
--context
Parent analysis/context fileNone
--parallel
Enable parallel delegation modefalse
--task-N
Task N for parallel mode (1-5)N/A
--depth
Current delegation depth (internal)0
--max-depth
Maximum recursion depth3
--output
Output file for resultsstdout
--verbose
Verbose loggingfalse
选项描述默认值
--task
代理的任务提示词必填
--system
代理的自定义系统提示词继承自父代理
--model
使用的LLM模型
claude-sonnet-4-5-20250929
--files
要包含的空格分隔文件路径
--context
父代理分析/上下文文件
--parallel
启用并行委托模式false
--task-N
并行模式下的第N个任务(1-5)N/A
--depth
当前委托深度(内部使用)0
--max-depth
最大递归深度3
--output
结果输出文件stdout
--verbose
详细日志模式false

Delegation Depth & Loop Prevention

委托深度与循环预防

To prevent infinite delegation loops:
  • Depth 0: Parent agent (you)
  • Depth 1: First-level delegates (spawned by parent)
  • Depth 2: Second-level delegates (spawned by depth-1)
  • Depth 3: Maximum - delegates at this depth cannot spawn further delegates
The delegate skill automatically tracks depth via the
DELEGATE_DEPTH
environment variable. Attempts to delegate beyond max depth return an error.
为防止无限委托循环:
  • 深度0:父代理(您)
  • 深度1:一级代理(由父代理生成)
  • 深度2:二级代理(由一级代理生成)
  • 深度3:最大深度 - 此深度的代理无法生成更多代理
委托技能通过
DELEGATE_DEPTH
环境变量自动跟踪深度。尝试超过最大深度进行委托会返回错误。

Context Handoff

上下文传递

When you delegate, the sub-agent receives:
  1. Task prompt - What to do
  2. System prompt - How to behave (expertise persona)
  3. File context - Relevant files from parent session
  4. Parent analysis - Your findings so far (optional)
  5. Depth metadata - Prevents infinite recursion
当您进行委托时,子代理会收到:
  1. 任务提示词 - 需要完成的工作
  2. 系统提示词 - 行为方式(专业角色设定)
  3. 文件上下文 - 父会话中的相关文件
  4. 父代理分析结果 - 您目前的发现(可选)
  5. 深度元数据 - 防止无限递归

Context Packaging

上下文打包

javascript
{
  delegateTask: "Check for SQL injection vulnerabilities",
  parentAnalysis: "Found 3 database queries in auth.ts...",
  relevantFiles: [
    { path: "./src/auth.ts", content: "..." },
    { path: "./src/db.ts", content: "..." }
  ],
  delegationDepth: 1,
  maxDepth: 3,
  parentSession: "job/abc123"
}
javascript
{
  delegateTask: "Check for SQL injection vulnerabilities",
  parentAnalysis: "Found 3 database queries in auth.ts...",
  relevantFiles: [
    { path: "./src/auth.ts", content: "..." },
    { path: "./src/db.ts", content: "..." }
  ],
  delegationDepth: 1,
  maxDepth: 3,
  parentSession: "job/abc123"
}

Structured Output Format

结构化输出格式

Delegates return JSON-structured results:
json
{
  "delegateTask": "Check for SQL injection vulnerabilities",
  "delegationDepth": 1,
  "status": "complete",
  "findings": [
    {
      "file": "./src/auth.ts",
      "line": 42,
      "severity": "high",
      "issue": "Potential SQL injection via unsanitized input",
      "codeSnippet": "const query = `SELECT * FROM users WHERE id = ${userId}`;",
      "recommendation": "Use parameterized queries"
    }
  ],
  "summary": "Found 2 high-severity SQL injection risks",
  "filesAnalyzed": 5,
  "nextSteps": [
    "Review ./src/api.ts for similar patterns",
    "Add input validation layer"
  ],
  "allowFurtherDelegation": true
}
代理返回JSON格式的结构化结果:
json
{
  "delegateTask": "Check for SQL injection vulnerabilities",
  "delegationDepth": 1,
  "status": "complete",
  "findings": [
    {
      "file": "./src/auth.ts",
      "line": 42,
      "severity": "high",
      "issue": "Potential SQL injection via unsanitized input",
      "codeSnippet": "const query = `SELECT * FROM users WHERE id = ${userId}`;",
      "recommendation": "Use parameterized queries"
    }
  ],
  "summary": "Found 2 high-severity SQL injection risks",
  "filesAnalyzed": 5,
  "nextSteps": [
    "Review ./src/api.ts for similar patterns",
    "Add input validation layer"
  ],
  "allowFurtherDelegation": true
}

Examples

示例

Example 1: Code Review with Specialized Delegates

示例1:使用专业代理进行代码审查

bash
undefined
bash
undefined

Parent: "Review this PR comprehensively"

Parent: "Review this PR comprehensively"

Delegate security review

Delegate security review

node /job/.pi/skills/agent-delegate/delegate.js
--task "Review for security vulnerabilities: SQL injection, XSS, auth bypass, CSRF"
--system "You are a security engineer with 10 years experience. Focus on OWASP Top 10."
--files "./src/auth.ts ./src/api.ts ./src/db.ts"
--output security_review.json
node /job/.pi/skills/agent-delegate/delegate.js
--task "Review for security vulnerabilities: SQL injection, XSS, auth bypass, CSRF"
--system "You are a security engineer with 10 years experience. Focus on OWASP Top 10."
--files "./src/auth.ts ./src/api.ts ./src/db.ts"
--output security_review.json

Delegate performance review

Delegate performance review

node /job/.pi/skills/agent-delegate/delegate.js
--task "Review for performance issues: N+1 queries, memory leaks, inefficient algorithms"
--system "You are a performance optimization expert. Identify bottlenecks and optimization opportunities."
--files "./src/db.ts ./src/cache.ts"
--output performance_review.json
node /job/.pi/skills/agent-delegate/delegate.js
--task "Review for performance issues: N+1 queries, memory leaks, inefficient algorithms"
--system "You are a performance optimization expert. Identify bottlenecks and optimization opportunities."
--files "./src/db.ts ./src/cache.ts"
--output performance_review.json

Merge results

Merge results

node /job/.pi/skills/agent-delegate/merge.js
--inputs security_review.json performance_review.json
--output combined_review.md
undefined
node /job/.pi/skills/agent-delegate/merge.js
--inputs security_review.json performance_review.json
--output combined_review.md
undefined

Example 2: Research with Parallel Delegates

示例2:使用并行代理进行研究

bash
undefined
bash
undefined

Parallel research delegation

Parallel research delegation

node /job/.pi/skills/agent-delegate/delegate.js
--parallel
--task-1 "Research market trends for AI agents in enterprise"
--task-2 "Identify top 10 competitors and their key features"
--task-3 "Summarize customer pain points from Reddit, Hacker News, Twitter"
--system-1 "You are a market analyst"
--system-2 "You are a competitive intelligence specialist"
--system-3 "You are a user research expert"
--output-1 market_trends.json
--output-2 competitor_analysis.json
--output-3 user_pain_points.json
undefined
node /job/.pi/skills/agent-delegate/delegate.js
--parallel
--task-1 "Research market trends for AI agents in enterprise"
--task-2 "Identify top 10 competitors and their key features"
--task-3 "Summarize customer pain points from Reddit, Hacker News, Twitter"
--system-1 "You are a market analyst"
--system-2 "You are a competitive intelligence specialist"
--system-3 "You are a user research expert"
--output-1 market_trends.json
--output-2 competitor_analysis.json
--output-3 user_pain_points.json
undefined

Example 3: Multi-Phase Problem Solving

示例3:多阶段问题解决

bash
undefined
bash
undefined

Phase 1: Analysis

Phase 1: Analysis

node /job/.pi/skills/agent-delegate/delegate.js
--task "Analyze this bug report and identify root cause candidates"
--context "./bug_report.md"
--output analysis.json
node /job/.pi/skills/agent-delegate/delegate.js
--task "Analyze this bug report and identify root cause candidates"
--context "./bug_report.md"
--output analysis.json

Phase 2: Solution Design (builds on Phase 1)

Phase 2: Solution Design (builds on Phase 1)

node /job/.pi/skills/agent-delegate/delegate.js
--task "Design a fix for the identified root cause"
--context "./analysis.json"
--system "You are a senior engineer specializing in system design"
--output solution.json
node /job/.pi/skills/agent-delegate/delegate.js
--task "Design a fix for the identified root cause"
--context "./analysis.json"
--system "You are a senior engineer specializing in system design"
--output solution.json

Phase 3: Implementation Plan

Phase 3: Implementation Plan

node /job/.pi/skills/agent-delegate/delegate.js
--task "Create a step-by-step implementation plan with code examples"
--context "./solution.json"
--system "You are a tech lead who writes clear implementation guides"
--output implementation_plan.md
undefined
node /job/.pi/skills/agent-delegate/delegate.js
--task "Create a step-by-step implementation plan with code examples"
--context "./solution.json"
--system "You are a tech lead who writes clear implementation guides"
--output implementation_plan.md
undefined

Integration with PopeBot Jobs

与PopeBot Jobs集成

Use agent delegation inside a job by calling the delegate script:
markdown
undefined
通过调用委托脚本,在任务中使用代理委托功能:
markdown
undefined

job.md

job.md

I need to review this pull request comprehensively.
I need to review this pull request comprehensively.

Step 1: Delegate Security Review

Step 1: Delegate Security Review

First, let me spawn a security specialist:
```bash node /job/.pi/skills/agent-delegate/delegate.js
--task "Review PR #{{pr_number}} for security vulnerabilities"
--system "You are a security engineer focused on OWASP Top 10"
--files "./src/auth.ts ./src/api.ts" ```
First, let me spawn a security specialist:
```bash node /job/.pi/skills/agent-delegate/delegate.js
--task "Review PR #{{pr_number}} for security vulnerabilities"
--system "You are a security engineer focused on OWASP Top 10"
--files "./src/auth.ts ./src/api.ts" ```

Step 2: Delegate Performance Review

Step 2: Delegate Performance Review

Now a performance specialist:
```bash node /job/.pi/skills/agent-delegate/delegate.js
--task "Review PR #{{pr_number}} for performance issues"
--system "You are a performance optimization expert" ```
Now a performance specialist:
```bash node /job/.pi/skills/agent-delegate/delegate.js
--task "Review PR #{{pr_number}} for performance issues"
--system "You are a performance optimization expert" ```

Step 3: Synthesize Results

Step 3: Synthesize Results

Now I'll merge the findings...
undefined
Now I'll merge the findings...
undefined

Parallel Delegation Mode

并行委托模式

The
--parallel
flag spawns multiple delegates concurrently:
bash
node /job/.pi/skills/agent-delegate/delegate.js \
  --parallel \
  --task-1 "Security review" \
  --task-2 "Performance review" \
  --task-3 "Style review" \
  --system-1 "Security expert persona..." \
  --system-2 "Performance expert persona..." \
  --system-3 "Style expert persona..." \
  --output-1 security.json \
  --output-2 performance.json \
  --output-3 style.json \
  --synthesize "Combine all reviews into unified report"
Parallel mode behavior:
  • Spawns all delegates simultaneously (up to 5)
  • Waits for all to complete
  • Optionally runs a synthesis step to merge results
  • Returns array of all delegate results
--parallel
标志会同时生成多个代理:
bash
node /job/.pi/skills/agent-delegate/delegate.js \
  --parallel \
  --task-1 "Security review" \
  --task-2 "Performance review" \
  --task-3 "Style review" \
  --system-1 "Security expert persona..." \
  --system-2 "Performance expert persona..." \
  --system-3 "Style expert persona..." \
  --output-1 security.json \
  --output-2 performance.json \
  --output-3 style.json \
  --synthesize "Combine all reviews into unified report"
并行模式行为:
  • 同时生成所有代理(最多5个)
  • 等待所有代理完成任务
  • 可选执行合成步骤以合并结果
  • 返回所有代理结果的数组

Error Handling

错误处理

Delegation Depth Exceeded

委托深度超出限制

json
{
  "error": "MAX_DEPTH_EXCEEDED",
  "message": "Cannot delegate further: maximum depth (3) reached",
  "currentDepth": 3,
  "recommendation": "Complete this subtask directly without further delegation"
}
json
{
  "error": "MAX_DEPTH_EXCEEDED",
  "message": "Cannot delegate further: maximum depth (3) reached",
  "currentDepth": 3,
  "recommendation": "Complete this subtask directly without further delegation"
}

Task Failure

任务执行失败

json
{
  "error": "DELEGATE_FAILED",
  "message": "Sub-agent failed to complete task",
  "task": "Check for SQL injection",
  "reason": "LLM API error: rate limit exceeded",
  "retryable": true
}
json
{
  "error": "DELEGATE_FAILED",
  "message": "Sub-agent failed to complete task",
  "task": "Check for SQL injection",
  "reason": "LLM API error: rate limit exceeded",
  "retryable": true
}

Best Practices

最佳实践

When to Delegate

何时进行委托

  1. Clear expertise boundaries - Security, performance, style, UX
  2. Independent subtasks - Can run in parallel
  3. Need different personas - Each delegate has unique system prompt
  4. Complex multi-phase work - Analysis → Design → Implementation
  1. 明确的专业边界 - 安全、性能、风格、UX等领域
  2. 独立子任务 - 可并行执行的任务
  3. 需要不同角色设定 - 每个代理有独特的系统提示词
  4. 复杂多阶段工作 - 分析 → 设计 → 实现

When NOT to Delegate

何时不进行委托

  1. Simple tasks - Overhead exceeds benefit
  2. Tightly coupled work - Requires continuous back-and-forth
  3. You have the expertise - No need to spawn a clone
  4. Time-critical - Delegation adds latency
  1. 简单任务 - 开销大于收益
  2. 紧密耦合的工作 - 需要持续的双向交互
  3. 您具备相关专业知识 - 无需生成代理
  4. 时间敏感任务 - 委托会增加延迟

Effective Delegation Patterns

有效的委托模式

Good:
Parent: "Comprehensive code review"
├─ Delegate 1: Security review (OWASP focus)
├─ Delegate 2: Performance review (bottleneck focus)
└─ Delegate 3: Style review (best practices focus)
Bad:
Parent: "Review code"
├─ Delegate 1: "Find bugs"
│  └─ Delegate 2: "Find security bugs"  # Too similar!
└─ Delegate 3: "Find security issues"   # Duplicate!
推荐:
Parent: "Comprehensive code review"
├─ Delegate 1: Security review (OWASP focus)
├─ Delegate 2: Performance review (bottleneck focus)
└─ Delegate 3: Style review (best practices focus)
不推荐:
Parent: "Review code"
├─ Delegate 1: "Find bugs"
│  └─ Delegate 2: "Find security bugs"  # Too similar!
└─ Delegate 3: "Find security issues"   # Duplicate!

Testing

测试

bash
undefined
bash
undefined

Run unit tests

Run unit tests

node /job/.pi/skills/agent-delegate/tests/unit.test.js
node /job/.pi/skills/agent-delegate/tests/unit.test.js

Run integration test

Run integration test

node /job/.pi/skills/agent-delegate/tests/integration.test.js
node /job/.pi/skills/agent-delegate/tests/integration.test.js

Test depth limiting

Test depth limiting

node /job/.pi/skills/agent-delegate/tests/depth.test.js
undefined
node /job/.pi/skills/agent-delegate/tests/depth.test.js
undefined

Files

文件说明

FilePurpose
delegate.js
Main delegation executor
merge.js
Merge multiple delegate results
parallel.js
Parallel delegation coordinator
context.js
Context packaging utilities
tests/
Unit and integration tests
SKILL.md
This documentation
文件用途
delegate.js
主委托执行器
merge.js
合并多个代理结果
parallel.js
并行委托协调器
context.js
上下文打包工具
tests/
单元测试和集成测试
SKILL.md
本文档

Environment Variables

环境变量

VariableDescriptionDefault
DELEGATE_DEPTH
Current delegation depth0
DELEGATE_MAX_DEPTH
Maximum recursion depth3
DELEGATE_PARALLEL_LIMIT
Max parallel delegates5
ANTHROPIC_API_KEY
API key for ClaudeRequired
变量描述默认值
DELEGATE_DEPTH
当前委托深度0
DELEGATE_MAX_DEPTH
最大递归深度3
DELEGATE_PARALLEL_LIMIT
最大并行代理数5
ANTHROPIC_API_KEY
Claude的API密钥必填

See Also

相关链接

  • Prose Runner - Multi-session workflow orchestration
  • Workflow Orchestrator - Job-based pipelines
  • SOP System - Standard operating procedures

Inspired by ZeroClaw's delegate.rs architecture - Real-time agent-to-agent context handoff with depth limiting and structured result merging.
  • Prose Runner - 多会话工作流编排
  • Workflow Orchestrator - 基于任务的流水线
  • SOP System - 标准操作流程

灵感来自ZeroClaw的delegate.rs架构 - 具备深度限制和结构化结果合并的实时代理间上下文传递。