Skip to content

perf(agent-runtime): parallelize independent read-only tool calls - #1596

Merged
vastsa merged 2 commits into
vastsa:mainfrom
caulif:perf/parallel-read-batches
Oct 26, 2026
Merged

vastsa merged 2 commits into
vastsa:mainfrom
caulif:perf/parallel-read-batches

Conversation

@caulif

@caulif caulif commented Oct 12, 2026

Copy link
Copy Markdown
Contributor

Refs #1519

这个 PR 实现 #1519 中的第一阶段优化:复用 pi-agent-core 1.1.0 已有的批次执行机制,开放主代理和子代理的内置 Read、Glob、Grep 并行,减少独立读取和搜索调用之间的串行等待。

行为变化

同一条模型回复中的工具批次 修改前 修改后
多个 Read / Glob / Grep 串行 并行
Task 与 Read / Glob / Grep 混合 串行 可以并行
仅包含多个 Task 可以并行 可以并行
包含 Write / Edit / Bash 或其他串行工具 整批串行 整批串行

同样的只读策略也应用于子代理。未明确允许并行的工具默认保持串行;扩展工具也不会因为与内置工具同名而自动获得并行资格。

实现思路

这个改动沿用 pi 的执行流程:

  1. 按模型给出的顺序准备每个调用。
  2. 如果整批工具均允许并行,则并发执行;只要包含一个串行工具,整批就顺序执行。
  3. 按原调用顺序向模型提交工具结果。

主要修改包括:

  • 在 tool-execution-policy.ts 中集中定义并行工具白名单,避免主代理和子代理的策略不一致。
  • 在主代理和子代理中开放内置 Read / Glob / Grep,保留现有 Task 并行能力。
  • 将路径相关的参数规范化和项目指令加载移到有序的调用准备阶段。
  • 将子代理的路径指令更新应用到子代理自己的系统提示状态。

路径指令处理是这个 PR 中除了执行模式之外的主要改动。现有文件工具执行过程会更新项目指令状态,如果只修改并行标记,这些更新就可能受并发完成顺序影响;子代理复用父代理工具时,也需要避免更新父代理的提示状态。

因此,这里先按调用顺序完成准备,再开始并发执行。直接或内部调用工具的路径仍保留准备逻辑。

规格与回归覆盖

同步更新以下规格和设计记录:

  • docs/spec/03-runtime/02-agent-runtime.md
  • docs/spec/03-runtime/03-tools-and-permissions.md
  • docs/adr/parallel-read-batches.md
  • ADR 0062 中与 Task 混合批次有关的说明。

新增 E2E 场景:E2E-TOOLS-parallel-read-batches,并提供 scripts/e2e-parallel-read-batches.mjs。

新增回归覆盖主要针对:

  • 只读工具的并行资格与其他工具的串行回退。
  • 路径指令的有序准备与工具结果的原顺序提交。
  • 子代理指令状态与父代理状态的隔离。
  • 同名扩展工具的来源识别。
  • 停止时尚未开始的调用与已经运行的文件调用的处理。

当前提交验证

  • Task candidate: ff6986ce9a9b(完整 SHA 见 PR head)。
  • Base main: 344861d66fb902d6be5d7ad908279d845396ad33,提交前重新 fetch 并确认它是候选提交的祖先。
  • Environment: Windows,本地 Node 24.11.1;复用已有依赖与 Cargo target,真实 Host 从当前主线源码重新构建,E2E 使用临时数据和项目目录。

已执行并通过:

  • shared、agent-runtime、host-runtime 的 TypeScript 构建。
  • cargo build -p host-core --locked。
  • cd packages/agent-runtime && node node_modules/vitest/vitest.mjs run --config vitest.config.ts:97 个测试文件通过,1,347 项通过、1 项跳过。
  • node scripts/e2e-parallel-read-batches.mjs:在上述候选提交上重跑通过,验证主/子代理并发 Read 分派、真实 Read/Glob/Grep 返回、结果顺序及父代理提示状态隔离。
  • node scripts/e2e-subagent-parent-error.mjs:error、abort、stop 和 direct 四种场景通过。
  • node scripts/check-pr-base-main.mjs --base upstream/main(本地 origin 为个人 fork,upstream 为本 PR 目标库)。
  • ARCHITECTURE_BASE=upstream/main node scripts/check-architecture.mjs。
  • node scripts/check-agent-policy-sync.mjs、仓库 Biome lint、git diff --check。

scripts/e2e-subagents.mjs:NOT RUN。该注册表 E2E 在 Windows 上明确拒绝运行,因为修改 HOME 不能隔离 Windows known-folder 路径。替代验证是上述真实主/子代理并行 E2E、子代理错误/停止 E2E 以及 agent-runtime 测试集;它们不覆盖 Linux/macOS 下的注册表发现流程。完整 Electron UI E2E 和 Rust 全套测试未在本地执行,本次未修改 UI 或 Rust 源码。

固定批次对照

以下为同步本次最新主线之前完成的对照:基线为 a1994812425ad567cb497169cc79f0f332a30902,候选为该基线上的同一并行实现。它们是机制与性能的辅助证据,不冒充当前主线上的重新计时结果;当前提交的验证见上一节。

通过真实的 RuntimeService → AgentSidecar → Host 链路进行了固定批次对照。两版使用相同输入、同一个 Host 二进制和独立测试目录,交替执行,共完成 50 次运行。工具内容、结果顺序及串行边界检查均通过。

以下为工具调用区间并集的中位耗时,并行重叠时间只计算一次,排除模型生成、启动预检和验收时间:

固定批次 每版次数 修改前 修改后
Read × 2 + Glob + Grep,真实小文件 10 69.5ms 40ms
相同只读批次,每次 RPC 回应额外延迟 250ms 5 1,110ms 296ms
Read → Write → Read,同样加入延迟 5 805ms 805ms
Read → Bash → Read,同样加入延迟 5 1,214ms 1,223ms

额外延迟用于验证等待是否能够重叠,以及串行批次是否仍保持顺序,不代表实际磁盘性能。真实小文件测试的绝对收益较小,这些结果也不能直接外推为完整 coding 任务的加速幅度。调用区间重叠也不等于证明底层文件系统函数同时执行。

还在上述旧基线上重跑了源码阅读、JSONL 汇总、JS 模块和 TodoMVC 四项真实任务,两版共 8 次运行均通过验收。由于模型选择的工具组合和工作量不同,这部分仅作为功能回归检查,不作为性能提升的主要依据。

兼容性与范围

本次沿用现有 Host 权限检查、容量控制和 RPC 接口,无需数据库迁移,也没有升级 pi 依赖。

这个 PR 只推进只读工具批次并行。

Write、Edit、Bash 保留串行策略;跨代理文件写入协调、Shell 冲突协调和目录快照一致性需要后续单独设计。停止时已经发出的文件 RPC 仍按现有行为完成,本次没有增加更细粒度的取消机制。

欢迎重点审查路径指令的有序准备、子代理状态隔离,以及 Task 与只读工具混合执行的行为。后续文件资源协调继续在 #1519 中讨论。

@muzimu217 muzimu217 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified on a Linux server (Alibaba Cloud Linux 3, x86_64; detached worktree at upstream origin/main, patch applied cleanly): agent-runtime full suite 97 files / 1347 tests pass, including the new parallel-batch tests.

The scoped rollout is exactly the right first stage for #1519:

  • Parallel eligibility is centralized in tool-execution-policy.ts (main agent and subagents share one allowlist — no strategy drift), defaulting everything not explicitly allowed to serial;
  • Batch semantics are conservative and match pi: a batch runs concurrently only if every tool in it is allowed parallel; one serial tool serializes the whole batch; results are returned to the model in the original call order regardless of completion order;
  • The path-instruction refactor (preparing calls in order before concurrent execution; subagents updating their own system-prompt state instead of the parent's) addresses exactly the race that would otherwise make Read/Glob/Grep parallelism unsafe.

Note for reviewers: this PR is built on a main that already includes #1591 (the browser screenshot timeout fix) — the two changes are complementary: #1591 removes a hung capture from the serial path, and this PR's batch gate keeps Write/Edit/Bash serial anyway, so the two don't conflict.

feat scope (implements stage 1 of #1519) — the maintainer already invited the direction. Nothing blocking.

@caulif

caulif commented Oct 13, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for testing this on Linux and reviewing the execution semantics! The cross-platform verification is very helpful. I’ll keep this PR focused on the first stage of #1519, with file-resource coordination left for a separate discussion and follow-up PR.

@vastsa vastsa left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the focused implementation and for preserving ordered preparation and result IDs. I found a concurrency case that still needs a guard before this is safe to land: Task can launch a delegate with Write/Edit, and outside an isolated session that delegate shares the session workspace. With Task and Read both marked parallel, a parent read can overlap a write to the same file; before this change, that mixed batch was sequential. Please preserve Task-only fan-out while ensuring mixed Task/read batches cannot race a writable delegate, and add a regression covering a Task that edits a file while the parent reads it. I can’t merge this candidate until that behavior is protected. Thanks for the substantial work.

case "Read":
case "Glob":
case "Grep":
case "Task":

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Task is not read-only: it can launch a delegate that writes in the same workspace in ordinary sessions. Marking both Task and Read parallel makes a mixed batch overlap the parent's read with the delegate's Write/Edit; there is no cross-runtime read/write lock. Please keep Task-only fan-out while guarding this mixed case, and add a regression that demonstrates the parent cannot read a file during a writable delegate's mutation.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

感谢指出问题和再次审阅,之前确实漏考虑了非隔离会话下 Task 派生可写子代理(Write/Edit)与父代理共享工作区时的竞态,今天进行了相关的思考和修改。

顺着思路排查,其实不只是 Task + Read,只要同一批次里 Task 和 Read / Glob / Grep 任一只读检索工具混用,一旦子代理在修改工作区,父代理的读取或目录/内容检索都可能读到中间态。因此在这次更新(已 rebase 到最新 main)中,我做了三处收敛:

  1. 混合批次退回顺序执行,保留纯 Task 与纯读批次并行:[Task, Task] 和纯 Read/Glob/Grep 批次继续并行;只要同一条消息把 Task 与 Read/Glob/Grep 混在一起,整批退回顺序执行(通过 Task 的动态 executionMode 实现)。
  2. 保持同批次顺序与同路径读写互斥,同时保留 ADR 0089 非阻塞返回
    • 若顺序为 [Read/Glob/Grep, Task],父代理先完成读取/检索再启动子代理;
    • 若顺序为 [Task, Read/Glob/Grep],Task 自身仍按 ADR 0089 立即返回 status: "running",但排在它后面的同批父代理读取/检索会等待前面带 Write/Edit 的子代理落定后再执行;
    • 在 PathMutex 中补了同路径共享读 / 独占写互斥,防止共享工作区下子代理进行中的 Edit/Write 与父代理同路径 Read 交叠。
  3. 回归测试:在 runtime.test.ts 和 scripts/e2e-task-read-batches.mjs 补齐了可写子代理修改同一文件时父代理读取/检索(Read/Glob/Grep)的单元与端到端回归测试(验证 task:end -> write:start -> write:end -> read:start -> read:end)。

另外,在修这个问题的过程中我认为:Task 现有的设计或许本身存在一个更底层的并发边界问题——它启动了一个可能跨轮次修改共享工作区的后台子任务,但按照 ADR 0089 只把“启动成功(running)”作为工具结果立即返回给父代理,并没有在父代理与后台子代理之间建立任何文件或工作区的资源占用关系,导致了现在的一些并发问题。

后续我希望从根源上解决跨agent/会话/工具的并发问题,目前正在重新思考和审查 #1519 的设计,当我考虑清楚之后,如果可以的话,我会修改这个issue,并分阶段提交pr。

vastsa commented Oct 26, 2026

Copy link
Copy Markdown
Owner

Re-reviewed ff6986ce9 against current main. The previous concurrency blocker remains: Task, Read, Glob, and Grep are all independently marked parallel, so a writable delegate can mutate the same workspace while the parent's read/search runs. Ignoring CI does not resolve this data-consistency regression. Please retain Task-only fan-out while guarding mixed Task/read batches, and cover a writable delegate overlapping a parent read with a behavioral regression. Leaving this open for that adjustment; merge conflicts can be handled once the execution policy is safe.

@caulif
caulif force-pushed the perf/parallel-read-batches branch from ff6986c to 0132ef9 Compare October 26, 2026 12:54
- allow Read/Glob/Grep to run in parallel batches while keeping Task mixed with Read/Glob/Grep sequential
- hold a same-batch parent read behind a preceding writable delegate until it settles while preserving Task's ADR 0089 non-blocking return
- coordinate shared Read against exclusive Write/Edit per path in PathMutex
- isolate delegate path-instruction preflight onto each delegate's own prompt
@caulif
caulif force-pushed the perf/parallel-read-batches branch from 0132ef9 to e9f1ca7 Compare October 26, 2026 13:05

vastsa commented Oct 26, 2026

Copy link
Copy Markdown
Owner

新版已经解决上次指出的同批 Task/读取竞态:混合批次顺序执行,排在可写 Task 后的父级读取等待该委托落定,纯 Task 和纯读取批次仍保留并行。已在合入最新主线的候选上独立通过 agent-runtime 全量 105 文件 / 1491 项测试,以及真实 Host + 本地 HTTP provider 的两项 E2E(混合 Task/Read 的 task:end → write:start → write:end → read:start → read:end;主/子代理并行 Read/Glob/Grep、结果顺序和提示隔离),委托 error/abort/stop/direct 四场景和 Desktop typecheck 也通过。刚更新的 e9f1ca7 与先前 0132ef9 的 tree 完全相同,无新增未测代码。当前主线已普通合并到 PR,核对 tree 一致后合入,保留贡献者历史。跨会话资源协调仍按文档留在独立阶段。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants