Repository navigation
perf(agent-runtime): parallelize independent read-only tool calls - #1596
Conversation
muzimu217
left a comment
There was a problem hiding this comment.
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.
|
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
left a comment
There was a problem hiding this comment.
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": |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
感谢指出问题和再次审阅,之前确实漏考虑了非隔离会话下 Task 派生可写子代理(Write/Edit)与父代理共享工作区时的竞态,今天进行了相关的思考和修改。
顺着思路排查,其实不只是 Task + Read,只要同一批次里 Task 和 Read / Glob / Grep 任一只读检索工具混用,一旦子代理在修改工作区,父代理的读取或目录/内容检索都可能读到中间态。因此在这次更新(已 rebase 到最新 main)中,我做了三处收敛:
- 混合批次退回顺序执行,保留纯
Task与纯读批次并行:[Task, Task]和纯Read/Glob/Grep批次继续并行;只要同一条消息把Task与Read/Glob/Grep混在一起,整批退回顺序执行(通过Task的动态executionMode实现)。 - 保持同批次顺序与同路径读写互斥,同时保留 ADR 0089 非阻塞返回
- 若顺序为
[Read/Glob/Grep, Task],父代理先完成读取/检索再启动子代理; - 若顺序为
[Task, Read/Glob/Grep],Task自身仍按 ADR 0089 立即返回status: "running",但排在它后面的同批父代理读取/检索会等待前面带Write/Edit的子代理落定后再执行; - 在
PathMutex中补了同路径共享读 / 独占写互斥,防止共享工作区下子代理进行中的Edit/Write与父代理同路径Read交叠。
- 若顺序为
- 回归测试:在
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。
|
Re-reviewed |
ff6986c to
0132ef9
Compare
- 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
0132ef9 to
e9f1ca7
Compare
|
新版已经解决上次指出的同批 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 一致后合入,保留贡献者历史。跨会话资源协调仍按文档留在独立阶段。 |
Refs #1519
这个 PR 实现 #1519 中的第一阶段优化:复用 pi-agent-core 1.1.0 已有的批次执行机制,开放主代理和子代理的内置
Read、Glob、Grep并行,减少独立读取和搜索调用之间的串行等待。行为变化
Read / Glob / GrepTask与Read / Glob / Grep混合TaskWrite / Edit / Bash或其他串行工具同样的只读策略也应用于子代理。未明确允许并行的工具默认保持串行;扩展工具也不会因为与内置工具同名而自动获得并行资格。
实现思路
这个改动沿用 pi 的执行流程:
主要修改包括:
tool-execution-policy.ts中集中定义并行工具白名单,避免主代理和子代理的策略不一致。Read / Glob / Grep,保留现有Task并行能力。路径指令处理是这个 PR 中除了执行模式之外的主要改动。现有文件工具执行过程会更新项目指令状态,如果只修改并行标记,这些更新就可能受并发完成顺序影响;子代理复用父代理工具时,也需要避免更新父代理的提示状态。
因此,这里先按调用顺序完成准备,再开始并发执行。直接或内部调用工具的路径仍保留准备逻辑。
规格与回归覆盖
同步更新以下规格和设计记录:
docs/spec/03-runtime/02-agent-runtime.mddocs/spec/03-runtime/03-tools-and-permissions.mddocs/adr/parallel-read-batches.md新增 E2E 场景:
E2E-TOOLS-parallel-read-batches,并提供scripts/e2e-parallel-read-batches.mjs。新增回归覆盖主要针对:
当前提交验证
ff6986ce9a9b(完整 SHA 见 PR head)。344861d66fb902d6be5d7ad908279d845396ad33,提交前重新 fetch 并确认它是候选提交的祖先。已执行并通过:
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,真实小文件Read → Write → Read,同样加入延迟Read → Bash → Read,同样加入延迟额外延迟用于验证等待是否能够重叠,以及串行批次是否仍保持顺序,不代表实际磁盘性能。真实小文件测试的绝对收益较小,这些结果也不能直接外推为完整 coding 任务的加速幅度。调用区间重叠也不等于证明底层文件系统函数同时执行。
还在上述旧基线上重跑了源码阅读、JSONL 汇总、JS 模块和 TodoMVC 四项真实任务,两版共 8 次运行均通过验收。由于模型选择的工具组合和工作量不同,这部分仅作为功能回归检查,不作为性能提升的主要依据。
兼容性与范围
本次沿用现有 Host 权限检查、容量控制和 RPC 接口,无需数据库迁移,也没有升级 pi 依赖。
这个 PR 只推进只读工具批次并行。
Write、Edit、Bash保留串行策略;跨代理文件写入协调、Shell 冲突协调和目录快照一致性需要后续单独设计。停止时已经发出的文件 RPC 仍按现有行为完成,本次没有增加更细粒度的取消机制。欢迎重点审查路径指令的有序准备、子代理状态隔离,以及 Task 与只读工具混合执行的行为。后续文件资源协调继续在 #1519 中讨论。