# Windows command shell

`host/_platform/command_shell.resolve_shell` decides which shell a command runs in, for an execution environment and the sandbox in force. The command tool, hooks, the environment block, permission rules, `doctor` and `/api/system/features` read its profile; nothing else probes for a shell.

A command runs sandboxed exactly when the profile says so, also for a caller that passes its own sandbox policy. The host part is cached per named bash path, so changing the setting takes effect on the next command; the sandbox is read on every call.

## Profiles

| Shell | When | Promises it does not keep |
| --- | --- | --- |
| bash, sh | macOS and Linux; `/bin/sh` only without bash | None |
| Git Bash | Windows, when [a bash is found](#finding-bash) | None |
| PowerShell 7 | Windows without bash, `pwsh.exe` in an absolute PATH folder | Recoverable delete; rule parsing is partial |
| Windows PowerShell 5.1 | Windows without either | Recoverable delete; UTF-8 file output is partial (`>` writes UTF-8 with a byte order mark); rule parsing is partial |
| bash in WSL | Windows with the sandbox in force and able to run | Recoverable delete; stopping reaches the tree only in part |
| Remote | An SSH host or a Docker container | Recoverable delete; stopping reaches the tree only in part |

The promises are recoverable delete, UTF-8 file output, the command's own exit code, stopping the whole process tree, and permission rules that can read the command. Each profile states all five as supported, partial or unsupported.

`doctor` prints the profile and runs `echo 你好; exit 3` through the command backend, sandbox included. The row is advice: OK when both come back, INFO with the reason when not, and it gates neither `openprogram upgrade` nor a self-update. It also names a sandbox that is on but cannot run, and a named bash that is not a file.

## Finding bash

1. The path the owner named: `OPENPROGRAM_GIT_BASH_PATH`, then the setting `shell.windows_bash_path`. A path that is not a file is passed over, never an error.
2. Git for Windows in Program Files or the user's programs.
3. The bash inside the Git the runtime ships, when the base's `base.json` names it as `gitBash`.
4. `bin\bash.exe`, then `usr\bin\bash.exe`, beside each `git.exe` on PATH: scoop, portable and custom installs.
5. A `bash` in an absolute PATH folder that is not the WSL launcher.
6. Otherwise PowerShell: `pwsh` 7, then Windows PowerShell 5.1.

The current directory and relative PATH entries are never searched, so a repository's own `bash.exe` never becomes the shell, and the answer does not depend on where the process started.

The Windows base build checks the unpacked dugite Git for `bin\bash.exe` and `usr\bin\bash.exe` and writes the one it finds, relative to `<base>\git`, or `null`. No extra Git or coreutils is downloaded.

**Why this design**

Peers split in two. Codex, OpenCode and Grok Build prefer PowerShell. Claude Code 2.1.321, Kimi Code, Pi and ZCode prefer bash. Oh My Pi embeds a bash engine written in Rust, which a Python runtime cannot reuse.

- Bash first, PowerShell second, as Claude Code does: only the bash shells keep all five promises. Codex prefers PowerShell because its rules and sandbox were written for it; ours were not.
- The bundled Git's bash costs no bytes when the archive has one. Downloading coreutils to make one would, and no peer does.
- Bash beside `git.exe` follows Kimi Code's order and its `usr\bin` candidate; Claude Code and OpenCode derive it the same way. The variable name follows Claude Code's `CLAUDE_CODE_GIT_BASH_PATH`.
- The PATH lookup stays: it reads only absolute folders and skips the WSL launcher, so removing it would only cost MSYS2 users a setting.
- A bad named path falls through instead of failing as in Pi, because a missing shell degrades and never halts.

## What runs in PowerShell

PowerShell source carries its own UTF-8 setup inside `try` and ends with `exit`, so in ConstrainedLanguage the setup fails quietly and the exit code still arrives. For 5.1 the setup also sets `$PSDefaultParameterValues['Out-File:Encoding']='utf8'`: `>` writes UTF-8 with a byte order mark, which read and grep decode.

What the environment block tells the model (`ShellProfile.syntax`)

| Shell | Rules |
| --- | --- |
| Windows PowerShell 5.1 | No `&&`, `\|\|`, `?:` or `??`: chain with `;` and test `if ($?)`. No `2>&1` on native programs. Write files with the edit tools or `Set-Content -Encoding utf8` |
| PowerShell 7 | Write PowerShell 7 |
| Both | Codex's three safety rules from `windows_shell_guidance`: quote paths with spaces, `-LiteralPath` for paths with brackets or stars, nothing deleted outside the working directory unasked |
| Git Bash | Bash syntax; quote Windows paths or use forward slashes |

The command tool's description is the same everywhere. A new shell changes only that line of the environment block, told once beside the next message ([Composition](https://fzkuji.github.io/OpenProgram/docs/reference/design/context/composition.html#l2)).

**Why this design**

- One command tool with the rules in the environment block follows Codex, which appends its guidance by platform. Claude Code splits Bash and PowerShell into two tools; here that would change the tool list whenever the shell changes and break the cached prompt prefix.
- The 5.1 rules are what 5.1 rejects with a ParserError, which needs no measurement.
- The Out-File default follows Claude Code's prelude. UTF-16 makes read and grep see NUL bytes; the byte order mark is the one fix that needs nothing from the model. Codex sets only the console encoding, so its `>` still writes UTF-16. `Set-Content` keeps its default, so one file never mixes two encodings.

## Delete shims under MSYS

The bash puts the delete shims first on PATH again after MSYS prepends `/usr/bin`. `cygpath` spells their folder for the bash it sits beside (`/c/x` for Git Bash and MSYS2, `/cygdrive/c/x` for Cygwin); a bash without it gets the MSYS spelling made in Python.

## Which shell runs a hook

| Hook entry | Windows | macOS and Linux |
| --- | --- | --- |
| `"shell": "bash"` (default) | The host's bash; without one, PowerShell, and the conversation is told once (`hook_shell_fallback`) | `/bin/sh` |
| `"shell": "powershell"` | PowerShell, beside Git Bash too | `pwsh` |
| `"args": [...]` | `[command, *args]` with no shell, so no quoting rule applies | `[command, *args]` with no shell, so no quoting rule applies |

The notice says how to fix it: install Git for Windows, declare the hook's shell, or use `command` with `args`. A hook that cannot run, or times out, still allows the action.

**Why this design**

| Compared | Does |
| --- | --- |
| Claude Code 2.1.321, followed | A `shell` field, a direct `command` plus `args` form, bash by default, PowerShell with one warning per session |
| Codex | `COMSPEC /C` with a `commandWindows` override, so an author writes every hook twice |
| Grok Build | Rewrites `$VAR` for PowerShell |

A declared language keeps `test -f .env && exit 2` from failing to parse in 5.1 and being allowed unnoticed. The notice is a line in the conversation, not a gate.

## Deny and ask rules for PowerShell

Where commands run in PowerShell, deny and ask rules also read the command lowered to POSIX words (`runtime/permissions/powershell_words.py`). Each pipeline element becomes an argv. Allow rules never read the lowered form: they match the command as written, and "Always allow" saves that exact text.

| Reads as | PowerShell |
| --- | --- |
| `rm` | `Remove-Item, ri, rm, del, erase, rd, rmdir` |
| `mv` | `Move-Item, mi, mv, move` |
| `cp` | `Copy-Item, cp, cpi, copy` |
| `-r`, `-f` | `-Recurse`, `-Force`, or any prefix PowerShell accepts |
| Operands | Positional words and the values of `-Path`, `-LiteralPath`, `-Destination` |

So `bash(rm -rf *)` stops `Remove-Item -Recurse -Force .`. A command the lowering cannot read for certain (variables, subexpressions, script blocks, arrays, redirections, `&`, dot-sourcing, comments, backticks, Unicode quotes and dashes) is asked about, not refused, when it names one of those cmdlets or runs a computed command, and only if a deny rule names `rm`, `mv` or `cp`.

**Why this design**

| Point | Reason |
| --- | --- |
| Codex, followed | Its in-process tree-sitter lowering accepts plain words and literal strings and treats the rest as unreadable, with the same deletion alias table |
| Claude Code | Parses PowerShell's AST and keeps Bash and PowerShell rules apart, suggesting the PowerShell tool be turned off when a Bash deny exists; with one command tool that turns off every command |
| ZCode | Matches each dialect separately |
| One rule, both shells | An owner who writes `bash(rm -rf *)` means "no recursive delete" |
| No grammar | Codex's accepted subset reads exactly without one, and `tree-sitter-language-pack` 1.x downloads grammars on first use, which a permission check must not do |
| Ask, not refuse | Prompts stay rare: only an owner who wrote such a deny rule sees the extra question |

## Implementation status

| Part | Status |
| --- | --- |
| Tested | Resolution, the shim folder's spelling with a stand-in `cygpath`, the 5.1 prelude, rule lowering and hook shells, with injected machines and environments. None has run on Windows, Cygwin included; the ConstrainedLanguage exit code test runs only there |
| Waiting on a build | Whether the dugite archive carries a bash is answered by the next Windows base build |
| Not built | The Web page does not show `hook_shell_fallback`: it is logged and sent as a WebSocket frame no client handles yet |
| Unmeasured | How often the model writes bash syntax into 5.1 or writes UTF-16 files; that baseline needs a Windows machine and real model calls. The byte order mark of `echo 你好 > a.txt` is checked only as prelude text |

[Windows support](https://fzkuji.github.io/OpenProgram/docs/reference/design/distribution/windows-support.html)
