Repository navigation
Todo
Description
总 issue:#2015。原则和扫描方法写在总 issue 的正文里,交付计划在这条评论里。
起因
#2002 已经把进程任务收归 HostPlatform:任务只记在一张表里,对外的操作都是带 task id 的数据面。剩下的部分不符合 #2015 的访问层规则:
- 还有对外但不在面上的方法,一部分直接返回 Rust 进程对象;
- 停止任务有两个入口;
- ShellSession 和 HostPlatform 都在发布任务的终结结果,也都在结算。
本 issue 在 #2002 合并后,把这些一次清理干净。扫描已在 #2002 的 head 3ddc826e7 上完成,结论直接写在下面的「已定决定」评论里,不再单独发扫描报告。
范围
- HostPlatform:
hostPlatform(主机事实、命令任务、交互式 shell、隔离、输出文件) - ShellSession:minimal profile 的持久 shell。它不在
generated/的两份快照里,所以以前没有分到任何子 issue,现在归这里。 - ToolchainSource:
toolchainSourceHost - ExecShellSettings、ExecutionBackend
- ExecutionSettings 和
executionSettingsHost仍然归 所有者扫描:Settings 与设置分节 #2017。
起点
- HostPlatform:
crates/dim-agent-capabilities/src/host_platform.rs、host_platform/ - ShellSession 与停止:
shell_session.rs、exec_stop.rs - 工具链:
toolchain.rs - 执行设置:
crates/dim-app-server/src/exec_shell_settings.rs、execution_settings.rs - 插件页面:
HostPlatform.md
相关
#2002 · #1295 · #1899 · #2028(BackgroundExec)· #2026(WorkspaceTools、ShellInput)
完成条件
按 #2015 的交付计划交付代码:
- 依据:插件与服务治理:所有者提供操作,逐个扫描所有者 #2015 的交付计划、§7、ADR 027、本 issue 的「已定决定」评论,以及代码。本 issue 下更早的两条评论来自已关闭的契约 PR,已经作废。
- PR 的内容:下表每一行是一个 PR。PR 让本板块的所有者符合 §7;它取代的旧入口在同一个 PR 里删除,旧入口的全部调用方也在同一个 PR 里迁移,不管调用方在哪个板块。
- 开 PR:
- PR 正文写
Refs #2032。 - 第一次提交就开 draft,做完、检查都通过后转 ready。
- 开 PR 后把链接填进下表。
- PR 正文写
- 设计问题:遇到 §7、ADR 027 和已定决定都回答不了的设计问题,在本 issue 里提问,对应的 PR 保持 draft。
板块 PR
| PR | 分支 | 所有者 | 代码 PR |
|---|---|---|---|
| A 停止与 shell 任务 | dim/2032-exec-stop |
ShellSession(BackgroundExec 作为调用方一起迁移) | #2206 |
| B 进程所有者 | dim/2032-host-platform |
HostPlatform、ToolchainSource、ExecShellSettings、ExecutionBackend | 待开 |
Activity
eric8810 commentedon Nov 23, 2026
来自 #2028 的目标契约(#2100):BackgroundExec 不再接收活的进程对象,改为按 task id 接管一条已启动的命令。需要 HostPlatform 的契约(#2032)登记:
spawn(...)的回复带{taskId},进程归 HostPlatform 的任务表;wait({taskId}, cancelled)→{status, exitStatus};调用方丢弃调用只停止本次等待,不影响任务;kill({taskId})→{},终止进程树;settle({taskId})→ 终结结算,所有者关闭或重载时按任务表结算,task id 在代次之间不复用。BackgroundExec 的
adopt与stop依赖这四项;output、output_page、wait只读已登记的输出文件与终结事实,不依赖它们。契约见 BackgroundExec.md。eric8810 commentedon Nov 23, 2026
来自 #2022 目标契约(#2101,head
42f5fa01c):Hooks、Skills、PluginManager 和 MCP server 子行依赖命令执行(stdin、超时、进程树取消)、home 值和 source fetch;HostPlatform 还在等 #2002,没有目标契约,#2022 只记录依赖意图,接口待 #2032 的契约定义。见 Hooks.md、Skills.md、PluginManager.md、Mcp.md。docs(design): link the sibling contracts from the tool chain contracts
eric8810 commentedon Nov 25, 2026
已定决定
访问层规则见 #2015。基线是 #2002 的 head
3ddc826e7;#2002 合并后,开发者用合并后的 main 再核对一次文件位置。本 issue 下更早的两条评论来自已关闭的契约 PR,已经作废。最终形状
flowchart TB subgraph P[进程层] HP["HostPlatform:唯一的进程所有者<br/>info · spawn · spawnWithStdin · spawnConfined<br/>shell · write · resize · read<br/>wait · output · tail · retainOutput · kill · settle"] end subgraph L["一条线(timelineContext 上的 Capability)"] BE["BackgroundExec:唯一的后台任务所有者"] SS["ShellSession:shell 和前台命令队列"] WT["WorkspaceTools exec"] end O["FileAccess · ShellInput · Hooks · Modality OCR · MCP stdio · 存储"] WT -->|spawn → taskId| HP SS -->|shell / write 起命令 → taskId| HP O --> HP WT -->|转后台:start hostTaskId| BE SS -->|转后台:start hostTaskId| BE BE -->|kill / wait / output| HP C["session.exec.stop"] --> BE一共三层,每层只有一个所有者:
A:停止与 shell 任务(
dim/2032-exec-stop)BackgroundExec.stop。recovery: Key<BackgroundExec>),所以这样做两个 profile 都能用。dyn ExecStop、exec_stop.rs,以及 Serve hostPlatform's data methods; withdraw the execHost face #2002 新增的Session::service_opt、Branch::service_opt。session.exec.stop只调BackgroundExec.stop,错误映射只保留一套,读取失败统一返回exec_task_read。BackgroundExec.start({hostTaskId, ...})登记。BackgroundTask、Settlement,以及合成reset_result的逻辑。wait等结果,但不发布结果。exec/ended。shell_events、job_marker的使用,以及自己保留的输出缓冲。tail。settle。B:进程所有者(
dim/2032-host-platform)spawn_tree、spawn_with_stdin_process、spawn_pty、build_process_spec、child_path_process、prepare_isolated_process、installed_toolchains、shell_events、job_marker;with_execution_settings、with_toolchain_source只在插件装配时使用。info()读。platform.home、platform.os、platform.timezone。它们是 PromptSources、Permission、ShellTools、WorkspaceTools、Hooks、Skills、skills_management、FileAccess 和存储测试。info(),字段改成私有。spawn:起一个要跟踪的任务,返回 id。spawnWithStdin:跑到结束,直接返回结果。spawnConfined:按明确的读写授权,在沙箱里跑辅助程序。spawnWithStdin,请求加两项:command增加「程序 + 参数」的写法,和 shell 字符串二选一;GIT_开头的变量。spawn,行为不变。spawn增加confine参数,默认按执行设置决定,ShellInput 传none。用户手打的命令现在就不进沙箱,改完以后也不进。tail。spawnWithStdin现在就已经在面上,而且不进沙箱。spawn支持stdin: pipe,写入用现有的write。shell_events改成公开的read({taskId, cursor, waitMs, limit}):无损读取,带等待。workspace_tools/exec.rs里的impl HostPlatform搬回 HostPlatform 的模块:start_command、command_task、retain_output_by_id、tail_by_id。OutputPage、CaptureError、regularize_output也搬回 HostPlatform。retained_output_page并进output:任务结算之后,也能读保留下来的文件。workspace_tools、shell_session的任何模块。HostDispatch和json!改成#[rutis::service]。顺序
Session上的转发方法,service_opt也在其中,而它探测的dyn ExecStop不是数据面,没法改成按 id 路由。workspace_tools/exec.rs、background.rs里调用 HostPlatform 的代码。fix(exec): confirm a shell command's stop at the owner
eric8810 commentedon Nov 26, 2026
A 实现记录(#2206,head
4037ca759)。停止入口与 owner 侧确认
BackgroundExec.stop;session.exec.stop只调它,错误映射一套。HostPlatform.kill对 shell 命令自己确认:Ctrl+C → 最多等 2s terminal。实际结果取决于 shell:有的 shell 中断后仍打印结束标记,终态就是被中断那一行的$?(sleep 60 || true会是completed);有的会打断整条包装行。2s 后仍在跑的命令,先对终端的前台进程组(命令所在组,不是 shell 自己的组)发 SIGKILL 并等它消失,再终止 shell,由 sweep 记为 reset。停止有界,记录在 terminal 存在后才 settle。原ShellSession::interrupt里的同一套确认仍保留在它自己的取消路径上。macOS 没有/proc树可走,前台组信号是让命令真正消失的那一步。终止 shell 本身(command stop、shell kill、Session close)在PtyProcessTree::terminate/Drop里也释放前台组并等待,所以session.close()不会留下前台命令。stop_an_occupying_command_returns_and_the_shell_keeps_working(minimal_execution):停一条前台超时转后台的命令,断言停止返回 failed 终态、下一条命令能跑;这两个断言对上述两种 shell 行为都成立。session.exec.stop处理器对这条路径只有一次BackgroundExec.stop调用,测试直接跑这个操作;minimal profile 没有 transport 级用例。允许名单
base-1(#2174)合并后已 rebase 到
origin/main(508d69911),删掉了xtask/access-layer.json里的session-forwarding crates/dim-agent-runtime/src/agent.rs Session::service_opt;xtask check通过。没有execStop条目,也没有shell_session.rs/ShellSession条目。A.4 的措辞
A.4 写「需要 shell 输出时,读 shell 任务的
tail」。实现里 ShellSession 不再读 shell 任务的输出;占用的前台命令按 id 用hostPlatform.output带 cursor 增量读,wait走 owner 的 terminal 记录。两者都是 owner 读,不改变 A.4 的结论,这里按实现把tail读作「按 id 读 owner 记录」。停止终态
两类 shell 任务不一样:
runInBackground的后台 job 是TaskKind::Job,停止后终态是cancelled,不发完成通知。TaskKind::ShellCommand,停止是发中断;终态是failed(被中断命令的退出状态,或 owner 终止 shell 时的 reset),因此会发完成通知。原生进程命令停止同样是
cancelled、不唤醒。plugins/WorkspaceTools.md已按这个区分改写。已终态 shell 的窗口
ensure_shell现在检查自己持有的 shell 在 owner 表里的记录,记录已终态时才换新 shell。worker 先让出 PTY、再等EXIT_STDIO_GRACE(100ms)才写 terminal,这一段窗口是 #2002 的既有行为:期间到达的命令仍会从write得到Shell is not running并形成错误结果。本板块把窗口收窄,不负责关闭它。首次命令初始化:宽容重写
CI 上
new_persistent_shells_and_environment_refresh_use_current_preferences的失败与停止路径无关。alternate是 zsh、且 runner 的 HOME 没有启动文件时,zsh 的 newuser 模块在启动时运行向导,把 init 包装行当成菜单答案,end marker 永远不打印;fresh HOME 下可确定复现(10/10 失败)。处理:
send_initial不再依赖哪个 home;它对每份 init 副本用独立的 marker 对,写到 shell 直到某一副本自己的 start marker 先出现、随后它自己的 end marker 也出现,最多 10s。旧副本的 end marker 不能满足新副本;顺序由init_complete判定(end 必须在 start 之后)。每一份写出的副本都保留 marker 到 deadline,任意一份满足「自己的 start 先于自己的 end」即完成;已经开始的副本不会被替换。比 retry 间隔慢的 shell(如preexec睡 1.5s)也能初始化。启动消费者读掉前缀、尾部printf只打印 end marker 的情况不算初始化;下一次重写会真正跑一遍。start marker 已经出现就不再重写;重写只会多跑一次幂等的 init。超时时报错带上 shell 的 tail。fresh HOME 下该用例 3/3 通过。强制消费者用例写在 shell 的 HOME(harness 的 cwd):.zshrc逐字符消费到最后一个printf前的边界并返回,让尾部printf单独打印 end marker、init 不运行;.zshrc先设HISTFILE,断言的histfile=unset因此依赖 init 的unset HISTFILE。把「只看 end marker」的旧判断放回去该用例会失败,本 head 3/3 通过。代价:如果 shell 没有启动消费者但启动很慢(>1s 才读到第一行),会多写一次 init;init 幂等,只多一行输出。
shell 结束时 job 的释放
shell 的 worker 在写 shell terminal 之前,对每个 pid 已知、仍活着的 job 尝试释放:SIGKILL 进程组(能枚举时连同后代),最多等 2s;terminal sweep 保留 SIGKILL 兜底,不再只发一次 SIGTERM。忽略 SIGTERM 的 job 在它的 terminal 和
exec/ended出现之前就已经不存在;测试a_term_ignoring_job_is_gone_before_its_terminal在收到exec/ended的那一刻先断言进程不存活,再断言保持消失;旧实现上失败、新实现通过。未使用的terminate_job删除。限定:worker 忽略
kill_job的失败,所以「确认后再写终态」是最努力保证,不是严格保证;进程若逃过 SIGKILL,只剩 sweep 的 SIGKILL 兜底。测试覆盖 POSIX 的进程组//proc树路径;Windows 的进程树分支只有在 Windows 上跑这个用例时才覆盖,scripts/test-windows-native.mjs没有列出它,CI 不覆盖该分支。停止与后台 job 的文档
plugins/WorkspaceTools.md已按「后台 job 与原生命令停为cancelled不唤醒;shell 命令停为failed会唤醒」区分。交接与分离 job 的退出码
owner 在 shell 命令/后台 job 以已知非零退出码结束时记
Failed+exit_code,此前settled_from_reply只对Exited/Signaled取退出信息,交接后的命令和分离 job 的exec/ended因此没有exitCode。现在只要reply.exit_code/signal存在就带出,CommandFailed/UnknownExit的 summary 用exit_failure_summary;a_handed_off_command_keeps_its_exit_code、a_detached_job_keeps_its_exit_code钉住这两条,并断言summary/结果文本为Command exited with code 7.;分离 job 的用例还断言BackgroundExec.stop对已结束 job 的回复里是同一个exitCode和summary。未确认的停止(按 #2002 契约)
cancelled)或 shell sweep(failed{shell_exited}/shell_reset/owner_unloaded);kill的失败只作为kill的回复。BackgroundExec.stop用一个 per-task pending 标记:supervisor 在kill不是确定成功时置位,stop立即返回TaskError::Running;另有兜底上限,保证回复有界。record、cleanup、Session hold 留给 supervisor 继续等,之后只发布一次exec/ended。session.exec.stop映射为 RPC app errorexec_task_running;exec_stop返回失败结果、code = "exec_task_running",文本说明未确认。host_platform_flow::a_job_without_a_pid_stays_running_until_the_shell_sweep(2s 内task_running、record 仍 Running、sweep 写failed{shell_reset});正常 release 仍是cancelled(stop_background_task)。exec_task_running形状 Serve hostPlatform's data methods; withdraw the execHost face #2002 没有定义,已在 所有者扫描:HostPlatform 与执行设置 #2032 提问(issuecomment-7185944893),按上述填法实现;pending-stop 的端到端 fixture 缺口也已记录。已知项(不阻塞合入)
stopping_a_command_that_ignores_the_interrupt_leaves_no_process和closing_a_session_releases_an_occupying_command通过cfg!(target_os = "macos")被scripts/test-macos-native.mjs选中,macOS job 会构建integration_flow并运行它们;这是前台组信号的证明。Linux 上同一批用例在常规套件里跑,那里的/proc遍历也会覆盖命令。/bin/zsh(没有时跳过),在其他 shell 上没有等价用例;重写的另一条路径(shell 启动慢)只会多写一次幂等的 init。决定(#issuecomment-7195933285):未确认 Stop 的后续
TaskError::Running、exec_task_running、record 保持 Running、不写终态、hold 保留。crates/dim-agent-capabilities/src/workspace_tools/exec.rs:139):现在写 "its result will follow" 不对(cancelled不唤醒模型)。改为「停止已请求、命令仍在运行;之后再次调用exec_stop读结果;如果 shell 清理了它,会收到失败通知」,docs/architecture/rust-agent/plugins/WorkspaceTools.md:146同步。docs(exec): a stopped shell command and a shell job end differently
test(exec): report the selected shell when the preference assertion f…
fix(exec): show the shell's output when its initialization times out
fix(host-platform): keep zsh's newuser wizard out of the first command
fix(exec): tolerate a shell's startup consumer and confirm job release
docs(host-platform): state the job release that precedes a shell term…
22 remaining items
test(exec): make the startup-consumer case consume a prefix
fix(exec): release the foreground group on shell termination and orde…
fix(exec): keep the exit status of handed-off commands and detached jobs
chore(exec): regenerate the service declarations for the stop doc
test(exec): assert the exit summary of handed-off commands and jobs
fix(exec): let a started init copy finish
fix(exec): answer a pending stop when release is unconfirmed
fix(exec): watch every written init copy
style(exec): fit the init started check on one line
chore(exec): drop the service_opt access-layer entry
eric8810 commentedon Nov 26, 2026
已定决定:停止没被确认时 RPC 和工具层的形状
答复 7185944893。#2206 的填法保留,只改工具结果的一句话。
保留的形状
BackgroundExec::TaskError::Running;session.exec.stop回 RPC 错误exec_task_running;exec_stop回失败的工具结果,structuredContent.code = "exec_task_running";理由:和 owner 的
task_running是同一个意思,前缀和exec_task_unknown等一致。#2002 §4.1 说的「stop-requested result」在 RPC 层就是这个错误码。要改:工具结果的文字说错了后面会发生什么
workspace_tools/exec.rs:139)说「its result will follow」。cancelled结束,而cancelled不唤醒模型(background.rs:4),所以模型等不到结果。只有 shell 清理时写的failed会唤醒。content,所以文字要写明下一步:停止已经请求,命令还在运行;之后再调一次exec_stop读结果(已结束的任务会回它的结果);如果 shell 清理了它,会收到一条失败通知。WorkspaceTools.md:146的「its terminal follows」一起改成同样的说法。不加测试注入点
不为端到端测试给 BackgroundExec 加注入点。owner 侧已经由
host_platform_flow::a_job_without_a_pid_stays_running_until_the_shell_sweep覆盖;RPC 和工具层只是把TaskError::Running映射成错误码,为它加一个只给测试用的入口不值得。fix(exec): drop the internal mark of a service #2206 removed
docs(shell): list the dependencies the shell session still injects
fix(exec): restore the repository checks after arcships#2206