教程 / 后端集成 / 调度并行 Agent
📝 文字 ● 中级 更新于 2026-05-19

如何为独立任务调度并行 Agent?

TL;DR:当你有 2 个以上无共享状态的任务时,在一条消息中发起多个 Agent 工具调用。这些 Agent 并发执行,各自返回独立结果,主循环随后继续。适用于并行搜索、并行测试运行或并行构建检查。

大多数"循环中的 agent"工作天然是串行的——读取文件、做出决策、写入文件、循环往复。但当你有两个或更多无共享状态且无顺序依赖的任务时,并行执行可以线性压缩总耗时。关键在于判断何时该这么做——因为共享状态意味着竞态条件,而代码中的竞态条件意味着工作成果的丢失。

串行默认大多数时候是正确的

0

LingCode 默认一次运行一个 agent 循环,这几乎始终是你想要的。一个典型任务——"重构这个模块"——的流程如下:

  1. 读取 module.swift
  2. 根据读取内容决定要做什么修改。
  3. 写入 module.swift
  4. 运行涉及 module.swift 的测试。
  5. 根据测试输出决定要修复什么。

每一步都依赖前一步。并行化此流程是不可能的——步骤 2 无法在步骤 1 返回之前运行。LingCode 的自然形态是串行的,因为推理本身就是串行的。

并行调度是例外情况,仅适用于以下场景。在不适合的任务上强行使用,会浪费上下文并产生冲突写入;而在正确的任务上使用,则能将 20 分钟的工作压缩至 5 分钟。

两问测试

1

在让 LingCode 调度并行 Agent 之前,先回答关于任务的两个问题。两者都必须是"是":

  • 无共享可变状态。各 Agent 不能写入同一个文件、同一条数据库记录、同一个 git 分支或同一个内存变量。读取共享状态没问题;写入才是陷阱。
  • 无顺序依赖。Agent B 的输入不能依赖 Agent A 的输出。如果你会写"先做 X,再做 Y"——那就是串行。

如果两个问题都能回答"是",并行调度既安全又更快。如果其中一个答案是"否",请保持串行。

"可能独立"和"真正独立"不是一回事。两个 Agent 重构同一个 Swift 文件的"不同"部分,其实仍然都在写同一个文件——它们会互相覆盖。请在文件粒度上验证独立性,而非概念粒度。

何时并行是正确选择——三个真实案例

2

在实践中真正有价值的模式:

  • 多目标构建。同一个 App 的 iOS、Android 和 macOS 目标各有独立的工具链和项目,无共享写入。并行可将 9 分钟的总构建时间压缩至 3 分钟。
  • 逐文件独立审查。对 12 个独立模块做代码审查,每份审查写入各自的 REVIEW_<module>.md。每个 Agent 读一个文件、写一个文件——不会发生冲突。
  • 独立研究问题。"找出 API A 的所有用法"、"找出 API B 的所有用法"、"找出 API C 的所有用法"。三个 Agent 各自读取代码库,写入各自的草稿笔记,最后由聚合步骤合并结果。

共同点:调度轮结束后有一个干净的合并步骤。并行 Agent 生成各自隔离的产物;由你(或后续的串行轮次)来整合它们。

何时并行是错误选择——四个陷阱

3
  • 实现单个功能。"添加登录"会涉及 UI、数据模型、网络层、Keychain——全部耦合在一起。并行运行会产生四个 Agent,各自写出相互不一致的接口约定。
  • 跨耦合文件重构。重命名一个被 8 个文件实现的 Swift 协议:每个文件的修改都依赖协议签名,而协议签名本身正在重构过程中被设计。使用带迭代判断的串行循环,端到端反而更快。
  • 任何写入同一 git 分支的操作。两个 Agent 并行提交到 main = 在你自己的会话中产生合并冲突。如果你确实需要这样做,请使用隔离的 worktree——参见 worktrees 教程
  • 迭代调试。"尝试一个修复、运行测试、再尝试另一个修复"本质上是串行的——每次尝试都需要基于上一次的结果。在这里并行化只会用过时信息烧掉多个并行尝试。
启发式规则:如果你对 LingCode 的提示词中自然包含"然后"——"做 X 然后做 Y"——那就是串行。如果自然包含"和"而没有任何"然后"——"找出所有 X 和找出所有 Y 和找出所有 Z"——那就是并行。

通过对话调度

4

通过以列表形式列出任务并说明它们是独立的,来让 LingCode 并行调度。一个有效的提示词示例:

I want three independent things done in parallel — no shared
state, no ordering between them:

1. Audit Sources/Auth/ for force-unwraps and write findings to
   AUDIT_AUTH.md.
2. Audit Sources/Networking/ for force-unwraps and write findings
   to AUDIT_NET.md.
3. Audit Sources/Storage/ for force-unwraps and write findings to
   AUDIT_STORAGE.md.

Dispatch one sub-agent per task. After all three return,
synthesize a top-level AUDIT_SUMMARY.md that aggregates the
counts and ranks modules by severity.

LingCode 会调度三个子 Agent,每个拥有独立的上下文窗口,并发运行,并在完成时汇合。合成步骤是最终的串行轮次——这就是合并发生的地方。

每个子 Agent 看到什么

5

子 Agent 不共享你的对话历史。它们只获得你分配给它们的任务和项目文件——仅此而已。这是一个特性,而非缺陷:

  • 每个 Agent 的上下文更小 = 更快、更省、更专注。每个子 Agent 不会把你 50 轮的对话历史带入一个 5 分钟的审计任务中。
  • Agent 之间无上下文泄漏 = 并行决策不会因为读取了同样的最近断言而意外达成一致。每个 Agent 得到独立的判断。

你需要付出的代价是:每个子 Agent 必须获得足够的指令,才能独立完成其任务。"审计 Auth"还不够——请说明目录、要查找什么、在哪里写入结果。上面的提示词对三个任务都做到了这一点。

不要期望子 Agent 读心术。如果子 Agent 的任务描述包含"像我们之前讨论的那样",子 Agent 并没有"之前的讨论"。请编写自包含的任务描述。

合并步骤比调度更重要

6

并行调度生成 N 个隔离的产物。价值来自于对它们的综合——而综合是串行的。在合并提示词上花更多心思,而不是调度提示词:

Now that AUDIT_AUTH.md, AUDIT_NET.md, and AUDIT_STORAGE.md exist,
synthesize AUDIT_SUMMARY.md:

- Total force-unwrap count across all three modules.
- Ranked list of files by count, highest first.
- Patterns that appeared in 2+ modules (likely systemic, fix once).
- Patterns unique to one module (likely local, fix in place).
- Three concrete next-step PR titles.

Read the three audit files; do not re-scan the source code.

"不要重新扫描"的指令至关重要。没有它,LingCode 往往会重新执行子 Agent 已完成的工作——从而抵消并行化的收益。请信任这些产物。

验证成效

7

并行运行结束后,检查两件事:

  • 没有文件被两个 Agent 写入。运行 git status 或检查时间戳。如果两个产物最终位于同一路径,说明一个覆盖了另一个——你的"独立"任务并不是真正独立的。
  • 实际耗时确实更短。如果你并行化了三个各 30 秒的任务,调度的开销可能已经吃掉了收益。当每个子任务至少需要一两分钟的真实工作时,并行化才真正划算。

如果并行运行没有超过串行运行,这是下次的参考信息。并非所有"看起来能并行"的任务实际上都适合并行。

在 LingCode 中使用此功能

8

将调度启发式规则封装为一个 skill,让 LingCode 在并行化之前先执行两问测试——并在串行更合适时保持串行:

---
name: dispatching-parallel-agents
description: Use when facing 2+ independent tasks that can be worked on without shared state or sequential dependencies. Triggers: 'do these in parallel', 'split this work', 'run multiple agents', 'parallelize', 'fan out', batch of independent file edits, multiple test fixtures, multiple isolated components. Actions: dispatch, parallelize, batch, fan-out, fan-in. Anti-pattern: tasks that share state, sequential dependencies, conflicting writes. Compresses wall time when the two-question test passes (no shared state + no sequential dep).
---

Before dispatching parallel agents, answer two questions about
the task set:

1. Is there shared mutable state? (Same file, same DB row, same
   git branch, same in-memory variable.) If yes — stay serial.
2. Does any task's input depend on another task's output? If yes
   — stay serial.

Both must be "no" to parallelize.

When parallelizing:
- Give each sub-agent a self-contained task description. They do
  not share conversation history.
- Have each sub-agent write to its own artifact path. No two
  agents writing the same file.
- Plan the merge step explicitly. The synthesis turn is serial
  and reads the artifacts; it should not re-scan source.

Good fits: multi-target builds, per-file independent reviews,
independent research questions.

Bad fits: implementing one feature, refactor across coupled
files, two writes to the same git branch, iterative debugging.

After parallel runs, sanity-check: no file written by two agents,
wall time actually beat serial.

保存至 ~/.lingcode/skills/dispatching-parallel-agents/SKILL.md——参见 安装 skill 了解确切位置以及 skill 如何被发现。

获取 LingCode →

下一步