更新日期:2026-07-15
本文最初是 Bash 执行形态的实现计划。实现已经完成并继续扩展到 Windows、session shell snapshot、 startup source 和 embedded search prelude;下面记录当前事实,不再是待执行 plan。
Bash tool 创建:
command: {
mode: "shell",
command: input.command,
shellProfile: "posix-bash",
}
shellProfile 是 execution port 的内部 profile:只有 Bash tool 设置,generic shell、hook 和配置 runner
不得继承。adapter 对这个 profile 解析真实 shell 并尽量用 shell: false 直接 spawn;无法解析时显式返回
legacy-shell selection,并降级到旧系统 shell。
session persisted selection(可用)
-> user-config selection(仅新 session)
-> platform auto-detect
-> legacy system shell fallback
POSIX:
$SHELL 是 bash/zsh 且可执行时优先;/bin、/usr/bin、/usr/local/bin、/opt/homebrew/bin
查找 bash/zsh;SHELL=<resolved path>、GIT_EDITOR=true,并直接执行该 binary。Windows:
git.exe 位置反推;dialect=git-bash、shell=false;CMD 使用 dialect=cmd 和 cmd shell;provider 只注入上面列出的通用变量和 ZCode 自己命名的变量,不引入其他产品命名的环境变量。
shell 是 session-start 配置,而不是每个 Bash call 重新读取的全局设置:
bashShellSelection;deferred draft 可在首发前刷新 Environment context;runtime:bash_shell_selection;shell_environment_change,避免模型继续按旧 shell 语法工作。workspace path/identity 不参与 shell identity;实际 executable 可用性由 Agent 运行环境判断。远程 session
因此使用远程机器的 shell,而不是 desktop 本机 $SHELL。
bash-startup-script.ts 只对 posix-bash profile 生效:
bash-startup/ 目录物化 0600 source script;. <quoted path>;这条链路用于把 bundled bfs/ugrep 等 search prelude 放进实际 Bash 环境,不改变 model-visible command, 也不把 generic execution 变成 Bash。
foreground Bash 的 captureCwdAfterSuccess 继续由 execution adapter 的 cwd capture plan 实现:
prepare command
-> apply startup sources
-> wrap cwd capture
-> resolve shell provider
-> spawn
-> exit 0 时读取 resolvedCwd
-> runtime.setWorkingDirectory
background Bash 在 start 时清掉 cwd capture,不更新 session cwd。shell provider 不能建立第二套 cwd file 或 把 cwd persistence 搬到 core handler。
packages/contracts/src/interfaces/execution.port.tspackages/core/src/tool/handlers/bash.tspackages/core/src/runtime/methods/session-shell-environment.tspackages/core/src/runtime/methods/bash-shell-snapshot.tspackages/adapters/src/exec/bash-shell-provider.tspackages/adapters/src/exec/bash-startup-script.tspackages/adapters/src/exec/cwd-capture.tspackages/adapters/tests/exec.test.tspackages/adapters/tests/bash-startup-script.test.tspackages/core/tests/bash-handler.test.ts测试覆盖 POSIX bash/zsh 语法、Windows Git Bash/CMD、固定目录和 PATH fallback、legacy fallback、env overlay、cwd capture、background behavior、startup source、snapshot restore/stale fallback,以及 generic shell 不读取 Bash profile。
apps/zcode-cli/docs/design/v2/tool/04-bash.md 为准;本文件只描述
selection/execution architecture。