Skip to content

perf(agent-runtime): order parallel tool batches by resource conflicts - #1982

Merged
vastsa merged 6 commits into
vastsa:mainfrom
caulif:perf/parallel-tool-batches
Oct 31, 2026
Merged

vastsa merged 6 commits into
vastsa:mainfrom
caulif:perf/parallel-tool-batches

Conversation

@caulif

@caulif caulif commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Refs #1908

让已证明独立的文件读写在同一模型批次中重叠,并由 Host 保证别名文件的冲突调用保持源顺序。根据 review,本次用可信 Host 文件身份替换最初的词法路径/basename 冲突判断。

行为与实现

  • Read/Glob/Grep 保持并行候选;本地 Host 支持文件批次协议后 Write/Edit 才加入。旧 Host 和远程工作区保持含写整批串行。
  • runtime 按源序登记并封存调用;Host 在现有权限检查通过后识别文件身份,检查批内前驱,再一次授予全部资源与容量。ticket 绑定具体调用,不授予权限。
  • 不同名称和不同根的硬链接仍冲突;符号链接按 Host 解析结果协调。身份未知、目标不存在、搜索、命名空间变化或命名寄存器按保守声明处理。
  • 实际 blocking 任务持有租约;RPC 取消或批次关闭不提前放锁。review 快照和回滚纳入同一协调机制。等待期间变化的工作区或文件绑定会在写入前拒绝,不重放写。
  • Task 混文件批次的串行边界、Bash 和插件/MCP 策略不变。每会话写上限仍为 1;不加入统一后台作业或远程端完整身份协议。

验证

候选:20abd41b51bd35606d3f9ec1db416ef251d631de;基线:97cc04debf261a423987a9d3ced4036d80073c5b,为候选祖先。

  • agent-runtime:1,631 通过、1 跳过;host-runtime:158 通过、3 跳过。
  • Native 定向 46 项、ToolBudget 26 项、文件批次 RPC 3 项通过,几组存在重叠,不相加。
  • 真实 Host + 本地 HTTP provider 的文件批次及 Task/read 两组 E2E 通过,覆盖父子代理硬链接两种源序、真实 tag Edit、分别回滚、同时审批。候选提交后再次运行这两组检查。
  • 类型检查、Clippy、格式、文档中英文一致性、架构、策略同步和依赖检查通过;Clippy 保留未改动 transcripts.rs 的警告。

Host 全量 release 测试未通过并已中止。在未修改的上述 upstream 基线上独立复现了远程权限通知与旧数据迁移路径格式两项失败;其余失败尚未逐项确认,不能据此将全量测试记为通过。

文件符号链接创建受本机 EPERM 限制,该项跳过;Linux/macOS 原生执行、Electron UI、远端 CI 和 PR integration candidate 尚未验证。未测量真实负载墙钟性能,不沿用旧版性能数字。

规格、协议、ADR 与 E2E 计划已同步。请重点复核文件别名、源序与公平性的关系,以及取消/回滚时的租约释放边界。此保证限于单个 Host 中参与协调的文件工具及回滚,不覆盖任意外部进程、Bash 或另一个 Host 实例。

Any Write or Edit in an assistant message made pi run the whole batch
sequentially, so unrelated reads and writes paid one Host round trip
each. Write and Edit now join pi's parallel batches, and a batch-local
planner orders calls only where their declared resource claims
conflict; same-file work keeps source order, everything else overlaps.

The execution policy becomes one spec table with per-call claims. An
Edit with MV, REM or a named register claims the whole workspace,
because host-core keeps named registers per session and a CUT @x must
stay ahead of a PUT @x on another file. A registered call is always
released: waiting and releasing wrap the catalog executor outside the
mode guard and argument checks, with a turn-end backstop for parent and
delegate batches, so a call that throws, is rejected or aborts never
leaves its successors waiting. Host-core, PathMutex and Bash/Task
semantics are unchanged; Task mixed with Write/Edit stays sequential.

Refs vastsa#1908
The amendment line named the change by its commit title because the
pull request number did not exist yet. Point it at the pull request so
the ADR history resolves to the review. Status stays Proposed.
The English tools spec gained a claim table, and the locale check
requires each Chinese mirror to keep the same table structure, so the
Docs checks job failed. Translate the Parallel file batches section into
the zh-CN tools spec and update the zh-CN concurrency paragraph, which
still described read-only batches.

@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.

当前 head 9eead29d0e9a99207d1422dd41f9d5ab49d27a29 暂不能合入:新增的读写并发存在文件别名遗漏,会破坏同一文件的源顺序保证。

具体复现:临时工作区创建 actual.txt,再创建 alias.txt -> actual.txt,用 realpath 确认是同一文件;同一模型回复发出 [Read alias.txt, Write actual.txt]。在现有 runtime.test.ts 的 startBatch/gatedHost 上增加回归,保持 Read 的 Host 响应未完成:候选已进入 [first, second],Write 没有等待 Read;替换为当前 main 的 runtime.ts 和 tool-execution-policy.ts 后同一测试通过(只进入 first,释放后才进入 second)。生产 Runtime/pi 执行链是真实的,Host RPC 为可控闸门;这不是实际磁盘读写撕裂 E2E,但直接证明新增调度丢失顺序。

原因在 batch-planner.ts 的 sameFile:完整 lexical key 或 basename 相同才判定冲突。不同 basename 的文件符号链接(以及硬链接等别名)均不满足;PathMutex 也使用 lexical key,不能兜底。Host 的 resolve_tool_path_with_external 会解析符号链接,而 tool_write 对 resolved 路径执行 std::fs::write,Host 的单写准入不能排除并发 Read。原来的整批串行保护在这个 PR 中被移除了。

建议沿用当前 planner 补齐资源身份:对 Host 实际访问的文件使用可信的别名/文件身份信息,无法证明独立的路径保守串行(远程工作区不能依赖 sidecar 本机 realpath);或者在能够保证别名安全前保留相关写批次的串行边界。仅增加 basename 特判无法覆盖这类输入。请加入不同文件名的符号链接读写回归,并检查硬链接、远程路径和不存在目标的保守策略。至少覆盖上述顺序及反向顺序,验证父代理与子代理共用的入口。

已完成:确认 main 的 Write/Edit sequential 策略确实让混合批次串行,性能问题存在;候选原有 batch-planner/runtime/subagent 三文件 489/489 测试通过,共享包构建通过,git diff --check 通过,六项远端 CI 全绿。新增别名回归在 main 通过、候选失败。因已有可复现的正确性阻碍,本轮没有继续运行完整本地 Host/Electron E2E,也没有修改或推送贡献者代码。

Replace lexical file independence with Host-owned identities and source-order admission. Keep execution leases alive through blocking file work, review snapshots and rollback, while preserving permission checks and legacy fallback.

Integrate upstream main at 97cc04d and retain existing write limits. Add alias, reversed-arrival, cancellation and queue-fairness regressions with synchronized protocol and runtime specifications.
@caulif

caulif commented Oct 31, 2026

Copy link
Copy Markdown
Contributor Author

感谢指出这个问题。原来的词法路径和 basename 判断确实漏掉了不同名称指向同一文件的情况,PathMutex 也不能兜底。这次将文件身份识别和冲突准入放到了实际访问文件的 Host,并补上了批内源序与执行租约的生命周期。

修改内容

  • runtime 在有序 prepare 阶段登记调用参数和源序号,批次封存后携带单次 ticket 执行。ticket 绑定 session/delegation、调用 ID、工具名和参数,只用于排序,不代替权限检查。
  • Host 在权限通过后识别真实文件身份。Windows 使用本地磁盘的卷标识与 FILE_ID_INFO,Unix 使用支持的本地文件系统的 dev/ino;排队和执行期间保留句柄。不同名称、不同根的硬链接仍视为同一资源。
  • 同批冲突调用按源序执行,不依赖 RPC 或审批的到达顺序。身份未知、目标不存在、MV/REM 和命名寄存器保守互斥;资源与容量一起授予,等待前驱时不占执行槽。
  • 租约由实际 blocking 任务持有,RPC 取消或批次关闭不会提前释放。review 快照、执行及回滚进入同一协调机制;工作区或文件绑定在等待期间变化时,写入前拒绝,不自动重放。
  • 支持新协议的路径由 Host 替代 sidecar 的资源锁;旧 Host 和远程工作区保持含写整批串行,不使用本机文件系统判断远程文件身份。

也补了一项排队回归:尚未满足源序依赖的后继,不能作为公平性屏障挡住另一个批次,否则可能间接卡住自己的前驱。

验证结果

候选提交:20abd41b51bd35606d3f9ec1db416ef251d631de。基线:upstream/main@97cc04debf261a423987a9d3ced4036d80073c5b,已确认是候选祖先。

在 Windows 上执行:

  • agent-runtime:1,631 项通过、1 项跳过;host-runtime:158 项通过、3 项跳过。
  • Native 定向测试 46 项、ToolBudget 26 项、文件批次 RPC 3 项通过。这几组有重叠,不合并计数。
  • 两组真实 Host E2E 通过:父代理及子代理的硬链接读写正反序、真实 tag 的 Edit、独立文件并发分派、分别回滚、同时等待审批,以及原有 Task/read 回归。
  • 类型检查、Clippy、格式、文档一致性、架构、策略同步及依赖检查通过。Clippy 保留一项未修改的 transcripts.rs 警告。

Host 全量 release 测试未通过,出现后台进程、数据迁移和远程权限用例失败。另用未修改的 upstream/main@97cc04deb 复现了其中两项:

  • denied_remote_write_and_cancelled_read_never_run_locally:收到 permissions.request,断言期待 tools.remoteExecute。
  • handles_legacy_schema_without_upgrading_or_creating_missing_tables:Windows 路径斜杠格式断言不一致。

其余失败尚未逐项确认;全量检查在这之后中止,因此这里不将 Host 全量测试记为通过。

文件符号链接创建受本机 EPERM 限制,该项跳过;Linux/macOS 原生执行、Electron UI、远端 CI 和 PR integration candidate 尚未验证。此次没有真实负载下的墙钟性能对照,原来的性能数字不再作为修订后实现的证据。

范围

这次增加的 Host 协议用于解决当前读写并发的正确性问题,规格、ADR 和 E2E 计划已同步。每会话写额度仍为 1;RESOURCE_BUSY、远程端完整身份协议、统一后台作业及其他吞吐优化仍留给后续。租约覆盖单个 Host 中接入协调的文件工具和回滚,不覆盖任意外部进程、Bash 或另一个 Host 实例。

请重点复核文件别名、源序与公平性的关系,以及取消和回滚时的租约释放边界。

Publish active turn state before awaiting Host capabilities and honor cancellation before provider dispatch. Update the history restore fixture and add a pending-negotiation regression test.

Avoid blocking identity classification on Unix FIFOs, retain post-open identity checks, and cover special-file fallback with a regression test.
@caulif

caulif commented Oct 31, 2026

Copy link
Copy Markdown
Contributor Author

已在 bdc35fa12 修复这轮 CI 暴露的两处问题:

  • 历史恢复测试的反向 RPC fixture 需要在成功恢复后响应 tools.fileBatchCapabilities;恢复失败时仍禁止进入能力协商。
  • 能力协商前就发布 turn 的运行状态,避免 UI 在协商等待期间误判 turn 已结束;Stop 也会在 provider dispatch 前生效。新增了挂起能力协商并触发 Stop 的回归测试。

另外做了一轮独立审查,发现 Unix FIFO 的身份分类可能阻塞。现在先筛选普通文件,Unix 打开时使用 O_NONBLOCK,打开后仍复核对象类型和身份;FIFO 回归测试已加入。

本地验证:agent-runtime 1,632 项通过、1 项跳过;历史恢复测试通过;Electron trusted extensions 57/57 通过;真实 Host 的并发文件与 Task/read E2E 通过。Native 定向测试、Clippy、格式及 JS 构建和 lint 通过;文件符号链接 E2E 仍受本机权限限制而跳过。

本次提交的远端 CI 已全部通过(6/6):JS 综合检查、Linux Rust 全量测试、Windows Rust / transcript cache E2E、Electron trusted extensions E2E、文档检查和最新基线检查。Linux CI 也覆盖了新增的 FIFO 用例。CI 结果

@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.

Re-reviewed Host identity/source-order admission against the previously independently reproduced whole-batch serialization bottleneck and differently named symlink ordering regression. Host-owned identities, conservative unknown-resource fencing, execution-owned leases and legacy/remote fallback address the root cause. No remaining material blocker found.

Original bdc35fa fails to compile on macOS: f_flags is u32 while MNT_LOCAL is i32 (E0308/E0277). Added minimal fix f571c78 on this PR branch: cast the flag constant only.

Validated f571c78 locally:

  • shared, agent-runtime, agent-host and host-runtime builds pass. Runtime: 117 files / 1632 tests pass. Host-runtime: 22 files / 161 tests pass.
  • macOS Host full suite: 1106 pass, 1 ignored, 0 fail. File-focused 47 and ToolBudget 26 tests pass (overlap full suite). cargo fmt and diff checks pass.
  • Production Runtime/pi with candidate-built real Host E2E passes parent/delegate differently named symlink AND hardlink Read/Write both orders, independent-file concurrent dispatch, real-tag Edit, two separate Write rollbacks, concurrent ask-mode approvals with independent allow/deny. macOS symlink cases executed without skips. History restore test passes.
  • Initially reused dependencies were incompatible/stale; installed locked dependencies in isolated worktree and reran successfully. Primary environment unchanged. Initial E2E binary path typo prevented launch; corrected path completed successfully.
  • Latest main 97cc04d is ancestor. PR integration 481663f and validated head have identical tree 99dfc960a3b02ec9d9d9d681efceef4c5adbbcde.

Concurrent-dispatch evidence is not a real-load wall-clock speedup claim. No local Electron UI or native Windows run; waiting for all new-head remote CI gates before merging.

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.

2 participants