Bash tool 的 shell 选择会同时影响三类行为:
# Environment 里展示哪个 Shell。过去这些信息分别从环境变量、执行 adapter、settings/runtime config 中读取,容易出现“模型看到的 shell”和“Bash 实际执行的 shell”不一致。这个文档定义统一后的运行时契约。
运行时维护一个 runtime 级别的 ExecutionShellSelection(config.bashShellSelection),它是 Bash tool 的有效 shell。selection 不落库:新 session 和 cold resume 都读取用户当前的 shell 设置,和其他环境字段一样,变化通过 environment 公告告诉模型。
apps/zcode-cli/packages/core/src/runtime/methods/session-shell-environment.ts 是 session shell environment 的唯一 owner,负责初始化、读取和首发前的 EnvInfo patch。session/requestRuntimePreferences 的 user-execution scope 向所属 Host 读取一次当前 shell 候选值,再由 initializeSessionShellEnvironmentIfNeeded() 初始化。该请求最多等待 15 秒;超时按协议错误阻断首次用户执行,不重试或静默回退到其他 shell。# Environment 公告的 Shell 字段(display.name)、Explore 子任务的 prompt 环境和 child runtime 都使用同一个 selection。session/send、V4 sendText、create/resume/subscribe 都不携带 shell 候选值;Host 读取只发生在 Agent 的统一用户执行边界和 resume 入口,不能让 desktop/mobile 或各 command 入口分别维护分支。session/create / session/resume 不接受 shell 候选值,避免调用方误以为可以在创建或恢复阶段覆盖 session shell。runtime/bash_shell_selection session entry 不再读取,也不迁移。.sh SnapshotsBash tool 运行时有三种名字相近但职责不同的 snapshot:
session shell selection
apps/zcode-cli/packages/core/src/runtime/methods/session-shell-environment.ts 管理。# Environment Shell 字段、Explore 继承的 shell display。.sh 文件,也不落库。shell init snapshot
apps/zcode-cli/packages/adapters/src/exec/shell-init-snapshot.ts 管理。shell-snapshots/snapshot-${shell}-${timestamp}-${random}.sh。winpty aliases。-c -l <command> 变为 -c <command>。-c -l <command>。-l。NodeExecutionAdapter.close() 时尝试 unlink;异常残留会由 retention cleanup 清理。shell-snapshots/ 顶层 mtime 超过 30 天的 .sh 文件。ZCode startup script snapshot
apps/zcode-cli/packages/adapters/src/exec/bash-startup-script.ts 根据 execution request 的 bashPrelude 生成并物化;不再有 core 侧 bash-startup.ts descriptor。rg fallback 始终保留;find / grep 是否映射到 bfs / ugrep,由根 Session runtime 物化时固定的增强搜索启动偏好决定,默认启用。执行顺序是:先 source shell init snapshot,再 source ZCode startup script,最后执行模型命令和 cwd capture trailer。snapshot 文件 path 不作为 session state 持久化;cold resume 后由 adapter 基于当前 shell selection lazy 创建新的随机 snapshot。旧文件不能直接作为 ready snapshot 被恢复。
ExecutionShellSelection 是跨 contracts、adapters、core、bootstrap 的唯一 shell selection 数据结构。
dialect:执行包装语义,当前为 posix、git-bash、cmd 或 legacy-shell。source:selection 来源,当前为 auto-detected、user-config 或 legacy-fallback。path:已解析出的可执行路径。legacy fallback 可以没有路径。display.name:provider-visible 的稳定名称,例如 zsh、bash、Git Bash、CMD、system shell。这里禁止展示绝对路径。id / label:设置项或诊断展示用元数据,不作为 provider-visible contract。Runtime config 只以 bashShellSelection 作为 source of truth。底层 execution adapter 的 command 仍使用既有 shellOverride 字段承载这个 selection,但 runtime / tool context 不再维护第二份 bashShellOverride 状态。
macOS / Linux:
zsh / bash。$SHELL 本身是可执行的 zsh 或 bash,可以直接使用。fish 等非 Bash-compatible shell,不把 fish 作为 Bash tool shell;继续从 PATH 和固定路径中找 zsh / bash。zsh / bash 时,返回 legacy-fallback,保留旧的 generic shell fallback 行为。Windows:
legacy-fallback,保留旧的 cmd / ComSpec fallback 行为。模型只通过 environment 公告知道当前 shell,没有单独的 shell 提醒。公告规则见 apps/zcode-cli/docs/design/v2/loop/meta-user-context.md;与 shell 相关的部分:
display.name 与历史里最近一次环境公告的 Shell。# Environment,Shell 是当前 selection。# Environment update 的 - Shell: <new> (was <old>) 一行;最近一次公告的 Shell 为空(旧 Windows 会话)时不写 (was …)。task_notification / subagent_reply),首个请求不收集输入附件,shell 变化要到工具结果之后的下一个请求才宣告。这一个请求内模型可能仍按旧 shell 写命令,Bash 已用新 shell 执行。cwd 等环境字段同样如此,不为 shell 单独开例外。apps/zcode-cli/packages/bootstrap/src/app/create-app.ts 在统一用户执行边界和 resume 入口 lazy 请求一次 Host 候选值,并在 app 生命周期内复用该结果。Host 候选无效或未显式选择时,再由 CLI 运行环境的 resolveEffectiveBashShellSelection() 自动解析。runtime 已有 selection 时忽略候选值。
deferred draft 预热不冻结 shell;首次真实用户执行前才读取。desktop continuous、web-remote replayable、legacy/V4 prompt、goal external activity、expert workflow 和 script workflow 都必须经过同一 app/runtime admission。desktop-attached remote 通过 workspace-scoped Agent service 请求回到 desktop shared Host 的本地 SettingService;手机 /remote 复用该 Host。relay/main 不保存 shell 设置,远端 ~/.zcode/v2 也不作为 desktop-attached workspace 的权威源。
旧 Host 返回 -32601 或 Agent 尚未连接 Host 返回 -32020 时,保持 CLI 自动解析;其他协议、resolver 或传输错误原样失败。cold resume 也走这条规则:这两种情况下用户选择的 shell(如 CMD)不会生效,改用自动解析结果,变化同样经环境公告告诉模型。这里不新增 per-send pull、设置广播、缓存同步或 Session 级 setting snapshot。
Explore 子任务继承父 runtime 的有效 shell selection:
EnvInfo.shell 使用父 selection 的 display.name。这样 Explore 里模型看到的 Shell 和真实 Bash 执行路径保持一致。