dispatching-parallel-agents
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinese并行分派智能体
Parallel Agent Dispatch
概述
Overview
你将任务委派给具有隔离上下文的专用智能体。通过精心设计它们的指令和上下文,确保它们专注并成功完成任务。它们不应继承你的会话上下文或历史记录——你要精确构造它们所需的一切。这样也能为你自己保留用于协调工作的上下文。
当你遇到多个不相关的失败(不同的测试文件、不同的子系统、不同的 bug),逐一排查会浪费时间。每个排查都是独立的,可以并行进行。
核心原则: 每个独立问题域分派一个智能体,让它们并发工作。
You delegate tasks to specialized agents with isolated contexts. By carefully designing their instructions and context, ensure they stay focused and complete tasks successfully. They should not inherit your session context or history—you precisely construct everything they need. This also preserves your own context for coordinating work.
When you encounter multiple unrelated failures (different test files, different subsystems, different bugs), troubleshooting them one by one wastes time. Each troubleshooting task is independent and can be done in parallel.
Core Principle: Assign one agent to each independent problem domain and let them work concurrently.
何时使用
When to Use
dot
digraph when_to_use {
"存在多个失败?" [shape=diamond];
"它们是否独立?" [shape=diamond];
"单个智能体排查所有问题" [shape=box];
"每个问题域一个智能体" [shape=box];
"能否并行工作?" [shape=diamond];
"顺序执行智能体" [shape=box];
"并行分派" [shape=box];
"存在多个失败?" -> "它们是否独立?" [label="是"];
"它们是否独立?" -> "单个智能体排查所有问题" [label="否 - 有关联"];
"它们是否独立?" -> "能否并行工作?" [label="是"];
"能否并行工作?" -> "并行分派" [label="是"];
"能否并行工作?" -> "顺序执行智能体" [label="否 - 有共享状态"];
}适用场景:
- 3 个以上测试文件因不同根因失败
- 多个子系统独立出现故障
- 每个问题无需其他问题的上下文即可理解
- 排查之间无共享状态
不适用场景:
- 失败是相关的(修复一个可能修复其他的)
- 需要理解完整的系统状态
- 智能体之间会互相干扰
dot
digraph when_to_use {
"Multiple failures exist?" [shape=diamond];
"Are they independent?" [shape=diamond];
"Single agent troubleshoots all issues" [shape=box];
"One agent per problem domain" [shape=box];
"Can they run in parallel?" [shape=diamond];
"Execute agents sequentially" [shape=box];
"Parallel dispatch" [shape=box];
"Multiple failures exist?" -> "Are they independent?" [label="Yes"];
"Are they independent?" -> "Single agent troubleshoots all issues" [label="No - Related"];
"Are they independent?" -> "Can they run in parallel?" [label="Yes"];
"Can they run in parallel?" -> "Parallel dispatch" [label="Yes"];
"Can they run in parallel?" -> "Execute agents sequentially" [label="No - Shared state"];
}Applicable Scenarios:
- 3 or more test files failing due to different root causes
- Multiple subsystems failing independently
- Each problem can be understood without context from other problems
- No shared state between troubleshooting tasks
Inapplicable Scenarios:
- Failures are related (fixing one may fix others)
- Need to understand the complete system state
- Agents will interfere with each other
模式
Pattern
1. 识别独立的问题域
1. Identify Independent Problem Domains
按故障分组:
- 文件 A 测试:工具审批流程
- 文件 B 测试:批量完成行为
- 文件 C 测试:中止功能
每个问题域是独立的——修复工具审批不会影响中止测试。
Group failures by:
- File A Tests: Tool approval process
- File B Tests: Batch completion behavior
- File C Tests: Abort functionality
Each problem domain is independent—fixing the tool approval won't affect the abort tests.
2. 创建聚焦的智能体任务
2. Create Focused Agent Tasks
每个智能体获得:
- 明确范围: 一个测试文件或子系统
- 清晰目标: 让这些测试通过
- 约束条件: 不修改其他代码
- 预期输出: 你发现和修复内容的总结
Each agent receives:
- Clear Scope: One test file or subsystem
- Clear Goal: Make these tests pass
- Constraints: Do not modify other code
- Expected Output: A summary of what you found and fixed
3. 并行分派
3. Parallel Dispatch
typescript
// 在 Claude Code / AI 环境中
Task("修复 agent-tool-abort.test.ts 的失败")
Task("修复 batch-completion-behavior.test.ts 的失败")
Task("修复 tool-approval-race-conditions.test.ts 的失败")
// 三个任务并发运行typescript
// In Claude Code / AI environment
Task("Fix failures in agent-tool-abort.test.ts")
Task("Fix failures in batch-completion-behavior.test.ts")
Task("Fix failures in tool-approval-race-conditions.test.ts")
// All three tasks run concurrently4. 审查与集成
4. Review and Integration
当智能体返回时:
- 阅读每个总结
- 验证修复之间没有冲突
- 运行完整测试套件
- 集成所有更改
When agents return:
- Read each summary
- Verify there are no conflicts between fixes
- Run the full test suite
- Integrate all changes
智能体提示词结构
Agent Prompt Structure
好的智能体提示词应该是:
- 聚焦的 - 一个清晰的问题域
- 自包含的 - 包含理解问题所需的所有上下文
- 明确输出要求 - 智能体应该返回什么?
markdown
修复 src/agents/agent-tool-abort.test.ts 中 3 个失败的测试:
1. "should abort tool with partial output capture" - 期望消息中包含 'interrupted at'
2. "should handle mixed completed and aborted tools" - 快速工具被中止而非完成
3. "should properly track pendingToolCount" - 期望 3 个结果但得到 0 个
这些是时序/竞态条件问题。你的任务:
1. 阅读测试文件,理解每个测试验证的内容
2. 找到根因——是时序问题还是实际 bug?
3. 修复方式:
- 用基于事件的等待替换任意超时
- 如果发现中止实现中的 bug 则修复
- 如果测试的是已变更的行为则调整测试期望
不要只是增加超时时间——找到真正的问题。
返回:你发现了什么以及修复了什么的总结。A good agent prompt should be:
- Focused - One clear problem domain
- Self-contained - Contains all context needed to understand the problem
- Clear Output Requirements - What should the agent return?
markdown
Fix the 3 failing tests in src/agents/agent-tool-abort.test.ts:
1. "should abort tool with partial output capture" - Expected message to contain 'interrupted at'
2. "should handle mixed completed and aborted tools" - Fast tool was aborted instead of completed
3. "should properly track pendingToolCount" - Expected 3 results but got 0
These are timing/race condition issues. Your tasks:
1. Read the test file and understand what each test verifies
2. Find the root cause - is it a timing issue or an actual bug?
3. Fix methods:
- Replace arbitrary timeouts with event-based waits
- Fix bugs in the abort implementation if found
- Adjust test expectations if testing changed behavior
Don't just increase timeouts - find the real issue.
Return: A summary of what you found and fixed.常见错误
Common Mistakes
错误做法:太宽泛: "修复所有测试" - 智能体会迷失方向
正确做法:具体明确: "修复 agent-tool-abort.test.ts" - 聚焦的范围
错误做法:无上下文: "修复竞态条件" - 智能体不知道在哪里
正确做法:提供上下文: 粘贴错误信息和测试名称
错误做法:无约束: 智能体可能会重构所有代码
正确做法:设置约束: "不要修改生产代码" 或 "只修复测试"
错误做法:模糊的输出要求: "修好它" - 你不知道改了什么
正确做法:明确要求: "返回根因和修改内容的总结"
Wrong Approach: Too Broad: "Fix all tests" - Agents will get lost
Correct Approach: Specific: "Fix agent-tool-abort.test.ts" - Focused scope
Wrong Approach: No Context: "Fix race conditions" - Agents don't know where
Correct Approach: Provide Context: Paste error messages and test names
Wrong Approach: No Constraints: Agents may refactor all code
Correct Approach: Set Constraints: "Do not modify production code" or "Only fix tests"
Wrong Approach: Vague Output Requirements: "Fix it" - You don't know what was changed
Correct Approach: Clear Requirements: "Return a summary of root causes and changes made"
不适用的场景
Inapplicable Scenarios
关联性失败: 修复一个可能修复其他的——先一起排查
需要完整上下文: 理解问题需要看到整个系统
探索性调试: 你还不知道什么坏了
共享状态: 智能体会互相干扰(编辑同一文件、使用同一资源)
Related Failures: Fixing one may fix others—troubleshoot together first
Requires Full Context: Understanding the problem requires seeing the entire system
Exploratory Debugging: You don't yet know what's broken
Shared State: Agents will interfere with each other (editing the same file, using the same resource)
实际案例
Real-World Case
场景: 大规模重构后,3 个文件中出现 6 个测试失败
失败情况:
- agent-tool-abort.test.ts:3 个失败(时序问题)
- batch-completion-behavior.test.ts:2 个失败(工具未执行)
- tool-approval-race-conditions.test.ts:1 个失败(执行计数 = 0)
决策: 独立的问题域——中止逻辑、批量完成、竞态条件各自独立
分派:
智能体 1 → 修复 agent-tool-abort.test.ts
智能体 2 → 修复 batch-completion-behavior.test.ts
智能体 3 → 修复 tool-approval-race-conditions.test.ts结果:
- 智能体 1:用基于事件的等待替换了超时
- 智能体 2:修复了事件结构 bug(threadId 位置不对)
- 智能体 3:添加了等待异步工具执行完成的逻辑
集成: 所有修复互相独立,无冲突,完整测试套件全部通过
节省的时间: 3 个问题并行解决 vs 顺序解决
Scenario: After a large-scale refactoring, 6 tests in 3 files are failing
Failures:
- agent-tool-abort.test.ts: 3 failures (timing issues)
- batch-completion-behavior.test.ts: 2 failures (tools not executing)
- tool-approval-race-conditions.test.ts: 1 failure (execution count = 0)
Decision: Independent problem domains—abort logic, batch completion, and race conditions are separate
Dispatch:
Agent 1 → Fix agent-tool-abort.test.ts
Agent 2 → Fix batch-completion-behavior.test.ts
Agent 3 → Fix tool-approval-race-conditions.test.tsResults:
- Agent 1: Replaced arbitrary timeouts with event-based waits
- Agent 2: Fixed an event structure bug (incorrect threadId position)
- Agent 3: Added logic to wait for asynchronous tool execution to complete
Integration: All fixes are independent, no conflicts, full test suite passes
Time Saved: 3 problems solved in parallel vs sequentially
核心优势
Core Advantages
- 并行化 - 多个排查同时进行
- 聚焦 - 每个智能体范围窄,需要跟踪的上下文少
- 独立性 - 智能体之间互不干扰
- 速度 - 3 个问题在 1 个问题的时间内解决
- Parallelization - Multiple troubleshooting tasks happen simultaneously
- Focus - Each agent has a narrow scope and less context to track
- Independence - Agents do not interfere with each other
- Speed - 3 problems solved in the time it takes to solve 1
验证
Validation
智能体返回后:
- 审查每个总结 - 理解改了什么
- 检查冲突 - 智能体是否编辑了同一段代码?
- 运行完整套件 - 验证所有修复协同工作
- 抽查 - 智能体可能犯系统性错误
After agents return:
- Review Each Summary - Understand what was changed
- Check for Conflicts - Did agents edit the same code segment?
- Run Full Suite - Verify all fixes work together
- Spot Check - Agents may make systematic errors
实际效果
Actual Results
来自调试会话(2025-10-03):
- 3 个文件中 6 个失败
- 并行分派 3 个智能体
- 所有排查并发完成
- 所有修复成功集成
- 智能体之间的更改零冲突
From a debugging session (2025-10-03):
- 6 failures across 3 files
- 3 agents dispatched in parallel
- All troubleshooting completed concurrently
- All fixes successfully integrated
- Zero conflicts between agent changes