Skip to content

feat: add ShellDispatcher for shell-agnostic command execution (rebased)#2008

Closed
aboimpinto wants to merge 20 commits into
Hmbown:mainfrom
aboimpinto:feat/shell-dispatcher-v2
Closed

feat: add ShellDispatcher for shell-agnostic command execution (rebased)#2008
aboimpinto wants to merge 20 commits into
Hmbown:mainfrom
aboimpinto:feat/shell-dispatcher-v2

Conversation

@aboimpinto
Copy link
Copy Markdown
Contributor

Rebased onto current \main\ per maintainer's request on closed PR #1781.

Two commits:

  • feat: add ShellDispatcher for shell-agnostic command execution
  • feat(shell_dispatcher): harden PowerShell detection and add execution logging

Introduces a central shell abstraction that replaces hardcoded
Command::new("cmd") / Command::new("sh") across the execution path.

- Shell detection at startup (pwsh -> powershell -> cmd -> sh)
- Correct quoting per shell (PowerShell uses -NoProfile -Command)
- Scope guards restore crossterm raw mode on all spawn paths (Hmbown#1690)
- Direct program+args builder for sandbox ExecEnv integration

Files:
- crates/tui/src/shell_dispatcher.rs (new, 12 tests)
- crates/tui/src/main.rs (register module)
- crates/tui/src/eval.rs (exec_shell delegates to dispatcher)
- crates/tui/src/sandbox/mod.rs (CommandSpec::shell uses dispatcher)
- crates/tui/src/tools/shell.rs (raw mode guards on all spawn paths)

Closes Hmbown#1690
Refs Hmbown#1779
… logging

- find_exe(): fall back to known install dirs when PATH lookup fails
  (C:\Program Files\PowerShell\7, System32\WindowsPowerShell\v1.0)
- Encoding prefix: use idiomatic [Console]::OutputEncoding for PowerShell
  instead of cmd.exe chcp 65001 >NUL
- Execution logging: write exec via <ShellKind> entries to
  SHELL_DISPATCHER_LOG when set
- System prompt: use ShellDispatcher detection instead of raw $SHELL
  so model knows it has PowerShell and generates native cmdlets
- display_command(): strip PowerShell encoding prefix for display
- Add tests for find_exe PATH + known-dir fallback

Refs Hmbown#1779
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a ShellDispatcher abstraction to centralize shell detection and command execution across the TUI, improving support for various shells like PowerShell and Bash while ensuring consistent UTF-8 handling on Windows. Key changes include refactoring CommandSpec and ShellManager to utilize the new dispatcher and implementing raw-mode guards to manage terminal state during command execution. Review feedback highlights the need to make raw-mode restoration conditional on its previous state to prevent side effects in non-interactive contexts and suggests preserving absolute shell paths from environment variables to ensure deterministic binary resolution.

Comment thread crates/tui/src/shell_dispatcher.rs
Comment thread crates/tui/src/shell_dispatcher.rs
Comment thread crates/tui/src/tools/shell.rs
Comment thread crates/tui/src/tools/shell.rs
aboimpinto added 17 commits May 24, 2026 19:56
- Fixed sandbox tests to accept sh/bash/zsh on non-Windows
- Added cfg_shell_program() helper that uses ShellDispatcher detection
- Marked find_exe_finds_cmd_on_path as #[cfg(windows)]
- Fixed shell::tests for issue_1691 to accept sh/bash/zsh
- Added #[cfg(windows)] to cmd-specific tests
ShellDispatcher may detect pwsh, powershell, or cmd on Windows.
Tests now verify the command is passed as the last arg instead of
hardcoding cmd.exe with chcp prefix.
Copy link
Copy Markdown
Contributor Author

Closing this PR because I folded the stacked work into #1848 to avoid presenting overlapping PRs as independently mergeable.

The ShellDispatcher changes from this PR are included in #1848.

Paulo Aboim Pinto

@aboimpinto aboimpinto closed this May 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant