| name | dispatching-parallel-agents |
| description (EN) | Use when facing 2+ independent tasks that can be worked on without shared state or sequential dependencies |
| 說明 (繁中) | 當面對 2 個以上彼此獨立、無需共享狀態或循序依賴即可並行處理的任務時使用 |
You delegate tasks to specialized agents with isolated context. By precisely crafting their instructions and context, you ensure they stay focused and succeed at their task. They should never inherit your session's context or history — you construct exactly what they need. This also preserves your own context for coordination work.
你將任務委派給具備隔離上下文的專門代理。透過精確構思它們的指令與上下文,你確保它們能保持專注並成功完成任務。它們不應繼承你 session 的上下文或歷史 —— 你為它們建構所需的全部內容。這也為協調工作保留了你的上下文。
When you have multiple unrelated failures (different test files, different subsystems, different bugs), investigating them sequentially wastes time. Each investigation is independent and can happen in parallel.
當你有多個互不相關的失敗(不同的測試檔、不同的子系統、不同的 bug)時,逐一調查會浪費時間。每次調查都彼此獨立,可以並行進行。
Core principle: Dispatch one agent per independent problem domain. Let them work concurrently.
核心原則: 每個獨立的問題領域派發一個代理,讓它們並行運作。
digraph when_to_use {
"Multiple failures?" [shape=diamond];
"Are they independent?" [shape=diamond];
"Single agent investigates all" [shape=box];
"One agent per problem domain" [shape=box];
"Can they work in parallel?" [shape=diamond];
"Sequential agents" [shape=box];
"Parallel dispatch" [shape=box];
"Multiple failures?" -> "Are they independent?" [label="yes"];
"Are they independent?" -> "Single agent investigates all" [label="no - related"];
"Are they independent?" -> "Can they work in parallel?" [label="yes"];
"Can they work in parallel?" -> "Parallel dispatch" [label="yes"];
"Can they work in parallel?" -> "Sequential agents" [label="no - shared state"];
}
digraph when_to_use {
"Multiple failures?" [shape=diamond];
"Are they independent?" [shape=diamond];
"Single agent investigates all" [shape=box];
"One agent per problem domain" [shape=box];
"Can they work in parallel?" [shape=diamond];
"Sequential agents" [shape=box];
"Parallel dispatch" [shape=box];
"Multiple failures?" -> "Are they independent?" [label="yes"];
"Are they independent?" -> "Single agent investigates all" [label="no - related"];
"Are they independent?" -> "Can they work in parallel?" [label="yes"];
"Can they work in parallel?" -> "Parallel dispatch" [label="yes"];
"Can they work in parallel?" -> "Sequential agents" [label="no - shared state"];
}
Use when: - 3+ test files failing with different root causes - Multiple subsystems broken independently - Each problem can be understood without context from others - No shared state between investigations
使用時機: - 3 個以上測試檔因不同根因而失敗 - 多個子系統各自獨立損壞 - 每個問題無需依賴其他問題的上下文即可理解 - 各調查之間沒有共享狀態
Don't use when: - Failures are related (fix one might fix others) - Need to understand full system state - Agents would interfere with each other
不要使用的時機: - 失敗彼此相關(修好一個可能連帶修好其他) - 需要理解完整的系統狀態 - 代理會互相干擾
Group failures by what's broken: - File A tests: Tool approval flow - File B tests: Batch completion behavior - File C tests: Abort functionality
按損壞的內容對失敗分組: - 檔案 A 測試:工具核准流程 - 檔案 B 測試:批次完成行為 - 檔案 C 測試:中止功能
Each domain is independent - fixing tool approval doesn't affect abort tests.
每個領域彼此獨立 —— 修正工具核准不會影響中止測試。
Each agent gets: - Specific scope: One test file or subsystem - Clear goal: Make these tests pass - Constraints: Don't change other code - Expected output: Summary of what you found and fixed
每個代理會收到: - 明確範圍: 單一測試檔或子系統 - 清楚目標: 讓這些測試通過 - 限制: 不要更動其他程式碼 - 預期輸出: 你發現與修正內容的摘要
Issue all three subagent dispatches in the same response — they run in parallel:
在同一則回應中發出全部三個子代理派發 —— 它們會並行執行:
Subagent (general-purpose): "Fix agent-tool-abort.test.ts failures"
Subagent (general-purpose): "Fix batch-completion-behavior.test.ts failures"
Subagent (general-purpose): "Fix tool-approval-race-conditions.test.ts failures"
# All three run concurrently.
Subagent (general-purpose): "Fix agent-tool-abort.test.ts failures"
Subagent (general-purpose): "Fix batch-completion-behavior.test.ts failures"
Subagent (general-purpose): "Fix tool-approval-race-conditions.test.ts failures"
# All three run concurrently.
Multiple dispatch calls in one response = parallel execution. One per response = sequential.
一則回應中多次派發呼叫 = 並行執行。每則回應一次 = 循序執行。
When agents return: - Read each summary - Verify fixes don't conflict - Run full test suite - Integrate all changes
當代理回報時: - 閱讀每份摘要 - 確認修正彼此不衝突 - 執行完整的測試套件 - 整合所有變更
Good agent prompts are: 1. Focused - One clear problem domain 2. Self-contained - All context needed to understand the problem 3. Specific about output - What should the agent return?
良好的代理 prompt 具備: 1. 聚焦 - 單一清晰的問題領域 2. 自足 - 包含理解問題所需的全部上下文 3. 明確輸出 - 代理應該回傳什麼?
Fix the 3 failing tests in src/agents/agent-tool-abort.test.ts:
1. "should abort tool with partial output capture" - expects 'interrupted at' in message
2. "should handle mixed completed and aborted tools" - fast tool aborted instead of completed
3. "should properly track pendingToolCount" - expects 3 results but gets 0
These are timing/race condition issues. Your task:
1. Read the test file and understand what each test verifies
2. Identify root cause - timing issues or actual bugs?
3. Fix by:
- Replacing arbitrary timeouts with event-based waiting
- Fixing bugs in abort implementation if found
- Adjusting test expectations if testing changed behavior
Do NOT just increase timeouts - find the real issue.
Return: Summary of what you found and what you fixed.
Fix the 3 failing tests in src/agents/agent-tool-abort.test.ts:
1. "should abort tool with partial output capture" - expects 'interrupted at' in message
2. "should handle mixed completed and aborted tools" - fast tool aborted instead of completed
3. "should properly track pendingToolCount" - expects 3 results but gets 0
These are timing/race condition issues. Your task:
1. Read the test file and understand what each test verifies
2. Identify root cause - timing issues or actual bugs?
3. Fix by:
- Replacing arbitrary timeouts with event-based waiting
- Fixing bugs in abort implementation if found
- Adjusting test expectations if testing changed behavior
Do NOT just increase timeouts - find the real issue.
Return: Summary of what you found and what you fixed.
❌ Too broad: "Fix all the tests" - agent gets lost ✅ Specific: "Fix agent-tool-abort.test.ts" - focused scope
❌ 範圍過大: 「修好所有測試」- 代理會迷失方向 ✅ 具體明確: 「修好 agent-tool-abort.test.ts」- 範圍聚焦
❌ No context: "Fix the race condition" - agent doesn't know where ✅ Context: Paste the error messages and test names
❌ 沒有上下文: 「修正競爭條件」- 代理不知道在哪裡 ✅ 提供上下文: 貼上錯誤訊息與測試名稱
❌ No constraints: Agent might refactor everything ✅ Constraints: "Do NOT change production code" or "Fix tests only"
❌ 沒有限制: 代理可能重構一切 ✅ 設定限制: 「不要更動正式程式碼」或「只修正測試」
❌ Vague output: "Fix it" - you don't know what changed ✅ Specific: "Return summary of root cause and changes"
❌ 輸出含糊: 「修好它」- 你不知道改了什麼 ✅ 具體明確: 「回傳根因與變更的摘要」
Related failures: Fixing one might fix others - investigate together first Need full context: Understanding requires seeing entire system Exploratory debugging: You don't know what's broken yet Shared state: Agents would interfere (editing same files, using same resources)
相關失敗: 修好一個可能連帶修好其他 —— 先一起調查 需要完整上下文: 必須檢視整個系統才能理解 探索式除錯: 你尚不知道哪裡壞了 共享狀態: 代理會互相干擾(編輯相同檔案、使用相同資源)
Scenario: 6 test failures across 3 files after major refactoring
情境: 大型重構後 3 個檔案共 6 個測試失敗
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)
失敗: - agent-tool-abort.test.ts:3 個失敗(時序問題) - batch-completion-behavior.test.ts:2 個失敗(工具未執行) - tool-approval-race-conditions.test.ts:1 個失敗(執行次數 = 0)
Decision: Independent domains - abort logic separate from batch completion separate from race conditions
決策: 獨立領域 —— 中止邏輯、批次完成、競爭條件各自獨立
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.ts
派發:
Agent 1 → Fix agent-tool-abort.test.ts
Agent 2 → Fix batch-completion-behavior.test.ts
Agent 3 → Fix tool-approval-race-conditions.test.ts
Results: - Agent 1: Replaced timeouts with event-based waiting - Agent 2: Fixed event structure bug (threadId in wrong place) - Agent 3: Added wait for async tool execution to complete
結果: - Agent 1:以事件驅動等待取代 timeout - Agent 2:修正事件結構 bug(threadId 放錯位置) - Agent 3:新增等待非同步工具執行完成
Integration: All fixes independent, no conflicts, full suite green
整合: 所有修正彼此獨立、無衝突,完整測試套件全綠
After agents return: 1. Review each summary - Understand what changed 2. Check for conflicts - Did agents edit same code? 3. Run full suite - Verify all fixes work together 4. Spot check - Agents can make systematic errors
代理回報後: 1. 審查每份摘要 - 了解變更了什麼 2. 檢查衝突 - 代理是否編輯了相同的程式碼? 3. 執行完整套件 - 確認所有修正能共同運作 4. 抽樣檢查 - 代理可能犯系統性錯誤