Skip to content

[Feature] 统一工具并发执行策略:资源声明 + Host 资源租约 + 统一后台作业(#1519 修订版) #1908

Done

Description

@caulif

Description

这是 #1519 的修订版设计。#1596(第一阶段:Read / Glob / Grep 并行)已经合并,感谢 review 时指出 Task 与父 Agent 读取之间的竞态。那次修复让我意识到,#1519 原来按"工具名 → 并行/串行"逐阶段开放的思路,会让每加一类工具都要再写一段特判。所以想先把整体方向提出来讨论,确认后再拆成小 PR。

本 issue 只讨论方向和拆分方式,不包含代码改动。基于 main@5aa2538(2026-10-26)阅读的代码。

Problem / 问题

目前确认到的代码行为

并发相关的判断目前分布在 sidecar 和 host-core 两个进程、TypeScript 和 Rust 两种语言中:

# 位置 规则
1 agent-runtime/src/tool-execution-policy.ts Read/Glob/Grep/Task → parallel;Task 与读混批 → sequential;其余 sequential
2 runtime.ts READ_ONLY_CORE_TOOLS / PATH_MUTATING_TOOLS / canAbandonWait / hostAbortsProcess 中止行为与加锁范围
3 agent-runtime/src/path-lock.ts PathMutex 进程内路径读写锁
4 runtime.ts waitForBatchWritableDelegations 同批 Task 之后的读等待写型委派结束
5 subagent.ts 子 Agent 构造时再次套用 toolExecutionMode
6 host-core/src/tool_budget.rs 总 16 / 读 8 / 写 2 / shell 4 / 插件 4;每会话 4;每会话写 1;排队 30s 超时
7 tool_budget.rs ToolClass::from_name 按工具名硬编码分类,与 #1 重复
8 host-core/src/background_tasks.rs Bash runInBackground 注册表
9 session_worktrees/ 隔离会话与写型子 Agent 的独立 worktree
10 Task 工具描述 写给模型的并发规则文字

由此产生的问题

  1. 整批降级。 pi 的 executeToolCalls 只要批内有一个 sequential 工具就整批串行。[Read a, Read b, Read c, Edit d] 四个全部串行;Write 不同文件也串行。
  2. 按工具名判断,不看参数。 新工具都要人工判断;插件和 MCP 工具即使只读也只能串行。
  3. 路径锁只在 sidecar 内。 同一项目的两个非隔离会话写同一文件,只有 Edit 的 hashline tag 兜底,Write 没有版本校验。
  4. Host 写额度按会话计。 MAX_IN_FLIGHT_MUTATIONS_PER_SESSION = 1 对拥有独立 worktree 的子 Agent 同样计数,多个写型子 Agent 写不同文件仍一次一个。
  5. Task 屏障粒度粗。 perf(agent-runtime): parallelize independent read-only tool calls #1596 保留的边界是正确的,但一个 Read 可能等整个写型委派结束(可能几分钟)。
  6. 后台 Bash 的输出模型读不到。 backgroundTasks.output 目前只有 UI 调用。模型启动后台命令后无法读取输出、等待或停止,所以 Bash 工具描述只能提醒"当前回合依赖输出时不要后台运行"。
  7. 预算公平性。 父 Agent 和最多 10 个子 Agent 共享每会话 4 个在途槽位,排队 30s 后返回 HOST_OVERLOADED。并发放开后,这类错误会更多地进入模型上下文。
  8. 规则重复。 TS、Rust、提示词各有一份,改一处容易漏另一处。

对性能影响的判断

和 #1519 一样,我目前没有真实使用场景下的对照数据,不做提速承诺。#1596 的微基准只能说明读批次的排程形状变了。因此下面的计划把"先采集数据"放在第一步。

Proposed change / 期望改动

总体思路

保留两个已有方向:pi 的"有序准备 → 并发执行 → 按源顺序提交结果"契约(不 fork pi),以及 Host 拥有资源和容量的架构。改变的是"哪些调用能同时跑"的判断方式:从按工具名白名单 + 整批降级,改为按资源声明的冲突排序。

一个批次在新方案中的执行路径

图中用一个批次 [Read a, Edit a, Read b, Write c] 走一遍完整路径,同时有一个子 Agent 要 Edit b。绿色是新增,橙色是修改,虚线是保持不变:

  1. 有序 prepare(不变)。 pi 按源顺序准备每个调用,包括参数规范化和路径指令解析,perf(agent-runtime): parallelize independent read-only tool calls #1596 已经完成这一步。
  2. 查规格表(新增)。 每个调用根据工具和参数得到资源声明(claims),例如 Edit a → path:a 独占。
  3. BatchPlanner 排依赖(新增)。 只有资源键相同且有一方独占时,后面的调用才等前面的。本例中只有 Edit a 要等 Read a,Read b 和 Write c 立即发出。今天批内只要有写,4 个调用就全部串行。
  4. Host 准入(修改)。 tools.execute 带着 claims 进入 host-core,Host 用同一份规格复算。父 Agent、子 Agent 和同项目其他会话的调用都查同一张租约表。子 Agent 的 Edit b 与父 Agent 正在执行的 Read b 冲突,因此排队。
  5. 执行 → 释放 → 唤醒(修改)。 执行本身不变。调用完成后释放租约,dispatch() 唤醒等待者:Read b 结束后,Edit b 获准执行。
  6. 按源顺序提交(不变)。 无论哪个调用先完成,结果都按源顺序进入上下文。
  7. 长操作(修改)。 后台 Bash 和 Task 都返回作业 id,之后统一用 TaskWait / TaskList / TaskStop 等待、查看和停止。

四类并发对应的步骤:① 模型一次提出多个调用 → 第 2、3 步;② 一个 Agent 同时使用工具 → 第 4、5 步;③ 长操作在后台运行 → 第 7 步;④ 多个 Agent 或会话 → 子 Agent 和其他会话同样经过第 2 – 5 步,冲突由同一张租约表处理。

下面按层说明细节。

L0:统一的工具执行规格

在 packages/shared 中用一张表描述每个工具:

工具 class claims(args) 屏障 中止
Read read path:p shared 否 放弃等待
Glob / Grep read tree:root shared 否 放弃等待
Write / Edit mutation path:p exclusive(重命名时加目标路径) 否 等待完成
Bash shell tree:root exclusive 否 杀进程树
Task delegate 写型且共享工作区:批内意向写;否则无 否 停止委派
TodoWrite state session/todos exclusive 否 等待完成
asktool、模式切换、ToolSearch、new_context control — 是 现有行为
插件 / MCP / 未知工具 external tree:root exclusive(默认) 否 等待完成

#1 – #7 和 #10 改为从这张表读取。Rust 侧手写镜像,加一份 TS/Rust 一致性测试(参照已有的 TOOL_QUEUE_WAIT_MS 镜像做法)。

L1:批内冲突排序

规则只有一条:批内调用 j 依赖于源顺序在它之前、且与它资源声明冲突的每个调用 i(同一个键,且至少一方 exclusive)。屏障工具单独成段。

实现上不改 pi:非屏障工具在 pi 层标为 parallel,pi 会按顺序完成全部 prepare 再同时启动 execute;我们在 execute 包装里先等待依赖。屏障工具保持 sequential,继续走 pi 的逐个 prepare 路径,所以模式切换等工具的语义不变。

例:[Read a, Edit a, Read b, Write c, Edit b] 中,Edit a 等 Read a,Edit b 等 Read b,Write c 立即开始。

同一批次的排程对比

失败与取消:

  • 前驱失败不级联,与现在串行执行时的语义一致。
  • Stop 时,还在等依赖或租约的调用立即以 TOOL_ABORTED 结束;已开始的调用沿用现有中止行为(读放弃等待,写等待 Host 返回,Bash 由 Host 杀进程树)。

L2:Host 资源租约

扩展 ToolBudget:

  • tools.execute 携带 claims,Host 用同一份规格复算,以 Host 结果为准。runtime 侧的 claim 只描述逻辑资源:{ kind: "path" | "tree" | "session-state" | "ext", arg: <参数中的路径>, access: "shared" | "exclusive" },不含任何文件系统路径。
  • can_admit 增加资源冲突判断;一次调用的容量和全部租约原子授予(全有或全无),因此不会死锁。同键存在更早的 exclusive 等待者时,后来的 shared 请求也排队,避免写饥饿。
  • 规范键由 Host 计算:(tool_root 的 realpath, 规范化相对路径)。按 ADR 0329,runtime 只发送 sessionId / delegationId,根目录由 Host 通过 tool_root() 解析。父 Agent、子 Agent、同项目的其他会话共用一张表;写型子 Agent 在独立 worktree 中时根不同,键自然不同,不再共用写槽位。
  • 资源冲突等待超时返回新的 RESOURCE_BUSY,附带持有者信息;容量不足仍返回 HOST_OVERLOADED。
  • 路径级租约就位后,可以提高每会话写上限(全局写上限保留),并删除 sidecar 的 PathMutex。
  • Edit hashline tag 不变:租约防止交错写,tag 防止基于过期内容的写。

关于 Bash:默认对工作区声明 exclusive,即同一批内与读写互斥,但 Bash 之间、Bash 与其他会话之间的行为与现在相同,不引入全局 shell 锁。识别只读命令后放宽为 shared,放在最后阶段按数据决定。

L3:统一后台作业

把 Bash runInBackground 纳入 Task 的作业生命周期,不新增工具名:

  • Bash{runInBackground:true} 仍返回 backgroundTaskId,该 id 可直接传给 TaskWait / TaskList / TaskStop。
  • TaskWait 对 Bash 作业返回状态、退出码和有界的 stdout/stderr 尾部。
  • Bash 作业默认不在结束时自动交付(服务器类进程常驻);启动时传 notifyOnExit 才交付。
  • 作业不持有资源租约,并在工具描述中写明。
  • Host 仍是 Bash 进程的唯一所有者,runtime 只做代理查询。

L4:委派与会话

保持 ADR 0165 的方向,只经父 Agent 汇报,不引入新的协调通道。改动只有:

  • Task 的批内意向写声明替代 waitForBatchWritableDelegations,语义与 perf(agent-runtime): parallelize independent read-only tool calls #1596 合并时一致:同批中 Task 之后的读仍等待写型委派结束。跨批次不再整体阻塞,由路径租约逐文件保护。
  • Host 每会话在途上限改为按 Agent 计,父 Agent 保底 1 个槽位。

分阶段 PR

每个 PR 可以单独合并、单独回滚。

  1. 策略表纯数据(零行为变化)。 新增 ToolExecutionSpec,固定 claim 的数据结构;新增与 toolExecutionMode、ToolClass::from_name 的等价测试;不改任何调度行为。
  2. BatchPlanner + 本地诊断。 批内冲突排序;本地日志记录批次形状、排队时间和 Host 等待原因,不上报。测试:同路径 Read→Edit→Read 的顺序、屏障独占、Stop 时未开始的调用立即结束、结果按源顺序,以及 perf(agent-runtime): parallelize independent read-only tool calls #1596 的全部回归测试。
  3. Host 资源租约。 包含 RESOURCE_BUSY、提高每会话写上限、替代 waitForBatchWritableDelegations、删除 PathMutex。测试:两个会话写同一文件有序、父读与子写同一文件不交错、重命名双声明无死锁(随机化压力测试)、取消释放租约、隔离 worktree 互不阻塞。附新 ADR。
  4. 统一后台作业。 修订 ADR 0089。
  5. 按数据放宽。 只读 Bash 识别、MCP 服务器级并行开关、预算按 Agent 公平分配,每项独立开关,依据第 2 步起采集的诊断数据排序。

必须守住的不变量:结果按源顺序进入上下文;批内冲突调用按源顺序执行;屏障工具单独成段;未知、插件、MCP 工具默认独占;多键获取全有或全无;Host 复算的声明不比 runtime 宽松;Stop 后写调用不在报告停止后继续落盘;父读不与共享工作区的子写交错(#1596 的回归测试继续保留)。

Alternatives / 其他方案

  • 继续按 [Feature] 工具并行执行优化:参考 pi,分阶段开放独立调用并统一执行策略 #1519 原计划逐工具开放。 改动小,但每阶段都要新增批次特判,TS/Rust/提示词三处规则继续分叉。
  • 直接采用 pi 的默认(全部并行 + 写队列)。 pi 的读不等写,在子 Agent 写、父 Agent 读的场景下会读到半途状态,正是 perf(agent-runtime): parallelize independent read-only tool calls #1596 review 中指出的问题。
  • 采用 Codex 的 turn 级读写锁。 比整批降级好,但锁只有一把,读和不相关文件的写仍然互斥;路径级声明能在相同正确性下保留更多并发。
  • 只在 sidecar 内加锁。 覆盖不到跨会话;host-core 已经是所有工具调用的唯一入口,放在 Host 更自然。
  • 维持现状。 如果诊断数据显示工具耗时不是主要瓶颈,后续阶段可以不做,第 1 步本身仍保留价值(规则统一、claim 结构固定)。

Additional context / 补充信息

参考实现(只借鉴原则,不搬代码):

  • pi:packages/agent/src/agent-loop.ts(executeToolCalls 的整批判定与有序提交),packages/coding-agent/src/core/tools/file-mutation-queue.ts(按 realpath 的写队列)。
  • Codex:codex-rs/core/src/tools/parallel.rs(supports_parallel_tool_calls 与 turn 级 RwLock),tools/handlers/unified_exec(长命令返回 session id 后轮询)。
  • Claude Code:公开文档中的并发安全工具分组、后台 Bash 任务与子 Agent worktree 隔离。

相关 ADR:0062、0089、0165、0166、0329、parallel-read-batches。

希望维护者给出的意见

  1. 把路径互斥从 sidecar 移到 host-core、扩展 ToolBudget 承担资源租约,这个方向是否符合项目的架构规划?
  2. 第 1 步(策略表纯数据 + 等价测试,零行为变化)是否可以作为下一个 PR 先提交?
  3. 后台 Bash 复用 TaskWait / TaskList / TaskStop,而不是新增工具,这个取舍是否可以接受?
  4. 第 2 – 5 步中,有没有希望缩小、推迟,或者由维护者自己规划的部分?

如果方向认可,我会按上面的顺序提交小 PR;涉及新 ADR 或公共协议(tools.execute 参数、新错误码)的变更,会在对应 PR 前先在这里确认。


修订记录(2026-10-27): 根据 @muzimu217 的 review,明确租约键由 Host 基于 tool_root() 计算、runtime 只声明逻辑资源;第 1 步缩小为纯数据加等价测试,本地诊断移到第 2 步。

Activity

  1. added
    enhancement
    on Oct 27, 2026
  2. muzimu217 commented on Oct 27, 2026

    @muzimu217
    Contributor

    Verified the factual base of this proposal on main@5aa253883. The three load-bearing claims I could check are exact, which is worth saying before the design discussion:

    • packages/agent-runtime/src/tool-execution-policy.ts:4-13 — toolExecutionMode is a name switch: Read | Glob | Grep | Task → parallel, everything else sequential.
    • Same file, resolveBatchExecutionMode — the perf(agent-runtime): parallelize independent read-only tool calls #1596 boundary is hasTask && hasFileRead ? "sequential" : undefined, i.e. the Task-plus-reads mix is a whole-batch downgrade.
    • crates/host-core/src/tool_budget.rs:7-14 and :31-38 — the budgets are as you list them, and ToolClass::from_name is a name match (Read|Glob|Grep, Write|Edit, Bash, _ => Plugin). Your problem 老哥 再提个优化建议 #7 (duplicate classification) is real: the TS switch and this Rust match encode the same knowledge in two languages.

    One question that decides the shape of the lease table. The per-session mutation cap is keyed on the session id alone (tool_budget.rs:205-209):

    self.sessions.get(&request.session_id).is_none_or(|usage| {
        usage.active < MAX_IN_FLIGHT_PER_SESSION
            && (request.class != ToolClass::Mutation
                || usage.mutations < MAX_IN_FLIGHT_MUTATIONS_PER_SESSION)
    })

    There is no worktree or path awareness in that key. If a write-type delegate carries the parent's session_id (the delegate's calls use this.opts.sessionId throughout subagent.ts), then two delegates editing different files in separate worktrees still share one mutation slot — your problem #4. So the lease key should be the write target's identity (the normalized path, and the worktree root it belongs to), not the session. Worth settling that before the spec table, because it decides what a claim has to contain.

    On the split, your "data before mechanism" ordering looks right, and I would make the first PR even smaller than the spec table: land the table as pure data plus a test that it agrees with the two existing encodings (toolExecutionMode and ToolClass::from_name) — no scheduling change at all. That closes problem #8 and gives later PRs a single source to delete the duplicates from, while being reviewable as "two switches become one table, behaviour identical". BatchPlanner and the Host lease table can then each be judged on their own, which is also how the #1596 review went.

    I have no real-workload timing data either, so I agree with not promising speedups; the [Read a, Read b, Read c, Edit d] case is easy to demonstrate as a shape change (three parallel, one serial) without claiming wall-clock wins.

  3. caulif commented on Oct 27, 2026

    @caulif
    ContributorAuthor

    Verified the factual base of this proposal on main@5aa253883. The three load-bearing claims I could check are exact, which is worth saying before the design discussion:

    • packages/agent-runtime/src/tool-execution-policy.ts:4-13 — toolExecutionMode is a name switch: Read | Glob | Grep | Task → parallel, everything else sequential.
    • Same file, resolveBatchExecutionMode — the perf(agent-runtime): parallelize independent read-only tool calls #1596 boundary is hasTask && hasFileRead ? "sequential" : undefined, i.e. the Task-plus-reads mix is a whole-batch downgrade.
    • crates/host-core/src/tool_budget.rs:7-14 and :31-38 — the budgets are as you list them, and ToolClass::from_name is a name match (Read|Glob|Grep, Write|Edit, Bash, _ => Plugin). Your problem 老哥 再提个优化建议 #7 (duplicate classification) is real: the TS switch and this Rust match encode the same knowledge in two languages.

    One question that decides the shape of the lease table. The per-session mutation cap is keyed on the session id alone (tool_budget.rs:205-209):

    self.sessions.get(&request.session_id).is_none_or(|usage| {
    usage.active < MAX_IN_FLIGHT_PER_SESSION
    && (request.class != ToolClass::Mutation
    || usage.mutations < MAX_IN_FLIGHT_MUTATIONS_PER_SESSION)
    })
    There is no worktree or path awareness in that key. If a write-type delegate carries the parent's session_id (the delegate's calls use this.opts.sessionId throughout subagent.ts), then two delegates editing different files in separate worktrees still share one mutation slot — your problem #4. So the lease key should be the write target's identity (the normalized path, and the worktree root it belongs to), not the session. Worth settling that before the spec table, because it decides what a claim has to contain.

    On the split, your "data before mechanism" ordering looks right, and I would make the first PR even smaller than the spec table: land the table as pure data plus a test that it agrees with the two existing encodings (toolExecutionMode and ToolClass::from_name) — no scheduling change at all. That closes problem #8 and gives later PRs a single source to delete the duplicates from, while being reviewable as "two switches become one table, behaviour identical". BatchPlanner and the Host lease table can then each be judged on their own, which is also how the #1596 review went.

    I have no real-workload timing data either, so I agree with not promising speedups; the [Read a, Read b, Read c, Edit d] case is easy to demonstrate as a shape change (three parallel, one serial) without claiming wall-clock wins.

    感谢核对,结论一致的部分就不重复了,下面回应几点:

    1. 租约键。 同意先定下来。补充一个约束:按 ADR 0329,runtime 不发送 worktree 路径,只发 sessionId / delegationId,由 Host 通过 tool_root() 解析根目录。因此我打算这样划分:

    • runtime 侧的 claim 只描述逻辑资源:{ kind: "path" | "tree" | "session-state" | "ext", arg: <参数中的路径>, access: "shared" | "exclusive" }。
    • Host 侧计算规范键:(tool_root 的 realpath, 规范化相对路径)。写型子 Agent 在独立 worktree 中时,根不同,键自然不同,不再共用写槽位。
    • 以 Host 复算的结果为准,runtime 不能声明得比 Host 宽松。

    这样 claim 的结构在第 1 步就能固定下来,后续 Host 租约那一步只是开始使用它。正文 L2 已同步更新。

    2. 第一个 PR 的范围。 同意。第 1 步只做规格表的纯数据,加上与 toolExecutionMode、ToolClass::from_name 的等价测试,不改任何调度行为。本地诊断移到 BatchPlanner 那个 PR 里一起做。正文的分阶段计划已同步更新。

    3. BatchPlanner 和 Host 租约表会分成两个 PR,分别评审。

    4. 同意。正文里的排程图只示意执行形状,后续 PR 也按"执行形状的变化"来展示,不给墙钟提速数字。

    @vastsa 如果方向上没有异议,我可以先提交第 1 步(纯数据 + 等价测试)的pr。

  4. muzimu217 commented on Oct 27, 2026

    @muzimu217
    Contributor

    Checked the lease-key premise on main@5aa253883. It holds further than my earlier comment assumed, but the work lands one layer later than the key itself.

    The root really does differ per delegate, and the runtime already sends the id. session_worktrees/delegate.rs:58-73 returns Some(row.workspace_root()) only when delegation_id is present and that delegate owns a worktree (None otherwise, missing_error() when the directory vanished); lifecycle.rs:136-153 gives an isolated session its own root and never falls back to the main checkout. On the client side, runtime.ts:5150-5155 decides a delegate needs a worktree when the session is isolated and the delegate's tools include Write/Edit; :4898-4906 creates it with {sessionId, delegationId}; :3617-3619 attaches that id to each of the delegate's tools.execute calls (the object built at :3576). So (tool_root realpath, normalized relative path) already separates a write delegate from its parent — no new plumbing is needed for the key.

    The gap is one layer later, and the resolution point is already in the admission path. rpc/mod.rs:4365-4384 calls delegate::tool_root(...) before admission, then collapses the result into a bool own_worktree. tool_budget.rs:193-212 uses that bool only to skip the exclusive-session gate (if !request.own_worktree && (exclusive...)); the per-session block at :205-209 still applies unconditionally, and its usage is keyed by the parent session_id. That is why two write delegates with separate worktrees still share MAX_IN_FLIGHT_MUTATIONS_PER_SESSION = 1 — problem #4 in the proposal.

    So the L2 PR does not need a runtime prerequisite: it can compute the canonical key at :4365, where the root is already resolved, and the only behavioural change is to stop discarding the path. That also keeps the diff reviewable as "same admission, different key", which is the shape you proposed for step 1.

    Step 1 (pure spec table + equivalence tests) is unaffected: nothing above changes what a claim must contain, only where the Host can compute its key.

  5. caulif commented on Oct 29, 2026

    @caulif
    ContributorAuthor

    补充一个流程约束:按 CONTRIBUTING.md 的 R6.1,外部贡献目前只接受 fix / perf。所以我没有单独提「纯数据规格表」的 refactor,而是把规格表和 BatchPlanner 合成了一个 perf(agent-runtime) PR:

    • Write/Edit 加入并行批次,同批内只在资源冲突时按源顺序等待;
    • Bash 及其他工具仍整批串行;Task 混批的整批串行规则扩展到 Write/Edit(只有读会等写型委派结束);
    • 不改 host-core,PathMutex 保留;Host 仍是每会话 1 个写,同批多个写会在 Host 排队;
    • Rust 侧镜像与等价测试、本地诊断推迟到后续 PR(R6 最小改动)。

    PR 链接稍后补上。如果维护者更希望拆开,或由维护者处理纯 refactor 部分,告诉我即可。

  6. caulif commented on Oct 31, 2026

    @caulif
    ContributorAuthor

    根据 #1982 的 review,调整一下这里的实施顺序和资源身份设计。

    原计划先用 BatchPlanner 开放路径级读写并发,再补 Host 租约。但词法路径、basename 和单独 realpath 都不足以证明两个文件独立,硬链接也可能跨不同根指向同一个文件。因此,可信文件身份和 Host 执行协调需要与读写并发一起交付。

    #1982 的范围调整

    将原第 1、2 步和第 3 步中安全必需的部分合并:runtime 按源序登记并封存批次;Host 在权限通过后识别资源,检查同批前驱,并统一授予资源与容量;实际磁盘任务完成后才释放租约。review 快照和后续 rollback 也进入同一协调机制。

    这样既保留已证明独立文件的读写重叠,也保证同一文件的别名调用不会因审批或 RPC 反向到达而反序。

    设计修订

    1. L0 区分逻辑意图、可信身份和活动租约。 runtime 登记调用意图;根仍由 Host 的 tool_root() 决定,客户端不提供可信物理 ID,登记凭据也不授予访问权限。
    2. L1 不以路径字符串不同作为并发依据。 同批较早调用身份未知且可能冲突时,后继保守等待;前驱失败或拒绝不级联失败。源序依赖与排队公平性一起检查,避免形成等待环。
    3. L2 不只使用“根 realpath + 相对路径”作为键。 相同物理对象跨根仍须冲突;缺失目标、命名空间变化和不支持的文件系统先保守互斥。执行前重验绑定,租约由实际任务持有,不能随 RPC 提前返回而释放。

    后续计划

    每会话写额度继续保持 1。提高写额度、细化委派屏障、远程端完整协议和预算公平性扩展仍独立评审;旧 Host 和远程工作区保持含写整批串行。第 4、5 步的统一后台作业、只读 Bash 与 MCP 并行也不纳入此次修改。

    验收需要覆盖不同名称的符号链接/硬链接正反序、跨根与跨会话别名、审批/RPC 反向到达、源序与公平性等待环、取消实际 blocking 任务、review 回滚、改参/重放及远程退化。

    目前 Windows 硬链接、父子代理真实 Host E2E 和相关 Native 定向测试已通过。文件符号链接创建受本机权限限制;Linux/macOS 和 Electron UI 尚未验证。Host 全量检查仍有失败,其中两项已在未修改的 upstream 上复现,详细结果放在 #1982 中,不将其记为全量通过。原排程图继续只用于说明执行形状,不作为修订后实现的墙钟提速承诺。

  7. caulif commented on Nov 23, 2026

    @caulif
    ContributorAuthor

    我在实际使用中发现,Agent 很少主动用 runInBackground 启动后台 Bash,遇到长命令时,经常要等命令结束才能继续,体验不及预期。

    我认为合理的设计是:普通 Bash 默认先等一个短窗口。命令在窗口内结束,就直接返回结果。窗口到了还在跑,就先返回已有输出和作业 ID,让进程继续运行。Agent 可以先做其他工作,需要结果时再用 TaskWait 取回。

    在 Codex 和 Claude Code 里都有相似的设计。Codex 用短等待窗口交还控制权,Claude Code 则在前台等待超时后自动转后台。我倾向于采用 Codex 的短等待方式。

  8. Tioit-Wang commented on Nov 24, 2026

    @Tioit-Wang
    Collaborator

    @caulif 想确认一下:#1908 按修订后的范围,现在是否已经完成?#2536 的实现是否已经可以进入最终评审?也请标一下哪些剩余项还计划做、哪些已延期或不再纳入,方便判断这个 Issue 是否可以收尾。

    我对照了 PR diff 和当前 main:#1982 已合并,完成了本地文件批次的冲突排序和 Host 文件租约;它没有覆盖整个提案。后来 #2093 已补上同一会话内对已确认独立文件的并行写入,#2115 已把后台 Bash 接入 TaskList / TaskWait / TaskStop,#2372 已补上可选的 notifyOnExit。

    正文中还有几项,在上述 PR 中未看到完整落地:

    • 通用工具规格表及 TS/Rust 一致性测试:main 里 TS 的 TOOL_EXECUTION_SPECS 与 Rust 的 ToolClass::from_name 仍分别维护。
    • RESOURCE_BUSY、远程工作区完整的文件身份协议,以及按 Agent 公平分配在途额度。远程工作区的含写批次仍走串行回退。
    • 细化共享工作区的写型委派屏障,以及只读 Bash / MCP 并行开关。现有的 waitForBatchWritableDelegations 仍保留;feat(tools): yield long-running Bash after the initial wait #2536 的短等待也不等同于只读 Bash 并行。

    #2536 对应的是后续评论提到的「普通 Bash 短等后交还控制权」,当前仍未合并,范围限于满足条件的本地父 Agent 调用。GitHub 显示无冲突、当前 head 的 CI 均通过,但这不等同于可以直接合并:按现行 R6.1,外部 feat PR 不属于可合入范围。也请维护者确认这项后续如何处理。

  9. Tioit-Wang commented on Nov 24, 2026

    @Tioit-Wang
    Collaborator

    感谢这个提案及分阶段实现。本阶段跟踪在 #2536 合入后收尾:符合条件的本地父 Agent Bash 最多先等 10 秒,仍在运行时返回作业引用,保留原进程和原超时。此前 #1982、#2093、#2115 和 #2372 已分别推进本地文件批次排序与 Host 租约、独立文件并行写、后台作业控制和可选完成续跑。

    关闭此 issue 不代表原提案全部实现。通用工具规格及 TS/Rust 一致性、远程文件租约与 RESOURCE_BUSY、按 Agent 的预算公平性、委派屏障细化、只读 Bash 与 MCP 并行均未在本次交付中完成;后续若推进,请分别跟踪。

  10. caulif commented on Nov 25, 2026

    @caulif
    ContributorAuthor

    感谢 @Tioit-Wang 核对,也感谢维护者在 #2536 合入后为本阶段收尾。我同意关闭 #1908。对照官方 main 的 eaabecf24,我把原提案各项的交付情况,以及我对剩余项的取舍补在这里,供以后单独讨论时参考。

    规划项 具体设计 当前情况
    只读工具并行、有序准备与结果交付 父、子 Agent 可并行执行独立的 Read、Glob、Grep。参数按模型给出的顺序准备,结果也按原顺序交付。 已完成。 #1596
    L0 · 通用工具规格表与 TS/Rust 一致性测试 统一描述每类工具的读写资源、屏障和取消行为,用测试检查 TS 与 Rust 的共同规则。 待完成。 #1982 TS 已有文件工具策略表,Rust 仍独立分类。但是为了后续可维护性,我认为仍需要建立统一的表和测试。
    L1 · 本地文件批内冲突排序 同批 Read a 在 Edit a 之前,就保证先读后改。Host 确认 b 是独立文件后,Read b 可与它们并行。 已完成。 #1982
    L1 · 批次与等待诊断 在本地记录哪些调用组成一批、为什么退回串行,以及 Host 排队的时间和原因。 已完成。 Host 已记录准入等待总时长和初始原因,我认为已经可以达到需求了。 #1982
    L2 · 本地文件身份与 Host 租约 Host 识别实际文件,协调跨 Agent、跨会话的冲突,同时授予资源和容量。实际磁盘操作结束后才释放租约,快照和回滚也纳入协调。 已完成。 #1982
    L2 · 替代旧路径锁 PathMutex 在 Host 能协调文件访问的路径上,由 Host 接管原先由 sidecar 承担的互斥。 已完成。 #1982
    L2 · 同一会话并行写独立文件 对持有效批次票据、且已确认独立的已有本地文件,允许同一会话最多同时执行两个写入。 已完成。 #2093
    L2 · RESOURCE_BUSY 错误码 区分“文件正被占用”和“执行名额已满”两类超时,资源冲突时可附持有者信息。 待完成。 “文件正被占用”和“执行名额已满”两类超时当前都可能返回 HOST_OVERLOADED。目前规划文件争用超时新增 RESOURCE_BUSY;队列已满和容量超时保留 HOST_OVERLOADED,通过结构化原因区分。仅对队列已满自动短重试,已排队超时的调用交回 Agent。
    L2 · 远程文件身份与租约 让远端确认不同路径是否指向同一文件,并在写入真正结束后释放租约,从而安全开放远程独立文件并行写。 待完成。 简单复用本机机制(锁只放在本机,由本机判断什么时候释放资源)无法应对断线情况,所以需要进行设计和修改。
    L3 · 统一后台 Bash 作业 显式后台启动后返回作业引用,模型可用 TaskList、TaskWait、TaskStop 查看、等待或停止,并取回退出码和输出尾部。 已完成。 #2115
    L3 · 后台完成通知与会话续跑 对选定的后台任务开启 notifyOnExit,任务完成后在原会话空闲时交付结果并续跑。 已完成。 #2372
    L3 后续追加 · 普通 Bash 短等待 符合条件的本地父 Agent 在批末调用普通 Bash 时,最多先等 10 秒。命令仍在运行就返回作业引用,保留原进程和原超时。 已完成。 #2536
    L4 · 细化共享工作区写型委派屏障 用批内写意图表达父读与子任务的依赖。父 Agent 同批先派子任务修改 b、再读 b 时,应等到子任务的写入结果可用。 待完成。 现有项目代码在大部分场景下的父子Agent之间的等待是符合预期的,基于Task 的 scope.write 参数,一种我认为可行的优化是在子Agent的写入范围被指定的时候,可以不阻塞父Agent对其他文件的写操作。也就是父 Agent 在同批写型委派之后读取单个文件时,只等待仍可能修改该文件的子任务;Host 能根据强制执行的写范围确认文件无关,就允许父 Agent 提前读取。
    L4 · 按 Agent 分配在途额度 在会话预算内给父 Agent 保底一个执行槽,避免子 Agent 占满名额后父 Agent 无法响应。 待完成。 当前每会话父子Agent共用 4 个执行槽。我认为可以保留总额 4,修改是给父 Agent 保底 1,子 Agent 合计最多 3,以兼顾会话间的资源隔离和父 Agent在委派期间的响应能力。
    第 5 步 · 只读 Bash 并行 识别能可靠确认只读的 Shell 命令,允许它与其他独立工具同时开始。 不再纳入。 之前没考虑清楚bash情况下,脚本、参数和重定向都可能产生写入,可靠判断的成本较高,没有必要为了这个增加复杂规则与设计。
    第 5 步 · MCP 服务器级并行开关 允许明确支持并发的 MCP 服务器开启并行,让独立查询同时执行。 不再纳入。 个人使用MCP比较少。且同一MCP服务可能兼有独立查询和需要按序执行的操作,可能会错判,这里我选择现有的保守设计。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions