Skip to content

委派与并行工作

hermes agent 委派与并行工作

Hermes 可以生成隔离的子 agent,并行处理任务。每个子 agent 都有自己的 conversation、terminal session 和 toolset。只有最终摘要会返回 —— 中间的工具调用永远不会进入你的 context window。

完整功能参考请参见 Subagent Delegation。

  • 推理密集型子任务(调试、代码审查、研究综合)
  • 会用中间数据淹没你上下文的任务
  • 并行的独立工作流(同时研究 A 和 B)
  • 需要新鲜上下文、希望 agent 不带偏见处理的任务
  • 单次工具调用 → 直接使用工具
  • 步骤之间带逻辑的机械式多步骤工作 → 使用 execute_code
  • 需要用户交互的任务 → 子 agent 不能使用 clarify
  • 快速文件编辑 → 直接处理
  • 需要在当前轮次之外持续运行的持久长任务 → 使用 cronjobterminal(background=True, notify_on_complete=True)delegate_task 是同步的:如果父轮次被中断,活跃的子任务会被取消,其工作会被丢弃。

同时研究三个主题,并获取结构化摘要:

Research these three topics in parallel:
1. Current state of WebAssembly outside the browser
2. RISC-V server chip adoption in 2025
3. Practical quantum computing applications
Focus on recent developments and key players.

在幕后,Hermes 会使用:

delegate_task(tasks=[
{
"goal": "Research WebAssembly outside the browser in 2025",
"context": "Focus on: runtimes (Wasmtime, Wasmer), cloud/edge use cases, WASI progress",
"toolsets": ["web"]
},
{
"goal": "Research RISC-V server chip adoption",
"context": "Focus on: server chips shipping, cloud providers adopting, software ecosystem",
"toolsets": ["web"]
},
{
"goal": "Research practical quantum computing applications",
"context": "Focus on: error correction breakthroughs, real-world use cases, key companies",
"toolsets": ["web"]
}
])

三个任务会并发运行。每个子 agent 都会独立搜索 web,并返回摘要。然后父 agent 会将它们综合成一份连贯的简报。

将安全审查委派给一个 fresh-context 子 agent,让它在没有先入为主判断的情况下审查代码:

Review the authentication module at src/auth/ for security issues.
Check for SQL injection, JWT validation problems, password handling,
and session management. Fix anything you find and run the tests.

关键是 context 字段 —— 它必须包含子 agent 需要的一切信息:

delegate_task(
goal="Review src/auth/ for security issues and fix any found",
context="""Project at /home/user/webapp. Python 3.11, Flask, PyJWT, bcrypt.
Auth files: src/auth/login.py, src/auth/jwt.py, src/auth/middleware.py
Test command: pytest tests/auth/ -v
Focus on: SQL injection, JWT validation, password hashing, session management.
Fix issues found and verify tests pass.""",
toolsets=["terminal", "file"]
)

并行评估同一个问题的多种方案,然后选择最佳方案:

I need to add full-text search to our Django app. Evaluate three approaches
in parallel:
1. PostgreSQL tsvector (built-in)
2. Elasticsearch via django-elasticsearch-dsl
3. Meilisearch via meilisearch-python
For each: setup complexity, query capabilities, resource requirements,
and maintenance overhead. Compare them and recommend one.

每个子 agent 独立研究一个选项。因为它们彼此隔离,所以不会相互污染 —— 每个评估都基于自身优缺点独立完成。父 agent 会获得三份摘要,并进行比较。

将大型重构任务拆分给多个并行子 agent,每个负责代码库中的不同部分:

delegate_task(tasks=[
{
"goal": "Refactor all API endpoint handlers to use the new response format",
"context": """Project at /home/user/api-server.
Files: src/handlers/users.py, src/handlers/auth.py, src/handlers/billing.py
Old format: return {"data": result, "status": "ok"}
New format: return APIResponse(data=result, status=200).to_dict()
Import: from src.responses import APIResponse
Run tests after: pytest tests/handlers/ -v""",
"toolsets": ["terminal", "file"]
},
{
"goal": "Update all client SDK methods to handle the new response format",
"context": """Project at /home/user/api-server.
Files: sdk/python/client.py, sdk/python/models.py
Old parsing: result = response.json()["data"]
New parsing: result = response.json()["data"] (same key, but add status code checking)
Also update sdk/python/tests/test_client.py""",
"toolsets": ["terminal", "file"]
},
{
"goal": "Update API documentation to reflect the new response format",
"context": """Project at /home/user/api-server.
Docs at: docs/api/. Format: Markdown with code examples.
Update all response examples from old format to new format.
Add a 'Response Format' section to docs/api/overview.md explaining the schema.""",
"toolsets": ["terminal", "file"]
}
])

使用 execute_code 进行机械式数据收集,然后把推理密集型分析委派出去:

# Step 1: Mechanical gathering (execute_code is better here — no reasoning needed)
execute_code("""
from hermes_tools import web_search, web_extract
results = []
for query in ["AI funding Q1 2026", "AI startup acquisitions 2026", "AI IPOs 2026"]:
r = web_search(query, limit=5)
for item in r["data"]["web"]:
results.append({"title": item["title"], "url": item["url"], "desc": item["description"]})
# Extract full content from top 5 most relevant
urls = [r["url"] for r in results[:5]]
content = web_extract(urls)
# Save for the analysis step
import json
with open("/tmp/ai-funding-data.json", "w") as f:
json.dump({"search_results": results, "extracted": content["results"]}, f)
print(f"Collected {len(results)} results, extracted {len(content['results'])} pages")
""")
# Step 2: Reasoning-heavy analysis (delegation is better here)
delegate_task(
goal="Analyze AI funding data and write a market report",
context="""Raw data at /tmp/ai-funding-data.json contains search results and
extracted web pages about AI funding, acquisitions, and IPOs in Q1 2026.
Write a structured market report: key deals, trends, notable players,
and outlook. Focus on deals over $100M.""",
toolsets=["terminal", "file"]
)

这通常是最高效的模式:execute_code 低成本地处理 10+ 次连续工具调用,然后由一个子 agent 在干净上下文中完成单个高成本推理任务。

根据子 agent 的需要选择 toolsets:

任务类型Toolsets原因
Web 研究["web"]只使用 web_search + web_extract
代码工作["terminal", "file"]Shell 访问 + 文件操作
全栈["terminal", "file", "web"]除 messaging 之外的一切
只读分析["file"]只能读取文件,没有 shell

限制 toolsets 可以让子 agent 保持专注,并防止意外副作用(比如研究型子 agent 运行 shell 命令)。

  • 默认 3 个并行任务:批处理默认使用 3 个并发子 agent(可通过 config.yaml 中的 delegation.max_concurrent_children 配置,没有硬性上限,只有最小值 1)。
  • 嵌套委派是 opt-in:叶子子 agent(默认)不能调用 delegate_taskclarifymemorysend_messageexecute_code。编排型子 agent(role="orchestrator")会保留 delegate_task 用于进一步委派,但只有当 delegation.max_spawn_depth 被提高到默认值 1 以上时才可用(支持 1-3);其他四个仍然被阻止。可通过 delegation.orchestrator_enabled: false 全局禁用。

调整并发和深度

配置默认值范围作用
max_concurrent_children3>=1每次 delegate_task 调用的并行批处理大小
max_spawn_depth11-3可以继续生成子 agent 的委派层级数

示例:运行 30 个并行 workers,并允许嵌套子 agent:

delegation:
max_concurrent_children: 30
max_spawn_depth: 2
  • 独立终端 —— 每个子 agent 都有自己的 terminal session,带有独立的工作目录和状态。
  • 无 conversation history —— 子 agent 只能看到父 agent 调用 delegate_task 时传入的 goal 和 context。
  • 默认 50 次 iterations —— 对于简单任务,可以把 max_iterations 设置得更低以节省成本。
  • 不持久 —— delegate_task 是同步的,并在父轮次内运行。如果父轮次被中断(新用户消息、/stop/new),所有活跃的子任务都会被取消(status="interrupted"),其工作会被丢弃。对于必须在当前轮次之外继续存在的工作,请使用 cronjobterminal(background=True, notify_on_complete=True)

目标要具体。“Fix the bug” 太模糊了。“Fix the TypeError in api/handlers.py line 47 where process_request() receives None from parse_body()” 能给子 agent 足够的信息去处理。

包含文件路径。子 agent 不知道你的项目结构。始终包含相关文件的绝对路径、项目根目录和测试命令。

使用 delegation 进行上下文隔离。有时候你需要一个新鲜视角。委派会迫使你清楚描述问题,而子 agent 会在没有你当前对话中累积假设的情况下处理它。

检查结果。子 agent 的 summary 只是 summary。如果子 agent 说 “已修复 bug 且测试通过”,请自己运行测试或阅读 diff 进行验证。

完整的 delegation 参考 —— 所有参数、ACP 集成和高级配置 —— 请参见 Subagent Delegation。

-
0:000:00