parallel-execution

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Parallel Execution

并行执行

Overview

概述

Execute multiple independent tasks simultaneously using multiple subagent invocations in a single message. Reduces implementation time by 50-80% for multi-component features.
Announce at start: "I'm using parallel-execution to run [N] independent tasks simultaneously."
通过在单条消息中调用多个subagent来同时执行多个独立任务。对于多组件功能,可将实现时间缩短50-80%。
开始时需声明: "我将使用parallel-execution同时运行[N]个独立任务。"

The Process

执行流程

Step 1: Identify Parallel Tasks

步骤1:识别可并行任务

Check subtask JSON files for
parallel: true
:
bash
bash .opencode/skills/task-management/router.sh parallel {feature}
Output shows which tasks can run together:
Batch 1 (Ready now):
- subtask_01.json (parallel: true)
- subtask_02.json (parallel: true)

Batch 2 (After Batch 1):
- subtask_03.json (depends_on: ["01", "02"])
检查子任务JSON文件中的
parallel: true
字段:
bash
bash .opencode/skills/task-management/router.sh parallel {feature}
输出将展示可并行执行的任务分组:
批次1(可立即执行):
- subtask_01.json (parallel: true)
- subtask_02.json (parallel: true)

批次2(需在批次1完成后执行):
- subtask_03.json (depends_on: ["01", "02"])

Step 2: Execute Parallel Batch

步骤2:执行并行批次

Make multiple task() calls in SINGLE message:
markdown
I'll execute Batch 1 tasks in parallel:

task(
  subagent_type="CoderAgent",
  description="Execute subtask 01",
  prompt="Read .tmp/tasks/feature/subtask_01.json and implement..."
)

task(
  subagent_type="CoderAgent",
  description="Execute subtask 02",
  prompt="Read .tmp/tasks/feature/subtask_02.json and implement..."
)
CRITICAL: Both task() calls in SAME message—not separate messages.
单条消息中调用多个task():
markdown
我将并行执行批次1的任务:

task(
  subagent_type="CoderAgent",
  description="执行子任务01",
  prompt="读取.tmp/tasks/feature/subtask_01.json并实现..."
)

task(
  subagent_type="CoderAgent",
  description="执行子任务02",
  prompt="读取.tmp/tasks/feature/subtask_02.json并实现..."
)
关键注意事项:两个task()调用必须在同一条消息中,而非分开发送。

Step 3: Wait for All to Complete

步骤3:等待所有任务完成

All tasks in batch must complete before proceeding to next batch.
当前批次的所有任务必须全部完成后,才能进入下一批次。

Step 4: Verify Batch Completion

步骤4:验证批次完成状态

bash
bash .opencode/skills/task-management/router.sh status {feature}
Check all tasks in batch marked
status: "completed"
.
bash
bash .opencode/skills/task-management/router.sh status {feature}
检查批次内所有任务是否标记为
status: "completed"

Step 5: Execute Next Batch

步骤5:执行下一批次

Once Batch 1 complete, proceed to Batch 2:
markdown
task(
  subagent_type="CoderAgent",
  description="Execute subtask 03",
  prompt="Read .tmp/tasks/feature/subtask_03.json and integrate..."
)
批次1完成后,继续执行批次2:
markdown
task(
  subagent_type="CoderAgent",
  description="执行子任务03",
  prompt="读取.tmp/tasks/feature/subtask_03.json并集成..."
)

Example: Converting Multiple Files

示例:多文件转换

markdown
I'll convert these 5 subagent files in parallel:

task(subagent_type="CoderAgent", description="Convert auth-agent",
     prompt="Convert .opencode/agent/subagents/auth-agent.md to new format...")

task(subagent_type="CoderAgent", description="Convert user-agent",
     prompt="Convert .opencode/agent/subagents/user-agent.md to new format...")

task(subagent_type="CoderAgent", description="Convert payment-agent",
     prompt="Convert .opencode/agent/subagents/payment-agent.md to new format...")

task(subagent_type="CoderAgent", description="Convert notification-agent",
     prompt="Convert .opencode/agent/subagents/notification-agent.md to new format...")

task(subagent_type="CoderAgent", description="Convert analytics-agent",
     prompt="Convert .opencode/agent/subagents/analytics-agent.md to new format...")
Time savings: 5 tasks × 10 min = 50 min sequential → ~10 min parallel (80% faster)
markdown
我将并行转换这5个subagent文件:

task(subagent_type="CoderAgent", description="转换auth-agent",
     prompt="将.opencode/agent/subagents/auth-agent.md转换为新格式...")

task(subagent_type="CoderAgent", description="转换user-agent",
     prompt="将.opencode/agent/subagents/user-agent.md转换为新格式...")

task(subagent_type="CoderAgent", description="转换payment-agent",
     prompt="将.opencode/agent/subagents/payment-agent.md转换为新格式...")

task(subagent_type="CoderAgent", description="转换notification-agent",
     prompt="将.opencode/agent/subagents/notification-agent.md转换为新格式...")

task(subagent_type="CoderAgent", description="转换analytics-agent",
     prompt="将.opencode/agent/subagents/analytics-agent.md转换为新格式...")
时间节省:5个任务×10分钟=串行执行需50分钟 → 并行执行约10分钟(提速80%)

Avoiding Conflicts

避免冲突

✅ GOOD (isolated files):
markdown
task(CoderAgent, "Create src/auth/service.ts")
task(CoderAgent, "Create src/user/service.ts")
task(CoderAgent, "Create src/payment/service.ts")
❌ BAD (same file):
markdown
task(CoderAgent, "Add auth function to src/utils/helpers.ts")
task(CoderAgent, "Add validation function to src/utils/helpers.ts")
This causes merge conflicts—run sequentially instead.
✅ 正确示例(文件独立):
markdown
task(CoderAgent, "创建src/auth/service.ts")
task(CoderAgent, "创建src/user/service.ts")
task(CoderAgent, "创建src/payment/service.ts")
❌ 错误示例(同一文件):
markdown
task(CoderAgent, "向src/utils/helpers.ts添加认证函数")
task(CoderAgent, "向src/utils/helpers.ts添加验证函数")
这种情况会导致合并冲突——应改为串行执行。

Pre-Load Shared Context

预加载共享上下文

Load context ONCE before parallel execution:
markdown
undefined
在并行执行前一次性加载上下文:
markdown
undefined

Load context BEFORE parallel tasks

在并行任务前加载上下文

Read: .opencode/context/core/standards/code-quality.md Read: .opencode/context/core/standards/security-patterns.md
Read: .opencode/context/core/standards/code-quality.md Read: .opencode/context/core/standards/security-patterns.md

Now all parallel tasks have same context

现在所有并行任务都拥有相同上下文

task(CoderAgent, "Implement auth service...") task(CoderAgent, "Implement user service...")

DO NOT let each task discover context separately (slower).
task(CoderAgent, "实现认证服务...") task(CoderAgent, "实现用户服务...")

不要让每个任务单独发现上下文(会降低效率)。

Error Handling

错误处理

Task fails during parallel execution:
  • Fix failed task before proceeding to next batch
  • Other tasks in batch can still be used
Tasks completing out of order:
  • Don't start next batch until ALL current batch complete
  • Use dependency tracking in subtask JSON
File conflicts:
  • Review task breakdown—ensure true isolation
  • If tasks MUST touch same file, mark
    parallel: false
并行执行中任务失败:
  • 在进入下一批次前修复失败任务
  • 批次中的其他任务仍可正常使用
任务完成顺序不一致:
  • 必须等待当前批次所有任务完成后,才能启动下一批次
  • 利用子任务JSON中的依赖跟踪机制
文件冲突:
  • 重新审视任务拆分逻辑——确保任务真正独立
  • 如果任务必须修改同一文件,标记
    parallel: false

Remember

注意事项

  • Make multiple task() calls in SINGLE message for parallel execution
  • Tasks must be truly independent (no shared files/state)
  • Pre-load shared context ONCE before parallel tasks
  • Wait for ALL tasks in batch to complete before next batch
  • Time savings: 50-80% reduction for multi-component features
  • Optimal batch size: 2-4 tasks (up to 8 if truly independent)
  • DO NOT parallelize tasks that modify same file
  • DO NOT parallelize tasks with dependencies
  • 需在单条消息中调用多个task()以实现并行执行
  • 任务必须真正独立(无共享文件/状态)
  • 并行任务前一次性预加载共享上下文
  • 等待当前批次所有任务完成后再执行下一批次
  • 时间节省:多组件功能的实现时间可缩短50-80%
  • 最优批次规模:2-4个任务(若完全独立最多可到8个)
  • 不要并行修改同一文件的任务
  • 不要并行存在依赖关系的任务

Related

相关内容

  • task-breakdown
  • context-discovery
  • code-execution

Key Pattern: Multiple independent task() calls in SAME message = parallel execution.
  • task-breakdown
  • context-discovery
  • code-execution

核心模式:在同一条消息中调用多个独立的task() = 并行执行。