Skip to content

feat(harness): let the local shell be configured instead of following the host OS - #4136

Open
ningmao-hlyz wants to merge 6 commits into
agentscope-ai:mainfrom
ningmao-hlyz:fix/windows-shell-discovery
Open

ningmao-hlyz wants to merge 6 commits into
agentscope-ai:mainfrom
ningmao-hlyz:fix/windows-shell-discovery

Conversation

@ningmao-hlyz

@ningmao-hlyz ningmao-hlyz commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

AgentScope-Java Version

2.1.0-SNAPSHOT, based on main at 3f6ff9e6 (#4261).

Description

Fixes #1396.

Expose LocalFilesystemSpec.shellPath(String) so applications can select an executable path or a shell name on PATH, with Git for Windows lookup for bare names. Defaults remain sh on POSIX and cmd.exe on Windows; an invalid configured shell fails when the filesystem is built.

Use the selected shell family for command flags, working_directory prefixes and workspace prompts, including through overlay and routed filesystems. Shell resolution also selects output decoding: PowerShell commands set console output to UTF-8; POSIX shells use UTF-8 and Windows cmd.exe retains native decoding. A custom environment keeps the small bootstrap set needed to find commands, and caller values take precedence.

Resolution accepts any executable that is not a directory, because a Store-installed pwsh is a runnable app-execution alias: Files.exists and Files.isRegularFile both report it absent while the JVM launches it. Names starting with pwsh select PowerShell, so preview builds get -Command and the UTF-8 console prefix. The workspace paragraph asks the detected local upper for its syntax, so a directly constructed OverlayFilesystem no longer reports POSIX for a non-POSIX upper.

This branch is updated for #4154: RoutedSandboxFilesystem retains the new journalStorage() API as well as shell delegation. Only docs/dev is updated. Other shell implementations such as ShellCommandTool and LocalHandsToolExecutor are outside this change.

Validation on Windows, Java 17:

  • Shell, shell-execute, local-shell, spec and workspace-prompt tests: 159 tests, 0 failures/errors, 6 skipped for unavailable shells/platform paths.
  • Resolution probe: the Store-installed pwsh alias fails the previous isRegularFile && isExecutable predicate and passes the new one; cmd.exe and Windows PowerShell 5.1 are unchanged.
  • Encoding probe: [Console]::OutputEncoding = UTF8; Set-Location -LiteralPath '...'; cmd /c echo <CJK> returns E4 B8 AD ... with exit 0. Without the prefix the same command returns GBK (D6 D0 CE C4), so the console prefix also covers native commands.
  • The last full Windows clean verify on this branch passed 13,003 tests. This revision changes only shell resolution and the workspace prompt, covered above.
  • Documentation: 20 tests and site validation passed. Broken-link output matches the base.
  • Spotless and git diff --check passed.

Checklist

  • Code has been formatted; Spotless checks pass.
  • Tests pass for the commands and scopes listed above.
  • Javadoc comments follow project conventions.
  • Related English and Chinese documentation has been updated.
  • Code is ready for review.

@mintlify

mintlify Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

Preview approval required

This pull request comes from a fork, so Mintlify won't build a preview until someone with write access approves commit d8f547a.

Approve preview

… the host OS

`execute()` picked the shell from `os.name`: `cmd.exe /c` on Windows, `sh -c` everywhere else. The
`cmd.exe` branch removed the reported crash, but a command written in POSIX syntax then fails
differently instead of working, and there was no way to switch (agentscope-ai#1396).

`LocalFilesystemSpec.shellPath(String)` takes a path or a bare executable name. A name is looked up
on `PATH`, and on Windows also in the Git for Windows install directories, so `shellPath("bash")` is
enough. The flag that passes the command follows the executable: `-c`, `/c`, or `-Command`. The
default stays the shell the host provides, because switching it would run every existing cmd command,
and this project's own Windows tests, under a POSIX shell.

`execute()` is not the only place that assumed the host shell: `ShellExecuteTool` prefixed
`working_directory` with `cd /d "..." &&` and the workspace prompt told the model `cmd.exe /c`, both
from `os.name`. Both now ask the filesystem through `ShellSyntax`, which wrappers report from the
backend that runs the command.

The same change fixes a defect a configured shell would not have survived. With a non-empty `env`
and `inheritEnv(false)`, `execute()` cleared the environment and kept only the caller's variables,
which dropped `PATH`:

    ProcessBuilder("cmd.exe", "/c", "where git") with a cleared environment
      -> 'where' is not recognized as an internal or external command

A caller that only wanted to add one variable lost the whole toolchain. The variables needed to start
a process and resolve its own executables are restored before `env` is applied; `env` still overrides
them, and home/profile directories stay dropped, which is what `inheritEnv(false)` asks for.

Fixes agentscope-ai#1396.
@ningmao-hlyz
ningmao-hlyz force-pushed the fix/windows-shell-discovery branch from 43472b1 to 33da426 Compare November 15, 2026 06:24
@ningmao-hlyz
ningmao-hlyz force-pushed the fix/windows-shell-discovery branch from 33da426 to a87045f Compare November 15, 2026 06:53
@jujn jujn added the P2 重要不紧急 label Nov 16, 2026
@ningmao-hlyz

Copy link
Copy Markdown
Contributor Author

@jujn Could you review this PR when you have time?

Resolutions:

- LocalFilesystemWithShell keeps main's StreamCapture/BoundedOutput pipeline
  (spooled output, digests, saveOutput, previewOutput) and only swaps the
  decode charset for resolvedShell.outputCharset(...), which is what a
  configured PowerShell needs. The host-level outputCharset(String) helper is
  kept and now delegates to ShellResolver, so main's fixture assumption still
  reads the same value.
- Constructor javadocs: the short note stays on the nine-argument overload; the
  long javadoc (already carrying @PARAM shellPath) stays on the ten-argument
  one.
- Tests: union imports, and drop the duplicate isWindows() helper that both
  sides added.

mvn -pl agentscope-harness -am test for the shell, overlay, routed and
workspace-context classes passes (189 tests, 7 skipped for unavailable
shells). spotless:check clean.
Three points in the configured-shell path:

- The workspace paragraph asked ShellSyntax.of(fs) for the family. A bare
  OverlayFilesystem does not implement AbstractSandboxFilesystem, so a caller
  that constructs one directly around a local shell answers POSIX even when that
  shell is cmd.exe. OverlayFilesystem.of() already returns a ShellAwareOverlay
  that answers correctly, so this only hardens the direct-construction path:
  ask the detected local upper, which is the layer that executes.

- A Store-installed pwsh is an app-execution alias: Files.isRegularFile and
  Files.exists both report it absent although the JVM launches it fine.
  Requiring a regular file rejected shellPath("pwsh") on a working shell.
  Accept any executable that is not a directory instead.

- pwsh-preview.exe fell through to POSIX, so it launched with -c and without the
  UTF-8 console prefix. Match every name starting with pwsh.

Verified on Windows 11 with pwsh from the Microsoft Store: the alias resolves,
and "[Console]::OutputEncoding = UTF8; Set-Location -LiteralPath ...; cmd /c
echo <CJK>" returns UTF-8 bytes (E4 B8 AD ...) with exit 0. cmd.exe and Windows
PowerShell 5.1 keep their existing behaviour.

mvn -pl agentscope-harness -am test for the shell, middleware and spec classes
passes (159 tests, 0 failures, 6 skipped for unavailable shells).

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P2 重要不紧急

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]:Command Execution Fails on Windows with "Cannot run program 'sh'" Error

2 participants