Skip to content

产品模式:normal / codemode / shell 三种工具模式 #2135

Done

Description

@eric8810

现状:产品只有一种工作方式

  • 模型每做一次决策调用一个工具,所有工具在第一个请求之前就声明好。产品 profile 下是 23 个内置工具(MCP 另算):read write edit glob grep exec exec_stop wait recall skill branch settings ask_user goal_update events schedule wakeup loop image_understand agent plugin web_fetch codemode。
  • codemode 已经存在,但它和直接工具摆在一起;模型有直接调用就不会用它(本次会话就是例子:codemode 在场,实际调用全走 exec/read/write)。
  • 持久 shell(ShellSession,跨调用保留 cwd、环境变量、后台任务)只装配在 install_minimal_execution(defaults.rs#L231),产品 profile 用的是 install_local_execution,这个形态今天只有 tests/integration/tests/minimal_execution.rs 在用。
  • 也就是说:仓库里已经有「一段程序」和「一个 shell 会话」两种动作方式,产品只暴露了「一次调用」这一种。

为什么提:换一种动作方式,让模型动态编排

需求的目的不是减少工具声明,而是给模型另一种动作方式:在一次决策里完成「发现工具 → 调用 → 按结果分支/循环 → 汇总」,而不是每个中间结果都回到模型、再发一次请求。

  • codemode:一段程序。 模型在一次调用里写脚本,可以循环、并发、条件分支;child 结果不进模型上下文;工具通过 bridge 访问,所以普通工具声明不需要出现。工具声明不出现是「只面对一种动作方式」的性质,不是目的。
  • shell:一个 shell 会话。 模型用命令组合完成同样的编排,cwd、环境变量、后台任务跨调用保留;execute 是唯一工具。
  • normal:逐步调用。 每一步单独决策、单独审批;需要精确控制或只需要一步时仍然最好。

三种都保留,因为编排粒度是任务属性,不是用户偏好。互斥是每种模式成立的条件:同时给 codemode 和直接工具,模型会退化回直接调用(今天就是这样);同时给 execute 和文件工具,会出现两条写入路径(shell 重定向 vs write/edit)。

结果,而不是原因: 请求里的工具声明变少、模型往返变少、上下文更干净。今天这 23 个工具的描述文本约 1.2 万字节(branch 2.0 kB、exec 1.3 kB、codemode 0.93 kB、loop 0.85 kB),加 schema 约 2 万字节,每请求数千 token;这些在 codemode/shell 下不需要出现,但省下来只是这个模式的结果。

真实风险:模型适配度

这个模式能不能用,主要取决于模型习不习惯这种动作单位:

  • 写程序(codemode)。 模型得会先 tools.list / tools.describe、处理 ToolCallError、给自己的循环设边界、把结果汇总。习惯逐步调用的模型可能只写一次调用、猜参数、语法出错,那样比直接工具还慢。
  • 写管道(shell)。 模型得熟平台的 shell:引号转义、交互式与非交互式命令的区别、什么时候后台、什么时候等。一条等输入的命令会占住 shell。
  • 任务是否合适。 只需要改一处的任务用程序或 shell 没有收益;默认 normal 加可切换让代价小,但模式本身是对工作方式的赌注。

机制没法让模型变强;它能提供的是可见性和判断材料:child 调用都是 Timeline 事实,程序有硬边界(时间/内存/调用数/输出),失败返回部分结果。模型够不够好要用评估去量,这里不做成功率承诺。

各模式还要补的 feature(额外需求,可补充)

这些不属于本设计,属于各模式自己的后续需求:

  • codemode
    • 失败清单:程序失败时明确列出已执行的 child 调用及其 effects,而不只是「可能已经执行」。
    • 审批说明:用户批准的是「一个程序」,child 逐个批准,提示要说清楚,并把 child 调用边跑边显示。
    • 分组:v4 有 parent_call_id,TUI/Web 要把一个程序的 child 归到一个行下。
    • 程序内提问:child ask_user 要有可回答的界面。
    • 停止:停掉程序与它的 child,给出明确的终态。
    • 描述与示例:教模型什么时候 describe、怎么给循环设界、怎么汇报部分结果。
  • shell
    • 后台任务的 title(exec 有 title,execute 没有)。
    • 命令级审批措辞与输出分页;命令规则已经覆盖 exec/execute。
    • 用户可见的 shell 状态:当前目录、运行中的任务、一条正在等输入的命令。
    • 描述与示例:方言、后台、超时。
  • 两者
    • 环境块按模式生成:Windows 上现在会带 read/edit/write 的编码说明,在 shell 模式下是噪音。

需求

每个模式 = 入口工具 + 程序消费不了的结果的工具。skill 给的是给模型的指令,程序消费不了;shell 没有程序,所以另外声明 ask_user、recall。

模式 声明给模型 程序可调用 隐藏
normal(默认) 除 codemode、execute 外的全部 — codemode、execute
codemode codemode、skill 除 codemode 外的全部 —
shell execute、ask_user、recall — 其余全部
  • codemode 不丢能力:程序能调除 codemode 外的全部。ask_user、recall 是编排输入,程序可以加工、可以连续问用户,所以是“调用”不是“声明”。
  • shell 没有程序,所以 ask_user、recall 必须声明;skill、图片、web、MCP、线/agent、定时、settings/plugin 不在这个模式里。
  • 入口工具只在自己的模式声明;新工具默认:normal 声明、codemode 程序可调、shell 隐藏。

粒度:谁选、何时能换

模式是 Session 的属性,归用户所有。它是能力选择(shell 用一条 shell 取代工作区工具,codemode 把它们藏到程序后面),所以不能由模型或别的组件在用户背后改。

场景 规则 理由
新建 Session 请求可以指定模式;不指定就是 normal;客户端记住上次选择并在创建时带上 不必每个会话重选,也不需要额外的「默认模式」设置
运行中的 Session 用户随时可以切换;从下一个被应用的输入生效,已开始或已应用的 turn 保持它 pin 住的模式;进程内预算恢复复用 pin,进程重启按现有 rebuild 结算、不重发 否则换工作方式等于放弃当前对话
分支(line) 没有自己的模式,同一 Session 的所有线都用 Session 的模式 一条线是同一个 agent;线用别的模式会拿到用户排除掉的工具
subagent Session 创建时继承创建者的当前模式;用户之后可以像普通 Session 一样切换,模型不能 同一个 agent 的委派工作;是否不同由用户决定
模型 从不选择或切换模式 这是用户的能力选择

切换不重写历史:模型在下一次请求里看到新的声明。已经运行的 Code Mode 程序或 shell 命令用它被批准时 pin 住的工具集继续跑完。

设计要点

  • 模式进 Session 配置:SessionConfig.toolMode(缺省 normal,旧记录缺字段即 normal),由 session.create 指定;没有 daemon 级的「默认模式」设置,客户端记住自己上次的选择并在创建时带上。
  • 模式决定的是动作单位,工具面是它的结果:分支 adopt/pin 的仍是 callable 集合(与模式无关);每个输入在被应用时 pin 住当时的模式,得到两套白名单:visible(顶层模型调用,即上表“声明给模型”)和 delegated(Code Mode child 调用;只有 codemode 非空,= 除 codemode 外的全部)。顶层调用只能落在 visible,child 调用只能落在 delegated;两者都继续各自过 Permission / Approval,且白名单检查在 handler 的 claim 之前,被拒绝的调用不登记等待者、不产生副作用。只声明 visible,上下文用量与压缩也按 visible 计。隐藏的工作工具不能靠顶层直接点名绕过,tools.list 与 child 白名单一致。
  • 运行中切换:ToolRegistry 持有当前模式(配置初始化),session.tool_mode.get/update 读写它;update 同时更新存储记录(与 session.language.update 同路)。get 返回「选择的模式 + 生效的模式」:入口工具不可用时按 normal 生效,这就是 fallback,不拒绝保存。模型没有切换工具,只有客户端方法。
  • codemode 模式把除 codemode 外的全部工具交给程序的 tools.list / tools.call(含工作工具、MCP 与其他来源),模型只看到 codemode + skill;嵌套调用仍复用请求 pin 住的 handler,不需要第二套查找。
  • 返回值带内容块:程序返回 child 结果(或它的 content)时,宿主把它发出去过的块原样重建——image block 必须与某个 child 结果返回过的块相等,程序不能指向没读过的像素;文本块变文本,其他一律 JSON 文本;仍受输出上限约束。这样 read 图片在 codemode 里能进模型上下文,消息拼装不用第二条路径。
  • 异步生命周期:交给 owner 的工作(后台 exec/execute、events 订阅、schedule/wakeup/loop、线、成员)不绑在 child 调用或程序上;程序取消不取消它们,完成按现有方式唤醒;交出前被取消则什么都不做。
  • shell 模式没有 goal_update:Goal 在内部 tool-mode transition 服务注册 settle handler;切到生效 shell 时,update 或 registry 注册切换在 mode gate 内、写记录之前调它,Goal 写 paused(reason=mode)、Append pause 文本,并撤回还没被 InputQueue 消费的 trigger/continuation;已 applied 的输入保持应用时的模式;pin shell 的 turn 在第一条请求前等待 pause 被 adopted,pause 文本在 publish 前以 Append 提交;已开始的 turn 保持旧 pin 跑完;应用输入时读同一把 gate,注册切换持 gate 到 publish,不会在 settle 前应用输入;shell 生效期间新 continuation 直接暂停,session.goal.set/resume 拒绝,pause/edit/clear/get 仍可用;离开 shell 不自动恢复,由用户显式 resume。Goal 自己的设计文档负责这个生命周期。
  • shell 模式需要把持久 shell 装进产品装配:execute 以普通注册工具的身份进入 Session(不再占用 Session 唯一的 ToolHost),否则同一进程无法同时服务 normal 与 shell Session。WorkspaceTools 保持装配,session.files 等客户端方法在各模式都可用。只装 execute 的 minimal-execution profile 按 shell 模式创建和恢复 Session(它的 host 负责,缺字段的旧记录也当 shell),否则 normal 会把它唯一的工具过滤掉;它还给 ToolHost 绑一个空的 driver,因为 ShellTools 不再提供。
  • 停止:session.exec.stop 现在只找 BackgroundExec(原生进程);持久 shell 的活任务归 ShellSession(提供 dyn ExecStop)。先解析活任务的所有者(ExecStop,再 BackgroundExec),两者都不认的 id 才用共享历史取终态;共享历史不能回答一个活任务。两部分部件在切换模式后仍挂载,所以切到 normal 后仍能停掉之前起的 shell 任务。session.stop 仍是停轮次,不停后台任务。
  • 契约更新随实现由各自 owner 落地:SessionConfig.toolMode 进 session access,live mode 与投影进 ToolRegistry,声明集合与准入拆分进 CodeMode,shell 生命周期进 Goal,环境块进 PromptSources。
  • fallback 由 ToolRegistry 判定:模式的入口工具不在 callable 集合里就按 normal 投影;v3 客户端的 excluded_tools 因此继续生效。

详细设计:docs/design/agent-tool-modes.md。

范围与非目标

不做:按分支(line)单独选模式;由模型选择或切换模式;每个模式自定义工具清单;Agent profile 指定模式。

不改:Permission / Approval、Timeline、Dispatcher、装配与插件管理。

验收

  • 三种模式各建一个 Session(请求指定;不带则 normal)并断言声明:normal 声明除 codemode/execute 外的全部;codemode 声明 codemode/ask_user/recall;shell 声明 execute/ask_user/recall。新注册的工具(含 MCP)在 normal 声明、codemode 程序可调、shell 不出现。恢复旧记录得到 normal;fork 保留当时的模式。
  • 运行中切换:切换后应用的输入采用新模式;同一 turn 里工具结果之后的请求保持这个 turn 的 pin;进程内预算恢复保持原来的 pin,进程重启按现有 rebuild 结算、不重发;已准入的调用跑完。切换前创建的 line 在下一个被应用的输入采用新模式。
  • codemode 模式下:顶层直接点名 read、ask_user、execute 被当作 unknown tool 拒绝,即使它为 child 绑定了;child 调用 codemode 也被拒绝。程序内调用 read 和 ask_user 都成功,并写入 child step/tool-call / step/tool-result。
  • 程序返回 read 的图片结果:外层结果带同一个 image block,模型请求把它当图片渲染而不是 JSON;伪造的 image block 退回文本。
  • shell 模式只有 execute 和 ask_user/recall,顶层点 read 被拒绝;normal 没有 codemode、execute。
  • 入口工具不可用时:get 报「选择 codemode,实际 normal」;工具重新出现后按选择生效。
  • 停止:shell 任务在切到 normal 后仍能用 session.exec.stop 停掉并写 exec/ended;路由先问活的 ExecStop,再问 BackgroundExec,历史不回答活任务。minimal-execution profile 的 Session 以 shell 模式创建/恢复并保留 execute。
  • 目标:切到生效 shell 时先暂停(reason=mode)并撤回未应用的 continuation;已应用的按旧模式跑完;shell 生效期间 set/resume 拒绝;离开后显式 resume 继续。
  • 程序启动后台 exec 后失败/被停:任务继续跑,完成时唤醒 Session,session.exec.stop 能停掉它。
  • child ask_user 显示在程序行下、可通过 session.question.respond 回答;答案驱动程序,不重复进模型上下文(除非程序返回)。
  • subagent:在 codemode 下创建的成员记录 codemode;之后切主 agent 不改成员。
  • web 与 TUI 都能切换当前 Session,并记住选择用于下一个 Session;不可用的模式在选择器里禁用。

关联

Activity

  1. added
    feature
    design
    on Nov 24, 2026
  2. self-assigned this
    on Nov 24, 2026
  3. 127 remaining items

  4. eric8810 commented on Nov 26, 2026

    @eric8810
    CollaboratorAuthor

    Product direction confirmed by the maintainer while reviewing #2197 (the implementation):

    • shell keeps goal_update beside execute, ask_user and recall. A mode switch does not touch Goal state; the ToolModeTransition settle, the Goal pause/withdrawal/refusal and the registration-settle wait are removed.
    • The tool mode is Session configuration read live when a request is prepared; it is not a Timeline fact or a per-turn pin. A switch applies to the next request prepared after it, a request already being prepared keeps the mode it read, and a turn carries no stored mode.
    • /usage prices the tools the last model request declared, falling back to the live projection before the first request.
    • The update gate serializes the record write and the live replacement.

    The design pages (docs/design/agent-tool-modes.md and the plugin pages) and the #2197 body are updated to this; #2197 is green and mergeable.

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

Metadata

Metadata

Assignees

Labels

designfeature

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions